blob: 512f91ed5d2512b89bab2ce492fd6cd8f50b8b78 [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 Smart92c13f22013-05-31 17:05:45 -04004 * Copyright (C) 2004-2013 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))
309 sprintf(phba->OptionROMVersion, "%d.%d%d",
310 prg->ver, prg->rev, prg->lev);
311 else
312 sprintf(phba->OptionROMVersion, "%d.%d%d%c%d",
313 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,
571 jiffies + msecs_to_jiffies(1000 * LPFC_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 **/
652int
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) &&
702 !(phba->lmt & LMT_16Gb))) {
703 /* Reset link speed to auto */
704 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
705 "1302 Invalid speed for this board:%d "
706 "Reset link speed to auto.\n",
707 phba->cfg_link_speed);
708 phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
709 }
James Smart1b511972011-12-13 13:23:09 -0500710 lpfc_init_link(phba, pmb, fc_topology, phba->cfg_link_speed);
James Smart84d1b002010-02-12 14:42:33 -0500711 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart1b511972011-12-13 13:23:09 -0500712 if (phba->sli_rev < LPFC_SLI_REV4)
713 lpfc_set_loopback_flag(phba);
James Smart6e7288d2010-06-07 15:23:35 -0400714 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart76a95d72010-11-20 23:11:48 -0500715 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
James Smart84d1b002010-02-12 14:42:33 -0500716 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
717 "0498 Adapter failed to init, mbxCmd x%x "
718 "INIT_LINK, mbxStatus x%x\n",
719 mb->mbxCommand, mb->mbxStatus);
James Smart76a95d72010-11-20 23:11:48 -0500720 if (phba->sli_rev <= LPFC_SLI_REV3) {
721 /* Clear all interrupt enable conditions */
722 writel(0, phba->HCregaddr);
723 readl(phba->HCregaddr); /* flush */
724 /* Clear all pending interrupts */
725 writel(0xffffffff, phba->HAregaddr);
726 readl(phba->HAregaddr); /* flush */
727 }
James Smart84d1b002010-02-12 14:42:33 -0500728 phba->link_state = LPFC_HBA_ERROR;
James Smart6e7288d2010-06-07 15:23:35 -0400729 if (rc != MBX_BUSY || flag == MBX_POLL)
James Smart84d1b002010-02-12 14:42:33 -0500730 mempool_free(pmb, phba->mbox_mem_pool);
731 return -EIO;
732 }
James Smarte40a02c2010-02-26 14:13:54 -0500733 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
James Smart6e7288d2010-06-07 15:23:35 -0400734 if (flag == MBX_POLL)
735 mempool_free(pmb, phba->mbox_mem_pool);
James Smart84d1b002010-02-12 14:42:33 -0500736
737 return 0;
738}
739
740/**
741 * lpfc_hba_down_link - this routine downs the FC link
James Smart6e7288d2010-06-07 15:23:35 -0400742 * @phba: pointer to lpfc hba data structure.
743 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500744 *
745 * This routine will issue the DOWN_LINK mailbox command call.
746 * It is available to other drivers through the lpfc_hba data
747 * structure for use to stop the link.
748 *
749 * Return code
750 * 0 - success
751 * Any other value - error
752 **/
753int
James Smart6e7288d2010-06-07 15:23:35 -0400754lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500755{
756 LPFC_MBOXQ_t *pmb;
757 int rc;
758
759 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
760 if (!pmb) {
761 phba->link_state = LPFC_HBA_ERROR;
762 return -ENOMEM;
763 }
764
765 lpfc_printf_log(phba,
766 KERN_ERR, LOG_INIT,
767 "0491 Adapter Link is disabled.\n");
768 lpfc_down_link(phba, pmb);
769 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6e7288d2010-06-07 15:23:35 -0400770 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart84d1b002010-02-12 14:42:33 -0500771 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
772 lpfc_printf_log(phba,
773 KERN_ERR, LOG_INIT,
774 "2522 Adapter failed to issue DOWN_LINK"
775 " mbox command rc 0x%x\n", rc);
776
777 mempool_free(pmb, phba->mbox_mem_pool);
778 return -EIO;
779 }
James Smart6e7288d2010-06-07 15:23:35 -0400780 if (flag == MBX_POLL)
781 mempool_free(pmb, phba->mbox_mem_pool);
782
James Smart84d1b002010-02-12 14:42:33 -0500783 return 0;
784}
785
786/**
James Smart3621a712009-04-06 18:47:14 -0400787 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400788 * @phba: pointer to lpfc HBA data structure.
789 *
790 * This routine will do LPFC uninitialization before the HBA is reset when
791 * bringing down the SLI Layer.
792 *
793 * Return codes
794 * 0 - success.
795 * Any other value - error.
796 **/
dea31012005-04-17 16:05:31 -0500797int
James Smart2e0fef82007-06-17 19:56:36 -0500798lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500799{
James Smart1b32f6a2008-02-08 18:49:39 -0500800 struct lpfc_vport **vports;
801 int i;
James Smart3772a992009-05-22 14:50:54 -0400802
803 if (phba->sli_rev <= LPFC_SLI_REV3) {
804 /* Disable interrupts */
805 writel(0, phba->HCregaddr);
806 readl(phba->HCregaddr); /* flush */
807 }
dea31012005-04-17 16:05:31 -0500808
James Smart1b32f6a2008-02-08 18:49:39 -0500809 if (phba->pport->load_flag & FC_UNLOADING)
810 lpfc_cleanup_discovery_resources(phba->pport);
811 else {
812 vports = lpfc_create_vport_work_array(phba);
813 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400814 for (i = 0; i <= phba->max_vports &&
815 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500816 lpfc_cleanup_discovery_resources(vports[i]);
817 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500818 }
819 return 0;
dea31012005-04-17 16:05:31 -0500820}
821
James Smarte59058c2008-08-24 21:49:00 -0400822/**
James Smartbcece5f2014-04-04 13:51:34 -0400823 * lpfc_hba_free_post_buf - Perform lpfc uninitialization after HBA reset
824 * @phba: pointer to lpfc HBA data structure.
825 *
826 * This routine will cleanup posted ELS buffers after the HBA is reset
827 * when bringing down the SLI Layer.
828 *
829 *
830 * Return codes
831 * void.
832 **/
833static void
834lpfc_hba_free_post_buf(struct lpfc_hba *phba)
835{
836 struct lpfc_sli *psli = &phba->sli;
837 struct lpfc_sli_ring *pring;
838 struct lpfc_dmabuf *mp, *next_mp;
James Smart07eab622014-04-04 13:51:44 -0400839 LIST_HEAD(buflist);
840 int count;
James Smartbcece5f2014-04-04 13:51:34 -0400841
842 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
843 lpfc_sli_hbqbuf_free_all(phba);
844 else {
845 /* Cleanup preposted buffers on the ELS ring */
James Smartbcece5f2014-04-04 13:51:34 -0400846 pring = &psli->ring[LPFC_ELS_RING];
James Smart07eab622014-04-04 13:51:44 -0400847 spin_lock_irq(&phba->hbalock);
848 list_splice_init(&pring->postbufq, &buflist);
849 spin_unlock_irq(&phba->hbalock);
850
851 count = 0;
852 list_for_each_entry_safe(mp, next_mp, &buflist, list) {
James Smartbcece5f2014-04-04 13:51:34 -0400853 list_del(&mp->list);
James Smart07eab622014-04-04 13:51:44 -0400854 count++;
James Smartbcece5f2014-04-04 13:51:34 -0400855 lpfc_mbuf_free(phba, mp->virt, mp->phys);
856 kfree(mp);
857 }
James Smart07eab622014-04-04 13:51:44 -0400858
859 spin_lock_irq(&phba->hbalock);
860 pring->postbufq_cnt -= count;
James Smartbcece5f2014-04-04 13:51:34 -0400861 spin_unlock_irq(&phba->hbalock);
862 }
863}
864
865/**
866 * lpfc_hba_clean_txcmplq - Perform lpfc uninitialization after HBA reset
867 * @phba: pointer to lpfc HBA data structure.
868 *
869 * This routine will cleanup the txcmplq after the HBA is reset when bringing
870 * down the SLI Layer.
871 *
872 * Return codes
873 * void
874 **/
875static void
876lpfc_hba_clean_txcmplq(struct lpfc_hba *phba)
877{
878 struct lpfc_sli *psli = &phba->sli;
879 struct lpfc_sli_ring *pring;
880 LIST_HEAD(completions);
881 int i;
882
883
884
885 for (i = 0; i < psli->num_rings; i++) {
886 pring = &psli->ring[i];
887 if (phba->sli_rev >= LPFC_SLI_REV4)
888 spin_lock_irq(&pring->ring_lock);
889 else
890 spin_lock_irq(&phba->hbalock);
891 /* At this point in time the HBA is either reset or DOA. Either
892 * way, nothing should be on txcmplq as it will NEVER complete.
893 */
894 list_splice_init(&pring->txcmplq, &completions);
895
896 if (phba->sli_rev >= LPFC_SLI_REV4)
897 spin_unlock_irq(&pring->ring_lock);
898 else
899 spin_unlock_irq(&phba->hbalock);
900
901 /* Cancel all the IOCBs from the completions list */
902 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
903 IOERR_SLI_ABORTED);
904 lpfc_sli_abort_iocb_ring(phba, pring);
905 }
906}
907
908/**
James Smart3772a992009-05-22 14:50:54 -0400909 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smartbcece5f2014-04-04 13:51:34 -0400910 int i;
James Smarte59058c2008-08-24 21:49:00 -0400911 * @phba: pointer to lpfc HBA data structure.
912 *
913 * This routine will do uninitialization after the HBA is reset when bring
914 * down the SLI Layer.
915 *
916 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200917 * 0 - success.
James Smarte59058c2008-08-24 21:49:00 -0400918 * Any other value - error.
919 **/
James Smart3772a992009-05-22 14:50:54 -0400920static int
921lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -0500922{
James Smartbcece5f2014-04-04 13:51:34 -0400923 lpfc_hba_free_post_buf(phba);
924 lpfc_hba_clean_txcmplq(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500925 return 0;
926}
James Smart5af5eee2010-10-22 11:06:38 -0400927
James Smartda0436e2009-05-22 14:51:39 -0400928/**
929 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
930 * @phba: pointer to lpfc HBA data structure.
931 *
932 * This routine will do uninitialization after the HBA is reset when bring
933 * down the SLI Layer.
934 *
935 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200936 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400937 * Any other value - error.
938 **/
939static int
940lpfc_hba_down_post_s4(struct lpfc_hba *phba)
941{
942 struct lpfc_scsi_buf *psb, *psb_next;
943 LIST_HEAD(aborts);
James Smartda0436e2009-05-22 14:51:39 -0400944 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500945 struct lpfc_sglq *sglq_entry = NULL;
946
James Smartbcece5f2014-04-04 13:51:34 -0400947 lpfc_hba_free_post_buf(phba);
948 lpfc_hba_clean_txcmplq(phba);
949
James Smartda0436e2009-05-22 14:51:39 -0400950 /* At this point in time the HBA is either reset or DOA. Either
951 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
952 * on the lpfc_sgl_list so that it can either be freed if the
953 * driver is unloading or reposted if the driver is restarting
954 * the port.
955 */
956 spin_lock_irq(&phba->hbalock); /* required for lpfc_sgl_list and */
957 /* scsl_buf_list */
958 /* abts_sgl_list_lock required because worker thread uses this
959 * list.
960 */
961 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -0500962 list_for_each_entry(sglq_entry,
963 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
964 sglq_entry->state = SGL_FREED;
965
James Smartda0436e2009-05-22 14:51:39 -0400966 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
967 &phba->sli4_hba.lpfc_sgl_list);
968 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
969 /* abts_scsi_buf_list_lock required because worker thread uses this
970 * list.
971 */
972 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
973 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
974 &aborts);
975 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
976 spin_unlock_irq(&phba->hbalock);
977
978 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
979 psb->pCmd = NULL;
980 psb->status = IOSTAT_SUCCESS;
981 }
James Smarta40fc5f2013-04-17 20:17:40 -0400982 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
983 list_splice(&aborts, &phba->lpfc_scsi_buf_list_put);
984 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400985 return 0;
986}
987
988/**
989 * lpfc_hba_down_post - Wrapper func for hba down post routine
990 * @phba: pointer to lpfc HBA data structure.
991 *
992 * This routine wraps the actual SLI3 or SLI4 routine for performing
993 * uninitialization after the HBA is reset when bring down the SLI Layer.
994 *
995 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200996 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400997 * Any other value - error.
998 **/
999int
1000lpfc_hba_down_post(struct lpfc_hba *phba)
1001{
1002 return (*phba->lpfc_hba_down_post)(phba);
1003}
Jamie Wellnitz41415862006-02-28 19:25:27 -05001004
James Smarte59058c2008-08-24 21:49:00 -04001005/**
James Smart3621a712009-04-06 18:47:14 -04001006 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001007 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1008 *
1009 * This is the HBA-timer timeout handler registered to the lpfc driver. When
1010 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
1011 * work-port-events bitmap and the worker thread is notified. This timeout
1012 * event will be used by the worker thread to invoke the actual timeout
1013 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
1014 * be performed in the timeout handler and the HBA timeout event bit shall
1015 * be cleared by the worker thread after it has taken the event bitmap out.
1016 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001017static void
James Smart858c9f62007-06-17 19:56:39 -05001018lpfc_hb_timeout(unsigned long ptr)
1019{
1020 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -04001021 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -05001022 unsigned long iflag;
1023
1024 phba = (struct lpfc_hba *)ptr;
James Smart93996272008-08-24 21:50:30 -04001025
1026 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -05001027 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04001028 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
1029 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -05001030 phba->pport->work_port_events |= WORKER_HB_TMO;
1031 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
1032
James Smart93996272008-08-24 21:50:30 -04001033 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -04001034 if (!tmo_posted)
1035 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -05001036 return;
1037}
1038
James Smarte59058c2008-08-24 21:49:00 -04001039/**
James Smart19ca7602010-11-20 23:11:55 -05001040 * lpfc_rrq_timeout - The RRQ-timer timeout handler
1041 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1042 *
1043 * This is the RRQ-timer timeout handler registered to the lpfc driver. When
1044 * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
1045 * work-port-events bitmap and the worker thread is notified. This timeout
1046 * event will be used by the worker thread to invoke the actual timeout
1047 * handler routine, lpfc_rrq_handler. Any periodical operations will
1048 * be performed in the timeout handler and the RRQ timeout event bit shall
1049 * be cleared by the worker thread after it has taken the event bitmap out.
1050 **/
1051static void
1052lpfc_rrq_timeout(unsigned long ptr)
1053{
1054 struct lpfc_hba *phba;
James Smart19ca7602010-11-20 23:11:55 -05001055 unsigned long iflag;
1056
1057 phba = (struct lpfc_hba *)ptr;
1058 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001059 if (!(phba->pport->load_flag & FC_UNLOADING))
1060 phba->hba_flag |= HBA_RRQ_ACTIVE;
1061 else
1062 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart19ca7602010-11-20 23:11:55 -05001063 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001064
1065 if (!(phba->pport->load_flag & FC_UNLOADING))
1066 lpfc_worker_wake_up(phba);
James Smart19ca7602010-11-20 23:11:55 -05001067}
1068
1069/**
James Smart3621a712009-04-06 18:47:14 -04001070 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -04001071 * @phba: pointer to lpfc hba data structure.
1072 * @pmboxq: pointer to the driver internal queue element for mailbox command.
1073 *
1074 * This is the callback function to the lpfc heart-beat mailbox command.
1075 * If configured, the lpfc driver issues the heart-beat mailbox command to
1076 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
1077 * heart-beat mailbox command is issued, the driver shall set up heart-beat
1078 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
1079 * heart-beat outstanding state. Once the mailbox command comes back and
1080 * no error conditions detected, the heart-beat mailbox command timer is
1081 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1082 * state is cleared for the next heart-beat. If the timer expired with the
1083 * heart-beat outstanding state set, the driver will put the HBA offline.
1084 **/
James Smart858c9f62007-06-17 19:56:39 -05001085static void
1086lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1087{
1088 unsigned long drvr_flag;
1089
1090 spin_lock_irqsave(&phba->hbalock, drvr_flag);
1091 phba->hb_outstanding = 0;
1092 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1093
James Smart93996272008-08-24 21:50:30 -04001094 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -05001095 mempool_free(pmboxq, phba->mbox_mem_pool);
1096 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1097 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -04001098 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -05001099 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001100 jiffies +
1101 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001102 return;
1103}
1104
James Smarte59058c2008-08-24 21:49:00 -04001105/**
James Smart3621a712009-04-06 18:47:14 -04001106 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001107 * @phba: pointer to lpfc hba data structure.
1108 *
1109 * This is the actual HBA-timer timeout handler to be invoked by the worker
1110 * thread whenever the HBA timer fired and HBA-timeout event posted. This
1111 * handler performs any periodic operations needed for the device. If such
1112 * periodic event has already been attended to either in the interrupt handler
1113 * or by processing slow-ring or fast-ring events within the HBA-timer
1114 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1115 * the timer for the next timeout period. If lpfc heart-beat mailbox command
1116 * is configured and there is no heart-beat mailbox command outstanding, a
1117 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1118 * has been a heart-beat mailbox command outstanding, the HBA shall be put
1119 * to offline.
1120 **/
James Smart858c9f62007-06-17 19:56:39 -05001121void
1122lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1123{
James Smart45ed1192009-10-02 15:17:02 -04001124 struct lpfc_vport **vports;
James Smart858c9f62007-06-17 19:56:39 -05001125 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -05001126 struct lpfc_dmabuf *buf_ptr;
James Smart45ed1192009-10-02 15:17:02 -04001127 int retval, i;
James Smart858c9f62007-06-17 19:56:39 -05001128 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -05001129 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -05001130
James Smart45ed1192009-10-02 15:17:02 -04001131 vports = lpfc_create_vport_work_array(phba);
1132 if (vports != NULL)
1133 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
1134 lpfc_rcv_seq_check_edtov(vports[i]);
1135 lpfc_destroy_vport_work_array(phba, vports);
1136
James Smart858c9f62007-06-17 19:56:39 -05001137 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -04001138 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -05001139 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1140 return;
1141
1142 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -05001143
James Smart256ec0d2013-04-17 20:14:58 -04001144 if (time_after(phba->last_completion_time +
1145 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL),
1146 jiffies)) {
James Smart858c9f62007-06-17 19:56:39 -05001147 spin_unlock_irq(&phba->pport->work_port_lock);
1148 if (!phba->hb_outstanding)
1149 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001150 jiffies +
1151 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001152 else
1153 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001154 jiffies +
1155 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001156 return;
1157 }
1158 spin_unlock_irq(&phba->pport->work_port_lock);
1159
James Smart0ff10d42008-01-11 01:52:36 -05001160 if (phba->elsbuf_cnt &&
1161 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1162 spin_lock_irq(&phba->hbalock);
1163 list_splice_init(&phba->elsbuf, &completions);
1164 phba->elsbuf_cnt = 0;
1165 phba->elsbuf_prev_cnt = 0;
1166 spin_unlock_irq(&phba->hbalock);
1167
1168 while (!list_empty(&completions)) {
1169 list_remove_head(&completions, buf_ptr,
1170 struct lpfc_dmabuf, list);
1171 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1172 kfree(buf_ptr);
1173 }
1174 }
1175 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1176
James Smart858c9f62007-06-17 19:56:39 -05001177 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -05001178 if (phba->cfg_enable_hba_heartbeat) {
1179 if (!phba->hb_outstanding) {
James Smartbc739052010-08-04 16:11:18 -04001180 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1181 (list_empty(&psli->mboxq))) {
1182 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1183 GFP_KERNEL);
1184 if (!pmboxq) {
1185 mod_timer(&phba->hb_tmofunc,
1186 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001187 msecs_to_jiffies(1000 *
1188 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001189 return;
1190 }
James Smart13815c82008-01-11 01:52:48 -05001191
James Smartbc739052010-08-04 16:11:18 -04001192 lpfc_heart_beat(phba, pmboxq);
1193 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1194 pmboxq->vport = phba->pport;
1195 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1196 MBX_NOWAIT);
James Smart13815c82008-01-11 01:52:48 -05001197
James Smartbc739052010-08-04 16:11:18 -04001198 if (retval != MBX_BUSY &&
1199 retval != MBX_SUCCESS) {
1200 mempool_free(pmboxq,
1201 phba->mbox_mem_pool);
1202 mod_timer(&phba->hb_tmofunc,
1203 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001204 msecs_to_jiffies(1000 *
1205 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001206 return;
1207 }
1208 phba->skipped_hb = 0;
1209 phba->hb_outstanding = 1;
1210 } else if (time_before_eq(phba->last_completion_time,
1211 phba->skipped_hb)) {
1212 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1213 "2857 Last completion time not "
1214 " updated in %d ms\n",
1215 jiffies_to_msecs(jiffies
1216 - phba->last_completion_time));
1217 } else
1218 phba->skipped_hb = jiffies;
1219
James Smart858c9f62007-06-17 19:56:39 -05001220 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001221 jiffies +
1222 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001223 return;
James Smart13815c82008-01-11 01:52:48 -05001224 } else {
1225 /*
1226 * If heart beat timeout called with hb_outstanding set
James Smartdcf2a4e2010-09-29 11:18:53 -04001227 * we need to give the hb mailbox cmd a chance to
1228 * complete or TMO.
James Smart13815c82008-01-11 01:52:48 -05001229 */
James Smartdcf2a4e2010-09-29 11:18:53 -04001230 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1231 "0459 Adapter heartbeat still out"
1232 "standing:last compl time was %d ms.\n",
1233 jiffies_to_msecs(jiffies
1234 - phba->last_completion_time));
1235 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001236 jiffies +
1237 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001238 }
James Smart858c9f62007-06-17 19:56:39 -05001239 }
1240}
1241
James Smarte59058c2008-08-24 21:49:00 -04001242/**
James Smart3621a712009-04-06 18:47:14 -04001243 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -04001244 * @phba: pointer to lpfc hba data structure.
1245 *
1246 * This routine is called to bring the HBA offline when HBA hardware error
1247 * other than Port Error 6 has been detected.
1248 **/
James Smart09372822008-01-11 01:52:54 -05001249static void
1250lpfc_offline_eratt(struct lpfc_hba *phba)
1251{
1252 struct lpfc_sli *psli = &phba->sli;
1253
1254 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001255 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -05001256 spin_unlock_irq(&phba->hbalock);
James Smart618a5232012-06-12 13:54:36 -04001257 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart09372822008-01-11 01:52:54 -05001258
1259 lpfc_offline(phba);
1260 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001261 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001262 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001263 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001264 lpfc_hba_down_post(phba);
1265 lpfc_sli_brdready(phba, HS_MBRDY);
1266 lpfc_unblock_mgmt_io(phba);
1267 phba->link_state = LPFC_HBA_ERROR;
1268 return;
1269}
1270
James Smarte59058c2008-08-24 21:49:00 -04001271/**
James Smartda0436e2009-05-22 14:51:39 -04001272 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1273 * @phba: pointer to lpfc hba data structure.
1274 *
1275 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1276 * other than Port Error 6 has been detected.
1277 **/
James Smarta88dbb62013-04-17 20:18:39 -04001278void
James Smartda0436e2009-05-22 14:51:39 -04001279lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1280{
James Smart618a5232012-06-12 13:54:36 -04001281 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smartda0436e2009-05-22 14:51:39 -04001282 lpfc_offline(phba);
1283 lpfc_sli4_brdreset(phba);
1284 lpfc_hba_down_post(phba);
1285 lpfc_sli4_post_status_check(phba);
1286 lpfc_unblock_mgmt_io(phba);
1287 phba->link_state = LPFC_HBA_ERROR;
1288}
1289
1290/**
James Smarta257bf92009-04-06 18:48:10 -04001291 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1292 * @phba: pointer to lpfc hba data structure.
1293 *
1294 * This routine is invoked to handle the deferred HBA hardware error
1295 * conditions. This type of error is indicated by HBA by setting ER1
1296 * and another ER bit in the host status register. The driver will
1297 * wait until the ER1 bit clears before handling the error condition.
1298 **/
1299static void
1300lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1301{
1302 uint32_t old_host_status = phba->work_hs;
1303 struct lpfc_sli_ring *pring;
1304 struct lpfc_sli *psli = &phba->sli;
1305
James Smartf4b4c682009-05-22 14:53:12 -04001306 /* If the pci channel is offline, ignore possible errors,
1307 * since we cannot communicate with the pci card anyway.
1308 */
1309 if (pci_channel_offline(phba->pcidev)) {
1310 spin_lock_irq(&phba->hbalock);
1311 phba->hba_flag &= ~DEFER_ERATT;
1312 spin_unlock_irq(&phba->hbalock);
1313 return;
1314 }
1315
James Smarta257bf92009-04-06 18:48:10 -04001316 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1317 "0479 Deferred Adapter Hardware Error "
1318 "Data: x%x x%x x%x\n",
1319 phba->work_hs,
1320 phba->work_status[0], phba->work_status[1]);
1321
1322 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001323 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001324 spin_unlock_irq(&phba->hbalock);
1325
1326
1327 /*
1328 * Firmware stops when it triggred erratt. That could cause the I/Os
1329 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1330 * SCSI layer retry it after re-establishing link.
1331 */
1332 pring = &psli->ring[psli->fcp_ring];
1333 lpfc_sli_abort_iocb_ring(phba, pring);
1334
1335 /*
1336 * There was a firmware error. Take the hba offline and then
1337 * attempt to restart it.
1338 */
James Smart618a5232012-06-12 13:54:36 -04001339 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smarta257bf92009-04-06 18:48:10 -04001340 lpfc_offline(phba);
1341
1342 /* Wait for the ER1 bit to clear.*/
1343 while (phba->work_hs & HS_FFER1) {
1344 msleep(100);
James Smart9940b972011-03-11 16:06:12 -05001345 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1346 phba->work_hs = UNPLUG_ERR ;
1347 break;
1348 }
James Smarta257bf92009-04-06 18:48:10 -04001349 /* If driver is unloading let the worker thread continue */
1350 if (phba->pport->load_flag & FC_UNLOADING) {
1351 phba->work_hs = 0;
1352 break;
1353 }
1354 }
1355
1356 /*
1357 * This is to ptrotect against a race condition in which
1358 * first write to the host attention register clear the
1359 * host status register.
1360 */
1361 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1362 phba->work_hs = old_host_status & ~HS_FFER1;
1363
James Smart3772a992009-05-22 14:50:54 -04001364 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001365 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001366 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001367 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1368 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1369}
1370
James Smart3772a992009-05-22 14:50:54 -04001371static void
1372lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1373{
1374 struct lpfc_board_event_header board_event;
1375 struct Scsi_Host *shost;
1376
1377 board_event.event_type = FC_REG_BOARD_EVENT;
1378 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1379 shost = lpfc_shost_from_vport(phba->pport);
1380 fc_host_post_vendor_event(shost, fc_get_event_number(),
1381 sizeof(board_event),
1382 (char *) &board_event,
1383 LPFC_NL_VENDOR_ID);
1384}
1385
James Smarta257bf92009-04-06 18:48:10 -04001386/**
James Smart3772a992009-05-22 14:50:54 -04001387 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001388 * @phba: pointer to lpfc hba data structure.
1389 *
1390 * This routine is invoked to handle the following HBA hardware error
1391 * conditions:
1392 * 1 - HBA error attention interrupt
1393 * 2 - DMA ring index out of range
1394 * 3 - Mailbox command came back as unknown
1395 **/
James Smart3772a992009-05-22 14:50:54 -04001396static void
1397lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001398{
James Smart2e0fef82007-06-17 19:56:36 -05001399 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001400 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001401 struct lpfc_sli_ring *pring;
James Smartd2873e42006-08-18 17:46:43 -04001402 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001403 unsigned long temperature;
1404 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001405 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001406
Linas Vepstas8d63f372007-02-14 14:28:36 -06001407 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001408 * since we cannot communicate with the pci card anyway.
1409 */
1410 if (pci_channel_offline(phba->pcidev)) {
1411 spin_lock_irq(&phba->hbalock);
1412 phba->hba_flag &= ~DEFER_ERATT;
1413 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001414 return;
James Smart3772a992009-05-22 14:50:54 -04001415 }
1416
James Smart13815c82008-01-11 01:52:48 -05001417 /* If resets are disabled then leave the HBA alone and return */
1418 if (!phba->cfg_enable_hba_reset)
1419 return;
dea31012005-04-17 16:05:31 -05001420
James Smartea2151b2008-09-07 11:52:10 -04001421 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001422 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001423
James Smarta257bf92009-04-06 18:48:10 -04001424 if (phba->hba_flag & DEFER_ERATT)
1425 lpfc_handle_deferred_eratt(phba);
1426
James Smartdcf2a4e2010-09-29 11:18:53 -04001427 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1428 if (phba->work_hs & HS_FFER6)
1429 /* Re-establishing Link */
1430 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1431 "1301 Re-establishing Link "
1432 "Data: x%x x%x x%x\n",
1433 phba->work_hs, phba->work_status[0],
1434 phba->work_status[1]);
1435 if (phba->work_hs & HS_FFER8)
1436 /* Device Zeroization */
1437 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1438 "2861 Host Authentication device "
1439 "zeroization Data:x%x x%x x%x\n",
1440 phba->work_hs, phba->work_status[0],
1441 phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001442
James Smart92d7f7b2007-06-17 19:56:38 -05001443 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001444 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001445 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001446
1447 /*
1448 * Firmware stops when it triggled erratt with HS_FFER6.
1449 * That could cause the I/Os dropped by the firmware.
1450 * Error iocb (I/O) on txcmplq and let the SCSI layer
1451 * retry it after re-establishing link.
1452 */
1453 pring = &psli->ring[psli->fcp_ring];
1454 lpfc_sli_abort_iocb_ring(phba, pring);
1455
dea31012005-04-17 16:05:31 -05001456 /*
1457 * There was a firmware error. Take the hba offline and then
1458 * attempt to restart it.
1459 */
James Smart618a5232012-06-12 13:54:36 -04001460 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
dea31012005-04-17 16:05:31 -05001461 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001462 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001463 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001464 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001465 return;
1466 }
James Smart46fa3112007-04-25 09:51:45 -04001467 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001468 } else if (phba->work_hs & HS_CRIT_TEMP) {
1469 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1470 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1471 temp_event_data.event_code = LPFC_CRIT_TEMP;
1472 temp_event_data.data = (uint32_t)temperature;
1473
1474 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001475 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001476 "(%ld), taking this port offline "
1477 "Data: x%x x%x x%x\n",
1478 temperature, phba->work_hs,
1479 phba->work_status[0], phba->work_status[1]);
1480
1481 shost = lpfc_shost_from_vport(phba->pport);
1482 fc_host_post_vendor_event(shost, fc_get_event_number(),
1483 sizeof(temp_event_data),
1484 (char *) &temp_event_data,
1485 SCSI_NL_VID_TYPE_PCI
1486 | PCI_VENDOR_ID_EMULEX);
1487
James Smart7af67052007-10-27 13:38:11 -04001488 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001489 phba->over_temp_state = HBA_OVER_TEMP;
1490 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001491 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001492
dea31012005-04-17 16:05:31 -05001493 } else {
1494 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001495 * failure is a value other than FFER6. Do not call the offline
1496 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001497 */
1498 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001499 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001500 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001501 phba->work_hs,
dea31012005-04-17 16:05:31 -05001502 phba->work_status[0], phba->work_status[1]);
1503
James Smartd2873e42006-08-18 17:46:43 -04001504 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001505 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001506 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001507 sizeof(event_data), (char *) &event_data,
1508 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1509
James Smart09372822008-01-11 01:52:54 -05001510 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001511 }
James Smart93996272008-08-24 21:50:30 -04001512 return;
dea31012005-04-17 16:05:31 -05001513}
1514
James Smarte59058c2008-08-24 21:49:00 -04001515/**
James Smart618a5232012-06-12 13:54:36 -04001516 * lpfc_sli4_port_sta_fn_reset - The SLI4 function reset due to port status reg
1517 * @phba: pointer to lpfc hba data structure.
1518 * @mbx_action: flag for mailbox shutdown action.
1519 *
1520 * This routine is invoked to perform an SLI4 port PCI function reset in
1521 * response to port status register polling attention. It waits for port
1522 * status register (ERR, RDY, RN) bits before proceeding with function reset.
1523 * During this process, interrupt vectors are freed and later requested
1524 * for handling possible port resource change.
1525 **/
1526static int
James Smarte10b2022014-02-20 09:57:43 -05001527lpfc_sli4_port_sta_fn_reset(struct lpfc_hba *phba, int mbx_action,
1528 bool en_rn_msg)
James Smart618a5232012-06-12 13:54:36 -04001529{
1530 int rc;
1531 uint32_t intr_mode;
1532
1533 /*
1534 * On error status condition, driver need to wait for port
1535 * ready before performing reset.
1536 */
1537 rc = lpfc_sli4_pdev_status_reg_wait(phba);
1538 if (!rc) {
1539 /* need reset: attempt for port recovery */
James Smarte10b2022014-02-20 09:57:43 -05001540 if (en_rn_msg)
1541 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1542 "2887 Reset Needed: Attempting Port "
1543 "Recovery...\n");
James Smart618a5232012-06-12 13:54:36 -04001544 lpfc_offline_prep(phba, mbx_action);
1545 lpfc_offline(phba);
1546 /* release interrupt for possible resource change */
1547 lpfc_sli4_disable_intr(phba);
1548 lpfc_sli_brdrestart(phba);
1549 /* request and enable interrupt */
1550 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
1551 if (intr_mode == LPFC_INTR_ERROR) {
1552 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1553 "3175 Failed to enable interrupt\n");
1554 return -EIO;
1555 } else {
1556 phba->intr_mode = intr_mode;
1557 }
1558 rc = lpfc_online(phba);
1559 if (rc == 0)
1560 lpfc_unblock_mgmt_io(phba);
1561 }
1562 return rc;
1563}
1564
1565/**
James Smartda0436e2009-05-22 14:51:39 -04001566 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1567 * @phba: pointer to lpfc hba data structure.
1568 *
1569 * This routine is invoked to handle the SLI4 HBA hardware error attention
1570 * conditions.
1571 **/
1572static void
1573lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1574{
1575 struct lpfc_vport *vport = phba->pport;
1576 uint32_t event_data;
1577 struct Scsi_Host *shost;
James Smart2fcee4b2010-12-15 17:57:46 -05001578 uint32_t if_type;
James Smart2e90f4b2011-12-13 13:22:37 -05001579 struct lpfc_register portstat_reg = {0};
1580 uint32_t reg_err1, reg_err2;
1581 uint32_t uerrlo_reg, uemasklo_reg;
1582 uint32_t pci_rd_rc1, pci_rd_rc2;
James Smarte10b2022014-02-20 09:57:43 -05001583 bool en_rn_msg = true;
James Smart73d91e52011-10-10 21:32:10 -04001584 int rc;
James Smartda0436e2009-05-22 14:51:39 -04001585
1586 /* If the pci channel is offline, ignore possible errors, since
1587 * we cannot communicate with the pci card anyway.
1588 */
1589 if (pci_channel_offline(phba->pcidev))
1590 return;
1591 /* If resets are disabled then leave the HBA alone and return */
1592 if (!phba->cfg_enable_hba_reset)
1593 return;
1594
James Smart2fcee4b2010-12-15 17:57:46 -05001595 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1596 switch (if_type) {
1597 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart2e90f4b2011-12-13 13:22:37 -05001598 pci_rd_rc1 = lpfc_readl(
1599 phba->sli4_hba.u.if_type0.UERRLOregaddr,
1600 &uerrlo_reg);
1601 pci_rd_rc2 = lpfc_readl(
1602 phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
1603 &uemasklo_reg);
1604 /* consider PCI bus read error as pci_channel_offline */
1605 if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
1606 return;
James Smart2fcee4b2010-12-15 17:57:46 -05001607 lpfc_sli4_offline_eratt(phba);
1608 break;
1609 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart2e90f4b2011-12-13 13:22:37 -05001610 pci_rd_rc1 = lpfc_readl(
1611 phba->sli4_hba.u.if_type2.STATUSregaddr,
1612 &portstat_reg.word0);
1613 /* consider PCI bus read error as pci_channel_offline */
James Smart6b5151f2012-01-18 16:24:06 -05001614 if (pci_rd_rc1 == -EIO) {
1615 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1616 "3151 PCI bus read access failure: x%x\n",
1617 readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
James Smart2e90f4b2011-12-13 13:22:37 -05001618 return;
James Smart6b5151f2012-01-18 16:24:06 -05001619 }
James Smart2e90f4b2011-12-13 13:22:37 -05001620 reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
1621 reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
James Smart2fcee4b2010-12-15 17:57:46 -05001622 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
1623 /* TODO: Register for Overtemp async events. */
1624 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1625 "2889 Port Overtemperature event, "
James Smart026abb82011-12-13 13:20:45 -05001626 "taking port offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001627 spin_lock_irq(&phba->hbalock);
1628 phba->over_temp_state = HBA_OVER_TEMP;
1629 spin_unlock_irq(&phba->hbalock);
1630 lpfc_sli4_offline_eratt(phba);
James Smart2e90f4b2011-12-13 13:22:37 -05001631 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001632 }
James Smart2e90f4b2011-12-13 13:22:37 -05001633 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smarte10b2022014-02-20 09:57:43 -05001634 reg_err2 == SLIPORT_ERR2_REG_FW_RESTART) {
James Smart2e90f4b2011-12-13 13:22:37 -05001635 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte10b2022014-02-20 09:57:43 -05001636 "3143 Port Down: Firmware Update "
1637 "Detected\n");
1638 en_rn_msg = false;
1639 } else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smart2e90f4b2011-12-13 13:22:37 -05001640 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1641 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1642 "3144 Port Down: Debug Dump\n");
1643 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1644 reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
1645 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1646 "3145 Port Down: Provisioning\n");
James Smart618a5232012-06-12 13:54:36 -04001647
1648 /* Check port status register for function reset */
James Smarte10b2022014-02-20 09:57:43 -05001649 rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT,
1650 en_rn_msg);
James Smart618a5232012-06-12 13:54:36 -04001651 if (rc == 0) {
1652 /* don't report event on forced debug dump */
1653 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1654 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1655 return;
1656 else
1657 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001658 }
James Smart618a5232012-06-12 13:54:36 -04001659 /* fall through for not able to recover */
James Smart6b5151f2012-01-18 16:24:06 -05001660 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1661 "3152 Unrecoverable error, bring the port "
1662 "offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001663 lpfc_sli4_offline_eratt(phba);
1664 break;
1665 case LPFC_SLI_INTF_IF_TYPE_1:
1666 default:
1667 break;
1668 }
James Smart2e90f4b2011-12-13 13:22:37 -05001669 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1670 "3123 Report dump event to upper layer\n");
1671 /* Send an internal error event to mgmt application */
1672 lpfc_board_errevt_to_mgmt(phba);
1673
1674 event_data = FC_REG_DUMP_EVENT;
1675 shost = lpfc_shost_from_vport(vport);
1676 fc_host_post_vendor_event(shost, fc_get_event_number(),
1677 sizeof(event_data), (char *) &event_data,
1678 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
James Smartda0436e2009-05-22 14:51:39 -04001679}
1680
1681/**
1682 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1683 * @phba: pointer to lpfc HBA data structure.
1684 *
1685 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1686 * routine from the API jump table function pointer from the lpfc_hba struct.
1687 *
1688 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001689 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001690 * Any other value - error.
1691 **/
1692void
1693lpfc_handle_eratt(struct lpfc_hba *phba)
1694{
1695 (*phba->lpfc_handle_eratt)(phba);
1696}
1697
1698/**
James Smart3621a712009-04-06 18:47:14 -04001699 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04001700 * @phba: pointer to lpfc hba data structure.
1701 *
1702 * This routine is invoked from the worker thread to handle a HBA host
1703 * attention link event.
1704 **/
dea31012005-04-17 16:05:31 -05001705void
James Smart2e0fef82007-06-17 19:56:36 -05001706lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001707{
James Smart2e0fef82007-06-17 19:56:36 -05001708 struct lpfc_vport *vport = phba->pport;
1709 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001710 LPFC_MBOXQ_t *pmb;
1711 volatile uint32_t control;
1712 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05001713 int rc = 0;
dea31012005-04-17 16:05:31 -05001714
1715 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001716 if (!pmb) {
1717 rc = 1;
dea31012005-04-17 16:05:31 -05001718 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05001719 }
dea31012005-04-17 16:05:31 -05001720
1721 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001722 if (!mp) {
1723 rc = 2;
dea31012005-04-17 16:05:31 -05001724 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05001725 }
dea31012005-04-17 16:05:31 -05001726
1727 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05001728 if (!mp->virt) {
1729 rc = 3;
dea31012005-04-17 16:05:31 -05001730 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05001731 }
dea31012005-04-17 16:05:31 -05001732
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05001733 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04001734 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05001735
1736 psli->slistat.link_event++;
James Smart76a95d72010-11-20 23:11:48 -05001737 lpfc_read_topology(phba, pmb, mp);
1738 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smart2e0fef82007-06-17 19:56:36 -05001739 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04001740 /* Block ELS IOCBs until we have processed this mbox command */
1741 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04001742 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05001743 if (rc == MBX_NOT_FINISHED) {
1744 rc = 4;
James Smart14691152006-12-02 13:34:28 -05001745 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05001746 }
dea31012005-04-17 16:05:31 -05001747
1748 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05001749 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001750 writel(HA_LATT, phba->HAregaddr);
1751 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001752 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001753
1754 return;
1755
James Smart14691152006-12-02 13:34:28 -05001756lpfc_handle_latt_free_mbuf:
James Smart0d2b6b82008-06-14 22:52:47 -04001757 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05001758 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001759lpfc_handle_latt_free_mp:
1760 kfree(mp);
1761lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04001762 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001763lpfc_handle_latt_err_exit:
1764 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001765 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001766 psli->sli_flag |= LPFC_PROCESS_LA;
1767 control = readl(phba->HCregaddr);
1768 control |= HC_LAINT_ENA;
1769 writel(control, phba->HCregaddr);
1770 readl(phba->HCregaddr); /* flush */
1771
1772 /* Clear Link Attention in HA REG */
1773 writel(HA_LATT, phba->HAregaddr);
1774 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001775 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001776 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001777 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001778
James Smart09372822008-01-11 01:52:54 -05001779 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1780 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05001781
1782 return;
1783}
1784
James Smarte59058c2008-08-24 21:49:00 -04001785/**
James Smart3621a712009-04-06 18:47:14 -04001786 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04001787 * @phba: pointer to lpfc hba data structure.
1788 * @vpd: pointer to the vital product data.
1789 * @len: length of the vital product data in bytes.
1790 *
1791 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1792 * an array of characters. In this routine, the ModelName, ProgramType, and
1793 * ModelDesc, etc. fields of the phba data structure will be populated.
1794 *
1795 * Return codes
1796 * 0 - pointer to the VPD passed in is NULL
1797 * 1 - success
1798 **/
James Smart3772a992009-05-22 14:50:54 -04001799int
James Smart2e0fef82007-06-17 19:56:36 -05001800lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05001801{
1802 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05001803 int Length;
dea31012005-04-17 16:05:31 -05001804 int i, j;
1805 int finished = 0;
1806 int index = 0;
1807
1808 if (!vpd)
1809 return 0;
1810
1811 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05001812 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001813 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05001814 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1815 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001816 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05001817 switch (vpd[index]) {
1818 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001819 case 0x91:
dea31012005-04-17 16:05:31 -05001820 index += 1;
1821 lenlo = vpd[index];
1822 index += 1;
1823 lenhi = vpd[index];
1824 index += 1;
1825 i = ((((unsigned short)lenhi) << 8) + lenlo);
1826 index += i;
1827 break;
1828 case 0x90:
1829 index += 1;
1830 lenlo = vpd[index];
1831 index += 1;
1832 lenhi = vpd[index];
1833 index += 1;
1834 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001835 if (Length > len - index)
1836 Length = len - index;
dea31012005-04-17 16:05:31 -05001837 while (Length > 0) {
1838 /* Look for Serial Number */
1839 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1840 index += 2;
1841 i = vpd[index];
1842 index += 1;
1843 j = 0;
1844 Length -= (3+i);
1845 while(i--) {
1846 phba->SerialNumber[j++] = vpd[index++];
1847 if (j == 31)
1848 break;
1849 }
1850 phba->SerialNumber[j] = 0;
1851 continue;
1852 }
1853 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1854 phba->vpd_flag |= VPD_MODEL_DESC;
1855 index += 2;
1856 i = vpd[index];
1857 index += 1;
1858 j = 0;
1859 Length -= (3+i);
1860 while(i--) {
1861 phba->ModelDesc[j++] = vpd[index++];
1862 if (j == 255)
1863 break;
1864 }
1865 phba->ModelDesc[j] = 0;
1866 continue;
1867 }
1868 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1869 phba->vpd_flag |= VPD_MODEL_NAME;
1870 index += 2;
1871 i = vpd[index];
1872 index += 1;
1873 j = 0;
1874 Length -= (3+i);
1875 while(i--) {
1876 phba->ModelName[j++] = vpd[index++];
1877 if (j == 79)
1878 break;
1879 }
1880 phba->ModelName[j] = 0;
1881 continue;
1882 }
1883 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1884 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1885 index += 2;
1886 i = vpd[index];
1887 index += 1;
1888 j = 0;
1889 Length -= (3+i);
1890 while(i--) {
1891 phba->ProgramType[j++] = vpd[index++];
1892 if (j == 255)
1893 break;
1894 }
1895 phba->ProgramType[j] = 0;
1896 continue;
1897 }
1898 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1899 phba->vpd_flag |= VPD_PORT;
1900 index += 2;
1901 i = vpd[index];
1902 index += 1;
1903 j = 0;
1904 Length -= (3+i);
1905 while(i--) {
James Smartcd1c8302011-10-10 21:33:25 -04001906 if ((phba->sli_rev == LPFC_SLI_REV4) &&
1907 (phba->sli4_hba.pport_name_sta ==
1908 LPFC_SLI4_PPNAME_GET)) {
1909 j++;
1910 index++;
1911 } else
1912 phba->Port[j++] = vpd[index++];
1913 if (j == 19)
1914 break;
dea31012005-04-17 16:05:31 -05001915 }
James Smartcd1c8302011-10-10 21:33:25 -04001916 if ((phba->sli_rev != LPFC_SLI_REV4) ||
1917 (phba->sli4_hba.pport_name_sta ==
1918 LPFC_SLI4_PPNAME_NON))
1919 phba->Port[j] = 0;
dea31012005-04-17 16:05:31 -05001920 continue;
1921 }
1922 else {
1923 index += 2;
1924 i = vpd[index];
1925 index += 1;
1926 index += i;
1927 Length -= (3 + i);
1928 }
1929 }
1930 finished = 0;
1931 break;
1932 case 0x78:
1933 finished = 1;
1934 break;
1935 default:
1936 index ++;
1937 break;
1938 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001939 }
dea31012005-04-17 16:05:31 -05001940
1941 return(1);
1942}
1943
James Smarte59058c2008-08-24 21:49:00 -04001944/**
James Smart3621a712009-04-06 18:47:14 -04001945 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04001946 * @phba: pointer to lpfc hba data structure.
1947 * @mdp: pointer to the data structure to hold the derived model name.
1948 * @descp: pointer to the data structure to hold the derived description.
1949 *
1950 * This routine retrieves HBA's description based on its registered PCI device
1951 * ID. The @descp passed into this function points to an array of 256 chars. It
1952 * shall be returned with the model name, maximum speed, and the host bus type.
1953 * The @mdp passed into this function points to an array of 80 chars. When the
1954 * function returns, the @mdp will be filled with the model name.
1955 **/
dea31012005-04-17 16:05:31 -05001956static void
James Smart2e0fef82007-06-17 19:56:36 -05001957lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05001958{
1959 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05001960 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001961 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04001962 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04001963 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001964 struct {
James Smarta747c9c2009-11-18 15:41:10 -05001965 char *name;
1966 char *bus;
1967 char *function;
1968 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001969
1970 if (mdp && mdp[0] != '\0'
1971 && descp && descp[0] != '\0')
1972 return;
1973
James Smartc0c11512011-05-24 11:41:34 -04001974 if (phba->lmt & LMT_16Gb)
1975 max_speed = 16;
1976 else if (phba->lmt & LMT_10Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001977 max_speed = 10;
1978 else if (phba->lmt & LMT_8Gb)
1979 max_speed = 8;
1980 else if (phba->lmt & LMT_4Gb)
1981 max_speed = 4;
1982 else if (phba->lmt & LMT_2Gb)
1983 max_speed = 2;
James Smart4169d862012-09-29 11:32:09 -04001984 else if (phba->lmt & LMT_1Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001985 max_speed = 1;
James Smart4169d862012-09-29 11:32:09 -04001986 else
1987 max_speed = 0;
dea31012005-04-17 16:05:31 -05001988
1989 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05001990
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001991 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001992 case PCI_DEVICE_ID_FIREFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001993 m = (typeof(m)){"LP6000", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001994 break;
dea31012005-04-17 16:05:31 -05001995 case PCI_DEVICE_ID_SUPERFLY:
1996 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smarta747c9c2009-11-18 15:41:10 -05001997 m = (typeof(m)){"LP7000", "PCI",
1998 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001999 else
James Smarta747c9c2009-11-18 15:41:10 -05002000 m = (typeof(m)){"LP7000E", "PCI",
2001 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002002 break;
2003 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002004 m = (typeof(m)){"LP8000", "PCI",
2005 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002006 break;
2007 case PCI_DEVICE_ID_CENTAUR:
2008 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smarta747c9c2009-11-18 15:41:10 -05002009 m = (typeof(m)){"LP9002", "PCI",
2010 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002011 else
James Smarta747c9c2009-11-18 15:41:10 -05002012 m = (typeof(m)){"LP9000", "PCI",
2013 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002014 break;
2015 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002016 m = (typeof(m)){"LP952", "PCI",
2017 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002018 break;
2019 case PCI_DEVICE_ID_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05002020 m = (typeof(m)){"LP9802", "PCI-X",
2021 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002022 break;
2023 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05002024 m = (typeof(m)){"LP10000", "PCI-X",
2025 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002026 break;
2027 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05002028 m = (typeof(m)){"LPX1000", "PCI-X",
2029 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002030 break;
2031 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002032 m = (typeof(m)){"LP982", "PCI-X",
2033 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002034 break;
2035 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002036 m = (typeof(m)){"LP1050", "PCI-X",
2037 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002038 break;
2039 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05002040 m = (typeof(m)){"LP11000", "PCI-X2",
2041 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002042 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002043 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002044 m = (typeof(m)){"LP11000-SP", "PCI-X2",
2045 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002046 break;
2047 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002048 m = (typeof(m)){"LP11002-SP", "PCI-X2",
2049 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002050 break;
2051 case PCI_DEVICE_ID_NEPTUNE:
James Smarta747c9c2009-11-18 15:41:10 -05002052 m = (typeof(m)){"LPe1000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002053 break;
2054 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002055 m = (typeof(m)){"LPe1000-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002056 break;
2057 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002058 m = (typeof(m)){"LPe1002-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002059 break;
dea31012005-04-17 16:05:31 -05002060 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05002061 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002062 break;
2063 case PCI_DEVICE_ID_BSMB:
James Smarta747c9c2009-11-18 15:41:10 -05002064 m = (typeof(m)){"LP111", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002065 break;
2066 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05002067 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002068 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002069 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002070 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002071 break;
2072 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002073 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04002074 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002075 break;
dea31012005-04-17 16:05:31 -05002076 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05002077 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002078 break;
2079 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05002080 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002081 break;
2082 case PCI_DEVICE_ID_LP101:
James Smarta747c9c2009-11-18 15:41:10 -05002083 m = (typeof(m)){"LP101", "PCI-X", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002084 break;
2085 case PCI_DEVICE_ID_LP10000S:
James Smarta747c9c2009-11-18 15:41:10 -05002086 m = (typeof(m)){"LP10000-S", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002087 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002088 case PCI_DEVICE_ID_LP11000S:
James Smarta747c9c2009-11-18 15:41:10 -05002089 m = (typeof(m)){"LP11000-S", "PCI-X2", "Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05002090 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002091 case PCI_DEVICE_ID_LPE11000S:
James Smarta747c9c2009-11-18 15:41:10 -05002092 m = (typeof(m)){"LPe11000-S", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002093 break;
James Smartb87eab32007-04-25 09:53:28 -04002094 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05002095 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002096 break;
2097 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05002098 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002099 break;
2100 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05002101 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002102 break;
2103 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002104 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002105 break;
2106 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002107 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002108 break;
2109 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05002110 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002111 break;
James Smart84774a42008-08-24 21:50:06 -04002112 case PCI_DEVICE_ID_HORNET:
James Smarta747c9c2009-11-18 15:41:10 -05002113 m = (typeof(m)){"LP21000", "PCIe", "FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002114 GE = 1;
2115 break;
2116 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05002117 m = (typeof(m)){"LPev12000", "PCIe IOV",
2118 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002119 break;
2120 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05002121 m = (typeof(m)){"LPev12000", "PCIe IOV",
2122 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002123 break;
2124 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05002125 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
2126 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002127 break;
James Smartda0436e2009-05-22 14:51:39 -04002128 case PCI_DEVICE_ID_TIGERSHARK:
2129 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002130 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04002131 break;
James Smarta747c9c2009-11-18 15:41:10 -05002132 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04002133 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002134 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2135 break;
2136 case PCI_DEVICE_ID_FALCON:
2137 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2138 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04002139 break;
James Smart98fc5dd2010-06-07 15:24:29 -04002140 case PCI_DEVICE_ID_BALIUS:
2141 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
2142 "Fibre Channel Adapter"};
2143 break;
James Smart085c6472010-11-20 23:11:37 -05002144 case PCI_DEVICE_ID_LANCER_FC:
James Smartc0c11512011-05-24 11:41:34 -04002145 case PCI_DEVICE_ID_LANCER_FC_VF:
2146 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
James Smart085c6472010-11-20 23:11:37 -05002147 break;
2148 case PCI_DEVICE_ID_LANCER_FCOE:
James Smartc0c11512011-05-24 11:41:34 -04002149 case PCI_DEVICE_ID_LANCER_FCOE_VF:
James Smart085c6472010-11-20 23:11:37 -05002150 oneConnect = 1;
James Smart079b5c92011-08-21 21:48:49 -04002151 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
James Smart085c6472010-11-20 23:11:37 -05002152 break;
James Smartf8cafd32012-08-03 12:42:51 -04002153 case PCI_DEVICE_ID_SKYHAWK:
2154 case PCI_DEVICE_ID_SKYHAWK_VF:
2155 oneConnect = 1;
2156 m = (typeof(m)){"OCe14000", "PCIe", "FCoE"};
2157 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002158 default:
James Smarta747c9c2009-11-18 15:41:10 -05002159 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002160 break;
dea31012005-04-17 16:05:31 -05002161 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002162
2163 if (mdp && mdp[0] == '\0')
2164 snprintf(mdp, 79,"%s", m.name);
James Smartc0c11512011-05-24 11:41:34 -04002165 /*
2166 * oneConnect hba requires special processing, they are all initiators
James Smartda0436e2009-05-22 14:51:39 -04002167 * and we put the port number on the end
2168 */
2169 if (descp && descp[0] == '\0') {
2170 if (oneConnect)
2171 snprintf(descp, 255,
James Smart4169d862012-09-29 11:32:09 -04002172 "Emulex OneConnect %s, %s Initiator %s",
James Smarta747c9c2009-11-18 15:41:10 -05002173 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04002174 phba->Port);
James Smart4169d862012-09-29 11:32:09 -04002175 else if (max_speed == 0)
2176 snprintf(descp, 255,
2177 "Emulex %s %s %s ",
2178 m.name, m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002179 else
2180 snprintf(descp, 255,
2181 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05002182 m.name, max_speed, (GE) ? "GE" : "Gb",
2183 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002184 }
dea31012005-04-17 16:05:31 -05002185}
2186
James Smarte59058c2008-08-24 21:49:00 -04002187/**
James Smart3621a712009-04-06 18:47:14 -04002188 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04002189 * @phba: pointer to lpfc hba data structure.
2190 * @pring: pointer to a IOCB ring.
2191 * @cnt: the number of IOCBs to be posted to the IOCB ring.
2192 *
2193 * This routine posts a given number of IOCBs with the associated DMA buffer
2194 * descriptors specified by the cnt argument to the given IOCB ring.
2195 *
2196 * Return codes
2197 * The number of IOCBs NOT able to be posted to the IOCB ring.
2198 **/
dea31012005-04-17 16:05:31 -05002199int
James Smart495a7142008-06-14 22:52:59 -04002200lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05002201{
2202 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002203 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05002204 struct lpfc_dmabuf *mp1, *mp2;
2205
2206 cnt += pring->missbufcnt;
2207
2208 /* While there are buffers to post */
2209 while (cnt > 0) {
2210 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002211 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002212 if (iocb == NULL) {
2213 pring->missbufcnt = cnt;
2214 return cnt;
2215 }
dea31012005-04-17 16:05:31 -05002216 icmd = &iocb->iocb;
2217
2218 /* 2 buffers can be posted per command */
2219 /* Allocate buffer to post */
2220 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2221 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04002222 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2223 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002224 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002225 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002226 pring->missbufcnt = cnt;
2227 return cnt;
2228 }
2229
2230 INIT_LIST_HEAD(&mp1->list);
2231 /* Allocate buffer to post */
2232 if (cnt > 1) {
2233 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2234 if (mp2)
2235 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2236 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04002237 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002238 kfree(mp2);
dea31012005-04-17 16:05:31 -05002239 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2240 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002241 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002242 pring->missbufcnt = cnt;
2243 return cnt;
2244 }
2245
2246 INIT_LIST_HEAD(&mp2->list);
2247 } else {
2248 mp2 = NULL;
2249 }
2250
2251 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2252 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2253 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2254 icmd->ulpBdeCount = 1;
2255 cnt--;
2256 if (mp2) {
2257 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2258 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2259 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2260 cnt--;
2261 icmd->ulpBdeCount = 2;
2262 }
2263
2264 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2265 icmd->ulpLe = 1;
2266
James Smart3772a992009-05-22 14:50:54 -04002267 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2268 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05002269 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2270 kfree(mp1);
2271 cnt++;
2272 if (mp2) {
2273 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2274 kfree(mp2);
2275 cnt++;
2276 }
James Bottomley604a3e32005-10-29 10:28:33 -05002277 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002278 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05002279 return cnt;
2280 }
dea31012005-04-17 16:05:31 -05002281 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05002282 if (mp2)
dea31012005-04-17 16:05:31 -05002283 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05002284 }
2285 pring->missbufcnt = 0;
2286 return 0;
2287}
2288
James Smarte59058c2008-08-24 21:49:00 -04002289/**
James Smart3621a712009-04-06 18:47:14 -04002290 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04002291 * @phba: pointer to lpfc hba data structure.
2292 *
2293 * This routine posts initial receive IOCB buffers to the ELS ring. The
2294 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
2295 * set to 64 IOCBs.
2296 *
2297 * Return codes
2298 * 0 - success (currently always success)
2299 **/
dea31012005-04-17 16:05:31 -05002300static int
James Smart2e0fef82007-06-17 19:56:36 -05002301lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002302{
2303 struct lpfc_sli *psli = &phba->sli;
2304
2305 /* Ring 0, ELS / CT buffers */
James Smart495a7142008-06-14 22:52:59 -04002306 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05002307 /* Ring 2 - FCP no buffers needed */
2308
2309 return 0;
2310}
2311
2312#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2313
James Smarte59058c2008-08-24 21:49:00 -04002314/**
James Smart3621a712009-04-06 18:47:14 -04002315 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04002316 * @HashResultPointer: pointer to an array as hash table.
2317 *
2318 * This routine sets up the initial values to the array of hash table entries
2319 * for the LC HBAs.
2320 **/
dea31012005-04-17 16:05:31 -05002321static void
2322lpfc_sha_init(uint32_t * HashResultPointer)
2323{
2324 HashResultPointer[0] = 0x67452301;
2325 HashResultPointer[1] = 0xEFCDAB89;
2326 HashResultPointer[2] = 0x98BADCFE;
2327 HashResultPointer[3] = 0x10325476;
2328 HashResultPointer[4] = 0xC3D2E1F0;
2329}
2330
James Smarte59058c2008-08-24 21:49:00 -04002331/**
James Smart3621a712009-04-06 18:47:14 -04002332 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04002333 * @HashResultPointer: pointer to an initial/result hash table.
2334 * @HashWorkingPointer: pointer to an working hash table.
2335 *
2336 * This routine iterates an initial hash table pointed by @HashResultPointer
2337 * with the values from the working hash table pointeed by @HashWorkingPointer.
2338 * The results are putting back to the initial hash table, returned through
2339 * the @HashResultPointer as the result hash table.
2340 **/
dea31012005-04-17 16:05:31 -05002341static void
2342lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2343{
2344 int t;
2345 uint32_t TEMP;
2346 uint32_t A, B, C, D, E;
2347 t = 16;
2348 do {
2349 HashWorkingPointer[t] =
2350 S(1,
2351 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2352 8] ^
2353 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2354 } while (++t <= 79);
2355 t = 0;
2356 A = HashResultPointer[0];
2357 B = HashResultPointer[1];
2358 C = HashResultPointer[2];
2359 D = HashResultPointer[3];
2360 E = HashResultPointer[4];
2361
2362 do {
2363 if (t < 20) {
2364 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2365 } else if (t < 40) {
2366 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2367 } else if (t < 60) {
2368 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2369 } else {
2370 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2371 }
2372 TEMP += S(5, A) + E + HashWorkingPointer[t];
2373 E = D;
2374 D = C;
2375 C = S(30, B);
2376 B = A;
2377 A = TEMP;
2378 } while (++t <= 79);
2379
2380 HashResultPointer[0] += A;
2381 HashResultPointer[1] += B;
2382 HashResultPointer[2] += C;
2383 HashResultPointer[3] += D;
2384 HashResultPointer[4] += E;
2385
2386}
2387
James Smarte59058c2008-08-24 21:49:00 -04002388/**
James Smart3621a712009-04-06 18:47:14 -04002389 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002390 * @RandomChallenge: pointer to the entry of host challenge random number array.
2391 * @HashWorking: pointer to the entry of the working hash array.
2392 *
2393 * This routine calculates the working hash array referred by @HashWorking
2394 * from the challenge random numbers associated with the host, referred by
2395 * @RandomChallenge. The result is put into the entry of the working hash
2396 * array and returned by reference through @HashWorking.
2397 **/
dea31012005-04-17 16:05:31 -05002398static void
2399lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2400{
2401 *HashWorking = (*RandomChallenge ^ *HashWorking);
2402}
2403
James Smarte59058c2008-08-24 21:49:00 -04002404/**
James Smart3621a712009-04-06 18:47:14 -04002405 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002406 * @phba: pointer to lpfc hba data structure.
2407 * @hbainit: pointer to an array of unsigned 32-bit integers.
2408 *
2409 * This routine performs the special handling for LC HBA initialization.
2410 **/
dea31012005-04-17 16:05:31 -05002411void
2412lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2413{
2414 int t;
2415 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002416 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002417
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002418 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002419 if (!HashWorking)
2420 return;
2421
dea31012005-04-17 16:05:31 -05002422 HashWorking[0] = HashWorking[78] = *pwwnn++;
2423 HashWorking[1] = HashWorking[79] = *pwwnn;
2424
2425 for (t = 0; t < 7; t++)
2426 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2427
2428 lpfc_sha_init(hbainit);
2429 lpfc_sha_iterate(hbainit, HashWorking);
2430 kfree(HashWorking);
2431}
2432
James Smarte59058c2008-08-24 21:49:00 -04002433/**
James Smart3621a712009-04-06 18:47:14 -04002434 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002435 * @vport: pointer to a virtual N_Port data structure.
2436 *
2437 * This routine performs the necessary cleanups before deleting the @vport.
2438 * It invokes the discovery state machine to perform necessary state
2439 * transitions and to release the ndlps associated with the @vport. Note,
2440 * the physical port is treated as @vport 0.
2441 **/
James Smart87af33f2007-10-27 13:37:43 -04002442void
James Smart2e0fef82007-06-17 19:56:36 -05002443lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002444{
James Smart87af33f2007-10-27 13:37:43 -04002445 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002446 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002447 int i = 0;
dea31012005-04-17 16:05:31 -05002448
James Smart87af33f2007-10-27 13:37:43 -04002449 if (phba->link_state > LPFC_LINK_DOWN)
2450 lpfc_port_link_failure(vport);
2451
2452 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002453 if (!NLP_CHK_NODE_ACT(ndlp)) {
2454 ndlp = lpfc_enable_node(vport, ndlp,
2455 NLP_STE_UNUSED_NODE);
2456 if (!ndlp)
2457 continue;
2458 spin_lock_irq(&phba->ndlp_lock);
2459 NLP_SET_FREE_REQ(ndlp);
2460 spin_unlock_irq(&phba->ndlp_lock);
2461 /* Trigger the release of the ndlp memory */
2462 lpfc_nlp_put(ndlp);
2463 continue;
2464 }
2465 spin_lock_irq(&phba->ndlp_lock);
2466 if (NLP_CHK_FREE_REQ(ndlp)) {
2467 /* The ndlp should not be in memory free mode already */
2468 spin_unlock_irq(&phba->ndlp_lock);
2469 continue;
2470 } else
2471 /* Indicate request for freeing ndlp memory */
2472 NLP_SET_FREE_REQ(ndlp);
2473 spin_unlock_irq(&phba->ndlp_lock);
2474
James Smart58da1ff2008-04-07 10:15:56 -04002475 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2476 ndlp->nlp_DID == Fabric_DID) {
2477 /* Just free up ndlp with Fabric_DID for vports */
2478 lpfc_nlp_put(ndlp);
2479 continue;
2480 }
2481
James Smarteff4a012012-01-18 16:25:25 -05002482 /* take care of nodes in unused state before the state
2483 * machine taking action.
2484 */
2485 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
2486 lpfc_nlp_put(ndlp);
2487 continue;
2488 }
2489
James Smart87af33f2007-10-27 13:37:43 -04002490 if (ndlp->nlp_type & NLP_FABRIC)
2491 lpfc_disc_state_machine(vport, ndlp, NULL,
2492 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002493
James Smart87af33f2007-10-27 13:37:43 -04002494 lpfc_disc_state_machine(vport, ndlp, NULL,
2495 NLP_EVT_DEVICE_RM);
2496 }
2497
James Smarta8adb832007-10-27 13:37:53 -04002498 /* At this point, ALL ndlp's should be gone
2499 * because of the previous NLP_EVT_DEVICE_RM.
2500 * Lets wait for this to happen, if needed.
2501 */
James Smart87af33f2007-10-27 13:37:43 -04002502 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002503 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002504 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002505 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002506 list_for_each_entry_safe(ndlp, next_ndlp,
2507 &vport->fc_nodes, nlp_listp) {
2508 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2509 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002510 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002511 "usgmap:x%x refcnt:%d\n",
2512 ndlp->nlp_DID, (void *)ndlp,
2513 ndlp->nlp_usg_map,
2514 atomic_read(
2515 &ndlp->kref.refcount));
2516 }
James Smarta8adb832007-10-27 13:37:53 -04002517 break;
James Smart87af33f2007-10-27 13:37:43 -04002518 }
James Smarta8adb832007-10-27 13:37:53 -04002519
2520 /* Wait for any activity on ndlps to settle */
2521 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002522 }
James Smart1151e3e2011-02-16 12:39:35 -05002523 lpfc_cleanup_vports_rrqs(vport, NULL);
dea31012005-04-17 16:05:31 -05002524}
2525
James Smarte59058c2008-08-24 21:49:00 -04002526/**
James Smart3621a712009-04-06 18:47:14 -04002527 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002528 * @vport: pointer to a virtual N_Port data structure.
2529 *
2530 * This routine stops all the timers associated with a @vport. This function
2531 * is invoked before disabling or deleting a @vport. Note that the physical
2532 * port is treated as @vport 0.
2533 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002534void
2535lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002536{
James Smart92d7f7b2007-06-17 19:56:38 -05002537 del_timer_sync(&vport->els_tmofunc);
2538 del_timer_sync(&vport->fc_fdmitmo);
James Smart92494142011-02-16 12:39:44 -05002539 del_timer_sync(&vport->delayed_disc_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002540 lpfc_can_disctmo(vport);
2541 return;
dea31012005-04-17 16:05:31 -05002542}
2543
James Smarte59058c2008-08-24 21:49:00 -04002544/**
James Smartecfd03c2010-02-12 14:41:27 -05002545 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2546 * @phba: pointer to lpfc hba data structure.
2547 *
2548 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2549 * caller of this routine should already hold the host lock.
2550 **/
2551void
2552__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2553{
James Smart5ac6b302010-10-22 11:05:36 -04002554 /* Clear pending FCF rediscovery wait flag */
2555 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2556
James Smartecfd03c2010-02-12 14:41:27 -05002557 /* Now, try to stop the timer */
2558 del_timer(&phba->fcf.redisc_wait);
2559}
2560
2561/**
2562 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2563 * @phba: pointer to lpfc hba data structure.
2564 *
2565 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2566 * checks whether the FCF rediscovery wait timer is pending with the host
2567 * lock held before proceeding with disabling the timer and clearing the
2568 * wait timer pendig flag.
2569 **/
2570void
2571lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2572{
2573 spin_lock_irq(&phba->hbalock);
2574 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2575 /* FCF rediscovery timer already fired or stopped */
2576 spin_unlock_irq(&phba->hbalock);
2577 return;
2578 }
2579 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart5ac6b302010-10-22 11:05:36 -04002580 /* Clear failover in progress flags */
2581 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05002582 spin_unlock_irq(&phba->hbalock);
2583}
2584
2585/**
James Smart3772a992009-05-22 14:50:54 -04002586 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002587 * @phba: pointer to lpfc hba data structure.
2588 *
2589 * This routine stops all the timers associated with a HBA. This function is
2590 * invoked before either putting a HBA offline or unloading the driver.
2591 **/
James Smart3772a992009-05-22 14:50:54 -04002592void
2593lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002594{
James Smart51ef4c22007-08-02 11:10:31 -04002595 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05002596 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002597 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002598 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002599 del_timer_sync(&phba->hb_tmofunc);
James Smart1151e3e2011-02-16 12:39:35 -05002600 if (phba->sli_rev == LPFC_SLI_REV4) {
2601 del_timer_sync(&phba->rrq_tmr);
2602 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2603 }
James Smart3772a992009-05-22 14:50:54 -04002604 phba->hb_outstanding = 0;
2605
2606 switch (phba->pci_dev_grp) {
2607 case LPFC_PCI_DEV_LP:
2608 /* Stop any LightPulse device specific driver timers */
2609 del_timer_sync(&phba->fcp_poll_timer);
2610 break;
2611 case LPFC_PCI_DEV_OC:
2612 /* Stop any OneConnect device sepcific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05002613 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04002614 break;
2615 default:
2616 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2617 "0297 Invalid device group (x%x)\n",
2618 phba->pci_dev_grp);
2619 break;
2620 }
James Smart2e0fef82007-06-17 19:56:36 -05002621 return;
dea31012005-04-17 16:05:31 -05002622}
2623
James Smarte59058c2008-08-24 21:49:00 -04002624/**
James Smart3621a712009-04-06 18:47:14 -04002625 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002626 * @phba: pointer to lpfc hba data structure.
2627 *
2628 * This routine marks a HBA's management interface as blocked. Once the HBA's
2629 * management interface is marked as blocked, all the user space access to
2630 * the HBA, whether they are from sysfs interface or libdfc interface will
2631 * all be blocked. The HBA is set to block the management interface when the
2632 * driver prepares the HBA interface for online or offline.
2633 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002634static void
James Smart618a5232012-06-12 13:54:36 -04002635lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
Adrian Bunka6ababd2007-11-05 18:07:33 +01002636{
2637 unsigned long iflag;
James Smart6e7288d2010-06-07 15:23:35 -04002638 uint8_t actcmd = MBX_HEARTBEAT;
2639 unsigned long timeout;
2640
Adrian Bunka6ababd2007-11-05 18:07:33 +01002641 spin_lock_irqsave(&phba->hbalock, iflag);
2642 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
James Smart618a5232012-06-12 13:54:36 -04002643 spin_unlock_irqrestore(&phba->hbalock, iflag);
2644 if (mbx_action == LPFC_MBX_NO_WAIT)
2645 return;
2646 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
2647 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002648 if (phba->sli.mbox_active) {
James Smart6e7288d2010-06-07 15:23:35 -04002649 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
James Smarta183a152011-10-10 21:32:43 -04002650 /* Determine how long we might wait for the active mailbox
2651 * command to be gracefully completed by firmware.
2652 */
2653 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
2654 phba->sli.mbox_active) * 1000) + jiffies;
2655 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002656 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002657
James Smart6e7288d2010-06-07 15:23:35 -04002658 /* Wait for the outstnading mailbox command to complete */
2659 while (phba->sli.mbox_active) {
2660 /* Check active mailbox complete status every 2ms */
2661 msleep(2);
2662 if (time_after(jiffies, timeout)) {
2663 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2664 "2813 Mgmt IO is Blocked %x "
2665 "- mbox cmd %x still active\n",
2666 phba->sli.sli_flag, actcmd);
2667 break;
2668 }
2669 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002670}
2671
James Smarte59058c2008-08-24 21:49:00 -04002672/**
James Smart6b5151f2012-01-18 16:24:06 -05002673 * lpfc_sli4_node_prep - Assign RPIs for active nodes.
2674 * @phba: pointer to lpfc hba data structure.
2675 *
2676 * Allocate RPIs for all active remote nodes. This is needed whenever
2677 * an SLI4 adapter is reset and the driver is not unloading. Its purpose
2678 * is to fixup the temporary rpi assignments.
2679 **/
2680void
2681lpfc_sli4_node_prep(struct lpfc_hba *phba)
2682{
2683 struct lpfc_nodelist *ndlp, *next_ndlp;
2684 struct lpfc_vport **vports;
2685 int i;
2686
2687 if (phba->sli_rev != LPFC_SLI_REV4)
2688 return;
2689
2690 vports = lpfc_create_vport_work_array(phba);
2691 if (vports != NULL) {
2692 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2693 if (vports[i]->load_flag & FC_UNLOADING)
2694 continue;
2695
2696 list_for_each_entry_safe(ndlp, next_ndlp,
2697 &vports[i]->fc_nodes,
2698 nlp_listp) {
2699 if (NLP_CHK_NODE_ACT(ndlp))
2700 ndlp->nlp_rpi =
2701 lpfc_sli4_alloc_rpi(phba);
2702 }
2703 }
2704 }
2705 lpfc_destroy_vport_work_array(phba, vports);
2706}
2707
2708/**
James Smart3621a712009-04-06 18:47:14 -04002709 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04002710 * @phba: pointer to lpfc hba data structure.
2711 *
2712 * This routine initializes the HBA and brings a HBA online. During this
2713 * process, the management interface is blocked to prevent user space access
2714 * to the HBA interfering with the driver initialization.
2715 *
2716 * Return codes
2717 * 0 - successful
2718 * 1 - failed
2719 **/
dea31012005-04-17 16:05:31 -05002720int
James Smart2e0fef82007-06-17 19:56:36 -05002721lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002722{
Julia Lawall372bd282008-12-16 16:15:08 +01002723 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04002724 struct lpfc_vport **vports;
2725 int i;
James Smart16a3a202013-04-17 20:14:38 -04002726 bool vpis_cleared = false;
James Smart2e0fef82007-06-17 19:56:36 -05002727
dea31012005-04-17 16:05:31 -05002728 if (!phba)
2729 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01002730 vport = phba->pport;
dea31012005-04-17 16:05:31 -05002731
James Smart2e0fef82007-06-17 19:56:36 -05002732 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05002733 return 0;
2734
James Smarted957682007-06-17 19:56:37 -05002735 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002736 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05002737
James Smart618a5232012-06-12 13:54:36 -04002738 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05002739
James Smart46fa3112007-04-25 09:51:45 -04002740 if (!lpfc_sli_queue_setup(phba)) {
2741 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002742 return 1;
James Smart46fa3112007-04-25 09:51:45 -04002743 }
2744
James Smartda0436e2009-05-22 14:51:39 -04002745 if (phba->sli_rev == LPFC_SLI_REV4) {
2746 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2747 lpfc_unblock_mgmt_io(phba);
2748 return 1;
2749 }
James Smart16a3a202013-04-17 20:14:38 -04002750 spin_lock_irq(&phba->hbalock);
2751 if (!phba->sli4_hba.max_cfg_param.vpi_used)
2752 vpis_cleared = true;
2753 spin_unlock_irq(&phba->hbalock);
James Smartda0436e2009-05-22 14:51:39 -04002754 } else {
2755 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2756 lpfc_unblock_mgmt_io(phba);
2757 return 1;
2758 }
James Smart46fa3112007-04-25 09:51:45 -04002759 }
dea31012005-04-17 16:05:31 -05002760
James Smart549e55c2007-08-02 11:09:51 -04002761 vports = lpfc_create_vport_work_array(phba);
2762 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002763 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002764 struct Scsi_Host *shost;
2765 shost = lpfc_shost_from_vport(vports[i]);
2766 spin_lock_irq(shost->host_lock);
2767 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2768 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2769 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart16a3a202013-04-17 20:14:38 -04002770 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart1c6834a2009-07-19 10:01:26 -04002771 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart16a3a202013-04-17 20:14:38 -04002772 if ((vpis_cleared) &&
2773 (vports[i]->port_type !=
2774 LPFC_PHYSICAL_PORT))
2775 vports[i]->vpi = 0;
2776 }
James Smart549e55c2007-08-02 11:09:51 -04002777 spin_unlock_irq(shost->host_lock);
2778 }
James Smart09372822008-01-11 01:52:54 -05002779 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002780
James Smart46fa3112007-04-25 09:51:45 -04002781 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002782 return 0;
2783}
2784
James Smarte59058c2008-08-24 21:49:00 -04002785/**
James Smart3621a712009-04-06 18:47:14 -04002786 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04002787 * @phba: pointer to lpfc hba data structure.
2788 *
2789 * This routine marks a HBA's management interface as not blocked. Once the
2790 * HBA's management interface is marked as not blocked, all the user space
2791 * access to the HBA, whether they are from sysfs interface or libdfc
2792 * interface will be allowed. The HBA is set to block the management interface
2793 * when the driver prepares the HBA interface for online or offline and then
2794 * set to unblock the management interface afterwards.
2795 **/
James Smart46fa3112007-04-25 09:51:45 -04002796void
James Smart46fa3112007-04-25 09:51:45 -04002797lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2798{
2799 unsigned long iflag;
2800
James Smart2e0fef82007-06-17 19:56:36 -05002801 spin_lock_irqsave(&phba->hbalock, iflag);
2802 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2803 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04002804}
2805
James Smarte59058c2008-08-24 21:49:00 -04002806/**
James Smart3621a712009-04-06 18:47:14 -04002807 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04002808 * @phba: pointer to lpfc hba data structure.
2809 *
2810 * This routine is invoked to prepare a HBA to be brought offline. It performs
2811 * unregistration login to all the nodes on all vports and flushes the mailbox
2812 * queue to make it ready to be brought offline.
2813 **/
James Smart46fa3112007-04-25 09:51:45 -04002814void
James Smart618a5232012-06-12 13:54:36 -04002815lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
James Smart46fa3112007-04-25 09:51:45 -04002816{
James Smart2e0fef82007-06-17 19:56:36 -05002817 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04002818 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04002819 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05002820 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04002821 int i;
dea31012005-04-17 16:05:31 -05002822
James Smart2e0fef82007-06-17 19:56:36 -05002823 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002824 return;
dea31012005-04-17 16:05:31 -05002825
James Smart618a5232012-06-12 13:54:36 -04002826 lpfc_block_mgmt_io(phba, mbx_action);
dea31012005-04-17 16:05:31 -05002827
2828 lpfc_linkdown(phba);
2829
James Smart87af33f2007-10-27 13:37:43 -04002830 /* Issue an unreg_login to all nodes on all vports */
2831 vports = lpfc_create_vport_work_array(phba);
2832 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04002833 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04002834 if (vports[i]->load_flag & FC_UNLOADING)
2835 continue;
James Smart72100cc2010-02-12 14:43:01 -05002836 shost = lpfc_shost_from_vport(vports[i]);
2837 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002838 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002839 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2840 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05002841 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002842
James Smart87af33f2007-10-27 13:37:43 -04002843 shost = lpfc_shost_from_vport(vports[i]);
2844 list_for_each_entry_safe(ndlp, next_ndlp,
2845 &vports[i]->fc_nodes,
2846 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002847 if (!NLP_CHK_NODE_ACT(ndlp))
2848 continue;
James Smart87af33f2007-10-27 13:37:43 -04002849 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2850 continue;
2851 if (ndlp->nlp_type & NLP_FABRIC) {
2852 lpfc_disc_state_machine(vports[i], ndlp,
2853 NULL, NLP_EVT_DEVICE_RECOVERY);
2854 lpfc_disc_state_machine(vports[i], ndlp,
2855 NULL, NLP_EVT_DEVICE_RM);
2856 }
2857 spin_lock_irq(shost->host_lock);
2858 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart401ee0c2012-03-01 22:35:34 -05002859 spin_unlock_irq(shost->host_lock);
James Smart6b5151f2012-01-18 16:24:06 -05002860 /*
2861 * Whenever an SLI4 port goes offline, free the
James Smart401ee0c2012-03-01 22:35:34 -05002862 * RPI. Get a new RPI when the adapter port
2863 * comes back online.
James Smart6b5151f2012-01-18 16:24:06 -05002864 */
2865 if (phba->sli_rev == LPFC_SLI_REV4)
2866 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smart87af33f2007-10-27 13:37:43 -04002867 lpfc_unreg_rpi(vports[i], ndlp);
2868 }
2869 }
2870 }
James Smart09372822008-01-11 01:52:54 -05002871 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002872
James Smart618a5232012-06-12 13:54:36 -04002873 lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
James Smart46fa3112007-04-25 09:51:45 -04002874}
2875
James Smarte59058c2008-08-24 21:49:00 -04002876/**
James Smart3621a712009-04-06 18:47:14 -04002877 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04002878 * @phba: pointer to lpfc hba data structure.
2879 *
2880 * This routine actually brings a HBA offline. It stops all the timers
2881 * associated with the HBA, brings down the SLI layer, and eventually
2882 * marks the HBA as in offline state for the upper layer protocol.
2883 **/
James Smart46fa3112007-04-25 09:51:45 -04002884void
James Smart2e0fef82007-06-17 19:56:36 -05002885lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04002886{
James Smart549e55c2007-08-02 11:09:51 -04002887 struct Scsi_Host *shost;
2888 struct lpfc_vport **vports;
2889 int i;
James Smart46fa3112007-04-25 09:51:45 -04002890
James Smart549e55c2007-08-02 11:09:51 -04002891 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002892 return;
James Smart688a8862006-07-06 15:49:56 -04002893
James Smartda0436e2009-05-22 14:51:39 -04002894 /* stop port and all timers associated with this hba */
2895 lpfc_stop_port(phba);
James Smart51ef4c22007-08-02 11:10:31 -04002896 vports = lpfc_create_vport_work_array(phba);
2897 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002898 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04002899 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05002900 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05002901 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002902 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05002903 /* Bring down the SLI Layer and cleanup. The HBA is offline
2904 now. */
2905 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002906 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04002907 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002908 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04002909 vports = lpfc_create_vport_work_array(phba);
2910 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002911 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002912 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04002913 spin_lock_irq(shost->host_lock);
2914 vports[i]->work_port_events = 0;
2915 vports[i]->fc_flag |= FC_OFFLINE_MODE;
2916 spin_unlock_irq(shost->host_lock);
2917 }
James Smart09372822008-01-11 01:52:54 -05002918 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002919}
2920
James Smarte59058c2008-08-24 21:49:00 -04002921/**
James Smart3621a712009-04-06 18:47:14 -04002922 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04002923 * @phba: pointer to lpfc hba data structure.
2924 *
2925 * This routine is to free all the SCSI buffers and IOCBs from the driver
2926 * list back to kernel. It is called from lpfc_pci_remove_one to free
2927 * the internal resources before the device is removed from the system.
James Smarte59058c2008-08-24 21:49:00 -04002928 **/
James Smart8a9d2e82012-05-09 21:16:12 -04002929static void
James Smart2e0fef82007-06-17 19:56:36 -05002930lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002931{
2932 struct lpfc_scsi_buf *sb, *sb_next;
2933 struct lpfc_iocbq *io, *io_next;
2934
James Smart2e0fef82007-06-17 19:56:36 -05002935 spin_lock_irq(&phba->hbalock);
James Smarta40fc5f2013-04-17 20:17:40 -04002936
dea31012005-04-17 16:05:31 -05002937 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smarta40fc5f2013-04-17 20:17:40 -04002938
2939 spin_lock(&phba->scsi_buf_list_put_lock);
2940 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_put,
2941 list) {
dea31012005-04-17 16:05:31 -05002942 list_del(&sb->list);
2943 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05002944 sb->dma_handle);
dea31012005-04-17 16:05:31 -05002945 kfree(sb);
2946 phba->total_scsi_bufs--;
2947 }
James Smarta40fc5f2013-04-17 20:17:40 -04002948 spin_unlock(&phba->scsi_buf_list_put_lock);
2949
2950 spin_lock(&phba->scsi_buf_list_get_lock);
2951 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_get,
2952 list) {
2953 list_del(&sb->list);
2954 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
2955 sb->dma_handle);
2956 kfree(sb);
2957 phba->total_scsi_bufs--;
2958 }
2959 spin_unlock(&phba->scsi_buf_list_get_lock);
dea31012005-04-17 16:05:31 -05002960
2961 /* Release all the lpfc_iocbq entries maintained by this host. */
2962 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2963 list_del(&io->list);
2964 kfree(io);
2965 phba->total_iocbq_bufs--;
2966 }
James Smart6d368e52011-05-24 11:44:12 -04002967
James Smart2e0fef82007-06-17 19:56:36 -05002968 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04002969}
2970
2971/**
2972 * lpfc_sli4_xri_sgl_update - update xri-sgl sizing and mapping
2973 * @phba: pointer to lpfc hba data structure.
2974 *
2975 * This routine first calculates the sizes of the current els and allocated
2976 * scsi sgl lists, and then goes through all sgls to updates the physical
2977 * XRIs assigned due to port function reset. During port initialization, the
2978 * current els and allocated scsi sgl lists are 0s.
2979 *
2980 * Return codes
2981 * 0 - successful (for now, it always returns 0)
2982 **/
2983int
2984lpfc_sli4_xri_sgl_update(struct lpfc_hba *phba)
2985{
2986 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
2987 struct lpfc_scsi_buf *psb = NULL, *psb_next = NULL;
2988 uint16_t i, lxri, xri_cnt, els_xri_cnt, scsi_xri_cnt;
2989 LIST_HEAD(els_sgl_list);
2990 LIST_HEAD(scsi_sgl_list);
2991 int rc;
2992
2993 /*
2994 * update on pci function's els xri-sgl list
2995 */
2996 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
2997 if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
2998 /* els xri-sgl expanded */
2999 xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
3000 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3001 "3157 ELS xri-sgl count increased from "
3002 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3003 els_xri_cnt);
3004 /* allocate the additional els sgls */
3005 for (i = 0; i < xri_cnt; i++) {
3006 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3007 GFP_KERNEL);
3008 if (sglq_entry == NULL) {
3009 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3010 "2562 Failure to allocate an "
3011 "ELS sgl entry:%d\n", i);
3012 rc = -ENOMEM;
3013 goto out_free_mem;
3014 }
3015 sglq_entry->buff_type = GEN_BUFF_TYPE;
3016 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
3017 &sglq_entry->phys);
3018 if (sglq_entry->virt == NULL) {
3019 kfree(sglq_entry);
3020 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3021 "2563 Failure to allocate an "
3022 "ELS mbuf:%d\n", i);
3023 rc = -ENOMEM;
3024 goto out_free_mem;
3025 }
3026 sglq_entry->sgl = sglq_entry->virt;
3027 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
3028 sglq_entry->state = SGL_FREED;
3029 list_add_tail(&sglq_entry->list, &els_sgl_list);
3030 }
James Smart38c20672013-03-01 16:37:44 -05003031 spin_lock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003032 list_splice_init(&els_sgl_list, &phba->sli4_hba.lpfc_sgl_list);
James Smart38c20672013-03-01 16:37:44 -05003033 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003034 } else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
3035 /* els xri-sgl shrinked */
3036 xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
3037 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3038 "3158 ELS xri-sgl count decreased from "
3039 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3040 els_xri_cnt);
3041 spin_lock_irq(&phba->hbalock);
3042 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &els_sgl_list);
3043 spin_unlock_irq(&phba->hbalock);
3044 /* release extra els sgls from list */
3045 for (i = 0; i < xri_cnt; i++) {
3046 list_remove_head(&els_sgl_list,
3047 sglq_entry, struct lpfc_sglq, list);
3048 if (sglq_entry) {
3049 lpfc_mbuf_free(phba, sglq_entry->virt,
3050 sglq_entry->phys);
3051 kfree(sglq_entry);
3052 }
3053 }
3054 spin_lock_irq(&phba->hbalock);
3055 list_splice_init(&els_sgl_list, &phba->sli4_hba.lpfc_sgl_list);
3056 spin_unlock_irq(&phba->hbalock);
3057 } else
3058 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3059 "3163 ELS xri-sgl count unchanged: %d\n",
3060 els_xri_cnt);
3061 phba->sli4_hba.els_xri_cnt = els_xri_cnt;
3062
3063 /* update xris to els sgls on the list */
3064 sglq_entry = NULL;
3065 sglq_entry_next = NULL;
3066 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
3067 &phba->sli4_hba.lpfc_sgl_list, list) {
3068 lxri = lpfc_sli4_next_xritag(phba);
3069 if (lxri == NO_XRI) {
3070 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3071 "2400 Failed to allocate xri for "
3072 "ELS sgl\n");
3073 rc = -ENOMEM;
3074 goto out_free_mem;
3075 }
3076 sglq_entry->sli4_lxritag = lxri;
3077 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3078 }
3079
3080 /*
3081 * update on pci function's allocated scsi xri-sgl list
3082 */
3083 phba->total_scsi_bufs = 0;
3084
3085 /* maximum number of xris available for scsi buffers */
3086 phba->sli4_hba.scsi_xri_max = phba->sli4_hba.max_cfg_param.max_xri -
3087 els_xri_cnt;
3088
3089 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3090 "2401 Current allocated SCSI xri-sgl count:%d, "
3091 "maximum SCSI xri count:%d\n",
3092 phba->sli4_hba.scsi_xri_cnt,
3093 phba->sli4_hba.scsi_xri_max);
3094
James Smarta40fc5f2013-04-17 20:17:40 -04003095 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart164cecd2013-09-06 12:22:38 -04003096 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003097 list_splice_init(&phba->lpfc_scsi_buf_list_get, &scsi_sgl_list);
3098 list_splice(&phba->lpfc_scsi_buf_list_put, &scsi_sgl_list);
James Smart164cecd2013-09-06 12:22:38 -04003099 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003100 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003101
3102 if (phba->sli4_hba.scsi_xri_cnt > phba->sli4_hba.scsi_xri_max) {
3103 /* max scsi xri shrinked below the allocated scsi buffers */
3104 scsi_xri_cnt = phba->sli4_hba.scsi_xri_cnt -
3105 phba->sli4_hba.scsi_xri_max;
3106 /* release the extra allocated scsi buffers */
3107 for (i = 0; i < scsi_xri_cnt; i++) {
3108 list_remove_head(&scsi_sgl_list, psb,
3109 struct lpfc_scsi_buf, list);
3110 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, psb->data,
3111 psb->dma_handle);
3112 kfree(psb);
3113 }
James Smarta40fc5f2013-04-17 20:17:40 -04003114 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003115 phba->sli4_hba.scsi_xri_cnt -= scsi_xri_cnt;
James Smarta40fc5f2013-04-17 20:17:40 -04003116 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003117 }
3118
3119 /* update xris associated to remaining allocated scsi buffers */
3120 psb = NULL;
3121 psb_next = NULL;
3122 list_for_each_entry_safe(psb, psb_next, &scsi_sgl_list, list) {
3123 lxri = lpfc_sli4_next_xritag(phba);
3124 if (lxri == NO_XRI) {
3125 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3126 "2560 Failed to allocate xri for "
3127 "scsi buffer\n");
3128 rc = -ENOMEM;
3129 goto out_free_mem;
3130 }
3131 psb->cur_iocbq.sli4_lxritag = lxri;
3132 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3133 }
James Smarta40fc5f2013-04-17 20:17:40 -04003134 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart164cecd2013-09-06 12:22:38 -04003135 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003136 list_splice_init(&scsi_sgl_list, &phba->lpfc_scsi_buf_list_get);
3137 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
James Smart164cecd2013-09-06 12:22:38 -04003138 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003139 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003140
dea31012005-04-17 16:05:31 -05003141 return 0;
James Smart8a9d2e82012-05-09 21:16:12 -04003142
3143out_free_mem:
3144 lpfc_free_els_sgl_list(phba);
3145 lpfc_scsi_free(phba);
3146 return rc;
dea31012005-04-17 16:05:31 -05003147}
3148
James Smarte59058c2008-08-24 21:49:00 -04003149/**
James Smart3621a712009-04-06 18:47:14 -04003150 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04003151 * @phba: pointer to lpfc hba data structure.
3152 * @instance: a unique integer ID to this FC port.
3153 * @dev: pointer to the device data structure.
3154 *
3155 * This routine creates a FC port for the upper layer protocol. The FC port
3156 * can be created on top of either a physical port or a virtual port provided
3157 * by the HBA. This routine also allocates a SCSI host data structure (shost)
3158 * and associates the FC port created before adding the shost into the SCSI
3159 * layer.
3160 *
3161 * Return codes
3162 * @vport - pointer to the virtual N_Port data structure.
3163 * NULL - port create failed.
3164 **/
James Smart2e0fef82007-06-17 19:56:36 -05003165struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04003166lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04003167{
James Smart2e0fef82007-06-17 19:56:36 -05003168 struct lpfc_vport *vport;
3169 struct Scsi_Host *shost;
3170 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04003171
James Smart3de2a652007-08-02 11:09:59 -04003172 if (dev != &phba->pcidev->dev)
3173 shost = scsi_host_alloc(&lpfc_vport_template,
3174 sizeof(struct lpfc_vport));
3175 else
3176 shost = scsi_host_alloc(&lpfc_template,
3177 sizeof(struct lpfc_vport));
James Smart2e0fef82007-06-17 19:56:36 -05003178 if (!shost)
3179 goto out;
James Smart47a86172007-04-25 09:53:22 -04003180
James Smart2e0fef82007-06-17 19:56:36 -05003181 vport = (struct lpfc_vport *) shost->hostdata;
3182 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05003183 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05003184 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05003185 vport->fc_rscn_flush = 0;
James Smart47a86172007-04-25 09:53:22 -04003186
James Smart3de2a652007-08-02 11:09:59 -04003187 lpfc_get_vport_cfgparam(vport);
James Smart2e0fef82007-06-17 19:56:36 -05003188 shost->unique_id = instance;
3189 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04003190 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05003191 shost->this_id = -1;
3192 shost->max_cmd_len = 16;
James Smartda0436e2009-05-22 14:51:39 -04003193 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart28baac72010-02-12 14:42:03 -05003194 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04003195 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smartda0436e2009-05-22 14:51:39 -04003196 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
3197 }
James Smart81301a92008-12-04 22:39:46 -05003198
James Smart47a86172007-04-25 09:53:22 -04003199 /*
James Smart2e0fef82007-06-17 19:56:36 -05003200 * Set initial can_queue value since 0 is no longer supported and
3201 * scsi_add_host will fail. This will be adjusted later based on the
3202 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04003203 */
James Smart2e0fef82007-06-17 19:56:36 -05003204 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04003205 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05003206 shost->transportt = lpfc_vport_transport_template;
3207 vport->port_type = LPFC_NPIV_PORT;
3208 } else {
3209 shost->transportt = lpfc_transport_template;
3210 vport->port_type = LPFC_PHYSICAL_PORT;
3211 }
James Smart47a86172007-04-25 09:53:22 -04003212
James Smart2e0fef82007-06-17 19:56:36 -05003213 /* Initialize all internally managed lists. */
3214 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04003215 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05003216 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04003217
James Smart2e0fef82007-06-17 19:56:36 -05003218 init_timer(&vport->fc_disctmo);
3219 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05003220 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04003221
James Smart2e0fef82007-06-17 19:56:36 -05003222 init_timer(&vport->fc_fdmitmo);
3223 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
James Smart92d7f7b2007-06-17 19:56:38 -05003224 vport->fc_fdmitmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04003225
James Smart2e0fef82007-06-17 19:56:36 -05003226 init_timer(&vport->els_tmofunc);
3227 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05003228 vport->els_tmofunc.data = (unsigned long)vport;
James Smart92494142011-02-16 12:39:44 -05003229
3230 init_timer(&vport->delayed_disc_tmo);
3231 vport->delayed_disc_tmo.function = lpfc_delayed_disc_tmo;
3232 vport->delayed_disc_tmo.data = (unsigned long)vport;
3233
James Bottomleyd139b9b2009-11-05 13:33:12 -06003234 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05003235 if (error)
3236 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04003237
James Smart549e55c2007-08-02 11:09:51 -04003238 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003239 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04003240 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003241 return vport;
James Smart47a86172007-04-25 09:53:22 -04003242
James Smart2e0fef82007-06-17 19:56:36 -05003243out_put_shost:
3244 scsi_host_put(shost);
3245out:
3246 return NULL;
James Smart47a86172007-04-25 09:53:22 -04003247}
3248
James Smarte59058c2008-08-24 21:49:00 -04003249/**
James Smart3621a712009-04-06 18:47:14 -04003250 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04003251 * @vport: pointer to an lpfc virtual N_Port data structure.
3252 *
3253 * This routine destroys a FC port from the upper layer protocol. All the
3254 * resources associated with the port are released.
3255 **/
James Smart2e0fef82007-06-17 19:56:36 -05003256void
3257destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04003258{
James Smart92d7f7b2007-06-17 19:56:38 -05003259 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3260 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003261
James Smart858c9f62007-06-17 19:56:39 -05003262 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05003263 fc_remove_host(shost);
3264 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04003265
James Smart92d7f7b2007-06-17 19:56:38 -05003266 spin_lock_irq(&phba->hbalock);
3267 list_del_init(&vport->listentry);
3268 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04003269
James Smart92d7f7b2007-06-17 19:56:38 -05003270 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04003271 return;
James Smart47a86172007-04-25 09:53:22 -04003272}
3273
James Smarte59058c2008-08-24 21:49:00 -04003274/**
James Smart3621a712009-04-06 18:47:14 -04003275 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04003276 *
3277 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
3278 * uses the kernel idr facility to perform the task.
3279 *
3280 * Return codes:
3281 * instance - a unique integer ID allocated as the new instance.
3282 * -1 - lpfc get instance failed.
3283 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003284int
3285lpfc_get_instance(void)
3286{
Tejun Heoab516032013-02-27 17:04:44 -08003287 int ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003288
Tejun Heoab516032013-02-27 17:04:44 -08003289 ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
3290 return ret < 0 ? -1 : ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003291}
3292
James Smarte59058c2008-08-24 21:49:00 -04003293/**
James Smart3621a712009-04-06 18:47:14 -04003294 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04003295 * @shost: pointer to SCSI host data structure.
3296 * @time: elapsed time of the scan in jiffies.
3297 *
3298 * This routine is called by the SCSI layer with a SCSI host to determine
3299 * whether the scan host is finished.
3300 *
3301 * Note: there is no scan_start function as adapter initialization will have
3302 * asynchronously kicked off the link initialization.
3303 *
3304 * Return codes
3305 * 0 - SCSI host scan is not over yet.
3306 * 1 - SCSI host scan is over.
3307 **/
James Smart47a86172007-04-25 09:53:22 -04003308int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
3309{
James Smart2e0fef82007-06-17 19:56:36 -05003310 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3311 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05003312 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04003313
James Smart858c9f62007-06-17 19:56:39 -05003314 spin_lock_irq(shost->host_lock);
3315
James Smart51ef4c22007-08-02 11:10:31 -04003316 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05003317 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003318 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05003319 }
James Smart256ec0d2013-04-17 20:14:58 -04003320 if (time >= msecs_to_jiffies(30 * 1000)) {
James Smart2e0fef82007-06-17 19:56:36 -05003321 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003322 "0461 Scanning longer than 30 "
3323 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003324 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003325 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003326 }
James Smart256ec0d2013-04-17 20:14:58 -04003327 if (time >= msecs_to_jiffies(15 * 1000) &&
3328 phba->link_state <= LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -05003329 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003330 "0465 Link down longer than 15 "
3331 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003332 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05003333 goto finished;
James Smart47a86172007-04-25 09:53:22 -04003334 }
3335
James Smart2e0fef82007-06-17 19:56:36 -05003336 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05003337 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003338 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05003339 goto finished;
James Smart256ec0d2013-04-17 20:14:58 -04003340 if (vport->fc_map_cnt == 0 && time < msecs_to_jiffies(2 * 1000))
James Smart858c9f62007-06-17 19:56:39 -05003341 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003342 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05003343 goto finished;
3344
3345 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003346
3347finished:
James Smart858c9f62007-06-17 19:56:39 -05003348 spin_unlock_irq(shost->host_lock);
3349 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05003350}
3351
James Smarte59058c2008-08-24 21:49:00 -04003352/**
James Smart3621a712009-04-06 18:47:14 -04003353 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04003354 * @shost: pointer to SCSI host data structure.
3355 *
3356 * This routine initializes a given SCSI host attributes on a FC port. The
3357 * SCSI host can be either on top of a physical port or a virtual port.
3358 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003359void lpfc_host_attrib_init(struct Scsi_Host *shost)
3360{
3361 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3362 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003363 /*
James Smart2e0fef82007-06-17 19:56:36 -05003364 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04003365 */
3366
James Smart2e0fef82007-06-17 19:56:36 -05003367 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
3368 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04003369 fc_host_supported_classes(shost) = FC_COS_CLASS3;
3370
3371 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003372 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003373 fc_host_supported_fc4s(shost)[2] = 1;
3374 fc_host_supported_fc4s(shost)[7] = 1;
3375
James Smart92d7f7b2007-06-17 19:56:38 -05003376 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
3377 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04003378
3379 fc_host_supported_speeds(shost) = 0;
James Smart88a2cfb2011-07-22 18:36:33 -04003380 if (phba->lmt & LMT_16Gb)
3381 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
James Smart47a86172007-04-25 09:53:22 -04003382 if (phba->lmt & LMT_10Gb)
3383 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04003384 if (phba->lmt & LMT_8Gb)
3385 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04003386 if (phba->lmt & LMT_4Gb)
3387 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
3388 if (phba->lmt & LMT_2Gb)
3389 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
3390 if (phba->lmt & LMT_1Gb)
3391 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
3392
3393 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05003394 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
3395 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04003396
Mike Christie0af5d702010-09-15 16:52:31 -05003397 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
3398
James Smart47a86172007-04-25 09:53:22 -04003399 /* This value is also unchanging */
3400 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003401 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003402 fc_host_active_fc4s(shost)[2] = 1;
3403 fc_host_active_fc4s(shost)[7] = 1;
3404
James Smart92d7f7b2007-06-17 19:56:38 -05003405 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04003406 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04003407 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04003408 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04003409}
dea31012005-04-17 16:05:31 -05003410
James Smarte59058c2008-08-24 21:49:00 -04003411/**
James Smartda0436e2009-05-22 14:51:39 -04003412 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04003413 * @phba: pointer to lpfc hba data structure.
3414 *
James Smartda0436e2009-05-22 14:51:39 -04003415 * This routine is invoked to stop an SLI3 device port, it stops the device
3416 * from generating interrupts and stops the device driver's timers for the
3417 * device.
James Smarte59058c2008-08-24 21:49:00 -04003418 **/
James Smartdb2378e2008-02-08 18:49:51 -05003419static void
James Smartda0436e2009-05-22 14:51:39 -04003420lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05003421{
James Smartda0436e2009-05-22 14:51:39 -04003422 /* Clear all interrupt enable conditions */
3423 writel(0, phba->HCregaddr);
3424 readl(phba->HCregaddr); /* flush */
3425 /* Clear all pending interrupts */
3426 writel(0xffffffff, phba->HAregaddr);
3427 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04003428
James Smartda0436e2009-05-22 14:51:39 -04003429 /* Reset some HBA SLI setup states */
3430 lpfc_stop_hba_timers(phba);
3431 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05003432}
3433
James Smarte59058c2008-08-24 21:49:00 -04003434/**
James Smartda0436e2009-05-22 14:51:39 -04003435 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05003436 * @phba: pointer to lpfc hba data structure.
3437 *
James Smartda0436e2009-05-22 14:51:39 -04003438 * This routine is invoked to stop an SLI4 device port, it stops the device
3439 * from generating interrupts and stops the device driver's timers for the
3440 * device.
3441 **/
3442static void
3443lpfc_stop_port_s4(struct lpfc_hba *phba)
3444{
3445 /* Reset some HBA SLI4 setup states */
3446 lpfc_stop_hba_timers(phba);
3447 phba->pport->work_port_events = 0;
3448 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04003449}
3450
3451/**
3452 * lpfc_stop_port - Wrapper function for stopping hba port
3453 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05003454 *
James Smartda0436e2009-05-22 14:51:39 -04003455 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
3456 * the API jump table function pointer from the lpfc_hba struct.
3457 **/
3458void
3459lpfc_stop_port(struct lpfc_hba *phba)
3460{
3461 phba->lpfc_stop_port(phba);
3462}
3463
3464/**
James Smartecfd03c2010-02-12 14:41:27 -05003465 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
3466 * @phba: Pointer to hba for which this call is being executed.
3467 *
3468 * This routine starts the timer waiting for the FCF rediscovery to complete.
3469 **/
3470void
3471lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
3472{
3473 unsigned long fcf_redisc_wait_tmo =
3474 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
3475 /* Start fcf rediscovery wait period timer */
3476 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
3477 spin_lock_irq(&phba->hbalock);
3478 /* Allow action to new fcf asynchronous event */
3479 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
3480 /* Mark the FCF rediscovery pending state */
3481 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
3482 spin_unlock_irq(&phba->hbalock);
3483}
3484
3485/**
3486 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
3487 * @ptr: Map to lpfc_hba data structure pointer.
3488 *
3489 * This routine is invoked when waiting for FCF table rediscover has been
3490 * timed out. If new FCF record(s) has (have) been discovered during the
3491 * wait period, a new FCF event shall be added to the FCOE async event
3492 * list, and then worker thread shall be waked up for processing from the
3493 * worker thread context.
3494 **/
3495void
3496lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
3497{
3498 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
3499
3500 /* Don't send FCF rediscovery event if timer cancelled */
3501 spin_lock_irq(&phba->hbalock);
3502 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
3503 spin_unlock_irq(&phba->hbalock);
3504 return;
3505 }
3506 /* Clear FCF rediscovery timer pending flag */
3507 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
3508 /* FCF rediscovery event to worker thread */
3509 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
3510 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003511 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04003512 "2776 FCF rediscover quiescent timer expired\n");
James Smartecfd03c2010-02-12 14:41:27 -05003513 /* wake up worker thread */
3514 lpfc_worker_wake_up(phba);
3515}
3516
3517/**
James Smartda0436e2009-05-22 14:51:39 -04003518 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
3519 * @phba: pointer to lpfc hba data structure.
3520 * @acqe_link: pointer to the async link completion queue entry.
3521 *
3522 * This routine is to parse the SLI4 link-attention link fault code and
3523 * translate it into the base driver's read link attention mailbox command
3524 * status.
3525 *
3526 * Return: Link-attention status in terms of base driver's coding.
3527 **/
3528static uint16_t
3529lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
3530 struct lpfc_acqe_link *acqe_link)
3531{
3532 uint16_t latt_fault;
3533
3534 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
3535 case LPFC_ASYNC_LINK_FAULT_NONE:
3536 case LPFC_ASYNC_LINK_FAULT_LOCAL:
3537 case LPFC_ASYNC_LINK_FAULT_REMOTE:
3538 latt_fault = 0;
3539 break;
3540 default:
3541 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3542 "0398 Invalid link fault code: x%x\n",
3543 bf_get(lpfc_acqe_link_fault, acqe_link));
3544 latt_fault = MBXERR_ERROR;
3545 break;
3546 }
3547 return latt_fault;
3548}
3549
3550/**
3551 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
3552 * @phba: pointer to lpfc hba data structure.
3553 * @acqe_link: pointer to the async link completion queue entry.
3554 *
3555 * This routine is to parse the SLI4 link attention type and translate it
3556 * into the base driver's link attention type coding.
3557 *
3558 * Return: Link attention type in terms of base driver's coding.
3559 **/
3560static uint8_t
3561lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
3562 struct lpfc_acqe_link *acqe_link)
3563{
3564 uint8_t att_type;
3565
3566 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
3567 case LPFC_ASYNC_LINK_STATUS_DOWN:
3568 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
James Smart76a95d72010-11-20 23:11:48 -05003569 att_type = LPFC_ATT_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04003570 break;
3571 case LPFC_ASYNC_LINK_STATUS_UP:
3572 /* Ignore physical link up events - wait for logical link up */
James Smart76a95d72010-11-20 23:11:48 -05003573 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003574 break;
3575 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
James Smart76a95d72010-11-20 23:11:48 -05003576 att_type = LPFC_ATT_LINK_UP;
James Smartda0436e2009-05-22 14:51:39 -04003577 break;
3578 default:
3579 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3580 "0399 Invalid link attention type: x%x\n",
3581 bf_get(lpfc_acqe_link_status, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003582 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003583 break;
3584 }
3585 return att_type;
3586}
3587
3588/**
3589 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
3590 * @phba: pointer to lpfc hba data structure.
3591 * @acqe_link: pointer to the async link completion queue entry.
3592 *
3593 * This routine is to parse the SLI4 link-attention link speed and translate
3594 * it into the base driver's link-attention link speed coding.
3595 *
3596 * Return: Link-attention link speed in terms of base driver's coding.
3597 **/
3598static uint8_t
3599lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
3600 struct lpfc_acqe_link *acqe_link)
3601{
3602 uint8_t link_speed;
3603
3604 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
3605 case LPFC_ASYNC_LINK_SPEED_ZERO:
James Smartda0436e2009-05-22 14:51:39 -04003606 case LPFC_ASYNC_LINK_SPEED_10MBPS:
James Smartda0436e2009-05-22 14:51:39 -04003607 case LPFC_ASYNC_LINK_SPEED_100MBPS:
James Smart76a95d72010-11-20 23:11:48 -05003608 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003609 break;
3610 case LPFC_ASYNC_LINK_SPEED_1GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003611 link_speed = LPFC_LINK_SPEED_1GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003612 break;
3613 case LPFC_ASYNC_LINK_SPEED_10GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003614 link_speed = LPFC_LINK_SPEED_10GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003615 break;
3616 default:
3617 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3618 "0483 Invalid link-attention link speed: x%x\n",
3619 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003620 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003621 break;
3622 }
3623 return link_speed;
3624}
3625
3626/**
James Smart8b68cd52012-09-29 11:32:37 -04003627 * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
3628 * @phba: pointer to lpfc hba data structure.
3629 *
3630 * This routine is to get an SLI3 FC port's link speed in Mbps.
3631 *
3632 * Return: link speed in terms of Mbps.
3633 **/
3634uint32_t
3635lpfc_sli_port_speed_get(struct lpfc_hba *phba)
3636{
3637 uint32_t link_speed;
3638
3639 if (!lpfc_is_link_up(phba))
3640 return 0;
3641
3642 switch (phba->fc_linkspeed) {
3643 case LPFC_LINK_SPEED_1GHZ:
3644 link_speed = 1000;
3645 break;
3646 case LPFC_LINK_SPEED_2GHZ:
3647 link_speed = 2000;
3648 break;
3649 case LPFC_LINK_SPEED_4GHZ:
3650 link_speed = 4000;
3651 break;
3652 case LPFC_LINK_SPEED_8GHZ:
3653 link_speed = 8000;
3654 break;
3655 case LPFC_LINK_SPEED_10GHZ:
3656 link_speed = 10000;
3657 break;
3658 case LPFC_LINK_SPEED_16GHZ:
3659 link_speed = 16000;
3660 break;
3661 default:
3662 link_speed = 0;
3663 }
3664 return link_speed;
3665}
3666
3667/**
3668 * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
3669 * @phba: pointer to lpfc hba data structure.
3670 * @evt_code: asynchronous event code.
3671 * @speed_code: asynchronous event link speed code.
3672 *
3673 * This routine is to parse the giving SLI4 async event link speed code into
3674 * value of Mbps for the link speed.
3675 *
3676 * Return: link speed in terms of Mbps.
3677 **/
3678static uint32_t
3679lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
3680 uint8_t speed_code)
3681{
3682 uint32_t port_speed;
3683
3684 switch (evt_code) {
3685 case LPFC_TRAILER_CODE_LINK:
3686 switch (speed_code) {
3687 case LPFC_EVT_CODE_LINK_NO_LINK:
3688 port_speed = 0;
3689 break;
3690 case LPFC_EVT_CODE_LINK_10_MBIT:
3691 port_speed = 10;
3692 break;
3693 case LPFC_EVT_CODE_LINK_100_MBIT:
3694 port_speed = 100;
3695 break;
3696 case LPFC_EVT_CODE_LINK_1_GBIT:
3697 port_speed = 1000;
3698 break;
3699 case LPFC_EVT_CODE_LINK_10_GBIT:
3700 port_speed = 10000;
3701 break;
3702 default:
3703 port_speed = 0;
3704 }
3705 break;
3706 case LPFC_TRAILER_CODE_FC:
3707 switch (speed_code) {
3708 case LPFC_EVT_CODE_FC_NO_LINK:
3709 port_speed = 0;
3710 break;
3711 case LPFC_EVT_CODE_FC_1_GBAUD:
3712 port_speed = 1000;
3713 break;
3714 case LPFC_EVT_CODE_FC_2_GBAUD:
3715 port_speed = 2000;
3716 break;
3717 case LPFC_EVT_CODE_FC_4_GBAUD:
3718 port_speed = 4000;
3719 break;
3720 case LPFC_EVT_CODE_FC_8_GBAUD:
3721 port_speed = 8000;
3722 break;
3723 case LPFC_EVT_CODE_FC_10_GBAUD:
3724 port_speed = 10000;
3725 break;
3726 case LPFC_EVT_CODE_FC_16_GBAUD:
3727 port_speed = 16000;
3728 break;
3729 default:
3730 port_speed = 0;
3731 }
3732 break;
3733 default:
3734 port_speed = 0;
3735 }
3736 return port_speed;
3737}
3738
3739/**
James Smart70f3c072010-12-15 17:57:33 -05003740 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
James Smartda0436e2009-05-22 14:51:39 -04003741 * @phba: pointer to lpfc hba data structure.
3742 * @acqe_link: pointer to the async link completion queue entry.
3743 *
James Smart70f3c072010-12-15 17:57:33 -05003744 * This routine is to handle the SLI4 asynchronous FCoE link event.
James Smartda0436e2009-05-22 14:51:39 -04003745 **/
3746static void
3747lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3748 struct lpfc_acqe_link *acqe_link)
3749{
3750 struct lpfc_dmabuf *mp;
3751 LPFC_MBOXQ_t *pmb;
3752 MAILBOX_t *mb;
James Smart76a95d72010-11-20 23:11:48 -05003753 struct lpfc_mbx_read_top *la;
James Smartda0436e2009-05-22 14:51:39 -04003754 uint8_t att_type;
James Smart76a95d72010-11-20 23:11:48 -05003755 int rc;
James Smartda0436e2009-05-22 14:51:39 -04003756
3757 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05003758 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
James Smartda0436e2009-05-22 14:51:39 -04003759 return;
James Smart32b97932009-07-19 10:01:21 -04003760 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003761 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3762 if (!pmb) {
3763 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3764 "0395 The mboxq allocation failed\n");
3765 return;
3766 }
3767 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3768 if (!mp) {
3769 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3770 "0396 The lpfc_dmabuf allocation failed\n");
3771 goto out_free_pmb;
3772 }
3773 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3774 if (!mp->virt) {
3775 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3776 "0397 The mbuf allocation failed\n");
3777 goto out_free_dmabuf;
3778 }
3779
3780 /* Cleanup any outstanding ELS commands */
3781 lpfc_els_flush_all_cmd(phba);
3782
3783 /* Block ELS IOCBs until we have done process link event */
3784 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3785
3786 /* Update link event statistics */
3787 phba->sli.slistat.link_event++;
3788
James Smart76a95d72010-11-20 23:11:48 -05003789 /* Create lpfc_handle_latt mailbox command from link ACQE */
3790 lpfc_read_topology(phba, pmb, mp);
3791 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smartda0436e2009-05-22 14:51:39 -04003792 pmb->vport = phba->pport;
3793
James Smartda0436e2009-05-22 14:51:39 -04003794 /* Keep the link status for extra SLI4 state machine reference */
3795 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04003796 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
3797 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smartda0436e2009-05-22 14:51:39 -04003798 phba->sli4_hba.link_state.duplex =
3799 bf_get(lpfc_acqe_link_duplex, acqe_link);
3800 phba->sli4_hba.link_state.status =
3801 bf_get(lpfc_acqe_link_status, acqe_link);
James Smart70f3c072010-12-15 17:57:33 -05003802 phba->sli4_hba.link_state.type =
3803 bf_get(lpfc_acqe_link_type, acqe_link);
3804 phba->sli4_hba.link_state.number =
3805 bf_get(lpfc_acqe_link_number, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04003806 phba->sli4_hba.link_state.fault =
3807 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05003808 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04003809 bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
3810
James Smart70f3c072010-12-15 17:57:33 -05003811 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartc31098c2011-04-16 11:03:33 -04003812 "2900 Async FC/FCoE Link event - Speed:%dGBit "
3813 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
3814 "Logical speed:%dMbps Fault:%d\n",
James Smart70f3c072010-12-15 17:57:33 -05003815 phba->sli4_hba.link_state.speed,
3816 phba->sli4_hba.link_state.topology,
3817 phba->sli4_hba.link_state.status,
3818 phba->sli4_hba.link_state.type,
3819 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04003820 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05003821 phba->sli4_hba.link_state.fault);
James Smart76a95d72010-11-20 23:11:48 -05003822 /*
3823 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
3824 * topology info. Note: Optional for non FC-AL ports.
3825 */
3826 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3827 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3828 if (rc == MBX_NOT_FINISHED)
3829 goto out_free_dmabuf;
3830 return;
3831 }
3832 /*
3833 * For FCoE Mode: fill in all the topology information we need and call
3834 * the READ_TOPOLOGY completion routine to continue without actually
3835 * sending the READ_TOPOLOGY mailbox command to the port.
3836 */
3837 /* Parse and translate status field */
3838 mb = &pmb->u.mb;
3839 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
3840
3841 /* Parse and translate link attention fields */
3842 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
3843 la->eventTag = acqe_link->event_tag;
3844 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
3845 bf_set(lpfc_mbx_read_top_link_spd, la,
3846 lpfc_sli4_parse_latt_link_speed(phba, acqe_link));
3847
3848 /* Fake the the following irrelvant fields */
3849 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
3850 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
3851 bf_set(lpfc_mbx_read_top_il, la, 0);
3852 bf_set(lpfc_mbx_read_top_pb, la, 0);
3853 bf_set(lpfc_mbx_read_top_fa, la, 0);
3854 bf_set(lpfc_mbx_read_top_mm, la, 0);
James Smartda0436e2009-05-22 14:51:39 -04003855
3856 /* Invoke the lpfc_handle_latt mailbox command callback function */
James Smart76a95d72010-11-20 23:11:48 -05003857 lpfc_mbx_cmpl_read_topology(phba, pmb);
James Smartda0436e2009-05-22 14:51:39 -04003858
3859 return;
3860
3861out_free_dmabuf:
3862 kfree(mp);
3863out_free_pmb:
3864 mempool_free(pmb, phba->mbox_mem_pool);
3865}
3866
3867/**
James Smart70f3c072010-12-15 17:57:33 -05003868 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
3869 * @phba: pointer to lpfc hba data structure.
3870 * @acqe_fc: pointer to the async fc completion queue entry.
3871 *
3872 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
3873 * that the event was received and then issue a read_topology mailbox command so
3874 * that the rest of the driver will treat it the same as SLI3.
3875 **/
3876static void
3877lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
3878{
3879 struct lpfc_dmabuf *mp;
3880 LPFC_MBOXQ_t *pmb;
3881 int rc;
3882
3883 if (bf_get(lpfc_trailer_type, acqe_fc) !=
3884 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
3885 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3886 "2895 Non FC link Event detected.(%d)\n",
3887 bf_get(lpfc_trailer_type, acqe_fc));
3888 return;
3889 }
3890 /* Keep the link status for extra SLI4 state machine reference */
3891 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04003892 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
3893 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
James Smart70f3c072010-12-15 17:57:33 -05003894 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
3895 phba->sli4_hba.link_state.topology =
3896 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
3897 phba->sli4_hba.link_state.status =
3898 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
3899 phba->sli4_hba.link_state.type =
3900 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
3901 phba->sli4_hba.link_state.number =
3902 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
3903 phba->sli4_hba.link_state.fault =
3904 bf_get(lpfc_acqe_link_fault, acqe_fc);
3905 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04003906 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
James Smart70f3c072010-12-15 17:57:33 -05003907 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3908 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
3909 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
3910 "%dMbps Fault:%d\n",
3911 phba->sli4_hba.link_state.speed,
3912 phba->sli4_hba.link_state.topology,
3913 phba->sli4_hba.link_state.status,
3914 phba->sli4_hba.link_state.type,
3915 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04003916 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05003917 phba->sli4_hba.link_state.fault);
3918 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3919 if (!pmb) {
3920 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3921 "2897 The mboxq allocation failed\n");
3922 return;
3923 }
3924 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3925 if (!mp) {
3926 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3927 "2898 The lpfc_dmabuf allocation failed\n");
3928 goto out_free_pmb;
3929 }
3930 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3931 if (!mp->virt) {
3932 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3933 "2899 The mbuf allocation failed\n");
3934 goto out_free_dmabuf;
3935 }
3936
3937 /* Cleanup any outstanding ELS commands */
3938 lpfc_els_flush_all_cmd(phba);
3939
3940 /* Block ELS IOCBs until we have done process link event */
3941 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3942
3943 /* Update link event statistics */
3944 phba->sli.slistat.link_event++;
3945
3946 /* Create lpfc_handle_latt mailbox command from link ACQE */
3947 lpfc_read_topology(phba, pmb, mp);
3948 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
3949 pmb->vport = phba->pport;
3950
3951 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3952 if (rc == MBX_NOT_FINISHED)
3953 goto out_free_dmabuf;
3954 return;
3955
3956out_free_dmabuf:
3957 kfree(mp);
3958out_free_pmb:
3959 mempool_free(pmb, phba->mbox_mem_pool);
3960}
3961
3962/**
3963 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
3964 * @phba: pointer to lpfc hba data structure.
3965 * @acqe_fc: pointer to the async SLI completion queue entry.
3966 *
3967 * This routine is to handle the SLI4 asynchronous SLI events.
3968 **/
3969static void
3970lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
3971{
James Smart4b8bae02012-06-12 13:55:07 -04003972 char port_name;
James Smart8c1312e2012-10-31 14:45:09 -04003973 char message[128];
James Smart4b8bae02012-06-12 13:55:07 -04003974 uint8_t status;
3975 struct lpfc_acqe_misconfigured_event *misconfigured;
3976
3977 /* special case misconfigured event as it contains data for all ports */
3978 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3979 LPFC_SLI_INTF_IF_TYPE_2) ||
3980 (bf_get(lpfc_trailer_type, acqe_sli) !=
3981 LPFC_SLI_EVENT_TYPE_MISCONFIGURED)) {
3982 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3983 "2901 Async SLI event - Event Data1:x%08x Event Data2:"
3984 "x%08x SLI Event Type:%d\n",
3985 acqe_sli->event_data1, acqe_sli->event_data2,
3986 bf_get(lpfc_trailer_type, acqe_sli));
3987 return;
3988 }
3989
3990 port_name = phba->Port[0];
3991 if (port_name == 0x00)
3992 port_name = '?'; /* get port name is empty */
3993
3994 misconfigured = (struct lpfc_acqe_misconfigured_event *)
3995 &acqe_sli->event_data1;
3996
3997 /* fetch the status for this port */
3998 switch (phba->sli4_hba.lnk_info.lnk_no) {
3999 case LPFC_LINK_NUMBER_0:
4000 status = bf_get(lpfc_sli_misconfigured_port0,
4001 &misconfigured->theEvent);
4002 break;
4003 case LPFC_LINK_NUMBER_1:
4004 status = bf_get(lpfc_sli_misconfigured_port1,
4005 &misconfigured->theEvent);
4006 break;
4007 case LPFC_LINK_NUMBER_2:
4008 status = bf_get(lpfc_sli_misconfigured_port2,
4009 &misconfigured->theEvent);
4010 break;
4011 case LPFC_LINK_NUMBER_3:
4012 status = bf_get(lpfc_sli_misconfigured_port3,
4013 &misconfigured->theEvent);
4014 break;
4015 default:
4016 status = ~LPFC_SLI_EVENT_STATUS_VALID;
4017 break;
4018 }
4019
4020 switch (status) {
4021 case LPFC_SLI_EVENT_STATUS_VALID:
4022 return; /* no message if the sfp is okay */
4023 case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
James Smart292098b2012-09-29 11:31:41 -04004024 sprintf(message, "Optics faulted/incorrectly installed/not " \
4025 "installed - Reseat optics, if issue not "
4026 "resolved, replace.");
James Smart4b8bae02012-06-12 13:55:07 -04004027 break;
4028 case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
4029 sprintf(message,
James Smart292098b2012-09-29 11:31:41 -04004030 "Optics of two types installed - Remove one optic or " \
4031 "install matching pair of optics.");
James Smart4b8bae02012-06-12 13:55:07 -04004032 break;
4033 case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
James Smart292098b2012-09-29 11:31:41 -04004034 sprintf(message, "Incompatible optics - Replace with " \
4035 "compatible optics for card to function.");
James Smart4b8bae02012-06-12 13:55:07 -04004036 break;
4037 default:
4038 /* firmware is reporting a status we don't know about */
4039 sprintf(message, "Unknown event status x%02x", status);
4040 break;
4041 }
4042
4043 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4044 "3176 Misconfigured Physical Port - "
4045 "Port Name %c %s\n", port_name, message);
James Smart70f3c072010-12-15 17:57:33 -05004046}
4047
4048/**
James Smartfc2b9892010-02-26 14:15:29 -05004049 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
4050 * @vport: pointer to vport data structure.
4051 *
4052 * This routine is to perform Clear Virtual Link (CVL) on a vport in
4053 * response to a CVL event.
4054 *
4055 * Return the pointer to the ndlp with the vport if successful, otherwise
4056 * return NULL.
4057 **/
4058static struct lpfc_nodelist *
4059lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
4060{
4061 struct lpfc_nodelist *ndlp;
4062 struct Scsi_Host *shost;
4063 struct lpfc_hba *phba;
4064
4065 if (!vport)
4066 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05004067 phba = vport->phba;
4068 if (!phba)
4069 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04004070 ndlp = lpfc_findnode_did(vport, Fabric_DID);
4071 if (!ndlp) {
4072 /* Cannot find existing Fabric ndlp, so allocate a new one */
4073 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
4074 if (!ndlp)
4075 return 0;
4076 lpfc_nlp_init(vport, ndlp, Fabric_DID);
4077 /* Set the node type */
4078 ndlp->nlp_type |= NLP_FABRIC;
4079 /* Put ndlp onto node list */
4080 lpfc_enqueue_node(vport, ndlp);
4081 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4082 /* re-setup ndlp without removing from node list */
4083 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
4084 if (!ndlp)
4085 return 0;
4086 }
James Smart63e801c2010-11-20 23:14:19 -05004087 if ((phba->pport->port_state < LPFC_FLOGI) &&
4088 (phba->pport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05004089 return NULL;
4090 /* If virtual link is not yet instantiated ignore CVL */
James Smart63e801c2010-11-20 23:14:19 -05004091 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
4092 && (vport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05004093 return NULL;
4094 shost = lpfc_shost_from_vport(vport);
4095 if (!shost)
4096 return NULL;
4097 lpfc_linkdown_port(vport);
4098 lpfc_cleanup_pending_mbox(vport);
4099 spin_lock_irq(shost->host_lock);
4100 vport->fc_flag |= FC_VPORT_CVL_RCVD;
4101 spin_unlock_irq(shost->host_lock);
4102
4103 return ndlp;
4104}
4105
4106/**
4107 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
4108 * @vport: pointer to lpfc hba data structure.
4109 *
4110 * This routine is to perform Clear Virtual Link (CVL) on all vports in
4111 * response to a FCF dead event.
4112 **/
4113static void
4114lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
4115{
4116 struct lpfc_vport **vports;
4117 int i;
4118
4119 vports = lpfc_create_vport_work_array(phba);
4120 if (vports)
4121 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
4122 lpfc_sli4_perform_vport_cvl(vports[i]);
4123 lpfc_destroy_vport_work_array(phba, vports);
4124}
4125
4126/**
James Smart76a95d72010-11-20 23:11:48 -05004127 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
James Smartda0436e2009-05-22 14:51:39 -04004128 * @phba: pointer to lpfc hba data structure.
4129 * @acqe_link: pointer to the async fcoe completion queue entry.
4130 *
4131 * This routine is to handle the SLI4 asynchronous fcoe event.
4132 **/
4133static void
James Smart76a95d72010-11-20 23:11:48 -05004134lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
James Smart70f3c072010-12-15 17:57:33 -05004135 struct lpfc_acqe_fip *acqe_fip)
James Smartda0436e2009-05-22 14:51:39 -04004136{
James Smart70f3c072010-12-15 17:57:33 -05004137 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004138 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04004139 struct lpfc_vport *vport;
4140 struct lpfc_nodelist *ndlp;
4141 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05004142 int active_vlink_present;
4143 struct lpfc_vport **vports;
4144 int i;
James Smartda0436e2009-05-22 14:51:39 -04004145
James Smart70f3c072010-12-15 17:57:33 -05004146 phba->fc_eventTag = acqe_fip->event_tag;
4147 phba->fcoe_eventtag = acqe_fip->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004148 switch (event_type) {
James Smart70f3c072010-12-15 17:57:33 -05004149 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
4150 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
4151 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
James Smart999d8132010-03-15 11:24:56 -04004152 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4153 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004154 "2546 New FCF event, evt_tag:x%x, "
4155 "index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004156 acqe_fip->event_tag,
4157 acqe_fip->index);
James Smart999d8132010-03-15 11:24:56 -04004158 else
4159 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
4160 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004161 "2788 FCF param modified event, "
4162 "evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004163 acqe_fip->event_tag,
4164 acqe_fip->index);
James Smart38b92ef2010-08-04 16:11:39 -04004165 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004166 /*
4167 * During period of FCF discovery, read the FCF
4168 * table record indexed by the event to update
James Smarta93ff372010-10-22 11:06:08 -04004169 * FCF roundrobin failover eligible FCF bmask.
James Smart0c9ab6f2010-02-26 14:15:57 -05004170 */
4171 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4172 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004173 "2779 Read FCF (x%x) for updating "
4174 "roundrobin FCF failover bmask\n",
James Smart70f3c072010-12-15 17:57:33 -05004175 acqe_fip->index);
4176 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004177 }
James Smart38b92ef2010-08-04 16:11:39 -04004178
4179 /* If the FCF discovery is in progress, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04004180 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04004181 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart38b92ef2010-08-04 16:11:39 -04004182 spin_unlock_irq(&phba->hbalock);
4183 break;
4184 }
4185 /* If fast FCF failover rescan event is pending, do nothing */
4186 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
4187 spin_unlock_irq(&phba->hbalock);
4188 break;
4189 }
4190
James Smartc2b97122013-05-31 17:05:36 -04004191 /* If the FCF has been in discovered state, do nothing. */
4192 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
James Smart3804dc82010-07-14 15:31:37 -04004193 spin_unlock_irq(&phba->hbalock);
4194 break;
4195 }
4196 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04004197
James Smart0c9ab6f2010-02-26 14:15:57 -05004198 /* Otherwise, scan the entire FCF table and re-discover SAN */
4199 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004200 "2770 Start FCF table scan per async FCF "
4201 "event, evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004202 acqe_fip->event_tag, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004203 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
4204 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04004205 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05004206 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4207 "2547 Issue FCF scan read FCF mailbox "
James Smarta93ff372010-10-22 11:06:08 -04004208 "command failed (x%x)\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004209 break;
4210
James Smart70f3c072010-12-15 17:57:33 -05004211 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
James Smartda0436e2009-05-22 14:51:39 -04004212 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04004213 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004214 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
4215 acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04004216 break;
4217
James Smart70f3c072010-12-15 17:57:33 -05004218 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
James Smart80c17842012-03-01 22:35:45 -05004219 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05004220 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004221 "2549 FCF (x%x) disconnected from network, "
James Smart70f3c072010-12-15 17:57:33 -05004222 "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart38b92ef2010-08-04 16:11:39 -04004223 /*
4224 * If we are in the middle of FCF failover process, clear
4225 * the corresponding FCF bit in the roundrobin bitmap.
James Smartda0436e2009-05-22 14:51:39 -04004226 */
James Smartfc2b9892010-02-26 14:15:29 -05004227 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004228 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05004229 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004230 /* Update FLOGI FCF failover eligible FCF bmask */
James Smart70f3c072010-12-15 17:57:33 -05004231 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004232 break;
4233 }
James Smart38b92ef2010-08-04 16:11:39 -04004234 spin_unlock_irq(&phba->hbalock);
4235
4236 /* If the event is not for currently used fcf do nothing */
James Smart70f3c072010-12-15 17:57:33 -05004237 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
James Smart38b92ef2010-08-04 16:11:39 -04004238 break;
4239
4240 /*
4241 * Otherwise, request the port to rediscover the entire FCF
4242 * table for a fast recovery from case that the current FCF
4243 * is no longer valid as we are not in the middle of FCF
4244 * failover process already.
4245 */
James Smartc2b97122013-05-31 17:05:36 -04004246 spin_lock_irq(&phba->hbalock);
4247 /* Mark the fast failover process in progress */
4248 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
4249 spin_unlock_irq(&phba->hbalock);
4250
4251 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
4252 "2771 Start FCF fast failover process due to "
4253 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
4254 "\n", acqe_fip->event_tag, acqe_fip->index);
4255 rc = lpfc_sli4_redisc_fcf_table(phba);
4256 if (rc) {
4257 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4258 LOG_DISCOVERY,
4259 "2772 Issue FCF rediscover mabilbox "
4260 "command failed, fail through to FCF "
4261 "dead event\n");
4262 spin_lock_irq(&phba->hbalock);
4263 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
4264 spin_unlock_irq(&phba->hbalock);
4265 /*
4266 * Last resort will fail over by treating this
4267 * as a link down to FCF registration.
4268 */
4269 lpfc_sli4_fcf_dead_failthrough(phba);
4270 } else {
4271 /* Reset FCF roundrobin bmask for new discovery */
4272 lpfc_sli4_clear_fcf_rr_bmask(phba);
4273 /*
4274 * Handling fast FCF failover to a DEAD FCF event is
4275 * considered equalivant to receiving CVL to all vports.
4276 */
4277 lpfc_sli4_perform_all_vport_cvl(phba);
4278 }
James Smartda0436e2009-05-22 14:51:39 -04004279 break;
James Smart70f3c072010-12-15 17:57:33 -05004280 case LPFC_FIP_EVENT_TYPE_CVL:
James Smart80c17842012-03-01 22:35:45 -05004281 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05004282 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6669f9b2009-10-02 15:16:45 -04004283 "2718 Clear Virtual Link Received for VPI 0x%x"
James Smart70f3c072010-12-15 17:57:33 -05004284 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart6d368e52011-05-24 11:44:12 -04004285
James Smart6669f9b2009-10-02 15:16:45 -04004286 vport = lpfc_find_vport_by_vpid(phba,
James Smart5248a742011-07-22 18:37:06 -04004287 acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004288 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004289 if (!ndlp)
4290 break;
James Smart695a8142010-01-26 23:08:03 -05004291 active_vlink_present = 0;
4292
4293 vports = lpfc_create_vport_work_array(phba);
4294 if (vports) {
4295 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
4296 i++) {
4297 if ((!(vports[i]->fc_flag &
4298 FC_VPORT_CVL_RCVD)) &&
4299 (vports[i]->port_state > LPFC_FDISC)) {
4300 active_vlink_present = 1;
4301 break;
4302 }
4303 }
4304 lpfc_destroy_vport_work_array(phba, vports);
4305 }
4306
4307 if (active_vlink_present) {
4308 /*
4309 * If there are other active VLinks present,
4310 * re-instantiate the Vlink using FDISC.
4311 */
James Smart256ec0d2013-04-17 20:14:58 -04004312 mod_timer(&ndlp->nlp_delayfunc,
4313 jiffies + msecs_to_jiffies(1000));
James Smartfc2b9892010-02-26 14:15:29 -05004314 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004315 spin_lock_irq(shost->host_lock);
4316 ndlp->nlp_flag |= NLP_DELAY_TMO;
4317 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05004318 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
4319 vport->port_state = LPFC_FDISC;
4320 } else {
James Smartecfd03c2010-02-12 14:41:27 -05004321 /*
4322 * Otherwise, we request port to rediscover
4323 * the entire FCF table for a fast recovery
4324 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05004325 * is no longer valid if we are not already
4326 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05004327 */
James Smartfc2b9892010-02-26 14:15:29 -05004328 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004329 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05004330 spin_unlock_irq(&phba->hbalock);
4331 break;
4332 }
4333 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05004334 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004335 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004336 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4337 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004338 "2773 Start FCF failover per CVL, "
James Smart70f3c072010-12-15 17:57:33 -05004339 "evt_tag:x%x\n", acqe_fip->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05004340 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05004341 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004342 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4343 LOG_DISCOVERY,
4344 "2774 Issue FCF rediscover "
4345 "mabilbox command failed, "
4346 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05004347 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004348 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004349 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05004350 /*
4351 * Last resort will be re-try on the
4352 * the current registered FCF entry.
4353 */
4354 lpfc_retry_pport_discovery(phba);
James Smart38b92ef2010-08-04 16:11:39 -04004355 } else
4356 /*
4357 * Reset FCF roundrobin bmask for new
4358 * discovery.
4359 */
James Smart7d791df2011-07-22 18:37:52 -04004360 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart6669f9b2009-10-02 15:16:45 -04004361 }
4362 break;
James Smartda0436e2009-05-22 14:51:39 -04004363 default:
4364 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4365 "0288 Unknown FCoE event type 0x%x event tag "
James Smart70f3c072010-12-15 17:57:33 -05004366 "0x%x\n", event_type, acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04004367 break;
4368 }
4369}
4370
4371/**
4372 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
4373 * @phba: pointer to lpfc hba data structure.
4374 * @acqe_link: pointer to the async dcbx completion queue entry.
4375 *
4376 * This routine is to handle the SLI4 asynchronous dcbx event.
4377 **/
4378static void
4379lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
4380 struct lpfc_acqe_dcbx *acqe_dcbx)
4381{
James Smart4d9ab992009-10-02 15:16:39 -04004382 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004383 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4384 "0290 The SLI4 DCBX asynchronous event is not "
4385 "handled yet\n");
4386}
4387
4388/**
James Smartb19a0612010-04-06 14:48:51 -04004389 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
4390 * @phba: pointer to lpfc hba data structure.
4391 * @acqe_link: pointer to the async grp5 completion queue entry.
4392 *
4393 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
4394 * is an asynchronous notified of a logical link speed change. The Port
4395 * reports the logical link speed in units of 10Mbps.
4396 **/
4397static void
4398lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
4399 struct lpfc_acqe_grp5 *acqe_grp5)
4400{
4401 uint16_t prev_ll_spd;
4402
4403 phba->fc_eventTag = acqe_grp5->event_tag;
4404 phba->fcoe_eventtag = acqe_grp5->event_tag;
4405 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
4406 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04004407 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
James Smartb19a0612010-04-06 14:48:51 -04004408 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4409 "2789 GRP5 Async Event: Updating logical link speed "
James Smart8b68cd52012-09-29 11:32:37 -04004410 "from %dMbps to %dMbps\n", prev_ll_spd,
4411 phba->sli4_hba.link_state.logical_speed);
James Smartb19a0612010-04-06 14:48:51 -04004412}
4413
4414/**
James Smartda0436e2009-05-22 14:51:39 -04004415 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
4416 * @phba: pointer to lpfc hba data structure.
4417 *
4418 * This routine is invoked by the worker thread to process all the pending
4419 * SLI4 asynchronous events.
4420 **/
4421void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
4422{
4423 struct lpfc_cq_event *cq_event;
4424
4425 /* First, declare the async event has been handled */
4426 spin_lock_irq(&phba->hbalock);
4427 phba->hba_flag &= ~ASYNC_EVENT;
4428 spin_unlock_irq(&phba->hbalock);
4429 /* Now, handle all the async events */
4430 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
4431 /* Get the first event from the head of the event queue */
4432 spin_lock_irq(&phba->hbalock);
4433 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
4434 cq_event, struct lpfc_cq_event, list);
4435 spin_unlock_irq(&phba->hbalock);
4436 /* Process the asynchronous event */
4437 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
4438 case LPFC_TRAILER_CODE_LINK:
4439 lpfc_sli4_async_link_evt(phba,
4440 &cq_event->cqe.acqe_link);
4441 break;
4442 case LPFC_TRAILER_CODE_FCOE:
James Smart70f3c072010-12-15 17:57:33 -05004443 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004444 break;
4445 case LPFC_TRAILER_CODE_DCBX:
4446 lpfc_sli4_async_dcbx_evt(phba,
4447 &cq_event->cqe.acqe_dcbx);
4448 break;
James Smartb19a0612010-04-06 14:48:51 -04004449 case LPFC_TRAILER_CODE_GRP5:
4450 lpfc_sli4_async_grp5_evt(phba,
4451 &cq_event->cqe.acqe_grp5);
4452 break;
James Smart70f3c072010-12-15 17:57:33 -05004453 case LPFC_TRAILER_CODE_FC:
4454 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
4455 break;
4456 case LPFC_TRAILER_CODE_SLI:
4457 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
4458 break;
James Smartda0436e2009-05-22 14:51:39 -04004459 default:
4460 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4461 "1804 Invalid asynchrous event code: "
4462 "x%x\n", bf_get(lpfc_trailer_code,
4463 &cq_event->cqe.mcqe_cmpl));
4464 break;
4465 }
4466 /* Free the completion event processed to the free pool */
4467 lpfc_sli4_cq_event_release(phba, cq_event);
4468 }
4469}
4470
4471/**
James Smartecfd03c2010-02-12 14:41:27 -05004472 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
4473 * @phba: pointer to lpfc hba data structure.
4474 *
4475 * This routine is invoked by the worker thread to process FCF table
4476 * rediscovery pending completion event.
4477 **/
4478void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
4479{
4480 int rc;
4481
4482 spin_lock_irq(&phba->hbalock);
4483 /* Clear FCF rediscovery timeout event */
4484 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
4485 /* Clear driver fast failover FCF record flag */
4486 phba->fcf.failover_rec.flag = 0;
4487 /* Set state for FCF fast failover */
4488 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
4489 spin_unlock_irq(&phba->hbalock);
4490
4491 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05004492 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004493 "2777 Start post-quiescent FCF table scan\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05004494 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05004495 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05004496 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4497 "2747 Issue FCF scan read FCF mailbox "
4498 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05004499}
4500
4501/**
James Smartda0436e2009-05-22 14:51:39 -04004502 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
4503 * @phba: pointer to lpfc hba data structure.
4504 * @dev_grp: The HBA PCI-Device group number.
4505 *
4506 * This routine is invoked to set up the per HBA PCI-Device group function
4507 * API jump table entries.
4508 *
4509 * Return: 0 if success, otherwise -ENODEV
4510 **/
4511int
4512lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05004513{
4514 int rc;
4515
James Smartda0436e2009-05-22 14:51:39 -04004516 /* Set up lpfc PCI-device group */
4517 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05004518
James Smartda0436e2009-05-22 14:51:39 -04004519 /* The LPFC_PCI_DEV_OC uses SLI4 */
4520 if (dev_grp == LPFC_PCI_DEV_OC)
4521 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05004522
James Smartda0436e2009-05-22 14:51:39 -04004523 /* Set up device INIT API function jump table */
4524 rc = lpfc_init_api_table_setup(phba, dev_grp);
4525 if (rc)
4526 return -ENODEV;
4527 /* Set up SCSI API function jump table */
4528 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
4529 if (rc)
4530 return -ENODEV;
4531 /* Set up SLI API function jump table */
4532 rc = lpfc_sli_api_table_setup(phba, dev_grp);
4533 if (rc)
4534 return -ENODEV;
4535 /* Set up MBOX API function jump table */
4536 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
4537 if (rc)
4538 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004539
James Smartda0436e2009-05-22 14:51:39 -04004540 return 0;
James Smart5b75da22008-12-04 22:39:35 -05004541}
4542
4543/**
James Smart3621a712009-04-06 18:47:14 -04004544 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05004545 * @phba: pointer to lpfc hba data structure.
4546 * @intr_mode: active interrupt mode adopted.
4547 *
4548 * This routine it invoked to log the currently used active interrupt mode
4549 * to the device.
James Smart3772a992009-05-22 14:50:54 -04004550 **/
4551static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05004552{
4553 switch (intr_mode) {
4554 case 0:
4555 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4556 "0470 Enable INTx interrupt mode.\n");
4557 break;
4558 case 1:
4559 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4560 "0481 Enabled MSI interrupt mode.\n");
4561 break;
4562 case 2:
4563 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4564 "0480 Enabled MSI-X interrupt mode.\n");
4565 break;
4566 default:
4567 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4568 "0482 Illegal interrupt mode.\n");
4569 break;
4570 }
4571 return;
4572}
4573
James Smart5b75da22008-12-04 22:39:35 -05004574/**
James Smart3772a992009-05-22 14:50:54 -04004575 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05004576 * @phba: pointer to lpfc hba data structure.
4577 *
James Smart3772a992009-05-22 14:50:54 -04004578 * This routine is invoked to enable the PCI device that is common to all
4579 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004580 *
4581 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004582 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004583 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05004584 **/
James Smart3772a992009-05-22 14:50:54 -04004585static int
4586lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004587{
James Smart3772a992009-05-22 14:50:54 -04004588 struct pci_dev *pdev;
James Smart079b5c92011-08-21 21:48:49 -04004589 int bars = 0;
James Smart5b75da22008-12-04 22:39:35 -05004590
James Smart3772a992009-05-22 14:50:54 -04004591 /* Obtain PCI device reference */
4592 if (!phba->pcidev)
4593 goto out_error;
4594 else
4595 pdev = phba->pcidev;
4596 /* Select PCI BARs */
4597 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4598 /* Enable PCI device */
4599 if (pci_enable_device_mem(pdev))
4600 goto out_error;
4601 /* Request PCI resource for the device */
4602 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
4603 goto out_disable_device;
4604 /* Set up device as PCI master and save state for EEH */
4605 pci_set_master(pdev);
4606 pci_try_set_mwi(pdev);
4607 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05004608
James Smart05580562011-05-24 11:40:48 -04004609 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
Jon Mason453193e2013-09-11 15:38:13 -07004610 if (pci_is_pcie(pdev))
James Smart05580562011-05-24 11:40:48 -04004611 pdev->needs_freset = 1;
4612
James Smart3772a992009-05-22 14:50:54 -04004613 return 0;
James Smart5b75da22008-12-04 22:39:35 -05004614
James Smart3772a992009-05-22 14:50:54 -04004615out_disable_device:
4616 pci_disable_device(pdev);
4617out_error:
James Smart079b5c92011-08-21 21:48:49 -04004618 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4619 "1401 Failed to enable pci device, bars:x%x\n", bars);
James Smart3772a992009-05-22 14:50:54 -04004620 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004621}
4622
4623/**
James Smart3772a992009-05-22 14:50:54 -04004624 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05004625 * @phba: pointer to lpfc hba data structure.
4626 *
James Smart3772a992009-05-22 14:50:54 -04004627 * This routine is invoked to disable the PCI device that is common to all
4628 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004629 **/
4630static void
James Smart3772a992009-05-22 14:50:54 -04004631lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004632{
James Smart3772a992009-05-22 14:50:54 -04004633 struct pci_dev *pdev;
4634 int bars;
James Smart5b75da22008-12-04 22:39:35 -05004635
James Smart3772a992009-05-22 14:50:54 -04004636 /* Obtain PCI device reference */
4637 if (!phba->pcidev)
4638 return;
4639 else
4640 pdev = phba->pcidev;
4641 /* Select PCI BARs */
4642 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4643 /* Release PCI resource and disable PCI device */
4644 pci_release_selected_regions(pdev, bars);
4645 pci_disable_device(pdev);
James Smart5b75da22008-12-04 22:39:35 -05004646
4647 return;
4648}
4649
4650/**
James Smart3772a992009-05-22 14:50:54 -04004651 * lpfc_reset_hba - Reset a hba
4652 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04004653 *
James Smart3772a992009-05-22 14:50:54 -04004654 * This routine is invoked to reset a hba device. It brings the HBA
4655 * offline, performs a board restart, and then brings the board back
4656 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
4657 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04004658 **/
James Smart3772a992009-05-22 14:50:54 -04004659void
4660lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004661{
James Smart3772a992009-05-22 14:50:54 -04004662 /* If resets are disabled then set error state and return. */
4663 if (!phba->cfg_enable_hba_reset) {
4664 phba->link_state = LPFC_HBA_ERROR;
4665 return;
4666 }
James Smart618a5232012-06-12 13:54:36 -04004667 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3772a992009-05-22 14:50:54 -04004668 lpfc_offline(phba);
4669 lpfc_sli_brdrestart(phba);
4670 lpfc_online(phba);
4671 lpfc_unblock_mgmt_io(phba);
4672}
dea31012005-04-17 16:05:31 -05004673
James Smart3772a992009-05-22 14:50:54 -04004674/**
James Smart0a96e972011-07-22 18:37:28 -04004675 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
4676 * @phba: pointer to lpfc hba data structure.
4677 *
4678 * This function enables the PCI SR-IOV virtual functions to a physical
4679 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4680 * enable the number of virtual functions to the physical function. As
4681 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4682 * API call does not considered as an error condition for most of the device.
4683 **/
4684uint16_t
4685lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
4686{
4687 struct pci_dev *pdev = phba->pcidev;
4688 uint16_t nr_virtfn;
4689 int pos;
4690
James Smart0a96e972011-07-22 18:37:28 -04004691 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
4692 if (pos == 0)
4693 return 0;
4694
4695 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
4696 return nr_virtfn;
4697}
4698
4699/**
James Smart912e3ac2011-05-24 11:42:11 -04004700 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
4701 * @phba: pointer to lpfc hba data structure.
4702 * @nr_vfn: number of virtual functions to be enabled.
4703 *
4704 * This function enables the PCI SR-IOV virtual functions to a physical
4705 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4706 * enable the number of virtual functions to the physical function. As
4707 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4708 * API call does not considered as an error condition for most of the device.
4709 **/
4710int
4711lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
4712{
4713 struct pci_dev *pdev = phba->pcidev;
James Smart0a96e972011-07-22 18:37:28 -04004714 uint16_t max_nr_vfn;
James Smart912e3ac2011-05-24 11:42:11 -04004715 int rc;
4716
James Smart0a96e972011-07-22 18:37:28 -04004717 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
4718 if (nr_vfn > max_nr_vfn) {
4719 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4720 "3057 Requested vfs (%d) greater than "
4721 "supported vfs (%d)", nr_vfn, max_nr_vfn);
4722 return -EINVAL;
4723 }
4724
James Smart912e3ac2011-05-24 11:42:11 -04004725 rc = pci_enable_sriov(pdev, nr_vfn);
4726 if (rc) {
4727 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4728 "2806 Failed to enable sriov on this device "
4729 "with vfn number nr_vf:%d, rc:%d\n",
4730 nr_vfn, rc);
4731 } else
4732 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4733 "2807 Successful enable sriov on this device "
4734 "with vfn number nr_vf:%d\n", nr_vfn);
4735 return rc;
4736}
4737
4738/**
James Smart3772a992009-05-22 14:50:54 -04004739 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
4740 * @phba: pointer to lpfc hba data structure.
4741 *
4742 * This routine is invoked to set up the driver internal resources specific to
4743 * support the SLI-3 HBA device it attached to.
4744 *
4745 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004746 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004747 * other values - error
4748 **/
4749static int
4750lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
4751{
4752 struct lpfc_sli *psli;
James Smart912e3ac2011-05-24 11:42:11 -04004753 int rc;
dea31012005-04-17 16:05:31 -05004754
James Smart3772a992009-05-22 14:50:54 -04004755 /*
4756 * Initialize timers used by driver
4757 */
dea31012005-04-17 16:05:31 -05004758
James Smart3772a992009-05-22 14:50:54 -04004759 /* Heartbeat timer */
4760 init_timer(&phba->hb_tmofunc);
4761 phba->hb_tmofunc.function = lpfc_hb_timeout;
4762 phba->hb_tmofunc.data = (unsigned long)phba;
4763
4764 psli = &phba->sli;
4765 /* MBOX heartbeat timer */
4766 init_timer(&psli->mbox_tmo);
4767 psli->mbox_tmo.function = lpfc_mbox_timeout;
4768 psli->mbox_tmo.data = (unsigned long) phba;
4769 /* FCP polling mode timer */
4770 init_timer(&phba->fcp_poll_timer);
4771 phba->fcp_poll_timer.function = lpfc_poll_timeout;
4772 phba->fcp_poll_timer.data = (unsigned long) phba;
4773 /* Fabric block timer */
4774 init_timer(&phba->fabric_block_timer);
4775 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4776 phba->fabric_block_timer.data = (unsigned long) phba;
4777 /* EA polling mode timer */
4778 init_timer(&phba->eratt_poll);
4779 phba->eratt_poll.function = lpfc_poll_eratt;
4780 phba->eratt_poll.data = (unsigned long) phba;
4781
4782 /* Host attention work mask setup */
4783 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
4784 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
4785
4786 /* Get all the module params for configuring this host */
4787 lpfc_get_cfgparam(phba);
James Smart49198b32010-04-06 15:04:33 -04004788 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
4789 phba->menlo_flag |= HBA_MENLO_SUPPORT;
4790 /* check for menlo minimum sg count */
4791 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
4792 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
4793 }
4794
James Smart2a76a282012-08-03 12:35:54 -04004795 if (!phba->sli.ring)
4796 phba->sli.ring = (struct lpfc_sli_ring *)
4797 kzalloc(LPFC_SLI3_MAX_RING *
4798 sizeof(struct lpfc_sli_ring), GFP_KERNEL);
4799 if (!phba->sli.ring)
4800 return -ENOMEM;
4801
James Smart3772a992009-05-22 14:50:54 -04004802 /*
James Smart96f70772013-04-17 20:16:15 -04004803 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smart3772a992009-05-22 14:50:54 -04004804 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart3772a992009-05-22 14:50:54 -04004805 */
James Smart3772a992009-05-22 14:50:54 -04004806
James Smart96f70772013-04-17 20:16:15 -04004807 /* Initialize the host templates the configured values. */
James Smart3772a992009-05-22 14:50:54 -04004808 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4809 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4810
James Smart96f70772013-04-17 20:16:15 -04004811 /* There are going to be 2 reserved BDEs: 1 FCP cmnd + 1 FCP rsp */
4812 if (phba->cfg_enable_bg) {
4813 /*
4814 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
4815 * the FCP rsp, and a BDE for each. Sice we have no control
4816 * over how many protection data segments the SCSI Layer
4817 * will hand us (ie: there could be one for every block
4818 * in the IO), we just allocate enough BDEs to accomidate
4819 * our max amount and we need to limit lpfc_sg_seg_cnt to
4820 * minimize the risk of running out.
4821 */
4822 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4823 sizeof(struct fcp_rsp) +
4824 (LPFC_MAX_SG_SEG_CNT * sizeof(struct ulp_bde64));
4825
4826 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SEG_CNT_DIF)
4827 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT_DIF;
4828
4829 /* Total BDEs in BPL for scsi_sg_list and scsi_sg_prot_list */
4830 phba->cfg_total_seg_cnt = LPFC_MAX_SG_SEG_CNT;
4831 } else {
4832 /*
4833 * The scsi_buf for a regular I/O will hold the FCP cmnd,
4834 * the FCP rsp, a BDE for each, and a BDE for up to
4835 * cfg_sg_seg_cnt data segments.
4836 */
4837 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4838 sizeof(struct fcp_rsp) +
4839 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
4840
4841 /* Total BDEs in BPL for scsi_sg_list */
4842 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
4843 }
4844
4845 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
4846 "9088 sg_tablesize:%d dmabuf_size:%d total_bde:%d\n",
4847 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
4848 phba->cfg_total_seg_cnt);
4849
James Smart3772a992009-05-22 14:50:54 -04004850 phba->max_vpi = LPFC_MAX_VPI;
4851 /* This will be set to correct value after config_port mbox */
4852 phba->max_vports = 0;
4853
4854 /*
4855 * Initialize the SLI Layer to run with lpfc HBAs.
4856 */
4857 lpfc_sli_setup(phba);
4858 lpfc_sli_queue_setup(phba);
4859
4860 /* Allocate device driver memory */
4861 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
4862 return -ENOMEM;
4863
James Smart912e3ac2011-05-24 11:42:11 -04004864 /*
4865 * Enable sr-iov virtual functions if supported and configured
4866 * through the module parameter.
4867 */
4868 if (phba->cfg_sriov_nr_virtfn > 0) {
4869 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
4870 phba->cfg_sriov_nr_virtfn);
4871 if (rc) {
4872 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4873 "2808 Requested number of SR-IOV "
4874 "virtual functions (%d) is not "
4875 "supported\n",
4876 phba->cfg_sriov_nr_virtfn);
4877 phba->cfg_sriov_nr_virtfn = 0;
4878 }
4879 }
4880
James Smart3772a992009-05-22 14:50:54 -04004881 return 0;
4882}
4883
4884/**
4885 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
4886 * @phba: pointer to lpfc hba data structure.
4887 *
4888 * This routine is invoked to unset the driver internal resources set up
4889 * specific for supporting the SLI-3 HBA device it attached to.
4890 **/
4891static void
4892lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
4893{
4894 /* Free device driver memory allocated */
4895 lpfc_mem_free_all(phba);
4896
4897 return;
4898}
4899
4900/**
James Smartda0436e2009-05-22 14:51:39 -04004901 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
4902 * @phba: pointer to lpfc hba data structure.
4903 *
4904 * This routine is invoked to set up the driver internal resources specific to
4905 * support the SLI-4 HBA device it attached to.
4906 *
4907 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004908 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004909 * other values - error
4910 **/
4911static int
4912lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
4913{
James Smart7bb03bb2013-04-17 20:19:16 -04004914 struct lpfc_vector_map_info *cpup;
James Smartda0436e2009-05-22 14:51:39 -04004915 struct lpfc_sli *psli;
James Smart28baac72010-02-12 14:42:03 -05004916 LPFC_MBOXQ_t *mboxq;
James Smart96f70772013-04-17 20:16:15 -04004917 int rc, i, hbq_count, max_buf_size;
James Smart28baac72010-02-12 14:42:03 -05004918 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
4919 struct lpfc_mqe *mqe;
James Smart09294d42013-04-17 20:16:05 -04004920 int longs;
James Smart1ba981f2014-02-20 09:56:45 -05004921 int fof_vectors = 0;
James Smartda0436e2009-05-22 14:51:39 -04004922
James Smart716d3bc2013-09-06 12:18:28 -04004923 /* Get all the module params for configuring this host */
4924 lpfc_get_cfgparam(phba);
4925
James Smartda0436e2009-05-22 14:51:39 -04004926 /* Before proceed, wait for POST done and device ready */
4927 rc = lpfc_sli4_post_status_check(phba);
4928 if (rc)
4929 return -ENODEV;
4930
4931 /*
4932 * Initialize timers used by driver
4933 */
4934
4935 /* Heartbeat timer */
4936 init_timer(&phba->hb_tmofunc);
4937 phba->hb_tmofunc.function = lpfc_hb_timeout;
4938 phba->hb_tmofunc.data = (unsigned long)phba;
James Smart19ca7602010-11-20 23:11:55 -05004939 init_timer(&phba->rrq_tmr);
4940 phba->rrq_tmr.function = lpfc_rrq_timeout;
4941 phba->rrq_tmr.data = (unsigned long)phba;
James Smartda0436e2009-05-22 14:51:39 -04004942
4943 psli = &phba->sli;
4944 /* MBOX heartbeat timer */
4945 init_timer(&psli->mbox_tmo);
4946 psli->mbox_tmo.function = lpfc_mbox_timeout;
4947 psli->mbox_tmo.data = (unsigned long) phba;
4948 /* Fabric block timer */
4949 init_timer(&phba->fabric_block_timer);
4950 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4951 phba->fabric_block_timer.data = (unsigned long) phba;
4952 /* EA polling mode timer */
4953 init_timer(&phba->eratt_poll);
4954 phba->eratt_poll.function = lpfc_poll_eratt;
4955 phba->eratt_poll.data = (unsigned long) phba;
James Smartecfd03c2010-02-12 14:41:27 -05004956 /* FCF rediscover timer */
4957 init_timer(&phba->fcf.redisc_wait);
4958 phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
4959 phba->fcf.redisc_wait.data = (unsigned long)phba;
4960
James Smartda0436e2009-05-22 14:51:39 -04004961 /*
James Smart7ad20aa2011-05-24 11:44:28 -04004962 * Control structure for handling external multi-buffer mailbox
4963 * command pass-through.
4964 */
4965 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
4966 sizeof(struct lpfc_mbox_ext_buf_ctx));
4967 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
4968
James Smartda0436e2009-05-22 14:51:39 -04004969 phba->max_vpi = LPFC_MAX_VPI;
James Smart67d12732012-08-03 12:36:13 -04004970
James Smartda0436e2009-05-22 14:51:39 -04004971 /* This will be set to correct value after the read_config mbox */
4972 phba->max_vports = 0;
4973
4974 /* Program the default value of vlan_id and fc_map */
4975 phba->valid_vlan = 0;
4976 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4977 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4978 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4979
4980 /*
James Smart2a76a282012-08-03 12:35:54 -04004981 * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
4982 * we will associate a new ring, for each FCP fastpath EQ/CQ/WQ tuple.
4983 */
4984 if (!phba->sli.ring)
4985 phba->sli.ring = kzalloc(
James Smart67d12732012-08-03 12:36:13 -04004986 (LPFC_SLI3_MAX_RING + phba->cfg_fcp_io_channel) *
James Smart2a76a282012-08-03 12:35:54 -04004987 sizeof(struct lpfc_sli_ring), GFP_KERNEL);
4988 if (!phba->sli.ring)
4989 return -ENOMEM;
James Smart085c6472010-11-20 23:11:37 -05004990
James Smart09294d42013-04-17 20:16:05 -04004991 /*
4992 * It doesn't matter what family our adapter is in, we are
4993 * limited to 2 Pages, 512 SGEs, for our SGL.
4994 * There are going to be 2 reserved SGEs: 1 FCP cmnd + 1 FCP rsp
4995 */
4996 max_buf_size = (2 * SLI4_PAGE_SIZE);
4997 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SGL_SEG_CNT - 2)
4998 phba->cfg_sg_seg_cnt = LPFC_MAX_SGL_SEG_CNT - 2;
James Smart09294d42013-04-17 20:16:05 -04004999
James Smartda0436e2009-05-22 14:51:39 -04005000 /*
James Smart96f70772013-04-17 20:16:15 -04005001 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smartda0436e2009-05-22 14:51:39 -04005002 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart28baac72010-02-12 14:42:03 -05005003 */
James Smart085c6472010-11-20 23:11:37 -05005004
James Smart96f70772013-04-17 20:16:15 -04005005 if (phba->cfg_enable_bg) {
5006 /*
5007 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
5008 * the FCP rsp, and a SGE for each. Sice we have no control
5009 * over how many protection data segments the SCSI Layer
5010 * will hand us (ie: there could be one for every block
5011 * in the IO), we just allocate enough SGEs to accomidate
5012 * our max amount and we need to limit lpfc_sg_seg_cnt to
5013 * minimize the risk of running out.
5014 */
5015 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5016 sizeof(struct fcp_rsp) + max_buf_size;
5017
5018 /* Total SGEs for scsi_sg_list and scsi_sg_prot_list */
5019 phba->cfg_total_seg_cnt = LPFC_MAX_SGL_SEG_CNT;
5020
5021 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SLI4_SEG_CNT_DIF)
5022 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SLI4_SEG_CNT_DIF;
5023 } else {
5024 /*
5025 * The scsi_buf for a regular I/O will hold the FCP cmnd,
5026 * the FCP rsp, a SGE for each, and a SGE for up to
5027 * cfg_sg_seg_cnt data segments.
5028 */
5029 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5030 sizeof(struct fcp_rsp) +
5031 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge));
5032
5033 /* Total SGEs for scsi_sg_list */
5034 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
5035 /*
5036 * NOTE: if (phba->cfg_sg_seg_cnt + 2) <= 256 we only need
5037 * to post 1 page for the SGL.
5038 */
James Smart085c6472010-11-20 23:11:37 -05005039 }
James Smartacd68592012-01-18 16:25:09 -05005040
James Smart96f70772013-04-17 20:16:15 -04005041 /* Initialize the host templates with the updated values. */
5042 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5043 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5044
5045 if (phba->cfg_sg_dma_buf_size <= LPFC_MIN_SG_SLI4_BUF_SZ)
5046 phba->cfg_sg_dma_buf_size = LPFC_MIN_SG_SLI4_BUF_SZ;
5047 else
5048 phba->cfg_sg_dma_buf_size =
5049 SLI4_PAGE_ALIGN(phba->cfg_sg_dma_buf_size);
5050
5051 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
5052 "9087 sg_tablesize:%d dmabuf_size:%d total_sge:%d\n",
5053 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
5054 phba->cfg_total_seg_cnt);
James Smartda0436e2009-05-22 14:51:39 -04005055
5056 /* Initialize buffer queue management fields */
5057 hbq_count = lpfc_sli_hbq_count();
5058 for (i = 0; i < hbq_count; ++i)
5059 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
5060 INIT_LIST_HEAD(&phba->rb_pend_list);
5061 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
5062 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
5063
5064 /*
5065 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
5066 */
5067 /* Initialize the Abort scsi buffer list used by driver */
5068 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
5069 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
5070 /* This abort list used by worker thread */
5071 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
5072
5073 /*
James Smart6d368e52011-05-24 11:44:12 -04005074 * Initialize driver internal slow-path work queues
James Smartda0436e2009-05-22 14:51:39 -04005075 */
5076
5077 /* Driver internel slow-path CQ Event pool */
5078 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
5079 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04005080 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04005081 /* Asynchronous event CQ Event work queue list */
5082 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
5083 /* Fast-path XRI aborted CQ Event work queue list */
5084 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
5085 /* Slow-path XRI aborted CQ Event work queue list */
5086 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
5087 /* Receive queue CQ Event work queue list */
5088 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
5089
James Smart6d368e52011-05-24 11:44:12 -04005090 /* Initialize extent block lists. */
5091 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
5092 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
5093 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
5094 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
5095
James Smartda0436e2009-05-22 14:51:39 -04005096 /* Initialize the driver internal SLI layer lists. */
5097 lpfc_sli_setup(phba);
5098 lpfc_sli_queue_setup(phba);
5099
5100 /* Allocate device driver memory */
5101 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
5102 if (rc)
5103 return -ENOMEM;
5104
James Smart2fcee4b2010-12-15 17:57:46 -05005105 /* IF Type 2 ports get initialized now. */
5106 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
5107 LPFC_SLI_INTF_IF_TYPE_2) {
5108 rc = lpfc_pci_function_reset(phba);
5109 if (unlikely(rc))
5110 return -ENODEV;
5111 }
5112
James Smartda0436e2009-05-22 14:51:39 -04005113 /* Create the bootstrap mailbox command */
5114 rc = lpfc_create_bootstrap_mbox(phba);
5115 if (unlikely(rc))
5116 goto out_free_mem;
5117
5118 /* Set up the host's endian order with the device. */
5119 rc = lpfc_setup_endian_order(phba);
5120 if (unlikely(rc))
5121 goto out_free_bsmbx;
5122
5123 /* Set up the hba's configuration parameters. */
5124 rc = lpfc_sli4_read_config(phba);
5125 if (unlikely(rc))
5126 goto out_free_bsmbx;
James Smartcff261f2013-12-17 20:29:47 -05005127 rc = lpfc_mem_alloc_active_rrq_pool_s4(phba);
5128 if (unlikely(rc))
5129 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04005130
James Smart2fcee4b2010-12-15 17:57:46 -05005131 /* IF Type 0 ports get initialized now. */
5132 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
5133 LPFC_SLI_INTF_IF_TYPE_0) {
5134 rc = lpfc_pci_function_reset(phba);
5135 if (unlikely(rc))
5136 goto out_free_bsmbx;
5137 }
James Smartda0436e2009-05-22 14:51:39 -04005138
James Smartcb5172e2010-03-15 11:25:07 -04005139 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
5140 GFP_KERNEL);
5141 if (!mboxq) {
5142 rc = -ENOMEM;
5143 goto out_free_bsmbx;
5144 }
5145
James Smartfedd3b72011-02-16 12:39:24 -05005146 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
James Smartcb5172e2010-03-15 11:25:07 -04005147 lpfc_supported_pages(mboxq);
5148 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smartfedd3b72011-02-16 12:39:24 -05005149 if (!rc) {
5150 mqe = &mboxq->u.mqe;
5151 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
5152 LPFC_MAX_SUPPORTED_PAGES);
5153 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
5154 switch (pn_page[i]) {
5155 case LPFC_SLI4_PARAMETERS:
5156 phba->sli4_hba.pc_sli4_params.supported = 1;
5157 break;
5158 default:
5159 break;
5160 }
5161 }
5162 /* Read the port's SLI4 Parameters capabilities if supported. */
5163 if (phba->sli4_hba.pc_sli4_params.supported)
5164 rc = lpfc_pc_sli4_params_get(phba, mboxq);
5165 if (rc) {
5166 mempool_free(mboxq, phba->mbox_mem_pool);
5167 rc = -EIO;
5168 goto out_free_bsmbx;
James Smartcb5172e2010-03-15 11:25:07 -04005169 }
5170 }
James Smartfedd3b72011-02-16 12:39:24 -05005171 /*
5172 * Get sli4 parameters that override parameters from Port capabilities.
James Smart6d368e52011-05-24 11:44:12 -04005173 * If this call fails, it isn't critical unless the SLI4 parameters come
5174 * back in conflict.
James Smartfedd3b72011-02-16 12:39:24 -05005175 */
James Smart6d368e52011-05-24 11:44:12 -04005176 rc = lpfc_get_sli4_parameters(phba, mboxq);
5177 if (rc) {
5178 if (phba->sli4_hba.extents_in_use &&
5179 phba->sli4_hba.rpi_hdrs_in_use) {
5180 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5181 "2999 Unsupported SLI4 Parameters "
5182 "Extents and RPI headers enabled.\n");
5183 goto out_free_bsmbx;
5184 }
5185 }
James Smartcb5172e2010-03-15 11:25:07 -04005186 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1ba981f2014-02-20 09:56:45 -05005187
5188 /* Verify OAS is supported */
5189 lpfc_sli4_oas_verify(phba);
5190 if (phba->cfg_fof)
5191 fof_vectors = 1;
5192
James Smart5350d872011-10-10 21:33:49 -04005193 /* Verify all the SLI4 queues */
5194 rc = lpfc_sli4_queue_verify(phba);
James Smartda0436e2009-05-22 14:51:39 -04005195 if (rc)
5196 goto out_free_bsmbx;
5197
5198 /* Create driver internal CQE event pool */
5199 rc = lpfc_sli4_cq_event_pool_create(phba);
5200 if (rc)
James Smart5350d872011-10-10 21:33:49 -04005201 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04005202
James Smart8a9d2e82012-05-09 21:16:12 -04005203 /* Initialize sgl lists per host */
5204 lpfc_init_sgl_list(phba);
5205
5206 /* Allocate and initialize active sgl array */
James Smartda0436e2009-05-22 14:51:39 -04005207 rc = lpfc_init_active_sgl_array(phba);
5208 if (rc) {
5209 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5210 "1430 Failed to initialize sgl list.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04005211 goto out_destroy_cq_event_pool;
James Smartda0436e2009-05-22 14:51:39 -04005212 }
James Smartda0436e2009-05-22 14:51:39 -04005213 rc = lpfc_sli4_init_rpi_hdrs(phba);
5214 if (rc) {
5215 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5216 "1432 Failed to initialize rpi headers.\n");
5217 goto out_free_active_sgl;
5218 }
5219
James Smarta93ff372010-10-22 11:06:08 -04005220 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
James Smart0c9ab6f2010-02-26 14:15:57 -05005221 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
5222 phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
5223 GFP_KERNEL);
5224 if (!phba->fcf.fcf_rr_bmask) {
5225 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5226 "2759 Failed allocate memory for FCF round "
5227 "robin failover bmask\n");
James Smart05580562011-05-24 11:40:48 -04005228 rc = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -05005229 goto out_remove_rpi_hdrs;
5230 }
5231
James Smart67d12732012-08-03 12:36:13 -04005232 phba->sli4_hba.fcp_eq_hdl =
5233 kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
James Smart1ba981f2014-02-20 09:56:45 -05005234 (fof_vectors + phba->cfg_fcp_io_channel)),
5235 GFP_KERNEL);
James Smart67d12732012-08-03 12:36:13 -04005236 if (!phba->sli4_hba.fcp_eq_hdl) {
5237 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5238 "2572 Failed allocate memory for "
5239 "fast-path per-EQ handle array\n");
5240 rc = -ENOMEM;
5241 goto out_free_fcf_rr_bmask;
James Smartda0436e2009-05-22 14:51:39 -04005242 }
5243
5244 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
James Smart1ba981f2014-02-20 09:56:45 -05005245 (fof_vectors +
5246 phba->cfg_fcp_io_channel)), GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005247 if (!phba->sli4_hba.msix_entries) {
5248 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5249 "2573 Failed allocate memory for msi-x "
5250 "interrupt vector entries\n");
James Smart05580562011-05-24 11:40:48 -04005251 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04005252 goto out_free_fcp_eq_hdl;
5253 }
5254
James Smart7bb03bb2013-04-17 20:19:16 -04005255 phba->sli4_hba.cpu_map = kzalloc((sizeof(struct lpfc_vector_map_info) *
5256 phba->sli4_hba.num_present_cpu),
5257 GFP_KERNEL);
5258 if (!phba->sli4_hba.cpu_map) {
5259 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5260 "3327 Failed allocate memory for msi-x "
5261 "interrupt vector mapping\n");
5262 rc = -ENOMEM;
5263 goto out_free_msix;
5264 }
James Smartb246de12013-05-31 17:03:07 -04005265 if (lpfc_used_cpu == NULL) {
5266 lpfc_used_cpu = kzalloc((sizeof(uint16_t) * lpfc_present_cpu),
5267 GFP_KERNEL);
5268 if (!lpfc_used_cpu) {
5269 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5270 "3335 Failed allocate memory for msi-x "
5271 "interrupt vector mapping\n");
5272 kfree(phba->sli4_hba.cpu_map);
5273 rc = -ENOMEM;
5274 goto out_free_msix;
5275 }
5276 for (i = 0; i < lpfc_present_cpu; i++)
5277 lpfc_used_cpu[i] = LPFC_VECTOR_MAP_EMPTY;
5278 }
5279
James Smart7bb03bb2013-04-17 20:19:16 -04005280 /* Initialize io channels for round robin */
5281 cpup = phba->sli4_hba.cpu_map;
5282 rc = 0;
5283 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
5284 cpup->channel_id = rc;
5285 rc++;
5286 if (rc >= phba->cfg_fcp_io_channel)
5287 rc = 0;
5288 }
5289
James Smart912e3ac2011-05-24 11:42:11 -04005290 /*
5291 * Enable sr-iov virtual functions if supported and configured
5292 * through the module parameter.
5293 */
5294 if (phba->cfg_sriov_nr_virtfn > 0) {
5295 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
5296 phba->cfg_sriov_nr_virtfn);
5297 if (rc) {
5298 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5299 "3020 Requested number of SR-IOV "
5300 "virtual functions (%d) is not "
5301 "supported\n",
5302 phba->cfg_sriov_nr_virtfn);
5303 phba->cfg_sriov_nr_virtfn = 0;
5304 }
5305 }
5306
James Smart5248a742011-07-22 18:37:06 -04005307 return 0;
James Smartda0436e2009-05-22 14:51:39 -04005308
James Smart7bb03bb2013-04-17 20:19:16 -04005309out_free_msix:
5310 kfree(phba->sli4_hba.msix_entries);
James Smartda0436e2009-05-22 14:51:39 -04005311out_free_fcp_eq_hdl:
5312 kfree(phba->sli4_hba.fcp_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05005313out_free_fcf_rr_bmask:
5314 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04005315out_remove_rpi_hdrs:
5316 lpfc_sli4_remove_rpi_hdrs(phba);
5317out_free_active_sgl:
5318 lpfc_free_active_sgl(phba);
James Smartda0436e2009-05-22 14:51:39 -04005319out_destroy_cq_event_pool:
5320 lpfc_sli4_cq_event_pool_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04005321out_free_bsmbx:
5322 lpfc_destroy_bootstrap_mbox(phba);
5323out_free_mem:
5324 lpfc_mem_free(phba);
5325 return rc;
5326}
5327
5328/**
5329 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
5330 * @phba: pointer to lpfc hba data structure.
5331 *
5332 * This routine is invoked to unset the driver internal resources set up
5333 * specific for supporting the SLI-4 HBA device it attached to.
5334 **/
5335static void
5336lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
5337{
5338 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
5339
James Smart7bb03bb2013-04-17 20:19:16 -04005340 /* Free memory allocated for msi-x interrupt vector to CPU mapping */
5341 kfree(phba->sli4_hba.cpu_map);
5342 phba->sli4_hba.num_present_cpu = 0;
5343 phba->sli4_hba.num_online_cpu = 0;
James Smart76fd07a2014-02-20 09:57:18 -05005344 phba->sli4_hba.curr_disp_cpu = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04005345
James Smartda0436e2009-05-22 14:51:39 -04005346 /* Free memory allocated for msi-x interrupt vector entries */
5347 kfree(phba->sli4_hba.msix_entries);
5348
5349 /* Free memory allocated for fast-path work queue handles */
5350 kfree(phba->sli4_hba.fcp_eq_hdl);
5351
5352 /* Free the allocated rpi headers. */
5353 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04005354 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04005355
James Smart0c9ab6f2010-02-26 14:15:57 -05005356 /* Free eligible FCF index bmask */
5357 kfree(phba->fcf.fcf_rr_bmask);
5358
James Smartda0436e2009-05-22 14:51:39 -04005359 /* Free the ELS sgl list */
5360 lpfc_free_active_sgl(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04005361 lpfc_free_els_sgl_list(phba);
James Smartda0436e2009-05-22 14:51:39 -04005362
James Smartda0436e2009-05-22 14:51:39 -04005363 /* Free the completion queue EQ event pool */
5364 lpfc_sli4_cq_event_release_all(phba);
5365 lpfc_sli4_cq_event_pool_destroy(phba);
5366
James Smart6d368e52011-05-24 11:44:12 -04005367 /* Release resource identifiers. */
5368 lpfc_sli4_dealloc_resource_identifiers(phba);
5369
James Smartda0436e2009-05-22 14:51:39 -04005370 /* Free the bsmbx region. */
5371 lpfc_destroy_bootstrap_mbox(phba);
5372
5373 /* Free the SLI Layer memory with SLI4 HBAs */
5374 lpfc_mem_free_all(phba);
5375
5376 /* Free the current connect table */
5377 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04005378 &phba->fcf_conn_rec_list, list) {
5379 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04005380 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04005381 }
James Smartda0436e2009-05-22 14:51:39 -04005382
5383 return;
5384}
5385
5386/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005387 * lpfc_init_api_table_setup - Set up init api function jump table
James Smart3772a992009-05-22 14:50:54 -04005388 * @phba: The hba struct for which this call is being executed.
5389 * @dev_grp: The HBA PCI-Device group number.
5390 *
5391 * This routine sets up the device INIT interface API function jump table
5392 * in @phba struct.
5393 *
5394 * Returns: 0 - success, -ENODEV - failure.
5395 **/
5396int
5397lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5398{
James Smart84d1b002010-02-12 14:42:33 -05005399 phba->lpfc_hba_init_link = lpfc_hba_init_link;
5400 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart7f860592011-03-11 16:05:52 -05005401 phba->lpfc_selective_reset = lpfc_selective_reset;
James Smart3772a992009-05-22 14:50:54 -04005402 switch (dev_grp) {
5403 case LPFC_PCI_DEV_LP:
5404 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
5405 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
5406 phba->lpfc_stop_port = lpfc_stop_port_s3;
5407 break;
James Smartda0436e2009-05-22 14:51:39 -04005408 case LPFC_PCI_DEV_OC:
5409 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
5410 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
5411 phba->lpfc_stop_port = lpfc_stop_port_s4;
5412 break;
James Smart3772a992009-05-22 14:50:54 -04005413 default:
5414 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5415 "1431 Invalid HBA PCI-device group: 0x%x\n",
5416 dev_grp);
5417 return -ENODEV;
5418 break;
5419 }
5420 return 0;
5421}
5422
5423/**
5424 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
5425 * @phba: pointer to lpfc hba data structure.
5426 *
5427 * This routine is invoked to set up the driver internal resources before the
5428 * device specific resource setup to support the HBA device it attached to.
5429 *
5430 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005431 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005432 * other values - error
5433 **/
5434static int
5435lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
5436{
5437 /*
5438 * Driver resources common to all SLI revisions
5439 */
James Smartea2151b2008-09-07 11:52:10 -04005440 atomic_set(&phba->fast_event_count, 0);
James Smart2e0fef82007-06-17 19:56:36 -05005441 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005442
James Smarte47c9092008-02-08 18:49:26 -05005443 /* Initialize ndlp management spinlock */
5444 spin_lock_init(&phba->ndlp_lock);
5445
James Smart2e0fef82007-06-17 19:56:36 -05005446 INIT_LIST_HEAD(&phba->port_list);
James Smart3772a992009-05-22 14:50:54 -04005447 INIT_LIST_HEAD(&phba->work_list);
James Smart84774a42008-08-24 21:50:06 -04005448 init_waitqueue_head(&phba->wait_4_mlo_m_q);
dea31012005-04-17 16:05:31 -05005449
James Smart3772a992009-05-22 14:50:54 -04005450 /* Initialize the wait queue head for the kernel thread */
5451 init_waitqueue_head(&phba->work_waitq);
James Smart858c9f62007-06-17 19:56:39 -05005452
James Smart3772a992009-05-22 14:50:54 -04005453 /* Initialize the scsi buffer list used by driver for scsi IO */
James Smarta40fc5f2013-04-17 20:17:40 -04005454 spin_lock_init(&phba->scsi_buf_list_get_lock);
5455 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_get);
5456 spin_lock_init(&phba->scsi_buf_list_put_lock);
5457 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
dea31012005-04-17 16:05:31 -05005458
James Smart3772a992009-05-22 14:50:54 -04005459 /* Initialize the fabric iocb list */
5460 INIT_LIST_HEAD(&phba->fabric_iocb_list);
dea31012005-04-17 16:05:31 -05005461
James Smart3772a992009-05-22 14:50:54 -04005462 /* Initialize list to save ELS buffers */
5463 INIT_LIST_HEAD(&phba->elsbuf);
dea31012005-04-17 16:05:31 -05005464
James Smart3772a992009-05-22 14:50:54 -04005465 /* Initialize FCF connection rec list */
5466 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
dea31012005-04-17 16:05:31 -05005467
James Smart1ba981f2014-02-20 09:56:45 -05005468 /* Initialize OAS configuration list */
5469 spin_lock_init(&phba->devicelock);
5470 INIT_LIST_HEAD(&phba->luns);
5471
James Smart3772a992009-05-22 14:50:54 -04005472 return 0;
5473}
dea31012005-04-17 16:05:31 -05005474
James Smart3772a992009-05-22 14:50:54 -04005475/**
5476 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
5477 * @phba: pointer to lpfc hba data structure.
5478 *
5479 * This routine is invoked to set up the driver internal resources after the
5480 * device specific resource setup to support the HBA device it attached to.
5481 *
5482 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005483 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005484 * other values - error
5485 **/
5486static int
5487lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
5488{
5489 int error;
5490
5491 /* Startup the kernel thread for this host adapter. */
5492 phba->worker_thread = kthread_run(lpfc_do_work, phba,
5493 "lpfc_worker_%d", phba->brd_no);
5494 if (IS_ERR(phba->worker_thread)) {
5495 error = PTR_ERR(phba->worker_thread);
5496 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005497 }
5498
James Smart3772a992009-05-22 14:50:54 -04005499 return 0;
5500}
5501
5502/**
5503 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
5504 * @phba: pointer to lpfc hba data structure.
5505 *
5506 * This routine is invoked to unset the driver internal resources set up after
5507 * the device specific resource setup for supporting the HBA device it
5508 * attached to.
5509 **/
5510static void
5511lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
5512{
5513 /* Stop kernel worker thread */
5514 kthread_stop(phba->worker_thread);
5515}
5516
5517/**
5518 * lpfc_free_iocb_list - Free iocb list.
5519 * @phba: pointer to lpfc hba data structure.
5520 *
5521 * This routine is invoked to free the driver's IOCB list and memory.
5522 **/
5523static void
5524lpfc_free_iocb_list(struct lpfc_hba *phba)
5525{
5526 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
5527
5528 spin_lock_irq(&phba->hbalock);
5529 list_for_each_entry_safe(iocbq_entry, iocbq_next,
5530 &phba->lpfc_iocb_list, list) {
5531 list_del(&iocbq_entry->list);
5532 kfree(iocbq_entry);
5533 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005534 }
James Smart3772a992009-05-22 14:50:54 -04005535 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005536
James Smart3772a992009-05-22 14:50:54 -04005537 return;
5538}
dea31012005-04-17 16:05:31 -05005539
James Smart3772a992009-05-22 14:50:54 -04005540/**
5541 * lpfc_init_iocb_list - Allocate and initialize iocb list.
5542 * @phba: pointer to lpfc hba data structure.
5543 *
5544 * This routine is invoked to allocate and initizlize the driver's IOCB
5545 * list and set up the IOCB tag array accordingly.
5546 *
5547 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005548 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005549 * other values - error
5550 **/
5551static int
5552lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
5553{
5554 struct lpfc_iocbq *iocbq_entry = NULL;
5555 uint16_t iotag;
5556 int i;
dea31012005-04-17 16:05:31 -05005557
5558 /* Initialize and populate the iocb list per host. */
5559 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04005560 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07005561 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05005562 if (iocbq_entry == NULL) {
5563 printk(KERN_ERR "%s: only allocated %d iocbs of "
5564 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07005565 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05005566 goto out_free_iocbq;
5567 }
5568
James Bottomley604a3e32005-10-29 10:28:33 -05005569 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
5570 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04005571 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05005572 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04005573 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05005574 goto out_free_iocbq;
5575 }
James Smart6d368e52011-05-24 11:44:12 -04005576 iocbq_entry->sli4_lxritag = NO_XRI;
James Smart3772a992009-05-22 14:50:54 -04005577 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05005578
5579 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005580 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
5581 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05005582 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005583 }
5584
James Smart3772a992009-05-22 14:50:54 -04005585 return 0;
5586
5587out_free_iocbq:
5588 lpfc_free_iocb_list(phba);
5589
5590 return -ENOMEM;
5591}
5592
5593/**
James Smart8a9d2e82012-05-09 21:16:12 -04005594 * lpfc_free_sgl_list - Free a given sgl list.
James Smartda0436e2009-05-22 14:51:39 -04005595 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -04005596 * @sglq_list: pointer to the head of sgl list.
James Smartda0436e2009-05-22 14:51:39 -04005597 *
James Smart8a9d2e82012-05-09 21:16:12 -04005598 * This routine is invoked to free a give sgl list and memory.
James Smartda0436e2009-05-22 14:51:39 -04005599 **/
James Smart8a9d2e82012-05-09 21:16:12 -04005600void
5601lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
James Smartda0436e2009-05-22 14:51:39 -04005602{
5603 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart8a9d2e82012-05-09 21:16:12 -04005604
5605 list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
5606 list_del(&sglq_entry->list);
5607 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
5608 kfree(sglq_entry);
5609 }
5610}
5611
5612/**
5613 * lpfc_free_els_sgl_list - Free els sgl list.
5614 * @phba: pointer to lpfc hba data structure.
5615 *
5616 * This routine is invoked to free the driver's els sgl list and memory.
5617 **/
5618static void
5619lpfc_free_els_sgl_list(struct lpfc_hba *phba)
5620{
James Smartda0436e2009-05-22 14:51:39 -04005621 LIST_HEAD(sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04005622
James Smart8a9d2e82012-05-09 21:16:12 -04005623 /* Retrieve all els sgls from driver list */
James Smartda0436e2009-05-22 14:51:39 -04005624 spin_lock_irq(&phba->hbalock);
5625 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
5626 spin_unlock_irq(&phba->hbalock);
5627
James Smart8a9d2e82012-05-09 21:16:12 -04005628 /* Now free the sgl list */
5629 lpfc_free_sgl_list(phba, &sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04005630}
5631
5632/**
5633 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
5634 * @phba: pointer to lpfc hba data structure.
5635 *
5636 * This routine is invoked to allocate the driver's active sgl memory.
5637 * This array will hold the sglq_entry's for active IOs.
5638 **/
5639static int
5640lpfc_init_active_sgl_array(struct lpfc_hba *phba)
5641{
5642 int size;
5643 size = sizeof(struct lpfc_sglq *);
5644 size *= phba->sli4_hba.max_cfg_param.max_xri;
5645
5646 phba->sli4_hba.lpfc_sglq_active_list =
5647 kzalloc(size, GFP_KERNEL);
5648 if (!phba->sli4_hba.lpfc_sglq_active_list)
5649 return -ENOMEM;
5650 return 0;
5651}
5652
5653/**
5654 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
5655 * @phba: pointer to lpfc hba data structure.
5656 *
5657 * This routine is invoked to walk through the array of active sglq entries
5658 * and free all of the resources.
5659 * This is just a place holder for now.
5660 **/
5661static void
5662lpfc_free_active_sgl(struct lpfc_hba *phba)
5663{
5664 kfree(phba->sli4_hba.lpfc_sglq_active_list);
5665}
5666
5667/**
5668 * lpfc_init_sgl_list - Allocate and initialize sgl list.
5669 * @phba: pointer to lpfc hba data structure.
5670 *
5671 * This routine is invoked to allocate and initizlize the driver's sgl
5672 * list and set up the sgl xritag tag array accordingly.
5673 *
James Smartda0436e2009-05-22 14:51:39 -04005674 **/
James Smart8a9d2e82012-05-09 21:16:12 -04005675static void
James Smartda0436e2009-05-22 14:51:39 -04005676lpfc_init_sgl_list(struct lpfc_hba *phba)
5677{
James Smartda0436e2009-05-22 14:51:39 -04005678 /* Initialize and populate the sglq list per host/VF. */
5679 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
5680 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
5681
James Smart8a9d2e82012-05-09 21:16:12 -04005682 /* els xri-sgl book keeping */
5683 phba->sli4_hba.els_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04005684
James Smart8a9d2e82012-05-09 21:16:12 -04005685 /* scsi xri-buffer book keeping */
James Smartda0436e2009-05-22 14:51:39 -04005686 phba->sli4_hba.scsi_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04005687}
5688
5689/**
5690 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
5691 * @phba: pointer to lpfc hba data structure.
5692 *
5693 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -04005694 * port for those SLI4 ports that do not support extents. This routine
James Smartda0436e2009-05-22 14:51:39 -04005695 * posts a PAGE_SIZE memory region to the port to hold up to
James Smart88a2cfb2011-07-22 18:36:33 -04005696 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
5697 * and should be called only when interrupts are disabled.
James Smartda0436e2009-05-22 14:51:39 -04005698 *
5699 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005700 * 0 - successful
James Smart88a2cfb2011-07-22 18:36:33 -04005701 * -ERROR - otherwise.
James Smartda0436e2009-05-22 14:51:39 -04005702 **/
5703int
5704lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
5705{
5706 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04005707 struct lpfc_rpi_hdr *rpi_hdr;
5708
5709 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
James Smartff78d8f2011-12-13 13:21:35 -05005710 if (!phba->sli4_hba.rpi_hdrs_in_use)
James Smart6d368e52011-05-24 11:44:12 -04005711 return rc;
James Smart6d368e52011-05-24 11:44:12 -04005712 if (phba->sli4_hba.extents_in_use)
5713 return -EIO;
James Smartda0436e2009-05-22 14:51:39 -04005714
5715 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
5716 if (!rpi_hdr) {
5717 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5718 "0391 Error during rpi post operation\n");
5719 lpfc_sli4_remove_rpis(phba);
5720 rc = -ENODEV;
5721 }
5722
5723 return rc;
5724}
5725
5726/**
5727 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
5728 * @phba: pointer to lpfc hba data structure.
5729 *
5730 * This routine is invoked to allocate a single 4KB memory region to
5731 * support rpis and stores them in the phba. This single region
5732 * provides support for up to 64 rpis. The region is used globally
5733 * by the device.
5734 *
5735 * Returns:
5736 * A valid rpi hdr on success.
5737 * A NULL pointer on any failure.
5738 **/
5739struct lpfc_rpi_hdr *
5740lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
5741{
5742 uint16_t rpi_limit, curr_rpi_range;
5743 struct lpfc_dmabuf *dmabuf;
5744 struct lpfc_rpi_hdr *rpi_hdr;
James Smart9589b0622011-04-16 11:03:17 -04005745 uint32_t rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04005746
James Smart6d368e52011-05-24 11:44:12 -04005747 /*
5748 * If the SLI4 port supports extents, posting the rpi header isn't
5749 * required. Set the expected maximum count and let the actual value
5750 * get set when extents are fully allocated.
5751 */
5752 if (!phba->sli4_hba.rpi_hdrs_in_use)
5753 return NULL;
5754 if (phba->sli4_hba.extents_in_use)
5755 return NULL;
5756
5757 /* The limit on the logical index is just the max_rpi count. */
James Smartda0436e2009-05-22 14:51:39 -04005758 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
James Smart6d368e52011-05-24 11:44:12 -04005759 phba->sli4_hba.max_cfg_param.max_rpi - 1;
James Smartda0436e2009-05-22 14:51:39 -04005760
5761 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04005762 /*
5763 * Establish the starting RPI in this header block. The starting
5764 * rpi is normalized to a zero base because the physical rpi is
5765 * port based.
5766 */
James Smart97f2ecf2012-03-01 22:35:23 -05005767 curr_rpi_range = phba->sli4_hba.next_rpi;
James Smartda0436e2009-05-22 14:51:39 -04005768 spin_unlock_irq(&phba->hbalock);
5769
5770 /*
5771 * The port has a limited number of rpis. The increment here
5772 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
5773 * and to allow the full max_rpi range per port.
5774 */
5775 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
James Smart9589b0622011-04-16 11:03:17 -04005776 rpi_count = rpi_limit - curr_rpi_range;
5777 else
5778 rpi_count = LPFC_RPI_HDR_COUNT;
James Smartda0436e2009-05-22 14:51:39 -04005779
James Smart6d368e52011-05-24 11:44:12 -04005780 if (!rpi_count)
5781 return NULL;
James Smartda0436e2009-05-22 14:51:39 -04005782 /*
5783 * First allocate the protocol header region for the port. The
5784 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
5785 */
5786 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5787 if (!dmabuf)
5788 return NULL;
5789
5790 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5791 LPFC_HDR_TEMPLATE_SIZE,
5792 &dmabuf->phys,
5793 GFP_KERNEL);
5794 if (!dmabuf->virt) {
5795 rpi_hdr = NULL;
5796 goto err_free_dmabuf;
5797 }
5798
5799 memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
5800 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
5801 rpi_hdr = NULL;
5802 goto err_free_coherent;
5803 }
5804
5805 /* Save the rpi header data for cleanup later. */
5806 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
5807 if (!rpi_hdr)
5808 goto err_free_coherent;
5809
5810 rpi_hdr->dmabuf = dmabuf;
5811 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
5812 rpi_hdr->page_count = 1;
5813 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04005814
5815 /* The rpi_hdr stores the logical index only. */
5816 rpi_hdr->start_rpi = curr_rpi_range;
James Smartda0436e2009-05-22 14:51:39 -04005817 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
5818
5819 /*
James Smart6d368e52011-05-24 11:44:12 -04005820 * The next_rpi stores the next logical module-64 rpi value used
5821 * to post physical rpis in subsequent rpi postings.
James Smartda0436e2009-05-22 14:51:39 -04005822 */
James Smart9589b0622011-04-16 11:03:17 -04005823 phba->sli4_hba.next_rpi += rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04005824 spin_unlock_irq(&phba->hbalock);
5825 return rpi_hdr;
5826
5827 err_free_coherent:
5828 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
5829 dmabuf->virt, dmabuf->phys);
5830 err_free_dmabuf:
5831 kfree(dmabuf);
5832 return NULL;
5833}
5834
5835/**
5836 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
5837 * @phba: pointer to lpfc hba data structure.
5838 *
5839 * This routine is invoked to remove all memory resources allocated
James Smart6d368e52011-05-24 11:44:12 -04005840 * to support rpis for SLI4 ports not supporting extents. This routine
5841 * presumes the caller has released all rpis consumed by fabric or port
5842 * logins and is prepared to have the header pages removed.
James Smartda0436e2009-05-22 14:51:39 -04005843 **/
5844void
5845lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
5846{
5847 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
5848
James Smart6d368e52011-05-24 11:44:12 -04005849 if (!phba->sli4_hba.rpi_hdrs_in_use)
5850 goto exit;
5851
James Smartda0436e2009-05-22 14:51:39 -04005852 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
5853 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
5854 list_del(&rpi_hdr->list);
5855 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
5856 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
5857 kfree(rpi_hdr->dmabuf);
5858 kfree(rpi_hdr);
5859 }
James Smart6d368e52011-05-24 11:44:12 -04005860 exit:
5861 /* There are no rpis available to the port now. */
5862 phba->sli4_hba.next_rpi = 0;
James Smartda0436e2009-05-22 14:51:39 -04005863}
5864
5865/**
James Smart3772a992009-05-22 14:50:54 -04005866 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
5867 * @pdev: pointer to pci device data structure.
5868 *
5869 * This routine is invoked to allocate the driver hba data structure for an
5870 * HBA device. If the allocation is successful, the phba reference to the
5871 * PCI device data structure is set.
5872 *
5873 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005874 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04005875 * NULL - error
5876 **/
5877static struct lpfc_hba *
5878lpfc_hba_alloc(struct pci_dev *pdev)
5879{
5880 struct lpfc_hba *phba;
5881
5882 /* Allocate memory for HBA structure */
5883 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
5884 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02005885 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04005886 return NULL;
5887 }
5888
5889 /* Set reference to PCI device in HBA structure */
5890 phba->pcidev = pdev;
5891
5892 /* Assign an unused board number */
5893 phba->brd_no = lpfc_get_instance();
5894 if (phba->brd_no < 0) {
5895 kfree(phba);
5896 return NULL;
5897 }
5898
James Smart4fede782010-01-26 23:08:55 -05005899 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04005900 INIT_LIST_HEAD(&phba->ct_ev_waiters);
5901
James Smart3772a992009-05-22 14:50:54 -04005902 return phba;
5903}
5904
5905/**
5906 * lpfc_hba_free - Free driver hba data structure with a device.
5907 * @phba: pointer to lpfc hba data structure.
5908 *
5909 * This routine is invoked to free the driver hba data structure with an
5910 * HBA device.
5911 **/
5912static void
5913lpfc_hba_free(struct lpfc_hba *phba)
5914{
5915 /* Release the driver assigned board number */
5916 idr_remove(&lpfc_hba_index, phba->brd_no);
5917
James Smart2a76a282012-08-03 12:35:54 -04005918 /* Free memory allocated with sli rings */
5919 kfree(phba->sli.ring);
5920 phba->sli.ring = NULL;
5921
James Smart3772a992009-05-22 14:50:54 -04005922 kfree(phba);
5923 return;
5924}
5925
5926/**
5927 * lpfc_create_shost - Create hba physical port with associated scsi host.
5928 * @phba: pointer to lpfc hba data structure.
5929 *
5930 * This routine is invoked to create HBA physical port and associate a SCSI
5931 * host with it.
5932 *
5933 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005934 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005935 * other values - error
5936 **/
5937static int
5938lpfc_create_shost(struct lpfc_hba *phba)
5939{
5940 struct lpfc_vport *vport;
5941 struct Scsi_Host *shost;
5942
5943 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05005944 phba->fc_edtov = FF_DEF_EDTOV;
5945 phba->fc_ratov = FF_DEF_RATOV;
5946 phba->fc_altov = FF_DEF_ALTOV;
5947 phba->fc_arbtov = FF_DEF_ARBTOV;
5948
James Smartd7c47992010-06-08 18:31:54 -04005949 atomic_set(&phba->sdev_cnt, 0);
James Smart3de2a652007-08-02 11:09:59 -04005950 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05005951 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04005952 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05005953
5954 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05005955 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05005956 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04005957 /* Put reference to SCSI host to driver's device private data */
5958 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05005959
James Smart3772a992009-05-22 14:50:54 -04005960 return 0;
5961}
dea31012005-04-17 16:05:31 -05005962
James Smart3772a992009-05-22 14:50:54 -04005963/**
5964 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
5965 * @phba: pointer to lpfc hba data structure.
5966 *
5967 * This routine is invoked to destroy HBA physical port and the associated
5968 * SCSI host.
5969 **/
5970static void
5971lpfc_destroy_shost(struct lpfc_hba *phba)
5972{
5973 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04005974
James Smart3772a992009-05-22 14:50:54 -04005975 /* Destroy physical port that associated with the SCSI host */
5976 destroy_port(vport);
5977
5978 return;
5979}
5980
5981/**
5982 * lpfc_setup_bg - Setup Block guard structures and debug areas.
5983 * @phba: pointer to lpfc hba data structure.
5984 * @shost: the shost to be used to detect Block guard settings.
5985 *
5986 * This routine sets up the local Block guard protocol settings for @shost.
5987 * This routine also allocates memory for debugging bg buffers.
5988 **/
5989static void
5990lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
5991{
James Smartbbeb79b2012-06-12 13:54:27 -04005992 uint32_t old_mask;
5993 uint32_t old_guard;
5994
James Smart3772a992009-05-22 14:50:54 -04005995 int pagecnt = 10;
5996 if (lpfc_prot_mask && lpfc_prot_guard) {
5997 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5998 "1478 Registering BlockGuard with the "
5999 "SCSI layer\n");
James Smartbbeb79b2012-06-12 13:54:27 -04006000
6001 old_mask = lpfc_prot_mask;
6002 old_guard = lpfc_prot_guard;
6003
6004 /* Only allow supported values */
6005 lpfc_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
6006 SHOST_DIX_TYPE0_PROTECTION |
6007 SHOST_DIX_TYPE1_PROTECTION);
6008 lpfc_prot_guard &= (SHOST_DIX_GUARD_IP | SHOST_DIX_GUARD_CRC);
6009
6010 /* DIF Type 1 protection for profiles AST1/C1 is end to end */
6011 if (lpfc_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
6012 lpfc_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
6013
6014 if (lpfc_prot_mask && lpfc_prot_guard) {
6015 if ((old_mask != lpfc_prot_mask) ||
6016 (old_guard != lpfc_prot_guard))
6017 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6018 "1475 Registering BlockGuard with the "
6019 "SCSI layer: mask %d guard %d\n",
6020 lpfc_prot_mask, lpfc_prot_guard);
6021
6022 scsi_host_set_prot(shost, lpfc_prot_mask);
6023 scsi_host_set_guard(shost, lpfc_prot_guard);
6024 } else
6025 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6026 "1479 Not Registering BlockGuard with the SCSI "
6027 "layer, Bad protection parameters: %d %d\n",
6028 old_mask, old_guard);
James Smart98c9ea52007-10-27 13:37:33 -04006029 }
James Smartbbeb79b2012-06-12 13:54:27 -04006030
James Smart81301a92008-12-04 22:39:46 -05006031 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05006032 while (pagecnt) {
6033 spin_lock_init(&_dump_buf_lock);
6034 _dump_buf_data =
6035 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
6036 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04006037 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6038 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04006039 "_dump_buf_data at 0x%p\n",
6040 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05006041 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04006042 memset(_dump_buf_data, 0,
6043 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05006044 break;
James Smart3772a992009-05-22 14:50:54 -04006045 } else
James Smart81301a92008-12-04 22:39:46 -05006046 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05006047 }
James Smart81301a92008-12-04 22:39:46 -05006048 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04006049 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6050 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04006051 "memory for hexdump\n");
6052 } else
James Smart6a9c52c2009-10-02 15:16:51 -04006053 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6054 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05006055 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05006056 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05006057 while (pagecnt) {
6058 _dump_buf_dif =
6059 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
6060 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04006061 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6062 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04006063 "_dump_buf_dif at 0x%p\n",
6064 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05006065 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04006066 memset(_dump_buf_dif, 0,
6067 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05006068 break;
James Smart3772a992009-05-22 14:50:54 -04006069 } else
James Smart81301a92008-12-04 22:39:46 -05006070 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05006071 }
James Smart81301a92008-12-04 22:39:46 -05006072 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04006073 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6074 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04006075 "memory for hexdump\n");
6076 } else
James Smart6a9c52c2009-10-02 15:16:51 -04006077 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6078 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04006079 _dump_buf_dif);
6080}
6081
6082/**
6083 * lpfc_post_init_setup - Perform necessary device post initialization setup.
6084 * @phba: pointer to lpfc hba data structure.
6085 *
6086 * This routine is invoked to perform all the necessary post initialization
6087 * setup for the device.
6088 **/
6089static void
6090lpfc_post_init_setup(struct lpfc_hba *phba)
6091{
6092 struct Scsi_Host *shost;
6093 struct lpfc_adapter_event_header adapter_event;
6094
6095 /* Get the default values for Model Name and Description */
6096 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
6097
6098 /*
6099 * hba setup may have changed the hba_queue_depth so we need to
6100 * adjust the value of can_queue.
6101 */
6102 shost = pci_get_drvdata(phba->pcidev);
6103 shost->can_queue = phba->cfg_hba_queue_depth - 10;
6104 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
6105 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05006106
6107 lpfc_host_attrib_init(shost);
6108
James Smart2e0fef82007-06-17 19:56:36 -05006109 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
6110 spin_lock_irq(shost->host_lock);
6111 lpfc_poll_start_timer(phba);
6112 spin_unlock_irq(shost->host_lock);
6113 }
James Smart8f6d98d2006-08-01 07:34:00 -04006114
James Smart93996272008-08-24 21:50:30 -04006115 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6116 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04006117 /* Send board arrival event to upper layer */
6118 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
6119 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
6120 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04006121 sizeof(adapter_event),
6122 (char *) &adapter_event,
6123 LPFC_NL_VENDOR_ID);
6124 return;
6125}
6126
6127/**
6128 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
6129 * @phba: pointer to lpfc hba data structure.
6130 *
6131 * This routine is invoked to set up the PCI device memory space for device
6132 * with SLI-3 interface spec.
6133 *
6134 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006135 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006136 * other values - error
6137 **/
6138static int
6139lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
6140{
6141 struct pci_dev *pdev;
6142 unsigned long bar0map_len, bar2map_len;
6143 int i, hbq_count;
6144 void *ptr;
6145 int error = -ENODEV;
6146
6147 /* Obtain PCI device reference */
6148 if (!phba->pcidev)
6149 return error;
6150 else
6151 pdev = phba->pcidev;
6152
6153 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05006154 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
6155 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
6156 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
6157 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smart3772a992009-05-22 14:50:54 -04006158 return error;
Michael Reed8e685972009-09-18 12:02:05 -05006159 }
6160 }
James Smart3772a992009-05-22 14:50:54 -04006161
6162 /* Get the bus address of Bar0 and Bar2 and the number of bytes
6163 * required by each mapping.
6164 */
6165 phba->pci_bar0_map = pci_resource_start(pdev, 0);
6166 bar0map_len = pci_resource_len(pdev, 0);
6167
6168 phba->pci_bar2_map = pci_resource_start(pdev, 2);
6169 bar2map_len = pci_resource_len(pdev, 2);
6170
6171 /* Map HBA SLIM to a kernel virtual address. */
6172 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
6173 if (!phba->slim_memmap_p) {
6174 dev_printk(KERN_ERR, &pdev->dev,
6175 "ioremap failed for SLIM memory.\n");
6176 goto out;
6177 }
6178
6179 /* Map HBA Control Registers to a kernel virtual address. */
6180 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
6181 if (!phba->ctrl_regs_memmap_p) {
6182 dev_printk(KERN_ERR, &pdev->dev,
6183 "ioremap failed for HBA control registers.\n");
6184 goto out_iounmap_slim;
6185 }
6186
6187 /* Allocate memory for SLI-2 structures */
6188 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
6189 SLI2_SLIM_SIZE,
6190 &phba->slim2p.phys,
6191 GFP_KERNEL);
6192 if (!phba->slim2p.virt)
6193 goto out_iounmap;
6194
6195 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
6196 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04006197 phba->mbox_ext = (phba->slim2p.virt +
6198 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04006199 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
6200 phba->IOCBs = (phba->slim2p.virt +
6201 offsetof(struct lpfc_sli2_slim, IOCBs));
6202
6203 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
6204 lpfc_sli_hbq_size(),
6205 &phba->hbqslimp.phys,
6206 GFP_KERNEL);
6207 if (!phba->hbqslimp.virt)
6208 goto out_free_slim;
6209
6210 hbq_count = lpfc_sli_hbq_count();
6211 ptr = phba->hbqslimp.virt;
6212 for (i = 0; i < hbq_count; ++i) {
6213 phba->hbqs[i].hbq_virt = ptr;
6214 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
6215 ptr += (lpfc_hbq_defs[i]->entry_count *
6216 sizeof(struct lpfc_hbq_entry));
6217 }
6218 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
6219 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
6220
6221 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
6222
6223 INIT_LIST_HEAD(&phba->rb_pend_list);
6224
6225 phba->MBslimaddr = phba->slim_memmap_p;
6226 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
6227 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
6228 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
6229 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04006230
dea31012005-04-17 16:05:31 -05006231 return 0;
6232
dea31012005-04-17 16:05:31 -05006233out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04006234 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6235 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05006236out_iounmap:
6237 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05006238out_iounmap_slim:
dea31012005-04-17 16:05:31 -05006239 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05006240out:
6241 return error;
6242}
6243
James Smarte59058c2008-08-24 21:49:00 -04006244/**
James Smart3772a992009-05-22 14:50:54 -04006245 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
6246 * @phba: pointer to lpfc hba data structure.
6247 *
6248 * This routine is invoked to unset the PCI device memory space for device
6249 * with SLI-3 interface spec.
6250 **/
6251static void
6252lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
6253{
6254 struct pci_dev *pdev;
6255
6256 /* Obtain PCI device reference */
6257 if (!phba->pcidev)
6258 return;
6259 else
6260 pdev = phba->pcidev;
6261
6262 /* Free coherent DMA memory allocated */
6263 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
6264 phba->hbqslimp.virt, phba->hbqslimp.phys);
6265 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6266 phba->slim2p.virt, phba->slim2p.phys);
6267
6268 /* I/O memory unmap */
6269 iounmap(phba->ctrl_regs_memmap_p);
6270 iounmap(phba->slim_memmap_p);
6271
6272 return;
6273}
6274
6275/**
James Smartda0436e2009-05-22 14:51:39 -04006276 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
6277 * @phba: pointer to lpfc hba data structure.
6278 *
6279 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
6280 * done and check status.
6281 *
6282 * Return 0 if successful, otherwise -ENODEV.
6283 **/
6284int
6285lpfc_sli4_post_status_check(struct lpfc_hba *phba)
6286{
James Smart2fcee4b2010-12-15 17:57:46 -05006287 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
6288 struct lpfc_register reg_data;
6289 int i, port_error = 0;
6290 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04006291
James Smart9940b972011-03-11 16:06:12 -05006292 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
6293 memset(&reg_data, 0, sizeof(reg_data));
James Smart2fcee4b2010-12-15 17:57:46 -05006294 if (!phba->sli4_hba.PSMPHRregaddr)
James Smartda0436e2009-05-22 14:51:39 -04006295 return -ENODEV;
6296
James Smartda0436e2009-05-22 14:51:39 -04006297 /* Wait up to 30 seconds for the SLI Port POST done and ready */
6298 for (i = 0; i < 3000; i++) {
James Smart9940b972011-03-11 16:06:12 -05006299 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
6300 &portsmphr_reg.word0) ||
6301 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
James Smart2fcee4b2010-12-15 17:57:46 -05006302 /* Port has a fatal POST error, break out */
James Smartda0436e2009-05-22 14:51:39 -04006303 port_error = -ENODEV;
6304 break;
6305 }
James Smart2fcee4b2010-12-15 17:57:46 -05006306 if (LPFC_POST_STAGE_PORT_READY ==
6307 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
James Smartda0436e2009-05-22 14:51:39 -04006308 break;
James Smartda0436e2009-05-22 14:51:39 -04006309 msleep(10);
6310 }
6311
James Smart2fcee4b2010-12-15 17:57:46 -05006312 /*
6313 * If there was a port error during POST, then don't proceed with
6314 * other register reads as the data may not be valid. Just exit.
6315 */
6316 if (port_error) {
James Smartda0436e2009-05-22 14:51:39 -04006317 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05006318 "1408 Port Failed POST - portsmphr=0x%x, "
6319 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
6320 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
6321 portsmphr_reg.word0,
6322 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
6323 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
6324 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
6325 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
6326 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
6327 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
6328 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
6329 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
6330 } else {
James Smart28baac72010-02-12 14:42:03 -05006331 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05006332 "2534 Device Info: SLIFamily=0x%x, "
6333 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
6334 "SLIHint_2=0x%x, FT=0x%x\n",
James Smart28baac72010-02-12 14:42:03 -05006335 bf_get(lpfc_sli_intf_sli_family,
6336 &phba->sli4_hba.sli_intf),
6337 bf_get(lpfc_sli_intf_slirev,
6338 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05006339 bf_get(lpfc_sli_intf_if_type,
James Smart28baac72010-02-12 14:42:03 -05006340 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05006341 bf_get(lpfc_sli_intf_sli_hint1,
6342 &phba->sli4_hba.sli_intf),
6343 bf_get(lpfc_sli_intf_sli_hint2,
6344 &phba->sli4_hba.sli_intf),
6345 bf_get(lpfc_sli_intf_func_type,
James Smart28baac72010-02-12 14:42:03 -05006346 &phba->sli4_hba.sli_intf));
James Smart2fcee4b2010-12-15 17:57:46 -05006347 /*
6348 * Check for other Port errors during the initialization
6349 * process. Fail the load if the port did not come up
6350 * correctly.
6351 */
6352 if_type = bf_get(lpfc_sli_intf_if_type,
6353 &phba->sli4_hba.sli_intf);
6354 switch (if_type) {
6355 case LPFC_SLI_INTF_IF_TYPE_0:
6356 phba->sli4_hba.ue_mask_lo =
6357 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
6358 phba->sli4_hba.ue_mask_hi =
6359 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
6360 uerrlo_reg.word0 =
6361 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
6362 uerrhi_reg.word0 =
6363 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
6364 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
6365 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
6366 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6367 "1422 Unrecoverable Error "
6368 "Detected during POST "
6369 "uerr_lo_reg=0x%x, "
6370 "uerr_hi_reg=0x%x, "
6371 "ue_mask_lo_reg=0x%x, "
6372 "ue_mask_hi_reg=0x%x\n",
6373 uerrlo_reg.word0,
6374 uerrhi_reg.word0,
6375 phba->sli4_hba.ue_mask_lo,
6376 phba->sli4_hba.ue_mask_hi);
6377 port_error = -ENODEV;
6378 }
6379 break;
6380 case LPFC_SLI_INTF_IF_TYPE_2:
6381 /* Final checks. The port status should be clean. */
James Smart9940b972011-03-11 16:06:12 -05006382 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
6383 &reg_data.word0) ||
James Smart05580562011-05-24 11:40:48 -04006384 (bf_get(lpfc_sliport_status_err, &reg_data) &&
6385 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
James Smart2fcee4b2010-12-15 17:57:46 -05006386 phba->work_status[0] =
6387 readl(phba->sli4_hba.u.if_type2.
6388 ERR1regaddr);
6389 phba->work_status[1] =
6390 readl(phba->sli4_hba.u.if_type2.
6391 ERR2regaddr);
6392 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05006393 "2888 Unrecoverable port error "
6394 "following POST: port status reg "
6395 "0x%x, port_smphr reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05006396 "error 1=0x%x, error 2=0x%x\n",
6397 reg_data.word0,
6398 portsmphr_reg.word0,
6399 phba->work_status[0],
6400 phba->work_status[1]);
6401 port_error = -ENODEV;
6402 }
6403 break;
6404 case LPFC_SLI_INTF_IF_TYPE_1:
6405 default:
6406 break;
6407 }
James Smart28baac72010-02-12 14:42:03 -05006408 }
James Smartda0436e2009-05-22 14:51:39 -04006409 return port_error;
6410}
6411
6412/**
6413 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
6414 * @phba: pointer to lpfc hba data structure.
James Smart2fcee4b2010-12-15 17:57:46 -05006415 * @if_type: The SLI4 interface type getting configured.
James Smartda0436e2009-05-22 14:51:39 -04006416 *
6417 * This routine is invoked to set up SLI4 BAR0 PCI config space register
6418 * memory map.
6419 **/
6420static void
James Smart2fcee4b2010-12-15 17:57:46 -05006421lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04006422{
James Smart2fcee4b2010-12-15 17:57:46 -05006423 switch (if_type) {
6424 case LPFC_SLI_INTF_IF_TYPE_0:
6425 phba->sli4_hba.u.if_type0.UERRLOregaddr =
6426 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
6427 phba->sli4_hba.u.if_type0.UERRHIregaddr =
6428 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
6429 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
6430 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
6431 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
6432 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
6433 phba->sli4_hba.SLIINTFregaddr =
6434 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
6435 break;
6436 case LPFC_SLI_INTF_IF_TYPE_2:
6437 phba->sli4_hba.u.if_type2.ERR1regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006438 phba->sli4_hba.conf_regs_memmap_p +
6439 LPFC_CTL_PORT_ER1_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006440 phba->sli4_hba.u.if_type2.ERR2regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006441 phba->sli4_hba.conf_regs_memmap_p +
6442 LPFC_CTL_PORT_ER2_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006443 phba->sli4_hba.u.if_type2.CTRLregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006444 phba->sli4_hba.conf_regs_memmap_p +
6445 LPFC_CTL_PORT_CTL_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006446 phba->sli4_hba.u.if_type2.STATUSregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006447 phba->sli4_hba.conf_regs_memmap_p +
6448 LPFC_CTL_PORT_STA_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006449 phba->sli4_hba.SLIINTFregaddr =
6450 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
6451 phba->sli4_hba.PSMPHRregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006452 phba->sli4_hba.conf_regs_memmap_p +
6453 LPFC_CTL_PORT_SEM_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006454 phba->sli4_hba.RQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05006455 phba->sli4_hba.conf_regs_memmap_p +
6456 LPFC_ULP0_RQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05006457 phba->sli4_hba.WQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05006458 phba->sli4_hba.conf_regs_memmap_p +
6459 LPFC_ULP0_WQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05006460 phba->sli4_hba.EQCQDBregaddr =
6461 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
6462 phba->sli4_hba.MQDBregaddr =
6463 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
6464 phba->sli4_hba.BMBXregaddr =
6465 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
6466 break;
6467 case LPFC_SLI_INTF_IF_TYPE_1:
6468 default:
6469 dev_printk(KERN_ERR, &phba->pcidev->dev,
6470 "FATAL - unsupported SLI4 interface type - %d\n",
6471 if_type);
6472 break;
6473 }
James Smartda0436e2009-05-22 14:51:39 -04006474}
6475
6476/**
6477 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
6478 * @phba: pointer to lpfc hba data structure.
6479 *
6480 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
6481 * memory map.
6482 **/
6483static void
6484lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
6485{
James Smart2fcee4b2010-12-15 17:57:46 -05006486 phba->sli4_hba.PSMPHRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
6487 LPFC_SLIPORT_IF0_SMPHR;
James Smartda0436e2009-05-22 14:51:39 -04006488 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006489 LPFC_HST_ISR0;
James Smartda0436e2009-05-22 14:51:39 -04006490 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006491 LPFC_HST_IMR0;
James Smartda0436e2009-05-22 14:51:39 -04006492 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006493 LPFC_HST_ISCR0;
James Smartda0436e2009-05-22 14:51:39 -04006494}
6495
6496/**
6497 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
6498 * @phba: pointer to lpfc hba data structure.
6499 * @vf: virtual function number
6500 *
6501 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
6502 * based on the given viftual function number, @vf.
6503 *
6504 * Return 0 if successful, otherwise -ENODEV.
6505 **/
6506static int
6507lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
6508{
6509 if (vf > LPFC_VIR_FUNC_MAX)
6510 return -ENODEV;
6511
6512 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05006513 vf * LPFC_VFR_PAGE_SIZE +
6514 LPFC_ULP0_RQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04006515 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05006516 vf * LPFC_VFR_PAGE_SIZE +
6517 LPFC_ULP0_WQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04006518 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6519 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
6520 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6521 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
6522 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6523 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
6524 return 0;
6525}
6526
6527/**
6528 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
6529 * @phba: pointer to lpfc hba data structure.
6530 *
6531 * This routine is invoked to create the bootstrap mailbox
6532 * region consistent with the SLI-4 interface spec. This
6533 * routine allocates all memory necessary to communicate
6534 * mailbox commands to the port and sets up all alignment
6535 * needs. No locks are expected to be held when calling
6536 * this routine.
6537 *
6538 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006539 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04006540 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04006541 **/
6542static int
6543lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
6544{
6545 uint32_t bmbx_size;
6546 struct lpfc_dmabuf *dmabuf;
6547 struct dma_address *dma_address;
6548 uint32_t pa_addr;
6549 uint64_t phys_addr;
6550
6551 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
6552 if (!dmabuf)
6553 return -ENOMEM;
6554
6555 /*
6556 * The bootstrap mailbox region is comprised of 2 parts
6557 * plus an alignment restriction of 16 bytes.
6558 */
6559 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
6560 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
6561 bmbx_size,
6562 &dmabuf->phys,
6563 GFP_KERNEL);
6564 if (!dmabuf->virt) {
6565 kfree(dmabuf);
6566 return -ENOMEM;
6567 }
6568 memset(dmabuf->virt, 0, bmbx_size);
6569
6570 /*
6571 * Initialize the bootstrap mailbox pointers now so that the register
6572 * operations are simple later. The mailbox dma address is required
6573 * to be 16-byte aligned. Also align the virtual memory as each
6574 * maibox is copied into the bmbx mailbox region before issuing the
6575 * command to the port.
6576 */
6577 phba->sli4_hba.bmbx.dmabuf = dmabuf;
6578 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
6579
6580 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
6581 LPFC_ALIGN_16_BYTE);
6582 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
6583 LPFC_ALIGN_16_BYTE);
6584
6585 /*
6586 * Set the high and low physical addresses now. The SLI4 alignment
6587 * requirement is 16 bytes and the mailbox is posted to the port
6588 * as two 30-bit addresses. The other data is a bit marking whether
6589 * the 30-bit address is the high or low address.
6590 * Upcast bmbx aphys to 64bits so shift instruction compiles
6591 * clean on 32 bit machines.
6592 */
6593 dma_address = &phba->sli4_hba.bmbx.dma_address;
6594 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
6595 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
6596 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
6597 LPFC_BMBX_BIT1_ADDR_HI);
6598
6599 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
6600 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
6601 LPFC_BMBX_BIT1_ADDR_LO);
6602 return 0;
6603}
6604
6605/**
6606 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
6607 * @phba: pointer to lpfc hba data structure.
6608 *
6609 * This routine is invoked to teardown the bootstrap mailbox
6610 * region and release all host resources. This routine requires
6611 * the caller to ensure all mailbox commands recovered, no
6612 * additional mailbox comands are sent, and interrupts are disabled
6613 * before calling this routine.
6614 *
6615 **/
6616static void
6617lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
6618{
6619 dma_free_coherent(&phba->pcidev->dev,
6620 phba->sli4_hba.bmbx.bmbx_size,
6621 phba->sli4_hba.bmbx.dmabuf->virt,
6622 phba->sli4_hba.bmbx.dmabuf->phys);
6623
6624 kfree(phba->sli4_hba.bmbx.dmabuf);
6625 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
6626}
6627
6628/**
6629 * lpfc_sli4_read_config - Get the config parameters.
6630 * @phba: pointer to lpfc hba data structure.
6631 *
6632 * This routine is invoked to read the configuration parameters from the HBA.
6633 * The configuration parameters are used to set the base and maximum values
6634 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
6635 * allocation for the port.
6636 *
6637 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006638 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006639 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006640 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006641 **/
James Smartff78d8f2011-12-13 13:21:35 -05006642int
James Smartda0436e2009-05-22 14:51:39 -04006643lpfc_sli4_read_config(struct lpfc_hba *phba)
6644{
6645 LPFC_MBOXQ_t *pmb;
6646 struct lpfc_mbx_read_config *rd_config;
James Smart912e3ac2011-05-24 11:42:11 -04006647 union lpfc_sli4_cfg_shdr *shdr;
6648 uint32_t shdr_status, shdr_add_status;
6649 struct lpfc_mbx_get_func_cfg *get_func_cfg;
6650 struct lpfc_rsrc_desc_fcfcoe *desc;
James Smart8aa134a2012-08-14 14:25:29 -04006651 char *pdesc_0;
James Smart912e3ac2011-05-24 11:42:11 -04006652 uint32_t desc_count;
James Smart8aa134a2012-08-14 14:25:29 -04006653 int length, i, rc = 0, rc2;
James Smartda0436e2009-05-22 14:51:39 -04006654
6655 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6656 if (!pmb) {
6657 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6658 "2011 Unable to allocate memory for issuing "
6659 "SLI_CONFIG_SPECIAL mailbox command\n");
6660 return -ENOMEM;
6661 }
6662
6663 lpfc_read_config(phba, pmb);
6664
6665 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6666 if (rc != MBX_SUCCESS) {
6667 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6668 "2012 Mailbox failed , mbxCmd x%x "
6669 "READ_CONFIG, mbxStatus x%x\n",
6670 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6671 bf_get(lpfc_mqe_status, &pmb->u.mqe));
6672 rc = -EIO;
6673 } else {
6674 rd_config = &pmb->u.mqe.un.rd_config;
James Smartff78d8f2011-12-13 13:21:35 -05006675 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
6676 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
6677 phba->sli4_hba.lnk_info.lnk_tp =
6678 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
6679 phba->sli4_hba.lnk_info.lnk_no =
6680 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
6681 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6682 "3081 lnk_type:%d, lnk_numb:%d\n",
6683 phba->sli4_hba.lnk_info.lnk_tp,
6684 phba->sli4_hba.lnk_info.lnk_no);
6685 } else
6686 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6687 "3082 Mailbox (x%x) returned ldv:x0\n",
6688 bf_get(lpfc_mqe_command, &pmb->u.mqe));
James Smart6d368e52011-05-24 11:44:12 -04006689 phba->sli4_hba.extents_in_use =
6690 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04006691 phba->sli4_hba.max_cfg_param.max_xri =
6692 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
6693 phba->sli4_hba.max_cfg_param.xri_base =
6694 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
6695 phba->sli4_hba.max_cfg_param.max_vpi =
6696 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
6697 phba->sli4_hba.max_cfg_param.vpi_base =
6698 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
6699 phba->sli4_hba.max_cfg_param.max_rpi =
6700 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
6701 phba->sli4_hba.max_cfg_param.rpi_base =
6702 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
6703 phba->sli4_hba.max_cfg_param.max_vfi =
6704 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
6705 phba->sli4_hba.max_cfg_param.vfi_base =
6706 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
6707 phba->sli4_hba.max_cfg_param.max_fcfi =
6708 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04006709 phba->sli4_hba.max_cfg_param.max_eq =
6710 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
6711 phba->sli4_hba.max_cfg_param.max_rq =
6712 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
6713 phba->sli4_hba.max_cfg_param.max_wq =
6714 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
6715 phba->sli4_hba.max_cfg_param.max_cq =
6716 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
6717 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
6718 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
6719 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
6720 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
James Smart5ffc2662009-11-18 15:39:44 -05006721 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
6722 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04006723 phba->max_vports = phba->max_vpi;
6724 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart6d368e52011-05-24 11:44:12 -04006725 "2003 cfg params Extents? %d "
6726 "XRI(B:%d M:%d), "
James Smartda0436e2009-05-22 14:51:39 -04006727 "VPI(B:%d M:%d) "
6728 "VFI(B:%d M:%d) "
6729 "RPI(B:%d M:%d) "
James Smart6d368e52011-05-24 11:44:12 -04006730 "FCFI(Count:%d)\n",
6731 phba->sli4_hba.extents_in_use,
James Smartda0436e2009-05-22 14:51:39 -04006732 phba->sli4_hba.max_cfg_param.xri_base,
6733 phba->sli4_hba.max_cfg_param.max_xri,
6734 phba->sli4_hba.max_cfg_param.vpi_base,
6735 phba->sli4_hba.max_cfg_param.max_vpi,
6736 phba->sli4_hba.max_cfg_param.vfi_base,
6737 phba->sli4_hba.max_cfg_param.max_vfi,
6738 phba->sli4_hba.max_cfg_param.rpi_base,
6739 phba->sli4_hba.max_cfg_param.max_rpi,
James Smartda0436e2009-05-22 14:51:39 -04006740 phba->sli4_hba.max_cfg_param.max_fcfi);
6741 }
James Smart912e3ac2011-05-24 11:42:11 -04006742
6743 if (rc)
6744 goto read_cfg_out;
James Smartda0436e2009-05-22 14:51:39 -04006745
6746 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -04006747 length = phba->sli4_hba.max_cfg_param.max_xri -
6748 lpfc_sli4_get_els_iocb_cnt(phba);
6749 if (phba->cfg_hba_queue_depth > length) {
6750 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6751 "3361 HBA queue depth changed from %d to %d\n",
6752 phba->cfg_hba_queue_depth, length);
6753 phba->cfg_hba_queue_depth = length;
6754 }
James Smart912e3ac2011-05-24 11:42:11 -04006755
6756 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
6757 LPFC_SLI_INTF_IF_TYPE_2)
6758 goto read_cfg_out;
6759
6760 /* get the pf# and vf# for SLI4 if_type 2 port */
6761 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
6762 sizeof(struct lpfc_sli4_cfg_mhdr));
6763 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
6764 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
6765 length, LPFC_SLI4_MBX_EMBED);
6766
James Smart8aa134a2012-08-14 14:25:29 -04006767 rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart912e3ac2011-05-24 11:42:11 -04006768 shdr = (union lpfc_sli4_cfg_shdr *)
6769 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
6770 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6771 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart8aa134a2012-08-14 14:25:29 -04006772 if (rc2 || shdr_status || shdr_add_status) {
James Smart912e3ac2011-05-24 11:42:11 -04006773 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6774 "3026 Mailbox failed , mbxCmd x%x "
6775 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
6776 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6777 bf_get(lpfc_mqe_status, &pmb->u.mqe));
James Smart912e3ac2011-05-24 11:42:11 -04006778 goto read_cfg_out;
6779 }
6780
6781 /* search for fc_fcoe resrouce descriptor */
6782 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
6783 desc_count = get_func_cfg->func_cfg.rsrc_desc_count;
6784
James Smart8aa134a2012-08-14 14:25:29 -04006785 pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
6786 desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
6787 length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
6788 if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
6789 length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
6790 else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
6791 goto read_cfg_out;
6792
James Smart912e3ac2011-05-24 11:42:11 -04006793 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
James Smart8aa134a2012-08-14 14:25:29 -04006794 desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
James Smart912e3ac2011-05-24 11:42:11 -04006795 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
James Smart8aa134a2012-08-14 14:25:29 -04006796 bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
James Smart912e3ac2011-05-24 11:42:11 -04006797 phba->sli4_hba.iov.pf_number =
6798 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
6799 phba->sli4_hba.iov.vf_number =
6800 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
6801 break;
6802 }
6803 }
6804
6805 if (i < LPFC_RSRC_DESC_MAX_NUM)
6806 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6807 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
6808 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
6809 phba->sli4_hba.iov.vf_number);
James Smart8aa134a2012-08-14 14:25:29 -04006810 else
James Smart912e3ac2011-05-24 11:42:11 -04006811 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6812 "3028 GET_FUNCTION_CONFIG: failed to find "
6813 "Resrouce Descriptor:x%x\n",
6814 LPFC_RSRC_DESC_TYPE_FCFCOE);
James Smart912e3ac2011-05-24 11:42:11 -04006815
6816read_cfg_out:
6817 mempool_free(pmb, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04006818 return rc;
6819}
6820
6821/**
James Smart2fcee4b2010-12-15 17:57:46 -05006822 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
James Smartda0436e2009-05-22 14:51:39 -04006823 * @phba: pointer to lpfc hba data structure.
6824 *
James Smart2fcee4b2010-12-15 17:57:46 -05006825 * This routine is invoked to setup the port-side endian order when
6826 * the port if_type is 0. This routine has no function for other
6827 * if_types.
James Smartda0436e2009-05-22 14:51:39 -04006828 *
6829 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006830 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006831 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006832 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006833 **/
6834static int
6835lpfc_setup_endian_order(struct lpfc_hba *phba)
6836{
6837 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05006838 uint32_t if_type, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04006839 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
6840 HOST_ENDIAN_HIGH_WORD1};
6841
James Smart2fcee4b2010-12-15 17:57:46 -05006842 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
6843 switch (if_type) {
6844 case LPFC_SLI_INTF_IF_TYPE_0:
6845 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6846 GFP_KERNEL);
6847 if (!mboxq) {
6848 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6849 "0492 Unable to allocate memory for "
6850 "issuing SLI_CONFIG_SPECIAL mailbox "
6851 "command\n");
6852 return -ENOMEM;
6853 }
James Smartda0436e2009-05-22 14:51:39 -04006854
James Smart2fcee4b2010-12-15 17:57:46 -05006855 /*
6856 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
6857 * two words to contain special data values and no other data.
6858 */
6859 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
6860 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
6861 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6862 if (rc != MBX_SUCCESS) {
6863 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6864 "0493 SLI_CONFIG_SPECIAL mailbox "
6865 "failed with status x%x\n",
6866 rc);
6867 rc = -EIO;
6868 }
6869 mempool_free(mboxq, phba->mbox_mem_pool);
6870 break;
6871 case LPFC_SLI_INTF_IF_TYPE_2:
6872 case LPFC_SLI_INTF_IF_TYPE_1:
6873 default:
6874 break;
James Smartda0436e2009-05-22 14:51:39 -04006875 }
James Smartda0436e2009-05-22 14:51:39 -04006876 return rc;
6877}
6878
6879/**
James Smart5350d872011-10-10 21:33:49 -04006880 * lpfc_sli4_queue_verify - Verify and update EQ and CQ counts
James Smartda0436e2009-05-22 14:51:39 -04006881 * @phba: pointer to lpfc hba data structure.
6882 *
James Smart5350d872011-10-10 21:33:49 -04006883 * This routine is invoked to check the user settable queue counts for EQs and
6884 * CQs. after this routine is called the counts will be set to valid values that
6885 * adhere to the constraints of the system's interrupt vectors and the port's
6886 * queue resources.
James Smartda0436e2009-05-22 14:51:39 -04006887 *
6888 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006889 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006890 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04006891 **/
6892static int
James Smart5350d872011-10-10 21:33:49 -04006893lpfc_sli4_queue_verify(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04006894{
James Smart67d12732012-08-03 12:36:13 -04006895 int cfg_fcp_io_channel;
James Smart90695ee2012-08-14 14:25:36 -04006896 uint32_t cpu;
6897 uint32_t i = 0;
James Smart1ba981f2014-02-20 09:56:45 -05006898 int fof_vectors = phba->cfg_fof ? 1 : 0;
James Smartda0436e2009-05-22 14:51:39 -04006899
6900 /*
James Smart67d12732012-08-03 12:36:13 -04006901 * Sanity check for configured queue parameters against the run-time
James Smartda0436e2009-05-22 14:51:39 -04006902 * device parameters
6903 */
6904
James Smart67d12732012-08-03 12:36:13 -04006905 /* Sanity check on HBA EQ parameters */
6906 cfg_fcp_io_channel = phba->cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04006907
James Smart7bb03bb2013-04-17 20:19:16 -04006908 /* It doesn't make sense to have more io channels then online CPUs */
6909 for_each_present_cpu(cpu) {
6910 if (cpu_online(cpu))
6911 i++;
James Smart90695ee2012-08-14 14:25:36 -04006912 }
James Smart7bb03bb2013-04-17 20:19:16 -04006913 phba->sli4_hba.num_online_cpu = i;
James Smartb246de12013-05-31 17:03:07 -04006914 phba->sli4_hba.num_present_cpu = lpfc_present_cpu;
James Smart76fd07a2014-02-20 09:57:18 -05006915 phba->sli4_hba.curr_disp_cpu = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04006916
James Smart90695ee2012-08-14 14:25:36 -04006917 if (i < cfg_fcp_io_channel) {
James Smart82c3e9b2012-09-29 11:29:50 -04006918 lpfc_printf_log(phba,
6919 KERN_ERR, LOG_INIT,
James Smart90695ee2012-08-14 14:25:36 -04006920 "3188 Reducing IO channels to match number of "
James Smart7bb03bb2013-04-17 20:19:16 -04006921 "online CPUs: from %d to %d\n",
6922 cfg_fcp_io_channel, i);
James Smart90695ee2012-08-14 14:25:36 -04006923 cfg_fcp_io_channel = i;
6924 }
6925
James Smart1ba981f2014-02-20 09:56:45 -05006926 if (cfg_fcp_io_channel + fof_vectors >
James Smart67d12732012-08-03 12:36:13 -04006927 phba->sli4_hba.max_cfg_param.max_eq) {
James Smart82c3e9b2012-09-29 11:29:50 -04006928 if (phba->sli4_hba.max_cfg_param.max_eq <
6929 LPFC_FCP_IO_CHAN_MIN) {
James Smartda0436e2009-05-22 14:51:39 -04006930 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6931 "2574 Not enough EQs (%d) from the "
6932 "pci function for supporting FCP "
6933 "EQs (%d)\n",
6934 phba->sli4_hba.max_cfg_param.max_eq,
James Smart67d12732012-08-03 12:36:13 -04006935 phba->cfg_fcp_io_channel);
James Smartda0436e2009-05-22 14:51:39 -04006936 goto out_error;
6937 }
James Smart82c3e9b2012-09-29 11:29:50 -04006938 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6939 "2575 Reducing IO channels to match number of "
6940 "available EQs: from %d to %d\n",
6941 cfg_fcp_io_channel,
6942 phba->sli4_hba.max_cfg_param.max_eq);
James Smart1ba981f2014-02-20 09:56:45 -05006943 cfg_fcp_io_channel = phba->sli4_hba.max_cfg_param.max_eq -
6944 fof_vectors;
James Smartda0436e2009-05-22 14:51:39 -04006945 }
James Smart67d12732012-08-03 12:36:13 -04006946
James Smartda0436e2009-05-22 14:51:39 -04006947 /* The actual number of FCP event queues adopted */
James Smart67d12732012-08-03 12:36:13 -04006948 phba->cfg_fcp_io_channel = cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04006949
James Smartda0436e2009-05-22 14:51:39 -04006950 /* Get EQ depth from module parameter, fake the default for now */
6951 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
6952 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
6953
James Smart5350d872011-10-10 21:33:49 -04006954 /* Get CQ depth from module parameter, fake the default for now */
6955 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
6956 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
6957
6958 return 0;
6959out_error:
6960 return -ENOMEM;
6961}
6962
6963/**
6964 * lpfc_sli4_queue_create - Create all the SLI4 queues
6965 * @phba: pointer to lpfc hba data structure.
6966 *
6967 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
6968 * operation. For each SLI4 queue type, the parameters such as queue entry
6969 * count (queue depth) shall be taken from the module parameter. For now,
6970 * we just use some constant number as place holder.
6971 *
6972 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07006973 * 0 - successful
James Smart5350d872011-10-10 21:33:49 -04006974 * -ENOMEM - No availble memory
6975 * -EIO - The mailbox failed to complete successfully.
6976 **/
6977int
6978lpfc_sli4_queue_create(struct lpfc_hba *phba)
6979{
6980 struct lpfc_queue *qdesc;
James Smart67d12732012-08-03 12:36:13 -04006981 int idx;
James Smart5350d872011-10-10 21:33:49 -04006982
6983 /*
James Smart67d12732012-08-03 12:36:13 -04006984 * Create HBA Record arrays.
James Smart5350d872011-10-10 21:33:49 -04006985 */
James Smart67d12732012-08-03 12:36:13 -04006986 if (!phba->cfg_fcp_io_channel)
6987 return -ERANGE;
James Smart5350d872011-10-10 21:33:49 -04006988
James Smart67d12732012-08-03 12:36:13 -04006989 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
6990 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
6991 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
6992 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
6993 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
6994 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
6995
6996 phba->sli4_hba.hba_eq = kzalloc((sizeof(struct lpfc_queue *) *
6997 phba->cfg_fcp_io_channel), GFP_KERNEL);
6998 if (!phba->sli4_hba.hba_eq) {
James Smartda0436e2009-05-22 14:51:39 -04006999 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04007000 "2576 Failed allocate memory for "
7001 "fast-path EQ record array\n");
James Smartda0436e2009-05-22 14:51:39 -04007002 goto out_error;
7003 }
James Smart67d12732012-08-03 12:36:13 -04007004
7005 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
7006 phba->cfg_fcp_io_channel), GFP_KERNEL);
7007 if (!phba->sli4_hba.fcp_cq) {
7008 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7009 "2577 Failed allocate memory for fast-path "
7010 "CQ record array\n");
7011 goto out_error;
7012 }
7013
7014 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
7015 phba->cfg_fcp_io_channel), GFP_KERNEL);
7016 if (!phba->sli4_hba.fcp_wq) {
7017 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7018 "2578 Failed allocate memory for fast-path "
7019 "WQ record array\n");
7020 goto out_error;
7021 }
James Smartda0436e2009-05-22 14:51:39 -04007022
James Smart5350d872011-10-10 21:33:49 -04007023 /*
James Smart67d12732012-08-03 12:36:13 -04007024 * Since the first EQ can have multiple CQs associated with it,
7025 * this array is used to quickly see if we have a FCP fast-path
7026 * CQ match.
James Smart5350d872011-10-10 21:33:49 -04007027 */
James Smart67d12732012-08-03 12:36:13 -04007028 phba->sli4_hba.fcp_cq_map = kzalloc((sizeof(uint16_t) *
7029 phba->cfg_fcp_io_channel), GFP_KERNEL);
7030 if (!phba->sli4_hba.fcp_cq_map) {
7031 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7032 "2545 Failed allocate memory for fast-path "
7033 "CQ map\n");
7034 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007035 }
James Smart67d12732012-08-03 12:36:13 -04007036
7037 /*
7038 * Create HBA Event Queues (EQs). The cfg_fcp_io_channel specifies
7039 * how many EQs to create.
7040 */
7041 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7042
7043 /* Create EQs */
James Smartda0436e2009-05-22 14:51:39 -04007044 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
7045 phba->sli4_hba.eq_ecount);
7046 if (!qdesc) {
7047 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04007048 "0497 Failed allocate EQ (%d)\n", idx);
7049 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007050 }
James Smart67d12732012-08-03 12:36:13 -04007051 phba->sli4_hba.hba_eq[idx] = qdesc;
7052
7053 /* Create Fast Path FCP CQs */
7054 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7055 phba->sli4_hba.cq_ecount);
7056 if (!qdesc) {
7057 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7058 "0499 Failed allocate fast-path FCP "
7059 "CQ (%d)\n", idx);
7060 goto out_error;
7061 }
7062 phba->sli4_hba.fcp_cq[idx] = qdesc;
7063
7064 /* Create Fast Path FCP WQs */
7065 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
7066 phba->sli4_hba.wq_ecount);
7067 if (!qdesc) {
7068 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7069 "0503 Failed allocate fast-path FCP "
7070 "WQ (%d)\n", idx);
7071 goto out_error;
7072 }
7073 phba->sli4_hba.fcp_wq[idx] = qdesc;
James Smartda0436e2009-05-22 14:51:39 -04007074 }
7075
James Smart67d12732012-08-03 12:36:13 -04007076
James Smartda0436e2009-05-22 14:51:39 -04007077 /*
James Smart67d12732012-08-03 12:36:13 -04007078 * Create Slow Path Completion Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04007079 */
7080
James Smartda0436e2009-05-22 14:51:39 -04007081 /* Create slow-path Mailbox Command Complete Queue */
7082 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7083 phba->sli4_hba.cq_ecount);
7084 if (!qdesc) {
7085 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7086 "0500 Failed allocate slow-path mailbox CQ\n");
James Smart67d12732012-08-03 12:36:13 -04007087 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007088 }
7089 phba->sli4_hba.mbx_cq = qdesc;
7090
7091 /* Create slow-path ELS Complete Queue */
7092 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7093 phba->sli4_hba.cq_ecount);
7094 if (!qdesc) {
7095 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7096 "0501 Failed allocate slow-path ELS CQ\n");
James Smart67d12732012-08-03 12:36:13 -04007097 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007098 }
7099 phba->sli4_hba.els_cq = qdesc;
7100
James Smartda0436e2009-05-22 14:51:39 -04007101
James Smart5350d872011-10-10 21:33:49 -04007102 /*
James Smart67d12732012-08-03 12:36:13 -04007103 * Create Slow Path Work Queues (WQs)
James Smart5350d872011-10-10 21:33:49 -04007104 */
James Smartda0436e2009-05-22 14:51:39 -04007105
7106 /* Create Mailbox Command Queue */
James Smartda0436e2009-05-22 14:51:39 -04007107
7108 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
7109 phba->sli4_hba.mq_ecount);
7110 if (!qdesc) {
7111 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7112 "0505 Failed allocate slow-path MQ\n");
James Smart67d12732012-08-03 12:36:13 -04007113 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007114 }
7115 phba->sli4_hba.mbx_wq = qdesc;
7116
7117 /*
James Smart67d12732012-08-03 12:36:13 -04007118 * Create ELS Work Queues
James Smartda0436e2009-05-22 14:51:39 -04007119 */
James Smartda0436e2009-05-22 14:51:39 -04007120
7121 /* Create slow-path ELS Work Queue */
7122 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
7123 phba->sli4_hba.wq_ecount);
7124 if (!qdesc) {
7125 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7126 "0504 Failed allocate slow-path ELS WQ\n");
James Smart67d12732012-08-03 12:36:13 -04007127 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007128 }
7129 phba->sli4_hba.els_wq = qdesc;
7130
James Smartda0436e2009-05-22 14:51:39 -04007131 /*
7132 * Create Receive Queue (RQ)
7133 */
James Smartda0436e2009-05-22 14:51:39 -04007134
7135 /* Create Receive Queue for header */
7136 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
7137 phba->sli4_hba.rq_ecount);
7138 if (!qdesc) {
7139 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7140 "0506 Failed allocate receive HRQ\n");
James Smart67d12732012-08-03 12:36:13 -04007141 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007142 }
7143 phba->sli4_hba.hdr_rq = qdesc;
7144
7145 /* Create Receive Queue for data */
7146 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
7147 phba->sli4_hba.rq_ecount);
7148 if (!qdesc) {
7149 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7150 "0507 Failed allocate receive DRQ\n");
James Smart67d12732012-08-03 12:36:13 -04007151 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007152 }
7153 phba->sli4_hba.dat_rq = qdesc;
7154
James Smart1ba981f2014-02-20 09:56:45 -05007155 /* Create the Queues needed for Flash Optimized Fabric operations */
7156 if (phba->cfg_fof)
7157 lpfc_fof_queue_create(phba);
James Smartda0436e2009-05-22 14:51:39 -04007158 return 0;
7159
James Smartda0436e2009-05-22 14:51:39 -04007160out_error:
James Smart67d12732012-08-03 12:36:13 -04007161 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007162 return -ENOMEM;
7163}
7164
7165/**
7166 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
7167 * @phba: pointer to lpfc hba data structure.
7168 *
7169 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
7170 * operation.
7171 *
7172 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007173 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007174 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007175 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007176 **/
James Smart5350d872011-10-10 21:33:49 -04007177void
James Smartda0436e2009-05-22 14:51:39 -04007178lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
7179{
James Smart67d12732012-08-03 12:36:13 -04007180 int idx;
7181
James Smart1ba981f2014-02-20 09:56:45 -05007182 if (phba->cfg_fof)
7183 lpfc_fof_queue_destroy(phba);
7184
James Smart67d12732012-08-03 12:36:13 -04007185 if (phba->sli4_hba.hba_eq != NULL) {
7186 /* Release HBA event queue */
7187 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7188 if (phba->sli4_hba.hba_eq[idx] != NULL) {
7189 lpfc_sli4_queue_free(
7190 phba->sli4_hba.hba_eq[idx]);
7191 phba->sli4_hba.hba_eq[idx] = NULL;
7192 }
7193 }
7194 kfree(phba->sli4_hba.hba_eq);
7195 phba->sli4_hba.hba_eq = NULL;
7196 }
7197
7198 if (phba->sli4_hba.fcp_cq != NULL) {
7199 /* Release FCP completion queue */
7200 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7201 if (phba->sli4_hba.fcp_cq[idx] != NULL) {
7202 lpfc_sli4_queue_free(
7203 phba->sli4_hba.fcp_cq[idx]);
7204 phba->sli4_hba.fcp_cq[idx] = NULL;
7205 }
7206 }
7207 kfree(phba->sli4_hba.fcp_cq);
7208 phba->sli4_hba.fcp_cq = NULL;
7209 }
7210
7211 if (phba->sli4_hba.fcp_wq != NULL) {
7212 /* Release FCP work queue */
7213 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7214 if (phba->sli4_hba.fcp_wq[idx] != NULL) {
7215 lpfc_sli4_queue_free(
7216 phba->sli4_hba.fcp_wq[idx]);
7217 phba->sli4_hba.fcp_wq[idx] = NULL;
7218 }
7219 }
7220 kfree(phba->sli4_hba.fcp_wq);
7221 phba->sli4_hba.fcp_wq = NULL;
7222 }
7223
7224 /* Release FCP CQ mapping array */
7225 if (phba->sli4_hba.fcp_cq_map != NULL) {
7226 kfree(phba->sli4_hba.fcp_cq_map);
7227 phba->sli4_hba.fcp_cq_map = NULL;
7228 }
James Smartda0436e2009-05-22 14:51:39 -04007229
7230 /* Release mailbox command work queue */
James Smart67d12732012-08-03 12:36:13 -04007231 if (phba->sli4_hba.mbx_wq != NULL) {
7232 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
7233 phba->sli4_hba.mbx_wq = NULL;
7234 }
James Smartda0436e2009-05-22 14:51:39 -04007235
7236 /* Release ELS work queue */
James Smart67d12732012-08-03 12:36:13 -04007237 if (phba->sli4_hba.els_wq != NULL) {
7238 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
7239 phba->sli4_hba.els_wq = NULL;
7240 }
James Smartda0436e2009-05-22 14:51:39 -04007241
7242 /* Release unsolicited receive queue */
James Smart67d12732012-08-03 12:36:13 -04007243 if (phba->sli4_hba.hdr_rq != NULL) {
7244 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
7245 phba->sli4_hba.hdr_rq = NULL;
7246 }
7247 if (phba->sli4_hba.dat_rq != NULL) {
7248 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
7249 phba->sli4_hba.dat_rq = NULL;
7250 }
James Smartda0436e2009-05-22 14:51:39 -04007251
James Smartda0436e2009-05-22 14:51:39 -04007252 /* Release ELS complete queue */
James Smart67d12732012-08-03 12:36:13 -04007253 if (phba->sli4_hba.els_cq != NULL) {
7254 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
7255 phba->sli4_hba.els_cq = NULL;
7256 }
James Smartda0436e2009-05-22 14:51:39 -04007257
7258 /* Release mailbox command complete queue */
James Smart67d12732012-08-03 12:36:13 -04007259 if (phba->sli4_hba.mbx_cq != NULL) {
7260 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
7261 phba->sli4_hba.mbx_cq = NULL;
7262 }
James Smartda0436e2009-05-22 14:51:39 -04007263
7264 return;
7265}
7266
7267/**
7268 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
7269 * @phba: pointer to lpfc hba data structure.
7270 *
7271 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
7272 * operation.
7273 *
7274 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007275 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007276 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007277 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007278 **/
7279int
7280lpfc_sli4_queue_setup(struct lpfc_hba *phba)
7281{
James Smart2a76a282012-08-03 12:35:54 -04007282 struct lpfc_sli *psli = &phba->sli;
7283 struct lpfc_sli_ring *pring;
James Smartda0436e2009-05-22 14:51:39 -04007284 int rc = -ENOMEM;
7285 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
7286 int fcp_cq_index = 0;
James Smart962bc512013-01-03 15:44:00 -05007287 uint32_t shdr_status, shdr_add_status;
7288 union lpfc_sli4_cfg_shdr *shdr;
7289 LPFC_MBOXQ_t *mboxq;
7290 uint32_t length;
7291
7292 /* Check for dual-ULP support */
7293 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7294 if (!mboxq) {
7295 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7296 "3249 Unable to allocate memory for "
7297 "QUERY_FW_CFG mailbox command\n");
7298 return -ENOMEM;
7299 }
7300 length = (sizeof(struct lpfc_mbx_query_fw_config) -
7301 sizeof(struct lpfc_sli4_cfg_mhdr));
7302 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7303 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
7304 length, LPFC_SLI4_MBX_EMBED);
7305
7306 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7307
7308 shdr = (union lpfc_sli4_cfg_shdr *)
7309 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7310 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7311 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7312 if (shdr_status || shdr_add_status || rc) {
7313 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7314 "3250 QUERY_FW_CFG mailbox failed with status "
7315 "x%x add_status x%x, mbx status x%x\n",
7316 shdr_status, shdr_add_status, rc);
7317 if (rc != MBX_TIMEOUT)
7318 mempool_free(mboxq, phba->mbox_mem_pool);
7319 rc = -ENXIO;
7320 goto out_error;
7321 }
7322
7323 phba->sli4_hba.fw_func_mode =
7324 mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
7325 phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
7326 phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
7327 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7328 "3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
7329 "ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
7330 phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
7331
7332 if (rc != MBX_TIMEOUT)
7333 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04007334
7335 /*
James Smart67d12732012-08-03 12:36:13 -04007336 * Set up HBA Event Queues (EQs)
James Smartda0436e2009-05-22 14:51:39 -04007337 */
7338
James Smart67d12732012-08-03 12:36:13 -04007339 /* Set up HBA event queue */
7340 if (phba->cfg_fcp_io_channel && !phba->sli4_hba.hba_eq) {
James Smart2e90f4b2011-12-13 13:22:37 -05007341 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7342 "3147 Fast-path EQs not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007343 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007344 goto out_error;
James Smart2e90f4b2011-12-13 13:22:37 -05007345 }
James Smart67d12732012-08-03 12:36:13 -04007346 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
7347 if (!phba->sli4_hba.hba_eq[fcp_eqidx]) {
James Smartda0436e2009-05-22 14:51:39 -04007348 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7349 "0522 Fast-path EQ (%d) not "
7350 "allocated\n", fcp_eqidx);
James Smart1b511972011-12-13 13:23:09 -05007351 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007352 goto out_destroy_hba_eq;
James Smartda0436e2009-05-22 14:51:39 -04007353 }
James Smart67d12732012-08-03 12:36:13 -04007354 rc = lpfc_eq_create(phba, phba->sli4_hba.hba_eq[fcp_eqidx],
James Smartbf8dae82012-08-03 12:36:24 -04007355 (phba->cfg_fcp_imax / phba->cfg_fcp_io_channel));
James Smartda0436e2009-05-22 14:51:39 -04007356 if (rc) {
7357 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7358 "0523 Failed setup of fast-path EQ "
7359 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
James Smart67d12732012-08-03 12:36:13 -04007360 goto out_destroy_hba_eq;
James Smartda0436e2009-05-22 14:51:39 -04007361 }
7362 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04007363 "2584 HBA EQ setup: "
James Smartda0436e2009-05-22 14:51:39 -04007364 "queue[%d]-id=%d\n", fcp_eqidx,
James Smart67d12732012-08-03 12:36:13 -04007365 phba->sli4_hba.hba_eq[fcp_eqidx]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007366 }
7367
James Smart67d12732012-08-03 12:36:13 -04007368 /* Set up fast-path FCP Response Complete Queue */
7369 if (!phba->sli4_hba.fcp_cq) {
7370 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7371 "3148 Fast-path FCP CQ array not "
7372 "allocated\n");
7373 rc = -ENOMEM;
7374 goto out_destroy_hba_eq;
7375 }
7376
7377 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_io_channel; fcp_cqidx++) {
7378 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
7379 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7380 "0526 Fast-path FCP CQ (%d) not "
7381 "allocated\n", fcp_cqidx);
7382 rc = -ENOMEM;
7383 goto out_destroy_fcp_cq;
7384 }
7385 rc = lpfc_cq_create(phba, phba->sli4_hba.fcp_cq[fcp_cqidx],
7386 phba->sli4_hba.hba_eq[fcp_cqidx], LPFC_WCQ, LPFC_FCP);
7387 if (rc) {
7388 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7389 "0527 Failed setup of fast-path FCP "
7390 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
7391 goto out_destroy_fcp_cq;
7392 }
7393
7394 /* Setup fcp_cq_map for fast lookup */
7395 phba->sli4_hba.fcp_cq_map[fcp_cqidx] =
7396 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id;
7397
7398 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7399 "2588 FCP CQ setup: cq[%d]-id=%d, "
7400 "parent seq[%d]-id=%d\n",
7401 fcp_cqidx,
7402 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
7403 fcp_cqidx,
7404 phba->sli4_hba.hba_eq[fcp_cqidx]->queue_id);
7405 }
7406
7407 /* Set up fast-path FCP Work Queue */
7408 if (!phba->sli4_hba.fcp_wq) {
7409 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7410 "3149 Fast-path FCP WQ array not "
7411 "allocated\n");
7412 rc = -ENOMEM;
7413 goto out_destroy_fcp_cq;
7414 }
7415
7416 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_io_channel; fcp_wqidx++) {
7417 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
7418 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7419 "0534 Fast-path FCP WQ (%d) not "
7420 "allocated\n", fcp_wqidx);
7421 rc = -ENOMEM;
7422 goto out_destroy_fcp_wq;
7423 }
7424 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
7425 phba->sli4_hba.fcp_cq[fcp_wqidx],
7426 LPFC_FCP);
7427 if (rc) {
7428 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7429 "0535 Failed setup of fast-path FCP "
7430 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
7431 goto out_destroy_fcp_wq;
7432 }
7433
7434 /* Bind this WQ to the next FCP ring */
7435 pring = &psli->ring[MAX_SLI3_CONFIGURED_RINGS + fcp_wqidx];
7436 pring->sli.sli4.wqp = (void *)phba->sli4_hba.fcp_wq[fcp_wqidx];
7437 phba->sli4_hba.fcp_cq[fcp_wqidx]->pring = pring;
7438
7439 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7440 "2591 FCP WQ setup: wq[%d]-id=%d, "
7441 "parent cq[%d]-id=%d\n",
7442 fcp_wqidx,
7443 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
7444 fcp_cq_index,
7445 phba->sli4_hba.fcp_cq[fcp_wqidx]->queue_id);
7446 }
James Smartda0436e2009-05-22 14:51:39 -04007447 /*
7448 * Set up Complete Queues (CQs)
7449 */
7450
7451 /* Set up slow-path MBOX Complete Queue as the first CQ */
7452 if (!phba->sli4_hba.mbx_cq) {
7453 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7454 "0528 Mailbox CQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007455 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007456 goto out_destroy_fcp_wq;
James Smartda0436e2009-05-22 14:51:39 -04007457 }
James Smart67d12732012-08-03 12:36:13 -04007458 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq,
7459 phba->sli4_hba.hba_eq[0], LPFC_MCQ, LPFC_MBOX);
James Smartda0436e2009-05-22 14:51:39 -04007460 if (rc) {
7461 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7462 "0529 Failed setup of slow-path mailbox CQ: "
7463 "rc = 0x%x\n", rc);
James Smart67d12732012-08-03 12:36:13 -04007464 goto out_destroy_fcp_wq;
James Smartda0436e2009-05-22 14:51:39 -04007465 }
7466 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7467 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
7468 phba->sli4_hba.mbx_cq->queue_id,
James Smart67d12732012-08-03 12:36:13 -04007469 phba->sli4_hba.hba_eq[0]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007470
7471 /* Set up slow-path ELS Complete Queue */
7472 if (!phba->sli4_hba.els_cq) {
7473 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7474 "0530 ELS CQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007475 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04007476 goto out_destroy_mbx_cq;
7477 }
James Smart67d12732012-08-03 12:36:13 -04007478 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq,
7479 phba->sli4_hba.hba_eq[0], LPFC_WCQ, LPFC_ELS);
James Smartda0436e2009-05-22 14:51:39 -04007480 if (rc) {
7481 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7482 "0531 Failed setup of slow-path ELS CQ: "
7483 "rc = 0x%x\n", rc);
7484 goto out_destroy_mbx_cq;
7485 }
7486 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7487 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
7488 phba->sli4_hba.els_cq->queue_id,
James Smart67d12732012-08-03 12:36:13 -04007489 phba->sli4_hba.hba_eq[0]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007490
7491 /*
7492 * Set up all the Work Queues (WQs)
7493 */
7494
7495 /* Set up Mailbox Command Queue */
7496 if (!phba->sli4_hba.mbx_wq) {
7497 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7498 "0538 Slow-path MQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007499 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007500 goto out_destroy_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04007501 }
7502 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
7503 phba->sli4_hba.mbx_cq, LPFC_MBOX);
7504 if (rc) {
7505 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7506 "0539 Failed setup of slow-path MQ: "
7507 "rc = 0x%x\n", rc);
James Smart67d12732012-08-03 12:36:13 -04007508 goto out_destroy_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04007509 }
7510 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7511 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
7512 phba->sli4_hba.mbx_wq->queue_id,
7513 phba->sli4_hba.mbx_cq->queue_id);
7514
7515 /* Set up slow-path ELS Work Queue */
7516 if (!phba->sli4_hba.els_wq) {
7517 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7518 "0536 Slow-path ELS WQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007519 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04007520 goto out_destroy_mbx_wq;
7521 }
7522 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
7523 phba->sli4_hba.els_cq, LPFC_ELS);
7524 if (rc) {
7525 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7526 "0537 Failed setup of slow-path ELS WQ: "
7527 "rc = 0x%x\n", rc);
7528 goto out_destroy_mbx_wq;
7529 }
James Smart2a76a282012-08-03 12:35:54 -04007530
7531 /* Bind this WQ to the ELS ring */
7532 pring = &psli->ring[LPFC_ELS_RING];
7533 pring->sli.sli4.wqp = (void *)phba->sli4_hba.els_wq;
7534 phba->sli4_hba.els_cq->pring = pring;
7535
James Smartda0436e2009-05-22 14:51:39 -04007536 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7537 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
7538 phba->sli4_hba.els_wq->queue_id,
7539 phba->sli4_hba.els_cq->queue_id);
7540
James Smartda0436e2009-05-22 14:51:39 -04007541 /*
7542 * Create Receive Queue (RQ)
7543 */
7544 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
7545 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7546 "0540 Receive Queue not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007547 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007548 goto out_destroy_els_wq;
James Smartda0436e2009-05-22 14:51:39 -04007549 }
James Smart73d91e52011-10-10 21:32:10 -04007550
7551 lpfc_rq_adjust_repost(phba, phba->sli4_hba.hdr_rq, LPFC_ELS_HBQ);
7552 lpfc_rq_adjust_repost(phba, phba->sli4_hba.dat_rq, LPFC_ELS_HBQ);
7553
James Smartda0436e2009-05-22 14:51:39 -04007554 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04007555 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04007556 if (rc) {
7557 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7558 "0541 Failed setup of Receive Queue: "
7559 "rc = 0x%x\n", rc);
7560 goto out_destroy_fcp_wq;
7561 }
James Smart73d91e52011-10-10 21:32:10 -04007562
James Smartda0436e2009-05-22 14:51:39 -04007563 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7564 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
7565 "parent cq-id=%d\n",
7566 phba->sli4_hba.hdr_rq->queue_id,
7567 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04007568 phba->sli4_hba.els_cq->queue_id);
James Smart1ba981f2014-02-20 09:56:45 -05007569
7570 if (phba->cfg_fof) {
7571 rc = lpfc_fof_queue_setup(phba);
7572 if (rc) {
7573 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7574 "0549 Failed setup of FOF Queues: "
7575 "rc = 0x%x\n", rc);
7576 goto out_destroy_els_rq;
7577 }
7578 }
James Smartda0436e2009-05-22 14:51:39 -04007579 return 0;
7580
James Smart1ba981f2014-02-20 09:56:45 -05007581out_destroy_els_rq:
7582 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
James Smart2e90f4b2011-12-13 13:22:37 -05007583out_destroy_els_wq:
James Smartda0436e2009-05-22 14:51:39 -04007584 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7585out_destroy_mbx_wq:
7586 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
James Smart2e90f4b2011-12-13 13:22:37 -05007587out_destroy_els_cq:
James Smartda0436e2009-05-22 14:51:39 -04007588 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
7589out_destroy_mbx_cq:
7590 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
James Smart67d12732012-08-03 12:36:13 -04007591out_destroy_fcp_wq:
7592 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
7593 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
7594out_destroy_fcp_cq:
7595 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
7596 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
7597out_destroy_hba_eq:
James Smartda0436e2009-05-22 14:51:39 -04007598 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
James Smart67d12732012-08-03 12:36:13 -04007599 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[fcp_eqidx]);
James Smartda0436e2009-05-22 14:51:39 -04007600out_error:
7601 return rc;
7602}
7603
7604/**
7605 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
7606 * @phba: pointer to lpfc hba data structure.
7607 *
7608 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
7609 * operation.
7610 *
7611 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007612 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007613 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007614 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007615 **/
7616void
7617lpfc_sli4_queue_unset(struct lpfc_hba *phba)
7618{
7619 int fcp_qidx;
7620
James Smart1ba981f2014-02-20 09:56:45 -05007621 /* Unset the queues created for Flash Optimized Fabric operations */
7622 if (phba->cfg_fof)
7623 lpfc_fof_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007624 /* Unset mailbox command work queue */
7625 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
7626 /* Unset ELS work queue */
7627 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7628 /* Unset unsolicited receive queue */
7629 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
7630 /* Unset FCP work queue */
James Smart67d12732012-08-03 12:36:13 -04007631 if (phba->sli4_hba.fcp_wq) {
7632 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
7633 fcp_qidx++)
7634 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
7635 }
James Smartda0436e2009-05-22 14:51:39 -04007636 /* Unset mailbox command complete queue */
7637 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
7638 /* Unset ELS complete queue */
7639 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
James Smartda0436e2009-05-22 14:51:39 -04007640 /* Unset FCP response complete queue */
James Smart2e90f4b2011-12-13 13:22:37 -05007641 if (phba->sli4_hba.fcp_cq) {
James Smart67d12732012-08-03 12:36:13 -04007642 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
7643 fcp_qidx++)
James Smart2e90f4b2011-12-13 13:22:37 -05007644 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05007645 }
James Smartda0436e2009-05-22 14:51:39 -04007646 /* Unset fast-path event queue */
James Smart67d12732012-08-03 12:36:13 -04007647 if (phba->sli4_hba.hba_eq) {
7648 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
James Smart2e90f4b2011-12-13 13:22:37 -05007649 fcp_qidx++)
James Smart67d12732012-08-03 12:36:13 -04007650 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[fcp_qidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05007651 }
James Smartda0436e2009-05-22 14:51:39 -04007652}
7653
7654/**
7655 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
7656 * @phba: pointer to lpfc hba data structure.
7657 *
7658 * This routine is invoked to allocate and set up a pool of completion queue
7659 * events. The body of the completion queue event is a completion queue entry
7660 * CQE. For now, this pool is used for the interrupt service routine to queue
7661 * the following HBA completion queue events for the worker thread to process:
7662 * - Mailbox asynchronous events
7663 * - Receive queue completion unsolicited events
7664 * Later, this can be used for all the slow-path events.
7665 *
7666 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007667 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007668 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04007669 **/
7670static int
7671lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
7672{
7673 struct lpfc_cq_event *cq_event;
7674 int i;
7675
7676 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
7677 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
7678 if (!cq_event)
7679 goto out_pool_create_fail;
7680 list_add_tail(&cq_event->list,
7681 &phba->sli4_hba.sp_cqe_event_pool);
7682 }
7683 return 0;
7684
7685out_pool_create_fail:
7686 lpfc_sli4_cq_event_pool_destroy(phba);
7687 return -ENOMEM;
7688}
7689
7690/**
7691 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
7692 * @phba: pointer to lpfc hba data structure.
7693 *
7694 * This routine is invoked to free the pool of completion queue events at
7695 * driver unload time. Note that, it is the responsibility of the driver
7696 * cleanup routine to free all the outstanding completion-queue events
7697 * allocated from this pool back into the pool before invoking this routine
7698 * to destroy the pool.
7699 **/
7700static void
7701lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
7702{
7703 struct lpfc_cq_event *cq_event, *next_cq_event;
7704
7705 list_for_each_entry_safe(cq_event, next_cq_event,
7706 &phba->sli4_hba.sp_cqe_event_pool, list) {
7707 list_del(&cq_event->list);
7708 kfree(cq_event);
7709 }
7710}
7711
7712/**
7713 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
7714 * @phba: pointer to lpfc hba data structure.
7715 *
7716 * This routine is the lock free version of the API invoked to allocate a
7717 * completion-queue event from the free pool.
7718 *
7719 * Return: Pointer to the newly allocated completion-queue event if successful
7720 * NULL otherwise.
7721 **/
7722struct lpfc_cq_event *
7723__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
7724{
7725 struct lpfc_cq_event *cq_event = NULL;
7726
7727 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
7728 struct lpfc_cq_event, list);
7729 return cq_event;
7730}
7731
7732/**
7733 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
7734 * @phba: pointer to lpfc hba data structure.
7735 *
7736 * This routine is the lock version of the API invoked to allocate a
7737 * completion-queue event from the free pool.
7738 *
7739 * Return: Pointer to the newly allocated completion-queue event if successful
7740 * NULL otherwise.
7741 **/
7742struct lpfc_cq_event *
7743lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
7744{
7745 struct lpfc_cq_event *cq_event;
7746 unsigned long iflags;
7747
7748 spin_lock_irqsave(&phba->hbalock, iflags);
7749 cq_event = __lpfc_sli4_cq_event_alloc(phba);
7750 spin_unlock_irqrestore(&phba->hbalock, iflags);
7751 return cq_event;
7752}
7753
7754/**
7755 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
7756 * @phba: pointer to lpfc hba data structure.
7757 * @cq_event: pointer to the completion queue event to be freed.
7758 *
7759 * This routine is the lock free version of the API invoked to release a
7760 * completion-queue event back into the free pool.
7761 **/
7762void
7763__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
7764 struct lpfc_cq_event *cq_event)
7765{
7766 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
7767}
7768
7769/**
7770 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
7771 * @phba: pointer to lpfc hba data structure.
7772 * @cq_event: pointer to the completion queue event to be freed.
7773 *
7774 * This routine is the lock version of the API invoked to release a
7775 * completion-queue event back into the free pool.
7776 **/
7777void
7778lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
7779 struct lpfc_cq_event *cq_event)
7780{
7781 unsigned long iflags;
7782 spin_lock_irqsave(&phba->hbalock, iflags);
7783 __lpfc_sli4_cq_event_release(phba, cq_event);
7784 spin_unlock_irqrestore(&phba->hbalock, iflags);
7785}
7786
7787/**
7788 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
7789 * @phba: pointer to lpfc hba data structure.
7790 *
7791 * This routine is to free all the pending completion-queue events to the
7792 * back into the free pool for device reset.
7793 **/
7794static void
7795lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
7796{
7797 LIST_HEAD(cqelist);
7798 struct lpfc_cq_event *cqe;
7799 unsigned long iflags;
7800
7801 /* Retrieve all the pending WCQEs from pending WCQE lists */
7802 spin_lock_irqsave(&phba->hbalock, iflags);
7803 /* Pending FCP XRI abort events */
7804 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
7805 &cqelist);
7806 /* Pending ELS XRI abort events */
7807 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
7808 &cqelist);
7809 /* Pending asynnc events */
7810 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
7811 &cqelist);
7812 spin_unlock_irqrestore(&phba->hbalock, iflags);
7813
7814 while (!list_empty(&cqelist)) {
7815 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
7816 lpfc_sli4_cq_event_release(phba, cqe);
7817 }
7818}
7819
7820/**
7821 * lpfc_pci_function_reset - Reset pci function.
7822 * @phba: pointer to lpfc hba data structure.
7823 *
7824 * This routine is invoked to request a PCI function reset. It will destroys
7825 * all resources assigned to the PCI function which originates this request.
7826 *
7827 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007828 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007829 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007830 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007831 **/
7832int
7833lpfc_pci_function_reset(struct lpfc_hba *phba)
7834{
7835 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05007836 uint32_t rc = 0, if_type;
James Smartda0436e2009-05-22 14:51:39 -04007837 uint32_t shdr_status, shdr_add_status;
James Smart2fcee4b2010-12-15 17:57:46 -05007838 uint32_t rdy_chk, num_resets = 0, reset_again = 0;
James Smartda0436e2009-05-22 14:51:39 -04007839 union lpfc_sli4_cfg_shdr *shdr;
James Smart2fcee4b2010-12-15 17:57:46 -05007840 struct lpfc_register reg_data;
James Smart2b81f942012-03-01 22:37:18 -05007841 uint16_t devid;
James Smartda0436e2009-05-22 14:51:39 -04007842
James Smart2fcee4b2010-12-15 17:57:46 -05007843 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7844 switch (if_type) {
7845 case LPFC_SLI_INTF_IF_TYPE_0:
7846 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
7847 GFP_KERNEL);
7848 if (!mboxq) {
7849 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7850 "0494 Unable to allocate memory for "
7851 "issuing SLI_FUNCTION_RESET mailbox "
7852 "command\n");
7853 return -ENOMEM;
7854 }
7855
7856 /* Setup PCI function reset mailbox-ioctl command */
7857 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7858 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
7859 LPFC_SLI4_MBX_EMBED);
7860 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7861 shdr = (union lpfc_sli4_cfg_shdr *)
7862 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7863 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7864 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
7865 &shdr->response);
7866 if (rc != MBX_TIMEOUT)
7867 mempool_free(mboxq, phba->mbox_mem_pool);
7868 if (shdr_status || shdr_add_status || rc) {
7869 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7870 "0495 SLI_FUNCTION_RESET mailbox "
7871 "failed with status x%x add_status x%x,"
7872 " mbx status x%x\n",
7873 shdr_status, shdr_add_status, rc);
7874 rc = -ENXIO;
7875 }
7876 break;
7877 case LPFC_SLI_INTF_IF_TYPE_2:
7878 for (num_resets = 0;
7879 num_resets < MAX_IF_TYPE_2_RESETS;
7880 num_resets++) {
7881 reg_data.word0 = 0;
7882 bf_set(lpfc_sliport_ctrl_end, &reg_data,
7883 LPFC_SLIPORT_LITTLE_ENDIAN);
7884 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
7885 LPFC_SLIPORT_INIT_PORT);
7886 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
7887 CTRLregaddr);
James Smart8fcb8ac2012-03-01 22:35:58 -05007888 /* flush */
James Smart2b81f942012-03-01 22:37:18 -05007889 pci_read_config_word(phba->pcidev,
7890 PCI_DEVICE_ID, &devid);
James Smart2fcee4b2010-12-15 17:57:46 -05007891 /*
7892 * Poll the Port Status Register and wait for RDY for
7893 * up to 10 seconds. If the port doesn't respond, treat
7894 * it as an error. If the port responds with RN, start
7895 * the loop again.
7896 */
7897 for (rdy_chk = 0; rdy_chk < 1000; rdy_chk++) {
James Smart73d91e52011-10-10 21:32:10 -04007898 msleep(10);
James Smart9940b972011-03-11 16:06:12 -05007899 if (lpfc_readl(phba->sli4_hba.u.if_type2.
7900 STATUSregaddr, &reg_data.word0)) {
7901 rc = -ENODEV;
James Smart73d91e52011-10-10 21:32:10 -04007902 goto out;
James Smart9940b972011-03-11 16:06:12 -05007903 }
James Smart6b5151f2012-01-18 16:24:06 -05007904 if (bf_get(lpfc_sliport_status_rn, &reg_data))
7905 reset_again++;
James Smart2fcee4b2010-12-15 17:57:46 -05007906 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
7907 break;
James Smart2fcee4b2010-12-15 17:57:46 -05007908 }
7909
7910 /*
7911 * If the port responds to the init request with
7912 * reset needed, delay for a bit and restart the loop.
7913 */
James Smart6b5151f2012-01-18 16:24:06 -05007914 if (reset_again && (rdy_chk < 1000)) {
James Smart2fcee4b2010-12-15 17:57:46 -05007915 msleep(10);
7916 reset_again = 0;
7917 continue;
7918 }
7919
7920 /* Detect any port errors. */
James Smart2fcee4b2010-12-15 17:57:46 -05007921 if ((bf_get(lpfc_sliport_status_err, &reg_data)) ||
7922 (rdy_chk >= 1000)) {
7923 phba->work_status[0] = readl(
7924 phba->sli4_hba.u.if_type2.ERR1regaddr);
7925 phba->work_status[1] = readl(
7926 phba->sli4_hba.u.if_type2.ERR2regaddr);
7927 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05007928 "2890 Port error detected during port "
James Smart8a9d2e82012-05-09 21:16:12 -04007929 "reset(%d): wait_tmo:%d ms, "
7930 "port status reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05007931 "error 1=0x%x, error 2=0x%x\n",
James Smart8a9d2e82012-05-09 21:16:12 -04007932 num_resets, rdy_chk*10,
7933 reg_data.word0,
James Smart2fcee4b2010-12-15 17:57:46 -05007934 phba->work_status[0],
7935 phba->work_status[1]);
7936 rc = -ENODEV;
7937 }
7938
7939 /*
7940 * Terminate the outer loop provided the Port indicated
7941 * ready within 10 seconds.
7942 */
7943 if (rdy_chk < 1000)
7944 break;
7945 }
James Smart05580562011-05-24 11:40:48 -04007946 /* delay driver action following IF_TYPE_2 function reset */
7947 msleep(100);
James Smart2fcee4b2010-12-15 17:57:46 -05007948 break;
7949 case LPFC_SLI_INTF_IF_TYPE_1:
7950 default:
7951 break;
James Smartda0436e2009-05-22 14:51:39 -04007952 }
7953
James Smart73d91e52011-10-10 21:32:10 -04007954out:
James Smart2fcee4b2010-12-15 17:57:46 -05007955 /* Catch the not-ready port failure after a port reset. */
James Smart229adb02013-04-17 20:16:51 -04007956 if (num_resets >= MAX_IF_TYPE_2_RESETS) {
7957 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7958 "3317 HBA not functional: IP Reset Failed "
7959 "after (%d) retries, try: "
7960 "echo fw_reset > board_mode\n", num_resets);
James Smart2fcee4b2010-12-15 17:57:46 -05007961 rc = -ENODEV;
James Smart229adb02013-04-17 20:16:51 -04007962 }
James Smart2fcee4b2010-12-15 17:57:46 -05007963
James Smartda0436e2009-05-22 14:51:39 -04007964 return rc;
7965}
7966
7967/**
James Smartda0436e2009-05-22 14:51:39 -04007968 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
7969 * @phba: pointer to lpfc hba data structure.
7970 *
7971 * This routine is invoked to set up the PCI device memory space for device
7972 * with SLI-4 interface spec.
7973 *
7974 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007975 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007976 * other values - error
7977 **/
7978static int
7979lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
7980{
7981 struct pci_dev *pdev;
7982 unsigned long bar0map_len, bar1map_len, bar2map_len;
7983 int error = -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -05007984 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04007985
7986 /* Obtain PCI device reference */
7987 if (!phba->pcidev)
7988 return error;
7989 else
7990 pdev = phba->pcidev;
7991
7992 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05007993 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
7994 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
7995 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
7996 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smartda0436e2009-05-22 14:51:39 -04007997 return error;
Michael Reed8e685972009-09-18 12:02:05 -05007998 }
7999 }
James Smartda0436e2009-05-22 14:51:39 -04008000
James Smart2fcee4b2010-12-15 17:57:46 -05008001 /*
8002 * The BARs and register set definitions and offset locations are
8003 * dependent on the if_type.
8004 */
8005 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
8006 &phba->sli4_hba.sli_intf.word0)) {
8007 return error;
8008 }
8009
8010 /* There is no SLI3 failback for SLI4 devices. */
8011 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
8012 LPFC_SLI_INTF_VALID) {
8013 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8014 "2894 SLI_INTF reg contents invalid "
8015 "sli_intf reg 0x%x\n",
8016 phba->sli4_hba.sli_intf.word0);
8017 return error;
8018 }
8019
8020 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
8021 /*
8022 * Get the bus address of SLI4 device Bar regions and the
8023 * number of bytes required by each mapping. The mapping of the
8024 * particular PCI BARs regions is dependent on the type of
8025 * SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -04008026 */
James Smartf5ca6f22013-09-06 12:21:09 -04008027 if (pci_resource_start(pdev, PCI_64BIT_BAR0)) {
8028 phba->pci_bar0_map = pci_resource_start(pdev, PCI_64BIT_BAR0);
8029 bar0map_len = pci_resource_len(pdev, PCI_64BIT_BAR0);
James Smart2fcee4b2010-12-15 17:57:46 -05008030
8031 /*
8032 * Map SLI4 PCI Config Space Register base to a kernel virtual
8033 * addr
8034 */
8035 phba->sli4_hba.conf_regs_memmap_p =
8036 ioremap(phba->pci_bar0_map, bar0map_len);
8037 if (!phba->sli4_hba.conf_regs_memmap_p) {
8038 dev_printk(KERN_ERR, &pdev->dev,
8039 "ioremap failed for SLI4 PCI config "
8040 "registers.\n");
8041 goto out;
8042 }
James Smartf5ca6f22013-09-06 12:21:09 -04008043 phba->pci_bar0_memmap_p = phba->sli4_hba.conf_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05008044 /* Set up BAR0 PCI config space register memory map */
8045 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smart1dfb5a42010-02-12 14:40:50 -05008046 } else {
8047 phba->pci_bar0_map = pci_resource_start(pdev, 1);
8048 bar0map_len = pci_resource_len(pdev, 1);
James Smart2fcee4b2010-12-15 17:57:46 -05008049 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8050 dev_printk(KERN_ERR, &pdev->dev,
8051 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
8052 goto out;
8053 }
8054 phba->sli4_hba.conf_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04008055 ioremap(phba->pci_bar0_map, bar0map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05008056 if (!phba->sli4_hba.conf_regs_memmap_p) {
8057 dev_printk(KERN_ERR, &pdev->dev,
8058 "ioremap failed for SLI4 PCI config "
8059 "registers.\n");
8060 goto out;
8061 }
8062 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smartda0436e2009-05-22 14:51:39 -04008063 }
8064
James Smartc31098c2011-04-16 11:03:33 -04008065 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
James Smartf5ca6f22013-09-06 12:21:09 -04008066 (pci_resource_start(pdev, PCI_64BIT_BAR2))) {
James Smart2fcee4b2010-12-15 17:57:46 -05008067 /*
8068 * Map SLI4 if type 0 HBA Control Register base to a kernel
8069 * virtual address and setup the registers.
8070 */
James Smartf5ca6f22013-09-06 12:21:09 -04008071 phba->pci_bar1_map = pci_resource_start(pdev, PCI_64BIT_BAR2);
8072 bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
James Smart2fcee4b2010-12-15 17:57:46 -05008073 phba->sli4_hba.ctrl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04008074 ioremap(phba->pci_bar1_map, bar1map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05008075 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
8076 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04008077 "ioremap failed for SLI4 HBA control registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05008078 goto out_iounmap_conf;
8079 }
James Smartf5ca6f22013-09-06 12:21:09 -04008080 phba->pci_bar2_memmap_p = phba->sli4_hba.ctrl_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05008081 lpfc_sli4_bar1_register_memmap(phba);
James Smartda0436e2009-05-22 14:51:39 -04008082 }
8083
James Smartc31098c2011-04-16 11:03:33 -04008084 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
James Smartf5ca6f22013-09-06 12:21:09 -04008085 (pci_resource_start(pdev, PCI_64BIT_BAR4))) {
James Smart2fcee4b2010-12-15 17:57:46 -05008086 /*
8087 * Map SLI4 if type 0 HBA Doorbell Register base to a kernel
8088 * virtual address and setup the registers.
8089 */
James Smartf5ca6f22013-09-06 12:21:09 -04008090 phba->pci_bar2_map = pci_resource_start(pdev, PCI_64BIT_BAR4);
8091 bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
James Smart2fcee4b2010-12-15 17:57:46 -05008092 phba->sli4_hba.drbl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04008093 ioremap(phba->pci_bar2_map, bar2map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05008094 if (!phba->sli4_hba.drbl_regs_memmap_p) {
8095 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04008096 "ioremap failed for SLI4 HBA doorbell registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05008097 goto out_iounmap_ctrl;
8098 }
James Smartf5ca6f22013-09-06 12:21:09 -04008099 phba->pci_bar4_memmap_p = phba->sli4_hba.drbl_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05008100 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
8101 if (error)
8102 goto out_iounmap_all;
James Smartda0436e2009-05-22 14:51:39 -04008103 }
8104
James Smartda0436e2009-05-22 14:51:39 -04008105 return 0;
8106
8107out_iounmap_all:
8108 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
8109out_iounmap_ctrl:
8110 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
8111out_iounmap_conf:
8112 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8113out:
8114 return error;
8115}
8116
8117/**
8118 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
8119 * @phba: pointer to lpfc hba data structure.
8120 *
8121 * This routine is invoked to unset the PCI device memory space for device
8122 * with SLI-4 interface spec.
8123 **/
8124static void
8125lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
8126{
James Smart2e90f4b2011-12-13 13:22:37 -05008127 uint32_t if_type;
8128 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smartda0436e2009-05-22 14:51:39 -04008129
James Smart2e90f4b2011-12-13 13:22:37 -05008130 switch (if_type) {
8131 case LPFC_SLI_INTF_IF_TYPE_0:
8132 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
8133 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
8134 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8135 break;
8136 case LPFC_SLI_INTF_IF_TYPE_2:
8137 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8138 break;
8139 case LPFC_SLI_INTF_IF_TYPE_1:
8140 default:
8141 dev_printk(KERN_ERR, &phba->pcidev->dev,
8142 "FATAL - unsupported SLI4 interface type - %d\n",
8143 if_type);
8144 break;
8145 }
James Smartda0436e2009-05-22 14:51:39 -04008146}
8147
8148/**
James Smart3772a992009-05-22 14:50:54 -04008149 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
8150 * @phba: pointer to lpfc hba data structure.
8151 *
8152 * This routine is invoked to enable the MSI-X interrupt vectors to device
8153 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
8154 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
8155 * invoked, enables either all or nothing, depending on the current
8156 * availability of PCI vector resources. The device driver is responsible
8157 * for calling the individual request_irq() to register each MSI-X vector
8158 * with a interrupt handler, which is done in this function. Note that
8159 * later when device is unloading, the driver should always call free_irq()
8160 * on all MSI-X vectors it has done request_irq() on before calling
8161 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
8162 * will be left with MSI-X enabled and leaks its vectors.
8163 *
8164 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008165 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008166 * other values - error
8167 **/
8168static int
8169lpfc_sli_enable_msix(struct lpfc_hba *phba)
8170{
8171 int rc, i;
8172 LPFC_MBOXQ_t *pmb;
8173
8174 /* Set up MSI-X multi-message vectors */
8175 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8176 phba->msix_entries[i].entry = i;
8177
8178 /* Configure MSI-X capability structure */
8179 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
8180 ARRAY_SIZE(phba->msix_entries));
8181 if (rc) {
8182 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8183 "0420 PCI enable MSI-X failed (%d)\n", rc);
8184 goto msi_fail_out;
8185 }
8186 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8187 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8188 "0477 MSI-X entry[%d]: vector=x%x "
8189 "message=%d\n", i,
8190 phba->msix_entries[i].vector,
8191 phba->msix_entries[i].entry);
8192 /*
8193 * Assign MSI-X vectors to interrupt handlers
8194 */
8195
8196 /* vector-0 is associated to slow-path handler */
8197 rc = request_irq(phba->msix_entries[0].vector,
8198 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
8199 LPFC_SP_DRIVER_HANDLER_NAME, phba);
8200 if (rc) {
8201 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8202 "0421 MSI-X slow-path request_irq failed "
8203 "(%d)\n", rc);
8204 goto msi_fail_out;
8205 }
8206
8207 /* vector-1 is associated to fast-path handler */
8208 rc = request_irq(phba->msix_entries[1].vector,
8209 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
8210 LPFC_FP_DRIVER_HANDLER_NAME, phba);
8211
8212 if (rc) {
8213 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8214 "0429 MSI-X fast-path request_irq failed "
8215 "(%d)\n", rc);
8216 goto irq_fail_out;
8217 }
8218
8219 /*
8220 * Configure HBA MSI-X attention conditions to messages
8221 */
8222 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8223
8224 if (!pmb) {
8225 rc = -ENOMEM;
8226 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8227 "0474 Unable to allocate memory for issuing "
8228 "MBOX_CONFIG_MSI command\n");
8229 goto mem_fail_out;
8230 }
8231 rc = lpfc_config_msi(phba, pmb);
8232 if (rc)
8233 goto mbx_fail_out;
8234 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
8235 if (rc != MBX_SUCCESS) {
8236 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
8237 "0351 Config MSI mailbox command failed, "
8238 "mbxCmd x%x, mbxStatus x%x\n",
8239 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
8240 goto mbx_fail_out;
8241 }
8242
8243 /* Free memory allocated for mailbox command */
8244 mempool_free(pmb, phba->mbox_mem_pool);
8245 return rc;
8246
8247mbx_fail_out:
8248 /* Free memory allocated for mailbox command */
8249 mempool_free(pmb, phba->mbox_mem_pool);
8250
8251mem_fail_out:
8252 /* free the irq already requested */
8253 free_irq(phba->msix_entries[1].vector, phba);
8254
8255irq_fail_out:
8256 /* free the irq already requested */
8257 free_irq(phba->msix_entries[0].vector, phba);
8258
8259msi_fail_out:
8260 /* Unconfigure MSI-X capability structure */
8261 pci_disable_msix(phba->pcidev);
8262 return rc;
8263}
8264
8265/**
8266 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
8267 * @phba: pointer to lpfc hba data structure.
8268 *
8269 * This routine is invoked to release the MSI-X vectors and then disable the
8270 * MSI-X interrupt mode to device with SLI-3 interface spec.
8271 **/
8272static void
8273lpfc_sli_disable_msix(struct lpfc_hba *phba)
8274{
8275 int i;
8276
8277 /* Free up MSI-X multi-message vectors */
8278 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8279 free_irq(phba->msix_entries[i].vector, phba);
8280 /* Disable MSI-X */
8281 pci_disable_msix(phba->pcidev);
8282
8283 return;
8284}
8285
8286/**
8287 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
8288 * @phba: pointer to lpfc hba data structure.
8289 *
8290 * This routine is invoked to enable the MSI interrupt mode to device with
8291 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
8292 * enable the MSI vector. The device driver is responsible for calling the
8293 * request_irq() to register MSI vector with a interrupt the handler, which
8294 * is done in this function.
8295 *
8296 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008297 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008298 * other values - error
8299 */
8300static int
8301lpfc_sli_enable_msi(struct lpfc_hba *phba)
8302{
8303 int rc;
8304
8305 rc = pci_enable_msi(phba->pcidev);
8306 if (!rc)
8307 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8308 "0462 PCI enable MSI mode success.\n");
8309 else {
8310 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8311 "0471 PCI enable MSI mode failed (%d)\n", rc);
8312 return rc;
8313 }
8314
8315 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
8316 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8317 if (rc) {
8318 pci_disable_msi(phba->pcidev);
8319 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8320 "0478 MSI request_irq failed (%d)\n", rc);
8321 }
8322 return rc;
8323}
8324
8325/**
8326 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
8327 * @phba: pointer to lpfc hba data structure.
8328 *
8329 * This routine is invoked to disable the MSI interrupt mode to device with
8330 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
8331 * done request_irq() on before calling pci_disable_msi(). Failure to do so
8332 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
8333 * its vector.
8334 */
8335static void
8336lpfc_sli_disable_msi(struct lpfc_hba *phba)
8337{
8338 free_irq(phba->pcidev->irq, phba);
8339 pci_disable_msi(phba->pcidev);
8340 return;
8341}
8342
8343/**
8344 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
8345 * @phba: pointer to lpfc hba data structure.
8346 *
8347 * This routine is invoked to enable device interrupt and associate driver's
8348 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
8349 * spec. Depends on the interrupt mode configured to the driver, the driver
8350 * will try to fallback from the configured interrupt mode to an interrupt
8351 * mode which is supported by the platform, kernel, and device in the order
8352 * of:
8353 * MSI-X -> MSI -> IRQ.
8354 *
8355 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008356 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008357 * other values - error
8358 **/
8359static uint32_t
8360lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
8361{
8362 uint32_t intr_mode = LPFC_INTR_ERROR;
8363 int retval;
8364
8365 if (cfg_mode == 2) {
8366 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
8367 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
8368 if (!retval) {
8369 /* Now, try to enable MSI-X interrupt mode */
8370 retval = lpfc_sli_enable_msix(phba);
8371 if (!retval) {
8372 /* Indicate initialization to MSI-X mode */
8373 phba->intr_type = MSIX;
8374 intr_mode = 2;
8375 }
8376 }
8377 }
8378
8379 /* Fallback to MSI if MSI-X initialization failed */
8380 if (cfg_mode >= 1 && phba->intr_type == NONE) {
8381 retval = lpfc_sli_enable_msi(phba);
8382 if (!retval) {
8383 /* Indicate initialization to MSI mode */
8384 phba->intr_type = MSI;
8385 intr_mode = 1;
8386 }
8387 }
8388
8389 /* Fallback to INTx if both MSI-X/MSI initalization failed */
8390 if (phba->intr_type == NONE) {
8391 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
8392 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8393 if (!retval) {
8394 /* Indicate initialization to INTx mode */
8395 phba->intr_type = INTx;
8396 intr_mode = 0;
8397 }
8398 }
8399 return intr_mode;
8400}
8401
8402/**
8403 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
8404 * @phba: pointer to lpfc hba data structure.
8405 *
8406 * This routine is invoked to disable device interrupt and disassociate the
8407 * driver's interrupt handler(s) from interrupt vector(s) to device with
8408 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
8409 * release the interrupt vector(s) for the message signaled interrupt.
8410 **/
8411static void
8412lpfc_sli_disable_intr(struct lpfc_hba *phba)
8413{
8414 /* Disable the currently initialized interrupt mode */
8415 if (phba->intr_type == MSIX)
8416 lpfc_sli_disable_msix(phba);
8417 else if (phba->intr_type == MSI)
8418 lpfc_sli_disable_msi(phba);
8419 else if (phba->intr_type == INTx)
8420 free_irq(phba->pcidev->irq, phba);
8421
8422 /* Reset interrupt management states */
8423 phba->intr_type = NONE;
8424 phba->sli.slistat.sli_intr = 0;
8425
8426 return;
8427}
8428
8429/**
James Smart7bb03bb2013-04-17 20:19:16 -04008430 * lpfc_find_next_cpu - Find next available CPU that matches the phys_id
8431 * @phba: pointer to lpfc hba data structure.
8432 *
8433 * Find next available CPU to use for IRQ to CPU affinity.
8434 */
8435static int
8436lpfc_find_next_cpu(struct lpfc_hba *phba, uint32_t phys_id)
8437{
8438 struct lpfc_vector_map_info *cpup;
8439 int cpu;
8440
8441 cpup = phba->sli4_hba.cpu_map;
8442 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8443 /* CPU must be online */
8444 if (cpu_online(cpu)) {
8445 if ((cpup->irq == LPFC_VECTOR_MAP_EMPTY) &&
8446 (lpfc_used_cpu[cpu] == LPFC_VECTOR_MAP_EMPTY) &&
8447 (cpup->phys_id == phys_id)) {
8448 return cpu;
8449 }
8450 }
8451 cpup++;
8452 }
8453
8454 /*
8455 * If we get here, we have used ALL CPUs for the specific
8456 * phys_id. Now we need to clear out lpfc_used_cpu and start
8457 * reusing CPUs.
8458 */
8459
8460 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8461 if (lpfc_used_cpu[cpu] == phys_id)
8462 lpfc_used_cpu[cpu] = LPFC_VECTOR_MAP_EMPTY;
8463 }
8464
8465 cpup = phba->sli4_hba.cpu_map;
8466 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8467 /* CPU must be online */
8468 if (cpu_online(cpu)) {
8469 if ((cpup->irq == LPFC_VECTOR_MAP_EMPTY) &&
8470 (cpup->phys_id == phys_id)) {
8471 return cpu;
8472 }
8473 }
8474 cpup++;
8475 }
8476 return LPFC_VECTOR_MAP_EMPTY;
8477}
8478
8479/**
8480 * lpfc_sli4_set_affinity - Set affinity for HBA IRQ vectors
8481 * @phba: pointer to lpfc hba data structure.
8482 * @vectors: number of HBA vectors
8483 *
8484 * Affinitize MSIX IRQ vectors to CPUs. Try to equally spread vector
8485 * affinization across multple physical CPUs (numa nodes).
8486 * In addition, this routine will assign an IO channel for each CPU
8487 * to use when issuing I/Os.
8488 */
8489static int
8490lpfc_sli4_set_affinity(struct lpfc_hba *phba, int vectors)
8491{
8492 int i, idx, saved_chann, used_chann, cpu, phys_id;
James Smartec2087a2013-09-06 12:20:36 -04008493 int max_phys_id, min_phys_id;
8494 int num_io_channel, first_cpu, chan;
James Smart7bb03bb2013-04-17 20:19:16 -04008495 struct lpfc_vector_map_info *cpup;
8496#ifdef CONFIG_X86
8497 struct cpuinfo_x86 *cpuinfo;
8498#endif
8499 struct cpumask *mask;
8500 uint8_t chann[LPFC_FCP_IO_CHAN_MAX+1];
8501
8502 /* If there is no mapping, just return */
8503 if (!phba->cfg_fcp_cpu_map)
8504 return 1;
8505
8506 /* Init cpu_map array */
8507 memset(phba->sli4_hba.cpu_map, 0xff,
8508 (sizeof(struct lpfc_vector_map_info) *
8509 phba->sli4_hba.num_present_cpu));
8510
8511 max_phys_id = 0;
James Smartec2087a2013-09-06 12:20:36 -04008512 min_phys_id = 0xff;
James Smart7bb03bb2013-04-17 20:19:16 -04008513 phys_id = 0;
8514 num_io_channel = 0;
8515 first_cpu = LPFC_VECTOR_MAP_EMPTY;
8516
8517 /* Update CPU map with physical id and core id of each CPU */
8518 cpup = phba->sli4_hba.cpu_map;
8519 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8520#ifdef CONFIG_X86
8521 cpuinfo = &cpu_data(cpu);
8522 cpup->phys_id = cpuinfo->phys_proc_id;
8523 cpup->core_id = cpuinfo->cpu_core_id;
8524#else
8525 /* No distinction between CPUs for other platforms */
8526 cpup->phys_id = 0;
8527 cpup->core_id = 0;
8528#endif
8529
8530 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8531 "3328 CPU physid %d coreid %d\n",
8532 cpup->phys_id, cpup->core_id);
8533
8534 if (cpup->phys_id > max_phys_id)
8535 max_phys_id = cpup->phys_id;
James Smartec2087a2013-09-06 12:20:36 -04008536 if (cpup->phys_id < min_phys_id)
8537 min_phys_id = cpup->phys_id;
James Smart7bb03bb2013-04-17 20:19:16 -04008538 cpup++;
8539 }
8540
James Smartec2087a2013-09-06 12:20:36 -04008541 phys_id = min_phys_id;
James Smart7bb03bb2013-04-17 20:19:16 -04008542 /* Now associate the HBA vectors with specific CPUs */
8543 for (idx = 0; idx < vectors; idx++) {
8544 cpup = phba->sli4_hba.cpu_map;
8545 cpu = lpfc_find_next_cpu(phba, phys_id);
8546 if (cpu == LPFC_VECTOR_MAP_EMPTY) {
8547
8548 /* Try for all phys_id's */
8549 for (i = 1; i < max_phys_id; i++) {
8550 phys_id++;
8551 if (phys_id > max_phys_id)
James Smartec2087a2013-09-06 12:20:36 -04008552 phys_id = min_phys_id;
James Smart7bb03bb2013-04-17 20:19:16 -04008553 cpu = lpfc_find_next_cpu(phba, phys_id);
8554 if (cpu == LPFC_VECTOR_MAP_EMPTY)
8555 continue;
8556 goto found;
8557 }
8558
James Smartec2087a2013-09-06 12:20:36 -04008559 /* Use round robin for scheduling */
8560 phba->cfg_fcp_io_sched = LPFC_FCP_SCHED_ROUND_ROBIN;
8561 chan = 0;
8562 cpup = phba->sli4_hba.cpu_map;
8563 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
8564 cpup->channel_id = chan;
8565 cpup++;
8566 chan++;
8567 if (chan >= phba->cfg_fcp_io_channel)
8568 chan = 0;
8569 }
8570
James Smart7bb03bb2013-04-17 20:19:16 -04008571 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8572 "3329 Cannot set affinity:"
8573 "Error mapping vector %d (%d)\n",
8574 idx, vectors);
8575 return 0;
8576 }
8577found:
8578 cpup += cpu;
8579 if (phba->cfg_fcp_cpu_map == LPFC_DRIVER_CPU_MAP)
8580 lpfc_used_cpu[cpu] = phys_id;
8581
8582 /* Associate vector with selected CPU */
8583 cpup->irq = phba->sli4_hba.msix_entries[idx].vector;
8584
8585 /* Associate IO channel with selected CPU */
8586 cpup->channel_id = idx;
8587 num_io_channel++;
8588
8589 if (first_cpu == LPFC_VECTOR_MAP_EMPTY)
8590 first_cpu = cpu;
8591
8592 /* Now affinitize to the selected CPU */
8593 mask = &cpup->maskbits;
8594 cpumask_clear(mask);
8595 cpumask_set_cpu(cpu, mask);
8596 i = irq_set_affinity_hint(phba->sli4_hba.msix_entries[idx].
8597 vector, mask);
8598
8599 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8600 "3330 Set Affinity: CPU %d channel %d "
8601 "irq %d (%x)\n",
8602 cpu, cpup->channel_id,
8603 phba->sli4_hba.msix_entries[idx].vector, i);
8604
8605 /* Spread vector mapping across multple physical CPU nodes */
8606 phys_id++;
8607 if (phys_id > max_phys_id)
James Smartec2087a2013-09-06 12:20:36 -04008608 phys_id = min_phys_id;
James Smart7bb03bb2013-04-17 20:19:16 -04008609 }
8610
8611 /*
8612 * Finally fill in the IO channel for any remaining CPUs.
8613 * At this point, all IO channels have been assigned to a specific
8614 * MSIx vector, mapped to a specific CPU.
8615 * Base the remaining IO channel assigned, to IO channels already
8616 * assigned to other CPUs on the same phys_id.
8617 */
James Smartec2087a2013-09-06 12:20:36 -04008618 for (i = min_phys_id; i <= max_phys_id; i++) {
James Smart7bb03bb2013-04-17 20:19:16 -04008619 /*
8620 * If there are no io channels already mapped to
8621 * this phys_id, just round robin thru the io_channels.
8622 * Setup chann[] for round robin.
8623 */
8624 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++)
8625 chann[idx] = idx;
8626
8627 saved_chann = 0;
8628 used_chann = 0;
8629
8630 /*
8631 * First build a list of IO channels already assigned
8632 * to this phys_id before reassigning the same IO
8633 * channels to the remaining CPUs.
8634 */
8635 cpup = phba->sli4_hba.cpu_map;
8636 cpu = first_cpu;
8637 cpup += cpu;
8638 for (idx = 0; idx < phba->sli4_hba.num_present_cpu;
8639 idx++) {
8640 if (cpup->phys_id == i) {
8641 /*
8642 * Save any IO channels that are
8643 * already mapped to this phys_id.
8644 */
8645 if (cpup->irq != LPFC_VECTOR_MAP_EMPTY) {
8646 chann[saved_chann] =
8647 cpup->channel_id;
8648 saved_chann++;
8649 goto out;
8650 }
8651
8652 /* See if we are using round-robin */
8653 if (saved_chann == 0)
8654 saved_chann =
8655 phba->cfg_fcp_io_channel;
8656
8657 /* Associate next IO channel with CPU */
8658 cpup->channel_id = chann[used_chann];
8659 num_io_channel++;
8660 used_chann++;
8661 if (used_chann == saved_chann)
8662 used_chann = 0;
8663
8664 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8665 "3331 Set IO_CHANN "
8666 "CPU %d channel %d\n",
8667 idx, cpup->channel_id);
8668 }
8669out:
8670 cpu++;
8671 if (cpu >= phba->sli4_hba.num_present_cpu) {
8672 cpup = phba->sli4_hba.cpu_map;
8673 cpu = 0;
8674 } else {
8675 cpup++;
8676 }
8677 }
8678 }
8679
8680 if (phba->sli4_hba.num_online_cpu != phba->sli4_hba.num_present_cpu) {
8681 cpup = phba->sli4_hba.cpu_map;
8682 for (idx = 0; idx < phba->sli4_hba.num_present_cpu; idx++) {
8683 if (cpup->channel_id == LPFC_VECTOR_MAP_EMPTY) {
8684 cpup->channel_id = 0;
8685 num_io_channel++;
8686
8687 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8688 "3332 Assign IO_CHANN "
8689 "CPU %d channel %d\n",
8690 idx, cpup->channel_id);
8691 }
8692 cpup++;
8693 }
8694 }
8695
8696 /* Sanity check */
8697 if (num_io_channel != phba->sli4_hba.num_present_cpu)
8698 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8699 "3333 Set affinity mismatch:"
James Smartec2087a2013-09-06 12:20:36 -04008700 "%d chann != %d cpus: %d vectors\n",
James Smart7bb03bb2013-04-17 20:19:16 -04008701 num_io_channel, phba->sli4_hba.num_present_cpu,
8702 vectors);
8703
James Smartec2087a2013-09-06 12:20:36 -04008704 /* Enable using cpu affinity for scheduling */
James Smart7bb03bb2013-04-17 20:19:16 -04008705 phba->cfg_fcp_io_sched = LPFC_FCP_SCHED_BY_CPU;
8706 return 1;
8707}
8708
8709
8710/**
James Smartda0436e2009-05-22 14:51:39 -04008711 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
8712 * @phba: pointer to lpfc hba data structure.
8713 *
8714 * This routine is invoked to enable the MSI-X interrupt vectors to device
8715 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
8716 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
8717 * enables either all or nothing, depending on the current availability of
8718 * PCI vector resources. The device driver is responsible for calling the
8719 * individual request_irq() to register each MSI-X vector with a interrupt
8720 * handler, which is done in this function. Note that later when device is
8721 * unloading, the driver should always call free_irq() on all MSI-X vectors
8722 * it has done request_irq() on before calling pci_disable_msix(). Failure
8723 * to do so results in a BUG_ON() and a device will be left with MSI-X
8724 * enabled and leaks its vectors.
8725 *
8726 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008727 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008728 * other values - error
8729 **/
8730static int
8731lpfc_sli4_enable_msix(struct lpfc_hba *phba)
8732{
James Smart75baf692010-06-08 18:31:21 -04008733 int vectors, rc, index;
James Smartda0436e2009-05-22 14:51:39 -04008734
8735 /* Set up MSI-X multi-message vectors */
James Smart82c3e9b2012-09-29 11:29:50 -04008736 for (index = 0; index < phba->cfg_fcp_io_channel; index++)
James Smartda0436e2009-05-22 14:51:39 -04008737 phba->sli4_hba.msix_entries[index].entry = index;
8738
8739 /* Configure MSI-X capability structure */
James Smart82c3e9b2012-09-29 11:29:50 -04008740 vectors = phba->cfg_fcp_io_channel;
James Smart1ba981f2014-02-20 09:56:45 -05008741 if (phba->cfg_fof) {
8742 phba->sli4_hba.msix_entries[index].entry = index;
8743 vectors++;
8744 }
James Smart75baf692010-06-08 18:31:21 -04008745enable_msix_vectors:
James Smartda0436e2009-05-22 14:51:39 -04008746 rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
James Smart75baf692010-06-08 18:31:21 -04008747 vectors);
8748 if (rc > 1) {
8749 vectors = rc;
8750 goto enable_msix_vectors;
8751 } else if (rc) {
James Smartda0436e2009-05-22 14:51:39 -04008752 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8753 "0484 PCI enable MSI-X failed (%d)\n", rc);
8754 goto msi_fail_out;
8755 }
James Smart75baf692010-06-08 18:31:21 -04008756
James Smartda0436e2009-05-22 14:51:39 -04008757 /* Log MSI-X vector assignment */
James Smart75baf692010-06-08 18:31:21 -04008758 for (index = 0; index < vectors; index++)
James Smartda0436e2009-05-22 14:51:39 -04008759 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8760 "0489 MSI-X entry[%d]: vector=x%x "
8761 "message=%d\n", index,
8762 phba->sli4_hba.msix_entries[index].vector,
8763 phba->sli4_hba.msix_entries[index].entry);
James Smart67d12732012-08-03 12:36:13 -04008764
James Smart7bb03bb2013-04-17 20:19:16 -04008765 /* Assign MSI-X vectors to interrupt handlers */
James Smart67d12732012-08-03 12:36:13 -04008766 for (index = 0; index < vectors; index++) {
James Smart4305f182012-08-03 12:36:33 -04008767 memset(&phba->sli4_hba.handler_name[index], 0, 16);
8768 sprintf((char *)&phba->sli4_hba.handler_name[index],
8769 LPFC_DRIVER_HANDLER_NAME"%d", index);
James Smartda0436e2009-05-22 14:51:39 -04008770
James Smart67d12732012-08-03 12:36:13 -04008771 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8772 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
James Smartba20c852012-08-03 12:36:52 -04008773 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].fcp_eq_in_use, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008774 if (phba->cfg_fof && (index == (vectors - 1)))
8775 rc = request_irq(
8776 phba->sli4_hba.msix_entries[index].vector,
8777 &lpfc_sli4_fof_intr_handler, IRQF_SHARED,
8778 (char *)&phba->sli4_hba.handler_name[index],
8779 &phba->sli4_hba.fcp_eq_hdl[index]);
8780 else
8781 rc = request_irq(
8782 phba->sli4_hba.msix_entries[index].vector,
James Smart67d12732012-08-03 12:36:13 -04008783 &lpfc_sli4_hba_intr_handler, IRQF_SHARED,
James Smart4305f182012-08-03 12:36:33 -04008784 (char *)&phba->sli4_hba.handler_name[index],
James Smart67d12732012-08-03 12:36:13 -04008785 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -04008786 if (rc) {
8787 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8788 "0486 MSI-X fast-path (%d) "
8789 "request_irq failed (%d)\n", index, rc);
8790 goto cfg_fail_out;
8791 }
8792 }
8793
James Smart1ba981f2014-02-20 09:56:45 -05008794 if (phba->cfg_fof)
8795 vectors--;
8796
James Smart82c3e9b2012-09-29 11:29:50 -04008797 if (vectors != phba->cfg_fcp_io_channel) {
8798 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8799 "3238 Reducing IO channels to match number of "
8800 "MSI-X vectors, requested %d got %d\n",
8801 phba->cfg_fcp_io_channel, vectors);
8802 phba->cfg_fcp_io_channel = vectors;
8803 }
James Smart7bb03bb2013-04-17 20:19:16 -04008804
8805 lpfc_sli4_set_affinity(phba, vectors);
James Smartda0436e2009-05-22 14:51:39 -04008806 return rc;
8807
8808cfg_fail_out:
8809 /* free the irq already requested */
James Smartacbd8612013-09-06 12:21:19 -04008810 for (--index; index >= 0; index--) {
8811 irq_set_affinity_hint(phba->sli4_hba.msix_entries[index].
8812 vector, NULL);
James Smart67d12732012-08-03 12:36:13 -04008813 free_irq(phba->sli4_hba.msix_entries[index].vector,
8814 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartacbd8612013-09-06 12:21:19 -04008815 }
James Smartda0436e2009-05-22 14:51:39 -04008816
8817msi_fail_out:
8818 /* Unconfigure MSI-X capability structure */
8819 pci_disable_msix(phba->pcidev);
8820 return rc;
8821}
8822
8823/**
8824 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
8825 * @phba: pointer to lpfc hba data structure.
8826 *
8827 * This routine is invoked to release the MSI-X vectors and then disable the
8828 * MSI-X interrupt mode to device with SLI-4 interface spec.
8829 **/
8830static void
8831lpfc_sli4_disable_msix(struct lpfc_hba *phba)
8832{
8833 int index;
8834
8835 /* Free up MSI-X multi-message vectors */
James Smartacbd8612013-09-06 12:21:19 -04008836 for (index = 0; index < phba->cfg_fcp_io_channel; index++) {
8837 irq_set_affinity_hint(phba->sli4_hba.msix_entries[index].
8838 vector, NULL);
James Smartda0436e2009-05-22 14:51:39 -04008839 free_irq(phba->sli4_hba.msix_entries[index].vector,
James Smart67d12732012-08-03 12:36:13 -04008840 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartacbd8612013-09-06 12:21:19 -04008841 }
James Smart1ba981f2014-02-20 09:56:45 -05008842 if (phba->cfg_fof) {
8843 free_irq(phba->sli4_hba.msix_entries[index].vector,
8844 &phba->sli4_hba.fcp_eq_hdl[index]);
8845 }
James Smartda0436e2009-05-22 14:51:39 -04008846 /* Disable MSI-X */
8847 pci_disable_msix(phba->pcidev);
8848
8849 return;
8850}
8851
8852/**
8853 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
8854 * @phba: pointer to lpfc hba data structure.
8855 *
8856 * This routine is invoked to enable the MSI interrupt mode to device with
8857 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
8858 * to enable the MSI vector. The device driver is responsible for calling
8859 * the request_irq() to register MSI vector with a interrupt the handler,
8860 * which is done in this function.
8861 *
8862 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008863 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008864 * other values - error
8865 **/
8866static int
8867lpfc_sli4_enable_msi(struct lpfc_hba *phba)
8868{
8869 int rc, index;
8870
8871 rc = pci_enable_msi(phba->pcidev);
8872 if (!rc)
8873 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8874 "0487 PCI enable MSI mode success.\n");
8875 else {
8876 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8877 "0488 PCI enable MSI mode failed (%d)\n", rc);
8878 return rc;
8879 }
8880
8881 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
8882 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8883 if (rc) {
8884 pci_disable_msi(phba->pcidev);
8885 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8886 "0490 MSI request_irq failed (%d)\n", rc);
James Smart75baf692010-06-08 18:31:21 -04008887 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008888 }
8889
James Smart67d12732012-08-03 12:36:13 -04008890 for (index = 0; index < phba->cfg_fcp_io_channel; index++) {
James Smartda0436e2009-05-22 14:51:39 -04008891 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8892 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
8893 }
8894
James Smart1ba981f2014-02-20 09:56:45 -05008895 if (phba->cfg_fof) {
8896 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8897 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
8898 }
James Smart75baf692010-06-08 18:31:21 -04008899 return 0;
James Smartda0436e2009-05-22 14:51:39 -04008900}
8901
8902/**
8903 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
8904 * @phba: pointer to lpfc hba data structure.
8905 *
8906 * This routine is invoked to disable the MSI interrupt mode to device with
8907 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
8908 * done request_irq() on before calling pci_disable_msi(). Failure to do so
8909 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
8910 * its vector.
8911 **/
8912static void
8913lpfc_sli4_disable_msi(struct lpfc_hba *phba)
8914{
8915 free_irq(phba->pcidev->irq, phba);
8916 pci_disable_msi(phba->pcidev);
8917 return;
8918}
8919
8920/**
8921 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
8922 * @phba: pointer to lpfc hba data structure.
8923 *
8924 * This routine is invoked to enable device interrupt and associate driver's
8925 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
8926 * interface spec. Depends on the interrupt mode configured to the driver,
8927 * the driver will try to fallback from the configured interrupt mode to an
8928 * interrupt mode which is supported by the platform, kernel, and device in
8929 * the order of:
8930 * MSI-X -> MSI -> IRQ.
8931 *
8932 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008933 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008934 * other values - error
8935 **/
8936static uint32_t
8937lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
8938{
8939 uint32_t intr_mode = LPFC_INTR_ERROR;
8940 int retval, index;
8941
8942 if (cfg_mode == 2) {
8943 /* Preparation before conf_msi mbox cmd */
8944 retval = 0;
8945 if (!retval) {
8946 /* Now, try to enable MSI-X interrupt mode */
8947 retval = lpfc_sli4_enable_msix(phba);
8948 if (!retval) {
8949 /* Indicate initialization to MSI-X mode */
8950 phba->intr_type = MSIX;
8951 intr_mode = 2;
8952 }
8953 }
8954 }
8955
8956 /* Fallback to MSI if MSI-X initialization failed */
8957 if (cfg_mode >= 1 && phba->intr_type == NONE) {
8958 retval = lpfc_sli4_enable_msi(phba);
8959 if (!retval) {
8960 /* Indicate initialization to MSI mode */
8961 phba->intr_type = MSI;
8962 intr_mode = 1;
8963 }
8964 }
8965
8966 /* Fallback to INTx if both MSI-X/MSI initalization failed */
8967 if (phba->intr_type == NONE) {
8968 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
8969 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8970 if (!retval) {
8971 /* Indicate initialization to INTx mode */
8972 phba->intr_type = INTx;
8973 intr_mode = 0;
James Smart67d12732012-08-03 12:36:13 -04008974 for (index = 0; index < phba->cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04008975 index++) {
8976 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8977 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
James Smartba20c852012-08-03 12:36:52 -04008978 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].
8979 fcp_eq_in_use, 1);
James Smartda0436e2009-05-22 14:51:39 -04008980 }
James Smart1ba981f2014-02-20 09:56:45 -05008981 if (phba->cfg_fof) {
8982 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8983 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
8984 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].
8985 fcp_eq_in_use, 1);
8986 }
James Smartda0436e2009-05-22 14:51:39 -04008987 }
8988 }
8989 return intr_mode;
8990}
8991
8992/**
8993 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
8994 * @phba: pointer to lpfc hba data structure.
8995 *
8996 * This routine is invoked to disable device interrupt and disassociate
8997 * the driver's interrupt handler(s) from interrupt vector(s) to device
8998 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
8999 * will release the interrupt vector(s) for the message signaled interrupt.
9000 **/
9001static void
9002lpfc_sli4_disable_intr(struct lpfc_hba *phba)
9003{
9004 /* Disable the currently initialized interrupt mode */
9005 if (phba->intr_type == MSIX)
9006 lpfc_sli4_disable_msix(phba);
9007 else if (phba->intr_type == MSI)
9008 lpfc_sli4_disable_msi(phba);
9009 else if (phba->intr_type == INTx)
9010 free_irq(phba->pcidev->irq, phba);
9011
9012 /* Reset interrupt management states */
9013 phba->intr_type = NONE;
9014 phba->sli.slistat.sli_intr = 0;
9015
9016 return;
9017}
9018
9019/**
James Smart3772a992009-05-22 14:50:54 -04009020 * lpfc_unset_hba - Unset SLI3 hba device initialization
9021 * @phba: pointer to lpfc hba data structure.
9022 *
9023 * This routine is invoked to unset the HBA device initialization steps to
9024 * a device with SLI-3 interface spec.
9025 **/
9026static void
9027lpfc_unset_hba(struct lpfc_hba *phba)
9028{
9029 struct lpfc_vport *vport = phba->pport;
9030 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
9031
9032 spin_lock_irq(shost->host_lock);
9033 vport->load_flag |= FC_UNLOADING;
9034 spin_unlock_irq(shost->host_lock);
9035
James Smart72859902012-01-18 16:25:38 -05009036 kfree(phba->vpi_bmask);
9037 kfree(phba->vpi_ids);
9038
James Smart3772a992009-05-22 14:50:54 -04009039 lpfc_stop_hba_timers(phba);
9040
9041 phba->pport->work_port_events = 0;
9042
9043 lpfc_sli_hba_down(phba);
9044
9045 lpfc_sli_brdrestart(phba);
9046
9047 lpfc_sli_disable_intr(phba);
9048
9049 return;
9050}
9051
9052/**
James Smart5af5eee2010-10-22 11:06:38 -04009053 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
9054 * @phba: Pointer to HBA context object.
9055 *
9056 * This function is called in the SLI4 code path to wait for completion
9057 * of device's XRIs exchange busy. It will check the XRI exchange busy
9058 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
9059 * that, it will check the XRI exchange busy on outstanding FCP and ELS
9060 * I/Os every 30 seconds, log error message, and wait forever. Only when
9061 * all XRI exchange busy complete, the driver unload shall proceed with
9062 * invoking the function reset ioctl mailbox command to the CNA and the
9063 * the rest of the driver unload resource release.
9064 **/
9065static void
9066lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
9067{
9068 int wait_time = 0;
9069 int fcp_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
9070 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
9071
9072 while (!fcp_xri_cmpl || !els_xri_cmpl) {
9073 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
9074 if (!fcp_xri_cmpl)
9075 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9076 "2877 FCP XRI exchange busy "
9077 "wait time: %d seconds.\n",
9078 wait_time/1000);
9079 if (!els_xri_cmpl)
9080 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9081 "2878 ELS XRI exchange busy "
9082 "wait time: %d seconds.\n",
9083 wait_time/1000);
9084 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
9085 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
9086 } else {
9087 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
9088 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
9089 }
9090 fcp_xri_cmpl =
9091 list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
9092 els_xri_cmpl =
9093 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
9094 }
9095}
9096
9097/**
James Smartda0436e2009-05-22 14:51:39 -04009098 * lpfc_sli4_hba_unset - Unset the fcoe hba
9099 * @phba: Pointer to HBA context object.
9100 *
9101 * This function is called in the SLI4 code path to reset the HBA's FCoE
9102 * function. The caller is not required to hold any lock. This routine
9103 * issues PCI function reset mailbox command to reset the FCoE function.
9104 * At the end of the function, it calls lpfc_hba_down_post function to
9105 * free any pending commands.
9106 **/
9107static void
9108lpfc_sli4_hba_unset(struct lpfc_hba *phba)
9109{
9110 int wait_cnt = 0;
9111 LPFC_MBOXQ_t *mboxq;
James Smart912e3ac2011-05-24 11:42:11 -04009112 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -04009113
9114 lpfc_stop_hba_timers(phba);
9115 phba->sli4_hba.intr_enable = 0;
9116
9117 /*
9118 * Gracefully wait out the potential current outstanding asynchronous
9119 * mailbox command.
9120 */
9121
9122 /* First, block any pending async mailbox command from posted */
9123 spin_lock_irq(&phba->hbalock);
9124 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
9125 spin_unlock_irq(&phba->hbalock);
9126 /* Now, trying to wait it out if we can */
9127 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9128 msleep(10);
9129 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
9130 break;
9131 }
9132 /* Forcefully release the outstanding mailbox command if timed out */
9133 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9134 spin_lock_irq(&phba->hbalock);
9135 mboxq = phba->sli.mbox_active;
9136 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
9137 __lpfc_mbox_cmpl_put(phba, mboxq);
9138 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9139 phba->sli.mbox_active = NULL;
9140 spin_unlock_irq(&phba->hbalock);
9141 }
9142
James Smart5af5eee2010-10-22 11:06:38 -04009143 /* Abort all iocbs associated with the hba */
9144 lpfc_sli_hba_iocb_abort(phba);
9145
9146 /* Wait for completion of device XRI exchange busy */
9147 lpfc_sli4_xri_exchange_busy_wait(phba);
9148
James Smartda0436e2009-05-22 14:51:39 -04009149 /* Disable PCI subsystem interrupt */
9150 lpfc_sli4_disable_intr(phba);
9151
James Smart912e3ac2011-05-24 11:42:11 -04009152 /* Disable SR-IOV if enabled */
9153 if (phba->cfg_sriov_nr_virtfn)
9154 pci_disable_sriov(pdev);
9155
James Smartda0436e2009-05-22 14:51:39 -04009156 /* Stop kthread signal shall trigger work_done one more time */
9157 kthread_stop(phba->worker_thread);
9158
James Smart3677a3a2010-09-29 11:19:14 -04009159 /* Reset SLI4 HBA FCoE function */
9160 lpfc_pci_function_reset(phba);
James Smart5350d872011-10-10 21:33:49 -04009161 lpfc_sli4_queue_destroy(phba);
James Smart3677a3a2010-09-29 11:19:14 -04009162
James Smartda0436e2009-05-22 14:51:39 -04009163 /* Stop the SLI4 device port */
9164 phba->pport->work_port_events = 0;
9165}
9166
James Smart28baac72010-02-12 14:42:03 -05009167 /**
9168 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
9169 * @phba: Pointer to HBA context object.
9170 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
9171 *
9172 * This function is called in the SLI4 code path to read the port's
9173 * sli4 capabilities.
9174 *
9175 * This function may be be called from any context that can block-wait
9176 * for the completion. The expectation is that this routine is called
9177 * typically from probe_one or from the online routine.
9178 **/
9179int
9180lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9181{
9182 int rc;
9183 struct lpfc_mqe *mqe;
9184 struct lpfc_pc_sli4_params *sli4_params;
9185 uint32_t mbox_tmo;
9186
9187 rc = 0;
9188 mqe = &mboxq->u.mqe;
9189
9190 /* Read the port's SLI4 Parameters port capabilities */
James Smartfedd3b72011-02-16 12:39:24 -05009191 lpfc_pc_sli4_params(mboxq);
James Smart28baac72010-02-12 14:42:03 -05009192 if (!phba->sli4_hba.intr_enable)
9193 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9194 else {
James Smarta183a152011-10-10 21:32:43 -04009195 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart28baac72010-02-12 14:42:03 -05009196 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
9197 }
9198
9199 if (unlikely(rc))
9200 return 1;
9201
9202 sli4_params = &phba->sli4_hba.pc_sli4_params;
9203 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
9204 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
9205 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
9206 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
9207 &mqe->un.sli4_params);
9208 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
9209 &mqe->un.sli4_params);
9210 sli4_params->proto_types = mqe->un.sli4_params.word3;
9211 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
9212 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
9213 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
9214 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
9215 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
9216 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
9217 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
9218 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
9219 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
9220 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
9221 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
9222 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
9223 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
9224 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
9225 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
9226 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
9227 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
9228 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
9229 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
9230 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
James Smart05580562011-05-24 11:40:48 -04009231
9232 /* Make sure that sge_supp_len can be handled by the driver */
9233 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
9234 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
9235
James Smart28baac72010-02-12 14:42:03 -05009236 return rc;
9237}
9238
James Smartda0436e2009-05-22 14:51:39 -04009239/**
James Smartfedd3b72011-02-16 12:39:24 -05009240 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
9241 * @phba: Pointer to HBA context object.
9242 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
9243 *
9244 * This function is called in the SLI4 code path to read the port's
9245 * sli4 capabilities.
9246 *
9247 * This function may be be called from any context that can block-wait
9248 * for the completion. The expectation is that this routine is called
9249 * typically from probe_one or from the online routine.
9250 **/
9251int
9252lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9253{
9254 int rc;
9255 struct lpfc_mqe *mqe = &mboxq->u.mqe;
9256 struct lpfc_pc_sli4_params *sli4_params;
James Smarta183a152011-10-10 21:32:43 -04009257 uint32_t mbox_tmo;
James Smartfedd3b72011-02-16 12:39:24 -05009258 int length;
9259 struct lpfc_sli4_parameters *mbx_sli4_parameters;
9260
James Smart6d368e52011-05-24 11:44:12 -04009261 /*
9262 * By default, the driver assumes the SLI4 port requires RPI
9263 * header postings. The SLI4_PARAM response will correct this
9264 * assumption.
9265 */
9266 phba->sli4_hba.rpi_hdrs_in_use = 1;
9267
James Smartfedd3b72011-02-16 12:39:24 -05009268 /* Read the port's SLI4 Config Parameters */
9269 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
9270 sizeof(struct lpfc_sli4_cfg_mhdr));
9271 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
9272 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
9273 length, LPFC_SLI4_MBX_EMBED);
9274 if (!phba->sli4_hba.intr_enable)
9275 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -04009276 else {
9277 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
9278 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
9279 }
James Smartfedd3b72011-02-16 12:39:24 -05009280 if (unlikely(rc))
9281 return rc;
9282 sli4_params = &phba->sli4_hba.pc_sli4_params;
9283 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
9284 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
9285 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
9286 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
9287 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
9288 mbx_sli4_parameters);
9289 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
9290 mbx_sli4_parameters);
9291 if (bf_get(cfg_phwq, mbx_sli4_parameters))
9292 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
9293 else
9294 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
9295 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
9296 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
James Smart1ba981f2014-02-20 09:56:45 -05009297 sli4_params->oas_supported = bf_get(cfg_oas, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -05009298 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
9299 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
9300 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
9301 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
James Smart0c651872013-07-15 18:33:23 -04009302 sli4_params->wqsize = bf_get(cfg_wqsize, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -05009303 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
9304 mbx_sli4_parameters);
9305 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
9306 mbx_sli4_parameters);
James Smart6d368e52011-05-24 11:44:12 -04009307 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
9308 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
James Smart05580562011-05-24 11:40:48 -04009309
9310 /* Make sure that sge_supp_len can be handled by the driver */
9311 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
9312 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
9313
James Smartfedd3b72011-02-16 12:39:24 -05009314 return 0;
9315}
9316
9317/**
James Smart3772a992009-05-22 14:50:54 -04009318 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
9319 * @pdev: pointer to PCI device
9320 * @pid: pointer to PCI device identifier
9321 *
9322 * This routine is to be called to attach a device with SLI-3 interface spec
9323 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
9324 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
9325 * information of the device and driver to see if the driver state that it can
9326 * support this kind of device. If the match is successful, the driver core
9327 * invokes this routine. If this routine determines it can claim the HBA, it
9328 * does all the initialization that it needs to do to handle the HBA properly.
9329 *
9330 * Return code
9331 * 0 - driver can claim the device
9332 * negative value - driver can not claim the device
9333 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009334static int
James Smart3772a992009-05-22 14:50:54 -04009335lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
9336{
9337 struct lpfc_hba *phba;
9338 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04009339 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -04009340 int error;
9341 uint32_t cfg_mode, intr_mode;
9342
9343 /* Allocate memory for HBA structure */
9344 phba = lpfc_hba_alloc(pdev);
9345 if (!phba)
9346 return -ENOMEM;
9347
9348 /* Perform generic PCI device enabling operation */
9349 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -04009350 if (error)
James Smart3772a992009-05-22 14:50:54 -04009351 goto out_free_phba;
James Smart3772a992009-05-22 14:50:54 -04009352
9353 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
9354 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
9355 if (error)
9356 goto out_disable_pci_dev;
9357
9358 /* Set up SLI-3 specific device PCI memory space */
9359 error = lpfc_sli_pci_mem_setup(phba);
9360 if (error) {
9361 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9362 "1402 Failed to set up pci memory space.\n");
9363 goto out_disable_pci_dev;
9364 }
9365
9366 /* Set up phase-1 common device driver resources */
9367 error = lpfc_setup_driver_resource_phase1(phba);
9368 if (error) {
9369 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9370 "1403 Failed to set up driver resource.\n");
9371 goto out_unset_pci_mem_s3;
9372 }
9373
9374 /* Set up SLI-3 specific device driver resources */
9375 error = lpfc_sli_driver_resource_setup(phba);
9376 if (error) {
9377 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9378 "1404 Failed to set up driver resource.\n");
9379 goto out_unset_pci_mem_s3;
9380 }
9381
9382 /* Initialize and populate the iocb list per host */
9383 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
9384 if (error) {
9385 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9386 "1405 Failed to initialize iocb list.\n");
9387 goto out_unset_driver_resource_s3;
9388 }
9389
9390 /* Set up common device driver resources */
9391 error = lpfc_setup_driver_resource_phase2(phba);
9392 if (error) {
9393 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9394 "1406 Failed to set up driver resource.\n");
9395 goto out_free_iocb_list;
9396 }
9397
James Smart079b5c92011-08-21 21:48:49 -04009398 /* Get the default values for Model Name and Description */
9399 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
9400
James Smart3772a992009-05-22 14:50:54 -04009401 /* Create SCSI host to the physical port */
9402 error = lpfc_create_shost(phba);
9403 if (error) {
9404 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9405 "1407 Failed to create scsi host.\n");
9406 goto out_unset_driver_resource;
9407 }
9408
9409 /* Configure sysfs attributes */
9410 vport = phba->pport;
9411 error = lpfc_alloc_sysfs_attr(vport);
9412 if (error) {
9413 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9414 "1476 Failed to allocate sysfs attr\n");
9415 goto out_destroy_shost;
9416 }
9417
James Smart6669f9b2009-10-02 15:16:45 -04009418 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -04009419 /* Now, trying to enable interrupt and bring up the device */
9420 cfg_mode = phba->cfg_use_msi;
9421 while (true) {
9422 /* Put device to a known state before enabling interrupt */
9423 lpfc_stop_port(phba);
9424 /* Configure and enable interrupt */
9425 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
9426 if (intr_mode == LPFC_INTR_ERROR) {
9427 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9428 "0431 Failed to enable interrupt.\n");
9429 error = -ENODEV;
9430 goto out_free_sysfs_attr;
9431 }
9432 /* SLI-3 HBA setup */
9433 if (lpfc_sli_hba_setup(phba)) {
9434 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9435 "1477 Failed to set up hba\n");
9436 error = -ENODEV;
9437 goto out_remove_device;
9438 }
9439
9440 /* Wait 50ms for the interrupts of previous mailbox commands */
9441 msleep(50);
9442 /* Check active interrupts on message signaled interrupts */
9443 if (intr_mode == 0 ||
9444 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
9445 /* Log the current active interrupt mode */
9446 phba->intr_mode = intr_mode;
9447 lpfc_log_intr_mode(phba, intr_mode);
9448 break;
9449 } else {
9450 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9451 "0447 Configure interrupt mode (%d) "
9452 "failed active interrupt test.\n",
9453 intr_mode);
9454 /* Disable the current interrupt mode */
9455 lpfc_sli_disable_intr(phba);
9456 /* Try next level of interrupt mode */
9457 cfg_mode = --intr_mode;
9458 }
9459 }
9460
9461 /* Perform post initialization setup */
9462 lpfc_post_init_setup(phba);
9463
9464 /* Check if there are static vports to be created. */
9465 lpfc_create_static_vport(phba);
9466
9467 return 0;
9468
9469out_remove_device:
9470 lpfc_unset_hba(phba);
9471out_free_sysfs_attr:
9472 lpfc_free_sysfs_attr(vport);
9473out_destroy_shost:
9474 lpfc_destroy_shost(phba);
9475out_unset_driver_resource:
9476 lpfc_unset_driver_resource_phase2(phba);
9477out_free_iocb_list:
9478 lpfc_free_iocb_list(phba);
9479out_unset_driver_resource_s3:
9480 lpfc_sli_driver_resource_unset(phba);
9481out_unset_pci_mem_s3:
9482 lpfc_sli_pci_mem_unset(phba);
9483out_disable_pci_dev:
9484 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04009485 if (shost)
9486 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -04009487out_free_phba:
9488 lpfc_hba_free(phba);
9489 return error;
9490}
9491
9492/**
9493 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -04009494 * @pdev: pointer to PCI device
9495 *
James Smart3772a992009-05-22 14:50:54 -04009496 * This routine is to be called to disattach a device with SLI-3 interface
9497 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
9498 * removed from PCI bus, it performs all the necessary cleanup for the HBA
9499 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -04009500 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009501static void
James Smart3772a992009-05-22 14:50:54 -04009502lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -05009503{
James Smart2e0fef82007-06-17 19:56:36 -05009504 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9505 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -05009506 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -05009507 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -05009508 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05009509 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
9510
James Smart549e55c2007-08-02 11:09:51 -04009511 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04009512 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04009513 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009514
James Smart858c9f62007-06-17 19:56:39 -05009515 lpfc_free_sysfs_attr(vport);
9516
James Smarteada2722008-12-04 22:39:13 -05009517 /* Release all the vports against this physical port */
9518 vports = lpfc_create_vport_work_array(phba);
9519 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -04009520 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
9521 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
9522 continue;
James Smarteada2722008-12-04 22:39:13 -05009523 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -04009524 }
James Smarteada2722008-12-04 22:39:13 -05009525 lpfc_destroy_vport_work_array(phba, vports);
9526
9527 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -05009528 fc_remove_host(shost);
9529 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04009530 lpfc_cleanup(vport);
9531
James Smart2e0fef82007-06-17 19:56:36 -05009532 /*
9533 * Bring down the SLI Layer. This step disable all interrupts,
9534 * clears the rings, discards all mailbox commands, and resets
9535 * the HBA.
9536 */
James Smarta257bf92009-04-06 18:48:10 -04009537
Justin P. Mattock48e34d02010-12-30 15:07:58 -08009538 /* HBA interrupt will be disabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -05009539 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -04009540 /* Stop kthread signal shall trigger work_done one more time */
9541 kthread_stop(phba->worker_thread);
9542 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -05009543 lpfc_sli_brdrestart(phba);
9544
James Smart72859902012-01-18 16:25:38 -05009545 kfree(phba->vpi_bmask);
9546 kfree(phba->vpi_ids);
9547
James Smart3772a992009-05-22 14:50:54 -04009548 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05009549 spin_lock_irq(&phba->hbalock);
9550 list_del_init(&vport->listentry);
9551 spin_unlock_irq(&phba->hbalock);
9552
James Smart858c9f62007-06-17 19:56:39 -05009553 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05009554
James Smart912e3ac2011-05-24 11:42:11 -04009555 /* Disable SR-IOV if enabled */
9556 if (phba->cfg_sriov_nr_virtfn)
9557 pci_disable_sriov(pdev);
9558
James Smart5b75da22008-12-04 22:39:35 -05009559 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009560 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -05009561
James Smart858c9f62007-06-17 19:56:39 -05009562 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05009563
9564 /*
9565 * Call scsi_free before mem_free since scsi bufs are released to their
9566 * corresponding pools here.
9567 */
9568 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -04009569 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -05009570
James Smart34b02dc2008-08-24 21:49:55 -04009571 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
9572 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -05009573
James Smart2e0fef82007-06-17 19:56:36 -05009574 /* Free resources associated with SLI2 interface */
9575 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -04009576 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -05009577
9578 /* unmap adapter SLIM and Control Registers */
9579 iounmap(phba->ctrl_regs_memmap_p);
9580 iounmap(phba->slim_memmap_p);
9581
James Smart3772a992009-05-22 14:50:54 -04009582 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -05009583
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05009584 pci_release_selected_regions(pdev, bars);
James Smart2e0fef82007-06-17 19:56:36 -05009585 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05009586}
9587
Linas Vepstas8d63f372007-02-14 14:28:36 -06009588/**
James Smart3772a992009-05-22 14:50:54 -04009589 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05009590 * @pdev: pointer to PCI device
9591 * @msg: power management message
9592 *
James Smart3772a992009-05-22 14:50:54 -04009593 * This routine is to be called from the kernel's PCI subsystem to support
9594 * system Power Management (PM) to device with SLI-3 interface spec. When
9595 * PM invokes this method, it quiesces the device by stopping the driver's
9596 * worker thread for the device, turning off device's interrupt and DMA,
9597 * and bring the device offline. Note that as the driver implements the
9598 * minimum PM requirements to a power-aware driver's PM support for the
9599 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
9600 * to the suspend() method call will be treated as SUSPEND and the driver will
9601 * fully reinitialize its device during resume() method call, the driver will
9602 * set device to PCI_D3hot state in PCI config space instead of setting it
9603 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -05009604 *
9605 * Return code
James Smart3772a992009-05-22 14:50:54 -04009606 * 0 - driver suspended the device
9607 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05009608 **/
9609static int
James Smart3772a992009-05-22 14:50:54 -04009610lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -05009611{
9612 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9613 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9614
9615 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9616 "0473 PCI device Power Management suspend.\n");
9617
9618 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -04009619 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3a55b532008-12-04 22:38:54 -05009620 lpfc_offline(phba);
9621 kthread_stop(phba->worker_thread);
9622
9623 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -04009624 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -05009625
9626 /* Save device state to PCI config space */
9627 pci_save_state(pdev);
9628 pci_set_power_state(pdev, PCI_D3hot);
9629
9630 return 0;
9631}
9632
9633/**
James Smart3772a992009-05-22 14:50:54 -04009634 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05009635 * @pdev: pointer to PCI device
9636 *
James Smart3772a992009-05-22 14:50:54 -04009637 * This routine is to be called from the kernel's PCI subsystem to support
9638 * system Power Management (PM) to device with SLI-3 interface spec. When PM
9639 * invokes this method, it restores the device's PCI config space state and
9640 * fully reinitializes the device and brings it online. Note that as the
9641 * driver implements the minimum PM requirements to a power-aware driver's
9642 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
9643 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
9644 * driver will fully reinitialize its device during resume() method call,
9645 * the device will be set to PCI_D0 directly in PCI config space before
9646 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -05009647 *
9648 * Return code
James Smart3772a992009-05-22 14:50:54 -04009649 * 0 - driver suspended the device
9650 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05009651 **/
9652static int
James Smart3772a992009-05-22 14:50:54 -04009653lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -05009654{
9655 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9656 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -05009657 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05009658 int error;
9659
9660 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9661 "0452 PCI device Power Management resume.\n");
9662
9663 /* Restore device state from PCI config space */
9664 pci_set_power_state(pdev, PCI_D0);
9665 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -04009666
James Smart1dfb5a42010-02-12 14:40:50 -05009667 /*
9668 * As the new kernel behavior of pci_restore_state() API call clears
9669 * device saved_state flag, need to save the restored state again.
9670 */
9671 pci_save_state(pdev);
9672
James Smart3a55b532008-12-04 22:38:54 -05009673 if (pdev->is_busmaster)
9674 pci_set_master(pdev);
9675
9676 /* Startup the kernel thread for this host adapter. */
9677 phba->worker_thread = kthread_run(lpfc_do_work, phba,
9678 "lpfc_worker_%d", phba->brd_no);
9679 if (IS_ERR(phba->worker_thread)) {
9680 error = PTR_ERR(phba->worker_thread);
9681 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9682 "0434 PM resume failed to start worker "
9683 "thread: error=x%x.\n", error);
9684 return error;
9685 }
9686
James Smart5b75da22008-12-04 22:39:35 -05009687 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009688 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05009689 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -05009690 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -05009691 "0430 PM resume Failed to enable interrupt\n");
9692 return -EIO;
9693 } else
9694 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05009695
9696 /* Restart HBA and bring it online */
9697 lpfc_sli_brdrestart(phba);
9698 lpfc_online(phba);
9699
James Smart5b75da22008-12-04 22:39:35 -05009700 /* Log the current active interrupt mode */
9701 lpfc_log_intr_mode(phba, phba->intr_mode);
9702
James Smart3a55b532008-12-04 22:38:54 -05009703 return 0;
9704}
9705
9706/**
James Smart891478a2009-11-18 15:40:23 -05009707 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
9708 * @phba: pointer to lpfc hba data structure.
9709 *
9710 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -04009711 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -05009712 **/
9713static void
9714lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
9715{
James Smarte2af0d22010-03-15 11:25:32 -04009716 struct lpfc_sli *psli = &phba->sli;
9717 struct lpfc_sli_ring *pring;
9718
James Smart891478a2009-11-18 15:40:23 -05009719 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9720 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -04009721
9722 /*
9723 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
9724 * and let the SCSI mid-layer to retry them to recover.
9725 */
9726 pring = &psli->ring[psli->fcp_ring];
9727 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart891478a2009-11-18 15:40:23 -05009728}
9729
9730/**
James Smart0d878412009-10-02 15:16:56 -04009731 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
9732 * @phba: pointer to lpfc hba data structure.
9733 *
9734 * This routine is called to prepare the SLI3 device for PCI slot reset. It
9735 * disables the device interrupt and pci device, and aborts the internal FCP
9736 * pending I/Os.
9737 **/
9738static void
9739lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
9740{
James Smart0d878412009-10-02 15:16:56 -04009741 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05009742 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -04009743
James Smart75baf692010-06-08 18:31:21 -04009744 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -04009745 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -04009746
James Smarte2af0d22010-03-15 11:25:32 -04009747 /* Block all SCSI devices' I/Os on the host */
9748 lpfc_scsi_dev_block(phba);
9749
James Smartea714f32013-04-17 20:18:47 -04009750 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
9751 lpfc_sli_flush_fcp_rings(phba);
9752
James Smarte2af0d22010-03-15 11:25:32 -04009753 /* stop all timers */
9754 lpfc_stop_hba_timers(phba);
9755
James Smart0d878412009-10-02 15:16:56 -04009756 /* Disable interrupt and pci device */
9757 lpfc_sli_disable_intr(phba);
9758 pci_disable_device(phba->pcidev);
James Smart0d878412009-10-02 15:16:56 -04009759}
9760
9761/**
9762 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
9763 * @phba: pointer to lpfc hba data structure.
9764 *
9765 * This routine is called to prepare the SLI3 device for PCI slot permanently
9766 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
9767 * pending I/Os.
9768 **/
9769static void
James Smart75baf692010-06-08 18:31:21 -04009770lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
James Smart0d878412009-10-02 15:16:56 -04009771{
9772 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05009773 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -04009774 /* Block all SCSI devices' I/Os on the host */
9775 lpfc_scsi_dev_block(phba);
9776
9777 /* stop all timers */
9778 lpfc_stop_hba_timers(phba);
9779
James Smart0d878412009-10-02 15:16:56 -04009780 /* Clean up all driver's outstanding SCSI I/Os */
9781 lpfc_sli_flush_fcp_rings(phba);
9782}
9783
9784/**
James Smart3772a992009-05-22 14:50:54 -04009785 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -04009786 * @pdev: pointer to PCI device.
9787 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009788 *
James Smart3772a992009-05-22 14:50:54 -04009789 * This routine is called from the PCI subsystem for I/O error handling to
9790 * device with SLI-3 interface spec. This function is called by the PCI
9791 * subsystem after a PCI bus error affecting this device has been detected.
9792 * When this function is invoked, it will need to stop all the I/Os and
9793 * interrupt(s) to the device. Once that is done, it will return
9794 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
9795 * as desired.
James Smarte59058c2008-08-24 21:49:00 -04009796 *
9797 * Return codes
James Smart0d878412009-10-02 15:16:56 -04009798 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -04009799 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
9800 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -04009801 **/
James Smart3772a992009-05-22 14:50:54 -04009802static pci_ers_result_t
9803lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009804{
James Smart51ef4c22007-08-02 11:10:31 -04009805 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9806 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009807
James Smart0d878412009-10-02 15:16:56 -04009808 switch (state) {
9809 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -05009810 /* Non-fatal error, prepare for recovery */
9811 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -04009812 return PCI_ERS_RESULT_CAN_RECOVER;
9813 case pci_channel_io_frozen:
9814 /* Fatal error, prepare for slot reset */
9815 lpfc_sli_prep_dev_for_reset(phba);
9816 return PCI_ERS_RESULT_NEED_RESET;
9817 case pci_channel_io_perm_failure:
9818 /* Permanent failure, prepare for device down */
James Smart75baf692010-06-08 18:31:21 -04009819 lpfc_sli_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009820 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -04009821 default:
9822 /* Unknown state, prepare and request slot reset */
9823 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9824 "0472 Unknown PCI error state: x%x\n", state);
9825 lpfc_sli_prep_dev_for_reset(phba);
9826 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -04009827 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06009828}
9829
9830/**
James Smart3772a992009-05-22 14:50:54 -04009831 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -04009832 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009833 *
James Smart3772a992009-05-22 14:50:54 -04009834 * This routine is called from the PCI subsystem for error handling to
9835 * device with SLI-3 interface spec. This is called after PCI bus has been
9836 * reset to restart the PCI card from scratch, as if from a cold-boot.
9837 * During the PCI subsystem error recovery, after driver returns
9838 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
9839 * recovery and then call this routine before calling the .resume method
9840 * to recover the device. This function will initialize the HBA device,
9841 * enable the interrupt, but it will just put the HBA to offline state
9842 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -04009843 *
9844 * Return codes
James Smart3772a992009-05-22 14:50:54 -04009845 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
9846 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -06009847 */
James Smart3772a992009-05-22 14:50:54 -04009848static pci_ers_result_t
9849lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009850{
James Smart51ef4c22007-08-02 11:10:31 -04009851 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9852 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009853 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -05009854 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009855
9856 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +11009857 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -06009858 printk(KERN_ERR "lpfc: Cannot re-enable "
9859 "PCI device after reset.\n");
9860 return PCI_ERS_RESULT_DISCONNECT;
9861 }
9862
James Smart97207482008-12-04 22:39:19 -05009863 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05009864
9865 /*
9866 * As the new kernel behavior of pci_restore_state() API call clears
9867 * device saved_state flag, need to save the restored state again.
9868 */
9869 pci_save_state(pdev);
9870
James Smart97207482008-12-04 22:39:19 -05009871 if (pdev->is_busmaster)
9872 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009873
James Smart92d7f7b2007-06-17 19:56:38 -05009874 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04009875 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05009876 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009877
James Smart5b75da22008-12-04 22:39:35 -05009878 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009879 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05009880 if (intr_mode == LPFC_INTR_ERROR) {
9881 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9882 "0427 Cannot re-enable interrupt after "
9883 "slot reset.\n");
9884 return PCI_ERS_RESULT_DISCONNECT;
9885 } else
9886 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009887
James Smart75baf692010-06-08 18:31:21 -04009888 /* Take device offline, it will perform cleanup */
James Smart618a5232012-06-12 13:54:36 -04009889 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009890 lpfc_offline(phba);
9891 lpfc_sli_brdrestart(phba);
9892
James Smart5b75da22008-12-04 22:39:35 -05009893 /* Log the current active interrupt mode */
9894 lpfc_log_intr_mode(phba, phba->intr_mode);
9895
Linas Vepstas8d63f372007-02-14 14:28:36 -06009896 return PCI_ERS_RESULT_RECOVERED;
9897}
9898
9899/**
James Smart3772a992009-05-22 14:50:54 -04009900 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -04009901 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -06009902 *
James Smart3772a992009-05-22 14:50:54 -04009903 * This routine is called from the PCI subsystem for error handling to device
9904 * with SLI-3 interface spec. It is called when kernel error recovery tells
9905 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
9906 * error recovery. After this call, traffic can start to flow from this device
9907 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009908 */
James Smart3772a992009-05-22 14:50:54 -04009909static void
9910lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009911{
James Smart51ef4c22007-08-02 11:10:31 -04009912 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9913 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009914
James Smarte2af0d22010-03-15 11:25:32 -04009915 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -04009916 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -04009917
9918 /* Clean up Advanced Error Reporting (AER) if needed */
9919 if (phba->hba_flag & HBA_AER_ENABLED)
9920 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009921}
9922
James Smart3772a992009-05-22 14:50:54 -04009923/**
James Smartda0436e2009-05-22 14:51:39 -04009924 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
9925 * @phba: pointer to lpfc hba data structure.
9926 *
9927 * returns the number of ELS/CT IOCBs to reserve
9928 **/
9929int
9930lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
9931{
9932 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
9933
James Smartf1126682009-06-10 17:22:44 -04009934 if (phba->sli_rev == LPFC_SLI_REV4) {
9935 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -04009936 return 10;
James Smartf1126682009-06-10 17:22:44 -04009937 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -04009938 return 25;
James Smartf1126682009-06-10 17:22:44 -04009939 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -04009940 return 50;
James Smartf1126682009-06-10 17:22:44 -04009941 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -04009942 return 100;
James Smart8a9d2e82012-05-09 21:16:12 -04009943 else if (max_xri <= 1536)
James Smart6a9c52c2009-10-02 15:16:51 -04009944 return 150;
James Smart8a9d2e82012-05-09 21:16:12 -04009945 else if (max_xri <= 2048)
9946 return 200;
9947 else
9948 return 250;
James Smartf1126682009-06-10 17:22:44 -04009949 } else
9950 return 0;
James Smartda0436e2009-05-22 14:51:39 -04009951}
9952
9953/**
James Smart52d52442011-05-24 11:42:45 -04009954 * lpfc_write_firmware - attempt to write a firmware image to the port
James Smart52d52442011-05-24 11:42:45 -04009955 * @fw: pointer to firmware image returned from request_firmware.
James Smartce396282012-09-29 11:30:56 -04009956 * @phba: pointer to lpfc hba data structure.
James Smart52d52442011-05-24 11:42:45 -04009957 *
James Smart52d52442011-05-24 11:42:45 -04009958 **/
James Smartce396282012-09-29 11:30:56 -04009959static void
9960lpfc_write_firmware(const struct firmware *fw, void *context)
James Smart52d52442011-05-24 11:42:45 -04009961{
James Smartce396282012-09-29 11:30:56 -04009962 struct lpfc_hba *phba = (struct lpfc_hba *)context;
James Smart6b5151f2012-01-18 16:24:06 -05009963 char fwrev[FW_REV_STR_SIZE];
James Smartce396282012-09-29 11:30:56 -04009964 struct lpfc_grp_hdr *image;
James Smart52d52442011-05-24 11:42:45 -04009965 struct list_head dma_buffer_list;
9966 int i, rc = 0;
9967 struct lpfc_dmabuf *dmabuf, *next;
9968 uint32_t offset = 0, temp_offset = 0;
9969
James Smartc71ab862012-10-31 14:44:33 -04009970 /* It can be null in no-wait mode, sanity check */
James Smartce396282012-09-29 11:30:56 -04009971 if (!fw) {
9972 rc = -ENXIO;
9973 goto out;
9974 }
9975 image = (struct lpfc_grp_hdr *)fw->data;
9976
James Smart52d52442011-05-24 11:42:45 -04009977 INIT_LIST_HEAD(&dma_buffer_list);
James Smart079b5c92011-08-21 21:48:49 -04009978 if ((be32_to_cpu(image->magic_number) != LPFC_GROUP_OJECT_MAGIC_NUM) ||
9979 (bf_get_be32(lpfc_grp_hdr_file_type, image) !=
9980 LPFC_FILE_TYPE_GROUP) ||
9981 (bf_get_be32(lpfc_grp_hdr_id, image) != LPFC_FILE_ID_GROUP) ||
9982 (be32_to_cpu(image->size) != fw->size)) {
James Smart52d52442011-05-24 11:42:45 -04009983 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9984 "3022 Invalid FW image found. "
James Smart079b5c92011-08-21 21:48:49 -04009985 "Magic:%x Type:%x ID:%x\n",
9986 be32_to_cpu(image->magic_number),
9987 bf_get_be32(lpfc_grp_hdr_file_type, image),
9988 bf_get_be32(lpfc_grp_hdr_id, image));
James Smartce396282012-09-29 11:30:56 -04009989 rc = -EINVAL;
9990 goto release_out;
James Smart52d52442011-05-24 11:42:45 -04009991 }
9992 lpfc_decode_firmware_rev(phba, fwrev, 1);
James Smart88a2cfb2011-07-22 18:36:33 -04009993 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
James Smart52d52442011-05-24 11:42:45 -04009994 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartce396282012-09-29 11:30:56 -04009995 "3023 Updating Firmware, Current Version:%s "
James Smart52d52442011-05-24 11:42:45 -04009996 "New Version:%s\n",
James Smart88a2cfb2011-07-22 18:36:33 -04009997 fwrev, image->revision);
James Smart52d52442011-05-24 11:42:45 -04009998 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
9999 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
10000 GFP_KERNEL);
10001 if (!dmabuf) {
10002 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040010003 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010004 }
10005 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
10006 SLI4_PAGE_SIZE,
10007 &dmabuf->phys,
10008 GFP_KERNEL);
10009 if (!dmabuf->virt) {
10010 kfree(dmabuf);
10011 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040010012 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010013 }
10014 list_add_tail(&dmabuf->list, &dma_buffer_list);
10015 }
10016 while (offset < fw->size) {
10017 temp_offset = offset;
10018 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
James Smart079b5c92011-08-21 21:48:49 -040010019 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
James Smart52d52442011-05-24 11:42:45 -040010020 memcpy(dmabuf->virt,
10021 fw->data + temp_offset,
James Smart079b5c92011-08-21 21:48:49 -040010022 fw->size - temp_offset);
10023 temp_offset = fw->size;
James Smart52d52442011-05-24 11:42:45 -040010024 break;
10025 }
James Smart52d52442011-05-24 11:42:45 -040010026 memcpy(dmabuf->virt, fw->data + temp_offset,
10027 SLI4_PAGE_SIZE);
James Smart88a2cfb2011-07-22 18:36:33 -040010028 temp_offset += SLI4_PAGE_SIZE;
James Smart52d52442011-05-24 11:42:45 -040010029 }
10030 rc = lpfc_wr_object(phba, &dma_buffer_list,
10031 (fw->size - offset), &offset);
James Smartce396282012-09-29 11:30:56 -040010032 if (rc)
10033 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010034 }
10035 rc = offset;
10036 }
James Smartce396282012-09-29 11:30:56 -040010037
10038release_out:
James Smart52d52442011-05-24 11:42:45 -040010039 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
10040 list_del(&dmabuf->list);
10041 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
10042 dmabuf->virt, dmabuf->phys);
10043 kfree(dmabuf);
10044 }
James Smartce396282012-09-29 11:30:56 -040010045 release_firmware(fw);
10046out:
10047 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartc71ab862012-10-31 14:44:33 -040010048 "3024 Firmware update done: %d.\n", rc);
James Smartce396282012-09-29 11:30:56 -040010049 return;
James Smart52d52442011-05-24 11:42:45 -040010050}
10051
10052/**
James Smartc71ab862012-10-31 14:44:33 -040010053 * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
10054 * @phba: pointer to lpfc hba data structure.
10055 *
10056 * This routine is called to perform Linux generic firmware upgrade on device
10057 * that supports such feature.
10058 **/
10059int
10060lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
10061{
10062 uint8_t file_name[ELX_MODEL_NAME_SIZE];
10063 int ret;
10064 const struct firmware *fw;
10065
10066 /* Only supported on SLI4 interface type 2 for now */
10067 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
10068 LPFC_SLI_INTF_IF_TYPE_2)
10069 return -EPERM;
10070
10071 snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
10072
10073 if (fw_upgrade == INT_FW_UPGRADE) {
10074 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
10075 file_name, &phba->pcidev->dev,
10076 GFP_KERNEL, (void *)phba,
10077 lpfc_write_firmware);
10078 } else if (fw_upgrade == RUN_FW_UPGRADE) {
10079 ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
10080 if (!ret)
10081 lpfc_write_firmware(fw, (void *)phba);
10082 } else {
10083 ret = -EINVAL;
10084 }
10085
10086 return ret;
10087}
10088
10089/**
James Smartda0436e2009-05-22 14:51:39 -040010090 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
10091 * @pdev: pointer to PCI device
10092 * @pid: pointer to PCI device identifier
10093 *
10094 * This routine is called from the kernel's PCI subsystem to device with
10095 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
10096 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
10097 * information of the device and driver to see if the driver state that it
10098 * can support this kind of device. If the match is successful, the driver
10099 * core invokes this routine. If this routine determines it can claim the HBA,
10100 * it does all the initialization that it needs to do to handle the HBA
10101 * properly.
10102 *
10103 * Return code
10104 * 0 - driver can claim the device
10105 * negative value - driver can not claim the device
10106 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010107static int
James Smartda0436e2009-05-22 14:51:39 -040010108lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
10109{
10110 struct lpfc_hba *phba;
10111 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -040010112 struct Scsi_Host *shost = NULL;
James Smartce396282012-09-29 11:30:56 -040010113 int error, ret;
James Smartda0436e2009-05-22 14:51:39 -040010114 uint32_t cfg_mode, intr_mode;
James Smart67d12732012-08-03 12:36:13 -040010115 int adjusted_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -040010116
10117 /* Allocate memory for HBA structure */
10118 phba = lpfc_hba_alloc(pdev);
10119 if (!phba)
10120 return -ENOMEM;
10121
10122 /* Perform generic PCI device enabling operation */
10123 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -040010124 if (error)
James Smartda0436e2009-05-22 14:51:39 -040010125 goto out_free_phba;
James Smartda0436e2009-05-22 14:51:39 -040010126
10127 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
10128 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
10129 if (error)
10130 goto out_disable_pci_dev;
10131
10132 /* Set up SLI-4 specific device PCI memory space */
10133 error = lpfc_sli4_pci_mem_setup(phba);
10134 if (error) {
10135 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10136 "1410 Failed to set up pci memory space.\n");
10137 goto out_disable_pci_dev;
10138 }
10139
10140 /* Set up phase-1 common device driver resources */
10141 error = lpfc_setup_driver_resource_phase1(phba);
10142 if (error) {
10143 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10144 "1411 Failed to set up driver resource.\n");
10145 goto out_unset_pci_mem_s4;
10146 }
10147
10148 /* Set up SLI-4 Specific device driver resources */
10149 error = lpfc_sli4_driver_resource_setup(phba);
10150 if (error) {
10151 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10152 "1412 Failed to set up driver resource.\n");
10153 goto out_unset_pci_mem_s4;
10154 }
10155
10156 /* Initialize and populate the iocb list per host */
James Smart2a9bf3d2010-06-07 15:24:45 -040010157
10158 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10159 "2821 initialize iocb list %d.\n",
10160 phba->cfg_iocb_cnt*1024);
10161 error = lpfc_init_iocb_list(phba, phba->cfg_iocb_cnt*1024);
10162
James Smartda0436e2009-05-22 14:51:39 -040010163 if (error) {
10164 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10165 "1413 Failed to initialize iocb list.\n");
10166 goto out_unset_driver_resource_s4;
10167 }
10168
James Smart19ca7602010-11-20 23:11:55 -050010169 INIT_LIST_HEAD(&phba->active_rrq_list);
James Smart7d791df2011-07-22 18:37:52 -040010170 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
James Smart19ca7602010-11-20 23:11:55 -050010171
James Smartda0436e2009-05-22 14:51:39 -040010172 /* Set up common device driver resources */
10173 error = lpfc_setup_driver_resource_phase2(phba);
10174 if (error) {
10175 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10176 "1414 Failed to set up driver resource.\n");
10177 goto out_free_iocb_list;
10178 }
10179
James Smart079b5c92011-08-21 21:48:49 -040010180 /* Get the default values for Model Name and Description */
10181 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
10182
James Smartda0436e2009-05-22 14:51:39 -040010183 /* Create SCSI host to the physical port */
10184 error = lpfc_create_shost(phba);
10185 if (error) {
10186 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10187 "1415 Failed to create scsi host.\n");
10188 goto out_unset_driver_resource;
10189 }
10190
10191 /* Configure sysfs attributes */
10192 vport = phba->pport;
10193 error = lpfc_alloc_sysfs_attr(vport);
10194 if (error) {
10195 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10196 "1416 Failed to allocate sysfs attr\n");
10197 goto out_destroy_shost;
10198 }
10199
James Smart6669f9b2009-10-02 15:16:45 -040010200 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -040010201 /* Now, trying to enable interrupt and bring up the device */
10202 cfg_mode = phba->cfg_use_msi;
James Smartda0436e2009-05-22 14:51:39 -040010203
James Smart7b15db32013-01-03 15:43:29 -050010204 /* Put device to a known state before enabling interrupt */
10205 lpfc_stop_port(phba);
10206 /* Configure and enable interrupt */
10207 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
10208 if (intr_mode == LPFC_INTR_ERROR) {
10209 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10210 "0426 Failed to enable interrupt.\n");
10211 error = -ENODEV;
10212 goto out_free_sysfs_attr;
James Smartda0436e2009-05-22 14:51:39 -040010213 }
James Smart7b15db32013-01-03 15:43:29 -050010214 /* Default to single EQ for non-MSI-X */
10215 if (phba->intr_type != MSIX)
10216 adjusted_fcp_io_channel = 1;
10217 else
10218 adjusted_fcp_io_channel = phba->cfg_fcp_io_channel;
10219 phba->cfg_fcp_io_channel = adjusted_fcp_io_channel;
10220 /* Set up SLI-4 HBA */
10221 if (lpfc_sli4_hba_setup(phba)) {
10222 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10223 "1421 Failed to set up hba\n");
10224 error = -ENODEV;
10225 goto out_disable_intr;
10226 }
10227
10228 /* Log the current active interrupt mode */
10229 phba->intr_mode = intr_mode;
10230 lpfc_log_intr_mode(phba, intr_mode);
James Smartda0436e2009-05-22 14:51:39 -040010231
10232 /* Perform post initialization setup */
10233 lpfc_post_init_setup(phba);
10234
James Smartc71ab862012-10-31 14:44:33 -040010235 /* check for firmware upgrade or downgrade */
10236 if (phba->cfg_request_firmware_upgrade)
10237 ret = lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
James Smart52d52442011-05-24 11:42:45 -040010238
James Smart1c6834a2009-07-19 10:01:26 -040010239 /* Check if there are static vports to be created. */
10240 lpfc_create_static_vport(phba);
James Smartda0436e2009-05-22 14:51:39 -040010241 return 0;
10242
10243out_disable_intr:
10244 lpfc_sli4_disable_intr(phba);
10245out_free_sysfs_attr:
10246 lpfc_free_sysfs_attr(vport);
10247out_destroy_shost:
10248 lpfc_destroy_shost(phba);
10249out_unset_driver_resource:
10250 lpfc_unset_driver_resource_phase2(phba);
10251out_free_iocb_list:
10252 lpfc_free_iocb_list(phba);
10253out_unset_driver_resource_s4:
10254 lpfc_sli4_driver_resource_unset(phba);
10255out_unset_pci_mem_s4:
10256 lpfc_sli4_pci_mem_unset(phba);
10257out_disable_pci_dev:
10258 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040010259 if (shost)
10260 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -040010261out_free_phba:
10262 lpfc_hba_free(phba);
10263 return error;
10264}
10265
10266/**
10267 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
10268 * @pdev: pointer to PCI device
10269 *
10270 * This routine is called from the kernel's PCI subsystem to device with
10271 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
10272 * removed from PCI bus, it performs all the necessary cleanup for the HBA
10273 * device to be removed from the PCI subsystem properly.
10274 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010275static void
James Smartda0436e2009-05-22 14:51:39 -040010276lpfc_pci_remove_one_s4(struct pci_dev *pdev)
10277{
10278 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10279 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
10280 struct lpfc_vport **vports;
10281 struct lpfc_hba *phba = vport->phba;
10282 int i;
10283
10284 /* Mark the device unloading flag */
10285 spin_lock_irq(&phba->hbalock);
10286 vport->load_flag |= FC_UNLOADING;
10287 spin_unlock_irq(&phba->hbalock);
10288
10289 /* Free the HBA sysfs attributes */
10290 lpfc_free_sysfs_attr(vport);
10291
10292 /* Release all the vports against this physical port */
10293 vports = lpfc_create_vport_work_array(phba);
10294 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040010295 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
10296 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
10297 continue;
James Smartda0436e2009-05-22 14:51:39 -040010298 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040010299 }
James Smartda0436e2009-05-22 14:51:39 -040010300 lpfc_destroy_vport_work_array(phba, vports);
10301
10302 /* Remove FC host and then SCSI host with the physical port */
10303 fc_remove_host(shost);
10304 scsi_remove_host(shost);
10305
10306 /* Perform cleanup on the physical port */
10307 lpfc_cleanup(vport);
10308
10309 /*
10310 * Bring down the SLI Layer. This step disables all interrupts,
10311 * clears the rings, discards all mailbox commands, and resets
10312 * the HBA FCoE function.
10313 */
10314 lpfc_debugfs_terminate(vport);
10315 lpfc_sli4_hba_unset(phba);
10316
10317 spin_lock_irq(&phba->hbalock);
10318 list_del_init(&vport->listentry);
10319 spin_unlock_irq(&phba->hbalock);
10320
James Smart3677a3a2010-09-29 11:19:14 -040010321 /* Perform scsi free before driver resource_unset since scsi
James Smartda0436e2009-05-22 14:51:39 -040010322 * buffers are released to their corresponding pools here.
10323 */
10324 lpfc_scsi_free(phba);
James Smart67d12732012-08-03 12:36:13 -040010325
James Smartda0436e2009-05-22 14:51:39 -040010326 lpfc_sli4_driver_resource_unset(phba);
10327
10328 /* Unmap adapter Control and Doorbell registers */
10329 lpfc_sli4_pci_mem_unset(phba);
10330
10331 /* Release PCI resources and disable device's PCI function */
10332 scsi_host_put(shost);
10333 lpfc_disable_pci_dev(phba);
10334
10335 /* Finally, free the driver's device data structure */
10336 lpfc_hba_free(phba);
10337
10338 return;
10339}
10340
10341/**
10342 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
10343 * @pdev: pointer to PCI device
10344 * @msg: power management message
10345 *
10346 * This routine is called from the kernel's PCI subsystem to support system
10347 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
10348 * this method, it quiesces the device by stopping the driver's worker
10349 * thread for the device, turning off device's interrupt and DMA, and bring
10350 * the device offline. Note that as the driver implements the minimum PM
10351 * requirements to a power-aware driver's PM support for suspend/resume -- all
10352 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
10353 * method call will be treated as SUSPEND and the driver will fully
10354 * reinitialize its device during resume() method call, the driver will set
10355 * device to PCI_D3hot state in PCI config space instead of setting it
10356 * according to the @msg provided by the PM.
10357 *
10358 * Return code
10359 * 0 - driver suspended the device
10360 * Error otherwise
10361 **/
10362static int
10363lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
10364{
10365 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10366 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10367
10368 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart75baf692010-06-08 18:31:21 -040010369 "2843 PCI device Power Management suspend.\n");
James Smartda0436e2009-05-22 14:51:39 -040010370
10371 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040010372 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smartda0436e2009-05-22 14:51:39 -040010373 lpfc_offline(phba);
10374 kthread_stop(phba->worker_thread);
10375
10376 /* Disable interrupt from device */
10377 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040010378 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -040010379
10380 /* Save device state to PCI config space */
10381 pci_save_state(pdev);
10382 pci_set_power_state(pdev, PCI_D3hot);
10383
10384 return 0;
10385}
10386
10387/**
10388 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
10389 * @pdev: pointer to PCI device
10390 *
10391 * This routine is called from the kernel's PCI subsystem to support system
10392 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
10393 * this method, it restores the device's PCI config space state and fully
10394 * reinitializes the device and brings it online. Note that as the driver
10395 * implements the minimum PM requirements to a power-aware driver's PM for
10396 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
10397 * to the suspend() method call will be treated as SUSPEND and the driver
10398 * will fully reinitialize its device during resume() method call, the device
10399 * will be set to PCI_D0 directly in PCI config space before restoring the
10400 * state.
10401 *
10402 * Return code
10403 * 0 - driver suspended the device
10404 * Error otherwise
10405 **/
10406static int
10407lpfc_pci_resume_one_s4(struct pci_dev *pdev)
10408{
10409 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10410 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10411 uint32_t intr_mode;
10412 int error;
10413
10414 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10415 "0292 PCI device Power Management resume.\n");
10416
10417 /* Restore device state from PCI config space */
10418 pci_set_power_state(pdev, PCI_D0);
10419 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050010420
10421 /*
10422 * As the new kernel behavior of pci_restore_state() API call clears
10423 * device saved_state flag, need to save the restored state again.
10424 */
10425 pci_save_state(pdev);
10426
James Smartda0436e2009-05-22 14:51:39 -040010427 if (pdev->is_busmaster)
10428 pci_set_master(pdev);
10429
10430 /* Startup the kernel thread for this host adapter. */
10431 phba->worker_thread = kthread_run(lpfc_do_work, phba,
10432 "lpfc_worker_%d", phba->brd_no);
10433 if (IS_ERR(phba->worker_thread)) {
10434 error = PTR_ERR(phba->worker_thread);
10435 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10436 "0293 PM resume failed to start worker "
10437 "thread: error=x%x.\n", error);
10438 return error;
10439 }
10440
10441 /* Configure and enable interrupt */
10442 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
10443 if (intr_mode == LPFC_INTR_ERROR) {
10444 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10445 "0294 PM resume Failed to enable interrupt\n");
10446 return -EIO;
10447 } else
10448 phba->intr_mode = intr_mode;
10449
10450 /* Restart HBA and bring it online */
10451 lpfc_sli_brdrestart(phba);
10452 lpfc_online(phba);
10453
10454 /* Log the current active interrupt mode */
10455 lpfc_log_intr_mode(phba, phba->intr_mode);
10456
10457 return 0;
10458}
10459
10460/**
James Smart75baf692010-06-08 18:31:21 -040010461 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
10462 * @phba: pointer to lpfc hba data structure.
10463 *
10464 * This routine is called to prepare the SLI4 device for PCI slot recover. It
10465 * aborts all the outstanding SCSI I/Os to the pci device.
10466 **/
10467static void
10468lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
10469{
10470 struct lpfc_sli *psli = &phba->sli;
10471 struct lpfc_sli_ring *pring;
10472
10473 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10474 "2828 PCI channel I/O abort preparing for recovery\n");
10475 /*
10476 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
10477 * and let the SCSI mid-layer to retry them to recover.
10478 */
10479 pring = &psli->ring[psli->fcp_ring];
10480 lpfc_sli_abort_iocb_ring(phba, pring);
10481}
10482
10483/**
10484 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
10485 * @phba: pointer to lpfc hba data structure.
10486 *
10487 * This routine is called to prepare the SLI4 device for PCI slot reset. It
10488 * disables the device interrupt and pci device, and aborts the internal FCP
10489 * pending I/Os.
10490 **/
10491static void
10492lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
10493{
10494 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10495 "2826 PCI channel disable preparing for reset\n");
10496
10497 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040010498 lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
James Smart75baf692010-06-08 18:31:21 -040010499
10500 /* Block all SCSI devices' I/Os on the host */
10501 lpfc_scsi_dev_block(phba);
10502
James Smartea714f32013-04-17 20:18:47 -040010503 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
10504 lpfc_sli_flush_fcp_rings(phba);
10505
James Smart75baf692010-06-08 18:31:21 -040010506 /* stop all timers */
10507 lpfc_stop_hba_timers(phba);
10508
10509 /* Disable interrupt and pci device */
10510 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040010511 lpfc_sli4_queue_destroy(phba);
James Smart75baf692010-06-08 18:31:21 -040010512 pci_disable_device(phba->pcidev);
James Smart75baf692010-06-08 18:31:21 -040010513}
10514
10515/**
10516 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
10517 * @phba: pointer to lpfc hba data structure.
10518 *
10519 * This routine is called to prepare the SLI4 device for PCI slot permanently
10520 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
10521 * pending I/Os.
10522 **/
10523static void
10524lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
10525{
10526 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10527 "2827 PCI channel permanent disable for failure\n");
10528
10529 /* Block all SCSI devices' I/Os on the host */
10530 lpfc_scsi_dev_block(phba);
10531
10532 /* stop all timers */
10533 lpfc_stop_hba_timers(phba);
10534
10535 /* Clean up all driver's outstanding SCSI I/Os */
10536 lpfc_sli_flush_fcp_rings(phba);
10537}
10538
10539/**
James Smartda0436e2009-05-22 14:51:39 -040010540 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
10541 * @pdev: pointer to PCI device.
10542 * @state: the current PCI connection state.
10543 *
10544 * This routine is called from the PCI subsystem for error handling to device
10545 * with SLI-4 interface spec. This function is called by the PCI subsystem
10546 * after a PCI bus error affecting this device has been detected. When this
10547 * function is invoked, it will need to stop all the I/Os and interrupt(s)
10548 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
10549 * for the PCI subsystem to perform proper recovery as desired.
10550 *
10551 * Return codes
10552 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10553 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10554 **/
10555static pci_ers_result_t
10556lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
10557{
James Smart75baf692010-06-08 18:31:21 -040010558 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10559 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10560
10561 switch (state) {
10562 case pci_channel_io_normal:
10563 /* Non-fatal error, prepare for recovery */
10564 lpfc_sli4_prep_dev_for_recover(phba);
10565 return PCI_ERS_RESULT_CAN_RECOVER;
10566 case pci_channel_io_frozen:
10567 /* Fatal error, prepare for slot reset */
10568 lpfc_sli4_prep_dev_for_reset(phba);
10569 return PCI_ERS_RESULT_NEED_RESET;
10570 case pci_channel_io_perm_failure:
10571 /* Permanent failure, prepare for device down */
10572 lpfc_sli4_prep_dev_for_perm_failure(phba);
10573 return PCI_ERS_RESULT_DISCONNECT;
10574 default:
10575 /* Unknown state, prepare and request slot reset */
10576 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10577 "2825 Unknown PCI error state: x%x\n", state);
10578 lpfc_sli4_prep_dev_for_reset(phba);
10579 return PCI_ERS_RESULT_NEED_RESET;
10580 }
James Smartda0436e2009-05-22 14:51:39 -040010581}
10582
10583/**
10584 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
10585 * @pdev: pointer to PCI device.
10586 *
10587 * This routine is called from the PCI subsystem for error handling to device
10588 * with SLI-4 interface spec. It is called after PCI bus has been reset to
10589 * restart the PCI card from scratch, as if from a cold-boot. During the
10590 * PCI subsystem error recovery, after the driver returns
10591 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
10592 * recovery and then call this routine before calling the .resume method to
10593 * recover the device. This function will initialize the HBA device, enable
10594 * the interrupt, but it will just put the HBA to offline state without
10595 * passing any I/O traffic.
10596 *
10597 * Return codes
10598 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10599 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10600 */
10601static pci_ers_result_t
10602lpfc_io_slot_reset_s4(struct pci_dev *pdev)
10603{
James Smart75baf692010-06-08 18:31:21 -040010604 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10605 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10606 struct lpfc_sli *psli = &phba->sli;
10607 uint32_t intr_mode;
10608
10609 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
10610 if (pci_enable_device_mem(pdev)) {
10611 printk(KERN_ERR "lpfc: Cannot re-enable "
10612 "PCI device after reset.\n");
10613 return PCI_ERS_RESULT_DISCONNECT;
10614 }
10615
10616 pci_restore_state(pdev);
James Smart0a96e972011-07-22 18:37:28 -040010617
10618 /*
10619 * As the new kernel behavior of pci_restore_state() API call clears
10620 * device saved_state flag, need to save the restored state again.
10621 */
10622 pci_save_state(pdev);
10623
James Smart75baf692010-06-08 18:31:21 -040010624 if (pdev->is_busmaster)
10625 pci_set_master(pdev);
10626
10627 spin_lock_irq(&phba->hbalock);
10628 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
10629 spin_unlock_irq(&phba->hbalock);
10630
10631 /* Configure and enable interrupt */
10632 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
10633 if (intr_mode == LPFC_INTR_ERROR) {
10634 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10635 "2824 Cannot re-enable interrupt after "
10636 "slot reset.\n");
10637 return PCI_ERS_RESULT_DISCONNECT;
10638 } else
10639 phba->intr_mode = intr_mode;
10640
10641 /* Log the current active interrupt mode */
10642 lpfc_log_intr_mode(phba, phba->intr_mode);
10643
James Smartda0436e2009-05-22 14:51:39 -040010644 return PCI_ERS_RESULT_RECOVERED;
10645}
10646
10647/**
10648 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
10649 * @pdev: pointer to PCI device
10650 *
10651 * This routine is called from the PCI subsystem for error handling to device
10652 * with SLI-4 interface spec. It is called when kernel error recovery tells
10653 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
10654 * error recovery. After this call, traffic can start to flow from this device
10655 * again.
10656 **/
10657static void
10658lpfc_io_resume_s4(struct pci_dev *pdev)
10659{
James Smart75baf692010-06-08 18:31:21 -040010660 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10661 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10662
10663 /*
10664 * In case of slot reset, as function reset is performed through
10665 * mailbox command which needs DMA to be enabled, this operation
10666 * has to be moved to the io resume phase. Taking device offline
10667 * will perform the necessary cleanup.
10668 */
10669 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
10670 /* Perform device reset */
James Smart618a5232012-06-12 13:54:36 -040010671 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040010672 lpfc_offline(phba);
10673 lpfc_sli_brdrestart(phba);
10674 /* Bring the device back online */
10675 lpfc_online(phba);
10676 }
10677
10678 /* Clean up Advanced Error Reporting (AER) if needed */
10679 if (phba->hba_flag & HBA_AER_ENABLED)
10680 pci_cleanup_aer_uncorrect_error_status(pdev);
James Smartda0436e2009-05-22 14:51:39 -040010681}
10682
10683/**
James Smart3772a992009-05-22 14:50:54 -040010684 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
10685 * @pdev: pointer to PCI device
10686 * @pid: pointer to PCI device identifier
10687 *
10688 * This routine is to be registered to the kernel's PCI subsystem. When an
10689 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
10690 * at PCI device-specific information of the device and driver to see if the
10691 * driver state that it can support this kind of device. If the match is
10692 * successful, the driver core invokes this routine. This routine dispatches
10693 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
10694 * do all the initialization that it needs to do to handle the HBA device
10695 * properly.
10696 *
10697 * Return code
10698 * 0 - driver can claim the device
10699 * negative value - driver can not claim the device
10700 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010701static int
James Smart3772a992009-05-22 14:50:54 -040010702lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
10703{
10704 int rc;
James Smart8fa38512009-07-19 10:01:03 -040010705 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -040010706
James Smart28baac72010-02-12 14:42:03 -050010707 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -040010708 return -ENODEV;
10709
James Smart8fa38512009-07-19 10:01:03 -040010710 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -050010711 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -040010712 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040010713 else
James Smart3772a992009-05-22 14:50:54 -040010714 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040010715
James Smart3772a992009-05-22 14:50:54 -040010716 return rc;
10717}
10718
10719/**
10720 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
10721 * @pdev: pointer to PCI device
10722 *
10723 * This routine is to be registered to the kernel's PCI subsystem. When an
10724 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
10725 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
10726 * remove routine, which will perform all the necessary cleanup for the
10727 * device to be removed from the PCI subsystem properly.
10728 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010729static void
James Smart3772a992009-05-22 14:50:54 -040010730lpfc_pci_remove_one(struct pci_dev *pdev)
10731{
10732 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10733 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10734
10735 switch (phba->pci_dev_grp) {
10736 case LPFC_PCI_DEV_LP:
10737 lpfc_pci_remove_one_s3(pdev);
10738 break;
James Smartda0436e2009-05-22 14:51:39 -040010739 case LPFC_PCI_DEV_OC:
10740 lpfc_pci_remove_one_s4(pdev);
10741 break;
James Smart3772a992009-05-22 14:50:54 -040010742 default:
10743 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10744 "1424 Invalid PCI device group: 0x%x\n",
10745 phba->pci_dev_grp);
10746 break;
10747 }
10748 return;
10749}
10750
10751/**
10752 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
10753 * @pdev: pointer to PCI device
10754 * @msg: power management message
10755 *
10756 * This routine is to be registered to the kernel's PCI subsystem to support
10757 * system Power Management (PM). When PM invokes this method, it dispatches
10758 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
10759 * suspend the device.
10760 *
10761 * Return code
10762 * 0 - driver suspended the device
10763 * Error otherwise
10764 **/
10765static int
10766lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
10767{
10768 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10769 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10770 int rc = -ENODEV;
10771
10772 switch (phba->pci_dev_grp) {
10773 case LPFC_PCI_DEV_LP:
10774 rc = lpfc_pci_suspend_one_s3(pdev, msg);
10775 break;
James Smartda0436e2009-05-22 14:51:39 -040010776 case LPFC_PCI_DEV_OC:
10777 rc = lpfc_pci_suspend_one_s4(pdev, msg);
10778 break;
James Smart3772a992009-05-22 14:50:54 -040010779 default:
10780 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10781 "1425 Invalid PCI device group: 0x%x\n",
10782 phba->pci_dev_grp);
10783 break;
10784 }
10785 return rc;
10786}
10787
10788/**
10789 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
10790 * @pdev: pointer to PCI device
10791 *
10792 * This routine is to be registered to the kernel's PCI subsystem to support
10793 * system Power Management (PM). When PM invokes this method, it dispatches
10794 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
10795 * resume the device.
10796 *
10797 * Return code
10798 * 0 - driver suspended the device
10799 * Error otherwise
10800 **/
10801static int
10802lpfc_pci_resume_one(struct pci_dev *pdev)
10803{
10804 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10805 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10806 int rc = -ENODEV;
10807
10808 switch (phba->pci_dev_grp) {
10809 case LPFC_PCI_DEV_LP:
10810 rc = lpfc_pci_resume_one_s3(pdev);
10811 break;
James Smartda0436e2009-05-22 14:51:39 -040010812 case LPFC_PCI_DEV_OC:
10813 rc = lpfc_pci_resume_one_s4(pdev);
10814 break;
James Smart3772a992009-05-22 14:50:54 -040010815 default:
10816 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10817 "1426 Invalid PCI device group: 0x%x\n",
10818 phba->pci_dev_grp);
10819 break;
10820 }
10821 return rc;
10822}
10823
10824/**
10825 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
10826 * @pdev: pointer to PCI device.
10827 * @state: the current PCI connection state.
10828 *
10829 * This routine is registered to the PCI subsystem for error handling. This
10830 * function is called by the PCI subsystem after a PCI bus error affecting
10831 * this device has been detected. When this routine is invoked, it dispatches
10832 * the action to the proper SLI-3 or SLI-4 device error detected handling
10833 * routine, which will perform the proper error detected operation.
10834 *
10835 * Return codes
10836 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10837 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10838 **/
10839static pci_ers_result_t
10840lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
10841{
10842 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10843 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10844 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
10845
10846 switch (phba->pci_dev_grp) {
10847 case LPFC_PCI_DEV_LP:
10848 rc = lpfc_io_error_detected_s3(pdev, state);
10849 break;
James Smartda0436e2009-05-22 14:51:39 -040010850 case LPFC_PCI_DEV_OC:
10851 rc = lpfc_io_error_detected_s4(pdev, state);
10852 break;
James Smart3772a992009-05-22 14:50:54 -040010853 default:
10854 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10855 "1427 Invalid PCI device group: 0x%x\n",
10856 phba->pci_dev_grp);
10857 break;
10858 }
10859 return rc;
10860}
10861
10862/**
10863 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
10864 * @pdev: pointer to PCI device.
10865 *
10866 * This routine is registered to the PCI subsystem for error handling. This
10867 * function is called after PCI bus has been reset to restart the PCI card
10868 * from scratch, as if from a cold-boot. When this routine is invoked, it
10869 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
10870 * routine, which will perform the proper device reset.
10871 *
10872 * Return codes
10873 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10874 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10875 **/
10876static pci_ers_result_t
10877lpfc_io_slot_reset(struct pci_dev *pdev)
10878{
10879 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10880 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10881 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
10882
10883 switch (phba->pci_dev_grp) {
10884 case LPFC_PCI_DEV_LP:
10885 rc = lpfc_io_slot_reset_s3(pdev);
10886 break;
James Smartda0436e2009-05-22 14:51:39 -040010887 case LPFC_PCI_DEV_OC:
10888 rc = lpfc_io_slot_reset_s4(pdev);
10889 break;
James Smart3772a992009-05-22 14:50:54 -040010890 default:
10891 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10892 "1428 Invalid PCI device group: 0x%x\n",
10893 phba->pci_dev_grp);
10894 break;
10895 }
10896 return rc;
10897}
10898
10899/**
10900 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
10901 * @pdev: pointer to PCI device
10902 *
10903 * This routine is registered to the PCI subsystem for error handling. It
10904 * is called when kernel error recovery tells the lpfc driver that it is
10905 * OK to resume normal PCI operation after PCI bus error recovery. When
10906 * this routine is invoked, it dispatches the action to the proper SLI-3
10907 * or SLI-4 device io_resume routine, which will resume the device operation.
10908 **/
10909static void
10910lpfc_io_resume(struct pci_dev *pdev)
10911{
10912 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10913 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10914
10915 switch (phba->pci_dev_grp) {
10916 case LPFC_PCI_DEV_LP:
10917 lpfc_io_resume_s3(pdev);
10918 break;
James Smartda0436e2009-05-22 14:51:39 -040010919 case LPFC_PCI_DEV_OC:
10920 lpfc_io_resume_s4(pdev);
10921 break;
James Smart3772a992009-05-22 14:50:54 -040010922 default:
10923 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10924 "1429 Invalid PCI device group: 0x%x\n",
10925 phba->pci_dev_grp);
10926 break;
10927 }
10928 return;
10929}
10930
James Smart1ba981f2014-02-20 09:56:45 -050010931/**
10932 * lpfc_sli4_oas_verify - Verify OAS is supported by this adapter
10933 * @phba: pointer to lpfc hba data structure.
10934 *
10935 * This routine checks to see if OAS is supported for this adapter. If
10936 * supported, the configure Flash Optimized Fabric flag is set. Otherwise,
10937 * the enable oas flag is cleared and the pool created for OAS device data
10938 * is destroyed.
10939 *
10940 **/
10941void
10942lpfc_sli4_oas_verify(struct lpfc_hba *phba)
10943{
10944
10945 if (!phba->cfg_EnableXLane)
10946 return;
10947
10948 if (phba->sli4_hba.pc_sli4_params.oas_supported) {
10949 phba->cfg_fof = 1;
10950 } else {
10951 phba->cfg_EnableXLane = 0;
10952 if (phba->device_data_mem_pool)
10953 mempool_destroy(phba->device_data_mem_pool);
10954 phba->device_data_mem_pool = NULL;
10955 }
10956
10957 return;
10958}
10959
10960/**
10961 * lpfc_fof_queue_setup - Set up all the fof queues
10962 * @phba: pointer to lpfc hba data structure.
10963 *
10964 * This routine is invoked to set up all the fof queues for the FC HBA
10965 * operation.
10966 *
10967 * Return codes
10968 * 0 - successful
10969 * -ENOMEM - No available memory
10970 **/
10971int
10972lpfc_fof_queue_setup(struct lpfc_hba *phba)
10973{
10974 struct lpfc_sli *psli = &phba->sli;
10975 int rc;
10976
10977 rc = lpfc_eq_create(phba, phba->sli4_hba.fof_eq, LPFC_MAX_IMAX);
10978 if (rc)
10979 return -ENOMEM;
10980
10981 if (phba->cfg_EnableXLane) {
10982
10983 rc = lpfc_cq_create(phba, phba->sli4_hba.oas_cq,
10984 phba->sli4_hba.fof_eq, LPFC_WCQ, LPFC_FCP);
10985 if (rc)
10986 goto out_oas_cq;
10987
10988 rc = lpfc_wq_create(phba, phba->sli4_hba.oas_wq,
10989 phba->sli4_hba.oas_cq, LPFC_FCP);
10990 if (rc)
10991 goto out_oas_wq;
10992
10993 phba->sli4_hba.oas_cq->pring = &psli->ring[LPFC_FCP_OAS_RING];
10994 phba->sli4_hba.oas_ring = &psli->ring[LPFC_FCP_OAS_RING];
10995 }
10996
10997 return 0;
10998
10999out_oas_wq:
11000 if (phba->cfg_EnableXLane)
11001 lpfc_cq_destroy(phba, phba->sli4_hba.oas_cq);
11002out_oas_cq:
11003 lpfc_eq_destroy(phba, phba->sli4_hba.fof_eq);
11004 return rc;
11005
11006}
11007
11008/**
11009 * lpfc_fof_queue_create - Create all the fof queues
11010 * @phba: pointer to lpfc hba data structure.
11011 *
11012 * This routine is invoked to allocate all the fof queues for the FC HBA
11013 * operation. For each SLI4 queue type, the parameters such as queue entry
11014 * count (queue depth) shall be taken from the module parameter. For now,
11015 * we just use some constant number as place holder.
11016 *
11017 * Return codes
11018 * 0 - successful
11019 * -ENOMEM - No availble memory
11020 * -EIO - The mailbox failed to complete successfully.
11021 **/
11022int
11023lpfc_fof_queue_create(struct lpfc_hba *phba)
11024{
11025 struct lpfc_queue *qdesc;
11026
11027 /* Create FOF EQ */
11028 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
11029 phba->sli4_hba.eq_ecount);
11030 if (!qdesc)
11031 goto out_error;
11032
11033 phba->sli4_hba.fof_eq = qdesc;
11034
11035 if (phba->cfg_EnableXLane) {
11036
11037 /* Create OAS CQ */
11038 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
11039 phba->sli4_hba.cq_ecount);
11040 if (!qdesc)
11041 goto out_error;
11042
11043 phba->sli4_hba.oas_cq = qdesc;
11044
11045 /* Create OAS WQ */
11046 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
11047 phba->sli4_hba.wq_ecount);
11048 if (!qdesc)
11049 goto out_error;
11050
11051 phba->sli4_hba.oas_wq = qdesc;
11052
11053 }
11054 return 0;
11055
11056out_error:
11057 lpfc_fof_queue_destroy(phba);
11058 return -ENOMEM;
11059}
11060
11061/**
11062 * lpfc_fof_queue_destroy - Destroy all the fof queues
11063 * @phba: pointer to lpfc hba data structure.
11064 *
11065 * This routine is invoked to release all the SLI4 queues with the FC HBA
11066 * operation.
11067 *
11068 * Return codes
11069 * 0 - successful
11070 **/
11071int
11072lpfc_fof_queue_destroy(struct lpfc_hba *phba)
11073{
11074 /* Release FOF Event queue */
11075 if (phba->sli4_hba.fof_eq != NULL) {
11076 lpfc_sli4_queue_free(phba->sli4_hba.fof_eq);
11077 phba->sli4_hba.fof_eq = NULL;
11078 }
11079
11080 /* Release OAS Completion queue */
11081 if (phba->sli4_hba.oas_cq != NULL) {
11082 lpfc_sli4_queue_free(phba->sli4_hba.oas_cq);
11083 phba->sli4_hba.oas_cq = NULL;
11084 }
11085
11086 /* Release OAS Work queue */
11087 if (phba->sli4_hba.oas_wq != NULL) {
11088 lpfc_sli4_queue_free(phba->sli4_hba.oas_wq);
11089 phba->sli4_hba.oas_wq = NULL;
11090 }
11091 return 0;
11092}
11093
dea31012005-04-17 16:05:31 -050011094static struct pci_device_id lpfc_id_table[] = {
11095 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
11096 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -040011097 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
11098 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011099 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
11100 PCI_ANY_ID, PCI_ANY_ID, },
11101 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
11102 PCI_ANY_ID, PCI_ANY_ID, },
11103 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
11104 PCI_ANY_ID, PCI_ANY_ID, },
11105 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
11106 PCI_ANY_ID, PCI_ANY_ID, },
11107 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
11108 PCI_ANY_ID, PCI_ANY_ID, },
11109 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
11110 PCI_ANY_ID, PCI_ANY_ID, },
11111 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
11112 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050011113 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
11114 PCI_ANY_ID, PCI_ANY_ID, },
11115 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
11116 PCI_ANY_ID, PCI_ANY_ID, },
11117 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
11118 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011119 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
11120 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050011121 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
11122 PCI_ANY_ID, PCI_ANY_ID, },
11123 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
11124 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011125 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
11126 PCI_ANY_ID, PCI_ANY_ID, },
11127 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
11128 PCI_ANY_ID, PCI_ANY_ID, },
11129 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
11130 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -040011131 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
11132 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050011133 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
11134 PCI_ANY_ID, PCI_ANY_ID, },
11135 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
11136 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011137 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
11138 PCI_ANY_ID, PCI_ANY_ID, },
11139 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
11140 PCI_ANY_ID, PCI_ANY_ID, },
11141 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
11142 PCI_ANY_ID, PCI_ANY_ID, },
11143 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
11144 PCI_ANY_ID, PCI_ANY_ID, },
11145 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
11146 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050011147 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
11148 PCI_ANY_ID, PCI_ANY_ID, },
11149 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
11150 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -040011151 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
11152 PCI_ANY_ID, PCI_ANY_ID, },
11153 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
11154 PCI_ANY_ID, PCI_ANY_ID, },
11155 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
11156 PCI_ANY_ID, PCI_ANY_ID, },
11157 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
11158 PCI_ANY_ID, PCI_ANY_ID, },
11159 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
11160 PCI_ANY_ID, PCI_ANY_ID, },
11161 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
11162 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -040011163 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
11164 PCI_ANY_ID, PCI_ANY_ID, },
11165 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
11166 PCI_ANY_ID, PCI_ANY_ID, },
11167 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
11168 PCI_ANY_ID, PCI_ANY_ID, },
James Smart3772a992009-05-22 14:50:54 -040011169 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
11170 PCI_ANY_ID, PCI_ANY_ID, },
James Smarta747c9c2009-11-18 15:41:10 -050011171 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
11172 PCI_ANY_ID, PCI_ANY_ID, },
11173 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
James Smart6669f9b2009-10-02 15:16:45 -040011174 PCI_ANY_ID, PCI_ANY_ID, },
James Smart98fc5dd2010-06-07 15:24:29 -040011175 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BALIUS,
11176 PCI_ANY_ID, PCI_ANY_ID, },
James Smart085c6472010-11-20 23:11:37 -050011177 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC,
11178 PCI_ANY_ID, PCI_ANY_ID, },
11179 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE,
11180 PCI_ANY_ID, PCI_ANY_ID, },
James Smartc0c11512011-05-24 11:41:34 -040011181 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC_VF,
11182 PCI_ANY_ID, PCI_ANY_ID, },
11183 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE_VF,
11184 PCI_ANY_ID, PCI_ANY_ID, },
James Smartf8cafd32012-08-03 12:42:51 -040011185 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SKYHAWK,
11186 PCI_ANY_ID, PCI_ANY_ID, },
11187 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SKYHAWK_VF,
11188 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011189 { 0 }
11190};
11191
11192MODULE_DEVICE_TABLE(pci, lpfc_id_table);
11193
Stephen Hemmingera55b2d22012-09-07 09:33:16 -070011194static const struct pci_error_handlers lpfc_err_handler = {
Linas Vepstas8d63f372007-02-14 14:28:36 -060011195 .error_detected = lpfc_io_error_detected,
11196 .slot_reset = lpfc_io_slot_reset,
11197 .resume = lpfc_io_resume,
11198};
11199
dea31012005-04-17 16:05:31 -050011200static struct pci_driver lpfc_driver = {
11201 .name = LPFC_DRIVER_NAME,
11202 .id_table = lpfc_id_table,
11203 .probe = lpfc_pci_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011204 .remove = lpfc_pci_remove_one,
James Smart3a55b532008-12-04 22:38:54 -050011205 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -040011206 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -050011207 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -050011208};
11209
James Smart3ef6d242012-01-18 16:23:48 -050011210static const struct file_operations lpfc_mgmt_fop = {
Al Viro858feac2013-04-14 22:39:37 -040011211 .owner = THIS_MODULE,
James Smart3ef6d242012-01-18 16:23:48 -050011212};
11213
11214static struct miscdevice lpfc_mgmt_dev = {
11215 .minor = MISC_DYNAMIC_MINOR,
11216 .name = "lpfcmgmt",
11217 .fops = &lpfc_mgmt_fop,
11218};
11219
James Smarte59058c2008-08-24 21:49:00 -040011220/**
James Smart3621a712009-04-06 18:47:14 -040011221 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -040011222 *
11223 * This routine is to be invoked when the lpfc module is loaded into the
11224 * kernel. The special kernel macro module_init() is used to indicate the
11225 * role of this routine to the kernel as lpfc module entry point.
11226 *
11227 * Return codes
11228 * 0 - successful
11229 * -ENOMEM - FC attach transport failed
11230 * all others - failed
11231 */
dea31012005-04-17 16:05:31 -050011232static int __init
11233lpfc_init(void)
11234{
James Smart7bb03bb2013-04-17 20:19:16 -040011235 int cpu;
dea31012005-04-17 16:05:31 -050011236 int error = 0;
11237
11238 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040011239 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -050011240
James Smart3ef6d242012-01-18 16:23:48 -050011241 error = misc_register(&lpfc_mgmt_dev);
11242 if (error)
11243 printk(KERN_ERR "Could not register lpfcmgmt device, "
11244 "misc_register returned with status %d", error);
11245
James Smart7ee5d432007-10-27 13:37:17 -040011246 if (lpfc_enable_npiv) {
11247 lpfc_transport_functions.vport_create = lpfc_vport_create;
11248 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
11249 }
dea31012005-04-17 16:05:31 -050011250 lpfc_transport_template =
11251 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -040011252 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -050011253 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -040011254 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -040011255 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -040011256 fc_attach_transport(&lpfc_vport_transport_functions);
11257 if (lpfc_vport_transport_template == NULL) {
11258 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040011259 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -040011260 }
James Smart7ee5d432007-10-27 13:37:17 -040011261 }
James Smart7bb03bb2013-04-17 20:19:16 -040011262
11263 /* Initialize in case vector mapping is needed */
James Smartb246de12013-05-31 17:03:07 -040011264 lpfc_used_cpu = NULL;
11265 lpfc_present_cpu = 0;
11266 for_each_present_cpu(cpu)
11267 lpfc_present_cpu++;
James Smart7bb03bb2013-04-17 20:19:16 -040011268
dea31012005-04-17 16:05:31 -050011269 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -050011270 if (error) {
dea31012005-04-17 16:05:31 -050011271 fc_release_transport(lpfc_transport_template);
James Smartd7c255b2008-08-24 21:50:00 -040011272 if (lpfc_enable_npiv)
11273 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -050011274 }
dea31012005-04-17 16:05:31 -050011275
11276 return error;
11277}
11278
James Smarte59058c2008-08-24 21:49:00 -040011279/**
James Smart3621a712009-04-06 18:47:14 -040011280 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -040011281 *
11282 * This routine is invoked when the lpfc module is removed from the kernel.
11283 * The special kernel macro module_exit() is used to indicate the role of
11284 * this routine to the kernel as lpfc module exit point.
11285 */
dea31012005-04-17 16:05:31 -050011286static void __exit
11287lpfc_exit(void)
11288{
James Smart3ef6d242012-01-18 16:23:48 -050011289 misc_deregister(&lpfc_mgmt_dev);
dea31012005-04-17 16:05:31 -050011290 pci_unregister_driver(&lpfc_driver);
11291 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040011292 if (lpfc_enable_npiv)
11293 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -050011294 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040011295 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
11296 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050011297 (1L << _dump_buf_data_order), _dump_buf_data);
11298 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
11299 }
11300
11301 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -040011302 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
11303 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050011304 (1L << _dump_buf_dif_order), _dump_buf_dif);
11305 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
11306 }
James Smartb246de12013-05-31 17:03:07 -040011307 kfree(lpfc_used_cpu);
dea31012005-04-17 16:05:31 -050011308}
11309
11310module_init(lpfc_init);
11311module_exit(lpfc_exit);
11312MODULE_LICENSE("GPL");
11313MODULE_DESCRIPTION(LPFC_MODULE_DESC);
11314MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
11315MODULE_VERSION("0:" LPFC_DRIVER_VERSION);