blob: 8b9c191a70198bda139e4f58afe9cec8e0303385 [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 Smartd4379ac2012-03-01 22:37:07 -05004 * Copyright (C) 2004-2012 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>
dea31012005-04-17 16:05:31 -050036
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040037#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050038#include <scsi/scsi_device.h>
39#include <scsi/scsi_host.h>
40#include <scsi/scsi_transport_fc.h>
41
James Smartda0436e2009-05-22 14:51:39 -040042#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050043#include "lpfc_hw.h"
44#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040045#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040046#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050047#include "lpfc_disc.h"
48#include "lpfc_scsi.h"
49#include "lpfc.h"
50#include "lpfc_logmsg.h"
51#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050052#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050053#include "lpfc_version.h"
54
James Smart81301a92008-12-04 22:39:46 -050055char *_dump_buf_data;
56unsigned long _dump_buf_data_order;
57char *_dump_buf_dif;
58unsigned long _dump_buf_dif_order;
59spinlock_t _dump_buf_lock;
60
dea31012005-04-17 16:05:31 -050061static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
62static int lpfc_post_rcv_buf(struct lpfc_hba *);
James Smart5350d872011-10-10 21:33:49 -040063static int lpfc_sli4_queue_verify(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040064static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
65static int lpfc_setup_endian_order(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040066static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
James Smart8a9d2e82012-05-09 21:16:12 -040067static void lpfc_free_els_sgl_list(struct lpfc_hba *);
68static void lpfc_init_sgl_list(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040069static int lpfc_init_active_sgl_array(struct lpfc_hba *);
70static void lpfc_free_active_sgl(struct lpfc_hba *);
71static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
72static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
73static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
74static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
75static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
James Smart618a5232012-06-12 13:54:36 -040076static void lpfc_sli4_disable_intr(struct lpfc_hba *);
77static uint32_t lpfc_sli4_enable_intr(struct lpfc_hba *, uint32_t);
dea31012005-04-17 16:05:31 -050078
79static struct scsi_transport_template *lpfc_transport_template = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050080static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea31012005-04-17 16:05:31 -050081static DEFINE_IDR(lpfc_hba_index);
82
James Smarte59058c2008-08-24 21:49:00 -040083/**
James Smart3621a712009-04-06 18:47:14 -040084 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
James Smarte59058c2008-08-24 21:49:00 -040085 * @phba: pointer to lpfc hba data structure.
86 *
87 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
88 * mailbox command. It retrieves the revision information from the HBA and
89 * collects the Vital Product Data (VPD) about the HBA for preparing the
90 * configuration of the HBA.
91 *
92 * Return codes:
93 * 0 - success.
94 * -ERESTART - requests the SLI layer to reset the HBA and try again.
95 * Any other value - indicates an error.
96 **/
dea31012005-04-17 16:05:31 -050097int
James Smart2e0fef82007-06-17 19:56:36 -050098lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050099{
100 lpfc_vpd_t *vp = &phba->vpd;
101 int i = 0, rc;
102 LPFC_MBOXQ_t *pmb;
103 MAILBOX_t *mb;
104 char *lpfc_vpd_data = NULL;
105 uint16_t offset = 0;
106 static char licensed[56] =
107 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -0400108 static int init_key = 1;
dea31012005-04-17 16:05:31 -0500109
110 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
111 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500112 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500113 return -ENOMEM;
114 }
115
James Smart04c68492009-05-22 14:52:52 -0400116 mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -0500117 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500118
119 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -0400120 if (init_key) {
121 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -0500122
James Smart65a29c12006-07-06 15:50:50 -0400123 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
124 *ptext = cpu_to_be32(*ptext);
125 init_key = 0;
126 }
dea31012005-04-17 16:05:31 -0500127
128 lpfc_read_nv(phba, pmb);
129 memset((char*)mb->un.varRDnvp.rsvd3, 0,
130 sizeof (mb->un.varRDnvp.rsvd3));
131 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
132 sizeof (licensed));
133
134 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
135
136 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500137 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smarte8b62012007-08-02 11:10:09 -0400138 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500139 "error, mbxCmd x%x READ_NVPARM, "
140 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500141 mb->mbxCommand, mb->mbxStatus);
142 mempool_free(pmb, phba->mbox_mem_pool);
143 return -ERESTART;
144 }
145 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500146 sizeof(phba->wwnn));
147 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
148 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500149 }
150
James Smart92d7f7b2007-06-17 19:56:38 -0500151 phba->sli3_options = 0x0;
152
dea31012005-04-17 16:05:31 -0500153 /* Setup and issue mailbox READ REV command */
154 lpfc_read_rev(phba, pmb);
155 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
156 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500157 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400158 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500159 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500160 mb->mbxCommand, mb->mbxStatus);
161 mempool_free( pmb, phba->mbox_mem_pool);
162 return -ERESTART;
163 }
164
James Smart92d7f7b2007-06-17 19:56:38 -0500165
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500166 /*
167 * The value of rr must be 1 since the driver set the cv field to 1.
168 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500169 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500170 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500171 vp->rev.rBit = 0;
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500172 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400173 "0440 Adapter failed to init, READ_REV has "
174 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500175 mempool_free(pmb, phba->mbox_mem_pool);
176 return -ERESTART;
dea31012005-04-17 16:05:31 -0500177 }
178
James Smart495a7142008-06-14 22:52:59 -0400179 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
180 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -0500181 return -EINVAL;
James Smart495a7142008-06-14 22:52:59 -0400182 }
James Smarted957682007-06-17 19:56:37 -0500183
dea31012005-04-17 16:05:31 -0500184 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500185 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500186 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500187 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
188 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
189 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
190 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500191 vp->rev.biuRev = mb->un.varRdRev.biuRev;
192 vp->rev.smRev = mb->un.varRdRev.smRev;
193 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
194 vp->rev.endecRev = mb->un.varRdRev.endecRev;
195 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
196 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
197 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
198 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
199 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
200 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
201
James Smart92d7f7b2007-06-17 19:56:38 -0500202 /* If the sli feature level is less then 9, we must
203 * tear down all RPIs and VPIs on link down if NPIV
204 * is enabled.
205 */
206 if (vp->rev.feaLevelHigh < 9)
207 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
208
dea31012005-04-17 16:05:31 -0500209 if (lpfc_is_LC_HBA(phba->pcidev->device))
210 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
211 sizeof (phba->RandomData));
212
dea31012005-04-17 16:05:31 -0500213 /* Get adapter VPD information */
dea31012005-04-17 16:05:31 -0500214 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
215 if (!lpfc_vpd_data)
James Smartd7c255b2008-08-24 21:50:00 -0400216 goto out_free_mbox;
dea31012005-04-17 16:05:31 -0500217 do {
James Smarta0c87cb2009-07-19 10:01:10 -0400218 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
dea31012005-04-17 16:05:31 -0500219 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
220
221 if (rc != MBX_SUCCESS) {
222 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400223 "0441 VPD not present on adapter, "
dea31012005-04-17 16:05:31 -0500224 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500225 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500226 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500227 }
James Smart04c68492009-05-22 14:52:52 -0400228 /* dump mem may return a zero when finished or we got a
229 * mailbox error, either way we are done.
230 */
231 if (mb->un.varDmp.word_cnt == 0)
232 break;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500233 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
234 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
James Smartd7c255b2008-08-24 21:50:00 -0400235 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
236 lpfc_vpd_data + offset,
James Smart92d7f7b2007-06-17 19:56:38 -0500237 mb->un.varDmp.word_cnt);
dea31012005-04-17 16:05:31 -0500238 offset += mb->un.varDmp.word_cnt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500239 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
240 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500241
242 kfree(lpfc_vpd_data);
dea31012005-04-17 16:05:31 -0500243out_free_mbox:
244 mempool_free(pmb, phba->mbox_mem_pool);
245 return 0;
246}
247
James Smarte59058c2008-08-24 21:49:00 -0400248/**
James Smart3621a712009-04-06 18:47:14 -0400249 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
James Smarte59058c2008-08-24 21:49:00 -0400250 * @phba: pointer to lpfc hba data structure.
251 * @pmboxq: pointer to the driver internal queue element for mailbox command.
252 *
253 * This is the completion handler for driver's configuring asynchronous event
254 * mailbox command to the device. If the mailbox command returns successfully,
255 * it will set internal async event support flag to 1; otherwise, it will
256 * set internal async event support flag to 0.
257 **/
James Smart57127f12007-10-27 13:37:05 -0400258static void
259lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
260{
James Smart04c68492009-05-22 14:52:52 -0400261 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
James Smart57127f12007-10-27 13:37:05 -0400262 phba->temp_sensor_support = 1;
263 else
264 phba->temp_sensor_support = 0;
265 mempool_free(pmboxq, phba->mbox_mem_pool);
266 return;
267}
268
James Smarte59058c2008-08-24 21:49:00 -0400269/**
James Smart3621a712009-04-06 18:47:14 -0400270 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
James Smart97207482008-12-04 22:39:19 -0500271 * @phba: pointer to lpfc hba data structure.
272 * @pmboxq: pointer to the driver internal queue element for mailbox command.
273 *
274 * This is the completion handler for dump mailbox command for getting
275 * wake up parameters. When this command complete, the response contain
276 * Option rom version of the HBA. This function translate the version number
277 * into a human readable string and store it in OptionROMVersion.
278 **/
279static void
280lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
281{
282 struct prog_id *prg;
283 uint32_t prog_id_word;
284 char dist = ' ';
285 /* character array used for decoding dist type. */
286 char dist_char[] = "nabx";
287
James Smart04c68492009-05-22 14:52:52 -0400288 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
James Smart9f1e1b52008-12-04 22:39:40 -0500289 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500290 return;
James Smart9f1e1b52008-12-04 22:39:40 -0500291 }
James Smart97207482008-12-04 22:39:19 -0500292
293 prg = (struct prog_id *) &prog_id_word;
294
295 /* word 7 contain option rom version */
James Smart04c68492009-05-22 14:52:52 -0400296 prog_id_word = pmboxq->u.mb.un.varWords[7];
James Smart97207482008-12-04 22:39:19 -0500297
298 /* Decode the Option rom version word to a readable string */
299 if (prg->dist < 4)
300 dist = dist_char[prg->dist];
301
302 if ((prg->dist == 3) && (prg->num == 0))
303 sprintf(phba->OptionROMVersion, "%d.%d%d",
304 prg->ver, prg->rev, prg->lev);
305 else
306 sprintf(phba->OptionROMVersion, "%d.%d%d%c%d",
307 prg->ver, prg->rev, prg->lev,
308 dist, prg->num);
James Smart9f1e1b52008-12-04 22:39:40 -0500309 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500310 return;
311}
312
313/**
James Smart05580562011-05-24 11:40:48 -0400314 * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
315 * cfg_soft_wwnn, cfg_soft_wwpn
316 * @vport: pointer to lpfc vport data structure.
317 *
318 *
319 * Return codes
320 * None.
321 **/
322void
323lpfc_update_vport_wwn(struct lpfc_vport *vport)
324{
325 /* If the soft name exists then update it using the service params */
326 if (vport->phba->cfg_soft_wwnn)
327 u64_to_wwn(vport->phba->cfg_soft_wwnn,
328 vport->fc_sparam.nodeName.u.wwn);
329 if (vport->phba->cfg_soft_wwpn)
330 u64_to_wwn(vport->phba->cfg_soft_wwpn,
331 vport->fc_sparam.portName.u.wwn);
332
333 /*
334 * If the name is empty or there exists a soft name
335 * then copy the service params name, otherwise use the fc name
336 */
337 if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
338 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
339 sizeof(struct lpfc_name));
340 else
341 memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
342 sizeof(struct lpfc_name));
343
344 if (vport->fc_portname.u.wwn[0] == 0 || vport->phba->cfg_soft_wwpn)
345 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
346 sizeof(struct lpfc_name));
347 else
348 memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
349 sizeof(struct lpfc_name));
350}
351
352/**
James Smart3621a712009-04-06 18:47:14 -0400353 * lpfc_config_port_post - Perform lpfc initialization after config port
James Smarte59058c2008-08-24 21:49:00 -0400354 * @phba: pointer to lpfc hba data structure.
355 *
356 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
357 * command call. It performs all internal resource and state setups on the
358 * port: post IOCB buffers, enable appropriate host interrupt attentions,
359 * ELS ring timers, etc.
360 *
361 * Return codes
362 * 0 - success.
363 * Any other value - error.
364 **/
dea31012005-04-17 16:05:31 -0500365int
James Smart2e0fef82007-06-17 19:56:36 -0500366lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500367{
James Smart2e0fef82007-06-17 19:56:36 -0500368 struct lpfc_vport *vport = phba->pport;
James Smarta257bf92009-04-06 18:48:10 -0400369 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -0500370 LPFC_MBOXQ_t *pmb;
371 MAILBOX_t *mb;
372 struct lpfc_dmabuf *mp;
373 struct lpfc_sli *psli = &phba->sli;
374 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500375 int i, j;
376 int rc;
dea31012005-04-17 16:05:31 -0500377
James Smart7af67052007-10-27 13:38:11 -0400378 spin_lock_irq(&phba->hbalock);
379 /*
380 * If the Config port completed correctly the HBA is not
381 * over heated any more.
382 */
383 if (phba->over_temp_state == HBA_OVER_TEMP)
384 phba->over_temp_state = HBA_NORMAL_TEMP;
385 spin_unlock_irq(&phba->hbalock);
386
dea31012005-04-17 16:05:31 -0500387 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
388 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500389 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500390 return -ENOMEM;
391 }
James Smart04c68492009-05-22 14:52:52 -0400392 mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500393
dea31012005-04-17 16:05:31 -0500394 /* Get login parameters for NID. */
James Smart9f1177a2010-02-26 14:12:57 -0500395 rc = lpfc_read_sparam(phba, pmb, 0);
396 if (rc) {
397 mempool_free(pmb, phba->mbox_mem_pool);
398 return -ENOMEM;
399 }
400
James Smarted957682007-06-17 19:56:37 -0500401 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500402 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500403 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400404 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500405 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500406 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500407 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500408 mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart9f1177a2010-02-26 14:12:57 -0500409 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500410 lpfc_mbuf_free(phba, mp->virt, mp->phys);
411 kfree(mp);
412 return -EIO;
413 }
414
415 mp = (struct lpfc_dmabuf *) pmb->context1;
416
James Smart2e0fef82007-06-17 19:56:36 -0500417 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500418 lpfc_mbuf_free(phba, mp->virt, mp->phys);
419 kfree(mp);
420 pmb->context1 = NULL;
James Smart05580562011-05-24 11:40:48 -0400421 lpfc_update_vport_wwn(vport);
James Smarta257bf92009-04-06 18:48:10 -0400422
423 /* Update the fc_host data structures with new wwn. */
424 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
425 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart21e9a0a2009-05-22 14:53:21 -0400426 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smarta257bf92009-04-06 18:48:10 -0400427
dea31012005-04-17 16:05:31 -0500428 /* If no serial number in VPD data, use low 6 bytes of WWNN */
429 /* This should be consolidated into parse_vpd ? - mr */
430 if (phba->SerialNumber[0] == 0) {
431 uint8_t *outptr;
432
James Smart2e0fef82007-06-17 19:56:36 -0500433 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500434 for (i = 0; i < 12; i++) {
435 status = *outptr++;
436 j = ((status & 0xf0) >> 4);
437 if (j <= 9)
438 phba->SerialNumber[i] =
439 (char)((uint8_t) 0x30 + (uint8_t) j);
440 else
441 phba->SerialNumber[i] =
442 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
443 i++;
444 j = (status & 0xf);
445 if (j <= 9)
446 phba->SerialNumber[i] =
447 (char)((uint8_t) 0x30 + (uint8_t) j);
448 else
449 phba->SerialNumber[i] =
450 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
451 }
452 }
453
dea31012005-04-17 16:05:31 -0500454 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500455 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500456 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500457 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400458 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500459 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500460 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500461 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500462 mempool_free( pmb, phba->mbox_mem_pool);
463 return -EIO;
464 }
465
James Smarta0c87cb2009-07-19 10:01:10 -0400466 /* Check if the port is disabled */
467 lpfc_sli_read_link_ste(phba);
468
dea31012005-04-17 16:05:31 -0500469 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
470 if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
471 phba->cfg_hba_queue_depth =
James Smartf1126682009-06-10 17:22:44 -0400472 (mb->un.varRdConfig.max_xri + 1) -
473 lpfc_sli4_get_els_iocb_cnt(phba);
dea31012005-04-17 16:05:31 -0500474
475 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500476
477 /* Get the default values for Model Name and Description */
478 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
479
James Smart2e0fef82007-06-17 19:56:36 -0500480 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500481
James Smart0b727fe2007-10-27 13:37:25 -0400482 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smart7e56aa22012-08-03 12:35:34 -0400483 if (psli->ring[psli->extra_ring].sli.sli3.cmdringaddr)
James Smarta4bc3372006-12-02 13:34:16 -0500484 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
James Smart7e56aa22012-08-03 12:35:34 -0400485 if (psli->ring[psli->fcp_ring].sli.sli3.cmdringaddr)
dea31012005-04-17 16:05:31 -0500486 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
James Smart7e56aa22012-08-03 12:35:34 -0400487 if (psli->ring[psli->next_ring].sli.sli3.cmdringaddr)
dea31012005-04-17 16:05:31 -0500488 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
489
490 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500491 if (phba->sli_rev != 3)
492 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500493
James Smart93996272008-08-24 21:50:30 -0400494 /*
495 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
496 */
497 if (phba->intr_type == MSIX) {
498 rc = lpfc_config_msi(phba, pmb);
499 if (rc) {
500 mempool_free(pmb, phba->mbox_mem_pool);
501 return -EIO;
502 }
503 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
504 if (rc != MBX_SUCCESS) {
505 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
506 "0352 Config MSI mailbox command "
507 "failed, mbxCmd x%x, mbxStatus x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400508 pmb->u.mb.mbxCommand,
509 pmb->u.mb.mbxStatus);
James Smart93996272008-08-24 21:50:30 -0400510 mempool_free(pmb, phba->mbox_mem_pool);
511 return -EIO;
512 }
513 }
514
James Smart04c68492009-05-22 14:52:52 -0400515 spin_lock_irq(&phba->hbalock);
James Smart93996272008-08-24 21:50:30 -0400516 /* Initialize ERATT handling flag */
517 phba->hba_flag &= ~HBA_ERATT_HANDLED;
518
dea31012005-04-17 16:05:31 -0500519 /* Enable appropriate host interrupts */
James Smart9940b972011-03-11 16:06:12 -0500520 if (lpfc_readl(phba->HCregaddr, &status)) {
521 spin_unlock_irq(&phba->hbalock);
522 return -EIO;
523 }
dea31012005-04-17 16:05:31 -0500524 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
525 if (psli->num_rings > 0)
526 status |= HC_R0INT_ENA;
527 if (psli->num_rings > 1)
528 status |= HC_R1INT_ENA;
529 if (psli->num_rings > 2)
530 status |= HC_R2INT_ENA;
531 if (psli->num_rings > 3)
532 status |= HC_R3INT_ENA;
533
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500534 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
535 (phba->cfg_poll & DISABLE_FCP_RING_INT))
James Smart93996272008-08-24 21:50:30 -0400536 status &= ~(HC_R0INT_ENA);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500537
dea31012005-04-17 16:05:31 -0500538 writel(status, phba->HCregaddr);
539 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500540 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500541
James Smart93996272008-08-24 21:50:30 -0400542 /* Set up ring-0 (ELS) timer */
543 timeout = phba->fc_ratov * 2;
James Smart2e0fef82007-06-17 19:56:36 -0500544 mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
James Smart93996272008-08-24 21:50:30 -0400545 /* Set up heart beat (HB) timer */
James Smart858c9f62007-06-17 19:56:39 -0500546 mod_timer(&phba->hb_tmofunc, jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
547 phba->hb_outstanding = 0;
548 phba->last_completion_time = jiffies;
James Smart93996272008-08-24 21:50:30 -0400549 /* Set up error attention (ERATT) polling timer */
550 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
dea31012005-04-17 16:05:31 -0500551
James Smarta0c87cb2009-07-19 10:01:10 -0400552 if (phba->hba_flag & LINK_DISABLED) {
553 lpfc_printf_log(phba,
554 KERN_ERR, LOG_INIT,
555 "2598 Adapter Link is disabled.\n");
556 lpfc_down_link(phba, pmb);
557 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
558 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
559 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
560 lpfc_printf_log(phba,
561 KERN_ERR, LOG_INIT,
562 "2599 Adapter failed to issue DOWN_LINK"
563 " mbox command rc 0x%x\n", rc);
564
565 mempool_free(pmb, phba->mbox_mem_pool);
566 return -EIO;
567 }
James Smarte40a02c2010-02-26 14:13:54 -0500568 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart026abb82011-12-13 13:20:45 -0500569 mempool_free(pmb, phba->mbox_mem_pool);
570 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
571 if (rc)
572 return rc;
dea31012005-04-17 16:05:31 -0500573 }
574 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400575 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500576 if (!pmb) {
577 phba->link_state = LPFC_HBA_ERROR;
578 return -ENOMEM;
579 }
580
James Smart57127f12007-10-27 13:37:05 -0400581 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
582 pmb->mbox_cmpl = lpfc_config_async_cmpl;
583 pmb->vport = phba->pport;
584 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500585
James Smart57127f12007-10-27 13:37:05 -0400586 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
587 lpfc_printf_log(phba,
588 KERN_ERR,
589 LOG_INIT,
590 "0456 Adapter failed to issue "
James Smarte4e74272009-07-19 10:01:38 -0400591 "ASYNCEVT_ENABLE mbox status x%x\n",
James Smart57127f12007-10-27 13:37:05 -0400592 rc);
593 mempool_free(pmb, phba->mbox_mem_pool);
594 }
James Smart97207482008-12-04 22:39:19 -0500595
596 /* Get Option rom version */
597 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500598 if (!pmb) {
599 phba->link_state = LPFC_HBA_ERROR;
600 return -ENOMEM;
601 }
602
James Smart97207482008-12-04 22:39:19 -0500603 lpfc_dump_wakeup_param(phba, pmb);
604 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
605 pmb->vport = phba->pport;
606 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
607
608 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
609 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
James Smarte4e74272009-07-19 10:01:38 -0400610 "to get Option ROM version status x%x\n", rc);
James Smart97207482008-12-04 22:39:19 -0500611 mempool_free(pmb, phba->mbox_mem_pool);
612 }
613
James Smartd7c255b2008-08-24 21:50:00 -0400614 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400615}
616
James Smarte59058c2008-08-24 21:49:00 -0400617/**
James Smart84d1b002010-02-12 14:42:33 -0500618 * lpfc_hba_init_link - Initialize the FC link
619 * @phba: pointer to lpfc hba data structure.
James Smart6e7288d2010-06-07 15:23:35 -0400620 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500621 *
622 * This routine will issue the INIT_LINK mailbox command call.
623 * It is available to other drivers through the lpfc_hba data
624 * structure for use as a delayed link up mechanism with the
625 * module parameter lpfc_suppress_link_up.
626 *
627 * Return code
628 * 0 - success
629 * Any other value - error
630 **/
631int
James Smart6e7288d2010-06-07 15:23:35 -0400632lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500633{
James Smart1b511972011-12-13 13:23:09 -0500634 return lpfc_hba_init_link_fc_topology(phba, phba->cfg_topology, flag);
635}
636
637/**
638 * lpfc_hba_init_link_fc_topology - Initialize FC link with desired topology
639 * @phba: pointer to lpfc hba data structure.
640 * @fc_topology: desired fc topology.
641 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
642 *
643 * This routine will issue the INIT_LINK mailbox command call.
644 * It is available to other drivers through the lpfc_hba data
645 * structure for use as a delayed link up mechanism with the
646 * module parameter lpfc_suppress_link_up.
647 *
648 * Return code
649 * 0 - success
650 * Any other value - error
651 **/
652int
653lpfc_hba_init_link_fc_topology(struct lpfc_hba *phba, uint32_t fc_topology,
654 uint32_t flag)
655{
James Smart84d1b002010-02-12 14:42:33 -0500656 struct lpfc_vport *vport = phba->pport;
657 LPFC_MBOXQ_t *pmb;
658 MAILBOX_t *mb;
659 int rc;
660
661 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
662 if (!pmb) {
663 phba->link_state = LPFC_HBA_ERROR;
664 return -ENOMEM;
665 }
666 mb = &pmb->u.mb;
667 pmb->vport = vport;
668
James Smart026abb82011-12-13 13:20:45 -0500669 if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_MAX) ||
670 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G) &&
671 !(phba->lmt & LMT_1Gb)) ||
672 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G) &&
673 !(phba->lmt & LMT_2Gb)) ||
674 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G) &&
675 !(phba->lmt & LMT_4Gb)) ||
676 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G) &&
677 !(phba->lmt & LMT_8Gb)) ||
678 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G) &&
679 !(phba->lmt & LMT_10Gb)) ||
680 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G) &&
681 !(phba->lmt & LMT_16Gb))) {
682 /* Reset link speed to auto */
683 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
684 "1302 Invalid speed for this board:%d "
685 "Reset link speed to auto.\n",
686 phba->cfg_link_speed);
687 phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
688 }
James Smart1b511972011-12-13 13:23:09 -0500689 lpfc_init_link(phba, pmb, fc_topology, phba->cfg_link_speed);
James Smart84d1b002010-02-12 14:42:33 -0500690 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart1b511972011-12-13 13:23:09 -0500691 if (phba->sli_rev < LPFC_SLI_REV4)
692 lpfc_set_loopback_flag(phba);
James Smart6e7288d2010-06-07 15:23:35 -0400693 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart76a95d72010-11-20 23:11:48 -0500694 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
James Smart84d1b002010-02-12 14:42:33 -0500695 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
696 "0498 Adapter failed to init, mbxCmd x%x "
697 "INIT_LINK, mbxStatus x%x\n",
698 mb->mbxCommand, mb->mbxStatus);
James Smart76a95d72010-11-20 23:11:48 -0500699 if (phba->sli_rev <= LPFC_SLI_REV3) {
700 /* Clear all interrupt enable conditions */
701 writel(0, phba->HCregaddr);
702 readl(phba->HCregaddr); /* flush */
703 /* Clear all pending interrupts */
704 writel(0xffffffff, phba->HAregaddr);
705 readl(phba->HAregaddr); /* flush */
706 }
James Smart84d1b002010-02-12 14:42:33 -0500707 phba->link_state = LPFC_HBA_ERROR;
James Smart6e7288d2010-06-07 15:23:35 -0400708 if (rc != MBX_BUSY || flag == MBX_POLL)
James Smart84d1b002010-02-12 14:42:33 -0500709 mempool_free(pmb, phba->mbox_mem_pool);
710 return -EIO;
711 }
James Smarte40a02c2010-02-26 14:13:54 -0500712 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
James Smart6e7288d2010-06-07 15:23:35 -0400713 if (flag == MBX_POLL)
714 mempool_free(pmb, phba->mbox_mem_pool);
James Smart84d1b002010-02-12 14:42:33 -0500715
716 return 0;
717}
718
719/**
720 * lpfc_hba_down_link - this routine downs the FC link
James Smart6e7288d2010-06-07 15:23:35 -0400721 * @phba: pointer to lpfc hba data structure.
722 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500723 *
724 * This routine will issue the DOWN_LINK mailbox command call.
725 * It is available to other drivers through the lpfc_hba data
726 * structure for use to stop the link.
727 *
728 * Return code
729 * 0 - success
730 * Any other value - error
731 **/
732int
James Smart6e7288d2010-06-07 15:23:35 -0400733lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500734{
735 LPFC_MBOXQ_t *pmb;
736 int rc;
737
738 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
739 if (!pmb) {
740 phba->link_state = LPFC_HBA_ERROR;
741 return -ENOMEM;
742 }
743
744 lpfc_printf_log(phba,
745 KERN_ERR, LOG_INIT,
746 "0491 Adapter Link is disabled.\n");
747 lpfc_down_link(phba, pmb);
748 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6e7288d2010-06-07 15:23:35 -0400749 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart84d1b002010-02-12 14:42:33 -0500750 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
751 lpfc_printf_log(phba,
752 KERN_ERR, LOG_INIT,
753 "2522 Adapter failed to issue DOWN_LINK"
754 " mbox command rc 0x%x\n", rc);
755
756 mempool_free(pmb, phba->mbox_mem_pool);
757 return -EIO;
758 }
James Smart6e7288d2010-06-07 15:23:35 -0400759 if (flag == MBX_POLL)
760 mempool_free(pmb, phba->mbox_mem_pool);
761
James Smart84d1b002010-02-12 14:42:33 -0500762 return 0;
763}
764
765/**
James Smart3621a712009-04-06 18:47:14 -0400766 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400767 * @phba: pointer to lpfc HBA data structure.
768 *
769 * This routine will do LPFC uninitialization before the HBA is reset when
770 * bringing down the SLI Layer.
771 *
772 * Return codes
773 * 0 - success.
774 * Any other value - error.
775 **/
dea31012005-04-17 16:05:31 -0500776int
James Smart2e0fef82007-06-17 19:56:36 -0500777lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500778{
James Smart1b32f6a2008-02-08 18:49:39 -0500779 struct lpfc_vport **vports;
780 int i;
James Smart3772a992009-05-22 14:50:54 -0400781
782 if (phba->sli_rev <= LPFC_SLI_REV3) {
783 /* Disable interrupts */
784 writel(0, phba->HCregaddr);
785 readl(phba->HCregaddr); /* flush */
786 }
dea31012005-04-17 16:05:31 -0500787
James Smart1b32f6a2008-02-08 18:49:39 -0500788 if (phba->pport->load_flag & FC_UNLOADING)
789 lpfc_cleanup_discovery_resources(phba->pport);
790 else {
791 vports = lpfc_create_vport_work_array(phba);
792 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400793 for (i = 0; i <= phba->max_vports &&
794 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500795 lpfc_cleanup_discovery_resources(vports[i]);
796 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500797 }
798 return 0;
dea31012005-04-17 16:05:31 -0500799}
800
James Smarte59058c2008-08-24 21:49:00 -0400801/**
James Smart3772a992009-05-22 14:50:54 -0400802 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400803 * @phba: pointer to lpfc HBA data structure.
804 *
805 * This routine will do uninitialization after the HBA is reset when bring
806 * down the SLI Layer.
807 *
808 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200809 * 0 - success.
James Smarte59058c2008-08-24 21:49:00 -0400810 * Any other value - error.
811 **/
James Smart3772a992009-05-22 14:50:54 -0400812static int
813lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -0500814{
815 struct lpfc_sli *psli = &phba->sli;
816 struct lpfc_sli_ring *pring;
817 struct lpfc_dmabuf *mp, *next_mp;
James Smart09372822008-01-11 01:52:54 -0500818 LIST_HEAD(completions);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500819 int i;
820
James Smart92d7f7b2007-06-17 19:56:38 -0500821 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
822 lpfc_sli_hbqbuf_free_all(phba);
823 else {
824 /* Cleanup preposted buffers on the ELS ring */
825 pring = &psli->ring[LPFC_ELS_RING];
826 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
827 list_del(&mp->list);
828 pring->postbufq_cnt--;
829 lpfc_mbuf_free(phba, mp->virt, mp->phys);
830 kfree(mp);
831 }
Jamie Wellnitz41415862006-02-28 19:25:27 -0500832 }
833
James Smart09372822008-01-11 01:52:54 -0500834 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500835 for (i = 0; i < psli->num_rings; i++) {
836 pring = &psli->ring[i];
James Smart09372822008-01-11 01:52:54 -0500837
838 /* At this point in time the HBA is either reset or DOA. Either
839 * way, nothing should be on txcmplq as it will NEVER complete.
840 */
841 list_splice_init(&pring->txcmplq, &completions);
842 pring->txcmplq_cnt = 0;
843 spin_unlock_irq(&phba->hbalock);
844
James Smarta257bf92009-04-06 18:48:10 -0400845 /* Cancel all the IOCBs from the completions list */
846 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
847 IOERR_SLI_ABORTED);
James Smart09372822008-01-11 01:52:54 -0500848
Jamie Wellnitz41415862006-02-28 19:25:27 -0500849 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart09372822008-01-11 01:52:54 -0500850 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500851 }
James Smart09372822008-01-11 01:52:54 -0500852 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500853
854 return 0;
855}
James Smart5af5eee2010-10-22 11:06:38 -0400856
James Smartda0436e2009-05-22 14:51:39 -0400857/**
858 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
859 * @phba: pointer to lpfc HBA data structure.
860 *
861 * This routine will do uninitialization after the HBA is reset when bring
862 * down the SLI Layer.
863 *
864 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200865 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400866 * Any other value - error.
867 **/
868static int
869lpfc_hba_down_post_s4(struct lpfc_hba *phba)
870{
871 struct lpfc_scsi_buf *psb, *psb_next;
872 LIST_HEAD(aborts);
873 int ret;
874 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500875 struct lpfc_sglq *sglq_entry = NULL;
876
James Smartda0436e2009-05-22 14:51:39 -0400877 ret = lpfc_hba_down_post_s3(phba);
878 if (ret)
879 return ret;
880 /* At this point in time the HBA is either reset or DOA. Either
881 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
882 * on the lpfc_sgl_list so that it can either be freed if the
883 * driver is unloading or reposted if the driver is restarting
884 * the port.
885 */
886 spin_lock_irq(&phba->hbalock); /* required for lpfc_sgl_list and */
887 /* scsl_buf_list */
888 /* abts_sgl_list_lock required because worker thread uses this
889 * list.
890 */
891 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -0500892 list_for_each_entry(sglq_entry,
893 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
894 sglq_entry->state = SGL_FREED;
895
James Smartda0436e2009-05-22 14:51:39 -0400896 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
897 &phba->sli4_hba.lpfc_sgl_list);
898 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
899 /* abts_scsi_buf_list_lock required because worker thread uses this
900 * list.
901 */
902 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
903 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
904 &aborts);
905 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
906 spin_unlock_irq(&phba->hbalock);
907
908 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
909 psb->pCmd = NULL;
910 psb->status = IOSTAT_SUCCESS;
911 }
912 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
913 list_splice(&aborts, &phba->lpfc_scsi_buf_list);
914 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
915 return 0;
916}
917
918/**
919 * lpfc_hba_down_post - Wrapper func for hba down post routine
920 * @phba: pointer to lpfc HBA data structure.
921 *
922 * This routine wraps the actual SLI3 or SLI4 routine for performing
923 * uninitialization after the HBA is reset when bring down the SLI Layer.
924 *
925 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200926 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400927 * Any other value - error.
928 **/
929int
930lpfc_hba_down_post(struct lpfc_hba *phba)
931{
932 return (*phba->lpfc_hba_down_post)(phba);
933}
Jamie Wellnitz41415862006-02-28 19:25:27 -0500934
James Smarte59058c2008-08-24 21:49:00 -0400935/**
James Smart3621a712009-04-06 18:47:14 -0400936 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -0400937 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
938 *
939 * This is the HBA-timer timeout handler registered to the lpfc driver. When
940 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
941 * work-port-events bitmap and the worker thread is notified. This timeout
942 * event will be used by the worker thread to invoke the actual timeout
943 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
944 * be performed in the timeout handler and the HBA timeout event bit shall
945 * be cleared by the worker thread after it has taken the event bitmap out.
946 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100947static void
James Smart858c9f62007-06-17 19:56:39 -0500948lpfc_hb_timeout(unsigned long ptr)
949{
950 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400951 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -0500952 unsigned long iflag;
953
954 phba = (struct lpfc_hba *)ptr;
James Smart93996272008-08-24 21:50:30 -0400955
956 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -0500957 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -0400958 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
959 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -0500960 phba->pport->work_port_events |= WORKER_HB_TMO;
961 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
962
James Smart93996272008-08-24 21:50:30 -0400963 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -0400964 if (!tmo_posted)
965 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -0500966 return;
967}
968
James Smarte59058c2008-08-24 21:49:00 -0400969/**
James Smart19ca7602010-11-20 23:11:55 -0500970 * lpfc_rrq_timeout - The RRQ-timer timeout handler
971 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
972 *
973 * This is the RRQ-timer timeout handler registered to the lpfc driver. When
974 * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
975 * work-port-events bitmap and the worker thread is notified. This timeout
976 * event will be used by the worker thread to invoke the actual timeout
977 * handler routine, lpfc_rrq_handler. Any periodical operations will
978 * be performed in the timeout handler and the RRQ timeout event bit shall
979 * be cleared by the worker thread after it has taken the event bitmap out.
980 **/
981static void
982lpfc_rrq_timeout(unsigned long ptr)
983{
984 struct lpfc_hba *phba;
James Smart19ca7602010-11-20 23:11:55 -0500985 unsigned long iflag;
986
987 phba = (struct lpfc_hba *)ptr;
988 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -0500989 phba->hba_flag |= HBA_RRQ_ACTIVE;
James Smart19ca7602010-11-20 23:11:55 -0500990 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -0500991 lpfc_worker_wake_up(phba);
James Smart19ca7602010-11-20 23:11:55 -0500992}
993
994/**
James Smart3621a712009-04-06 18:47:14 -0400995 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -0400996 * @phba: pointer to lpfc hba data structure.
997 * @pmboxq: pointer to the driver internal queue element for mailbox command.
998 *
999 * This is the callback function to the lpfc heart-beat mailbox command.
1000 * If configured, the lpfc driver issues the heart-beat mailbox command to
1001 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
1002 * heart-beat mailbox command is issued, the driver shall set up heart-beat
1003 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
1004 * heart-beat outstanding state. Once the mailbox command comes back and
1005 * no error conditions detected, the heart-beat mailbox command timer is
1006 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1007 * state is cleared for the next heart-beat. If the timer expired with the
1008 * heart-beat outstanding state set, the driver will put the HBA offline.
1009 **/
James Smart858c9f62007-06-17 19:56:39 -05001010static void
1011lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1012{
1013 unsigned long drvr_flag;
1014
1015 spin_lock_irqsave(&phba->hbalock, drvr_flag);
1016 phba->hb_outstanding = 0;
1017 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1018
James Smart93996272008-08-24 21:50:30 -04001019 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -05001020 mempool_free(pmboxq, phba->mbox_mem_pool);
1021 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1022 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -04001023 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -05001024 mod_timer(&phba->hb_tmofunc,
1025 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1026 return;
1027}
1028
James Smarte59058c2008-08-24 21:49:00 -04001029/**
James Smart3621a712009-04-06 18:47:14 -04001030 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001031 * @phba: pointer to lpfc hba data structure.
1032 *
1033 * This is the actual HBA-timer timeout handler to be invoked by the worker
1034 * thread whenever the HBA timer fired and HBA-timeout event posted. This
1035 * handler performs any periodic operations needed for the device. If such
1036 * periodic event has already been attended to either in the interrupt handler
1037 * or by processing slow-ring or fast-ring events within the HBA-timer
1038 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1039 * the timer for the next timeout period. If lpfc heart-beat mailbox command
1040 * is configured and there is no heart-beat mailbox command outstanding, a
1041 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1042 * has been a heart-beat mailbox command outstanding, the HBA shall be put
1043 * to offline.
1044 **/
James Smart858c9f62007-06-17 19:56:39 -05001045void
1046lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1047{
James Smart45ed1192009-10-02 15:17:02 -04001048 struct lpfc_vport **vports;
James Smart858c9f62007-06-17 19:56:39 -05001049 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -05001050 struct lpfc_dmabuf *buf_ptr;
James Smart45ed1192009-10-02 15:17:02 -04001051 int retval, i;
James Smart858c9f62007-06-17 19:56:39 -05001052 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -05001053 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -05001054
James Smart45ed1192009-10-02 15:17:02 -04001055 vports = lpfc_create_vport_work_array(phba);
1056 if (vports != NULL)
1057 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
1058 lpfc_rcv_seq_check_edtov(vports[i]);
1059 lpfc_destroy_vport_work_array(phba, vports);
1060
James Smart858c9f62007-06-17 19:56:39 -05001061 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -04001062 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -05001063 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1064 return;
1065
1066 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -05001067
1068 if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
1069 jiffies)) {
1070 spin_unlock_irq(&phba->pport->work_port_lock);
1071 if (!phba->hb_outstanding)
1072 mod_timer(&phba->hb_tmofunc,
1073 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1074 else
1075 mod_timer(&phba->hb_tmofunc,
1076 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
1077 return;
1078 }
1079 spin_unlock_irq(&phba->pport->work_port_lock);
1080
James Smart0ff10d42008-01-11 01:52:36 -05001081 if (phba->elsbuf_cnt &&
1082 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1083 spin_lock_irq(&phba->hbalock);
1084 list_splice_init(&phba->elsbuf, &completions);
1085 phba->elsbuf_cnt = 0;
1086 phba->elsbuf_prev_cnt = 0;
1087 spin_unlock_irq(&phba->hbalock);
1088
1089 while (!list_empty(&completions)) {
1090 list_remove_head(&completions, buf_ptr,
1091 struct lpfc_dmabuf, list);
1092 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1093 kfree(buf_ptr);
1094 }
1095 }
1096 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1097
James Smart858c9f62007-06-17 19:56:39 -05001098 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -05001099 if (phba->cfg_enable_hba_heartbeat) {
1100 if (!phba->hb_outstanding) {
James Smartbc739052010-08-04 16:11:18 -04001101 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1102 (list_empty(&psli->mboxq))) {
1103 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1104 GFP_KERNEL);
1105 if (!pmboxq) {
1106 mod_timer(&phba->hb_tmofunc,
1107 jiffies +
1108 HZ * LPFC_HB_MBOX_INTERVAL);
1109 return;
1110 }
James Smart13815c82008-01-11 01:52:48 -05001111
James Smartbc739052010-08-04 16:11:18 -04001112 lpfc_heart_beat(phba, pmboxq);
1113 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1114 pmboxq->vport = phba->pport;
1115 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1116 MBX_NOWAIT);
James Smart13815c82008-01-11 01:52:48 -05001117
James Smartbc739052010-08-04 16:11:18 -04001118 if (retval != MBX_BUSY &&
1119 retval != MBX_SUCCESS) {
1120 mempool_free(pmboxq,
1121 phba->mbox_mem_pool);
1122 mod_timer(&phba->hb_tmofunc,
1123 jiffies +
1124 HZ * LPFC_HB_MBOX_INTERVAL);
1125 return;
1126 }
1127 phba->skipped_hb = 0;
1128 phba->hb_outstanding = 1;
1129 } else if (time_before_eq(phba->last_completion_time,
1130 phba->skipped_hb)) {
1131 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1132 "2857 Last completion time not "
1133 " updated in %d ms\n",
1134 jiffies_to_msecs(jiffies
1135 - phba->last_completion_time));
1136 } else
1137 phba->skipped_hb = jiffies;
1138
James Smart858c9f62007-06-17 19:56:39 -05001139 mod_timer(&phba->hb_tmofunc,
James Smart13815c82008-01-11 01:52:48 -05001140 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
James Smart858c9f62007-06-17 19:56:39 -05001141 return;
James Smart13815c82008-01-11 01:52:48 -05001142 } else {
1143 /*
1144 * If heart beat timeout called with hb_outstanding set
James Smartdcf2a4e2010-09-29 11:18:53 -04001145 * we need to give the hb mailbox cmd a chance to
1146 * complete or TMO.
James Smart13815c82008-01-11 01:52:48 -05001147 */
James Smartdcf2a4e2010-09-29 11:18:53 -04001148 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1149 "0459 Adapter heartbeat still out"
1150 "standing:last compl time was %d ms.\n",
1151 jiffies_to_msecs(jiffies
1152 - phba->last_completion_time));
1153 mod_timer(&phba->hb_tmofunc,
1154 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
James Smart858c9f62007-06-17 19:56:39 -05001155 }
James Smart858c9f62007-06-17 19:56:39 -05001156 }
1157}
1158
James Smarte59058c2008-08-24 21:49:00 -04001159/**
James Smart3621a712009-04-06 18:47:14 -04001160 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -04001161 * @phba: pointer to lpfc hba data structure.
1162 *
1163 * This routine is called to bring the HBA offline when HBA hardware error
1164 * other than Port Error 6 has been detected.
1165 **/
James Smart09372822008-01-11 01:52:54 -05001166static void
1167lpfc_offline_eratt(struct lpfc_hba *phba)
1168{
1169 struct lpfc_sli *psli = &phba->sli;
1170
1171 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001172 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -05001173 spin_unlock_irq(&phba->hbalock);
James Smart618a5232012-06-12 13:54:36 -04001174 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart09372822008-01-11 01:52:54 -05001175
1176 lpfc_offline(phba);
1177 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001178 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001179 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001180 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001181 lpfc_hba_down_post(phba);
1182 lpfc_sli_brdready(phba, HS_MBRDY);
1183 lpfc_unblock_mgmt_io(phba);
1184 phba->link_state = LPFC_HBA_ERROR;
1185 return;
1186}
1187
James Smarte59058c2008-08-24 21:49:00 -04001188/**
James Smartda0436e2009-05-22 14:51:39 -04001189 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1190 * @phba: pointer to lpfc hba data structure.
1191 *
1192 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1193 * other than Port Error 6 has been detected.
1194 **/
1195static void
1196lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1197{
James Smart618a5232012-06-12 13:54:36 -04001198 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smartda0436e2009-05-22 14:51:39 -04001199 lpfc_offline(phba);
1200 lpfc_sli4_brdreset(phba);
1201 lpfc_hba_down_post(phba);
1202 lpfc_sli4_post_status_check(phba);
1203 lpfc_unblock_mgmt_io(phba);
1204 phba->link_state = LPFC_HBA_ERROR;
1205}
1206
1207/**
James Smarta257bf92009-04-06 18:48:10 -04001208 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1209 * @phba: pointer to lpfc hba data structure.
1210 *
1211 * This routine is invoked to handle the deferred HBA hardware error
1212 * conditions. This type of error is indicated by HBA by setting ER1
1213 * and another ER bit in the host status register. The driver will
1214 * wait until the ER1 bit clears before handling the error condition.
1215 **/
1216static void
1217lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1218{
1219 uint32_t old_host_status = phba->work_hs;
1220 struct lpfc_sli_ring *pring;
1221 struct lpfc_sli *psli = &phba->sli;
1222
James Smartf4b4c682009-05-22 14:53:12 -04001223 /* If the pci channel is offline, ignore possible errors,
1224 * since we cannot communicate with the pci card anyway.
1225 */
1226 if (pci_channel_offline(phba->pcidev)) {
1227 spin_lock_irq(&phba->hbalock);
1228 phba->hba_flag &= ~DEFER_ERATT;
1229 spin_unlock_irq(&phba->hbalock);
1230 return;
1231 }
1232
James Smarta257bf92009-04-06 18:48:10 -04001233 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1234 "0479 Deferred Adapter Hardware Error "
1235 "Data: x%x x%x x%x\n",
1236 phba->work_hs,
1237 phba->work_status[0], phba->work_status[1]);
1238
1239 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001240 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001241 spin_unlock_irq(&phba->hbalock);
1242
1243
1244 /*
1245 * Firmware stops when it triggred erratt. That could cause the I/Os
1246 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1247 * SCSI layer retry it after re-establishing link.
1248 */
1249 pring = &psli->ring[psli->fcp_ring];
1250 lpfc_sli_abort_iocb_ring(phba, pring);
1251
1252 /*
1253 * There was a firmware error. Take the hba offline and then
1254 * attempt to restart it.
1255 */
James Smart618a5232012-06-12 13:54:36 -04001256 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smarta257bf92009-04-06 18:48:10 -04001257 lpfc_offline(phba);
1258
1259 /* Wait for the ER1 bit to clear.*/
1260 while (phba->work_hs & HS_FFER1) {
1261 msleep(100);
James Smart9940b972011-03-11 16:06:12 -05001262 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1263 phba->work_hs = UNPLUG_ERR ;
1264 break;
1265 }
James Smarta257bf92009-04-06 18:48:10 -04001266 /* If driver is unloading let the worker thread continue */
1267 if (phba->pport->load_flag & FC_UNLOADING) {
1268 phba->work_hs = 0;
1269 break;
1270 }
1271 }
1272
1273 /*
1274 * This is to ptrotect against a race condition in which
1275 * first write to the host attention register clear the
1276 * host status register.
1277 */
1278 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1279 phba->work_hs = old_host_status & ~HS_FFER1;
1280
James Smart3772a992009-05-22 14:50:54 -04001281 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001282 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001283 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001284 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1285 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1286}
1287
James Smart3772a992009-05-22 14:50:54 -04001288static void
1289lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1290{
1291 struct lpfc_board_event_header board_event;
1292 struct Scsi_Host *shost;
1293
1294 board_event.event_type = FC_REG_BOARD_EVENT;
1295 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1296 shost = lpfc_shost_from_vport(phba->pport);
1297 fc_host_post_vendor_event(shost, fc_get_event_number(),
1298 sizeof(board_event),
1299 (char *) &board_event,
1300 LPFC_NL_VENDOR_ID);
1301}
1302
James Smarta257bf92009-04-06 18:48:10 -04001303/**
James Smart3772a992009-05-22 14:50:54 -04001304 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001305 * @phba: pointer to lpfc hba data structure.
1306 *
1307 * This routine is invoked to handle the following HBA hardware error
1308 * conditions:
1309 * 1 - HBA error attention interrupt
1310 * 2 - DMA ring index out of range
1311 * 3 - Mailbox command came back as unknown
1312 **/
James Smart3772a992009-05-22 14:50:54 -04001313static void
1314lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001315{
James Smart2e0fef82007-06-17 19:56:36 -05001316 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001317 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001318 struct lpfc_sli_ring *pring;
James Smartd2873e42006-08-18 17:46:43 -04001319 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001320 unsigned long temperature;
1321 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001322 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001323
Linas Vepstas8d63f372007-02-14 14:28:36 -06001324 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001325 * since we cannot communicate with the pci card anyway.
1326 */
1327 if (pci_channel_offline(phba->pcidev)) {
1328 spin_lock_irq(&phba->hbalock);
1329 phba->hba_flag &= ~DEFER_ERATT;
1330 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001331 return;
James Smart3772a992009-05-22 14:50:54 -04001332 }
1333
James Smart13815c82008-01-11 01:52:48 -05001334 /* If resets are disabled then leave the HBA alone and return */
1335 if (!phba->cfg_enable_hba_reset)
1336 return;
dea31012005-04-17 16:05:31 -05001337
James Smartea2151b2008-09-07 11:52:10 -04001338 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001339 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001340
James Smarta257bf92009-04-06 18:48:10 -04001341 if (phba->hba_flag & DEFER_ERATT)
1342 lpfc_handle_deferred_eratt(phba);
1343
James Smartdcf2a4e2010-09-29 11:18:53 -04001344 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1345 if (phba->work_hs & HS_FFER6)
1346 /* Re-establishing Link */
1347 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1348 "1301 Re-establishing Link "
1349 "Data: x%x x%x x%x\n",
1350 phba->work_hs, phba->work_status[0],
1351 phba->work_status[1]);
1352 if (phba->work_hs & HS_FFER8)
1353 /* Device Zeroization */
1354 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1355 "2861 Host Authentication device "
1356 "zeroization Data:x%x x%x x%x\n",
1357 phba->work_hs, phba->work_status[0],
1358 phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001359
James Smart92d7f7b2007-06-17 19:56:38 -05001360 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001361 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001362 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001363
1364 /*
1365 * Firmware stops when it triggled erratt with HS_FFER6.
1366 * That could cause the I/Os dropped by the firmware.
1367 * Error iocb (I/O) on txcmplq and let the SCSI layer
1368 * retry it after re-establishing link.
1369 */
1370 pring = &psli->ring[psli->fcp_ring];
1371 lpfc_sli_abort_iocb_ring(phba, pring);
1372
dea31012005-04-17 16:05:31 -05001373 /*
1374 * There was a firmware error. Take the hba offline and then
1375 * attempt to restart it.
1376 */
James Smart618a5232012-06-12 13:54:36 -04001377 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
dea31012005-04-17 16:05:31 -05001378 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001379 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001380 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001381 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001382 return;
1383 }
James Smart46fa3112007-04-25 09:51:45 -04001384 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001385 } else if (phba->work_hs & HS_CRIT_TEMP) {
1386 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1387 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1388 temp_event_data.event_code = LPFC_CRIT_TEMP;
1389 temp_event_data.data = (uint32_t)temperature;
1390
1391 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001392 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001393 "(%ld), taking this port offline "
1394 "Data: x%x x%x x%x\n",
1395 temperature, phba->work_hs,
1396 phba->work_status[0], phba->work_status[1]);
1397
1398 shost = lpfc_shost_from_vport(phba->pport);
1399 fc_host_post_vendor_event(shost, fc_get_event_number(),
1400 sizeof(temp_event_data),
1401 (char *) &temp_event_data,
1402 SCSI_NL_VID_TYPE_PCI
1403 | PCI_VENDOR_ID_EMULEX);
1404
James Smart7af67052007-10-27 13:38:11 -04001405 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001406 phba->over_temp_state = HBA_OVER_TEMP;
1407 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001408 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001409
dea31012005-04-17 16:05:31 -05001410 } else {
1411 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001412 * failure is a value other than FFER6. Do not call the offline
1413 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001414 */
1415 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001416 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001417 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001418 phba->work_hs,
dea31012005-04-17 16:05:31 -05001419 phba->work_status[0], phba->work_status[1]);
1420
James Smartd2873e42006-08-18 17:46:43 -04001421 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001422 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001423 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001424 sizeof(event_data), (char *) &event_data,
1425 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1426
James Smart09372822008-01-11 01:52:54 -05001427 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001428 }
James Smart93996272008-08-24 21:50:30 -04001429 return;
dea31012005-04-17 16:05:31 -05001430}
1431
James Smarte59058c2008-08-24 21:49:00 -04001432/**
James Smart618a5232012-06-12 13:54:36 -04001433 * lpfc_sli4_port_sta_fn_reset - The SLI4 function reset due to port status reg
1434 * @phba: pointer to lpfc hba data structure.
1435 * @mbx_action: flag for mailbox shutdown action.
1436 *
1437 * This routine is invoked to perform an SLI4 port PCI function reset in
1438 * response to port status register polling attention. It waits for port
1439 * status register (ERR, RDY, RN) bits before proceeding with function reset.
1440 * During this process, interrupt vectors are freed and later requested
1441 * for handling possible port resource change.
1442 **/
1443static int
1444lpfc_sli4_port_sta_fn_reset(struct lpfc_hba *phba, int mbx_action)
1445{
1446 int rc;
1447 uint32_t intr_mode;
1448
1449 /*
1450 * On error status condition, driver need to wait for port
1451 * ready before performing reset.
1452 */
1453 rc = lpfc_sli4_pdev_status_reg_wait(phba);
1454 if (!rc) {
1455 /* need reset: attempt for port recovery */
1456 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1457 "2887 Reset Needed: Attempting Port "
1458 "Recovery...\n");
1459 lpfc_offline_prep(phba, mbx_action);
1460 lpfc_offline(phba);
1461 /* release interrupt for possible resource change */
1462 lpfc_sli4_disable_intr(phba);
1463 lpfc_sli_brdrestart(phba);
1464 /* request and enable interrupt */
1465 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
1466 if (intr_mode == LPFC_INTR_ERROR) {
1467 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1468 "3175 Failed to enable interrupt\n");
1469 return -EIO;
1470 } else {
1471 phba->intr_mode = intr_mode;
1472 }
1473 rc = lpfc_online(phba);
1474 if (rc == 0)
1475 lpfc_unblock_mgmt_io(phba);
1476 }
1477 return rc;
1478}
1479
1480/**
James Smartda0436e2009-05-22 14:51:39 -04001481 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1482 * @phba: pointer to lpfc hba data structure.
1483 *
1484 * This routine is invoked to handle the SLI4 HBA hardware error attention
1485 * conditions.
1486 **/
1487static void
1488lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1489{
1490 struct lpfc_vport *vport = phba->pport;
1491 uint32_t event_data;
1492 struct Scsi_Host *shost;
James Smart2fcee4b2010-12-15 17:57:46 -05001493 uint32_t if_type;
James Smart2e90f4b2011-12-13 13:22:37 -05001494 struct lpfc_register portstat_reg = {0};
1495 uint32_t reg_err1, reg_err2;
1496 uint32_t uerrlo_reg, uemasklo_reg;
1497 uint32_t pci_rd_rc1, pci_rd_rc2;
James Smart73d91e52011-10-10 21:32:10 -04001498 int rc;
James Smartda0436e2009-05-22 14:51:39 -04001499
1500 /* If the pci channel is offline, ignore possible errors, since
1501 * we cannot communicate with the pci card anyway.
1502 */
1503 if (pci_channel_offline(phba->pcidev))
1504 return;
1505 /* If resets are disabled then leave the HBA alone and return */
1506 if (!phba->cfg_enable_hba_reset)
1507 return;
1508
James Smart2fcee4b2010-12-15 17:57:46 -05001509 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1510 switch (if_type) {
1511 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart2e90f4b2011-12-13 13:22:37 -05001512 pci_rd_rc1 = lpfc_readl(
1513 phba->sli4_hba.u.if_type0.UERRLOregaddr,
1514 &uerrlo_reg);
1515 pci_rd_rc2 = lpfc_readl(
1516 phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
1517 &uemasklo_reg);
1518 /* consider PCI bus read error as pci_channel_offline */
1519 if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
1520 return;
James Smart2fcee4b2010-12-15 17:57:46 -05001521 lpfc_sli4_offline_eratt(phba);
1522 break;
1523 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart2e90f4b2011-12-13 13:22:37 -05001524 pci_rd_rc1 = lpfc_readl(
1525 phba->sli4_hba.u.if_type2.STATUSregaddr,
1526 &portstat_reg.word0);
1527 /* consider PCI bus read error as pci_channel_offline */
James Smart6b5151f2012-01-18 16:24:06 -05001528 if (pci_rd_rc1 == -EIO) {
1529 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1530 "3151 PCI bus read access failure: x%x\n",
1531 readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
James Smart2e90f4b2011-12-13 13:22:37 -05001532 return;
James Smart6b5151f2012-01-18 16:24:06 -05001533 }
James Smart2e90f4b2011-12-13 13:22:37 -05001534 reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
1535 reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
James Smart2fcee4b2010-12-15 17:57:46 -05001536 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
1537 /* TODO: Register for Overtemp async events. */
1538 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1539 "2889 Port Overtemperature event, "
James Smart026abb82011-12-13 13:20:45 -05001540 "taking port offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001541 spin_lock_irq(&phba->hbalock);
1542 phba->over_temp_state = HBA_OVER_TEMP;
1543 spin_unlock_irq(&phba->hbalock);
1544 lpfc_sli4_offline_eratt(phba);
James Smart2e90f4b2011-12-13 13:22:37 -05001545 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001546 }
James Smart2e90f4b2011-12-13 13:22:37 -05001547 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1548 reg_err2 == SLIPORT_ERR2_REG_FW_RESTART)
1549 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1550 "3143 Port Down: Firmware Restarted\n");
1551 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1552 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1553 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1554 "3144 Port Down: Debug Dump\n");
1555 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1556 reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
1557 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1558 "3145 Port Down: Provisioning\n");
James Smart618a5232012-06-12 13:54:36 -04001559
1560 /* Check port status register for function reset */
1561 rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT);
1562 if (rc == 0) {
1563 /* don't report event on forced debug dump */
1564 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1565 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1566 return;
1567 else
1568 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001569 }
James Smart618a5232012-06-12 13:54:36 -04001570 /* fall through for not able to recover */
James Smart6b5151f2012-01-18 16:24:06 -05001571 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1572 "3152 Unrecoverable error, bring the port "
1573 "offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001574 lpfc_sli4_offline_eratt(phba);
1575 break;
1576 case LPFC_SLI_INTF_IF_TYPE_1:
1577 default:
1578 break;
1579 }
James Smart2e90f4b2011-12-13 13:22:37 -05001580 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1581 "3123 Report dump event to upper layer\n");
1582 /* Send an internal error event to mgmt application */
1583 lpfc_board_errevt_to_mgmt(phba);
1584
1585 event_data = FC_REG_DUMP_EVENT;
1586 shost = lpfc_shost_from_vport(vport);
1587 fc_host_post_vendor_event(shost, fc_get_event_number(),
1588 sizeof(event_data), (char *) &event_data,
1589 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
James Smartda0436e2009-05-22 14:51:39 -04001590}
1591
1592/**
1593 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1594 * @phba: pointer to lpfc HBA data structure.
1595 *
1596 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1597 * routine from the API jump table function pointer from the lpfc_hba struct.
1598 *
1599 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001600 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001601 * Any other value - error.
1602 **/
1603void
1604lpfc_handle_eratt(struct lpfc_hba *phba)
1605{
1606 (*phba->lpfc_handle_eratt)(phba);
1607}
1608
1609/**
James Smart3621a712009-04-06 18:47:14 -04001610 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04001611 * @phba: pointer to lpfc hba data structure.
1612 *
1613 * This routine is invoked from the worker thread to handle a HBA host
1614 * attention link event.
1615 **/
dea31012005-04-17 16:05:31 -05001616void
James Smart2e0fef82007-06-17 19:56:36 -05001617lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001618{
James Smart2e0fef82007-06-17 19:56:36 -05001619 struct lpfc_vport *vport = phba->pport;
1620 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001621 LPFC_MBOXQ_t *pmb;
1622 volatile uint32_t control;
1623 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05001624 int rc = 0;
dea31012005-04-17 16:05:31 -05001625
1626 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001627 if (!pmb) {
1628 rc = 1;
dea31012005-04-17 16:05:31 -05001629 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05001630 }
dea31012005-04-17 16:05:31 -05001631
1632 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001633 if (!mp) {
1634 rc = 2;
dea31012005-04-17 16:05:31 -05001635 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05001636 }
dea31012005-04-17 16:05:31 -05001637
1638 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05001639 if (!mp->virt) {
1640 rc = 3;
dea31012005-04-17 16:05:31 -05001641 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05001642 }
dea31012005-04-17 16:05:31 -05001643
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05001644 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04001645 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05001646
1647 psli->slistat.link_event++;
James Smart76a95d72010-11-20 23:11:48 -05001648 lpfc_read_topology(phba, pmb, mp);
1649 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smart2e0fef82007-06-17 19:56:36 -05001650 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04001651 /* Block ELS IOCBs until we have processed this mbox command */
1652 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04001653 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05001654 if (rc == MBX_NOT_FINISHED) {
1655 rc = 4;
James Smart14691152006-12-02 13:34:28 -05001656 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05001657 }
dea31012005-04-17 16:05:31 -05001658
1659 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05001660 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001661 writel(HA_LATT, phba->HAregaddr);
1662 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001663 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001664
1665 return;
1666
James Smart14691152006-12-02 13:34:28 -05001667lpfc_handle_latt_free_mbuf:
James Smart0d2b6b82008-06-14 22:52:47 -04001668 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05001669 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001670lpfc_handle_latt_free_mp:
1671 kfree(mp);
1672lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04001673 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001674lpfc_handle_latt_err_exit:
1675 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001676 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001677 psli->sli_flag |= LPFC_PROCESS_LA;
1678 control = readl(phba->HCregaddr);
1679 control |= HC_LAINT_ENA;
1680 writel(control, phba->HCregaddr);
1681 readl(phba->HCregaddr); /* flush */
1682
1683 /* Clear Link Attention in HA REG */
1684 writel(HA_LATT, phba->HAregaddr);
1685 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001686 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001687 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001688 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001689
James Smart09372822008-01-11 01:52:54 -05001690 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1691 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05001692
1693 return;
1694}
1695
James Smarte59058c2008-08-24 21:49:00 -04001696/**
James Smart3621a712009-04-06 18:47:14 -04001697 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04001698 * @phba: pointer to lpfc hba data structure.
1699 * @vpd: pointer to the vital product data.
1700 * @len: length of the vital product data in bytes.
1701 *
1702 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1703 * an array of characters. In this routine, the ModelName, ProgramType, and
1704 * ModelDesc, etc. fields of the phba data structure will be populated.
1705 *
1706 * Return codes
1707 * 0 - pointer to the VPD passed in is NULL
1708 * 1 - success
1709 **/
James Smart3772a992009-05-22 14:50:54 -04001710int
James Smart2e0fef82007-06-17 19:56:36 -05001711lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05001712{
1713 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05001714 int Length;
dea31012005-04-17 16:05:31 -05001715 int i, j;
1716 int finished = 0;
1717 int index = 0;
1718
1719 if (!vpd)
1720 return 0;
1721
1722 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05001723 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001724 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05001725 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1726 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001727 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05001728 switch (vpd[index]) {
1729 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001730 case 0x91:
dea31012005-04-17 16:05:31 -05001731 index += 1;
1732 lenlo = vpd[index];
1733 index += 1;
1734 lenhi = vpd[index];
1735 index += 1;
1736 i = ((((unsigned short)lenhi) << 8) + lenlo);
1737 index += i;
1738 break;
1739 case 0x90:
1740 index += 1;
1741 lenlo = vpd[index];
1742 index += 1;
1743 lenhi = vpd[index];
1744 index += 1;
1745 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001746 if (Length > len - index)
1747 Length = len - index;
dea31012005-04-17 16:05:31 -05001748 while (Length > 0) {
1749 /* Look for Serial Number */
1750 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1751 index += 2;
1752 i = vpd[index];
1753 index += 1;
1754 j = 0;
1755 Length -= (3+i);
1756 while(i--) {
1757 phba->SerialNumber[j++] = vpd[index++];
1758 if (j == 31)
1759 break;
1760 }
1761 phba->SerialNumber[j] = 0;
1762 continue;
1763 }
1764 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1765 phba->vpd_flag |= VPD_MODEL_DESC;
1766 index += 2;
1767 i = vpd[index];
1768 index += 1;
1769 j = 0;
1770 Length -= (3+i);
1771 while(i--) {
1772 phba->ModelDesc[j++] = vpd[index++];
1773 if (j == 255)
1774 break;
1775 }
1776 phba->ModelDesc[j] = 0;
1777 continue;
1778 }
1779 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1780 phba->vpd_flag |= VPD_MODEL_NAME;
1781 index += 2;
1782 i = vpd[index];
1783 index += 1;
1784 j = 0;
1785 Length -= (3+i);
1786 while(i--) {
1787 phba->ModelName[j++] = vpd[index++];
1788 if (j == 79)
1789 break;
1790 }
1791 phba->ModelName[j] = 0;
1792 continue;
1793 }
1794 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1795 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1796 index += 2;
1797 i = vpd[index];
1798 index += 1;
1799 j = 0;
1800 Length -= (3+i);
1801 while(i--) {
1802 phba->ProgramType[j++] = vpd[index++];
1803 if (j == 255)
1804 break;
1805 }
1806 phba->ProgramType[j] = 0;
1807 continue;
1808 }
1809 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1810 phba->vpd_flag |= VPD_PORT;
1811 index += 2;
1812 i = vpd[index];
1813 index += 1;
1814 j = 0;
1815 Length -= (3+i);
1816 while(i--) {
James Smartcd1c8302011-10-10 21:33:25 -04001817 if ((phba->sli_rev == LPFC_SLI_REV4) &&
1818 (phba->sli4_hba.pport_name_sta ==
1819 LPFC_SLI4_PPNAME_GET)) {
1820 j++;
1821 index++;
1822 } else
1823 phba->Port[j++] = vpd[index++];
1824 if (j == 19)
1825 break;
dea31012005-04-17 16:05:31 -05001826 }
James Smartcd1c8302011-10-10 21:33:25 -04001827 if ((phba->sli_rev != LPFC_SLI_REV4) ||
1828 (phba->sli4_hba.pport_name_sta ==
1829 LPFC_SLI4_PPNAME_NON))
1830 phba->Port[j] = 0;
dea31012005-04-17 16:05:31 -05001831 continue;
1832 }
1833 else {
1834 index += 2;
1835 i = vpd[index];
1836 index += 1;
1837 index += i;
1838 Length -= (3 + i);
1839 }
1840 }
1841 finished = 0;
1842 break;
1843 case 0x78:
1844 finished = 1;
1845 break;
1846 default:
1847 index ++;
1848 break;
1849 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001850 }
dea31012005-04-17 16:05:31 -05001851
1852 return(1);
1853}
1854
James Smarte59058c2008-08-24 21:49:00 -04001855/**
James Smart3621a712009-04-06 18:47:14 -04001856 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04001857 * @phba: pointer to lpfc hba data structure.
1858 * @mdp: pointer to the data structure to hold the derived model name.
1859 * @descp: pointer to the data structure to hold the derived description.
1860 *
1861 * This routine retrieves HBA's description based on its registered PCI device
1862 * ID. The @descp passed into this function points to an array of 256 chars. It
1863 * shall be returned with the model name, maximum speed, and the host bus type.
1864 * The @mdp passed into this function points to an array of 80 chars. When the
1865 * function returns, the @mdp will be filled with the model name.
1866 **/
dea31012005-04-17 16:05:31 -05001867static void
James Smart2e0fef82007-06-17 19:56:36 -05001868lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05001869{
1870 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05001871 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001872 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04001873 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04001874 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001875 struct {
James Smarta747c9c2009-11-18 15:41:10 -05001876 char *name;
1877 char *bus;
1878 char *function;
1879 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001880
1881 if (mdp && mdp[0] != '\0'
1882 && descp && descp[0] != '\0')
1883 return;
1884
James Smartc0c11512011-05-24 11:41:34 -04001885 if (phba->lmt & LMT_16Gb)
1886 max_speed = 16;
1887 else if (phba->lmt & LMT_10Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001888 max_speed = 10;
1889 else if (phba->lmt & LMT_8Gb)
1890 max_speed = 8;
1891 else if (phba->lmt & LMT_4Gb)
1892 max_speed = 4;
1893 else if (phba->lmt & LMT_2Gb)
1894 max_speed = 2;
James Smart4169d862012-09-29 11:32:09 -04001895 else if (phba->lmt & LMT_1Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001896 max_speed = 1;
James Smart4169d862012-09-29 11:32:09 -04001897 else
1898 max_speed = 0;
dea31012005-04-17 16:05:31 -05001899
1900 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05001901
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001902 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001903 case PCI_DEVICE_ID_FIREFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001904 m = (typeof(m)){"LP6000", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001905 break;
dea31012005-04-17 16:05:31 -05001906 case PCI_DEVICE_ID_SUPERFLY:
1907 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smarta747c9c2009-11-18 15:41:10 -05001908 m = (typeof(m)){"LP7000", "PCI",
1909 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001910 else
James Smarta747c9c2009-11-18 15:41:10 -05001911 m = (typeof(m)){"LP7000E", "PCI",
1912 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001913 break;
1914 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001915 m = (typeof(m)){"LP8000", "PCI",
1916 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001917 break;
1918 case PCI_DEVICE_ID_CENTAUR:
1919 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smarta747c9c2009-11-18 15:41:10 -05001920 m = (typeof(m)){"LP9002", "PCI",
1921 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001922 else
James Smarta747c9c2009-11-18 15:41:10 -05001923 m = (typeof(m)){"LP9000", "PCI",
1924 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001925 break;
1926 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001927 m = (typeof(m)){"LP952", "PCI",
1928 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001929 break;
1930 case PCI_DEVICE_ID_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05001931 m = (typeof(m)){"LP9802", "PCI-X",
1932 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001933 break;
1934 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05001935 m = (typeof(m)){"LP10000", "PCI-X",
1936 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001937 break;
1938 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05001939 m = (typeof(m)){"LPX1000", "PCI-X",
1940 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001941 break;
1942 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001943 m = (typeof(m)){"LP982", "PCI-X",
1944 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001945 break;
1946 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001947 m = (typeof(m)){"LP1050", "PCI-X",
1948 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001949 break;
1950 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05001951 m = (typeof(m)){"LP11000", "PCI-X2",
1952 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001953 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001954 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001955 m = (typeof(m)){"LP11000-SP", "PCI-X2",
1956 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001957 break;
1958 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001959 m = (typeof(m)){"LP11002-SP", "PCI-X2",
1960 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001961 break;
1962 case PCI_DEVICE_ID_NEPTUNE:
James Smarta747c9c2009-11-18 15:41:10 -05001963 m = (typeof(m)){"LPe1000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001964 break;
1965 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001966 m = (typeof(m)){"LPe1000-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001967 break;
1968 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001969 m = (typeof(m)){"LPe1002-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001970 break;
dea31012005-04-17 16:05:31 -05001971 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05001972 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001973 break;
1974 case PCI_DEVICE_ID_BSMB:
James Smarta747c9c2009-11-18 15:41:10 -05001975 m = (typeof(m)){"LP111", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001976 break;
1977 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05001978 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001979 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001980 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001981 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001982 break;
1983 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001984 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04001985 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001986 break;
dea31012005-04-17 16:05:31 -05001987 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05001988 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001989 break;
1990 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05001991 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001992 break;
1993 case PCI_DEVICE_ID_LP101:
James Smarta747c9c2009-11-18 15:41:10 -05001994 m = (typeof(m)){"LP101", "PCI-X", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001995 break;
1996 case PCI_DEVICE_ID_LP10000S:
James Smarta747c9c2009-11-18 15:41:10 -05001997 m = (typeof(m)){"LP10000-S", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001998 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001999 case PCI_DEVICE_ID_LP11000S:
James Smarta747c9c2009-11-18 15:41:10 -05002000 m = (typeof(m)){"LP11000-S", "PCI-X2", "Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05002001 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002002 case PCI_DEVICE_ID_LPE11000S:
James Smarta747c9c2009-11-18 15:41:10 -05002003 m = (typeof(m)){"LPe11000-S", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002004 break;
James Smartb87eab32007-04-25 09:53:28 -04002005 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05002006 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002007 break;
2008 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05002009 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002010 break;
2011 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05002012 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002013 break;
2014 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002015 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002016 break;
2017 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002018 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002019 break;
2020 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05002021 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002022 break;
James Smart84774a42008-08-24 21:50:06 -04002023 case PCI_DEVICE_ID_HORNET:
James Smarta747c9c2009-11-18 15:41:10 -05002024 m = (typeof(m)){"LP21000", "PCIe", "FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002025 GE = 1;
2026 break;
2027 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05002028 m = (typeof(m)){"LPev12000", "PCIe IOV",
2029 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002030 break;
2031 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05002032 m = (typeof(m)){"LPev12000", "PCIe IOV",
2033 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002034 break;
2035 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05002036 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
2037 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002038 break;
James Smartda0436e2009-05-22 14:51:39 -04002039 case PCI_DEVICE_ID_TIGERSHARK:
2040 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002041 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04002042 break;
James Smarta747c9c2009-11-18 15:41:10 -05002043 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04002044 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002045 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2046 break;
2047 case PCI_DEVICE_ID_FALCON:
2048 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2049 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04002050 break;
James Smart98fc5dd2010-06-07 15:24:29 -04002051 case PCI_DEVICE_ID_BALIUS:
2052 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
2053 "Fibre Channel Adapter"};
2054 break;
James Smart085c6472010-11-20 23:11:37 -05002055 case PCI_DEVICE_ID_LANCER_FC:
James Smartc0c11512011-05-24 11:41:34 -04002056 case PCI_DEVICE_ID_LANCER_FC_VF:
2057 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
James Smart085c6472010-11-20 23:11:37 -05002058 break;
2059 case PCI_DEVICE_ID_LANCER_FCOE:
James Smartc0c11512011-05-24 11:41:34 -04002060 case PCI_DEVICE_ID_LANCER_FCOE_VF:
James Smart085c6472010-11-20 23:11:37 -05002061 oneConnect = 1;
James Smart079b5c92011-08-21 21:48:49 -04002062 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
James Smart085c6472010-11-20 23:11:37 -05002063 break;
James Smartf8cafd32012-08-03 12:42:51 -04002064 case PCI_DEVICE_ID_SKYHAWK:
2065 case PCI_DEVICE_ID_SKYHAWK_VF:
2066 oneConnect = 1;
2067 m = (typeof(m)){"OCe14000", "PCIe", "FCoE"};
2068 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002069 default:
James Smarta747c9c2009-11-18 15:41:10 -05002070 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002071 break;
dea31012005-04-17 16:05:31 -05002072 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002073
2074 if (mdp && mdp[0] == '\0')
2075 snprintf(mdp, 79,"%s", m.name);
James Smartc0c11512011-05-24 11:41:34 -04002076 /*
2077 * oneConnect hba requires special processing, they are all initiators
James Smartda0436e2009-05-22 14:51:39 -04002078 * and we put the port number on the end
2079 */
2080 if (descp && descp[0] == '\0') {
2081 if (oneConnect)
2082 snprintf(descp, 255,
James Smart4169d862012-09-29 11:32:09 -04002083 "Emulex OneConnect %s, %s Initiator %s",
James Smarta747c9c2009-11-18 15:41:10 -05002084 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04002085 phba->Port);
James Smart4169d862012-09-29 11:32:09 -04002086 else if (max_speed == 0)
2087 snprintf(descp, 255,
2088 "Emulex %s %s %s ",
2089 m.name, m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002090 else
2091 snprintf(descp, 255,
2092 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05002093 m.name, max_speed, (GE) ? "GE" : "Gb",
2094 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002095 }
dea31012005-04-17 16:05:31 -05002096}
2097
James Smarte59058c2008-08-24 21:49:00 -04002098/**
James Smart3621a712009-04-06 18:47:14 -04002099 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04002100 * @phba: pointer to lpfc hba data structure.
2101 * @pring: pointer to a IOCB ring.
2102 * @cnt: the number of IOCBs to be posted to the IOCB ring.
2103 *
2104 * This routine posts a given number of IOCBs with the associated DMA buffer
2105 * descriptors specified by the cnt argument to the given IOCB ring.
2106 *
2107 * Return codes
2108 * The number of IOCBs NOT able to be posted to the IOCB ring.
2109 **/
dea31012005-04-17 16:05:31 -05002110int
James Smart495a7142008-06-14 22:52:59 -04002111lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05002112{
2113 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002114 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05002115 struct lpfc_dmabuf *mp1, *mp2;
2116
2117 cnt += pring->missbufcnt;
2118
2119 /* While there are buffers to post */
2120 while (cnt > 0) {
2121 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002122 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002123 if (iocb == NULL) {
2124 pring->missbufcnt = cnt;
2125 return cnt;
2126 }
dea31012005-04-17 16:05:31 -05002127 icmd = &iocb->iocb;
2128
2129 /* 2 buffers can be posted per command */
2130 /* Allocate buffer to post */
2131 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2132 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04002133 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2134 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002135 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002136 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002137 pring->missbufcnt = cnt;
2138 return cnt;
2139 }
2140
2141 INIT_LIST_HEAD(&mp1->list);
2142 /* Allocate buffer to post */
2143 if (cnt > 1) {
2144 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2145 if (mp2)
2146 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2147 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04002148 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002149 kfree(mp2);
dea31012005-04-17 16:05:31 -05002150 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2151 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002152 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002153 pring->missbufcnt = cnt;
2154 return cnt;
2155 }
2156
2157 INIT_LIST_HEAD(&mp2->list);
2158 } else {
2159 mp2 = NULL;
2160 }
2161
2162 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2163 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2164 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2165 icmd->ulpBdeCount = 1;
2166 cnt--;
2167 if (mp2) {
2168 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2169 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2170 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2171 cnt--;
2172 icmd->ulpBdeCount = 2;
2173 }
2174
2175 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2176 icmd->ulpLe = 1;
2177
James Smart3772a992009-05-22 14:50:54 -04002178 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2179 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05002180 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2181 kfree(mp1);
2182 cnt++;
2183 if (mp2) {
2184 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2185 kfree(mp2);
2186 cnt++;
2187 }
James Bottomley604a3e32005-10-29 10:28:33 -05002188 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002189 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05002190 return cnt;
2191 }
dea31012005-04-17 16:05:31 -05002192 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05002193 if (mp2)
dea31012005-04-17 16:05:31 -05002194 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05002195 }
2196 pring->missbufcnt = 0;
2197 return 0;
2198}
2199
James Smarte59058c2008-08-24 21:49:00 -04002200/**
James Smart3621a712009-04-06 18:47:14 -04002201 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04002202 * @phba: pointer to lpfc hba data structure.
2203 *
2204 * This routine posts initial receive IOCB buffers to the ELS ring. The
2205 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
2206 * set to 64 IOCBs.
2207 *
2208 * Return codes
2209 * 0 - success (currently always success)
2210 **/
dea31012005-04-17 16:05:31 -05002211static int
James Smart2e0fef82007-06-17 19:56:36 -05002212lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002213{
2214 struct lpfc_sli *psli = &phba->sli;
2215
2216 /* Ring 0, ELS / CT buffers */
James Smart495a7142008-06-14 22:52:59 -04002217 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05002218 /* Ring 2 - FCP no buffers needed */
2219
2220 return 0;
2221}
2222
2223#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2224
James Smarte59058c2008-08-24 21:49:00 -04002225/**
James Smart3621a712009-04-06 18:47:14 -04002226 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04002227 * @HashResultPointer: pointer to an array as hash table.
2228 *
2229 * This routine sets up the initial values to the array of hash table entries
2230 * for the LC HBAs.
2231 **/
dea31012005-04-17 16:05:31 -05002232static void
2233lpfc_sha_init(uint32_t * HashResultPointer)
2234{
2235 HashResultPointer[0] = 0x67452301;
2236 HashResultPointer[1] = 0xEFCDAB89;
2237 HashResultPointer[2] = 0x98BADCFE;
2238 HashResultPointer[3] = 0x10325476;
2239 HashResultPointer[4] = 0xC3D2E1F0;
2240}
2241
James Smarte59058c2008-08-24 21:49:00 -04002242/**
James Smart3621a712009-04-06 18:47:14 -04002243 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04002244 * @HashResultPointer: pointer to an initial/result hash table.
2245 * @HashWorkingPointer: pointer to an working hash table.
2246 *
2247 * This routine iterates an initial hash table pointed by @HashResultPointer
2248 * with the values from the working hash table pointeed by @HashWorkingPointer.
2249 * The results are putting back to the initial hash table, returned through
2250 * the @HashResultPointer as the result hash table.
2251 **/
dea31012005-04-17 16:05:31 -05002252static void
2253lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2254{
2255 int t;
2256 uint32_t TEMP;
2257 uint32_t A, B, C, D, E;
2258 t = 16;
2259 do {
2260 HashWorkingPointer[t] =
2261 S(1,
2262 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2263 8] ^
2264 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2265 } while (++t <= 79);
2266 t = 0;
2267 A = HashResultPointer[0];
2268 B = HashResultPointer[1];
2269 C = HashResultPointer[2];
2270 D = HashResultPointer[3];
2271 E = HashResultPointer[4];
2272
2273 do {
2274 if (t < 20) {
2275 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2276 } else if (t < 40) {
2277 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2278 } else if (t < 60) {
2279 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2280 } else {
2281 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2282 }
2283 TEMP += S(5, A) + E + HashWorkingPointer[t];
2284 E = D;
2285 D = C;
2286 C = S(30, B);
2287 B = A;
2288 A = TEMP;
2289 } while (++t <= 79);
2290
2291 HashResultPointer[0] += A;
2292 HashResultPointer[1] += B;
2293 HashResultPointer[2] += C;
2294 HashResultPointer[3] += D;
2295 HashResultPointer[4] += E;
2296
2297}
2298
James Smarte59058c2008-08-24 21:49:00 -04002299/**
James Smart3621a712009-04-06 18:47:14 -04002300 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002301 * @RandomChallenge: pointer to the entry of host challenge random number array.
2302 * @HashWorking: pointer to the entry of the working hash array.
2303 *
2304 * This routine calculates the working hash array referred by @HashWorking
2305 * from the challenge random numbers associated with the host, referred by
2306 * @RandomChallenge. The result is put into the entry of the working hash
2307 * array and returned by reference through @HashWorking.
2308 **/
dea31012005-04-17 16:05:31 -05002309static void
2310lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2311{
2312 *HashWorking = (*RandomChallenge ^ *HashWorking);
2313}
2314
James Smarte59058c2008-08-24 21:49:00 -04002315/**
James Smart3621a712009-04-06 18:47:14 -04002316 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002317 * @phba: pointer to lpfc hba data structure.
2318 * @hbainit: pointer to an array of unsigned 32-bit integers.
2319 *
2320 * This routine performs the special handling for LC HBA initialization.
2321 **/
dea31012005-04-17 16:05:31 -05002322void
2323lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2324{
2325 int t;
2326 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002327 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002328
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002329 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002330 if (!HashWorking)
2331 return;
2332
dea31012005-04-17 16:05:31 -05002333 HashWorking[0] = HashWorking[78] = *pwwnn++;
2334 HashWorking[1] = HashWorking[79] = *pwwnn;
2335
2336 for (t = 0; t < 7; t++)
2337 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2338
2339 lpfc_sha_init(hbainit);
2340 lpfc_sha_iterate(hbainit, HashWorking);
2341 kfree(HashWorking);
2342}
2343
James Smarte59058c2008-08-24 21:49:00 -04002344/**
James Smart3621a712009-04-06 18:47:14 -04002345 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002346 * @vport: pointer to a virtual N_Port data structure.
2347 *
2348 * This routine performs the necessary cleanups before deleting the @vport.
2349 * It invokes the discovery state machine to perform necessary state
2350 * transitions and to release the ndlps associated with the @vport. Note,
2351 * the physical port is treated as @vport 0.
2352 **/
James Smart87af33f2007-10-27 13:37:43 -04002353void
James Smart2e0fef82007-06-17 19:56:36 -05002354lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002355{
James Smart87af33f2007-10-27 13:37:43 -04002356 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002357 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002358 int i = 0;
dea31012005-04-17 16:05:31 -05002359
James Smart87af33f2007-10-27 13:37:43 -04002360 if (phba->link_state > LPFC_LINK_DOWN)
2361 lpfc_port_link_failure(vport);
2362
2363 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002364 if (!NLP_CHK_NODE_ACT(ndlp)) {
2365 ndlp = lpfc_enable_node(vport, ndlp,
2366 NLP_STE_UNUSED_NODE);
2367 if (!ndlp)
2368 continue;
2369 spin_lock_irq(&phba->ndlp_lock);
2370 NLP_SET_FREE_REQ(ndlp);
2371 spin_unlock_irq(&phba->ndlp_lock);
2372 /* Trigger the release of the ndlp memory */
2373 lpfc_nlp_put(ndlp);
2374 continue;
2375 }
2376 spin_lock_irq(&phba->ndlp_lock);
2377 if (NLP_CHK_FREE_REQ(ndlp)) {
2378 /* The ndlp should not be in memory free mode already */
2379 spin_unlock_irq(&phba->ndlp_lock);
2380 continue;
2381 } else
2382 /* Indicate request for freeing ndlp memory */
2383 NLP_SET_FREE_REQ(ndlp);
2384 spin_unlock_irq(&phba->ndlp_lock);
2385
James Smart58da1ff2008-04-07 10:15:56 -04002386 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2387 ndlp->nlp_DID == Fabric_DID) {
2388 /* Just free up ndlp with Fabric_DID for vports */
2389 lpfc_nlp_put(ndlp);
2390 continue;
2391 }
2392
James Smarteff4a012012-01-18 16:25:25 -05002393 /* take care of nodes in unused state before the state
2394 * machine taking action.
2395 */
2396 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
2397 lpfc_nlp_put(ndlp);
2398 continue;
2399 }
2400
James Smart87af33f2007-10-27 13:37:43 -04002401 if (ndlp->nlp_type & NLP_FABRIC)
2402 lpfc_disc_state_machine(vport, ndlp, NULL,
2403 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002404
James Smart87af33f2007-10-27 13:37:43 -04002405 lpfc_disc_state_machine(vport, ndlp, NULL,
2406 NLP_EVT_DEVICE_RM);
2407 }
2408
James Smarta8adb832007-10-27 13:37:53 -04002409 /* At this point, ALL ndlp's should be gone
2410 * because of the previous NLP_EVT_DEVICE_RM.
2411 * Lets wait for this to happen, if needed.
2412 */
James Smart87af33f2007-10-27 13:37:43 -04002413 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002414 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002415 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002416 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002417 list_for_each_entry_safe(ndlp, next_ndlp,
2418 &vport->fc_nodes, nlp_listp) {
2419 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2420 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002421 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002422 "usgmap:x%x refcnt:%d\n",
2423 ndlp->nlp_DID, (void *)ndlp,
2424 ndlp->nlp_usg_map,
2425 atomic_read(
2426 &ndlp->kref.refcount));
2427 }
James Smarta8adb832007-10-27 13:37:53 -04002428 break;
James Smart87af33f2007-10-27 13:37:43 -04002429 }
James Smarta8adb832007-10-27 13:37:53 -04002430
2431 /* Wait for any activity on ndlps to settle */
2432 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002433 }
James Smart1151e3e2011-02-16 12:39:35 -05002434 lpfc_cleanup_vports_rrqs(vport, NULL);
dea31012005-04-17 16:05:31 -05002435}
2436
James Smarte59058c2008-08-24 21:49:00 -04002437/**
James Smart3621a712009-04-06 18:47:14 -04002438 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002439 * @vport: pointer to a virtual N_Port data structure.
2440 *
2441 * This routine stops all the timers associated with a @vport. This function
2442 * is invoked before disabling or deleting a @vport. Note that the physical
2443 * port is treated as @vport 0.
2444 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002445void
2446lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002447{
James Smart92d7f7b2007-06-17 19:56:38 -05002448 del_timer_sync(&vport->els_tmofunc);
2449 del_timer_sync(&vport->fc_fdmitmo);
James Smart92494142011-02-16 12:39:44 -05002450 del_timer_sync(&vport->delayed_disc_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002451 lpfc_can_disctmo(vport);
2452 return;
dea31012005-04-17 16:05:31 -05002453}
2454
James Smarte59058c2008-08-24 21:49:00 -04002455/**
James Smartecfd03c2010-02-12 14:41:27 -05002456 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2457 * @phba: pointer to lpfc hba data structure.
2458 *
2459 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2460 * caller of this routine should already hold the host lock.
2461 **/
2462void
2463__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2464{
James Smart5ac6b302010-10-22 11:05:36 -04002465 /* Clear pending FCF rediscovery wait flag */
2466 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2467
James Smartecfd03c2010-02-12 14:41:27 -05002468 /* Now, try to stop the timer */
2469 del_timer(&phba->fcf.redisc_wait);
2470}
2471
2472/**
2473 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2474 * @phba: pointer to lpfc hba data structure.
2475 *
2476 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2477 * checks whether the FCF rediscovery wait timer is pending with the host
2478 * lock held before proceeding with disabling the timer and clearing the
2479 * wait timer pendig flag.
2480 **/
2481void
2482lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2483{
2484 spin_lock_irq(&phba->hbalock);
2485 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2486 /* FCF rediscovery timer already fired or stopped */
2487 spin_unlock_irq(&phba->hbalock);
2488 return;
2489 }
2490 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart5ac6b302010-10-22 11:05:36 -04002491 /* Clear failover in progress flags */
2492 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05002493 spin_unlock_irq(&phba->hbalock);
2494}
2495
2496/**
James Smart3772a992009-05-22 14:50:54 -04002497 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002498 * @phba: pointer to lpfc hba data structure.
2499 *
2500 * This routine stops all the timers associated with a HBA. This function is
2501 * invoked before either putting a HBA offline or unloading the driver.
2502 **/
James Smart3772a992009-05-22 14:50:54 -04002503void
2504lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002505{
James Smart51ef4c22007-08-02 11:10:31 -04002506 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05002507 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002508 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002509 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002510 del_timer_sync(&phba->hb_tmofunc);
James Smart1151e3e2011-02-16 12:39:35 -05002511 if (phba->sli_rev == LPFC_SLI_REV4) {
2512 del_timer_sync(&phba->rrq_tmr);
2513 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2514 }
James Smart3772a992009-05-22 14:50:54 -04002515 phba->hb_outstanding = 0;
2516
2517 switch (phba->pci_dev_grp) {
2518 case LPFC_PCI_DEV_LP:
2519 /* Stop any LightPulse device specific driver timers */
2520 del_timer_sync(&phba->fcp_poll_timer);
2521 break;
2522 case LPFC_PCI_DEV_OC:
2523 /* Stop any OneConnect device sepcific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05002524 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04002525 break;
2526 default:
2527 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2528 "0297 Invalid device group (x%x)\n",
2529 phba->pci_dev_grp);
2530 break;
2531 }
James Smart2e0fef82007-06-17 19:56:36 -05002532 return;
dea31012005-04-17 16:05:31 -05002533}
2534
James Smarte59058c2008-08-24 21:49:00 -04002535/**
James Smart3621a712009-04-06 18:47:14 -04002536 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002537 * @phba: pointer to lpfc hba data structure.
2538 *
2539 * This routine marks a HBA's management interface as blocked. Once the HBA's
2540 * management interface is marked as blocked, all the user space access to
2541 * the HBA, whether they are from sysfs interface or libdfc interface will
2542 * all be blocked. The HBA is set to block the management interface when the
2543 * driver prepares the HBA interface for online or offline.
2544 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002545static void
James Smart618a5232012-06-12 13:54:36 -04002546lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
Adrian Bunka6ababd2007-11-05 18:07:33 +01002547{
2548 unsigned long iflag;
James Smart6e7288d2010-06-07 15:23:35 -04002549 uint8_t actcmd = MBX_HEARTBEAT;
2550 unsigned long timeout;
2551
Adrian Bunka6ababd2007-11-05 18:07:33 +01002552 spin_lock_irqsave(&phba->hbalock, iflag);
2553 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
James Smart618a5232012-06-12 13:54:36 -04002554 spin_unlock_irqrestore(&phba->hbalock, iflag);
2555 if (mbx_action == LPFC_MBX_NO_WAIT)
2556 return;
2557 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
2558 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002559 if (phba->sli.mbox_active) {
James Smart6e7288d2010-06-07 15:23:35 -04002560 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
James Smarta183a152011-10-10 21:32:43 -04002561 /* Determine how long we might wait for the active mailbox
2562 * command to be gracefully completed by firmware.
2563 */
2564 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
2565 phba->sli.mbox_active) * 1000) + jiffies;
2566 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002567 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002568
James Smart6e7288d2010-06-07 15:23:35 -04002569 /* Wait for the outstnading mailbox command to complete */
2570 while (phba->sli.mbox_active) {
2571 /* Check active mailbox complete status every 2ms */
2572 msleep(2);
2573 if (time_after(jiffies, timeout)) {
2574 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2575 "2813 Mgmt IO is Blocked %x "
2576 "- mbox cmd %x still active\n",
2577 phba->sli.sli_flag, actcmd);
2578 break;
2579 }
2580 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002581}
2582
James Smarte59058c2008-08-24 21:49:00 -04002583/**
James Smart6b5151f2012-01-18 16:24:06 -05002584 * lpfc_sli4_node_prep - Assign RPIs for active nodes.
2585 * @phba: pointer to lpfc hba data structure.
2586 *
2587 * Allocate RPIs for all active remote nodes. This is needed whenever
2588 * an SLI4 adapter is reset and the driver is not unloading. Its purpose
2589 * is to fixup the temporary rpi assignments.
2590 **/
2591void
2592lpfc_sli4_node_prep(struct lpfc_hba *phba)
2593{
2594 struct lpfc_nodelist *ndlp, *next_ndlp;
2595 struct lpfc_vport **vports;
2596 int i;
2597
2598 if (phba->sli_rev != LPFC_SLI_REV4)
2599 return;
2600
2601 vports = lpfc_create_vport_work_array(phba);
2602 if (vports != NULL) {
2603 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2604 if (vports[i]->load_flag & FC_UNLOADING)
2605 continue;
2606
2607 list_for_each_entry_safe(ndlp, next_ndlp,
2608 &vports[i]->fc_nodes,
2609 nlp_listp) {
2610 if (NLP_CHK_NODE_ACT(ndlp))
2611 ndlp->nlp_rpi =
2612 lpfc_sli4_alloc_rpi(phba);
2613 }
2614 }
2615 }
2616 lpfc_destroy_vport_work_array(phba, vports);
2617}
2618
2619/**
James Smart3621a712009-04-06 18:47:14 -04002620 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04002621 * @phba: pointer to lpfc hba data structure.
2622 *
2623 * This routine initializes the HBA and brings a HBA online. During this
2624 * process, the management interface is blocked to prevent user space access
2625 * to the HBA interfering with the driver initialization.
2626 *
2627 * Return codes
2628 * 0 - successful
2629 * 1 - failed
2630 **/
dea31012005-04-17 16:05:31 -05002631int
James Smart2e0fef82007-06-17 19:56:36 -05002632lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002633{
Julia Lawall372bd282008-12-16 16:15:08 +01002634 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04002635 struct lpfc_vport **vports;
2636 int i;
James Smart2e0fef82007-06-17 19:56:36 -05002637
dea31012005-04-17 16:05:31 -05002638 if (!phba)
2639 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01002640 vport = phba->pport;
dea31012005-04-17 16:05:31 -05002641
James Smart2e0fef82007-06-17 19:56:36 -05002642 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05002643 return 0;
2644
James Smarted957682007-06-17 19:56:37 -05002645 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002646 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05002647
James Smart618a5232012-06-12 13:54:36 -04002648 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05002649
James Smart46fa3112007-04-25 09:51:45 -04002650 if (!lpfc_sli_queue_setup(phba)) {
2651 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002652 return 1;
James Smart46fa3112007-04-25 09:51:45 -04002653 }
2654
James Smartda0436e2009-05-22 14:51:39 -04002655 if (phba->sli_rev == LPFC_SLI_REV4) {
2656 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2657 lpfc_unblock_mgmt_io(phba);
2658 return 1;
2659 }
2660 } else {
2661 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2662 lpfc_unblock_mgmt_io(phba);
2663 return 1;
2664 }
James Smart46fa3112007-04-25 09:51:45 -04002665 }
dea31012005-04-17 16:05:31 -05002666
James Smart549e55c2007-08-02 11:09:51 -04002667 vports = lpfc_create_vport_work_array(phba);
2668 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002669 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002670 struct Scsi_Host *shost;
2671 shost = lpfc_shost_from_vport(vports[i]);
2672 spin_lock_irq(shost->host_lock);
2673 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2674 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2675 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart1c6834a2009-07-19 10:01:26 -04002676 if (phba->sli_rev == LPFC_SLI_REV4)
2677 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart549e55c2007-08-02 11:09:51 -04002678 spin_unlock_irq(shost->host_lock);
2679 }
James Smart09372822008-01-11 01:52:54 -05002680 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002681
James Smart46fa3112007-04-25 09:51:45 -04002682 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002683 return 0;
2684}
2685
James Smarte59058c2008-08-24 21:49:00 -04002686/**
James Smart3621a712009-04-06 18:47:14 -04002687 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04002688 * @phba: pointer to lpfc hba data structure.
2689 *
2690 * This routine marks a HBA's management interface as not blocked. Once the
2691 * HBA's management interface is marked as not blocked, all the user space
2692 * access to the HBA, whether they are from sysfs interface or libdfc
2693 * interface will be allowed. The HBA is set to block the management interface
2694 * when the driver prepares the HBA interface for online or offline and then
2695 * set to unblock the management interface afterwards.
2696 **/
James Smart46fa3112007-04-25 09:51:45 -04002697void
James Smart46fa3112007-04-25 09:51:45 -04002698lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2699{
2700 unsigned long iflag;
2701
James Smart2e0fef82007-06-17 19:56:36 -05002702 spin_lock_irqsave(&phba->hbalock, iflag);
2703 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2704 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04002705}
2706
James Smarte59058c2008-08-24 21:49:00 -04002707/**
James Smart3621a712009-04-06 18:47:14 -04002708 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04002709 * @phba: pointer to lpfc hba data structure.
2710 *
2711 * This routine is invoked to prepare a HBA to be brought offline. It performs
2712 * unregistration login to all the nodes on all vports and flushes the mailbox
2713 * queue to make it ready to be brought offline.
2714 **/
James Smart46fa3112007-04-25 09:51:45 -04002715void
James Smart618a5232012-06-12 13:54:36 -04002716lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
James Smart46fa3112007-04-25 09:51:45 -04002717{
James Smart2e0fef82007-06-17 19:56:36 -05002718 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04002719 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04002720 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05002721 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04002722 int i;
dea31012005-04-17 16:05:31 -05002723
James Smart2e0fef82007-06-17 19:56:36 -05002724 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002725 return;
dea31012005-04-17 16:05:31 -05002726
James Smart618a5232012-06-12 13:54:36 -04002727 lpfc_block_mgmt_io(phba, mbx_action);
dea31012005-04-17 16:05:31 -05002728
2729 lpfc_linkdown(phba);
2730
James Smart87af33f2007-10-27 13:37:43 -04002731 /* Issue an unreg_login to all nodes on all vports */
2732 vports = lpfc_create_vport_work_array(phba);
2733 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04002734 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04002735 if (vports[i]->load_flag & FC_UNLOADING)
2736 continue;
James Smart72100cc2010-02-12 14:43:01 -05002737 shost = lpfc_shost_from_vport(vports[i]);
2738 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002739 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002740 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2741 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05002742 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002743
James Smart87af33f2007-10-27 13:37:43 -04002744 shost = lpfc_shost_from_vport(vports[i]);
2745 list_for_each_entry_safe(ndlp, next_ndlp,
2746 &vports[i]->fc_nodes,
2747 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002748 if (!NLP_CHK_NODE_ACT(ndlp))
2749 continue;
James Smart87af33f2007-10-27 13:37:43 -04002750 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2751 continue;
2752 if (ndlp->nlp_type & NLP_FABRIC) {
2753 lpfc_disc_state_machine(vports[i], ndlp,
2754 NULL, NLP_EVT_DEVICE_RECOVERY);
2755 lpfc_disc_state_machine(vports[i], ndlp,
2756 NULL, NLP_EVT_DEVICE_RM);
2757 }
2758 spin_lock_irq(shost->host_lock);
2759 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart401ee0c2012-03-01 22:35:34 -05002760 spin_unlock_irq(shost->host_lock);
James Smart6b5151f2012-01-18 16:24:06 -05002761 /*
2762 * Whenever an SLI4 port goes offline, free the
James Smart401ee0c2012-03-01 22:35:34 -05002763 * RPI. Get a new RPI when the adapter port
2764 * comes back online.
James Smart6b5151f2012-01-18 16:24:06 -05002765 */
2766 if (phba->sli_rev == LPFC_SLI_REV4)
2767 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smart87af33f2007-10-27 13:37:43 -04002768 lpfc_unreg_rpi(vports[i], ndlp);
2769 }
2770 }
2771 }
James Smart09372822008-01-11 01:52:54 -05002772 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002773
James Smart618a5232012-06-12 13:54:36 -04002774 lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
James Smart46fa3112007-04-25 09:51:45 -04002775}
2776
James Smarte59058c2008-08-24 21:49:00 -04002777/**
James Smart3621a712009-04-06 18:47:14 -04002778 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04002779 * @phba: pointer to lpfc hba data structure.
2780 *
2781 * This routine actually brings a HBA offline. It stops all the timers
2782 * associated with the HBA, brings down the SLI layer, and eventually
2783 * marks the HBA as in offline state for the upper layer protocol.
2784 **/
James Smart46fa3112007-04-25 09:51:45 -04002785void
James Smart2e0fef82007-06-17 19:56:36 -05002786lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04002787{
James Smart549e55c2007-08-02 11:09:51 -04002788 struct Scsi_Host *shost;
2789 struct lpfc_vport **vports;
2790 int i;
James Smart46fa3112007-04-25 09:51:45 -04002791
James Smart549e55c2007-08-02 11:09:51 -04002792 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002793 return;
James Smart688a8862006-07-06 15:49:56 -04002794
James Smartda0436e2009-05-22 14:51:39 -04002795 /* stop port and all timers associated with this hba */
2796 lpfc_stop_port(phba);
James Smart51ef4c22007-08-02 11:10:31 -04002797 vports = lpfc_create_vport_work_array(phba);
2798 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002799 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04002800 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05002801 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05002802 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002803 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05002804 /* Bring down the SLI Layer and cleanup. The HBA is offline
2805 now. */
2806 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002807 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04002808 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002809 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04002810 vports = lpfc_create_vport_work_array(phba);
2811 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002812 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002813 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04002814 spin_lock_irq(shost->host_lock);
2815 vports[i]->work_port_events = 0;
2816 vports[i]->fc_flag |= FC_OFFLINE_MODE;
2817 spin_unlock_irq(shost->host_lock);
2818 }
James Smart09372822008-01-11 01:52:54 -05002819 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002820}
2821
James Smarte59058c2008-08-24 21:49:00 -04002822/**
James Smart3621a712009-04-06 18:47:14 -04002823 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04002824 * @phba: pointer to lpfc hba data structure.
2825 *
2826 * This routine is to free all the SCSI buffers and IOCBs from the driver
2827 * list back to kernel. It is called from lpfc_pci_remove_one to free
2828 * the internal resources before the device is removed from the system.
James Smarte59058c2008-08-24 21:49:00 -04002829 **/
James Smart8a9d2e82012-05-09 21:16:12 -04002830static void
James Smart2e0fef82007-06-17 19:56:36 -05002831lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002832{
2833 struct lpfc_scsi_buf *sb, *sb_next;
2834 struct lpfc_iocbq *io, *io_next;
2835
James Smart2e0fef82007-06-17 19:56:36 -05002836 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002837 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smart1c6f4ef52009-11-18 15:40:49 -05002838 spin_lock(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05002839 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
2840 list_del(&sb->list);
2841 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05002842 sb->dma_handle);
dea31012005-04-17 16:05:31 -05002843 kfree(sb);
2844 phba->total_scsi_bufs--;
2845 }
James Smart1c6f4ef52009-11-18 15:40:49 -05002846 spin_unlock(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05002847
2848 /* Release all the lpfc_iocbq entries maintained by this host. */
2849 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2850 list_del(&io->list);
2851 kfree(io);
2852 phba->total_iocbq_bufs--;
2853 }
James Smart6d368e52011-05-24 11:44:12 -04002854
James Smart2e0fef82007-06-17 19:56:36 -05002855 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04002856}
2857
2858/**
2859 * lpfc_sli4_xri_sgl_update - update xri-sgl sizing and mapping
2860 * @phba: pointer to lpfc hba data structure.
2861 *
2862 * This routine first calculates the sizes of the current els and allocated
2863 * scsi sgl lists, and then goes through all sgls to updates the physical
2864 * XRIs assigned due to port function reset. During port initialization, the
2865 * current els and allocated scsi sgl lists are 0s.
2866 *
2867 * Return codes
2868 * 0 - successful (for now, it always returns 0)
2869 **/
2870int
2871lpfc_sli4_xri_sgl_update(struct lpfc_hba *phba)
2872{
2873 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
2874 struct lpfc_scsi_buf *psb = NULL, *psb_next = NULL;
2875 uint16_t i, lxri, xri_cnt, els_xri_cnt, scsi_xri_cnt;
2876 LIST_HEAD(els_sgl_list);
2877 LIST_HEAD(scsi_sgl_list);
2878 int rc;
2879
2880 /*
2881 * update on pci function's els xri-sgl list
2882 */
2883 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
2884 if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
2885 /* els xri-sgl expanded */
2886 xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
2887 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2888 "3157 ELS xri-sgl count increased from "
2889 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
2890 els_xri_cnt);
2891 /* allocate the additional els sgls */
2892 for (i = 0; i < xri_cnt; i++) {
2893 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
2894 GFP_KERNEL);
2895 if (sglq_entry == NULL) {
2896 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2897 "2562 Failure to allocate an "
2898 "ELS sgl entry:%d\n", i);
2899 rc = -ENOMEM;
2900 goto out_free_mem;
2901 }
2902 sglq_entry->buff_type = GEN_BUFF_TYPE;
2903 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
2904 &sglq_entry->phys);
2905 if (sglq_entry->virt == NULL) {
2906 kfree(sglq_entry);
2907 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2908 "2563 Failure to allocate an "
2909 "ELS mbuf:%d\n", i);
2910 rc = -ENOMEM;
2911 goto out_free_mem;
2912 }
2913 sglq_entry->sgl = sglq_entry->virt;
2914 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
2915 sglq_entry->state = SGL_FREED;
2916 list_add_tail(&sglq_entry->list, &els_sgl_list);
2917 }
2918 spin_lock(&phba->hbalock);
2919 list_splice_init(&els_sgl_list, &phba->sli4_hba.lpfc_sgl_list);
2920 spin_unlock(&phba->hbalock);
2921 } else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
2922 /* els xri-sgl shrinked */
2923 xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
2924 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2925 "3158 ELS xri-sgl count decreased from "
2926 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
2927 els_xri_cnt);
2928 spin_lock_irq(&phba->hbalock);
2929 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &els_sgl_list);
2930 spin_unlock_irq(&phba->hbalock);
2931 /* release extra els sgls from list */
2932 for (i = 0; i < xri_cnt; i++) {
2933 list_remove_head(&els_sgl_list,
2934 sglq_entry, struct lpfc_sglq, list);
2935 if (sglq_entry) {
2936 lpfc_mbuf_free(phba, sglq_entry->virt,
2937 sglq_entry->phys);
2938 kfree(sglq_entry);
2939 }
2940 }
2941 spin_lock_irq(&phba->hbalock);
2942 list_splice_init(&els_sgl_list, &phba->sli4_hba.lpfc_sgl_list);
2943 spin_unlock_irq(&phba->hbalock);
2944 } else
2945 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2946 "3163 ELS xri-sgl count unchanged: %d\n",
2947 els_xri_cnt);
2948 phba->sli4_hba.els_xri_cnt = els_xri_cnt;
2949
2950 /* update xris to els sgls on the list */
2951 sglq_entry = NULL;
2952 sglq_entry_next = NULL;
2953 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
2954 &phba->sli4_hba.lpfc_sgl_list, list) {
2955 lxri = lpfc_sli4_next_xritag(phba);
2956 if (lxri == NO_XRI) {
2957 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2958 "2400 Failed to allocate xri for "
2959 "ELS sgl\n");
2960 rc = -ENOMEM;
2961 goto out_free_mem;
2962 }
2963 sglq_entry->sli4_lxritag = lxri;
2964 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
2965 }
2966
2967 /*
2968 * update on pci function's allocated scsi xri-sgl list
2969 */
2970 phba->total_scsi_bufs = 0;
2971
2972 /* maximum number of xris available for scsi buffers */
2973 phba->sli4_hba.scsi_xri_max = phba->sli4_hba.max_cfg_param.max_xri -
2974 els_xri_cnt;
2975
2976 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2977 "2401 Current allocated SCSI xri-sgl count:%d, "
2978 "maximum SCSI xri count:%d\n",
2979 phba->sli4_hba.scsi_xri_cnt,
2980 phba->sli4_hba.scsi_xri_max);
2981
2982 spin_lock_irq(&phba->scsi_buf_list_lock);
2983 list_splice_init(&phba->lpfc_scsi_buf_list, &scsi_sgl_list);
2984 spin_unlock_irq(&phba->scsi_buf_list_lock);
2985
2986 if (phba->sli4_hba.scsi_xri_cnt > phba->sli4_hba.scsi_xri_max) {
2987 /* max scsi xri shrinked below the allocated scsi buffers */
2988 scsi_xri_cnt = phba->sli4_hba.scsi_xri_cnt -
2989 phba->sli4_hba.scsi_xri_max;
2990 /* release the extra allocated scsi buffers */
2991 for (i = 0; i < scsi_xri_cnt; i++) {
2992 list_remove_head(&scsi_sgl_list, psb,
2993 struct lpfc_scsi_buf, list);
2994 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, psb->data,
2995 psb->dma_handle);
2996 kfree(psb);
2997 }
2998 spin_lock_irq(&phba->scsi_buf_list_lock);
2999 phba->sli4_hba.scsi_xri_cnt -= scsi_xri_cnt;
3000 spin_unlock_irq(&phba->scsi_buf_list_lock);
3001 }
3002
3003 /* update xris associated to remaining allocated scsi buffers */
3004 psb = NULL;
3005 psb_next = NULL;
3006 list_for_each_entry_safe(psb, psb_next, &scsi_sgl_list, list) {
3007 lxri = lpfc_sli4_next_xritag(phba);
3008 if (lxri == NO_XRI) {
3009 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3010 "2560 Failed to allocate xri for "
3011 "scsi buffer\n");
3012 rc = -ENOMEM;
3013 goto out_free_mem;
3014 }
3015 psb->cur_iocbq.sli4_lxritag = lxri;
3016 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3017 }
3018 spin_lock(&phba->scsi_buf_list_lock);
3019 list_splice_init(&scsi_sgl_list, &phba->lpfc_scsi_buf_list);
3020 spin_unlock(&phba->scsi_buf_list_lock);
3021
dea31012005-04-17 16:05:31 -05003022 return 0;
James Smart8a9d2e82012-05-09 21:16:12 -04003023
3024out_free_mem:
3025 lpfc_free_els_sgl_list(phba);
3026 lpfc_scsi_free(phba);
3027 return rc;
dea31012005-04-17 16:05:31 -05003028}
3029
James Smarte59058c2008-08-24 21:49:00 -04003030/**
James Smart3621a712009-04-06 18:47:14 -04003031 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04003032 * @phba: pointer to lpfc hba data structure.
3033 * @instance: a unique integer ID to this FC port.
3034 * @dev: pointer to the device data structure.
3035 *
3036 * This routine creates a FC port for the upper layer protocol. The FC port
3037 * can be created on top of either a physical port or a virtual port provided
3038 * by the HBA. This routine also allocates a SCSI host data structure (shost)
3039 * and associates the FC port created before adding the shost into the SCSI
3040 * layer.
3041 *
3042 * Return codes
3043 * @vport - pointer to the virtual N_Port data structure.
3044 * NULL - port create failed.
3045 **/
James Smart2e0fef82007-06-17 19:56:36 -05003046struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04003047lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04003048{
James Smart2e0fef82007-06-17 19:56:36 -05003049 struct lpfc_vport *vport;
3050 struct Scsi_Host *shost;
3051 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04003052
James Smart3de2a652007-08-02 11:09:59 -04003053 if (dev != &phba->pcidev->dev)
3054 shost = scsi_host_alloc(&lpfc_vport_template,
3055 sizeof(struct lpfc_vport));
3056 else
3057 shost = scsi_host_alloc(&lpfc_template,
3058 sizeof(struct lpfc_vport));
James Smart2e0fef82007-06-17 19:56:36 -05003059 if (!shost)
3060 goto out;
James Smart47a86172007-04-25 09:53:22 -04003061
James Smart2e0fef82007-06-17 19:56:36 -05003062 vport = (struct lpfc_vport *) shost->hostdata;
3063 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05003064 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05003065 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05003066 vport->fc_rscn_flush = 0;
James Smart47a86172007-04-25 09:53:22 -04003067
James Smart3de2a652007-08-02 11:09:59 -04003068 lpfc_get_vport_cfgparam(vport);
James Smart2e0fef82007-06-17 19:56:36 -05003069 shost->unique_id = instance;
3070 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04003071 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05003072 shost->this_id = -1;
3073 shost->max_cmd_len = 16;
James Smartda0436e2009-05-22 14:51:39 -04003074 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart28baac72010-02-12 14:42:03 -05003075 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04003076 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smartda0436e2009-05-22 14:51:39 -04003077 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
3078 }
James Smart81301a92008-12-04 22:39:46 -05003079
James Smart47a86172007-04-25 09:53:22 -04003080 /*
James Smart2e0fef82007-06-17 19:56:36 -05003081 * Set initial can_queue value since 0 is no longer supported and
3082 * scsi_add_host will fail. This will be adjusted later based on the
3083 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04003084 */
James Smart2e0fef82007-06-17 19:56:36 -05003085 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04003086 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05003087 shost->transportt = lpfc_vport_transport_template;
3088 vport->port_type = LPFC_NPIV_PORT;
3089 } else {
3090 shost->transportt = lpfc_transport_template;
3091 vport->port_type = LPFC_PHYSICAL_PORT;
3092 }
James Smart47a86172007-04-25 09:53:22 -04003093
James Smart2e0fef82007-06-17 19:56:36 -05003094 /* Initialize all internally managed lists. */
3095 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04003096 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05003097 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04003098
James Smart2e0fef82007-06-17 19:56:36 -05003099 init_timer(&vport->fc_disctmo);
3100 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05003101 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04003102
James Smart2e0fef82007-06-17 19:56:36 -05003103 init_timer(&vport->fc_fdmitmo);
3104 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
James Smart92d7f7b2007-06-17 19:56:38 -05003105 vport->fc_fdmitmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04003106
James Smart2e0fef82007-06-17 19:56:36 -05003107 init_timer(&vport->els_tmofunc);
3108 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05003109 vport->els_tmofunc.data = (unsigned long)vport;
James Smart92494142011-02-16 12:39:44 -05003110
3111 init_timer(&vport->delayed_disc_tmo);
3112 vport->delayed_disc_tmo.function = lpfc_delayed_disc_tmo;
3113 vport->delayed_disc_tmo.data = (unsigned long)vport;
3114
James Bottomleyd139b9b2009-11-05 13:33:12 -06003115 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05003116 if (error)
3117 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04003118
James Smart549e55c2007-08-02 11:09:51 -04003119 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003120 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04003121 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003122 return vport;
James Smart47a86172007-04-25 09:53:22 -04003123
James Smart2e0fef82007-06-17 19:56:36 -05003124out_put_shost:
3125 scsi_host_put(shost);
3126out:
3127 return NULL;
James Smart47a86172007-04-25 09:53:22 -04003128}
3129
James Smarte59058c2008-08-24 21:49:00 -04003130/**
James Smart3621a712009-04-06 18:47:14 -04003131 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04003132 * @vport: pointer to an lpfc virtual N_Port data structure.
3133 *
3134 * This routine destroys a FC port from the upper layer protocol. All the
3135 * resources associated with the port are released.
3136 **/
James Smart2e0fef82007-06-17 19:56:36 -05003137void
3138destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04003139{
James Smart92d7f7b2007-06-17 19:56:38 -05003140 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3141 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003142
James Smart858c9f62007-06-17 19:56:39 -05003143 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05003144 fc_remove_host(shost);
3145 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04003146
James Smart92d7f7b2007-06-17 19:56:38 -05003147 spin_lock_irq(&phba->hbalock);
3148 list_del_init(&vport->listentry);
3149 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04003150
James Smart92d7f7b2007-06-17 19:56:38 -05003151 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04003152 return;
James Smart47a86172007-04-25 09:53:22 -04003153}
3154
James Smarte59058c2008-08-24 21:49:00 -04003155/**
James Smart3621a712009-04-06 18:47:14 -04003156 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04003157 *
3158 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
3159 * uses the kernel idr facility to perform the task.
3160 *
3161 * Return codes:
3162 * instance - a unique integer ID allocated as the new instance.
3163 * -1 - lpfc get instance failed.
3164 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003165int
3166lpfc_get_instance(void)
3167{
Tejun Heoab516032013-02-27 17:04:44 -08003168 int ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003169
Tejun Heoab516032013-02-27 17:04:44 -08003170 ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
3171 return ret < 0 ? -1 : ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003172}
3173
James Smarte59058c2008-08-24 21:49:00 -04003174/**
James Smart3621a712009-04-06 18:47:14 -04003175 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04003176 * @shost: pointer to SCSI host data structure.
3177 * @time: elapsed time of the scan in jiffies.
3178 *
3179 * This routine is called by the SCSI layer with a SCSI host to determine
3180 * whether the scan host is finished.
3181 *
3182 * Note: there is no scan_start function as adapter initialization will have
3183 * asynchronously kicked off the link initialization.
3184 *
3185 * Return codes
3186 * 0 - SCSI host scan is not over yet.
3187 * 1 - SCSI host scan is over.
3188 **/
James Smart47a86172007-04-25 09:53:22 -04003189int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
3190{
James Smart2e0fef82007-06-17 19:56:36 -05003191 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3192 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05003193 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04003194
James Smart858c9f62007-06-17 19:56:39 -05003195 spin_lock_irq(shost->host_lock);
3196
James Smart51ef4c22007-08-02 11:10:31 -04003197 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05003198 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003199 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05003200 }
James Smart2e0fef82007-06-17 19:56:36 -05003201 if (time >= 30 * HZ) {
3202 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003203 "0461 Scanning longer than 30 "
3204 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003205 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003206 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003207 }
3208 if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
3209 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003210 "0465 Link down longer than 15 "
3211 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003212 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05003213 goto finished;
James Smart47a86172007-04-25 09:53:22 -04003214 }
3215
James Smart2e0fef82007-06-17 19:56:36 -05003216 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05003217 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003218 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05003219 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003220 if (vport->fc_map_cnt == 0 && time < 2 * HZ)
James Smart858c9f62007-06-17 19:56:39 -05003221 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003222 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05003223 goto finished;
3224
3225 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003226
3227finished:
James Smart858c9f62007-06-17 19:56:39 -05003228 spin_unlock_irq(shost->host_lock);
3229 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05003230}
3231
James Smarte59058c2008-08-24 21:49:00 -04003232/**
James Smart3621a712009-04-06 18:47:14 -04003233 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04003234 * @shost: pointer to SCSI host data structure.
3235 *
3236 * This routine initializes a given SCSI host attributes on a FC port. The
3237 * SCSI host can be either on top of a physical port or a virtual port.
3238 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003239void lpfc_host_attrib_init(struct Scsi_Host *shost)
3240{
3241 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3242 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003243 /*
James Smart2e0fef82007-06-17 19:56:36 -05003244 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04003245 */
3246
James Smart2e0fef82007-06-17 19:56:36 -05003247 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
3248 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04003249 fc_host_supported_classes(shost) = FC_COS_CLASS3;
3250
3251 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003252 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003253 fc_host_supported_fc4s(shost)[2] = 1;
3254 fc_host_supported_fc4s(shost)[7] = 1;
3255
James Smart92d7f7b2007-06-17 19:56:38 -05003256 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
3257 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04003258
3259 fc_host_supported_speeds(shost) = 0;
James Smart88a2cfb2011-07-22 18:36:33 -04003260 if (phba->lmt & LMT_16Gb)
3261 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
James Smart47a86172007-04-25 09:53:22 -04003262 if (phba->lmt & LMT_10Gb)
3263 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04003264 if (phba->lmt & LMT_8Gb)
3265 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04003266 if (phba->lmt & LMT_4Gb)
3267 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
3268 if (phba->lmt & LMT_2Gb)
3269 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
3270 if (phba->lmt & LMT_1Gb)
3271 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
3272
3273 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05003274 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
3275 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04003276
Mike Christie0af5d702010-09-15 16:52:31 -05003277 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
3278
James Smart47a86172007-04-25 09:53:22 -04003279 /* This value is also unchanging */
3280 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003281 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003282 fc_host_active_fc4s(shost)[2] = 1;
3283 fc_host_active_fc4s(shost)[7] = 1;
3284
James Smart92d7f7b2007-06-17 19:56:38 -05003285 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04003286 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04003287 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04003288 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04003289}
dea31012005-04-17 16:05:31 -05003290
James Smarte59058c2008-08-24 21:49:00 -04003291/**
James Smartda0436e2009-05-22 14:51:39 -04003292 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04003293 * @phba: pointer to lpfc hba data structure.
3294 *
James Smartda0436e2009-05-22 14:51:39 -04003295 * This routine is invoked to stop an SLI3 device port, it stops the device
3296 * from generating interrupts and stops the device driver's timers for the
3297 * device.
James Smarte59058c2008-08-24 21:49:00 -04003298 **/
James Smartdb2378e2008-02-08 18:49:51 -05003299static void
James Smartda0436e2009-05-22 14:51:39 -04003300lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05003301{
James Smartda0436e2009-05-22 14:51:39 -04003302 /* Clear all interrupt enable conditions */
3303 writel(0, phba->HCregaddr);
3304 readl(phba->HCregaddr); /* flush */
3305 /* Clear all pending interrupts */
3306 writel(0xffffffff, phba->HAregaddr);
3307 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04003308
James Smartda0436e2009-05-22 14:51:39 -04003309 /* Reset some HBA SLI setup states */
3310 lpfc_stop_hba_timers(phba);
3311 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05003312}
3313
James Smarte59058c2008-08-24 21:49:00 -04003314/**
James Smartda0436e2009-05-22 14:51:39 -04003315 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05003316 * @phba: pointer to lpfc hba data structure.
3317 *
James Smartda0436e2009-05-22 14:51:39 -04003318 * This routine is invoked to stop an SLI4 device port, it stops the device
3319 * from generating interrupts and stops the device driver's timers for the
3320 * device.
3321 **/
3322static void
3323lpfc_stop_port_s4(struct lpfc_hba *phba)
3324{
3325 /* Reset some HBA SLI4 setup states */
3326 lpfc_stop_hba_timers(phba);
3327 phba->pport->work_port_events = 0;
3328 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04003329}
3330
3331/**
3332 * lpfc_stop_port - Wrapper function for stopping hba port
3333 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05003334 *
James Smartda0436e2009-05-22 14:51:39 -04003335 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
3336 * the API jump table function pointer from the lpfc_hba struct.
3337 **/
3338void
3339lpfc_stop_port(struct lpfc_hba *phba)
3340{
3341 phba->lpfc_stop_port(phba);
3342}
3343
3344/**
James Smartecfd03c2010-02-12 14:41:27 -05003345 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
3346 * @phba: Pointer to hba for which this call is being executed.
3347 *
3348 * This routine starts the timer waiting for the FCF rediscovery to complete.
3349 **/
3350void
3351lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
3352{
3353 unsigned long fcf_redisc_wait_tmo =
3354 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
3355 /* Start fcf rediscovery wait period timer */
3356 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
3357 spin_lock_irq(&phba->hbalock);
3358 /* Allow action to new fcf asynchronous event */
3359 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
3360 /* Mark the FCF rediscovery pending state */
3361 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
3362 spin_unlock_irq(&phba->hbalock);
3363}
3364
3365/**
3366 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
3367 * @ptr: Map to lpfc_hba data structure pointer.
3368 *
3369 * This routine is invoked when waiting for FCF table rediscover has been
3370 * timed out. If new FCF record(s) has (have) been discovered during the
3371 * wait period, a new FCF event shall be added to the FCOE async event
3372 * list, and then worker thread shall be waked up for processing from the
3373 * worker thread context.
3374 **/
3375void
3376lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
3377{
3378 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
3379
3380 /* Don't send FCF rediscovery event if timer cancelled */
3381 spin_lock_irq(&phba->hbalock);
3382 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
3383 spin_unlock_irq(&phba->hbalock);
3384 return;
3385 }
3386 /* Clear FCF rediscovery timer pending flag */
3387 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
3388 /* FCF rediscovery event to worker thread */
3389 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
3390 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003391 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04003392 "2776 FCF rediscover quiescent timer expired\n");
James Smartecfd03c2010-02-12 14:41:27 -05003393 /* wake up worker thread */
3394 lpfc_worker_wake_up(phba);
3395}
3396
3397/**
James Smartda0436e2009-05-22 14:51:39 -04003398 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
3399 * @phba: pointer to lpfc hba data structure.
3400 * @acqe_link: pointer to the async link completion queue entry.
3401 *
3402 * This routine is to parse the SLI4 link-attention link fault code and
3403 * translate it into the base driver's read link attention mailbox command
3404 * status.
3405 *
3406 * Return: Link-attention status in terms of base driver's coding.
3407 **/
3408static uint16_t
3409lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
3410 struct lpfc_acqe_link *acqe_link)
3411{
3412 uint16_t latt_fault;
3413
3414 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
3415 case LPFC_ASYNC_LINK_FAULT_NONE:
3416 case LPFC_ASYNC_LINK_FAULT_LOCAL:
3417 case LPFC_ASYNC_LINK_FAULT_REMOTE:
3418 latt_fault = 0;
3419 break;
3420 default:
3421 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3422 "0398 Invalid link fault code: x%x\n",
3423 bf_get(lpfc_acqe_link_fault, acqe_link));
3424 latt_fault = MBXERR_ERROR;
3425 break;
3426 }
3427 return latt_fault;
3428}
3429
3430/**
3431 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
3432 * @phba: pointer to lpfc hba data structure.
3433 * @acqe_link: pointer to the async link completion queue entry.
3434 *
3435 * This routine is to parse the SLI4 link attention type and translate it
3436 * into the base driver's link attention type coding.
3437 *
3438 * Return: Link attention type in terms of base driver's coding.
3439 **/
3440static uint8_t
3441lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
3442 struct lpfc_acqe_link *acqe_link)
3443{
3444 uint8_t att_type;
3445
3446 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
3447 case LPFC_ASYNC_LINK_STATUS_DOWN:
3448 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
James Smart76a95d72010-11-20 23:11:48 -05003449 att_type = LPFC_ATT_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04003450 break;
3451 case LPFC_ASYNC_LINK_STATUS_UP:
3452 /* Ignore physical link up events - wait for logical link up */
James Smart76a95d72010-11-20 23:11:48 -05003453 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003454 break;
3455 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
James Smart76a95d72010-11-20 23:11:48 -05003456 att_type = LPFC_ATT_LINK_UP;
James Smartda0436e2009-05-22 14:51:39 -04003457 break;
3458 default:
3459 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3460 "0399 Invalid link attention type: x%x\n",
3461 bf_get(lpfc_acqe_link_status, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003462 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003463 break;
3464 }
3465 return att_type;
3466}
3467
3468/**
3469 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
3470 * @phba: pointer to lpfc hba data structure.
3471 * @acqe_link: pointer to the async link completion queue entry.
3472 *
3473 * This routine is to parse the SLI4 link-attention link speed and translate
3474 * it into the base driver's link-attention link speed coding.
3475 *
3476 * Return: Link-attention link speed in terms of base driver's coding.
3477 **/
3478static uint8_t
3479lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
3480 struct lpfc_acqe_link *acqe_link)
3481{
3482 uint8_t link_speed;
3483
3484 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
3485 case LPFC_ASYNC_LINK_SPEED_ZERO:
James Smartda0436e2009-05-22 14:51:39 -04003486 case LPFC_ASYNC_LINK_SPEED_10MBPS:
James Smartda0436e2009-05-22 14:51:39 -04003487 case LPFC_ASYNC_LINK_SPEED_100MBPS:
James Smart76a95d72010-11-20 23:11:48 -05003488 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003489 break;
3490 case LPFC_ASYNC_LINK_SPEED_1GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003491 link_speed = LPFC_LINK_SPEED_1GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003492 break;
3493 case LPFC_ASYNC_LINK_SPEED_10GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003494 link_speed = LPFC_LINK_SPEED_10GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003495 break;
3496 default:
3497 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3498 "0483 Invalid link-attention link speed: x%x\n",
3499 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003500 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003501 break;
3502 }
3503 return link_speed;
3504}
3505
3506/**
James Smart8b68cd52012-09-29 11:32:37 -04003507 * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
3508 * @phba: pointer to lpfc hba data structure.
3509 *
3510 * This routine is to get an SLI3 FC port's link speed in Mbps.
3511 *
3512 * Return: link speed in terms of Mbps.
3513 **/
3514uint32_t
3515lpfc_sli_port_speed_get(struct lpfc_hba *phba)
3516{
3517 uint32_t link_speed;
3518
3519 if (!lpfc_is_link_up(phba))
3520 return 0;
3521
3522 switch (phba->fc_linkspeed) {
3523 case LPFC_LINK_SPEED_1GHZ:
3524 link_speed = 1000;
3525 break;
3526 case LPFC_LINK_SPEED_2GHZ:
3527 link_speed = 2000;
3528 break;
3529 case LPFC_LINK_SPEED_4GHZ:
3530 link_speed = 4000;
3531 break;
3532 case LPFC_LINK_SPEED_8GHZ:
3533 link_speed = 8000;
3534 break;
3535 case LPFC_LINK_SPEED_10GHZ:
3536 link_speed = 10000;
3537 break;
3538 case LPFC_LINK_SPEED_16GHZ:
3539 link_speed = 16000;
3540 break;
3541 default:
3542 link_speed = 0;
3543 }
3544 return link_speed;
3545}
3546
3547/**
3548 * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
3549 * @phba: pointer to lpfc hba data structure.
3550 * @evt_code: asynchronous event code.
3551 * @speed_code: asynchronous event link speed code.
3552 *
3553 * This routine is to parse the giving SLI4 async event link speed code into
3554 * value of Mbps for the link speed.
3555 *
3556 * Return: link speed in terms of Mbps.
3557 **/
3558static uint32_t
3559lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
3560 uint8_t speed_code)
3561{
3562 uint32_t port_speed;
3563
3564 switch (evt_code) {
3565 case LPFC_TRAILER_CODE_LINK:
3566 switch (speed_code) {
3567 case LPFC_EVT_CODE_LINK_NO_LINK:
3568 port_speed = 0;
3569 break;
3570 case LPFC_EVT_CODE_LINK_10_MBIT:
3571 port_speed = 10;
3572 break;
3573 case LPFC_EVT_CODE_LINK_100_MBIT:
3574 port_speed = 100;
3575 break;
3576 case LPFC_EVT_CODE_LINK_1_GBIT:
3577 port_speed = 1000;
3578 break;
3579 case LPFC_EVT_CODE_LINK_10_GBIT:
3580 port_speed = 10000;
3581 break;
3582 default:
3583 port_speed = 0;
3584 }
3585 break;
3586 case LPFC_TRAILER_CODE_FC:
3587 switch (speed_code) {
3588 case LPFC_EVT_CODE_FC_NO_LINK:
3589 port_speed = 0;
3590 break;
3591 case LPFC_EVT_CODE_FC_1_GBAUD:
3592 port_speed = 1000;
3593 break;
3594 case LPFC_EVT_CODE_FC_2_GBAUD:
3595 port_speed = 2000;
3596 break;
3597 case LPFC_EVT_CODE_FC_4_GBAUD:
3598 port_speed = 4000;
3599 break;
3600 case LPFC_EVT_CODE_FC_8_GBAUD:
3601 port_speed = 8000;
3602 break;
3603 case LPFC_EVT_CODE_FC_10_GBAUD:
3604 port_speed = 10000;
3605 break;
3606 case LPFC_EVT_CODE_FC_16_GBAUD:
3607 port_speed = 16000;
3608 break;
3609 default:
3610 port_speed = 0;
3611 }
3612 break;
3613 default:
3614 port_speed = 0;
3615 }
3616 return port_speed;
3617}
3618
3619/**
James Smart70f3c072010-12-15 17:57:33 -05003620 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
James Smartda0436e2009-05-22 14:51:39 -04003621 * @phba: pointer to lpfc hba data structure.
3622 * @acqe_link: pointer to the async link completion queue entry.
3623 *
James Smart70f3c072010-12-15 17:57:33 -05003624 * This routine is to handle the SLI4 asynchronous FCoE link event.
James Smartda0436e2009-05-22 14:51:39 -04003625 **/
3626static void
3627lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3628 struct lpfc_acqe_link *acqe_link)
3629{
3630 struct lpfc_dmabuf *mp;
3631 LPFC_MBOXQ_t *pmb;
3632 MAILBOX_t *mb;
James Smart76a95d72010-11-20 23:11:48 -05003633 struct lpfc_mbx_read_top *la;
James Smartda0436e2009-05-22 14:51:39 -04003634 uint8_t att_type;
James Smart76a95d72010-11-20 23:11:48 -05003635 int rc;
James Smartda0436e2009-05-22 14:51:39 -04003636
3637 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05003638 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
James Smartda0436e2009-05-22 14:51:39 -04003639 return;
James Smart32b97932009-07-19 10:01:21 -04003640 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003641 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3642 if (!pmb) {
3643 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3644 "0395 The mboxq allocation failed\n");
3645 return;
3646 }
3647 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3648 if (!mp) {
3649 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3650 "0396 The lpfc_dmabuf allocation failed\n");
3651 goto out_free_pmb;
3652 }
3653 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3654 if (!mp->virt) {
3655 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3656 "0397 The mbuf allocation failed\n");
3657 goto out_free_dmabuf;
3658 }
3659
3660 /* Cleanup any outstanding ELS commands */
3661 lpfc_els_flush_all_cmd(phba);
3662
3663 /* Block ELS IOCBs until we have done process link event */
3664 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3665
3666 /* Update link event statistics */
3667 phba->sli.slistat.link_event++;
3668
James Smart76a95d72010-11-20 23:11:48 -05003669 /* Create lpfc_handle_latt mailbox command from link ACQE */
3670 lpfc_read_topology(phba, pmb, mp);
3671 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smartda0436e2009-05-22 14:51:39 -04003672 pmb->vport = phba->pport;
3673
James Smartda0436e2009-05-22 14:51:39 -04003674 /* Keep the link status for extra SLI4 state machine reference */
3675 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04003676 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
3677 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smartda0436e2009-05-22 14:51:39 -04003678 phba->sli4_hba.link_state.duplex =
3679 bf_get(lpfc_acqe_link_duplex, acqe_link);
3680 phba->sli4_hba.link_state.status =
3681 bf_get(lpfc_acqe_link_status, acqe_link);
James Smart70f3c072010-12-15 17:57:33 -05003682 phba->sli4_hba.link_state.type =
3683 bf_get(lpfc_acqe_link_type, acqe_link);
3684 phba->sli4_hba.link_state.number =
3685 bf_get(lpfc_acqe_link_number, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04003686 phba->sli4_hba.link_state.fault =
3687 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05003688 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04003689 bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
3690
James Smart70f3c072010-12-15 17:57:33 -05003691 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartc31098c2011-04-16 11:03:33 -04003692 "2900 Async FC/FCoE Link event - Speed:%dGBit "
3693 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
3694 "Logical speed:%dMbps Fault:%d\n",
James Smart70f3c072010-12-15 17:57:33 -05003695 phba->sli4_hba.link_state.speed,
3696 phba->sli4_hba.link_state.topology,
3697 phba->sli4_hba.link_state.status,
3698 phba->sli4_hba.link_state.type,
3699 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04003700 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05003701 phba->sli4_hba.link_state.fault);
James Smart76a95d72010-11-20 23:11:48 -05003702 /*
3703 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
3704 * topology info. Note: Optional for non FC-AL ports.
3705 */
3706 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3707 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3708 if (rc == MBX_NOT_FINISHED)
3709 goto out_free_dmabuf;
3710 return;
3711 }
3712 /*
3713 * For FCoE Mode: fill in all the topology information we need and call
3714 * the READ_TOPOLOGY completion routine to continue without actually
3715 * sending the READ_TOPOLOGY mailbox command to the port.
3716 */
3717 /* Parse and translate status field */
3718 mb = &pmb->u.mb;
3719 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
3720
3721 /* Parse and translate link attention fields */
3722 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
3723 la->eventTag = acqe_link->event_tag;
3724 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
3725 bf_set(lpfc_mbx_read_top_link_spd, la,
3726 lpfc_sli4_parse_latt_link_speed(phba, acqe_link));
3727
3728 /* Fake the the following irrelvant fields */
3729 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
3730 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
3731 bf_set(lpfc_mbx_read_top_il, la, 0);
3732 bf_set(lpfc_mbx_read_top_pb, la, 0);
3733 bf_set(lpfc_mbx_read_top_fa, la, 0);
3734 bf_set(lpfc_mbx_read_top_mm, la, 0);
James Smartda0436e2009-05-22 14:51:39 -04003735
3736 /* Invoke the lpfc_handle_latt mailbox command callback function */
James Smart76a95d72010-11-20 23:11:48 -05003737 lpfc_mbx_cmpl_read_topology(phba, pmb);
James Smartda0436e2009-05-22 14:51:39 -04003738
3739 return;
3740
3741out_free_dmabuf:
3742 kfree(mp);
3743out_free_pmb:
3744 mempool_free(pmb, phba->mbox_mem_pool);
3745}
3746
3747/**
James Smart70f3c072010-12-15 17:57:33 -05003748 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
3749 * @phba: pointer to lpfc hba data structure.
3750 * @acqe_fc: pointer to the async fc completion queue entry.
3751 *
3752 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
3753 * that the event was received and then issue a read_topology mailbox command so
3754 * that the rest of the driver will treat it the same as SLI3.
3755 **/
3756static void
3757lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
3758{
3759 struct lpfc_dmabuf *mp;
3760 LPFC_MBOXQ_t *pmb;
3761 int rc;
3762
3763 if (bf_get(lpfc_trailer_type, acqe_fc) !=
3764 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
3765 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3766 "2895 Non FC link Event detected.(%d)\n",
3767 bf_get(lpfc_trailer_type, acqe_fc));
3768 return;
3769 }
3770 /* Keep the link status for extra SLI4 state machine reference */
3771 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04003772 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
3773 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
James Smart70f3c072010-12-15 17:57:33 -05003774 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
3775 phba->sli4_hba.link_state.topology =
3776 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
3777 phba->sli4_hba.link_state.status =
3778 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
3779 phba->sli4_hba.link_state.type =
3780 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
3781 phba->sli4_hba.link_state.number =
3782 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
3783 phba->sli4_hba.link_state.fault =
3784 bf_get(lpfc_acqe_link_fault, acqe_fc);
3785 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04003786 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
James Smart70f3c072010-12-15 17:57:33 -05003787 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3788 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
3789 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
3790 "%dMbps Fault:%d\n",
3791 phba->sli4_hba.link_state.speed,
3792 phba->sli4_hba.link_state.topology,
3793 phba->sli4_hba.link_state.status,
3794 phba->sli4_hba.link_state.type,
3795 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04003796 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05003797 phba->sli4_hba.link_state.fault);
3798 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3799 if (!pmb) {
3800 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3801 "2897 The mboxq allocation failed\n");
3802 return;
3803 }
3804 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3805 if (!mp) {
3806 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3807 "2898 The lpfc_dmabuf allocation failed\n");
3808 goto out_free_pmb;
3809 }
3810 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3811 if (!mp->virt) {
3812 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3813 "2899 The mbuf allocation failed\n");
3814 goto out_free_dmabuf;
3815 }
3816
3817 /* Cleanup any outstanding ELS commands */
3818 lpfc_els_flush_all_cmd(phba);
3819
3820 /* Block ELS IOCBs until we have done process link event */
3821 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3822
3823 /* Update link event statistics */
3824 phba->sli.slistat.link_event++;
3825
3826 /* Create lpfc_handle_latt mailbox command from link ACQE */
3827 lpfc_read_topology(phba, pmb, mp);
3828 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
3829 pmb->vport = phba->pport;
3830
3831 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3832 if (rc == MBX_NOT_FINISHED)
3833 goto out_free_dmabuf;
3834 return;
3835
3836out_free_dmabuf:
3837 kfree(mp);
3838out_free_pmb:
3839 mempool_free(pmb, phba->mbox_mem_pool);
3840}
3841
3842/**
3843 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
3844 * @phba: pointer to lpfc hba data structure.
3845 * @acqe_fc: pointer to the async SLI completion queue entry.
3846 *
3847 * This routine is to handle the SLI4 asynchronous SLI events.
3848 **/
3849static void
3850lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
3851{
James Smart4b8bae02012-06-12 13:55:07 -04003852 char port_name;
James Smart8c1312e2012-10-31 14:45:09 -04003853 char message[128];
James Smart4b8bae02012-06-12 13:55:07 -04003854 uint8_t status;
3855 struct lpfc_acqe_misconfigured_event *misconfigured;
3856
3857 /* special case misconfigured event as it contains data for all ports */
3858 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3859 LPFC_SLI_INTF_IF_TYPE_2) ||
3860 (bf_get(lpfc_trailer_type, acqe_sli) !=
3861 LPFC_SLI_EVENT_TYPE_MISCONFIGURED)) {
3862 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3863 "2901 Async SLI event - Event Data1:x%08x Event Data2:"
3864 "x%08x SLI Event Type:%d\n",
3865 acqe_sli->event_data1, acqe_sli->event_data2,
3866 bf_get(lpfc_trailer_type, acqe_sli));
3867 return;
3868 }
3869
3870 port_name = phba->Port[0];
3871 if (port_name == 0x00)
3872 port_name = '?'; /* get port name is empty */
3873
3874 misconfigured = (struct lpfc_acqe_misconfigured_event *)
3875 &acqe_sli->event_data1;
3876
3877 /* fetch the status for this port */
3878 switch (phba->sli4_hba.lnk_info.lnk_no) {
3879 case LPFC_LINK_NUMBER_0:
3880 status = bf_get(lpfc_sli_misconfigured_port0,
3881 &misconfigured->theEvent);
3882 break;
3883 case LPFC_LINK_NUMBER_1:
3884 status = bf_get(lpfc_sli_misconfigured_port1,
3885 &misconfigured->theEvent);
3886 break;
3887 case LPFC_LINK_NUMBER_2:
3888 status = bf_get(lpfc_sli_misconfigured_port2,
3889 &misconfigured->theEvent);
3890 break;
3891 case LPFC_LINK_NUMBER_3:
3892 status = bf_get(lpfc_sli_misconfigured_port3,
3893 &misconfigured->theEvent);
3894 break;
3895 default:
3896 status = ~LPFC_SLI_EVENT_STATUS_VALID;
3897 break;
3898 }
3899
3900 switch (status) {
3901 case LPFC_SLI_EVENT_STATUS_VALID:
3902 return; /* no message if the sfp is okay */
3903 case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
James Smart292098b2012-09-29 11:31:41 -04003904 sprintf(message, "Optics faulted/incorrectly installed/not " \
3905 "installed - Reseat optics, if issue not "
3906 "resolved, replace.");
James Smart4b8bae02012-06-12 13:55:07 -04003907 break;
3908 case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
3909 sprintf(message,
James Smart292098b2012-09-29 11:31:41 -04003910 "Optics of two types installed - Remove one optic or " \
3911 "install matching pair of optics.");
James Smart4b8bae02012-06-12 13:55:07 -04003912 break;
3913 case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
James Smart292098b2012-09-29 11:31:41 -04003914 sprintf(message, "Incompatible optics - Replace with " \
3915 "compatible optics for card to function.");
James Smart4b8bae02012-06-12 13:55:07 -04003916 break;
3917 default:
3918 /* firmware is reporting a status we don't know about */
3919 sprintf(message, "Unknown event status x%02x", status);
3920 break;
3921 }
3922
3923 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3924 "3176 Misconfigured Physical Port - "
3925 "Port Name %c %s\n", port_name, message);
James Smart70f3c072010-12-15 17:57:33 -05003926}
3927
3928/**
James Smartfc2b9892010-02-26 14:15:29 -05003929 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
3930 * @vport: pointer to vport data structure.
3931 *
3932 * This routine is to perform Clear Virtual Link (CVL) on a vport in
3933 * response to a CVL event.
3934 *
3935 * Return the pointer to the ndlp with the vport if successful, otherwise
3936 * return NULL.
3937 **/
3938static struct lpfc_nodelist *
3939lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
3940{
3941 struct lpfc_nodelist *ndlp;
3942 struct Scsi_Host *shost;
3943 struct lpfc_hba *phba;
3944
3945 if (!vport)
3946 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05003947 phba = vport->phba;
3948 if (!phba)
3949 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04003950 ndlp = lpfc_findnode_did(vport, Fabric_DID);
3951 if (!ndlp) {
3952 /* Cannot find existing Fabric ndlp, so allocate a new one */
3953 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
3954 if (!ndlp)
3955 return 0;
3956 lpfc_nlp_init(vport, ndlp, Fabric_DID);
3957 /* Set the node type */
3958 ndlp->nlp_type |= NLP_FABRIC;
3959 /* Put ndlp onto node list */
3960 lpfc_enqueue_node(vport, ndlp);
3961 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
3962 /* re-setup ndlp without removing from node list */
3963 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
3964 if (!ndlp)
3965 return 0;
3966 }
James Smart63e801c2010-11-20 23:14:19 -05003967 if ((phba->pport->port_state < LPFC_FLOGI) &&
3968 (phba->pport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05003969 return NULL;
3970 /* If virtual link is not yet instantiated ignore CVL */
James Smart63e801c2010-11-20 23:14:19 -05003971 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
3972 && (vport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05003973 return NULL;
3974 shost = lpfc_shost_from_vport(vport);
3975 if (!shost)
3976 return NULL;
3977 lpfc_linkdown_port(vport);
3978 lpfc_cleanup_pending_mbox(vport);
3979 spin_lock_irq(shost->host_lock);
3980 vport->fc_flag |= FC_VPORT_CVL_RCVD;
3981 spin_unlock_irq(shost->host_lock);
3982
3983 return ndlp;
3984}
3985
3986/**
3987 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
3988 * @vport: pointer to lpfc hba data structure.
3989 *
3990 * This routine is to perform Clear Virtual Link (CVL) on all vports in
3991 * response to a FCF dead event.
3992 **/
3993static void
3994lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
3995{
3996 struct lpfc_vport **vports;
3997 int i;
3998
3999 vports = lpfc_create_vport_work_array(phba);
4000 if (vports)
4001 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
4002 lpfc_sli4_perform_vport_cvl(vports[i]);
4003 lpfc_destroy_vport_work_array(phba, vports);
4004}
4005
4006/**
James Smart76a95d72010-11-20 23:11:48 -05004007 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
James Smartda0436e2009-05-22 14:51:39 -04004008 * @phba: pointer to lpfc hba data structure.
4009 * @acqe_link: pointer to the async fcoe completion queue entry.
4010 *
4011 * This routine is to handle the SLI4 asynchronous fcoe event.
4012 **/
4013static void
James Smart76a95d72010-11-20 23:11:48 -05004014lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
James Smart70f3c072010-12-15 17:57:33 -05004015 struct lpfc_acqe_fip *acqe_fip)
James Smartda0436e2009-05-22 14:51:39 -04004016{
James Smart70f3c072010-12-15 17:57:33 -05004017 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004018 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04004019 struct lpfc_vport *vport;
4020 struct lpfc_nodelist *ndlp;
4021 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05004022 int active_vlink_present;
4023 struct lpfc_vport **vports;
4024 int i;
James Smartda0436e2009-05-22 14:51:39 -04004025
James Smart70f3c072010-12-15 17:57:33 -05004026 phba->fc_eventTag = acqe_fip->event_tag;
4027 phba->fcoe_eventtag = acqe_fip->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004028 switch (event_type) {
James Smart70f3c072010-12-15 17:57:33 -05004029 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
4030 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
4031 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
James Smart999d8132010-03-15 11:24:56 -04004032 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4033 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004034 "2546 New FCF event, evt_tag:x%x, "
4035 "index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004036 acqe_fip->event_tag,
4037 acqe_fip->index);
James Smart999d8132010-03-15 11:24:56 -04004038 else
4039 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
4040 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004041 "2788 FCF param modified event, "
4042 "evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004043 acqe_fip->event_tag,
4044 acqe_fip->index);
James Smart38b92ef2010-08-04 16:11:39 -04004045 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004046 /*
4047 * During period of FCF discovery, read the FCF
4048 * table record indexed by the event to update
James Smarta93ff372010-10-22 11:06:08 -04004049 * FCF roundrobin failover eligible FCF bmask.
James Smart0c9ab6f2010-02-26 14:15:57 -05004050 */
4051 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4052 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004053 "2779 Read FCF (x%x) for updating "
4054 "roundrobin FCF failover bmask\n",
James Smart70f3c072010-12-15 17:57:33 -05004055 acqe_fip->index);
4056 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004057 }
James Smart38b92ef2010-08-04 16:11:39 -04004058
4059 /* If the FCF discovery is in progress, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04004060 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04004061 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart38b92ef2010-08-04 16:11:39 -04004062 spin_unlock_irq(&phba->hbalock);
4063 break;
4064 }
4065 /* If fast FCF failover rescan event is pending, do nothing */
4066 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
4067 spin_unlock_irq(&phba->hbalock);
4068 break;
4069 }
4070
4071 /* If the FCF has been in discovered state, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04004072 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
4073 spin_unlock_irq(&phba->hbalock);
4074 break;
4075 }
4076 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04004077
James Smart0c9ab6f2010-02-26 14:15:57 -05004078 /* Otherwise, scan the entire FCF table and re-discover SAN */
4079 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004080 "2770 Start FCF table scan per async FCF "
4081 "event, evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004082 acqe_fip->event_tag, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004083 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
4084 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04004085 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05004086 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4087 "2547 Issue FCF scan read FCF mailbox "
James Smarta93ff372010-10-22 11:06:08 -04004088 "command failed (x%x)\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004089 break;
4090
James Smart70f3c072010-12-15 17:57:33 -05004091 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
James Smartda0436e2009-05-22 14:51:39 -04004092 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04004093 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004094 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
4095 acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04004096 break;
4097
James Smart70f3c072010-12-15 17:57:33 -05004098 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
James Smart80c17842012-03-01 22:35:45 -05004099 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05004100 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004101 "2549 FCF (x%x) disconnected from network, "
James Smart70f3c072010-12-15 17:57:33 -05004102 "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart38b92ef2010-08-04 16:11:39 -04004103 /*
4104 * If we are in the middle of FCF failover process, clear
4105 * the corresponding FCF bit in the roundrobin bitmap.
James Smartda0436e2009-05-22 14:51:39 -04004106 */
James Smartfc2b9892010-02-26 14:15:29 -05004107 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004108 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05004109 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004110 /* Update FLOGI FCF failover eligible FCF bmask */
James Smart70f3c072010-12-15 17:57:33 -05004111 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004112 break;
4113 }
James Smart38b92ef2010-08-04 16:11:39 -04004114 spin_unlock_irq(&phba->hbalock);
4115
4116 /* If the event is not for currently used fcf do nothing */
James Smart70f3c072010-12-15 17:57:33 -05004117 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
James Smart38b92ef2010-08-04 16:11:39 -04004118 break;
4119
4120 /*
4121 * Otherwise, request the port to rediscover the entire FCF
4122 * table for a fast recovery from case that the current FCF
4123 * is no longer valid as we are not in the middle of FCF
4124 * failover process already.
4125 */
4126 spin_lock_irq(&phba->hbalock);
James Smartfc2b9892010-02-26 14:15:29 -05004127 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05004128 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004129 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04004130
James Smart0c9ab6f2010-02-26 14:15:57 -05004131 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
4132 "2771 Start FCF fast failover process due to "
4133 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
James Smart70f3c072010-12-15 17:57:33 -05004134 "\n", acqe_fip->event_tag, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004135 rc = lpfc_sli4_redisc_fcf_table(phba);
4136 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004137 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4138 LOG_DISCOVERY,
4139 "2772 Issue FCF rediscover mabilbox "
4140 "command failed, fail through to FCF "
4141 "dead event\n");
James Smartfc2b9892010-02-26 14:15:29 -05004142 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004143 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004144 spin_unlock_irq(&phba->hbalock);
4145 /*
4146 * Last resort will fail over by treating this
4147 * as a link down to FCF registration.
4148 */
4149 lpfc_sli4_fcf_dead_failthrough(phba);
James Smart38b92ef2010-08-04 16:11:39 -04004150 } else {
4151 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04004152 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart38b92ef2010-08-04 16:11:39 -04004153 /*
4154 * Handling fast FCF failover to a DEAD FCF event is
4155 * considered equalivant to receiving CVL to all vports.
James Smartfc2b9892010-02-26 14:15:29 -05004156 */
4157 lpfc_sli4_perform_all_vport_cvl(phba);
James Smart38b92ef2010-08-04 16:11:39 -04004158 }
James Smartda0436e2009-05-22 14:51:39 -04004159 break;
James Smart70f3c072010-12-15 17:57:33 -05004160 case LPFC_FIP_EVENT_TYPE_CVL:
James Smart80c17842012-03-01 22:35:45 -05004161 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05004162 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6669f9b2009-10-02 15:16:45 -04004163 "2718 Clear Virtual Link Received for VPI 0x%x"
James Smart70f3c072010-12-15 17:57:33 -05004164 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart6d368e52011-05-24 11:44:12 -04004165
James Smart6669f9b2009-10-02 15:16:45 -04004166 vport = lpfc_find_vport_by_vpid(phba,
James Smart5248a742011-07-22 18:37:06 -04004167 acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004168 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004169 if (!ndlp)
4170 break;
James Smart695a8142010-01-26 23:08:03 -05004171 active_vlink_present = 0;
4172
4173 vports = lpfc_create_vport_work_array(phba);
4174 if (vports) {
4175 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
4176 i++) {
4177 if ((!(vports[i]->fc_flag &
4178 FC_VPORT_CVL_RCVD)) &&
4179 (vports[i]->port_state > LPFC_FDISC)) {
4180 active_vlink_present = 1;
4181 break;
4182 }
4183 }
4184 lpfc_destroy_vport_work_array(phba, vports);
4185 }
4186
4187 if (active_vlink_present) {
4188 /*
4189 * If there are other active VLinks present,
4190 * re-instantiate the Vlink using FDISC.
4191 */
James Smart6669f9b2009-10-02 15:16:45 -04004192 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
James Smartfc2b9892010-02-26 14:15:29 -05004193 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004194 spin_lock_irq(shost->host_lock);
4195 ndlp->nlp_flag |= NLP_DELAY_TMO;
4196 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05004197 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
4198 vport->port_state = LPFC_FDISC;
4199 } else {
James Smartecfd03c2010-02-12 14:41:27 -05004200 /*
4201 * Otherwise, we request port to rediscover
4202 * the entire FCF table for a fast recovery
4203 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05004204 * is no longer valid if we are not already
4205 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05004206 */
James Smartfc2b9892010-02-26 14:15:29 -05004207 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004208 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05004209 spin_unlock_irq(&phba->hbalock);
4210 break;
4211 }
4212 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05004213 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004214 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004215 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4216 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004217 "2773 Start FCF failover per CVL, "
James Smart70f3c072010-12-15 17:57:33 -05004218 "evt_tag:x%x\n", acqe_fip->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05004219 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05004220 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004221 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4222 LOG_DISCOVERY,
4223 "2774 Issue FCF rediscover "
4224 "mabilbox command failed, "
4225 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05004226 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004227 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004228 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05004229 /*
4230 * Last resort will be re-try on the
4231 * the current registered FCF entry.
4232 */
4233 lpfc_retry_pport_discovery(phba);
James Smart38b92ef2010-08-04 16:11:39 -04004234 } else
4235 /*
4236 * Reset FCF roundrobin bmask for new
4237 * discovery.
4238 */
James Smart7d791df2011-07-22 18:37:52 -04004239 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart6669f9b2009-10-02 15:16:45 -04004240 }
4241 break;
James Smartda0436e2009-05-22 14:51:39 -04004242 default:
4243 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4244 "0288 Unknown FCoE event type 0x%x event tag "
James Smart70f3c072010-12-15 17:57:33 -05004245 "0x%x\n", event_type, acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04004246 break;
4247 }
4248}
4249
4250/**
4251 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
4252 * @phba: pointer to lpfc hba data structure.
4253 * @acqe_link: pointer to the async dcbx completion queue entry.
4254 *
4255 * This routine is to handle the SLI4 asynchronous dcbx event.
4256 **/
4257static void
4258lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
4259 struct lpfc_acqe_dcbx *acqe_dcbx)
4260{
James Smart4d9ab992009-10-02 15:16:39 -04004261 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004262 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4263 "0290 The SLI4 DCBX asynchronous event is not "
4264 "handled yet\n");
4265}
4266
4267/**
James Smartb19a0612010-04-06 14:48:51 -04004268 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
4269 * @phba: pointer to lpfc hba data structure.
4270 * @acqe_link: pointer to the async grp5 completion queue entry.
4271 *
4272 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
4273 * is an asynchronous notified of a logical link speed change. The Port
4274 * reports the logical link speed in units of 10Mbps.
4275 **/
4276static void
4277lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
4278 struct lpfc_acqe_grp5 *acqe_grp5)
4279{
4280 uint16_t prev_ll_spd;
4281
4282 phba->fc_eventTag = acqe_grp5->event_tag;
4283 phba->fcoe_eventtag = acqe_grp5->event_tag;
4284 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
4285 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04004286 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
James Smartb19a0612010-04-06 14:48:51 -04004287 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4288 "2789 GRP5 Async Event: Updating logical link speed "
James Smart8b68cd52012-09-29 11:32:37 -04004289 "from %dMbps to %dMbps\n", prev_ll_spd,
4290 phba->sli4_hba.link_state.logical_speed);
James Smartb19a0612010-04-06 14:48:51 -04004291}
4292
4293/**
James Smartda0436e2009-05-22 14:51:39 -04004294 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
4295 * @phba: pointer to lpfc hba data structure.
4296 *
4297 * This routine is invoked by the worker thread to process all the pending
4298 * SLI4 asynchronous events.
4299 **/
4300void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
4301{
4302 struct lpfc_cq_event *cq_event;
4303
4304 /* First, declare the async event has been handled */
4305 spin_lock_irq(&phba->hbalock);
4306 phba->hba_flag &= ~ASYNC_EVENT;
4307 spin_unlock_irq(&phba->hbalock);
4308 /* Now, handle all the async events */
4309 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
4310 /* Get the first event from the head of the event queue */
4311 spin_lock_irq(&phba->hbalock);
4312 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
4313 cq_event, struct lpfc_cq_event, list);
4314 spin_unlock_irq(&phba->hbalock);
4315 /* Process the asynchronous event */
4316 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
4317 case LPFC_TRAILER_CODE_LINK:
4318 lpfc_sli4_async_link_evt(phba,
4319 &cq_event->cqe.acqe_link);
4320 break;
4321 case LPFC_TRAILER_CODE_FCOE:
James Smart70f3c072010-12-15 17:57:33 -05004322 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004323 break;
4324 case LPFC_TRAILER_CODE_DCBX:
4325 lpfc_sli4_async_dcbx_evt(phba,
4326 &cq_event->cqe.acqe_dcbx);
4327 break;
James Smartb19a0612010-04-06 14:48:51 -04004328 case LPFC_TRAILER_CODE_GRP5:
4329 lpfc_sli4_async_grp5_evt(phba,
4330 &cq_event->cqe.acqe_grp5);
4331 break;
James Smart70f3c072010-12-15 17:57:33 -05004332 case LPFC_TRAILER_CODE_FC:
4333 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
4334 break;
4335 case LPFC_TRAILER_CODE_SLI:
4336 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
4337 break;
James Smartda0436e2009-05-22 14:51:39 -04004338 default:
4339 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4340 "1804 Invalid asynchrous event code: "
4341 "x%x\n", bf_get(lpfc_trailer_code,
4342 &cq_event->cqe.mcqe_cmpl));
4343 break;
4344 }
4345 /* Free the completion event processed to the free pool */
4346 lpfc_sli4_cq_event_release(phba, cq_event);
4347 }
4348}
4349
4350/**
James Smartecfd03c2010-02-12 14:41:27 -05004351 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
4352 * @phba: pointer to lpfc hba data structure.
4353 *
4354 * This routine is invoked by the worker thread to process FCF table
4355 * rediscovery pending completion event.
4356 **/
4357void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
4358{
4359 int rc;
4360
4361 spin_lock_irq(&phba->hbalock);
4362 /* Clear FCF rediscovery timeout event */
4363 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
4364 /* Clear driver fast failover FCF record flag */
4365 phba->fcf.failover_rec.flag = 0;
4366 /* Set state for FCF fast failover */
4367 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
4368 spin_unlock_irq(&phba->hbalock);
4369
4370 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05004371 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004372 "2777 Start post-quiescent FCF table scan\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05004373 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05004374 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05004375 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4376 "2747 Issue FCF scan read FCF mailbox "
4377 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05004378}
4379
4380/**
James Smartda0436e2009-05-22 14:51:39 -04004381 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
4382 * @phba: pointer to lpfc hba data structure.
4383 * @dev_grp: The HBA PCI-Device group number.
4384 *
4385 * This routine is invoked to set up the per HBA PCI-Device group function
4386 * API jump table entries.
4387 *
4388 * Return: 0 if success, otherwise -ENODEV
4389 **/
4390int
4391lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05004392{
4393 int rc;
4394
James Smartda0436e2009-05-22 14:51:39 -04004395 /* Set up lpfc PCI-device group */
4396 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05004397
James Smartda0436e2009-05-22 14:51:39 -04004398 /* The LPFC_PCI_DEV_OC uses SLI4 */
4399 if (dev_grp == LPFC_PCI_DEV_OC)
4400 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05004401
James Smartda0436e2009-05-22 14:51:39 -04004402 /* Set up device INIT API function jump table */
4403 rc = lpfc_init_api_table_setup(phba, dev_grp);
4404 if (rc)
4405 return -ENODEV;
4406 /* Set up SCSI API function jump table */
4407 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
4408 if (rc)
4409 return -ENODEV;
4410 /* Set up SLI API function jump table */
4411 rc = lpfc_sli_api_table_setup(phba, dev_grp);
4412 if (rc)
4413 return -ENODEV;
4414 /* Set up MBOX API function jump table */
4415 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
4416 if (rc)
4417 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004418
James Smartda0436e2009-05-22 14:51:39 -04004419 return 0;
James Smart5b75da22008-12-04 22:39:35 -05004420}
4421
4422/**
James Smart3621a712009-04-06 18:47:14 -04004423 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05004424 * @phba: pointer to lpfc hba data structure.
4425 * @intr_mode: active interrupt mode adopted.
4426 *
4427 * This routine it invoked to log the currently used active interrupt mode
4428 * to the device.
James Smart3772a992009-05-22 14:50:54 -04004429 **/
4430static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05004431{
4432 switch (intr_mode) {
4433 case 0:
4434 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4435 "0470 Enable INTx interrupt mode.\n");
4436 break;
4437 case 1:
4438 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4439 "0481 Enabled MSI interrupt mode.\n");
4440 break;
4441 case 2:
4442 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4443 "0480 Enabled MSI-X interrupt mode.\n");
4444 break;
4445 default:
4446 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4447 "0482 Illegal interrupt mode.\n");
4448 break;
4449 }
4450 return;
4451}
4452
James Smart5b75da22008-12-04 22:39:35 -05004453/**
James Smart3772a992009-05-22 14:50:54 -04004454 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05004455 * @phba: pointer to lpfc hba data structure.
4456 *
James Smart3772a992009-05-22 14:50:54 -04004457 * This routine is invoked to enable the PCI device that is common to all
4458 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004459 *
4460 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004461 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004462 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05004463 **/
James Smart3772a992009-05-22 14:50:54 -04004464static int
4465lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004466{
James Smart3772a992009-05-22 14:50:54 -04004467 struct pci_dev *pdev;
James Smart079b5c92011-08-21 21:48:49 -04004468 int bars = 0;
James Smart5b75da22008-12-04 22:39:35 -05004469
James Smart3772a992009-05-22 14:50:54 -04004470 /* Obtain PCI device reference */
4471 if (!phba->pcidev)
4472 goto out_error;
4473 else
4474 pdev = phba->pcidev;
4475 /* Select PCI BARs */
4476 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4477 /* Enable PCI device */
4478 if (pci_enable_device_mem(pdev))
4479 goto out_error;
4480 /* Request PCI resource for the device */
4481 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
4482 goto out_disable_device;
4483 /* Set up device as PCI master and save state for EEH */
4484 pci_set_master(pdev);
4485 pci_try_set_mwi(pdev);
4486 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05004487
James Smart05580562011-05-24 11:40:48 -04004488 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
4489 if (pci_find_capability(pdev, PCI_CAP_ID_EXP))
4490 pdev->needs_freset = 1;
4491
James Smart3772a992009-05-22 14:50:54 -04004492 return 0;
James Smart5b75da22008-12-04 22:39:35 -05004493
James Smart3772a992009-05-22 14:50:54 -04004494out_disable_device:
4495 pci_disable_device(pdev);
4496out_error:
James Smart079b5c92011-08-21 21:48:49 -04004497 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4498 "1401 Failed to enable pci device, bars:x%x\n", bars);
James Smart3772a992009-05-22 14:50:54 -04004499 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004500}
4501
4502/**
James Smart3772a992009-05-22 14:50:54 -04004503 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05004504 * @phba: pointer to lpfc hba data structure.
4505 *
James Smart3772a992009-05-22 14:50:54 -04004506 * This routine is invoked to disable the PCI device that is common to all
4507 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004508 **/
4509static void
James Smart3772a992009-05-22 14:50:54 -04004510lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004511{
James Smart3772a992009-05-22 14:50:54 -04004512 struct pci_dev *pdev;
4513 int bars;
James Smart5b75da22008-12-04 22:39:35 -05004514
James Smart3772a992009-05-22 14:50:54 -04004515 /* Obtain PCI device reference */
4516 if (!phba->pcidev)
4517 return;
4518 else
4519 pdev = phba->pcidev;
4520 /* Select PCI BARs */
4521 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4522 /* Release PCI resource and disable PCI device */
4523 pci_release_selected_regions(pdev, bars);
4524 pci_disable_device(pdev);
4525 /* Null out PCI private reference to driver */
4526 pci_set_drvdata(pdev, NULL);
James Smart5b75da22008-12-04 22:39:35 -05004527
4528 return;
4529}
4530
4531/**
James Smart3772a992009-05-22 14:50:54 -04004532 * lpfc_reset_hba - Reset a hba
4533 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04004534 *
James Smart3772a992009-05-22 14:50:54 -04004535 * This routine is invoked to reset a hba device. It brings the HBA
4536 * offline, performs a board restart, and then brings the board back
4537 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
4538 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04004539 **/
James Smart3772a992009-05-22 14:50:54 -04004540void
4541lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004542{
James Smart3772a992009-05-22 14:50:54 -04004543 /* If resets are disabled then set error state and return. */
4544 if (!phba->cfg_enable_hba_reset) {
4545 phba->link_state = LPFC_HBA_ERROR;
4546 return;
4547 }
James Smart618a5232012-06-12 13:54:36 -04004548 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3772a992009-05-22 14:50:54 -04004549 lpfc_offline(phba);
4550 lpfc_sli_brdrestart(phba);
4551 lpfc_online(phba);
4552 lpfc_unblock_mgmt_io(phba);
4553}
dea31012005-04-17 16:05:31 -05004554
James Smart3772a992009-05-22 14:50:54 -04004555/**
James Smart0a96e972011-07-22 18:37:28 -04004556 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
4557 * @phba: pointer to lpfc hba data structure.
4558 *
4559 * This function enables the PCI SR-IOV virtual functions to a physical
4560 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4561 * enable the number of virtual functions to the physical function. As
4562 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4563 * API call does not considered as an error condition for most of the device.
4564 **/
4565uint16_t
4566lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
4567{
4568 struct pci_dev *pdev = phba->pcidev;
4569 uint16_t nr_virtfn;
4570 int pos;
4571
James Smart0a96e972011-07-22 18:37:28 -04004572 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
4573 if (pos == 0)
4574 return 0;
4575
4576 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
4577 return nr_virtfn;
4578}
4579
4580/**
James Smart912e3ac2011-05-24 11:42:11 -04004581 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
4582 * @phba: pointer to lpfc hba data structure.
4583 * @nr_vfn: number of virtual functions to be enabled.
4584 *
4585 * This function enables the PCI SR-IOV virtual functions to a physical
4586 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4587 * enable the number of virtual functions to the physical function. As
4588 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4589 * API call does not considered as an error condition for most of the device.
4590 **/
4591int
4592lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
4593{
4594 struct pci_dev *pdev = phba->pcidev;
James Smart0a96e972011-07-22 18:37:28 -04004595 uint16_t max_nr_vfn;
James Smart912e3ac2011-05-24 11:42:11 -04004596 int rc;
4597
James Smart0a96e972011-07-22 18:37:28 -04004598 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
4599 if (nr_vfn > max_nr_vfn) {
4600 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4601 "3057 Requested vfs (%d) greater than "
4602 "supported vfs (%d)", nr_vfn, max_nr_vfn);
4603 return -EINVAL;
4604 }
4605
James Smart912e3ac2011-05-24 11:42:11 -04004606 rc = pci_enable_sriov(pdev, nr_vfn);
4607 if (rc) {
4608 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4609 "2806 Failed to enable sriov on this device "
4610 "with vfn number nr_vf:%d, rc:%d\n",
4611 nr_vfn, rc);
4612 } else
4613 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4614 "2807 Successful enable sriov on this device "
4615 "with vfn number nr_vf:%d\n", nr_vfn);
4616 return rc;
4617}
4618
4619/**
James Smart3772a992009-05-22 14:50:54 -04004620 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
4621 * @phba: pointer to lpfc hba data structure.
4622 *
4623 * This routine is invoked to set up the driver internal resources specific to
4624 * support the SLI-3 HBA device it attached to.
4625 *
4626 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004627 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004628 * other values - error
4629 **/
4630static int
4631lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
4632{
4633 struct lpfc_sli *psli;
James Smart912e3ac2011-05-24 11:42:11 -04004634 int rc;
dea31012005-04-17 16:05:31 -05004635
James Smart3772a992009-05-22 14:50:54 -04004636 /*
4637 * Initialize timers used by driver
4638 */
dea31012005-04-17 16:05:31 -05004639
James Smart3772a992009-05-22 14:50:54 -04004640 /* Heartbeat timer */
4641 init_timer(&phba->hb_tmofunc);
4642 phba->hb_tmofunc.function = lpfc_hb_timeout;
4643 phba->hb_tmofunc.data = (unsigned long)phba;
4644
4645 psli = &phba->sli;
4646 /* MBOX heartbeat timer */
4647 init_timer(&psli->mbox_tmo);
4648 psli->mbox_tmo.function = lpfc_mbox_timeout;
4649 psli->mbox_tmo.data = (unsigned long) phba;
4650 /* FCP polling mode timer */
4651 init_timer(&phba->fcp_poll_timer);
4652 phba->fcp_poll_timer.function = lpfc_poll_timeout;
4653 phba->fcp_poll_timer.data = (unsigned long) phba;
4654 /* Fabric block timer */
4655 init_timer(&phba->fabric_block_timer);
4656 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4657 phba->fabric_block_timer.data = (unsigned long) phba;
4658 /* EA polling mode timer */
4659 init_timer(&phba->eratt_poll);
4660 phba->eratt_poll.function = lpfc_poll_eratt;
4661 phba->eratt_poll.data = (unsigned long) phba;
4662
4663 /* Host attention work mask setup */
4664 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
4665 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
4666
4667 /* Get all the module params for configuring this host */
4668 lpfc_get_cfgparam(phba);
James Smart49198b32010-04-06 15:04:33 -04004669 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
4670 phba->menlo_flag |= HBA_MENLO_SUPPORT;
4671 /* check for menlo minimum sg count */
4672 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
4673 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
4674 }
4675
James Smart2a76a282012-08-03 12:35:54 -04004676 if (!phba->sli.ring)
4677 phba->sli.ring = (struct lpfc_sli_ring *)
4678 kzalloc(LPFC_SLI3_MAX_RING *
4679 sizeof(struct lpfc_sli_ring), GFP_KERNEL);
4680 if (!phba->sli.ring)
4681 return -ENOMEM;
4682
James Smart3772a992009-05-22 14:50:54 -04004683 /*
4684 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
4685 * used to create the sg_dma_buf_pool must be dynamically calculated.
4686 * 2 segments are added since the IOCB needs a command and response bde.
4687 */
4688 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4689 sizeof(struct fcp_rsp) +
4690 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
4691
4692 if (phba->cfg_enable_bg) {
4693 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT;
4694 phba->cfg_sg_dma_buf_size +=
4695 phba->cfg_prot_sg_seg_cnt * sizeof(struct ulp_bde64);
4696 }
4697
4698 /* Also reinitialize the host templates with new values. */
4699 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4700 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4701
4702 phba->max_vpi = LPFC_MAX_VPI;
4703 /* This will be set to correct value after config_port mbox */
4704 phba->max_vports = 0;
4705
4706 /*
4707 * Initialize the SLI Layer to run with lpfc HBAs.
4708 */
4709 lpfc_sli_setup(phba);
4710 lpfc_sli_queue_setup(phba);
4711
4712 /* Allocate device driver memory */
4713 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
4714 return -ENOMEM;
4715
James Smart912e3ac2011-05-24 11:42:11 -04004716 /*
4717 * Enable sr-iov virtual functions if supported and configured
4718 * through the module parameter.
4719 */
4720 if (phba->cfg_sriov_nr_virtfn > 0) {
4721 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
4722 phba->cfg_sriov_nr_virtfn);
4723 if (rc) {
4724 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4725 "2808 Requested number of SR-IOV "
4726 "virtual functions (%d) is not "
4727 "supported\n",
4728 phba->cfg_sriov_nr_virtfn);
4729 phba->cfg_sriov_nr_virtfn = 0;
4730 }
4731 }
4732
James Smart3772a992009-05-22 14:50:54 -04004733 return 0;
4734}
4735
4736/**
4737 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
4738 * @phba: pointer to lpfc hba data structure.
4739 *
4740 * This routine is invoked to unset the driver internal resources set up
4741 * specific for supporting the SLI-3 HBA device it attached to.
4742 **/
4743static void
4744lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
4745{
4746 /* Free device driver memory allocated */
4747 lpfc_mem_free_all(phba);
4748
4749 return;
4750}
4751
4752/**
James Smartda0436e2009-05-22 14:51:39 -04004753 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
4754 * @phba: pointer to lpfc hba data structure.
4755 *
4756 * This routine is invoked to set up the driver internal resources specific to
4757 * support the SLI-4 HBA device it attached to.
4758 *
4759 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004760 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004761 * other values - error
4762 **/
4763static int
4764lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
4765{
4766 struct lpfc_sli *psli;
James Smart28baac72010-02-12 14:42:03 -05004767 LPFC_MBOXQ_t *mboxq;
4768 int rc, i, hbq_count, buf_size, dma_buf_size, max_buf_size;
4769 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
4770 struct lpfc_mqe *mqe;
James Smart085c6472010-11-20 23:11:37 -05004771 int longs, sli_family;
James Smartacd68592012-01-18 16:25:09 -05004772 int sges_per_segment;
James Smartda0436e2009-05-22 14:51:39 -04004773
4774 /* Before proceed, wait for POST done and device ready */
4775 rc = lpfc_sli4_post_status_check(phba);
4776 if (rc)
4777 return -ENODEV;
4778
4779 /*
4780 * Initialize timers used by driver
4781 */
4782
4783 /* Heartbeat timer */
4784 init_timer(&phba->hb_tmofunc);
4785 phba->hb_tmofunc.function = lpfc_hb_timeout;
4786 phba->hb_tmofunc.data = (unsigned long)phba;
James Smart19ca7602010-11-20 23:11:55 -05004787 init_timer(&phba->rrq_tmr);
4788 phba->rrq_tmr.function = lpfc_rrq_timeout;
4789 phba->rrq_tmr.data = (unsigned long)phba;
James Smartda0436e2009-05-22 14:51:39 -04004790
4791 psli = &phba->sli;
4792 /* MBOX heartbeat timer */
4793 init_timer(&psli->mbox_tmo);
4794 psli->mbox_tmo.function = lpfc_mbox_timeout;
4795 psli->mbox_tmo.data = (unsigned long) phba;
4796 /* Fabric block timer */
4797 init_timer(&phba->fabric_block_timer);
4798 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4799 phba->fabric_block_timer.data = (unsigned long) phba;
4800 /* EA polling mode timer */
4801 init_timer(&phba->eratt_poll);
4802 phba->eratt_poll.function = lpfc_poll_eratt;
4803 phba->eratt_poll.data = (unsigned long) phba;
James Smartecfd03c2010-02-12 14:41:27 -05004804 /* FCF rediscover timer */
4805 init_timer(&phba->fcf.redisc_wait);
4806 phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
4807 phba->fcf.redisc_wait.data = (unsigned long)phba;
4808
James Smartda0436e2009-05-22 14:51:39 -04004809 /*
James Smart7ad20aa2011-05-24 11:44:28 -04004810 * Control structure for handling external multi-buffer mailbox
4811 * command pass-through.
4812 */
4813 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
4814 sizeof(struct lpfc_mbox_ext_buf_ctx));
4815 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
4816
4817 /*
James Smartda0436e2009-05-22 14:51:39 -04004818 * We need to do a READ_CONFIG mailbox command here before
4819 * calling lpfc_get_cfgparam. For VFs this will report the
4820 * MAX_XRI, MAX_VPI, MAX_RPI, MAX_IOCB, and MAX_VFI settings.
4821 * All of the resources allocated
4822 * for this Port are tied to these values.
4823 */
4824 /* Get all the module params for configuring this host */
4825 lpfc_get_cfgparam(phba);
4826 phba->max_vpi = LPFC_MAX_VPI;
James Smart67d12732012-08-03 12:36:13 -04004827
4828 /* Eventually cfg_fcp_eq_count / cfg_fcp_wq_count will be depricated */
4829 phba->cfg_fcp_io_channel = phba->cfg_fcp_eq_count;
4830
James Smartda0436e2009-05-22 14:51:39 -04004831 /* This will be set to correct value after the read_config mbox */
4832 phba->max_vports = 0;
4833
4834 /* Program the default value of vlan_id and fc_map */
4835 phba->valid_vlan = 0;
4836 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4837 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4838 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4839
James Smartacd68592012-01-18 16:25:09 -05004840 /* With BlockGuard we can have multiple SGEs per Data Segemnt */
4841 sges_per_segment = 1;
4842 if (phba->cfg_enable_bg)
4843 sges_per_segment = 2;
4844
James Smartda0436e2009-05-22 14:51:39 -04004845 /*
James Smart2a76a282012-08-03 12:35:54 -04004846 * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
4847 * we will associate a new ring, for each FCP fastpath EQ/CQ/WQ tuple.
4848 */
4849 if (!phba->sli.ring)
4850 phba->sli.ring = kzalloc(
James Smart67d12732012-08-03 12:36:13 -04004851 (LPFC_SLI3_MAX_RING + phba->cfg_fcp_io_channel) *
James Smart2a76a282012-08-03 12:35:54 -04004852 sizeof(struct lpfc_sli_ring), GFP_KERNEL);
4853 if (!phba->sli.ring)
4854 return -ENOMEM;
4855 /*
James Smartda0436e2009-05-22 14:51:39 -04004856 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
4857 * used to create the sg_dma_buf_pool must be dynamically calculated.
4858 * 2 segments are added since the IOCB needs a command and response bde.
4859 * To insure that the scsi sgl does not cross a 4k page boundary only
James Smart28baac72010-02-12 14:42:03 -05004860 * sgl sizes of must be a power of 2.
James Smartda0436e2009-05-22 14:51:39 -04004861 */
James Smart28baac72010-02-12 14:42:03 -05004862 buf_size = (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp) +
James Smartacd68592012-01-18 16:25:09 -05004863 (((phba->cfg_sg_seg_cnt * sges_per_segment) + 2) *
4864 sizeof(struct sli4_sge)));
James Smart085c6472010-11-20 23:11:37 -05004865
4866 sli_family = bf_get(lpfc_sli_intf_sli_family, &phba->sli4_hba.sli_intf);
4867 max_buf_size = LPFC_SLI4_MAX_BUF_SIZE;
4868 switch (sli_family) {
4869 case LPFC_SLI_INTF_FAMILY_BE2:
4870 case LPFC_SLI_INTF_FAMILY_BE3:
4871 /* There is a single hint for BE - 2 pages per BPL. */
4872 if (bf_get(lpfc_sli_intf_sli_hint1, &phba->sli4_hba.sli_intf) ==
4873 LPFC_SLI_INTF_SLI_HINT1_1)
4874 max_buf_size = LPFC_SLI4_FL1_MAX_BUF_SIZE;
4875 break;
4876 case LPFC_SLI_INTF_FAMILY_LNCR_A0:
4877 case LPFC_SLI_INTF_FAMILY_LNCR_B0:
4878 default:
4879 break;
4880 }
James Smartacd68592012-01-18 16:25:09 -05004881
James Smart28baac72010-02-12 14:42:03 -05004882 for (dma_buf_size = LPFC_SLI4_MIN_BUF_SIZE;
4883 dma_buf_size < max_buf_size && buf_size > dma_buf_size;
4884 dma_buf_size = dma_buf_size << 1)
4885 ;
4886 if (dma_buf_size == max_buf_size)
4887 phba->cfg_sg_seg_cnt = (dma_buf_size -
4888 sizeof(struct fcp_cmnd) - sizeof(struct fcp_rsp) -
4889 (2 * sizeof(struct sli4_sge))) /
4890 sizeof(struct sli4_sge);
4891 phba->cfg_sg_dma_buf_size = dma_buf_size;
James Smartda0436e2009-05-22 14:51:39 -04004892
4893 /* Initialize buffer queue management fields */
4894 hbq_count = lpfc_sli_hbq_count();
4895 for (i = 0; i < hbq_count; ++i)
4896 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
4897 INIT_LIST_HEAD(&phba->rb_pend_list);
4898 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
4899 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
4900
4901 /*
4902 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
4903 */
4904 /* Initialize the Abort scsi buffer list used by driver */
4905 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
4906 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
4907 /* This abort list used by worker thread */
4908 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
4909
4910 /*
James Smart6d368e52011-05-24 11:44:12 -04004911 * Initialize driver internal slow-path work queues
James Smartda0436e2009-05-22 14:51:39 -04004912 */
4913
4914 /* Driver internel slow-path CQ Event pool */
4915 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
4916 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04004917 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04004918 /* Asynchronous event CQ Event work queue list */
4919 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
4920 /* Fast-path XRI aborted CQ Event work queue list */
4921 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
4922 /* Slow-path XRI aborted CQ Event work queue list */
4923 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
4924 /* Receive queue CQ Event work queue list */
4925 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
4926
James Smart6d368e52011-05-24 11:44:12 -04004927 /* Initialize extent block lists. */
4928 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
4929 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
4930 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
4931 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
4932
James Smartda0436e2009-05-22 14:51:39 -04004933 /* Initialize the driver internal SLI layer lists. */
4934 lpfc_sli_setup(phba);
4935 lpfc_sli_queue_setup(phba);
4936
4937 /* Allocate device driver memory */
4938 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
4939 if (rc)
4940 return -ENOMEM;
4941
James Smart2fcee4b2010-12-15 17:57:46 -05004942 /* IF Type 2 ports get initialized now. */
4943 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
4944 LPFC_SLI_INTF_IF_TYPE_2) {
4945 rc = lpfc_pci_function_reset(phba);
4946 if (unlikely(rc))
4947 return -ENODEV;
4948 }
4949
James Smartda0436e2009-05-22 14:51:39 -04004950 /* Create the bootstrap mailbox command */
4951 rc = lpfc_create_bootstrap_mbox(phba);
4952 if (unlikely(rc))
4953 goto out_free_mem;
4954
4955 /* Set up the host's endian order with the device. */
4956 rc = lpfc_setup_endian_order(phba);
4957 if (unlikely(rc))
4958 goto out_free_bsmbx;
4959
4960 /* Set up the hba's configuration parameters. */
4961 rc = lpfc_sli4_read_config(phba);
4962 if (unlikely(rc))
4963 goto out_free_bsmbx;
4964
James Smart2fcee4b2010-12-15 17:57:46 -05004965 /* IF Type 0 ports get initialized now. */
4966 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
4967 LPFC_SLI_INTF_IF_TYPE_0) {
4968 rc = lpfc_pci_function_reset(phba);
4969 if (unlikely(rc))
4970 goto out_free_bsmbx;
4971 }
James Smartda0436e2009-05-22 14:51:39 -04004972
James Smartcb5172e2010-03-15 11:25:07 -04004973 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
4974 GFP_KERNEL);
4975 if (!mboxq) {
4976 rc = -ENOMEM;
4977 goto out_free_bsmbx;
4978 }
4979
James Smartfedd3b72011-02-16 12:39:24 -05004980 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
James Smartcb5172e2010-03-15 11:25:07 -04004981 lpfc_supported_pages(mboxq);
4982 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smartfedd3b72011-02-16 12:39:24 -05004983 if (!rc) {
4984 mqe = &mboxq->u.mqe;
4985 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
4986 LPFC_MAX_SUPPORTED_PAGES);
4987 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
4988 switch (pn_page[i]) {
4989 case LPFC_SLI4_PARAMETERS:
4990 phba->sli4_hba.pc_sli4_params.supported = 1;
4991 break;
4992 default:
4993 break;
4994 }
4995 }
4996 /* Read the port's SLI4 Parameters capabilities if supported. */
4997 if (phba->sli4_hba.pc_sli4_params.supported)
4998 rc = lpfc_pc_sli4_params_get(phba, mboxq);
4999 if (rc) {
5000 mempool_free(mboxq, phba->mbox_mem_pool);
5001 rc = -EIO;
5002 goto out_free_bsmbx;
James Smartcb5172e2010-03-15 11:25:07 -04005003 }
5004 }
James Smartfedd3b72011-02-16 12:39:24 -05005005 /*
5006 * Get sli4 parameters that override parameters from Port capabilities.
James Smart6d368e52011-05-24 11:44:12 -04005007 * If this call fails, it isn't critical unless the SLI4 parameters come
5008 * back in conflict.
James Smartfedd3b72011-02-16 12:39:24 -05005009 */
James Smart6d368e52011-05-24 11:44:12 -04005010 rc = lpfc_get_sli4_parameters(phba, mboxq);
5011 if (rc) {
5012 if (phba->sli4_hba.extents_in_use &&
5013 phba->sli4_hba.rpi_hdrs_in_use) {
5014 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5015 "2999 Unsupported SLI4 Parameters "
5016 "Extents and RPI headers enabled.\n");
5017 goto out_free_bsmbx;
5018 }
5019 }
James Smartcb5172e2010-03-15 11:25:07 -04005020 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart5350d872011-10-10 21:33:49 -04005021 /* Verify all the SLI4 queues */
5022 rc = lpfc_sli4_queue_verify(phba);
James Smartda0436e2009-05-22 14:51:39 -04005023 if (rc)
5024 goto out_free_bsmbx;
5025
5026 /* Create driver internal CQE event pool */
5027 rc = lpfc_sli4_cq_event_pool_create(phba);
5028 if (rc)
James Smart5350d872011-10-10 21:33:49 -04005029 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04005030
James Smart8a9d2e82012-05-09 21:16:12 -04005031 /* Initialize sgl lists per host */
5032 lpfc_init_sgl_list(phba);
5033
5034 /* Allocate and initialize active sgl array */
James Smartda0436e2009-05-22 14:51:39 -04005035 rc = lpfc_init_active_sgl_array(phba);
5036 if (rc) {
5037 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5038 "1430 Failed to initialize sgl list.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04005039 goto out_destroy_cq_event_pool;
James Smartda0436e2009-05-22 14:51:39 -04005040 }
James Smartda0436e2009-05-22 14:51:39 -04005041 rc = lpfc_sli4_init_rpi_hdrs(phba);
5042 if (rc) {
5043 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5044 "1432 Failed to initialize rpi headers.\n");
5045 goto out_free_active_sgl;
5046 }
5047
James Smarta93ff372010-10-22 11:06:08 -04005048 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
James Smart0c9ab6f2010-02-26 14:15:57 -05005049 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
5050 phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
5051 GFP_KERNEL);
5052 if (!phba->fcf.fcf_rr_bmask) {
5053 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5054 "2759 Failed allocate memory for FCF round "
5055 "robin failover bmask\n");
James Smart05580562011-05-24 11:40:48 -04005056 rc = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -05005057 goto out_remove_rpi_hdrs;
5058 }
5059
James Smart67d12732012-08-03 12:36:13 -04005060 phba->sli4_hba.fcp_eq_hdl =
5061 kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
5062 phba->cfg_fcp_io_channel), GFP_KERNEL);
5063 if (!phba->sli4_hba.fcp_eq_hdl) {
5064 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5065 "2572 Failed allocate memory for "
5066 "fast-path per-EQ handle array\n");
5067 rc = -ENOMEM;
5068 goto out_free_fcf_rr_bmask;
James Smartda0436e2009-05-22 14:51:39 -04005069 }
5070
5071 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
James Smart82c3e9b2012-09-29 11:29:50 -04005072 phba->cfg_fcp_io_channel), GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005073 if (!phba->sli4_hba.msix_entries) {
5074 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5075 "2573 Failed allocate memory for msi-x "
5076 "interrupt vector entries\n");
James Smart05580562011-05-24 11:40:48 -04005077 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04005078 goto out_free_fcp_eq_hdl;
5079 }
5080
James Smart912e3ac2011-05-24 11:42:11 -04005081 /*
5082 * Enable sr-iov virtual functions if supported and configured
5083 * through the module parameter.
5084 */
5085 if (phba->cfg_sriov_nr_virtfn > 0) {
5086 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
5087 phba->cfg_sriov_nr_virtfn);
5088 if (rc) {
5089 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5090 "3020 Requested number of SR-IOV "
5091 "virtual functions (%d) is not "
5092 "supported\n",
5093 phba->cfg_sriov_nr_virtfn);
5094 phba->cfg_sriov_nr_virtfn = 0;
5095 }
5096 }
5097
James Smart5248a742011-07-22 18:37:06 -04005098 return 0;
James Smartda0436e2009-05-22 14:51:39 -04005099
5100out_free_fcp_eq_hdl:
5101 kfree(phba->sli4_hba.fcp_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05005102out_free_fcf_rr_bmask:
5103 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04005104out_remove_rpi_hdrs:
5105 lpfc_sli4_remove_rpi_hdrs(phba);
5106out_free_active_sgl:
5107 lpfc_free_active_sgl(phba);
James Smartda0436e2009-05-22 14:51:39 -04005108out_destroy_cq_event_pool:
5109 lpfc_sli4_cq_event_pool_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04005110out_free_bsmbx:
5111 lpfc_destroy_bootstrap_mbox(phba);
5112out_free_mem:
5113 lpfc_mem_free(phba);
5114 return rc;
5115}
5116
5117/**
5118 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
5119 * @phba: pointer to lpfc hba data structure.
5120 *
5121 * This routine is invoked to unset the driver internal resources set up
5122 * specific for supporting the SLI-4 HBA device it attached to.
5123 **/
5124static void
5125lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
5126{
5127 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
5128
James Smartda0436e2009-05-22 14:51:39 -04005129 /* Free memory allocated for msi-x interrupt vector entries */
5130 kfree(phba->sli4_hba.msix_entries);
5131
5132 /* Free memory allocated for fast-path work queue handles */
5133 kfree(phba->sli4_hba.fcp_eq_hdl);
5134
5135 /* Free the allocated rpi headers. */
5136 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04005137 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04005138
James Smart0c9ab6f2010-02-26 14:15:57 -05005139 /* Free eligible FCF index bmask */
5140 kfree(phba->fcf.fcf_rr_bmask);
5141
James Smartda0436e2009-05-22 14:51:39 -04005142 /* Free the ELS sgl list */
5143 lpfc_free_active_sgl(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04005144 lpfc_free_els_sgl_list(phba);
James Smartda0436e2009-05-22 14:51:39 -04005145
James Smartda0436e2009-05-22 14:51:39 -04005146 /* Free the completion queue EQ event pool */
5147 lpfc_sli4_cq_event_release_all(phba);
5148 lpfc_sli4_cq_event_pool_destroy(phba);
5149
James Smart6d368e52011-05-24 11:44:12 -04005150 /* Release resource identifiers. */
5151 lpfc_sli4_dealloc_resource_identifiers(phba);
5152
James Smartda0436e2009-05-22 14:51:39 -04005153 /* Free the bsmbx region. */
5154 lpfc_destroy_bootstrap_mbox(phba);
5155
5156 /* Free the SLI Layer memory with SLI4 HBAs */
5157 lpfc_mem_free_all(phba);
5158
5159 /* Free the current connect table */
5160 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04005161 &phba->fcf_conn_rec_list, list) {
5162 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04005163 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04005164 }
James Smartda0436e2009-05-22 14:51:39 -04005165
5166 return;
5167}
5168
5169/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005170 * lpfc_init_api_table_setup - Set up init api function jump table
James Smart3772a992009-05-22 14:50:54 -04005171 * @phba: The hba struct for which this call is being executed.
5172 * @dev_grp: The HBA PCI-Device group number.
5173 *
5174 * This routine sets up the device INIT interface API function jump table
5175 * in @phba struct.
5176 *
5177 * Returns: 0 - success, -ENODEV - failure.
5178 **/
5179int
5180lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5181{
James Smart84d1b002010-02-12 14:42:33 -05005182 phba->lpfc_hba_init_link = lpfc_hba_init_link;
5183 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart7f860592011-03-11 16:05:52 -05005184 phba->lpfc_selective_reset = lpfc_selective_reset;
James Smart3772a992009-05-22 14:50:54 -04005185 switch (dev_grp) {
5186 case LPFC_PCI_DEV_LP:
5187 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
5188 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
5189 phba->lpfc_stop_port = lpfc_stop_port_s3;
5190 break;
James Smartda0436e2009-05-22 14:51:39 -04005191 case LPFC_PCI_DEV_OC:
5192 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
5193 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
5194 phba->lpfc_stop_port = lpfc_stop_port_s4;
5195 break;
James Smart3772a992009-05-22 14:50:54 -04005196 default:
5197 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5198 "1431 Invalid HBA PCI-device group: 0x%x\n",
5199 dev_grp);
5200 return -ENODEV;
5201 break;
5202 }
5203 return 0;
5204}
5205
5206/**
5207 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
5208 * @phba: pointer to lpfc hba data structure.
5209 *
5210 * This routine is invoked to set up the driver internal resources before the
5211 * device specific resource setup to support the HBA device it attached to.
5212 *
5213 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005214 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005215 * other values - error
5216 **/
5217static int
5218lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
5219{
5220 /*
5221 * Driver resources common to all SLI revisions
5222 */
James Smartea2151b2008-09-07 11:52:10 -04005223 atomic_set(&phba->fast_event_count, 0);
James Smart2e0fef82007-06-17 19:56:36 -05005224 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005225
James Smarte47c9092008-02-08 18:49:26 -05005226 /* Initialize ndlp management spinlock */
5227 spin_lock_init(&phba->ndlp_lock);
5228
James Smart2e0fef82007-06-17 19:56:36 -05005229 INIT_LIST_HEAD(&phba->port_list);
James Smart3772a992009-05-22 14:50:54 -04005230 INIT_LIST_HEAD(&phba->work_list);
James Smart84774a42008-08-24 21:50:06 -04005231 init_waitqueue_head(&phba->wait_4_mlo_m_q);
dea31012005-04-17 16:05:31 -05005232
James Smart3772a992009-05-22 14:50:54 -04005233 /* Initialize the wait queue head for the kernel thread */
5234 init_waitqueue_head(&phba->work_waitq);
James Smart858c9f62007-06-17 19:56:39 -05005235
James Smart3772a992009-05-22 14:50:54 -04005236 /* Initialize the scsi buffer list used by driver for scsi IO */
5237 spin_lock_init(&phba->scsi_buf_list_lock);
5238 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
dea31012005-04-17 16:05:31 -05005239
James Smart3772a992009-05-22 14:50:54 -04005240 /* Initialize the fabric iocb list */
5241 INIT_LIST_HEAD(&phba->fabric_iocb_list);
dea31012005-04-17 16:05:31 -05005242
James Smart3772a992009-05-22 14:50:54 -04005243 /* Initialize list to save ELS buffers */
5244 INIT_LIST_HEAD(&phba->elsbuf);
dea31012005-04-17 16:05:31 -05005245
James Smart3772a992009-05-22 14:50:54 -04005246 /* Initialize FCF connection rec list */
5247 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
dea31012005-04-17 16:05:31 -05005248
James Smart3772a992009-05-22 14:50:54 -04005249 return 0;
5250}
dea31012005-04-17 16:05:31 -05005251
James Smart3772a992009-05-22 14:50:54 -04005252/**
5253 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
5254 * @phba: pointer to lpfc hba data structure.
5255 *
5256 * This routine is invoked to set up the driver internal resources after the
5257 * device specific resource setup to support the HBA device it attached to.
5258 *
5259 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005260 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005261 * other values - error
5262 **/
5263static int
5264lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
5265{
5266 int error;
5267
5268 /* Startup the kernel thread for this host adapter. */
5269 phba->worker_thread = kthread_run(lpfc_do_work, phba,
5270 "lpfc_worker_%d", phba->brd_no);
5271 if (IS_ERR(phba->worker_thread)) {
5272 error = PTR_ERR(phba->worker_thread);
5273 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005274 }
5275
James Smart3772a992009-05-22 14:50:54 -04005276 return 0;
5277}
5278
5279/**
5280 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
5281 * @phba: pointer to lpfc hba data structure.
5282 *
5283 * This routine is invoked to unset the driver internal resources set up after
5284 * the device specific resource setup for supporting the HBA device it
5285 * attached to.
5286 **/
5287static void
5288lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
5289{
5290 /* Stop kernel worker thread */
5291 kthread_stop(phba->worker_thread);
5292}
5293
5294/**
5295 * lpfc_free_iocb_list - Free iocb list.
5296 * @phba: pointer to lpfc hba data structure.
5297 *
5298 * This routine is invoked to free the driver's IOCB list and memory.
5299 **/
5300static void
5301lpfc_free_iocb_list(struct lpfc_hba *phba)
5302{
5303 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
5304
5305 spin_lock_irq(&phba->hbalock);
5306 list_for_each_entry_safe(iocbq_entry, iocbq_next,
5307 &phba->lpfc_iocb_list, list) {
5308 list_del(&iocbq_entry->list);
5309 kfree(iocbq_entry);
5310 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005311 }
James Smart3772a992009-05-22 14:50:54 -04005312 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005313
James Smart3772a992009-05-22 14:50:54 -04005314 return;
5315}
dea31012005-04-17 16:05:31 -05005316
James Smart3772a992009-05-22 14:50:54 -04005317/**
5318 * lpfc_init_iocb_list - Allocate and initialize iocb list.
5319 * @phba: pointer to lpfc hba data structure.
5320 *
5321 * This routine is invoked to allocate and initizlize the driver's IOCB
5322 * list and set up the IOCB tag array accordingly.
5323 *
5324 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005325 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005326 * other values - error
5327 **/
5328static int
5329lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
5330{
5331 struct lpfc_iocbq *iocbq_entry = NULL;
5332 uint16_t iotag;
5333 int i;
dea31012005-04-17 16:05:31 -05005334
5335 /* Initialize and populate the iocb list per host. */
5336 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04005337 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07005338 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05005339 if (iocbq_entry == NULL) {
5340 printk(KERN_ERR "%s: only allocated %d iocbs of "
5341 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07005342 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05005343 goto out_free_iocbq;
5344 }
5345
James Bottomley604a3e32005-10-29 10:28:33 -05005346 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
5347 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04005348 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05005349 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04005350 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05005351 goto out_free_iocbq;
5352 }
James Smart6d368e52011-05-24 11:44:12 -04005353 iocbq_entry->sli4_lxritag = NO_XRI;
James Smart3772a992009-05-22 14:50:54 -04005354 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05005355
5356 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005357 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
5358 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05005359 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005360 }
5361
James Smart3772a992009-05-22 14:50:54 -04005362 return 0;
5363
5364out_free_iocbq:
5365 lpfc_free_iocb_list(phba);
5366
5367 return -ENOMEM;
5368}
5369
5370/**
James Smart8a9d2e82012-05-09 21:16:12 -04005371 * lpfc_free_sgl_list - Free a given sgl list.
James Smartda0436e2009-05-22 14:51:39 -04005372 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -04005373 * @sglq_list: pointer to the head of sgl list.
James Smartda0436e2009-05-22 14:51:39 -04005374 *
James Smart8a9d2e82012-05-09 21:16:12 -04005375 * This routine is invoked to free a give sgl list and memory.
James Smartda0436e2009-05-22 14:51:39 -04005376 **/
James Smart8a9d2e82012-05-09 21:16:12 -04005377void
5378lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
James Smartda0436e2009-05-22 14:51:39 -04005379{
5380 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart8a9d2e82012-05-09 21:16:12 -04005381
5382 list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
5383 list_del(&sglq_entry->list);
5384 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
5385 kfree(sglq_entry);
5386 }
5387}
5388
5389/**
5390 * lpfc_free_els_sgl_list - Free els sgl list.
5391 * @phba: pointer to lpfc hba data structure.
5392 *
5393 * This routine is invoked to free the driver's els sgl list and memory.
5394 **/
5395static void
5396lpfc_free_els_sgl_list(struct lpfc_hba *phba)
5397{
James Smartda0436e2009-05-22 14:51:39 -04005398 LIST_HEAD(sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04005399
James Smart8a9d2e82012-05-09 21:16:12 -04005400 /* Retrieve all els sgls from driver list */
James Smartda0436e2009-05-22 14:51:39 -04005401 spin_lock_irq(&phba->hbalock);
5402 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
5403 spin_unlock_irq(&phba->hbalock);
5404
James Smart8a9d2e82012-05-09 21:16:12 -04005405 /* Now free the sgl list */
5406 lpfc_free_sgl_list(phba, &sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04005407}
5408
5409/**
5410 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
5411 * @phba: pointer to lpfc hba data structure.
5412 *
5413 * This routine is invoked to allocate the driver's active sgl memory.
5414 * This array will hold the sglq_entry's for active IOs.
5415 **/
5416static int
5417lpfc_init_active_sgl_array(struct lpfc_hba *phba)
5418{
5419 int size;
5420 size = sizeof(struct lpfc_sglq *);
5421 size *= phba->sli4_hba.max_cfg_param.max_xri;
5422
5423 phba->sli4_hba.lpfc_sglq_active_list =
5424 kzalloc(size, GFP_KERNEL);
5425 if (!phba->sli4_hba.lpfc_sglq_active_list)
5426 return -ENOMEM;
5427 return 0;
5428}
5429
5430/**
5431 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
5432 * @phba: pointer to lpfc hba data structure.
5433 *
5434 * This routine is invoked to walk through the array of active sglq entries
5435 * and free all of the resources.
5436 * This is just a place holder for now.
5437 **/
5438static void
5439lpfc_free_active_sgl(struct lpfc_hba *phba)
5440{
5441 kfree(phba->sli4_hba.lpfc_sglq_active_list);
5442}
5443
5444/**
5445 * lpfc_init_sgl_list - Allocate and initialize sgl list.
5446 * @phba: pointer to lpfc hba data structure.
5447 *
5448 * This routine is invoked to allocate and initizlize the driver's sgl
5449 * list and set up the sgl xritag tag array accordingly.
5450 *
James Smartda0436e2009-05-22 14:51:39 -04005451 **/
James Smart8a9d2e82012-05-09 21:16:12 -04005452static void
James Smartda0436e2009-05-22 14:51:39 -04005453lpfc_init_sgl_list(struct lpfc_hba *phba)
5454{
James Smartda0436e2009-05-22 14:51:39 -04005455 /* Initialize and populate the sglq list per host/VF. */
5456 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
5457 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
5458
James Smart8a9d2e82012-05-09 21:16:12 -04005459 /* els xri-sgl book keeping */
5460 phba->sli4_hba.els_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04005461
James Smart8a9d2e82012-05-09 21:16:12 -04005462 /* scsi xri-buffer book keeping */
James Smartda0436e2009-05-22 14:51:39 -04005463 phba->sli4_hba.scsi_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04005464}
5465
5466/**
5467 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
5468 * @phba: pointer to lpfc hba data structure.
5469 *
5470 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -04005471 * port for those SLI4 ports that do not support extents. This routine
James Smartda0436e2009-05-22 14:51:39 -04005472 * posts a PAGE_SIZE memory region to the port to hold up to
James Smart88a2cfb2011-07-22 18:36:33 -04005473 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
5474 * and should be called only when interrupts are disabled.
James Smartda0436e2009-05-22 14:51:39 -04005475 *
5476 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005477 * 0 - successful
James Smart88a2cfb2011-07-22 18:36:33 -04005478 * -ERROR - otherwise.
James Smartda0436e2009-05-22 14:51:39 -04005479 **/
5480int
5481lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
5482{
5483 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04005484 struct lpfc_rpi_hdr *rpi_hdr;
5485
5486 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
James Smartff78d8f2011-12-13 13:21:35 -05005487 if (!phba->sli4_hba.rpi_hdrs_in_use)
James Smart6d368e52011-05-24 11:44:12 -04005488 return rc;
James Smart6d368e52011-05-24 11:44:12 -04005489 if (phba->sli4_hba.extents_in_use)
5490 return -EIO;
James Smartda0436e2009-05-22 14:51:39 -04005491
5492 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
5493 if (!rpi_hdr) {
5494 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5495 "0391 Error during rpi post operation\n");
5496 lpfc_sli4_remove_rpis(phba);
5497 rc = -ENODEV;
5498 }
5499
5500 return rc;
5501}
5502
5503/**
5504 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
5505 * @phba: pointer to lpfc hba data structure.
5506 *
5507 * This routine is invoked to allocate a single 4KB memory region to
5508 * support rpis and stores them in the phba. This single region
5509 * provides support for up to 64 rpis. The region is used globally
5510 * by the device.
5511 *
5512 * Returns:
5513 * A valid rpi hdr on success.
5514 * A NULL pointer on any failure.
5515 **/
5516struct lpfc_rpi_hdr *
5517lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
5518{
5519 uint16_t rpi_limit, curr_rpi_range;
5520 struct lpfc_dmabuf *dmabuf;
5521 struct lpfc_rpi_hdr *rpi_hdr;
James Smart9589b0622011-04-16 11:03:17 -04005522 uint32_t rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04005523
James Smart6d368e52011-05-24 11:44:12 -04005524 /*
5525 * If the SLI4 port supports extents, posting the rpi header isn't
5526 * required. Set the expected maximum count and let the actual value
5527 * get set when extents are fully allocated.
5528 */
5529 if (!phba->sli4_hba.rpi_hdrs_in_use)
5530 return NULL;
5531 if (phba->sli4_hba.extents_in_use)
5532 return NULL;
5533
5534 /* The limit on the logical index is just the max_rpi count. */
James Smartda0436e2009-05-22 14:51:39 -04005535 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
James Smart6d368e52011-05-24 11:44:12 -04005536 phba->sli4_hba.max_cfg_param.max_rpi - 1;
James Smartda0436e2009-05-22 14:51:39 -04005537
5538 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04005539 /*
5540 * Establish the starting RPI in this header block. The starting
5541 * rpi is normalized to a zero base because the physical rpi is
5542 * port based.
5543 */
James Smart97f2ecf2012-03-01 22:35:23 -05005544 curr_rpi_range = phba->sli4_hba.next_rpi;
James Smartda0436e2009-05-22 14:51:39 -04005545 spin_unlock_irq(&phba->hbalock);
5546
5547 /*
5548 * The port has a limited number of rpis. The increment here
5549 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
5550 * and to allow the full max_rpi range per port.
5551 */
5552 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
James Smart9589b0622011-04-16 11:03:17 -04005553 rpi_count = rpi_limit - curr_rpi_range;
5554 else
5555 rpi_count = LPFC_RPI_HDR_COUNT;
James Smartda0436e2009-05-22 14:51:39 -04005556
James Smart6d368e52011-05-24 11:44:12 -04005557 if (!rpi_count)
5558 return NULL;
James Smartda0436e2009-05-22 14:51:39 -04005559 /*
5560 * First allocate the protocol header region for the port. The
5561 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
5562 */
5563 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5564 if (!dmabuf)
5565 return NULL;
5566
5567 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5568 LPFC_HDR_TEMPLATE_SIZE,
5569 &dmabuf->phys,
5570 GFP_KERNEL);
5571 if (!dmabuf->virt) {
5572 rpi_hdr = NULL;
5573 goto err_free_dmabuf;
5574 }
5575
5576 memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
5577 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
5578 rpi_hdr = NULL;
5579 goto err_free_coherent;
5580 }
5581
5582 /* Save the rpi header data for cleanup later. */
5583 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
5584 if (!rpi_hdr)
5585 goto err_free_coherent;
5586
5587 rpi_hdr->dmabuf = dmabuf;
5588 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
5589 rpi_hdr->page_count = 1;
5590 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04005591
5592 /* The rpi_hdr stores the logical index only. */
5593 rpi_hdr->start_rpi = curr_rpi_range;
James Smartda0436e2009-05-22 14:51:39 -04005594 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
5595
5596 /*
James Smart6d368e52011-05-24 11:44:12 -04005597 * The next_rpi stores the next logical module-64 rpi value used
5598 * to post physical rpis in subsequent rpi postings.
James Smartda0436e2009-05-22 14:51:39 -04005599 */
James Smart9589b0622011-04-16 11:03:17 -04005600 phba->sli4_hba.next_rpi += rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04005601 spin_unlock_irq(&phba->hbalock);
5602 return rpi_hdr;
5603
5604 err_free_coherent:
5605 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
5606 dmabuf->virt, dmabuf->phys);
5607 err_free_dmabuf:
5608 kfree(dmabuf);
5609 return NULL;
5610}
5611
5612/**
5613 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
5614 * @phba: pointer to lpfc hba data structure.
5615 *
5616 * This routine is invoked to remove all memory resources allocated
James Smart6d368e52011-05-24 11:44:12 -04005617 * to support rpis for SLI4 ports not supporting extents. This routine
5618 * presumes the caller has released all rpis consumed by fabric or port
5619 * logins and is prepared to have the header pages removed.
James Smartda0436e2009-05-22 14:51:39 -04005620 **/
5621void
5622lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
5623{
5624 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
5625
James Smart6d368e52011-05-24 11:44:12 -04005626 if (!phba->sli4_hba.rpi_hdrs_in_use)
5627 goto exit;
5628
James Smartda0436e2009-05-22 14:51:39 -04005629 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
5630 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
5631 list_del(&rpi_hdr->list);
5632 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
5633 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
5634 kfree(rpi_hdr->dmabuf);
5635 kfree(rpi_hdr);
5636 }
James Smart6d368e52011-05-24 11:44:12 -04005637 exit:
5638 /* There are no rpis available to the port now. */
5639 phba->sli4_hba.next_rpi = 0;
James Smartda0436e2009-05-22 14:51:39 -04005640}
5641
5642/**
James Smart3772a992009-05-22 14:50:54 -04005643 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
5644 * @pdev: pointer to pci device data structure.
5645 *
5646 * This routine is invoked to allocate the driver hba data structure for an
5647 * HBA device. If the allocation is successful, the phba reference to the
5648 * PCI device data structure is set.
5649 *
5650 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005651 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04005652 * NULL - error
5653 **/
5654static struct lpfc_hba *
5655lpfc_hba_alloc(struct pci_dev *pdev)
5656{
5657 struct lpfc_hba *phba;
5658
5659 /* Allocate memory for HBA structure */
5660 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
5661 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02005662 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04005663 return NULL;
5664 }
5665
5666 /* Set reference to PCI device in HBA structure */
5667 phba->pcidev = pdev;
5668
5669 /* Assign an unused board number */
5670 phba->brd_no = lpfc_get_instance();
5671 if (phba->brd_no < 0) {
5672 kfree(phba);
5673 return NULL;
5674 }
5675
James Smart4fede782010-01-26 23:08:55 -05005676 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04005677 INIT_LIST_HEAD(&phba->ct_ev_waiters);
5678
James Smart3772a992009-05-22 14:50:54 -04005679 return phba;
5680}
5681
5682/**
5683 * lpfc_hba_free - Free driver hba data structure with a device.
5684 * @phba: pointer to lpfc hba data structure.
5685 *
5686 * This routine is invoked to free the driver hba data structure with an
5687 * HBA device.
5688 **/
5689static void
5690lpfc_hba_free(struct lpfc_hba *phba)
5691{
5692 /* Release the driver assigned board number */
5693 idr_remove(&lpfc_hba_index, phba->brd_no);
5694
James Smart2a76a282012-08-03 12:35:54 -04005695 /* Free memory allocated with sli rings */
5696 kfree(phba->sli.ring);
5697 phba->sli.ring = NULL;
5698
James Smart3772a992009-05-22 14:50:54 -04005699 kfree(phba);
5700 return;
5701}
5702
5703/**
5704 * lpfc_create_shost - Create hba physical port with associated scsi host.
5705 * @phba: pointer to lpfc hba data structure.
5706 *
5707 * This routine is invoked to create HBA physical port and associate a SCSI
5708 * host with it.
5709 *
5710 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005711 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005712 * other values - error
5713 **/
5714static int
5715lpfc_create_shost(struct lpfc_hba *phba)
5716{
5717 struct lpfc_vport *vport;
5718 struct Scsi_Host *shost;
5719
5720 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05005721 phba->fc_edtov = FF_DEF_EDTOV;
5722 phba->fc_ratov = FF_DEF_RATOV;
5723 phba->fc_altov = FF_DEF_ALTOV;
5724 phba->fc_arbtov = FF_DEF_ARBTOV;
5725
James Smartd7c47992010-06-08 18:31:54 -04005726 atomic_set(&phba->sdev_cnt, 0);
James Smart3de2a652007-08-02 11:09:59 -04005727 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05005728 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04005729 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05005730
5731 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05005732 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05005733 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04005734 /* Put reference to SCSI host to driver's device private data */
5735 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05005736
James Smart3772a992009-05-22 14:50:54 -04005737 return 0;
5738}
dea31012005-04-17 16:05:31 -05005739
James Smart3772a992009-05-22 14:50:54 -04005740/**
5741 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
5742 * @phba: pointer to lpfc hba data structure.
5743 *
5744 * This routine is invoked to destroy HBA physical port and the associated
5745 * SCSI host.
5746 **/
5747static void
5748lpfc_destroy_shost(struct lpfc_hba *phba)
5749{
5750 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04005751
James Smart3772a992009-05-22 14:50:54 -04005752 /* Destroy physical port that associated with the SCSI host */
5753 destroy_port(vport);
5754
5755 return;
5756}
5757
5758/**
5759 * lpfc_setup_bg - Setup Block guard structures and debug areas.
5760 * @phba: pointer to lpfc hba data structure.
5761 * @shost: the shost to be used to detect Block guard settings.
5762 *
5763 * This routine sets up the local Block guard protocol settings for @shost.
5764 * This routine also allocates memory for debugging bg buffers.
5765 **/
5766static void
5767lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
5768{
James Smartbbeb79b2012-06-12 13:54:27 -04005769 uint32_t old_mask;
5770 uint32_t old_guard;
5771
James Smart3772a992009-05-22 14:50:54 -04005772 int pagecnt = 10;
5773 if (lpfc_prot_mask && lpfc_prot_guard) {
5774 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5775 "1478 Registering BlockGuard with the "
5776 "SCSI layer\n");
James Smartbbeb79b2012-06-12 13:54:27 -04005777
5778 old_mask = lpfc_prot_mask;
5779 old_guard = lpfc_prot_guard;
5780
5781 /* Only allow supported values */
5782 lpfc_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
5783 SHOST_DIX_TYPE0_PROTECTION |
5784 SHOST_DIX_TYPE1_PROTECTION);
5785 lpfc_prot_guard &= (SHOST_DIX_GUARD_IP | SHOST_DIX_GUARD_CRC);
5786
5787 /* DIF Type 1 protection for profiles AST1/C1 is end to end */
5788 if (lpfc_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
5789 lpfc_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
5790
5791 if (lpfc_prot_mask && lpfc_prot_guard) {
5792 if ((old_mask != lpfc_prot_mask) ||
5793 (old_guard != lpfc_prot_guard))
5794 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5795 "1475 Registering BlockGuard with the "
5796 "SCSI layer: mask %d guard %d\n",
5797 lpfc_prot_mask, lpfc_prot_guard);
5798
5799 scsi_host_set_prot(shost, lpfc_prot_mask);
5800 scsi_host_set_guard(shost, lpfc_prot_guard);
5801 } else
5802 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5803 "1479 Not Registering BlockGuard with the SCSI "
5804 "layer, Bad protection parameters: %d %d\n",
5805 old_mask, old_guard);
James Smart98c9ea52007-10-27 13:37:33 -04005806 }
James Smartbbeb79b2012-06-12 13:54:27 -04005807
James Smart81301a92008-12-04 22:39:46 -05005808 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05005809 while (pagecnt) {
5810 spin_lock_init(&_dump_buf_lock);
5811 _dump_buf_data =
5812 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
5813 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04005814 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5815 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04005816 "_dump_buf_data at 0x%p\n",
5817 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05005818 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04005819 memset(_dump_buf_data, 0,
5820 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05005821 break;
James Smart3772a992009-05-22 14:50:54 -04005822 } else
James Smart81301a92008-12-04 22:39:46 -05005823 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05005824 }
James Smart81301a92008-12-04 22:39:46 -05005825 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04005826 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5827 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04005828 "memory for hexdump\n");
5829 } else
James Smart6a9c52c2009-10-02 15:16:51 -04005830 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5831 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05005832 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05005833 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05005834 while (pagecnt) {
5835 _dump_buf_dif =
5836 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
5837 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04005838 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5839 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04005840 "_dump_buf_dif at 0x%p\n",
5841 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05005842 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04005843 memset(_dump_buf_dif, 0,
5844 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05005845 break;
James Smart3772a992009-05-22 14:50:54 -04005846 } else
James Smart81301a92008-12-04 22:39:46 -05005847 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05005848 }
James Smart81301a92008-12-04 22:39:46 -05005849 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04005850 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5851 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04005852 "memory for hexdump\n");
5853 } else
James Smart6a9c52c2009-10-02 15:16:51 -04005854 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5855 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04005856 _dump_buf_dif);
5857}
5858
5859/**
5860 * lpfc_post_init_setup - Perform necessary device post initialization setup.
5861 * @phba: pointer to lpfc hba data structure.
5862 *
5863 * This routine is invoked to perform all the necessary post initialization
5864 * setup for the device.
5865 **/
5866static void
5867lpfc_post_init_setup(struct lpfc_hba *phba)
5868{
5869 struct Scsi_Host *shost;
5870 struct lpfc_adapter_event_header adapter_event;
5871
5872 /* Get the default values for Model Name and Description */
5873 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
5874
5875 /*
5876 * hba setup may have changed the hba_queue_depth so we need to
5877 * adjust the value of can_queue.
5878 */
5879 shost = pci_get_drvdata(phba->pcidev);
5880 shost->can_queue = phba->cfg_hba_queue_depth - 10;
5881 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
5882 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05005883
5884 lpfc_host_attrib_init(shost);
5885
James Smart2e0fef82007-06-17 19:56:36 -05005886 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
5887 spin_lock_irq(shost->host_lock);
5888 lpfc_poll_start_timer(phba);
5889 spin_unlock_irq(shost->host_lock);
5890 }
James Smart8f6d98d2006-08-01 07:34:00 -04005891
James Smart93996272008-08-24 21:50:30 -04005892 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5893 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04005894 /* Send board arrival event to upper layer */
5895 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
5896 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
5897 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04005898 sizeof(adapter_event),
5899 (char *) &adapter_event,
5900 LPFC_NL_VENDOR_ID);
5901 return;
5902}
5903
5904/**
5905 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
5906 * @phba: pointer to lpfc hba data structure.
5907 *
5908 * This routine is invoked to set up the PCI device memory space for device
5909 * with SLI-3 interface spec.
5910 *
5911 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005912 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005913 * other values - error
5914 **/
5915static int
5916lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
5917{
5918 struct pci_dev *pdev;
5919 unsigned long bar0map_len, bar2map_len;
5920 int i, hbq_count;
5921 void *ptr;
5922 int error = -ENODEV;
5923
5924 /* Obtain PCI device reference */
5925 if (!phba->pcidev)
5926 return error;
5927 else
5928 pdev = phba->pcidev;
5929
5930 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05005931 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
5932 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
5933 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
5934 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smart3772a992009-05-22 14:50:54 -04005935 return error;
Michael Reed8e685972009-09-18 12:02:05 -05005936 }
5937 }
James Smart3772a992009-05-22 14:50:54 -04005938
5939 /* Get the bus address of Bar0 and Bar2 and the number of bytes
5940 * required by each mapping.
5941 */
5942 phba->pci_bar0_map = pci_resource_start(pdev, 0);
5943 bar0map_len = pci_resource_len(pdev, 0);
5944
5945 phba->pci_bar2_map = pci_resource_start(pdev, 2);
5946 bar2map_len = pci_resource_len(pdev, 2);
5947
5948 /* Map HBA SLIM to a kernel virtual address. */
5949 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
5950 if (!phba->slim_memmap_p) {
5951 dev_printk(KERN_ERR, &pdev->dev,
5952 "ioremap failed for SLIM memory.\n");
5953 goto out;
5954 }
5955
5956 /* Map HBA Control Registers to a kernel virtual address. */
5957 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
5958 if (!phba->ctrl_regs_memmap_p) {
5959 dev_printk(KERN_ERR, &pdev->dev,
5960 "ioremap failed for HBA control registers.\n");
5961 goto out_iounmap_slim;
5962 }
5963
5964 /* Allocate memory for SLI-2 structures */
5965 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
5966 SLI2_SLIM_SIZE,
5967 &phba->slim2p.phys,
5968 GFP_KERNEL);
5969 if (!phba->slim2p.virt)
5970 goto out_iounmap;
5971
5972 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
5973 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04005974 phba->mbox_ext = (phba->slim2p.virt +
5975 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04005976 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
5977 phba->IOCBs = (phba->slim2p.virt +
5978 offsetof(struct lpfc_sli2_slim, IOCBs));
5979
5980 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
5981 lpfc_sli_hbq_size(),
5982 &phba->hbqslimp.phys,
5983 GFP_KERNEL);
5984 if (!phba->hbqslimp.virt)
5985 goto out_free_slim;
5986
5987 hbq_count = lpfc_sli_hbq_count();
5988 ptr = phba->hbqslimp.virt;
5989 for (i = 0; i < hbq_count; ++i) {
5990 phba->hbqs[i].hbq_virt = ptr;
5991 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
5992 ptr += (lpfc_hbq_defs[i]->entry_count *
5993 sizeof(struct lpfc_hbq_entry));
5994 }
5995 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
5996 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
5997
5998 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
5999
6000 INIT_LIST_HEAD(&phba->rb_pend_list);
6001
6002 phba->MBslimaddr = phba->slim_memmap_p;
6003 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
6004 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
6005 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
6006 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04006007
dea31012005-04-17 16:05:31 -05006008 return 0;
6009
dea31012005-04-17 16:05:31 -05006010out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04006011 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6012 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05006013out_iounmap:
6014 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05006015out_iounmap_slim:
dea31012005-04-17 16:05:31 -05006016 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05006017out:
6018 return error;
6019}
6020
James Smarte59058c2008-08-24 21:49:00 -04006021/**
James Smart3772a992009-05-22 14:50:54 -04006022 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
6023 * @phba: pointer to lpfc hba data structure.
6024 *
6025 * This routine is invoked to unset the PCI device memory space for device
6026 * with SLI-3 interface spec.
6027 **/
6028static void
6029lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
6030{
6031 struct pci_dev *pdev;
6032
6033 /* Obtain PCI device reference */
6034 if (!phba->pcidev)
6035 return;
6036 else
6037 pdev = phba->pcidev;
6038
6039 /* Free coherent DMA memory allocated */
6040 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
6041 phba->hbqslimp.virt, phba->hbqslimp.phys);
6042 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6043 phba->slim2p.virt, phba->slim2p.phys);
6044
6045 /* I/O memory unmap */
6046 iounmap(phba->ctrl_regs_memmap_p);
6047 iounmap(phba->slim_memmap_p);
6048
6049 return;
6050}
6051
6052/**
James Smartda0436e2009-05-22 14:51:39 -04006053 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
6054 * @phba: pointer to lpfc hba data structure.
6055 *
6056 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
6057 * done and check status.
6058 *
6059 * Return 0 if successful, otherwise -ENODEV.
6060 **/
6061int
6062lpfc_sli4_post_status_check(struct lpfc_hba *phba)
6063{
James Smart2fcee4b2010-12-15 17:57:46 -05006064 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
6065 struct lpfc_register reg_data;
6066 int i, port_error = 0;
6067 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04006068
James Smart9940b972011-03-11 16:06:12 -05006069 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
6070 memset(&reg_data, 0, sizeof(reg_data));
James Smart2fcee4b2010-12-15 17:57:46 -05006071 if (!phba->sli4_hba.PSMPHRregaddr)
James Smartda0436e2009-05-22 14:51:39 -04006072 return -ENODEV;
6073
James Smartda0436e2009-05-22 14:51:39 -04006074 /* Wait up to 30 seconds for the SLI Port POST done and ready */
6075 for (i = 0; i < 3000; i++) {
James Smart9940b972011-03-11 16:06:12 -05006076 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
6077 &portsmphr_reg.word0) ||
6078 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
James Smart2fcee4b2010-12-15 17:57:46 -05006079 /* Port has a fatal POST error, break out */
James Smartda0436e2009-05-22 14:51:39 -04006080 port_error = -ENODEV;
6081 break;
6082 }
James Smart2fcee4b2010-12-15 17:57:46 -05006083 if (LPFC_POST_STAGE_PORT_READY ==
6084 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
James Smartda0436e2009-05-22 14:51:39 -04006085 break;
James Smartda0436e2009-05-22 14:51:39 -04006086 msleep(10);
6087 }
6088
James Smart2fcee4b2010-12-15 17:57:46 -05006089 /*
6090 * If there was a port error during POST, then don't proceed with
6091 * other register reads as the data may not be valid. Just exit.
6092 */
6093 if (port_error) {
James Smartda0436e2009-05-22 14:51:39 -04006094 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05006095 "1408 Port Failed POST - portsmphr=0x%x, "
6096 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
6097 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
6098 portsmphr_reg.word0,
6099 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
6100 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
6101 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
6102 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
6103 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
6104 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
6105 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
6106 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
6107 } else {
James Smart28baac72010-02-12 14:42:03 -05006108 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05006109 "2534 Device Info: SLIFamily=0x%x, "
6110 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
6111 "SLIHint_2=0x%x, FT=0x%x\n",
James Smart28baac72010-02-12 14:42:03 -05006112 bf_get(lpfc_sli_intf_sli_family,
6113 &phba->sli4_hba.sli_intf),
6114 bf_get(lpfc_sli_intf_slirev,
6115 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05006116 bf_get(lpfc_sli_intf_if_type,
James Smart28baac72010-02-12 14:42:03 -05006117 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05006118 bf_get(lpfc_sli_intf_sli_hint1,
6119 &phba->sli4_hba.sli_intf),
6120 bf_get(lpfc_sli_intf_sli_hint2,
6121 &phba->sli4_hba.sli_intf),
6122 bf_get(lpfc_sli_intf_func_type,
James Smart28baac72010-02-12 14:42:03 -05006123 &phba->sli4_hba.sli_intf));
James Smart2fcee4b2010-12-15 17:57:46 -05006124 /*
6125 * Check for other Port errors during the initialization
6126 * process. Fail the load if the port did not come up
6127 * correctly.
6128 */
6129 if_type = bf_get(lpfc_sli_intf_if_type,
6130 &phba->sli4_hba.sli_intf);
6131 switch (if_type) {
6132 case LPFC_SLI_INTF_IF_TYPE_0:
6133 phba->sli4_hba.ue_mask_lo =
6134 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
6135 phba->sli4_hba.ue_mask_hi =
6136 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
6137 uerrlo_reg.word0 =
6138 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
6139 uerrhi_reg.word0 =
6140 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
6141 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
6142 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
6143 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6144 "1422 Unrecoverable Error "
6145 "Detected during POST "
6146 "uerr_lo_reg=0x%x, "
6147 "uerr_hi_reg=0x%x, "
6148 "ue_mask_lo_reg=0x%x, "
6149 "ue_mask_hi_reg=0x%x\n",
6150 uerrlo_reg.word0,
6151 uerrhi_reg.word0,
6152 phba->sli4_hba.ue_mask_lo,
6153 phba->sli4_hba.ue_mask_hi);
6154 port_error = -ENODEV;
6155 }
6156 break;
6157 case LPFC_SLI_INTF_IF_TYPE_2:
6158 /* Final checks. The port status should be clean. */
James Smart9940b972011-03-11 16:06:12 -05006159 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
6160 &reg_data.word0) ||
James Smart05580562011-05-24 11:40:48 -04006161 (bf_get(lpfc_sliport_status_err, &reg_data) &&
6162 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
James Smart2fcee4b2010-12-15 17:57:46 -05006163 phba->work_status[0] =
6164 readl(phba->sli4_hba.u.if_type2.
6165 ERR1regaddr);
6166 phba->work_status[1] =
6167 readl(phba->sli4_hba.u.if_type2.
6168 ERR2regaddr);
6169 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05006170 "2888 Unrecoverable port error "
6171 "following POST: port status reg "
6172 "0x%x, port_smphr reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05006173 "error 1=0x%x, error 2=0x%x\n",
6174 reg_data.word0,
6175 portsmphr_reg.word0,
6176 phba->work_status[0],
6177 phba->work_status[1]);
6178 port_error = -ENODEV;
6179 }
6180 break;
6181 case LPFC_SLI_INTF_IF_TYPE_1:
6182 default:
6183 break;
6184 }
James Smart28baac72010-02-12 14:42:03 -05006185 }
James Smartda0436e2009-05-22 14:51:39 -04006186 return port_error;
6187}
6188
6189/**
6190 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
6191 * @phba: pointer to lpfc hba data structure.
James Smart2fcee4b2010-12-15 17:57:46 -05006192 * @if_type: The SLI4 interface type getting configured.
James Smartda0436e2009-05-22 14:51:39 -04006193 *
6194 * This routine is invoked to set up SLI4 BAR0 PCI config space register
6195 * memory map.
6196 **/
6197static void
James Smart2fcee4b2010-12-15 17:57:46 -05006198lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04006199{
James Smart2fcee4b2010-12-15 17:57:46 -05006200 switch (if_type) {
6201 case LPFC_SLI_INTF_IF_TYPE_0:
6202 phba->sli4_hba.u.if_type0.UERRLOregaddr =
6203 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
6204 phba->sli4_hba.u.if_type0.UERRHIregaddr =
6205 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
6206 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
6207 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
6208 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
6209 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
6210 phba->sli4_hba.SLIINTFregaddr =
6211 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
6212 break;
6213 case LPFC_SLI_INTF_IF_TYPE_2:
6214 phba->sli4_hba.u.if_type2.ERR1regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006215 phba->sli4_hba.conf_regs_memmap_p +
6216 LPFC_CTL_PORT_ER1_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006217 phba->sli4_hba.u.if_type2.ERR2regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006218 phba->sli4_hba.conf_regs_memmap_p +
6219 LPFC_CTL_PORT_ER2_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006220 phba->sli4_hba.u.if_type2.CTRLregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006221 phba->sli4_hba.conf_regs_memmap_p +
6222 LPFC_CTL_PORT_CTL_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006223 phba->sli4_hba.u.if_type2.STATUSregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006224 phba->sli4_hba.conf_regs_memmap_p +
6225 LPFC_CTL_PORT_STA_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006226 phba->sli4_hba.SLIINTFregaddr =
6227 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
6228 phba->sli4_hba.PSMPHRregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006229 phba->sli4_hba.conf_regs_memmap_p +
6230 LPFC_CTL_PORT_SEM_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006231 phba->sli4_hba.RQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05006232 phba->sli4_hba.conf_regs_memmap_p +
6233 LPFC_ULP0_RQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05006234 phba->sli4_hba.WQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05006235 phba->sli4_hba.conf_regs_memmap_p +
6236 LPFC_ULP0_WQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05006237 phba->sli4_hba.EQCQDBregaddr =
6238 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
6239 phba->sli4_hba.MQDBregaddr =
6240 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
6241 phba->sli4_hba.BMBXregaddr =
6242 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
6243 break;
6244 case LPFC_SLI_INTF_IF_TYPE_1:
6245 default:
6246 dev_printk(KERN_ERR, &phba->pcidev->dev,
6247 "FATAL - unsupported SLI4 interface type - %d\n",
6248 if_type);
6249 break;
6250 }
James Smartda0436e2009-05-22 14:51:39 -04006251}
6252
6253/**
6254 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
6255 * @phba: pointer to lpfc hba data structure.
6256 *
6257 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
6258 * memory map.
6259 **/
6260static void
6261lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
6262{
James Smart2fcee4b2010-12-15 17:57:46 -05006263 phba->sli4_hba.PSMPHRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
6264 LPFC_SLIPORT_IF0_SMPHR;
James Smartda0436e2009-05-22 14:51:39 -04006265 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006266 LPFC_HST_ISR0;
James Smartda0436e2009-05-22 14:51:39 -04006267 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006268 LPFC_HST_IMR0;
James Smartda0436e2009-05-22 14:51:39 -04006269 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006270 LPFC_HST_ISCR0;
James Smartda0436e2009-05-22 14:51:39 -04006271}
6272
6273/**
6274 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
6275 * @phba: pointer to lpfc hba data structure.
6276 * @vf: virtual function number
6277 *
6278 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
6279 * based on the given viftual function number, @vf.
6280 *
6281 * Return 0 if successful, otherwise -ENODEV.
6282 **/
6283static int
6284lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
6285{
6286 if (vf > LPFC_VIR_FUNC_MAX)
6287 return -ENODEV;
6288
6289 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05006290 vf * LPFC_VFR_PAGE_SIZE +
6291 LPFC_ULP0_RQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04006292 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05006293 vf * LPFC_VFR_PAGE_SIZE +
6294 LPFC_ULP0_WQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04006295 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6296 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
6297 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6298 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
6299 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6300 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
6301 return 0;
6302}
6303
6304/**
6305 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
6306 * @phba: pointer to lpfc hba data structure.
6307 *
6308 * This routine is invoked to create the bootstrap mailbox
6309 * region consistent with the SLI-4 interface spec. This
6310 * routine allocates all memory necessary to communicate
6311 * mailbox commands to the port and sets up all alignment
6312 * needs. No locks are expected to be held when calling
6313 * this routine.
6314 *
6315 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006316 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04006317 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04006318 **/
6319static int
6320lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
6321{
6322 uint32_t bmbx_size;
6323 struct lpfc_dmabuf *dmabuf;
6324 struct dma_address *dma_address;
6325 uint32_t pa_addr;
6326 uint64_t phys_addr;
6327
6328 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
6329 if (!dmabuf)
6330 return -ENOMEM;
6331
6332 /*
6333 * The bootstrap mailbox region is comprised of 2 parts
6334 * plus an alignment restriction of 16 bytes.
6335 */
6336 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
6337 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
6338 bmbx_size,
6339 &dmabuf->phys,
6340 GFP_KERNEL);
6341 if (!dmabuf->virt) {
6342 kfree(dmabuf);
6343 return -ENOMEM;
6344 }
6345 memset(dmabuf->virt, 0, bmbx_size);
6346
6347 /*
6348 * Initialize the bootstrap mailbox pointers now so that the register
6349 * operations are simple later. The mailbox dma address is required
6350 * to be 16-byte aligned. Also align the virtual memory as each
6351 * maibox is copied into the bmbx mailbox region before issuing the
6352 * command to the port.
6353 */
6354 phba->sli4_hba.bmbx.dmabuf = dmabuf;
6355 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
6356
6357 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
6358 LPFC_ALIGN_16_BYTE);
6359 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
6360 LPFC_ALIGN_16_BYTE);
6361
6362 /*
6363 * Set the high and low physical addresses now. The SLI4 alignment
6364 * requirement is 16 bytes and the mailbox is posted to the port
6365 * as two 30-bit addresses. The other data is a bit marking whether
6366 * the 30-bit address is the high or low address.
6367 * Upcast bmbx aphys to 64bits so shift instruction compiles
6368 * clean on 32 bit machines.
6369 */
6370 dma_address = &phba->sli4_hba.bmbx.dma_address;
6371 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
6372 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
6373 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
6374 LPFC_BMBX_BIT1_ADDR_HI);
6375
6376 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
6377 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
6378 LPFC_BMBX_BIT1_ADDR_LO);
6379 return 0;
6380}
6381
6382/**
6383 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
6384 * @phba: pointer to lpfc hba data structure.
6385 *
6386 * This routine is invoked to teardown the bootstrap mailbox
6387 * region and release all host resources. This routine requires
6388 * the caller to ensure all mailbox commands recovered, no
6389 * additional mailbox comands are sent, and interrupts are disabled
6390 * before calling this routine.
6391 *
6392 **/
6393static void
6394lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
6395{
6396 dma_free_coherent(&phba->pcidev->dev,
6397 phba->sli4_hba.bmbx.bmbx_size,
6398 phba->sli4_hba.bmbx.dmabuf->virt,
6399 phba->sli4_hba.bmbx.dmabuf->phys);
6400
6401 kfree(phba->sli4_hba.bmbx.dmabuf);
6402 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
6403}
6404
6405/**
6406 * lpfc_sli4_read_config - Get the config parameters.
6407 * @phba: pointer to lpfc hba data structure.
6408 *
6409 * This routine is invoked to read the configuration parameters from the HBA.
6410 * The configuration parameters are used to set the base and maximum values
6411 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
6412 * allocation for the port.
6413 *
6414 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006415 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006416 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006417 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006418 **/
James Smartff78d8f2011-12-13 13:21:35 -05006419int
James Smartda0436e2009-05-22 14:51:39 -04006420lpfc_sli4_read_config(struct lpfc_hba *phba)
6421{
6422 LPFC_MBOXQ_t *pmb;
6423 struct lpfc_mbx_read_config *rd_config;
James Smart912e3ac2011-05-24 11:42:11 -04006424 union lpfc_sli4_cfg_shdr *shdr;
6425 uint32_t shdr_status, shdr_add_status;
6426 struct lpfc_mbx_get_func_cfg *get_func_cfg;
6427 struct lpfc_rsrc_desc_fcfcoe *desc;
James Smart8aa134a2012-08-14 14:25:29 -04006428 char *pdesc_0;
James Smart912e3ac2011-05-24 11:42:11 -04006429 uint32_t desc_count;
James Smart8aa134a2012-08-14 14:25:29 -04006430 int length, i, rc = 0, rc2;
James Smartda0436e2009-05-22 14:51:39 -04006431
6432 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6433 if (!pmb) {
6434 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6435 "2011 Unable to allocate memory for issuing "
6436 "SLI_CONFIG_SPECIAL mailbox command\n");
6437 return -ENOMEM;
6438 }
6439
6440 lpfc_read_config(phba, pmb);
6441
6442 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6443 if (rc != MBX_SUCCESS) {
6444 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6445 "2012 Mailbox failed , mbxCmd x%x "
6446 "READ_CONFIG, mbxStatus x%x\n",
6447 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6448 bf_get(lpfc_mqe_status, &pmb->u.mqe));
6449 rc = -EIO;
6450 } else {
6451 rd_config = &pmb->u.mqe.un.rd_config;
James Smartff78d8f2011-12-13 13:21:35 -05006452 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
6453 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
6454 phba->sli4_hba.lnk_info.lnk_tp =
6455 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
6456 phba->sli4_hba.lnk_info.lnk_no =
6457 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
6458 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6459 "3081 lnk_type:%d, lnk_numb:%d\n",
6460 phba->sli4_hba.lnk_info.lnk_tp,
6461 phba->sli4_hba.lnk_info.lnk_no);
6462 } else
6463 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6464 "3082 Mailbox (x%x) returned ldv:x0\n",
6465 bf_get(lpfc_mqe_command, &pmb->u.mqe));
James Smart6d368e52011-05-24 11:44:12 -04006466 phba->sli4_hba.extents_in_use =
6467 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04006468 phba->sli4_hba.max_cfg_param.max_xri =
6469 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
6470 phba->sli4_hba.max_cfg_param.xri_base =
6471 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
6472 phba->sli4_hba.max_cfg_param.max_vpi =
6473 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
6474 phba->sli4_hba.max_cfg_param.vpi_base =
6475 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
6476 phba->sli4_hba.max_cfg_param.max_rpi =
6477 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
6478 phba->sli4_hba.max_cfg_param.rpi_base =
6479 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
6480 phba->sli4_hba.max_cfg_param.max_vfi =
6481 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
6482 phba->sli4_hba.max_cfg_param.vfi_base =
6483 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
6484 phba->sli4_hba.max_cfg_param.max_fcfi =
6485 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04006486 phba->sli4_hba.max_cfg_param.max_eq =
6487 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
6488 phba->sli4_hba.max_cfg_param.max_rq =
6489 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
6490 phba->sli4_hba.max_cfg_param.max_wq =
6491 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
6492 phba->sli4_hba.max_cfg_param.max_cq =
6493 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
6494 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
6495 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
6496 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
6497 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
James Smart5ffc2662009-11-18 15:39:44 -05006498 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
6499 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04006500 phba->max_vports = phba->max_vpi;
6501 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart6d368e52011-05-24 11:44:12 -04006502 "2003 cfg params Extents? %d "
6503 "XRI(B:%d M:%d), "
James Smartda0436e2009-05-22 14:51:39 -04006504 "VPI(B:%d M:%d) "
6505 "VFI(B:%d M:%d) "
6506 "RPI(B:%d M:%d) "
James Smart6d368e52011-05-24 11:44:12 -04006507 "FCFI(Count:%d)\n",
6508 phba->sli4_hba.extents_in_use,
James Smartda0436e2009-05-22 14:51:39 -04006509 phba->sli4_hba.max_cfg_param.xri_base,
6510 phba->sli4_hba.max_cfg_param.max_xri,
6511 phba->sli4_hba.max_cfg_param.vpi_base,
6512 phba->sli4_hba.max_cfg_param.max_vpi,
6513 phba->sli4_hba.max_cfg_param.vfi_base,
6514 phba->sli4_hba.max_cfg_param.max_vfi,
6515 phba->sli4_hba.max_cfg_param.rpi_base,
6516 phba->sli4_hba.max_cfg_param.max_rpi,
James Smartda0436e2009-05-22 14:51:39 -04006517 phba->sli4_hba.max_cfg_param.max_fcfi);
6518 }
James Smart912e3ac2011-05-24 11:42:11 -04006519
6520 if (rc)
6521 goto read_cfg_out;
James Smartda0436e2009-05-22 14:51:39 -04006522
6523 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smartb92938b2010-06-07 15:24:12 -04006524 if (phba->cfg_hba_queue_depth >
6525 (phba->sli4_hba.max_cfg_param.max_xri -
6526 lpfc_sli4_get_els_iocb_cnt(phba)))
James Smartda0436e2009-05-22 14:51:39 -04006527 phba->cfg_hba_queue_depth =
James Smartb92938b2010-06-07 15:24:12 -04006528 phba->sli4_hba.max_cfg_param.max_xri -
6529 lpfc_sli4_get_els_iocb_cnt(phba);
James Smart912e3ac2011-05-24 11:42:11 -04006530
6531 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
6532 LPFC_SLI_INTF_IF_TYPE_2)
6533 goto read_cfg_out;
6534
6535 /* get the pf# and vf# for SLI4 if_type 2 port */
6536 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
6537 sizeof(struct lpfc_sli4_cfg_mhdr));
6538 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
6539 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
6540 length, LPFC_SLI4_MBX_EMBED);
6541
James Smart8aa134a2012-08-14 14:25:29 -04006542 rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart912e3ac2011-05-24 11:42:11 -04006543 shdr = (union lpfc_sli4_cfg_shdr *)
6544 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
6545 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6546 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart8aa134a2012-08-14 14:25:29 -04006547 if (rc2 || shdr_status || shdr_add_status) {
James Smart912e3ac2011-05-24 11:42:11 -04006548 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6549 "3026 Mailbox failed , mbxCmd x%x "
6550 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
6551 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6552 bf_get(lpfc_mqe_status, &pmb->u.mqe));
James Smart912e3ac2011-05-24 11:42:11 -04006553 goto read_cfg_out;
6554 }
6555
6556 /* search for fc_fcoe resrouce descriptor */
6557 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
6558 desc_count = get_func_cfg->func_cfg.rsrc_desc_count;
6559
James Smart8aa134a2012-08-14 14:25:29 -04006560 pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
6561 desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
6562 length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
6563 if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
6564 length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
6565 else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
6566 goto read_cfg_out;
6567
James Smart912e3ac2011-05-24 11:42:11 -04006568 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
James Smart8aa134a2012-08-14 14:25:29 -04006569 desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
James Smart912e3ac2011-05-24 11:42:11 -04006570 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
James Smart8aa134a2012-08-14 14:25:29 -04006571 bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
James Smart912e3ac2011-05-24 11:42:11 -04006572 phba->sli4_hba.iov.pf_number =
6573 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
6574 phba->sli4_hba.iov.vf_number =
6575 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
6576 break;
6577 }
6578 }
6579
6580 if (i < LPFC_RSRC_DESC_MAX_NUM)
6581 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6582 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
6583 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
6584 phba->sli4_hba.iov.vf_number);
James Smart8aa134a2012-08-14 14:25:29 -04006585 else
James Smart912e3ac2011-05-24 11:42:11 -04006586 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6587 "3028 GET_FUNCTION_CONFIG: failed to find "
6588 "Resrouce Descriptor:x%x\n",
6589 LPFC_RSRC_DESC_TYPE_FCFCOE);
James Smart912e3ac2011-05-24 11:42:11 -04006590
6591read_cfg_out:
6592 mempool_free(pmb, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04006593 return rc;
6594}
6595
6596/**
James Smart2fcee4b2010-12-15 17:57:46 -05006597 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
James Smartda0436e2009-05-22 14:51:39 -04006598 * @phba: pointer to lpfc hba data structure.
6599 *
James Smart2fcee4b2010-12-15 17:57:46 -05006600 * This routine is invoked to setup the port-side endian order when
6601 * the port if_type is 0. This routine has no function for other
6602 * if_types.
James Smartda0436e2009-05-22 14:51:39 -04006603 *
6604 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006605 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006606 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006607 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006608 **/
6609static int
6610lpfc_setup_endian_order(struct lpfc_hba *phba)
6611{
6612 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05006613 uint32_t if_type, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04006614 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
6615 HOST_ENDIAN_HIGH_WORD1};
6616
James Smart2fcee4b2010-12-15 17:57:46 -05006617 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
6618 switch (if_type) {
6619 case LPFC_SLI_INTF_IF_TYPE_0:
6620 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6621 GFP_KERNEL);
6622 if (!mboxq) {
6623 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6624 "0492 Unable to allocate memory for "
6625 "issuing SLI_CONFIG_SPECIAL mailbox "
6626 "command\n");
6627 return -ENOMEM;
6628 }
James Smartda0436e2009-05-22 14:51:39 -04006629
James Smart2fcee4b2010-12-15 17:57:46 -05006630 /*
6631 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
6632 * two words to contain special data values and no other data.
6633 */
6634 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
6635 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
6636 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6637 if (rc != MBX_SUCCESS) {
6638 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6639 "0493 SLI_CONFIG_SPECIAL mailbox "
6640 "failed with status x%x\n",
6641 rc);
6642 rc = -EIO;
6643 }
6644 mempool_free(mboxq, phba->mbox_mem_pool);
6645 break;
6646 case LPFC_SLI_INTF_IF_TYPE_2:
6647 case LPFC_SLI_INTF_IF_TYPE_1:
6648 default:
6649 break;
James Smartda0436e2009-05-22 14:51:39 -04006650 }
James Smartda0436e2009-05-22 14:51:39 -04006651 return rc;
6652}
6653
6654/**
James Smart5350d872011-10-10 21:33:49 -04006655 * lpfc_sli4_queue_verify - Verify and update EQ and CQ counts
James Smartda0436e2009-05-22 14:51:39 -04006656 * @phba: pointer to lpfc hba data structure.
6657 *
James Smart5350d872011-10-10 21:33:49 -04006658 * This routine is invoked to check the user settable queue counts for EQs and
6659 * CQs. after this routine is called the counts will be set to valid values that
6660 * adhere to the constraints of the system's interrupt vectors and the port's
6661 * queue resources.
James Smartda0436e2009-05-22 14:51:39 -04006662 *
6663 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006664 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006665 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04006666 **/
6667static int
James Smart5350d872011-10-10 21:33:49 -04006668lpfc_sli4_queue_verify(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04006669{
James Smart67d12732012-08-03 12:36:13 -04006670 int cfg_fcp_io_channel;
James Smart90695ee2012-08-14 14:25:36 -04006671 uint32_t cpu;
6672 uint32_t i = 0;
6673
James Smartda0436e2009-05-22 14:51:39 -04006674
6675 /*
James Smart67d12732012-08-03 12:36:13 -04006676 * Sanity check for configured queue parameters against the run-time
James Smartda0436e2009-05-22 14:51:39 -04006677 * device parameters
6678 */
6679
James Smart67d12732012-08-03 12:36:13 -04006680 /* Sanity check on HBA EQ parameters */
6681 cfg_fcp_io_channel = phba->cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04006682
James Smart90695ee2012-08-14 14:25:36 -04006683 /* It doesn't make sense to have more io channels then CPUs */
6684 for_each_online_cpu(cpu) {
6685 i++;
6686 }
6687 if (i < cfg_fcp_io_channel) {
James Smart82c3e9b2012-09-29 11:29:50 -04006688 lpfc_printf_log(phba,
6689 KERN_ERR, LOG_INIT,
James Smart90695ee2012-08-14 14:25:36 -04006690 "3188 Reducing IO channels to match number of "
6691 "CPUs: from %d to %d\n", cfg_fcp_io_channel, i);
6692 cfg_fcp_io_channel = i;
6693 }
6694
James Smart67d12732012-08-03 12:36:13 -04006695 if (cfg_fcp_io_channel >
6696 phba->sli4_hba.max_cfg_param.max_eq) {
James Smart82c3e9b2012-09-29 11:29:50 -04006697 if (phba->sli4_hba.max_cfg_param.max_eq <
6698 LPFC_FCP_IO_CHAN_MIN) {
James Smartda0436e2009-05-22 14:51:39 -04006699 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6700 "2574 Not enough EQs (%d) from the "
6701 "pci function for supporting FCP "
6702 "EQs (%d)\n",
6703 phba->sli4_hba.max_cfg_param.max_eq,
James Smart67d12732012-08-03 12:36:13 -04006704 phba->cfg_fcp_io_channel);
James Smartda0436e2009-05-22 14:51:39 -04006705 goto out_error;
6706 }
James Smart82c3e9b2012-09-29 11:29:50 -04006707 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6708 "2575 Reducing IO channels to match number of "
6709 "available EQs: from %d to %d\n",
6710 cfg_fcp_io_channel,
6711 phba->sli4_hba.max_cfg_param.max_eq);
6712 cfg_fcp_io_channel = phba->sli4_hba.max_cfg_param.max_eq;
James Smartda0436e2009-05-22 14:51:39 -04006713 }
James Smart67d12732012-08-03 12:36:13 -04006714
6715 /* Eventually cfg_fcp_eq_count / cfg_fcp_wq_count will be depricated */
6716
James Smartda0436e2009-05-22 14:51:39 -04006717 /* The actual number of FCP event queues adopted */
James Smart67d12732012-08-03 12:36:13 -04006718 phba->cfg_fcp_eq_count = cfg_fcp_io_channel;
6719 phba->cfg_fcp_wq_count = cfg_fcp_io_channel;
6720 phba->cfg_fcp_io_channel = cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04006721
James Smartda0436e2009-05-22 14:51:39 -04006722 /* Get EQ depth from module parameter, fake the default for now */
6723 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
6724 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
6725
James Smart5350d872011-10-10 21:33:49 -04006726 /* Get CQ depth from module parameter, fake the default for now */
6727 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
6728 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
6729
6730 return 0;
6731out_error:
6732 return -ENOMEM;
6733}
6734
6735/**
6736 * lpfc_sli4_queue_create - Create all the SLI4 queues
6737 * @phba: pointer to lpfc hba data structure.
6738 *
6739 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
6740 * operation. For each SLI4 queue type, the parameters such as queue entry
6741 * count (queue depth) shall be taken from the module parameter. For now,
6742 * we just use some constant number as place holder.
6743 *
6744 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07006745 * 0 - successful
James Smart5350d872011-10-10 21:33:49 -04006746 * -ENOMEM - No availble memory
6747 * -EIO - The mailbox failed to complete successfully.
6748 **/
6749int
6750lpfc_sli4_queue_create(struct lpfc_hba *phba)
6751{
6752 struct lpfc_queue *qdesc;
James Smart67d12732012-08-03 12:36:13 -04006753 int idx;
James Smart5350d872011-10-10 21:33:49 -04006754
6755 /*
James Smart67d12732012-08-03 12:36:13 -04006756 * Create HBA Record arrays.
James Smart5350d872011-10-10 21:33:49 -04006757 */
James Smart67d12732012-08-03 12:36:13 -04006758 if (!phba->cfg_fcp_io_channel)
6759 return -ERANGE;
James Smart5350d872011-10-10 21:33:49 -04006760
James Smart67d12732012-08-03 12:36:13 -04006761 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
6762 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
6763 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
6764 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
6765 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
6766 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
6767
6768 phba->sli4_hba.hba_eq = kzalloc((sizeof(struct lpfc_queue *) *
6769 phba->cfg_fcp_io_channel), GFP_KERNEL);
6770 if (!phba->sli4_hba.hba_eq) {
James Smartda0436e2009-05-22 14:51:39 -04006771 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04006772 "2576 Failed allocate memory for "
6773 "fast-path EQ record array\n");
James Smartda0436e2009-05-22 14:51:39 -04006774 goto out_error;
6775 }
James Smart67d12732012-08-03 12:36:13 -04006776
6777 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
6778 phba->cfg_fcp_io_channel), GFP_KERNEL);
6779 if (!phba->sli4_hba.fcp_cq) {
6780 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6781 "2577 Failed allocate memory for fast-path "
6782 "CQ record array\n");
6783 goto out_error;
6784 }
6785
6786 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
6787 phba->cfg_fcp_io_channel), GFP_KERNEL);
6788 if (!phba->sli4_hba.fcp_wq) {
6789 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6790 "2578 Failed allocate memory for fast-path "
6791 "WQ record array\n");
6792 goto out_error;
6793 }
James Smartda0436e2009-05-22 14:51:39 -04006794
James Smart5350d872011-10-10 21:33:49 -04006795 /*
James Smart67d12732012-08-03 12:36:13 -04006796 * Since the first EQ can have multiple CQs associated with it,
6797 * this array is used to quickly see if we have a FCP fast-path
6798 * CQ match.
James Smart5350d872011-10-10 21:33:49 -04006799 */
James Smart67d12732012-08-03 12:36:13 -04006800 phba->sli4_hba.fcp_cq_map = kzalloc((sizeof(uint16_t) *
6801 phba->cfg_fcp_io_channel), GFP_KERNEL);
6802 if (!phba->sli4_hba.fcp_cq_map) {
6803 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6804 "2545 Failed allocate memory for fast-path "
6805 "CQ map\n");
6806 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04006807 }
James Smart67d12732012-08-03 12:36:13 -04006808
6809 /*
6810 * Create HBA Event Queues (EQs). The cfg_fcp_io_channel specifies
6811 * how many EQs to create.
6812 */
6813 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
6814
6815 /* Create EQs */
James Smartda0436e2009-05-22 14:51:39 -04006816 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
6817 phba->sli4_hba.eq_ecount);
6818 if (!qdesc) {
6819 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04006820 "0497 Failed allocate EQ (%d)\n", idx);
6821 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04006822 }
James Smart67d12732012-08-03 12:36:13 -04006823 phba->sli4_hba.hba_eq[idx] = qdesc;
6824
6825 /* Create Fast Path FCP CQs */
6826 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6827 phba->sli4_hba.cq_ecount);
6828 if (!qdesc) {
6829 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6830 "0499 Failed allocate fast-path FCP "
6831 "CQ (%d)\n", idx);
6832 goto out_error;
6833 }
6834 phba->sli4_hba.fcp_cq[idx] = qdesc;
6835
6836 /* Create Fast Path FCP WQs */
6837 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
6838 phba->sli4_hba.wq_ecount);
6839 if (!qdesc) {
6840 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6841 "0503 Failed allocate fast-path FCP "
6842 "WQ (%d)\n", idx);
6843 goto out_error;
6844 }
6845 phba->sli4_hba.fcp_wq[idx] = qdesc;
James Smartda0436e2009-05-22 14:51:39 -04006846 }
6847
James Smart67d12732012-08-03 12:36:13 -04006848
James Smartda0436e2009-05-22 14:51:39 -04006849 /*
James Smart67d12732012-08-03 12:36:13 -04006850 * Create Slow Path Completion Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04006851 */
6852
James Smartda0436e2009-05-22 14:51:39 -04006853 /* Create slow-path Mailbox Command Complete Queue */
6854 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6855 phba->sli4_hba.cq_ecount);
6856 if (!qdesc) {
6857 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6858 "0500 Failed allocate slow-path mailbox CQ\n");
James Smart67d12732012-08-03 12:36:13 -04006859 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04006860 }
6861 phba->sli4_hba.mbx_cq = qdesc;
6862
6863 /* Create slow-path ELS Complete Queue */
6864 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6865 phba->sli4_hba.cq_ecount);
6866 if (!qdesc) {
6867 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6868 "0501 Failed allocate slow-path ELS CQ\n");
James Smart67d12732012-08-03 12:36:13 -04006869 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04006870 }
6871 phba->sli4_hba.els_cq = qdesc;
6872
James Smartda0436e2009-05-22 14:51:39 -04006873
James Smart5350d872011-10-10 21:33:49 -04006874 /*
James Smart67d12732012-08-03 12:36:13 -04006875 * Create Slow Path Work Queues (WQs)
James Smart5350d872011-10-10 21:33:49 -04006876 */
James Smartda0436e2009-05-22 14:51:39 -04006877
6878 /* Create Mailbox Command Queue */
James Smartda0436e2009-05-22 14:51:39 -04006879
6880 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
6881 phba->sli4_hba.mq_ecount);
6882 if (!qdesc) {
6883 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6884 "0505 Failed allocate slow-path MQ\n");
James Smart67d12732012-08-03 12:36:13 -04006885 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04006886 }
6887 phba->sli4_hba.mbx_wq = qdesc;
6888
6889 /*
James Smart67d12732012-08-03 12:36:13 -04006890 * Create ELS Work Queues
James Smartda0436e2009-05-22 14:51:39 -04006891 */
James Smartda0436e2009-05-22 14:51:39 -04006892
6893 /* Create slow-path ELS Work Queue */
6894 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
6895 phba->sli4_hba.wq_ecount);
6896 if (!qdesc) {
6897 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6898 "0504 Failed allocate slow-path ELS WQ\n");
James Smart67d12732012-08-03 12:36:13 -04006899 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04006900 }
6901 phba->sli4_hba.els_wq = qdesc;
6902
James Smartda0436e2009-05-22 14:51:39 -04006903 /*
6904 * Create Receive Queue (RQ)
6905 */
James Smartda0436e2009-05-22 14:51:39 -04006906
6907 /* Create Receive Queue for header */
6908 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
6909 phba->sli4_hba.rq_ecount);
6910 if (!qdesc) {
6911 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6912 "0506 Failed allocate receive HRQ\n");
James Smart67d12732012-08-03 12:36:13 -04006913 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04006914 }
6915 phba->sli4_hba.hdr_rq = qdesc;
6916
6917 /* Create Receive Queue for data */
6918 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
6919 phba->sli4_hba.rq_ecount);
6920 if (!qdesc) {
6921 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6922 "0507 Failed allocate receive DRQ\n");
James Smart67d12732012-08-03 12:36:13 -04006923 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04006924 }
6925 phba->sli4_hba.dat_rq = qdesc;
6926
6927 return 0;
6928
James Smartda0436e2009-05-22 14:51:39 -04006929out_error:
James Smart67d12732012-08-03 12:36:13 -04006930 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04006931 return -ENOMEM;
6932}
6933
6934/**
6935 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
6936 * @phba: pointer to lpfc hba data structure.
6937 *
6938 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
6939 * operation.
6940 *
6941 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006942 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006943 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006944 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006945 **/
James Smart5350d872011-10-10 21:33:49 -04006946void
James Smartda0436e2009-05-22 14:51:39 -04006947lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
6948{
James Smart67d12732012-08-03 12:36:13 -04006949 int idx;
6950
6951 if (phba->sli4_hba.hba_eq != NULL) {
6952 /* Release HBA event queue */
6953 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
6954 if (phba->sli4_hba.hba_eq[idx] != NULL) {
6955 lpfc_sli4_queue_free(
6956 phba->sli4_hba.hba_eq[idx]);
6957 phba->sli4_hba.hba_eq[idx] = NULL;
6958 }
6959 }
6960 kfree(phba->sli4_hba.hba_eq);
6961 phba->sli4_hba.hba_eq = NULL;
6962 }
6963
6964 if (phba->sli4_hba.fcp_cq != NULL) {
6965 /* Release FCP completion queue */
6966 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
6967 if (phba->sli4_hba.fcp_cq[idx] != NULL) {
6968 lpfc_sli4_queue_free(
6969 phba->sli4_hba.fcp_cq[idx]);
6970 phba->sli4_hba.fcp_cq[idx] = NULL;
6971 }
6972 }
6973 kfree(phba->sli4_hba.fcp_cq);
6974 phba->sli4_hba.fcp_cq = NULL;
6975 }
6976
6977 if (phba->sli4_hba.fcp_wq != NULL) {
6978 /* Release FCP work queue */
6979 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
6980 if (phba->sli4_hba.fcp_wq[idx] != NULL) {
6981 lpfc_sli4_queue_free(
6982 phba->sli4_hba.fcp_wq[idx]);
6983 phba->sli4_hba.fcp_wq[idx] = NULL;
6984 }
6985 }
6986 kfree(phba->sli4_hba.fcp_wq);
6987 phba->sli4_hba.fcp_wq = NULL;
6988 }
6989
James Smart962bc512013-01-03 15:44:00 -05006990 if (phba->pci_bar0_memmap_p) {
6991 iounmap(phba->pci_bar0_memmap_p);
6992 phba->pci_bar0_memmap_p = NULL;
6993 }
6994 if (phba->pci_bar2_memmap_p) {
6995 iounmap(phba->pci_bar2_memmap_p);
6996 phba->pci_bar2_memmap_p = NULL;
6997 }
6998 if (phba->pci_bar4_memmap_p) {
6999 iounmap(phba->pci_bar4_memmap_p);
7000 phba->pci_bar4_memmap_p = NULL;
7001 }
7002
James Smart67d12732012-08-03 12:36:13 -04007003 /* Release FCP CQ mapping array */
7004 if (phba->sli4_hba.fcp_cq_map != NULL) {
7005 kfree(phba->sli4_hba.fcp_cq_map);
7006 phba->sli4_hba.fcp_cq_map = NULL;
7007 }
James Smartda0436e2009-05-22 14:51:39 -04007008
7009 /* Release mailbox command work queue */
James Smart67d12732012-08-03 12:36:13 -04007010 if (phba->sli4_hba.mbx_wq != NULL) {
7011 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
7012 phba->sli4_hba.mbx_wq = NULL;
7013 }
James Smartda0436e2009-05-22 14:51:39 -04007014
7015 /* Release ELS work queue */
James Smart67d12732012-08-03 12:36:13 -04007016 if (phba->sli4_hba.els_wq != NULL) {
7017 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
7018 phba->sli4_hba.els_wq = NULL;
7019 }
James Smartda0436e2009-05-22 14:51:39 -04007020
7021 /* Release unsolicited receive queue */
James Smart67d12732012-08-03 12:36:13 -04007022 if (phba->sli4_hba.hdr_rq != NULL) {
7023 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
7024 phba->sli4_hba.hdr_rq = NULL;
7025 }
7026 if (phba->sli4_hba.dat_rq != NULL) {
7027 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
7028 phba->sli4_hba.dat_rq = NULL;
7029 }
James Smartda0436e2009-05-22 14:51:39 -04007030
James Smartda0436e2009-05-22 14:51:39 -04007031 /* Release ELS complete queue */
James Smart67d12732012-08-03 12:36:13 -04007032 if (phba->sli4_hba.els_cq != NULL) {
7033 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
7034 phba->sli4_hba.els_cq = NULL;
7035 }
James Smartda0436e2009-05-22 14:51:39 -04007036
7037 /* Release mailbox command complete queue */
James Smart67d12732012-08-03 12:36:13 -04007038 if (phba->sli4_hba.mbx_cq != NULL) {
7039 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
7040 phba->sli4_hba.mbx_cq = NULL;
7041 }
James Smartda0436e2009-05-22 14:51:39 -04007042
7043 return;
7044}
7045
7046/**
7047 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
7048 * @phba: pointer to lpfc hba data structure.
7049 *
7050 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
7051 * operation.
7052 *
7053 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007054 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007055 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007056 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007057 **/
7058int
7059lpfc_sli4_queue_setup(struct lpfc_hba *phba)
7060{
James Smart2a76a282012-08-03 12:35:54 -04007061 struct lpfc_sli *psli = &phba->sli;
7062 struct lpfc_sli_ring *pring;
James Smartda0436e2009-05-22 14:51:39 -04007063 int rc = -ENOMEM;
7064 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
7065 int fcp_cq_index = 0;
James Smart962bc512013-01-03 15:44:00 -05007066 uint32_t shdr_status, shdr_add_status;
7067 union lpfc_sli4_cfg_shdr *shdr;
7068 LPFC_MBOXQ_t *mboxq;
7069 uint32_t length;
7070
7071 /* Check for dual-ULP support */
7072 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7073 if (!mboxq) {
7074 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7075 "3249 Unable to allocate memory for "
7076 "QUERY_FW_CFG mailbox command\n");
7077 return -ENOMEM;
7078 }
7079 length = (sizeof(struct lpfc_mbx_query_fw_config) -
7080 sizeof(struct lpfc_sli4_cfg_mhdr));
7081 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7082 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
7083 length, LPFC_SLI4_MBX_EMBED);
7084
7085 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7086
7087 shdr = (union lpfc_sli4_cfg_shdr *)
7088 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7089 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7090 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7091 if (shdr_status || shdr_add_status || rc) {
7092 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7093 "3250 QUERY_FW_CFG mailbox failed with status "
7094 "x%x add_status x%x, mbx status x%x\n",
7095 shdr_status, shdr_add_status, rc);
7096 if (rc != MBX_TIMEOUT)
7097 mempool_free(mboxq, phba->mbox_mem_pool);
7098 rc = -ENXIO;
7099 goto out_error;
7100 }
7101
7102 phba->sli4_hba.fw_func_mode =
7103 mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
7104 phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
7105 phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
7106 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7107 "3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
7108 "ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
7109 phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
7110
7111 if (rc != MBX_TIMEOUT)
7112 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04007113
7114 /*
James Smart67d12732012-08-03 12:36:13 -04007115 * Set up HBA Event Queues (EQs)
James Smartda0436e2009-05-22 14:51:39 -04007116 */
7117
James Smart67d12732012-08-03 12:36:13 -04007118 /* Set up HBA event queue */
7119 if (phba->cfg_fcp_io_channel && !phba->sli4_hba.hba_eq) {
James Smart2e90f4b2011-12-13 13:22:37 -05007120 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7121 "3147 Fast-path EQs not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007122 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007123 goto out_error;
James Smart2e90f4b2011-12-13 13:22:37 -05007124 }
James Smart67d12732012-08-03 12:36:13 -04007125 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
7126 if (!phba->sli4_hba.hba_eq[fcp_eqidx]) {
James Smartda0436e2009-05-22 14:51:39 -04007127 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7128 "0522 Fast-path EQ (%d) not "
7129 "allocated\n", fcp_eqidx);
James Smart1b511972011-12-13 13:23:09 -05007130 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007131 goto out_destroy_hba_eq;
James Smartda0436e2009-05-22 14:51:39 -04007132 }
James Smart67d12732012-08-03 12:36:13 -04007133 rc = lpfc_eq_create(phba, phba->sli4_hba.hba_eq[fcp_eqidx],
James Smartbf8dae82012-08-03 12:36:24 -04007134 (phba->cfg_fcp_imax / phba->cfg_fcp_io_channel));
James Smartda0436e2009-05-22 14:51:39 -04007135 if (rc) {
7136 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7137 "0523 Failed setup of fast-path EQ "
7138 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
James Smart67d12732012-08-03 12:36:13 -04007139 goto out_destroy_hba_eq;
James Smartda0436e2009-05-22 14:51:39 -04007140 }
7141 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04007142 "2584 HBA EQ setup: "
James Smartda0436e2009-05-22 14:51:39 -04007143 "queue[%d]-id=%d\n", fcp_eqidx,
James Smart67d12732012-08-03 12:36:13 -04007144 phba->sli4_hba.hba_eq[fcp_eqidx]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007145 }
7146
James Smart67d12732012-08-03 12:36:13 -04007147 /* Set up fast-path FCP Response Complete Queue */
7148 if (!phba->sli4_hba.fcp_cq) {
7149 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7150 "3148 Fast-path FCP CQ array not "
7151 "allocated\n");
7152 rc = -ENOMEM;
7153 goto out_destroy_hba_eq;
7154 }
7155
7156 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_io_channel; fcp_cqidx++) {
7157 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
7158 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7159 "0526 Fast-path FCP CQ (%d) not "
7160 "allocated\n", fcp_cqidx);
7161 rc = -ENOMEM;
7162 goto out_destroy_fcp_cq;
7163 }
7164 rc = lpfc_cq_create(phba, phba->sli4_hba.fcp_cq[fcp_cqidx],
7165 phba->sli4_hba.hba_eq[fcp_cqidx], LPFC_WCQ, LPFC_FCP);
7166 if (rc) {
7167 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7168 "0527 Failed setup of fast-path FCP "
7169 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
7170 goto out_destroy_fcp_cq;
7171 }
7172
7173 /* Setup fcp_cq_map for fast lookup */
7174 phba->sli4_hba.fcp_cq_map[fcp_cqidx] =
7175 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id;
7176
7177 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7178 "2588 FCP CQ setup: cq[%d]-id=%d, "
7179 "parent seq[%d]-id=%d\n",
7180 fcp_cqidx,
7181 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
7182 fcp_cqidx,
7183 phba->sli4_hba.hba_eq[fcp_cqidx]->queue_id);
7184 }
7185
7186 /* Set up fast-path FCP Work Queue */
7187 if (!phba->sli4_hba.fcp_wq) {
7188 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7189 "3149 Fast-path FCP WQ array not "
7190 "allocated\n");
7191 rc = -ENOMEM;
7192 goto out_destroy_fcp_cq;
7193 }
7194
7195 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_io_channel; fcp_wqidx++) {
7196 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
7197 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7198 "0534 Fast-path FCP WQ (%d) not "
7199 "allocated\n", fcp_wqidx);
7200 rc = -ENOMEM;
7201 goto out_destroy_fcp_wq;
7202 }
7203 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
7204 phba->sli4_hba.fcp_cq[fcp_wqidx],
7205 LPFC_FCP);
7206 if (rc) {
7207 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7208 "0535 Failed setup of fast-path FCP "
7209 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
7210 goto out_destroy_fcp_wq;
7211 }
7212
7213 /* Bind this WQ to the next FCP ring */
7214 pring = &psli->ring[MAX_SLI3_CONFIGURED_RINGS + fcp_wqidx];
7215 pring->sli.sli4.wqp = (void *)phba->sli4_hba.fcp_wq[fcp_wqidx];
7216 phba->sli4_hba.fcp_cq[fcp_wqidx]->pring = pring;
7217
7218 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7219 "2591 FCP WQ setup: wq[%d]-id=%d, "
7220 "parent cq[%d]-id=%d\n",
7221 fcp_wqidx,
7222 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
7223 fcp_cq_index,
7224 phba->sli4_hba.fcp_cq[fcp_wqidx]->queue_id);
7225 }
James Smartda0436e2009-05-22 14:51:39 -04007226 /*
7227 * Set up Complete Queues (CQs)
7228 */
7229
7230 /* Set up slow-path MBOX Complete Queue as the first CQ */
7231 if (!phba->sli4_hba.mbx_cq) {
7232 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7233 "0528 Mailbox CQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007234 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007235 goto out_destroy_fcp_wq;
James Smartda0436e2009-05-22 14:51:39 -04007236 }
James Smart67d12732012-08-03 12:36:13 -04007237 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq,
7238 phba->sli4_hba.hba_eq[0], LPFC_MCQ, LPFC_MBOX);
James Smartda0436e2009-05-22 14:51:39 -04007239 if (rc) {
7240 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7241 "0529 Failed setup of slow-path mailbox CQ: "
7242 "rc = 0x%x\n", rc);
James Smart67d12732012-08-03 12:36:13 -04007243 goto out_destroy_fcp_wq;
James Smartda0436e2009-05-22 14:51:39 -04007244 }
7245 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7246 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
7247 phba->sli4_hba.mbx_cq->queue_id,
James Smart67d12732012-08-03 12:36:13 -04007248 phba->sli4_hba.hba_eq[0]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007249
7250 /* Set up slow-path ELS Complete Queue */
7251 if (!phba->sli4_hba.els_cq) {
7252 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7253 "0530 ELS CQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007254 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04007255 goto out_destroy_mbx_cq;
7256 }
James Smart67d12732012-08-03 12:36:13 -04007257 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq,
7258 phba->sli4_hba.hba_eq[0], LPFC_WCQ, LPFC_ELS);
James Smartda0436e2009-05-22 14:51:39 -04007259 if (rc) {
7260 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7261 "0531 Failed setup of slow-path ELS CQ: "
7262 "rc = 0x%x\n", rc);
7263 goto out_destroy_mbx_cq;
7264 }
7265 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7266 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
7267 phba->sli4_hba.els_cq->queue_id,
James Smart67d12732012-08-03 12:36:13 -04007268 phba->sli4_hba.hba_eq[0]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007269
7270 /*
7271 * Set up all the Work Queues (WQs)
7272 */
7273
7274 /* Set up Mailbox Command Queue */
7275 if (!phba->sli4_hba.mbx_wq) {
7276 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7277 "0538 Slow-path MQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007278 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007279 goto out_destroy_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04007280 }
7281 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
7282 phba->sli4_hba.mbx_cq, LPFC_MBOX);
7283 if (rc) {
7284 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7285 "0539 Failed setup of slow-path MQ: "
7286 "rc = 0x%x\n", rc);
James Smart67d12732012-08-03 12:36:13 -04007287 goto out_destroy_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04007288 }
7289 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7290 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
7291 phba->sli4_hba.mbx_wq->queue_id,
7292 phba->sli4_hba.mbx_cq->queue_id);
7293
7294 /* Set up slow-path ELS Work Queue */
7295 if (!phba->sli4_hba.els_wq) {
7296 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7297 "0536 Slow-path ELS WQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007298 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04007299 goto out_destroy_mbx_wq;
7300 }
7301 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
7302 phba->sli4_hba.els_cq, LPFC_ELS);
7303 if (rc) {
7304 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7305 "0537 Failed setup of slow-path ELS WQ: "
7306 "rc = 0x%x\n", rc);
7307 goto out_destroy_mbx_wq;
7308 }
James Smart2a76a282012-08-03 12:35:54 -04007309
7310 /* Bind this WQ to the ELS ring */
7311 pring = &psli->ring[LPFC_ELS_RING];
7312 pring->sli.sli4.wqp = (void *)phba->sli4_hba.els_wq;
7313 phba->sli4_hba.els_cq->pring = pring;
7314
James Smartda0436e2009-05-22 14:51:39 -04007315 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7316 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
7317 phba->sli4_hba.els_wq->queue_id,
7318 phba->sli4_hba.els_cq->queue_id);
7319
James Smartda0436e2009-05-22 14:51:39 -04007320 /*
7321 * Create Receive Queue (RQ)
7322 */
7323 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
7324 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7325 "0540 Receive Queue not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007326 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007327 goto out_destroy_els_wq;
James Smartda0436e2009-05-22 14:51:39 -04007328 }
James Smart73d91e52011-10-10 21:32:10 -04007329
7330 lpfc_rq_adjust_repost(phba, phba->sli4_hba.hdr_rq, LPFC_ELS_HBQ);
7331 lpfc_rq_adjust_repost(phba, phba->sli4_hba.dat_rq, LPFC_ELS_HBQ);
7332
James Smartda0436e2009-05-22 14:51:39 -04007333 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04007334 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04007335 if (rc) {
7336 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7337 "0541 Failed setup of Receive Queue: "
7338 "rc = 0x%x\n", rc);
7339 goto out_destroy_fcp_wq;
7340 }
James Smart73d91e52011-10-10 21:32:10 -04007341
James Smartda0436e2009-05-22 14:51:39 -04007342 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7343 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
7344 "parent cq-id=%d\n",
7345 phba->sli4_hba.hdr_rq->queue_id,
7346 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04007347 phba->sli4_hba.els_cq->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007348 return 0;
7349
James Smart2e90f4b2011-12-13 13:22:37 -05007350out_destroy_els_wq:
James Smartda0436e2009-05-22 14:51:39 -04007351 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7352out_destroy_mbx_wq:
7353 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
James Smart2e90f4b2011-12-13 13:22:37 -05007354out_destroy_els_cq:
James Smartda0436e2009-05-22 14:51:39 -04007355 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
7356out_destroy_mbx_cq:
7357 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
James Smart67d12732012-08-03 12:36:13 -04007358out_destroy_fcp_wq:
7359 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
7360 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
7361out_destroy_fcp_cq:
7362 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
7363 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
7364out_destroy_hba_eq:
James Smartda0436e2009-05-22 14:51:39 -04007365 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
James Smart67d12732012-08-03 12:36:13 -04007366 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[fcp_eqidx]);
James Smartda0436e2009-05-22 14:51:39 -04007367out_error:
7368 return rc;
7369}
7370
7371/**
7372 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
7373 * @phba: pointer to lpfc hba data structure.
7374 *
7375 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
7376 * operation.
7377 *
7378 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007379 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007380 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007381 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007382 **/
7383void
7384lpfc_sli4_queue_unset(struct lpfc_hba *phba)
7385{
7386 int fcp_qidx;
7387
7388 /* Unset mailbox command work queue */
7389 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
7390 /* Unset ELS work queue */
7391 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7392 /* Unset unsolicited receive queue */
7393 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
7394 /* Unset FCP work queue */
James Smart67d12732012-08-03 12:36:13 -04007395 if (phba->sli4_hba.fcp_wq) {
7396 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
7397 fcp_qidx++)
7398 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
7399 }
James Smartda0436e2009-05-22 14:51:39 -04007400 /* Unset mailbox command complete queue */
7401 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
7402 /* Unset ELS complete queue */
7403 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
James Smartda0436e2009-05-22 14:51:39 -04007404 /* Unset FCP response complete queue */
James Smart2e90f4b2011-12-13 13:22:37 -05007405 if (phba->sli4_hba.fcp_cq) {
James Smart67d12732012-08-03 12:36:13 -04007406 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
7407 fcp_qidx++)
James Smart2e90f4b2011-12-13 13:22:37 -05007408 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05007409 }
James Smartda0436e2009-05-22 14:51:39 -04007410 /* Unset fast-path event queue */
James Smart67d12732012-08-03 12:36:13 -04007411 if (phba->sli4_hba.hba_eq) {
7412 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
James Smart2e90f4b2011-12-13 13:22:37 -05007413 fcp_qidx++)
James Smart67d12732012-08-03 12:36:13 -04007414 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[fcp_qidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05007415 }
James Smartda0436e2009-05-22 14:51:39 -04007416}
7417
7418/**
7419 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
7420 * @phba: pointer to lpfc hba data structure.
7421 *
7422 * This routine is invoked to allocate and set up a pool of completion queue
7423 * events. The body of the completion queue event is a completion queue entry
7424 * CQE. For now, this pool is used for the interrupt service routine to queue
7425 * the following HBA completion queue events for the worker thread to process:
7426 * - Mailbox asynchronous events
7427 * - Receive queue completion unsolicited events
7428 * Later, this can be used for all the slow-path events.
7429 *
7430 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007431 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007432 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04007433 **/
7434static int
7435lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
7436{
7437 struct lpfc_cq_event *cq_event;
7438 int i;
7439
7440 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
7441 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
7442 if (!cq_event)
7443 goto out_pool_create_fail;
7444 list_add_tail(&cq_event->list,
7445 &phba->sli4_hba.sp_cqe_event_pool);
7446 }
7447 return 0;
7448
7449out_pool_create_fail:
7450 lpfc_sli4_cq_event_pool_destroy(phba);
7451 return -ENOMEM;
7452}
7453
7454/**
7455 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
7456 * @phba: pointer to lpfc hba data structure.
7457 *
7458 * This routine is invoked to free the pool of completion queue events at
7459 * driver unload time. Note that, it is the responsibility of the driver
7460 * cleanup routine to free all the outstanding completion-queue events
7461 * allocated from this pool back into the pool before invoking this routine
7462 * to destroy the pool.
7463 **/
7464static void
7465lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
7466{
7467 struct lpfc_cq_event *cq_event, *next_cq_event;
7468
7469 list_for_each_entry_safe(cq_event, next_cq_event,
7470 &phba->sli4_hba.sp_cqe_event_pool, list) {
7471 list_del(&cq_event->list);
7472 kfree(cq_event);
7473 }
7474}
7475
7476/**
7477 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
7478 * @phba: pointer to lpfc hba data structure.
7479 *
7480 * This routine is the lock free version of the API invoked to allocate a
7481 * completion-queue event from the free pool.
7482 *
7483 * Return: Pointer to the newly allocated completion-queue event if successful
7484 * NULL otherwise.
7485 **/
7486struct lpfc_cq_event *
7487__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
7488{
7489 struct lpfc_cq_event *cq_event = NULL;
7490
7491 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
7492 struct lpfc_cq_event, list);
7493 return cq_event;
7494}
7495
7496/**
7497 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
7498 * @phba: pointer to lpfc hba data structure.
7499 *
7500 * This routine is the lock version of the API invoked to allocate a
7501 * completion-queue event from the free pool.
7502 *
7503 * Return: Pointer to the newly allocated completion-queue event if successful
7504 * NULL otherwise.
7505 **/
7506struct lpfc_cq_event *
7507lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
7508{
7509 struct lpfc_cq_event *cq_event;
7510 unsigned long iflags;
7511
7512 spin_lock_irqsave(&phba->hbalock, iflags);
7513 cq_event = __lpfc_sli4_cq_event_alloc(phba);
7514 spin_unlock_irqrestore(&phba->hbalock, iflags);
7515 return cq_event;
7516}
7517
7518/**
7519 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
7520 * @phba: pointer to lpfc hba data structure.
7521 * @cq_event: pointer to the completion queue event to be freed.
7522 *
7523 * This routine is the lock free version of the API invoked to release a
7524 * completion-queue event back into the free pool.
7525 **/
7526void
7527__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
7528 struct lpfc_cq_event *cq_event)
7529{
7530 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
7531}
7532
7533/**
7534 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
7535 * @phba: pointer to lpfc hba data structure.
7536 * @cq_event: pointer to the completion queue event to be freed.
7537 *
7538 * This routine is the lock version of the API invoked to release a
7539 * completion-queue event back into the free pool.
7540 **/
7541void
7542lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
7543 struct lpfc_cq_event *cq_event)
7544{
7545 unsigned long iflags;
7546 spin_lock_irqsave(&phba->hbalock, iflags);
7547 __lpfc_sli4_cq_event_release(phba, cq_event);
7548 spin_unlock_irqrestore(&phba->hbalock, iflags);
7549}
7550
7551/**
7552 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
7553 * @phba: pointer to lpfc hba data structure.
7554 *
7555 * This routine is to free all the pending completion-queue events to the
7556 * back into the free pool for device reset.
7557 **/
7558static void
7559lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
7560{
7561 LIST_HEAD(cqelist);
7562 struct lpfc_cq_event *cqe;
7563 unsigned long iflags;
7564
7565 /* Retrieve all the pending WCQEs from pending WCQE lists */
7566 spin_lock_irqsave(&phba->hbalock, iflags);
7567 /* Pending FCP XRI abort events */
7568 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
7569 &cqelist);
7570 /* Pending ELS XRI abort events */
7571 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
7572 &cqelist);
7573 /* Pending asynnc events */
7574 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
7575 &cqelist);
7576 spin_unlock_irqrestore(&phba->hbalock, iflags);
7577
7578 while (!list_empty(&cqelist)) {
7579 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
7580 lpfc_sli4_cq_event_release(phba, cqe);
7581 }
7582}
7583
7584/**
7585 * lpfc_pci_function_reset - Reset pci function.
7586 * @phba: pointer to lpfc hba data structure.
7587 *
7588 * This routine is invoked to request a PCI function reset. It will destroys
7589 * all resources assigned to the PCI function which originates this request.
7590 *
7591 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007592 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007593 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007594 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007595 **/
7596int
7597lpfc_pci_function_reset(struct lpfc_hba *phba)
7598{
7599 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05007600 uint32_t rc = 0, if_type;
James Smartda0436e2009-05-22 14:51:39 -04007601 uint32_t shdr_status, shdr_add_status;
James Smart2fcee4b2010-12-15 17:57:46 -05007602 uint32_t rdy_chk, num_resets = 0, reset_again = 0;
James Smartda0436e2009-05-22 14:51:39 -04007603 union lpfc_sli4_cfg_shdr *shdr;
James Smart2fcee4b2010-12-15 17:57:46 -05007604 struct lpfc_register reg_data;
James Smart2b81f942012-03-01 22:37:18 -05007605 uint16_t devid;
James Smartda0436e2009-05-22 14:51:39 -04007606
James Smart2fcee4b2010-12-15 17:57:46 -05007607 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7608 switch (if_type) {
7609 case LPFC_SLI_INTF_IF_TYPE_0:
7610 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
7611 GFP_KERNEL);
7612 if (!mboxq) {
7613 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7614 "0494 Unable to allocate memory for "
7615 "issuing SLI_FUNCTION_RESET mailbox "
7616 "command\n");
7617 return -ENOMEM;
7618 }
7619
7620 /* Setup PCI function reset mailbox-ioctl command */
7621 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7622 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
7623 LPFC_SLI4_MBX_EMBED);
7624 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7625 shdr = (union lpfc_sli4_cfg_shdr *)
7626 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7627 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7628 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
7629 &shdr->response);
7630 if (rc != MBX_TIMEOUT)
7631 mempool_free(mboxq, phba->mbox_mem_pool);
7632 if (shdr_status || shdr_add_status || rc) {
7633 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7634 "0495 SLI_FUNCTION_RESET mailbox "
7635 "failed with status x%x add_status x%x,"
7636 " mbx status x%x\n",
7637 shdr_status, shdr_add_status, rc);
7638 rc = -ENXIO;
7639 }
7640 break;
7641 case LPFC_SLI_INTF_IF_TYPE_2:
7642 for (num_resets = 0;
7643 num_resets < MAX_IF_TYPE_2_RESETS;
7644 num_resets++) {
7645 reg_data.word0 = 0;
7646 bf_set(lpfc_sliport_ctrl_end, &reg_data,
7647 LPFC_SLIPORT_LITTLE_ENDIAN);
7648 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
7649 LPFC_SLIPORT_INIT_PORT);
7650 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
7651 CTRLregaddr);
James Smart8fcb8ac2012-03-01 22:35:58 -05007652 /* flush */
James Smart2b81f942012-03-01 22:37:18 -05007653 pci_read_config_word(phba->pcidev,
7654 PCI_DEVICE_ID, &devid);
James Smart2fcee4b2010-12-15 17:57:46 -05007655 /*
7656 * Poll the Port Status Register and wait for RDY for
7657 * up to 10 seconds. If the port doesn't respond, treat
7658 * it as an error. If the port responds with RN, start
7659 * the loop again.
7660 */
7661 for (rdy_chk = 0; rdy_chk < 1000; rdy_chk++) {
James Smart73d91e52011-10-10 21:32:10 -04007662 msleep(10);
James Smart9940b972011-03-11 16:06:12 -05007663 if (lpfc_readl(phba->sli4_hba.u.if_type2.
7664 STATUSregaddr, &reg_data.word0)) {
7665 rc = -ENODEV;
James Smart73d91e52011-10-10 21:32:10 -04007666 goto out;
James Smart9940b972011-03-11 16:06:12 -05007667 }
James Smart6b5151f2012-01-18 16:24:06 -05007668 if (bf_get(lpfc_sliport_status_rn, &reg_data))
7669 reset_again++;
James Smart2fcee4b2010-12-15 17:57:46 -05007670 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
7671 break;
James Smart2fcee4b2010-12-15 17:57:46 -05007672 }
7673
7674 /*
7675 * If the port responds to the init request with
7676 * reset needed, delay for a bit and restart the loop.
7677 */
James Smart6b5151f2012-01-18 16:24:06 -05007678 if (reset_again && (rdy_chk < 1000)) {
James Smart2fcee4b2010-12-15 17:57:46 -05007679 msleep(10);
7680 reset_again = 0;
7681 continue;
7682 }
7683
7684 /* Detect any port errors. */
James Smart2fcee4b2010-12-15 17:57:46 -05007685 if ((bf_get(lpfc_sliport_status_err, &reg_data)) ||
7686 (rdy_chk >= 1000)) {
7687 phba->work_status[0] = readl(
7688 phba->sli4_hba.u.if_type2.ERR1regaddr);
7689 phba->work_status[1] = readl(
7690 phba->sli4_hba.u.if_type2.ERR2regaddr);
7691 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05007692 "2890 Port error detected during port "
James Smart8a9d2e82012-05-09 21:16:12 -04007693 "reset(%d): wait_tmo:%d ms, "
7694 "port status reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05007695 "error 1=0x%x, error 2=0x%x\n",
James Smart8a9d2e82012-05-09 21:16:12 -04007696 num_resets, rdy_chk*10,
7697 reg_data.word0,
James Smart2fcee4b2010-12-15 17:57:46 -05007698 phba->work_status[0],
7699 phba->work_status[1]);
7700 rc = -ENODEV;
7701 }
7702
7703 /*
7704 * Terminate the outer loop provided the Port indicated
7705 * ready within 10 seconds.
7706 */
7707 if (rdy_chk < 1000)
7708 break;
7709 }
James Smart05580562011-05-24 11:40:48 -04007710 /* delay driver action following IF_TYPE_2 function reset */
7711 msleep(100);
James Smart2fcee4b2010-12-15 17:57:46 -05007712 break;
7713 case LPFC_SLI_INTF_IF_TYPE_1:
7714 default:
7715 break;
James Smartda0436e2009-05-22 14:51:39 -04007716 }
7717
James Smart73d91e52011-10-10 21:32:10 -04007718out:
James Smart2fcee4b2010-12-15 17:57:46 -05007719 /* Catch the not-ready port failure after a port reset. */
7720 if (num_resets >= MAX_IF_TYPE_2_RESETS)
7721 rc = -ENODEV;
7722
James Smartda0436e2009-05-22 14:51:39 -04007723 return rc;
7724}
7725
7726/**
James Smartda0436e2009-05-22 14:51:39 -04007727 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
7728 * @phba: pointer to lpfc hba data structure.
7729 *
7730 * This routine is invoked to set up the PCI device memory space for device
7731 * with SLI-4 interface spec.
7732 *
7733 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007734 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007735 * other values - error
7736 **/
7737static int
7738lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
7739{
7740 struct pci_dev *pdev;
7741 unsigned long bar0map_len, bar1map_len, bar2map_len;
7742 int error = -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -05007743 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04007744
7745 /* Obtain PCI device reference */
7746 if (!phba->pcidev)
7747 return error;
7748 else
7749 pdev = phba->pcidev;
7750
7751 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05007752 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
7753 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
7754 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
7755 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smartda0436e2009-05-22 14:51:39 -04007756 return error;
Michael Reed8e685972009-09-18 12:02:05 -05007757 }
7758 }
James Smartda0436e2009-05-22 14:51:39 -04007759
James Smart2fcee4b2010-12-15 17:57:46 -05007760 /*
7761 * The BARs and register set definitions and offset locations are
7762 * dependent on the if_type.
7763 */
7764 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
7765 &phba->sli4_hba.sli_intf.word0)) {
7766 return error;
7767 }
7768
7769 /* There is no SLI3 failback for SLI4 devices. */
7770 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
7771 LPFC_SLI_INTF_VALID) {
7772 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7773 "2894 SLI_INTF reg contents invalid "
7774 "sli_intf reg 0x%x\n",
7775 phba->sli4_hba.sli_intf.word0);
7776 return error;
7777 }
7778
7779 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7780 /*
7781 * Get the bus address of SLI4 device Bar regions and the
7782 * number of bytes required by each mapping. The mapping of the
7783 * particular PCI BARs regions is dependent on the type of
7784 * SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -04007785 */
James Smart1dfb5a42010-02-12 14:40:50 -05007786 if (pci_resource_start(pdev, 0)) {
7787 phba->pci_bar0_map = pci_resource_start(pdev, 0);
7788 bar0map_len = pci_resource_len(pdev, 0);
James Smart2fcee4b2010-12-15 17:57:46 -05007789
7790 /*
7791 * Map SLI4 PCI Config Space Register base to a kernel virtual
7792 * addr
7793 */
7794 phba->sli4_hba.conf_regs_memmap_p =
7795 ioremap(phba->pci_bar0_map, bar0map_len);
7796 if (!phba->sli4_hba.conf_regs_memmap_p) {
7797 dev_printk(KERN_ERR, &pdev->dev,
7798 "ioremap failed for SLI4 PCI config "
7799 "registers.\n");
7800 goto out;
7801 }
7802 /* Set up BAR0 PCI config space register memory map */
7803 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smart1dfb5a42010-02-12 14:40:50 -05007804 } else {
7805 phba->pci_bar0_map = pci_resource_start(pdev, 1);
7806 bar0map_len = pci_resource_len(pdev, 1);
James Smart2fcee4b2010-12-15 17:57:46 -05007807 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
7808 dev_printk(KERN_ERR, &pdev->dev,
7809 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
7810 goto out;
7811 }
7812 phba->sli4_hba.conf_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04007813 ioremap(phba->pci_bar0_map, bar0map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05007814 if (!phba->sli4_hba.conf_regs_memmap_p) {
7815 dev_printk(KERN_ERR, &pdev->dev,
7816 "ioremap failed for SLI4 PCI config "
7817 "registers.\n");
7818 goto out;
7819 }
7820 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smartda0436e2009-05-22 14:51:39 -04007821 }
7822
James Smartc31098c2011-04-16 11:03:33 -04007823 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
7824 (pci_resource_start(pdev, 2))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007825 /*
7826 * Map SLI4 if type 0 HBA Control Register base to a kernel
7827 * virtual address and setup the registers.
7828 */
7829 phba->pci_bar1_map = pci_resource_start(pdev, 2);
7830 bar1map_len = pci_resource_len(pdev, 2);
7831 phba->sli4_hba.ctrl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04007832 ioremap(phba->pci_bar1_map, bar1map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05007833 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
7834 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04007835 "ioremap failed for SLI4 HBA control registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05007836 goto out_iounmap_conf;
7837 }
7838 lpfc_sli4_bar1_register_memmap(phba);
James Smartda0436e2009-05-22 14:51:39 -04007839 }
7840
James Smartc31098c2011-04-16 11:03:33 -04007841 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
7842 (pci_resource_start(pdev, 4))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007843 /*
7844 * Map SLI4 if type 0 HBA Doorbell Register base to a kernel
7845 * virtual address and setup the registers.
7846 */
7847 phba->pci_bar2_map = pci_resource_start(pdev, 4);
7848 bar2map_len = pci_resource_len(pdev, 4);
7849 phba->sli4_hba.drbl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04007850 ioremap(phba->pci_bar2_map, bar2map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05007851 if (!phba->sli4_hba.drbl_regs_memmap_p) {
7852 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04007853 "ioremap failed for SLI4 HBA doorbell registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05007854 goto out_iounmap_ctrl;
7855 }
7856 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
7857 if (error)
7858 goto out_iounmap_all;
James Smartda0436e2009-05-22 14:51:39 -04007859 }
7860
James Smartda0436e2009-05-22 14:51:39 -04007861 return 0;
7862
7863out_iounmap_all:
7864 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
7865out_iounmap_ctrl:
7866 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
7867out_iounmap_conf:
7868 iounmap(phba->sli4_hba.conf_regs_memmap_p);
7869out:
7870 return error;
7871}
7872
7873/**
7874 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
7875 * @phba: pointer to lpfc hba data structure.
7876 *
7877 * This routine is invoked to unset the PCI device memory space for device
7878 * with SLI-4 interface spec.
7879 **/
7880static void
7881lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
7882{
James Smart2e90f4b2011-12-13 13:22:37 -05007883 uint32_t if_type;
7884 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smartda0436e2009-05-22 14:51:39 -04007885
James Smart2e90f4b2011-12-13 13:22:37 -05007886 switch (if_type) {
7887 case LPFC_SLI_INTF_IF_TYPE_0:
7888 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
7889 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
7890 iounmap(phba->sli4_hba.conf_regs_memmap_p);
7891 break;
7892 case LPFC_SLI_INTF_IF_TYPE_2:
7893 iounmap(phba->sli4_hba.conf_regs_memmap_p);
7894 break;
7895 case LPFC_SLI_INTF_IF_TYPE_1:
7896 default:
7897 dev_printk(KERN_ERR, &phba->pcidev->dev,
7898 "FATAL - unsupported SLI4 interface type - %d\n",
7899 if_type);
7900 break;
7901 }
James Smartda0436e2009-05-22 14:51:39 -04007902}
7903
7904/**
James Smart3772a992009-05-22 14:50:54 -04007905 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
7906 * @phba: pointer to lpfc hba data structure.
7907 *
7908 * This routine is invoked to enable the MSI-X interrupt vectors to device
7909 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
7910 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
7911 * invoked, enables either all or nothing, depending on the current
7912 * availability of PCI vector resources. The device driver is responsible
7913 * for calling the individual request_irq() to register each MSI-X vector
7914 * with a interrupt handler, which is done in this function. Note that
7915 * later when device is unloading, the driver should always call free_irq()
7916 * on all MSI-X vectors it has done request_irq() on before calling
7917 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
7918 * will be left with MSI-X enabled and leaks its vectors.
7919 *
7920 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007921 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007922 * other values - error
7923 **/
7924static int
7925lpfc_sli_enable_msix(struct lpfc_hba *phba)
7926{
7927 int rc, i;
7928 LPFC_MBOXQ_t *pmb;
7929
7930 /* Set up MSI-X multi-message vectors */
7931 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
7932 phba->msix_entries[i].entry = i;
7933
7934 /* Configure MSI-X capability structure */
7935 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
7936 ARRAY_SIZE(phba->msix_entries));
7937 if (rc) {
7938 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7939 "0420 PCI enable MSI-X failed (%d)\n", rc);
7940 goto msi_fail_out;
7941 }
7942 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
7943 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7944 "0477 MSI-X entry[%d]: vector=x%x "
7945 "message=%d\n", i,
7946 phba->msix_entries[i].vector,
7947 phba->msix_entries[i].entry);
7948 /*
7949 * Assign MSI-X vectors to interrupt handlers
7950 */
7951
7952 /* vector-0 is associated to slow-path handler */
7953 rc = request_irq(phba->msix_entries[0].vector,
7954 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
7955 LPFC_SP_DRIVER_HANDLER_NAME, phba);
7956 if (rc) {
7957 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7958 "0421 MSI-X slow-path request_irq failed "
7959 "(%d)\n", rc);
7960 goto msi_fail_out;
7961 }
7962
7963 /* vector-1 is associated to fast-path handler */
7964 rc = request_irq(phba->msix_entries[1].vector,
7965 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
7966 LPFC_FP_DRIVER_HANDLER_NAME, phba);
7967
7968 if (rc) {
7969 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7970 "0429 MSI-X fast-path request_irq failed "
7971 "(%d)\n", rc);
7972 goto irq_fail_out;
7973 }
7974
7975 /*
7976 * Configure HBA MSI-X attention conditions to messages
7977 */
7978 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7979
7980 if (!pmb) {
7981 rc = -ENOMEM;
7982 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7983 "0474 Unable to allocate memory for issuing "
7984 "MBOX_CONFIG_MSI command\n");
7985 goto mem_fail_out;
7986 }
7987 rc = lpfc_config_msi(phba, pmb);
7988 if (rc)
7989 goto mbx_fail_out;
7990 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
7991 if (rc != MBX_SUCCESS) {
7992 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
7993 "0351 Config MSI mailbox command failed, "
7994 "mbxCmd x%x, mbxStatus x%x\n",
7995 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
7996 goto mbx_fail_out;
7997 }
7998
7999 /* Free memory allocated for mailbox command */
8000 mempool_free(pmb, phba->mbox_mem_pool);
8001 return rc;
8002
8003mbx_fail_out:
8004 /* Free memory allocated for mailbox command */
8005 mempool_free(pmb, phba->mbox_mem_pool);
8006
8007mem_fail_out:
8008 /* free the irq already requested */
8009 free_irq(phba->msix_entries[1].vector, phba);
8010
8011irq_fail_out:
8012 /* free the irq already requested */
8013 free_irq(phba->msix_entries[0].vector, phba);
8014
8015msi_fail_out:
8016 /* Unconfigure MSI-X capability structure */
8017 pci_disable_msix(phba->pcidev);
8018 return rc;
8019}
8020
8021/**
8022 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
8023 * @phba: pointer to lpfc hba data structure.
8024 *
8025 * This routine is invoked to release the MSI-X vectors and then disable the
8026 * MSI-X interrupt mode to device with SLI-3 interface spec.
8027 **/
8028static void
8029lpfc_sli_disable_msix(struct lpfc_hba *phba)
8030{
8031 int i;
8032
8033 /* Free up MSI-X multi-message vectors */
8034 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8035 free_irq(phba->msix_entries[i].vector, phba);
8036 /* Disable MSI-X */
8037 pci_disable_msix(phba->pcidev);
8038
8039 return;
8040}
8041
8042/**
8043 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
8044 * @phba: pointer to lpfc hba data structure.
8045 *
8046 * This routine is invoked to enable the MSI interrupt mode to device with
8047 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
8048 * enable the MSI vector. The device driver is responsible for calling the
8049 * request_irq() to register MSI vector with a interrupt the handler, which
8050 * is done in this function.
8051 *
8052 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008053 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008054 * other values - error
8055 */
8056static int
8057lpfc_sli_enable_msi(struct lpfc_hba *phba)
8058{
8059 int rc;
8060
8061 rc = pci_enable_msi(phba->pcidev);
8062 if (!rc)
8063 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8064 "0462 PCI enable MSI mode success.\n");
8065 else {
8066 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8067 "0471 PCI enable MSI mode failed (%d)\n", rc);
8068 return rc;
8069 }
8070
8071 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
8072 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8073 if (rc) {
8074 pci_disable_msi(phba->pcidev);
8075 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8076 "0478 MSI request_irq failed (%d)\n", rc);
8077 }
8078 return rc;
8079}
8080
8081/**
8082 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
8083 * @phba: pointer to lpfc hba data structure.
8084 *
8085 * This routine is invoked to disable the MSI interrupt mode to device with
8086 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
8087 * done request_irq() on before calling pci_disable_msi(). Failure to do so
8088 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
8089 * its vector.
8090 */
8091static void
8092lpfc_sli_disable_msi(struct lpfc_hba *phba)
8093{
8094 free_irq(phba->pcidev->irq, phba);
8095 pci_disable_msi(phba->pcidev);
8096 return;
8097}
8098
8099/**
8100 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
8101 * @phba: pointer to lpfc hba data structure.
8102 *
8103 * This routine is invoked to enable device interrupt and associate driver's
8104 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
8105 * spec. Depends on the interrupt mode configured to the driver, the driver
8106 * will try to fallback from the configured interrupt mode to an interrupt
8107 * mode which is supported by the platform, kernel, and device in the order
8108 * of:
8109 * MSI-X -> MSI -> IRQ.
8110 *
8111 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008112 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008113 * other values - error
8114 **/
8115static uint32_t
8116lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
8117{
8118 uint32_t intr_mode = LPFC_INTR_ERROR;
8119 int retval;
8120
8121 if (cfg_mode == 2) {
8122 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
8123 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
8124 if (!retval) {
8125 /* Now, try to enable MSI-X interrupt mode */
8126 retval = lpfc_sli_enable_msix(phba);
8127 if (!retval) {
8128 /* Indicate initialization to MSI-X mode */
8129 phba->intr_type = MSIX;
8130 intr_mode = 2;
8131 }
8132 }
8133 }
8134
8135 /* Fallback to MSI if MSI-X initialization failed */
8136 if (cfg_mode >= 1 && phba->intr_type == NONE) {
8137 retval = lpfc_sli_enable_msi(phba);
8138 if (!retval) {
8139 /* Indicate initialization to MSI mode */
8140 phba->intr_type = MSI;
8141 intr_mode = 1;
8142 }
8143 }
8144
8145 /* Fallback to INTx if both MSI-X/MSI initalization failed */
8146 if (phba->intr_type == NONE) {
8147 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
8148 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8149 if (!retval) {
8150 /* Indicate initialization to INTx mode */
8151 phba->intr_type = INTx;
8152 intr_mode = 0;
8153 }
8154 }
8155 return intr_mode;
8156}
8157
8158/**
8159 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
8160 * @phba: pointer to lpfc hba data structure.
8161 *
8162 * This routine is invoked to disable device interrupt and disassociate the
8163 * driver's interrupt handler(s) from interrupt vector(s) to device with
8164 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
8165 * release the interrupt vector(s) for the message signaled interrupt.
8166 **/
8167static void
8168lpfc_sli_disable_intr(struct lpfc_hba *phba)
8169{
8170 /* Disable the currently initialized interrupt mode */
8171 if (phba->intr_type == MSIX)
8172 lpfc_sli_disable_msix(phba);
8173 else if (phba->intr_type == MSI)
8174 lpfc_sli_disable_msi(phba);
8175 else if (phba->intr_type == INTx)
8176 free_irq(phba->pcidev->irq, phba);
8177
8178 /* Reset interrupt management states */
8179 phba->intr_type = NONE;
8180 phba->sli.slistat.sli_intr = 0;
8181
8182 return;
8183}
8184
8185/**
James Smartda0436e2009-05-22 14:51:39 -04008186 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
8187 * @phba: pointer to lpfc hba data structure.
8188 *
8189 * This routine is invoked to enable the MSI-X interrupt vectors to device
8190 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
8191 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
8192 * enables either all or nothing, depending on the current availability of
8193 * PCI vector resources. The device driver is responsible for calling the
8194 * individual request_irq() to register each MSI-X vector with a interrupt
8195 * handler, which is done in this function. Note that later when device is
8196 * unloading, the driver should always call free_irq() on all MSI-X vectors
8197 * it has done request_irq() on before calling pci_disable_msix(). Failure
8198 * to do so results in a BUG_ON() and a device will be left with MSI-X
8199 * enabled and leaks its vectors.
8200 *
8201 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008202 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008203 * other values - error
8204 **/
8205static int
8206lpfc_sli4_enable_msix(struct lpfc_hba *phba)
8207{
James Smart75baf692010-06-08 18:31:21 -04008208 int vectors, rc, index;
James Smartda0436e2009-05-22 14:51:39 -04008209
8210 /* Set up MSI-X multi-message vectors */
James Smart82c3e9b2012-09-29 11:29:50 -04008211 for (index = 0; index < phba->cfg_fcp_io_channel; index++)
James Smartda0436e2009-05-22 14:51:39 -04008212 phba->sli4_hba.msix_entries[index].entry = index;
8213
8214 /* Configure MSI-X capability structure */
James Smart82c3e9b2012-09-29 11:29:50 -04008215 vectors = phba->cfg_fcp_io_channel;
James Smart75baf692010-06-08 18:31:21 -04008216enable_msix_vectors:
James Smartda0436e2009-05-22 14:51:39 -04008217 rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
James Smart75baf692010-06-08 18:31:21 -04008218 vectors);
8219 if (rc > 1) {
8220 vectors = rc;
8221 goto enable_msix_vectors;
8222 } else if (rc) {
James Smartda0436e2009-05-22 14:51:39 -04008223 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8224 "0484 PCI enable MSI-X failed (%d)\n", rc);
8225 goto msi_fail_out;
8226 }
James Smart75baf692010-06-08 18:31:21 -04008227
James Smartda0436e2009-05-22 14:51:39 -04008228 /* Log MSI-X vector assignment */
James Smart75baf692010-06-08 18:31:21 -04008229 for (index = 0; index < vectors; index++)
James Smartda0436e2009-05-22 14:51:39 -04008230 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8231 "0489 MSI-X entry[%d]: vector=x%x "
8232 "message=%d\n", index,
8233 phba->sli4_hba.msix_entries[index].vector,
8234 phba->sli4_hba.msix_entries[index].entry);
James Smart67d12732012-08-03 12:36:13 -04008235
James Smartda0436e2009-05-22 14:51:39 -04008236 /*
8237 * Assign MSI-X vectors to interrupt handlers
8238 */
James Smart67d12732012-08-03 12:36:13 -04008239 for (index = 0; index < vectors; index++) {
James Smart4305f182012-08-03 12:36:33 -04008240 memset(&phba->sli4_hba.handler_name[index], 0, 16);
8241 sprintf((char *)&phba->sli4_hba.handler_name[index],
8242 LPFC_DRIVER_HANDLER_NAME"%d", index);
James Smartda0436e2009-05-22 14:51:39 -04008243
James Smart67d12732012-08-03 12:36:13 -04008244 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8245 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
James Smartba20c852012-08-03 12:36:52 -04008246 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].fcp_eq_in_use, 1);
James Smartda0436e2009-05-22 14:51:39 -04008247 rc = request_irq(phba->sli4_hba.msix_entries[index].vector,
James Smart67d12732012-08-03 12:36:13 -04008248 &lpfc_sli4_hba_intr_handler, IRQF_SHARED,
James Smart4305f182012-08-03 12:36:33 -04008249 (char *)&phba->sli4_hba.handler_name[index],
James Smart67d12732012-08-03 12:36:13 -04008250 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -04008251 if (rc) {
8252 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8253 "0486 MSI-X fast-path (%d) "
8254 "request_irq failed (%d)\n", index, rc);
8255 goto cfg_fail_out;
8256 }
8257 }
8258
James Smart82c3e9b2012-09-29 11:29:50 -04008259 if (vectors != phba->cfg_fcp_io_channel) {
8260 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8261 "3238 Reducing IO channels to match number of "
8262 "MSI-X vectors, requested %d got %d\n",
8263 phba->cfg_fcp_io_channel, vectors);
8264 phba->cfg_fcp_io_channel = vectors;
8265 }
James Smartda0436e2009-05-22 14:51:39 -04008266 return rc;
8267
8268cfg_fail_out:
8269 /* free the irq already requested */
James Smart67d12732012-08-03 12:36:13 -04008270 for (--index; index >= 0; index--)
8271 free_irq(phba->sli4_hba.msix_entries[index].vector,
8272 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -04008273
8274msi_fail_out:
8275 /* Unconfigure MSI-X capability structure */
8276 pci_disable_msix(phba->pcidev);
8277 return rc;
8278}
8279
8280/**
8281 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
8282 * @phba: pointer to lpfc hba data structure.
8283 *
8284 * This routine is invoked to release the MSI-X vectors and then disable the
8285 * MSI-X interrupt mode to device with SLI-4 interface spec.
8286 **/
8287static void
8288lpfc_sli4_disable_msix(struct lpfc_hba *phba)
8289{
8290 int index;
8291
8292 /* Free up MSI-X multi-message vectors */
James Smart82c3e9b2012-09-29 11:29:50 -04008293 for (index = 0; index < phba->cfg_fcp_io_channel; index++)
James Smartda0436e2009-05-22 14:51:39 -04008294 free_irq(phba->sli4_hba.msix_entries[index].vector,
James Smart67d12732012-08-03 12:36:13 -04008295 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smart75baf692010-06-08 18:31:21 -04008296
James Smartda0436e2009-05-22 14:51:39 -04008297 /* Disable MSI-X */
8298 pci_disable_msix(phba->pcidev);
8299
8300 return;
8301}
8302
8303/**
8304 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
8305 * @phba: pointer to lpfc hba data structure.
8306 *
8307 * This routine is invoked to enable the MSI interrupt mode to device with
8308 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
8309 * to enable the MSI vector. The device driver is responsible for calling
8310 * the request_irq() to register MSI vector with a interrupt the handler,
8311 * which is done in this function.
8312 *
8313 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008314 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008315 * other values - error
8316 **/
8317static int
8318lpfc_sli4_enable_msi(struct lpfc_hba *phba)
8319{
8320 int rc, index;
8321
8322 rc = pci_enable_msi(phba->pcidev);
8323 if (!rc)
8324 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8325 "0487 PCI enable MSI mode success.\n");
8326 else {
8327 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8328 "0488 PCI enable MSI mode failed (%d)\n", rc);
8329 return rc;
8330 }
8331
8332 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
8333 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8334 if (rc) {
8335 pci_disable_msi(phba->pcidev);
8336 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8337 "0490 MSI request_irq failed (%d)\n", rc);
James Smart75baf692010-06-08 18:31:21 -04008338 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008339 }
8340
James Smart67d12732012-08-03 12:36:13 -04008341 for (index = 0; index < phba->cfg_fcp_io_channel; index++) {
James Smartda0436e2009-05-22 14:51:39 -04008342 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8343 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
8344 }
8345
James Smart75baf692010-06-08 18:31:21 -04008346 return 0;
James Smartda0436e2009-05-22 14:51:39 -04008347}
8348
8349/**
8350 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
8351 * @phba: pointer to lpfc hba data structure.
8352 *
8353 * This routine is invoked to disable the MSI interrupt mode to device with
8354 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
8355 * done request_irq() on before calling pci_disable_msi(). Failure to do so
8356 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
8357 * its vector.
8358 **/
8359static void
8360lpfc_sli4_disable_msi(struct lpfc_hba *phba)
8361{
8362 free_irq(phba->pcidev->irq, phba);
8363 pci_disable_msi(phba->pcidev);
8364 return;
8365}
8366
8367/**
8368 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
8369 * @phba: pointer to lpfc hba data structure.
8370 *
8371 * This routine is invoked to enable device interrupt and associate driver's
8372 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
8373 * interface spec. Depends on the interrupt mode configured to the driver,
8374 * the driver will try to fallback from the configured interrupt mode to an
8375 * interrupt mode which is supported by the platform, kernel, and device in
8376 * the order of:
8377 * MSI-X -> MSI -> IRQ.
8378 *
8379 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008380 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008381 * other values - error
8382 **/
8383static uint32_t
8384lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
8385{
8386 uint32_t intr_mode = LPFC_INTR_ERROR;
8387 int retval, index;
8388
8389 if (cfg_mode == 2) {
8390 /* Preparation before conf_msi mbox cmd */
8391 retval = 0;
8392 if (!retval) {
8393 /* Now, try to enable MSI-X interrupt mode */
8394 retval = lpfc_sli4_enable_msix(phba);
8395 if (!retval) {
8396 /* Indicate initialization to MSI-X mode */
8397 phba->intr_type = MSIX;
8398 intr_mode = 2;
8399 }
8400 }
8401 }
8402
8403 /* Fallback to MSI if MSI-X initialization failed */
8404 if (cfg_mode >= 1 && phba->intr_type == NONE) {
8405 retval = lpfc_sli4_enable_msi(phba);
8406 if (!retval) {
8407 /* Indicate initialization to MSI mode */
8408 phba->intr_type = MSI;
8409 intr_mode = 1;
8410 }
8411 }
8412
8413 /* Fallback to INTx if both MSI-X/MSI initalization failed */
8414 if (phba->intr_type == NONE) {
8415 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
8416 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8417 if (!retval) {
8418 /* Indicate initialization to INTx mode */
8419 phba->intr_type = INTx;
8420 intr_mode = 0;
James Smart67d12732012-08-03 12:36:13 -04008421 for (index = 0; index < phba->cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04008422 index++) {
8423 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8424 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
James Smartba20c852012-08-03 12:36:52 -04008425 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].
8426 fcp_eq_in_use, 1);
James Smartda0436e2009-05-22 14:51:39 -04008427 }
8428 }
8429 }
8430 return intr_mode;
8431}
8432
8433/**
8434 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
8435 * @phba: pointer to lpfc hba data structure.
8436 *
8437 * This routine is invoked to disable device interrupt and disassociate
8438 * the driver's interrupt handler(s) from interrupt vector(s) to device
8439 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
8440 * will release the interrupt vector(s) for the message signaled interrupt.
8441 **/
8442static void
8443lpfc_sli4_disable_intr(struct lpfc_hba *phba)
8444{
8445 /* Disable the currently initialized interrupt mode */
8446 if (phba->intr_type == MSIX)
8447 lpfc_sli4_disable_msix(phba);
8448 else if (phba->intr_type == MSI)
8449 lpfc_sli4_disable_msi(phba);
8450 else if (phba->intr_type == INTx)
8451 free_irq(phba->pcidev->irq, phba);
8452
8453 /* Reset interrupt management states */
8454 phba->intr_type = NONE;
8455 phba->sli.slistat.sli_intr = 0;
8456
8457 return;
8458}
8459
8460/**
James Smart3772a992009-05-22 14:50:54 -04008461 * lpfc_unset_hba - Unset SLI3 hba device initialization
8462 * @phba: pointer to lpfc hba data structure.
8463 *
8464 * This routine is invoked to unset the HBA device initialization steps to
8465 * a device with SLI-3 interface spec.
8466 **/
8467static void
8468lpfc_unset_hba(struct lpfc_hba *phba)
8469{
8470 struct lpfc_vport *vport = phba->pport;
8471 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
8472
8473 spin_lock_irq(shost->host_lock);
8474 vport->load_flag |= FC_UNLOADING;
8475 spin_unlock_irq(shost->host_lock);
8476
James Smart72859902012-01-18 16:25:38 -05008477 kfree(phba->vpi_bmask);
8478 kfree(phba->vpi_ids);
8479
James Smart3772a992009-05-22 14:50:54 -04008480 lpfc_stop_hba_timers(phba);
8481
8482 phba->pport->work_port_events = 0;
8483
8484 lpfc_sli_hba_down(phba);
8485
8486 lpfc_sli_brdrestart(phba);
8487
8488 lpfc_sli_disable_intr(phba);
8489
8490 return;
8491}
8492
8493/**
James Smart5af5eee2010-10-22 11:06:38 -04008494 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
8495 * @phba: Pointer to HBA context object.
8496 *
8497 * This function is called in the SLI4 code path to wait for completion
8498 * of device's XRIs exchange busy. It will check the XRI exchange busy
8499 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
8500 * that, it will check the XRI exchange busy on outstanding FCP and ELS
8501 * I/Os every 30 seconds, log error message, and wait forever. Only when
8502 * all XRI exchange busy complete, the driver unload shall proceed with
8503 * invoking the function reset ioctl mailbox command to the CNA and the
8504 * the rest of the driver unload resource release.
8505 **/
8506static void
8507lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
8508{
8509 int wait_time = 0;
8510 int fcp_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
8511 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
8512
8513 while (!fcp_xri_cmpl || !els_xri_cmpl) {
8514 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
8515 if (!fcp_xri_cmpl)
8516 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8517 "2877 FCP XRI exchange busy "
8518 "wait time: %d seconds.\n",
8519 wait_time/1000);
8520 if (!els_xri_cmpl)
8521 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8522 "2878 ELS XRI exchange busy "
8523 "wait time: %d seconds.\n",
8524 wait_time/1000);
8525 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
8526 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
8527 } else {
8528 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
8529 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
8530 }
8531 fcp_xri_cmpl =
8532 list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
8533 els_xri_cmpl =
8534 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
8535 }
8536}
8537
8538/**
James Smartda0436e2009-05-22 14:51:39 -04008539 * lpfc_sli4_hba_unset - Unset the fcoe hba
8540 * @phba: Pointer to HBA context object.
8541 *
8542 * This function is called in the SLI4 code path to reset the HBA's FCoE
8543 * function. The caller is not required to hold any lock. This routine
8544 * issues PCI function reset mailbox command to reset the FCoE function.
8545 * At the end of the function, it calls lpfc_hba_down_post function to
8546 * free any pending commands.
8547 **/
8548static void
8549lpfc_sli4_hba_unset(struct lpfc_hba *phba)
8550{
8551 int wait_cnt = 0;
8552 LPFC_MBOXQ_t *mboxq;
James Smart912e3ac2011-05-24 11:42:11 -04008553 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -04008554
8555 lpfc_stop_hba_timers(phba);
8556 phba->sli4_hba.intr_enable = 0;
8557
8558 /*
8559 * Gracefully wait out the potential current outstanding asynchronous
8560 * mailbox command.
8561 */
8562
8563 /* First, block any pending async mailbox command from posted */
8564 spin_lock_irq(&phba->hbalock);
8565 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
8566 spin_unlock_irq(&phba->hbalock);
8567 /* Now, trying to wait it out if we can */
8568 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8569 msleep(10);
8570 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
8571 break;
8572 }
8573 /* Forcefully release the outstanding mailbox command if timed out */
8574 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8575 spin_lock_irq(&phba->hbalock);
8576 mboxq = phba->sli.mbox_active;
8577 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8578 __lpfc_mbox_cmpl_put(phba, mboxq);
8579 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8580 phba->sli.mbox_active = NULL;
8581 spin_unlock_irq(&phba->hbalock);
8582 }
8583
James Smart5af5eee2010-10-22 11:06:38 -04008584 /* Abort all iocbs associated with the hba */
8585 lpfc_sli_hba_iocb_abort(phba);
8586
8587 /* Wait for completion of device XRI exchange busy */
8588 lpfc_sli4_xri_exchange_busy_wait(phba);
8589
James Smartda0436e2009-05-22 14:51:39 -04008590 /* Disable PCI subsystem interrupt */
8591 lpfc_sli4_disable_intr(phba);
8592
James Smart912e3ac2011-05-24 11:42:11 -04008593 /* Disable SR-IOV if enabled */
8594 if (phba->cfg_sriov_nr_virtfn)
8595 pci_disable_sriov(pdev);
8596
James Smartda0436e2009-05-22 14:51:39 -04008597 /* Stop kthread signal shall trigger work_done one more time */
8598 kthread_stop(phba->worker_thread);
8599
James Smart3677a3a2010-09-29 11:19:14 -04008600 /* Reset SLI4 HBA FCoE function */
8601 lpfc_pci_function_reset(phba);
James Smart5350d872011-10-10 21:33:49 -04008602 lpfc_sli4_queue_destroy(phba);
James Smart3677a3a2010-09-29 11:19:14 -04008603
James Smartda0436e2009-05-22 14:51:39 -04008604 /* Stop the SLI4 device port */
8605 phba->pport->work_port_events = 0;
8606}
8607
James Smart28baac72010-02-12 14:42:03 -05008608 /**
8609 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
8610 * @phba: Pointer to HBA context object.
8611 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
8612 *
8613 * This function is called in the SLI4 code path to read the port's
8614 * sli4 capabilities.
8615 *
8616 * This function may be be called from any context that can block-wait
8617 * for the completion. The expectation is that this routine is called
8618 * typically from probe_one or from the online routine.
8619 **/
8620int
8621lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8622{
8623 int rc;
8624 struct lpfc_mqe *mqe;
8625 struct lpfc_pc_sli4_params *sli4_params;
8626 uint32_t mbox_tmo;
8627
8628 rc = 0;
8629 mqe = &mboxq->u.mqe;
8630
8631 /* Read the port's SLI4 Parameters port capabilities */
James Smartfedd3b72011-02-16 12:39:24 -05008632 lpfc_pc_sli4_params(mboxq);
James Smart28baac72010-02-12 14:42:03 -05008633 if (!phba->sli4_hba.intr_enable)
8634 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8635 else {
James Smarta183a152011-10-10 21:32:43 -04008636 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart28baac72010-02-12 14:42:03 -05008637 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
8638 }
8639
8640 if (unlikely(rc))
8641 return 1;
8642
8643 sli4_params = &phba->sli4_hba.pc_sli4_params;
8644 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
8645 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
8646 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
8647 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
8648 &mqe->un.sli4_params);
8649 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
8650 &mqe->un.sli4_params);
8651 sli4_params->proto_types = mqe->un.sli4_params.word3;
8652 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
8653 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
8654 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
8655 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
8656 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
8657 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
8658 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
8659 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
8660 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
8661 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
8662 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
8663 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
8664 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
8665 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
8666 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
8667 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
8668 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
8669 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
8670 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
8671 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
James Smart05580562011-05-24 11:40:48 -04008672
8673 /* Make sure that sge_supp_len can be handled by the driver */
8674 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
8675 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
8676
James Smart28baac72010-02-12 14:42:03 -05008677 return rc;
8678}
8679
James Smartda0436e2009-05-22 14:51:39 -04008680/**
James Smartfedd3b72011-02-16 12:39:24 -05008681 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
8682 * @phba: Pointer to HBA context object.
8683 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
8684 *
8685 * This function is called in the SLI4 code path to read the port's
8686 * sli4 capabilities.
8687 *
8688 * This function may be be called from any context that can block-wait
8689 * for the completion. The expectation is that this routine is called
8690 * typically from probe_one or from the online routine.
8691 **/
8692int
8693lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8694{
8695 int rc;
8696 struct lpfc_mqe *mqe = &mboxq->u.mqe;
8697 struct lpfc_pc_sli4_params *sli4_params;
James Smarta183a152011-10-10 21:32:43 -04008698 uint32_t mbox_tmo;
James Smartfedd3b72011-02-16 12:39:24 -05008699 int length;
8700 struct lpfc_sli4_parameters *mbx_sli4_parameters;
8701
James Smart6d368e52011-05-24 11:44:12 -04008702 /*
8703 * By default, the driver assumes the SLI4 port requires RPI
8704 * header postings. The SLI4_PARAM response will correct this
8705 * assumption.
8706 */
8707 phba->sli4_hba.rpi_hdrs_in_use = 1;
8708
James Smartfedd3b72011-02-16 12:39:24 -05008709 /* Read the port's SLI4 Config Parameters */
8710 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
8711 sizeof(struct lpfc_sli4_cfg_mhdr));
8712 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
8713 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
8714 length, LPFC_SLI4_MBX_EMBED);
8715 if (!phba->sli4_hba.intr_enable)
8716 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -04008717 else {
8718 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
8719 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
8720 }
James Smartfedd3b72011-02-16 12:39:24 -05008721 if (unlikely(rc))
8722 return rc;
8723 sli4_params = &phba->sli4_hba.pc_sli4_params;
8724 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
8725 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
8726 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
8727 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
8728 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
8729 mbx_sli4_parameters);
8730 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
8731 mbx_sli4_parameters);
8732 if (bf_get(cfg_phwq, mbx_sli4_parameters))
8733 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
8734 else
8735 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
8736 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
8737 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
8738 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
8739 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
8740 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
8741 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
8742 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
8743 mbx_sli4_parameters);
8744 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
8745 mbx_sli4_parameters);
James Smart6d368e52011-05-24 11:44:12 -04008746 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
8747 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
James Smart05580562011-05-24 11:40:48 -04008748
8749 /* Make sure that sge_supp_len can be handled by the driver */
8750 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
8751 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
8752
James Smartfedd3b72011-02-16 12:39:24 -05008753 return 0;
8754}
8755
8756/**
James Smart3772a992009-05-22 14:50:54 -04008757 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
8758 * @pdev: pointer to PCI device
8759 * @pid: pointer to PCI device identifier
8760 *
8761 * This routine is to be called to attach a device with SLI-3 interface spec
8762 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
8763 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
8764 * information of the device and driver to see if the driver state that it can
8765 * support this kind of device. If the match is successful, the driver core
8766 * invokes this routine. If this routine determines it can claim the HBA, it
8767 * does all the initialization that it needs to do to handle the HBA properly.
8768 *
8769 * Return code
8770 * 0 - driver can claim the device
8771 * negative value - driver can not claim the device
8772 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008773static int
James Smart3772a992009-05-22 14:50:54 -04008774lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
8775{
8776 struct lpfc_hba *phba;
8777 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04008778 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -04008779 int error;
8780 uint32_t cfg_mode, intr_mode;
8781
8782 /* Allocate memory for HBA structure */
8783 phba = lpfc_hba_alloc(pdev);
8784 if (!phba)
8785 return -ENOMEM;
8786
8787 /* Perform generic PCI device enabling operation */
8788 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -04008789 if (error)
James Smart3772a992009-05-22 14:50:54 -04008790 goto out_free_phba;
James Smart3772a992009-05-22 14:50:54 -04008791
8792 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
8793 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
8794 if (error)
8795 goto out_disable_pci_dev;
8796
8797 /* Set up SLI-3 specific device PCI memory space */
8798 error = lpfc_sli_pci_mem_setup(phba);
8799 if (error) {
8800 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8801 "1402 Failed to set up pci memory space.\n");
8802 goto out_disable_pci_dev;
8803 }
8804
8805 /* Set up phase-1 common device driver resources */
8806 error = lpfc_setup_driver_resource_phase1(phba);
8807 if (error) {
8808 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8809 "1403 Failed to set up driver resource.\n");
8810 goto out_unset_pci_mem_s3;
8811 }
8812
8813 /* Set up SLI-3 specific device driver resources */
8814 error = lpfc_sli_driver_resource_setup(phba);
8815 if (error) {
8816 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8817 "1404 Failed to set up driver resource.\n");
8818 goto out_unset_pci_mem_s3;
8819 }
8820
8821 /* Initialize and populate the iocb list per host */
8822 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
8823 if (error) {
8824 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8825 "1405 Failed to initialize iocb list.\n");
8826 goto out_unset_driver_resource_s3;
8827 }
8828
8829 /* Set up common device driver resources */
8830 error = lpfc_setup_driver_resource_phase2(phba);
8831 if (error) {
8832 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8833 "1406 Failed to set up driver resource.\n");
8834 goto out_free_iocb_list;
8835 }
8836
James Smart079b5c92011-08-21 21:48:49 -04008837 /* Get the default values for Model Name and Description */
8838 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
8839
James Smart3772a992009-05-22 14:50:54 -04008840 /* Create SCSI host to the physical port */
8841 error = lpfc_create_shost(phba);
8842 if (error) {
8843 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8844 "1407 Failed to create scsi host.\n");
8845 goto out_unset_driver_resource;
8846 }
8847
8848 /* Configure sysfs attributes */
8849 vport = phba->pport;
8850 error = lpfc_alloc_sysfs_attr(vport);
8851 if (error) {
8852 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8853 "1476 Failed to allocate sysfs attr\n");
8854 goto out_destroy_shost;
8855 }
8856
James Smart6669f9b2009-10-02 15:16:45 -04008857 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -04008858 /* Now, trying to enable interrupt and bring up the device */
8859 cfg_mode = phba->cfg_use_msi;
8860 while (true) {
8861 /* Put device to a known state before enabling interrupt */
8862 lpfc_stop_port(phba);
8863 /* Configure and enable interrupt */
8864 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
8865 if (intr_mode == LPFC_INTR_ERROR) {
8866 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8867 "0431 Failed to enable interrupt.\n");
8868 error = -ENODEV;
8869 goto out_free_sysfs_attr;
8870 }
8871 /* SLI-3 HBA setup */
8872 if (lpfc_sli_hba_setup(phba)) {
8873 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8874 "1477 Failed to set up hba\n");
8875 error = -ENODEV;
8876 goto out_remove_device;
8877 }
8878
8879 /* Wait 50ms for the interrupts of previous mailbox commands */
8880 msleep(50);
8881 /* Check active interrupts on message signaled interrupts */
8882 if (intr_mode == 0 ||
8883 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
8884 /* Log the current active interrupt mode */
8885 phba->intr_mode = intr_mode;
8886 lpfc_log_intr_mode(phba, intr_mode);
8887 break;
8888 } else {
8889 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8890 "0447 Configure interrupt mode (%d) "
8891 "failed active interrupt test.\n",
8892 intr_mode);
8893 /* Disable the current interrupt mode */
8894 lpfc_sli_disable_intr(phba);
8895 /* Try next level of interrupt mode */
8896 cfg_mode = --intr_mode;
8897 }
8898 }
8899
8900 /* Perform post initialization setup */
8901 lpfc_post_init_setup(phba);
8902
8903 /* Check if there are static vports to be created. */
8904 lpfc_create_static_vport(phba);
8905
8906 return 0;
8907
8908out_remove_device:
8909 lpfc_unset_hba(phba);
8910out_free_sysfs_attr:
8911 lpfc_free_sysfs_attr(vport);
8912out_destroy_shost:
8913 lpfc_destroy_shost(phba);
8914out_unset_driver_resource:
8915 lpfc_unset_driver_resource_phase2(phba);
8916out_free_iocb_list:
8917 lpfc_free_iocb_list(phba);
8918out_unset_driver_resource_s3:
8919 lpfc_sli_driver_resource_unset(phba);
8920out_unset_pci_mem_s3:
8921 lpfc_sli_pci_mem_unset(phba);
8922out_disable_pci_dev:
8923 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04008924 if (shost)
8925 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -04008926out_free_phba:
8927 lpfc_hba_free(phba);
8928 return error;
8929}
8930
8931/**
8932 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -04008933 * @pdev: pointer to PCI device
8934 *
James Smart3772a992009-05-22 14:50:54 -04008935 * This routine is to be called to disattach a device with SLI-3 interface
8936 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
8937 * removed from PCI bus, it performs all the necessary cleanup for the HBA
8938 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -04008939 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008940static void
James Smart3772a992009-05-22 14:50:54 -04008941lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -05008942{
James Smart2e0fef82007-06-17 19:56:36 -05008943 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8944 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -05008945 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -05008946 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -05008947 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05008948 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
8949
James Smart549e55c2007-08-02 11:09:51 -04008950 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04008951 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04008952 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008953
James Smart858c9f62007-06-17 19:56:39 -05008954 lpfc_free_sysfs_attr(vport);
8955
James Smarteada2722008-12-04 22:39:13 -05008956 /* Release all the vports against this physical port */
8957 vports = lpfc_create_vport_work_array(phba);
8958 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -04008959 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
8960 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
8961 continue;
James Smarteada2722008-12-04 22:39:13 -05008962 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -04008963 }
James Smarteada2722008-12-04 22:39:13 -05008964 lpfc_destroy_vport_work_array(phba, vports);
8965
8966 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -05008967 fc_remove_host(shost);
8968 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04008969 lpfc_cleanup(vport);
8970
James Smart2e0fef82007-06-17 19:56:36 -05008971 /*
8972 * Bring down the SLI Layer. This step disable all interrupts,
8973 * clears the rings, discards all mailbox commands, and resets
8974 * the HBA.
8975 */
James Smarta257bf92009-04-06 18:48:10 -04008976
Justin P. Mattock48e34d02010-12-30 15:07:58 -08008977 /* HBA interrupt will be disabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -05008978 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -04008979 /* Stop kthread signal shall trigger work_done one more time */
8980 kthread_stop(phba->worker_thread);
8981 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -05008982 lpfc_sli_brdrestart(phba);
8983
James Smart72859902012-01-18 16:25:38 -05008984 kfree(phba->vpi_bmask);
8985 kfree(phba->vpi_ids);
8986
James Smart3772a992009-05-22 14:50:54 -04008987 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05008988 spin_lock_irq(&phba->hbalock);
8989 list_del_init(&vport->listentry);
8990 spin_unlock_irq(&phba->hbalock);
8991
James Smart858c9f62007-06-17 19:56:39 -05008992 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05008993
James Smart912e3ac2011-05-24 11:42:11 -04008994 /* Disable SR-IOV if enabled */
8995 if (phba->cfg_sriov_nr_virtfn)
8996 pci_disable_sriov(pdev);
8997
James Smart5b75da22008-12-04 22:39:35 -05008998 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04008999 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -05009000
9001 pci_set_drvdata(pdev, NULL);
James Smart858c9f62007-06-17 19:56:39 -05009002 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05009003
9004 /*
9005 * Call scsi_free before mem_free since scsi bufs are released to their
9006 * corresponding pools here.
9007 */
9008 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -04009009 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -05009010
James Smart34b02dc2008-08-24 21:49:55 -04009011 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
9012 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -05009013
James Smart2e0fef82007-06-17 19:56:36 -05009014 /* Free resources associated with SLI2 interface */
9015 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -04009016 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -05009017
9018 /* unmap adapter SLIM and Control Registers */
9019 iounmap(phba->ctrl_regs_memmap_p);
9020 iounmap(phba->slim_memmap_p);
9021
James Smart3772a992009-05-22 14:50:54 -04009022 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -05009023
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05009024 pci_release_selected_regions(pdev, bars);
James Smart2e0fef82007-06-17 19:56:36 -05009025 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05009026}
9027
Linas Vepstas8d63f372007-02-14 14:28:36 -06009028/**
James Smart3772a992009-05-22 14:50:54 -04009029 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05009030 * @pdev: pointer to PCI device
9031 * @msg: power management message
9032 *
James Smart3772a992009-05-22 14:50:54 -04009033 * This routine is to be called from the kernel's PCI subsystem to support
9034 * system Power Management (PM) to device with SLI-3 interface spec. When
9035 * PM invokes this method, it quiesces the device by stopping the driver's
9036 * worker thread for the device, turning off device's interrupt and DMA,
9037 * and bring the device offline. Note that as the driver implements the
9038 * minimum PM requirements to a power-aware driver's PM support for the
9039 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
9040 * to the suspend() method call will be treated as SUSPEND and the driver will
9041 * fully reinitialize its device during resume() method call, the driver will
9042 * set device to PCI_D3hot state in PCI config space instead of setting it
9043 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -05009044 *
9045 * Return code
James Smart3772a992009-05-22 14:50:54 -04009046 * 0 - driver suspended the device
9047 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05009048 **/
9049static int
James Smart3772a992009-05-22 14:50:54 -04009050lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -05009051{
9052 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9053 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9054
9055 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9056 "0473 PCI device Power Management suspend.\n");
9057
9058 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -04009059 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3a55b532008-12-04 22:38:54 -05009060 lpfc_offline(phba);
9061 kthread_stop(phba->worker_thread);
9062
9063 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -04009064 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -05009065
9066 /* Save device state to PCI config space */
9067 pci_save_state(pdev);
9068 pci_set_power_state(pdev, PCI_D3hot);
9069
9070 return 0;
9071}
9072
9073/**
James Smart3772a992009-05-22 14:50:54 -04009074 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05009075 * @pdev: pointer to PCI device
9076 *
James Smart3772a992009-05-22 14:50:54 -04009077 * This routine is to be called from the kernel's PCI subsystem to support
9078 * system Power Management (PM) to device with SLI-3 interface spec. When PM
9079 * invokes this method, it restores the device's PCI config space state and
9080 * fully reinitializes the device and brings it online. Note that as the
9081 * driver implements the minimum PM requirements to a power-aware driver's
9082 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
9083 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
9084 * driver will fully reinitialize its device during resume() method call,
9085 * the device will be set to PCI_D0 directly in PCI config space before
9086 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -05009087 *
9088 * Return code
James Smart3772a992009-05-22 14:50:54 -04009089 * 0 - driver suspended the device
9090 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05009091 **/
9092static int
James Smart3772a992009-05-22 14:50:54 -04009093lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -05009094{
9095 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9096 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -05009097 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05009098 int error;
9099
9100 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9101 "0452 PCI device Power Management resume.\n");
9102
9103 /* Restore device state from PCI config space */
9104 pci_set_power_state(pdev, PCI_D0);
9105 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -04009106
James Smart1dfb5a42010-02-12 14:40:50 -05009107 /*
9108 * As the new kernel behavior of pci_restore_state() API call clears
9109 * device saved_state flag, need to save the restored state again.
9110 */
9111 pci_save_state(pdev);
9112
James Smart3a55b532008-12-04 22:38:54 -05009113 if (pdev->is_busmaster)
9114 pci_set_master(pdev);
9115
9116 /* Startup the kernel thread for this host adapter. */
9117 phba->worker_thread = kthread_run(lpfc_do_work, phba,
9118 "lpfc_worker_%d", phba->brd_no);
9119 if (IS_ERR(phba->worker_thread)) {
9120 error = PTR_ERR(phba->worker_thread);
9121 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9122 "0434 PM resume failed to start worker "
9123 "thread: error=x%x.\n", error);
9124 return error;
9125 }
9126
James Smart5b75da22008-12-04 22:39:35 -05009127 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009128 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05009129 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -05009130 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -05009131 "0430 PM resume Failed to enable interrupt\n");
9132 return -EIO;
9133 } else
9134 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05009135
9136 /* Restart HBA and bring it online */
9137 lpfc_sli_brdrestart(phba);
9138 lpfc_online(phba);
9139
James Smart5b75da22008-12-04 22:39:35 -05009140 /* Log the current active interrupt mode */
9141 lpfc_log_intr_mode(phba, phba->intr_mode);
9142
James Smart3a55b532008-12-04 22:38:54 -05009143 return 0;
9144}
9145
9146/**
James Smart891478a2009-11-18 15:40:23 -05009147 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
9148 * @phba: pointer to lpfc hba data structure.
9149 *
9150 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -04009151 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -05009152 **/
9153static void
9154lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
9155{
James Smarte2af0d22010-03-15 11:25:32 -04009156 struct lpfc_sli *psli = &phba->sli;
9157 struct lpfc_sli_ring *pring;
9158
James Smart891478a2009-11-18 15:40:23 -05009159 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9160 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -04009161
9162 /*
9163 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
9164 * and let the SCSI mid-layer to retry them to recover.
9165 */
9166 pring = &psli->ring[psli->fcp_ring];
9167 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart891478a2009-11-18 15:40:23 -05009168}
9169
9170/**
James Smart0d878412009-10-02 15:16:56 -04009171 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
9172 * @phba: pointer to lpfc hba data structure.
9173 *
9174 * This routine is called to prepare the SLI3 device for PCI slot reset. It
9175 * disables the device interrupt and pci device, and aborts the internal FCP
9176 * pending I/Os.
9177 **/
9178static void
9179lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
9180{
James Smart0d878412009-10-02 15:16:56 -04009181 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05009182 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -04009183
James Smart75baf692010-06-08 18:31:21 -04009184 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -04009185 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -04009186
James Smarte2af0d22010-03-15 11:25:32 -04009187 /* Block all SCSI devices' I/Os on the host */
9188 lpfc_scsi_dev_block(phba);
9189
9190 /* stop all timers */
9191 lpfc_stop_hba_timers(phba);
9192
James Smart0d878412009-10-02 15:16:56 -04009193 /* Disable interrupt and pci device */
9194 lpfc_sli_disable_intr(phba);
9195 pci_disable_device(phba->pcidev);
James Smart75baf692010-06-08 18:31:21 -04009196
James Smarte2af0d22010-03-15 11:25:32 -04009197 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
9198 lpfc_sli_flush_fcp_rings(phba);
James Smart0d878412009-10-02 15:16:56 -04009199}
9200
9201/**
9202 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
9203 * @phba: pointer to lpfc hba data structure.
9204 *
9205 * This routine is called to prepare the SLI3 device for PCI slot permanently
9206 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
9207 * pending I/Os.
9208 **/
9209static void
James Smart75baf692010-06-08 18:31:21 -04009210lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
James Smart0d878412009-10-02 15:16:56 -04009211{
9212 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05009213 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -04009214 /* Block all SCSI devices' I/Os on the host */
9215 lpfc_scsi_dev_block(phba);
9216
9217 /* stop all timers */
9218 lpfc_stop_hba_timers(phba);
9219
James Smart0d878412009-10-02 15:16:56 -04009220 /* Clean up all driver's outstanding SCSI I/Os */
9221 lpfc_sli_flush_fcp_rings(phba);
9222}
9223
9224/**
James Smart3772a992009-05-22 14:50:54 -04009225 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -04009226 * @pdev: pointer to PCI device.
9227 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009228 *
James Smart3772a992009-05-22 14:50:54 -04009229 * This routine is called from the PCI subsystem for I/O error handling to
9230 * device with SLI-3 interface spec. This function is called by the PCI
9231 * subsystem after a PCI bus error affecting this device has been detected.
9232 * When this function is invoked, it will need to stop all the I/Os and
9233 * interrupt(s) to the device. Once that is done, it will return
9234 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
9235 * as desired.
James Smarte59058c2008-08-24 21:49:00 -04009236 *
9237 * Return codes
James Smart0d878412009-10-02 15:16:56 -04009238 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -04009239 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
9240 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -04009241 **/
James Smart3772a992009-05-22 14:50:54 -04009242static pci_ers_result_t
9243lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009244{
James Smart51ef4c22007-08-02 11:10:31 -04009245 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9246 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009247
James Smart0d878412009-10-02 15:16:56 -04009248 switch (state) {
9249 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -05009250 /* Non-fatal error, prepare for recovery */
9251 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -04009252 return PCI_ERS_RESULT_CAN_RECOVER;
9253 case pci_channel_io_frozen:
9254 /* Fatal error, prepare for slot reset */
9255 lpfc_sli_prep_dev_for_reset(phba);
9256 return PCI_ERS_RESULT_NEED_RESET;
9257 case pci_channel_io_perm_failure:
9258 /* Permanent failure, prepare for device down */
James Smart75baf692010-06-08 18:31:21 -04009259 lpfc_sli_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009260 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -04009261 default:
9262 /* Unknown state, prepare and request slot reset */
9263 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9264 "0472 Unknown PCI error state: x%x\n", state);
9265 lpfc_sli_prep_dev_for_reset(phba);
9266 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -04009267 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06009268}
9269
9270/**
James Smart3772a992009-05-22 14:50:54 -04009271 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -04009272 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009273 *
James Smart3772a992009-05-22 14:50:54 -04009274 * This routine is called from the PCI subsystem for error handling to
9275 * device with SLI-3 interface spec. This is called after PCI bus has been
9276 * reset to restart the PCI card from scratch, as if from a cold-boot.
9277 * During the PCI subsystem error recovery, after driver returns
9278 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
9279 * recovery and then call this routine before calling the .resume method
9280 * to recover the device. This function will initialize the HBA device,
9281 * enable the interrupt, but it will just put the HBA to offline state
9282 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -04009283 *
9284 * Return codes
James Smart3772a992009-05-22 14:50:54 -04009285 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
9286 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -06009287 */
James Smart3772a992009-05-22 14:50:54 -04009288static pci_ers_result_t
9289lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009290{
James Smart51ef4c22007-08-02 11:10:31 -04009291 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9292 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009293 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -05009294 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009295
9296 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +11009297 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -06009298 printk(KERN_ERR "lpfc: Cannot re-enable "
9299 "PCI device after reset.\n");
9300 return PCI_ERS_RESULT_DISCONNECT;
9301 }
9302
James Smart97207482008-12-04 22:39:19 -05009303 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05009304
9305 /*
9306 * As the new kernel behavior of pci_restore_state() API call clears
9307 * device saved_state flag, need to save the restored state again.
9308 */
9309 pci_save_state(pdev);
9310
James Smart97207482008-12-04 22:39:19 -05009311 if (pdev->is_busmaster)
9312 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009313
James Smart92d7f7b2007-06-17 19:56:38 -05009314 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04009315 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05009316 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009317
James Smart5b75da22008-12-04 22:39:35 -05009318 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009319 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05009320 if (intr_mode == LPFC_INTR_ERROR) {
9321 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9322 "0427 Cannot re-enable interrupt after "
9323 "slot reset.\n");
9324 return PCI_ERS_RESULT_DISCONNECT;
9325 } else
9326 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009327
James Smart75baf692010-06-08 18:31:21 -04009328 /* Take device offline, it will perform cleanup */
James Smart618a5232012-06-12 13:54:36 -04009329 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009330 lpfc_offline(phba);
9331 lpfc_sli_brdrestart(phba);
9332
James Smart5b75da22008-12-04 22:39:35 -05009333 /* Log the current active interrupt mode */
9334 lpfc_log_intr_mode(phba, phba->intr_mode);
9335
Linas Vepstas8d63f372007-02-14 14:28:36 -06009336 return PCI_ERS_RESULT_RECOVERED;
9337}
9338
9339/**
James Smart3772a992009-05-22 14:50:54 -04009340 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -04009341 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -06009342 *
James Smart3772a992009-05-22 14:50:54 -04009343 * This routine is called from the PCI subsystem for error handling to device
9344 * with SLI-3 interface spec. It is called when kernel error recovery tells
9345 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
9346 * error recovery. After this call, traffic can start to flow from this device
9347 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009348 */
James Smart3772a992009-05-22 14:50:54 -04009349static void
9350lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009351{
James Smart51ef4c22007-08-02 11:10:31 -04009352 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9353 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009354
James Smarte2af0d22010-03-15 11:25:32 -04009355 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -04009356 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -04009357
9358 /* Clean up Advanced Error Reporting (AER) if needed */
9359 if (phba->hba_flag & HBA_AER_ENABLED)
9360 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009361}
9362
James Smart3772a992009-05-22 14:50:54 -04009363/**
James Smartda0436e2009-05-22 14:51:39 -04009364 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
9365 * @phba: pointer to lpfc hba data structure.
9366 *
9367 * returns the number of ELS/CT IOCBs to reserve
9368 **/
9369int
9370lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
9371{
9372 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
9373
James Smartf1126682009-06-10 17:22:44 -04009374 if (phba->sli_rev == LPFC_SLI_REV4) {
9375 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -04009376 return 10;
James Smartf1126682009-06-10 17:22:44 -04009377 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -04009378 return 25;
James Smartf1126682009-06-10 17:22:44 -04009379 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -04009380 return 50;
James Smartf1126682009-06-10 17:22:44 -04009381 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -04009382 return 100;
James Smart8a9d2e82012-05-09 21:16:12 -04009383 else if (max_xri <= 1536)
James Smart6a9c52c2009-10-02 15:16:51 -04009384 return 150;
James Smart8a9d2e82012-05-09 21:16:12 -04009385 else if (max_xri <= 2048)
9386 return 200;
9387 else
9388 return 250;
James Smartf1126682009-06-10 17:22:44 -04009389 } else
9390 return 0;
James Smartda0436e2009-05-22 14:51:39 -04009391}
9392
9393/**
James Smart52d52442011-05-24 11:42:45 -04009394 * lpfc_write_firmware - attempt to write a firmware image to the port
James Smart52d52442011-05-24 11:42:45 -04009395 * @fw: pointer to firmware image returned from request_firmware.
James Smartce396282012-09-29 11:30:56 -04009396 * @phba: pointer to lpfc hba data structure.
James Smart52d52442011-05-24 11:42:45 -04009397 *
James Smart52d52442011-05-24 11:42:45 -04009398 **/
James Smartce396282012-09-29 11:30:56 -04009399static void
9400lpfc_write_firmware(const struct firmware *fw, void *context)
James Smart52d52442011-05-24 11:42:45 -04009401{
James Smartce396282012-09-29 11:30:56 -04009402 struct lpfc_hba *phba = (struct lpfc_hba *)context;
James Smart6b5151f2012-01-18 16:24:06 -05009403 char fwrev[FW_REV_STR_SIZE];
James Smartce396282012-09-29 11:30:56 -04009404 struct lpfc_grp_hdr *image;
James Smart52d52442011-05-24 11:42:45 -04009405 struct list_head dma_buffer_list;
9406 int i, rc = 0;
9407 struct lpfc_dmabuf *dmabuf, *next;
9408 uint32_t offset = 0, temp_offset = 0;
9409
James Smartc71ab862012-10-31 14:44:33 -04009410 /* It can be null in no-wait mode, sanity check */
James Smartce396282012-09-29 11:30:56 -04009411 if (!fw) {
9412 rc = -ENXIO;
9413 goto out;
9414 }
9415 image = (struct lpfc_grp_hdr *)fw->data;
9416
James Smart52d52442011-05-24 11:42:45 -04009417 INIT_LIST_HEAD(&dma_buffer_list);
James Smart079b5c92011-08-21 21:48:49 -04009418 if ((be32_to_cpu(image->magic_number) != LPFC_GROUP_OJECT_MAGIC_NUM) ||
9419 (bf_get_be32(lpfc_grp_hdr_file_type, image) !=
9420 LPFC_FILE_TYPE_GROUP) ||
9421 (bf_get_be32(lpfc_grp_hdr_id, image) != LPFC_FILE_ID_GROUP) ||
9422 (be32_to_cpu(image->size) != fw->size)) {
James Smart52d52442011-05-24 11:42:45 -04009423 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9424 "3022 Invalid FW image found. "
James Smart079b5c92011-08-21 21:48:49 -04009425 "Magic:%x Type:%x ID:%x\n",
9426 be32_to_cpu(image->magic_number),
9427 bf_get_be32(lpfc_grp_hdr_file_type, image),
9428 bf_get_be32(lpfc_grp_hdr_id, image));
James Smartce396282012-09-29 11:30:56 -04009429 rc = -EINVAL;
9430 goto release_out;
James Smart52d52442011-05-24 11:42:45 -04009431 }
9432 lpfc_decode_firmware_rev(phba, fwrev, 1);
James Smart88a2cfb2011-07-22 18:36:33 -04009433 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
James Smart52d52442011-05-24 11:42:45 -04009434 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartce396282012-09-29 11:30:56 -04009435 "3023 Updating Firmware, Current Version:%s "
James Smart52d52442011-05-24 11:42:45 -04009436 "New Version:%s\n",
James Smart88a2cfb2011-07-22 18:36:33 -04009437 fwrev, image->revision);
James Smart52d52442011-05-24 11:42:45 -04009438 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
9439 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
9440 GFP_KERNEL);
9441 if (!dmabuf) {
9442 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -04009443 goto release_out;
James Smart52d52442011-05-24 11:42:45 -04009444 }
9445 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
9446 SLI4_PAGE_SIZE,
9447 &dmabuf->phys,
9448 GFP_KERNEL);
9449 if (!dmabuf->virt) {
9450 kfree(dmabuf);
9451 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -04009452 goto release_out;
James Smart52d52442011-05-24 11:42:45 -04009453 }
9454 list_add_tail(&dmabuf->list, &dma_buffer_list);
9455 }
9456 while (offset < fw->size) {
9457 temp_offset = offset;
9458 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
James Smart079b5c92011-08-21 21:48:49 -04009459 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
James Smart52d52442011-05-24 11:42:45 -04009460 memcpy(dmabuf->virt,
9461 fw->data + temp_offset,
James Smart079b5c92011-08-21 21:48:49 -04009462 fw->size - temp_offset);
9463 temp_offset = fw->size;
James Smart52d52442011-05-24 11:42:45 -04009464 break;
9465 }
James Smart52d52442011-05-24 11:42:45 -04009466 memcpy(dmabuf->virt, fw->data + temp_offset,
9467 SLI4_PAGE_SIZE);
James Smart88a2cfb2011-07-22 18:36:33 -04009468 temp_offset += SLI4_PAGE_SIZE;
James Smart52d52442011-05-24 11:42:45 -04009469 }
9470 rc = lpfc_wr_object(phba, &dma_buffer_list,
9471 (fw->size - offset), &offset);
James Smartce396282012-09-29 11:30:56 -04009472 if (rc)
9473 goto release_out;
James Smart52d52442011-05-24 11:42:45 -04009474 }
9475 rc = offset;
9476 }
James Smartce396282012-09-29 11:30:56 -04009477
9478release_out:
James Smart52d52442011-05-24 11:42:45 -04009479 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
9480 list_del(&dmabuf->list);
9481 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
9482 dmabuf->virt, dmabuf->phys);
9483 kfree(dmabuf);
9484 }
James Smartce396282012-09-29 11:30:56 -04009485 release_firmware(fw);
9486out:
9487 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartc71ab862012-10-31 14:44:33 -04009488 "3024 Firmware update done: %d.\n", rc);
James Smartce396282012-09-29 11:30:56 -04009489 return;
James Smart52d52442011-05-24 11:42:45 -04009490}
9491
9492/**
James Smartc71ab862012-10-31 14:44:33 -04009493 * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
9494 * @phba: pointer to lpfc hba data structure.
9495 *
9496 * This routine is called to perform Linux generic firmware upgrade on device
9497 * that supports such feature.
9498 **/
9499int
9500lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
9501{
9502 uint8_t file_name[ELX_MODEL_NAME_SIZE];
9503 int ret;
9504 const struct firmware *fw;
9505
9506 /* Only supported on SLI4 interface type 2 for now */
9507 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
9508 LPFC_SLI_INTF_IF_TYPE_2)
9509 return -EPERM;
9510
9511 snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
9512
9513 if (fw_upgrade == INT_FW_UPGRADE) {
9514 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
9515 file_name, &phba->pcidev->dev,
9516 GFP_KERNEL, (void *)phba,
9517 lpfc_write_firmware);
9518 } else if (fw_upgrade == RUN_FW_UPGRADE) {
9519 ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
9520 if (!ret)
9521 lpfc_write_firmware(fw, (void *)phba);
9522 } else {
9523 ret = -EINVAL;
9524 }
9525
9526 return ret;
9527}
9528
9529/**
James Smartda0436e2009-05-22 14:51:39 -04009530 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
9531 * @pdev: pointer to PCI device
9532 * @pid: pointer to PCI device identifier
9533 *
9534 * This routine is called from the kernel's PCI subsystem to device with
9535 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
9536 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
9537 * information of the device and driver to see if the driver state that it
9538 * can support this kind of device. If the match is successful, the driver
9539 * core invokes this routine. If this routine determines it can claim the HBA,
9540 * it does all the initialization that it needs to do to handle the HBA
9541 * properly.
9542 *
9543 * Return code
9544 * 0 - driver can claim the device
9545 * negative value - driver can not claim the device
9546 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009547static int
James Smartda0436e2009-05-22 14:51:39 -04009548lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
9549{
9550 struct lpfc_hba *phba;
9551 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04009552 struct Scsi_Host *shost = NULL;
James Smartce396282012-09-29 11:30:56 -04009553 int error, ret;
James Smartda0436e2009-05-22 14:51:39 -04009554 uint32_t cfg_mode, intr_mode;
James Smart67d12732012-08-03 12:36:13 -04009555 int adjusted_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04009556
9557 /* Allocate memory for HBA structure */
9558 phba = lpfc_hba_alloc(pdev);
9559 if (!phba)
9560 return -ENOMEM;
9561
9562 /* Perform generic PCI device enabling operation */
9563 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -04009564 if (error)
James Smartda0436e2009-05-22 14:51:39 -04009565 goto out_free_phba;
James Smartda0436e2009-05-22 14:51:39 -04009566
9567 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
9568 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
9569 if (error)
9570 goto out_disable_pci_dev;
9571
9572 /* Set up SLI-4 specific device PCI memory space */
9573 error = lpfc_sli4_pci_mem_setup(phba);
9574 if (error) {
9575 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9576 "1410 Failed to set up pci memory space.\n");
9577 goto out_disable_pci_dev;
9578 }
9579
9580 /* Set up phase-1 common device driver resources */
9581 error = lpfc_setup_driver_resource_phase1(phba);
9582 if (error) {
9583 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9584 "1411 Failed to set up driver resource.\n");
9585 goto out_unset_pci_mem_s4;
9586 }
9587
9588 /* Set up SLI-4 Specific device driver resources */
9589 error = lpfc_sli4_driver_resource_setup(phba);
9590 if (error) {
9591 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9592 "1412 Failed to set up driver resource.\n");
9593 goto out_unset_pci_mem_s4;
9594 }
9595
9596 /* Initialize and populate the iocb list per host */
James Smart2a9bf3d2010-06-07 15:24:45 -04009597
9598 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9599 "2821 initialize iocb list %d.\n",
9600 phba->cfg_iocb_cnt*1024);
9601 error = lpfc_init_iocb_list(phba, phba->cfg_iocb_cnt*1024);
9602
James Smartda0436e2009-05-22 14:51:39 -04009603 if (error) {
9604 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9605 "1413 Failed to initialize iocb list.\n");
9606 goto out_unset_driver_resource_s4;
9607 }
9608
James Smart19ca7602010-11-20 23:11:55 -05009609 INIT_LIST_HEAD(&phba->active_rrq_list);
James Smart7d791df2011-07-22 18:37:52 -04009610 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
James Smart19ca7602010-11-20 23:11:55 -05009611
James Smartda0436e2009-05-22 14:51:39 -04009612 /* Set up common device driver resources */
9613 error = lpfc_setup_driver_resource_phase2(phba);
9614 if (error) {
9615 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9616 "1414 Failed to set up driver resource.\n");
9617 goto out_free_iocb_list;
9618 }
9619
James Smart079b5c92011-08-21 21:48:49 -04009620 /* Get the default values for Model Name and Description */
9621 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
9622
James Smartda0436e2009-05-22 14:51:39 -04009623 /* Create SCSI host to the physical port */
9624 error = lpfc_create_shost(phba);
9625 if (error) {
9626 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9627 "1415 Failed to create scsi host.\n");
9628 goto out_unset_driver_resource;
9629 }
9630
9631 /* Configure sysfs attributes */
9632 vport = phba->pport;
9633 error = lpfc_alloc_sysfs_attr(vport);
9634 if (error) {
9635 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9636 "1416 Failed to allocate sysfs attr\n");
9637 goto out_destroy_shost;
9638 }
9639
James Smart6669f9b2009-10-02 15:16:45 -04009640 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -04009641 /* Now, trying to enable interrupt and bring up the device */
9642 cfg_mode = phba->cfg_use_msi;
James Smartda0436e2009-05-22 14:51:39 -04009643
James Smart7b15db32013-01-03 15:43:29 -05009644 /* Put device to a known state before enabling interrupt */
9645 lpfc_stop_port(phba);
9646 /* Configure and enable interrupt */
9647 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
9648 if (intr_mode == LPFC_INTR_ERROR) {
9649 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9650 "0426 Failed to enable interrupt.\n");
9651 error = -ENODEV;
9652 goto out_free_sysfs_attr;
James Smartda0436e2009-05-22 14:51:39 -04009653 }
James Smart7b15db32013-01-03 15:43:29 -05009654 /* Default to single EQ for non-MSI-X */
9655 if (phba->intr_type != MSIX)
9656 adjusted_fcp_io_channel = 1;
9657 else
9658 adjusted_fcp_io_channel = phba->cfg_fcp_io_channel;
9659 phba->cfg_fcp_io_channel = adjusted_fcp_io_channel;
9660 /* Set up SLI-4 HBA */
9661 if (lpfc_sli4_hba_setup(phba)) {
9662 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9663 "1421 Failed to set up hba\n");
9664 error = -ENODEV;
9665 goto out_disable_intr;
9666 }
9667
9668 /* Log the current active interrupt mode */
9669 phba->intr_mode = intr_mode;
9670 lpfc_log_intr_mode(phba, intr_mode);
James Smartda0436e2009-05-22 14:51:39 -04009671
9672 /* Perform post initialization setup */
9673 lpfc_post_init_setup(phba);
9674
James Smartc71ab862012-10-31 14:44:33 -04009675 /* check for firmware upgrade or downgrade */
9676 if (phba->cfg_request_firmware_upgrade)
9677 ret = lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
James Smart52d52442011-05-24 11:42:45 -04009678
James Smart1c6834a2009-07-19 10:01:26 -04009679 /* Check if there are static vports to be created. */
9680 lpfc_create_static_vport(phba);
James Smartda0436e2009-05-22 14:51:39 -04009681 return 0;
9682
9683out_disable_intr:
9684 lpfc_sli4_disable_intr(phba);
9685out_free_sysfs_attr:
9686 lpfc_free_sysfs_attr(vport);
9687out_destroy_shost:
9688 lpfc_destroy_shost(phba);
9689out_unset_driver_resource:
9690 lpfc_unset_driver_resource_phase2(phba);
9691out_free_iocb_list:
9692 lpfc_free_iocb_list(phba);
9693out_unset_driver_resource_s4:
9694 lpfc_sli4_driver_resource_unset(phba);
9695out_unset_pci_mem_s4:
9696 lpfc_sli4_pci_mem_unset(phba);
9697out_disable_pci_dev:
9698 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04009699 if (shost)
9700 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -04009701out_free_phba:
9702 lpfc_hba_free(phba);
9703 return error;
9704}
9705
9706/**
9707 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
9708 * @pdev: pointer to PCI device
9709 *
9710 * This routine is called from the kernel's PCI subsystem to device with
9711 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
9712 * removed from PCI bus, it performs all the necessary cleanup for the HBA
9713 * device to be removed from the PCI subsystem properly.
9714 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009715static void
James Smartda0436e2009-05-22 14:51:39 -04009716lpfc_pci_remove_one_s4(struct pci_dev *pdev)
9717{
9718 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9719 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
9720 struct lpfc_vport **vports;
9721 struct lpfc_hba *phba = vport->phba;
9722 int i;
9723
9724 /* Mark the device unloading flag */
9725 spin_lock_irq(&phba->hbalock);
9726 vport->load_flag |= FC_UNLOADING;
9727 spin_unlock_irq(&phba->hbalock);
9728
9729 /* Free the HBA sysfs attributes */
9730 lpfc_free_sysfs_attr(vport);
9731
9732 /* Release all the vports against this physical port */
9733 vports = lpfc_create_vport_work_array(phba);
9734 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -04009735 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
9736 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
9737 continue;
James Smartda0436e2009-05-22 14:51:39 -04009738 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -04009739 }
James Smartda0436e2009-05-22 14:51:39 -04009740 lpfc_destroy_vport_work_array(phba, vports);
9741
9742 /* Remove FC host and then SCSI host with the physical port */
9743 fc_remove_host(shost);
9744 scsi_remove_host(shost);
9745
9746 /* Perform cleanup on the physical port */
9747 lpfc_cleanup(vport);
9748
9749 /*
9750 * Bring down the SLI Layer. This step disables all interrupts,
9751 * clears the rings, discards all mailbox commands, and resets
9752 * the HBA FCoE function.
9753 */
9754 lpfc_debugfs_terminate(vport);
9755 lpfc_sli4_hba_unset(phba);
9756
9757 spin_lock_irq(&phba->hbalock);
9758 list_del_init(&vport->listentry);
9759 spin_unlock_irq(&phba->hbalock);
9760
James Smart3677a3a2010-09-29 11:19:14 -04009761 /* Perform scsi free before driver resource_unset since scsi
James Smartda0436e2009-05-22 14:51:39 -04009762 * buffers are released to their corresponding pools here.
9763 */
9764 lpfc_scsi_free(phba);
James Smart67d12732012-08-03 12:36:13 -04009765
James Smartda0436e2009-05-22 14:51:39 -04009766 lpfc_sli4_driver_resource_unset(phba);
9767
9768 /* Unmap adapter Control and Doorbell registers */
9769 lpfc_sli4_pci_mem_unset(phba);
9770
9771 /* Release PCI resources and disable device's PCI function */
9772 scsi_host_put(shost);
9773 lpfc_disable_pci_dev(phba);
9774
9775 /* Finally, free the driver's device data structure */
9776 lpfc_hba_free(phba);
9777
9778 return;
9779}
9780
9781/**
9782 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
9783 * @pdev: pointer to PCI device
9784 * @msg: power management message
9785 *
9786 * This routine is called from the kernel's PCI subsystem to support system
9787 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
9788 * this method, it quiesces the device by stopping the driver's worker
9789 * thread for the device, turning off device's interrupt and DMA, and bring
9790 * the device offline. Note that as the driver implements the minimum PM
9791 * requirements to a power-aware driver's PM support for suspend/resume -- all
9792 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
9793 * method call will be treated as SUSPEND and the driver will fully
9794 * reinitialize its device during resume() method call, the driver will set
9795 * device to PCI_D3hot state in PCI config space instead of setting it
9796 * according to the @msg provided by the PM.
9797 *
9798 * Return code
9799 * 0 - driver suspended the device
9800 * Error otherwise
9801 **/
9802static int
9803lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
9804{
9805 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9806 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9807
9808 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart75baf692010-06-08 18:31:21 -04009809 "2843 PCI device Power Management suspend.\n");
James Smartda0436e2009-05-22 14:51:39 -04009810
9811 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -04009812 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smartda0436e2009-05-22 14:51:39 -04009813 lpfc_offline(phba);
9814 kthread_stop(phba->worker_thread);
9815
9816 /* Disable interrupt from device */
9817 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -04009818 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04009819
9820 /* Save device state to PCI config space */
9821 pci_save_state(pdev);
9822 pci_set_power_state(pdev, PCI_D3hot);
9823
9824 return 0;
9825}
9826
9827/**
9828 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
9829 * @pdev: pointer to PCI device
9830 *
9831 * This routine is called from the kernel's PCI subsystem to support system
9832 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
9833 * this method, it restores the device's PCI config space state and fully
9834 * reinitializes the device and brings it online. Note that as the driver
9835 * implements the minimum PM requirements to a power-aware driver's PM for
9836 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
9837 * to the suspend() method call will be treated as SUSPEND and the driver
9838 * will fully reinitialize its device during resume() method call, the device
9839 * will be set to PCI_D0 directly in PCI config space before restoring the
9840 * state.
9841 *
9842 * Return code
9843 * 0 - driver suspended the device
9844 * Error otherwise
9845 **/
9846static int
9847lpfc_pci_resume_one_s4(struct pci_dev *pdev)
9848{
9849 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9850 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9851 uint32_t intr_mode;
9852 int error;
9853
9854 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9855 "0292 PCI device Power Management resume.\n");
9856
9857 /* Restore device state from PCI config space */
9858 pci_set_power_state(pdev, PCI_D0);
9859 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05009860
9861 /*
9862 * As the new kernel behavior of pci_restore_state() API call clears
9863 * device saved_state flag, need to save the restored state again.
9864 */
9865 pci_save_state(pdev);
9866
James Smartda0436e2009-05-22 14:51:39 -04009867 if (pdev->is_busmaster)
9868 pci_set_master(pdev);
9869
9870 /* Startup the kernel thread for this host adapter. */
9871 phba->worker_thread = kthread_run(lpfc_do_work, phba,
9872 "lpfc_worker_%d", phba->brd_no);
9873 if (IS_ERR(phba->worker_thread)) {
9874 error = PTR_ERR(phba->worker_thread);
9875 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9876 "0293 PM resume failed to start worker "
9877 "thread: error=x%x.\n", error);
9878 return error;
9879 }
9880
9881 /* Configure and enable interrupt */
9882 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
9883 if (intr_mode == LPFC_INTR_ERROR) {
9884 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9885 "0294 PM resume Failed to enable interrupt\n");
9886 return -EIO;
9887 } else
9888 phba->intr_mode = intr_mode;
9889
9890 /* Restart HBA and bring it online */
9891 lpfc_sli_brdrestart(phba);
9892 lpfc_online(phba);
9893
9894 /* Log the current active interrupt mode */
9895 lpfc_log_intr_mode(phba, phba->intr_mode);
9896
9897 return 0;
9898}
9899
9900/**
James Smart75baf692010-06-08 18:31:21 -04009901 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
9902 * @phba: pointer to lpfc hba data structure.
9903 *
9904 * This routine is called to prepare the SLI4 device for PCI slot recover. It
9905 * aborts all the outstanding SCSI I/Os to the pci device.
9906 **/
9907static void
9908lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
9909{
9910 struct lpfc_sli *psli = &phba->sli;
9911 struct lpfc_sli_ring *pring;
9912
9913 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9914 "2828 PCI channel I/O abort preparing for recovery\n");
9915 /*
9916 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
9917 * and let the SCSI mid-layer to retry them to recover.
9918 */
9919 pring = &psli->ring[psli->fcp_ring];
9920 lpfc_sli_abort_iocb_ring(phba, pring);
9921}
9922
9923/**
9924 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
9925 * @phba: pointer to lpfc hba data structure.
9926 *
9927 * This routine is called to prepare the SLI4 device for PCI slot reset. It
9928 * disables the device interrupt and pci device, and aborts the internal FCP
9929 * pending I/Os.
9930 **/
9931static void
9932lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
9933{
9934 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9935 "2826 PCI channel disable preparing for reset\n");
9936
9937 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -04009938 lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
James Smart75baf692010-06-08 18:31:21 -04009939
9940 /* Block all SCSI devices' I/Os on the host */
9941 lpfc_scsi_dev_block(phba);
9942
9943 /* stop all timers */
9944 lpfc_stop_hba_timers(phba);
9945
9946 /* Disable interrupt and pci device */
9947 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -04009948 lpfc_sli4_queue_destroy(phba);
James Smart75baf692010-06-08 18:31:21 -04009949 pci_disable_device(phba->pcidev);
9950
9951 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
9952 lpfc_sli_flush_fcp_rings(phba);
9953}
9954
9955/**
9956 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
9957 * @phba: pointer to lpfc hba data structure.
9958 *
9959 * This routine is called to prepare the SLI4 device for PCI slot permanently
9960 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
9961 * pending I/Os.
9962 **/
9963static void
9964lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
9965{
9966 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9967 "2827 PCI channel permanent disable for failure\n");
9968
9969 /* Block all SCSI devices' I/Os on the host */
9970 lpfc_scsi_dev_block(phba);
9971
9972 /* stop all timers */
9973 lpfc_stop_hba_timers(phba);
9974
9975 /* Clean up all driver's outstanding SCSI I/Os */
9976 lpfc_sli_flush_fcp_rings(phba);
9977}
9978
9979/**
James Smartda0436e2009-05-22 14:51:39 -04009980 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
9981 * @pdev: pointer to PCI device.
9982 * @state: the current PCI connection state.
9983 *
9984 * This routine is called from the PCI subsystem for error handling to device
9985 * with SLI-4 interface spec. This function is called by the PCI subsystem
9986 * after a PCI bus error affecting this device has been detected. When this
9987 * function is invoked, it will need to stop all the I/Os and interrupt(s)
9988 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
9989 * for the PCI subsystem to perform proper recovery as desired.
9990 *
9991 * Return codes
9992 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
9993 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
9994 **/
9995static pci_ers_result_t
9996lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
9997{
James Smart75baf692010-06-08 18:31:21 -04009998 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9999 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10000
10001 switch (state) {
10002 case pci_channel_io_normal:
10003 /* Non-fatal error, prepare for recovery */
10004 lpfc_sli4_prep_dev_for_recover(phba);
10005 return PCI_ERS_RESULT_CAN_RECOVER;
10006 case pci_channel_io_frozen:
10007 /* Fatal error, prepare for slot reset */
10008 lpfc_sli4_prep_dev_for_reset(phba);
10009 return PCI_ERS_RESULT_NEED_RESET;
10010 case pci_channel_io_perm_failure:
10011 /* Permanent failure, prepare for device down */
10012 lpfc_sli4_prep_dev_for_perm_failure(phba);
10013 return PCI_ERS_RESULT_DISCONNECT;
10014 default:
10015 /* Unknown state, prepare and request slot reset */
10016 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10017 "2825 Unknown PCI error state: x%x\n", state);
10018 lpfc_sli4_prep_dev_for_reset(phba);
10019 return PCI_ERS_RESULT_NEED_RESET;
10020 }
James Smartda0436e2009-05-22 14:51:39 -040010021}
10022
10023/**
10024 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
10025 * @pdev: pointer to PCI device.
10026 *
10027 * This routine is called from the PCI subsystem for error handling to device
10028 * with SLI-4 interface spec. It is called after PCI bus has been reset to
10029 * restart the PCI card from scratch, as if from a cold-boot. During the
10030 * PCI subsystem error recovery, after the driver returns
10031 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
10032 * recovery and then call this routine before calling the .resume method to
10033 * recover the device. This function will initialize the HBA device, enable
10034 * the interrupt, but it will just put the HBA to offline state without
10035 * passing any I/O traffic.
10036 *
10037 * Return codes
10038 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10039 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10040 */
10041static pci_ers_result_t
10042lpfc_io_slot_reset_s4(struct pci_dev *pdev)
10043{
James Smart75baf692010-06-08 18:31:21 -040010044 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10045 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10046 struct lpfc_sli *psli = &phba->sli;
10047 uint32_t intr_mode;
10048
10049 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
10050 if (pci_enable_device_mem(pdev)) {
10051 printk(KERN_ERR "lpfc: Cannot re-enable "
10052 "PCI device after reset.\n");
10053 return PCI_ERS_RESULT_DISCONNECT;
10054 }
10055
10056 pci_restore_state(pdev);
James Smart0a96e972011-07-22 18:37:28 -040010057
10058 /*
10059 * As the new kernel behavior of pci_restore_state() API call clears
10060 * device saved_state flag, need to save the restored state again.
10061 */
10062 pci_save_state(pdev);
10063
James Smart75baf692010-06-08 18:31:21 -040010064 if (pdev->is_busmaster)
10065 pci_set_master(pdev);
10066
10067 spin_lock_irq(&phba->hbalock);
10068 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
10069 spin_unlock_irq(&phba->hbalock);
10070
10071 /* Configure and enable interrupt */
10072 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
10073 if (intr_mode == LPFC_INTR_ERROR) {
10074 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10075 "2824 Cannot re-enable interrupt after "
10076 "slot reset.\n");
10077 return PCI_ERS_RESULT_DISCONNECT;
10078 } else
10079 phba->intr_mode = intr_mode;
10080
10081 /* Log the current active interrupt mode */
10082 lpfc_log_intr_mode(phba, phba->intr_mode);
10083
James Smartda0436e2009-05-22 14:51:39 -040010084 return PCI_ERS_RESULT_RECOVERED;
10085}
10086
10087/**
10088 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
10089 * @pdev: pointer to PCI device
10090 *
10091 * This routine is called from the PCI subsystem for error handling to device
10092 * with SLI-4 interface spec. It is called when kernel error recovery tells
10093 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
10094 * error recovery. After this call, traffic can start to flow from this device
10095 * again.
10096 **/
10097static void
10098lpfc_io_resume_s4(struct pci_dev *pdev)
10099{
James Smart75baf692010-06-08 18:31:21 -040010100 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10101 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10102
10103 /*
10104 * In case of slot reset, as function reset is performed through
10105 * mailbox command which needs DMA to be enabled, this operation
10106 * has to be moved to the io resume phase. Taking device offline
10107 * will perform the necessary cleanup.
10108 */
10109 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
10110 /* Perform device reset */
James Smart618a5232012-06-12 13:54:36 -040010111 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040010112 lpfc_offline(phba);
10113 lpfc_sli_brdrestart(phba);
10114 /* Bring the device back online */
10115 lpfc_online(phba);
10116 }
10117
10118 /* Clean up Advanced Error Reporting (AER) if needed */
10119 if (phba->hba_flag & HBA_AER_ENABLED)
10120 pci_cleanup_aer_uncorrect_error_status(pdev);
James Smartda0436e2009-05-22 14:51:39 -040010121}
10122
10123/**
James Smart3772a992009-05-22 14:50:54 -040010124 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
10125 * @pdev: pointer to PCI device
10126 * @pid: pointer to PCI device identifier
10127 *
10128 * This routine is to be registered to the kernel's PCI subsystem. When an
10129 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
10130 * at PCI device-specific information of the device and driver to see if the
10131 * driver state that it can support this kind of device. If the match is
10132 * successful, the driver core invokes this routine. This routine dispatches
10133 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
10134 * do all the initialization that it needs to do to handle the HBA device
10135 * properly.
10136 *
10137 * Return code
10138 * 0 - driver can claim the device
10139 * negative value - driver can not claim the device
10140 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010141static int
James Smart3772a992009-05-22 14:50:54 -040010142lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
10143{
10144 int rc;
James Smart8fa38512009-07-19 10:01:03 -040010145 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -040010146
James Smart28baac72010-02-12 14:42:03 -050010147 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -040010148 return -ENODEV;
10149
James Smart8fa38512009-07-19 10:01:03 -040010150 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -050010151 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -040010152 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040010153 else
James Smart3772a992009-05-22 14:50:54 -040010154 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040010155
James Smart3772a992009-05-22 14:50:54 -040010156 return rc;
10157}
10158
10159/**
10160 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
10161 * @pdev: pointer to PCI device
10162 *
10163 * This routine is to be registered to the kernel's PCI subsystem. When an
10164 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
10165 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
10166 * remove routine, which will perform all the necessary cleanup for the
10167 * device to be removed from the PCI subsystem properly.
10168 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010169static void
James Smart3772a992009-05-22 14:50:54 -040010170lpfc_pci_remove_one(struct pci_dev *pdev)
10171{
10172 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10173 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10174
10175 switch (phba->pci_dev_grp) {
10176 case LPFC_PCI_DEV_LP:
10177 lpfc_pci_remove_one_s3(pdev);
10178 break;
James Smartda0436e2009-05-22 14:51:39 -040010179 case LPFC_PCI_DEV_OC:
10180 lpfc_pci_remove_one_s4(pdev);
10181 break;
James Smart3772a992009-05-22 14:50:54 -040010182 default:
10183 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10184 "1424 Invalid PCI device group: 0x%x\n",
10185 phba->pci_dev_grp);
10186 break;
10187 }
10188 return;
10189}
10190
10191/**
10192 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
10193 * @pdev: pointer to PCI device
10194 * @msg: power management message
10195 *
10196 * This routine is to be registered to the kernel's PCI subsystem to support
10197 * system Power Management (PM). When PM invokes this method, it dispatches
10198 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
10199 * suspend the device.
10200 *
10201 * Return code
10202 * 0 - driver suspended the device
10203 * Error otherwise
10204 **/
10205static int
10206lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
10207{
10208 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10209 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10210 int rc = -ENODEV;
10211
10212 switch (phba->pci_dev_grp) {
10213 case LPFC_PCI_DEV_LP:
10214 rc = lpfc_pci_suspend_one_s3(pdev, msg);
10215 break;
James Smartda0436e2009-05-22 14:51:39 -040010216 case LPFC_PCI_DEV_OC:
10217 rc = lpfc_pci_suspend_one_s4(pdev, msg);
10218 break;
James Smart3772a992009-05-22 14:50:54 -040010219 default:
10220 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10221 "1425 Invalid PCI device group: 0x%x\n",
10222 phba->pci_dev_grp);
10223 break;
10224 }
10225 return rc;
10226}
10227
10228/**
10229 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
10230 * @pdev: pointer to PCI device
10231 *
10232 * This routine is to be registered to the kernel's PCI subsystem to support
10233 * system Power Management (PM). When PM invokes this method, it dispatches
10234 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
10235 * resume the device.
10236 *
10237 * Return code
10238 * 0 - driver suspended the device
10239 * Error otherwise
10240 **/
10241static int
10242lpfc_pci_resume_one(struct pci_dev *pdev)
10243{
10244 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10245 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10246 int rc = -ENODEV;
10247
10248 switch (phba->pci_dev_grp) {
10249 case LPFC_PCI_DEV_LP:
10250 rc = lpfc_pci_resume_one_s3(pdev);
10251 break;
James Smartda0436e2009-05-22 14:51:39 -040010252 case LPFC_PCI_DEV_OC:
10253 rc = lpfc_pci_resume_one_s4(pdev);
10254 break;
James Smart3772a992009-05-22 14:50:54 -040010255 default:
10256 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10257 "1426 Invalid PCI device group: 0x%x\n",
10258 phba->pci_dev_grp);
10259 break;
10260 }
10261 return rc;
10262}
10263
10264/**
10265 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
10266 * @pdev: pointer to PCI device.
10267 * @state: the current PCI connection state.
10268 *
10269 * This routine is registered to the PCI subsystem for error handling. This
10270 * function is called by the PCI subsystem after a PCI bus error affecting
10271 * this device has been detected. When this routine is invoked, it dispatches
10272 * the action to the proper SLI-3 or SLI-4 device error detected handling
10273 * routine, which will perform the proper error detected operation.
10274 *
10275 * Return codes
10276 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10277 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10278 **/
10279static pci_ers_result_t
10280lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
10281{
10282 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10283 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10284 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
10285
10286 switch (phba->pci_dev_grp) {
10287 case LPFC_PCI_DEV_LP:
10288 rc = lpfc_io_error_detected_s3(pdev, state);
10289 break;
James Smartda0436e2009-05-22 14:51:39 -040010290 case LPFC_PCI_DEV_OC:
10291 rc = lpfc_io_error_detected_s4(pdev, state);
10292 break;
James Smart3772a992009-05-22 14:50:54 -040010293 default:
10294 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10295 "1427 Invalid PCI device group: 0x%x\n",
10296 phba->pci_dev_grp);
10297 break;
10298 }
10299 return rc;
10300}
10301
10302/**
10303 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
10304 * @pdev: pointer to PCI device.
10305 *
10306 * This routine is registered to the PCI subsystem for error handling. This
10307 * function is called after PCI bus has been reset to restart the PCI card
10308 * from scratch, as if from a cold-boot. When this routine is invoked, it
10309 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
10310 * routine, which will perform the proper device reset.
10311 *
10312 * Return codes
10313 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10314 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10315 **/
10316static pci_ers_result_t
10317lpfc_io_slot_reset(struct pci_dev *pdev)
10318{
10319 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10320 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10321 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
10322
10323 switch (phba->pci_dev_grp) {
10324 case LPFC_PCI_DEV_LP:
10325 rc = lpfc_io_slot_reset_s3(pdev);
10326 break;
James Smartda0436e2009-05-22 14:51:39 -040010327 case LPFC_PCI_DEV_OC:
10328 rc = lpfc_io_slot_reset_s4(pdev);
10329 break;
James Smart3772a992009-05-22 14:50:54 -040010330 default:
10331 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10332 "1428 Invalid PCI device group: 0x%x\n",
10333 phba->pci_dev_grp);
10334 break;
10335 }
10336 return rc;
10337}
10338
10339/**
10340 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
10341 * @pdev: pointer to PCI device
10342 *
10343 * This routine is registered to the PCI subsystem for error handling. It
10344 * is called when kernel error recovery tells the lpfc driver that it is
10345 * OK to resume normal PCI operation after PCI bus error recovery. When
10346 * this routine is invoked, it dispatches the action to the proper SLI-3
10347 * or SLI-4 device io_resume routine, which will resume the device operation.
10348 **/
10349static void
10350lpfc_io_resume(struct pci_dev *pdev)
10351{
10352 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10353 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10354
10355 switch (phba->pci_dev_grp) {
10356 case LPFC_PCI_DEV_LP:
10357 lpfc_io_resume_s3(pdev);
10358 break;
James Smartda0436e2009-05-22 14:51:39 -040010359 case LPFC_PCI_DEV_OC:
10360 lpfc_io_resume_s4(pdev);
10361 break;
James Smart3772a992009-05-22 14:50:54 -040010362 default:
10363 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10364 "1429 Invalid PCI device group: 0x%x\n",
10365 phba->pci_dev_grp);
10366 break;
10367 }
10368 return;
10369}
10370
dea31012005-04-17 16:05:31 -050010371static struct pci_device_id lpfc_id_table[] = {
10372 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
10373 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -040010374 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
10375 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010376 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
10377 PCI_ANY_ID, PCI_ANY_ID, },
10378 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
10379 PCI_ANY_ID, PCI_ANY_ID, },
10380 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
10381 PCI_ANY_ID, PCI_ANY_ID, },
10382 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
10383 PCI_ANY_ID, PCI_ANY_ID, },
10384 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
10385 PCI_ANY_ID, PCI_ANY_ID, },
10386 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
10387 PCI_ANY_ID, PCI_ANY_ID, },
10388 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
10389 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050010390 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
10391 PCI_ANY_ID, PCI_ANY_ID, },
10392 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
10393 PCI_ANY_ID, PCI_ANY_ID, },
10394 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
10395 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010396 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
10397 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050010398 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
10399 PCI_ANY_ID, PCI_ANY_ID, },
10400 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
10401 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010402 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
10403 PCI_ANY_ID, PCI_ANY_ID, },
10404 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
10405 PCI_ANY_ID, PCI_ANY_ID, },
10406 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
10407 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -040010408 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
10409 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050010410 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
10411 PCI_ANY_ID, PCI_ANY_ID, },
10412 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
10413 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010414 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
10415 PCI_ANY_ID, PCI_ANY_ID, },
10416 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
10417 PCI_ANY_ID, PCI_ANY_ID, },
10418 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
10419 PCI_ANY_ID, PCI_ANY_ID, },
10420 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
10421 PCI_ANY_ID, PCI_ANY_ID, },
10422 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
10423 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050010424 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
10425 PCI_ANY_ID, PCI_ANY_ID, },
10426 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
10427 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -040010428 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
10429 PCI_ANY_ID, PCI_ANY_ID, },
10430 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
10431 PCI_ANY_ID, PCI_ANY_ID, },
10432 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
10433 PCI_ANY_ID, PCI_ANY_ID, },
10434 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
10435 PCI_ANY_ID, PCI_ANY_ID, },
10436 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
10437 PCI_ANY_ID, PCI_ANY_ID, },
10438 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
10439 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -040010440 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
10441 PCI_ANY_ID, PCI_ANY_ID, },
10442 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
10443 PCI_ANY_ID, PCI_ANY_ID, },
10444 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
10445 PCI_ANY_ID, PCI_ANY_ID, },
James Smart3772a992009-05-22 14:50:54 -040010446 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
10447 PCI_ANY_ID, PCI_ANY_ID, },
James Smarta747c9c2009-11-18 15:41:10 -050010448 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
10449 PCI_ANY_ID, PCI_ANY_ID, },
10450 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
James Smart6669f9b2009-10-02 15:16:45 -040010451 PCI_ANY_ID, PCI_ANY_ID, },
James Smart98fc5dd2010-06-07 15:24:29 -040010452 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BALIUS,
10453 PCI_ANY_ID, PCI_ANY_ID, },
James Smart085c6472010-11-20 23:11:37 -050010454 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC,
10455 PCI_ANY_ID, PCI_ANY_ID, },
10456 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE,
10457 PCI_ANY_ID, PCI_ANY_ID, },
James Smartc0c11512011-05-24 11:41:34 -040010458 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC_VF,
10459 PCI_ANY_ID, PCI_ANY_ID, },
10460 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE_VF,
10461 PCI_ANY_ID, PCI_ANY_ID, },
James Smartf8cafd32012-08-03 12:42:51 -040010462 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SKYHAWK,
10463 PCI_ANY_ID, PCI_ANY_ID, },
10464 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SKYHAWK_VF,
10465 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010466 { 0 }
10467};
10468
10469MODULE_DEVICE_TABLE(pci, lpfc_id_table);
10470
Stephen Hemmingera55b2d22012-09-07 09:33:16 -070010471static const struct pci_error_handlers lpfc_err_handler = {
Linas Vepstas8d63f372007-02-14 14:28:36 -060010472 .error_detected = lpfc_io_error_detected,
10473 .slot_reset = lpfc_io_slot_reset,
10474 .resume = lpfc_io_resume,
10475};
10476
dea31012005-04-17 16:05:31 -050010477static struct pci_driver lpfc_driver = {
10478 .name = LPFC_DRIVER_NAME,
10479 .id_table = lpfc_id_table,
10480 .probe = lpfc_pci_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010481 .remove = lpfc_pci_remove_one,
James Smart3a55b532008-12-04 22:38:54 -050010482 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -040010483 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -050010484 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -050010485};
10486
James Smart3ef6d242012-01-18 16:23:48 -050010487static const struct file_operations lpfc_mgmt_fop = {
Al Viro858feac2013-04-14 22:39:37 -040010488 .owner = THIS_MODULE,
James Smart3ef6d242012-01-18 16:23:48 -050010489};
10490
10491static struct miscdevice lpfc_mgmt_dev = {
10492 .minor = MISC_DYNAMIC_MINOR,
10493 .name = "lpfcmgmt",
10494 .fops = &lpfc_mgmt_fop,
10495};
10496
James Smarte59058c2008-08-24 21:49:00 -040010497/**
James Smart3621a712009-04-06 18:47:14 -040010498 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -040010499 *
10500 * This routine is to be invoked when the lpfc module is loaded into the
10501 * kernel. The special kernel macro module_init() is used to indicate the
10502 * role of this routine to the kernel as lpfc module entry point.
10503 *
10504 * Return codes
10505 * 0 - successful
10506 * -ENOMEM - FC attach transport failed
10507 * all others - failed
10508 */
dea31012005-04-17 16:05:31 -050010509static int __init
10510lpfc_init(void)
10511{
10512 int error = 0;
10513
10514 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010515 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -050010516
James Smart3ef6d242012-01-18 16:23:48 -050010517 error = misc_register(&lpfc_mgmt_dev);
10518 if (error)
10519 printk(KERN_ERR "Could not register lpfcmgmt device, "
10520 "misc_register returned with status %d", error);
10521
James Smart7ee5d432007-10-27 13:37:17 -040010522 if (lpfc_enable_npiv) {
10523 lpfc_transport_functions.vport_create = lpfc_vport_create;
10524 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
10525 }
dea31012005-04-17 16:05:31 -050010526 lpfc_transport_template =
10527 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -040010528 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -050010529 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -040010530 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -040010531 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -040010532 fc_attach_transport(&lpfc_vport_transport_functions);
10533 if (lpfc_vport_transport_template == NULL) {
10534 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040010535 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -040010536 }
James Smart7ee5d432007-10-27 13:37:17 -040010537 }
dea31012005-04-17 16:05:31 -050010538 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -050010539 if (error) {
dea31012005-04-17 16:05:31 -050010540 fc_release_transport(lpfc_transport_template);
James Smartd7c255b2008-08-24 21:50:00 -040010541 if (lpfc_enable_npiv)
10542 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -050010543 }
dea31012005-04-17 16:05:31 -050010544
10545 return error;
10546}
10547
James Smarte59058c2008-08-24 21:49:00 -040010548/**
James Smart3621a712009-04-06 18:47:14 -040010549 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -040010550 *
10551 * This routine is invoked when the lpfc module is removed from the kernel.
10552 * The special kernel macro module_exit() is used to indicate the role of
10553 * this routine to the kernel as lpfc module exit point.
10554 */
dea31012005-04-17 16:05:31 -050010555static void __exit
10556lpfc_exit(void)
10557{
James Smart3ef6d242012-01-18 16:23:48 -050010558 misc_deregister(&lpfc_mgmt_dev);
dea31012005-04-17 16:05:31 -050010559 pci_unregister_driver(&lpfc_driver);
10560 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040010561 if (lpfc_enable_npiv)
10562 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -050010563 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040010564 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
10565 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050010566 (1L << _dump_buf_data_order), _dump_buf_data);
10567 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
10568 }
10569
10570 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -040010571 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
10572 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050010573 (1L << _dump_buf_dif_order), _dump_buf_dif);
10574 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
10575 }
dea31012005-04-17 16:05:31 -050010576}
10577
10578module_init(lpfc_init);
10579module_exit(lpfc_exit);
10580MODULE_LICENSE("GPL");
10581MODULE_DESCRIPTION(LPFC_MODULE_DESC);
10582MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
10583MODULE_VERSION("0:" LPFC_DRIVER_VERSION);