blob: c1fca8df6355e28fa6056616dbc52ba66290edb7 [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);
dea31012005-04-17 16:05:31 -050083
84static struct scsi_transport_template *lpfc_transport_template = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050085static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea31012005-04-17 16:05:31 -050086static DEFINE_IDR(lpfc_hba_index);
87
James Smarte59058c2008-08-24 21:49:00 -040088/**
James Smart3621a712009-04-06 18:47:14 -040089 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
James Smarte59058c2008-08-24 21:49:00 -040090 * @phba: pointer to lpfc hba data structure.
91 *
92 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
93 * mailbox command. It retrieves the revision information from the HBA and
94 * collects the Vital Product Data (VPD) about the HBA for preparing the
95 * configuration of the HBA.
96 *
97 * Return codes:
98 * 0 - success.
99 * -ERESTART - requests the SLI layer to reset the HBA and try again.
100 * Any other value - indicates an error.
101 **/
dea31012005-04-17 16:05:31 -0500102int
James Smart2e0fef82007-06-17 19:56:36 -0500103lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500104{
105 lpfc_vpd_t *vp = &phba->vpd;
106 int i = 0, rc;
107 LPFC_MBOXQ_t *pmb;
108 MAILBOX_t *mb;
109 char *lpfc_vpd_data = NULL;
110 uint16_t offset = 0;
111 static char licensed[56] =
112 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -0400113 static int init_key = 1;
dea31012005-04-17 16:05:31 -0500114
115 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
116 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500117 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500118 return -ENOMEM;
119 }
120
James Smart04c68492009-05-22 14:52:52 -0400121 mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -0500122 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500123
124 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -0400125 if (init_key) {
126 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -0500127
James Smart65a29c12006-07-06 15:50:50 -0400128 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
129 *ptext = cpu_to_be32(*ptext);
130 init_key = 0;
131 }
dea31012005-04-17 16:05:31 -0500132
133 lpfc_read_nv(phba, pmb);
134 memset((char*)mb->un.varRDnvp.rsvd3, 0,
135 sizeof (mb->un.varRDnvp.rsvd3));
136 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
137 sizeof (licensed));
138
139 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
140
141 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500142 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smarte8b62012007-08-02 11:10:09 -0400143 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500144 "error, mbxCmd x%x READ_NVPARM, "
145 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500146 mb->mbxCommand, mb->mbxStatus);
147 mempool_free(pmb, phba->mbox_mem_pool);
148 return -ERESTART;
149 }
150 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500151 sizeof(phba->wwnn));
152 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
153 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500154 }
155
James Smart92d7f7b2007-06-17 19:56:38 -0500156 phba->sli3_options = 0x0;
157
dea31012005-04-17 16:05:31 -0500158 /* Setup and issue mailbox READ REV command */
159 lpfc_read_rev(phba, pmb);
160 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
161 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500162 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400163 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500164 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500165 mb->mbxCommand, mb->mbxStatus);
166 mempool_free( pmb, phba->mbox_mem_pool);
167 return -ERESTART;
168 }
169
James Smart92d7f7b2007-06-17 19:56:38 -0500170
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500171 /*
172 * The value of rr must be 1 since the driver set the cv field to 1.
173 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500174 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500175 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500176 vp->rev.rBit = 0;
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500177 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400178 "0440 Adapter failed to init, READ_REV has "
179 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500180 mempool_free(pmb, phba->mbox_mem_pool);
181 return -ERESTART;
dea31012005-04-17 16:05:31 -0500182 }
183
James Smart495a7142008-06-14 22:52:59 -0400184 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
185 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -0500186 return -EINVAL;
James Smart495a7142008-06-14 22:52:59 -0400187 }
James Smarted957682007-06-17 19:56:37 -0500188
dea31012005-04-17 16:05:31 -0500189 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500190 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500191 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500192 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
193 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
194 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
195 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500196 vp->rev.biuRev = mb->un.varRdRev.biuRev;
197 vp->rev.smRev = mb->un.varRdRev.smRev;
198 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
199 vp->rev.endecRev = mb->un.varRdRev.endecRev;
200 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
201 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
202 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
203 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
204 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
205 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
206
James Smart92d7f7b2007-06-17 19:56:38 -0500207 /* If the sli feature level is less then 9, we must
208 * tear down all RPIs and VPIs on link down if NPIV
209 * is enabled.
210 */
211 if (vp->rev.feaLevelHigh < 9)
212 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
213
dea31012005-04-17 16:05:31 -0500214 if (lpfc_is_LC_HBA(phba->pcidev->device))
215 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
216 sizeof (phba->RandomData));
217
dea31012005-04-17 16:05:31 -0500218 /* Get adapter VPD information */
dea31012005-04-17 16:05:31 -0500219 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
220 if (!lpfc_vpd_data)
James Smartd7c255b2008-08-24 21:50:00 -0400221 goto out_free_mbox;
dea31012005-04-17 16:05:31 -0500222 do {
James Smarta0c87cb2009-07-19 10:01:10 -0400223 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
dea31012005-04-17 16:05:31 -0500224 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
225
226 if (rc != MBX_SUCCESS) {
227 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400228 "0441 VPD not present on adapter, "
dea31012005-04-17 16:05:31 -0500229 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500230 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500231 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500232 }
James Smart04c68492009-05-22 14:52:52 -0400233 /* dump mem may return a zero when finished or we got a
234 * mailbox error, either way we are done.
235 */
236 if (mb->un.varDmp.word_cnt == 0)
237 break;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500238 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
239 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
James Smartd7c255b2008-08-24 21:50:00 -0400240 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
241 lpfc_vpd_data + offset,
James Smart92d7f7b2007-06-17 19:56:38 -0500242 mb->un.varDmp.word_cnt);
dea31012005-04-17 16:05:31 -0500243 offset += mb->un.varDmp.word_cnt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500244 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
245 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500246
247 kfree(lpfc_vpd_data);
dea31012005-04-17 16:05:31 -0500248out_free_mbox:
249 mempool_free(pmb, phba->mbox_mem_pool);
250 return 0;
251}
252
James Smarte59058c2008-08-24 21:49:00 -0400253/**
James Smart3621a712009-04-06 18:47:14 -0400254 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
James Smarte59058c2008-08-24 21:49:00 -0400255 * @phba: pointer to lpfc hba data structure.
256 * @pmboxq: pointer to the driver internal queue element for mailbox command.
257 *
258 * This is the completion handler for driver's configuring asynchronous event
259 * mailbox command to the device. If the mailbox command returns successfully,
260 * it will set internal async event support flag to 1; otherwise, it will
261 * set internal async event support flag to 0.
262 **/
James Smart57127f12007-10-27 13:37:05 -0400263static void
264lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
265{
James Smart04c68492009-05-22 14:52:52 -0400266 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
James Smart57127f12007-10-27 13:37:05 -0400267 phba->temp_sensor_support = 1;
268 else
269 phba->temp_sensor_support = 0;
270 mempool_free(pmboxq, phba->mbox_mem_pool);
271 return;
272}
273
James Smarte59058c2008-08-24 21:49:00 -0400274/**
James Smart3621a712009-04-06 18:47:14 -0400275 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
James Smart97207482008-12-04 22:39:19 -0500276 * @phba: pointer to lpfc hba data structure.
277 * @pmboxq: pointer to the driver internal queue element for mailbox command.
278 *
279 * This is the completion handler for dump mailbox command for getting
280 * wake up parameters. When this command complete, the response contain
281 * Option rom version of the HBA. This function translate the version number
282 * into a human readable string and store it in OptionROMVersion.
283 **/
284static void
285lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
286{
287 struct prog_id *prg;
288 uint32_t prog_id_word;
289 char dist = ' ';
290 /* character array used for decoding dist type. */
291 char dist_char[] = "nabx";
292
James Smart04c68492009-05-22 14:52:52 -0400293 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
James Smart9f1e1b52008-12-04 22:39:40 -0500294 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500295 return;
James Smart9f1e1b52008-12-04 22:39:40 -0500296 }
James Smart97207482008-12-04 22:39:19 -0500297
298 prg = (struct prog_id *) &prog_id_word;
299
300 /* word 7 contain option rom version */
James Smart04c68492009-05-22 14:52:52 -0400301 prog_id_word = pmboxq->u.mb.un.varWords[7];
James Smart97207482008-12-04 22:39:19 -0500302
303 /* Decode the Option rom version word to a readable string */
304 if (prg->dist < 4)
305 dist = dist_char[prg->dist];
306
307 if ((prg->dist == 3) && (prg->num == 0))
308 sprintf(phba->OptionROMVersion, "%d.%d%d",
309 prg->ver, prg->rev, prg->lev);
310 else
311 sprintf(phba->OptionROMVersion, "%d.%d%d%c%d",
312 prg->ver, prg->rev, prg->lev,
313 dist, prg->num);
James Smart9f1e1b52008-12-04 22:39:40 -0500314 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500315 return;
316}
317
318/**
James Smart05580562011-05-24 11:40:48 -0400319 * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
320 * cfg_soft_wwnn, cfg_soft_wwpn
321 * @vport: pointer to lpfc vport data structure.
322 *
323 *
324 * Return codes
325 * None.
326 **/
327void
328lpfc_update_vport_wwn(struct lpfc_vport *vport)
329{
330 /* If the soft name exists then update it using the service params */
331 if (vport->phba->cfg_soft_wwnn)
332 u64_to_wwn(vport->phba->cfg_soft_wwnn,
333 vport->fc_sparam.nodeName.u.wwn);
334 if (vport->phba->cfg_soft_wwpn)
335 u64_to_wwn(vport->phba->cfg_soft_wwpn,
336 vport->fc_sparam.portName.u.wwn);
337
338 /*
339 * If the name is empty or there exists a soft name
340 * then copy the service params name, otherwise use the fc name
341 */
342 if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
343 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
344 sizeof(struct lpfc_name));
345 else
346 memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
347 sizeof(struct lpfc_name));
348
349 if (vport->fc_portname.u.wwn[0] == 0 || vport->phba->cfg_soft_wwpn)
350 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
351 sizeof(struct lpfc_name));
352 else
353 memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
354 sizeof(struct lpfc_name));
355}
356
357/**
James Smart3621a712009-04-06 18:47:14 -0400358 * lpfc_config_port_post - Perform lpfc initialization after config port
James Smarte59058c2008-08-24 21:49:00 -0400359 * @phba: pointer to lpfc hba data structure.
360 *
361 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
362 * command call. It performs all internal resource and state setups on the
363 * port: post IOCB buffers, enable appropriate host interrupt attentions,
364 * ELS ring timers, etc.
365 *
366 * Return codes
367 * 0 - success.
368 * Any other value - error.
369 **/
dea31012005-04-17 16:05:31 -0500370int
James Smart2e0fef82007-06-17 19:56:36 -0500371lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500372{
James Smart2e0fef82007-06-17 19:56:36 -0500373 struct lpfc_vport *vport = phba->pport;
James Smarta257bf92009-04-06 18:48:10 -0400374 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -0500375 LPFC_MBOXQ_t *pmb;
376 MAILBOX_t *mb;
377 struct lpfc_dmabuf *mp;
378 struct lpfc_sli *psli = &phba->sli;
379 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500380 int i, j;
381 int rc;
dea31012005-04-17 16:05:31 -0500382
James Smart7af67052007-10-27 13:38:11 -0400383 spin_lock_irq(&phba->hbalock);
384 /*
385 * If the Config port completed correctly the HBA is not
386 * over heated any more.
387 */
388 if (phba->over_temp_state == HBA_OVER_TEMP)
389 phba->over_temp_state = HBA_NORMAL_TEMP;
390 spin_unlock_irq(&phba->hbalock);
391
dea31012005-04-17 16:05:31 -0500392 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
393 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500394 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500395 return -ENOMEM;
396 }
James Smart04c68492009-05-22 14:52:52 -0400397 mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500398
dea31012005-04-17 16:05:31 -0500399 /* Get login parameters for NID. */
James Smart9f1177a2010-02-26 14:12:57 -0500400 rc = lpfc_read_sparam(phba, pmb, 0);
401 if (rc) {
402 mempool_free(pmb, phba->mbox_mem_pool);
403 return -ENOMEM;
404 }
405
James Smarted957682007-06-17 19:56:37 -0500406 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500407 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500408 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400409 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500410 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500411 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500412 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500413 mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart9f1177a2010-02-26 14:12:57 -0500414 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500415 lpfc_mbuf_free(phba, mp->virt, mp->phys);
416 kfree(mp);
417 return -EIO;
418 }
419
420 mp = (struct lpfc_dmabuf *) pmb->context1;
421
James Smart2e0fef82007-06-17 19:56:36 -0500422 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500423 lpfc_mbuf_free(phba, mp->virt, mp->phys);
424 kfree(mp);
425 pmb->context1 = NULL;
James Smart05580562011-05-24 11:40:48 -0400426 lpfc_update_vport_wwn(vport);
James Smarta257bf92009-04-06 18:48:10 -0400427
428 /* Update the fc_host data structures with new wwn. */
429 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
430 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart21e9a0a2009-05-22 14:53:21 -0400431 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smarta257bf92009-04-06 18:48:10 -0400432
dea31012005-04-17 16:05:31 -0500433 /* If no serial number in VPD data, use low 6 bytes of WWNN */
434 /* This should be consolidated into parse_vpd ? - mr */
435 if (phba->SerialNumber[0] == 0) {
436 uint8_t *outptr;
437
James Smart2e0fef82007-06-17 19:56:36 -0500438 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500439 for (i = 0; i < 12; i++) {
440 status = *outptr++;
441 j = ((status & 0xf0) >> 4);
442 if (j <= 9)
443 phba->SerialNumber[i] =
444 (char)((uint8_t) 0x30 + (uint8_t) j);
445 else
446 phba->SerialNumber[i] =
447 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
448 i++;
449 j = (status & 0xf);
450 if (j <= 9)
451 phba->SerialNumber[i] =
452 (char)((uint8_t) 0x30 + (uint8_t) j);
453 else
454 phba->SerialNumber[i] =
455 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
456 }
457 }
458
dea31012005-04-17 16:05:31 -0500459 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500460 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500461 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500462 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400463 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500464 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500465 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500466 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500467 mempool_free( pmb, phba->mbox_mem_pool);
468 return -EIO;
469 }
470
James Smarta0c87cb2009-07-19 10:01:10 -0400471 /* Check if the port is disabled */
472 lpfc_sli_read_link_ste(phba);
473
dea31012005-04-17 16:05:31 -0500474 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -0400475 i = (mb->un.varRdConfig.max_xri + 1);
476 if (phba->cfg_hba_queue_depth > i) {
477 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
478 "3359 HBA queue depth changed from %d to %d\n",
479 phba->cfg_hba_queue_depth, i);
480 phba->cfg_hba_queue_depth = i;
481 }
482
483 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
484 i = (mb->un.varRdConfig.max_xri >> 3);
485 if (phba->pport->cfg_lun_queue_depth > i) {
486 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
487 "3360 LUN queue depth changed from %d to %d\n",
488 phba->pport->cfg_lun_queue_depth, i);
489 phba->pport->cfg_lun_queue_depth = i;
490 }
dea31012005-04-17 16:05:31 -0500491
492 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500493
494 /* Get the default values for Model Name and Description */
495 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
496
James Smart2e0fef82007-06-17 19:56:36 -0500497 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500498
James Smart0b727fe2007-10-27 13:37:25 -0400499 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smart7e56aa22012-08-03 12:35:34 -0400500 if (psli->ring[psli->extra_ring].sli.sli3.cmdringaddr)
James Smarta4bc3372006-12-02 13:34:16 -0500501 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
James Smart7e56aa22012-08-03 12:35:34 -0400502 if (psli->ring[psli->fcp_ring].sli.sli3.cmdringaddr)
dea31012005-04-17 16:05:31 -0500503 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
James Smart7e56aa22012-08-03 12:35:34 -0400504 if (psli->ring[psli->next_ring].sli.sli3.cmdringaddr)
dea31012005-04-17 16:05:31 -0500505 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
506
507 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500508 if (phba->sli_rev != 3)
509 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500510
James Smart93996272008-08-24 21:50:30 -0400511 /*
512 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
513 */
514 if (phba->intr_type == MSIX) {
515 rc = lpfc_config_msi(phba, pmb);
516 if (rc) {
517 mempool_free(pmb, phba->mbox_mem_pool);
518 return -EIO;
519 }
520 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
521 if (rc != MBX_SUCCESS) {
522 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
523 "0352 Config MSI mailbox command "
524 "failed, mbxCmd x%x, mbxStatus x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400525 pmb->u.mb.mbxCommand,
526 pmb->u.mb.mbxStatus);
James Smart93996272008-08-24 21:50:30 -0400527 mempool_free(pmb, phba->mbox_mem_pool);
528 return -EIO;
529 }
530 }
531
James Smart04c68492009-05-22 14:52:52 -0400532 spin_lock_irq(&phba->hbalock);
James Smart93996272008-08-24 21:50:30 -0400533 /* Initialize ERATT handling flag */
534 phba->hba_flag &= ~HBA_ERATT_HANDLED;
535
dea31012005-04-17 16:05:31 -0500536 /* Enable appropriate host interrupts */
James Smart9940b972011-03-11 16:06:12 -0500537 if (lpfc_readl(phba->HCregaddr, &status)) {
538 spin_unlock_irq(&phba->hbalock);
539 return -EIO;
540 }
dea31012005-04-17 16:05:31 -0500541 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
542 if (psli->num_rings > 0)
543 status |= HC_R0INT_ENA;
544 if (psli->num_rings > 1)
545 status |= HC_R1INT_ENA;
546 if (psli->num_rings > 2)
547 status |= HC_R2INT_ENA;
548 if (psli->num_rings > 3)
549 status |= HC_R3INT_ENA;
550
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500551 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
552 (phba->cfg_poll & DISABLE_FCP_RING_INT))
James Smart93996272008-08-24 21:50:30 -0400553 status &= ~(HC_R0INT_ENA);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500554
dea31012005-04-17 16:05:31 -0500555 writel(status, phba->HCregaddr);
556 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500557 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500558
James Smart93996272008-08-24 21:50:30 -0400559 /* Set up ring-0 (ELS) timer */
560 timeout = phba->fc_ratov * 2;
James Smart256ec0d2013-04-17 20:14:58 -0400561 mod_timer(&vport->els_tmofunc,
562 jiffies + msecs_to_jiffies(1000 * timeout));
James Smart93996272008-08-24 21:50:30 -0400563 /* Set up heart beat (HB) timer */
James Smart256ec0d2013-04-17 20:14:58 -0400564 mod_timer(&phba->hb_tmofunc,
565 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -0500566 phba->hb_outstanding = 0;
567 phba->last_completion_time = jiffies;
James Smart93996272008-08-24 21:50:30 -0400568 /* Set up error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -0400569 mod_timer(&phba->eratt_poll,
570 jiffies + msecs_to_jiffies(1000 * LPFC_ERATT_POLL_INTERVAL));
dea31012005-04-17 16:05:31 -0500571
James Smarta0c87cb2009-07-19 10:01:10 -0400572 if (phba->hba_flag & LINK_DISABLED) {
573 lpfc_printf_log(phba,
574 KERN_ERR, LOG_INIT,
575 "2598 Adapter Link is disabled.\n");
576 lpfc_down_link(phba, pmb);
577 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
578 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
579 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
580 lpfc_printf_log(phba,
581 KERN_ERR, LOG_INIT,
582 "2599 Adapter failed to issue DOWN_LINK"
583 " mbox command rc 0x%x\n", rc);
584
585 mempool_free(pmb, phba->mbox_mem_pool);
586 return -EIO;
587 }
James Smarte40a02c2010-02-26 14:13:54 -0500588 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart026abb82011-12-13 13:20:45 -0500589 mempool_free(pmb, phba->mbox_mem_pool);
590 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
591 if (rc)
592 return rc;
dea31012005-04-17 16:05:31 -0500593 }
594 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400595 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500596 if (!pmb) {
597 phba->link_state = LPFC_HBA_ERROR;
598 return -ENOMEM;
599 }
600
James Smart57127f12007-10-27 13:37:05 -0400601 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
602 pmb->mbox_cmpl = lpfc_config_async_cmpl;
603 pmb->vport = phba->pport;
604 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500605
James Smart57127f12007-10-27 13:37:05 -0400606 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
607 lpfc_printf_log(phba,
608 KERN_ERR,
609 LOG_INIT,
610 "0456 Adapter failed to issue "
James Smarte4e74272009-07-19 10:01:38 -0400611 "ASYNCEVT_ENABLE mbox status x%x\n",
James Smart57127f12007-10-27 13:37:05 -0400612 rc);
613 mempool_free(pmb, phba->mbox_mem_pool);
614 }
James Smart97207482008-12-04 22:39:19 -0500615
616 /* Get Option rom version */
617 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500618 if (!pmb) {
619 phba->link_state = LPFC_HBA_ERROR;
620 return -ENOMEM;
621 }
622
James Smart97207482008-12-04 22:39:19 -0500623 lpfc_dump_wakeup_param(phba, pmb);
624 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
625 pmb->vport = phba->pport;
626 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
627
628 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
629 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
James Smarte4e74272009-07-19 10:01:38 -0400630 "to get Option ROM version status x%x\n", rc);
James Smart97207482008-12-04 22:39:19 -0500631 mempool_free(pmb, phba->mbox_mem_pool);
632 }
633
James Smartd7c255b2008-08-24 21:50:00 -0400634 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400635}
636
James Smarte59058c2008-08-24 21:49:00 -0400637/**
James Smart84d1b002010-02-12 14:42:33 -0500638 * lpfc_hba_init_link - Initialize the FC link
639 * @phba: pointer to lpfc hba data structure.
James Smart6e7288d2010-06-07 15:23:35 -0400640 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500641 *
642 * This routine will issue the INIT_LINK mailbox command call.
643 * It is available to other drivers through the lpfc_hba data
644 * structure for use as a delayed link up mechanism with the
645 * module parameter lpfc_suppress_link_up.
646 *
647 * Return code
648 * 0 - success
649 * Any other value - error
650 **/
651int
James Smart6e7288d2010-06-07 15:23:35 -0400652lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500653{
James Smart1b511972011-12-13 13:23:09 -0500654 return lpfc_hba_init_link_fc_topology(phba, phba->cfg_topology, flag);
655}
656
657/**
658 * lpfc_hba_init_link_fc_topology - Initialize FC link with desired topology
659 * @phba: pointer to lpfc hba data structure.
660 * @fc_topology: desired fc topology.
661 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
662 *
663 * This routine will issue the INIT_LINK mailbox command call.
664 * It is available to other drivers through the lpfc_hba data
665 * structure for use as a delayed link up mechanism with the
666 * module parameter lpfc_suppress_link_up.
667 *
668 * Return code
669 * 0 - success
670 * Any other value - error
671 **/
672int
673lpfc_hba_init_link_fc_topology(struct lpfc_hba *phba, uint32_t fc_topology,
674 uint32_t flag)
675{
James Smart84d1b002010-02-12 14:42:33 -0500676 struct lpfc_vport *vport = phba->pport;
677 LPFC_MBOXQ_t *pmb;
678 MAILBOX_t *mb;
679 int rc;
680
681 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
682 if (!pmb) {
683 phba->link_state = LPFC_HBA_ERROR;
684 return -ENOMEM;
685 }
686 mb = &pmb->u.mb;
687 pmb->vport = vport;
688
James Smart026abb82011-12-13 13:20:45 -0500689 if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_MAX) ||
690 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G) &&
691 !(phba->lmt & LMT_1Gb)) ||
692 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G) &&
693 !(phba->lmt & LMT_2Gb)) ||
694 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G) &&
695 !(phba->lmt & LMT_4Gb)) ||
696 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G) &&
697 !(phba->lmt & LMT_8Gb)) ||
698 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G) &&
699 !(phba->lmt & LMT_10Gb)) ||
700 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G) &&
701 !(phba->lmt & LMT_16Gb))) {
702 /* Reset link speed to auto */
703 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
704 "1302 Invalid speed for this board:%d "
705 "Reset link speed to auto.\n",
706 phba->cfg_link_speed);
707 phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
708 }
James Smart1b511972011-12-13 13:23:09 -0500709 lpfc_init_link(phba, pmb, fc_topology, phba->cfg_link_speed);
James Smart84d1b002010-02-12 14:42:33 -0500710 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart1b511972011-12-13 13:23:09 -0500711 if (phba->sli_rev < LPFC_SLI_REV4)
712 lpfc_set_loopback_flag(phba);
James Smart6e7288d2010-06-07 15:23:35 -0400713 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart76a95d72010-11-20 23:11:48 -0500714 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
James Smart84d1b002010-02-12 14:42:33 -0500715 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
716 "0498 Adapter failed to init, mbxCmd x%x "
717 "INIT_LINK, mbxStatus x%x\n",
718 mb->mbxCommand, mb->mbxStatus);
James Smart76a95d72010-11-20 23:11:48 -0500719 if (phba->sli_rev <= LPFC_SLI_REV3) {
720 /* Clear all interrupt enable conditions */
721 writel(0, phba->HCregaddr);
722 readl(phba->HCregaddr); /* flush */
723 /* Clear all pending interrupts */
724 writel(0xffffffff, phba->HAregaddr);
725 readl(phba->HAregaddr); /* flush */
726 }
James Smart84d1b002010-02-12 14:42:33 -0500727 phba->link_state = LPFC_HBA_ERROR;
James Smart6e7288d2010-06-07 15:23:35 -0400728 if (rc != MBX_BUSY || flag == MBX_POLL)
James Smart84d1b002010-02-12 14:42:33 -0500729 mempool_free(pmb, phba->mbox_mem_pool);
730 return -EIO;
731 }
James Smarte40a02c2010-02-26 14:13:54 -0500732 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
James Smart6e7288d2010-06-07 15:23:35 -0400733 if (flag == MBX_POLL)
734 mempool_free(pmb, phba->mbox_mem_pool);
James Smart84d1b002010-02-12 14:42:33 -0500735
736 return 0;
737}
738
739/**
740 * lpfc_hba_down_link - this routine downs the FC link
James Smart6e7288d2010-06-07 15:23:35 -0400741 * @phba: pointer to lpfc hba data structure.
742 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500743 *
744 * This routine will issue the DOWN_LINK mailbox command call.
745 * It is available to other drivers through the lpfc_hba data
746 * structure for use to stop the link.
747 *
748 * Return code
749 * 0 - success
750 * Any other value - error
751 **/
752int
James Smart6e7288d2010-06-07 15:23:35 -0400753lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500754{
755 LPFC_MBOXQ_t *pmb;
756 int rc;
757
758 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
759 if (!pmb) {
760 phba->link_state = LPFC_HBA_ERROR;
761 return -ENOMEM;
762 }
763
764 lpfc_printf_log(phba,
765 KERN_ERR, LOG_INIT,
766 "0491 Adapter Link is disabled.\n");
767 lpfc_down_link(phba, pmb);
768 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6e7288d2010-06-07 15:23:35 -0400769 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart84d1b002010-02-12 14:42:33 -0500770 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
771 lpfc_printf_log(phba,
772 KERN_ERR, LOG_INIT,
773 "2522 Adapter failed to issue DOWN_LINK"
774 " mbox command rc 0x%x\n", rc);
775
776 mempool_free(pmb, phba->mbox_mem_pool);
777 return -EIO;
778 }
James Smart6e7288d2010-06-07 15:23:35 -0400779 if (flag == MBX_POLL)
780 mempool_free(pmb, phba->mbox_mem_pool);
781
James Smart84d1b002010-02-12 14:42:33 -0500782 return 0;
783}
784
785/**
James Smart3621a712009-04-06 18:47:14 -0400786 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400787 * @phba: pointer to lpfc HBA data structure.
788 *
789 * This routine will do LPFC uninitialization before the HBA is reset when
790 * bringing down the SLI Layer.
791 *
792 * Return codes
793 * 0 - success.
794 * Any other value - error.
795 **/
dea31012005-04-17 16:05:31 -0500796int
James Smart2e0fef82007-06-17 19:56:36 -0500797lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500798{
James Smart1b32f6a2008-02-08 18:49:39 -0500799 struct lpfc_vport **vports;
800 int i;
James Smart3772a992009-05-22 14:50:54 -0400801
802 if (phba->sli_rev <= LPFC_SLI_REV3) {
803 /* Disable interrupts */
804 writel(0, phba->HCregaddr);
805 readl(phba->HCregaddr); /* flush */
806 }
dea31012005-04-17 16:05:31 -0500807
James Smart1b32f6a2008-02-08 18:49:39 -0500808 if (phba->pport->load_flag & FC_UNLOADING)
809 lpfc_cleanup_discovery_resources(phba->pport);
810 else {
811 vports = lpfc_create_vport_work_array(phba);
812 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400813 for (i = 0; i <= phba->max_vports &&
814 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500815 lpfc_cleanup_discovery_resources(vports[i]);
816 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500817 }
818 return 0;
dea31012005-04-17 16:05:31 -0500819}
820
James Smarte59058c2008-08-24 21:49:00 -0400821/**
James Smart3772a992009-05-22 14:50:54 -0400822 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400823 * @phba: pointer to lpfc HBA data structure.
824 *
825 * This routine will do uninitialization after the HBA is reset when bring
826 * down the SLI Layer.
827 *
828 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200829 * 0 - success.
James Smarte59058c2008-08-24 21:49:00 -0400830 * Any other value - error.
831 **/
James Smart3772a992009-05-22 14:50:54 -0400832static int
833lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -0500834{
835 struct lpfc_sli *psli = &phba->sli;
836 struct lpfc_sli_ring *pring;
837 struct lpfc_dmabuf *mp, *next_mp;
James Smart09372822008-01-11 01:52:54 -0500838 LIST_HEAD(completions);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500839 int i;
840
James Smart92d7f7b2007-06-17 19:56:38 -0500841 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
842 lpfc_sli_hbqbuf_free_all(phba);
843 else {
844 /* Cleanup preposted buffers on the ELS ring */
845 pring = &psli->ring[LPFC_ELS_RING];
846 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
847 list_del(&mp->list);
848 pring->postbufq_cnt--;
849 lpfc_mbuf_free(phba, mp->virt, mp->phys);
850 kfree(mp);
851 }
Jamie Wellnitz41415862006-02-28 19:25:27 -0500852 }
853
James Smart09372822008-01-11 01:52:54 -0500854 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500855 for (i = 0; i < psli->num_rings; i++) {
856 pring = &psli->ring[i];
James Smart09372822008-01-11 01:52:54 -0500857
858 /* At this point in time the HBA is either reset or DOA. Either
859 * way, nothing should be on txcmplq as it will NEVER complete.
860 */
861 list_splice_init(&pring->txcmplq, &completions);
James Smart09372822008-01-11 01:52:54 -0500862 spin_unlock_irq(&phba->hbalock);
863
James Smarta257bf92009-04-06 18:48:10 -0400864 /* Cancel all the IOCBs from the completions list */
865 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
866 IOERR_SLI_ABORTED);
James Smart09372822008-01-11 01:52:54 -0500867
Jamie Wellnitz41415862006-02-28 19:25:27 -0500868 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart09372822008-01-11 01:52:54 -0500869 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500870 }
James Smart09372822008-01-11 01:52:54 -0500871 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500872
873 return 0;
874}
James Smart5af5eee2010-10-22 11:06:38 -0400875
James Smartda0436e2009-05-22 14:51:39 -0400876/**
877 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
878 * @phba: pointer to lpfc HBA data structure.
879 *
880 * This routine will do uninitialization after the HBA is reset when bring
881 * down the SLI Layer.
882 *
883 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200884 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400885 * Any other value - error.
886 **/
887static int
888lpfc_hba_down_post_s4(struct lpfc_hba *phba)
889{
890 struct lpfc_scsi_buf *psb, *psb_next;
891 LIST_HEAD(aborts);
892 int ret;
893 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500894 struct lpfc_sglq *sglq_entry = NULL;
895
James Smartda0436e2009-05-22 14:51:39 -0400896 ret = lpfc_hba_down_post_s3(phba);
897 if (ret)
898 return ret;
899 /* At this point in time the HBA is either reset or DOA. Either
900 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
901 * on the lpfc_sgl_list so that it can either be freed if the
902 * driver is unloading or reposted if the driver is restarting
903 * the port.
904 */
905 spin_lock_irq(&phba->hbalock); /* required for lpfc_sgl_list and */
906 /* scsl_buf_list */
907 /* abts_sgl_list_lock required because worker thread uses this
908 * list.
909 */
910 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -0500911 list_for_each_entry(sglq_entry,
912 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
913 sglq_entry->state = SGL_FREED;
914
James Smartda0436e2009-05-22 14:51:39 -0400915 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
916 &phba->sli4_hba.lpfc_sgl_list);
917 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
918 /* abts_scsi_buf_list_lock required because worker thread uses this
919 * list.
920 */
921 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
922 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
923 &aborts);
924 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
925 spin_unlock_irq(&phba->hbalock);
926
927 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
928 psb->pCmd = NULL;
929 psb->status = IOSTAT_SUCCESS;
930 }
James Smarta40fc5f2013-04-17 20:17:40 -0400931 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
932 list_splice(&aborts, &phba->lpfc_scsi_buf_list_put);
933 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400934 return 0;
935}
936
937/**
938 * lpfc_hba_down_post - Wrapper func for hba down post routine
939 * @phba: pointer to lpfc HBA data structure.
940 *
941 * This routine wraps the actual SLI3 or SLI4 routine for performing
942 * uninitialization after the HBA is reset when bring down the SLI Layer.
943 *
944 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200945 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400946 * Any other value - error.
947 **/
948int
949lpfc_hba_down_post(struct lpfc_hba *phba)
950{
951 return (*phba->lpfc_hba_down_post)(phba);
952}
Jamie Wellnitz41415862006-02-28 19:25:27 -0500953
James Smarte59058c2008-08-24 21:49:00 -0400954/**
James Smart3621a712009-04-06 18:47:14 -0400955 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -0400956 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
957 *
958 * This is the HBA-timer timeout handler registered to the lpfc driver. When
959 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
960 * work-port-events bitmap and the worker thread is notified. This timeout
961 * event will be used by the worker thread to invoke the actual timeout
962 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
963 * be performed in the timeout handler and the HBA timeout event bit shall
964 * be cleared by the worker thread after it has taken the event bitmap out.
965 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100966static void
James Smart858c9f62007-06-17 19:56:39 -0500967lpfc_hb_timeout(unsigned long ptr)
968{
969 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400970 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -0500971 unsigned long iflag;
972
973 phba = (struct lpfc_hba *)ptr;
James Smart93996272008-08-24 21:50:30 -0400974
975 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -0500976 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -0400977 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
978 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -0500979 phba->pport->work_port_events |= WORKER_HB_TMO;
980 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
981
James Smart93996272008-08-24 21:50:30 -0400982 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -0400983 if (!tmo_posted)
984 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -0500985 return;
986}
987
James Smarte59058c2008-08-24 21:49:00 -0400988/**
James Smart19ca7602010-11-20 23:11:55 -0500989 * lpfc_rrq_timeout - The RRQ-timer timeout handler
990 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
991 *
992 * This is the RRQ-timer timeout handler registered to the lpfc driver. When
993 * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
994 * work-port-events bitmap and the worker thread is notified. This timeout
995 * event will be used by the worker thread to invoke the actual timeout
996 * handler routine, lpfc_rrq_handler. Any periodical operations will
997 * be performed in the timeout handler and the RRQ timeout event bit shall
998 * be cleared by the worker thread after it has taken the event bitmap out.
999 **/
1000static void
1001lpfc_rrq_timeout(unsigned long ptr)
1002{
1003 struct lpfc_hba *phba;
James Smart19ca7602010-11-20 23:11:55 -05001004 unsigned long iflag;
1005
1006 phba = (struct lpfc_hba *)ptr;
1007 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -05001008 phba->hba_flag |= HBA_RRQ_ACTIVE;
James Smart19ca7602010-11-20 23:11:55 -05001009 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -05001010 lpfc_worker_wake_up(phba);
James Smart19ca7602010-11-20 23:11:55 -05001011}
1012
1013/**
James Smart3621a712009-04-06 18:47:14 -04001014 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -04001015 * @phba: pointer to lpfc hba data structure.
1016 * @pmboxq: pointer to the driver internal queue element for mailbox command.
1017 *
1018 * This is the callback function to the lpfc heart-beat mailbox command.
1019 * If configured, the lpfc driver issues the heart-beat mailbox command to
1020 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
1021 * heart-beat mailbox command is issued, the driver shall set up heart-beat
1022 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
1023 * heart-beat outstanding state. Once the mailbox command comes back and
1024 * no error conditions detected, the heart-beat mailbox command timer is
1025 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1026 * state is cleared for the next heart-beat. If the timer expired with the
1027 * heart-beat outstanding state set, the driver will put the HBA offline.
1028 **/
James Smart858c9f62007-06-17 19:56:39 -05001029static void
1030lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1031{
1032 unsigned long drvr_flag;
1033
1034 spin_lock_irqsave(&phba->hbalock, drvr_flag);
1035 phba->hb_outstanding = 0;
1036 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1037
James Smart93996272008-08-24 21:50:30 -04001038 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -05001039 mempool_free(pmboxq, phba->mbox_mem_pool);
1040 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1041 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -04001042 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -05001043 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001044 jiffies +
1045 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001046 return;
1047}
1048
James Smarte59058c2008-08-24 21:49:00 -04001049/**
James Smart3621a712009-04-06 18:47:14 -04001050 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001051 * @phba: pointer to lpfc hba data structure.
1052 *
1053 * This is the actual HBA-timer timeout handler to be invoked by the worker
1054 * thread whenever the HBA timer fired and HBA-timeout event posted. This
1055 * handler performs any periodic operations needed for the device. If such
1056 * periodic event has already been attended to either in the interrupt handler
1057 * or by processing slow-ring or fast-ring events within the HBA-timer
1058 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1059 * the timer for the next timeout period. If lpfc heart-beat mailbox command
1060 * is configured and there is no heart-beat mailbox command outstanding, a
1061 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1062 * has been a heart-beat mailbox command outstanding, the HBA shall be put
1063 * to offline.
1064 **/
James Smart858c9f62007-06-17 19:56:39 -05001065void
1066lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1067{
James Smart45ed1192009-10-02 15:17:02 -04001068 struct lpfc_vport **vports;
James Smart858c9f62007-06-17 19:56:39 -05001069 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -05001070 struct lpfc_dmabuf *buf_ptr;
James Smart45ed1192009-10-02 15:17:02 -04001071 int retval, i;
James Smart858c9f62007-06-17 19:56:39 -05001072 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -05001073 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -05001074
James Smart45ed1192009-10-02 15:17:02 -04001075 vports = lpfc_create_vport_work_array(phba);
1076 if (vports != NULL)
1077 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
1078 lpfc_rcv_seq_check_edtov(vports[i]);
1079 lpfc_destroy_vport_work_array(phba, vports);
1080
James Smart858c9f62007-06-17 19:56:39 -05001081 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -04001082 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -05001083 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1084 return;
1085
1086 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -05001087
James Smart256ec0d2013-04-17 20:14:58 -04001088 if (time_after(phba->last_completion_time +
1089 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL),
1090 jiffies)) {
James Smart858c9f62007-06-17 19:56:39 -05001091 spin_unlock_irq(&phba->pport->work_port_lock);
1092 if (!phba->hb_outstanding)
1093 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001094 jiffies +
1095 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001096 else
1097 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001098 jiffies +
1099 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001100 return;
1101 }
1102 spin_unlock_irq(&phba->pport->work_port_lock);
1103
James Smart0ff10d42008-01-11 01:52:36 -05001104 if (phba->elsbuf_cnt &&
1105 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1106 spin_lock_irq(&phba->hbalock);
1107 list_splice_init(&phba->elsbuf, &completions);
1108 phba->elsbuf_cnt = 0;
1109 phba->elsbuf_prev_cnt = 0;
1110 spin_unlock_irq(&phba->hbalock);
1111
1112 while (!list_empty(&completions)) {
1113 list_remove_head(&completions, buf_ptr,
1114 struct lpfc_dmabuf, list);
1115 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1116 kfree(buf_ptr);
1117 }
1118 }
1119 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1120
James Smart858c9f62007-06-17 19:56:39 -05001121 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -05001122 if (phba->cfg_enable_hba_heartbeat) {
1123 if (!phba->hb_outstanding) {
James Smartbc739052010-08-04 16:11:18 -04001124 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1125 (list_empty(&psli->mboxq))) {
1126 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1127 GFP_KERNEL);
1128 if (!pmboxq) {
1129 mod_timer(&phba->hb_tmofunc,
1130 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001131 msecs_to_jiffies(1000 *
1132 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001133 return;
1134 }
James Smart13815c82008-01-11 01:52:48 -05001135
James Smartbc739052010-08-04 16:11:18 -04001136 lpfc_heart_beat(phba, pmboxq);
1137 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1138 pmboxq->vport = phba->pport;
1139 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1140 MBX_NOWAIT);
James Smart13815c82008-01-11 01:52:48 -05001141
James Smartbc739052010-08-04 16:11:18 -04001142 if (retval != MBX_BUSY &&
1143 retval != MBX_SUCCESS) {
1144 mempool_free(pmboxq,
1145 phba->mbox_mem_pool);
1146 mod_timer(&phba->hb_tmofunc,
1147 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001148 msecs_to_jiffies(1000 *
1149 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001150 return;
1151 }
1152 phba->skipped_hb = 0;
1153 phba->hb_outstanding = 1;
1154 } else if (time_before_eq(phba->last_completion_time,
1155 phba->skipped_hb)) {
1156 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1157 "2857 Last completion time not "
1158 " updated in %d ms\n",
1159 jiffies_to_msecs(jiffies
1160 - phba->last_completion_time));
1161 } else
1162 phba->skipped_hb = jiffies;
1163
James Smart858c9f62007-06-17 19:56:39 -05001164 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001165 jiffies +
1166 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001167 return;
James Smart13815c82008-01-11 01:52:48 -05001168 } else {
1169 /*
1170 * If heart beat timeout called with hb_outstanding set
James Smartdcf2a4e2010-09-29 11:18:53 -04001171 * we need to give the hb mailbox cmd a chance to
1172 * complete or TMO.
James Smart13815c82008-01-11 01:52:48 -05001173 */
James Smartdcf2a4e2010-09-29 11:18:53 -04001174 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1175 "0459 Adapter heartbeat still out"
1176 "standing:last compl time was %d ms.\n",
1177 jiffies_to_msecs(jiffies
1178 - phba->last_completion_time));
1179 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001180 jiffies +
1181 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001182 }
James Smart858c9f62007-06-17 19:56:39 -05001183 }
1184}
1185
James Smarte59058c2008-08-24 21:49:00 -04001186/**
James Smart3621a712009-04-06 18:47:14 -04001187 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -04001188 * @phba: pointer to lpfc hba data structure.
1189 *
1190 * This routine is called to bring the HBA offline when HBA hardware error
1191 * other than Port Error 6 has been detected.
1192 **/
James Smart09372822008-01-11 01:52:54 -05001193static void
1194lpfc_offline_eratt(struct lpfc_hba *phba)
1195{
1196 struct lpfc_sli *psli = &phba->sli;
1197
1198 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001199 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -05001200 spin_unlock_irq(&phba->hbalock);
James Smart618a5232012-06-12 13:54:36 -04001201 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart09372822008-01-11 01:52:54 -05001202
1203 lpfc_offline(phba);
1204 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001205 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001206 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001207 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001208 lpfc_hba_down_post(phba);
1209 lpfc_sli_brdready(phba, HS_MBRDY);
1210 lpfc_unblock_mgmt_io(phba);
1211 phba->link_state = LPFC_HBA_ERROR;
1212 return;
1213}
1214
James Smarte59058c2008-08-24 21:49:00 -04001215/**
James Smartda0436e2009-05-22 14:51:39 -04001216 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1217 * @phba: pointer to lpfc hba data structure.
1218 *
1219 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1220 * other than Port Error 6 has been detected.
1221 **/
James Smarta88dbb62013-04-17 20:18:39 -04001222void
James Smartda0436e2009-05-22 14:51:39 -04001223lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1224{
James Smart618a5232012-06-12 13:54:36 -04001225 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smartda0436e2009-05-22 14:51:39 -04001226 lpfc_offline(phba);
1227 lpfc_sli4_brdreset(phba);
1228 lpfc_hba_down_post(phba);
1229 lpfc_sli4_post_status_check(phba);
1230 lpfc_unblock_mgmt_io(phba);
1231 phba->link_state = LPFC_HBA_ERROR;
1232}
1233
1234/**
James Smarta257bf92009-04-06 18:48:10 -04001235 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1236 * @phba: pointer to lpfc hba data structure.
1237 *
1238 * This routine is invoked to handle the deferred HBA hardware error
1239 * conditions. This type of error is indicated by HBA by setting ER1
1240 * and another ER bit in the host status register. The driver will
1241 * wait until the ER1 bit clears before handling the error condition.
1242 **/
1243static void
1244lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1245{
1246 uint32_t old_host_status = phba->work_hs;
1247 struct lpfc_sli_ring *pring;
1248 struct lpfc_sli *psli = &phba->sli;
1249
James Smartf4b4c682009-05-22 14:53:12 -04001250 /* If the pci channel is offline, ignore possible errors,
1251 * since we cannot communicate with the pci card anyway.
1252 */
1253 if (pci_channel_offline(phba->pcidev)) {
1254 spin_lock_irq(&phba->hbalock);
1255 phba->hba_flag &= ~DEFER_ERATT;
1256 spin_unlock_irq(&phba->hbalock);
1257 return;
1258 }
1259
James Smarta257bf92009-04-06 18:48:10 -04001260 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1261 "0479 Deferred Adapter Hardware Error "
1262 "Data: x%x x%x x%x\n",
1263 phba->work_hs,
1264 phba->work_status[0], phba->work_status[1]);
1265
1266 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001267 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001268 spin_unlock_irq(&phba->hbalock);
1269
1270
1271 /*
1272 * Firmware stops when it triggred erratt. That could cause the I/Os
1273 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1274 * SCSI layer retry it after re-establishing link.
1275 */
1276 pring = &psli->ring[psli->fcp_ring];
1277 lpfc_sli_abort_iocb_ring(phba, pring);
1278
1279 /*
1280 * There was a firmware error. Take the hba offline and then
1281 * attempt to restart it.
1282 */
James Smart618a5232012-06-12 13:54:36 -04001283 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smarta257bf92009-04-06 18:48:10 -04001284 lpfc_offline(phba);
1285
1286 /* Wait for the ER1 bit to clear.*/
1287 while (phba->work_hs & HS_FFER1) {
1288 msleep(100);
James Smart9940b972011-03-11 16:06:12 -05001289 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1290 phba->work_hs = UNPLUG_ERR ;
1291 break;
1292 }
James Smarta257bf92009-04-06 18:48:10 -04001293 /* If driver is unloading let the worker thread continue */
1294 if (phba->pport->load_flag & FC_UNLOADING) {
1295 phba->work_hs = 0;
1296 break;
1297 }
1298 }
1299
1300 /*
1301 * This is to ptrotect against a race condition in which
1302 * first write to the host attention register clear the
1303 * host status register.
1304 */
1305 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1306 phba->work_hs = old_host_status & ~HS_FFER1;
1307
James Smart3772a992009-05-22 14:50:54 -04001308 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001309 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001310 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001311 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1312 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1313}
1314
James Smart3772a992009-05-22 14:50:54 -04001315static void
1316lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1317{
1318 struct lpfc_board_event_header board_event;
1319 struct Scsi_Host *shost;
1320
1321 board_event.event_type = FC_REG_BOARD_EVENT;
1322 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1323 shost = lpfc_shost_from_vport(phba->pport);
1324 fc_host_post_vendor_event(shost, fc_get_event_number(),
1325 sizeof(board_event),
1326 (char *) &board_event,
1327 LPFC_NL_VENDOR_ID);
1328}
1329
James Smarta257bf92009-04-06 18:48:10 -04001330/**
James Smart3772a992009-05-22 14:50:54 -04001331 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001332 * @phba: pointer to lpfc hba data structure.
1333 *
1334 * This routine is invoked to handle the following HBA hardware error
1335 * conditions:
1336 * 1 - HBA error attention interrupt
1337 * 2 - DMA ring index out of range
1338 * 3 - Mailbox command came back as unknown
1339 **/
James Smart3772a992009-05-22 14:50:54 -04001340static void
1341lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001342{
James Smart2e0fef82007-06-17 19:56:36 -05001343 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001344 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001345 struct lpfc_sli_ring *pring;
James Smartd2873e42006-08-18 17:46:43 -04001346 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001347 unsigned long temperature;
1348 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001349 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001350
Linas Vepstas8d63f372007-02-14 14:28:36 -06001351 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001352 * since we cannot communicate with the pci card anyway.
1353 */
1354 if (pci_channel_offline(phba->pcidev)) {
1355 spin_lock_irq(&phba->hbalock);
1356 phba->hba_flag &= ~DEFER_ERATT;
1357 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001358 return;
James Smart3772a992009-05-22 14:50:54 -04001359 }
1360
James Smart13815c82008-01-11 01:52:48 -05001361 /* If resets are disabled then leave the HBA alone and return */
1362 if (!phba->cfg_enable_hba_reset)
1363 return;
dea31012005-04-17 16:05:31 -05001364
James Smartea2151b2008-09-07 11:52:10 -04001365 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001366 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001367
James Smarta257bf92009-04-06 18:48:10 -04001368 if (phba->hba_flag & DEFER_ERATT)
1369 lpfc_handle_deferred_eratt(phba);
1370
James Smartdcf2a4e2010-09-29 11:18:53 -04001371 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1372 if (phba->work_hs & HS_FFER6)
1373 /* Re-establishing Link */
1374 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1375 "1301 Re-establishing Link "
1376 "Data: x%x x%x x%x\n",
1377 phba->work_hs, phba->work_status[0],
1378 phba->work_status[1]);
1379 if (phba->work_hs & HS_FFER8)
1380 /* Device Zeroization */
1381 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1382 "2861 Host Authentication device "
1383 "zeroization Data:x%x x%x x%x\n",
1384 phba->work_hs, phba->work_status[0],
1385 phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001386
James Smart92d7f7b2007-06-17 19:56:38 -05001387 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001388 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001389 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001390
1391 /*
1392 * Firmware stops when it triggled erratt with HS_FFER6.
1393 * That could cause the I/Os dropped by the firmware.
1394 * Error iocb (I/O) on txcmplq and let the SCSI layer
1395 * retry it after re-establishing link.
1396 */
1397 pring = &psli->ring[psli->fcp_ring];
1398 lpfc_sli_abort_iocb_ring(phba, pring);
1399
dea31012005-04-17 16:05:31 -05001400 /*
1401 * There was a firmware error. Take the hba offline and then
1402 * attempt to restart it.
1403 */
James Smart618a5232012-06-12 13:54:36 -04001404 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
dea31012005-04-17 16:05:31 -05001405 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001406 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001407 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001408 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001409 return;
1410 }
James Smart46fa3112007-04-25 09:51:45 -04001411 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001412 } else if (phba->work_hs & HS_CRIT_TEMP) {
1413 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1414 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1415 temp_event_data.event_code = LPFC_CRIT_TEMP;
1416 temp_event_data.data = (uint32_t)temperature;
1417
1418 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001419 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001420 "(%ld), taking this port offline "
1421 "Data: x%x x%x x%x\n",
1422 temperature, phba->work_hs,
1423 phba->work_status[0], phba->work_status[1]);
1424
1425 shost = lpfc_shost_from_vport(phba->pport);
1426 fc_host_post_vendor_event(shost, fc_get_event_number(),
1427 sizeof(temp_event_data),
1428 (char *) &temp_event_data,
1429 SCSI_NL_VID_TYPE_PCI
1430 | PCI_VENDOR_ID_EMULEX);
1431
James Smart7af67052007-10-27 13:38:11 -04001432 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001433 phba->over_temp_state = HBA_OVER_TEMP;
1434 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001435 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001436
dea31012005-04-17 16:05:31 -05001437 } else {
1438 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001439 * failure is a value other than FFER6. Do not call the offline
1440 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001441 */
1442 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001443 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001444 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001445 phba->work_hs,
dea31012005-04-17 16:05:31 -05001446 phba->work_status[0], phba->work_status[1]);
1447
James Smartd2873e42006-08-18 17:46:43 -04001448 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001449 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001450 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001451 sizeof(event_data), (char *) &event_data,
1452 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1453
James Smart09372822008-01-11 01:52:54 -05001454 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001455 }
James Smart93996272008-08-24 21:50:30 -04001456 return;
dea31012005-04-17 16:05:31 -05001457}
1458
James Smarte59058c2008-08-24 21:49:00 -04001459/**
James Smart618a5232012-06-12 13:54:36 -04001460 * lpfc_sli4_port_sta_fn_reset - The SLI4 function reset due to port status reg
1461 * @phba: pointer to lpfc hba data structure.
1462 * @mbx_action: flag for mailbox shutdown action.
1463 *
1464 * This routine is invoked to perform an SLI4 port PCI function reset in
1465 * response to port status register polling attention. It waits for port
1466 * status register (ERR, RDY, RN) bits before proceeding with function reset.
1467 * During this process, interrupt vectors are freed and later requested
1468 * for handling possible port resource change.
1469 **/
1470static int
1471lpfc_sli4_port_sta_fn_reset(struct lpfc_hba *phba, int mbx_action)
1472{
1473 int rc;
1474 uint32_t intr_mode;
1475
1476 /*
1477 * On error status condition, driver need to wait for port
1478 * ready before performing reset.
1479 */
1480 rc = lpfc_sli4_pdev_status_reg_wait(phba);
1481 if (!rc) {
1482 /* need reset: attempt for port recovery */
1483 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1484 "2887 Reset Needed: Attempting Port "
1485 "Recovery...\n");
1486 lpfc_offline_prep(phba, mbx_action);
1487 lpfc_offline(phba);
1488 /* release interrupt for possible resource change */
1489 lpfc_sli4_disable_intr(phba);
1490 lpfc_sli_brdrestart(phba);
1491 /* request and enable interrupt */
1492 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
1493 if (intr_mode == LPFC_INTR_ERROR) {
1494 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1495 "3175 Failed to enable interrupt\n");
1496 return -EIO;
1497 } else {
1498 phba->intr_mode = intr_mode;
1499 }
1500 rc = lpfc_online(phba);
1501 if (rc == 0)
1502 lpfc_unblock_mgmt_io(phba);
1503 }
1504 return rc;
1505}
1506
1507/**
James Smartda0436e2009-05-22 14:51:39 -04001508 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1509 * @phba: pointer to lpfc hba data structure.
1510 *
1511 * This routine is invoked to handle the SLI4 HBA hardware error attention
1512 * conditions.
1513 **/
1514static void
1515lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1516{
1517 struct lpfc_vport *vport = phba->pport;
1518 uint32_t event_data;
1519 struct Scsi_Host *shost;
James Smart2fcee4b2010-12-15 17:57:46 -05001520 uint32_t if_type;
James Smart2e90f4b2011-12-13 13:22:37 -05001521 struct lpfc_register portstat_reg = {0};
1522 uint32_t reg_err1, reg_err2;
1523 uint32_t uerrlo_reg, uemasklo_reg;
1524 uint32_t pci_rd_rc1, pci_rd_rc2;
James Smart73d91e52011-10-10 21:32:10 -04001525 int rc;
James Smartda0436e2009-05-22 14:51:39 -04001526
1527 /* If the pci channel is offline, ignore possible errors, since
1528 * we cannot communicate with the pci card anyway.
1529 */
1530 if (pci_channel_offline(phba->pcidev))
1531 return;
1532 /* If resets are disabled then leave the HBA alone and return */
1533 if (!phba->cfg_enable_hba_reset)
1534 return;
1535
James Smart2fcee4b2010-12-15 17:57:46 -05001536 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1537 switch (if_type) {
1538 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart2e90f4b2011-12-13 13:22:37 -05001539 pci_rd_rc1 = lpfc_readl(
1540 phba->sli4_hba.u.if_type0.UERRLOregaddr,
1541 &uerrlo_reg);
1542 pci_rd_rc2 = lpfc_readl(
1543 phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
1544 &uemasklo_reg);
1545 /* consider PCI bus read error as pci_channel_offline */
1546 if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
1547 return;
James Smart2fcee4b2010-12-15 17:57:46 -05001548 lpfc_sli4_offline_eratt(phba);
1549 break;
1550 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart2e90f4b2011-12-13 13:22:37 -05001551 pci_rd_rc1 = lpfc_readl(
1552 phba->sli4_hba.u.if_type2.STATUSregaddr,
1553 &portstat_reg.word0);
1554 /* consider PCI bus read error as pci_channel_offline */
James Smart6b5151f2012-01-18 16:24:06 -05001555 if (pci_rd_rc1 == -EIO) {
1556 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1557 "3151 PCI bus read access failure: x%x\n",
1558 readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
James Smart2e90f4b2011-12-13 13:22:37 -05001559 return;
James Smart6b5151f2012-01-18 16:24:06 -05001560 }
James Smart2e90f4b2011-12-13 13:22:37 -05001561 reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
1562 reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
James Smart2fcee4b2010-12-15 17:57:46 -05001563 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
1564 /* TODO: Register for Overtemp async events. */
1565 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1566 "2889 Port Overtemperature event, "
James Smart026abb82011-12-13 13:20:45 -05001567 "taking port offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001568 spin_lock_irq(&phba->hbalock);
1569 phba->over_temp_state = HBA_OVER_TEMP;
1570 spin_unlock_irq(&phba->hbalock);
1571 lpfc_sli4_offline_eratt(phba);
James Smart2e90f4b2011-12-13 13:22:37 -05001572 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001573 }
James Smart2e90f4b2011-12-13 13:22:37 -05001574 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1575 reg_err2 == SLIPORT_ERR2_REG_FW_RESTART)
1576 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1577 "3143 Port Down: Firmware Restarted\n");
1578 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1579 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1580 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1581 "3144 Port Down: Debug Dump\n");
1582 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1583 reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
1584 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1585 "3145 Port Down: Provisioning\n");
James Smart618a5232012-06-12 13:54:36 -04001586
1587 /* Check port status register for function reset */
1588 rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT);
1589 if (rc == 0) {
1590 /* don't report event on forced debug dump */
1591 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1592 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1593 return;
1594 else
1595 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001596 }
James Smart618a5232012-06-12 13:54:36 -04001597 /* fall through for not able to recover */
James Smart6b5151f2012-01-18 16:24:06 -05001598 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1599 "3152 Unrecoverable error, bring the port "
1600 "offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001601 lpfc_sli4_offline_eratt(phba);
1602 break;
1603 case LPFC_SLI_INTF_IF_TYPE_1:
1604 default:
1605 break;
1606 }
James Smart2e90f4b2011-12-13 13:22:37 -05001607 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1608 "3123 Report dump event to upper layer\n");
1609 /* Send an internal error event to mgmt application */
1610 lpfc_board_errevt_to_mgmt(phba);
1611
1612 event_data = FC_REG_DUMP_EVENT;
1613 shost = lpfc_shost_from_vport(vport);
1614 fc_host_post_vendor_event(shost, fc_get_event_number(),
1615 sizeof(event_data), (char *) &event_data,
1616 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
James Smartda0436e2009-05-22 14:51:39 -04001617}
1618
1619/**
1620 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1621 * @phba: pointer to lpfc HBA data structure.
1622 *
1623 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1624 * routine from the API jump table function pointer from the lpfc_hba struct.
1625 *
1626 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001627 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001628 * Any other value - error.
1629 **/
1630void
1631lpfc_handle_eratt(struct lpfc_hba *phba)
1632{
1633 (*phba->lpfc_handle_eratt)(phba);
1634}
1635
1636/**
James Smart3621a712009-04-06 18:47:14 -04001637 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04001638 * @phba: pointer to lpfc hba data structure.
1639 *
1640 * This routine is invoked from the worker thread to handle a HBA host
1641 * attention link event.
1642 **/
dea31012005-04-17 16:05:31 -05001643void
James Smart2e0fef82007-06-17 19:56:36 -05001644lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001645{
James Smart2e0fef82007-06-17 19:56:36 -05001646 struct lpfc_vport *vport = phba->pport;
1647 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001648 LPFC_MBOXQ_t *pmb;
1649 volatile uint32_t control;
1650 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05001651 int rc = 0;
dea31012005-04-17 16:05:31 -05001652
1653 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001654 if (!pmb) {
1655 rc = 1;
dea31012005-04-17 16:05:31 -05001656 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05001657 }
dea31012005-04-17 16:05:31 -05001658
1659 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001660 if (!mp) {
1661 rc = 2;
dea31012005-04-17 16:05:31 -05001662 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05001663 }
dea31012005-04-17 16:05:31 -05001664
1665 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05001666 if (!mp->virt) {
1667 rc = 3;
dea31012005-04-17 16:05:31 -05001668 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05001669 }
dea31012005-04-17 16:05:31 -05001670
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05001671 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04001672 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05001673
1674 psli->slistat.link_event++;
James Smart76a95d72010-11-20 23:11:48 -05001675 lpfc_read_topology(phba, pmb, mp);
1676 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smart2e0fef82007-06-17 19:56:36 -05001677 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04001678 /* Block ELS IOCBs until we have processed this mbox command */
1679 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04001680 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05001681 if (rc == MBX_NOT_FINISHED) {
1682 rc = 4;
James Smart14691152006-12-02 13:34:28 -05001683 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05001684 }
dea31012005-04-17 16:05:31 -05001685
1686 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05001687 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001688 writel(HA_LATT, phba->HAregaddr);
1689 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001690 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001691
1692 return;
1693
James Smart14691152006-12-02 13:34:28 -05001694lpfc_handle_latt_free_mbuf:
James Smart0d2b6b82008-06-14 22:52:47 -04001695 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05001696 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001697lpfc_handle_latt_free_mp:
1698 kfree(mp);
1699lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04001700 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001701lpfc_handle_latt_err_exit:
1702 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001703 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001704 psli->sli_flag |= LPFC_PROCESS_LA;
1705 control = readl(phba->HCregaddr);
1706 control |= HC_LAINT_ENA;
1707 writel(control, phba->HCregaddr);
1708 readl(phba->HCregaddr); /* flush */
1709
1710 /* Clear Link Attention in HA REG */
1711 writel(HA_LATT, phba->HAregaddr);
1712 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001713 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001714 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001715 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001716
James Smart09372822008-01-11 01:52:54 -05001717 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1718 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05001719
1720 return;
1721}
1722
James Smarte59058c2008-08-24 21:49:00 -04001723/**
James Smart3621a712009-04-06 18:47:14 -04001724 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04001725 * @phba: pointer to lpfc hba data structure.
1726 * @vpd: pointer to the vital product data.
1727 * @len: length of the vital product data in bytes.
1728 *
1729 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1730 * an array of characters. In this routine, the ModelName, ProgramType, and
1731 * ModelDesc, etc. fields of the phba data structure will be populated.
1732 *
1733 * Return codes
1734 * 0 - pointer to the VPD passed in is NULL
1735 * 1 - success
1736 **/
James Smart3772a992009-05-22 14:50:54 -04001737int
James Smart2e0fef82007-06-17 19:56:36 -05001738lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05001739{
1740 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05001741 int Length;
dea31012005-04-17 16:05:31 -05001742 int i, j;
1743 int finished = 0;
1744 int index = 0;
1745
1746 if (!vpd)
1747 return 0;
1748
1749 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05001750 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001751 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05001752 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1753 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001754 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05001755 switch (vpd[index]) {
1756 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001757 case 0x91:
dea31012005-04-17 16:05:31 -05001758 index += 1;
1759 lenlo = vpd[index];
1760 index += 1;
1761 lenhi = vpd[index];
1762 index += 1;
1763 i = ((((unsigned short)lenhi) << 8) + lenlo);
1764 index += i;
1765 break;
1766 case 0x90:
1767 index += 1;
1768 lenlo = vpd[index];
1769 index += 1;
1770 lenhi = vpd[index];
1771 index += 1;
1772 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001773 if (Length > len - index)
1774 Length = len - index;
dea31012005-04-17 16:05:31 -05001775 while (Length > 0) {
1776 /* Look for Serial Number */
1777 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1778 index += 2;
1779 i = vpd[index];
1780 index += 1;
1781 j = 0;
1782 Length -= (3+i);
1783 while(i--) {
1784 phba->SerialNumber[j++] = vpd[index++];
1785 if (j == 31)
1786 break;
1787 }
1788 phba->SerialNumber[j] = 0;
1789 continue;
1790 }
1791 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1792 phba->vpd_flag |= VPD_MODEL_DESC;
1793 index += 2;
1794 i = vpd[index];
1795 index += 1;
1796 j = 0;
1797 Length -= (3+i);
1798 while(i--) {
1799 phba->ModelDesc[j++] = vpd[index++];
1800 if (j == 255)
1801 break;
1802 }
1803 phba->ModelDesc[j] = 0;
1804 continue;
1805 }
1806 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1807 phba->vpd_flag |= VPD_MODEL_NAME;
1808 index += 2;
1809 i = vpd[index];
1810 index += 1;
1811 j = 0;
1812 Length -= (3+i);
1813 while(i--) {
1814 phba->ModelName[j++] = vpd[index++];
1815 if (j == 79)
1816 break;
1817 }
1818 phba->ModelName[j] = 0;
1819 continue;
1820 }
1821 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1822 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1823 index += 2;
1824 i = vpd[index];
1825 index += 1;
1826 j = 0;
1827 Length -= (3+i);
1828 while(i--) {
1829 phba->ProgramType[j++] = vpd[index++];
1830 if (j == 255)
1831 break;
1832 }
1833 phba->ProgramType[j] = 0;
1834 continue;
1835 }
1836 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1837 phba->vpd_flag |= VPD_PORT;
1838 index += 2;
1839 i = vpd[index];
1840 index += 1;
1841 j = 0;
1842 Length -= (3+i);
1843 while(i--) {
James Smartcd1c8302011-10-10 21:33:25 -04001844 if ((phba->sli_rev == LPFC_SLI_REV4) &&
1845 (phba->sli4_hba.pport_name_sta ==
1846 LPFC_SLI4_PPNAME_GET)) {
1847 j++;
1848 index++;
1849 } else
1850 phba->Port[j++] = vpd[index++];
1851 if (j == 19)
1852 break;
dea31012005-04-17 16:05:31 -05001853 }
James Smartcd1c8302011-10-10 21:33:25 -04001854 if ((phba->sli_rev != LPFC_SLI_REV4) ||
1855 (phba->sli4_hba.pport_name_sta ==
1856 LPFC_SLI4_PPNAME_NON))
1857 phba->Port[j] = 0;
dea31012005-04-17 16:05:31 -05001858 continue;
1859 }
1860 else {
1861 index += 2;
1862 i = vpd[index];
1863 index += 1;
1864 index += i;
1865 Length -= (3 + i);
1866 }
1867 }
1868 finished = 0;
1869 break;
1870 case 0x78:
1871 finished = 1;
1872 break;
1873 default:
1874 index ++;
1875 break;
1876 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001877 }
dea31012005-04-17 16:05:31 -05001878
1879 return(1);
1880}
1881
James Smarte59058c2008-08-24 21:49:00 -04001882/**
James Smart3621a712009-04-06 18:47:14 -04001883 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04001884 * @phba: pointer to lpfc hba data structure.
1885 * @mdp: pointer to the data structure to hold the derived model name.
1886 * @descp: pointer to the data structure to hold the derived description.
1887 *
1888 * This routine retrieves HBA's description based on its registered PCI device
1889 * ID. The @descp passed into this function points to an array of 256 chars. It
1890 * shall be returned with the model name, maximum speed, and the host bus type.
1891 * The @mdp passed into this function points to an array of 80 chars. When the
1892 * function returns, the @mdp will be filled with the model name.
1893 **/
dea31012005-04-17 16:05:31 -05001894static void
James Smart2e0fef82007-06-17 19:56:36 -05001895lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05001896{
1897 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05001898 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001899 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04001900 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04001901 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001902 struct {
James Smarta747c9c2009-11-18 15:41:10 -05001903 char *name;
1904 char *bus;
1905 char *function;
1906 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001907
1908 if (mdp && mdp[0] != '\0'
1909 && descp && descp[0] != '\0')
1910 return;
1911
James Smartc0c11512011-05-24 11:41:34 -04001912 if (phba->lmt & LMT_16Gb)
1913 max_speed = 16;
1914 else if (phba->lmt & LMT_10Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001915 max_speed = 10;
1916 else if (phba->lmt & LMT_8Gb)
1917 max_speed = 8;
1918 else if (phba->lmt & LMT_4Gb)
1919 max_speed = 4;
1920 else if (phba->lmt & LMT_2Gb)
1921 max_speed = 2;
James Smart4169d862012-09-29 11:32:09 -04001922 else if (phba->lmt & LMT_1Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001923 max_speed = 1;
James Smart4169d862012-09-29 11:32:09 -04001924 else
1925 max_speed = 0;
dea31012005-04-17 16:05:31 -05001926
1927 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05001928
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001929 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001930 case PCI_DEVICE_ID_FIREFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001931 m = (typeof(m)){"LP6000", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001932 break;
dea31012005-04-17 16:05:31 -05001933 case PCI_DEVICE_ID_SUPERFLY:
1934 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smarta747c9c2009-11-18 15:41:10 -05001935 m = (typeof(m)){"LP7000", "PCI",
1936 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001937 else
James Smarta747c9c2009-11-18 15:41:10 -05001938 m = (typeof(m)){"LP7000E", "PCI",
1939 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001940 break;
1941 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001942 m = (typeof(m)){"LP8000", "PCI",
1943 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001944 break;
1945 case PCI_DEVICE_ID_CENTAUR:
1946 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smarta747c9c2009-11-18 15:41:10 -05001947 m = (typeof(m)){"LP9002", "PCI",
1948 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001949 else
James Smarta747c9c2009-11-18 15:41:10 -05001950 m = (typeof(m)){"LP9000", "PCI",
1951 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001952 break;
1953 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001954 m = (typeof(m)){"LP952", "PCI",
1955 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001956 break;
1957 case PCI_DEVICE_ID_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05001958 m = (typeof(m)){"LP9802", "PCI-X",
1959 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001960 break;
1961 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05001962 m = (typeof(m)){"LP10000", "PCI-X",
1963 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001964 break;
1965 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05001966 m = (typeof(m)){"LPX1000", "PCI-X",
1967 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001968 break;
1969 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001970 m = (typeof(m)){"LP982", "PCI-X",
1971 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001972 break;
1973 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001974 m = (typeof(m)){"LP1050", "PCI-X",
1975 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001976 break;
1977 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05001978 m = (typeof(m)){"LP11000", "PCI-X2",
1979 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001980 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001981 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001982 m = (typeof(m)){"LP11000-SP", "PCI-X2",
1983 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001984 break;
1985 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001986 m = (typeof(m)){"LP11002-SP", "PCI-X2",
1987 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001988 break;
1989 case PCI_DEVICE_ID_NEPTUNE:
James Smarta747c9c2009-11-18 15:41:10 -05001990 m = (typeof(m)){"LPe1000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001991 break;
1992 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001993 m = (typeof(m)){"LPe1000-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001994 break;
1995 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001996 m = (typeof(m)){"LPe1002-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001997 break;
dea31012005-04-17 16:05:31 -05001998 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05001999 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002000 break;
2001 case PCI_DEVICE_ID_BSMB:
James Smarta747c9c2009-11-18 15:41:10 -05002002 m = (typeof(m)){"LP111", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002003 break;
2004 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05002005 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002006 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002007 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002008 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002009 break;
2010 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002011 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04002012 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002013 break;
dea31012005-04-17 16:05:31 -05002014 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05002015 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002016 break;
2017 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05002018 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002019 break;
2020 case PCI_DEVICE_ID_LP101:
James Smarta747c9c2009-11-18 15:41:10 -05002021 m = (typeof(m)){"LP101", "PCI-X", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002022 break;
2023 case PCI_DEVICE_ID_LP10000S:
James Smarta747c9c2009-11-18 15:41:10 -05002024 m = (typeof(m)){"LP10000-S", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002025 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002026 case PCI_DEVICE_ID_LP11000S:
James Smarta747c9c2009-11-18 15:41:10 -05002027 m = (typeof(m)){"LP11000-S", "PCI-X2", "Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05002028 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002029 case PCI_DEVICE_ID_LPE11000S:
James Smarta747c9c2009-11-18 15:41:10 -05002030 m = (typeof(m)){"LPe11000-S", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002031 break;
James Smartb87eab32007-04-25 09:53:28 -04002032 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05002033 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002034 break;
2035 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05002036 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002037 break;
2038 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05002039 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002040 break;
2041 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002042 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002043 break;
2044 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002045 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002046 break;
2047 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05002048 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002049 break;
James Smart84774a42008-08-24 21:50:06 -04002050 case PCI_DEVICE_ID_HORNET:
James Smarta747c9c2009-11-18 15:41:10 -05002051 m = (typeof(m)){"LP21000", "PCIe", "FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002052 GE = 1;
2053 break;
2054 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05002055 m = (typeof(m)){"LPev12000", "PCIe IOV",
2056 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002057 break;
2058 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05002059 m = (typeof(m)){"LPev12000", "PCIe IOV",
2060 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002061 break;
2062 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05002063 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
2064 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002065 break;
James Smartda0436e2009-05-22 14:51:39 -04002066 case PCI_DEVICE_ID_TIGERSHARK:
2067 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002068 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04002069 break;
James Smarta747c9c2009-11-18 15:41:10 -05002070 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04002071 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002072 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2073 break;
2074 case PCI_DEVICE_ID_FALCON:
2075 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2076 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04002077 break;
James Smart98fc5dd2010-06-07 15:24:29 -04002078 case PCI_DEVICE_ID_BALIUS:
2079 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
2080 "Fibre Channel Adapter"};
2081 break;
James Smart085c6472010-11-20 23:11:37 -05002082 case PCI_DEVICE_ID_LANCER_FC:
James Smartc0c11512011-05-24 11:41:34 -04002083 case PCI_DEVICE_ID_LANCER_FC_VF:
2084 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
James Smart085c6472010-11-20 23:11:37 -05002085 break;
2086 case PCI_DEVICE_ID_LANCER_FCOE:
James Smartc0c11512011-05-24 11:41:34 -04002087 case PCI_DEVICE_ID_LANCER_FCOE_VF:
James Smart085c6472010-11-20 23:11:37 -05002088 oneConnect = 1;
James Smart079b5c92011-08-21 21:48:49 -04002089 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
James Smart085c6472010-11-20 23:11:37 -05002090 break;
James Smartf8cafd32012-08-03 12:42:51 -04002091 case PCI_DEVICE_ID_SKYHAWK:
2092 case PCI_DEVICE_ID_SKYHAWK_VF:
2093 oneConnect = 1;
2094 m = (typeof(m)){"OCe14000", "PCIe", "FCoE"};
2095 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002096 default:
James Smarta747c9c2009-11-18 15:41:10 -05002097 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002098 break;
dea31012005-04-17 16:05:31 -05002099 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002100
2101 if (mdp && mdp[0] == '\0')
2102 snprintf(mdp, 79,"%s", m.name);
James Smartc0c11512011-05-24 11:41:34 -04002103 /*
2104 * oneConnect hba requires special processing, they are all initiators
James Smartda0436e2009-05-22 14:51:39 -04002105 * and we put the port number on the end
2106 */
2107 if (descp && descp[0] == '\0') {
2108 if (oneConnect)
2109 snprintf(descp, 255,
James Smart4169d862012-09-29 11:32:09 -04002110 "Emulex OneConnect %s, %s Initiator %s",
James Smarta747c9c2009-11-18 15:41:10 -05002111 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04002112 phba->Port);
James Smart4169d862012-09-29 11:32:09 -04002113 else if (max_speed == 0)
2114 snprintf(descp, 255,
2115 "Emulex %s %s %s ",
2116 m.name, m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002117 else
2118 snprintf(descp, 255,
2119 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05002120 m.name, max_speed, (GE) ? "GE" : "Gb",
2121 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002122 }
dea31012005-04-17 16:05:31 -05002123}
2124
James Smarte59058c2008-08-24 21:49:00 -04002125/**
James Smart3621a712009-04-06 18:47:14 -04002126 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04002127 * @phba: pointer to lpfc hba data structure.
2128 * @pring: pointer to a IOCB ring.
2129 * @cnt: the number of IOCBs to be posted to the IOCB ring.
2130 *
2131 * This routine posts a given number of IOCBs with the associated DMA buffer
2132 * descriptors specified by the cnt argument to the given IOCB ring.
2133 *
2134 * Return codes
2135 * The number of IOCBs NOT able to be posted to the IOCB ring.
2136 **/
dea31012005-04-17 16:05:31 -05002137int
James Smart495a7142008-06-14 22:52:59 -04002138lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05002139{
2140 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002141 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05002142 struct lpfc_dmabuf *mp1, *mp2;
2143
2144 cnt += pring->missbufcnt;
2145
2146 /* While there are buffers to post */
2147 while (cnt > 0) {
2148 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002149 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002150 if (iocb == NULL) {
2151 pring->missbufcnt = cnt;
2152 return cnt;
2153 }
dea31012005-04-17 16:05:31 -05002154 icmd = &iocb->iocb;
2155
2156 /* 2 buffers can be posted per command */
2157 /* Allocate buffer to post */
2158 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2159 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04002160 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2161 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002162 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002163 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002164 pring->missbufcnt = cnt;
2165 return cnt;
2166 }
2167
2168 INIT_LIST_HEAD(&mp1->list);
2169 /* Allocate buffer to post */
2170 if (cnt > 1) {
2171 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2172 if (mp2)
2173 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2174 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04002175 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002176 kfree(mp2);
dea31012005-04-17 16:05:31 -05002177 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2178 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002179 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002180 pring->missbufcnt = cnt;
2181 return cnt;
2182 }
2183
2184 INIT_LIST_HEAD(&mp2->list);
2185 } else {
2186 mp2 = NULL;
2187 }
2188
2189 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2190 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2191 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2192 icmd->ulpBdeCount = 1;
2193 cnt--;
2194 if (mp2) {
2195 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2196 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2197 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2198 cnt--;
2199 icmd->ulpBdeCount = 2;
2200 }
2201
2202 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2203 icmd->ulpLe = 1;
2204
James Smart3772a992009-05-22 14:50:54 -04002205 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2206 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05002207 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2208 kfree(mp1);
2209 cnt++;
2210 if (mp2) {
2211 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2212 kfree(mp2);
2213 cnt++;
2214 }
James Bottomley604a3e32005-10-29 10:28:33 -05002215 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002216 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05002217 return cnt;
2218 }
dea31012005-04-17 16:05:31 -05002219 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05002220 if (mp2)
dea31012005-04-17 16:05:31 -05002221 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05002222 }
2223 pring->missbufcnt = 0;
2224 return 0;
2225}
2226
James Smarte59058c2008-08-24 21:49:00 -04002227/**
James Smart3621a712009-04-06 18:47:14 -04002228 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04002229 * @phba: pointer to lpfc hba data structure.
2230 *
2231 * This routine posts initial receive IOCB buffers to the ELS ring. The
2232 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
2233 * set to 64 IOCBs.
2234 *
2235 * Return codes
2236 * 0 - success (currently always success)
2237 **/
dea31012005-04-17 16:05:31 -05002238static int
James Smart2e0fef82007-06-17 19:56:36 -05002239lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002240{
2241 struct lpfc_sli *psli = &phba->sli;
2242
2243 /* Ring 0, ELS / CT buffers */
James Smart495a7142008-06-14 22:52:59 -04002244 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05002245 /* Ring 2 - FCP no buffers needed */
2246
2247 return 0;
2248}
2249
2250#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2251
James Smarte59058c2008-08-24 21:49:00 -04002252/**
James Smart3621a712009-04-06 18:47:14 -04002253 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04002254 * @HashResultPointer: pointer to an array as hash table.
2255 *
2256 * This routine sets up the initial values to the array of hash table entries
2257 * for the LC HBAs.
2258 **/
dea31012005-04-17 16:05:31 -05002259static void
2260lpfc_sha_init(uint32_t * HashResultPointer)
2261{
2262 HashResultPointer[0] = 0x67452301;
2263 HashResultPointer[1] = 0xEFCDAB89;
2264 HashResultPointer[2] = 0x98BADCFE;
2265 HashResultPointer[3] = 0x10325476;
2266 HashResultPointer[4] = 0xC3D2E1F0;
2267}
2268
James Smarte59058c2008-08-24 21:49:00 -04002269/**
James Smart3621a712009-04-06 18:47:14 -04002270 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04002271 * @HashResultPointer: pointer to an initial/result hash table.
2272 * @HashWorkingPointer: pointer to an working hash table.
2273 *
2274 * This routine iterates an initial hash table pointed by @HashResultPointer
2275 * with the values from the working hash table pointeed by @HashWorkingPointer.
2276 * The results are putting back to the initial hash table, returned through
2277 * the @HashResultPointer as the result hash table.
2278 **/
dea31012005-04-17 16:05:31 -05002279static void
2280lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2281{
2282 int t;
2283 uint32_t TEMP;
2284 uint32_t A, B, C, D, E;
2285 t = 16;
2286 do {
2287 HashWorkingPointer[t] =
2288 S(1,
2289 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2290 8] ^
2291 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2292 } while (++t <= 79);
2293 t = 0;
2294 A = HashResultPointer[0];
2295 B = HashResultPointer[1];
2296 C = HashResultPointer[2];
2297 D = HashResultPointer[3];
2298 E = HashResultPointer[4];
2299
2300 do {
2301 if (t < 20) {
2302 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2303 } else if (t < 40) {
2304 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2305 } else if (t < 60) {
2306 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2307 } else {
2308 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2309 }
2310 TEMP += S(5, A) + E + HashWorkingPointer[t];
2311 E = D;
2312 D = C;
2313 C = S(30, B);
2314 B = A;
2315 A = TEMP;
2316 } while (++t <= 79);
2317
2318 HashResultPointer[0] += A;
2319 HashResultPointer[1] += B;
2320 HashResultPointer[2] += C;
2321 HashResultPointer[3] += D;
2322 HashResultPointer[4] += E;
2323
2324}
2325
James Smarte59058c2008-08-24 21:49:00 -04002326/**
James Smart3621a712009-04-06 18:47:14 -04002327 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002328 * @RandomChallenge: pointer to the entry of host challenge random number array.
2329 * @HashWorking: pointer to the entry of the working hash array.
2330 *
2331 * This routine calculates the working hash array referred by @HashWorking
2332 * from the challenge random numbers associated with the host, referred by
2333 * @RandomChallenge. The result is put into the entry of the working hash
2334 * array and returned by reference through @HashWorking.
2335 **/
dea31012005-04-17 16:05:31 -05002336static void
2337lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2338{
2339 *HashWorking = (*RandomChallenge ^ *HashWorking);
2340}
2341
James Smarte59058c2008-08-24 21:49:00 -04002342/**
James Smart3621a712009-04-06 18:47:14 -04002343 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002344 * @phba: pointer to lpfc hba data structure.
2345 * @hbainit: pointer to an array of unsigned 32-bit integers.
2346 *
2347 * This routine performs the special handling for LC HBA initialization.
2348 **/
dea31012005-04-17 16:05:31 -05002349void
2350lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2351{
2352 int t;
2353 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002354 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002355
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002356 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002357 if (!HashWorking)
2358 return;
2359
dea31012005-04-17 16:05:31 -05002360 HashWorking[0] = HashWorking[78] = *pwwnn++;
2361 HashWorking[1] = HashWorking[79] = *pwwnn;
2362
2363 for (t = 0; t < 7; t++)
2364 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2365
2366 lpfc_sha_init(hbainit);
2367 lpfc_sha_iterate(hbainit, HashWorking);
2368 kfree(HashWorking);
2369}
2370
James Smarte59058c2008-08-24 21:49:00 -04002371/**
James Smart3621a712009-04-06 18:47:14 -04002372 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002373 * @vport: pointer to a virtual N_Port data structure.
2374 *
2375 * This routine performs the necessary cleanups before deleting the @vport.
2376 * It invokes the discovery state machine to perform necessary state
2377 * transitions and to release the ndlps associated with the @vport. Note,
2378 * the physical port is treated as @vport 0.
2379 **/
James Smart87af33f2007-10-27 13:37:43 -04002380void
James Smart2e0fef82007-06-17 19:56:36 -05002381lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002382{
James Smart87af33f2007-10-27 13:37:43 -04002383 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002384 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002385 int i = 0;
dea31012005-04-17 16:05:31 -05002386
James Smart87af33f2007-10-27 13:37:43 -04002387 if (phba->link_state > LPFC_LINK_DOWN)
2388 lpfc_port_link_failure(vport);
2389
2390 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002391 if (!NLP_CHK_NODE_ACT(ndlp)) {
2392 ndlp = lpfc_enable_node(vport, ndlp,
2393 NLP_STE_UNUSED_NODE);
2394 if (!ndlp)
2395 continue;
2396 spin_lock_irq(&phba->ndlp_lock);
2397 NLP_SET_FREE_REQ(ndlp);
2398 spin_unlock_irq(&phba->ndlp_lock);
2399 /* Trigger the release of the ndlp memory */
2400 lpfc_nlp_put(ndlp);
2401 continue;
2402 }
2403 spin_lock_irq(&phba->ndlp_lock);
2404 if (NLP_CHK_FREE_REQ(ndlp)) {
2405 /* The ndlp should not be in memory free mode already */
2406 spin_unlock_irq(&phba->ndlp_lock);
2407 continue;
2408 } else
2409 /* Indicate request for freeing ndlp memory */
2410 NLP_SET_FREE_REQ(ndlp);
2411 spin_unlock_irq(&phba->ndlp_lock);
2412
James Smart58da1ff2008-04-07 10:15:56 -04002413 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2414 ndlp->nlp_DID == Fabric_DID) {
2415 /* Just free up ndlp with Fabric_DID for vports */
2416 lpfc_nlp_put(ndlp);
2417 continue;
2418 }
2419
James Smarteff4a012012-01-18 16:25:25 -05002420 /* take care of nodes in unused state before the state
2421 * machine taking action.
2422 */
2423 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
2424 lpfc_nlp_put(ndlp);
2425 continue;
2426 }
2427
James Smart87af33f2007-10-27 13:37:43 -04002428 if (ndlp->nlp_type & NLP_FABRIC)
2429 lpfc_disc_state_machine(vport, ndlp, NULL,
2430 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002431
James Smart87af33f2007-10-27 13:37:43 -04002432 lpfc_disc_state_machine(vport, ndlp, NULL,
2433 NLP_EVT_DEVICE_RM);
2434 }
2435
James Smarta8adb832007-10-27 13:37:53 -04002436 /* At this point, ALL ndlp's should be gone
2437 * because of the previous NLP_EVT_DEVICE_RM.
2438 * Lets wait for this to happen, if needed.
2439 */
James Smart87af33f2007-10-27 13:37:43 -04002440 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002441 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002442 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002443 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002444 list_for_each_entry_safe(ndlp, next_ndlp,
2445 &vport->fc_nodes, nlp_listp) {
2446 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2447 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002448 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002449 "usgmap:x%x refcnt:%d\n",
2450 ndlp->nlp_DID, (void *)ndlp,
2451 ndlp->nlp_usg_map,
2452 atomic_read(
2453 &ndlp->kref.refcount));
2454 }
James Smarta8adb832007-10-27 13:37:53 -04002455 break;
James Smart87af33f2007-10-27 13:37:43 -04002456 }
James Smarta8adb832007-10-27 13:37:53 -04002457
2458 /* Wait for any activity on ndlps to settle */
2459 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002460 }
James Smart1151e3e2011-02-16 12:39:35 -05002461 lpfc_cleanup_vports_rrqs(vport, NULL);
dea31012005-04-17 16:05:31 -05002462}
2463
James Smarte59058c2008-08-24 21:49:00 -04002464/**
James Smart3621a712009-04-06 18:47:14 -04002465 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002466 * @vport: pointer to a virtual N_Port data structure.
2467 *
2468 * This routine stops all the timers associated with a @vport. This function
2469 * is invoked before disabling or deleting a @vport. Note that the physical
2470 * port is treated as @vport 0.
2471 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002472void
2473lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002474{
James Smart92d7f7b2007-06-17 19:56:38 -05002475 del_timer_sync(&vport->els_tmofunc);
2476 del_timer_sync(&vport->fc_fdmitmo);
James Smart92494142011-02-16 12:39:44 -05002477 del_timer_sync(&vport->delayed_disc_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002478 lpfc_can_disctmo(vport);
2479 return;
dea31012005-04-17 16:05:31 -05002480}
2481
James Smarte59058c2008-08-24 21:49:00 -04002482/**
James Smartecfd03c2010-02-12 14:41:27 -05002483 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2484 * @phba: pointer to lpfc hba data structure.
2485 *
2486 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2487 * caller of this routine should already hold the host lock.
2488 **/
2489void
2490__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2491{
James Smart5ac6b302010-10-22 11:05:36 -04002492 /* Clear pending FCF rediscovery wait flag */
2493 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2494
James Smartecfd03c2010-02-12 14:41:27 -05002495 /* Now, try to stop the timer */
2496 del_timer(&phba->fcf.redisc_wait);
2497}
2498
2499/**
2500 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2501 * @phba: pointer to lpfc hba data structure.
2502 *
2503 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2504 * checks whether the FCF rediscovery wait timer is pending with the host
2505 * lock held before proceeding with disabling the timer and clearing the
2506 * wait timer pendig flag.
2507 **/
2508void
2509lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2510{
2511 spin_lock_irq(&phba->hbalock);
2512 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2513 /* FCF rediscovery timer already fired or stopped */
2514 spin_unlock_irq(&phba->hbalock);
2515 return;
2516 }
2517 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart5ac6b302010-10-22 11:05:36 -04002518 /* Clear failover in progress flags */
2519 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05002520 spin_unlock_irq(&phba->hbalock);
2521}
2522
2523/**
James Smart3772a992009-05-22 14:50:54 -04002524 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002525 * @phba: pointer to lpfc hba data structure.
2526 *
2527 * This routine stops all the timers associated with a HBA. This function is
2528 * invoked before either putting a HBA offline or unloading the driver.
2529 **/
James Smart3772a992009-05-22 14:50:54 -04002530void
2531lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002532{
James Smart51ef4c22007-08-02 11:10:31 -04002533 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05002534 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002535 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002536 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002537 del_timer_sync(&phba->hb_tmofunc);
James Smart1151e3e2011-02-16 12:39:35 -05002538 if (phba->sli_rev == LPFC_SLI_REV4) {
2539 del_timer_sync(&phba->rrq_tmr);
2540 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2541 }
James Smart3772a992009-05-22 14:50:54 -04002542 phba->hb_outstanding = 0;
2543
2544 switch (phba->pci_dev_grp) {
2545 case LPFC_PCI_DEV_LP:
2546 /* Stop any LightPulse device specific driver timers */
2547 del_timer_sync(&phba->fcp_poll_timer);
2548 break;
2549 case LPFC_PCI_DEV_OC:
2550 /* Stop any OneConnect device sepcific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05002551 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04002552 break;
2553 default:
2554 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2555 "0297 Invalid device group (x%x)\n",
2556 phba->pci_dev_grp);
2557 break;
2558 }
James Smart2e0fef82007-06-17 19:56:36 -05002559 return;
dea31012005-04-17 16:05:31 -05002560}
2561
James Smarte59058c2008-08-24 21:49:00 -04002562/**
James Smart3621a712009-04-06 18:47:14 -04002563 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002564 * @phba: pointer to lpfc hba data structure.
2565 *
2566 * This routine marks a HBA's management interface as blocked. Once the HBA's
2567 * management interface is marked as blocked, all the user space access to
2568 * the HBA, whether they are from sysfs interface or libdfc interface will
2569 * all be blocked. The HBA is set to block the management interface when the
2570 * driver prepares the HBA interface for online or offline.
2571 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002572static void
James Smart618a5232012-06-12 13:54:36 -04002573lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
Adrian Bunka6ababd2007-11-05 18:07:33 +01002574{
2575 unsigned long iflag;
James Smart6e7288d2010-06-07 15:23:35 -04002576 uint8_t actcmd = MBX_HEARTBEAT;
2577 unsigned long timeout;
2578
Adrian Bunka6ababd2007-11-05 18:07:33 +01002579 spin_lock_irqsave(&phba->hbalock, iflag);
2580 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
James Smart618a5232012-06-12 13:54:36 -04002581 spin_unlock_irqrestore(&phba->hbalock, iflag);
2582 if (mbx_action == LPFC_MBX_NO_WAIT)
2583 return;
2584 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
2585 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002586 if (phba->sli.mbox_active) {
James Smart6e7288d2010-06-07 15:23:35 -04002587 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
James Smarta183a152011-10-10 21:32:43 -04002588 /* Determine how long we might wait for the active mailbox
2589 * command to be gracefully completed by firmware.
2590 */
2591 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
2592 phba->sli.mbox_active) * 1000) + jiffies;
2593 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002594 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002595
James Smart6e7288d2010-06-07 15:23:35 -04002596 /* Wait for the outstnading mailbox command to complete */
2597 while (phba->sli.mbox_active) {
2598 /* Check active mailbox complete status every 2ms */
2599 msleep(2);
2600 if (time_after(jiffies, timeout)) {
2601 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2602 "2813 Mgmt IO is Blocked %x "
2603 "- mbox cmd %x still active\n",
2604 phba->sli.sli_flag, actcmd);
2605 break;
2606 }
2607 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002608}
2609
James Smarte59058c2008-08-24 21:49:00 -04002610/**
James Smart6b5151f2012-01-18 16:24:06 -05002611 * lpfc_sli4_node_prep - Assign RPIs for active nodes.
2612 * @phba: pointer to lpfc hba data structure.
2613 *
2614 * Allocate RPIs for all active remote nodes. This is needed whenever
2615 * an SLI4 adapter is reset and the driver is not unloading. Its purpose
2616 * is to fixup the temporary rpi assignments.
2617 **/
2618void
2619lpfc_sli4_node_prep(struct lpfc_hba *phba)
2620{
2621 struct lpfc_nodelist *ndlp, *next_ndlp;
2622 struct lpfc_vport **vports;
2623 int i;
2624
2625 if (phba->sli_rev != LPFC_SLI_REV4)
2626 return;
2627
2628 vports = lpfc_create_vport_work_array(phba);
2629 if (vports != NULL) {
2630 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2631 if (vports[i]->load_flag & FC_UNLOADING)
2632 continue;
2633
2634 list_for_each_entry_safe(ndlp, next_ndlp,
2635 &vports[i]->fc_nodes,
2636 nlp_listp) {
2637 if (NLP_CHK_NODE_ACT(ndlp))
2638 ndlp->nlp_rpi =
2639 lpfc_sli4_alloc_rpi(phba);
2640 }
2641 }
2642 }
2643 lpfc_destroy_vport_work_array(phba, vports);
2644}
2645
2646/**
James Smart3621a712009-04-06 18:47:14 -04002647 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04002648 * @phba: pointer to lpfc hba data structure.
2649 *
2650 * This routine initializes the HBA and brings a HBA online. During this
2651 * process, the management interface is blocked to prevent user space access
2652 * to the HBA interfering with the driver initialization.
2653 *
2654 * Return codes
2655 * 0 - successful
2656 * 1 - failed
2657 **/
dea31012005-04-17 16:05:31 -05002658int
James Smart2e0fef82007-06-17 19:56:36 -05002659lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002660{
Julia Lawall372bd282008-12-16 16:15:08 +01002661 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04002662 struct lpfc_vport **vports;
2663 int i;
James Smart16a3a202013-04-17 20:14:38 -04002664 bool vpis_cleared = false;
James Smart2e0fef82007-06-17 19:56:36 -05002665
dea31012005-04-17 16:05:31 -05002666 if (!phba)
2667 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01002668 vport = phba->pport;
dea31012005-04-17 16:05:31 -05002669
James Smart2e0fef82007-06-17 19:56:36 -05002670 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05002671 return 0;
2672
James Smarted957682007-06-17 19:56:37 -05002673 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002674 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05002675
James Smart618a5232012-06-12 13:54:36 -04002676 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05002677
James Smart46fa3112007-04-25 09:51:45 -04002678 if (!lpfc_sli_queue_setup(phba)) {
2679 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002680 return 1;
James Smart46fa3112007-04-25 09:51:45 -04002681 }
2682
James Smartda0436e2009-05-22 14:51:39 -04002683 if (phba->sli_rev == LPFC_SLI_REV4) {
2684 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2685 lpfc_unblock_mgmt_io(phba);
2686 return 1;
2687 }
James Smart16a3a202013-04-17 20:14:38 -04002688 spin_lock_irq(&phba->hbalock);
2689 if (!phba->sli4_hba.max_cfg_param.vpi_used)
2690 vpis_cleared = true;
2691 spin_unlock_irq(&phba->hbalock);
James Smartda0436e2009-05-22 14:51:39 -04002692 } else {
2693 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2694 lpfc_unblock_mgmt_io(phba);
2695 return 1;
2696 }
James Smart46fa3112007-04-25 09:51:45 -04002697 }
dea31012005-04-17 16:05:31 -05002698
James Smart549e55c2007-08-02 11:09:51 -04002699 vports = lpfc_create_vport_work_array(phba);
2700 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002701 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002702 struct Scsi_Host *shost;
2703 shost = lpfc_shost_from_vport(vports[i]);
2704 spin_lock_irq(shost->host_lock);
2705 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2706 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2707 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart16a3a202013-04-17 20:14:38 -04002708 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart1c6834a2009-07-19 10:01:26 -04002709 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart16a3a202013-04-17 20:14:38 -04002710 if ((vpis_cleared) &&
2711 (vports[i]->port_type !=
2712 LPFC_PHYSICAL_PORT))
2713 vports[i]->vpi = 0;
2714 }
James Smart549e55c2007-08-02 11:09:51 -04002715 spin_unlock_irq(shost->host_lock);
2716 }
James Smart09372822008-01-11 01:52:54 -05002717 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002718
James Smart46fa3112007-04-25 09:51:45 -04002719 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002720 return 0;
2721}
2722
James Smarte59058c2008-08-24 21:49:00 -04002723/**
James Smart3621a712009-04-06 18:47:14 -04002724 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04002725 * @phba: pointer to lpfc hba data structure.
2726 *
2727 * This routine marks a HBA's management interface as not blocked. Once the
2728 * HBA's management interface is marked as not blocked, all the user space
2729 * access to the HBA, whether they are from sysfs interface or libdfc
2730 * interface will be allowed. The HBA is set to block the management interface
2731 * when the driver prepares the HBA interface for online or offline and then
2732 * set to unblock the management interface afterwards.
2733 **/
James Smart46fa3112007-04-25 09:51:45 -04002734void
James Smart46fa3112007-04-25 09:51:45 -04002735lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2736{
2737 unsigned long iflag;
2738
James Smart2e0fef82007-06-17 19:56:36 -05002739 spin_lock_irqsave(&phba->hbalock, iflag);
2740 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2741 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04002742}
2743
James Smarte59058c2008-08-24 21:49:00 -04002744/**
James Smart3621a712009-04-06 18:47:14 -04002745 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04002746 * @phba: pointer to lpfc hba data structure.
2747 *
2748 * This routine is invoked to prepare a HBA to be brought offline. It performs
2749 * unregistration login to all the nodes on all vports and flushes the mailbox
2750 * queue to make it ready to be brought offline.
2751 **/
James Smart46fa3112007-04-25 09:51:45 -04002752void
James Smart618a5232012-06-12 13:54:36 -04002753lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
James Smart46fa3112007-04-25 09:51:45 -04002754{
James Smart2e0fef82007-06-17 19:56:36 -05002755 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04002756 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04002757 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05002758 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04002759 int i;
dea31012005-04-17 16:05:31 -05002760
James Smart2e0fef82007-06-17 19:56:36 -05002761 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002762 return;
dea31012005-04-17 16:05:31 -05002763
James Smart618a5232012-06-12 13:54:36 -04002764 lpfc_block_mgmt_io(phba, mbx_action);
dea31012005-04-17 16:05:31 -05002765
2766 lpfc_linkdown(phba);
2767
James Smart87af33f2007-10-27 13:37:43 -04002768 /* Issue an unreg_login to all nodes on all vports */
2769 vports = lpfc_create_vport_work_array(phba);
2770 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04002771 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04002772 if (vports[i]->load_flag & FC_UNLOADING)
2773 continue;
James Smart72100cc2010-02-12 14:43:01 -05002774 shost = lpfc_shost_from_vport(vports[i]);
2775 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002776 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002777 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2778 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05002779 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002780
James Smart87af33f2007-10-27 13:37:43 -04002781 shost = lpfc_shost_from_vport(vports[i]);
2782 list_for_each_entry_safe(ndlp, next_ndlp,
2783 &vports[i]->fc_nodes,
2784 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002785 if (!NLP_CHK_NODE_ACT(ndlp))
2786 continue;
James Smart87af33f2007-10-27 13:37:43 -04002787 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2788 continue;
2789 if (ndlp->nlp_type & NLP_FABRIC) {
2790 lpfc_disc_state_machine(vports[i], ndlp,
2791 NULL, NLP_EVT_DEVICE_RECOVERY);
2792 lpfc_disc_state_machine(vports[i], ndlp,
2793 NULL, NLP_EVT_DEVICE_RM);
2794 }
2795 spin_lock_irq(shost->host_lock);
2796 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart401ee0c2012-03-01 22:35:34 -05002797 spin_unlock_irq(shost->host_lock);
James Smart6b5151f2012-01-18 16:24:06 -05002798 /*
2799 * Whenever an SLI4 port goes offline, free the
James Smart401ee0c2012-03-01 22:35:34 -05002800 * RPI. Get a new RPI when the adapter port
2801 * comes back online.
James Smart6b5151f2012-01-18 16:24:06 -05002802 */
2803 if (phba->sli_rev == LPFC_SLI_REV4)
2804 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smart87af33f2007-10-27 13:37:43 -04002805 lpfc_unreg_rpi(vports[i], ndlp);
2806 }
2807 }
2808 }
James Smart09372822008-01-11 01:52:54 -05002809 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002810
James Smart618a5232012-06-12 13:54:36 -04002811 lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
James Smart46fa3112007-04-25 09:51:45 -04002812}
2813
James Smarte59058c2008-08-24 21:49:00 -04002814/**
James Smart3621a712009-04-06 18:47:14 -04002815 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04002816 * @phba: pointer to lpfc hba data structure.
2817 *
2818 * This routine actually brings a HBA offline. It stops all the timers
2819 * associated with the HBA, brings down the SLI layer, and eventually
2820 * marks the HBA as in offline state for the upper layer protocol.
2821 **/
James Smart46fa3112007-04-25 09:51:45 -04002822void
James Smart2e0fef82007-06-17 19:56:36 -05002823lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04002824{
James Smart549e55c2007-08-02 11:09:51 -04002825 struct Scsi_Host *shost;
2826 struct lpfc_vport **vports;
2827 int i;
James Smart46fa3112007-04-25 09:51:45 -04002828
James Smart549e55c2007-08-02 11:09:51 -04002829 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002830 return;
James Smart688a8862006-07-06 15:49:56 -04002831
James Smartda0436e2009-05-22 14:51:39 -04002832 /* stop port and all timers associated with this hba */
2833 lpfc_stop_port(phba);
James Smart51ef4c22007-08-02 11:10:31 -04002834 vports = lpfc_create_vport_work_array(phba);
2835 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002836 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04002837 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05002838 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05002839 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002840 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05002841 /* Bring down the SLI Layer and cleanup. The HBA is offline
2842 now. */
2843 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002844 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04002845 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002846 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04002847 vports = lpfc_create_vport_work_array(phba);
2848 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002849 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002850 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04002851 spin_lock_irq(shost->host_lock);
2852 vports[i]->work_port_events = 0;
2853 vports[i]->fc_flag |= FC_OFFLINE_MODE;
2854 spin_unlock_irq(shost->host_lock);
2855 }
James Smart09372822008-01-11 01:52:54 -05002856 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002857}
2858
James Smarte59058c2008-08-24 21:49:00 -04002859/**
James Smart3621a712009-04-06 18:47:14 -04002860 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04002861 * @phba: pointer to lpfc hba data structure.
2862 *
2863 * This routine is to free all the SCSI buffers and IOCBs from the driver
2864 * list back to kernel. It is called from lpfc_pci_remove_one to free
2865 * the internal resources before the device is removed from the system.
James Smarte59058c2008-08-24 21:49:00 -04002866 **/
James Smart8a9d2e82012-05-09 21:16:12 -04002867static void
James Smart2e0fef82007-06-17 19:56:36 -05002868lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002869{
2870 struct lpfc_scsi_buf *sb, *sb_next;
2871 struct lpfc_iocbq *io, *io_next;
2872
James Smart2e0fef82007-06-17 19:56:36 -05002873 spin_lock_irq(&phba->hbalock);
James Smarta40fc5f2013-04-17 20:17:40 -04002874
dea31012005-04-17 16:05:31 -05002875 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smarta40fc5f2013-04-17 20:17:40 -04002876
2877 spin_lock(&phba->scsi_buf_list_put_lock);
2878 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_put,
2879 list) {
dea31012005-04-17 16:05:31 -05002880 list_del(&sb->list);
2881 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05002882 sb->dma_handle);
dea31012005-04-17 16:05:31 -05002883 kfree(sb);
2884 phba->total_scsi_bufs--;
2885 }
James Smarta40fc5f2013-04-17 20:17:40 -04002886 spin_unlock(&phba->scsi_buf_list_put_lock);
2887
2888 spin_lock(&phba->scsi_buf_list_get_lock);
2889 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_get,
2890 list) {
2891 list_del(&sb->list);
2892 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
2893 sb->dma_handle);
2894 kfree(sb);
2895 phba->total_scsi_bufs--;
2896 }
2897 spin_unlock(&phba->scsi_buf_list_get_lock);
dea31012005-04-17 16:05:31 -05002898
2899 /* Release all the lpfc_iocbq entries maintained by this host. */
2900 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2901 list_del(&io->list);
2902 kfree(io);
2903 phba->total_iocbq_bufs--;
2904 }
James Smart6d368e52011-05-24 11:44:12 -04002905
James Smart2e0fef82007-06-17 19:56:36 -05002906 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04002907}
2908
2909/**
2910 * lpfc_sli4_xri_sgl_update - update xri-sgl sizing and mapping
2911 * @phba: pointer to lpfc hba data structure.
2912 *
2913 * This routine first calculates the sizes of the current els and allocated
2914 * scsi sgl lists, and then goes through all sgls to updates the physical
2915 * XRIs assigned due to port function reset. During port initialization, the
2916 * current els and allocated scsi sgl lists are 0s.
2917 *
2918 * Return codes
2919 * 0 - successful (for now, it always returns 0)
2920 **/
2921int
2922lpfc_sli4_xri_sgl_update(struct lpfc_hba *phba)
2923{
2924 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
2925 struct lpfc_scsi_buf *psb = NULL, *psb_next = NULL;
2926 uint16_t i, lxri, xri_cnt, els_xri_cnt, scsi_xri_cnt;
2927 LIST_HEAD(els_sgl_list);
2928 LIST_HEAD(scsi_sgl_list);
2929 int rc;
2930
2931 /*
2932 * update on pci function's els xri-sgl list
2933 */
2934 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
2935 if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
2936 /* els xri-sgl expanded */
2937 xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
2938 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2939 "3157 ELS xri-sgl count increased from "
2940 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
2941 els_xri_cnt);
2942 /* allocate the additional els sgls */
2943 for (i = 0; i < xri_cnt; i++) {
2944 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
2945 GFP_KERNEL);
2946 if (sglq_entry == NULL) {
2947 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2948 "2562 Failure to allocate an "
2949 "ELS sgl entry:%d\n", i);
2950 rc = -ENOMEM;
2951 goto out_free_mem;
2952 }
2953 sglq_entry->buff_type = GEN_BUFF_TYPE;
2954 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
2955 &sglq_entry->phys);
2956 if (sglq_entry->virt == NULL) {
2957 kfree(sglq_entry);
2958 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2959 "2563 Failure to allocate an "
2960 "ELS mbuf:%d\n", i);
2961 rc = -ENOMEM;
2962 goto out_free_mem;
2963 }
2964 sglq_entry->sgl = sglq_entry->virt;
2965 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
2966 sglq_entry->state = SGL_FREED;
2967 list_add_tail(&sglq_entry->list, &els_sgl_list);
2968 }
James Smart38c20672013-03-01 16:37:44 -05002969 spin_lock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04002970 list_splice_init(&els_sgl_list, &phba->sli4_hba.lpfc_sgl_list);
James Smart38c20672013-03-01 16:37:44 -05002971 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04002972 } else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
2973 /* els xri-sgl shrinked */
2974 xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
2975 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2976 "3158 ELS xri-sgl count decreased from "
2977 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
2978 els_xri_cnt);
2979 spin_lock_irq(&phba->hbalock);
2980 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &els_sgl_list);
2981 spin_unlock_irq(&phba->hbalock);
2982 /* release extra els sgls from list */
2983 for (i = 0; i < xri_cnt; i++) {
2984 list_remove_head(&els_sgl_list,
2985 sglq_entry, struct lpfc_sglq, list);
2986 if (sglq_entry) {
2987 lpfc_mbuf_free(phba, sglq_entry->virt,
2988 sglq_entry->phys);
2989 kfree(sglq_entry);
2990 }
2991 }
2992 spin_lock_irq(&phba->hbalock);
2993 list_splice_init(&els_sgl_list, &phba->sli4_hba.lpfc_sgl_list);
2994 spin_unlock_irq(&phba->hbalock);
2995 } else
2996 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2997 "3163 ELS xri-sgl count unchanged: %d\n",
2998 els_xri_cnt);
2999 phba->sli4_hba.els_xri_cnt = els_xri_cnt;
3000
3001 /* update xris to els sgls on the list */
3002 sglq_entry = NULL;
3003 sglq_entry_next = NULL;
3004 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
3005 &phba->sli4_hba.lpfc_sgl_list, list) {
3006 lxri = lpfc_sli4_next_xritag(phba);
3007 if (lxri == NO_XRI) {
3008 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3009 "2400 Failed to allocate xri for "
3010 "ELS sgl\n");
3011 rc = -ENOMEM;
3012 goto out_free_mem;
3013 }
3014 sglq_entry->sli4_lxritag = lxri;
3015 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3016 }
3017
3018 /*
3019 * update on pci function's allocated scsi xri-sgl list
3020 */
3021 phba->total_scsi_bufs = 0;
3022
3023 /* maximum number of xris available for scsi buffers */
3024 phba->sli4_hba.scsi_xri_max = phba->sli4_hba.max_cfg_param.max_xri -
3025 els_xri_cnt;
3026
3027 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3028 "2401 Current allocated SCSI xri-sgl count:%d, "
3029 "maximum SCSI xri count:%d\n",
3030 phba->sli4_hba.scsi_xri_cnt,
3031 phba->sli4_hba.scsi_xri_max);
3032
James Smarta40fc5f2013-04-17 20:17:40 -04003033 spin_lock_irq(&phba->scsi_buf_list_get_lock);
3034 spin_lock_irq(&phba->scsi_buf_list_put_lock);
3035 list_splice_init(&phba->lpfc_scsi_buf_list_get, &scsi_sgl_list);
3036 list_splice(&phba->lpfc_scsi_buf_list_put, &scsi_sgl_list);
3037 spin_unlock_irq(&phba->scsi_buf_list_put_lock);
3038 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003039
3040 if (phba->sli4_hba.scsi_xri_cnt > phba->sli4_hba.scsi_xri_max) {
3041 /* max scsi xri shrinked below the allocated scsi buffers */
3042 scsi_xri_cnt = phba->sli4_hba.scsi_xri_cnt -
3043 phba->sli4_hba.scsi_xri_max;
3044 /* release the extra allocated scsi buffers */
3045 for (i = 0; i < scsi_xri_cnt; i++) {
3046 list_remove_head(&scsi_sgl_list, psb,
3047 struct lpfc_scsi_buf, list);
3048 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, psb->data,
3049 psb->dma_handle);
3050 kfree(psb);
3051 }
James Smarta40fc5f2013-04-17 20:17:40 -04003052 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003053 phba->sli4_hba.scsi_xri_cnt -= scsi_xri_cnt;
James Smarta40fc5f2013-04-17 20:17:40 -04003054 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003055 }
3056
3057 /* update xris associated to remaining allocated scsi buffers */
3058 psb = NULL;
3059 psb_next = NULL;
3060 list_for_each_entry_safe(psb, psb_next, &scsi_sgl_list, list) {
3061 lxri = lpfc_sli4_next_xritag(phba);
3062 if (lxri == NO_XRI) {
3063 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3064 "2560 Failed to allocate xri for "
3065 "scsi buffer\n");
3066 rc = -ENOMEM;
3067 goto out_free_mem;
3068 }
3069 psb->cur_iocbq.sli4_lxritag = lxri;
3070 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3071 }
James Smarta40fc5f2013-04-17 20:17:40 -04003072 spin_lock_irq(&phba->scsi_buf_list_get_lock);
3073 spin_lock_irq(&phba->scsi_buf_list_put_lock);
3074 list_splice_init(&scsi_sgl_list, &phba->lpfc_scsi_buf_list_get);
3075 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
3076 spin_unlock_irq(&phba->scsi_buf_list_put_lock);
3077 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003078
dea31012005-04-17 16:05:31 -05003079 return 0;
James Smart8a9d2e82012-05-09 21:16:12 -04003080
3081out_free_mem:
3082 lpfc_free_els_sgl_list(phba);
3083 lpfc_scsi_free(phba);
3084 return rc;
dea31012005-04-17 16:05:31 -05003085}
3086
James Smarte59058c2008-08-24 21:49:00 -04003087/**
James Smart3621a712009-04-06 18:47:14 -04003088 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04003089 * @phba: pointer to lpfc hba data structure.
3090 * @instance: a unique integer ID to this FC port.
3091 * @dev: pointer to the device data structure.
3092 *
3093 * This routine creates a FC port for the upper layer protocol. The FC port
3094 * can be created on top of either a physical port or a virtual port provided
3095 * by the HBA. This routine also allocates a SCSI host data structure (shost)
3096 * and associates the FC port created before adding the shost into the SCSI
3097 * layer.
3098 *
3099 * Return codes
3100 * @vport - pointer to the virtual N_Port data structure.
3101 * NULL - port create failed.
3102 **/
James Smart2e0fef82007-06-17 19:56:36 -05003103struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04003104lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04003105{
James Smart2e0fef82007-06-17 19:56:36 -05003106 struct lpfc_vport *vport;
3107 struct Scsi_Host *shost;
3108 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04003109
James Smart3de2a652007-08-02 11:09:59 -04003110 if (dev != &phba->pcidev->dev)
3111 shost = scsi_host_alloc(&lpfc_vport_template,
3112 sizeof(struct lpfc_vport));
3113 else
3114 shost = scsi_host_alloc(&lpfc_template,
3115 sizeof(struct lpfc_vport));
James Smart2e0fef82007-06-17 19:56:36 -05003116 if (!shost)
3117 goto out;
James Smart47a86172007-04-25 09:53:22 -04003118
James Smart2e0fef82007-06-17 19:56:36 -05003119 vport = (struct lpfc_vport *) shost->hostdata;
3120 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05003121 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05003122 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05003123 vport->fc_rscn_flush = 0;
James Smart47a86172007-04-25 09:53:22 -04003124
James Smart3de2a652007-08-02 11:09:59 -04003125 lpfc_get_vport_cfgparam(vport);
James Smart2e0fef82007-06-17 19:56:36 -05003126 shost->unique_id = instance;
3127 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04003128 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05003129 shost->this_id = -1;
3130 shost->max_cmd_len = 16;
James Smartda0436e2009-05-22 14:51:39 -04003131 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart28baac72010-02-12 14:42:03 -05003132 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04003133 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smartda0436e2009-05-22 14:51:39 -04003134 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
3135 }
James Smart81301a92008-12-04 22:39:46 -05003136
James Smart47a86172007-04-25 09:53:22 -04003137 /*
James Smart2e0fef82007-06-17 19:56:36 -05003138 * Set initial can_queue value since 0 is no longer supported and
3139 * scsi_add_host will fail. This will be adjusted later based on the
3140 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04003141 */
James Smart2e0fef82007-06-17 19:56:36 -05003142 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04003143 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05003144 shost->transportt = lpfc_vport_transport_template;
3145 vport->port_type = LPFC_NPIV_PORT;
3146 } else {
3147 shost->transportt = lpfc_transport_template;
3148 vport->port_type = LPFC_PHYSICAL_PORT;
3149 }
James Smart47a86172007-04-25 09:53:22 -04003150
James Smart2e0fef82007-06-17 19:56:36 -05003151 /* Initialize all internally managed lists. */
3152 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04003153 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05003154 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04003155
James Smart2e0fef82007-06-17 19:56:36 -05003156 init_timer(&vport->fc_disctmo);
3157 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05003158 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04003159
James Smart2e0fef82007-06-17 19:56:36 -05003160 init_timer(&vport->fc_fdmitmo);
3161 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
James Smart92d7f7b2007-06-17 19:56:38 -05003162 vport->fc_fdmitmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04003163
James Smart2e0fef82007-06-17 19:56:36 -05003164 init_timer(&vport->els_tmofunc);
3165 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05003166 vport->els_tmofunc.data = (unsigned long)vport;
James Smart92494142011-02-16 12:39:44 -05003167
3168 init_timer(&vport->delayed_disc_tmo);
3169 vport->delayed_disc_tmo.function = lpfc_delayed_disc_tmo;
3170 vport->delayed_disc_tmo.data = (unsigned long)vport;
3171
James Bottomleyd139b9b2009-11-05 13:33:12 -06003172 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05003173 if (error)
3174 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04003175
James Smart549e55c2007-08-02 11:09:51 -04003176 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003177 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04003178 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003179 return vport;
James Smart47a86172007-04-25 09:53:22 -04003180
James Smart2e0fef82007-06-17 19:56:36 -05003181out_put_shost:
3182 scsi_host_put(shost);
3183out:
3184 return NULL;
James Smart47a86172007-04-25 09:53:22 -04003185}
3186
James Smarte59058c2008-08-24 21:49:00 -04003187/**
James Smart3621a712009-04-06 18:47:14 -04003188 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04003189 * @vport: pointer to an lpfc virtual N_Port data structure.
3190 *
3191 * This routine destroys a FC port from the upper layer protocol. All the
3192 * resources associated with the port are released.
3193 **/
James Smart2e0fef82007-06-17 19:56:36 -05003194void
3195destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04003196{
James Smart92d7f7b2007-06-17 19:56:38 -05003197 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3198 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003199
James Smart858c9f62007-06-17 19:56:39 -05003200 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05003201 fc_remove_host(shost);
3202 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04003203
James Smart92d7f7b2007-06-17 19:56:38 -05003204 spin_lock_irq(&phba->hbalock);
3205 list_del_init(&vport->listentry);
3206 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04003207
James Smart92d7f7b2007-06-17 19:56:38 -05003208 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04003209 return;
James Smart47a86172007-04-25 09:53:22 -04003210}
3211
James Smarte59058c2008-08-24 21:49:00 -04003212/**
James Smart3621a712009-04-06 18:47:14 -04003213 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04003214 *
3215 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
3216 * uses the kernel idr facility to perform the task.
3217 *
3218 * Return codes:
3219 * instance - a unique integer ID allocated as the new instance.
3220 * -1 - lpfc get instance failed.
3221 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003222int
3223lpfc_get_instance(void)
3224{
Tejun Heoab516032013-02-27 17:04:44 -08003225 int ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003226
Tejun Heoab516032013-02-27 17:04:44 -08003227 ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
3228 return ret < 0 ? -1 : ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003229}
3230
James Smarte59058c2008-08-24 21:49:00 -04003231/**
James Smart3621a712009-04-06 18:47:14 -04003232 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04003233 * @shost: pointer to SCSI host data structure.
3234 * @time: elapsed time of the scan in jiffies.
3235 *
3236 * This routine is called by the SCSI layer with a SCSI host to determine
3237 * whether the scan host is finished.
3238 *
3239 * Note: there is no scan_start function as adapter initialization will have
3240 * asynchronously kicked off the link initialization.
3241 *
3242 * Return codes
3243 * 0 - SCSI host scan is not over yet.
3244 * 1 - SCSI host scan is over.
3245 **/
James Smart47a86172007-04-25 09:53:22 -04003246int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
3247{
James Smart2e0fef82007-06-17 19:56:36 -05003248 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3249 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05003250 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04003251
James Smart858c9f62007-06-17 19:56:39 -05003252 spin_lock_irq(shost->host_lock);
3253
James Smart51ef4c22007-08-02 11:10:31 -04003254 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05003255 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003256 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05003257 }
James Smart256ec0d2013-04-17 20:14:58 -04003258 if (time >= msecs_to_jiffies(30 * 1000)) {
James Smart2e0fef82007-06-17 19:56:36 -05003259 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003260 "0461 Scanning longer than 30 "
3261 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003262 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003263 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003264 }
James Smart256ec0d2013-04-17 20:14:58 -04003265 if (time >= msecs_to_jiffies(15 * 1000) &&
3266 phba->link_state <= LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -05003267 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003268 "0465 Link down longer than 15 "
3269 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003270 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05003271 goto finished;
James Smart47a86172007-04-25 09:53:22 -04003272 }
3273
James Smart2e0fef82007-06-17 19:56:36 -05003274 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05003275 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003276 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05003277 goto finished;
James Smart256ec0d2013-04-17 20:14:58 -04003278 if (vport->fc_map_cnt == 0 && time < msecs_to_jiffies(2 * 1000))
James Smart858c9f62007-06-17 19:56:39 -05003279 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003280 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05003281 goto finished;
3282
3283 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003284
3285finished:
James Smart858c9f62007-06-17 19:56:39 -05003286 spin_unlock_irq(shost->host_lock);
3287 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05003288}
3289
James Smarte59058c2008-08-24 21:49:00 -04003290/**
James Smart3621a712009-04-06 18:47:14 -04003291 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04003292 * @shost: pointer to SCSI host data structure.
3293 *
3294 * This routine initializes a given SCSI host attributes on a FC port. The
3295 * SCSI host can be either on top of a physical port or a virtual port.
3296 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003297void lpfc_host_attrib_init(struct Scsi_Host *shost)
3298{
3299 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3300 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003301 /*
James Smart2e0fef82007-06-17 19:56:36 -05003302 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04003303 */
3304
James Smart2e0fef82007-06-17 19:56:36 -05003305 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
3306 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04003307 fc_host_supported_classes(shost) = FC_COS_CLASS3;
3308
3309 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003310 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003311 fc_host_supported_fc4s(shost)[2] = 1;
3312 fc_host_supported_fc4s(shost)[7] = 1;
3313
James Smart92d7f7b2007-06-17 19:56:38 -05003314 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
3315 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04003316
3317 fc_host_supported_speeds(shost) = 0;
James Smart88a2cfb2011-07-22 18:36:33 -04003318 if (phba->lmt & LMT_16Gb)
3319 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
James Smart47a86172007-04-25 09:53:22 -04003320 if (phba->lmt & LMT_10Gb)
3321 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04003322 if (phba->lmt & LMT_8Gb)
3323 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04003324 if (phba->lmt & LMT_4Gb)
3325 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
3326 if (phba->lmt & LMT_2Gb)
3327 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
3328 if (phba->lmt & LMT_1Gb)
3329 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
3330
3331 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05003332 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
3333 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04003334
Mike Christie0af5d702010-09-15 16:52:31 -05003335 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
3336
James Smart47a86172007-04-25 09:53:22 -04003337 /* This value is also unchanging */
3338 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003339 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003340 fc_host_active_fc4s(shost)[2] = 1;
3341 fc_host_active_fc4s(shost)[7] = 1;
3342
James Smart92d7f7b2007-06-17 19:56:38 -05003343 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04003344 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04003345 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04003346 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04003347}
dea31012005-04-17 16:05:31 -05003348
James Smarte59058c2008-08-24 21:49:00 -04003349/**
James Smartda0436e2009-05-22 14:51:39 -04003350 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04003351 * @phba: pointer to lpfc hba data structure.
3352 *
James Smartda0436e2009-05-22 14:51:39 -04003353 * This routine is invoked to stop an SLI3 device port, it stops the device
3354 * from generating interrupts and stops the device driver's timers for the
3355 * device.
James Smarte59058c2008-08-24 21:49:00 -04003356 **/
James Smartdb2378e2008-02-08 18:49:51 -05003357static void
James Smartda0436e2009-05-22 14:51:39 -04003358lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05003359{
James Smartda0436e2009-05-22 14:51:39 -04003360 /* Clear all interrupt enable conditions */
3361 writel(0, phba->HCregaddr);
3362 readl(phba->HCregaddr); /* flush */
3363 /* Clear all pending interrupts */
3364 writel(0xffffffff, phba->HAregaddr);
3365 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04003366
James Smartda0436e2009-05-22 14:51:39 -04003367 /* Reset some HBA SLI setup states */
3368 lpfc_stop_hba_timers(phba);
3369 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05003370}
3371
James Smarte59058c2008-08-24 21:49:00 -04003372/**
James Smartda0436e2009-05-22 14:51:39 -04003373 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05003374 * @phba: pointer to lpfc hba data structure.
3375 *
James Smartda0436e2009-05-22 14:51:39 -04003376 * This routine is invoked to stop an SLI4 device port, it stops the device
3377 * from generating interrupts and stops the device driver's timers for the
3378 * device.
3379 **/
3380static void
3381lpfc_stop_port_s4(struct lpfc_hba *phba)
3382{
3383 /* Reset some HBA SLI4 setup states */
3384 lpfc_stop_hba_timers(phba);
3385 phba->pport->work_port_events = 0;
3386 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04003387}
3388
3389/**
3390 * lpfc_stop_port - Wrapper function for stopping hba port
3391 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05003392 *
James Smartda0436e2009-05-22 14:51:39 -04003393 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
3394 * the API jump table function pointer from the lpfc_hba struct.
3395 **/
3396void
3397lpfc_stop_port(struct lpfc_hba *phba)
3398{
3399 phba->lpfc_stop_port(phba);
3400}
3401
3402/**
James Smartecfd03c2010-02-12 14:41:27 -05003403 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
3404 * @phba: Pointer to hba for which this call is being executed.
3405 *
3406 * This routine starts the timer waiting for the FCF rediscovery to complete.
3407 **/
3408void
3409lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
3410{
3411 unsigned long fcf_redisc_wait_tmo =
3412 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
3413 /* Start fcf rediscovery wait period timer */
3414 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
3415 spin_lock_irq(&phba->hbalock);
3416 /* Allow action to new fcf asynchronous event */
3417 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
3418 /* Mark the FCF rediscovery pending state */
3419 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
3420 spin_unlock_irq(&phba->hbalock);
3421}
3422
3423/**
3424 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
3425 * @ptr: Map to lpfc_hba data structure pointer.
3426 *
3427 * This routine is invoked when waiting for FCF table rediscover has been
3428 * timed out. If new FCF record(s) has (have) been discovered during the
3429 * wait period, a new FCF event shall be added to the FCOE async event
3430 * list, and then worker thread shall be waked up for processing from the
3431 * worker thread context.
3432 **/
3433void
3434lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
3435{
3436 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
3437
3438 /* Don't send FCF rediscovery event if timer cancelled */
3439 spin_lock_irq(&phba->hbalock);
3440 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
3441 spin_unlock_irq(&phba->hbalock);
3442 return;
3443 }
3444 /* Clear FCF rediscovery timer pending flag */
3445 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
3446 /* FCF rediscovery event to worker thread */
3447 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
3448 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003449 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04003450 "2776 FCF rediscover quiescent timer expired\n");
James Smartecfd03c2010-02-12 14:41:27 -05003451 /* wake up worker thread */
3452 lpfc_worker_wake_up(phba);
3453}
3454
3455/**
James Smartda0436e2009-05-22 14:51:39 -04003456 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
3457 * @phba: pointer to lpfc hba data structure.
3458 * @acqe_link: pointer to the async link completion queue entry.
3459 *
3460 * This routine is to parse the SLI4 link-attention link fault code and
3461 * translate it into the base driver's read link attention mailbox command
3462 * status.
3463 *
3464 * Return: Link-attention status in terms of base driver's coding.
3465 **/
3466static uint16_t
3467lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
3468 struct lpfc_acqe_link *acqe_link)
3469{
3470 uint16_t latt_fault;
3471
3472 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
3473 case LPFC_ASYNC_LINK_FAULT_NONE:
3474 case LPFC_ASYNC_LINK_FAULT_LOCAL:
3475 case LPFC_ASYNC_LINK_FAULT_REMOTE:
3476 latt_fault = 0;
3477 break;
3478 default:
3479 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3480 "0398 Invalid link fault code: x%x\n",
3481 bf_get(lpfc_acqe_link_fault, acqe_link));
3482 latt_fault = MBXERR_ERROR;
3483 break;
3484 }
3485 return latt_fault;
3486}
3487
3488/**
3489 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
3490 * @phba: pointer to lpfc hba data structure.
3491 * @acqe_link: pointer to the async link completion queue entry.
3492 *
3493 * This routine is to parse the SLI4 link attention type and translate it
3494 * into the base driver's link attention type coding.
3495 *
3496 * Return: Link attention type in terms of base driver's coding.
3497 **/
3498static uint8_t
3499lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
3500 struct lpfc_acqe_link *acqe_link)
3501{
3502 uint8_t att_type;
3503
3504 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
3505 case LPFC_ASYNC_LINK_STATUS_DOWN:
3506 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
James Smart76a95d72010-11-20 23:11:48 -05003507 att_type = LPFC_ATT_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04003508 break;
3509 case LPFC_ASYNC_LINK_STATUS_UP:
3510 /* Ignore physical link up events - wait for logical link up */
James Smart76a95d72010-11-20 23:11:48 -05003511 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003512 break;
3513 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
James Smart76a95d72010-11-20 23:11:48 -05003514 att_type = LPFC_ATT_LINK_UP;
James Smartda0436e2009-05-22 14:51:39 -04003515 break;
3516 default:
3517 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3518 "0399 Invalid link attention type: x%x\n",
3519 bf_get(lpfc_acqe_link_status, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003520 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003521 break;
3522 }
3523 return att_type;
3524}
3525
3526/**
3527 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
3528 * @phba: pointer to lpfc hba data structure.
3529 * @acqe_link: pointer to the async link completion queue entry.
3530 *
3531 * This routine is to parse the SLI4 link-attention link speed and translate
3532 * it into the base driver's link-attention link speed coding.
3533 *
3534 * Return: Link-attention link speed in terms of base driver's coding.
3535 **/
3536static uint8_t
3537lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
3538 struct lpfc_acqe_link *acqe_link)
3539{
3540 uint8_t link_speed;
3541
3542 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
3543 case LPFC_ASYNC_LINK_SPEED_ZERO:
James Smartda0436e2009-05-22 14:51:39 -04003544 case LPFC_ASYNC_LINK_SPEED_10MBPS:
James Smartda0436e2009-05-22 14:51:39 -04003545 case LPFC_ASYNC_LINK_SPEED_100MBPS:
James Smart76a95d72010-11-20 23:11:48 -05003546 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003547 break;
3548 case LPFC_ASYNC_LINK_SPEED_1GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003549 link_speed = LPFC_LINK_SPEED_1GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003550 break;
3551 case LPFC_ASYNC_LINK_SPEED_10GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003552 link_speed = LPFC_LINK_SPEED_10GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003553 break;
3554 default:
3555 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3556 "0483 Invalid link-attention link speed: x%x\n",
3557 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003558 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003559 break;
3560 }
3561 return link_speed;
3562}
3563
3564/**
James Smart8b68cd52012-09-29 11:32:37 -04003565 * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
3566 * @phba: pointer to lpfc hba data structure.
3567 *
3568 * This routine is to get an SLI3 FC port's link speed in Mbps.
3569 *
3570 * Return: link speed in terms of Mbps.
3571 **/
3572uint32_t
3573lpfc_sli_port_speed_get(struct lpfc_hba *phba)
3574{
3575 uint32_t link_speed;
3576
3577 if (!lpfc_is_link_up(phba))
3578 return 0;
3579
3580 switch (phba->fc_linkspeed) {
3581 case LPFC_LINK_SPEED_1GHZ:
3582 link_speed = 1000;
3583 break;
3584 case LPFC_LINK_SPEED_2GHZ:
3585 link_speed = 2000;
3586 break;
3587 case LPFC_LINK_SPEED_4GHZ:
3588 link_speed = 4000;
3589 break;
3590 case LPFC_LINK_SPEED_8GHZ:
3591 link_speed = 8000;
3592 break;
3593 case LPFC_LINK_SPEED_10GHZ:
3594 link_speed = 10000;
3595 break;
3596 case LPFC_LINK_SPEED_16GHZ:
3597 link_speed = 16000;
3598 break;
3599 default:
3600 link_speed = 0;
3601 }
3602 return link_speed;
3603}
3604
3605/**
3606 * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
3607 * @phba: pointer to lpfc hba data structure.
3608 * @evt_code: asynchronous event code.
3609 * @speed_code: asynchronous event link speed code.
3610 *
3611 * This routine is to parse the giving SLI4 async event link speed code into
3612 * value of Mbps for the link speed.
3613 *
3614 * Return: link speed in terms of Mbps.
3615 **/
3616static uint32_t
3617lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
3618 uint8_t speed_code)
3619{
3620 uint32_t port_speed;
3621
3622 switch (evt_code) {
3623 case LPFC_TRAILER_CODE_LINK:
3624 switch (speed_code) {
3625 case LPFC_EVT_CODE_LINK_NO_LINK:
3626 port_speed = 0;
3627 break;
3628 case LPFC_EVT_CODE_LINK_10_MBIT:
3629 port_speed = 10;
3630 break;
3631 case LPFC_EVT_CODE_LINK_100_MBIT:
3632 port_speed = 100;
3633 break;
3634 case LPFC_EVT_CODE_LINK_1_GBIT:
3635 port_speed = 1000;
3636 break;
3637 case LPFC_EVT_CODE_LINK_10_GBIT:
3638 port_speed = 10000;
3639 break;
3640 default:
3641 port_speed = 0;
3642 }
3643 break;
3644 case LPFC_TRAILER_CODE_FC:
3645 switch (speed_code) {
3646 case LPFC_EVT_CODE_FC_NO_LINK:
3647 port_speed = 0;
3648 break;
3649 case LPFC_EVT_CODE_FC_1_GBAUD:
3650 port_speed = 1000;
3651 break;
3652 case LPFC_EVT_CODE_FC_2_GBAUD:
3653 port_speed = 2000;
3654 break;
3655 case LPFC_EVT_CODE_FC_4_GBAUD:
3656 port_speed = 4000;
3657 break;
3658 case LPFC_EVT_CODE_FC_8_GBAUD:
3659 port_speed = 8000;
3660 break;
3661 case LPFC_EVT_CODE_FC_10_GBAUD:
3662 port_speed = 10000;
3663 break;
3664 case LPFC_EVT_CODE_FC_16_GBAUD:
3665 port_speed = 16000;
3666 break;
3667 default:
3668 port_speed = 0;
3669 }
3670 break;
3671 default:
3672 port_speed = 0;
3673 }
3674 return port_speed;
3675}
3676
3677/**
James Smart70f3c072010-12-15 17:57:33 -05003678 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
James Smartda0436e2009-05-22 14:51:39 -04003679 * @phba: pointer to lpfc hba data structure.
3680 * @acqe_link: pointer to the async link completion queue entry.
3681 *
James Smart70f3c072010-12-15 17:57:33 -05003682 * This routine is to handle the SLI4 asynchronous FCoE link event.
James Smartda0436e2009-05-22 14:51:39 -04003683 **/
3684static void
3685lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3686 struct lpfc_acqe_link *acqe_link)
3687{
3688 struct lpfc_dmabuf *mp;
3689 LPFC_MBOXQ_t *pmb;
3690 MAILBOX_t *mb;
James Smart76a95d72010-11-20 23:11:48 -05003691 struct lpfc_mbx_read_top *la;
James Smartda0436e2009-05-22 14:51:39 -04003692 uint8_t att_type;
James Smart76a95d72010-11-20 23:11:48 -05003693 int rc;
James Smartda0436e2009-05-22 14:51:39 -04003694
3695 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05003696 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
James Smartda0436e2009-05-22 14:51:39 -04003697 return;
James Smart32b97932009-07-19 10:01:21 -04003698 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003699 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3700 if (!pmb) {
3701 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3702 "0395 The mboxq allocation failed\n");
3703 return;
3704 }
3705 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3706 if (!mp) {
3707 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3708 "0396 The lpfc_dmabuf allocation failed\n");
3709 goto out_free_pmb;
3710 }
3711 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3712 if (!mp->virt) {
3713 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3714 "0397 The mbuf allocation failed\n");
3715 goto out_free_dmabuf;
3716 }
3717
3718 /* Cleanup any outstanding ELS commands */
3719 lpfc_els_flush_all_cmd(phba);
3720
3721 /* Block ELS IOCBs until we have done process link event */
3722 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3723
3724 /* Update link event statistics */
3725 phba->sli.slistat.link_event++;
3726
James Smart76a95d72010-11-20 23:11:48 -05003727 /* Create lpfc_handle_latt mailbox command from link ACQE */
3728 lpfc_read_topology(phba, pmb, mp);
3729 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smartda0436e2009-05-22 14:51:39 -04003730 pmb->vport = phba->pport;
3731
James Smartda0436e2009-05-22 14:51:39 -04003732 /* Keep the link status for extra SLI4 state machine reference */
3733 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04003734 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
3735 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smartda0436e2009-05-22 14:51:39 -04003736 phba->sli4_hba.link_state.duplex =
3737 bf_get(lpfc_acqe_link_duplex, acqe_link);
3738 phba->sli4_hba.link_state.status =
3739 bf_get(lpfc_acqe_link_status, acqe_link);
James Smart70f3c072010-12-15 17:57:33 -05003740 phba->sli4_hba.link_state.type =
3741 bf_get(lpfc_acqe_link_type, acqe_link);
3742 phba->sli4_hba.link_state.number =
3743 bf_get(lpfc_acqe_link_number, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04003744 phba->sli4_hba.link_state.fault =
3745 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05003746 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04003747 bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
3748
James Smart70f3c072010-12-15 17:57:33 -05003749 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartc31098c2011-04-16 11:03:33 -04003750 "2900 Async FC/FCoE Link event - Speed:%dGBit "
3751 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
3752 "Logical speed:%dMbps Fault:%d\n",
James Smart70f3c072010-12-15 17:57:33 -05003753 phba->sli4_hba.link_state.speed,
3754 phba->sli4_hba.link_state.topology,
3755 phba->sli4_hba.link_state.status,
3756 phba->sli4_hba.link_state.type,
3757 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04003758 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05003759 phba->sli4_hba.link_state.fault);
James Smart76a95d72010-11-20 23:11:48 -05003760 /*
3761 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
3762 * topology info. Note: Optional for non FC-AL ports.
3763 */
3764 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3765 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3766 if (rc == MBX_NOT_FINISHED)
3767 goto out_free_dmabuf;
3768 return;
3769 }
3770 /*
3771 * For FCoE Mode: fill in all the topology information we need and call
3772 * the READ_TOPOLOGY completion routine to continue without actually
3773 * sending the READ_TOPOLOGY mailbox command to the port.
3774 */
3775 /* Parse and translate status field */
3776 mb = &pmb->u.mb;
3777 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
3778
3779 /* Parse and translate link attention fields */
3780 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
3781 la->eventTag = acqe_link->event_tag;
3782 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
3783 bf_set(lpfc_mbx_read_top_link_spd, la,
3784 lpfc_sli4_parse_latt_link_speed(phba, acqe_link));
3785
3786 /* Fake the the following irrelvant fields */
3787 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
3788 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
3789 bf_set(lpfc_mbx_read_top_il, la, 0);
3790 bf_set(lpfc_mbx_read_top_pb, la, 0);
3791 bf_set(lpfc_mbx_read_top_fa, la, 0);
3792 bf_set(lpfc_mbx_read_top_mm, la, 0);
James Smartda0436e2009-05-22 14:51:39 -04003793
3794 /* Invoke the lpfc_handle_latt mailbox command callback function */
James Smart76a95d72010-11-20 23:11:48 -05003795 lpfc_mbx_cmpl_read_topology(phba, pmb);
James Smartda0436e2009-05-22 14:51:39 -04003796
3797 return;
3798
3799out_free_dmabuf:
3800 kfree(mp);
3801out_free_pmb:
3802 mempool_free(pmb, phba->mbox_mem_pool);
3803}
3804
3805/**
James Smart70f3c072010-12-15 17:57:33 -05003806 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
3807 * @phba: pointer to lpfc hba data structure.
3808 * @acqe_fc: pointer to the async fc completion queue entry.
3809 *
3810 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
3811 * that the event was received and then issue a read_topology mailbox command so
3812 * that the rest of the driver will treat it the same as SLI3.
3813 **/
3814static void
3815lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
3816{
3817 struct lpfc_dmabuf *mp;
3818 LPFC_MBOXQ_t *pmb;
3819 int rc;
3820
3821 if (bf_get(lpfc_trailer_type, acqe_fc) !=
3822 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
3823 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3824 "2895 Non FC link Event detected.(%d)\n",
3825 bf_get(lpfc_trailer_type, acqe_fc));
3826 return;
3827 }
3828 /* Keep the link status for extra SLI4 state machine reference */
3829 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04003830 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
3831 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
James Smart70f3c072010-12-15 17:57:33 -05003832 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
3833 phba->sli4_hba.link_state.topology =
3834 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
3835 phba->sli4_hba.link_state.status =
3836 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
3837 phba->sli4_hba.link_state.type =
3838 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
3839 phba->sli4_hba.link_state.number =
3840 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
3841 phba->sli4_hba.link_state.fault =
3842 bf_get(lpfc_acqe_link_fault, acqe_fc);
3843 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04003844 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
James Smart70f3c072010-12-15 17:57:33 -05003845 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3846 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
3847 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
3848 "%dMbps Fault:%d\n",
3849 phba->sli4_hba.link_state.speed,
3850 phba->sli4_hba.link_state.topology,
3851 phba->sli4_hba.link_state.status,
3852 phba->sli4_hba.link_state.type,
3853 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04003854 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05003855 phba->sli4_hba.link_state.fault);
3856 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3857 if (!pmb) {
3858 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3859 "2897 The mboxq allocation failed\n");
3860 return;
3861 }
3862 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3863 if (!mp) {
3864 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3865 "2898 The lpfc_dmabuf allocation failed\n");
3866 goto out_free_pmb;
3867 }
3868 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3869 if (!mp->virt) {
3870 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3871 "2899 The mbuf allocation failed\n");
3872 goto out_free_dmabuf;
3873 }
3874
3875 /* Cleanup any outstanding ELS commands */
3876 lpfc_els_flush_all_cmd(phba);
3877
3878 /* Block ELS IOCBs until we have done process link event */
3879 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3880
3881 /* Update link event statistics */
3882 phba->sli.slistat.link_event++;
3883
3884 /* Create lpfc_handle_latt mailbox command from link ACQE */
3885 lpfc_read_topology(phba, pmb, mp);
3886 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
3887 pmb->vport = phba->pport;
3888
3889 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3890 if (rc == MBX_NOT_FINISHED)
3891 goto out_free_dmabuf;
3892 return;
3893
3894out_free_dmabuf:
3895 kfree(mp);
3896out_free_pmb:
3897 mempool_free(pmb, phba->mbox_mem_pool);
3898}
3899
3900/**
3901 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
3902 * @phba: pointer to lpfc hba data structure.
3903 * @acqe_fc: pointer to the async SLI completion queue entry.
3904 *
3905 * This routine is to handle the SLI4 asynchronous SLI events.
3906 **/
3907static void
3908lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
3909{
James Smart4b8bae02012-06-12 13:55:07 -04003910 char port_name;
James Smart8c1312e2012-10-31 14:45:09 -04003911 char message[128];
James Smart4b8bae02012-06-12 13:55:07 -04003912 uint8_t status;
3913 struct lpfc_acqe_misconfigured_event *misconfigured;
3914
3915 /* special case misconfigured event as it contains data for all ports */
3916 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3917 LPFC_SLI_INTF_IF_TYPE_2) ||
3918 (bf_get(lpfc_trailer_type, acqe_sli) !=
3919 LPFC_SLI_EVENT_TYPE_MISCONFIGURED)) {
3920 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3921 "2901 Async SLI event - Event Data1:x%08x Event Data2:"
3922 "x%08x SLI Event Type:%d\n",
3923 acqe_sli->event_data1, acqe_sli->event_data2,
3924 bf_get(lpfc_trailer_type, acqe_sli));
3925 return;
3926 }
3927
3928 port_name = phba->Port[0];
3929 if (port_name == 0x00)
3930 port_name = '?'; /* get port name is empty */
3931
3932 misconfigured = (struct lpfc_acqe_misconfigured_event *)
3933 &acqe_sli->event_data1;
3934
3935 /* fetch the status for this port */
3936 switch (phba->sli4_hba.lnk_info.lnk_no) {
3937 case LPFC_LINK_NUMBER_0:
3938 status = bf_get(lpfc_sli_misconfigured_port0,
3939 &misconfigured->theEvent);
3940 break;
3941 case LPFC_LINK_NUMBER_1:
3942 status = bf_get(lpfc_sli_misconfigured_port1,
3943 &misconfigured->theEvent);
3944 break;
3945 case LPFC_LINK_NUMBER_2:
3946 status = bf_get(lpfc_sli_misconfigured_port2,
3947 &misconfigured->theEvent);
3948 break;
3949 case LPFC_LINK_NUMBER_3:
3950 status = bf_get(lpfc_sli_misconfigured_port3,
3951 &misconfigured->theEvent);
3952 break;
3953 default:
3954 status = ~LPFC_SLI_EVENT_STATUS_VALID;
3955 break;
3956 }
3957
3958 switch (status) {
3959 case LPFC_SLI_EVENT_STATUS_VALID:
3960 return; /* no message if the sfp is okay */
3961 case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
James Smart292098b2012-09-29 11:31:41 -04003962 sprintf(message, "Optics faulted/incorrectly installed/not " \
3963 "installed - Reseat optics, if issue not "
3964 "resolved, replace.");
James Smart4b8bae02012-06-12 13:55:07 -04003965 break;
3966 case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
3967 sprintf(message,
James Smart292098b2012-09-29 11:31:41 -04003968 "Optics of two types installed - Remove one optic or " \
3969 "install matching pair of optics.");
James Smart4b8bae02012-06-12 13:55:07 -04003970 break;
3971 case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
James Smart292098b2012-09-29 11:31:41 -04003972 sprintf(message, "Incompatible optics - Replace with " \
3973 "compatible optics for card to function.");
James Smart4b8bae02012-06-12 13:55:07 -04003974 break;
3975 default:
3976 /* firmware is reporting a status we don't know about */
3977 sprintf(message, "Unknown event status x%02x", status);
3978 break;
3979 }
3980
3981 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3982 "3176 Misconfigured Physical Port - "
3983 "Port Name %c %s\n", port_name, message);
James Smart70f3c072010-12-15 17:57:33 -05003984}
3985
3986/**
James Smartfc2b9892010-02-26 14:15:29 -05003987 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
3988 * @vport: pointer to vport data structure.
3989 *
3990 * This routine is to perform Clear Virtual Link (CVL) on a vport in
3991 * response to a CVL event.
3992 *
3993 * Return the pointer to the ndlp with the vport if successful, otherwise
3994 * return NULL.
3995 **/
3996static struct lpfc_nodelist *
3997lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
3998{
3999 struct lpfc_nodelist *ndlp;
4000 struct Scsi_Host *shost;
4001 struct lpfc_hba *phba;
4002
4003 if (!vport)
4004 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05004005 phba = vport->phba;
4006 if (!phba)
4007 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04004008 ndlp = lpfc_findnode_did(vport, Fabric_DID);
4009 if (!ndlp) {
4010 /* Cannot find existing Fabric ndlp, so allocate a new one */
4011 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
4012 if (!ndlp)
4013 return 0;
4014 lpfc_nlp_init(vport, ndlp, Fabric_DID);
4015 /* Set the node type */
4016 ndlp->nlp_type |= NLP_FABRIC;
4017 /* Put ndlp onto node list */
4018 lpfc_enqueue_node(vport, ndlp);
4019 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4020 /* re-setup ndlp without removing from node list */
4021 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
4022 if (!ndlp)
4023 return 0;
4024 }
James Smart63e801c2010-11-20 23:14:19 -05004025 if ((phba->pport->port_state < LPFC_FLOGI) &&
4026 (phba->pport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05004027 return NULL;
4028 /* If virtual link is not yet instantiated ignore CVL */
James Smart63e801c2010-11-20 23:14:19 -05004029 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
4030 && (vport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05004031 return NULL;
4032 shost = lpfc_shost_from_vport(vport);
4033 if (!shost)
4034 return NULL;
4035 lpfc_linkdown_port(vport);
4036 lpfc_cleanup_pending_mbox(vport);
4037 spin_lock_irq(shost->host_lock);
4038 vport->fc_flag |= FC_VPORT_CVL_RCVD;
4039 spin_unlock_irq(shost->host_lock);
4040
4041 return ndlp;
4042}
4043
4044/**
4045 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
4046 * @vport: pointer to lpfc hba data structure.
4047 *
4048 * This routine is to perform Clear Virtual Link (CVL) on all vports in
4049 * response to a FCF dead event.
4050 **/
4051static void
4052lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
4053{
4054 struct lpfc_vport **vports;
4055 int i;
4056
4057 vports = lpfc_create_vport_work_array(phba);
4058 if (vports)
4059 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
4060 lpfc_sli4_perform_vport_cvl(vports[i]);
4061 lpfc_destroy_vport_work_array(phba, vports);
4062}
4063
4064/**
James Smart76a95d72010-11-20 23:11:48 -05004065 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
James Smartda0436e2009-05-22 14:51:39 -04004066 * @phba: pointer to lpfc hba data structure.
4067 * @acqe_link: pointer to the async fcoe completion queue entry.
4068 *
4069 * This routine is to handle the SLI4 asynchronous fcoe event.
4070 **/
4071static void
James Smart76a95d72010-11-20 23:11:48 -05004072lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
James Smart70f3c072010-12-15 17:57:33 -05004073 struct lpfc_acqe_fip *acqe_fip)
James Smartda0436e2009-05-22 14:51:39 -04004074{
James Smart70f3c072010-12-15 17:57:33 -05004075 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004076 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04004077 struct lpfc_vport *vport;
4078 struct lpfc_nodelist *ndlp;
4079 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05004080 int active_vlink_present;
4081 struct lpfc_vport **vports;
4082 int i;
James Smartda0436e2009-05-22 14:51:39 -04004083
James Smart70f3c072010-12-15 17:57:33 -05004084 phba->fc_eventTag = acqe_fip->event_tag;
4085 phba->fcoe_eventtag = acqe_fip->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004086 switch (event_type) {
James Smart70f3c072010-12-15 17:57:33 -05004087 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
4088 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
4089 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
James Smart999d8132010-03-15 11:24:56 -04004090 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4091 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004092 "2546 New FCF event, evt_tag:x%x, "
4093 "index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004094 acqe_fip->event_tag,
4095 acqe_fip->index);
James Smart999d8132010-03-15 11:24:56 -04004096 else
4097 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
4098 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004099 "2788 FCF param modified event, "
4100 "evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004101 acqe_fip->event_tag,
4102 acqe_fip->index);
James Smart38b92ef2010-08-04 16:11:39 -04004103 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004104 /*
4105 * During period of FCF discovery, read the FCF
4106 * table record indexed by the event to update
James Smarta93ff372010-10-22 11:06:08 -04004107 * FCF roundrobin failover eligible FCF bmask.
James Smart0c9ab6f2010-02-26 14:15:57 -05004108 */
4109 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4110 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004111 "2779 Read FCF (x%x) for updating "
4112 "roundrobin FCF failover bmask\n",
James Smart70f3c072010-12-15 17:57:33 -05004113 acqe_fip->index);
4114 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004115 }
James Smart38b92ef2010-08-04 16:11:39 -04004116
4117 /* If the FCF discovery is in progress, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04004118 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04004119 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart38b92ef2010-08-04 16:11:39 -04004120 spin_unlock_irq(&phba->hbalock);
4121 break;
4122 }
4123 /* If fast FCF failover rescan event is pending, do nothing */
4124 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
4125 spin_unlock_irq(&phba->hbalock);
4126 break;
4127 }
4128
James Smartc2b97122013-05-31 17:05:36 -04004129 /* If the FCF has been in discovered state, do nothing. */
4130 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
James Smart3804dc82010-07-14 15:31:37 -04004131 spin_unlock_irq(&phba->hbalock);
4132 break;
4133 }
4134 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04004135
James Smart0c9ab6f2010-02-26 14:15:57 -05004136 /* Otherwise, scan the entire FCF table and re-discover SAN */
4137 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004138 "2770 Start FCF table scan per async FCF "
4139 "event, evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004140 acqe_fip->event_tag, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004141 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
4142 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04004143 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05004144 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4145 "2547 Issue FCF scan read FCF mailbox "
James Smarta93ff372010-10-22 11:06:08 -04004146 "command failed (x%x)\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004147 break;
4148
James Smart70f3c072010-12-15 17:57:33 -05004149 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
James Smartda0436e2009-05-22 14:51:39 -04004150 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04004151 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004152 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
4153 acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04004154 break;
4155
James Smart70f3c072010-12-15 17:57:33 -05004156 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
James Smart80c17842012-03-01 22:35:45 -05004157 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05004158 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004159 "2549 FCF (x%x) disconnected from network, "
James Smart70f3c072010-12-15 17:57:33 -05004160 "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart38b92ef2010-08-04 16:11:39 -04004161 /*
4162 * If we are in the middle of FCF failover process, clear
4163 * the corresponding FCF bit in the roundrobin bitmap.
James Smartda0436e2009-05-22 14:51:39 -04004164 */
James Smartfc2b9892010-02-26 14:15:29 -05004165 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004166 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05004167 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004168 /* Update FLOGI FCF failover eligible FCF bmask */
James Smart70f3c072010-12-15 17:57:33 -05004169 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004170 break;
4171 }
James Smart38b92ef2010-08-04 16:11:39 -04004172 spin_unlock_irq(&phba->hbalock);
4173
4174 /* If the event is not for currently used fcf do nothing */
James Smart70f3c072010-12-15 17:57:33 -05004175 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
James Smart38b92ef2010-08-04 16:11:39 -04004176 break;
4177
4178 /*
4179 * Otherwise, request the port to rediscover the entire FCF
4180 * table for a fast recovery from case that the current FCF
4181 * is no longer valid as we are not in the middle of FCF
4182 * failover process already.
4183 */
James Smartc2b97122013-05-31 17:05:36 -04004184 spin_lock_irq(&phba->hbalock);
4185 /* Mark the fast failover process in progress */
4186 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
4187 spin_unlock_irq(&phba->hbalock);
4188
4189 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
4190 "2771 Start FCF fast failover process due to "
4191 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
4192 "\n", acqe_fip->event_tag, acqe_fip->index);
4193 rc = lpfc_sli4_redisc_fcf_table(phba);
4194 if (rc) {
4195 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4196 LOG_DISCOVERY,
4197 "2772 Issue FCF rediscover mabilbox "
4198 "command failed, fail through to FCF "
4199 "dead event\n");
4200 spin_lock_irq(&phba->hbalock);
4201 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
4202 spin_unlock_irq(&phba->hbalock);
4203 /*
4204 * Last resort will fail over by treating this
4205 * as a link down to FCF registration.
4206 */
4207 lpfc_sli4_fcf_dead_failthrough(phba);
4208 } else {
4209 /* Reset FCF roundrobin bmask for new discovery */
4210 lpfc_sli4_clear_fcf_rr_bmask(phba);
4211 /*
4212 * Handling fast FCF failover to a DEAD FCF event is
4213 * considered equalivant to receiving CVL to all vports.
4214 */
4215 lpfc_sli4_perform_all_vport_cvl(phba);
4216 }
James Smartda0436e2009-05-22 14:51:39 -04004217 break;
James Smart70f3c072010-12-15 17:57:33 -05004218 case LPFC_FIP_EVENT_TYPE_CVL:
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 Smart6669f9b2009-10-02 15:16:45 -04004221 "2718 Clear Virtual Link Received for VPI 0x%x"
James Smart70f3c072010-12-15 17:57:33 -05004222 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart6d368e52011-05-24 11:44:12 -04004223
James Smart6669f9b2009-10-02 15:16:45 -04004224 vport = lpfc_find_vport_by_vpid(phba,
James Smart5248a742011-07-22 18:37:06 -04004225 acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004226 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004227 if (!ndlp)
4228 break;
James Smart695a8142010-01-26 23:08:03 -05004229 active_vlink_present = 0;
4230
4231 vports = lpfc_create_vport_work_array(phba);
4232 if (vports) {
4233 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
4234 i++) {
4235 if ((!(vports[i]->fc_flag &
4236 FC_VPORT_CVL_RCVD)) &&
4237 (vports[i]->port_state > LPFC_FDISC)) {
4238 active_vlink_present = 1;
4239 break;
4240 }
4241 }
4242 lpfc_destroy_vport_work_array(phba, vports);
4243 }
4244
4245 if (active_vlink_present) {
4246 /*
4247 * If there are other active VLinks present,
4248 * re-instantiate the Vlink using FDISC.
4249 */
James Smart256ec0d2013-04-17 20:14:58 -04004250 mod_timer(&ndlp->nlp_delayfunc,
4251 jiffies + msecs_to_jiffies(1000));
James Smartfc2b9892010-02-26 14:15:29 -05004252 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004253 spin_lock_irq(shost->host_lock);
4254 ndlp->nlp_flag |= NLP_DELAY_TMO;
4255 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05004256 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
4257 vport->port_state = LPFC_FDISC;
4258 } else {
James Smartecfd03c2010-02-12 14:41:27 -05004259 /*
4260 * Otherwise, we request port to rediscover
4261 * the entire FCF table for a fast recovery
4262 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05004263 * is no longer valid if we are not already
4264 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05004265 */
James Smartfc2b9892010-02-26 14:15:29 -05004266 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004267 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05004268 spin_unlock_irq(&phba->hbalock);
4269 break;
4270 }
4271 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05004272 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004273 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004274 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4275 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004276 "2773 Start FCF failover per CVL, "
James Smart70f3c072010-12-15 17:57:33 -05004277 "evt_tag:x%x\n", acqe_fip->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05004278 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05004279 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004280 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4281 LOG_DISCOVERY,
4282 "2774 Issue FCF rediscover "
4283 "mabilbox command failed, "
4284 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05004285 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004286 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004287 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05004288 /*
4289 * Last resort will be re-try on the
4290 * the current registered FCF entry.
4291 */
4292 lpfc_retry_pport_discovery(phba);
James Smart38b92ef2010-08-04 16:11:39 -04004293 } else
4294 /*
4295 * Reset FCF roundrobin bmask for new
4296 * discovery.
4297 */
James Smart7d791df2011-07-22 18:37:52 -04004298 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart6669f9b2009-10-02 15:16:45 -04004299 }
4300 break;
James Smartda0436e2009-05-22 14:51:39 -04004301 default:
4302 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4303 "0288 Unknown FCoE event type 0x%x event tag "
James Smart70f3c072010-12-15 17:57:33 -05004304 "0x%x\n", event_type, acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04004305 break;
4306 }
4307}
4308
4309/**
4310 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
4311 * @phba: pointer to lpfc hba data structure.
4312 * @acqe_link: pointer to the async dcbx completion queue entry.
4313 *
4314 * This routine is to handle the SLI4 asynchronous dcbx event.
4315 **/
4316static void
4317lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
4318 struct lpfc_acqe_dcbx *acqe_dcbx)
4319{
James Smart4d9ab992009-10-02 15:16:39 -04004320 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004321 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4322 "0290 The SLI4 DCBX asynchronous event is not "
4323 "handled yet\n");
4324}
4325
4326/**
James Smartb19a0612010-04-06 14:48:51 -04004327 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
4328 * @phba: pointer to lpfc hba data structure.
4329 * @acqe_link: pointer to the async grp5 completion queue entry.
4330 *
4331 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
4332 * is an asynchronous notified of a logical link speed change. The Port
4333 * reports the logical link speed in units of 10Mbps.
4334 **/
4335static void
4336lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
4337 struct lpfc_acqe_grp5 *acqe_grp5)
4338{
4339 uint16_t prev_ll_spd;
4340
4341 phba->fc_eventTag = acqe_grp5->event_tag;
4342 phba->fcoe_eventtag = acqe_grp5->event_tag;
4343 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
4344 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04004345 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
James Smartb19a0612010-04-06 14:48:51 -04004346 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4347 "2789 GRP5 Async Event: Updating logical link speed "
James Smart8b68cd52012-09-29 11:32:37 -04004348 "from %dMbps to %dMbps\n", prev_ll_spd,
4349 phba->sli4_hba.link_state.logical_speed);
James Smartb19a0612010-04-06 14:48:51 -04004350}
4351
4352/**
James Smartda0436e2009-05-22 14:51:39 -04004353 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
4354 * @phba: pointer to lpfc hba data structure.
4355 *
4356 * This routine is invoked by the worker thread to process all the pending
4357 * SLI4 asynchronous events.
4358 **/
4359void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
4360{
4361 struct lpfc_cq_event *cq_event;
4362
4363 /* First, declare the async event has been handled */
4364 spin_lock_irq(&phba->hbalock);
4365 phba->hba_flag &= ~ASYNC_EVENT;
4366 spin_unlock_irq(&phba->hbalock);
4367 /* Now, handle all the async events */
4368 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
4369 /* Get the first event from the head of the event queue */
4370 spin_lock_irq(&phba->hbalock);
4371 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
4372 cq_event, struct lpfc_cq_event, list);
4373 spin_unlock_irq(&phba->hbalock);
4374 /* Process the asynchronous event */
4375 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
4376 case LPFC_TRAILER_CODE_LINK:
4377 lpfc_sli4_async_link_evt(phba,
4378 &cq_event->cqe.acqe_link);
4379 break;
4380 case LPFC_TRAILER_CODE_FCOE:
James Smart70f3c072010-12-15 17:57:33 -05004381 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004382 break;
4383 case LPFC_TRAILER_CODE_DCBX:
4384 lpfc_sli4_async_dcbx_evt(phba,
4385 &cq_event->cqe.acqe_dcbx);
4386 break;
James Smartb19a0612010-04-06 14:48:51 -04004387 case LPFC_TRAILER_CODE_GRP5:
4388 lpfc_sli4_async_grp5_evt(phba,
4389 &cq_event->cqe.acqe_grp5);
4390 break;
James Smart70f3c072010-12-15 17:57:33 -05004391 case LPFC_TRAILER_CODE_FC:
4392 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
4393 break;
4394 case LPFC_TRAILER_CODE_SLI:
4395 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
4396 break;
James Smartda0436e2009-05-22 14:51:39 -04004397 default:
4398 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4399 "1804 Invalid asynchrous event code: "
4400 "x%x\n", bf_get(lpfc_trailer_code,
4401 &cq_event->cqe.mcqe_cmpl));
4402 break;
4403 }
4404 /* Free the completion event processed to the free pool */
4405 lpfc_sli4_cq_event_release(phba, cq_event);
4406 }
4407}
4408
4409/**
James Smartecfd03c2010-02-12 14:41:27 -05004410 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
4411 * @phba: pointer to lpfc hba data structure.
4412 *
4413 * This routine is invoked by the worker thread to process FCF table
4414 * rediscovery pending completion event.
4415 **/
4416void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
4417{
4418 int rc;
4419
4420 spin_lock_irq(&phba->hbalock);
4421 /* Clear FCF rediscovery timeout event */
4422 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
4423 /* Clear driver fast failover FCF record flag */
4424 phba->fcf.failover_rec.flag = 0;
4425 /* Set state for FCF fast failover */
4426 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
4427 spin_unlock_irq(&phba->hbalock);
4428
4429 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05004430 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004431 "2777 Start post-quiescent FCF table scan\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05004432 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05004433 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05004434 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4435 "2747 Issue FCF scan read FCF mailbox "
4436 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05004437}
4438
4439/**
James Smartda0436e2009-05-22 14:51:39 -04004440 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
4441 * @phba: pointer to lpfc hba data structure.
4442 * @dev_grp: The HBA PCI-Device group number.
4443 *
4444 * This routine is invoked to set up the per HBA PCI-Device group function
4445 * API jump table entries.
4446 *
4447 * Return: 0 if success, otherwise -ENODEV
4448 **/
4449int
4450lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05004451{
4452 int rc;
4453
James Smartda0436e2009-05-22 14:51:39 -04004454 /* Set up lpfc PCI-device group */
4455 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05004456
James Smartda0436e2009-05-22 14:51:39 -04004457 /* The LPFC_PCI_DEV_OC uses SLI4 */
4458 if (dev_grp == LPFC_PCI_DEV_OC)
4459 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05004460
James Smartda0436e2009-05-22 14:51:39 -04004461 /* Set up device INIT API function jump table */
4462 rc = lpfc_init_api_table_setup(phba, dev_grp);
4463 if (rc)
4464 return -ENODEV;
4465 /* Set up SCSI API function jump table */
4466 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
4467 if (rc)
4468 return -ENODEV;
4469 /* Set up SLI API function jump table */
4470 rc = lpfc_sli_api_table_setup(phba, dev_grp);
4471 if (rc)
4472 return -ENODEV;
4473 /* Set up MBOX API function jump table */
4474 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
4475 if (rc)
4476 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004477
James Smartda0436e2009-05-22 14:51:39 -04004478 return 0;
James Smart5b75da22008-12-04 22:39:35 -05004479}
4480
4481/**
James Smart3621a712009-04-06 18:47:14 -04004482 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05004483 * @phba: pointer to lpfc hba data structure.
4484 * @intr_mode: active interrupt mode adopted.
4485 *
4486 * This routine it invoked to log the currently used active interrupt mode
4487 * to the device.
James Smart3772a992009-05-22 14:50:54 -04004488 **/
4489static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05004490{
4491 switch (intr_mode) {
4492 case 0:
4493 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4494 "0470 Enable INTx interrupt mode.\n");
4495 break;
4496 case 1:
4497 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4498 "0481 Enabled MSI interrupt mode.\n");
4499 break;
4500 case 2:
4501 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4502 "0480 Enabled MSI-X interrupt mode.\n");
4503 break;
4504 default:
4505 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4506 "0482 Illegal interrupt mode.\n");
4507 break;
4508 }
4509 return;
4510}
4511
James Smart5b75da22008-12-04 22:39:35 -05004512/**
James Smart3772a992009-05-22 14:50:54 -04004513 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05004514 * @phba: pointer to lpfc hba data structure.
4515 *
James Smart3772a992009-05-22 14:50:54 -04004516 * This routine is invoked to enable the PCI device that is common to all
4517 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004518 *
4519 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004520 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004521 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05004522 **/
James Smart3772a992009-05-22 14:50:54 -04004523static int
4524lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004525{
James Smart3772a992009-05-22 14:50:54 -04004526 struct pci_dev *pdev;
James Smart079b5c92011-08-21 21:48:49 -04004527 int bars = 0;
James Smart5b75da22008-12-04 22:39:35 -05004528
James Smart3772a992009-05-22 14:50:54 -04004529 /* Obtain PCI device reference */
4530 if (!phba->pcidev)
4531 goto out_error;
4532 else
4533 pdev = phba->pcidev;
4534 /* Select PCI BARs */
4535 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4536 /* Enable PCI device */
4537 if (pci_enable_device_mem(pdev))
4538 goto out_error;
4539 /* Request PCI resource for the device */
4540 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
4541 goto out_disable_device;
4542 /* Set up device as PCI master and save state for EEH */
4543 pci_set_master(pdev);
4544 pci_try_set_mwi(pdev);
4545 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05004546
James Smart05580562011-05-24 11:40:48 -04004547 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
4548 if (pci_find_capability(pdev, PCI_CAP_ID_EXP))
4549 pdev->needs_freset = 1;
4550
James Smart3772a992009-05-22 14:50:54 -04004551 return 0;
James Smart5b75da22008-12-04 22:39:35 -05004552
James Smart3772a992009-05-22 14:50:54 -04004553out_disable_device:
4554 pci_disable_device(pdev);
4555out_error:
James Smart079b5c92011-08-21 21:48:49 -04004556 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4557 "1401 Failed to enable pci device, bars:x%x\n", bars);
James Smart3772a992009-05-22 14:50:54 -04004558 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004559}
4560
4561/**
James Smart3772a992009-05-22 14:50:54 -04004562 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05004563 * @phba: pointer to lpfc hba data structure.
4564 *
James Smart3772a992009-05-22 14:50:54 -04004565 * This routine is invoked to disable the PCI device that is common to all
4566 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004567 **/
4568static void
James Smart3772a992009-05-22 14:50:54 -04004569lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004570{
James Smart3772a992009-05-22 14:50:54 -04004571 struct pci_dev *pdev;
4572 int bars;
James Smart5b75da22008-12-04 22:39:35 -05004573
James Smart3772a992009-05-22 14:50:54 -04004574 /* Obtain PCI device reference */
4575 if (!phba->pcidev)
4576 return;
4577 else
4578 pdev = phba->pcidev;
4579 /* Select PCI BARs */
4580 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4581 /* Release PCI resource and disable PCI device */
4582 pci_release_selected_regions(pdev, bars);
4583 pci_disable_device(pdev);
James Smart5b75da22008-12-04 22:39:35 -05004584
4585 return;
4586}
4587
4588/**
James Smart3772a992009-05-22 14:50:54 -04004589 * lpfc_reset_hba - Reset a hba
4590 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04004591 *
James Smart3772a992009-05-22 14:50:54 -04004592 * This routine is invoked to reset a hba device. It brings the HBA
4593 * offline, performs a board restart, and then brings the board back
4594 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
4595 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04004596 **/
James Smart3772a992009-05-22 14:50:54 -04004597void
4598lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004599{
James Smart3772a992009-05-22 14:50:54 -04004600 /* If resets are disabled then set error state and return. */
4601 if (!phba->cfg_enable_hba_reset) {
4602 phba->link_state = LPFC_HBA_ERROR;
4603 return;
4604 }
James Smart618a5232012-06-12 13:54:36 -04004605 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3772a992009-05-22 14:50:54 -04004606 lpfc_offline(phba);
4607 lpfc_sli_brdrestart(phba);
4608 lpfc_online(phba);
4609 lpfc_unblock_mgmt_io(phba);
4610}
dea31012005-04-17 16:05:31 -05004611
James Smart3772a992009-05-22 14:50:54 -04004612/**
James Smart0a96e972011-07-22 18:37:28 -04004613 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
4614 * @phba: pointer to lpfc hba data structure.
4615 *
4616 * This function enables the PCI SR-IOV virtual functions to a physical
4617 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4618 * enable the number of virtual functions to the physical function. As
4619 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4620 * API call does not considered as an error condition for most of the device.
4621 **/
4622uint16_t
4623lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
4624{
4625 struct pci_dev *pdev = phba->pcidev;
4626 uint16_t nr_virtfn;
4627 int pos;
4628
James Smart0a96e972011-07-22 18:37:28 -04004629 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
4630 if (pos == 0)
4631 return 0;
4632
4633 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
4634 return nr_virtfn;
4635}
4636
4637/**
James Smart912e3ac2011-05-24 11:42:11 -04004638 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
4639 * @phba: pointer to lpfc hba data structure.
4640 * @nr_vfn: number of virtual functions to be enabled.
4641 *
4642 * This function enables the PCI SR-IOV virtual functions to a physical
4643 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4644 * enable the number of virtual functions to the physical function. As
4645 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4646 * API call does not considered as an error condition for most of the device.
4647 **/
4648int
4649lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
4650{
4651 struct pci_dev *pdev = phba->pcidev;
James Smart0a96e972011-07-22 18:37:28 -04004652 uint16_t max_nr_vfn;
James Smart912e3ac2011-05-24 11:42:11 -04004653 int rc;
4654
James Smart0a96e972011-07-22 18:37:28 -04004655 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
4656 if (nr_vfn > max_nr_vfn) {
4657 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4658 "3057 Requested vfs (%d) greater than "
4659 "supported vfs (%d)", nr_vfn, max_nr_vfn);
4660 return -EINVAL;
4661 }
4662
James Smart912e3ac2011-05-24 11:42:11 -04004663 rc = pci_enable_sriov(pdev, nr_vfn);
4664 if (rc) {
4665 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4666 "2806 Failed to enable sriov on this device "
4667 "with vfn number nr_vf:%d, rc:%d\n",
4668 nr_vfn, rc);
4669 } else
4670 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4671 "2807 Successful enable sriov on this device "
4672 "with vfn number nr_vf:%d\n", nr_vfn);
4673 return rc;
4674}
4675
4676/**
James Smart3772a992009-05-22 14:50:54 -04004677 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
4678 * @phba: pointer to lpfc hba data structure.
4679 *
4680 * This routine is invoked to set up the driver internal resources specific to
4681 * support the SLI-3 HBA device it attached to.
4682 *
4683 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004684 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004685 * other values - error
4686 **/
4687static int
4688lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
4689{
4690 struct lpfc_sli *psli;
James Smart912e3ac2011-05-24 11:42:11 -04004691 int rc;
dea31012005-04-17 16:05:31 -05004692
James Smart3772a992009-05-22 14:50:54 -04004693 /*
4694 * Initialize timers used by driver
4695 */
dea31012005-04-17 16:05:31 -05004696
James Smart3772a992009-05-22 14:50:54 -04004697 /* Heartbeat timer */
4698 init_timer(&phba->hb_tmofunc);
4699 phba->hb_tmofunc.function = lpfc_hb_timeout;
4700 phba->hb_tmofunc.data = (unsigned long)phba;
4701
4702 psli = &phba->sli;
4703 /* MBOX heartbeat timer */
4704 init_timer(&psli->mbox_tmo);
4705 psli->mbox_tmo.function = lpfc_mbox_timeout;
4706 psli->mbox_tmo.data = (unsigned long) phba;
4707 /* FCP polling mode timer */
4708 init_timer(&phba->fcp_poll_timer);
4709 phba->fcp_poll_timer.function = lpfc_poll_timeout;
4710 phba->fcp_poll_timer.data = (unsigned long) phba;
4711 /* Fabric block timer */
4712 init_timer(&phba->fabric_block_timer);
4713 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4714 phba->fabric_block_timer.data = (unsigned long) phba;
4715 /* EA polling mode timer */
4716 init_timer(&phba->eratt_poll);
4717 phba->eratt_poll.function = lpfc_poll_eratt;
4718 phba->eratt_poll.data = (unsigned long) phba;
4719
4720 /* Host attention work mask setup */
4721 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
4722 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
4723
4724 /* Get all the module params for configuring this host */
4725 lpfc_get_cfgparam(phba);
James Smart49198b32010-04-06 15:04:33 -04004726 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
4727 phba->menlo_flag |= HBA_MENLO_SUPPORT;
4728 /* check for menlo minimum sg count */
4729 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
4730 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
4731 }
4732
James Smart2a76a282012-08-03 12:35:54 -04004733 if (!phba->sli.ring)
4734 phba->sli.ring = (struct lpfc_sli_ring *)
4735 kzalloc(LPFC_SLI3_MAX_RING *
4736 sizeof(struct lpfc_sli_ring), GFP_KERNEL);
4737 if (!phba->sli.ring)
4738 return -ENOMEM;
4739
James Smart3772a992009-05-22 14:50:54 -04004740 /*
James Smart96f70772013-04-17 20:16:15 -04004741 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smart3772a992009-05-22 14:50:54 -04004742 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart3772a992009-05-22 14:50:54 -04004743 */
James Smart3772a992009-05-22 14:50:54 -04004744
James Smart96f70772013-04-17 20:16:15 -04004745 /* Initialize the host templates the configured values. */
James Smart3772a992009-05-22 14:50:54 -04004746 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4747 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4748
James Smart96f70772013-04-17 20:16:15 -04004749 /* There are going to be 2 reserved BDEs: 1 FCP cmnd + 1 FCP rsp */
4750 if (phba->cfg_enable_bg) {
4751 /*
4752 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
4753 * the FCP rsp, and a BDE for each. Sice we have no control
4754 * over how many protection data segments the SCSI Layer
4755 * will hand us (ie: there could be one for every block
4756 * in the IO), we just allocate enough BDEs to accomidate
4757 * our max amount and we need to limit lpfc_sg_seg_cnt to
4758 * minimize the risk of running out.
4759 */
4760 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4761 sizeof(struct fcp_rsp) +
4762 (LPFC_MAX_SG_SEG_CNT * sizeof(struct ulp_bde64));
4763
4764 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SEG_CNT_DIF)
4765 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT_DIF;
4766
4767 /* Total BDEs in BPL for scsi_sg_list and scsi_sg_prot_list */
4768 phba->cfg_total_seg_cnt = LPFC_MAX_SG_SEG_CNT;
4769 } else {
4770 /*
4771 * The scsi_buf for a regular I/O will hold the FCP cmnd,
4772 * the FCP rsp, a BDE for each, and a BDE for up to
4773 * cfg_sg_seg_cnt data segments.
4774 */
4775 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4776 sizeof(struct fcp_rsp) +
4777 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
4778
4779 /* Total BDEs in BPL for scsi_sg_list */
4780 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
4781 }
4782
4783 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
4784 "9088 sg_tablesize:%d dmabuf_size:%d total_bde:%d\n",
4785 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
4786 phba->cfg_total_seg_cnt);
4787
James Smart3772a992009-05-22 14:50:54 -04004788 phba->max_vpi = LPFC_MAX_VPI;
4789 /* This will be set to correct value after config_port mbox */
4790 phba->max_vports = 0;
4791
4792 /*
4793 * Initialize the SLI Layer to run with lpfc HBAs.
4794 */
4795 lpfc_sli_setup(phba);
4796 lpfc_sli_queue_setup(phba);
4797
4798 /* Allocate device driver memory */
4799 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
4800 return -ENOMEM;
4801
James Smart912e3ac2011-05-24 11:42:11 -04004802 /*
4803 * Enable sr-iov virtual functions if supported and configured
4804 * through the module parameter.
4805 */
4806 if (phba->cfg_sriov_nr_virtfn > 0) {
4807 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
4808 phba->cfg_sriov_nr_virtfn);
4809 if (rc) {
4810 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4811 "2808 Requested number of SR-IOV "
4812 "virtual functions (%d) is not "
4813 "supported\n",
4814 phba->cfg_sriov_nr_virtfn);
4815 phba->cfg_sriov_nr_virtfn = 0;
4816 }
4817 }
4818
James Smart3772a992009-05-22 14:50:54 -04004819 return 0;
4820}
4821
4822/**
4823 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
4824 * @phba: pointer to lpfc hba data structure.
4825 *
4826 * This routine is invoked to unset the driver internal resources set up
4827 * specific for supporting the SLI-3 HBA device it attached to.
4828 **/
4829static void
4830lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
4831{
4832 /* Free device driver memory allocated */
4833 lpfc_mem_free_all(phba);
4834
4835 return;
4836}
4837
4838/**
James Smartda0436e2009-05-22 14:51:39 -04004839 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
4840 * @phba: pointer to lpfc hba data structure.
4841 *
4842 * This routine is invoked to set up the driver internal resources specific to
4843 * support the SLI-4 HBA device it attached to.
4844 *
4845 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004846 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004847 * other values - error
4848 **/
4849static int
4850lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
4851{
James Smart7bb03bb2013-04-17 20:19:16 -04004852 struct lpfc_vector_map_info *cpup;
James Smartda0436e2009-05-22 14:51:39 -04004853 struct lpfc_sli *psli;
James Smart28baac72010-02-12 14:42:03 -05004854 LPFC_MBOXQ_t *mboxq;
James Smart96f70772013-04-17 20:16:15 -04004855 int rc, i, hbq_count, max_buf_size;
James Smart28baac72010-02-12 14:42:03 -05004856 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
4857 struct lpfc_mqe *mqe;
James Smart09294d42013-04-17 20:16:05 -04004858 int longs;
James Smartda0436e2009-05-22 14:51:39 -04004859
4860 /* Before proceed, wait for POST done and device ready */
4861 rc = lpfc_sli4_post_status_check(phba);
4862 if (rc)
4863 return -ENODEV;
4864
4865 /*
4866 * Initialize timers used by driver
4867 */
4868
4869 /* Heartbeat timer */
4870 init_timer(&phba->hb_tmofunc);
4871 phba->hb_tmofunc.function = lpfc_hb_timeout;
4872 phba->hb_tmofunc.data = (unsigned long)phba;
James Smart19ca7602010-11-20 23:11:55 -05004873 init_timer(&phba->rrq_tmr);
4874 phba->rrq_tmr.function = lpfc_rrq_timeout;
4875 phba->rrq_tmr.data = (unsigned long)phba;
James Smartda0436e2009-05-22 14:51:39 -04004876
4877 psli = &phba->sli;
4878 /* MBOX heartbeat timer */
4879 init_timer(&psli->mbox_tmo);
4880 psli->mbox_tmo.function = lpfc_mbox_timeout;
4881 psli->mbox_tmo.data = (unsigned long) phba;
4882 /* Fabric block timer */
4883 init_timer(&phba->fabric_block_timer);
4884 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4885 phba->fabric_block_timer.data = (unsigned long) phba;
4886 /* EA polling mode timer */
4887 init_timer(&phba->eratt_poll);
4888 phba->eratt_poll.function = lpfc_poll_eratt;
4889 phba->eratt_poll.data = (unsigned long) phba;
James Smartecfd03c2010-02-12 14:41:27 -05004890 /* FCF rediscover timer */
4891 init_timer(&phba->fcf.redisc_wait);
4892 phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
4893 phba->fcf.redisc_wait.data = (unsigned long)phba;
4894
James Smartda0436e2009-05-22 14:51:39 -04004895 /*
James Smart7ad20aa2011-05-24 11:44:28 -04004896 * Control structure for handling external multi-buffer mailbox
4897 * command pass-through.
4898 */
4899 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
4900 sizeof(struct lpfc_mbox_ext_buf_ctx));
4901 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
4902
4903 /*
James Smartda0436e2009-05-22 14:51:39 -04004904 * We need to do a READ_CONFIG mailbox command here before
4905 * calling lpfc_get_cfgparam. For VFs this will report the
4906 * MAX_XRI, MAX_VPI, MAX_RPI, MAX_IOCB, and MAX_VFI settings.
4907 * All of the resources allocated
4908 * for this Port are tied to these values.
4909 */
4910 /* Get all the module params for configuring this host */
4911 lpfc_get_cfgparam(phba);
4912 phba->max_vpi = LPFC_MAX_VPI;
James Smart67d12732012-08-03 12:36:13 -04004913
James Smartda0436e2009-05-22 14:51:39 -04004914 /* This will be set to correct value after the read_config mbox */
4915 phba->max_vports = 0;
4916
4917 /* Program the default value of vlan_id and fc_map */
4918 phba->valid_vlan = 0;
4919 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4920 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4921 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4922
4923 /*
James Smart2a76a282012-08-03 12:35:54 -04004924 * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
4925 * we will associate a new ring, for each FCP fastpath EQ/CQ/WQ tuple.
4926 */
4927 if (!phba->sli.ring)
4928 phba->sli.ring = kzalloc(
James Smart67d12732012-08-03 12:36:13 -04004929 (LPFC_SLI3_MAX_RING + phba->cfg_fcp_io_channel) *
James Smart2a76a282012-08-03 12:35:54 -04004930 sizeof(struct lpfc_sli_ring), GFP_KERNEL);
4931 if (!phba->sli.ring)
4932 return -ENOMEM;
James Smart085c6472010-11-20 23:11:37 -05004933
James Smart09294d42013-04-17 20:16:05 -04004934 /*
4935 * It doesn't matter what family our adapter is in, we are
4936 * limited to 2 Pages, 512 SGEs, for our SGL.
4937 * There are going to be 2 reserved SGEs: 1 FCP cmnd + 1 FCP rsp
4938 */
4939 max_buf_size = (2 * SLI4_PAGE_SIZE);
4940 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SGL_SEG_CNT - 2)
4941 phba->cfg_sg_seg_cnt = LPFC_MAX_SGL_SEG_CNT - 2;
James Smart09294d42013-04-17 20:16:05 -04004942
James Smartda0436e2009-05-22 14:51:39 -04004943 /*
James Smart96f70772013-04-17 20:16:15 -04004944 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smartda0436e2009-05-22 14:51:39 -04004945 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart28baac72010-02-12 14:42:03 -05004946 */
James Smart085c6472010-11-20 23:11:37 -05004947
James Smart96f70772013-04-17 20:16:15 -04004948 if (phba->cfg_enable_bg) {
4949 /*
4950 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
4951 * the FCP rsp, and a SGE for each. Sice we have no control
4952 * over how many protection data segments the SCSI Layer
4953 * will hand us (ie: there could be one for every block
4954 * in the IO), we just allocate enough SGEs to accomidate
4955 * our max amount and we need to limit lpfc_sg_seg_cnt to
4956 * minimize the risk of running out.
4957 */
4958 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4959 sizeof(struct fcp_rsp) + max_buf_size;
4960
4961 /* Total SGEs for scsi_sg_list and scsi_sg_prot_list */
4962 phba->cfg_total_seg_cnt = LPFC_MAX_SGL_SEG_CNT;
4963
4964 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SLI4_SEG_CNT_DIF)
4965 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SLI4_SEG_CNT_DIF;
4966 } else {
4967 /*
4968 * The scsi_buf for a regular I/O will hold the FCP cmnd,
4969 * the FCP rsp, a SGE for each, and a SGE for up to
4970 * cfg_sg_seg_cnt data segments.
4971 */
4972 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4973 sizeof(struct fcp_rsp) +
4974 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge));
4975
4976 /* Total SGEs for scsi_sg_list */
4977 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
4978 /*
4979 * NOTE: if (phba->cfg_sg_seg_cnt + 2) <= 256 we only need
4980 * to post 1 page for the SGL.
4981 */
James Smart085c6472010-11-20 23:11:37 -05004982 }
James Smartacd68592012-01-18 16:25:09 -05004983
James Smart96f70772013-04-17 20:16:15 -04004984 /* Initialize the host templates with the updated values. */
4985 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4986 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4987
4988 if (phba->cfg_sg_dma_buf_size <= LPFC_MIN_SG_SLI4_BUF_SZ)
4989 phba->cfg_sg_dma_buf_size = LPFC_MIN_SG_SLI4_BUF_SZ;
4990 else
4991 phba->cfg_sg_dma_buf_size =
4992 SLI4_PAGE_ALIGN(phba->cfg_sg_dma_buf_size);
4993
4994 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
4995 "9087 sg_tablesize:%d dmabuf_size:%d total_sge:%d\n",
4996 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
4997 phba->cfg_total_seg_cnt);
James Smartda0436e2009-05-22 14:51:39 -04004998
4999 /* Initialize buffer queue management fields */
5000 hbq_count = lpfc_sli_hbq_count();
5001 for (i = 0; i < hbq_count; ++i)
5002 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
5003 INIT_LIST_HEAD(&phba->rb_pend_list);
5004 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
5005 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
5006
5007 /*
5008 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
5009 */
5010 /* Initialize the Abort scsi buffer list used by driver */
5011 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
5012 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
5013 /* This abort list used by worker thread */
5014 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
5015
5016 /*
James Smart6d368e52011-05-24 11:44:12 -04005017 * Initialize driver internal slow-path work queues
James Smartda0436e2009-05-22 14:51:39 -04005018 */
5019
5020 /* Driver internel slow-path CQ Event pool */
5021 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
5022 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04005023 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04005024 /* Asynchronous event CQ Event work queue list */
5025 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
5026 /* Fast-path XRI aborted CQ Event work queue list */
5027 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
5028 /* Slow-path XRI aborted CQ Event work queue list */
5029 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
5030 /* Receive queue CQ Event work queue list */
5031 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
5032
James Smart6d368e52011-05-24 11:44:12 -04005033 /* Initialize extent block lists. */
5034 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
5035 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
5036 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
5037 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
5038
James Smartda0436e2009-05-22 14:51:39 -04005039 /* Initialize the driver internal SLI layer lists. */
5040 lpfc_sli_setup(phba);
5041 lpfc_sli_queue_setup(phba);
5042
5043 /* Allocate device driver memory */
5044 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
5045 if (rc)
5046 return -ENOMEM;
5047
James Smart2fcee4b2010-12-15 17:57:46 -05005048 /* IF Type 2 ports get initialized now. */
5049 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
5050 LPFC_SLI_INTF_IF_TYPE_2) {
5051 rc = lpfc_pci_function_reset(phba);
5052 if (unlikely(rc))
5053 return -ENODEV;
5054 }
5055
James Smartda0436e2009-05-22 14:51:39 -04005056 /* Create the bootstrap mailbox command */
5057 rc = lpfc_create_bootstrap_mbox(phba);
5058 if (unlikely(rc))
5059 goto out_free_mem;
5060
5061 /* Set up the host's endian order with the device. */
5062 rc = lpfc_setup_endian_order(phba);
5063 if (unlikely(rc))
5064 goto out_free_bsmbx;
5065
5066 /* Set up the hba's configuration parameters. */
5067 rc = lpfc_sli4_read_config(phba);
5068 if (unlikely(rc))
5069 goto out_free_bsmbx;
5070
James Smart2fcee4b2010-12-15 17:57:46 -05005071 /* IF Type 0 ports get initialized now. */
5072 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
5073 LPFC_SLI_INTF_IF_TYPE_0) {
5074 rc = lpfc_pci_function_reset(phba);
5075 if (unlikely(rc))
5076 goto out_free_bsmbx;
5077 }
James Smartda0436e2009-05-22 14:51:39 -04005078
James Smartcb5172e2010-03-15 11:25:07 -04005079 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
5080 GFP_KERNEL);
5081 if (!mboxq) {
5082 rc = -ENOMEM;
5083 goto out_free_bsmbx;
5084 }
5085
James Smartfedd3b72011-02-16 12:39:24 -05005086 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
James Smartcb5172e2010-03-15 11:25:07 -04005087 lpfc_supported_pages(mboxq);
5088 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smartfedd3b72011-02-16 12:39:24 -05005089 if (!rc) {
5090 mqe = &mboxq->u.mqe;
5091 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
5092 LPFC_MAX_SUPPORTED_PAGES);
5093 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
5094 switch (pn_page[i]) {
5095 case LPFC_SLI4_PARAMETERS:
5096 phba->sli4_hba.pc_sli4_params.supported = 1;
5097 break;
5098 default:
5099 break;
5100 }
5101 }
5102 /* Read the port's SLI4 Parameters capabilities if supported. */
5103 if (phba->sli4_hba.pc_sli4_params.supported)
5104 rc = lpfc_pc_sli4_params_get(phba, mboxq);
5105 if (rc) {
5106 mempool_free(mboxq, phba->mbox_mem_pool);
5107 rc = -EIO;
5108 goto out_free_bsmbx;
James Smartcb5172e2010-03-15 11:25:07 -04005109 }
5110 }
James Smartfedd3b72011-02-16 12:39:24 -05005111 /*
5112 * Get sli4 parameters that override parameters from Port capabilities.
James Smart6d368e52011-05-24 11:44:12 -04005113 * If this call fails, it isn't critical unless the SLI4 parameters come
5114 * back in conflict.
James Smartfedd3b72011-02-16 12:39:24 -05005115 */
James Smart6d368e52011-05-24 11:44:12 -04005116 rc = lpfc_get_sli4_parameters(phba, mboxq);
5117 if (rc) {
5118 if (phba->sli4_hba.extents_in_use &&
5119 phba->sli4_hba.rpi_hdrs_in_use) {
5120 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5121 "2999 Unsupported SLI4 Parameters "
5122 "Extents and RPI headers enabled.\n");
5123 goto out_free_bsmbx;
5124 }
5125 }
James Smartcb5172e2010-03-15 11:25:07 -04005126 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart5350d872011-10-10 21:33:49 -04005127 /* Verify all the SLI4 queues */
5128 rc = lpfc_sli4_queue_verify(phba);
James Smartda0436e2009-05-22 14:51:39 -04005129 if (rc)
5130 goto out_free_bsmbx;
5131
5132 /* Create driver internal CQE event pool */
5133 rc = lpfc_sli4_cq_event_pool_create(phba);
5134 if (rc)
James Smart5350d872011-10-10 21:33:49 -04005135 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04005136
James Smart8a9d2e82012-05-09 21:16:12 -04005137 /* Initialize sgl lists per host */
5138 lpfc_init_sgl_list(phba);
5139
5140 /* Allocate and initialize active sgl array */
James Smartda0436e2009-05-22 14:51:39 -04005141 rc = lpfc_init_active_sgl_array(phba);
5142 if (rc) {
5143 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5144 "1430 Failed to initialize sgl list.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04005145 goto out_destroy_cq_event_pool;
James Smartda0436e2009-05-22 14:51:39 -04005146 }
James Smartda0436e2009-05-22 14:51:39 -04005147 rc = lpfc_sli4_init_rpi_hdrs(phba);
5148 if (rc) {
5149 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5150 "1432 Failed to initialize rpi headers.\n");
5151 goto out_free_active_sgl;
5152 }
5153
James Smarta93ff372010-10-22 11:06:08 -04005154 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
James Smart0c9ab6f2010-02-26 14:15:57 -05005155 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
5156 phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
5157 GFP_KERNEL);
5158 if (!phba->fcf.fcf_rr_bmask) {
5159 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5160 "2759 Failed allocate memory for FCF round "
5161 "robin failover bmask\n");
James Smart05580562011-05-24 11:40:48 -04005162 rc = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -05005163 goto out_remove_rpi_hdrs;
5164 }
5165
James Smart67d12732012-08-03 12:36:13 -04005166 phba->sli4_hba.fcp_eq_hdl =
5167 kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
5168 phba->cfg_fcp_io_channel), GFP_KERNEL);
5169 if (!phba->sli4_hba.fcp_eq_hdl) {
5170 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5171 "2572 Failed allocate memory for "
5172 "fast-path per-EQ handle array\n");
5173 rc = -ENOMEM;
5174 goto out_free_fcf_rr_bmask;
James Smartda0436e2009-05-22 14:51:39 -04005175 }
5176
5177 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
James Smart82c3e9b2012-09-29 11:29:50 -04005178 phba->cfg_fcp_io_channel), GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005179 if (!phba->sli4_hba.msix_entries) {
5180 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5181 "2573 Failed allocate memory for msi-x "
5182 "interrupt vector entries\n");
James Smart05580562011-05-24 11:40:48 -04005183 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04005184 goto out_free_fcp_eq_hdl;
5185 }
5186
James Smart7bb03bb2013-04-17 20:19:16 -04005187 phba->sli4_hba.cpu_map = kzalloc((sizeof(struct lpfc_vector_map_info) *
5188 phba->sli4_hba.num_present_cpu),
5189 GFP_KERNEL);
5190 if (!phba->sli4_hba.cpu_map) {
5191 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5192 "3327 Failed allocate memory for msi-x "
5193 "interrupt vector mapping\n");
5194 rc = -ENOMEM;
5195 goto out_free_msix;
5196 }
James Smartb246de12013-05-31 17:03:07 -04005197 if (lpfc_used_cpu == NULL) {
5198 lpfc_used_cpu = kzalloc((sizeof(uint16_t) * lpfc_present_cpu),
5199 GFP_KERNEL);
5200 if (!lpfc_used_cpu) {
5201 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5202 "3335 Failed allocate memory for msi-x "
5203 "interrupt vector mapping\n");
5204 kfree(phba->sli4_hba.cpu_map);
5205 rc = -ENOMEM;
5206 goto out_free_msix;
5207 }
5208 for (i = 0; i < lpfc_present_cpu; i++)
5209 lpfc_used_cpu[i] = LPFC_VECTOR_MAP_EMPTY;
5210 }
5211
James Smart7bb03bb2013-04-17 20:19:16 -04005212 /* Initialize io channels for round robin */
5213 cpup = phba->sli4_hba.cpu_map;
5214 rc = 0;
5215 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
5216 cpup->channel_id = rc;
5217 rc++;
5218 if (rc >= phba->cfg_fcp_io_channel)
5219 rc = 0;
5220 }
5221
James Smart912e3ac2011-05-24 11:42:11 -04005222 /*
5223 * Enable sr-iov virtual functions if supported and configured
5224 * through the module parameter.
5225 */
5226 if (phba->cfg_sriov_nr_virtfn > 0) {
5227 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
5228 phba->cfg_sriov_nr_virtfn);
5229 if (rc) {
5230 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5231 "3020 Requested number of SR-IOV "
5232 "virtual functions (%d) is not "
5233 "supported\n",
5234 phba->cfg_sriov_nr_virtfn);
5235 phba->cfg_sriov_nr_virtfn = 0;
5236 }
5237 }
5238
James Smart5248a742011-07-22 18:37:06 -04005239 return 0;
James Smartda0436e2009-05-22 14:51:39 -04005240
James Smart7bb03bb2013-04-17 20:19:16 -04005241out_free_msix:
5242 kfree(phba->sli4_hba.msix_entries);
James Smartda0436e2009-05-22 14:51:39 -04005243out_free_fcp_eq_hdl:
5244 kfree(phba->sli4_hba.fcp_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05005245out_free_fcf_rr_bmask:
5246 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04005247out_remove_rpi_hdrs:
5248 lpfc_sli4_remove_rpi_hdrs(phba);
5249out_free_active_sgl:
5250 lpfc_free_active_sgl(phba);
James Smartda0436e2009-05-22 14:51:39 -04005251out_destroy_cq_event_pool:
5252 lpfc_sli4_cq_event_pool_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04005253out_free_bsmbx:
5254 lpfc_destroy_bootstrap_mbox(phba);
5255out_free_mem:
5256 lpfc_mem_free(phba);
5257 return rc;
5258}
5259
5260/**
5261 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
5262 * @phba: pointer to lpfc hba data structure.
5263 *
5264 * This routine is invoked to unset the driver internal resources set up
5265 * specific for supporting the SLI-4 HBA device it attached to.
5266 **/
5267static void
5268lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
5269{
5270 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
5271
James Smart7bb03bb2013-04-17 20:19:16 -04005272 /* Free memory allocated for msi-x interrupt vector to CPU mapping */
5273 kfree(phba->sli4_hba.cpu_map);
5274 phba->sli4_hba.num_present_cpu = 0;
5275 phba->sli4_hba.num_online_cpu = 0;
5276
James Smartda0436e2009-05-22 14:51:39 -04005277 /* Free memory allocated for msi-x interrupt vector entries */
5278 kfree(phba->sli4_hba.msix_entries);
5279
5280 /* Free memory allocated for fast-path work queue handles */
5281 kfree(phba->sli4_hba.fcp_eq_hdl);
5282
5283 /* Free the allocated rpi headers. */
5284 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04005285 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04005286
James Smart0c9ab6f2010-02-26 14:15:57 -05005287 /* Free eligible FCF index bmask */
5288 kfree(phba->fcf.fcf_rr_bmask);
5289
James Smartda0436e2009-05-22 14:51:39 -04005290 /* Free the ELS sgl list */
5291 lpfc_free_active_sgl(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04005292 lpfc_free_els_sgl_list(phba);
James Smartda0436e2009-05-22 14:51:39 -04005293
James Smartda0436e2009-05-22 14:51:39 -04005294 /* Free the completion queue EQ event pool */
5295 lpfc_sli4_cq_event_release_all(phba);
5296 lpfc_sli4_cq_event_pool_destroy(phba);
5297
James Smart6d368e52011-05-24 11:44:12 -04005298 /* Release resource identifiers. */
5299 lpfc_sli4_dealloc_resource_identifiers(phba);
5300
James Smartda0436e2009-05-22 14:51:39 -04005301 /* Free the bsmbx region. */
5302 lpfc_destroy_bootstrap_mbox(phba);
5303
5304 /* Free the SLI Layer memory with SLI4 HBAs */
5305 lpfc_mem_free_all(phba);
5306
5307 /* Free the current connect table */
5308 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04005309 &phba->fcf_conn_rec_list, list) {
5310 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04005311 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04005312 }
James Smartda0436e2009-05-22 14:51:39 -04005313
5314 return;
5315}
5316
5317/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005318 * lpfc_init_api_table_setup - Set up init api function jump table
James Smart3772a992009-05-22 14:50:54 -04005319 * @phba: The hba struct for which this call is being executed.
5320 * @dev_grp: The HBA PCI-Device group number.
5321 *
5322 * This routine sets up the device INIT interface API function jump table
5323 * in @phba struct.
5324 *
5325 * Returns: 0 - success, -ENODEV - failure.
5326 **/
5327int
5328lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5329{
James Smart84d1b002010-02-12 14:42:33 -05005330 phba->lpfc_hba_init_link = lpfc_hba_init_link;
5331 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart7f860592011-03-11 16:05:52 -05005332 phba->lpfc_selective_reset = lpfc_selective_reset;
James Smart3772a992009-05-22 14:50:54 -04005333 switch (dev_grp) {
5334 case LPFC_PCI_DEV_LP:
5335 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
5336 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
5337 phba->lpfc_stop_port = lpfc_stop_port_s3;
5338 break;
James Smartda0436e2009-05-22 14:51:39 -04005339 case LPFC_PCI_DEV_OC:
5340 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
5341 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
5342 phba->lpfc_stop_port = lpfc_stop_port_s4;
5343 break;
James Smart3772a992009-05-22 14:50:54 -04005344 default:
5345 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5346 "1431 Invalid HBA PCI-device group: 0x%x\n",
5347 dev_grp);
5348 return -ENODEV;
5349 break;
5350 }
5351 return 0;
5352}
5353
5354/**
5355 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
5356 * @phba: pointer to lpfc hba data structure.
5357 *
5358 * This routine is invoked to set up the driver internal resources before the
5359 * device specific resource setup to support the HBA device it attached to.
5360 *
5361 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005362 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005363 * other values - error
5364 **/
5365static int
5366lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
5367{
5368 /*
5369 * Driver resources common to all SLI revisions
5370 */
James Smartea2151b2008-09-07 11:52:10 -04005371 atomic_set(&phba->fast_event_count, 0);
James Smart2e0fef82007-06-17 19:56:36 -05005372 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005373
James Smarte47c9092008-02-08 18:49:26 -05005374 /* Initialize ndlp management spinlock */
5375 spin_lock_init(&phba->ndlp_lock);
5376
James Smart2e0fef82007-06-17 19:56:36 -05005377 INIT_LIST_HEAD(&phba->port_list);
James Smart3772a992009-05-22 14:50:54 -04005378 INIT_LIST_HEAD(&phba->work_list);
James Smart84774a42008-08-24 21:50:06 -04005379 init_waitqueue_head(&phba->wait_4_mlo_m_q);
dea31012005-04-17 16:05:31 -05005380
James Smart3772a992009-05-22 14:50:54 -04005381 /* Initialize the wait queue head for the kernel thread */
5382 init_waitqueue_head(&phba->work_waitq);
James Smart858c9f62007-06-17 19:56:39 -05005383
James Smart3772a992009-05-22 14:50:54 -04005384 /* Initialize the scsi buffer list used by driver for scsi IO */
James Smarta40fc5f2013-04-17 20:17:40 -04005385 spin_lock_init(&phba->scsi_buf_list_get_lock);
5386 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_get);
5387 spin_lock_init(&phba->scsi_buf_list_put_lock);
5388 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
dea31012005-04-17 16:05:31 -05005389
James Smart3772a992009-05-22 14:50:54 -04005390 /* Initialize the fabric iocb list */
5391 INIT_LIST_HEAD(&phba->fabric_iocb_list);
dea31012005-04-17 16:05:31 -05005392
James Smart3772a992009-05-22 14:50:54 -04005393 /* Initialize list to save ELS buffers */
5394 INIT_LIST_HEAD(&phba->elsbuf);
dea31012005-04-17 16:05:31 -05005395
James Smart3772a992009-05-22 14:50:54 -04005396 /* Initialize FCF connection rec list */
5397 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
dea31012005-04-17 16:05:31 -05005398
James Smart3772a992009-05-22 14:50:54 -04005399 return 0;
5400}
dea31012005-04-17 16:05:31 -05005401
James Smart3772a992009-05-22 14:50:54 -04005402/**
5403 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
5404 * @phba: pointer to lpfc hba data structure.
5405 *
5406 * This routine is invoked to set up the driver internal resources after the
5407 * device specific resource setup to support the HBA device it attached to.
5408 *
5409 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005410 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005411 * other values - error
5412 **/
5413static int
5414lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
5415{
5416 int error;
5417
5418 /* Startup the kernel thread for this host adapter. */
5419 phba->worker_thread = kthread_run(lpfc_do_work, phba,
5420 "lpfc_worker_%d", phba->brd_no);
5421 if (IS_ERR(phba->worker_thread)) {
5422 error = PTR_ERR(phba->worker_thread);
5423 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005424 }
5425
James Smart3772a992009-05-22 14:50:54 -04005426 return 0;
5427}
5428
5429/**
5430 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
5431 * @phba: pointer to lpfc hba data structure.
5432 *
5433 * This routine is invoked to unset the driver internal resources set up after
5434 * the device specific resource setup for supporting the HBA device it
5435 * attached to.
5436 **/
5437static void
5438lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
5439{
5440 /* Stop kernel worker thread */
5441 kthread_stop(phba->worker_thread);
5442}
5443
5444/**
5445 * lpfc_free_iocb_list - Free iocb list.
5446 * @phba: pointer to lpfc hba data structure.
5447 *
5448 * This routine is invoked to free the driver's IOCB list and memory.
5449 **/
5450static void
5451lpfc_free_iocb_list(struct lpfc_hba *phba)
5452{
5453 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
5454
5455 spin_lock_irq(&phba->hbalock);
5456 list_for_each_entry_safe(iocbq_entry, iocbq_next,
5457 &phba->lpfc_iocb_list, list) {
5458 list_del(&iocbq_entry->list);
5459 kfree(iocbq_entry);
5460 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005461 }
James Smart3772a992009-05-22 14:50:54 -04005462 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005463
James Smart3772a992009-05-22 14:50:54 -04005464 return;
5465}
dea31012005-04-17 16:05:31 -05005466
James Smart3772a992009-05-22 14:50:54 -04005467/**
5468 * lpfc_init_iocb_list - Allocate and initialize iocb list.
5469 * @phba: pointer to lpfc hba data structure.
5470 *
5471 * This routine is invoked to allocate and initizlize the driver's IOCB
5472 * list and set up the IOCB tag array accordingly.
5473 *
5474 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005475 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005476 * other values - error
5477 **/
5478static int
5479lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
5480{
5481 struct lpfc_iocbq *iocbq_entry = NULL;
5482 uint16_t iotag;
5483 int i;
dea31012005-04-17 16:05:31 -05005484
5485 /* Initialize and populate the iocb list per host. */
5486 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04005487 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07005488 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05005489 if (iocbq_entry == NULL) {
5490 printk(KERN_ERR "%s: only allocated %d iocbs of "
5491 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07005492 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05005493 goto out_free_iocbq;
5494 }
5495
James Bottomley604a3e32005-10-29 10:28:33 -05005496 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
5497 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04005498 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05005499 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04005500 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05005501 goto out_free_iocbq;
5502 }
James Smart6d368e52011-05-24 11:44:12 -04005503 iocbq_entry->sli4_lxritag = NO_XRI;
James Smart3772a992009-05-22 14:50:54 -04005504 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05005505
5506 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005507 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
5508 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05005509 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005510 }
5511
James Smart3772a992009-05-22 14:50:54 -04005512 return 0;
5513
5514out_free_iocbq:
5515 lpfc_free_iocb_list(phba);
5516
5517 return -ENOMEM;
5518}
5519
5520/**
James Smart8a9d2e82012-05-09 21:16:12 -04005521 * lpfc_free_sgl_list - Free a given sgl list.
James Smartda0436e2009-05-22 14:51:39 -04005522 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -04005523 * @sglq_list: pointer to the head of sgl list.
James Smartda0436e2009-05-22 14:51:39 -04005524 *
James Smart8a9d2e82012-05-09 21:16:12 -04005525 * This routine is invoked to free a give sgl list and memory.
James Smartda0436e2009-05-22 14:51:39 -04005526 **/
James Smart8a9d2e82012-05-09 21:16:12 -04005527void
5528lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
James Smartda0436e2009-05-22 14:51:39 -04005529{
5530 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart8a9d2e82012-05-09 21:16:12 -04005531
5532 list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
5533 list_del(&sglq_entry->list);
5534 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
5535 kfree(sglq_entry);
5536 }
5537}
5538
5539/**
5540 * lpfc_free_els_sgl_list - Free els sgl list.
5541 * @phba: pointer to lpfc hba data structure.
5542 *
5543 * This routine is invoked to free the driver's els sgl list and memory.
5544 **/
5545static void
5546lpfc_free_els_sgl_list(struct lpfc_hba *phba)
5547{
James Smartda0436e2009-05-22 14:51:39 -04005548 LIST_HEAD(sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04005549
James Smart8a9d2e82012-05-09 21:16:12 -04005550 /* Retrieve all els sgls from driver list */
James Smartda0436e2009-05-22 14:51:39 -04005551 spin_lock_irq(&phba->hbalock);
5552 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
5553 spin_unlock_irq(&phba->hbalock);
5554
James Smart8a9d2e82012-05-09 21:16:12 -04005555 /* Now free the sgl list */
5556 lpfc_free_sgl_list(phba, &sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04005557}
5558
5559/**
5560 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
5561 * @phba: pointer to lpfc hba data structure.
5562 *
5563 * This routine is invoked to allocate the driver's active sgl memory.
5564 * This array will hold the sglq_entry's for active IOs.
5565 **/
5566static int
5567lpfc_init_active_sgl_array(struct lpfc_hba *phba)
5568{
5569 int size;
5570 size = sizeof(struct lpfc_sglq *);
5571 size *= phba->sli4_hba.max_cfg_param.max_xri;
5572
5573 phba->sli4_hba.lpfc_sglq_active_list =
5574 kzalloc(size, GFP_KERNEL);
5575 if (!phba->sli4_hba.lpfc_sglq_active_list)
5576 return -ENOMEM;
5577 return 0;
5578}
5579
5580/**
5581 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
5582 * @phba: pointer to lpfc hba data structure.
5583 *
5584 * This routine is invoked to walk through the array of active sglq entries
5585 * and free all of the resources.
5586 * This is just a place holder for now.
5587 **/
5588static void
5589lpfc_free_active_sgl(struct lpfc_hba *phba)
5590{
5591 kfree(phba->sli4_hba.lpfc_sglq_active_list);
5592}
5593
5594/**
5595 * lpfc_init_sgl_list - Allocate and initialize sgl list.
5596 * @phba: pointer to lpfc hba data structure.
5597 *
5598 * This routine is invoked to allocate and initizlize the driver's sgl
5599 * list and set up the sgl xritag tag array accordingly.
5600 *
James Smartda0436e2009-05-22 14:51:39 -04005601 **/
James Smart8a9d2e82012-05-09 21:16:12 -04005602static void
James Smartda0436e2009-05-22 14:51:39 -04005603lpfc_init_sgl_list(struct lpfc_hba *phba)
5604{
James Smartda0436e2009-05-22 14:51:39 -04005605 /* Initialize and populate the sglq list per host/VF. */
5606 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
5607 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
5608
James Smart8a9d2e82012-05-09 21:16:12 -04005609 /* els xri-sgl book keeping */
5610 phba->sli4_hba.els_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04005611
James Smart8a9d2e82012-05-09 21:16:12 -04005612 /* scsi xri-buffer book keeping */
James Smartda0436e2009-05-22 14:51:39 -04005613 phba->sli4_hba.scsi_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04005614}
5615
5616/**
5617 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
5618 * @phba: pointer to lpfc hba data structure.
5619 *
5620 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -04005621 * port for those SLI4 ports that do not support extents. This routine
James Smartda0436e2009-05-22 14:51:39 -04005622 * posts a PAGE_SIZE memory region to the port to hold up to
James Smart88a2cfb2011-07-22 18:36:33 -04005623 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
5624 * and should be called only when interrupts are disabled.
James Smartda0436e2009-05-22 14:51:39 -04005625 *
5626 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005627 * 0 - successful
James Smart88a2cfb2011-07-22 18:36:33 -04005628 * -ERROR - otherwise.
James Smartda0436e2009-05-22 14:51:39 -04005629 **/
5630int
5631lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
5632{
5633 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04005634 struct lpfc_rpi_hdr *rpi_hdr;
5635
5636 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
James Smartff78d8f2011-12-13 13:21:35 -05005637 if (!phba->sli4_hba.rpi_hdrs_in_use)
James Smart6d368e52011-05-24 11:44:12 -04005638 return rc;
James Smart6d368e52011-05-24 11:44:12 -04005639 if (phba->sli4_hba.extents_in_use)
5640 return -EIO;
James Smartda0436e2009-05-22 14:51:39 -04005641
5642 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
5643 if (!rpi_hdr) {
5644 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5645 "0391 Error during rpi post operation\n");
5646 lpfc_sli4_remove_rpis(phba);
5647 rc = -ENODEV;
5648 }
5649
5650 return rc;
5651}
5652
5653/**
5654 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
5655 * @phba: pointer to lpfc hba data structure.
5656 *
5657 * This routine is invoked to allocate a single 4KB memory region to
5658 * support rpis and stores them in the phba. This single region
5659 * provides support for up to 64 rpis. The region is used globally
5660 * by the device.
5661 *
5662 * Returns:
5663 * A valid rpi hdr on success.
5664 * A NULL pointer on any failure.
5665 **/
5666struct lpfc_rpi_hdr *
5667lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
5668{
5669 uint16_t rpi_limit, curr_rpi_range;
5670 struct lpfc_dmabuf *dmabuf;
5671 struct lpfc_rpi_hdr *rpi_hdr;
James Smart9589b0622011-04-16 11:03:17 -04005672 uint32_t rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04005673
James Smart6d368e52011-05-24 11:44:12 -04005674 /*
5675 * If the SLI4 port supports extents, posting the rpi header isn't
5676 * required. Set the expected maximum count and let the actual value
5677 * get set when extents are fully allocated.
5678 */
5679 if (!phba->sli4_hba.rpi_hdrs_in_use)
5680 return NULL;
5681 if (phba->sli4_hba.extents_in_use)
5682 return NULL;
5683
5684 /* The limit on the logical index is just the max_rpi count. */
James Smartda0436e2009-05-22 14:51:39 -04005685 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
James Smart6d368e52011-05-24 11:44:12 -04005686 phba->sli4_hba.max_cfg_param.max_rpi - 1;
James Smartda0436e2009-05-22 14:51:39 -04005687
5688 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04005689 /*
5690 * Establish the starting RPI in this header block. The starting
5691 * rpi is normalized to a zero base because the physical rpi is
5692 * port based.
5693 */
James Smart97f2ecf2012-03-01 22:35:23 -05005694 curr_rpi_range = phba->sli4_hba.next_rpi;
James Smartda0436e2009-05-22 14:51:39 -04005695 spin_unlock_irq(&phba->hbalock);
5696
5697 /*
5698 * The port has a limited number of rpis. The increment here
5699 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
5700 * and to allow the full max_rpi range per port.
5701 */
5702 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
James Smart9589b0622011-04-16 11:03:17 -04005703 rpi_count = rpi_limit - curr_rpi_range;
5704 else
5705 rpi_count = LPFC_RPI_HDR_COUNT;
James Smartda0436e2009-05-22 14:51:39 -04005706
James Smart6d368e52011-05-24 11:44:12 -04005707 if (!rpi_count)
5708 return NULL;
James Smartda0436e2009-05-22 14:51:39 -04005709 /*
5710 * First allocate the protocol header region for the port. The
5711 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
5712 */
5713 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5714 if (!dmabuf)
5715 return NULL;
5716
5717 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5718 LPFC_HDR_TEMPLATE_SIZE,
5719 &dmabuf->phys,
5720 GFP_KERNEL);
5721 if (!dmabuf->virt) {
5722 rpi_hdr = NULL;
5723 goto err_free_dmabuf;
5724 }
5725
5726 memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
5727 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
5728 rpi_hdr = NULL;
5729 goto err_free_coherent;
5730 }
5731
5732 /* Save the rpi header data for cleanup later. */
5733 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
5734 if (!rpi_hdr)
5735 goto err_free_coherent;
5736
5737 rpi_hdr->dmabuf = dmabuf;
5738 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
5739 rpi_hdr->page_count = 1;
5740 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04005741
5742 /* The rpi_hdr stores the logical index only. */
5743 rpi_hdr->start_rpi = curr_rpi_range;
James Smartda0436e2009-05-22 14:51:39 -04005744 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
5745
5746 /*
James Smart6d368e52011-05-24 11:44:12 -04005747 * The next_rpi stores the next logical module-64 rpi value used
5748 * to post physical rpis in subsequent rpi postings.
James Smartda0436e2009-05-22 14:51:39 -04005749 */
James Smart9589b0622011-04-16 11:03:17 -04005750 phba->sli4_hba.next_rpi += rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04005751 spin_unlock_irq(&phba->hbalock);
5752 return rpi_hdr;
5753
5754 err_free_coherent:
5755 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
5756 dmabuf->virt, dmabuf->phys);
5757 err_free_dmabuf:
5758 kfree(dmabuf);
5759 return NULL;
5760}
5761
5762/**
5763 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
5764 * @phba: pointer to lpfc hba data structure.
5765 *
5766 * This routine is invoked to remove all memory resources allocated
James Smart6d368e52011-05-24 11:44:12 -04005767 * to support rpis for SLI4 ports not supporting extents. This routine
5768 * presumes the caller has released all rpis consumed by fabric or port
5769 * logins and is prepared to have the header pages removed.
James Smartda0436e2009-05-22 14:51:39 -04005770 **/
5771void
5772lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
5773{
5774 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
5775
James Smart6d368e52011-05-24 11:44:12 -04005776 if (!phba->sli4_hba.rpi_hdrs_in_use)
5777 goto exit;
5778
James Smartda0436e2009-05-22 14:51:39 -04005779 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
5780 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
5781 list_del(&rpi_hdr->list);
5782 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
5783 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
5784 kfree(rpi_hdr->dmabuf);
5785 kfree(rpi_hdr);
5786 }
James Smart6d368e52011-05-24 11:44:12 -04005787 exit:
5788 /* There are no rpis available to the port now. */
5789 phba->sli4_hba.next_rpi = 0;
James Smartda0436e2009-05-22 14:51:39 -04005790}
5791
5792/**
James Smart3772a992009-05-22 14:50:54 -04005793 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
5794 * @pdev: pointer to pci device data structure.
5795 *
5796 * This routine is invoked to allocate the driver hba data structure for an
5797 * HBA device. If the allocation is successful, the phba reference to the
5798 * PCI device data structure is set.
5799 *
5800 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005801 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04005802 * NULL - error
5803 **/
5804static struct lpfc_hba *
5805lpfc_hba_alloc(struct pci_dev *pdev)
5806{
5807 struct lpfc_hba *phba;
5808
5809 /* Allocate memory for HBA structure */
5810 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
5811 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02005812 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04005813 return NULL;
5814 }
5815
5816 /* Set reference to PCI device in HBA structure */
5817 phba->pcidev = pdev;
5818
5819 /* Assign an unused board number */
5820 phba->brd_no = lpfc_get_instance();
5821 if (phba->brd_no < 0) {
5822 kfree(phba);
5823 return NULL;
5824 }
5825
James Smart4fede782010-01-26 23:08:55 -05005826 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04005827 INIT_LIST_HEAD(&phba->ct_ev_waiters);
5828
James Smart3772a992009-05-22 14:50:54 -04005829 return phba;
5830}
5831
5832/**
5833 * lpfc_hba_free - Free driver hba data structure with a device.
5834 * @phba: pointer to lpfc hba data structure.
5835 *
5836 * This routine is invoked to free the driver hba data structure with an
5837 * HBA device.
5838 **/
5839static void
5840lpfc_hba_free(struct lpfc_hba *phba)
5841{
5842 /* Release the driver assigned board number */
5843 idr_remove(&lpfc_hba_index, phba->brd_no);
5844
James Smart2a76a282012-08-03 12:35:54 -04005845 /* Free memory allocated with sli rings */
5846 kfree(phba->sli.ring);
5847 phba->sli.ring = NULL;
5848
James Smart3772a992009-05-22 14:50:54 -04005849 kfree(phba);
5850 return;
5851}
5852
5853/**
5854 * lpfc_create_shost - Create hba physical port with associated scsi host.
5855 * @phba: pointer to lpfc hba data structure.
5856 *
5857 * This routine is invoked to create HBA physical port and associate a SCSI
5858 * host with it.
5859 *
5860 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005861 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005862 * other values - error
5863 **/
5864static int
5865lpfc_create_shost(struct lpfc_hba *phba)
5866{
5867 struct lpfc_vport *vport;
5868 struct Scsi_Host *shost;
5869
5870 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05005871 phba->fc_edtov = FF_DEF_EDTOV;
5872 phba->fc_ratov = FF_DEF_RATOV;
5873 phba->fc_altov = FF_DEF_ALTOV;
5874 phba->fc_arbtov = FF_DEF_ARBTOV;
5875
James Smartd7c47992010-06-08 18:31:54 -04005876 atomic_set(&phba->sdev_cnt, 0);
James Smart3de2a652007-08-02 11:09:59 -04005877 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05005878 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04005879 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05005880
5881 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05005882 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05005883 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04005884 /* Put reference to SCSI host to driver's device private data */
5885 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05005886
James Smart3772a992009-05-22 14:50:54 -04005887 return 0;
5888}
dea31012005-04-17 16:05:31 -05005889
James Smart3772a992009-05-22 14:50:54 -04005890/**
5891 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
5892 * @phba: pointer to lpfc hba data structure.
5893 *
5894 * This routine is invoked to destroy HBA physical port and the associated
5895 * SCSI host.
5896 **/
5897static void
5898lpfc_destroy_shost(struct lpfc_hba *phba)
5899{
5900 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04005901
James Smart3772a992009-05-22 14:50:54 -04005902 /* Destroy physical port that associated with the SCSI host */
5903 destroy_port(vport);
5904
5905 return;
5906}
5907
5908/**
5909 * lpfc_setup_bg - Setup Block guard structures and debug areas.
5910 * @phba: pointer to lpfc hba data structure.
5911 * @shost: the shost to be used to detect Block guard settings.
5912 *
5913 * This routine sets up the local Block guard protocol settings for @shost.
5914 * This routine also allocates memory for debugging bg buffers.
5915 **/
5916static void
5917lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
5918{
James Smartbbeb79b2012-06-12 13:54:27 -04005919 uint32_t old_mask;
5920 uint32_t old_guard;
5921
James Smart3772a992009-05-22 14:50:54 -04005922 int pagecnt = 10;
5923 if (lpfc_prot_mask && lpfc_prot_guard) {
5924 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5925 "1478 Registering BlockGuard with the "
5926 "SCSI layer\n");
James Smartbbeb79b2012-06-12 13:54:27 -04005927
5928 old_mask = lpfc_prot_mask;
5929 old_guard = lpfc_prot_guard;
5930
5931 /* Only allow supported values */
5932 lpfc_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
5933 SHOST_DIX_TYPE0_PROTECTION |
5934 SHOST_DIX_TYPE1_PROTECTION);
5935 lpfc_prot_guard &= (SHOST_DIX_GUARD_IP | SHOST_DIX_GUARD_CRC);
5936
5937 /* DIF Type 1 protection for profiles AST1/C1 is end to end */
5938 if (lpfc_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
5939 lpfc_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
5940
5941 if (lpfc_prot_mask && lpfc_prot_guard) {
5942 if ((old_mask != lpfc_prot_mask) ||
5943 (old_guard != lpfc_prot_guard))
5944 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5945 "1475 Registering BlockGuard with the "
5946 "SCSI layer: mask %d guard %d\n",
5947 lpfc_prot_mask, lpfc_prot_guard);
5948
5949 scsi_host_set_prot(shost, lpfc_prot_mask);
5950 scsi_host_set_guard(shost, lpfc_prot_guard);
5951 } else
5952 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5953 "1479 Not Registering BlockGuard with the SCSI "
5954 "layer, Bad protection parameters: %d %d\n",
5955 old_mask, old_guard);
James Smart98c9ea52007-10-27 13:37:33 -04005956 }
James Smartbbeb79b2012-06-12 13:54:27 -04005957
James Smart81301a92008-12-04 22:39:46 -05005958 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05005959 while (pagecnt) {
5960 spin_lock_init(&_dump_buf_lock);
5961 _dump_buf_data =
5962 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
5963 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04005964 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5965 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04005966 "_dump_buf_data at 0x%p\n",
5967 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05005968 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04005969 memset(_dump_buf_data, 0,
5970 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05005971 break;
James Smart3772a992009-05-22 14:50:54 -04005972 } else
James Smart81301a92008-12-04 22:39:46 -05005973 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05005974 }
James Smart81301a92008-12-04 22:39:46 -05005975 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04005976 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5977 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04005978 "memory for hexdump\n");
5979 } else
James Smart6a9c52c2009-10-02 15:16:51 -04005980 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5981 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05005982 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05005983 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05005984 while (pagecnt) {
5985 _dump_buf_dif =
5986 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
5987 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04005988 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5989 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04005990 "_dump_buf_dif at 0x%p\n",
5991 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05005992 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04005993 memset(_dump_buf_dif, 0,
5994 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05005995 break;
James Smart3772a992009-05-22 14:50:54 -04005996 } else
James Smart81301a92008-12-04 22:39:46 -05005997 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05005998 }
James Smart81301a92008-12-04 22:39:46 -05005999 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04006000 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6001 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04006002 "memory for hexdump\n");
6003 } else
James Smart6a9c52c2009-10-02 15:16:51 -04006004 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6005 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04006006 _dump_buf_dif);
6007}
6008
6009/**
6010 * lpfc_post_init_setup - Perform necessary device post initialization setup.
6011 * @phba: pointer to lpfc hba data structure.
6012 *
6013 * This routine is invoked to perform all the necessary post initialization
6014 * setup for the device.
6015 **/
6016static void
6017lpfc_post_init_setup(struct lpfc_hba *phba)
6018{
6019 struct Scsi_Host *shost;
6020 struct lpfc_adapter_event_header adapter_event;
6021
6022 /* Get the default values for Model Name and Description */
6023 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
6024
6025 /*
6026 * hba setup may have changed the hba_queue_depth so we need to
6027 * adjust the value of can_queue.
6028 */
6029 shost = pci_get_drvdata(phba->pcidev);
6030 shost->can_queue = phba->cfg_hba_queue_depth - 10;
6031 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
6032 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05006033
6034 lpfc_host_attrib_init(shost);
6035
James Smart2e0fef82007-06-17 19:56:36 -05006036 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
6037 spin_lock_irq(shost->host_lock);
6038 lpfc_poll_start_timer(phba);
6039 spin_unlock_irq(shost->host_lock);
6040 }
James Smart8f6d98d2006-08-01 07:34:00 -04006041
James Smart93996272008-08-24 21:50:30 -04006042 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6043 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04006044 /* Send board arrival event to upper layer */
6045 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
6046 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
6047 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04006048 sizeof(adapter_event),
6049 (char *) &adapter_event,
6050 LPFC_NL_VENDOR_ID);
6051 return;
6052}
6053
6054/**
6055 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
6056 * @phba: pointer to lpfc hba data structure.
6057 *
6058 * This routine is invoked to set up the PCI device memory space for device
6059 * with SLI-3 interface spec.
6060 *
6061 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006062 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006063 * other values - error
6064 **/
6065static int
6066lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
6067{
6068 struct pci_dev *pdev;
6069 unsigned long bar0map_len, bar2map_len;
6070 int i, hbq_count;
6071 void *ptr;
6072 int error = -ENODEV;
6073
6074 /* Obtain PCI device reference */
6075 if (!phba->pcidev)
6076 return error;
6077 else
6078 pdev = phba->pcidev;
6079
6080 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05006081 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
6082 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
6083 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
6084 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smart3772a992009-05-22 14:50:54 -04006085 return error;
Michael Reed8e685972009-09-18 12:02:05 -05006086 }
6087 }
James Smart3772a992009-05-22 14:50:54 -04006088
6089 /* Get the bus address of Bar0 and Bar2 and the number of bytes
6090 * required by each mapping.
6091 */
6092 phba->pci_bar0_map = pci_resource_start(pdev, 0);
6093 bar0map_len = pci_resource_len(pdev, 0);
6094
6095 phba->pci_bar2_map = pci_resource_start(pdev, 2);
6096 bar2map_len = pci_resource_len(pdev, 2);
6097
6098 /* Map HBA SLIM to a kernel virtual address. */
6099 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
6100 if (!phba->slim_memmap_p) {
6101 dev_printk(KERN_ERR, &pdev->dev,
6102 "ioremap failed for SLIM memory.\n");
6103 goto out;
6104 }
6105
6106 /* Map HBA Control Registers to a kernel virtual address. */
6107 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
6108 if (!phba->ctrl_regs_memmap_p) {
6109 dev_printk(KERN_ERR, &pdev->dev,
6110 "ioremap failed for HBA control registers.\n");
6111 goto out_iounmap_slim;
6112 }
6113
6114 /* Allocate memory for SLI-2 structures */
6115 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
6116 SLI2_SLIM_SIZE,
6117 &phba->slim2p.phys,
6118 GFP_KERNEL);
6119 if (!phba->slim2p.virt)
6120 goto out_iounmap;
6121
6122 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
6123 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04006124 phba->mbox_ext = (phba->slim2p.virt +
6125 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04006126 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
6127 phba->IOCBs = (phba->slim2p.virt +
6128 offsetof(struct lpfc_sli2_slim, IOCBs));
6129
6130 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
6131 lpfc_sli_hbq_size(),
6132 &phba->hbqslimp.phys,
6133 GFP_KERNEL);
6134 if (!phba->hbqslimp.virt)
6135 goto out_free_slim;
6136
6137 hbq_count = lpfc_sli_hbq_count();
6138 ptr = phba->hbqslimp.virt;
6139 for (i = 0; i < hbq_count; ++i) {
6140 phba->hbqs[i].hbq_virt = ptr;
6141 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
6142 ptr += (lpfc_hbq_defs[i]->entry_count *
6143 sizeof(struct lpfc_hbq_entry));
6144 }
6145 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
6146 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
6147
6148 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
6149
6150 INIT_LIST_HEAD(&phba->rb_pend_list);
6151
6152 phba->MBslimaddr = phba->slim_memmap_p;
6153 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
6154 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
6155 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
6156 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04006157
dea31012005-04-17 16:05:31 -05006158 return 0;
6159
dea31012005-04-17 16:05:31 -05006160out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04006161 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6162 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05006163out_iounmap:
6164 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05006165out_iounmap_slim:
dea31012005-04-17 16:05:31 -05006166 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05006167out:
6168 return error;
6169}
6170
James Smarte59058c2008-08-24 21:49:00 -04006171/**
James Smart3772a992009-05-22 14:50:54 -04006172 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
6173 * @phba: pointer to lpfc hba data structure.
6174 *
6175 * This routine is invoked to unset the PCI device memory space for device
6176 * with SLI-3 interface spec.
6177 **/
6178static void
6179lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
6180{
6181 struct pci_dev *pdev;
6182
6183 /* Obtain PCI device reference */
6184 if (!phba->pcidev)
6185 return;
6186 else
6187 pdev = phba->pcidev;
6188
6189 /* Free coherent DMA memory allocated */
6190 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
6191 phba->hbqslimp.virt, phba->hbqslimp.phys);
6192 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6193 phba->slim2p.virt, phba->slim2p.phys);
6194
6195 /* I/O memory unmap */
6196 iounmap(phba->ctrl_regs_memmap_p);
6197 iounmap(phba->slim_memmap_p);
6198
6199 return;
6200}
6201
6202/**
James Smartda0436e2009-05-22 14:51:39 -04006203 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
6204 * @phba: pointer to lpfc hba data structure.
6205 *
6206 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
6207 * done and check status.
6208 *
6209 * Return 0 if successful, otherwise -ENODEV.
6210 **/
6211int
6212lpfc_sli4_post_status_check(struct lpfc_hba *phba)
6213{
James Smart2fcee4b2010-12-15 17:57:46 -05006214 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
6215 struct lpfc_register reg_data;
6216 int i, port_error = 0;
6217 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04006218
James Smart9940b972011-03-11 16:06:12 -05006219 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
6220 memset(&reg_data, 0, sizeof(reg_data));
James Smart2fcee4b2010-12-15 17:57:46 -05006221 if (!phba->sli4_hba.PSMPHRregaddr)
James Smartda0436e2009-05-22 14:51:39 -04006222 return -ENODEV;
6223
James Smartda0436e2009-05-22 14:51:39 -04006224 /* Wait up to 30 seconds for the SLI Port POST done and ready */
6225 for (i = 0; i < 3000; i++) {
James Smart9940b972011-03-11 16:06:12 -05006226 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
6227 &portsmphr_reg.word0) ||
6228 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
James Smart2fcee4b2010-12-15 17:57:46 -05006229 /* Port has a fatal POST error, break out */
James Smartda0436e2009-05-22 14:51:39 -04006230 port_error = -ENODEV;
6231 break;
6232 }
James Smart2fcee4b2010-12-15 17:57:46 -05006233 if (LPFC_POST_STAGE_PORT_READY ==
6234 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
James Smartda0436e2009-05-22 14:51:39 -04006235 break;
James Smartda0436e2009-05-22 14:51:39 -04006236 msleep(10);
6237 }
6238
James Smart2fcee4b2010-12-15 17:57:46 -05006239 /*
6240 * If there was a port error during POST, then don't proceed with
6241 * other register reads as the data may not be valid. Just exit.
6242 */
6243 if (port_error) {
James Smartda0436e2009-05-22 14:51:39 -04006244 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05006245 "1408 Port Failed POST - portsmphr=0x%x, "
6246 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
6247 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
6248 portsmphr_reg.word0,
6249 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
6250 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
6251 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
6252 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
6253 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
6254 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
6255 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
6256 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
6257 } else {
James Smart28baac72010-02-12 14:42:03 -05006258 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05006259 "2534 Device Info: SLIFamily=0x%x, "
6260 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
6261 "SLIHint_2=0x%x, FT=0x%x\n",
James Smart28baac72010-02-12 14:42:03 -05006262 bf_get(lpfc_sli_intf_sli_family,
6263 &phba->sli4_hba.sli_intf),
6264 bf_get(lpfc_sli_intf_slirev,
6265 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05006266 bf_get(lpfc_sli_intf_if_type,
James Smart28baac72010-02-12 14:42:03 -05006267 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05006268 bf_get(lpfc_sli_intf_sli_hint1,
6269 &phba->sli4_hba.sli_intf),
6270 bf_get(lpfc_sli_intf_sli_hint2,
6271 &phba->sli4_hba.sli_intf),
6272 bf_get(lpfc_sli_intf_func_type,
James Smart28baac72010-02-12 14:42:03 -05006273 &phba->sli4_hba.sli_intf));
James Smart2fcee4b2010-12-15 17:57:46 -05006274 /*
6275 * Check for other Port errors during the initialization
6276 * process. Fail the load if the port did not come up
6277 * correctly.
6278 */
6279 if_type = bf_get(lpfc_sli_intf_if_type,
6280 &phba->sli4_hba.sli_intf);
6281 switch (if_type) {
6282 case LPFC_SLI_INTF_IF_TYPE_0:
6283 phba->sli4_hba.ue_mask_lo =
6284 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
6285 phba->sli4_hba.ue_mask_hi =
6286 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
6287 uerrlo_reg.word0 =
6288 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
6289 uerrhi_reg.word0 =
6290 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
6291 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
6292 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
6293 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6294 "1422 Unrecoverable Error "
6295 "Detected during POST "
6296 "uerr_lo_reg=0x%x, "
6297 "uerr_hi_reg=0x%x, "
6298 "ue_mask_lo_reg=0x%x, "
6299 "ue_mask_hi_reg=0x%x\n",
6300 uerrlo_reg.word0,
6301 uerrhi_reg.word0,
6302 phba->sli4_hba.ue_mask_lo,
6303 phba->sli4_hba.ue_mask_hi);
6304 port_error = -ENODEV;
6305 }
6306 break;
6307 case LPFC_SLI_INTF_IF_TYPE_2:
6308 /* Final checks. The port status should be clean. */
James Smart9940b972011-03-11 16:06:12 -05006309 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
6310 &reg_data.word0) ||
James Smart05580562011-05-24 11:40:48 -04006311 (bf_get(lpfc_sliport_status_err, &reg_data) &&
6312 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
James Smart2fcee4b2010-12-15 17:57:46 -05006313 phba->work_status[0] =
6314 readl(phba->sli4_hba.u.if_type2.
6315 ERR1regaddr);
6316 phba->work_status[1] =
6317 readl(phba->sli4_hba.u.if_type2.
6318 ERR2regaddr);
6319 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05006320 "2888 Unrecoverable port error "
6321 "following POST: port status reg "
6322 "0x%x, port_smphr reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05006323 "error 1=0x%x, error 2=0x%x\n",
6324 reg_data.word0,
6325 portsmphr_reg.word0,
6326 phba->work_status[0],
6327 phba->work_status[1]);
6328 port_error = -ENODEV;
6329 }
6330 break;
6331 case LPFC_SLI_INTF_IF_TYPE_1:
6332 default:
6333 break;
6334 }
James Smart28baac72010-02-12 14:42:03 -05006335 }
James Smartda0436e2009-05-22 14:51:39 -04006336 return port_error;
6337}
6338
6339/**
6340 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
6341 * @phba: pointer to lpfc hba data structure.
James Smart2fcee4b2010-12-15 17:57:46 -05006342 * @if_type: The SLI4 interface type getting configured.
James Smartda0436e2009-05-22 14:51:39 -04006343 *
6344 * This routine is invoked to set up SLI4 BAR0 PCI config space register
6345 * memory map.
6346 **/
6347static void
James Smart2fcee4b2010-12-15 17:57:46 -05006348lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04006349{
James Smart2fcee4b2010-12-15 17:57:46 -05006350 switch (if_type) {
6351 case LPFC_SLI_INTF_IF_TYPE_0:
6352 phba->sli4_hba.u.if_type0.UERRLOregaddr =
6353 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
6354 phba->sli4_hba.u.if_type0.UERRHIregaddr =
6355 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
6356 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
6357 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
6358 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
6359 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
6360 phba->sli4_hba.SLIINTFregaddr =
6361 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
6362 break;
6363 case LPFC_SLI_INTF_IF_TYPE_2:
6364 phba->sli4_hba.u.if_type2.ERR1regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006365 phba->sli4_hba.conf_regs_memmap_p +
6366 LPFC_CTL_PORT_ER1_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006367 phba->sli4_hba.u.if_type2.ERR2regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006368 phba->sli4_hba.conf_regs_memmap_p +
6369 LPFC_CTL_PORT_ER2_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006370 phba->sli4_hba.u.if_type2.CTRLregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006371 phba->sli4_hba.conf_regs_memmap_p +
6372 LPFC_CTL_PORT_CTL_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006373 phba->sli4_hba.u.if_type2.STATUSregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006374 phba->sli4_hba.conf_regs_memmap_p +
6375 LPFC_CTL_PORT_STA_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006376 phba->sli4_hba.SLIINTFregaddr =
6377 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
6378 phba->sli4_hba.PSMPHRregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006379 phba->sli4_hba.conf_regs_memmap_p +
6380 LPFC_CTL_PORT_SEM_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006381 phba->sli4_hba.RQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05006382 phba->sli4_hba.conf_regs_memmap_p +
6383 LPFC_ULP0_RQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05006384 phba->sli4_hba.WQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05006385 phba->sli4_hba.conf_regs_memmap_p +
6386 LPFC_ULP0_WQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05006387 phba->sli4_hba.EQCQDBregaddr =
6388 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
6389 phba->sli4_hba.MQDBregaddr =
6390 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
6391 phba->sli4_hba.BMBXregaddr =
6392 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
6393 break;
6394 case LPFC_SLI_INTF_IF_TYPE_1:
6395 default:
6396 dev_printk(KERN_ERR, &phba->pcidev->dev,
6397 "FATAL - unsupported SLI4 interface type - %d\n",
6398 if_type);
6399 break;
6400 }
James Smartda0436e2009-05-22 14:51:39 -04006401}
6402
6403/**
6404 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
6405 * @phba: pointer to lpfc hba data structure.
6406 *
6407 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
6408 * memory map.
6409 **/
6410static void
6411lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
6412{
James Smart2fcee4b2010-12-15 17:57:46 -05006413 phba->sli4_hba.PSMPHRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
6414 LPFC_SLIPORT_IF0_SMPHR;
James Smartda0436e2009-05-22 14:51:39 -04006415 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006416 LPFC_HST_ISR0;
James Smartda0436e2009-05-22 14:51:39 -04006417 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006418 LPFC_HST_IMR0;
James Smartda0436e2009-05-22 14:51:39 -04006419 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006420 LPFC_HST_ISCR0;
James Smartda0436e2009-05-22 14:51:39 -04006421}
6422
6423/**
6424 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
6425 * @phba: pointer to lpfc hba data structure.
6426 * @vf: virtual function number
6427 *
6428 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
6429 * based on the given viftual function number, @vf.
6430 *
6431 * Return 0 if successful, otherwise -ENODEV.
6432 **/
6433static int
6434lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
6435{
6436 if (vf > LPFC_VIR_FUNC_MAX)
6437 return -ENODEV;
6438
6439 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05006440 vf * LPFC_VFR_PAGE_SIZE +
6441 LPFC_ULP0_RQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04006442 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05006443 vf * LPFC_VFR_PAGE_SIZE +
6444 LPFC_ULP0_WQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04006445 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6446 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
6447 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6448 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
6449 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6450 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
6451 return 0;
6452}
6453
6454/**
6455 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
6456 * @phba: pointer to lpfc hba data structure.
6457 *
6458 * This routine is invoked to create the bootstrap mailbox
6459 * region consistent with the SLI-4 interface spec. This
6460 * routine allocates all memory necessary to communicate
6461 * mailbox commands to the port and sets up all alignment
6462 * needs. No locks are expected to be held when calling
6463 * this routine.
6464 *
6465 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006466 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04006467 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04006468 **/
6469static int
6470lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
6471{
6472 uint32_t bmbx_size;
6473 struct lpfc_dmabuf *dmabuf;
6474 struct dma_address *dma_address;
6475 uint32_t pa_addr;
6476 uint64_t phys_addr;
6477
6478 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
6479 if (!dmabuf)
6480 return -ENOMEM;
6481
6482 /*
6483 * The bootstrap mailbox region is comprised of 2 parts
6484 * plus an alignment restriction of 16 bytes.
6485 */
6486 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
6487 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
6488 bmbx_size,
6489 &dmabuf->phys,
6490 GFP_KERNEL);
6491 if (!dmabuf->virt) {
6492 kfree(dmabuf);
6493 return -ENOMEM;
6494 }
6495 memset(dmabuf->virt, 0, bmbx_size);
6496
6497 /*
6498 * Initialize the bootstrap mailbox pointers now so that the register
6499 * operations are simple later. The mailbox dma address is required
6500 * to be 16-byte aligned. Also align the virtual memory as each
6501 * maibox is copied into the bmbx mailbox region before issuing the
6502 * command to the port.
6503 */
6504 phba->sli4_hba.bmbx.dmabuf = dmabuf;
6505 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
6506
6507 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
6508 LPFC_ALIGN_16_BYTE);
6509 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
6510 LPFC_ALIGN_16_BYTE);
6511
6512 /*
6513 * Set the high and low physical addresses now. The SLI4 alignment
6514 * requirement is 16 bytes and the mailbox is posted to the port
6515 * as two 30-bit addresses. The other data is a bit marking whether
6516 * the 30-bit address is the high or low address.
6517 * Upcast bmbx aphys to 64bits so shift instruction compiles
6518 * clean on 32 bit machines.
6519 */
6520 dma_address = &phba->sli4_hba.bmbx.dma_address;
6521 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
6522 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
6523 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
6524 LPFC_BMBX_BIT1_ADDR_HI);
6525
6526 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
6527 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
6528 LPFC_BMBX_BIT1_ADDR_LO);
6529 return 0;
6530}
6531
6532/**
6533 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
6534 * @phba: pointer to lpfc hba data structure.
6535 *
6536 * This routine is invoked to teardown the bootstrap mailbox
6537 * region and release all host resources. This routine requires
6538 * the caller to ensure all mailbox commands recovered, no
6539 * additional mailbox comands are sent, and interrupts are disabled
6540 * before calling this routine.
6541 *
6542 **/
6543static void
6544lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
6545{
6546 dma_free_coherent(&phba->pcidev->dev,
6547 phba->sli4_hba.bmbx.bmbx_size,
6548 phba->sli4_hba.bmbx.dmabuf->virt,
6549 phba->sli4_hba.bmbx.dmabuf->phys);
6550
6551 kfree(phba->sli4_hba.bmbx.dmabuf);
6552 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
6553}
6554
6555/**
6556 * lpfc_sli4_read_config - Get the config parameters.
6557 * @phba: pointer to lpfc hba data structure.
6558 *
6559 * This routine is invoked to read the configuration parameters from the HBA.
6560 * The configuration parameters are used to set the base and maximum values
6561 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
6562 * allocation for the port.
6563 *
6564 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006565 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006566 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006567 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006568 **/
James Smartff78d8f2011-12-13 13:21:35 -05006569int
James Smartda0436e2009-05-22 14:51:39 -04006570lpfc_sli4_read_config(struct lpfc_hba *phba)
6571{
6572 LPFC_MBOXQ_t *pmb;
6573 struct lpfc_mbx_read_config *rd_config;
James Smart912e3ac2011-05-24 11:42:11 -04006574 union lpfc_sli4_cfg_shdr *shdr;
6575 uint32_t shdr_status, shdr_add_status;
6576 struct lpfc_mbx_get_func_cfg *get_func_cfg;
6577 struct lpfc_rsrc_desc_fcfcoe *desc;
James Smart8aa134a2012-08-14 14:25:29 -04006578 char *pdesc_0;
James Smart912e3ac2011-05-24 11:42:11 -04006579 uint32_t desc_count;
James Smart8aa134a2012-08-14 14:25:29 -04006580 int length, i, rc = 0, rc2;
James Smartda0436e2009-05-22 14:51:39 -04006581
6582 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6583 if (!pmb) {
6584 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6585 "2011 Unable to allocate memory for issuing "
6586 "SLI_CONFIG_SPECIAL mailbox command\n");
6587 return -ENOMEM;
6588 }
6589
6590 lpfc_read_config(phba, pmb);
6591
6592 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6593 if (rc != MBX_SUCCESS) {
6594 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6595 "2012 Mailbox failed , mbxCmd x%x "
6596 "READ_CONFIG, mbxStatus x%x\n",
6597 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6598 bf_get(lpfc_mqe_status, &pmb->u.mqe));
6599 rc = -EIO;
6600 } else {
6601 rd_config = &pmb->u.mqe.un.rd_config;
James Smartff78d8f2011-12-13 13:21:35 -05006602 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
6603 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
6604 phba->sli4_hba.lnk_info.lnk_tp =
6605 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
6606 phba->sli4_hba.lnk_info.lnk_no =
6607 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
6608 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6609 "3081 lnk_type:%d, lnk_numb:%d\n",
6610 phba->sli4_hba.lnk_info.lnk_tp,
6611 phba->sli4_hba.lnk_info.lnk_no);
6612 } else
6613 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6614 "3082 Mailbox (x%x) returned ldv:x0\n",
6615 bf_get(lpfc_mqe_command, &pmb->u.mqe));
James Smart6d368e52011-05-24 11:44:12 -04006616 phba->sli4_hba.extents_in_use =
6617 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04006618 phba->sli4_hba.max_cfg_param.max_xri =
6619 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
6620 phba->sli4_hba.max_cfg_param.xri_base =
6621 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
6622 phba->sli4_hba.max_cfg_param.max_vpi =
6623 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
6624 phba->sli4_hba.max_cfg_param.vpi_base =
6625 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
6626 phba->sli4_hba.max_cfg_param.max_rpi =
6627 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
6628 phba->sli4_hba.max_cfg_param.rpi_base =
6629 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
6630 phba->sli4_hba.max_cfg_param.max_vfi =
6631 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
6632 phba->sli4_hba.max_cfg_param.vfi_base =
6633 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
6634 phba->sli4_hba.max_cfg_param.max_fcfi =
6635 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04006636 phba->sli4_hba.max_cfg_param.max_eq =
6637 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
6638 phba->sli4_hba.max_cfg_param.max_rq =
6639 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
6640 phba->sli4_hba.max_cfg_param.max_wq =
6641 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
6642 phba->sli4_hba.max_cfg_param.max_cq =
6643 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
6644 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
6645 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
6646 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
6647 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
James Smart5ffc2662009-11-18 15:39:44 -05006648 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
6649 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04006650 phba->max_vports = phba->max_vpi;
6651 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart6d368e52011-05-24 11:44:12 -04006652 "2003 cfg params Extents? %d "
6653 "XRI(B:%d M:%d), "
James Smartda0436e2009-05-22 14:51:39 -04006654 "VPI(B:%d M:%d) "
6655 "VFI(B:%d M:%d) "
6656 "RPI(B:%d M:%d) "
James Smart6d368e52011-05-24 11:44:12 -04006657 "FCFI(Count:%d)\n",
6658 phba->sli4_hba.extents_in_use,
James Smartda0436e2009-05-22 14:51:39 -04006659 phba->sli4_hba.max_cfg_param.xri_base,
6660 phba->sli4_hba.max_cfg_param.max_xri,
6661 phba->sli4_hba.max_cfg_param.vpi_base,
6662 phba->sli4_hba.max_cfg_param.max_vpi,
6663 phba->sli4_hba.max_cfg_param.vfi_base,
6664 phba->sli4_hba.max_cfg_param.max_vfi,
6665 phba->sli4_hba.max_cfg_param.rpi_base,
6666 phba->sli4_hba.max_cfg_param.max_rpi,
James Smartda0436e2009-05-22 14:51:39 -04006667 phba->sli4_hba.max_cfg_param.max_fcfi);
6668 }
James Smart912e3ac2011-05-24 11:42:11 -04006669
6670 if (rc)
6671 goto read_cfg_out;
James Smartda0436e2009-05-22 14:51:39 -04006672
6673 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -04006674 length = phba->sli4_hba.max_cfg_param.max_xri -
6675 lpfc_sli4_get_els_iocb_cnt(phba);
6676 if (phba->cfg_hba_queue_depth > length) {
6677 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6678 "3361 HBA queue depth changed from %d to %d\n",
6679 phba->cfg_hba_queue_depth, length);
6680 phba->cfg_hba_queue_depth = length;
6681 }
James Smart912e3ac2011-05-24 11:42:11 -04006682
6683 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
6684 LPFC_SLI_INTF_IF_TYPE_2)
6685 goto read_cfg_out;
6686
6687 /* get the pf# and vf# for SLI4 if_type 2 port */
6688 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
6689 sizeof(struct lpfc_sli4_cfg_mhdr));
6690 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
6691 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
6692 length, LPFC_SLI4_MBX_EMBED);
6693
James Smart8aa134a2012-08-14 14:25:29 -04006694 rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart912e3ac2011-05-24 11:42:11 -04006695 shdr = (union lpfc_sli4_cfg_shdr *)
6696 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
6697 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6698 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart8aa134a2012-08-14 14:25:29 -04006699 if (rc2 || shdr_status || shdr_add_status) {
James Smart912e3ac2011-05-24 11:42:11 -04006700 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6701 "3026 Mailbox failed , mbxCmd x%x "
6702 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
6703 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6704 bf_get(lpfc_mqe_status, &pmb->u.mqe));
James Smart912e3ac2011-05-24 11:42:11 -04006705 goto read_cfg_out;
6706 }
6707
6708 /* search for fc_fcoe resrouce descriptor */
6709 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
6710 desc_count = get_func_cfg->func_cfg.rsrc_desc_count;
6711
James Smart8aa134a2012-08-14 14:25:29 -04006712 pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
6713 desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
6714 length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
6715 if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
6716 length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
6717 else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
6718 goto read_cfg_out;
6719
James Smart912e3ac2011-05-24 11:42:11 -04006720 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
James Smart8aa134a2012-08-14 14:25:29 -04006721 desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
James Smart912e3ac2011-05-24 11:42:11 -04006722 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
James Smart8aa134a2012-08-14 14:25:29 -04006723 bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
James Smart912e3ac2011-05-24 11:42:11 -04006724 phba->sli4_hba.iov.pf_number =
6725 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
6726 phba->sli4_hba.iov.vf_number =
6727 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
6728 break;
6729 }
6730 }
6731
6732 if (i < LPFC_RSRC_DESC_MAX_NUM)
6733 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6734 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
6735 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
6736 phba->sli4_hba.iov.vf_number);
James Smart8aa134a2012-08-14 14:25:29 -04006737 else
James Smart912e3ac2011-05-24 11:42:11 -04006738 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6739 "3028 GET_FUNCTION_CONFIG: failed to find "
6740 "Resrouce Descriptor:x%x\n",
6741 LPFC_RSRC_DESC_TYPE_FCFCOE);
James Smart912e3ac2011-05-24 11:42:11 -04006742
6743read_cfg_out:
6744 mempool_free(pmb, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04006745 return rc;
6746}
6747
6748/**
James Smart2fcee4b2010-12-15 17:57:46 -05006749 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
James Smartda0436e2009-05-22 14:51:39 -04006750 * @phba: pointer to lpfc hba data structure.
6751 *
James Smart2fcee4b2010-12-15 17:57:46 -05006752 * This routine is invoked to setup the port-side endian order when
6753 * the port if_type is 0. This routine has no function for other
6754 * if_types.
James Smartda0436e2009-05-22 14:51:39 -04006755 *
6756 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006757 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006758 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006759 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006760 **/
6761static int
6762lpfc_setup_endian_order(struct lpfc_hba *phba)
6763{
6764 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05006765 uint32_t if_type, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04006766 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
6767 HOST_ENDIAN_HIGH_WORD1};
6768
James Smart2fcee4b2010-12-15 17:57:46 -05006769 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
6770 switch (if_type) {
6771 case LPFC_SLI_INTF_IF_TYPE_0:
6772 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6773 GFP_KERNEL);
6774 if (!mboxq) {
6775 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6776 "0492 Unable to allocate memory for "
6777 "issuing SLI_CONFIG_SPECIAL mailbox "
6778 "command\n");
6779 return -ENOMEM;
6780 }
James Smartda0436e2009-05-22 14:51:39 -04006781
James Smart2fcee4b2010-12-15 17:57:46 -05006782 /*
6783 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
6784 * two words to contain special data values and no other data.
6785 */
6786 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
6787 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
6788 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6789 if (rc != MBX_SUCCESS) {
6790 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6791 "0493 SLI_CONFIG_SPECIAL mailbox "
6792 "failed with status x%x\n",
6793 rc);
6794 rc = -EIO;
6795 }
6796 mempool_free(mboxq, phba->mbox_mem_pool);
6797 break;
6798 case LPFC_SLI_INTF_IF_TYPE_2:
6799 case LPFC_SLI_INTF_IF_TYPE_1:
6800 default:
6801 break;
James Smartda0436e2009-05-22 14:51:39 -04006802 }
James Smartda0436e2009-05-22 14:51:39 -04006803 return rc;
6804}
6805
6806/**
James Smart5350d872011-10-10 21:33:49 -04006807 * lpfc_sli4_queue_verify - Verify and update EQ and CQ counts
James Smartda0436e2009-05-22 14:51:39 -04006808 * @phba: pointer to lpfc hba data structure.
6809 *
James Smart5350d872011-10-10 21:33:49 -04006810 * This routine is invoked to check the user settable queue counts for EQs and
6811 * CQs. after this routine is called the counts will be set to valid values that
6812 * adhere to the constraints of the system's interrupt vectors and the port's
6813 * queue resources.
James Smartda0436e2009-05-22 14:51:39 -04006814 *
6815 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006816 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006817 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04006818 **/
6819static int
James Smart5350d872011-10-10 21:33:49 -04006820lpfc_sli4_queue_verify(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04006821{
James Smart67d12732012-08-03 12:36:13 -04006822 int cfg_fcp_io_channel;
James Smart90695ee2012-08-14 14:25:36 -04006823 uint32_t cpu;
6824 uint32_t i = 0;
James Smartda0436e2009-05-22 14:51:39 -04006825
6826 /*
James Smart67d12732012-08-03 12:36:13 -04006827 * Sanity check for configured queue parameters against the run-time
James Smartda0436e2009-05-22 14:51:39 -04006828 * device parameters
6829 */
6830
James Smart67d12732012-08-03 12:36:13 -04006831 /* Sanity check on HBA EQ parameters */
6832 cfg_fcp_io_channel = phba->cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04006833
James Smart7bb03bb2013-04-17 20:19:16 -04006834 /* It doesn't make sense to have more io channels then online CPUs */
6835 for_each_present_cpu(cpu) {
6836 if (cpu_online(cpu))
6837 i++;
James Smart90695ee2012-08-14 14:25:36 -04006838 }
James Smart7bb03bb2013-04-17 20:19:16 -04006839 phba->sli4_hba.num_online_cpu = i;
James Smartb246de12013-05-31 17:03:07 -04006840 phba->sli4_hba.num_present_cpu = lpfc_present_cpu;
James Smart7bb03bb2013-04-17 20:19:16 -04006841
James Smart90695ee2012-08-14 14:25:36 -04006842 if (i < cfg_fcp_io_channel) {
James Smart82c3e9b2012-09-29 11:29:50 -04006843 lpfc_printf_log(phba,
6844 KERN_ERR, LOG_INIT,
James Smart90695ee2012-08-14 14:25:36 -04006845 "3188 Reducing IO channels to match number of "
James Smart7bb03bb2013-04-17 20:19:16 -04006846 "online CPUs: from %d to %d\n",
6847 cfg_fcp_io_channel, i);
James Smart90695ee2012-08-14 14:25:36 -04006848 cfg_fcp_io_channel = i;
6849 }
6850
James Smart67d12732012-08-03 12:36:13 -04006851 if (cfg_fcp_io_channel >
6852 phba->sli4_hba.max_cfg_param.max_eq) {
James Smart82c3e9b2012-09-29 11:29:50 -04006853 if (phba->sli4_hba.max_cfg_param.max_eq <
6854 LPFC_FCP_IO_CHAN_MIN) {
James Smartda0436e2009-05-22 14:51:39 -04006855 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6856 "2574 Not enough EQs (%d) from the "
6857 "pci function for supporting FCP "
6858 "EQs (%d)\n",
6859 phba->sli4_hba.max_cfg_param.max_eq,
James Smart67d12732012-08-03 12:36:13 -04006860 phba->cfg_fcp_io_channel);
James Smartda0436e2009-05-22 14:51:39 -04006861 goto out_error;
6862 }
James Smart82c3e9b2012-09-29 11:29:50 -04006863 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6864 "2575 Reducing IO channels to match number of "
6865 "available EQs: from %d to %d\n",
6866 cfg_fcp_io_channel,
6867 phba->sli4_hba.max_cfg_param.max_eq);
6868 cfg_fcp_io_channel = phba->sli4_hba.max_cfg_param.max_eq;
James Smartda0436e2009-05-22 14:51:39 -04006869 }
James Smart67d12732012-08-03 12:36:13 -04006870
James Smartda0436e2009-05-22 14:51:39 -04006871 /* The actual number of FCP event queues adopted */
James Smart67d12732012-08-03 12:36:13 -04006872 phba->cfg_fcp_io_channel = cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04006873
James Smartda0436e2009-05-22 14:51:39 -04006874 /* Get EQ depth from module parameter, fake the default for now */
6875 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
6876 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
6877
James Smart5350d872011-10-10 21:33:49 -04006878 /* Get CQ depth from module parameter, fake the default for now */
6879 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
6880 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
6881
6882 return 0;
6883out_error:
6884 return -ENOMEM;
6885}
6886
6887/**
6888 * lpfc_sli4_queue_create - Create all the SLI4 queues
6889 * @phba: pointer to lpfc hba data structure.
6890 *
6891 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
6892 * operation. For each SLI4 queue type, the parameters such as queue entry
6893 * count (queue depth) shall be taken from the module parameter. For now,
6894 * we just use some constant number as place holder.
6895 *
6896 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07006897 * 0 - successful
James Smart5350d872011-10-10 21:33:49 -04006898 * -ENOMEM - No availble memory
6899 * -EIO - The mailbox failed to complete successfully.
6900 **/
6901int
6902lpfc_sli4_queue_create(struct lpfc_hba *phba)
6903{
6904 struct lpfc_queue *qdesc;
James Smart67d12732012-08-03 12:36:13 -04006905 int idx;
James Smart5350d872011-10-10 21:33:49 -04006906
6907 /*
James Smart67d12732012-08-03 12:36:13 -04006908 * Create HBA Record arrays.
James Smart5350d872011-10-10 21:33:49 -04006909 */
James Smart67d12732012-08-03 12:36:13 -04006910 if (!phba->cfg_fcp_io_channel)
6911 return -ERANGE;
James Smart5350d872011-10-10 21:33:49 -04006912
James Smart67d12732012-08-03 12:36:13 -04006913 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
6914 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
6915 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
6916 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
6917 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
6918 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
6919
6920 phba->sli4_hba.hba_eq = kzalloc((sizeof(struct lpfc_queue *) *
6921 phba->cfg_fcp_io_channel), GFP_KERNEL);
6922 if (!phba->sli4_hba.hba_eq) {
James Smartda0436e2009-05-22 14:51:39 -04006923 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04006924 "2576 Failed allocate memory for "
6925 "fast-path EQ record array\n");
James Smartda0436e2009-05-22 14:51:39 -04006926 goto out_error;
6927 }
James Smart67d12732012-08-03 12:36:13 -04006928
6929 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
6930 phba->cfg_fcp_io_channel), GFP_KERNEL);
6931 if (!phba->sli4_hba.fcp_cq) {
6932 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6933 "2577 Failed allocate memory for fast-path "
6934 "CQ record array\n");
6935 goto out_error;
6936 }
6937
6938 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
6939 phba->cfg_fcp_io_channel), GFP_KERNEL);
6940 if (!phba->sli4_hba.fcp_wq) {
6941 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6942 "2578 Failed allocate memory for fast-path "
6943 "WQ record array\n");
6944 goto out_error;
6945 }
James Smartda0436e2009-05-22 14:51:39 -04006946
James Smart5350d872011-10-10 21:33:49 -04006947 /*
James Smart67d12732012-08-03 12:36:13 -04006948 * Since the first EQ can have multiple CQs associated with it,
6949 * this array is used to quickly see if we have a FCP fast-path
6950 * CQ match.
James Smart5350d872011-10-10 21:33:49 -04006951 */
James Smart67d12732012-08-03 12:36:13 -04006952 phba->sli4_hba.fcp_cq_map = kzalloc((sizeof(uint16_t) *
6953 phba->cfg_fcp_io_channel), GFP_KERNEL);
6954 if (!phba->sli4_hba.fcp_cq_map) {
6955 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6956 "2545 Failed allocate memory for fast-path "
6957 "CQ map\n");
6958 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04006959 }
James Smart67d12732012-08-03 12:36:13 -04006960
6961 /*
6962 * Create HBA Event Queues (EQs). The cfg_fcp_io_channel specifies
6963 * how many EQs to create.
6964 */
6965 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
6966
6967 /* Create EQs */
James Smartda0436e2009-05-22 14:51:39 -04006968 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
6969 phba->sli4_hba.eq_ecount);
6970 if (!qdesc) {
6971 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04006972 "0497 Failed allocate EQ (%d)\n", idx);
6973 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04006974 }
James Smart67d12732012-08-03 12:36:13 -04006975 phba->sli4_hba.hba_eq[idx] = qdesc;
6976
6977 /* Create Fast Path FCP CQs */
6978 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6979 phba->sli4_hba.cq_ecount);
6980 if (!qdesc) {
6981 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6982 "0499 Failed allocate fast-path FCP "
6983 "CQ (%d)\n", idx);
6984 goto out_error;
6985 }
6986 phba->sli4_hba.fcp_cq[idx] = qdesc;
6987
6988 /* Create Fast Path FCP WQs */
6989 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
6990 phba->sli4_hba.wq_ecount);
6991 if (!qdesc) {
6992 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6993 "0503 Failed allocate fast-path FCP "
6994 "WQ (%d)\n", idx);
6995 goto out_error;
6996 }
6997 phba->sli4_hba.fcp_wq[idx] = qdesc;
James Smartda0436e2009-05-22 14:51:39 -04006998 }
6999
James Smart67d12732012-08-03 12:36:13 -04007000
James Smartda0436e2009-05-22 14:51:39 -04007001 /*
James Smart67d12732012-08-03 12:36:13 -04007002 * Create Slow Path Completion Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04007003 */
7004
James Smartda0436e2009-05-22 14:51:39 -04007005 /* Create slow-path Mailbox Command Complete Queue */
7006 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7007 phba->sli4_hba.cq_ecount);
7008 if (!qdesc) {
7009 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7010 "0500 Failed allocate slow-path mailbox CQ\n");
James Smart67d12732012-08-03 12:36:13 -04007011 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007012 }
7013 phba->sli4_hba.mbx_cq = qdesc;
7014
7015 /* Create slow-path ELS Complete Queue */
7016 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7017 phba->sli4_hba.cq_ecount);
7018 if (!qdesc) {
7019 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7020 "0501 Failed allocate slow-path ELS CQ\n");
James Smart67d12732012-08-03 12:36:13 -04007021 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007022 }
7023 phba->sli4_hba.els_cq = qdesc;
7024
James Smartda0436e2009-05-22 14:51:39 -04007025
James Smart5350d872011-10-10 21:33:49 -04007026 /*
James Smart67d12732012-08-03 12:36:13 -04007027 * Create Slow Path Work Queues (WQs)
James Smart5350d872011-10-10 21:33:49 -04007028 */
James Smartda0436e2009-05-22 14:51:39 -04007029
7030 /* Create Mailbox Command Queue */
James Smartda0436e2009-05-22 14:51:39 -04007031
7032 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
7033 phba->sli4_hba.mq_ecount);
7034 if (!qdesc) {
7035 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7036 "0505 Failed allocate slow-path MQ\n");
James Smart67d12732012-08-03 12:36:13 -04007037 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007038 }
7039 phba->sli4_hba.mbx_wq = qdesc;
7040
7041 /*
James Smart67d12732012-08-03 12:36:13 -04007042 * Create ELS Work Queues
James Smartda0436e2009-05-22 14:51:39 -04007043 */
James Smartda0436e2009-05-22 14:51:39 -04007044
7045 /* Create slow-path ELS Work Queue */
7046 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
7047 phba->sli4_hba.wq_ecount);
7048 if (!qdesc) {
7049 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7050 "0504 Failed allocate slow-path ELS WQ\n");
James Smart67d12732012-08-03 12:36:13 -04007051 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007052 }
7053 phba->sli4_hba.els_wq = qdesc;
7054
James Smartda0436e2009-05-22 14:51:39 -04007055 /*
7056 * Create Receive Queue (RQ)
7057 */
James Smartda0436e2009-05-22 14:51:39 -04007058
7059 /* Create Receive Queue for header */
7060 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
7061 phba->sli4_hba.rq_ecount);
7062 if (!qdesc) {
7063 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7064 "0506 Failed allocate receive HRQ\n");
James Smart67d12732012-08-03 12:36:13 -04007065 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007066 }
7067 phba->sli4_hba.hdr_rq = qdesc;
7068
7069 /* Create Receive Queue for data */
7070 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
7071 phba->sli4_hba.rq_ecount);
7072 if (!qdesc) {
7073 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7074 "0507 Failed allocate receive DRQ\n");
James Smart67d12732012-08-03 12:36:13 -04007075 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007076 }
7077 phba->sli4_hba.dat_rq = qdesc;
7078
7079 return 0;
7080
James Smartda0436e2009-05-22 14:51:39 -04007081out_error:
James Smart67d12732012-08-03 12:36:13 -04007082 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007083 return -ENOMEM;
7084}
7085
7086/**
7087 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
7088 * @phba: pointer to lpfc hba data structure.
7089 *
7090 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
7091 * operation.
7092 *
7093 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007094 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007095 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007096 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007097 **/
James Smart5350d872011-10-10 21:33:49 -04007098void
James Smartda0436e2009-05-22 14:51:39 -04007099lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
7100{
James Smart67d12732012-08-03 12:36:13 -04007101 int idx;
7102
7103 if (phba->sli4_hba.hba_eq != NULL) {
7104 /* Release HBA event queue */
7105 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7106 if (phba->sli4_hba.hba_eq[idx] != NULL) {
7107 lpfc_sli4_queue_free(
7108 phba->sli4_hba.hba_eq[idx]);
7109 phba->sli4_hba.hba_eq[idx] = NULL;
7110 }
7111 }
7112 kfree(phba->sli4_hba.hba_eq);
7113 phba->sli4_hba.hba_eq = NULL;
7114 }
7115
7116 if (phba->sli4_hba.fcp_cq != NULL) {
7117 /* Release FCP completion queue */
7118 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7119 if (phba->sli4_hba.fcp_cq[idx] != NULL) {
7120 lpfc_sli4_queue_free(
7121 phba->sli4_hba.fcp_cq[idx]);
7122 phba->sli4_hba.fcp_cq[idx] = NULL;
7123 }
7124 }
7125 kfree(phba->sli4_hba.fcp_cq);
7126 phba->sli4_hba.fcp_cq = NULL;
7127 }
7128
7129 if (phba->sli4_hba.fcp_wq != NULL) {
7130 /* Release FCP work queue */
7131 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7132 if (phba->sli4_hba.fcp_wq[idx] != NULL) {
7133 lpfc_sli4_queue_free(
7134 phba->sli4_hba.fcp_wq[idx]);
7135 phba->sli4_hba.fcp_wq[idx] = NULL;
7136 }
7137 }
7138 kfree(phba->sli4_hba.fcp_wq);
7139 phba->sli4_hba.fcp_wq = NULL;
7140 }
7141
James Smart962bc512013-01-03 15:44:00 -05007142 if (phba->pci_bar0_memmap_p) {
7143 iounmap(phba->pci_bar0_memmap_p);
7144 phba->pci_bar0_memmap_p = NULL;
7145 }
7146 if (phba->pci_bar2_memmap_p) {
7147 iounmap(phba->pci_bar2_memmap_p);
7148 phba->pci_bar2_memmap_p = NULL;
7149 }
7150 if (phba->pci_bar4_memmap_p) {
7151 iounmap(phba->pci_bar4_memmap_p);
7152 phba->pci_bar4_memmap_p = NULL;
7153 }
7154
James Smart67d12732012-08-03 12:36:13 -04007155 /* Release FCP CQ mapping array */
7156 if (phba->sli4_hba.fcp_cq_map != NULL) {
7157 kfree(phba->sli4_hba.fcp_cq_map);
7158 phba->sli4_hba.fcp_cq_map = NULL;
7159 }
James Smartda0436e2009-05-22 14:51:39 -04007160
7161 /* Release mailbox command work queue */
James Smart67d12732012-08-03 12:36:13 -04007162 if (phba->sli4_hba.mbx_wq != NULL) {
7163 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
7164 phba->sli4_hba.mbx_wq = NULL;
7165 }
James Smartda0436e2009-05-22 14:51:39 -04007166
7167 /* Release ELS work queue */
James Smart67d12732012-08-03 12:36:13 -04007168 if (phba->sli4_hba.els_wq != NULL) {
7169 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
7170 phba->sli4_hba.els_wq = NULL;
7171 }
James Smartda0436e2009-05-22 14:51:39 -04007172
7173 /* Release unsolicited receive queue */
James Smart67d12732012-08-03 12:36:13 -04007174 if (phba->sli4_hba.hdr_rq != NULL) {
7175 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
7176 phba->sli4_hba.hdr_rq = NULL;
7177 }
7178 if (phba->sli4_hba.dat_rq != NULL) {
7179 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
7180 phba->sli4_hba.dat_rq = NULL;
7181 }
James Smartda0436e2009-05-22 14:51:39 -04007182
James Smartda0436e2009-05-22 14:51:39 -04007183 /* Release ELS complete queue */
James Smart67d12732012-08-03 12:36:13 -04007184 if (phba->sli4_hba.els_cq != NULL) {
7185 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
7186 phba->sli4_hba.els_cq = NULL;
7187 }
James Smartda0436e2009-05-22 14:51:39 -04007188
7189 /* Release mailbox command complete queue */
James Smart67d12732012-08-03 12:36:13 -04007190 if (phba->sli4_hba.mbx_cq != NULL) {
7191 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
7192 phba->sli4_hba.mbx_cq = NULL;
7193 }
James Smartda0436e2009-05-22 14:51:39 -04007194
7195 return;
7196}
7197
7198/**
7199 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
7200 * @phba: pointer to lpfc hba data structure.
7201 *
7202 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
7203 * operation.
7204 *
7205 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007206 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007207 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007208 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007209 **/
7210int
7211lpfc_sli4_queue_setup(struct lpfc_hba *phba)
7212{
James Smart2a76a282012-08-03 12:35:54 -04007213 struct lpfc_sli *psli = &phba->sli;
7214 struct lpfc_sli_ring *pring;
James Smartda0436e2009-05-22 14:51:39 -04007215 int rc = -ENOMEM;
7216 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
7217 int fcp_cq_index = 0;
James Smart962bc512013-01-03 15:44:00 -05007218 uint32_t shdr_status, shdr_add_status;
7219 union lpfc_sli4_cfg_shdr *shdr;
7220 LPFC_MBOXQ_t *mboxq;
7221 uint32_t length;
7222
7223 /* Check for dual-ULP support */
7224 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7225 if (!mboxq) {
7226 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7227 "3249 Unable to allocate memory for "
7228 "QUERY_FW_CFG mailbox command\n");
7229 return -ENOMEM;
7230 }
7231 length = (sizeof(struct lpfc_mbx_query_fw_config) -
7232 sizeof(struct lpfc_sli4_cfg_mhdr));
7233 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7234 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
7235 length, LPFC_SLI4_MBX_EMBED);
7236
7237 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7238
7239 shdr = (union lpfc_sli4_cfg_shdr *)
7240 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7241 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7242 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7243 if (shdr_status || shdr_add_status || rc) {
7244 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7245 "3250 QUERY_FW_CFG mailbox failed with status "
7246 "x%x add_status x%x, mbx status x%x\n",
7247 shdr_status, shdr_add_status, rc);
7248 if (rc != MBX_TIMEOUT)
7249 mempool_free(mboxq, phba->mbox_mem_pool);
7250 rc = -ENXIO;
7251 goto out_error;
7252 }
7253
7254 phba->sli4_hba.fw_func_mode =
7255 mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
7256 phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
7257 phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
7258 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7259 "3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
7260 "ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
7261 phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
7262
7263 if (rc != MBX_TIMEOUT)
7264 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04007265
7266 /*
James Smart67d12732012-08-03 12:36:13 -04007267 * Set up HBA Event Queues (EQs)
James Smartda0436e2009-05-22 14:51:39 -04007268 */
7269
James Smart67d12732012-08-03 12:36:13 -04007270 /* Set up HBA event queue */
7271 if (phba->cfg_fcp_io_channel && !phba->sli4_hba.hba_eq) {
James Smart2e90f4b2011-12-13 13:22:37 -05007272 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7273 "3147 Fast-path EQs not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007274 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007275 goto out_error;
James Smart2e90f4b2011-12-13 13:22:37 -05007276 }
James Smart67d12732012-08-03 12:36:13 -04007277 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
7278 if (!phba->sli4_hba.hba_eq[fcp_eqidx]) {
James Smartda0436e2009-05-22 14:51:39 -04007279 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7280 "0522 Fast-path EQ (%d) not "
7281 "allocated\n", fcp_eqidx);
James Smart1b511972011-12-13 13:23:09 -05007282 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007283 goto out_destroy_hba_eq;
James Smartda0436e2009-05-22 14:51:39 -04007284 }
James Smart67d12732012-08-03 12:36:13 -04007285 rc = lpfc_eq_create(phba, phba->sli4_hba.hba_eq[fcp_eqidx],
James Smartbf8dae82012-08-03 12:36:24 -04007286 (phba->cfg_fcp_imax / phba->cfg_fcp_io_channel));
James Smartda0436e2009-05-22 14:51:39 -04007287 if (rc) {
7288 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7289 "0523 Failed setup of fast-path EQ "
7290 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
James Smart67d12732012-08-03 12:36:13 -04007291 goto out_destroy_hba_eq;
James Smartda0436e2009-05-22 14:51:39 -04007292 }
7293 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04007294 "2584 HBA EQ setup: "
James Smartda0436e2009-05-22 14:51:39 -04007295 "queue[%d]-id=%d\n", fcp_eqidx,
James Smart67d12732012-08-03 12:36:13 -04007296 phba->sli4_hba.hba_eq[fcp_eqidx]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007297 }
7298
James Smart67d12732012-08-03 12:36:13 -04007299 /* Set up fast-path FCP Response Complete Queue */
7300 if (!phba->sli4_hba.fcp_cq) {
7301 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7302 "3148 Fast-path FCP CQ array not "
7303 "allocated\n");
7304 rc = -ENOMEM;
7305 goto out_destroy_hba_eq;
7306 }
7307
7308 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_io_channel; fcp_cqidx++) {
7309 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
7310 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7311 "0526 Fast-path FCP CQ (%d) not "
7312 "allocated\n", fcp_cqidx);
7313 rc = -ENOMEM;
7314 goto out_destroy_fcp_cq;
7315 }
7316 rc = lpfc_cq_create(phba, phba->sli4_hba.fcp_cq[fcp_cqidx],
7317 phba->sli4_hba.hba_eq[fcp_cqidx], LPFC_WCQ, LPFC_FCP);
7318 if (rc) {
7319 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7320 "0527 Failed setup of fast-path FCP "
7321 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
7322 goto out_destroy_fcp_cq;
7323 }
7324
7325 /* Setup fcp_cq_map for fast lookup */
7326 phba->sli4_hba.fcp_cq_map[fcp_cqidx] =
7327 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id;
7328
7329 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7330 "2588 FCP CQ setup: cq[%d]-id=%d, "
7331 "parent seq[%d]-id=%d\n",
7332 fcp_cqidx,
7333 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
7334 fcp_cqidx,
7335 phba->sli4_hba.hba_eq[fcp_cqidx]->queue_id);
7336 }
7337
7338 /* Set up fast-path FCP Work Queue */
7339 if (!phba->sli4_hba.fcp_wq) {
7340 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7341 "3149 Fast-path FCP WQ array not "
7342 "allocated\n");
7343 rc = -ENOMEM;
7344 goto out_destroy_fcp_cq;
7345 }
7346
7347 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_io_channel; fcp_wqidx++) {
7348 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
7349 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7350 "0534 Fast-path FCP WQ (%d) not "
7351 "allocated\n", fcp_wqidx);
7352 rc = -ENOMEM;
7353 goto out_destroy_fcp_wq;
7354 }
7355 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
7356 phba->sli4_hba.fcp_cq[fcp_wqidx],
7357 LPFC_FCP);
7358 if (rc) {
7359 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7360 "0535 Failed setup of fast-path FCP "
7361 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
7362 goto out_destroy_fcp_wq;
7363 }
7364
7365 /* Bind this WQ to the next FCP ring */
7366 pring = &psli->ring[MAX_SLI3_CONFIGURED_RINGS + fcp_wqidx];
7367 pring->sli.sli4.wqp = (void *)phba->sli4_hba.fcp_wq[fcp_wqidx];
7368 phba->sli4_hba.fcp_cq[fcp_wqidx]->pring = pring;
7369
7370 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7371 "2591 FCP WQ setup: wq[%d]-id=%d, "
7372 "parent cq[%d]-id=%d\n",
7373 fcp_wqidx,
7374 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
7375 fcp_cq_index,
7376 phba->sli4_hba.fcp_cq[fcp_wqidx]->queue_id);
7377 }
James Smartda0436e2009-05-22 14:51:39 -04007378 /*
7379 * Set up Complete Queues (CQs)
7380 */
7381
7382 /* Set up slow-path MBOX Complete Queue as the first CQ */
7383 if (!phba->sli4_hba.mbx_cq) {
7384 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7385 "0528 Mailbox CQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007386 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007387 goto out_destroy_fcp_wq;
James Smartda0436e2009-05-22 14:51:39 -04007388 }
James Smart67d12732012-08-03 12:36:13 -04007389 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq,
7390 phba->sli4_hba.hba_eq[0], LPFC_MCQ, LPFC_MBOX);
James Smartda0436e2009-05-22 14:51:39 -04007391 if (rc) {
7392 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7393 "0529 Failed setup of slow-path mailbox CQ: "
7394 "rc = 0x%x\n", rc);
James Smart67d12732012-08-03 12:36:13 -04007395 goto out_destroy_fcp_wq;
James Smartda0436e2009-05-22 14:51:39 -04007396 }
7397 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7398 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
7399 phba->sli4_hba.mbx_cq->queue_id,
James Smart67d12732012-08-03 12:36:13 -04007400 phba->sli4_hba.hba_eq[0]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007401
7402 /* Set up slow-path ELS Complete Queue */
7403 if (!phba->sli4_hba.els_cq) {
7404 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7405 "0530 ELS CQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007406 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04007407 goto out_destroy_mbx_cq;
7408 }
James Smart67d12732012-08-03 12:36:13 -04007409 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq,
7410 phba->sli4_hba.hba_eq[0], LPFC_WCQ, LPFC_ELS);
James Smartda0436e2009-05-22 14:51:39 -04007411 if (rc) {
7412 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7413 "0531 Failed setup of slow-path ELS CQ: "
7414 "rc = 0x%x\n", rc);
7415 goto out_destroy_mbx_cq;
7416 }
7417 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7418 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
7419 phba->sli4_hba.els_cq->queue_id,
James Smart67d12732012-08-03 12:36:13 -04007420 phba->sli4_hba.hba_eq[0]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007421
7422 /*
7423 * Set up all the Work Queues (WQs)
7424 */
7425
7426 /* Set up Mailbox Command Queue */
7427 if (!phba->sli4_hba.mbx_wq) {
7428 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7429 "0538 Slow-path MQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007430 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007431 goto out_destroy_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04007432 }
7433 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
7434 phba->sli4_hba.mbx_cq, LPFC_MBOX);
7435 if (rc) {
7436 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7437 "0539 Failed setup of slow-path MQ: "
7438 "rc = 0x%x\n", rc);
James Smart67d12732012-08-03 12:36:13 -04007439 goto out_destroy_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04007440 }
7441 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7442 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
7443 phba->sli4_hba.mbx_wq->queue_id,
7444 phba->sli4_hba.mbx_cq->queue_id);
7445
7446 /* Set up slow-path ELS Work Queue */
7447 if (!phba->sli4_hba.els_wq) {
7448 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7449 "0536 Slow-path ELS WQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007450 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04007451 goto out_destroy_mbx_wq;
7452 }
7453 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
7454 phba->sli4_hba.els_cq, LPFC_ELS);
7455 if (rc) {
7456 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7457 "0537 Failed setup of slow-path ELS WQ: "
7458 "rc = 0x%x\n", rc);
7459 goto out_destroy_mbx_wq;
7460 }
James Smart2a76a282012-08-03 12:35:54 -04007461
7462 /* Bind this WQ to the ELS ring */
7463 pring = &psli->ring[LPFC_ELS_RING];
7464 pring->sli.sli4.wqp = (void *)phba->sli4_hba.els_wq;
7465 phba->sli4_hba.els_cq->pring = pring;
7466
James Smartda0436e2009-05-22 14:51:39 -04007467 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7468 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
7469 phba->sli4_hba.els_wq->queue_id,
7470 phba->sli4_hba.els_cq->queue_id);
7471
James Smartda0436e2009-05-22 14:51:39 -04007472 /*
7473 * Create Receive Queue (RQ)
7474 */
7475 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
7476 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7477 "0540 Receive Queue not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007478 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007479 goto out_destroy_els_wq;
James Smartda0436e2009-05-22 14:51:39 -04007480 }
James Smart73d91e52011-10-10 21:32:10 -04007481
7482 lpfc_rq_adjust_repost(phba, phba->sli4_hba.hdr_rq, LPFC_ELS_HBQ);
7483 lpfc_rq_adjust_repost(phba, phba->sli4_hba.dat_rq, LPFC_ELS_HBQ);
7484
James Smartda0436e2009-05-22 14:51:39 -04007485 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04007486 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04007487 if (rc) {
7488 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7489 "0541 Failed setup of Receive Queue: "
7490 "rc = 0x%x\n", rc);
7491 goto out_destroy_fcp_wq;
7492 }
James Smart73d91e52011-10-10 21:32:10 -04007493
James Smartda0436e2009-05-22 14:51:39 -04007494 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7495 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
7496 "parent cq-id=%d\n",
7497 phba->sli4_hba.hdr_rq->queue_id,
7498 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04007499 phba->sli4_hba.els_cq->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007500 return 0;
7501
James Smart2e90f4b2011-12-13 13:22:37 -05007502out_destroy_els_wq:
James Smartda0436e2009-05-22 14:51:39 -04007503 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7504out_destroy_mbx_wq:
7505 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
James Smart2e90f4b2011-12-13 13:22:37 -05007506out_destroy_els_cq:
James Smartda0436e2009-05-22 14:51:39 -04007507 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
7508out_destroy_mbx_cq:
7509 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
James Smart67d12732012-08-03 12:36:13 -04007510out_destroy_fcp_wq:
7511 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
7512 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
7513out_destroy_fcp_cq:
7514 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
7515 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
7516out_destroy_hba_eq:
James Smartda0436e2009-05-22 14:51:39 -04007517 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
James Smart67d12732012-08-03 12:36:13 -04007518 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[fcp_eqidx]);
James Smartda0436e2009-05-22 14:51:39 -04007519out_error:
7520 return rc;
7521}
7522
7523/**
7524 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
7525 * @phba: pointer to lpfc hba data structure.
7526 *
7527 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
7528 * operation.
7529 *
7530 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007531 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007532 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007533 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007534 **/
7535void
7536lpfc_sli4_queue_unset(struct lpfc_hba *phba)
7537{
7538 int fcp_qidx;
7539
7540 /* Unset mailbox command work queue */
7541 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
7542 /* Unset ELS work queue */
7543 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7544 /* Unset unsolicited receive queue */
7545 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
7546 /* Unset FCP work queue */
James Smart67d12732012-08-03 12:36:13 -04007547 if (phba->sli4_hba.fcp_wq) {
7548 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
7549 fcp_qidx++)
7550 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
7551 }
James Smartda0436e2009-05-22 14:51:39 -04007552 /* Unset mailbox command complete queue */
7553 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
7554 /* Unset ELS complete queue */
7555 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
James Smartda0436e2009-05-22 14:51:39 -04007556 /* Unset FCP response complete queue */
James Smart2e90f4b2011-12-13 13:22:37 -05007557 if (phba->sli4_hba.fcp_cq) {
James Smart67d12732012-08-03 12:36:13 -04007558 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
7559 fcp_qidx++)
James Smart2e90f4b2011-12-13 13:22:37 -05007560 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05007561 }
James Smartda0436e2009-05-22 14:51:39 -04007562 /* Unset fast-path event queue */
James Smart67d12732012-08-03 12:36:13 -04007563 if (phba->sli4_hba.hba_eq) {
7564 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
James Smart2e90f4b2011-12-13 13:22:37 -05007565 fcp_qidx++)
James Smart67d12732012-08-03 12:36:13 -04007566 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[fcp_qidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05007567 }
James Smartda0436e2009-05-22 14:51:39 -04007568}
7569
7570/**
7571 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
7572 * @phba: pointer to lpfc hba data structure.
7573 *
7574 * This routine is invoked to allocate and set up a pool of completion queue
7575 * events. The body of the completion queue event is a completion queue entry
7576 * CQE. For now, this pool is used for the interrupt service routine to queue
7577 * the following HBA completion queue events for the worker thread to process:
7578 * - Mailbox asynchronous events
7579 * - Receive queue completion unsolicited events
7580 * Later, this can be used for all the slow-path events.
7581 *
7582 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007583 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007584 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04007585 **/
7586static int
7587lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
7588{
7589 struct lpfc_cq_event *cq_event;
7590 int i;
7591
7592 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
7593 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
7594 if (!cq_event)
7595 goto out_pool_create_fail;
7596 list_add_tail(&cq_event->list,
7597 &phba->sli4_hba.sp_cqe_event_pool);
7598 }
7599 return 0;
7600
7601out_pool_create_fail:
7602 lpfc_sli4_cq_event_pool_destroy(phba);
7603 return -ENOMEM;
7604}
7605
7606/**
7607 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
7608 * @phba: pointer to lpfc hba data structure.
7609 *
7610 * This routine is invoked to free the pool of completion queue events at
7611 * driver unload time. Note that, it is the responsibility of the driver
7612 * cleanup routine to free all the outstanding completion-queue events
7613 * allocated from this pool back into the pool before invoking this routine
7614 * to destroy the pool.
7615 **/
7616static void
7617lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
7618{
7619 struct lpfc_cq_event *cq_event, *next_cq_event;
7620
7621 list_for_each_entry_safe(cq_event, next_cq_event,
7622 &phba->sli4_hba.sp_cqe_event_pool, list) {
7623 list_del(&cq_event->list);
7624 kfree(cq_event);
7625 }
7626}
7627
7628/**
7629 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
7630 * @phba: pointer to lpfc hba data structure.
7631 *
7632 * This routine is the lock free version of the API invoked to allocate a
7633 * completion-queue event from the free pool.
7634 *
7635 * Return: Pointer to the newly allocated completion-queue event if successful
7636 * NULL otherwise.
7637 **/
7638struct lpfc_cq_event *
7639__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
7640{
7641 struct lpfc_cq_event *cq_event = NULL;
7642
7643 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
7644 struct lpfc_cq_event, list);
7645 return cq_event;
7646}
7647
7648/**
7649 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
7650 * @phba: pointer to lpfc hba data structure.
7651 *
7652 * This routine is the lock version of the API invoked to allocate a
7653 * completion-queue event from the free pool.
7654 *
7655 * Return: Pointer to the newly allocated completion-queue event if successful
7656 * NULL otherwise.
7657 **/
7658struct lpfc_cq_event *
7659lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
7660{
7661 struct lpfc_cq_event *cq_event;
7662 unsigned long iflags;
7663
7664 spin_lock_irqsave(&phba->hbalock, iflags);
7665 cq_event = __lpfc_sli4_cq_event_alloc(phba);
7666 spin_unlock_irqrestore(&phba->hbalock, iflags);
7667 return cq_event;
7668}
7669
7670/**
7671 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
7672 * @phba: pointer to lpfc hba data structure.
7673 * @cq_event: pointer to the completion queue event to be freed.
7674 *
7675 * This routine is the lock free version of the API invoked to release a
7676 * completion-queue event back into the free pool.
7677 **/
7678void
7679__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
7680 struct lpfc_cq_event *cq_event)
7681{
7682 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
7683}
7684
7685/**
7686 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
7687 * @phba: pointer to lpfc hba data structure.
7688 * @cq_event: pointer to the completion queue event to be freed.
7689 *
7690 * This routine is the lock version of the API invoked to release a
7691 * completion-queue event back into the free pool.
7692 **/
7693void
7694lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
7695 struct lpfc_cq_event *cq_event)
7696{
7697 unsigned long iflags;
7698 spin_lock_irqsave(&phba->hbalock, iflags);
7699 __lpfc_sli4_cq_event_release(phba, cq_event);
7700 spin_unlock_irqrestore(&phba->hbalock, iflags);
7701}
7702
7703/**
7704 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
7705 * @phba: pointer to lpfc hba data structure.
7706 *
7707 * This routine is to free all the pending completion-queue events to the
7708 * back into the free pool for device reset.
7709 **/
7710static void
7711lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
7712{
7713 LIST_HEAD(cqelist);
7714 struct lpfc_cq_event *cqe;
7715 unsigned long iflags;
7716
7717 /* Retrieve all the pending WCQEs from pending WCQE lists */
7718 spin_lock_irqsave(&phba->hbalock, iflags);
7719 /* Pending FCP XRI abort events */
7720 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
7721 &cqelist);
7722 /* Pending ELS XRI abort events */
7723 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
7724 &cqelist);
7725 /* Pending asynnc events */
7726 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
7727 &cqelist);
7728 spin_unlock_irqrestore(&phba->hbalock, iflags);
7729
7730 while (!list_empty(&cqelist)) {
7731 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
7732 lpfc_sli4_cq_event_release(phba, cqe);
7733 }
7734}
7735
7736/**
7737 * lpfc_pci_function_reset - Reset pci function.
7738 * @phba: pointer to lpfc hba data structure.
7739 *
7740 * This routine is invoked to request a PCI function reset. It will destroys
7741 * all resources assigned to the PCI function which originates this request.
7742 *
7743 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007744 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007745 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007746 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007747 **/
7748int
7749lpfc_pci_function_reset(struct lpfc_hba *phba)
7750{
7751 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05007752 uint32_t rc = 0, if_type;
James Smartda0436e2009-05-22 14:51:39 -04007753 uint32_t shdr_status, shdr_add_status;
James Smart2fcee4b2010-12-15 17:57:46 -05007754 uint32_t rdy_chk, num_resets = 0, reset_again = 0;
James Smartda0436e2009-05-22 14:51:39 -04007755 union lpfc_sli4_cfg_shdr *shdr;
James Smart2fcee4b2010-12-15 17:57:46 -05007756 struct lpfc_register reg_data;
James Smart2b81f942012-03-01 22:37:18 -05007757 uint16_t devid;
James Smartda0436e2009-05-22 14:51:39 -04007758
James Smart2fcee4b2010-12-15 17:57:46 -05007759 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7760 switch (if_type) {
7761 case LPFC_SLI_INTF_IF_TYPE_0:
7762 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
7763 GFP_KERNEL);
7764 if (!mboxq) {
7765 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7766 "0494 Unable to allocate memory for "
7767 "issuing SLI_FUNCTION_RESET mailbox "
7768 "command\n");
7769 return -ENOMEM;
7770 }
7771
7772 /* Setup PCI function reset mailbox-ioctl command */
7773 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7774 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
7775 LPFC_SLI4_MBX_EMBED);
7776 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7777 shdr = (union lpfc_sli4_cfg_shdr *)
7778 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7779 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7780 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
7781 &shdr->response);
7782 if (rc != MBX_TIMEOUT)
7783 mempool_free(mboxq, phba->mbox_mem_pool);
7784 if (shdr_status || shdr_add_status || rc) {
7785 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7786 "0495 SLI_FUNCTION_RESET mailbox "
7787 "failed with status x%x add_status x%x,"
7788 " mbx status x%x\n",
7789 shdr_status, shdr_add_status, rc);
7790 rc = -ENXIO;
7791 }
7792 break;
7793 case LPFC_SLI_INTF_IF_TYPE_2:
7794 for (num_resets = 0;
7795 num_resets < MAX_IF_TYPE_2_RESETS;
7796 num_resets++) {
7797 reg_data.word0 = 0;
7798 bf_set(lpfc_sliport_ctrl_end, &reg_data,
7799 LPFC_SLIPORT_LITTLE_ENDIAN);
7800 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
7801 LPFC_SLIPORT_INIT_PORT);
7802 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
7803 CTRLregaddr);
James Smart8fcb8ac2012-03-01 22:35:58 -05007804 /* flush */
James Smart2b81f942012-03-01 22:37:18 -05007805 pci_read_config_word(phba->pcidev,
7806 PCI_DEVICE_ID, &devid);
James Smart2fcee4b2010-12-15 17:57:46 -05007807 /*
7808 * Poll the Port Status Register and wait for RDY for
7809 * up to 10 seconds. If the port doesn't respond, treat
7810 * it as an error. If the port responds with RN, start
7811 * the loop again.
7812 */
7813 for (rdy_chk = 0; rdy_chk < 1000; rdy_chk++) {
James Smart73d91e52011-10-10 21:32:10 -04007814 msleep(10);
James Smart9940b972011-03-11 16:06:12 -05007815 if (lpfc_readl(phba->sli4_hba.u.if_type2.
7816 STATUSregaddr, &reg_data.word0)) {
7817 rc = -ENODEV;
James Smart73d91e52011-10-10 21:32:10 -04007818 goto out;
James Smart9940b972011-03-11 16:06:12 -05007819 }
James Smart6b5151f2012-01-18 16:24:06 -05007820 if (bf_get(lpfc_sliport_status_rn, &reg_data))
7821 reset_again++;
James Smart2fcee4b2010-12-15 17:57:46 -05007822 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
7823 break;
James Smart2fcee4b2010-12-15 17:57:46 -05007824 }
7825
7826 /*
7827 * If the port responds to the init request with
7828 * reset needed, delay for a bit and restart the loop.
7829 */
James Smart6b5151f2012-01-18 16:24:06 -05007830 if (reset_again && (rdy_chk < 1000)) {
James Smart2fcee4b2010-12-15 17:57:46 -05007831 msleep(10);
7832 reset_again = 0;
7833 continue;
7834 }
7835
7836 /* Detect any port errors. */
James Smart2fcee4b2010-12-15 17:57:46 -05007837 if ((bf_get(lpfc_sliport_status_err, &reg_data)) ||
7838 (rdy_chk >= 1000)) {
7839 phba->work_status[0] = readl(
7840 phba->sli4_hba.u.if_type2.ERR1regaddr);
7841 phba->work_status[1] = readl(
7842 phba->sli4_hba.u.if_type2.ERR2regaddr);
7843 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05007844 "2890 Port error detected during port "
James Smart8a9d2e82012-05-09 21:16:12 -04007845 "reset(%d): wait_tmo:%d ms, "
7846 "port status reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05007847 "error 1=0x%x, error 2=0x%x\n",
James Smart8a9d2e82012-05-09 21:16:12 -04007848 num_resets, rdy_chk*10,
7849 reg_data.word0,
James Smart2fcee4b2010-12-15 17:57:46 -05007850 phba->work_status[0],
7851 phba->work_status[1]);
7852 rc = -ENODEV;
7853 }
7854
7855 /*
7856 * Terminate the outer loop provided the Port indicated
7857 * ready within 10 seconds.
7858 */
7859 if (rdy_chk < 1000)
7860 break;
7861 }
James Smart05580562011-05-24 11:40:48 -04007862 /* delay driver action following IF_TYPE_2 function reset */
7863 msleep(100);
James Smart2fcee4b2010-12-15 17:57:46 -05007864 break;
7865 case LPFC_SLI_INTF_IF_TYPE_1:
7866 default:
7867 break;
James Smartda0436e2009-05-22 14:51:39 -04007868 }
7869
James Smart73d91e52011-10-10 21:32:10 -04007870out:
James Smart2fcee4b2010-12-15 17:57:46 -05007871 /* Catch the not-ready port failure after a port reset. */
James Smart229adb02013-04-17 20:16:51 -04007872 if (num_resets >= MAX_IF_TYPE_2_RESETS) {
7873 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7874 "3317 HBA not functional: IP Reset Failed "
7875 "after (%d) retries, try: "
7876 "echo fw_reset > board_mode\n", num_resets);
James Smart2fcee4b2010-12-15 17:57:46 -05007877 rc = -ENODEV;
James Smart229adb02013-04-17 20:16:51 -04007878 }
James Smart2fcee4b2010-12-15 17:57:46 -05007879
James Smartda0436e2009-05-22 14:51:39 -04007880 return rc;
7881}
7882
7883/**
James Smartda0436e2009-05-22 14:51:39 -04007884 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
7885 * @phba: pointer to lpfc hba data structure.
7886 *
7887 * This routine is invoked to set up the PCI device memory space for device
7888 * with SLI-4 interface spec.
7889 *
7890 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007891 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007892 * other values - error
7893 **/
7894static int
7895lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
7896{
7897 struct pci_dev *pdev;
7898 unsigned long bar0map_len, bar1map_len, bar2map_len;
7899 int error = -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -05007900 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04007901
7902 /* Obtain PCI device reference */
7903 if (!phba->pcidev)
7904 return error;
7905 else
7906 pdev = phba->pcidev;
7907
7908 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05007909 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
7910 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
7911 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
7912 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smartda0436e2009-05-22 14:51:39 -04007913 return error;
Michael Reed8e685972009-09-18 12:02:05 -05007914 }
7915 }
James Smartda0436e2009-05-22 14:51:39 -04007916
James Smart2fcee4b2010-12-15 17:57:46 -05007917 /*
7918 * The BARs and register set definitions and offset locations are
7919 * dependent on the if_type.
7920 */
7921 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
7922 &phba->sli4_hba.sli_intf.word0)) {
7923 return error;
7924 }
7925
7926 /* There is no SLI3 failback for SLI4 devices. */
7927 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
7928 LPFC_SLI_INTF_VALID) {
7929 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7930 "2894 SLI_INTF reg contents invalid "
7931 "sli_intf reg 0x%x\n",
7932 phba->sli4_hba.sli_intf.word0);
7933 return error;
7934 }
7935
7936 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7937 /*
7938 * Get the bus address of SLI4 device Bar regions and the
7939 * number of bytes required by each mapping. The mapping of the
7940 * particular PCI BARs regions is dependent on the type of
7941 * SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -04007942 */
James Smart1dfb5a42010-02-12 14:40:50 -05007943 if (pci_resource_start(pdev, 0)) {
7944 phba->pci_bar0_map = pci_resource_start(pdev, 0);
7945 bar0map_len = pci_resource_len(pdev, 0);
James Smart2fcee4b2010-12-15 17:57:46 -05007946
7947 /*
7948 * Map SLI4 PCI Config Space Register base to a kernel virtual
7949 * addr
7950 */
7951 phba->sli4_hba.conf_regs_memmap_p =
7952 ioremap(phba->pci_bar0_map, bar0map_len);
7953 if (!phba->sli4_hba.conf_regs_memmap_p) {
7954 dev_printk(KERN_ERR, &pdev->dev,
7955 "ioremap failed for SLI4 PCI config "
7956 "registers.\n");
7957 goto out;
7958 }
7959 /* Set up BAR0 PCI config space register memory map */
7960 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smart1dfb5a42010-02-12 14:40:50 -05007961 } else {
7962 phba->pci_bar0_map = pci_resource_start(pdev, 1);
7963 bar0map_len = pci_resource_len(pdev, 1);
James Smart2fcee4b2010-12-15 17:57:46 -05007964 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
7965 dev_printk(KERN_ERR, &pdev->dev,
7966 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
7967 goto out;
7968 }
7969 phba->sli4_hba.conf_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04007970 ioremap(phba->pci_bar0_map, bar0map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05007971 if (!phba->sli4_hba.conf_regs_memmap_p) {
7972 dev_printk(KERN_ERR, &pdev->dev,
7973 "ioremap failed for SLI4 PCI config "
7974 "registers.\n");
7975 goto out;
7976 }
7977 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smartda0436e2009-05-22 14:51:39 -04007978 }
7979
James Smartc31098c2011-04-16 11:03:33 -04007980 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
7981 (pci_resource_start(pdev, 2))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007982 /*
7983 * Map SLI4 if type 0 HBA Control Register base to a kernel
7984 * virtual address and setup the registers.
7985 */
7986 phba->pci_bar1_map = pci_resource_start(pdev, 2);
7987 bar1map_len = pci_resource_len(pdev, 2);
7988 phba->sli4_hba.ctrl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04007989 ioremap(phba->pci_bar1_map, bar1map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05007990 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
7991 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04007992 "ioremap failed for SLI4 HBA control registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05007993 goto out_iounmap_conf;
7994 }
7995 lpfc_sli4_bar1_register_memmap(phba);
James Smartda0436e2009-05-22 14:51:39 -04007996 }
7997
James Smartc31098c2011-04-16 11:03:33 -04007998 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
7999 (pci_resource_start(pdev, 4))) {
James Smart2fcee4b2010-12-15 17:57:46 -05008000 /*
8001 * Map SLI4 if type 0 HBA Doorbell Register base to a kernel
8002 * virtual address and setup the registers.
8003 */
8004 phba->pci_bar2_map = pci_resource_start(pdev, 4);
8005 bar2map_len = pci_resource_len(pdev, 4);
8006 phba->sli4_hba.drbl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04008007 ioremap(phba->pci_bar2_map, bar2map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05008008 if (!phba->sli4_hba.drbl_regs_memmap_p) {
8009 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04008010 "ioremap failed for SLI4 HBA doorbell registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05008011 goto out_iounmap_ctrl;
8012 }
8013 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
8014 if (error)
8015 goto out_iounmap_all;
James Smartda0436e2009-05-22 14:51:39 -04008016 }
8017
James Smartda0436e2009-05-22 14:51:39 -04008018 return 0;
8019
8020out_iounmap_all:
8021 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
8022out_iounmap_ctrl:
8023 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
8024out_iounmap_conf:
8025 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8026out:
8027 return error;
8028}
8029
8030/**
8031 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
8032 * @phba: pointer to lpfc hba data structure.
8033 *
8034 * This routine is invoked to unset the PCI device memory space for device
8035 * with SLI-4 interface spec.
8036 **/
8037static void
8038lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
8039{
James Smart2e90f4b2011-12-13 13:22:37 -05008040 uint32_t if_type;
8041 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smartda0436e2009-05-22 14:51:39 -04008042
James Smart2e90f4b2011-12-13 13:22:37 -05008043 switch (if_type) {
8044 case LPFC_SLI_INTF_IF_TYPE_0:
8045 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
8046 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
8047 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8048 break;
8049 case LPFC_SLI_INTF_IF_TYPE_2:
8050 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8051 break;
8052 case LPFC_SLI_INTF_IF_TYPE_1:
8053 default:
8054 dev_printk(KERN_ERR, &phba->pcidev->dev,
8055 "FATAL - unsupported SLI4 interface type - %d\n",
8056 if_type);
8057 break;
8058 }
James Smartda0436e2009-05-22 14:51:39 -04008059}
8060
8061/**
James Smart3772a992009-05-22 14:50:54 -04008062 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
8063 * @phba: pointer to lpfc hba data structure.
8064 *
8065 * This routine is invoked to enable the MSI-X interrupt vectors to device
8066 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
8067 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
8068 * invoked, enables either all or nothing, depending on the current
8069 * availability of PCI vector resources. The device driver is responsible
8070 * for calling the individual request_irq() to register each MSI-X vector
8071 * with a interrupt handler, which is done in this function. Note that
8072 * later when device is unloading, the driver should always call free_irq()
8073 * on all MSI-X vectors it has done request_irq() on before calling
8074 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
8075 * will be left with MSI-X enabled and leaks its vectors.
8076 *
8077 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008078 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008079 * other values - error
8080 **/
8081static int
8082lpfc_sli_enable_msix(struct lpfc_hba *phba)
8083{
8084 int rc, i;
8085 LPFC_MBOXQ_t *pmb;
8086
8087 /* Set up MSI-X multi-message vectors */
8088 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8089 phba->msix_entries[i].entry = i;
8090
8091 /* Configure MSI-X capability structure */
8092 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
8093 ARRAY_SIZE(phba->msix_entries));
8094 if (rc) {
8095 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8096 "0420 PCI enable MSI-X failed (%d)\n", rc);
8097 goto msi_fail_out;
8098 }
8099 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8100 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8101 "0477 MSI-X entry[%d]: vector=x%x "
8102 "message=%d\n", i,
8103 phba->msix_entries[i].vector,
8104 phba->msix_entries[i].entry);
8105 /*
8106 * Assign MSI-X vectors to interrupt handlers
8107 */
8108
8109 /* vector-0 is associated to slow-path handler */
8110 rc = request_irq(phba->msix_entries[0].vector,
8111 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
8112 LPFC_SP_DRIVER_HANDLER_NAME, phba);
8113 if (rc) {
8114 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8115 "0421 MSI-X slow-path request_irq failed "
8116 "(%d)\n", rc);
8117 goto msi_fail_out;
8118 }
8119
8120 /* vector-1 is associated to fast-path handler */
8121 rc = request_irq(phba->msix_entries[1].vector,
8122 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
8123 LPFC_FP_DRIVER_HANDLER_NAME, phba);
8124
8125 if (rc) {
8126 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8127 "0429 MSI-X fast-path request_irq failed "
8128 "(%d)\n", rc);
8129 goto irq_fail_out;
8130 }
8131
8132 /*
8133 * Configure HBA MSI-X attention conditions to messages
8134 */
8135 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8136
8137 if (!pmb) {
8138 rc = -ENOMEM;
8139 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8140 "0474 Unable to allocate memory for issuing "
8141 "MBOX_CONFIG_MSI command\n");
8142 goto mem_fail_out;
8143 }
8144 rc = lpfc_config_msi(phba, pmb);
8145 if (rc)
8146 goto mbx_fail_out;
8147 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
8148 if (rc != MBX_SUCCESS) {
8149 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
8150 "0351 Config MSI mailbox command failed, "
8151 "mbxCmd x%x, mbxStatus x%x\n",
8152 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
8153 goto mbx_fail_out;
8154 }
8155
8156 /* Free memory allocated for mailbox command */
8157 mempool_free(pmb, phba->mbox_mem_pool);
8158 return rc;
8159
8160mbx_fail_out:
8161 /* Free memory allocated for mailbox command */
8162 mempool_free(pmb, phba->mbox_mem_pool);
8163
8164mem_fail_out:
8165 /* free the irq already requested */
8166 free_irq(phba->msix_entries[1].vector, phba);
8167
8168irq_fail_out:
8169 /* free the irq already requested */
8170 free_irq(phba->msix_entries[0].vector, phba);
8171
8172msi_fail_out:
8173 /* Unconfigure MSI-X capability structure */
8174 pci_disable_msix(phba->pcidev);
8175 return rc;
8176}
8177
8178/**
8179 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
8180 * @phba: pointer to lpfc hba data structure.
8181 *
8182 * This routine is invoked to release the MSI-X vectors and then disable the
8183 * MSI-X interrupt mode to device with SLI-3 interface spec.
8184 **/
8185static void
8186lpfc_sli_disable_msix(struct lpfc_hba *phba)
8187{
8188 int i;
8189
8190 /* Free up MSI-X multi-message vectors */
8191 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8192 free_irq(phba->msix_entries[i].vector, phba);
8193 /* Disable MSI-X */
8194 pci_disable_msix(phba->pcidev);
8195
8196 return;
8197}
8198
8199/**
8200 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
8201 * @phba: pointer to lpfc hba data structure.
8202 *
8203 * This routine is invoked to enable the MSI interrupt mode to device with
8204 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
8205 * enable the MSI vector. The device driver is responsible for calling the
8206 * request_irq() to register MSI vector with a interrupt the handler, which
8207 * is done in this function.
8208 *
8209 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008210 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008211 * other values - error
8212 */
8213static int
8214lpfc_sli_enable_msi(struct lpfc_hba *phba)
8215{
8216 int rc;
8217
8218 rc = pci_enable_msi(phba->pcidev);
8219 if (!rc)
8220 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8221 "0462 PCI enable MSI mode success.\n");
8222 else {
8223 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8224 "0471 PCI enable MSI mode failed (%d)\n", rc);
8225 return rc;
8226 }
8227
8228 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
8229 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8230 if (rc) {
8231 pci_disable_msi(phba->pcidev);
8232 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8233 "0478 MSI request_irq failed (%d)\n", rc);
8234 }
8235 return rc;
8236}
8237
8238/**
8239 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
8240 * @phba: pointer to lpfc hba data structure.
8241 *
8242 * This routine is invoked to disable the MSI interrupt mode to device with
8243 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
8244 * done request_irq() on before calling pci_disable_msi(). Failure to do so
8245 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
8246 * its vector.
8247 */
8248static void
8249lpfc_sli_disable_msi(struct lpfc_hba *phba)
8250{
8251 free_irq(phba->pcidev->irq, phba);
8252 pci_disable_msi(phba->pcidev);
8253 return;
8254}
8255
8256/**
8257 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
8258 * @phba: pointer to lpfc hba data structure.
8259 *
8260 * This routine is invoked to enable device interrupt and associate driver's
8261 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
8262 * spec. Depends on the interrupt mode configured to the driver, the driver
8263 * will try to fallback from the configured interrupt mode to an interrupt
8264 * mode which is supported by the platform, kernel, and device in the order
8265 * of:
8266 * MSI-X -> MSI -> IRQ.
8267 *
8268 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008269 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008270 * other values - error
8271 **/
8272static uint32_t
8273lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
8274{
8275 uint32_t intr_mode = LPFC_INTR_ERROR;
8276 int retval;
8277
8278 if (cfg_mode == 2) {
8279 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
8280 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
8281 if (!retval) {
8282 /* Now, try to enable MSI-X interrupt mode */
8283 retval = lpfc_sli_enable_msix(phba);
8284 if (!retval) {
8285 /* Indicate initialization to MSI-X mode */
8286 phba->intr_type = MSIX;
8287 intr_mode = 2;
8288 }
8289 }
8290 }
8291
8292 /* Fallback to MSI if MSI-X initialization failed */
8293 if (cfg_mode >= 1 && phba->intr_type == NONE) {
8294 retval = lpfc_sli_enable_msi(phba);
8295 if (!retval) {
8296 /* Indicate initialization to MSI mode */
8297 phba->intr_type = MSI;
8298 intr_mode = 1;
8299 }
8300 }
8301
8302 /* Fallback to INTx if both MSI-X/MSI initalization failed */
8303 if (phba->intr_type == NONE) {
8304 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
8305 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8306 if (!retval) {
8307 /* Indicate initialization to INTx mode */
8308 phba->intr_type = INTx;
8309 intr_mode = 0;
8310 }
8311 }
8312 return intr_mode;
8313}
8314
8315/**
8316 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
8317 * @phba: pointer to lpfc hba data structure.
8318 *
8319 * This routine is invoked to disable device interrupt and disassociate the
8320 * driver's interrupt handler(s) from interrupt vector(s) to device with
8321 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
8322 * release the interrupt vector(s) for the message signaled interrupt.
8323 **/
8324static void
8325lpfc_sli_disable_intr(struct lpfc_hba *phba)
8326{
8327 /* Disable the currently initialized interrupt mode */
8328 if (phba->intr_type == MSIX)
8329 lpfc_sli_disable_msix(phba);
8330 else if (phba->intr_type == MSI)
8331 lpfc_sli_disable_msi(phba);
8332 else if (phba->intr_type == INTx)
8333 free_irq(phba->pcidev->irq, phba);
8334
8335 /* Reset interrupt management states */
8336 phba->intr_type = NONE;
8337 phba->sli.slistat.sli_intr = 0;
8338
8339 return;
8340}
8341
8342/**
James Smart7bb03bb2013-04-17 20:19:16 -04008343 * lpfc_find_next_cpu - Find next available CPU that matches the phys_id
8344 * @phba: pointer to lpfc hba data structure.
8345 *
8346 * Find next available CPU to use for IRQ to CPU affinity.
8347 */
8348static int
8349lpfc_find_next_cpu(struct lpfc_hba *phba, uint32_t phys_id)
8350{
8351 struct lpfc_vector_map_info *cpup;
8352 int cpu;
8353
8354 cpup = phba->sli4_hba.cpu_map;
8355 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8356 /* CPU must be online */
8357 if (cpu_online(cpu)) {
8358 if ((cpup->irq == LPFC_VECTOR_MAP_EMPTY) &&
8359 (lpfc_used_cpu[cpu] == LPFC_VECTOR_MAP_EMPTY) &&
8360 (cpup->phys_id == phys_id)) {
8361 return cpu;
8362 }
8363 }
8364 cpup++;
8365 }
8366
8367 /*
8368 * If we get here, we have used ALL CPUs for the specific
8369 * phys_id. Now we need to clear out lpfc_used_cpu and start
8370 * reusing CPUs.
8371 */
8372
8373 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8374 if (lpfc_used_cpu[cpu] == phys_id)
8375 lpfc_used_cpu[cpu] = LPFC_VECTOR_MAP_EMPTY;
8376 }
8377
8378 cpup = phba->sli4_hba.cpu_map;
8379 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8380 /* CPU must be online */
8381 if (cpu_online(cpu)) {
8382 if ((cpup->irq == LPFC_VECTOR_MAP_EMPTY) &&
8383 (cpup->phys_id == phys_id)) {
8384 return cpu;
8385 }
8386 }
8387 cpup++;
8388 }
8389 return LPFC_VECTOR_MAP_EMPTY;
8390}
8391
8392/**
8393 * lpfc_sli4_set_affinity - Set affinity for HBA IRQ vectors
8394 * @phba: pointer to lpfc hba data structure.
8395 * @vectors: number of HBA vectors
8396 *
8397 * Affinitize MSIX IRQ vectors to CPUs. Try to equally spread vector
8398 * affinization across multple physical CPUs (numa nodes).
8399 * In addition, this routine will assign an IO channel for each CPU
8400 * to use when issuing I/Os.
8401 */
8402static int
8403lpfc_sli4_set_affinity(struct lpfc_hba *phba, int vectors)
8404{
8405 int i, idx, saved_chann, used_chann, cpu, phys_id;
8406 int max_phys_id, num_io_channel, first_cpu;
8407 struct lpfc_vector_map_info *cpup;
8408#ifdef CONFIG_X86
8409 struct cpuinfo_x86 *cpuinfo;
8410#endif
8411 struct cpumask *mask;
8412 uint8_t chann[LPFC_FCP_IO_CHAN_MAX+1];
8413
8414 /* If there is no mapping, just return */
8415 if (!phba->cfg_fcp_cpu_map)
8416 return 1;
8417
8418 /* Init cpu_map array */
8419 memset(phba->sli4_hba.cpu_map, 0xff,
8420 (sizeof(struct lpfc_vector_map_info) *
8421 phba->sli4_hba.num_present_cpu));
8422
8423 max_phys_id = 0;
8424 phys_id = 0;
8425 num_io_channel = 0;
8426 first_cpu = LPFC_VECTOR_MAP_EMPTY;
8427
8428 /* Update CPU map with physical id and core id of each CPU */
8429 cpup = phba->sli4_hba.cpu_map;
8430 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8431#ifdef CONFIG_X86
8432 cpuinfo = &cpu_data(cpu);
8433 cpup->phys_id = cpuinfo->phys_proc_id;
8434 cpup->core_id = cpuinfo->cpu_core_id;
8435#else
8436 /* No distinction between CPUs for other platforms */
8437 cpup->phys_id = 0;
8438 cpup->core_id = 0;
8439#endif
8440
8441 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8442 "3328 CPU physid %d coreid %d\n",
8443 cpup->phys_id, cpup->core_id);
8444
8445 if (cpup->phys_id > max_phys_id)
8446 max_phys_id = cpup->phys_id;
8447 cpup++;
8448 }
8449
8450 /* Now associate the HBA vectors with specific CPUs */
8451 for (idx = 0; idx < vectors; idx++) {
8452 cpup = phba->sli4_hba.cpu_map;
8453 cpu = lpfc_find_next_cpu(phba, phys_id);
8454 if (cpu == LPFC_VECTOR_MAP_EMPTY) {
8455
8456 /* Try for all phys_id's */
8457 for (i = 1; i < max_phys_id; i++) {
8458 phys_id++;
8459 if (phys_id > max_phys_id)
8460 phys_id = 0;
8461 cpu = lpfc_find_next_cpu(phba, phys_id);
8462 if (cpu == LPFC_VECTOR_MAP_EMPTY)
8463 continue;
8464 goto found;
8465 }
8466
8467 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8468 "3329 Cannot set affinity:"
8469 "Error mapping vector %d (%d)\n",
8470 idx, vectors);
8471 return 0;
8472 }
8473found:
8474 cpup += cpu;
8475 if (phba->cfg_fcp_cpu_map == LPFC_DRIVER_CPU_MAP)
8476 lpfc_used_cpu[cpu] = phys_id;
8477
8478 /* Associate vector with selected CPU */
8479 cpup->irq = phba->sli4_hba.msix_entries[idx].vector;
8480
8481 /* Associate IO channel with selected CPU */
8482 cpup->channel_id = idx;
8483 num_io_channel++;
8484
8485 if (first_cpu == LPFC_VECTOR_MAP_EMPTY)
8486 first_cpu = cpu;
8487
8488 /* Now affinitize to the selected CPU */
8489 mask = &cpup->maskbits;
8490 cpumask_clear(mask);
8491 cpumask_set_cpu(cpu, mask);
8492 i = irq_set_affinity_hint(phba->sli4_hba.msix_entries[idx].
8493 vector, mask);
8494
8495 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8496 "3330 Set Affinity: CPU %d channel %d "
8497 "irq %d (%x)\n",
8498 cpu, cpup->channel_id,
8499 phba->sli4_hba.msix_entries[idx].vector, i);
8500
8501 /* Spread vector mapping across multple physical CPU nodes */
8502 phys_id++;
8503 if (phys_id > max_phys_id)
8504 phys_id = 0;
8505 }
8506
8507 /*
8508 * Finally fill in the IO channel for any remaining CPUs.
8509 * At this point, all IO channels have been assigned to a specific
8510 * MSIx vector, mapped to a specific CPU.
8511 * Base the remaining IO channel assigned, to IO channels already
8512 * assigned to other CPUs on the same phys_id.
8513 */
8514 for (i = 0; i <= max_phys_id; i++) {
8515 /*
8516 * If there are no io channels already mapped to
8517 * this phys_id, just round robin thru the io_channels.
8518 * Setup chann[] for round robin.
8519 */
8520 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++)
8521 chann[idx] = idx;
8522
8523 saved_chann = 0;
8524 used_chann = 0;
8525
8526 /*
8527 * First build a list of IO channels already assigned
8528 * to this phys_id before reassigning the same IO
8529 * channels to the remaining CPUs.
8530 */
8531 cpup = phba->sli4_hba.cpu_map;
8532 cpu = first_cpu;
8533 cpup += cpu;
8534 for (idx = 0; idx < phba->sli4_hba.num_present_cpu;
8535 idx++) {
8536 if (cpup->phys_id == i) {
8537 /*
8538 * Save any IO channels that are
8539 * already mapped to this phys_id.
8540 */
8541 if (cpup->irq != LPFC_VECTOR_MAP_EMPTY) {
8542 chann[saved_chann] =
8543 cpup->channel_id;
8544 saved_chann++;
8545 goto out;
8546 }
8547
8548 /* See if we are using round-robin */
8549 if (saved_chann == 0)
8550 saved_chann =
8551 phba->cfg_fcp_io_channel;
8552
8553 /* Associate next IO channel with CPU */
8554 cpup->channel_id = chann[used_chann];
8555 num_io_channel++;
8556 used_chann++;
8557 if (used_chann == saved_chann)
8558 used_chann = 0;
8559
8560 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8561 "3331 Set IO_CHANN "
8562 "CPU %d channel %d\n",
8563 idx, cpup->channel_id);
8564 }
8565out:
8566 cpu++;
8567 if (cpu >= phba->sli4_hba.num_present_cpu) {
8568 cpup = phba->sli4_hba.cpu_map;
8569 cpu = 0;
8570 } else {
8571 cpup++;
8572 }
8573 }
8574 }
8575
8576 if (phba->sli4_hba.num_online_cpu != phba->sli4_hba.num_present_cpu) {
8577 cpup = phba->sli4_hba.cpu_map;
8578 for (idx = 0; idx < phba->sli4_hba.num_present_cpu; idx++) {
8579 if (cpup->channel_id == LPFC_VECTOR_MAP_EMPTY) {
8580 cpup->channel_id = 0;
8581 num_io_channel++;
8582
8583 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8584 "3332 Assign IO_CHANN "
8585 "CPU %d channel %d\n",
8586 idx, cpup->channel_id);
8587 }
8588 cpup++;
8589 }
8590 }
8591
8592 /* Sanity check */
8593 if (num_io_channel != phba->sli4_hba.num_present_cpu)
8594 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8595 "3333 Set affinity mismatch:"
8596 "%d chann != %d cpus: %d vactors\n",
8597 num_io_channel, phba->sli4_hba.num_present_cpu,
8598 vectors);
8599
8600 phba->cfg_fcp_io_sched = LPFC_FCP_SCHED_BY_CPU;
8601 return 1;
8602}
8603
8604
8605/**
James Smartda0436e2009-05-22 14:51:39 -04008606 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
8607 * @phba: pointer to lpfc hba data structure.
8608 *
8609 * This routine is invoked to enable the MSI-X interrupt vectors to device
8610 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
8611 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
8612 * enables either all or nothing, depending on the current availability of
8613 * PCI vector resources. The device driver is responsible for calling the
8614 * individual request_irq() to register each MSI-X vector with a interrupt
8615 * handler, which is done in this function. Note that later when device is
8616 * unloading, the driver should always call free_irq() on all MSI-X vectors
8617 * it has done request_irq() on before calling pci_disable_msix(). Failure
8618 * to do so results in a BUG_ON() and a device will be left with MSI-X
8619 * enabled and leaks its vectors.
8620 *
8621 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008622 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008623 * other values - error
8624 **/
8625static int
8626lpfc_sli4_enable_msix(struct lpfc_hba *phba)
8627{
James Smart75baf692010-06-08 18:31:21 -04008628 int vectors, rc, index;
James Smartda0436e2009-05-22 14:51:39 -04008629
8630 /* Set up MSI-X multi-message vectors */
James Smart82c3e9b2012-09-29 11:29:50 -04008631 for (index = 0; index < phba->cfg_fcp_io_channel; index++)
James Smartda0436e2009-05-22 14:51:39 -04008632 phba->sli4_hba.msix_entries[index].entry = index;
8633
8634 /* Configure MSI-X capability structure */
James Smart82c3e9b2012-09-29 11:29:50 -04008635 vectors = phba->cfg_fcp_io_channel;
James Smart75baf692010-06-08 18:31:21 -04008636enable_msix_vectors:
James Smartda0436e2009-05-22 14:51:39 -04008637 rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
James Smart75baf692010-06-08 18:31:21 -04008638 vectors);
8639 if (rc > 1) {
8640 vectors = rc;
8641 goto enable_msix_vectors;
8642 } else if (rc) {
James Smartda0436e2009-05-22 14:51:39 -04008643 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8644 "0484 PCI enable MSI-X failed (%d)\n", rc);
8645 goto msi_fail_out;
8646 }
James Smart75baf692010-06-08 18:31:21 -04008647
James Smartda0436e2009-05-22 14:51:39 -04008648 /* Log MSI-X vector assignment */
James Smart75baf692010-06-08 18:31:21 -04008649 for (index = 0; index < vectors; index++)
James Smartda0436e2009-05-22 14:51:39 -04008650 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8651 "0489 MSI-X entry[%d]: vector=x%x "
8652 "message=%d\n", index,
8653 phba->sli4_hba.msix_entries[index].vector,
8654 phba->sli4_hba.msix_entries[index].entry);
James Smart67d12732012-08-03 12:36:13 -04008655
James Smart7bb03bb2013-04-17 20:19:16 -04008656 /* Assign MSI-X vectors to interrupt handlers */
James Smart67d12732012-08-03 12:36:13 -04008657 for (index = 0; index < vectors; index++) {
James Smart4305f182012-08-03 12:36:33 -04008658 memset(&phba->sli4_hba.handler_name[index], 0, 16);
8659 sprintf((char *)&phba->sli4_hba.handler_name[index],
8660 LPFC_DRIVER_HANDLER_NAME"%d", index);
James Smartda0436e2009-05-22 14:51:39 -04008661
James Smart67d12732012-08-03 12:36:13 -04008662 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8663 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
James Smartba20c852012-08-03 12:36:52 -04008664 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].fcp_eq_in_use, 1);
James Smartda0436e2009-05-22 14:51:39 -04008665 rc = request_irq(phba->sli4_hba.msix_entries[index].vector,
James Smart67d12732012-08-03 12:36:13 -04008666 &lpfc_sli4_hba_intr_handler, IRQF_SHARED,
James Smart4305f182012-08-03 12:36:33 -04008667 (char *)&phba->sli4_hba.handler_name[index],
James Smart67d12732012-08-03 12:36:13 -04008668 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -04008669 if (rc) {
8670 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8671 "0486 MSI-X fast-path (%d) "
8672 "request_irq failed (%d)\n", index, rc);
8673 goto cfg_fail_out;
8674 }
8675 }
8676
James Smart82c3e9b2012-09-29 11:29:50 -04008677 if (vectors != phba->cfg_fcp_io_channel) {
8678 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8679 "3238 Reducing IO channels to match number of "
8680 "MSI-X vectors, requested %d got %d\n",
8681 phba->cfg_fcp_io_channel, vectors);
8682 phba->cfg_fcp_io_channel = vectors;
8683 }
James Smart7bb03bb2013-04-17 20:19:16 -04008684
8685 lpfc_sli4_set_affinity(phba, vectors);
James Smartda0436e2009-05-22 14:51:39 -04008686 return rc;
8687
8688cfg_fail_out:
8689 /* free the irq already requested */
James Smart67d12732012-08-03 12:36:13 -04008690 for (--index; index >= 0; index--)
8691 free_irq(phba->sli4_hba.msix_entries[index].vector,
8692 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -04008693
8694msi_fail_out:
8695 /* Unconfigure MSI-X capability structure */
8696 pci_disable_msix(phba->pcidev);
8697 return rc;
8698}
8699
8700/**
8701 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
8702 * @phba: pointer to lpfc hba data structure.
8703 *
8704 * This routine is invoked to release the MSI-X vectors and then disable the
8705 * MSI-X interrupt mode to device with SLI-4 interface spec.
8706 **/
8707static void
8708lpfc_sli4_disable_msix(struct lpfc_hba *phba)
8709{
8710 int index;
8711
8712 /* Free up MSI-X multi-message vectors */
James Smart82c3e9b2012-09-29 11:29:50 -04008713 for (index = 0; index < phba->cfg_fcp_io_channel; index++)
James Smartda0436e2009-05-22 14:51:39 -04008714 free_irq(phba->sli4_hba.msix_entries[index].vector,
James Smart67d12732012-08-03 12:36:13 -04008715 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smart75baf692010-06-08 18:31:21 -04008716
James Smartda0436e2009-05-22 14:51:39 -04008717 /* Disable MSI-X */
8718 pci_disable_msix(phba->pcidev);
8719
8720 return;
8721}
8722
8723/**
8724 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
8725 * @phba: pointer to lpfc hba data structure.
8726 *
8727 * This routine is invoked to enable the MSI interrupt mode to device with
8728 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
8729 * to enable the MSI vector. The device driver is responsible for calling
8730 * the request_irq() to register MSI vector with a interrupt the handler,
8731 * which is done in this function.
8732 *
8733 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008734 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008735 * other values - error
8736 **/
8737static int
8738lpfc_sli4_enable_msi(struct lpfc_hba *phba)
8739{
8740 int rc, index;
8741
8742 rc = pci_enable_msi(phba->pcidev);
8743 if (!rc)
8744 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8745 "0487 PCI enable MSI mode success.\n");
8746 else {
8747 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8748 "0488 PCI enable MSI mode failed (%d)\n", rc);
8749 return rc;
8750 }
8751
8752 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
8753 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8754 if (rc) {
8755 pci_disable_msi(phba->pcidev);
8756 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8757 "0490 MSI request_irq failed (%d)\n", rc);
James Smart75baf692010-06-08 18:31:21 -04008758 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008759 }
8760
James Smart67d12732012-08-03 12:36:13 -04008761 for (index = 0; index < phba->cfg_fcp_io_channel; index++) {
James Smartda0436e2009-05-22 14:51:39 -04008762 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8763 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
8764 }
8765
James Smart75baf692010-06-08 18:31:21 -04008766 return 0;
James Smartda0436e2009-05-22 14:51:39 -04008767}
8768
8769/**
8770 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
8771 * @phba: pointer to lpfc hba data structure.
8772 *
8773 * This routine is invoked to disable the MSI interrupt mode to device with
8774 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
8775 * done request_irq() on before calling pci_disable_msi(). Failure to do so
8776 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
8777 * its vector.
8778 **/
8779static void
8780lpfc_sli4_disable_msi(struct lpfc_hba *phba)
8781{
8782 free_irq(phba->pcidev->irq, phba);
8783 pci_disable_msi(phba->pcidev);
8784 return;
8785}
8786
8787/**
8788 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
8789 * @phba: pointer to lpfc hba data structure.
8790 *
8791 * This routine is invoked to enable device interrupt and associate driver's
8792 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
8793 * interface spec. Depends on the interrupt mode configured to the driver,
8794 * the driver will try to fallback from the configured interrupt mode to an
8795 * interrupt mode which is supported by the platform, kernel, and device in
8796 * the order of:
8797 * MSI-X -> MSI -> IRQ.
8798 *
8799 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008800 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008801 * other values - error
8802 **/
8803static uint32_t
8804lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
8805{
8806 uint32_t intr_mode = LPFC_INTR_ERROR;
8807 int retval, index;
8808
8809 if (cfg_mode == 2) {
8810 /* Preparation before conf_msi mbox cmd */
8811 retval = 0;
8812 if (!retval) {
8813 /* Now, try to enable MSI-X interrupt mode */
8814 retval = lpfc_sli4_enable_msix(phba);
8815 if (!retval) {
8816 /* Indicate initialization to MSI-X mode */
8817 phba->intr_type = MSIX;
8818 intr_mode = 2;
8819 }
8820 }
8821 }
8822
8823 /* Fallback to MSI if MSI-X initialization failed */
8824 if (cfg_mode >= 1 && phba->intr_type == NONE) {
8825 retval = lpfc_sli4_enable_msi(phba);
8826 if (!retval) {
8827 /* Indicate initialization to MSI mode */
8828 phba->intr_type = MSI;
8829 intr_mode = 1;
8830 }
8831 }
8832
8833 /* Fallback to INTx if both MSI-X/MSI initalization failed */
8834 if (phba->intr_type == NONE) {
8835 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
8836 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8837 if (!retval) {
8838 /* Indicate initialization to INTx mode */
8839 phba->intr_type = INTx;
8840 intr_mode = 0;
James Smart67d12732012-08-03 12:36:13 -04008841 for (index = 0; index < phba->cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04008842 index++) {
8843 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8844 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
James Smartba20c852012-08-03 12:36:52 -04008845 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].
8846 fcp_eq_in_use, 1);
James Smartda0436e2009-05-22 14:51:39 -04008847 }
8848 }
8849 }
8850 return intr_mode;
8851}
8852
8853/**
8854 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
8855 * @phba: pointer to lpfc hba data structure.
8856 *
8857 * This routine is invoked to disable device interrupt and disassociate
8858 * the driver's interrupt handler(s) from interrupt vector(s) to device
8859 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
8860 * will release the interrupt vector(s) for the message signaled interrupt.
8861 **/
8862static void
8863lpfc_sli4_disable_intr(struct lpfc_hba *phba)
8864{
8865 /* Disable the currently initialized interrupt mode */
8866 if (phba->intr_type == MSIX)
8867 lpfc_sli4_disable_msix(phba);
8868 else if (phba->intr_type == MSI)
8869 lpfc_sli4_disable_msi(phba);
8870 else if (phba->intr_type == INTx)
8871 free_irq(phba->pcidev->irq, phba);
8872
8873 /* Reset interrupt management states */
8874 phba->intr_type = NONE;
8875 phba->sli.slistat.sli_intr = 0;
8876
8877 return;
8878}
8879
8880/**
James Smart3772a992009-05-22 14:50:54 -04008881 * lpfc_unset_hba - Unset SLI3 hba device initialization
8882 * @phba: pointer to lpfc hba data structure.
8883 *
8884 * This routine is invoked to unset the HBA device initialization steps to
8885 * a device with SLI-3 interface spec.
8886 **/
8887static void
8888lpfc_unset_hba(struct lpfc_hba *phba)
8889{
8890 struct lpfc_vport *vport = phba->pport;
8891 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
8892
8893 spin_lock_irq(shost->host_lock);
8894 vport->load_flag |= FC_UNLOADING;
8895 spin_unlock_irq(shost->host_lock);
8896
James Smart72859902012-01-18 16:25:38 -05008897 kfree(phba->vpi_bmask);
8898 kfree(phba->vpi_ids);
8899
James Smart3772a992009-05-22 14:50:54 -04008900 lpfc_stop_hba_timers(phba);
8901
8902 phba->pport->work_port_events = 0;
8903
8904 lpfc_sli_hba_down(phba);
8905
8906 lpfc_sli_brdrestart(phba);
8907
8908 lpfc_sli_disable_intr(phba);
8909
8910 return;
8911}
8912
8913/**
James Smart5af5eee2010-10-22 11:06:38 -04008914 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
8915 * @phba: Pointer to HBA context object.
8916 *
8917 * This function is called in the SLI4 code path to wait for completion
8918 * of device's XRIs exchange busy. It will check the XRI exchange busy
8919 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
8920 * that, it will check the XRI exchange busy on outstanding FCP and ELS
8921 * I/Os every 30 seconds, log error message, and wait forever. Only when
8922 * all XRI exchange busy complete, the driver unload shall proceed with
8923 * invoking the function reset ioctl mailbox command to the CNA and the
8924 * the rest of the driver unload resource release.
8925 **/
8926static void
8927lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
8928{
8929 int wait_time = 0;
8930 int fcp_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
8931 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
8932
8933 while (!fcp_xri_cmpl || !els_xri_cmpl) {
8934 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
8935 if (!fcp_xri_cmpl)
8936 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8937 "2877 FCP XRI exchange busy "
8938 "wait time: %d seconds.\n",
8939 wait_time/1000);
8940 if (!els_xri_cmpl)
8941 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8942 "2878 ELS XRI exchange busy "
8943 "wait time: %d seconds.\n",
8944 wait_time/1000);
8945 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
8946 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
8947 } else {
8948 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
8949 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
8950 }
8951 fcp_xri_cmpl =
8952 list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
8953 els_xri_cmpl =
8954 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
8955 }
8956}
8957
8958/**
James Smartda0436e2009-05-22 14:51:39 -04008959 * lpfc_sli4_hba_unset - Unset the fcoe hba
8960 * @phba: Pointer to HBA context object.
8961 *
8962 * This function is called in the SLI4 code path to reset the HBA's FCoE
8963 * function. The caller is not required to hold any lock. This routine
8964 * issues PCI function reset mailbox command to reset the FCoE function.
8965 * At the end of the function, it calls lpfc_hba_down_post function to
8966 * free any pending commands.
8967 **/
8968static void
8969lpfc_sli4_hba_unset(struct lpfc_hba *phba)
8970{
8971 int wait_cnt = 0;
8972 LPFC_MBOXQ_t *mboxq;
James Smart912e3ac2011-05-24 11:42:11 -04008973 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -04008974
8975 lpfc_stop_hba_timers(phba);
8976 phba->sli4_hba.intr_enable = 0;
8977
8978 /*
8979 * Gracefully wait out the potential current outstanding asynchronous
8980 * mailbox command.
8981 */
8982
8983 /* First, block any pending async mailbox command from posted */
8984 spin_lock_irq(&phba->hbalock);
8985 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
8986 spin_unlock_irq(&phba->hbalock);
8987 /* Now, trying to wait it out if we can */
8988 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8989 msleep(10);
8990 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
8991 break;
8992 }
8993 /* Forcefully release the outstanding mailbox command if timed out */
8994 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8995 spin_lock_irq(&phba->hbalock);
8996 mboxq = phba->sli.mbox_active;
8997 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8998 __lpfc_mbox_cmpl_put(phba, mboxq);
8999 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9000 phba->sli.mbox_active = NULL;
9001 spin_unlock_irq(&phba->hbalock);
9002 }
9003
James Smart5af5eee2010-10-22 11:06:38 -04009004 /* Abort all iocbs associated with the hba */
9005 lpfc_sli_hba_iocb_abort(phba);
9006
9007 /* Wait for completion of device XRI exchange busy */
9008 lpfc_sli4_xri_exchange_busy_wait(phba);
9009
James Smartda0436e2009-05-22 14:51:39 -04009010 /* Disable PCI subsystem interrupt */
9011 lpfc_sli4_disable_intr(phba);
9012
James Smart912e3ac2011-05-24 11:42:11 -04009013 /* Disable SR-IOV if enabled */
9014 if (phba->cfg_sriov_nr_virtfn)
9015 pci_disable_sriov(pdev);
9016
James Smartda0436e2009-05-22 14:51:39 -04009017 /* Stop kthread signal shall trigger work_done one more time */
9018 kthread_stop(phba->worker_thread);
9019
James Smart3677a3a2010-09-29 11:19:14 -04009020 /* Reset SLI4 HBA FCoE function */
9021 lpfc_pci_function_reset(phba);
James Smart5350d872011-10-10 21:33:49 -04009022 lpfc_sli4_queue_destroy(phba);
James Smart3677a3a2010-09-29 11:19:14 -04009023
James Smartda0436e2009-05-22 14:51:39 -04009024 /* Stop the SLI4 device port */
9025 phba->pport->work_port_events = 0;
9026}
9027
James Smart28baac72010-02-12 14:42:03 -05009028 /**
9029 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
9030 * @phba: Pointer to HBA context object.
9031 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
9032 *
9033 * This function is called in the SLI4 code path to read the port's
9034 * sli4 capabilities.
9035 *
9036 * This function may be be called from any context that can block-wait
9037 * for the completion. The expectation is that this routine is called
9038 * typically from probe_one or from the online routine.
9039 **/
9040int
9041lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9042{
9043 int rc;
9044 struct lpfc_mqe *mqe;
9045 struct lpfc_pc_sli4_params *sli4_params;
9046 uint32_t mbox_tmo;
9047
9048 rc = 0;
9049 mqe = &mboxq->u.mqe;
9050
9051 /* Read the port's SLI4 Parameters port capabilities */
James Smartfedd3b72011-02-16 12:39:24 -05009052 lpfc_pc_sli4_params(mboxq);
James Smart28baac72010-02-12 14:42:03 -05009053 if (!phba->sli4_hba.intr_enable)
9054 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9055 else {
James Smarta183a152011-10-10 21:32:43 -04009056 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart28baac72010-02-12 14:42:03 -05009057 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
9058 }
9059
9060 if (unlikely(rc))
9061 return 1;
9062
9063 sli4_params = &phba->sli4_hba.pc_sli4_params;
9064 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
9065 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
9066 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
9067 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
9068 &mqe->un.sli4_params);
9069 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
9070 &mqe->un.sli4_params);
9071 sli4_params->proto_types = mqe->un.sli4_params.word3;
9072 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
9073 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
9074 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
9075 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
9076 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
9077 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
9078 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
9079 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
9080 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
9081 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
9082 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
9083 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
9084 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
9085 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
9086 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
9087 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
9088 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
9089 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
9090 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
9091 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
James Smart05580562011-05-24 11:40:48 -04009092
9093 /* Make sure that sge_supp_len can be handled by the driver */
9094 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
9095 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
9096
James Smart28baac72010-02-12 14:42:03 -05009097 return rc;
9098}
9099
James Smartda0436e2009-05-22 14:51:39 -04009100/**
James Smartfedd3b72011-02-16 12:39:24 -05009101 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
9102 * @phba: Pointer to HBA context object.
9103 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
9104 *
9105 * This function is called in the SLI4 code path to read the port's
9106 * sli4 capabilities.
9107 *
9108 * This function may be be called from any context that can block-wait
9109 * for the completion. The expectation is that this routine is called
9110 * typically from probe_one or from the online routine.
9111 **/
9112int
9113lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9114{
9115 int rc;
9116 struct lpfc_mqe *mqe = &mboxq->u.mqe;
9117 struct lpfc_pc_sli4_params *sli4_params;
James Smarta183a152011-10-10 21:32:43 -04009118 uint32_t mbox_tmo;
James Smartfedd3b72011-02-16 12:39:24 -05009119 int length;
9120 struct lpfc_sli4_parameters *mbx_sli4_parameters;
9121
James Smart6d368e52011-05-24 11:44:12 -04009122 /*
9123 * By default, the driver assumes the SLI4 port requires RPI
9124 * header postings. The SLI4_PARAM response will correct this
9125 * assumption.
9126 */
9127 phba->sli4_hba.rpi_hdrs_in_use = 1;
9128
James Smartfedd3b72011-02-16 12:39:24 -05009129 /* Read the port's SLI4 Config Parameters */
9130 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
9131 sizeof(struct lpfc_sli4_cfg_mhdr));
9132 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
9133 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
9134 length, LPFC_SLI4_MBX_EMBED);
9135 if (!phba->sli4_hba.intr_enable)
9136 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -04009137 else {
9138 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
9139 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
9140 }
James Smartfedd3b72011-02-16 12:39:24 -05009141 if (unlikely(rc))
9142 return rc;
9143 sli4_params = &phba->sli4_hba.pc_sli4_params;
9144 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
9145 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
9146 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
9147 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
9148 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
9149 mbx_sli4_parameters);
9150 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
9151 mbx_sli4_parameters);
9152 if (bf_get(cfg_phwq, mbx_sli4_parameters))
9153 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
9154 else
9155 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
9156 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
9157 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
9158 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
9159 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
9160 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
9161 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
James Smart0c651872013-07-15 18:33:23 -04009162 sli4_params->wqsize = bf_get(cfg_wqsize, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -05009163 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
9164 mbx_sli4_parameters);
9165 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
9166 mbx_sli4_parameters);
James Smart6d368e52011-05-24 11:44:12 -04009167 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
9168 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
James Smart05580562011-05-24 11:40:48 -04009169
9170 /* Make sure that sge_supp_len can be handled by the driver */
9171 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
9172 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
9173
James Smartfedd3b72011-02-16 12:39:24 -05009174 return 0;
9175}
9176
9177/**
James Smart3772a992009-05-22 14:50:54 -04009178 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
9179 * @pdev: pointer to PCI device
9180 * @pid: pointer to PCI device identifier
9181 *
9182 * This routine is to be called to attach a device with SLI-3 interface spec
9183 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
9184 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
9185 * information of the device and driver to see if the driver state that it can
9186 * support this kind of device. If the match is successful, the driver core
9187 * invokes this routine. If this routine determines it can claim the HBA, it
9188 * does all the initialization that it needs to do to handle the HBA properly.
9189 *
9190 * Return code
9191 * 0 - driver can claim the device
9192 * negative value - driver can not claim the device
9193 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009194static int
James Smart3772a992009-05-22 14:50:54 -04009195lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
9196{
9197 struct lpfc_hba *phba;
9198 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04009199 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -04009200 int error;
9201 uint32_t cfg_mode, intr_mode;
9202
9203 /* Allocate memory for HBA structure */
9204 phba = lpfc_hba_alloc(pdev);
9205 if (!phba)
9206 return -ENOMEM;
9207
9208 /* Perform generic PCI device enabling operation */
9209 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -04009210 if (error)
James Smart3772a992009-05-22 14:50:54 -04009211 goto out_free_phba;
James Smart3772a992009-05-22 14:50:54 -04009212
9213 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
9214 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
9215 if (error)
9216 goto out_disable_pci_dev;
9217
9218 /* Set up SLI-3 specific device PCI memory space */
9219 error = lpfc_sli_pci_mem_setup(phba);
9220 if (error) {
9221 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9222 "1402 Failed to set up pci memory space.\n");
9223 goto out_disable_pci_dev;
9224 }
9225
9226 /* Set up phase-1 common device driver resources */
9227 error = lpfc_setup_driver_resource_phase1(phba);
9228 if (error) {
9229 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9230 "1403 Failed to set up driver resource.\n");
9231 goto out_unset_pci_mem_s3;
9232 }
9233
9234 /* Set up SLI-3 specific device driver resources */
9235 error = lpfc_sli_driver_resource_setup(phba);
9236 if (error) {
9237 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9238 "1404 Failed to set up driver resource.\n");
9239 goto out_unset_pci_mem_s3;
9240 }
9241
9242 /* Initialize and populate the iocb list per host */
9243 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
9244 if (error) {
9245 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9246 "1405 Failed to initialize iocb list.\n");
9247 goto out_unset_driver_resource_s3;
9248 }
9249
9250 /* Set up common device driver resources */
9251 error = lpfc_setup_driver_resource_phase2(phba);
9252 if (error) {
9253 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9254 "1406 Failed to set up driver resource.\n");
9255 goto out_free_iocb_list;
9256 }
9257
James Smart079b5c92011-08-21 21:48:49 -04009258 /* Get the default values for Model Name and Description */
9259 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
9260
James Smart3772a992009-05-22 14:50:54 -04009261 /* Create SCSI host to the physical port */
9262 error = lpfc_create_shost(phba);
9263 if (error) {
9264 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9265 "1407 Failed to create scsi host.\n");
9266 goto out_unset_driver_resource;
9267 }
9268
9269 /* Configure sysfs attributes */
9270 vport = phba->pport;
9271 error = lpfc_alloc_sysfs_attr(vport);
9272 if (error) {
9273 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9274 "1476 Failed to allocate sysfs attr\n");
9275 goto out_destroy_shost;
9276 }
9277
James Smart6669f9b2009-10-02 15:16:45 -04009278 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -04009279 /* Now, trying to enable interrupt and bring up the device */
9280 cfg_mode = phba->cfg_use_msi;
9281 while (true) {
9282 /* Put device to a known state before enabling interrupt */
9283 lpfc_stop_port(phba);
9284 /* Configure and enable interrupt */
9285 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
9286 if (intr_mode == LPFC_INTR_ERROR) {
9287 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9288 "0431 Failed to enable interrupt.\n");
9289 error = -ENODEV;
9290 goto out_free_sysfs_attr;
9291 }
9292 /* SLI-3 HBA setup */
9293 if (lpfc_sli_hba_setup(phba)) {
9294 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9295 "1477 Failed to set up hba\n");
9296 error = -ENODEV;
9297 goto out_remove_device;
9298 }
9299
9300 /* Wait 50ms for the interrupts of previous mailbox commands */
9301 msleep(50);
9302 /* Check active interrupts on message signaled interrupts */
9303 if (intr_mode == 0 ||
9304 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
9305 /* Log the current active interrupt mode */
9306 phba->intr_mode = intr_mode;
9307 lpfc_log_intr_mode(phba, intr_mode);
9308 break;
9309 } else {
9310 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9311 "0447 Configure interrupt mode (%d) "
9312 "failed active interrupt test.\n",
9313 intr_mode);
9314 /* Disable the current interrupt mode */
9315 lpfc_sli_disable_intr(phba);
9316 /* Try next level of interrupt mode */
9317 cfg_mode = --intr_mode;
9318 }
9319 }
9320
9321 /* Perform post initialization setup */
9322 lpfc_post_init_setup(phba);
9323
9324 /* Check if there are static vports to be created. */
9325 lpfc_create_static_vport(phba);
9326
9327 return 0;
9328
9329out_remove_device:
9330 lpfc_unset_hba(phba);
9331out_free_sysfs_attr:
9332 lpfc_free_sysfs_attr(vport);
9333out_destroy_shost:
9334 lpfc_destroy_shost(phba);
9335out_unset_driver_resource:
9336 lpfc_unset_driver_resource_phase2(phba);
9337out_free_iocb_list:
9338 lpfc_free_iocb_list(phba);
9339out_unset_driver_resource_s3:
9340 lpfc_sli_driver_resource_unset(phba);
9341out_unset_pci_mem_s3:
9342 lpfc_sli_pci_mem_unset(phba);
9343out_disable_pci_dev:
9344 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04009345 if (shost)
9346 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -04009347out_free_phba:
9348 lpfc_hba_free(phba);
9349 return error;
9350}
9351
9352/**
9353 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -04009354 * @pdev: pointer to PCI device
9355 *
James Smart3772a992009-05-22 14:50:54 -04009356 * This routine is to be called to disattach a device with SLI-3 interface
9357 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
9358 * removed from PCI bus, it performs all the necessary cleanup for the HBA
9359 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -04009360 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009361static void
James Smart3772a992009-05-22 14:50:54 -04009362lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -05009363{
James Smart2e0fef82007-06-17 19:56:36 -05009364 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9365 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -05009366 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -05009367 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -05009368 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05009369 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
9370
James Smart549e55c2007-08-02 11:09:51 -04009371 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04009372 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04009373 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009374
James Smart858c9f62007-06-17 19:56:39 -05009375 lpfc_free_sysfs_attr(vport);
9376
James Smarteada2722008-12-04 22:39:13 -05009377 /* Release all the vports against this physical port */
9378 vports = lpfc_create_vport_work_array(phba);
9379 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -04009380 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
9381 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
9382 continue;
James Smarteada2722008-12-04 22:39:13 -05009383 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -04009384 }
James Smarteada2722008-12-04 22:39:13 -05009385 lpfc_destroy_vport_work_array(phba, vports);
9386
9387 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -05009388 fc_remove_host(shost);
9389 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04009390 lpfc_cleanup(vport);
9391
James Smart2e0fef82007-06-17 19:56:36 -05009392 /*
9393 * Bring down the SLI Layer. This step disable all interrupts,
9394 * clears the rings, discards all mailbox commands, and resets
9395 * the HBA.
9396 */
James Smarta257bf92009-04-06 18:48:10 -04009397
Justin P. Mattock48e34d02010-12-30 15:07:58 -08009398 /* HBA interrupt will be disabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -05009399 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -04009400 /* Stop kthread signal shall trigger work_done one more time */
9401 kthread_stop(phba->worker_thread);
9402 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -05009403 lpfc_sli_brdrestart(phba);
9404
James Smart72859902012-01-18 16:25:38 -05009405 kfree(phba->vpi_bmask);
9406 kfree(phba->vpi_ids);
9407
James Smart3772a992009-05-22 14:50:54 -04009408 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05009409 spin_lock_irq(&phba->hbalock);
9410 list_del_init(&vport->listentry);
9411 spin_unlock_irq(&phba->hbalock);
9412
James Smart858c9f62007-06-17 19:56:39 -05009413 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05009414
James Smart912e3ac2011-05-24 11:42:11 -04009415 /* Disable SR-IOV if enabled */
9416 if (phba->cfg_sriov_nr_virtfn)
9417 pci_disable_sriov(pdev);
9418
James Smart5b75da22008-12-04 22:39:35 -05009419 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009420 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -05009421
James Smart858c9f62007-06-17 19:56:39 -05009422 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05009423
9424 /*
9425 * Call scsi_free before mem_free since scsi bufs are released to their
9426 * corresponding pools here.
9427 */
9428 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -04009429 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -05009430
James Smart34b02dc2008-08-24 21:49:55 -04009431 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
9432 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -05009433
James Smart2e0fef82007-06-17 19:56:36 -05009434 /* Free resources associated with SLI2 interface */
9435 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -04009436 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -05009437
9438 /* unmap adapter SLIM and Control Registers */
9439 iounmap(phba->ctrl_regs_memmap_p);
9440 iounmap(phba->slim_memmap_p);
9441
James Smart3772a992009-05-22 14:50:54 -04009442 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -05009443
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05009444 pci_release_selected_regions(pdev, bars);
James Smart2e0fef82007-06-17 19:56:36 -05009445 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05009446}
9447
Linas Vepstas8d63f372007-02-14 14:28:36 -06009448/**
James Smart3772a992009-05-22 14:50:54 -04009449 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05009450 * @pdev: pointer to PCI device
9451 * @msg: power management message
9452 *
James Smart3772a992009-05-22 14:50:54 -04009453 * This routine is to be called from the kernel's PCI subsystem to support
9454 * system Power Management (PM) to device with SLI-3 interface spec. When
9455 * PM invokes this method, it quiesces the device by stopping the driver's
9456 * worker thread for the device, turning off device's interrupt and DMA,
9457 * and bring the device offline. Note that as the driver implements the
9458 * minimum PM requirements to a power-aware driver's PM support for the
9459 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
9460 * to the suspend() method call will be treated as SUSPEND and the driver will
9461 * fully reinitialize its device during resume() method call, the driver will
9462 * set device to PCI_D3hot state in PCI config space instead of setting it
9463 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -05009464 *
9465 * Return code
James Smart3772a992009-05-22 14:50:54 -04009466 * 0 - driver suspended the device
9467 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05009468 **/
9469static int
James Smart3772a992009-05-22 14:50:54 -04009470lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -05009471{
9472 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9473 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9474
9475 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9476 "0473 PCI device Power Management suspend.\n");
9477
9478 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -04009479 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3a55b532008-12-04 22:38:54 -05009480 lpfc_offline(phba);
9481 kthread_stop(phba->worker_thread);
9482
9483 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -04009484 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -05009485
9486 /* Save device state to PCI config space */
9487 pci_save_state(pdev);
9488 pci_set_power_state(pdev, PCI_D3hot);
9489
9490 return 0;
9491}
9492
9493/**
James Smart3772a992009-05-22 14:50:54 -04009494 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05009495 * @pdev: pointer to PCI device
9496 *
James Smart3772a992009-05-22 14:50:54 -04009497 * This routine is to be called from the kernel's PCI subsystem to support
9498 * system Power Management (PM) to device with SLI-3 interface spec. When PM
9499 * invokes this method, it restores the device's PCI config space state and
9500 * fully reinitializes the device and brings it online. Note that as the
9501 * driver implements the minimum PM requirements to a power-aware driver's
9502 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
9503 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
9504 * driver will fully reinitialize its device during resume() method call,
9505 * the device will be set to PCI_D0 directly in PCI config space before
9506 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -05009507 *
9508 * Return code
James Smart3772a992009-05-22 14:50:54 -04009509 * 0 - driver suspended the device
9510 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05009511 **/
9512static int
James Smart3772a992009-05-22 14:50:54 -04009513lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -05009514{
9515 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9516 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -05009517 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05009518 int error;
9519
9520 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9521 "0452 PCI device Power Management resume.\n");
9522
9523 /* Restore device state from PCI config space */
9524 pci_set_power_state(pdev, PCI_D0);
9525 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -04009526
James Smart1dfb5a42010-02-12 14:40:50 -05009527 /*
9528 * As the new kernel behavior of pci_restore_state() API call clears
9529 * device saved_state flag, need to save the restored state again.
9530 */
9531 pci_save_state(pdev);
9532
James Smart3a55b532008-12-04 22:38:54 -05009533 if (pdev->is_busmaster)
9534 pci_set_master(pdev);
9535
9536 /* Startup the kernel thread for this host adapter. */
9537 phba->worker_thread = kthread_run(lpfc_do_work, phba,
9538 "lpfc_worker_%d", phba->brd_no);
9539 if (IS_ERR(phba->worker_thread)) {
9540 error = PTR_ERR(phba->worker_thread);
9541 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9542 "0434 PM resume failed to start worker "
9543 "thread: error=x%x.\n", error);
9544 return error;
9545 }
9546
James Smart5b75da22008-12-04 22:39:35 -05009547 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009548 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05009549 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -05009550 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -05009551 "0430 PM resume Failed to enable interrupt\n");
9552 return -EIO;
9553 } else
9554 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05009555
9556 /* Restart HBA and bring it online */
9557 lpfc_sli_brdrestart(phba);
9558 lpfc_online(phba);
9559
James Smart5b75da22008-12-04 22:39:35 -05009560 /* Log the current active interrupt mode */
9561 lpfc_log_intr_mode(phba, phba->intr_mode);
9562
James Smart3a55b532008-12-04 22:38:54 -05009563 return 0;
9564}
9565
9566/**
James Smart891478a2009-11-18 15:40:23 -05009567 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
9568 * @phba: pointer to lpfc hba data structure.
9569 *
9570 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -04009571 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -05009572 **/
9573static void
9574lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
9575{
James Smarte2af0d22010-03-15 11:25:32 -04009576 struct lpfc_sli *psli = &phba->sli;
9577 struct lpfc_sli_ring *pring;
9578
James Smart891478a2009-11-18 15:40:23 -05009579 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9580 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -04009581
9582 /*
9583 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
9584 * and let the SCSI mid-layer to retry them to recover.
9585 */
9586 pring = &psli->ring[psli->fcp_ring];
9587 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart891478a2009-11-18 15:40:23 -05009588}
9589
9590/**
James Smart0d878412009-10-02 15:16:56 -04009591 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
9592 * @phba: pointer to lpfc hba data structure.
9593 *
9594 * This routine is called to prepare the SLI3 device for PCI slot reset. It
9595 * disables the device interrupt and pci device, and aborts the internal FCP
9596 * pending I/Os.
9597 **/
9598static void
9599lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
9600{
James Smart0d878412009-10-02 15:16:56 -04009601 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05009602 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -04009603
James Smart75baf692010-06-08 18:31:21 -04009604 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -04009605 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -04009606
James Smarte2af0d22010-03-15 11:25:32 -04009607 /* Block all SCSI devices' I/Os on the host */
9608 lpfc_scsi_dev_block(phba);
9609
James Smartea714f32013-04-17 20:18:47 -04009610 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
9611 lpfc_sli_flush_fcp_rings(phba);
9612
James Smarte2af0d22010-03-15 11:25:32 -04009613 /* stop all timers */
9614 lpfc_stop_hba_timers(phba);
9615
James Smart0d878412009-10-02 15:16:56 -04009616 /* Disable interrupt and pci device */
9617 lpfc_sli_disable_intr(phba);
9618 pci_disable_device(phba->pcidev);
James Smart0d878412009-10-02 15:16:56 -04009619}
9620
9621/**
9622 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
9623 * @phba: pointer to lpfc hba data structure.
9624 *
9625 * This routine is called to prepare the SLI3 device for PCI slot permanently
9626 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
9627 * pending I/Os.
9628 **/
9629static void
James Smart75baf692010-06-08 18:31:21 -04009630lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
James Smart0d878412009-10-02 15:16:56 -04009631{
9632 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05009633 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -04009634 /* Block all SCSI devices' I/Os on the host */
9635 lpfc_scsi_dev_block(phba);
9636
9637 /* stop all timers */
9638 lpfc_stop_hba_timers(phba);
9639
James Smart0d878412009-10-02 15:16:56 -04009640 /* Clean up all driver's outstanding SCSI I/Os */
9641 lpfc_sli_flush_fcp_rings(phba);
9642}
9643
9644/**
James Smart3772a992009-05-22 14:50:54 -04009645 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -04009646 * @pdev: pointer to PCI device.
9647 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009648 *
James Smart3772a992009-05-22 14:50:54 -04009649 * This routine is called from the PCI subsystem for I/O error handling to
9650 * device with SLI-3 interface spec. This function is called by the PCI
9651 * subsystem after a PCI bus error affecting this device has been detected.
9652 * When this function is invoked, it will need to stop all the I/Os and
9653 * interrupt(s) to the device. Once that is done, it will return
9654 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
9655 * as desired.
James Smarte59058c2008-08-24 21:49:00 -04009656 *
9657 * Return codes
James Smart0d878412009-10-02 15:16:56 -04009658 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -04009659 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
9660 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -04009661 **/
James Smart3772a992009-05-22 14:50:54 -04009662static pci_ers_result_t
9663lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009664{
James Smart51ef4c22007-08-02 11:10:31 -04009665 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9666 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009667
James Smart0d878412009-10-02 15:16:56 -04009668 switch (state) {
9669 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -05009670 /* Non-fatal error, prepare for recovery */
9671 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -04009672 return PCI_ERS_RESULT_CAN_RECOVER;
9673 case pci_channel_io_frozen:
9674 /* Fatal error, prepare for slot reset */
9675 lpfc_sli_prep_dev_for_reset(phba);
9676 return PCI_ERS_RESULT_NEED_RESET;
9677 case pci_channel_io_perm_failure:
9678 /* Permanent failure, prepare for device down */
James Smart75baf692010-06-08 18:31:21 -04009679 lpfc_sli_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009680 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -04009681 default:
9682 /* Unknown state, prepare and request slot reset */
9683 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9684 "0472 Unknown PCI error state: x%x\n", state);
9685 lpfc_sli_prep_dev_for_reset(phba);
9686 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -04009687 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06009688}
9689
9690/**
James Smart3772a992009-05-22 14:50:54 -04009691 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -04009692 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009693 *
James Smart3772a992009-05-22 14:50:54 -04009694 * This routine is called from the PCI subsystem for error handling to
9695 * device with SLI-3 interface spec. This is called after PCI bus has been
9696 * reset to restart the PCI card from scratch, as if from a cold-boot.
9697 * During the PCI subsystem error recovery, after driver returns
9698 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
9699 * recovery and then call this routine before calling the .resume method
9700 * to recover the device. This function will initialize the HBA device,
9701 * enable the interrupt, but it will just put the HBA to offline state
9702 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -04009703 *
9704 * Return codes
James Smart3772a992009-05-22 14:50:54 -04009705 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
9706 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -06009707 */
James Smart3772a992009-05-22 14:50:54 -04009708static pci_ers_result_t
9709lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009710{
James Smart51ef4c22007-08-02 11:10:31 -04009711 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9712 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009713 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -05009714 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009715
9716 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +11009717 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -06009718 printk(KERN_ERR "lpfc: Cannot re-enable "
9719 "PCI device after reset.\n");
9720 return PCI_ERS_RESULT_DISCONNECT;
9721 }
9722
James Smart97207482008-12-04 22:39:19 -05009723 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05009724
9725 /*
9726 * As the new kernel behavior of pci_restore_state() API call clears
9727 * device saved_state flag, need to save the restored state again.
9728 */
9729 pci_save_state(pdev);
9730
James Smart97207482008-12-04 22:39:19 -05009731 if (pdev->is_busmaster)
9732 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009733
James Smart92d7f7b2007-06-17 19:56:38 -05009734 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04009735 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05009736 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009737
James Smart5b75da22008-12-04 22:39:35 -05009738 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009739 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05009740 if (intr_mode == LPFC_INTR_ERROR) {
9741 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9742 "0427 Cannot re-enable interrupt after "
9743 "slot reset.\n");
9744 return PCI_ERS_RESULT_DISCONNECT;
9745 } else
9746 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009747
James Smart75baf692010-06-08 18:31:21 -04009748 /* Take device offline, it will perform cleanup */
James Smart618a5232012-06-12 13:54:36 -04009749 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009750 lpfc_offline(phba);
9751 lpfc_sli_brdrestart(phba);
9752
James Smart5b75da22008-12-04 22:39:35 -05009753 /* Log the current active interrupt mode */
9754 lpfc_log_intr_mode(phba, phba->intr_mode);
9755
Linas Vepstas8d63f372007-02-14 14:28:36 -06009756 return PCI_ERS_RESULT_RECOVERED;
9757}
9758
9759/**
James Smart3772a992009-05-22 14:50:54 -04009760 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -04009761 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -06009762 *
James Smart3772a992009-05-22 14:50:54 -04009763 * This routine is called from the PCI subsystem for error handling to device
9764 * with SLI-3 interface spec. It is called when kernel error recovery tells
9765 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
9766 * error recovery. After this call, traffic can start to flow from this device
9767 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009768 */
James Smart3772a992009-05-22 14:50:54 -04009769static void
9770lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009771{
James Smart51ef4c22007-08-02 11:10:31 -04009772 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9773 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009774
James Smarte2af0d22010-03-15 11:25:32 -04009775 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -04009776 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -04009777
9778 /* Clean up Advanced Error Reporting (AER) if needed */
9779 if (phba->hba_flag & HBA_AER_ENABLED)
9780 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009781}
9782
James Smart3772a992009-05-22 14:50:54 -04009783/**
James Smartda0436e2009-05-22 14:51:39 -04009784 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
9785 * @phba: pointer to lpfc hba data structure.
9786 *
9787 * returns the number of ELS/CT IOCBs to reserve
9788 **/
9789int
9790lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
9791{
9792 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
9793
James Smartf1126682009-06-10 17:22:44 -04009794 if (phba->sli_rev == LPFC_SLI_REV4) {
9795 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -04009796 return 10;
James Smartf1126682009-06-10 17:22:44 -04009797 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -04009798 return 25;
James Smartf1126682009-06-10 17:22:44 -04009799 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -04009800 return 50;
James Smartf1126682009-06-10 17:22:44 -04009801 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -04009802 return 100;
James Smart8a9d2e82012-05-09 21:16:12 -04009803 else if (max_xri <= 1536)
James Smart6a9c52c2009-10-02 15:16:51 -04009804 return 150;
James Smart8a9d2e82012-05-09 21:16:12 -04009805 else if (max_xri <= 2048)
9806 return 200;
9807 else
9808 return 250;
James Smartf1126682009-06-10 17:22:44 -04009809 } else
9810 return 0;
James Smartda0436e2009-05-22 14:51:39 -04009811}
9812
9813/**
James Smart52d52442011-05-24 11:42:45 -04009814 * lpfc_write_firmware - attempt to write a firmware image to the port
James Smart52d52442011-05-24 11:42:45 -04009815 * @fw: pointer to firmware image returned from request_firmware.
James Smartce396282012-09-29 11:30:56 -04009816 * @phba: pointer to lpfc hba data structure.
James Smart52d52442011-05-24 11:42:45 -04009817 *
James Smart52d52442011-05-24 11:42:45 -04009818 **/
James Smartce396282012-09-29 11:30:56 -04009819static void
9820lpfc_write_firmware(const struct firmware *fw, void *context)
James Smart52d52442011-05-24 11:42:45 -04009821{
James Smartce396282012-09-29 11:30:56 -04009822 struct lpfc_hba *phba = (struct lpfc_hba *)context;
James Smart6b5151f2012-01-18 16:24:06 -05009823 char fwrev[FW_REV_STR_SIZE];
James Smartce396282012-09-29 11:30:56 -04009824 struct lpfc_grp_hdr *image;
James Smart52d52442011-05-24 11:42:45 -04009825 struct list_head dma_buffer_list;
9826 int i, rc = 0;
9827 struct lpfc_dmabuf *dmabuf, *next;
9828 uint32_t offset = 0, temp_offset = 0;
9829
James Smartc71ab862012-10-31 14:44:33 -04009830 /* It can be null in no-wait mode, sanity check */
James Smartce396282012-09-29 11:30:56 -04009831 if (!fw) {
9832 rc = -ENXIO;
9833 goto out;
9834 }
9835 image = (struct lpfc_grp_hdr *)fw->data;
9836
James Smart52d52442011-05-24 11:42:45 -04009837 INIT_LIST_HEAD(&dma_buffer_list);
James Smart079b5c92011-08-21 21:48:49 -04009838 if ((be32_to_cpu(image->magic_number) != LPFC_GROUP_OJECT_MAGIC_NUM) ||
9839 (bf_get_be32(lpfc_grp_hdr_file_type, image) !=
9840 LPFC_FILE_TYPE_GROUP) ||
9841 (bf_get_be32(lpfc_grp_hdr_id, image) != LPFC_FILE_ID_GROUP) ||
9842 (be32_to_cpu(image->size) != fw->size)) {
James Smart52d52442011-05-24 11:42:45 -04009843 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9844 "3022 Invalid FW image found. "
James Smart079b5c92011-08-21 21:48:49 -04009845 "Magic:%x Type:%x ID:%x\n",
9846 be32_to_cpu(image->magic_number),
9847 bf_get_be32(lpfc_grp_hdr_file_type, image),
9848 bf_get_be32(lpfc_grp_hdr_id, image));
James Smartce396282012-09-29 11:30:56 -04009849 rc = -EINVAL;
9850 goto release_out;
James Smart52d52442011-05-24 11:42:45 -04009851 }
9852 lpfc_decode_firmware_rev(phba, fwrev, 1);
James Smart88a2cfb2011-07-22 18:36:33 -04009853 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
James Smart52d52442011-05-24 11:42:45 -04009854 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartce396282012-09-29 11:30:56 -04009855 "3023 Updating Firmware, Current Version:%s "
James Smart52d52442011-05-24 11:42:45 -04009856 "New Version:%s\n",
James Smart88a2cfb2011-07-22 18:36:33 -04009857 fwrev, image->revision);
James Smart52d52442011-05-24 11:42:45 -04009858 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
9859 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
9860 GFP_KERNEL);
9861 if (!dmabuf) {
9862 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -04009863 goto release_out;
James Smart52d52442011-05-24 11:42:45 -04009864 }
9865 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
9866 SLI4_PAGE_SIZE,
9867 &dmabuf->phys,
9868 GFP_KERNEL);
9869 if (!dmabuf->virt) {
9870 kfree(dmabuf);
9871 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -04009872 goto release_out;
James Smart52d52442011-05-24 11:42:45 -04009873 }
9874 list_add_tail(&dmabuf->list, &dma_buffer_list);
9875 }
9876 while (offset < fw->size) {
9877 temp_offset = offset;
9878 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
James Smart079b5c92011-08-21 21:48:49 -04009879 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
James Smart52d52442011-05-24 11:42:45 -04009880 memcpy(dmabuf->virt,
9881 fw->data + temp_offset,
James Smart079b5c92011-08-21 21:48:49 -04009882 fw->size - temp_offset);
9883 temp_offset = fw->size;
James Smart52d52442011-05-24 11:42:45 -04009884 break;
9885 }
James Smart52d52442011-05-24 11:42:45 -04009886 memcpy(dmabuf->virt, fw->data + temp_offset,
9887 SLI4_PAGE_SIZE);
James Smart88a2cfb2011-07-22 18:36:33 -04009888 temp_offset += SLI4_PAGE_SIZE;
James Smart52d52442011-05-24 11:42:45 -04009889 }
9890 rc = lpfc_wr_object(phba, &dma_buffer_list,
9891 (fw->size - offset), &offset);
James Smartce396282012-09-29 11:30:56 -04009892 if (rc)
9893 goto release_out;
James Smart52d52442011-05-24 11:42:45 -04009894 }
9895 rc = offset;
9896 }
James Smartce396282012-09-29 11:30:56 -04009897
9898release_out:
James Smart52d52442011-05-24 11:42:45 -04009899 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
9900 list_del(&dmabuf->list);
9901 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
9902 dmabuf->virt, dmabuf->phys);
9903 kfree(dmabuf);
9904 }
James Smartce396282012-09-29 11:30:56 -04009905 release_firmware(fw);
9906out:
9907 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartc71ab862012-10-31 14:44:33 -04009908 "3024 Firmware update done: %d.\n", rc);
James Smartce396282012-09-29 11:30:56 -04009909 return;
James Smart52d52442011-05-24 11:42:45 -04009910}
9911
9912/**
James Smartc71ab862012-10-31 14:44:33 -04009913 * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
9914 * @phba: pointer to lpfc hba data structure.
9915 *
9916 * This routine is called to perform Linux generic firmware upgrade on device
9917 * that supports such feature.
9918 **/
9919int
9920lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
9921{
9922 uint8_t file_name[ELX_MODEL_NAME_SIZE];
9923 int ret;
9924 const struct firmware *fw;
9925
9926 /* Only supported on SLI4 interface type 2 for now */
9927 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
9928 LPFC_SLI_INTF_IF_TYPE_2)
9929 return -EPERM;
9930
9931 snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
9932
9933 if (fw_upgrade == INT_FW_UPGRADE) {
9934 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
9935 file_name, &phba->pcidev->dev,
9936 GFP_KERNEL, (void *)phba,
9937 lpfc_write_firmware);
9938 } else if (fw_upgrade == RUN_FW_UPGRADE) {
9939 ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
9940 if (!ret)
9941 lpfc_write_firmware(fw, (void *)phba);
9942 } else {
9943 ret = -EINVAL;
9944 }
9945
9946 return ret;
9947}
9948
9949/**
James Smartda0436e2009-05-22 14:51:39 -04009950 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
9951 * @pdev: pointer to PCI device
9952 * @pid: pointer to PCI device identifier
9953 *
9954 * This routine is called from the kernel's PCI subsystem to device with
9955 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
9956 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
9957 * information of the device and driver to see if the driver state that it
9958 * can support this kind of device. If the match is successful, the driver
9959 * core invokes this routine. If this routine determines it can claim the HBA,
9960 * it does all the initialization that it needs to do to handle the HBA
9961 * properly.
9962 *
9963 * Return code
9964 * 0 - driver can claim the device
9965 * negative value - driver can not claim the device
9966 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009967static int
James Smartda0436e2009-05-22 14:51:39 -04009968lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
9969{
9970 struct lpfc_hba *phba;
9971 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04009972 struct Scsi_Host *shost = NULL;
James Smartce396282012-09-29 11:30:56 -04009973 int error, ret;
James Smartda0436e2009-05-22 14:51:39 -04009974 uint32_t cfg_mode, intr_mode;
James Smart67d12732012-08-03 12:36:13 -04009975 int adjusted_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04009976
9977 /* Allocate memory for HBA structure */
9978 phba = lpfc_hba_alloc(pdev);
9979 if (!phba)
9980 return -ENOMEM;
9981
9982 /* Perform generic PCI device enabling operation */
9983 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -04009984 if (error)
James Smartda0436e2009-05-22 14:51:39 -04009985 goto out_free_phba;
James Smartda0436e2009-05-22 14:51:39 -04009986
9987 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
9988 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
9989 if (error)
9990 goto out_disable_pci_dev;
9991
9992 /* Set up SLI-4 specific device PCI memory space */
9993 error = lpfc_sli4_pci_mem_setup(phba);
9994 if (error) {
9995 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9996 "1410 Failed to set up pci memory space.\n");
9997 goto out_disable_pci_dev;
9998 }
9999
10000 /* Set up phase-1 common device driver resources */
10001 error = lpfc_setup_driver_resource_phase1(phba);
10002 if (error) {
10003 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10004 "1411 Failed to set up driver resource.\n");
10005 goto out_unset_pci_mem_s4;
10006 }
10007
10008 /* Set up SLI-4 Specific device driver resources */
10009 error = lpfc_sli4_driver_resource_setup(phba);
10010 if (error) {
10011 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10012 "1412 Failed to set up driver resource.\n");
10013 goto out_unset_pci_mem_s4;
10014 }
10015
10016 /* Initialize and populate the iocb list per host */
James Smart2a9bf3d2010-06-07 15:24:45 -040010017
10018 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10019 "2821 initialize iocb list %d.\n",
10020 phba->cfg_iocb_cnt*1024);
10021 error = lpfc_init_iocb_list(phba, phba->cfg_iocb_cnt*1024);
10022
James Smartda0436e2009-05-22 14:51:39 -040010023 if (error) {
10024 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10025 "1413 Failed to initialize iocb list.\n");
10026 goto out_unset_driver_resource_s4;
10027 }
10028
James Smart19ca7602010-11-20 23:11:55 -050010029 INIT_LIST_HEAD(&phba->active_rrq_list);
James Smart7d791df2011-07-22 18:37:52 -040010030 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
James Smart19ca7602010-11-20 23:11:55 -050010031
James Smartda0436e2009-05-22 14:51:39 -040010032 /* Set up common device driver resources */
10033 error = lpfc_setup_driver_resource_phase2(phba);
10034 if (error) {
10035 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10036 "1414 Failed to set up driver resource.\n");
10037 goto out_free_iocb_list;
10038 }
10039
James Smart079b5c92011-08-21 21:48:49 -040010040 /* Get the default values for Model Name and Description */
10041 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
10042
James Smartda0436e2009-05-22 14:51:39 -040010043 /* Create SCSI host to the physical port */
10044 error = lpfc_create_shost(phba);
10045 if (error) {
10046 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10047 "1415 Failed to create scsi host.\n");
10048 goto out_unset_driver_resource;
10049 }
10050
10051 /* Configure sysfs attributes */
10052 vport = phba->pport;
10053 error = lpfc_alloc_sysfs_attr(vport);
10054 if (error) {
10055 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10056 "1416 Failed to allocate sysfs attr\n");
10057 goto out_destroy_shost;
10058 }
10059
James Smart6669f9b2009-10-02 15:16:45 -040010060 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -040010061 /* Now, trying to enable interrupt and bring up the device */
10062 cfg_mode = phba->cfg_use_msi;
James Smartda0436e2009-05-22 14:51:39 -040010063
James Smart7b15db32013-01-03 15:43:29 -050010064 /* Put device to a known state before enabling interrupt */
10065 lpfc_stop_port(phba);
10066 /* Configure and enable interrupt */
10067 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
10068 if (intr_mode == LPFC_INTR_ERROR) {
10069 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10070 "0426 Failed to enable interrupt.\n");
10071 error = -ENODEV;
10072 goto out_free_sysfs_attr;
James Smartda0436e2009-05-22 14:51:39 -040010073 }
James Smart7b15db32013-01-03 15:43:29 -050010074 /* Default to single EQ for non-MSI-X */
10075 if (phba->intr_type != MSIX)
10076 adjusted_fcp_io_channel = 1;
10077 else
10078 adjusted_fcp_io_channel = phba->cfg_fcp_io_channel;
10079 phba->cfg_fcp_io_channel = adjusted_fcp_io_channel;
10080 /* Set up SLI-4 HBA */
10081 if (lpfc_sli4_hba_setup(phba)) {
10082 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10083 "1421 Failed to set up hba\n");
10084 error = -ENODEV;
10085 goto out_disable_intr;
10086 }
10087
10088 /* Log the current active interrupt mode */
10089 phba->intr_mode = intr_mode;
10090 lpfc_log_intr_mode(phba, intr_mode);
James Smartda0436e2009-05-22 14:51:39 -040010091
10092 /* Perform post initialization setup */
10093 lpfc_post_init_setup(phba);
10094
James Smartc71ab862012-10-31 14:44:33 -040010095 /* check for firmware upgrade or downgrade */
10096 if (phba->cfg_request_firmware_upgrade)
10097 ret = lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
James Smart52d52442011-05-24 11:42:45 -040010098
James Smart1c6834a2009-07-19 10:01:26 -040010099 /* Check if there are static vports to be created. */
10100 lpfc_create_static_vport(phba);
James Smartda0436e2009-05-22 14:51:39 -040010101 return 0;
10102
10103out_disable_intr:
10104 lpfc_sli4_disable_intr(phba);
10105out_free_sysfs_attr:
10106 lpfc_free_sysfs_attr(vport);
10107out_destroy_shost:
10108 lpfc_destroy_shost(phba);
10109out_unset_driver_resource:
10110 lpfc_unset_driver_resource_phase2(phba);
10111out_free_iocb_list:
10112 lpfc_free_iocb_list(phba);
10113out_unset_driver_resource_s4:
10114 lpfc_sli4_driver_resource_unset(phba);
10115out_unset_pci_mem_s4:
10116 lpfc_sli4_pci_mem_unset(phba);
10117out_disable_pci_dev:
10118 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040010119 if (shost)
10120 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -040010121out_free_phba:
10122 lpfc_hba_free(phba);
10123 return error;
10124}
10125
10126/**
10127 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
10128 * @pdev: pointer to PCI device
10129 *
10130 * This routine is called from the kernel's PCI subsystem to device with
10131 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
10132 * removed from PCI bus, it performs all the necessary cleanup for the HBA
10133 * device to be removed from the PCI subsystem properly.
10134 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010135static void
James Smartda0436e2009-05-22 14:51:39 -040010136lpfc_pci_remove_one_s4(struct pci_dev *pdev)
10137{
10138 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10139 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
10140 struct lpfc_vport **vports;
10141 struct lpfc_hba *phba = vport->phba;
10142 int i;
10143
10144 /* Mark the device unloading flag */
10145 spin_lock_irq(&phba->hbalock);
10146 vport->load_flag |= FC_UNLOADING;
10147 spin_unlock_irq(&phba->hbalock);
10148
10149 /* Free the HBA sysfs attributes */
10150 lpfc_free_sysfs_attr(vport);
10151
10152 /* Release all the vports against this physical port */
10153 vports = lpfc_create_vport_work_array(phba);
10154 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040010155 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
10156 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
10157 continue;
James Smartda0436e2009-05-22 14:51:39 -040010158 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040010159 }
James Smartda0436e2009-05-22 14:51:39 -040010160 lpfc_destroy_vport_work_array(phba, vports);
10161
10162 /* Remove FC host and then SCSI host with the physical port */
10163 fc_remove_host(shost);
10164 scsi_remove_host(shost);
10165
10166 /* Perform cleanup on the physical port */
10167 lpfc_cleanup(vport);
10168
10169 /*
10170 * Bring down the SLI Layer. This step disables all interrupts,
10171 * clears the rings, discards all mailbox commands, and resets
10172 * the HBA FCoE function.
10173 */
10174 lpfc_debugfs_terminate(vport);
10175 lpfc_sli4_hba_unset(phba);
10176
10177 spin_lock_irq(&phba->hbalock);
10178 list_del_init(&vport->listentry);
10179 spin_unlock_irq(&phba->hbalock);
10180
James Smart3677a3a2010-09-29 11:19:14 -040010181 /* Perform scsi free before driver resource_unset since scsi
James Smartda0436e2009-05-22 14:51:39 -040010182 * buffers are released to their corresponding pools here.
10183 */
10184 lpfc_scsi_free(phba);
James Smart67d12732012-08-03 12:36:13 -040010185
James Smartda0436e2009-05-22 14:51:39 -040010186 lpfc_sli4_driver_resource_unset(phba);
10187
10188 /* Unmap adapter Control and Doorbell registers */
10189 lpfc_sli4_pci_mem_unset(phba);
10190
10191 /* Release PCI resources and disable device's PCI function */
10192 scsi_host_put(shost);
10193 lpfc_disable_pci_dev(phba);
10194
10195 /* Finally, free the driver's device data structure */
10196 lpfc_hba_free(phba);
10197
10198 return;
10199}
10200
10201/**
10202 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
10203 * @pdev: pointer to PCI device
10204 * @msg: power management message
10205 *
10206 * This routine is called from the kernel's PCI subsystem to support system
10207 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
10208 * this method, it quiesces the device by stopping the driver's worker
10209 * thread for the device, turning off device's interrupt and DMA, and bring
10210 * the device offline. Note that as the driver implements the minimum PM
10211 * requirements to a power-aware driver's PM support for suspend/resume -- all
10212 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
10213 * method call will be treated as SUSPEND and the driver will fully
10214 * reinitialize its device during resume() method call, the driver will set
10215 * device to PCI_D3hot state in PCI config space instead of setting it
10216 * according to the @msg provided by the PM.
10217 *
10218 * Return code
10219 * 0 - driver suspended the device
10220 * Error otherwise
10221 **/
10222static int
10223lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
10224{
10225 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10226 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10227
10228 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart75baf692010-06-08 18:31:21 -040010229 "2843 PCI device Power Management suspend.\n");
James Smartda0436e2009-05-22 14:51:39 -040010230
10231 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040010232 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smartda0436e2009-05-22 14:51:39 -040010233 lpfc_offline(phba);
10234 kthread_stop(phba->worker_thread);
10235
10236 /* Disable interrupt from device */
10237 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040010238 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -040010239
10240 /* Save device state to PCI config space */
10241 pci_save_state(pdev);
10242 pci_set_power_state(pdev, PCI_D3hot);
10243
10244 return 0;
10245}
10246
10247/**
10248 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
10249 * @pdev: pointer to PCI device
10250 *
10251 * This routine is called from the kernel's PCI subsystem to support system
10252 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
10253 * this method, it restores the device's PCI config space state and fully
10254 * reinitializes the device and brings it online. Note that as the driver
10255 * implements the minimum PM requirements to a power-aware driver's PM for
10256 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
10257 * to the suspend() method call will be treated as SUSPEND and the driver
10258 * will fully reinitialize its device during resume() method call, the device
10259 * will be set to PCI_D0 directly in PCI config space before restoring the
10260 * state.
10261 *
10262 * Return code
10263 * 0 - driver suspended the device
10264 * Error otherwise
10265 **/
10266static int
10267lpfc_pci_resume_one_s4(struct pci_dev *pdev)
10268{
10269 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10270 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10271 uint32_t intr_mode;
10272 int error;
10273
10274 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10275 "0292 PCI device Power Management resume.\n");
10276
10277 /* Restore device state from PCI config space */
10278 pci_set_power_state(pdev, PCI_D0);
10279 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050010280
10281 /*
10282 * As the new kernel behavior of pci_restore_state() API call clears
10283 * device saved_state flag, need to save the restored state again.
10284 */
10285 pci_save_state(pdev);
10286
James Smartda0436e2009-05-22 14:51:39 -040010287 if (pdev->is_busmaster)
10288 pci_set_master(pdev);
10289
10290 /* Startup the kernel thread for this host adapter. */
10291 phba->worker_thread = kthread_run(lpfc_do_work, phba,
10292 "lpfc_worker_%d", phba->brd_no);
10293 if (IS_ERR(phba->worker_thread)) {
10294 error = PTR_ERR(phba->worker_thread);
10295 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10296 "0293 PM resume failed to start worker "
10297 "thread: error=x%x.\n", error);
10298 return error;
10299 }
10300
10301 /* Configure and enable interrupt */
10302 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
10303 if (intr_mode == LPFC_INTR_ERROR) {
10304 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10305 "0294 PM resume Failed to enable interrupt\n");
10306 return -EIO;
10307 } else
10308 phba->intr_mode = intr_mode;
10309
10310 /* Restart HBA and bring it online */
10311 lpfc_sli_brdrestart(phba);
10312 lpfc_online(phba);
10313
10314 /* Log the current active interrupt mode */
10315 lpfc_log_intr_mode(phba, phba->intr_mode);
10316
10317 return 0;
10318}
10319
10320/**
James Smart75baf692010-06-08 18:31:21 -040010321 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
10322 * @phba: pointer to lpfc hba data structure.
10323 *
10324 * This routine is called to prepare the SLI4 device for PCI slot recover. It
10325 * aborts all the outstanding SCSI I/Os to the pci device.
10326 **/
10327static void
10328lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
10329{
10330 struct lpfc_sli *psli = &phba->sli;
10331 struct lpfc_sli_ring *pring;
10332
10333 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10334 "2828 PCI channel I/O abort preparing for recovery\n");
10335 /*
10336 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
10337 * and let the SCSI mid-layer to retry them to recover.
10338 */
10339 pring = &psli->ring[psli->fcp_ring];
10340 lpfc_sli_abort_iocb_ring(phba, pring);
10341}
10342
10343/**
10344 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
10345 * @phba: pointer to lpfc hba data structure.
10346 *
10347 * This routine is called to prepare the SLI4 device for PCI slot reset. It
10348 * disables the device interrupt and pci device, and aborts the internal FCP
10349 * pending I/Os.
10350 **/
10351static void
10352lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
10353{
10354 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10355 "2826 PCI channel disable preparing for reset\n");
10356
10357 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040010358 lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
James Smart75baf692010-06-08 18:31:21 -040010359
10360 /* Block all SCSI devices' I/Os on the host */
10361 lpfc_scsi_dev_block(phba);
10362
James Smartea714f32013-04-17 20:18:47 -040010363 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
10364 lpfc_sli_flush_fcp_rings(phba);
10365
James Smart75baf692010-06-08 18:31:21 -040010366 /* stop all timers */
10367 lpfc_stop_hba_timers(phba);
10368
10369 /* Disable interrupt and pci device */
10370 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040010371 lpfc_sli4_queue_destroy(phba);
James Smart75baf692010-06-08 18:31:21 -040010372 pci_disable_device(phba->pcidev);
James Smart75baf692010-06-08 18:31:21 -040010373}
10374
10375/**
10376 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
10377 * @phba: pointer to lpfc hba data structure.
10378 *
10379 * This routine is called to prepare the SLI4 device for PCI slot permanently
10380 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
10381 * pending I/Os.
10382 **/
10383static void
10384lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
10385{
10386 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10387 "2827 PCI channel permanent disable for failure\n");
10388
10389 /* Block all SCSI devices' I/Os on the host */
10390 lpfc_scsi_dev_block(phba);
10391
10392 /* stop all timers */
10393 lpfc_stop_hba_timers(phba);
10394
10395 /* Clean up all driver's outstanding SCSI I/Os */
10396 lpfc_sli_flush_fcp_rings(phba);
10397}
10398
10399/**
James Smartda0436e2009-05-22 14:51:39 -040010400 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
10401 * @pdev: pointer to PCI device.
10402 * @state: the current PCI connection state.
10403 *
10404 * This routine is called from the PCI subsystem for error handling to device
10405 * with SLI-4 interface spec. This function is called by the PCI subsystem
10406 * after a PCI bus error affecting this device has been detected. When this
10407 * function is invoked, it will need to stop all the I/Os and interrupt(s)
10408 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
10409 * for the PCI subsystem to perform proper recovery as desired.
10410 *
10411 * Return codes
10412 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10413 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10414 **/
10415static pci_ers_result_t
10416lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
10417{
James Smart75baf692010-06-08 18:31:21 -040010418 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10419 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10420
10421 switch (state) {
10422 case pci_channel_io_normal:
10423 /* Non-fatal error, prepare for recovery */
10424 lpfc_sli4_prep_dev_for_recover(phba);
10425 return PCI_ERS_RESULT_CAN_RECOVER;
10426 case pci_channel_io_frozen:
10427 /* Fatal error, prepare for slot reset */
10428 lpfc_sli4_prep_dev_for_reset(phba);
10429 return PCI_ERS_RESULT_NEED_RESET;
10430 case pci_channel_io_perm_failure:
10431 /* Permanent failure, prepare for device down */
10432 lpfc_sli4_prep_dev_for_perm_failure(phba);
10433 return PCI_ERS_RESULT_DISCONNECT;
10434 default:
10435 /* Unknown state, prepare and request slot reset */
10436 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10437 "2825 Unknown PCI error state: x%x\n", state);
10438 lpfc_sli4_prep_dev_for_reset(phba);
10439 return PCI_ERS_RESULT_NEED_RESET;
10440 }
James Smartda0436e2009-05-22 14:51:39 -040010441}
10442
10443/**
10444 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
10445 * @pdev: pointer to PCI device.
10446 *
10447 * This routine is called from the PCI subsystem for error handling to device
10448 * with SLI-4 interface spec. It is called after PCI bus has been reset to
10449 * restart the PCI card from scratch, as if from a cold-boot. During the
10450 * PCI subsystem error recovery, after the driver returns
10451 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
10452 * recovery and then call this routine before calling the .resume method to
10453 * recover the device. This function will initialize the HBA device, enable
10454 * the interrupt, but it will just put the HBA to offline state without
10455 * passing any I/O traffic.
10456 *
10457 * Return codes
10458 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10459 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10460 */
10461static pci_ers_result_t
10462lpfc_io_slot_reset_s4(struct pci_dev *pdev)
10463{
James Smart75baf692010-06-08 18:31:21 -040010464 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10465 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10466 struct lpfc_sli *psli = &phba->sli;
10467 uint32_t intr_mode;
10468
10469 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
10470 if (pci_enable_device_mem(pdev)) {
10471 printk(KERN_ERR "lpfc: Cannot re-enable "
10472 "PCI device after reset.\n");
10473 return PCI_ERS_RESULT_DISCONNECT;
10474 }
10475
10476 pci_restore_state(pdev);
James Smart0a96e972011-07-22 18:37:28 -040010477
10478 /*
10479 * As the new kernel behavior of pci_restore_state() API call clears
10480 * device saved_state flag, need to save the restored state again.
10481 */
10482 pci_save_state(pdev);
10483
James Smart75baf692010-06-08 18:31:21 -040010484 if (pdev->is_busmaster)
10485 pci_set_master(pdev);
10486
10487 spin_lock_irq(&phba->hbalock);
10488 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
10489 spin_unlock_irq(&phba->hbalock);
10490
10491 /* Configure and enable interrupt */
10492 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
10493 if (intr_mode == LPFC_INTR_ERROR) {
10494 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10495 "2824 Cannot re-enable interrupt after "
10496 "slot reset.\n");
10497 return PCI_ERS_RESULT_DISCONNECT;
10498 } else
10499 phba->intr_mode = intr_mode;
10500
10501 /* Log the current active interrupt mode */
10502 lpfc_log_intr_mode(phba, phba->intr_mode);
10503
James Smartda0436e2009-05-22 14:51:39 -040010504 return PCI_ERS_RESULT_RECOVERED;
10505}
10506
10507/**
10508 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
10509 * @pdev: pointer to PCI device
10510 *
10511 * This routine is called from the PCI subsystem for error handling to device
10512 * with SLI-4 interface spec. It is called when kernel error recovery tells
10513 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
10514 * error recovery. After this call, traffic can start to flow from this device
10515 * again.
10516 **/
10517static void
10518lpfc_io_resume_s4(struct pci_dev *pdev)
10519{
James Smart75baf692010-06-08 18:31:21 -040010520 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10521 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10522
10523 /*
10524 * In case of slot reset, as function reset is performed through
10525 * mailbox command which needs DMA to be enabled, this operation
10526 * has to be moved to the io resume phase. Taking device offline
10527 * will perform the necessary cleanup.
10528 */
10529 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
10530 /* Perform device reset */
James Smart618a5232012-06-12 13:54:36 -040010531 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040010532 lpfc_offline(phba);
10533 lpfc_sli_brdrestart(phba);
10534 /* Bring the device back online */
10535 lpfc_online(phba);
10536 }
10537
10538 /* Clean up Advanced Error Reporting (AER) if needed */
10539 if (phba->hba_flag & HBA_AER_ENABLED)
10540 pci_cleanup_aer_uncorrect_error_status(pdev);
James Smartda0436e2009-05-22 14:51:39 -040010541}
10542
10543/**
James Smart3772a992009-05-22 14:50:54 -040010544 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
10545 * @pdev: pointer to PCI device
10546 * @pid: pointer to PCI device identifier
10547 *
10548 * This routine is to be registered to the kernel's PCI subsystem. When an
10549 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
10550 * at PCI device-specific information of the device and driver to see if the
10551 * driver state that it can support this kind of device. If the match is
10552 * successful, the driver core invokes this routine. This routine dispatches
10553 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
10554 * do all the initialization that it needs to do to handle the HBA device
10555 * properly.
10556 *
10557 * Return code
10558 * 0 - driver can claim the device
10559 * negative value - driver can not claim the device
10560 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010561static int
James Smart3772a992009-05-22 14:50:54 -040010562lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
10563{
10564 int rc;
James Smart8fa38512009-07-19 10:01:03 -040010565 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -040010566
James Smart28baac72010-02-12 14:42:03 -050010567 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -040010568 return -ENODEV;
10569
James Smart8fa38512009-07-19 10:01:03 -040010570 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -050010571 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -040010572 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040010573 else
James Smart3772a992009-05-22 14:50:54 -040010574 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040010575
James Smart3772a992009-05-22 14:50:54 -040010576 return rc;
10577}
10578
10579/**
10580 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
10581 * @pdev: pointer to PCI device
10582 *
10583 * This routine is to be registered to the kernel's PCI subsystem. When an
10584 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
10585 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
10586 * remove routine, which will perform all the necessary cleanup for the
10587 * device to be removed from the PCI subsystem properly.
10588 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010589static void
James Smart3772a992009-05-22 14:50:54 -040010590lpfc_pci_remove_one(struct pci_dev *pdev)
10591{
10592 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10593 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10594
10595 switch (phba->pci_dev_grp) {
10596 case LPFC_PCI_DEV_LP:
10597 lpfc_pci_remove_one_s3(pdev);
10598 break;
James Smartda0436e2009-05-22 14:51:39 -040010599 case LPFC_PCI_DEV_OC:
10600 lpfc_pci_remove_one_s4(pdev);
10601 break;
James Smart3772a992009-05-22 14:50:54 -040010602 default:
10603 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10604 "1424 Invalid PCI device group: 0x%x\n",
10605 phba->pci_dev_grp);
10606 break;
10607 }
10608 return;
10609}
10610
10611/**
10612 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
10613 * @pdev: pointer to PCI device
10614 * @msg: power management message
10615 *
10616 * This routine is to be registered to the kernel's PCI subsystem to support
10617 * system Power Management (PM). When PM invokes this method, it dispatches
10618 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
10619 * suspend the device.
10620 *
10621 * Return code
10622 * 0 - driver suspended the device
10623 * Error otherwise
10624 **/
10625static int
10626lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
10627{
10628 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10629 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10630 int rc = -ENODEV;
10631
10632 switch (phba->pci_dev_grp) {
10633 case LPFC_PCI_DEV_LP:
10634 rc = lpfc_pci_suspend_one_s3(pdev, msg);
10635 break;
James Smartda0436e2009-05-22 14:51:39 -040010636 case LPFC_PCI_DEV_OC:
10637 rc = lpfc_pci_suspend_one_s4(pdev, msg);
10638 break;
James Smart3772a992009-05-22 14:50:54 -040010639 default:
10640 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10641 "1425 Invalid PCI device group: 0x%x\n",
10642 phba->pci_dev_grp);
10643 break;
10644 }
10645 return rc;
10646}
10647
10648/**
10649 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
10650 * @pdev: pointer to PCI device
10651 *
10652 * This routine is to be registered to the kernel's PCI subsystem to support
10653 * system Power Management (PM). When PM invokes this method, it dispatches
10654 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
10655 * resume the device.
10656 *
10657 * Return code
10658 * 0 - driver suspended the device
10659 * Error otherwise
10660 **/
10661static int
10662lpfc_pci_resume_one(struct pci_dev *pdev)
10663{
10664 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10665 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10666 int rc = -ENODEV;
10667
10668 switch (phba->pci_dev_grp) {
10669 case LPFC_PCI_DEV_LP:
10670 rc = lpfc_pci_resume_one_s3(pdev);
10671 break;
James Smartda0436e2009-05-22 14:51:39 -040010672 case LPFC_PCI_DEV_OC:
10673 rc = lpfc_pci_resume_one_s4(pdev);
10674 break;
James Smart3772a992009-05-22 14:50:54 -040010675 default:
10676 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10677 "1426 Invalid PCI device group: 0x%x\n",
10678 phba->pci_dev_grp);
10679 break;
10680 }
10681 return rc;
10682}
10683
10684/**
10685 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
10686 * @pdev: pointer to PCI device.
10687 * @state: the current PCI connection state.
10688 *
10689 * This routine is registered to the PCI subsystem for error handling. This
10690 * function is called by the PCI subsystem after a PCI bus error affecting
10691 * this device has been detected. When this routine is invoked, it dispatches
10692 * the action to the proper SLI-3 or SLI-4 device error detected handling
10693 * routine, which will perform the proper error detected operation.
10694 *
10695 * Return codes
10696 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10697 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10698 **/
10699static pci_ers_result_t
10700lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
10701{
10702 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10703 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10704 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
10705
10706 switch (phba->pci_dev_grp) {
10707 case LPFC_PCI_DEV_LP:
10708 rc = lpfc_io_error_detected_s3(pdev, state);
10709 break;
James Smartda0436e2009-05-22 14:51:39 -040010710 case LPFC_PCI_DEV_OC:
10711 rc = lpfc_io_error_detected_s4(pdev, state);
10712 break;
James Smart3772a992009-05-22 14:50:54 -040010713 default:
10714 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10715 "1427 Invalid PCI device group: 0x%x\n",
10716 phba->pci_dev_grp);
10717 break;
10718 }
10719 return rc;
10720}
10721
10722/**
10723 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
10724 * @pdev: pointer to PCI device.
10725 *
10726 * This routine is registered to the PCI subsystem for error handling. This
10727 * function is called after PCI bus has been reset to restart the PCI card
10728 * from scratch, as if from a cold-boot. When this routine is invoked, it
10729 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
10730 * routine, which will perform the proper device reset.
10731 *
10732 * Return codes
10733 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10734 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10735 **/
10736static pci_ers_result_t
10737lpfc_io_slot_reset(struct pci_dev *pdev)
10738{
10739 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10740 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10741 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
10742
10743 switch (phba->pci_dev_grp) {
10744 case LPFC_PCI_DEV_LP:
10745 rc = lpfc_io_slot_reset_s3(pdev);
10746 break;
James Smartda0436e2009-05-22 14:51:39 -040010747 case LPFC_PCI_DEV_OC:
10748 rc = lpfc_io_slot_reset_s4(pdev);
10749 break;
James Smart3772a992009-05-22 14:50:54 -040010750 default:
10751 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10752 "1428 Invalid PCI device group: 0x%x\n",
10753 phba->pci_dev_grp);
10754 break;
10755 }
10756 return rc;
10757}
10758
10759/**
10760 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
10761 * @pdev: pointer to PCI device
10762 *
10763 * This routine is registered to the PCI subsystem for error handling. It
10764 * is called when kernel error recovery tells the lpfc driver that it is
10765 * OK to resume normal PCI operation after PCI bus error recovery. When
10766 * this routine is invoked, it dispatches the action to the proper SLI-3
10767 * or SLI-4 device io_resume routine, which will resume the device operation.
10768 **/
10769static void
10770lpfc_io_resume(struct pci_dev *pdev)
10771{
10772 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10773 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10774
10775 switch (phba->pci_dev_grp) {
10776 case LPFC_PCI_DEV_LP:
10777 lpfc_io_resume_s3(pdev);
10778 break;
James Smartda0436e2009-05-22 14:51:39 -040010779 case LPFC_PCI_DEV_OC:
10780 lpfc_io_resume_s4(pdev);
10781 break;
James Smart3772a992009-05-22 14:50:54 -040010782 default:
10783 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10784 "1429 Invalid PCI device group: 0x%x\n",
10785 phba->pci_dev_grp);
10786 break;
10787 }
10788 return;
10789}
10790
dea31012005-04-17 16:05:31 -050010791static struct pci_device_id lpfc_id_table[] = {
10792 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
10793 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -040010794 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
10795 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010796 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
10797 PCI_ANY_ID, PCI_ANY_ID, },
10798 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
10799 PCI_ANY_ID, PCI_ANY_ID, },
10800 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
10801 PCI_ANY_ID, PCI_ANY_ID, },
10802 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
10803 PCI_ANY_ID, PCI_ANY_ID, },
10804 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
10805 PCI_ANY_ID, PCI_ANY_ID, },
10806 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
10807 PCI_ANY_ID, PCI_ANY_ID, },
10808 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
10809 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050010810 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
10811 PCI_ANY_ID, PCI_ANY_ID, },
10812 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
10813 PCI_ANY_ID, PCI_ANY_ID, },
10814 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
10815 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010816 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
10817 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050010818 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
10819 PCI_ANY_ID, PCI_ANY_ID, },
10820 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
10821 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010822 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
10823 PCI_ANY_ID, PCI_ANY_ID, },
10824 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
10825 PCI_ANY_ID, PCI_ANY_ID, },
10826 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
10827 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -040010828 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
10829 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050010830 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
10831 PCI_ANY_ID, PCI_ANY_ID, },
10832 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
10833 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010834 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
10835 PCI_ANY_ID, PCI_ANY_ID, },
10836 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
10837 PCI_ANY_ID, PCI_ANY_ID, },
10838 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
10839 PCI_ANY_ID, PCI_ANY_ID, },
10840 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
10841 PCI_ANY_ID, PCI_ANY_ID, },
10842 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
10843 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050010844 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
10845 PCI_ANY_ID, PCI_ANY_ID, },
10846 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
10847 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -040010848 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
10849 PCI_ANY_ID, PCI_ANY_ID, },
10850 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
10851 PCI_ANY_ID, PCI_ANY_ID, },
10852 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
10853 PCI_ANY_ID, PCI_ANY_ID, },
10854 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
10855 PCI_ANY_ID, PCI_ANY_ID, },
10856 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
10857 PCI_ANY_ID, PCI_ANY_ID, },
10858 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
10859 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -040010860 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
10861 PCI_ANY_ID, PCI_ANY_ID, },
10862 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
10863 PCI_ANY_ID, PCI_ANY_ID, },
10864 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
10865 PCI_ANY_ID, PCI_ANY_ID, },
James Smart3772a992009-05-22 14:50:54 -040010866 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
10867 PCI_ANY_ID, PCI_ANY_ID, },
James Smarta747c9c2009-11-18 15:41:10 -050010868 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
10869 PCI_ANY_ID, PCI_ANY_ID, },
10870 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
James Smart6669f9b2009-10-02 15:16:45 -040010871 PCI_ANY_ID, PCI_ANY_ID, },
James Smart98fc5dd2010-06-07 15:24:29 -040010872 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BALIUS,
10873 PCI_ANY_ID, PCI_ANY_ID, },
James Smart085c6472010-11-20 23:11:37 -050010874 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC,
10875 PCI_ANY_ID, PCI_ANY_ID, },
10876 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE,
10877 PCI_ANY_ID, PCI_ANY_ID, },
James Smartc0c11512011-05-24 11:41:34 -040010878 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC_VF,
10879 PCI_ANY_ID, PCI_ANY_ID, },
10880 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE_VF,
10881 PCI_ANY_ID, PCI_ANY_ID, },
James Smartf8cafd32012-08-03 12:42:51 -040010882 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SKYHAWK,
10883 PCI_ANY_ID, PCI_ANY_ID, },
10884 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SKYHAWK_VF,
10885 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010886 { 0 }
10887};
10888
10889MODULE_DEVICE_TABLE(pci, lpfc_id_table);
10890
Stephen Hemmingera55b2d22012-09-07 09:33:16 -070010891static const struct pci_error_handlers lpfc_err_handler = {
Linas Vepstas8d63f372007-02-14 14:28:36 -060010892 .error_detected = lpfc_io_error_detected,
10893 .slot_reset = lpfc_io_slot_reset,
10894 .resume = lpfc_io_resume,
10895};
10896
dea31012005-04-17 16:05:31 -050010897static struct pci_driver lpfc_driver = {
10898 .name = LPFC_DRIVER_NAME,
10899 .id_table = lpfc_id_table,
10900 .probe = lpfc_pci_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010901 .remove = lpfc_pci_remove_one,
James Smart3a55b532008-12-04 22:38:54 -050010902 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -040010903 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -050010904 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -050010905};
10906
James Smart3ef6d242012-01-18 16:23:48 -050010907static const struct file_operations lpfc_mgmt_fop = {
Al Viro858feac2013-04-14 22:39:37 -040010908 .owner = THIS_MODULE,
James Smart3ef6d242012-01-18 16:23:48 -050010909};
10910
10911static struct miscdevice lpfc_mgmt_dev = {
10912 .minor = MISC_DYNAMIC_MINOR,
10913 .name = "lpfcmgmt",
10914 .fops = &lpfc_mgmt_fop,
10915};
10916
James Smarte59058c2008-08-24 21:49:00 -040010917/**
James Smart3621a712009-04-06 18:47:14 -040010918 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -040010919 *
10920 * This routine is to be invoked when the lpfc module is loaded into the
10921 * kernel. The special kernel macro module_init() is used to indicate the
10922 * role of this routine to the kernel as lpfc module entry point.
10923 *
10924 * Return codes
10925 * 0 - successful
10926 * -ENOMEM - FC attach transport failed
10927 * all others - failed
10928 */
dea31012005-04-17 16:05:31 -050010929static int __init
10930lpfc_init(void)
10931{
James Smart7bb03bb2013-04-17 20:19:16 -040010932 int cpu;
dea31012005-04-17 16:05:31 -050010933 int error = 0;
10934
10935 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010936 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -050010937
James Smart3ef6d242012-01-18 16:23:48 -050010938 error = misc_register(&lpfc_mgmt_dev);
10939 if (error)
10940 printk(KERN_ERR "Could not register lpfcmgmt device, "
10941 "misc_register returned with status %d", error);
10942
James Smart7ee5d432007-10-27 13:37:17 -040010943 if (lpfc_enable_npiv) {
10944 lpfc_transport_functions.vport_create = lpfc_vport_create;
10945 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
10946 }
dea31012005-04-17 16:05:31 -050010947 lpfc_transport_template =
10948 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -040010949 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -050010950 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -040010951 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -040010952 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -040010953 fc_attach_transport(&lpfc_vport_transport_functions);
10954 if (lpfc_vport_transport_template == NULL) {
10955 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040010956 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -040010957 }
James Smart7ee5d432007-10-27 13:37:17 -040010958 }
James Smart7bb03bb2013-04-17 20:19:16 -040010959
10960 /* Initialize in case vector mapping is needed */
James Smartb246de12013-05-31 17:03:07 -040010961 lpfc_used_cpu = NULL;
10962 lpfc_present_cpu = 0;
10963 for_each_present_cpu(cpu)
10964 lpfc_present_cpu++;
James Smart7bb03bb2013-04-17 20:19:16 -040010965
dea31012005-04-17 16:05:31 -050010966 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -050010967 if (error) {
dea31012005-04-17 16:05:31 -050010968 fc_release_transport(lpfc_transport_template);
James Smartd7c255b2008-08-24 21:50:00 -040010969 if (lpfc_enable_npiv)
10970 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -050010971 }
dea31012005-04-17 16:05:31 -050010972
10973 return error;
10974}
10975
James Smarte59058c2008-08-24 21:49:00 -040010976/**
James Smart3621a712009-04-06 18:47:14 -040010977 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -040010978 *
10979 * This routine is invoked when the lpfc module is removed from the kernel.
10980 * The special kernel macro module_exit() is used to indicate the role of
10981 * this routine to the kernel as lpfc module exit point.
10982 */
dea31012005-04-17 16:05:31 -050010983static void __exit
10984lpfc_exit(void)
10985{
James Smart3ef6d242012-01-18 16:23:48 -050010986 misc_deregister(&lpfc_mgmt_dev);
dea31012005-04-17 16:05:31 -050010987 pci_unregister_driver(&lpfc_driver);
10988 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040010989 if (lpfc_enable_npiv)
10990 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -050010991 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040010992 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
10993 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050010994 (1L << _dump_buf_data_order), _dump_buf_data);
10995 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
10996 }
10997
10998 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -040010999 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
11000 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050011001 (1L << _dump_buf_dif_order), _dump_buf_dif);
11002 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
11003 }
James Smartb246de12013-05-31 17:03:07 -040011004 kfree(lpfc_used_cpu);
dea31012005-04-17 16:05:31 -050011005}
11006
11007module_init(lpfc_init);
11008module_exit(lpfc_exit);
11009MODULE_LICENSE("GPL");
11010MODULE_DESCRIPTION(LPFC_MODULE_DESC);
11011MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
11012MODULE_VERSION("0:" LPFC_DRIVER_VERSION);