blob: 7bb89f49df9bc5399096fd1e3c61fd372b9bcbb8 [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 Smart256ec0d2013-04-17 20:14:58 -0400544 mod_timer(&vport->els_tmofunc,
545 jiffies + msecs_to_jiffies(1000 * timeout));
James Smart93996272008-08-24 21:50:30 -0400546 /* Set up heart beat (HB) timer */
James Smart256ec0d2013-04-17 20:14:58 -0400547 mod_timer(&phba->hb_tmofunc,
548 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -0500549 phba->hb_outstanding = 0;
550 phba->last_completion_time = jiffies;
James Smart93996272008-08-24 21:50:30 -0400551 /* Set up error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -0400552 mod_timer(&phba->eratt_poll,
553 jiffies + msecs_to_jiffies(1000 * LPFC_ERATT_POLL_INTERVAL));
dea31012005-04-17 16:05:31 -0500554
James Smarta0c87cb2009-07-19 10:01:10 -0400555 if (phba->hba_flag & LINK_DISABLED) {
556 lpfc_printf_log(phba,
557 KERN_ERR, LOG_INIT,
558 "2598 Adapter Link is disabled.\n");
559 lpfc_down_link(phba, pmb);
560 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
561 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
562 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
563 lpfc_printf_log(phba,
564 KERN_ERR, LOG_INIT,
565 "2599 Adapter failed to issue DOWN_LINK"
566 " mbox command rc 0x%x\n", rc);
567
568 mempool_free(pmb, phba->mbox_mem_pool);
569 return -EIO;
570 }
James Smarte40a02c2010-02-26 14:13:54 -0500571 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart026abb82011-12-13 13:20:45 -0500572 mempool_free(pmb, phba->mbox_mem_pool);
573 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
574 if (rc)
575 return rc;
dea31012005-04-17 16:05:31 -0500576 }
577 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400578 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500579 if (!pmb) {
580 phba->link_state = LPFC_HBA_ERROR;
581 return -ENOMEM;
582 }
583
James Smart57127f12007-10-27 13:37:05 -0400584 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
585 pmb->mbox_cmpl = lpfc_config_async_cmpl;
586 pmb->vport = phba->pport;
587 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500588
James Smart57127f12007-10-27 13:37:05 -0400589 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
590 lpfc_printf_log(phba,
591 KERN_ERR,
592 LOG_INIT,
593 "0456 Adapter failed to issue "
James Smarte4e74272009-07-19 10:01:38 -0400594 "ASYNCEVT_ENABLE mbox status x%x\n",
James Smart57127f12007-10-27 13:37:05 -0400595 rc);
596 mempool_free(pmb, phba->mbox_mem_pool);
597 }
James Smart97207482008-12-04 22:39:19 -0500598
599 /* Get Option rom version */
600 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500601 if (!pmb) {
602 phba->link_state = LPFC_HBA_ERROR;
603 return -ENOMEM;
604 }
605
James Smart97207482008-12-04 22:39:19 -0500606 lpfc_dump_wakeup_param(phba, pmb);
607 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
608 pmb->vport = phba->pport;
609 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
610
611 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
612 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
James Smarte4e74272009-07-19 10:01:38 -0400613 "to get Option ROM version status x%x\n", rc);
James Smart97207482008-12-04 22:39:19 -0500614 mempool_free(pmb, phba->mbox_mem_pool);
615 }
616
James Smartd7c255b2008-08-24 21:50:00 -0400617 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400618}
619
James Smarte59058c2008-08-24 21:49:00 -0400620/**
James Smart84d1b002010-02-12 14:42:33 -0500621 * lpfc_hba_init_link - Initialize the FC link
622 * @phba: pointer to lpfc hba data structure.
James Smart6e7288d2010-06-07 15:23:35 -0400623 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500624 *
625 * This routine will issue the INIT_LINK mailbox command call.
626 * It is available to other drivers through the lpfc_hba data
627 * structure for use as a delayed link up mechanism with the
628 * module parameter lpfc_suppress_link_up.
629 *
630 * Return code
631 * 0 - success
632 * Any other value - error
633 **/
634int
James Smart6e7288d2010-06-07 15:23:35 -0400635lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500636{
James Smart1b511972011-12-13 13:23:09 -0500637 return lpfc_hba_init_link_fc_topology(phba, phba->cfg_topology, flag);
638}
639
640/**
641 * lpfc_hba_init_link_fc_topology - Initialize FC link with desired topology
642 * @phba: pointer to lpfc hba data structure.
643 * @fc_topology: desired fc topology.
644 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
645 *
646 * This routine will issue the INIT_LINK mailbox command call.
647 * It is available to other drivers through the lpfc_hba data
648 * structure for use as a delayed link up mechanism with the
649 * module parameter lpfc_suppress_link_up.
650 *
651 * Return code
652 * 0 - success
653 * Any other value - error
654 **/
655int
656lpfc_hba_init_link_fc_topology(struct lpfc_hba *phba, uint32_t fc_topology,
657 uint32_t flag)
658{
James Smart84d1b002010-02-12 14:42:33 -0500659 struct lpfc_vport *vport = phba->pport;
660 LPFC_MBOXQ_t *pmb;
661 MAILBOX_t *mb;
662 int rc;
663
664 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
665 if (!pmb) {
666 phba->link_state = LPFC_HBA_ERROR;
667 return -ENOMEM;
668 }
669 mb = &pmb->u.mb;
670 pmb->vport = vport;
671
James Smart026abb82011-12-13 13:20:45 -0500672 if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_MAX) ||
673 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G) &&
674 !(phba->lmt & LMT_1Gb)) ||
675 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G) &&
676 !(phba->lmt & LMT_2Gb)) ||
677 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G) &&
678 !(phba->lmt & LMT_4Gb)) ||
679 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G) &&
680 !(phba->lmt & LMT_8Gb)) ||
681 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G) &&
682 !(phba->lmt & LMT_10Gb)) ||
683 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G) &&
684 !(phba->lmt & LMT_16Gb))) {
685 /* Reset link speed to auto */
686 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
687 "1302 Invalid speed for this board:%d "
688 "Reset link speed to auto.\n",
689 phba->cfg_link_speed);
690 phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
691 }
James Smart1b511972011-12-13 13:23:09 -0500692 lpfc_init_link(phba, pmb, fc_topology, phba->cfg_link_speed);
James Smart84d1b002010-02-12 14:42:33 -0500693 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart1b511972011-12-13 13:23:09 -0500694 if (phba->sli_rev < LPFC_SLI_REV4)
695 lpfc_set_loopback_flag(phba);
James Smart6e7288d2010-06-07 15:23:35 -0400696 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart76a95d72010-11-20 23:11:48 -0500697 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
James Smart84d1b002010-02-12 14:42:33 -0500698 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
699 "0498 Adapter failed to init, mbxCmd x%x "
700 "INIT_LINK, mbxStatus x%x\n",
701 mb->mbxCommand, mb->mbxStatus);
James Smart76a95d72010-11-20 23:11:48 -0500702 if (phba->sli_rev <= LPFC_SLI_REV3) {
703 /* Clear all interrupt enable conditions */
704 writel(0, phba->HCregaddr);
705 readl(phba->HCregaddr); /* flush */
706 /* Clear all pending interrupts */
707 writel(0xffffffff, phba->HAregaddr);
708 readl(phba->HAregaddr); /* flush */
709 }
James Smart84d1b002010-02-12 14:42:33 -0500710 phba->link_state = LPFC_HBA_ERROR;
James Smart6e7288d2010-06-07 15:23:35 -0400711 if (rc != MBX_BUSY || flag == MBX_POLL)
James Smart84d1b002010-02-12 14:42:33 -0500712 mempool_free(pmb, phba->mbox_mem_pool);
713 return -EIO;
714 }
James Smarte40a02c2010-02-26 14:13:54 -0500715 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
James Smart6e7288d2010-06-07 15:23:35 -0400716 if (flag == MBX_POLL)
717 mempool_free(pmb, phba->mbox_mem_pool);
James Smart84d1b002010-02-12 14:42:33 -0500718
719 return 0;
720}
721
722/**
723 * lpfc_hba_down_link - this routine downs the FC link
James Smart6e7288d2010-06-07 15:23:35 -0400724 * @phba: pointer to lpfc hba data structure.
725 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500726 *
727 * This routine will issue the DOWN_LINK mailbox command call.
728 * It is available to other drivers through the lpfc_hba data
729 * structure for use to stop the link.
730 *
731 * Return code
732 * 0 - success
733 * Any other value - error
734 **/
735int
James Smart6e7288d2010-06-07 15:23:35 -0400736lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500737{
738 LPFC_MBOXQ_t *pmb;
739 int rc;
740
741 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
742 if (!pmb) {
743 phba->link_state = LPFC_HBA_ERROR;
744 return -ENOMEM;
745 }
746
747 lpfc_printf_log(phba,
748 KERN_ERR, LOG_INIT,
749 "0491 Adapter Link is disabled.\n");
750 lpfc_down_link(phba, pmb);
751 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6e7288d2010-06-07 15:23:35 -0400752 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart84d1b002010-02-12 14:42:33 -0500753 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
754 lpfc_printf_log(phba,
755 KERN_ERR, LOG_INIT,
756 "2522 Adapter failed to issue DOWN_LINK"
757 " mbox command rc 0x%x\n", rc);
758
759 mempool_free(pmb, phba->mbox_mem_pool);
760 return -EIO;
761 }
James Smart6e7288d2010-06-07 15:23:35 -0400762 if (flag == MBX_POLL)
763 mempool_free(pmb, phba->mbox_mem_pool);
764
James Smart84d1b002010-02-12 14:42:33 -0500765 return 0;
766}
767
768/**
James Smart3621a712009-04-06 18:47:14 -0400769 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400770 * @phba: pointer to lpfc HBA data structure.
771 *
772 * This routine will do LPFC uninitialization before the HBA is reset when
773 * bringing down the SLI Layer.
774 *
775 * Return codes
776 * 0 - success.
777 * Any other value - error.
778 **/
dea31012005-04-17 16:05:31 -0500779int
James Smart2e0fef82007-06-17 19:56:36 -0500780lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500781{
James Smart1b32f6a2008-02-08 18:49:39 -0500782 struct lpfc_vport **vports;
783 int i;
James Smart3772a992009-05-22 14:50:54 -0400784
785 if (phba->sli_rev <= LPFC_SLI_REV3) {
786 /* Disable interrupts */
787 writel(0, phba->HCregaddr);
788 readl(phba->HCregaddr); /* flush */
789 }
dea31012005-04-17 16:05:31 -0500790
James Smart1b32f6a2008-02-08 18:49:39 -0500791 if (phba->pport->load_flag & FC_UNLOADING)
792 lpfc_cleanup_discovery_resources(phba->pport);
793 else {
794 vports = lpfc_create_vport_work_array(phba);
795 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400796 for (i = 0; i <= phba->max_vports &&
797 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500798 lpfc_cleanup_discovery_resources(vports[i]);
799 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500800 }
801 return 0;
dea31012005-04-17 16:05:31 -0500802}
803
James Smarte59058c2008-08-24 21:49:00 -0400804/**
James Smart3772a992009-05-22 14:50:54 -0400805 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400806 * @phba: pointer to lpfc HBA data structure.
807 *
808 * This routine will do uninitialization after the HBA is reset when bring
809 * down the SLI Layer.
810 *
811 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200812 * 0 - success.
James Smarte59058c2008-08-24 21:49:00 -0400813 * Any other value - error.
814 **/
James Smart3772a992009-05-22 14:50:54 -0400815static int
816lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -0500817{
818 struct lpfc_sli *psli = &phba->sli;
819 struct lpfc_sli_ring *pring;
820 struct lpfc_dmabuf *mp, *next_mp;
James Smart09372822008-01-11 01:52:54 -0500821 LIST_HEAD(completions);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500822 int i;
823
James Smart92d7f7b2007-06-17 19:56:38 -0500824 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
825 lpfc_sli_hbqbuf_free_all(phba);
826 else {
827 /* Cleanup preposted buffers on the ELS ring */
828 pring = &psli->ring[LPFC_ELS_RING];
829 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
830 list_del(&mp->list);
831 pring->postbufq_cnt--;
832 lpfc_mbuf_free(phba, mp->virt, mp->phys);
833 kfree(mp);
834 }
Jamie Wellnitz41415862006-02-28 19:25:27 -0500835 }
836
James Smart09372822008-01-11 01:52:54 -0500837 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500838 for (i = 0; i < psli->num_rings; i++) {
839 pring = &psli->ring[i];
James Smart09372822008-01-11 01:52:54 -0500840
841 /* At this point in time the HBA is either reset or DOA. Either
842 * way, nothing should be on txcmplq as it will NEVER complete.
843 */
844 list_splice_init(&pring->txcmplq, &completions);
James Smart09372822008-01-11 01:52:54 -0500845 spin_unlock_irq(&phba->hbalock);
846
James Smarta257bf92009-04-06 18:48:10 -0400847 /* Cancel all the IOCBs from the completions list */
848 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
849 IOERR_SLI_ABORTED);
James Smart09372822008-01-11 01:52:54 -0500850
Jamie Wellnitz41415862006-02-28 19:25:27 -0500851 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart09372822008-01-11 01:52:54 -0500852 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500853 }
James Smart09372822008-01-11 01:52:54 -0500854 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500855
856 return 0;
857}
James Smart5af5eee2010-10-22 11:06:38 -0400858
James Smartda0436e2009-05-22 14:51:39 -0400859/**
860 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
861 * @phba: pointer to lpfc HBA data structure.
862 *
863 * This routine will do uninitialization after the HBA is reset when bring
864 * down the SLI Layer.
865 *
866 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200867 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400868 * Any other value - error.
869 **/
870static int
871lpfc_hba_down_post_s4(struct lpfc_hba *phba)
872{
873 struct lpfc_scsi_buf *psb, *psb_next;
874 LIST_HEAD(aborts);
875 int ret;
876 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500877 struct lpfc_sglq *sglq_entry = NULL;
878
James Smartda0436e2009-05-22 14:51:39 -0400879 ret = lpfc_hba_down_post_s3(phba);
880 if (ret)
881 return ret;
882 /* At this point in time the HBA is either reset or DOA. Either
883 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
884 * on the lpfc_sgl_list so that it can either be freed if the
885 * driver is unloading or reposted if the driver is restarting
886 * the port.
887 */
888 spin_lock_irq(&phba->hbalock); /* required for lpfc_sgl_list and */
889 /* scsl_buf_list */
890 /* abts_sgl_list_lock required because worker thread uses this
891 * list.
892 */
893 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -0500894 list_for_each_entry(sglq_entry,
895 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
896 sglq_entry->state = SGL_FREED;
897
James Smartda0436e2009-05-22 14:51:39 -0400898 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
899 &phba->sli4_hba.lpfc_sgl_list);
900 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
901 /* abts_scsi_buf_list_lock required because worker thread uses this
902 * list.
903 */
904 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
905 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
906 &aborts);
907 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
908 spin_unlock_irq(&phba->hbalock);
909
910 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
911 psb->pCmd = NULL;
912 psb->status = IOSTAT_SUCCESS;
913 }
914 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
915 list_splice(&aborts, &phba->lpfc_scsi_buf_list);
916 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
917 return 0;
918}
919
920/**
921 * lpfc_hba_down_post - Wrapper func for hba down post routine
922 * @phba: pointer to lpfc HBA data structure.
923 *
924 * This routine wraps the actual SLI3 or SLI4 routine for performing
925 * uninitialization after the HBA is reset when bring down the SLI Layer.
926 *
927 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200928 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400929 * Any other value - error.
930 **/
931int
932lpfc_hba_down_post(struct lpfc_hba *phba)
933{
934 return (*phba->lpfc_hba_down_post)(phba);
935}
Jamie Wellnitz41415862006-02-28 19:25:27 -0500936
James Smarte59058c2008-08-24 21:49:00 -0400937/**
James Smart3621a712009-04-06 18:47:14 -0400938 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -0400939 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
940 *
941 * This is the HBA-timer timeout handler registered to the lpfc driver. When
942 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
943 * work-port-events bitmap and the worker thread is notified. This timeout
944 * event will be used by the worker thread to invoke the actual timeout
945 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
946 * be performed in the timeout handler and the HBA timeout event bit shall
947 * be cleared by the worker thread after it has taken the event bitmap out.
948 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100949static void
James Smart858c9f62007-06-17 19:56:39 -0500950lpfc_hb_timeout(unsigned long ptr)
951{
952 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400953 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -0500954 unsigned long iflag;
955
956 phba = (struct lpfc_hba *)ptr;
James Smart93996272008-08-24 21:50:30 -0400957
958 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -0500959 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -0400960 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
961 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -0500962 phba->pport->work_port_events |= WORKER_HB_TMO;
963 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
964
James Smart93996272008-08-24 21:50:30 -0400965 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -0400966 if (!tmo_posted)
967 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -0500968 return;
969}
970
James Smarte59058c2008-08-24 21:49:00 -0400971/**
James Smart19ca7602010-11-20 23:11:55 -0500972 * lpfc_rrq_timeout - The RRQ-timer timeout handler
973 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
974 *
975 * This is the RRQ-timer timeout handler registered to the lpfc driver. When
976 * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
977 * work-port-events bitmap and the worker thread is notified. This timeout
978 * event will be used by the worker thread to invoke the actual timeout
979 * handler routine, lpfc_rrq_handler. Any periodical operations will
980 * be performed in the timeout handler and the RRQ timeout event bit shall
981 * be cleared by the worker thread after it has taken the event bitmap out.
982 **/
983static void
984lpfc_rrq_timeout(unsigned long ptr)
985{
986 struct lpfc_hba *phba;
James Smart19ca7602010-11-20 23:11:55 -0500987 unsigned long iflag;
988
989 phba = (struct lpfc_hba *)ptr;
990 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -0500991 phba->hba_flag |= HBA_RRQ_ACTIVE;
James Smart19ca7602010-11-20 23:11:55 -0500992 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -0500993 lpfc_worker_wake_up(phba);
James Smart19ca7602010-11-20 23:11:55 -0500994}
995
996/**
James Smart3621a712009-04-06 18:47:14 -0400997 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -0400998 * @phba: pointer to lpfc hba data structure.
999 * @pmboxq: pointer to the driver internal queue element for mailbox command.
1000 *
1001 * This is the callback function to the lpfc heart-beat mailbox command.
1002 * If configured, the lpfc driver issues the heart-beat mailbox command to
1003 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
1004 * heart-beat mailbox command is issued, the driver shall set up heart-beat
1005 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
1006 * heart-beat outstanding state. Once the mailbox command comes back and
1007 * no error conditions detected, the heart-beat mailbox command timer is
1008 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1009 * state is cleared for the next heart-beat. If the timer expired with the
1010 * heart-beat outstanding state set, the driver will put the HBA offline.
1011 **/
James Smart858c9f62007-06-17 19:56:39 -05001012static void
1013lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1014{
1015 unsigned long drvr_flag;
1016
1017 spin_lock_irqsave(&phba->hbalock, drvr_flag);
1018 phba->hb_outstanding = 0;
1019 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1020
James Smart93996272008-08-24 21:50:30 -04001021 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -05001022 mempool_free(pmboxq, phba->mbox_mem_pool);
1023 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1024 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -04001025 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -05001026 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001027 jiffies +
1028 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001029 return;
1030}
1031
James Smarte59058c2008-08-24 21:49:00 -04001032/**
James Smart3621a712009-04-06 18:47:14 -04001033 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001034 * @phba: pointer to lpfc hba data structure.
1035 *
1036 * This is the actual HBA-timer timeout handler to be invoked by the worker
1037 * thread whenever the HBA timer fired and HBA-timeout event posted. This
1038 * handler performs any periodic operations needed for the device. If such
1039 * periodic event has already been attended to either in the interrupt handler
1040 * or by processing slow-ring or fast-ring events within the HBA-timer
1041 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1042 * the timer for the next timeout period. If lpfc heart-beat mailbox command
1043 * is configured and there is no heart-beat mailbox command outstanding, a
1044 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1045 * has been a heart-beat mailbox command outstanding, the HBA shall be put
1046 * to offline.
1047 **/
James Smart858c9f62007-06-17 19:56:39 -05001048void
1049lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1050{
James Smart45ed1192009-10-02 15:17:02 -04001051 struct lpfc_vport **vports;
James Smart858c9f62007-06-17 19:56:39 -05001052 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -05001053 struct lpfc_dmabuf *buf_ptr;
James Smart45ed1192009-10-02 15:17:02 -04001054 int retval, i;
James Smart858c9f62007-06-17 19:56:39 -05001055 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -05001056 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -05001057
James Smart45ed1192009-10-02 15:17:02 -04001058 vports = lpfc_create_vport_work_array(phba);
1059 if (vports != NULL)
1060 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
1061 lpfc_rcv_seq_check_edtov(vports[i]);
1062 lpfc_destroy_vport_work_array(phba, vports);
1063
James Smart858c9f62007-06-17 19:56:39 -05001064 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -04001065 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -05001066 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1067 return;
1068
1069 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -05001070
James Smart256ec0d2013-04-17 20:14:58 -04001071 if (time_after(phba->last_completion_time +
1072 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL),
1073 jiffies)) {
James Smart858c9f62007-06-17 19:56:39 -05001074 spin_unlock_irq(&phba->pport->work_port_lock);
1075 if (!phba->hb_outstanding)
1076 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001077 jiffies +
1078 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001079 else
1080 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001081 jiffies +
1082 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001083 return;
1084 }
1085 spin_unlock_irq(&phba->pport->work_port_lock);
1086
James Smart0ff10d42008-01-11 01:52:36 -05001087 if (phba->elsbuf_cnt &&
1088 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1089 spin_lock_irq(&phba->hbalock);
1090 list_splice_init(&phba->elsbuf, &completions);
1091 phba->elsbuf_cnt = 0;
1092 phba->elsbuf_prev_cnt = 0;
1093 spin_unlock_irq(&phba->hbalock);
1094
1095 while (!list_empty(&completions)) {
1096 list_remove_head(&completions, buf_ptr,
1097 struct lpfc_dmabuf, list);
1098 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1099 kfree(buf_ptr);
1100 }
1101 }
1102 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1103
James Smart858c9f62007-06-17 19:56:39 -05001104 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -05001105 if (phba->cfg_enable_hba_heartbeat) {
1106 if (!phba->hb_outstanding) {
James Smartbc739052010-08-04 16:11:18 -04001107 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1108 (list_empty(&psli->mboxq))) {
1109 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1110 GFP_KERNEL);
1111 if (!pmboxq) {
1112 mod_timer(&phba->hb_tmofunc,
1113 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001114 msecs_to_jiffies(1000 *
1115 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001116 return;
1117 }
James Smart13815c82008-01-11 01:52:48 -05001118
James Smartbc739052010-08-04 16:11:18 -04001119 lpfc_heart_beat(phba, pmboxq);
1120 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1121 pmboxq->vport = phba->pport;
1122 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1123 MBX_NOWAIT);
James Smart13815c82008-01-11 01:52:48 -05001124
James Smartbc739052010-08-04 16:11:18 -04001125 if (retval != MBX_BUSY &&
1126 retval != MBX_SUCCESS) {
1127 mempool_free(pmboxq,
1128 phba->mbox_mem_pool);
1129 mod_timer(&phba->hb_tmofunc,
1130 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001131 msecs_to_jiffies(1000 *
1132 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001133 return;
1134 }
1135 phba->skipped_hb = 0;
1136 phba->hb_outstanding = 1;
1137 } else if (time_before_eq(phba->last_completion_time,
1138 phba->skipped_hb)) {
1139 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1140 "2857 Last completion time not "
1141 " updated in %d ms\n",
1142 jiffies_to_msecs(jiffies
1143 - phba->last_completion_time));
1144 } else
1145 phba->skipped_hb = jiffies;
1146
James Smart858c9f62007-06-17 19:56:39 -05001147 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001148 jiffies +
1149 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001150 return;
James Smart13815c82008-01-11 01:52:48 -05001151 } else {
1152 /*
1153 * If heart beat timeout called with hb_outstanding set
James Smartdcf2a4e2010-09-29 11:18:53 -04001154 * we need to give the hb mailbox cmd a chance to
1155 * complete or TMO.
James Smart13815c82008-01-11 01:52:48 -05001156 */
James Smartdcf2a4e2010-09-29 11:18:53 -04001157 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1158 "0459 Adapter heartbeat still out"
1159 "standing:last compl time was %d ms.\n",
1160 jiffies_to_msecs(jiffies
1161 - phba->last_completion_time));
1162 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001163 jiffies +
1164 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001165 }
James Smart858c9f62007-06-17 19:56:39 -05001166 }
1167}
1168
James Smarte59058c2008-08-24 21:49:00 -04001169/**
James Smart3621a712009-04-06 18:47:14 -04001170 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -04001171 * @phba: pointer to lpfc hba data structure.
1172 *
1173 * This routine is called to bring the HBA offline when HBA hardware error
1174 * other than Port Error 6 has been detected.
1175 **/
James Smart09372822008-01-11 01:52:54 -05001176static void
1177lpfc_offline_eratt(struct lpfc_hba *phba)
1178{
1179 struct lpfc_sli *psli = &phba->sli;
1180
1181 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001182 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -05001183 spin_unlock_irq(&phba->hbalock);
James Smart618a5232012-06-12 13:54:36 -04001184 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart09372822008-01-11 01:52:54 -05001185
1186 lpfc_offline(phba);
1187 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001188 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001189 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001190 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001191 lpfc_hba_down_post(phba);
1192 lpfc_sli_brdready(phba, HS_MBRDY);
1193 lpfc_unblock_mgmt_io(phba);
1194 phba->link_state = LPFC_HBA_ERROR;
1195 return;
1196}
1197
James Smarte59058c2008-08-24 21:49:00 -04001198/**
James Smartda0436e2009-05-22 14:51:39 -04001199 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1200 * @phba: pointer to lpfc hba data structure.
1201 *
1202 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1203 * other than Port Error 6 has been detected.
1204 **/
1205static void
1206lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1207{
James Smart618a5232012-06-12 13:54:36 -04001208 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smartda0436e2009-05-22 14:51:39 -04001209 lpfc_offline(phba);
1210 lpfc_sli4_brdreset(phba);
1211 lpfc_hba_down_post(phba);
1212 lpfc_sli4_post_status_check(phba);
1213 lpfc_unblock_mgmt_io(phba);
1214 phba->link_state = LPFC_HBA_ERROR;
1215}
1216
1217/**
James Smarta257bf92009-04-06 18:48:10 -04001218 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1219 * @phba: pointer to lpfc hba data structure.
1220 *
1221 * This routine is invoked to handle the deferred HBA hardware error
1222 * conditions. This type of error is indicated by HBA by setting ER1
1223 * and another ER bit in the host status register. The driver will
1224 * wait until the ER1 bit clears before handling the error condition.
1225 **/
1226static void
1227lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1228{
1229 uint32_t old_host_status = phba->work_hs;
1230 struct lpfc_sli_ring *pring;
1231 struct lpfc_sli *psli = &phba->sli;
1232
James Smartf4b4c682009-05-22 14:53:12 -04001233 /* If the pci channel is offline, ignore possible errors,
1234 * since we cannot communicate with the pci card anyway.
1235 */
1236 if (pci_channel_offline(phba->pcidev)) {
1237 spin_lock_irq(&phba->hbalock);
1238 phba->hba_flag &= ~DEFER_ERATT;
1239 spin_unlock_irq(&phba->hbalock);
1240 return;
1241 }
1242
James Smarta257bf92009-04-06 18:48:10 -04001243 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1244 "0479 Deferred Adapter Hardware Error "
1245 "Data: x%x x%x x%x\n",
1246 phba->work_hs,
1247 phba->work_status[0], phba->work_status[1]);
1248
1249 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001250 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001251 spin_unlock_irq(&phba->hbalock);
1252
1253
1254 /*
1255 * Firmware stops when it triggred erratt. That could cause the I/Os
1256 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1257 * SCSI layer retry it after re-establishing link.
1258 */
1259 pring = &psli->ring[psli->fcp_ring];
1260 lpfc_sli_abort_iocb_ring(phba, pring);
1261
1262 /*
1263 * There was a firmware error. Take the hba offline and then
1264 * attempt to restart it.
1265 */
James Smart618a5232012-06-12 13:54:36 -04001266 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smarta257bf92009-04-06 18:48:10 -04001267 lpfc_offline(phba);
1268
1269 /* Wait for the ER1 bit to clear.*/
1270 while (phba->work_hs & HS_FFER1) {
1271 msleep(100);
James Smart9940b972011-03-11 16:06:12 -05001272 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1273 phba->work_hs = UNPLUG_ERR ;
1274 break;
1275 }
James Smarta257bf92009-04-06 18:48:10 -04001276 /* If driver is unloading let the worker thread continue */
1277 if (phba->pport->load_flag & FC_UNLOADING) {
1278 phba->work_hs = 0;
1279 break;
1280 }
1281 }
1282
1283 /*
1284 * This is to ptrotect against a race condition in which
1285 * first write to the host attention register clear the
1286 * host status register.
1287 */
1288 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1289 phba->work_hs = old_host_status & ~HS_FFER1;
1290
James Smart3772a992009-05-22 14:50:54 -04001291 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001292 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001293 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001294 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1295 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1296}
1297
James Smart3772a992009-05-22 14:50:54 -04001298static void
1299lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1300{
1301 struct lpfc_board_event_header board_event;
1302 struct Scsi_Host *shost;
1303
1304 board_event.event_type = FC_REG_BOARD_EVENT;
1305 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1306 shost = lpfc_shost_from_vport(phba->pport);
1307 fc_host_post_vendor_event(shost, fc_get_event_number(),
1308 sizeof(board_event),
1309 (char *) &board_event,
1310 LPFC_NL_VENDOR_ID);
1311}
1312
James Smarta257bf92009-04-06 18:48:10 -04001313/**
James Smart3772a992009-05-22 14:50:54 -04001314 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001315 * @phba: pointer to lpfc hba data structure.
1316 *
1317 * This routine is invoked to handle the following HBA hardware error
1318 * conditions:
1319 * 1 - HBA error attention interrupt
1320 * 2 - DMA ring index out of range
1321 * 3 - Mailbox command came back as unknown
1322 **/
James Smart3772a992009-05-22 14:50:54 -04001323static void
1324lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001325{
James Smart2e0fef82007-06-17 19:56:36 -05001326 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001327 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001328 struct lpfc_sli_ring *pring;
James Smartd2873e42006-08-18 17:46:43 -04001329 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001330 unsigned long temperature;
1331 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001332 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001333
Linas Vepstas8d63f372007-02-14 14:28:36 -06001334 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001335 * since we cannot communicate with the pci card anyway.
1336 */
1337 if (pci_channel_offline(phba->pcidev)) {
1338 spin_lock_irq(&phba->hbalock);
1339 phba->hba_flag &= ~DEFER_ERATT;
1340 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001341 return;
James Smart3772a992009-05-22 14:50:54 -04001342 }
1343
James Smart13815c82008-01-11 01:52:48 -05001344 /* If resets are disabled then leave the HBA alone and return */
1345 if (!phba->cfg_enable_hba_reset)
1346 return;
dea31012005-04-17 16:05:31 -05001347
James Smartea2151b2008-09-07 11:52:10 -04001348 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001349 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001350
James Smarta257bf92009-04-06 18:48:10 -04001351 if (phba->hba_flag & DEFER_ERATT)
1352 lpfc_handle_deferred_eratt(phba);
1353
James Smartdcf2a4e2010-09-29 11:18:53 -04001354 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1355 if (phba->work_hs & HS_FFER6)
1356 /* Re-establishing Link */
1357 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1358 "1301 Re-establishing Link "
1359 "Data: x%x x%x x%x\n",
1360 phba->work_hs, phba->work_status[0],
1361 phba->work_status[1]);
1362 if (phba->work_hs & HS_FFER8)
1363 /* Device Zeroization */
1364 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1365 "2861 Host Authentication device "
1366 "zeroization Data:x%x x%x x%x\n",
1367 phba->work_hs, phba->work_status[0],
1368 phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001369
James Smart92d7f7b2007-06-17 19:56:38 -05001370 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001371 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001372 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001373
1374 /*
1375 * Firmware stops when it triggled erratt with HS_FFER6.
1376 * That could cause the I/Os dropped by the firmware.
1377 * Error iocb (I/O) on txcmplq and let the SCSI layer
1378 * retry it after re-establishing link.
1379 */
1380 pring = &psli->ring[psli->fcp_ring];
1381 lpfc_sli_abort_iocb_ring(phba, pring);
1382
dea31012005-04-17 16:05:31 -05001383 /*
1384 * There was a firmware error. Take the hba offline and then
1385 * attempt to restart it.
1386 */
James Smart618a5232012-06-12 13:54:36 -04001387 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
dea31012005-04-17 16:05:31 -05001388 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001389 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001390 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001391 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001392 return;
1393 }
James Smart46fa3112007-04-25 09:51:45 -04001394 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001395 } else if (phba->work_hs & HS_CRIT_TEMP) {
1396 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1397 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1398 temp_event_data.event_code = LPFC_CRIT_TEMP;
1399 temp_event_data.data = (uint32_t)temperature;
1400
1401 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001402 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001403 "(%ld), taking this port offline "
1404 "Data: x%x x%x x%x\n",
1405 temperature, phba->work_hs,
1406 phba->work_status[0], phba->work_status[1]);
1407
1408 shost = lpfc_shost_from_vport(phba->pport);
1409 fc_host_post_vendor_event(shost, fc_get_event_number(),
1410 sizeof(temp_event_data),
1411 (char *) &temp_event_data,
1412 SCSI_NL_VID_TYPE_PCI
1413 | PCI_VENDOR_ID_EMULEX);
1414
James Smart7af67052007-10-27 13:38:11 -04001415 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001416 phba->over_temp_state = HBA_OVER_TEMP;
1417 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001418 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001419
dea31012005-04-17 16:05:31 -05001420 } else {
1421 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001422 * failure is a value other than FFER6. Do not call the offline
1423 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001424 */
1425 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001426 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001427 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001428 phba->work_hs,
dea31012005-04-17 16:05:31 -05001429 phba->work_status[0], phba->work_status[1]);
1430
James Smartd2873e42006-08-18 17:46:43 -04001431 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001432 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001433 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001434 sizeof(event_data), (char *) &event_data,
1435 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1436
James Smart09372822008-01-11 01:52:54 -05001437 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001438 }
James Smart93996272008-08-24 21:50:30 -04001439 return;
dea31012005-04-17 16:05:31 -05001440}
1441
James Smarte59058c2008-08-24 21:49:00 -04001442/**
James Smart618a5232012-06-12 13:54:36 -04001443 * lpfc_sli4_port_sta_fn_reset - The SLI4 function reset due to port status reg
1444 * @phba: pointer to lpfc hba data structure.
1445 * @mbx_action: flag for mailbox shutdown action.
1446 *
1447 * This routine is invoked to perform an SLI4 port PCI function reset in
1448 * response to port status register polling attention. It waits for port
1449 * status register (ERR, RDY, RN) bits before proceeding with function reset.
1450 * During this process, interrupt vectors are freed and later requested
1451 * for handling possible port resource change.
1452 **/
1453static int
1454lpfc_sli4_port_sta_fn_reset(struct lpfc_hba *phba, int mbx_action)
1455{
1456 int rc;
1457 uint32_t intr_mode;
1458
1459 /*
1460 * On error status condition, driver need to wait for port
1461 * ready before performing reset.
1462 */
1463 rc = lpfc_sli4_pdev_status_reg_wait(phba);
1464 if (!rc) {
1465 /* need reset: attempt for port recovery */
1466 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1467 "2887 Reset Needed: Attempting Port "
1468 "Recovery...\n");
1469 lpfc_offline_prep(phba, mbx_action);
1470 lpfc_offline(phba);
1471 /* release interrupt for possible resource change */
1472 lpfc_sli4_disable_intr(phba);
1473 lpfc_sli_brdrestart(phba);
1474 /* request and enable interrupt */
1475 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
1476 if (intr_mode == LPFC_INTR_ERROR) {
1477 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1478 "3175 Failed to enable interrupt\n");
1479 return -EIO;
1480 } else {
1481 phba->intr_mode = intr_mode;
1482 }
1483 rc = lpfc_online(phba);
1484 if (rc == 0)
1485 lpfc_unblock_mgmt_io(phba);
1486 }
1487 return rc;
1488}
1489
1490/**
James Smartda0436e2009-05-22 14:51:39 -04001491 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1492 * @phba: pointer to lpfc hba data structure.
1493 *
1494 * This routine is invoked to handle the SLI4 HBA hardware error attention
1495 * conditions.
1496 **/
1497static void
1498lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1499{
1500 struct lpfc_vport *vport = phba->pport;
1501 uint32_t event_data;
1502 struct Scsi_Host *shost;
James Smart2fcee4b2010-12-15 17:57:46 -05001503 uint32_t if_type;
James Smart2e90f4b2011-12-13 13:22:37 -05001504 struct lpfc_register portstat_reg = {0};
1505 uint32_t reg_err1, reg_err2;
1506 uint32_t uerrlo_reg, uemasklo_reg;
1507 uint32_t pci_rd_rc1, pci_rd_rc2;
James Smart73d91e52011-10-10 21:32:10 -04001508 int rc;
James Smartda0436e2009-05-22 14:51:39 -04001509
1510 /* If the pci channel is offline, ignore possible errors, since
1511 * we cannot communicate with the pci card anyway.
1512 */
1513 if (pci_channel_offline(phba->pcidev))
1514 return;
1515 /* If resets are disabled then leave the HBA alone and return */
1516 if (!phba->cfg_enable_hba_reset)
1517 return;
1518
James Smart2fcee4b2010-12-15 17:57:46 -05001519 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1520 switch (if_type) {
1521 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart2e90f4b2011-12-13 13:22:37 -05001522 pci_rd_rc1 = lpfc_readl(
1523 phba->sli4_hba.u.if_type0.UERRLOregaddr,
1524 &uerrlo_reg);
1525 pci_rd_rc2 = lpfc_readl(
1526 phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
1527 &uemasklo_reg);
1528 /* consider PCI bus read error as pci_channel_offline */
1529 if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
1530 return;
James Smart2fcee4b2010-12-15 17:57:46 -05001531 lpfc_sli4_offline_eratt(phba);
1532 break;
1533 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart2e90f4b2011-12-13 13:22:37 -05001534 pci_rd_rc1 = lpfc_readl(
1535 phba->sli4_hba.u.if_type2.STATUSregaddr,
1536 &portstat_reg.word0);
1537 /* consider PCI bus read error as pci_channel_offline */
James Smart6b5151f2012-01-18 16:24:06 -05001538 if (pci_rd_rc1 == -EIO) {
1539 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1540 "3151 PCI bus read access failure: x%x\n",
1541 readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
James Smart2e90f4b2011-12-13 13:22:37 -05001542 return;
James Smart6b5151f2012-01-18 16:24:06 -05001543 }
James Smart2e90f4b2011-12-13 13:22:37 -05001544 reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
1545 reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
James Smart2fcee4b2010-12-15 17:57:46 -05001546 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
1547 /* TODO: Register for Overtemp async events. */
1548 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1549 "2889 Port Overtemperature event, "
James Smart026abb82011-12-13 13:20:45 -05001550 "taking port offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001551 spin_lock_irq(&phba->hbalock);
1552 phba->over_temp_state = HBA_OVER_TEMP;
1553 spin_unlock_irq(&phba->hbalock);
1554 lpfc_sli4_offline_eratt(phba);
James Smart2e90f4b2011-12-13 13:22:37 -05001555 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001556 }
James Smart2e90f4b2011-12-13 13:22:37 -05001557 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1558 reg_err2 == SLIPORT_ERR2_REG_FW_RESTART)
1559 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1560 "3143 Port Down: Firmware Restarted\n");
1561 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1562 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1563 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1564 "3144 Port Down: Debug Dump\n");
1565 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1566 reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
1567 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1568 "3145 Port Down: Provisioning\n");
James Smart618a5232012-06-12 13:54:36 -04001569
1570 /* Check port status register for function reset */
1571 rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT);
1572 if (rc == 0) {
1573 /* don't report event on forced debug dump */
1574 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1575 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1576 return;
1577 else
1578 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001579 }
James Smart618a5232012-06-12 13:54:36 -04001580 /* fall through for not able to recover */
James Smart6b5151f2012-01-18 16:24:06 -05001581 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1582 "3152 Unrecoverable error, bring the port "
1583 "offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001584 lpfc_sli4_offline_eratt(phba);
1585 break;
1586 case LPFC_SLI_INTF_IF_TYPE_1:
1587 default:
1588 break;
1589 }
James Smart2e90f4b2011-12-13 13:22:37 -05001590 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1591 "3123 Report dump event to upper layer\n");
1592 /* Send an internal error event to mgmt application */
1593 lpfc_board_errevt_to_mgmt(phba);
1594
1595 event_data = FC_REG_DUMP_EVENT;
1596 shost = lpfc_shost_from_vport(vport);
1597 fc_host_post_vendor_event(shost, fc_get_event_number(),
1598 sizeof(event_data), (char *) &event_data,
1599 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
James Smartda0436e2009-05-22 14:51:39 -04001600}
1601
1602/**
1603 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1604 * @phba: pointer to lpfc HBA data structure.
1605 *
1606 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1607 * routine from the API jump table function pointer from the lpfc_hba struct.
1608 *
1609 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001610 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001611 * Any other value - error.
1612 **/
1613void
1614lpfc_handle_eratt(struct lpfc_hba *phba)
1615{
1616 (*phba->lpfc_handle_eratt)(phba);
1617}
1618
1619/**
James Smart3621a712009-04-06 18:47:14 -04001620 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04001621 * @phba: pointer to lpfc hba data structure.
1622 *
1623 * This routine is invoked from the worker thread to handle a HBA host
1624 * attention link event.
1625 **/
dea31012005-04-17 16:05:31 -05001626void
James Smart2e0fef82007-06-17 19:56:36 -05001627lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001628{
James Smart2e0fef82007-06-17 19:56:36 -05001629 struct lpfc_vport *vport = phba->pport;
1630 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001631 LPFC_MBOXQ_t *pmb;
1632 volatile uint32_t control;
1633 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05001634 int rc = 0;
dea31012005-04-17 16:05:31 -05001635
1636 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001637 if (!pmb) {
1638 rc = 1;
dea31012005-04-17 16:05:31 -05001639 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05001640 }
dea31012005-04-17 16:05:31 -05001641
1642 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001643 if (!mp) {
1644 rc = 2;
dea31012005-04-17 16:05:31 -05001645 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05001646 }
dea31012005-04-17 16:05:31 -05001647
1648 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05001649 if (!mp->virt) {
1650 rc = 3;
dea31012005-04-17 16:05:31 -05001651 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05001652 }
dea31012005-04-17 16:05:31 -05001653
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05001654 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04001655 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05001656
1657 psli->slistat.link_event++;
James Smart76a95d72010-11-20 23:11:48 -05001658 lpfc_read_topology(phba, pmb, mp);
1659 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smart2e0fef82007-06-17 19:56:36 -05001660 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04001661 /* Block ELS IOCBs until we have processed this mbox command */
1662 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04001663 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05001664 if (rc == MBX_NOT_FINISHED) {
1665 rc = 4;
James Smart14691152006-12-02 13:34:28 -05001666 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05001667 }
dea31012005-04-17 16:05:31 -05001668
1669 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05001670 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001671 writel(HA_LATT, phba->HAregaddr);
1672 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001673 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001674
1675 return;
1676
James Smart14691152006-12-02 13:34:28 -05001677lpfc_handle_latt_free_mbuf:
James Smart0d2b6b82008-06-14 22:52:47 -04001678 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05001679 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001680lpfc_handle_latt_free_mp:
1681 kfree(mp);
1682lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04001683 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001684lpfc_handle_latt_err_exit:
1685 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001686 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001687 psli->sli_flag |= LPFC_PROCESS_LA;
1688 control = readl(phba->HCregaddr);
1689 control |= HC_LAINT_ENA;
1690 writel(control, phba->HCregaddr);
1691 readl(phba->HCregaddr); /* flush */
1692
1693 /* Clear Link Attention in HA REG */
1694 writel(HA_LATT, phba->HAregaddr);
1695 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001696 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001697 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001698 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001699
James Smart09372822008-01-11 01:52:54 -05001700 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1701 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05001702
1703 return;
1704}
1705
James Smarte59058c2008-08-24 21:49:00 -04001706/**
James Smart3621a712009-04-06 18:47:14 -04001707 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04001708 * @phba: pointer to lpfc hba data structure.
1709 * @vpd: pointer to the vital product data.
1710 * @len: length of the vital product data in bytes.
1711 *
1712 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1713 * an array of characters. In this routine, the ModelName, ProgramType, and
1714 * ModelDesc, etc. fields of the phba data structure will be populated.
1715 *
1716 * Return codes
1717 * 0 - pointer to the VPD passed in is NULL
1718 * 1 - success
1719 **/
James Smart3772a992009-05-22 14:50:54 -04001720int
James Smart2e0fef82007-06-17 19:56:36 -05001721lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05001722{
1723 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05001724 int Length;
dea31012005-04-17 16:05:31 -05001725 int i, j;
1726 int finished = 0;
1727 int index = 0;
1728
1729 if (!vpd)
1730 return 0;
1731
1732 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05001733 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001734 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05001735 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1736 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001737 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05001738 switch (vpd[index]) {
1739 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001740 case 0x91:
dea31012005-04-17 16:05:31 -05001741 index += 1;
1742 lenlo = vpd[index];
1743 index += 1;
1744 lenhi = vpd[index];
1745 index += 1;
1746 i = ((((unsigned short)lenhi) << 8) + lenlo);
1747 index += i;
1748 break;
1749 case 0x90:
1750 index += 1;
1751 lenlo = vpd[index];
1752 index += 1;
1753 lenhi = vpd[index];
1754 index += 1;
1755 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001756 if (Length > len - index)
1757 Length = len - index;
dea31012005-04-17 16:05:31 -05001758 while (Length > 0) {
1759 /* Look for Serial Number */
1760 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1761 index += 2;
1762 i = vpd[index];
1763 index += 1;
1764 j = 0;
1765 Length -= (3+i);
1766 while(i--) {
1767 phba->SerialNumber[j++] = vpd[index++];
1768 if (j == 31)
1769 break;
1770 }
1771 phba->SerialNumber[j] = 0;
1772 continue;
1773 }
1774 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1775 phba->vpd_flag |= VPD_MODEL_DESC;
1776 index += 2;
1777 i = vpd[index];
1778 index += 1;
1779 j = 0;
1780 Length -= (3+i);
1781 while(i--) {
1782 phba->ModelDesc[j++] = vpd[index++];
1783 if (j == 255)
1784 break;
1785 }
1786 phba->ModelDesc[j] = 0;
1787 continue;
1788 }
1789 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1790 phba->vpd_flag |= VPD_MODEL_NAME;
1791 index += 2;
1792 i = vpd[index];
1793 index += 1;
1794 j = 0;
1795 Length -= (3+i);
1796 while(i--) {
1797 phba->ModelName[j++] = vpd[index++];
1798 if (j == 79)
1799 break;
1800 }
1801 phba->ModelName[j] = 0;
1802 continue;
1803 }
1804 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1805 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1806 index += 2;
1807 i = vpd[index];
1808 index += 1;
1809 j = 0;
1810 Length -= (3+i);
1811 while(i--) {
1812 phba->ProgramType[j++] = vpd[index++];
1813 if (j == 255)
1814 break;
1815 }
1816 phba->ProgramType[j] = 0;
1817 continue;
1818 }
1819 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1820 phba->vpd_flag |= VPD_PORT;
1821 index += 2;
1822 i = vpd[index];
1823 index += 1;
1824 j = 0;
1825 Length -= (3+i);
1826 while(i--) {
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_GET)) {
1830 j++;
1831 index++;
1832 } else
1833 phba->Port[j++] = vpd[index++];
1834 if (j == 19)
1835 break;
dea31012005-04-17 16:05:31 -05001836 }
James Smartcd1c8302011-10-10 21:33:25 -04001837 if ((phba->sli_rev != LPFC_SLI_REV4) ||
1838 (phba->sli4_hba.pport_name_sta ==
1839 LPFC_SLI4_PPNAME_NON))
1840 phba->Port[j] = 0;
dea31012005-04-17 16:05:31 -05001841 continue;
1842 }
1843 else {
1844 index += 2;
1845 i = vpd[index];
1846 index += 1;
1847 index += i;
1848 Length -= (3 + i);
1849 }
1850 }
1851 finished = 0;
1852 break;
1853 case 0x78:
1854 finished = 1;
1855 break;
1856 default:
1857 index ++;
1858 break;
1859 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001860 }
dea31012005-04-17 16:05:31 -05001861
1862 return(1);
1863}
1864
James Smarte59058c2008-08-24 21:49:00 -04001865/**
James Smart3621a712009-04-06 18:47:14 -04001866 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04001867 * @phba: pointer to lpfc hba data structure.
1868 * @mdp: pointer to the data structure to hold the derived model name.
1869 * @descp: pointer to the data structure to hold the derived description.
1870 *
1871 * This routine retrieves HBA's description based on its registered PCI device
1872 * ID. The @descp passed into this function points to an array of 256 chars. It
1873 * shall be returned with the model name, maximum speed, and the host bus type.
1874 * The @mdp passed into this function points to an array of 80 chars. When the
1875 * function returns, the @mdp will be filled with the model name.
1876 **/
dea31012005-04-17 16:05:31 -05001877static void
James Smart2e0fef82007-06-17 19:56:36 -05001878lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05001879{
1880 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05001881 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001882 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04001883 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04001884 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001885 struct {
James Smarta747c9c2009-11-18 15:41:10 -05001886 char *name;
1887 char *bus;
1888 char *function;
1889 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001890
1891 if (mdp && mdp[0] != '\0'
1892 && descp && descp[0] != '\0')
1893 return;
1894
James Smartc0c11512011-05-24 11:41:34 -04001895 if (phba->lmt & LMT_16Gb)
1896 max_speed = 16;
1897 else if (phba->lmt & LMT_10Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001898 max_speed = 10;
1899 else if (phba->lmt & LMT_8Gb)
1900 max_speed = 8;
1901 else if (phba->lmt & LMT_4Gb)
1902 max_speed = 4;
1903 else if (phba->lmt & LMT_2Gb)
1904 max_speed = 2;
James Smart4169d862012-09-29 11:32:09 -04001905 else if (phba->lmt & LMT_1Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001906 max_speed = 1;
James Smart4169d862012-09-29 11:32:09 -04001907 else
1908 max_speed = 0;
dea31012005-04-17 16:05:31 -05001909
1910 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05001911
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001912 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001913 case PCI_DEVICE_ID_FIREFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001914 m = (typeof(m)){"LP6000", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001915 break;
dea31012005-04-17 16:05:31 -05001916 case PCI_DEVICE_ID_SUPERFLY:
1917 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smarta747c9c2009-11-18 15:41:10 -05001918 m = (typeof(m)){"LP7000", "PCI",
1919 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001920 else
James Smarta747c9c2009-11-18 15:41:10 -05001921 m = (typeof(m)){"LP7000E", "PCI",
1922 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001923 break;
1924 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001925 m = (typeof(m)){"LP8000", "PCI",
1926 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001927 break;
1928 case PCI_DEVICE_ID_CENTAUR:
1929 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smarta747c9c2009-11-18 15:41:10 -05001930 m = (typeof(m)){"LP9002", "PCI",
1931 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001932 else
James Smarta747c9c2009-11-18 15:41:10 -05001933 m = (typeof(m)){"LP9000", "PCI",
1934 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001935 break;
1936 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001937 m = (typeof(m)){"LP952", "PCI",
1938 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001939 break;
1940 case PCI_DEVICE_ID_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05001941 m = (typeof(m)){"LP9802", "PCI-X",
1942 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001943 break;
1944 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05001945 m = (typeof(m)){"LP10000", "PCI-X",
1946 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001947 break;
1948 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05001949 m = (typeof(m)){"LPX1000", "PCI-X",
1950 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001951 break;
1952 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001953 m = (typeof(m)){"LP982", "PCI-X",
1954 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001955 break;
1956 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001957 m = (typeof(m)){"LP1050", "PCI-X",
1958 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001959 break;
1960 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05001961 m = (typeof(m)){"LP11000", "PCI-X2",
1962 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001963 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001964 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001965 m = (typeof(m)){"LP11000-SP", "PCI-X2",
1966 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001967 break;
1968 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001969 m = (typeof(m)){"LP11002-SP", "PCI-X2",
1970 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001971 break;
1972 case PCI_DEVICE_ID_NEPTUNE:
James Smarta747c9c2009-11-18 15:41:10 -05001973 m = (typeof(m)){"LPe1000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001974 break;
1975 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001976 m = (typeof(m)){"LPe1000-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001977 break;
1978 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001979 m = (typeof(m)){"LPe1002-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001980 break;
dea31012005-04-17 16:05:31 -05001981 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05001982 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001983 break;
1984 case PCI_DEVICE_ID_BSMB:
James Smarta747c9c2009-11-18 15:41:10 -05001985 m = (typeof(m)){"LP111", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001986 break;
1987 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05001988 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001989 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001990 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001991 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001992 break;
1993 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001994 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04001995 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001996 break;
dea31012005-04-17 16:05:31 -05001997 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05001998 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001999 break;
2000 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05002001 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002002 break;
2003 case PCI_DEVICE_ID_LP101:
James Smarta747c9c2009-11-18 15:41:10 -05002004 m = (typeof(m)){"LP101", "PCI-X", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002005 break;
2006 case PCI_DEVICE_ID_LP10000S:
James Smarta747c9c2009-11-18 15:41:10 -05002007 m = (typeof(m)){"LP10000-S", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002008 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002009 case PCI_DEVICE_ID_LP11000S:
James Smarta747c9c2009-11-18 15:41:10 -05002010 m = (typeof(m)){"LP11000-S", "PCI-X2", "Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05002011 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002012 case PCI_DEVICE_ID_LPE11000S:
James Smarta747c9c2009-11-18 15:41:10 -05002013 m = (typeof(m)){"LPe11000-S", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002014 break;
James Smartb87eab32007-04-25 09:53:28 -04002015 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05002016 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002017 break;
2018 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05002019 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002020 break;
2021 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05002022 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002023 break;
2024 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002025 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002026 break;
2027 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002028 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002029 break;
2030 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05002031 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002032 break;
James Smart84774a42008-08-24 21:50:06 -04002033 case PCI_DEVICE_ID_HORNET:
James Smarta747c9c2009-11-18 15:41:10 -05002034 m = (typeof(m)){"LP21000", "PCIe", "FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002035 GE = 1;
2036 break;
2037 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05002038 m = (typeof(m)){"LPev12000", "PCIe IOV",
2039 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002040 break;
2041 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05002042 m = (typeof(m)){"LPev12000", "PCIe IOV",
2043 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002044 break;
2045 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05002046 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
2047 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002048 break;
James Smartda0436e2009-05-22 14:51:39 -04002049 case PCI_DEVICE_ID_TIGERSHARK:
2050 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002051 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04002052 break;
James Smarta747c9c2009-11-18 15:41:10 -05002053 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04002054 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002055 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2056 break;
2057 case PCI_DEVICE_ID_FALCON:
2058 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2059 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04002060 break;
James Smart98fc5dd2010-06-07 15:24:29 -04002061 case PCI_DEVICE_ID_BALIUS:
2062 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
2063 "Fibre Channel Adapter"};
2064 break;
James Smart085c6472010-11-20 23:11:37 -05002065 case PCI_DEVICE_ID_LANCER_FC:
James Smartc0c11512011-05-24 11:41:34 -04002066 case PCI_DEVICE_ID_LANCER_FC_VF:
2067 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
James Smart085c6472010-11-20 23:11:37 -05002068 break;
2069 case PCI_DEVICE_ID_LANCER_FCOE:
James Smartc0c11512011-05-24 11:41:34 -04002070 case PCI_DEVICE_ID_LANCER_FCOE_VF:
James Smart085c6472010-11-20 23:11:37 -05002071 oneConnect = 1;
James Smart079b5c92011-08-21 21:48:49 -04002072 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
James Smart085c6472010-11-20 23:11:37 -05002073 break;
James Smartf8cafd32012-08-03 12:42:51 -04002074 case PCI_DEVICE_ID_SKYHAWK:
2075 case PCI_DEVICE_ID_SKYHAWK_VF:
2076 oneConnect = 1;
2077 m = (typeof(m)){"OCe14000", "PCIe", "FCoE"};
2078 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002079 default:
James Smarta747c9c2009-11-18 15:41:10 -05002080 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002081 break;
dea31012005-04-17 16:05:31 -05002082 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002083
2084 if (mdp && mdp[0] == '\0')
2085 snprintf(mdp, 79,"%s", m.name);
James Smartc0c11512011-05-24 11:41:34 -04002086 /*
2087 * oneConnect hba requires special processing, they are all initiators
James Smartda0436e2009-05-22 14:51:39 -04002088 * and we put the port number on the end
2089 */
2090 if (descp && descp[0] == '\0') {
2091 if (oneConnect)
2092 snprintf(descp, 255,
James Smart4169d862012-09-29 11:32:09 -04002093 "Emulex OneConnect %s, %s Initiator %s",
James Smarta747c9c2009-11-18 15:41:10 -05002094 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04002095 phba->Port);
James Smart4169d862012-09-29 11:32:09 -04002096 else if (max_speed == 0)
2097 snprintf(descp, 255,
2098 "Emulex %s %s %s ",
2099 m.name, m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002100 else
2101 snprintf(descp, 255,
2102 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05002103 m.name, max_speed, (GE) ? "GE" : "Gb",
2104 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002105 }
dea31012005-04-17 16:05:31 -05002106}
2107
James Smarte59058c2008-08-24 21:49:00 -04002108/**
James Smart3621a712009-04-06 18:47:14 -04002109 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04002110 * @phba: pointer to lpfc hba data structure.
2111 * @pring: pointer to a IOCB ring.
2112 * @cnt: the number of IOCBs to be posted to the IOCB ring.
2113 *
2114 * This routine posts a given number of IOCBs with the associated DMA buffer
2115 * descriptors specified by the cnt argument to the given IOCB ring.
2116 *
2117 * Return codes
2118 * The number of IOCBs NOT able to be posted to the IOCB ring.
2119 **/
dea31012005-04-17 16:05:31 -05002120int
James Smart495a7142008-06-14 22:52:59 -04002121lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05002122{
2123 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002124 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05002125 struct lpfc_dmabuf *mp1, *mp2;
2126
2127 cnt += pring->missbufcnt;
2128
2129 /* While there are buffers to post */
2130 while (cnt > 0) {
2131 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002132 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002133 if (iocb == NULL) {
2134 pring->missbufcnt = cnt;
2135 return cnt;
2136 }
dea31012005-04-17 16:05:31 -05002137 icmd = &iocb->iocb;
2138
2139 /* 2 buffers can be posted per command */
2140 /* Allocate buffer to post */
2141 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2142 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04002143 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2144 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002145 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002146 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002147 pring->missbufcnt = cnt;
2148 return cnt;
2149 }
2150
2151 INIT_LIST_HEAD(&mp1->list);
2152 /* Allocate buffer to post */
2153 if (cnt > 1) {
2154 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2155 if (mp2)
2156 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2157 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04002158 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002159 kfree(mp2);
dea31012005-04-17 16:05:31 -05002160 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2161 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002162 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002163 pring->missbufcnt = cnt;
2164 return cnt;
2165 }
2166
2167 INIT_LIST_HEAD(&mp2->list);
2168 } else {
2169 mp2 = NULL;
2170 }
2171
2172 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2173 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2174 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2175 icmd->ulpBdeCount = 1;
2176 cnt--;
2177 if (mp2) {
2178 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2179 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2180 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2181 cnt--;
2182 icmd->ulpBdeCount = 2;
2183 }
2184
2185 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2186 icmd->ulpLe = 1;
2187
James Smart3772a992009-05-22 14:50:54 -04002188 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2189 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05002190 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2191 kfree(mp1);
2192 cnt++;
2193 if (mp2) {
2194 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2195 kfree(mp2);
2196 cnt++;
2197 }
James Bottomley604a3e32005-10-29 10:28:33 -05002198 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002199 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05002200 return cnt;
2201 }
dea31012005-04-17 16:05:31 -05002202 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05002203 if (mp2)
dea31012005-04-17 16:05:31 -05002204 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05002205 }
2206 pring->missbufcnt = 0;
2207 return 0;
2208}
2209
James Smarte59058c2008-08-24 21:49:00 -04002210/**
James Smart3621a712009-04-06 18:47:14 -04002211 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04002212 * @phba: pointer to lpfc hba data structure.
2213 *
2214 * This routine posts initial receive IOCB buffers to the ELS ring. The
2215 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
2216 * set to 64 IOCBs.
2217 *
2218 * Return codes
2219 * 0 - success (currently always success)
2220 **/
dea31012005-04-17 16:05:31 -05002221static int
James Smart2e0fef82007-06-17 19:56:36 -05002222lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002223{
2224 struct lpfc_sli *psli = &phba->sli;
2225
2226 /* Ring 0, ELS / CT buffers */
James Smart495a7142008-06-14 22:52:59 -04002227 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05002228 /* Ring 2 - FCP no buffers needed */
2229
2230 return 0;
2231}
2232
2233#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2234
James Smarte59058c2008-08-24 21:49:00 -04002235/**
James Smart3621a712009-04-06 18:47:14 -04002236 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04002237 * @HashResultPointer: pointer to an array as hash table.
2238 *
2239 * This routine sets up the initial values to the array of hash table entries
2240 * for the LC HBAs.
2241 **/
dea31012005-04-17 16:05:31 -05002242static void
2243lpfc_sha_init(uint32_t * HashResultPointer)
2244{
2245 HashResultPointer[0] = 0x67452301;
2246 HashResultPointer[1] = 0xEFCDAB89;
2247 HashResultPointer[2] = 0x98BADCFE;
2248 HashResultPointer[3] = 0x10325476;
2249 HashResultPointer[4] = 0xC3D2E1F0;
2250}
2251
James Smarte59058c2008-08-24 21:49:00 -04002252/**
James Smart3621a712009-04-06 18:47:14 -04002253 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04002254 * @HashResultPointer: pointer to an initial/result hash table.
2255 * @HashWorkingPointer: pointer to an working hash table.
2256 *
2257 * This routine iterates an initial hash table pointed by @HashResultPointer
2258 * with the values from the working hash table pointeed by @HashWorkingPointer.
2259 * The results are putting back to the initial hash table, returned through
2260 * the @HashResultPointer as the result hash table.
2261 **/
dea31012005-04-17 16:05:31 -05002262static void
2263lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2264{
2265 int t;
2266 uint32_t TEMP;
2267 uint32_t A, B, C, D, E;
2268 t = 16;
2269 do {
2270 HashWorkingPointer[t] =
2271 S(1,
2272 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2273 8] ^
2274 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2275 } while (++t <= 79);
2276 t = 0;
2277 A = HashResultPointer[0];
2278 B = HashResultPointer[1];
2279 C = HashResultPointer[2];
2280 D = HashResultPointer[3];
2281 E = HashResultPointer[4];
2282
2283 do {
2284 if (t < 20) {
2285 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2286 } else if (t < 40) {
2287 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2288 } else if (t < 60) {
2289 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2290 } else {
2291 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2292 }
2293 TEMP += S(5, A) + E + HashWorkingPointer[t];
2294 E = D;
2295 D = C;
2296 C = S(30, B);
2297 B = A;
2298 A = TEMP;
2299 } while (++t <= 79);
2300
2301 HashResultPointer[0] += A;
2302 HashResultPointer[1] += B;
2303 HashResultPointer[2] += C;
2304 HashResultPointer[3] += D;
2305 HashResultPointer[4] += E;
2306
2307}
2308
James Smarte59058c2008-08-24 21:49:00 -04002309/**
James Smart3621a712009-04-06 18:47:14 -04002310 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002311 * @RandomChallenge: pointer to the entry of host challenge random number array.
2312 * @HashWorking: pointer to the entry of the working hash array.
2313 *
2314 * This routine calculates the working hash array referred by @HashWorking
2315 * from the challenge random numbers associated with the host, referred by
2316 * @RandomChallenge. The result is put into the entry of the working hash
2317 * array and returned by reference through @HashWorking.
2318 **/
dea31012005-04-17 16:05:31 -05002319static void
2320lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2321{
2322 *HashWorking = (*RandomChallenge ^ *HashWorking);
2323}
2324
James Smarte59058c2008-08-24 21:49:00 -04002325/**
James Smart3621a712009-04-06 18:47:14 -04002326 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002327 * @phba: pointer to lpfc hba data structure.
2328 * @hbainit: pointer to an array of unsigned 32-bit integers.
2329 *
2330 * This routine performs the special handling for LC HBA initialization.
2331 **/
dea31012005-04-17 16:05:31 -05002332void
2333lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2334{
2335 int t;
2336 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002337 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002338
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002339 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002340 if (!HashWorking)
2341 return;
2342
dea31012005-04-17 16:05:31 -05002343 HashWorking[0] = HashWorking[78] = *pwwnn++;
2344 HashWorking[1] = HashWorking[79] = *pwwnn;
2345
2346 for (t = 0; t < 7; t++)
2347 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2348
2349 lpfc_sha_init(hbainit);
2350 lpfc_sha_iterate(hbainit, HashWorking);
2351 kfree(HashWorking);
2352}
2353
James Smarte59058c2008-08-24 21:49:00 -04002354/**
James Smart3621a712009-04-06 18:47:14 -04002355 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002356 * @vport: pointer to a virtual N_Port data structure.
2357 *
2358 * This routine performs the necessary cleanups before deleting the @vport.
2359 * It invokes the discovery state machine to perform necessary state
2360 * transitions and to release the ndlps associated with the @vport. Note,
2361 * the physical port is treated as @vport 0.
2362 **/
James Smart87af33f2007-10-27 13:37:43 -04002363void
James Smart2e0fef82007-06-17 19:56:36 -05002364lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002365{
James Smart87af33f2007-10-27 13:37:43 -04002366 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002367 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002368 int i = 0;
dea31012005-04-17 16:05:31 -05002369
James Smart87af33f2007-10-27 13:37:43 -04002370 if (phba->link_state > LPFC_LINK_DOWN)
2371 lpfc_port_link_failure(vport);
2372
2373 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002374 if (!NLP_CHK_NODE_ACT(ndlp)) {
2375 ndlp = lpfc_enable_node(vport, ndlp,
2376 NLP_STE_UNUSED_NODE);
2377 if (!ndlp)
2378 continue;
2379 spin_lock_irq(&phba->ndlp_lock);
2380 NLP_SET_FREE_REQ(ndlp);
2381 spin_unlock_irq(&phba->ndlp_lock);
2382 /* Trigger the release of the ndlp memory */
2383 lpfc_nlp_put(ndlp);
2384 continue;
2385 }
2386 spin_lock_irq(&phba->ndlp_lock);
2387 if (NLP_CHK_FREE_REQ(ndlp)) {
2388 /* The ndlp should not be in memory free mode already */
2389 spin_unlock_irq(&phba->ndlp_lock);
2390 continue;
2391 } else
2392 /* Indicate request for freeing ndlp memory */
2393 NLP_SET_FREE_REQ(ndlp);
2394 spin_unlock_irq(&phba->ndlp_lock);
2395
James Smart58da1ff2008-04-07 10:15:56 -04002396 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2397 ndlp->nlp_DID == Fabric_DID) {
2398 /* Just free up ndlp with Fabric_DID for vports */
2399 lpfc_nlp_put(ndlp);
2400 continue;
2401 }
2402
James Smarteff4a012012-01-18 16:25:25 -05002403 /* take care of nodes in unused state before the state
2404 * machine taking action.
2405 */
2406 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
2407 lpfc_nlp_put(ndlp);
2408 continue;
2409 }
2410
James Smart87af33f2007-10-27 13:37:43 -04002411 if (ndlp->nlp_type & NLP_FABRIC)
2412 lpfc_disc_state_machine(vport, ndlp, NULL,
2413 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002414
James Smart87af33f2007-10-27 13:37:43 -04002415 lpfc_disc_state_machine(vport, ndlp, NULL,
2416 NLP_EVT_DEVICE_RM);
2417 }
2418
James Smarta8adb832007-10-27 13:37:53 -04002419 /* At this point, ALL ndlp's should be gone
2420 * because of the previous NLP_EVT_DEVICE_RM.
2421 * Lets wait for this to happen, if needed.
2422 */
James Smart87af33f2007-10-27 13:37:43 -04002423 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002424 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002425 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002426 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002427 list_for_each_entry_safe(ndlp, next_ndlp,
2428 &vport->fc_nodes, nlp_listp) {
2429 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2430 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002431 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002432 "usgmap:x%x refcnt:%d\n",
2433 ndlp->nlp_DID, (void *)ndlp,
2434 ndlp->nlp_usg_map,
2435 atomic_read(
2436 &ndlp->kref.refcount));
2437 }
James Smarta8adb832007-10-27 13:37:53 -04002438 break;
James Smart87af33f2007-10-27 13:37:43 -04002439 }
James Smarta8adb832007-10-27 13:37:53 -04002440
2441 /* Wait for any activity on ndlps to settle */
2442 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002443 }
James Smart1151e3e2011-02-16 12:39:35 -05002444 lpfc_cleanup_vports_rrqs(vport, NULL);
dea31012005-04-17 16:05:31 -05002445}
2446
James Smarte59058c2008-08-24 21:49:00 -04002447/**
James Smart3621a712009-04-06 18:47:14 -04002448 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002449 * @vport: pointer to a virtual N_Port data structure.
2450 *
2451 * This routine stops all the timers associated with a @vport. This function
2452 * is invoked before disabling or deleting a @vport. Note that the physical
2453 * port is treated as @vport 0.
2454 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002455void
2456lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002457{
James Smart92d7f7b2007-06-17 19:56:38 -05002458 del_timer_sync(&vport->els_tmofunc);
2459 del_timer_sync(&vport->fc_fdmitmo);
James Smart92494142011-02-16 12:39:44 -05002460 del_timer_sync(&vport->delayed_disc_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002461 lpfc_can_disctmo(vport);
2462 return;
dea31012005-04-17 16:05:31 -05002463}
2464
James Smarte59058c2008-08-24 21:49:00 -04002465/**
James Smartecfd03c2010-02-12 14:41:27 -05002466 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2467 * @phba: pointer to lpfc hba data structure.
2468 *
2469 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2470 * caller of this routine should already hold the host lock.
2471 **/
2472void
2473__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2474{
James Smart5ac6b302010-10-22 11:05:36 -04002475 /* Clear pending FCF rediscovery wait flag */
2476 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2477
James Smartecfd03c2010-02-12 14:41:27 -05002478 /* Now, try to stop the timer */
2479 del_timer(&phba->fcf.redisc_wait);
2480}
2481
2482/**
2483 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2484 * @phba: pointer to lpfc hba data structure.
2485 *
2486 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2487 * checks whether the FCF rediscovery wait timer is pending with the host
2488 * lock held before proceeding with disabling the timer and clearing the
2489 * wait timer pendig flag.
2490 **/
2491void
2492lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2493{
2494 spin_lock_irq(&phba->hbalock);
2495 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2496 /* FCF rediscovery timer already fired or stopped */
2497 spin_unlock_irq(&phba->hbalock);
2498 return;
2499 }
2500 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart5ac6b302010-10-22 11:05:36 -04002501 /* Clear failover in progress flags */
2502 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05002503 spin_unlock_irq(&phba->hbalock);
2504}
2505
2506/**
James Smart3772a992009-05-22 14:50:54 -04002507 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002508 * @phba: pointer to lpfc hba data structure.
2509 *
2510 * This routine stops all the timers associated with a HBA. This function is
2511 * invoked before either putting a HBA offline or unloading the driver.
2512 **/
James Smart3772a992009-05-22 14:50:54 -04002513void
2514lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002515{
James Smart51ef4c22007-08-02 11:10:31 -04002516 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05002517 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002518 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002519 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002520 del_timer_sync(&phba->hb_tmofunc);
James Smart1151e3e2011-02-16 12:39:35 -05002521 if (phba->sli_rev == LPFC_SLI_REV4) {
2522 del_timer_sync(&phba->rrq_tmr);
2523 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2524 }
James Smart3772a992009-05-22 14:50:54 -04002525 phba->hb_outstanding = 0;
2526
2527 switch (phba->pci_dev_grp) {
2528 case LPFC_PCI_DEV_LP:
2529 /* Stop any LightPulse device specific driver timers */
2530 del_timer_sync(&phba->fcp_poll_timer);
2531 break;
2532 case LPFC_PCI_DEV_OC:
2533 /* Stop any OneConnect device sepcific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05002534 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04002535 break;
2536 default:
2537 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2538 "0297 Invalid device group (x%x)\n",
2539 phba->pci_dev_grp);
2540 break;
2541 }
James Smart2e0fef82007-06-17 19:56:36 -05002542 return;
dea31012005-04-17 16:05:31 -05002543}
2544
James Smarte59058c2008-08-24 21:49:00 -04002545/**
James Smart3621a712009-04-06 18:47:14 -04002546 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002547 * @phba: pointer to lpfc hba data structure.
2548 *
2549 * This routine marks a HBA's management interface as blocked. Once the HBA's
2550 * management interface is marked as blocked, all the user space access to
2551 * the HBA, whether they are from sysfs interface or libdfc interface will
2552 * all be blocked. The HBA is set to block the management interface when the
2553 * driver prepares the HBA interface for online or offline.
2554 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002555static void
James Smart618a5232012-06-12 13:54:36 -04002556lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
Adrian Bunka6ababd2007-11-05 18:07:33 +01002557{
2558 unsigned long iflag;
James Smart6e7288d2010-06-07 15:23:35 -04002559 uint8_t actcmd = MBX_HEARTBEAT;
2560 unsigned long timeout;
2561
Adrian Bunka6ababd2007-11-05 18:07:33 +01002562 spin_lock_irqsave(&phba->hbalock, iflag);
2563 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
James Smart618a5232012-06-12 13:54:36 -04002564 spin_unlock_irqrestore(&phba->hbalock, iflag);
2565 if (mbx_action == LPFC_MBX_NO_WAIT)
2566 return;
2567 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
2568 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002569 if (phba->sli.mbox_active) {
James Smart6e7288d2010-06-07 15:23:35 -04002570 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
James Smarta183a152011-10-10 21:32:43 -04002571 /* Determine how long we might wait for the active mailbox
2572 * command to be gracefully completed by firmware.
2573 */
2574 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
2575 phba->sli.mbox_active) * 1000) + jiffies;
2576 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002577 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002578
James Smart6e7288d2010-06-07 15:23:35 -04002579 /* Wait for the outstnading mailbox command to complete */
2580 while (phba->sli.mbox_active) {
2581 /* Check active mailbox complete status every 2ms */
2582 msleep(2);
2583 if (time_after(jiffies, timeout)) {
2584 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2585 "2813 Mgmt IO is Blocked %x "
2586 "- mbox cmd %x still active\n",
2587 phba->sli.sli_flag, actcmd);
2588 break;
2589 }
2590 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002591}
2592
James Smarte59058c2008-08-24 21:49:00 -04002593/**
James Smart6b5151f2012-01-18 16:24:06 -05002594 * lpfc_sli4_node_prep - Assign RPIs for active nodes.
2595 * @phba: pointer to lpfc hba data structure.
2596 *
2597 * Allocate RPIs for all active remote nodes. This is needed whenever
2598 * an SLI4 adapter is reset and the driver is not unloading. Its purpose
2599 * is to fixup the temporary rpi assignments.
2600 **/
2601void
2602lpfc_sli4_node_prep(struct lpfc_hba *phba)
2603{
2604 struct lpfc_nodelist *ndlp, *next_ndlp;
2605 struct lpfc_vport **vports;
2606 int i;
2607
2608 if (phba->sli_rev != LPFC_SLI_REV4)
2609 return;
2610
2611 vports = lpfc_create_vport_work_array(phba);
2612 if (vports != NULL) {
2613 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2614 if (vports[i]->load_flag & FC_UNLOADING)
2615 continue;
2616
2617 list_for_each_entry_safe(ndlp, next_ndlp,
2618 &vports[i]->fc_nodes,
2619 nlp_listp) {
2620 if (NLP_CHK_NODE_ACT(ndlp))
2621 ndlp->nlp_rpi =
2622 lpfc_sli4_alloc_rpi(phba);
2623 }
2624 }
2625 }
2626 lpfc_destroy_vport_work_array(phba, vports);
2627}
2628
2629/**
James Smart3621a712009-04-06 18:47:14 -04002630 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04002631 * @phba: pointer to lpfc hba data structure.
2632 *
2633 * This routine initializes the HBA and brings a HBA online. During this
2634 * process, the management interface is blocked to prevent user space access
2635 * to the HBA interfering with the driver initialization.
2636 *
2637 * Return codes
2638 * 0 - successful
2639 * 1 - failed
2640 **/
dea31012005-04-17 16:05:31 -05002641int
James Smart2e0fef82007-06-17 19:56:36 -05002642lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002643{
Julia Lawall372bd282008-12-16 16:15:08 +01002644 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04002645 struct lpfc_vport **vports;
2646 int i;
James Smart16a3a202013-04-17 20:14:38 -04002647 bool vpis_cleared = false;
James Smart2e0fef82007-06-17 19:56:36 -05002648
dea31012005-04-17 16:05:31 -05002649 if (!phba)
2650 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01002651 vport = phba->pport;
dea31012005-04-17 16:05:31 -05002652
James Smart2e0fef82007-06-17 19:56:36 -05002653 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05002654 return 0;
2655
James Smarted957682007-06-17 19:56:37 -05002656 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002657 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05002658
James Smart618a5232012-06-12 13:54:36 -04002659 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05002660
James Smart46fa3112007-04-25 09:51:45 -04002661 if (!lpfc_sli_queue_setup(phba)) {
2662 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002663 return 1;
James Smart46fa3112007-04-25 09:51:45 -04002664 }
2665
James Smartda0436e2009-05-22 14:51:39 -04002666 if (phba->sli_rev == LPFC_SLI_REV4) {
2667 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2668 lpfc_unblock_mgmt_io(phba);
2669 return 1;
2670 }
James Smart16a3a202013-04-17 20:14:38 -04002671 spin_lock_irq(&phba->hbalock);
2672 if (!phba->sli4_hba.max_cfg_param.vpi_used)
2673 vpis_cleared = true;
2674 spin_unlock_irq(&phba->hbalock);
James Smartda0436e2009-05-22 14:51:39 -04002675 } else {
2676 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2677 lpfc_unblock_mgmt_io(phba);
2678 return 1;
2679 }
James Smart46fa3112007-04-25 09:51:45 -04002680 }
dea31012005-04-17 16:05:31 -05002681
James Smart549e55c2007-08-02 11:09:51 -04002682 vports = lpfc_create_vport_work_array(phba);
2683 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002684 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002685 struct Scsi_Host *shost;
2686 shost = lpfc_shost_from_vport(vports[i]);
2687 spin_lock_irq(shost->host_lock);
2688 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2689 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2690 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart16a3a202013-04-17 20:14:38 -04002691 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart1c6834a2009-07-19 10:01:26 -04002692 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart16a3a202013-04-17 20:14:38 -04002693 if ((vpis_cleared) &&
2694 (vports[i]->port_type !=
2695 LPFC_PHYSICAL_PORT))
2696 vports[i]->vpi = 0;
2697 }
James Smart549e55c2007-08-02 11:09:51 -04002698 spin_unlock_irq(shost->host_lock);
2699 }
James Smart09372822008-01-11 01:52:54 -05002700 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002701
James Smart46fa3112007-04-25 09:51:45 -04002702 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002703 return 0;
2704}
2705
James Smarte59058c2008-08-24 21:49:00 -04002706/**
James Smart3621a712009-04-06 18:47:14 -04002707 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04002708 * @phba: pointer to lpfc hba data structure.
2709 *
2710 * This routine marks a HBA's management interface as not blocked. Once the
2711 * HBA's management interface is marked as not blocked, all the user space
2712 * access to the HBA, whether they are from sysfs interface or libdfc
2713 * interface will be allowed. The HBA is set to block the management interface
2714 * when the driver prepares the HBA interface for online or offline and then
2715 * set to unblock the management interface afterwards.
2716 **/
James Smart46fa3112007-04-25 09:51:45 -04002717void
James Smart46fa3112007-04-25 09:51:45 -04002718lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2719{
2720 unsigned long iflag;
2721
James Smart2e0fef82007-06-17 19:56:36 -05002722 spin_lock_irqsave(&phba->hbalock, iflag);
2723 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2724 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04002725}
2726
James Smarte59058c2008-08-24 21:49:00 -04002727/**
James Smart3621a712009-04-06 18:47:14 -04002728 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04002729 * @phba: pointer to lpfc hba data structure.
2730 *
2731 * This routine is invoked to prepare a HBA to be brought offline. It performs
2732 * unregistration login to all the nodes on all vports and flushes the mailbox
2733 * queue to make it ready to be brought offline.
2734 **/
James Smart46fa3112007-04-25 09:51:45 -04002735void
James Smart618a5232012-06-12 13:54:36 -04002736lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
James Smart46fa3112007-04-25 09:51:45 -04002737{
James Smart2e0fef82007-06-17 19:56:36 -05002738 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04002739 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04002740 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05002741 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04002742 int i;
dea31012005-04-17 16:05:31 -05002743
James Smart2e0fef82007-06-17 19:56:36 -05002744 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002745 return;
dea31012005-04-17 16:05:31 -05002746
James Smart618a5232012-06-12 13:54:36 -04002747 lpfc_block_mgmt_io(phba, mbx_action);
dea31012005-04-17 16:05:31 -05002748
2749 lpfc_linkdown(phba);
2750
James Smart87af33f2007-10-27 13:37:43 -04002751 /* Issue an unreg_login to all nodes on all vports */
2752 vports = lpfc_create_vport_work_array(phba);
2753 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04002754 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04002755 if (vports[i]->load_flag & FC_UNLOADING)
2756 continue;
James Smart72100cc2010-02-12 14:43:01 -05002757 shost = lpfc_shost_from_vport(vports[i]);
2758 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002759 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002760 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2761 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05002762 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002763
James Smart87af33f2007-10-27 13:37:43 -04002764 shost = lpfc_shost_from_vport(vports[i]);
2765 list_for_each_entry_safe(ndlp, next_ndlp,
2766 &vports[i]->fc_nodes,
2767 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002768 if (!NLP_CHK_NODE_ACT(ndlp))
2769 continue;
James Smart87af33f2007-10-27 13:37:43 -04002770 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2771 continue;
2772 if (ndlp->nlp_type & NLP_FABRIC) {
2773 lpfc_disc_state_machine(vports[i], ndlp,
2774 NULL, NLP_EVT_DEVICE_RECOVERY);
2775 lpfc_disc_state_machine(vports[i], ndlp,
2776 NULL, NLP_EVT_DEVICE_RM);
2777 }
2778 spin_lock_irq(shost->host_lock);
2779 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart401ee0c2012-03-01 22:35:34 -05002780 spin_unlock_irq(shost->host_lock);
James Smart6b5151f2012-01-18 16:24:06 -05002781 /*
2782 * Whenever an SLI4 port goes offline, free the
James Smart401ee0c2012-03-01 22:35:34 -05002783 * RPI. Get a new RPI when the adapter port
2784 * comes back online.
James Smart6b5151f2012-01-18 16:24:06 -05002785 */
2786 if (phba->sli_rev == LPFC_SLI_REV4)
2787 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smart87af33f2007-10-27 13:37:43 -04002788 lpfc_unreg_rpi(vports[i], ndlp);
2789 }
2790 }
2791 }
James Smart09372822008-01-11 01:52:54 -05002792 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002793
James Smart618a5232012-06-12 13:54:36 -04002794 lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
James Smart46fa3112007-04-25 09:51:45 -04002795}
2796
James Smarte59058c2008-08-24 21:49:00 -04002797/**
James Smart3621a712009-04-06 18:47:14 -04002798 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04002799 * @phba: pointer to lpfc hba data structure.
2800 *
2801 * This routine actually brings a HBA offline. It stops all the timers
2802 * associated with the HBA, brings down the SLI layer, and eventually
2803 * marks the HBA as in offline state for the upper layer protocol.
2804 **/
James Smart46fa3112007-04-25 09:51:45 -04002805void
James Smart2e0fef82007-06-17 19:56:36 -05002806lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04002807{
James Smart549e55c2007-08-02 11:09:51 -04002808 struct Scsi_Host *shost;
2809 struct lpfc_vport **vports;
2810 int i;
James Smart46fa3112007-04-25 09:51:45 -04002811
James Smart549e55c2007-08-02 11:09:51 -04002812 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002813 return;
James Smart688a8862006-07-06 15:49:56 -04002814
James Smartda0436e2009-05-22 14:51:39 -04002815 /* stop port and all timers associated with this hba */
2816 lpfc_stop_port(phba);
James Smart51ef4c22007-08-02 11:10:31 -04002817 vports = lpfc_create_vport_work_array(phba);
2818 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002819 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04002820 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05002821 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05002822 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002823 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05002824 /* Bring down the SLI Layer and cleanup. The HBA is offline
2825 now. */
2826 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002827 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04002828 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002829 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04002830 vports = lpfc_create_vport_work_array(phba);
2831 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002832 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002833 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04002834 spin_lock_irq(shost->host_lock);
2835 vports[i]->work_port_events = 0;
2836 vports[i]->fc_flag |= FC_OFFLINE_MODE;
2837 spin_unlock_irq(shost->host_lock);
2838 }
James Smart09372822008-01-11 01:52:54 -05002839 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002840}
2841
James Smarte59058c2008-08-24 21:49:00 -04002842/**
James Smart3621a712009-04-06 18:47:14 -04002843 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04002844 * @phba: pointer to lpfc hba data structure.
2845 *
2846 * This routine is to free all the SCSI buffers and IOCBs from the driver
2847 * list back to kernel. It is called from lpfc_pci_remove_one to free
2848 * the internal resources before the device is removed from the system.
James Smarte59058c2008-08-24 21:49:00 -04002849 **/
James Smart8a9d2e82012-05-09 21:16:12 -04002850static void
James Smart2e0fef82007-06-17 19:56:36 -05002851lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002852{
2853 struct lpfc_scsi_buf *sb, *sb_next;
2854 struct lpfc_iocbq *io, *io_next;
2855
James Smart2e0fef82007-06-17 19:56:36 -05002856 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002857 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smart1c6f4ef52009-11-18 15:40:49 -05002858 spin_lock(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05002859 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
2860 list_del(&sb->list);
2861 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05002862 sb->dma_handle);
dea31012005-04-17 16:05:31 -05002863 kfree(sb);
2864 phba->total_scsi_bufs--;
2865 }
James Smart1c6f4ef52009-11-18 15:40:49 -05002866 spin_unlock(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05002867
2868 /* Release all the lpfc_iocbq entries maintained by this host. */
2869 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2870 list_del(&io->list);
2871 kfree(io);
2872 phba->total_iocbq_bufs--;
2873 }
James Smart6d368e52011-05-24 11:44:12 -04002874
James Smart2e0fef82007-06-17 19:56:36 -05002875 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04002876}
2877
2878/**
2879 * lpfc_sli4_xri_sgl_update - update xri-sgl sizing and mapping
2880 * @phba: pointer to lpfc hba data structure.
2881 *
2882 * This routine first calculates the sizes of the current els and allocated
2883 * scsi sgl lists, and then goes through all sgls to updates the physical
2884 * XRIs assigned due to port function reset. During port initialization, the
2885 * current els and allocated scsi sgl lists are 0s.
2886 *
2887 * Return codes
2888 * 0 - successful (for now, it always returns 0)
2889 **/
2890int
2891lpfc_sli4_xri_sgl_update(struct lpfc_hba *phba)
2892{
2893 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
2894 struct lpfc_scsi_buf *psb = NULL, *psb_next = NULL;
2895 uint16_t i, lxri, xri_cnt, els_xri_cnt, scsi_xri_cnt;
2896 LIST_HEAD(els_sgl_list);
2897 LIST_HEAD(scsi_sgl_list);
2898 int rc;
2899
2900 /*
2901 * update on pci function's els xri-sgl list
2902 */
2903 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
2904 if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
2905 /* els xri-sgl expanded */
2906 xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
2907 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2908 "3157 ELS xri-sgl count increased from "
2909 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
2910 els_xri_cnt);
2911 /* allocate the additional els sgls */
2912 for (i = 0; i < xri_cnt; i++) {
2913 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
2914 GFP_KERNEL);
2915 if (sglq_entry == NULL) {
2916 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2917 "2562 Failure to allocate an "
2918 "ELS sgl entry:%d\n", i);
2919 rc = -ENOMEM;
2920 goto out_free_mem;
2921 }
2922 sglq_entry->buff_type = GEN_BUFF_TYPE;
2923 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
2924 &sglq_entry->phys);
2925 if (sglq_entry->virt == NULL) {
2926 kfree(sglq_entry);
2927 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2928 "2563 Failure to allocate an "
2929 "ELS mbuf:%d\n", i);
2930 rc = -ENOMEM;
2931 goto out_free_mem;
2932 }
2933 sglq_entry->sgl = sglq_entry->virt;
2934 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
2935 sglq_entry->state = SGL_FREED;
2936 list_add_tail(&sglq_entry->list, &els_sgl_list);
2937 }
James Smart38c20672013-03-01 16:37:44 -05002938 spin_lock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04002939 list_splice_init(&els_sgl_list, &phba->sli4_hba.lpfc_sgl_list);
James Smart38c20672013-03-01 16:37:44 -05002940 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04002941 } else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
2942 /* els xri-sgl shrinked */
2943 xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
2944 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2945 "3158 ELS xri-sgl count decreased from "
2946 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
2947 els_xri_cnt);
2948 spin_lock_irq(&phba->hbalock);
2949 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &els_sgl_list);
2950 spin_unlock_irq(&phba->hbalock);
2951 /* release extra els sgls from list */
2952 for (i = 0; i < xri_cnt; i++) {
2953 list_remove_head(&els_sgl_list,
2954 sglq_entry, struct lpfc_sglq, list);
2955 if (sglq_entry) {
2956 lpfc_mbuf_free(phba, sglq_entry->virt,
2957 sglq_entry->phys);
2958 kfree(sglq_entry);
2959 }
2960 }
2961 spin_lock_irq(&phba->hbalock);
2962 list_splice_init(&els_sgl_list, &phba->sli4_hba.lpfc_sgl_list);
2963 spin_unlock_irq(&phba->hbalock);
2964 } else
2965 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2966 "3163 ELS xri-sgl count unchanged: %d\n",
2967 els_xri_cnt);
2968 phba->sli4_hba.els_xri_cnt = els_xri_cnt;
2969
2970 /* update xris to els sgls on the list */
2971 sglq_entry = NULL;
2972 sglq_entry_next = NULL;
2973 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
2974 &phba->sli4_hba.lpfc_sgl_list, list) {
2975 lxri = lpfc_sli4_next_xritag(phba);
2976 if (lxri == NO_XRI) {
2977 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2978 "2400 Failed to allocate xri for "
2979 "ELS sgl\n");
2980 rc = -ENOMEM;
2981 goto out_free_mem;
2982 }
2983 sglq_entry->sli4_lxritag = lxri;
2984 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
2985 }
2986
2987 /*
2988 * update on pci function's allocated scsi xri-sgl list
2989 */
2990 phba->total_scsi_bufs = 0;
2991
2992 /* maximum number of xris available for scsi buffers */
2993 phba->sli4_hba.scsi_xri_max = phba->sli4_hba.max_cfg_param.max_xri -
2994 els_xri_cnt;
2995
2996 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2997 "2401 Current allocated SCSI xri-sgl count:%d, "
2998 "maximum SCSI xri count:%d\n",
2999 phba->sli4_hba.scsi_xri_cnt,
3000 phba->sli4_hba.scsi_xri_max);
3001
3002 spin_lock_irq(&phba->scsi_buf_list_lock);
3003 list_splice_init(&phba->lpfc_scsi_buf_list, &scsi_sgl_list);
3004 spin_unlock_irq(&phba->scsi_buf_list_lock);
3005
3006 if (phba->sli4_hba.scsi_xri_cnt > phba->sli4_hba.scsi_xri_max) {
3007 /* max scsi xri shrinked below the allocated scsi buffers */
3008 scsi_xri_cnt = phba->sli4_hba.scsi_xri_cnt -
3009 phba->sli4_hba.scsi_xri_max;
3010 /* release the extra allocated scsi buffers */
3011 for (i = 0; i < scsi_xri_cnt; i++) {
3012 list_remove_head(&scsi_sgl_list, psb,
3013 struct lpfc_scsi_buf, list);
3014 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, psb->data,
3015 psb->dma_handle);
3016 kfree(psb);
3017 }
3018 spin_lock_irq(&phba->scsi_buf_list_lock);
3019 phba->sli4_hba.scsi_xri_cnt -= scsi_xri_cnt;
3020 spin_unlock_irq(&phba->scsi_buf_list_lock);
3021 }
3022
3023 /* update xris associated to remaining allocated scsi buffers */
3024 psb = NULL;
3025 psb_next = NULL;
3026 list_for_each_entry_safe(psb, psb_next, &scsi_sgl_list, list) {
3027 lxri = lpfc_sli4_next_xritag(phba);
3028 if (lxri == NO_XRI) {
3029 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3030 "2560 Failed to allocate xri for "
3031 "scsi buffer\n");
3032 rc = -ENOMEM;
3033 goto out_free_mem;
3034 }
3035 psb->cur_iocbq.sli4_lxritag = lxri;
3036 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3037 }
James Smart38c20672013-03-01 16:37:44 -05003038 spin_lock_irq(&phba->scsi_buf_list_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003039 list_splice_init(&scsi_sgl_list, &phba->lpfc_scsi_buf_list);
James Smart38c20672013-03-01 16:37:44 -05003040 spin_unlock_irq(&phba->scsi_buf_list_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003041
dea31012005-04-17 16:05:31 -05003042 return 0;
James Smart8a9d2e82012-05-09 21:16:12 -04003043
3044out_free_mem:
3045 lpfc_free_els_sgl_list(phba);
3046 lpfc_scsi_free(phba);
3047 return rc;
dea31012005-04-17 16:05:31 -05003048}
3049
James Smarte59058c2008-08-24 21:49:00 -04003050/**
James Smart3621a712009-04-06 18:47:14 -04003051 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04003052 * @phba: pointer to lpfc hba data structure.
3053 * @instance: a unique integer ID to this FC port.
3054 * @dev: pointer to the device data structure.
3055 *
3056 * This routine creates a FC port for the upper layer protocol. The FC port
3057 * can be created on top of either a physical port or a virtual port provided
3058 * by the HBA. This routine also allocates a SCSI host data structure (shost)
3059 * and associates the FC port created before adding the shost into the SCSI
3060 * layer.
3061 *
3062 * Return codes
3063 * @vport - pointer to the virtual N_Port data structure.
3064 * NULL - port create failed.
3065 **/
James Smart2e0fef82007-06-17 19:56:36 -05003066struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04003067lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04003068{
James Smart2e0fef82007-06-17 19:56:36 -05003069 struct lpfc_vport *vport;
3070 struct Scsi_Host *shost;
3071 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04003072
James Smart3de2a652007-08-02 11:09:59 -04003073 if (dev != &phba->pcidev->dev)
3074 shost = scsi_host_alloc(&lpfc_vport_template,
3075 sizeof(struct lpfc_vport));
3076 else
3077 shost = scsi_host_alloc(&lpfc_template,
3078 sizeof(struct lpfc_vport));
James Smart2e0fef82007-06-17 19:56:36 -05003079 if (!shost)
3080 goto out;
James Smart47a86172007-04-25 09:53:22 -04003081
James Smart2e0fef82007-06-17 19:56:36 -05003082 vport = (struct lpfc_vport *) shost->hostdata;
3083 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05003084 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05003085 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05003086 vport->fc_rscn_flush = 0;
James Smart47a86172007-04-25 09:53:22 -04003087
James Smart3de2a652007-08-02 11:09:59 -04003088 lpfc_get_vport_cfgparam(vport);
James Smart2e0fef82007-06-17 19:56:36 -05003089 shost->unique_id = instance;
3090 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04003091 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05003092 shost->this_id = -1;
3093 shost->max_cmd_len = 16;
James Smartda0436e2009-05-22 14:51:39 -04003094 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart28baac72010-02-12 14:42:03 -05003095 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04003096 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smartda0436e2009-05-22 14:51:39 -04003097 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
3098 }
James Smart81301a92008-12-04 22:39:46 -05003099
James Smart47a86172007-04-25 09:53:22 -04003100 /*
James Smart2e0fef82007-06-17 19:56:36 -05003101 * Set initial can_queue value since 0 is no longer supported and
3102 * scsi_add_host will fail. This will be adjusted later based on the
3103 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04003104 */
James Smart2e0fef82007-06-17 19:56:36 -05003105 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04003106 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05003107 shost->transportt = lpfc_vport_transport_template;
3108 vport->port_type = LPFC_NPIV_PORT;
3109 } else {
3110 shost->transportt = lpfc_transport_template;
3111 vport->port_type = LPFC_PHYSICAL_PORT;
3112 }
James Smart47a86172007-04-25 09:53:22 -04003113
James Smart2e0fef82007-06-17 19:56:36 -05003114 /* Initialize all internally managed lists. */
3115 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04003116 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05003117 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04003118
James Smart2e0fef82007-06-17 19:56:36 -05003119 init_timer(&vport->fc_disctmo);
3120 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05003121 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04003122
James Smart2e0fef82007-06-17 19:56:36 -05003123 init_timer(&vport->fc_fdmitmo);
3124 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
James Smart92d7f7b2007-06-17 19:56:38 -05003125 vport->fc_fdmitmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04003126
James Smart2e0fef82007-06-17 19:56:36 -05003127 init_timer(&vport->els_tmofunc);
3128 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05003129 vport->els_tmofunc.data = (unsigned long)vport;
James Smart92494142011-02-16 12:39:44 -05003130
3131 init_timer(&vport->delayed_disc_tmo);
3132 vport->delayed_disc_tmo.function = lpfc_delayed_disc_tmo;
3133 vport->delayed_disc_tmo.data = (unsigned long)vport;
3134
James Bottomleyd139b9b2009-11-05 13:33:12 -06003135 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05003136 if (error)
3137 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04003138
James Smart549e55c2007-08-02 11:09:51 -04003139 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003140 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04003141 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003142 return vport;
James Smart47a86172007-04-25 09:53:22 -04003143
James Smart2e0fef82007-06-17 19:56:36 -05003144out_put_shost:
3145 scsi_host_put(shost);
3146out:
3147 return NULL;
James Smart47a86172007-04-25 09:53:22 -04003148}
3149
James Smarte59058c2008-08-24 21:49:00 -04003150/**
James Smart3621a712009-04-06 18:47:14 -04003151 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04003152 * @vport: pointer to an lpfc virtual N_Port data structure.
3153 *
3154 * This routine destroys a FC port from the upper layer protocol. All the
3155 * resources associated with the port are released.
3156 **/
James Smart2e0fef82007-06-17 19:56:36 -05003157void
3158destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04003159{
James Smart92d7f7b2007-06-17 19:56:38 -05003160 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3161 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003162
James Smart858c9f62007-06-17 19:56:39 -05003163 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05003164 fc_remove_host(shost);
3165 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04003166
James Smart92d7f7b2007-06-17 19:56:38 -05003167 spin_lock_irq(&phba->hbalock);
3168 list_del_init(&vport->listentry);
3169 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04003170
James Smart92d7f7b2007-06-17 19:56:38 -05003171 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04003172 return;
James Smart47a86172007-04-25 09:53:22 -04003173}
3174
James Smarte59058c2008-08-24 21:49:00 -04003175/**
James Smart3621a712009-04-06 18:47:14 -04003176 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04003177 *
3178 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
3179 * uses the kernel idr facility to perform the task.
3180 *
3181 * Return codes:
3182 * instance - a unique integer ID allocated as the new instance.
3183 * -1 - lpfc get instance failed.
3184 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003185int
3186lpfc_get_instance(void)
3187{
Tejun Heoab516032013-02-27 17:04:44 -08003188 int ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003189
Tejun Heoab516032013-02-27 17:04:44 -08003190 ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
3191 return ret < 0 ? -1 : ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003192}
3193
James Smarte59058c2008-08-24 21:49:00 -04003194/**
James Smart3621a712009-04-06 18:47:14 -04003195 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04003196 * @shost: pointer to SCSI host data structure.
3197 * @time: elapsed time of the scan in jiffies.
3198 *
3199 * This routine is called by the SCSI layer with a SCSI host to determine
3200 * whether the scan host is finished.
3201 *
3202 * Note: there is no scan_start function as adapter initialization will have
3203 * asynchronously kicked off the link initialization.
3204 *
3205 * Return codes
3206 * 0 - SCSI host scan is not over yet.
3207 * 1 - SCSI host scan is over.
3208 **/
James Smart47a86172007-04-25 09:53:22 -04003209int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
3210{
James Smart2e0fef82007-06-17 19:56:36 -05003211 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3212 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05003213 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04003214
James Smart858c9f62007-06-17 19:56:39 -05003215 spin_lock_irq(shost->host_lock);
3216
James Smart51ef4c22007-08-02 11:10:31 -04003217 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05003218 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003219 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05003220 }
James Smart256ec0d2013-04-17 20:14:58 -04003221 if (time >= msecs_to_jiffies(30 * 1000)) {
James Smart2e0fef82007-06-17 19:56:36 -05003222 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003223 "0461 Scanning longer than 30 "
3224 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003225 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003226 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003227 }
James Smart256ec0d2013-04-17 20:14:58 -04003228 if (time >= msecs_to_jiffies(15 * 1000) &&
3229 phba->link_state <= LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -05003230 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003231 "0465 Link down longer than 15 "
3232 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003233 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05003234 goto finished;
James Smart47a86172007-04-25 09:53:22 -04003235 }
3236
James Smart2e0fef82007-06-17 19:56:36 -05003237 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05003238 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003239 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05003240 goto finished;
James Smart256ec0d2013-04-17 20:14:58 -04003241 if (vport->fc_map_cnt == 0 && time < msecs_to_jiffies(2 * 1000))
James Smart858c9f62007-06-17 19:56:39 -05003242 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003243 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05003244 goto finished;
3245
3246 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003247
3248finished:
James Smart858c9f62007-06-17 19:56:39 -05003249 spin_unlock_irq(shost->host_lock);
3250 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05003251}
3252
James Smarte59058c2008-08-24 21:49:00 -04003253/**
James Smart3621a712009-04-06 18:47:14 -04003254 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04003255 * @shost: pointer to SCSI host data structure.
3256 *
3257 * This routine initializes a given SCSI host attributes on a FC port. The
3258 * SCSI host can be either on top of a physical port or a virtual port.
3259 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003260void lpfc_host_attrib_init(struct Scsi_Host *shost)
3261{
3262 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3263 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003264 /*
James Smart2e0fef82007-06-17 19:56:36 -05003265 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04003266 */
3267
James Smart2e0fef82007-06-17 19:56:36 -05003268 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
3269 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04003270 fc_host_supported_classes(shost) = FC_COS_CLASS3;
3271
3272 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003273 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003274 fc_host_supported_fc4s(shost)[2] = 1;
3275 fc_host_supported_fc4s(shost)[7] = 1;
3276
James Smart92d7f7b2007-06-17 19:56:38 -05003277 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
3278 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04003279
3280 fc_host_supported_speeds(shost) = 0;
James Smart88a2cfb2011-07-22 18:36:33 -04003281 if (phba->lmt & LMT_16Gb)
3282 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
James Smart47a86172007-04-25 09:53:22 -04003283 if (phba->lmt & LMT_10Gb)
3284 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04003285 if (phba->lmt & LMT_8Gb)
3286 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04003287 if (phba->lmt & LMT_4Gb)
3288 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
3289 if (phba->lmt & LMT_2Gb)
3290 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
3291 if (phba->lmt & LMT_1Gb)
3292 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
3293
3294 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05003295 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
3296 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04003297
Mike Christie0af5d702010-09-15 16:52:31 -05003298 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
3299
James Smart47a86172007-04-25 09:53:22 -04003300 /* This value is also unchanging */
3301 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003302 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003303 fc_host_active_fc4s(shost)[2] = 1;
3304 fc_host_active_fc4s(shost)[7] = 1;
3305
James Smart92d7f7b2007-06-17 19:56:38 -05003306 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04003307 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04003308 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04003309 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04003310}
dea31012005-04-17 16:05:31 -05003311
James Smarte59058c2008-08-24 21:49:00 -04003312/**
James Smartda0436e2009-05-22 14:51:39 -04003313 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04003314 * @phba: pointer to lpfc hba data structure.
3315 *
James Smartda0436e2009-05-22 14:51:39 -04003316 * This routine is invoked to stop an SLI3 device port, it stops the device
3317 * from generating interrupts and stops the device driver's timers for the
3318 * device.
James Smarte59058c2008-08-24 21:49:00 -04003319 **/
James Smartdb2378e2008-02-08 18:49:51 -05003320static void
James Smartda0436e2009-05-22 14:51:39 -04003321lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05003322{
James Smartda0436e2009-05-22 14:51:39 -04003323 /* Clear all interrupt enable conditions */
3324 writel(0, phba->HCregaddr);
3325 readl(phba->HCregaddr); /* flush */
3326 /* Clear all pending interrupts */
3327 writel(0xffffffff, phba->HAregaddr);
3328 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04003329
James Smartda0436e2009-05-22 14:51:39 -04003330 /* Reset some HBA SLI setup states */
3331 lpfc_stop_hba_timers(phba);
3332 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05003333}
3334
James Smarte59058c2008-08-24 21:49:00 -04003335/**
James Smartda0436e2009-05-22 14:51:39 -04003336 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05003337 * @phba: pointer to lpfc hba data structure.
3338 *
James Smartda0436e2009-05-22 14:51:39 -04003339 * This routine is invoked to stop an SLI4 device port, it stops the device
3340 * from generating interrupts and stops the device driver's timers for the
3341 * device.
3342 **/
3343static void
3344lpfc_stop_port_s4(struct lpfc_hba *phba)
3345{
3346 /* Reset some HBA SLI4 setup states */
3347 lpfc_stop_hba_timers(phba);
3348 phba->pport->work_port_events = 0;
3349 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04003350}
3351
3352/**
3353 * lpfc_stop_port - Wrapper function for stopping hba port
3354 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05003355 *
James Smartda0436e2009-05-22 14:51:39 -04003356 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
3357 * the API jump table function pointer from the lpfc_hba struct.
3358 **/
3359void
3360lpfc_stop_port(struct lpfc_hba *phba)
3361{
3362 phba->lpfc_stop_port(phba);
3363}
3364
3365/**
James Smartecfd03c2010-02-12 14:41:27 -05003366 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
3367 * @phba: Pointer to hba for which this call is being executed.
3368 *
3369 * This routine starts the timer waiting for the FCF rediscovery to complete.
3370 **/
3371void
3372lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
3373{
3374 unsigned long fcf_redisc_wait_tmo =
3375 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
3376 /* Start fcf rediscovery wait period timer */
3377 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
3378 spin_lock_irq(&phba->hbalock);
3379 /* Allow action to new fcf asynchronous event */
3380 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
3381 /* Mark the FCF rediscovery pending state */
3382 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
3383 spin_unlock_irq(&phba->hbalock);
3384}
3385
3386/**
3387 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
3388 * @ptr: Map to lpfc_hba data structure pointer.
3389 *
3390 * This routine is invoked when waiting for FCF table rediscover has been
3391 * timed out. If new FCF record(s) has (have) been discovered during the
3392 * wait period, a new FCF event shall be added to the FCOE async event
3393 * list, and then worker thread shall be waked up for processing from the
3394 * worker thread context.
3395 **/
3396void
3397lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
3398{
3399 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
3400
3401 /* Don't send FCF rediscovery event if timer cancelled */
3402 spin_lock_irq(&phba->hbalock);
3403 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
3404 spin_unlock_irq(&phba->hbalock);
3405 return;
3406 }
3407 /* Clear FCF rediscovery timer pending flag */
3408 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
3409 /* FCF rediscovery event to worker thread */
3410 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
3411 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003412 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04003413 "2776 FCF rediscover quiescent timer expired\n");
James Smartecfd03c2010-02-12 14:41:27 -05003414 /* wake up worker thread */
3415 lpfc_worker_wake_up(phba);
3416}
3417
3418/**
James Smartda0436e2009-05-22 14:51:39 -04003419 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
3420 * @phba: pointer to lpfc hba data structure.
3421 * @acqe_link: pointer to the async link completion queue entry.
3422 *
3423 * This routine is to parse the SLI4 link-attention link fault code and
3424 * translate it into the base driver's read link attention mailbox command
3425 * status.
3426 *
3427 * Return: Link-attention status in terms of base driver's coding.
3428 **/
3429static uint16_t
3430lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
3431 struct lpfc_acqe_link *acqe_link)
3432{
3433 uint16_t latt_fault;
3434
3435 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
3436 case LPFC_ASYNC_LINK_FAULT_NONE:
3437 case LPFC_ASYNC_LINK_FAULT_LOCAL:
3438 case LPFC_ASYNC_LINK_FAULT_REMOTE:
3439 latt_fault = 0;
3440 break;
3441 default:
3442 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3443 "0398 Invalid link fault code: x%x\n",
3444 bf_get(lpfc_acqe_link_fault, acqe_link));
3445 latt_fault = MBXERR_ERROR;
3446 break;
3447 }
3448 return latt_fault;
3449}
3450
3451/**
3452 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
3453 * @phba: pointer to lpfc hba data structure.
3454 * @acqe_link: pointer to the async link completion queue entry.
3455 *
3456 * This routine is to parse the SLI4 link attention type and translate it
3457 * into the base driver's link attention type coding.
3458 *
3459 * Return: Link attention type in terms of base driver's coding.
3460 **/
3461static uint8_t
3462lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
3463 struct lpfc_acqe_link *acqe_link)
3464{
3465 uint8_t att_type;
3466
3467 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
3468 case LPFC_ASYNC_LINK_STATUS_DOWN:
3469 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
James Smart76a95d72010-11-20 23:11:48 -05003470 att_type = LPFC_ATT_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04003471 break;
3472 case LPFC_ASYNC_LINK_STATUS_UP:
3473 /* Ignore physical link up events - wait for logical link up */
James Smart76a95d72010-11-20 23:11:48 -05003474 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003475 break;
3476 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
James Smart76a95d72010-11-20 23:11:48 -05003477 att_type = LPFC_ATT_LINK_UP;
James Smartda0436e2009-05-22 14:51:39 -04003478 break;
3479 default:
3480 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3481 "0399 Invalid link attention type: x%x\n",
3482 bf_get(lpfc_acqe_link_status, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003483 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003484 break;
3485 }
3486 return att_type;
3487}
3488
3489/**
3490 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
3491 * @phba: pointer to lpfc hba data structure.
3492 * @acqe_link: pointer to the async link completion queue entry.
3493 *
3494 * This routine is to parse the SLI4 link-attention link speed and translate
3495 * it into the base driver's link-attention link speed coding.
3496 *
3497 * Return: Link-attention link speed in terms of base driver's coding.
3498 **/
3499static uint8_t
3500lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
3501 struct lpfc_acqe_link *acqe_link)
3502{
3503 uint8_t link_speed;
3504
3505 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
3506 case LPFC_ASYNC_LINK_SPEED_ZERO:
James Smartda0436e2009-05-22 14:51:39 -04003507 case LPFC_ASYNC_LINK_SPEED_10MBPS:
James Smartda0436e2009-05-22 14:51:39 -04003508 case LPFC_ASYNC_LINK_SPEED_100MBPS:
James Smart76a95d72010-11-20 23:11:48 -05003509 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003510 break;
3511 case LPFC_ASYNC_LINK_SPEED_1GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003512 link_speed = LPFC_LINK_SPEED_1GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003513 break;
3514 case LPFC_ASYNC_LINK_SPEED_10GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003515 link_speed = LPFC_LINK_SPEED_10GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003516 break;
3517 default:
3518 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3519 "0483 Invalid link-attention link speed: x%x\n",
3520 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003521 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003522 break;
3523 }
3524 return link_speed;
3525}
3526
3527/**
James Smart8b68cd52012-09-29 11:32:37 -04003528 * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
3529 * @phba: pointer to lpfc hba data structure.
3530 *
3531 * This routine is to get an SLI3 FC port's link speed in Mbps.
3532 *
3533 * Return: link speed in terms of Mbps.
3534 **/
3535uint32_t
3536lpfc_sli_port_speed_get(struct lpfc_hba *phba)
3537{
3538 uint32_t link_speed;
3539
3540 if (!lpfc_is_link_up(phba))
3541 return 0;
3542
3543 switch (phba->fc_linkspeed) {
3544 case LPFC_LINK_SPEED_1GHZ:
3545 link_speed = 1000;
3546 break;
3547 case LPFC_LINK_SPEED_2GHZ:
3548 link_speed = 2000;
3549 break;
3550 case LPFC_LINK_SPEED_4GHZ:
3551 link_speed = 4000;
3552 break;
3553 case LPFC_LINK_SPEED_8GHZ:
3554 link_speed = 8000;
3555 break;
3556 case LPFC_LINK_SPEED_10GHZ:
3557 link_speed = 10000;
3558 break;
3559 case LPFC_LINK_SPEED_16GHZ:
3560 link_speed = 16000;
3561 break;
3562 default:
3563 link_speed = 0;
3564 }
3565 return link_speed;
3566}
3567
3568/**
3569 * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
3570 * @phba: pointer to lpfc hba data structure.
3571 * @evt_code: asynchronous event code.
3572 * @speed_code: asynchronous event link speed code.
3573 *
3574 * This routine is to parse the giving SLI4 async event link speed code into
3575 * value of Mbps for the link speed.
3576 *
3577 * Return: link speed in terms of Mbps.
3578 **/
3579static uint32_t
3580lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
3581 uint8_t speed_code)
3582{
3583 uint32_t port_speed;
3584
3585 switch (evt_code) {
3586 case LPFC_TRAILER_CODE_LINK:
3587 switch (speed_code) {
3588 case LPFC_EVT_CODE_LINK_NO_LINK:
3589 port_speed = 0;
3590 break;
3591 case LPFC_EVT_CODE_LINK_10_MBIT:
3592 port_speed = 10;
3593 break;
3594 case LPFC_EVT_CODE_LINK_100_MBIT:
3595 port_speed = 100;
3596 break;
3597 case LPFC_EVT_CODE_LINK_1_GBIT:
3598 port_speed = 1000;
3599 break;
3600 case LPFC_EVT_CODE_LINK_10_GBIT:
3601 port_speed = 10000;
3602 break;
3603 default:
3604 port_speed = 0;
3605 }
3606 break;
3607 case LPFC_TRAILER_CODE_FC:
3608 switch (speed_code) {
3609 case LPFC_EVT_CODE_FC_NO_LINK:
3610 port_speed = 0;
3611 break;
3612 case LPFC_EVT_CODE_FC_1_GBAUD:
3613 port_speed = 1000;
3614 break;
3615 case LPFC_EVT_CODE_FC_2_GBAUD:
3616 port_speed = 2000;
3617 break;
3618 case LPFC_EVT_CODE_FC_4_GBAUD:
3619 port_speed = 4000;
3620 break;
3621 case LPFC_EVT_CODE_FC_8_GBAUD:
3622 port_speed = 8000;
3623 break;
3624 case LPFC_EVT_CODE_FC_10_GBAUD:
3625 port_speed = 10000;
3626 break;
3627 case LPFC_EVT_CODE_FC_16_GBAUD:
3628 port_speed = 16000;
3629 break;
3630 default:
3631 port_speed = 0;
3632 }
3633 break;
3634 default:
3635 port_speed = 0;
3636 }
3637 return port_speed;
3638}
3639
3640/**
James Smart70f3c072010-12-15 17:57:33 -05003641 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
James Smartda0436e2009-05-22 14:51:39 -04003642 * @phba: pointer to lpfc hba data structure.
3643 * @acqe_link: pointer to the async link completion queue entry.
3644 *
James Smart70f3c072010-12-15 17:57:33 -05003645 * This routine is to handle the SLI4 asynchronous FCoE link event.
James Smartda0436e2009-05-22 14:51:39 -04003646 **/
3647static void
3648lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3649 struct lpfc_acqe_link *acqe_link)
3650{
3651 struct lpfc_dmabuf *mp;
3652 LPFC_MBOXQ_t *pmb;
3653 MAILBOX_t *mb;
James Smart76a95d72010-11-20 23:11:48 -05003654 struct lpfc_mbx_read_top *la;
James Smartda0436e2009-05-22 14:51:39 -04003655 uint8_t att_type;
James Smart76a95d72010-11-20 23:11:48 -05003656 int rc;
James Smartda0436e2009-05-22 14:51:39 -04003657
3658 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05003659 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
James Smartda0436e2009-05-22 14:51:39 -04003660 return;
James Smart32b97932009-07-19 10:01:21 -04003661 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003662 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3663 if (!pmb) {
3664 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3665 "0395 The mboxq allocation failed\n");
3666 return;
3667 }
3668 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3669 if (!mp) {
3670 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3671 "0396 The lpfc_dmabuf allocation failed\n");
3672 goto out_free_pmb;
3673 }
3674 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3675 if (!mp->virt) {
3676 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3677 "0397 The mbuf allocation failed\n");
3678 goto out_free_dmabuf;
3679 }
3680
3681 /* Cleanup any outstanding ELS commands */
3682 lpfc_els_flush_all_cmd(phba);
3683
3684 /* Block ELS IOCBs until we have done process link event */
3685 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3686
3687 /* Update link event statistics */
3688 phba->sli.slistat.link_event++;
3689
James Smart76a95d72010-11-20 23:11:48 -05003690 /* Create lpfc_handle_latt mailbox command from link ACQE */
3691 lpfc_read_topology(phba, pmb, mp);
3692 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smartda0436e2009-05-22 14:51:39 -04003693 pmb->vport = phba->pport;
3694
James Smartda0436e2009-05-22 14:51:39 -04003695 /* Keep the link status for extra SLI4 state machine reference */
3696 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04003697 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
3698 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smartda0436e2009-05-22 14:51:39 -04003699 phba->sli4_hba.link_state.duplex =
3700 bf_get(lpfc_acqe_link_duplex, acqe_link);
3701 phba->sli4_hba.link_state.status =
3702 bf_get(lpfc_acqe_link_status, acqe_link);
James Smart70f3c072010-12-15 17:57:33 -05003703 phba->sli4_hba.link_state.type =
3704 bf_get(lpfc_acqe_link_type, acqe_link);
3705 phba->sli4_hba.link_state.number =
3706 bf_get(lpfc_acqe_link_number, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04003707 phba->sli4_hba.link_state.fault =
3708 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05003709 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04003710 bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
3711
James Smart70f3c072010-12-15 17:57:33 -05003712 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartc31098c2011-04-16 11:03:33 -04003713 "2900 Async FC/FCoE Link event - Speed:%dGBit "
3714 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
3715 "Logical speed:%dMbps Fault:%d\n",
James Smart70f3c072010-12-15 17:57:33 -05003716 phba->sli4_hba.link_state.speed,
3717 phba->sli4_hba.link_state.topology,
3718 phba->sli4_hba.link_state.status,
3719 phba->sli4_hba.link_state.type,
3720 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04003721 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05003722 phba->sli4_hba.link_state.fault);
James Smart76a95d72010-11-20 23:11:48 -05003723 /*
3724 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
3725 * topology info. Note: Optional for non FC-AL ports.
3726 */
3727 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3728 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3729 if (rc == MBX_NOT_FINISHED)
3730 goto out_free_dmabuf;
3731 return;
3732 }
3733 /*
3734 * For FCoE Mode: fill in all the topology information we need and call
3735 * the READ_TOPOLOGY completion routine to continue without actually
3736 * sending the READ_TOPOLOGY mailbox command to the port.
3737 */
3738 /* Parse and translate status field */
3739 mb = &pmb->u.mb;
3740 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
3741
3742 /* Parse and translate link attention fields */
3743 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
3744 la->eventTag = acqe_link->event_tag;
3745 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
3746 bf_set(lpfc_mbx_read_top_link_spd, la,
3747 lpfc_sli4_parse_latt_link_speed(phba, acqe_link));
3748
3749 /* Fake the the following irrelvant fields */
3750 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
3751 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
3752 bf_set(lpfc_mbx_read_top_il, la, 0);
3753 bf_set(lpfc_mbx_read_top_pb, la, 0);
3754 bf_set(lpfc_mbx_read_top_fa, la, 0);
3755 bf_set(lpfc_mbx_read_top_mm, la, 0);
James Smartda0436e2009-05-22 14:51:39 -04003756
3757 /* Invoke the lpfc_handle_latt mailbox command callback function */
James Smart76a95d72010-11-20 23:11:48 -05003758 lpfc_mbx_cmpl_read_topology(phba, pmb);
James Smartda0436e2009-05-22 14:51:39 -04003759
3760 return;
3761
3762out_free_dmabuf:
3763 kfree(mp);
3764out_free_pmb:
3765 mempool_free(pmb, phba->mbox_mem_pool);
3766}
3767
3768/**
James Smart70f3c072010-12-15 17:57:33 -05003769 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
3770 * @phba: pointer to lpfc hba data structure.
3771 * @acqe_fc: pointer to the async fc completion queue entry.
3772 *
3773 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
3774 * that the event was received and then issue a read_topology mailbox command so
3775 * that the rest of the driver will treat it the same as SLI3.
3776 **/
3777static void
3778lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
3779{
3780 struct lpfc_dmabuf *mp;
3781 LPFC_MBOXQ_t *pmb;
3782 int rc;
3783
3784 if (bf_get(lpfc_trailer_type, acqe_fc) !=
3785 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
3786 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3787 "2895 Non FC link Event detected.(%d)\n",
3788 bf_get(lpfc_trailer_type, acqe_fc));
3789 return;
3790 }
3791 /* Keep the link status for extra SLI4 state machine reference */
3792 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04003793 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
3794 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
James Smart70f3c072010-12-15 17:57:33 -05003795 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
3796 phba->sli4_hba.link_state.topology =
3797 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
3798 phba->sli4_hba.link_state.status =
3799 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
3800 phba->sli4_hba.link_state.type =
3801 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
3802 phba->sli4_hba.link_state.number =
3803 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
3804 phba->sli4_hba.link_state.fault =
3805 bf_get(lpfc_acqe_link_fault, acqe_fc);
3806 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04003807 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
James Smart70f3c072010-12-15 17:57:33 -05003808 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3809 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
3810 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
3811 "%dMbps Fault:%d\n",
3812 phba->sli4_hba.link_state.speed,
3813 phba->sli4_hba.link_state.topology,
3814 phba->sli4_hba.link_state.status,
3815 phba->sli4_hba.link_state.type,
3816 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04003817 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05003818 phba->sli4_hba.link_state.fault);
3819 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3820 if (!pmb) {
3821 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3822 "2897 The mboxq allocation failed\n");
3823 return;
3824 }
3825 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3826 if (!mp) {
3827 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3828 "2898 The lpfc_dmabuf allocation failed\n");
3829 goto out_free_pmb;
3830 }
3831 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3832 if (!mp->virt) {
3833 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3834 "2899 The mbuf allocation failed\n");
3835 goto out_free_dmabuf;
3836 }
3837
3838 /* Cleanup any outstanding ELS commands */
3839 lpfc_els_flush_all_cmd(phba);
3840
3841 /* Block ELS IOCBs until we have done process link event */
3842 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3843
3844 /* Update link event statistics */
3845 phba->sli.slistat.link_event++;
3846
3847 /* Create lpfc_handle_latt mailbox command from link ACQE */
3848 lpfc_read_topology(phba, pmb, mp);
3849 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
3850 pmb->vport = phba->pport;
3851
3852 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3853 if (rc == MBX_NOT_FINISHED)
3854 goto out_free_dmabuf;
3855 return;
3856
3857out_free_dmabuf:
3858 kfree(mp);
3859out_free_pmb:
3860 mempool_free(pmb, phba->mbox_mem_pool);
3861}
3862
3863/**
3864 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
3865 * @phba: pointer to lpfc hba data structure.
3866 * @acqe_fc: pointer to the async SLI completion queue entry.
3867 *
3868 * This routine is to handle the SLI4 asynchronous SLI events.
3869 **/
3870static void
3871lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
3872{
James Smart4b8bae02012-06-12 13:55:07 -04003873 char port_name;
James Smart8c1312e2012-10-31 14:45:09 -04003874 char message[128];
James Smart4b8bae02012-06-12 13:55:07 -04003875 uint8_t status;
3876 struct lpfc_acqe_misconfigured_event *misconfigured;
3877
3878 /* special case misconfigured event as it contains data for all ports */
3879 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3880 LPFC_SLI_INTF_IF_TYPE_2) ||
3881 (bf_get(lpfc_trailer_type, acqe_sli) !=
3882 LPFC_SLI_EVENT_TYPE_MISCONFIGURED)) {
3883 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3884 "2901 Async SLI event - Event Data1:x%08x Event Data2:"
3885 "x%08x SLI Event Type:%d\n",
3886 acqe_sli->event_data1, acqe_sli->event_data2,
3887 bf_get(lpfc_trailer_type, acqe_sli));
3888 return;
3889 }
3890
3891 port_name = phba->Port[0];
3892 if (port_name == 0x00)
3893 port_name = '?'; /* get port name is empty */
3894
3895 misconfigured = (struct lpfc_acqe_misconfigured_event *)
3896 &acqe_sli->event_data1;
3897
3898 /* fetch the status for this port */
3899 switch (phba->sli4_hba.lnk_info.lnk_no) {
3900 case LPFC_LINK_NUMBER_0:
3901 status = bf_get(lpfc_sli_misconfigured_port0,
3902 &misconfigured->theEvent);
3903 break;
3904 case LPFC_LINK_NUMBER_1:
3905 status = bf_get(lpfc_sli_misconfigured_port1,
3906 &misconfigured->theEvent);
3907 break;
3908 case LPFC_LINK_NUMBER_2:
3909 status = bf_get(lpfc_sli_misconfigured_port2,
3910 &misconfigured->theEvent);
3911 break;
3912 case LPFC_LINK_NUMBER_3:
3913 status = bf_get(lpfc_sli_misconfigured_port3,
3914 &misconfigured->theEvent);
3915 break;
3916 default:
3917 status = ~LPFC_SLI_EVENT_STATUS_VALID;
3918 break;
3919 }
3920
3921 switch (status) {
3922 case LPFC_SLI_EVENT_STATUS_VALID:
3923 return; /* no message if the sfp is okay */
3924 case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
James Smart292098b2012-09-29 11:31:41 -04003925 sprintf(message, "Optics faulted/incorrectly installed/not " \
3926 "installed - Reseat optics, if issue not "
3927 "resolved, replace.");
James Smart4b8bae02012-06-12 13:55:07 -04003928 break;
3929 case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
3930 sprintf(message,
James Smart292098b2012-09-29 11:31:41 -04003931 "Optics of two types installed - Remove one optic or " \
3932 "install matching pair of optics.");
James Smart4b8bae02012-06-12 13:55:07 -04003933 break;
3934 case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
James Smart292098b2012-09-29 11:31:41 -04003935 sprintf(message, "Incompatible optics - Replace with " \
3936 "compatible optics for card to function.");
James Smart4b8bae02012-06-12 13:55:07 -04003937 break;
3938 default:
3939 /* firmware is reporting a status we don't know about */
3940 sprintf(message, "Unknown event status x%02x", status);
3941 break;
3942 }
3943
3944 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3945 "3176 Misconfigured Physical Port - "
3946 "Port Name %c %s\n", port_name, message);
James Smart70f3c072010-12-15 17:57:33 -05003947}
3948
3949/**
James Smartfc2b9892010-02-26 14:15:29 -05003950 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
3951 * @vport: pointer to vport data structure.
3952 *
3953 * This routine is to perform Clear Virtual Link (CVL) on a vport in
3954 * response to a CVL event.
3955 *
3956 * Return the pointer to the ndlp with the vport if successful, otherwise
3957 * return NULL.
3958 **/
3959static struct lpfc_nodelist *
3960lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
3961{
3962 struct lpfc_nodelist *ndlp;
3963 struct Scsi_Host *shost;
3964 struct lpfc_hba *phba;
3965
3966 if (!vport)
3967 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05003968 phba = vport->phba;
3969 if (!phba)
3970 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04003971 ndlp = lpfc_findnode_did(vport, Fabric_DID);
3972 if (!ndlp) {
3973 /* Cannot find existing Fabric ndlp, so allocate a new one */
3974 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
3975 if (!ndlp)
3976 return 0;
3977 lpfc_nlp_init(vport, ndlp, Fabric_DID);
3978 /* Set the node type */
3979 ndlp->nlp_type |= NLP_FABRIC;
3980 /* Put ndlp onto node list */
3981 lpfc_enqueue_node(vport, ndlp);
3982 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
3983 /* re-setup ndlp without removing from node list */
3984 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
3985 if (!ndlp)
3986 return 0;
3987 }
James Smart63e801c2010-11-20 23:14:19 -05003988 if ((phba->pport->port_state < LPFC_FLOGI) &&
3989 (phba->pport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05003990 return NULL;
3991 /* If virtual link is not yet instantiated ignore CVL */
James Smart63e801c2010-11-20 23:14:19 -05003992 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
3993 && (vport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05003994 return NULL;
3995 shost = lpfc_shost_from_vport(vport);
3996 if (!shost)
3997 return NULL;
3998 lpfc_linkdown_port(vport);
3999 lpfc_cleanup_pending_mbox(vport);
4000 spin_lock_irq(shost->host_lock);
4001 vport->fc_flag |= FC_VPORT_CVL_RCVD;
4002 spin_unlock_irq(shost->host_lock);
4003
4004 return ndlp;
4005}
4006
4007/**
4008 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
4009 * @vport: pointer to lpfc hba data structure.
4010 *
4011 * This routine is to perform Clear Virtual Link (CVL) on all vports in
4012 * response to a FCF dead event.
4013 **/
4014static void
4015lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
4016{
4017 struct lpfc_vport **vports;
4018 int i;
4019
4020 vports = lpfc_create_vport_work_array(phba);
4021 if (vports)
4022 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
4023 lpfc_sli4_perform_vport_cvl(vports[i]);
4024 lpfc_destroy_vport_work_array(phba, vports);
4025}
4026
4027/**
James Smart25aee402013-03-01 16:36:34 -05004028 * lpfc_sli4_perform_inuse_fcf_recovery - Perform inuse fcf recovery
4029 * @vport: pointer to lpfc hba data structure.
4030 *
4031 * This routine is to perform FCF recovery when the in-use FCF either dead or
4032 * got modified.
4033 **/
4034static void
4035lpfc_sli4_perform_inuse_fcf_recovery(struct lpfc_hba *phba,
4036 struct lpfc_acqe_fip *acqe_fip)
4037{
4038 int rc;
4039
4040 spin_lock_irq(&phba->hbalock);
4041 /* Mark the fast failover process in progress */
4042 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
4043 spin_unlock_irq(&phba->hbalock);
4044
4045 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
4046 "2771 Start FCF fast failover process due to in-use "
4047 "FCF DEAD/MODIFIED event: evt_tag:x%x, index:x%x\n",
4048 acqe_fip->event_tag, acqe_fip->index);
4049 rc = lpfc_sli4_redisc_fcf_table(phba);
4050 if (rc) {
4051 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4052 "2772 Issue FCF rediscover mabilbox command "
4053 "failed, fail through to FCF dead event\n");
4054 spin_lock_irq(&phba->hbalock);
4055 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
4056 spin_unlock_irq(&phba->hbalock);
4057 /*
4058 * Last resort will fail over by treating this as a link
4059 * down to FCF registration.
4060 */
4061 lpfc_sli4_fcf_dead_failthrough(phba);
4062 } else {
4063 /* Reset FCF roundrobin bmask for new discovery */
4064 lpfc_sli4_clear_fcf_rr_bmask(phba);
4065 /*
4066 * Handling fast FCF failover to a DEAD FCF event is
4067 * considered equalivant to receiving CVL to all vports.
4068 */
4069 lpfc_sli4_perform_all_vport_cvl(phba);
4070 }
4071}
4072
4073/**
James Smart76a95d72010-11-20 23:11:48 -05004074 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
James Smartda0436e2009-05-22 14:51:39 -04004075 * @phba: pointer to lpfc hba data structure.
4076 * @acqe_link: pointer to the async fcoe completion queue entry.
4077 *
4078 * This routine is to handle the SLI4 asynchronous fcoe event.
4079 **/
4080static void
James Smart76a95d72010-11-20 23:11:48 -05004081lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
James Smart70f3c072010-12-15 17:57:33 -05004082 struct lpfc_acqe_fip *acqe_fip)
James Smartda0436e2009-05-22 14:51:39 -04004083{
James Smart70f3c072010-12-15 17:57:33 -05004084 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004085 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04004086 struct lpfc_vport *vport;
4087 struct lpfc_nodelist *ndlp;
4088 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05004089 int active_vlink_present;
4090 struct lpfc_vport **vports;
4091 int i;
James Smartda0436e2009-05-22 14:51:39 -04004092
James Smart70f3c072010-12-15 17:57:33 -05004093 phba->fc_eventTag = acqe_fip->event_tag;
4094 phba->fcoe_eventtag = acqe_fip->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004095 switch (event_type) {
James Smart70f3c072010-12-15 17:57:33 -05004096 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
4097 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
4098 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
James Smart999d8132010-03-15 11:24:56 -04004099 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4100 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004101 "2546 New FCF event, evt_tag:x%x, "
4102 "index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004103 acqe_fip->event_tag,
4104 acqe_fip->index);
James Smart999d8132010-03-15 11:24:56 -04004105 else
4106 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
4107 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004108 "2788 FCF param modified event, "
4109 "evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004110 acqe_fip->event_tag,
4111 acqe_fip->index);
James Smart38b92ef2010-08-04 16:11:39 -04004112 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004113 /*
4114 * During period of FCF discovery, read the FCF
4115 * table record indexed by the event to update
James Smarta93ff372010-10-22 11:06:08 -04004116 * FCF roundrobin failover eligible FCF bmask.
James Smart0c9ab6f2010-02-26 14:15:57 -05004117 */
4118 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4119 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004120 "2779 Read FCF (x%x) for updating "
4121 "roundrobin FCF failover bmask\n",
James Smart70f3c072010-12-15 17:57:33 -05004122 acqe_fip->index);
4123 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004124 }
James Smart38b92ef2010-08-04 16:11:39 -04004125
4126 /* If the FCF discovery is in progress, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04004127 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04004128 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart38b92ef2010-08-04 16:11:39 -04004129 spin_unlock_irq(&phba->hbalock);
4130 break;
4131 }
4132 /* If fast FCF failover rescan event is pending, do nothing */
4133 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
4134 spin_unlock_irq(&phba->hbalock);
4135 break;
4136 }
4137
James Smart25aee402013-03-01 16:36:34 -05004138 /* If FCF has been in discovered state, perform rediscovery
4139 * only if the FCF with the same index of the in-use FCF got
4140 * modified during normal operation. Otherwise, do nothing.
4141 */
4142 if (phba->pport->port_state > LPFC_FLOGI) {
James Smart3804dc82010-07-14 15:31:37 -04004143 spin_unlock_irq(&phba->hbalock);
James Smart25aee402013-03-01 16:36:34 -05004144 if (phba->fcf.current_rec.fcf_indx ==
4145 acqe_fip->index) {
4146 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
4147 "3300 In-use FCF (%d) "
4148 "modified, perform FCF "
4149 "rediscovery\n",
4150 acqe_fip->index);
4151 lpfc_sli4_perform_inuse_fcf_recovery(phba,
4152 acqe_fip);
4153 }
James Smart3804dc82010-07-14 15:31:37 -04004154 break;
4155 }
4156 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04004157
James Smart0c9ab6f2010-02-26 14:15:57 -05004158 /* Otherwise, scan the entire FCF table and re-discover SAN */
4159 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004160 "2770 Start FCF table scan per async FCF "
4161 "event, evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004162 acqe_fip->event_tag, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004163 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
4164 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04004165 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05004166 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4167 "2547 Issue FCF scan read FCF mailbox "
James Smarta93ff372010-10-22 11:06:08 -04004168 "command failed (x%x)\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004169 break;
4170
James Smart70f3c072010-12-15 17:57:33 -05004171 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
James Smartda0436e2009-05-22 14:51:39 -04004172 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04004173 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004174 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
4175 acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04004176 break;
4177
James Smart70f3c072010-12-15 17:57:33 -05004178 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
James Smart80c17842012-03-01 22:35:45 -05004179 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05004180 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004181 "2549 FCF (x%x) disconnected from network, "
James Smart70f3c072010-12-15 17:57:33 -05004182 "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart38b92ef2010-08-04 16:11:39 -04004183 /*
4184 * If we are in the middle of FCF failover process, clear
4185 * the corresponding FCF bit in the roundrobin bitmap.
James Smartda0436e2009-05-22 14:51:39 -04004186 */
James Smartfc2b9892010-02-26 14:15:29 -05004187 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004188 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05004189 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004190 /* Update FLOGI FCF failover eligible FCF bmask */
James Smart70f3c072010-12-15 17:57:33 -05004191 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004192 break;
4193 }
James Smart38b92ef2010-08-04 16:11:39 -04004194 spin_unlock_irq(&phba->hbalock);
4195
4196 /* If the event is not for currently used fcf do nothing */
James Smart70f3c072010-12-15 17:57:33 -05004197 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
James Smart38b92ef2010-08-04 16:11:39 -04004198 break;
4199
4200 /*
4201 * Otherwise, request the port to rediscover the entire FCF
4202 * table for a fast recovery from case that the current FCF
4203 * is no longer valid as we are not in the middle of FCF
4204 * failover process already.
4205 */
James Smart25aee402013-03-01 16:36:34 -05004206 lpfc_sli4_perform_inuse_fcf_recovery(phba, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004207 break;
James Smart70f3c072010-12-15 17:57:33 -05004208 case LPFC_FIP_EVENT_TYPE_CVL:
James Smart80c17842012-03-01 22:35:45 -05004209 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05004210 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6669f9b2009-10-02 15:16:45 -04004211 "2718 Clear Virtual Link Received for VPI 0x%x"
James Smart70f3c072010-12-15 17:57:33 -05004212 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart6d368e52011-05-24 11:44:12 -04004213
James Smart6669f9b2009-10-02 15:16:45 -04004214 vport = lpfc_find_vport_by_vpid(phba,
James Smart5248a742011-07-22 18:37:06 -04004215 acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004216 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004217 if (!ndlp)
4218 break;
James Smart695a8142010-01-26 23:08:03 -05004219 active_vlink_present = 0;
4220
4221 vports = lpfc_create_vport_work_array(phba);
4222 if (vports) {
4223 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
4224 i++) {
4225 if ((!(vports[i]->fc_flag &
4226 FC_VPORT_CVL_RCVD)) &&
4227 (vports[i]->port_state > LPFC_FDISC)) {
4228 active_vlink_present = 1;
4229 break;
4230 }
4231 }
4232 lpfc_destroy_vport_work_array(phba, vports);
4233 }
4234
4235 if (active_vlink_present) {
4236 /*
4237 * If there are other active VLinks present,
4238 * re-instantiate the Vlink using FDISC.
4239 */
James Smart256ec0d2013-04-17 20:14:58 -04004240 mod_timer(&ndlp->nlp_delayfunc,
4241 jiffies + msecs_to_jiffies(1000));
James Smartfc2b9892010-02-26 14:15:29 -05004242 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004243 spin_lock_irq(shost->host_lock);
4244 ndlp->nlp_flag |= NLP_DELAY_TMO;
4245 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05004246 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
4247 vport->port_state = LPFC_FDISC;
4248 } else {
James Smartecfd03c2010-02-12 14:41:27 -05004249 /*
4250 * Otherwise, we request port to rediscover
4251 * the entire FCF table for a fast recovery
4252 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05004253 * is no longer valid if we are not already
4254 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05004255 */
James Smartfc2b9892010-02-26 14:15:29 -05004256 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004257 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05004258 spin_unlock_irq(&phba->hbalock);
4259 break;
4260 }
4261 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05004262 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004263 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004264 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4265 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004266 "2773 Start FCF failover per CVL, "
James Smart70f3c072010-12-15 17:57:33 -05004267 "evt_tag:x%x\n", acqe_fip->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05004268 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05004269 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004270 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4271 LOG_DISCOVERY,
4272 "2774 Issue FCF rediscover "
4273 "mabilbox command failed, "
4274 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05004275 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004276 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004277 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05004278 /*
4279 * Last resort will be re-try on the
4280 * the current registered FCF entry.
4281 */
4282 lpfc_retry_pport_discovery(phba);
James Smart38b92ef2010-08-04 16:11:39 -04004283 } else
4284 /*
4285 * Reset FCF roundrobin bmask for new
4286 * discovery.
4287 */
James Smart7d791df2011-07-22 18:37:52 -04004288 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart6669f9b2009-10-02 15:16:45 -04004289 }
4290 break;
James Smartda0436e2009-05-22 14:51:39 -04004291 default:
4292 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4293 "0288 Unknown FCoE event type 0x%x event tag "
James Smart70f3c072010-12-15 17:57:33 -05004294 "0x%x\n", event_type, acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04004295 break;
4296 }
4297}
4298
4299/**
4300 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
4301 * @phba: pointer to lpfc hba data structure.
4302 * @acqe_link: pointer to the async dcbx completion queue entry.
4303 *
4304 * This routine is to handle the SLI4 asynchronous dcbx event.
4305 **/
4306static void
4307lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
4308 struct lpfc_acqe_dcbx *acqe_dcbx)
4309{
James Smart4d9ab992009-10-02 15:16:39 -04004310 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004311 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4312 "0290 The SLI4 DCBX asynchronous event is not "
4313 "handled yet\n");
4314}
4315
4316/**
James Smartb19a0612010-04-06 14:48:51 -04004317 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
4318 * @phba: pointer to lpfc hba data structure.
4319 * @acqe_link: pointer to the async grp5 completion queue entry.
4320 *
4321 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
4322 * is an asynchronous notified of a logical link speed change. The Port
4323 * reports the logical link speed in units of 10Mbps.
4324 **/
4325static void
4326lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
4327 struct lpfc_acqe_grp5 *acqe_grp5)
4328{
4329 uint16_t prev_ll_spd;
4330
4331 phba->fc_eventTag = acqe_grp5->event_tag;
4332 phba->fcoe_eventtag = acqe_grp5->event_tag;
4333 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
4334 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04004335 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
James Smartb19a0612010-04-06 14:48:51 -04004336 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4337 "2789 GRP5 Async Event: Updating logical link speed "
James Smart8b68cd52012-09-29 11:32:37 -04004338 "from %dMbps to %dMbps\n", prev_ll_spd,
4339 phba->sli4_hba.link_state.logical_speed);
James Smartb19a0612010-04-06 14:48:51 -04004340}
4341
4342/**
James Smartda0436e2009-05-22 14:51:39 -04004343 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
4344 * @phba: pointer to lpfc hba data structure.
4345 *
4346 * This routine is invoked by the worker thread to process all the pending
4347 * SLI4 asynchronous events.
4348 **/
4349void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
4350{
4351 struct lpfc_cq_event *cq_event;
4352
4353 /* First, declare the async event has been handled */
4354 spin_lock_irq(&phba->hbalock);
4355 phba->hba_flag &= ~ASYNC_EVENT;
4356 spin_unlock_irq(&phba->hbalock);
4357 /* Now, handle all the async events */
4358 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
4359 /* Get the first event from the head of the event queue */
4360 spin_lock_irq(&phba->hbalock);
4361 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
4362 cq_event, struct lpfc_cq_event, list);
4363 spin_unlock_irq(&phba->hbalock);
4364 /* Process the asynchronous event */
4365 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
4366 case LPFC_TRAILER_CODE_LINK:
4367 lpfc_sli4_async_link_evt(phba,
4368 &cq_event->cqe.acqe_link);
4369 break;
4370 case LPFC_TRAILER_CODE_FCOE:
James Smart70f3c072010-12-15 17:57:33 -05004371 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004372 break;
4373 case LPFC_TRAILER_CODE_DCBX:
4374 lpfc_sli4_async_dcbx_evt(phba,
4375 &cq_event->cqe.acqe_dcbx);
4376 break;
James Smartb19a0612010-04-06 14:48:51 -04004377 case LPFC_TRAILER_CODE_GRP5:
4378 lpfc_sli4_async_grp5_evt(phba,
4379 &cq_event->cqe.acqe_grp5);
4380 break;
James Smart70f3c072010-12-15 17:57:33 -05004381 case LPFC_TRAILER_CODE_FC:
4382 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
4383 break;
4384 case LPFC_TRAILER_CODE_SLI:
4385 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
4386 break;
James Smartda0436e2009-05-22 14:51:39 -04004387 default:
4388 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4389 "1804 Invalid asynchrous event code: "
4390 "x%x\n", bf_get(lpfc_trailer_code,
4391 &cq_event->cqe.mcqe_cmpl));
4392 break;
4393 }
4394 /* Free the completion event processed to the free pool */
4395 lpfc_sli4_cq_event_release(phba, cq_event);
4396 }
4397}
4398
4399/**
James Smartecfd03c2010-02-12 14:41:27 -05004400 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
4401 * @phba: pointer to lpfc hba data structure.
4402 *
4403 * This routine is invoked by the worker thread to process FCF table
4404 * rediscovery pending completion event.
4405 **/
4406void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
4407{
4408 int rc;
4409
4410 spin_lock_irq(&phba->hbalock);
4411 /* Clear FCF rediscovery timeout event */
4412 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
4413 /* Clear driver fast failover FCF record flag */
4414 phba->fcf.failover_rec.flag = 0;
4415 /* Set state for FCF fast failover */
4416 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
4417 spin_unlock_irq(&phba->hbalock);
4418
4419 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05004420 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004421 "2777 Start post-quiescent FCF table scan\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05004422 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05004423 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05004424 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4425 "2747 Issue FCF scan read FCF mailbox "
4426 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05004427}
4428
4429/**
James Smartda0436e2009-05-22 14:51:39 -04004430 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
4431 * @phba: pointer to lpfc hba data structure.
4432 * @dev_grp: The HBA PCI-Device group number.
4433 *
4434 * This routine is invoked to set up the per HBA PCI-Device group function
4435 * API jump table entries.
4436 *
4437 * Return: 0 if success, otherwise -ENODEV
4438 **/
4439int
4440lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05004441{
4442 int rc;
4443
James Smartda0436e2009-05-22 14:51:39 -04004444 /* Set up lpfc PCI-device group */
4445 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05004446
James Smartda0436e2009-05-22 14:51:39 -04004447 /* The LPFC_PCI_DEV_OC uses SLI4 */
4448 if (dev_grp == LPFC_PCI_DEV_OC)
4449 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05004450
James Smartda0436e2009-05-22 14:51:39 -04004451 /* Set up device INIT API function jump table */
4452 rc = lpfc_init_api_table_setup(phba, dev_grp);
4453 if (rc)
4454 return -ENODEV;
4455 /* Set up SCSI API function jump table */
4456 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
4457 if (rc)
4458 return -ENODEV;
4459 /* Set up SLI API function jump table */
4460 rc = lpfc_sli_api_table_setup(phba, dev_grp);
4461 if (rc)
4462 return -ENODEV;
4463 /* Set up MBOX API function jump table */
4464 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
4465 if (rc)
4466 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004467
James Smartda0436e2009-05-22 14:51:39 -04004468 return 0;
James Smart5b75da22008-12-04 22:39:35 -05004469}
4470
4471/**
James Smart3621a712009-04-06 18:47:14 -04004472 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05004473 * @phba: pointer to lpfc hba data structure.
4474 * @intr_mode: active interrupt mode adopted.
4475 *
4476 * This routine it invoked to log the currently used active interrupt mode
4477 * to the device.
James Smart3772a992009-05-22 14:50:54 -04004478 **/
4479static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05004480{
4481 switch (intr_mode) {
4482 case 0:
4483 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4484 "0470 Enable INTx interrupt mode.\n");
4485 break;
4486 case 1:
4487 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4488 "0481 Enabled MSI interrupt mode.\n");
4489 break;
4490 case 2:
4491 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4492 "0480 Enabled MSI-X interrupt mode.\n");
4493 break;
4494 default:
4495 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4496 "0482 Illegal interrupt mode.\n");
4497 break;
4498 }
4499 return;
4500}
4501
James Smart5b75da22008-12-04 22:39:35 -05004502/**
James Smart3772a992009-05-22 14:50:54 -04004503 * lpfc_enable_pci_dev - Enable 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 enable the PCI device that is common to all
4507 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004508 *
4509 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004510 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004511 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05004512 **/
James Smart3772a992009-05-22 14:50:54 -04004513static int
4514lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004515{
James Smart3772a992009-05-22 14:50:54 -04004516 struct pci_dev *pdev;
James Smart079b5c92011-08-21 21:48:49 -04004517 int bars = 0;
James Smart5b75da22008-12-04 22:39:35 -05004518
James Smart3772a992009-05-22 14:50:54 -04004519 /* Obtain PCI device reference */
4520 if (!phba->pcidev)
4521 goto out_error;
4522 else
4523 pdev = phba->pcidev;
4524 /* Select PCI BARs */
4525 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4526 /* Enable PCI device */
4527 if (pci_enable_device_mem(pdev))
4528 goto out_error;
4529 /* Request PCI resource for the device */
4530 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
4531 goto out_disable_device;
4532 /* Set up device as PCI master and save state for EEH */
4533 pci_set_master(pdev);
4534 pci_try_set_mwi(pdev);
4535 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05004536
James Smart05580562011-05-24 11:40:48 -04004537 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
4538 if (pci_find_capability(pdev, PCI_CAP_ID_EXP))
4539 pdev->needs_freset = 1;
4540
James Smart3772a992009-05-22 14:50:54 -04004541 return 0;
James Smart5b75da22008-12-04 22:39:35 -05004542
James Smart3772a992009-05-22 14:50:54 -04004543out_disable_device:
4544 pci_disable_device(pdev);
4545out_error:
James Smart079b5c92011-08-21 21:48:49 -04004546 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4547 "1401 Failed to enable pci device, bars:x%x\n", bars);
James Smart3772a992009-05-22 14:50:54 -04004548 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004549}
4550
4551/**
James Smart3772a992009-05-22 14:50:54 -04004552 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05004553 * @phba: pointer to lpfc hba data structure.
4554 *
James Smart3772a992009-05-22 14:50:54 -04004555 * This routine is invoked to disable the PCI device that is common to all
4556 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004557 **/
4558static void
James Smart3772a992009-05-22 14:50:54 -04004559lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004560{
James Smart3772a992009-05-22 14:50:54 -04004561 struct pci_dev *pdev;
4562 int bars;
James Smart5b75da22008-12-04 22:39:35 -05004563
James Smart3772a992009-05-22 14:50:54 -04004564 /* Obtain PCI device reference */
4565 if (!phba->pcidev)
4566 return;
4567 else
4568 pdev = phba->pcidev;
4569 /* Select PCI BARs */
4570 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4571 /* Release PCI resource and disable PCI device */
4572 pci_release_selected_regions(pdev, bars);
4573 pci_disable_device(pdev);
4574 /* Null out PCI private reference to driver */
4575 pci_set_drvdata(pdev, NULL);
James Smart5b75da22008-12-04 22:39:35 -05004576
4577 return;
4578}
4579
4580/**
James Smart3772a992009-05-22 14:50:54 -04004581 * lpfc_reset_hba - Reset a hba
4582 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04004583 *
James Smart3772a992009-05-22 14:50:54 -04004584 * This routine is invoked to reset a hba device. It brings the HBA
4585 * offline, performs a board restart, and then brings the board back
4586 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
4587 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04004588 **/
James Smart3772a992009-05-22 14:50:54 -04004589void
4590lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004591{
James Smart3772a992009-05-22 14:50:54 -04004592 /* If resets are disabled then set error state and return. */
4593 if (!phba->cfg_enable_hba_reset) {
4594 phba->link_state = LPFC_HBA_ERROR;
4595 return;
4596 }
James Smart618a5232012-06-12 13:54:36 -04004597 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3772a992009-05-22 14:50:54 -04004598 lpfc_offline(phba);
4599 lpfc_sli_brdrestart(phba);
4600 lpfc_online(phba);
4601 lpfc_unblock_mgmt_io(phba);
4602}
dea31012005-04-17 16:05:31 -05004603
James Smart3772a992009-05-22 14:50:54 -04004604/**
James Smart0a96e972011-07-22 18:37:28 -04004605 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
4606 * @phba: pointer to lpfc hba data structure.
4607 *
4608 * This function enables the PCI SR-IOV virtual functions to a physical
4609 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4610 * enable the number of virtual functions to the physical function. As
4611 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4612 * API call does not considered as an error condition for most of the device.
4613 **/
4614uint16_t
4615lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
4616{
4617 struct pci_dev *pdev = phba->pcidev;
4618 uint16_t nr_virtfn;
4619 int pos;
4620
James Smart0a96e972011-07-22 18:37:28 -04004621 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
4622 if (pos == 0)
4623 return 0;
4624
4625 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
4626 return nr_virtfn;
4627}
4628
4629/**
James Smart912e3ac2011-05-24 11:42:11 -04004630 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
4631 * @phba: pointer to lpfc hba data structure.
4632 * @nr_vfn: number of virtual functions to be enabled.
4633 *
4634 * This function enables the PCI SR-IOV virtual functions to a physical
4635 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4636 * enable the number of virtual functions to the physical function. As
4637 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4638 * API call does not considered as an error condition for most of the device.
4639 **/
4640int
4641lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
4642{
4643 struct pci_dev *pdev = phba->pcidev;
James Smart0a96e972011-07-22 18:37:28 -04004644 uint16_t max_nr_vfn;
James Smart912e3ac2011-05-24 11:42:11 -04004645 int rc;
4646
James Smart0a96e972011-07-22 18:37:28 -04004647 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
4648 if (nr_vfn > max_nr_vfn) {
4649 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4650 "3057 Requested vfs (%d) greater than "
4651 "supported vfs (%d)", nr_vfn, max_nr_vfn);
4652 return -EINVAL;
4653 }
4654
James Smart912e3ac2011-05-24 11:42:11 -04004655 rc = pci_enable_sriov(pdev, nr_vfn);
4656 if (rc) {
4657 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4658 "2806 Failed to enable sriov on this device "
4659 "with vfn number nr_vf:%d, rc:%d\n",
4660 nr_vfn, rc);
4661 } else
4662 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4663 "2807 Successful enable sriov on this device "
4664 "with vfn number nr_vf:%d\n", nr_vfn);
4665 return rc;
4666}
4667
4668/**
James Smart3772a992009-05-22 14:50:54 -04004669 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
4670 * @phba: pointer to lpfc hba data structure.
4671 *
4672 * This routine is invoked to set up the driver internal resources specific to
4673 * support the SLI-3 HBA device it attached to.
4674 *
4675 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004676 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004677 * other values - error
4678 **/
4679static int
4680lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
4681{
4682 struct lpfc_sli *psli;
James Smart912e3ac2011-05-24 11:42:11 -04004683 int rc;
dea31012005-04-17 16:05:31 -05004684
James Smart3772a992009-05-22 14:50:54 -04004685 /*
4686 * Initialize timers used by driver
4687 */
dea31012005-04-17 16:05:31 -05004688
James Smart3772a992009-05-22 14:50:54 -04004689 /* Heartbeat timer */
4690 init_timer(&phba->hb_tmofunc);
4691 phba->hb_tmofunc.function = lpfc_hb_timeout;
4692 phba->hb_tmofunc.data = (unsigned long)phba;
4693
4694 psli = &phba->sli;
4695 /* MBOX heartbeat timer */
4696 init_timer(&psli->mbox_tmo);
4697 psli->mbox_tmo.function = lpfc_mbox_timeout;
4698 psli->mbox_tmo.data = (unsigned long) phba;
4699 /* FCP polling mode timer */
4700 init_timer(&phba->fcp_poll_timer);
4701 phba->fcp_poll_timer.function = lpfc_poll_timeout;
4702 phba->fcp_poll_timer.data = (unsigned long) phba;
4703 /* Fabric block timer */
4704 init_timer(&phba->fabric_block_timer);
4705 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4706 phba->fabric_block_timer.data = (unsigned long) phba;
4707 /* EA polling mode timer */
4708 init_timer(&phba->eratt_poll);
4709 phba->eratt_poll.function = lpfc_poll_eratt;
4710 phba->eratt_poll.data = (unsigned long) phba;
4711
4712 /* Host attention work mask setup */
4713 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
4714 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
4715
4716 /* Get all the module params for configuring this host */
4717 lpfc_get_cfgparam(phba);
James Smart49198b32010-04-06 15:04:33 -04004718 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
4719 phba->menlo_flag |= HBA_MENLO_SUPPORT;
4720 /* check for menlo minimum sg count */
4721 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
4722 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
4723 }
4724
James Smart2a76a282012-08-03 12:35:54 -04004725 if (!phba->sli.ring)
4726 phba->sli.ring = (struct lpfc_sli_ring *)
4727 kzalloc(LPFC_SLI3_MAX_RING *
4728 sizeof(struct lpfc_sli_ring), GFP_KERNEL);
4729 if (!phba->sli.ring)
4730 return -ENOMEM;
4731
James Smart3772a992009-05-22 14:50:54 -04004732 /*
James Smart96f70772013-04-17 20:16:15 -04004733 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smart3772a992009-05-22 14:50:54 -04004734 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart3772a992009-05-22 14:50:54 -04004735 */
James Smart3772a992009-05-22 14:50:54 -04004736
James Smart96f70772013-04-17 20:16:15 -04004737 /* Initialize the host templates the configured values. */
James Smart3772a992009-05-22 14:50:54 -04004738 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4739 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4740
James Smart96f70772013-04-17 20:16:15 -04004741 /* There are going to be 2 reserved BDEs: 1 FCP cmnd + 1 FCP rsp */
4742 if (phba->cfg_enable_bg) {
4743 /*
4744 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
4745 * the FCP rsp, and a BDE for each. Sice we have no control
4746 * over how many protection data segments the SCSI Layer
4747 * will hand us (ie: there could be one for every block
4748 * in the IO), we just allocate enough BDEs to accomidate
4749 * our max amount and we need to limit lpfc_sg_seg_cnt to
4750 * minimize the risk of running out.
4751 */
4752 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4753 sizeof(struct fcp_rsp) +
4754 (LPFC_MAX_SG_SEG_CNT * sizeof(struct ulp_bde64));
4755
4756 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SEG_CNT_DIF)
4757 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT_DIF;
4758
4759 /* Total BDEs in BPL for scsi_sg_list and scsi_sg_prot_list */
4760 phba->cfg_total_seg_cnt = LPFC_MAX_SG_SEG_CNT;
4761 } else {
4762 /*
4763 * The scsi_buf for a regular I/O will hold the FCP cmnd,
4764 * the FCP rsp, a BDE for each, and a BDE for up to
4765 * cfg_sg_seg_cnt data segments.
4766 */
4767 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4768 sizeof(struct fcp_rsp) +
4769 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
4770
4771 /* Total BDEs in BPL for scsi_sg_list */
4772 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
4773 }
4774
4775 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
4776 "9088 sg_tablesize:%d dmabuf_size:%d total_bde:%d\n",
4777 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
4778 phba->cfg_total_seg_cnt);
4779
James Smart3772a992009-05-22 14:50:54 -04004780 phba->max_vpi = LPFC_MAX_VPI;
4781 /* This will be set to correct value after config_port mbox */
4782 phba->max_vports = 0;
4783
4784 /*
4785 * Initialize the SLI Layer to run with lpfc HBAs.
4786 */
4787 lpfc_sli_setup(phba);
4788 lpfc_sli_queue_setup(phba);
4789
4790 /* Allocate device driver memory */
4791 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
4792 return -ENOMEM;
4793
James Smart912e3ac2011-05-24 11:42:11 -04004794 /*
4795 * Enable sr-iov virtual functions if supported and configured
4796 * through the module parameter.
4797 */
4798 if (phba->cfg_sriov_nr_virtfn > 0) {
4799 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
4800 phba->cfg_sriov_nr_virtfn);
4801 if (rc) {
4802 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4803 "2808 Requested number of SR-IOV "
4804 "virtual functions (%d) is not "
4805 "supported\n",
4806 phba->cfg_sriov_nr_virtfn);
4807 phba->cfg_sriov_nr_virtfn = 0;
4808 }
4809 }
4810
James Smart3772a992009-05-22 14:50:54 -04004811 return 0;
4812}
4813
4814/**
4815 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
4816 * @phba: pointer to lpfc hba data structure.
4817 *
4818 * This routine is invoked to unset the driver internal resources set up
4819 * specific for supporting the SLI-3 HBA device it attached to.
4820 **/
4821static void
4822lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
4823{
4824 /* Free device driver memory allocated */
4825 lpfc_mem_free_all(phba);
4826
4827 return;
4828}
4829
4830/**
James Smartda0436e2009-05-22 14:51:39 -04004831 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
4832 * @phba: pointer to lpfc hba data structure.
4833 *
4834 * This routine is invoked to set up the driver internal resources specific to
4835 * support the SLI-4 HBA device it attached to.
4836 *
4837 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004838 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004839 * other values - error
4840 **/
4841static int
4842lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
4843{
4844 struct lpfc_sli *psli;
James Smart28baac72010-02-12 14:42:03 -05004845 LPFC_MBOXQ_t *mboxq;
James Smart96f70772013-04-17 20:16:15 -04004846 int rc, i, hbq_count, max_buf_size;
James Smart28baac72010-02-12 14:42:03 -05004847 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
4848 struct lpfc_mqe *mqe;
James Smart09294d42013-04-17 20:16:05 -04004849 int longs;
James Smartda0436e2009-05-22 14:51:39 -04004850
4851 /* Before proceed, wait for POST done and device ready */
4852 rc = lpfc_sli4_post_status_check(phba);
4853 if (rc)
4854 return -ENODEV;
4855
4856 /*
4857 * Initialize timers used by driver
4858 */
4859
4860 /* Heartbeat timer */
4861 init_timer(&phba->hb_tmofunc);
4862 phba->hb_tmofunc.function = lpfc_hb_timeout;
4863 phba->hb_tmofunc.data = (unsigned long)phba;
James Smart19ca7602010-11-20 23:11:55 -05004864 init_timer(&phba->rrq_tmr);
4865 phba->rrq_tmr.function = lpfc_rrq_timeout;
4866 phba->rrq_tmr.data = (unsigned long)phba;
James Smartda0436e2009-05-22 14:51:39 -04004867
4868 psli = &phba->sli;
4869 /* MBOX heartbeat timer */
4870 init_timer(&psli->mbox_tmo);
4871 psli->mbox_tmo.function = lpfc_mbox_timeout;
4872 psli->mbox_tmo.data = (unsigned long) phba;
4873 /* Fabric block timer */
4874 init_timer(&phba->fabric_block_timer);
4875 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4876 phba->fabric_block_timer.data = (unsigned long) phba;
4877 /* EA polling mode timer */
4878 init_timer(&phba->eratt_poll);
4879 phba->eratt_poll.function = lpfc_poll_eratt;
4880 phba->eratt_poll.data = (unsigned long) phba;
James Smartecfd03c2010-02-12 14:41:27 -05004881 /* FCF rediscover timer */
4882 init_timer(&phba->fcf.redisc_wait);
4883 phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
4884 phba->fcf.redisc_wait.data = (unsigned long)phba;
4885
James Smartda0436e2009-05-22 14:51:39 -04004886 /*
James Smart7ad20aa2011-05-24 11:44:28 -04004887 * Control structure for handling external multi-buffer mailbox
4888 * command pass-through.
4889 */
4890 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
4891 sizeof(struct lpfc_mbox_ext_buf_ctx));
4892 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
4893
4894 /*
James Smartda0436e2009-05-22 14:51:39 -04004895 * We need to do a READ_CONFIG mailbox command here before
4896 * calling lpfc_get_cfgparam. For VFs this will report the
4897 * MAX_XRI, MAX_VPI, MAX_RPI, MAX_IOCB, and MAX_VFI settings.
4898 * All of the resources allocated
4899 * for this Port are tied to these values.
4900 */
4901 /* Get all the module params for configuring this host */
4902 lpfc_get_cfgparam(phba);
4903 phba->max_vpi = LPFC_MAX_VPI;
James Smart67d12732012-08-03 12:36:13 -04004904
4905 /* Eventually cfg_fcp_eq_count / cfg_fcp_wq_count will be depricated */
4906 phba->cfg_fcp_io_channel = phba->cfg_fcp_eq_count;
4907
James Smartda0436e2009-05-22 14:51:39 -04004908 /* This will be set to correct value after the read_config mbox */
4909 phba->max_vports = 0;
4910
4911 /* Program the default value of vlan_id and fc_map */
4912 phba->valid_vlan = 0;
4913 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4914 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4915 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4916
4917 /*
James Smart2a76a282012-08-03 12:35:54 -04004918 * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
4919 * we will associate a new ring, for each FCP fastpath EQ/CQ/WQ tuple.
4920 */
4921 if (!phba->sli.ring)
4922 phba->sli.ring = kzalloc(
James Smart67d12732012-08-03 12:36:13 -04004923 (LPFC_SLI3_MAX_RING + phba->cfg_fcp_io_channel) *
James Smart2a76a282012-08-03 12:35:54 -04004924 sizeof(struct lpfc_sli_ring), GFP_KERNEL);
4925 if (!phba->sli.ring)
4926 return -ENOMEM;
James Smart09294d42013-04-17 20:16:05 -04004927
4928 /*
4929 * It doesn't matter what family our adapter is in, we are
4930 * limited to 2 Pages, 512 SGEs, for our SGL.
4931 * There are going to be 2 reserved SGEs: 1 FCP cmnd + 1 FCP rsp
4932 */
4933 max_buf_size = (2 * SLI4_PAGE_SIZE);
4934 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SGL_SEG_CNT - 2)
4935 phba->cfg_sg_seg_cnt = LPFC_MAX_SGL_SEG_CNT - 2;
James Smart09294d42013-04-17 20:16:05 -04004936
James Smart2a76a282012-08-03 12:35:54 -04004937 /*
James Smart96f70772013-04-17 20:16:15 -04004938 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smartda0436e2009-05-22 14:51:39 -04004939 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smartda0436e2009-05-22 14:51:39 -04004940 */
James Smart085c6472010-11-20 23:11:37 -05004941
James Smart96f70772013-04-17 20:16:15 -04004942 if (phba->cfg_enable_bg) {
4943 /*
4944 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
4945 * the FCP rsp, and a SGE for each. Sice we have no control
4946 * over how many protection data segments the SCSI Layer
4947 * will hand us (ie: there could be one for every block
4948 * in the IO), we just allocate enough SGEs to accomidate
4949 * our max amount and we need to limit lpfc_sg_seg_cnt to
4950 * minimize the risk of running out.
4951 */
4952 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4953 sizeof(struct fcp_rsp) + max_buf_size;
4954
4955 /* Total SGEs for scsi_sg_list and scsi_sg_prot_list */
4956 phba->cfg_total_seg_cnt = LPFC_MAX_SGL_SEG_CNT;
4957
4958 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SLI4_SEG_CNT_DIF)
4959 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SLI4_SEG_CNT_DIF;
4960 } else {
4961 /*
4962 * The scsi_buf for a regular I/O will hold the FCP cmnd,
4963 * the FCP rsp, a SGE for each, and a SGE for up to
4964 * cfg_sg_seg_cnt data segments.
4965 */
4966 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4967 sizeof(struct fcp_rsp) +
4968 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge));
4969
4970 /* Total SGEs for scsi_sg_list */
4971 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
4972 /*
4973 * NOTE: if (phba->cfg_sg_seg_cnt + 2) <= 256 we only need
4974 * to post 1 page for the SGL.
4975 */
4976 }
4977
4978 /* Initialize the host templates with the updated values. */
4979 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4980 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4981
4982 if (phba->cfg_sg_dma_buf_size <= LPFC_MIN_SG_SLI4_BUF_SZ)
4983 phba->cfg_sg_dma_buf_size = LPFC_MIN_SG_SLI4_BUF_SZ;
4984 else
4985 phba->cfg_sg_dma_buf_size =
4986 SLI4_PAGE_ALIGN(phba->cfg_sg_dma_buf_size);
4987
4988 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
4989 "9087 sg_tablesize:%d dmabuf_size:%d total_sge:%d\n",
4990 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
4991 phba->cfg_total_seg_cnt);
James Smartda0436e2009-05-22 14:51:39 -04004992
4993 /* Initialize buffer queue management fields */
4994 hbq_count = lpfc_sli_hbq_count();
4995 for (i = 0; i < hbq_count; ++i)
4996 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
4997 INIT_LIST_HEAD(&phba->rb_pend_list);
4998 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
4999 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
5000
5001 /*
5002 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
5003 */
5004 /* Initialize the Abort scsi buffer list used by driver */
5005 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
5006 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
5007 /* This abort list used by worker thread */
5008 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
5009
5010 /*
James Smart6d368e52011-05-24 11:44:12 -04005011 * Initialize driver internal slow-path work queues
James Smartda0436e2009-05-22 14:51:39 -04005012 */
5013
5014 /* Driver internel slow-path CQ Event pool */
5015 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
5016 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04005017 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04005018 /* Asynchronous event CQ Event work queue list */
5019 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
5020 /* Fast-path XRI aborted CQ Event work queue list */
5021 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
5022 /* Slow-path XRI aborted CQ Event work queue list */
5023 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
5024 /* Receive queue CQ Event work queue list */
5025 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
5026
James Smart6d368e52011-05-24 11:44:12 -04005027 /* Initialize extent block lists. */
5028 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
5029 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
5030 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
5031 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
5032
James Smartda0436e2009-05-22 14:51:39 -04005033 /* Initialize the driver internal SLI layer lists. */
5034 lpfc_sli_setup(phba);
5035 lpfc_sli_queue_setup(phba);
5036
5037 /* Allocate device driver memory */
5038 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
5039 if (rc)
5040 return -ENOMEM;
5041
James Smart2fcee4b2010-12-15 17:57:46 -05005042 /* IF Type 2 ports get initialized now. */
5043 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
5044 LPFC_SLI_INTF_IF_TYPE_2) {
5045 rc = lpfc_pci_function_reset(phba);
5046 if (unlikely(rc))
5047 return -ENODEV;
5048 }
5049
James Smartda0436e2009-05-22 14:51:39 -04005050 /* Create the bootstrap mailbox command */
5051 rc = lpfc_create_bootstrap_mbox(phba);
5052 if (unlikely(rc))
5053 goto out_free_mem;
5054
5055 /* Set up the host's endian order with the device. */
5056 rc = lpfc_setup_endian_order(phba);
5057 if (unlikely(rc))
5058 goto out_free_bsmbx;
5059
5060 /* Set up the hba's configuration parameters. */
5061 rc = lpfc_sli4_read_config(phba);
5062 if (unlikely(rc))
5063 goto out_free_bsmbx;
5064
James Smart2fcee4b2010-12-15 17:57:46 -05005065 /* IF Type 0 ports get initialized now. */
5066 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
5067 LPFC_SLI_INTF_IF_TYPE_0) {
5068 rc = lpfc_pci_function_reset(phba);
5069 if (unlikely(rc))
5070 goto out_free_bsmbx;
5071 }
James Smartda0436e2009-05-22 14:51:39 -04005072
James Smartcb5172e2010-03-15 11:25:07 -04005073 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
5074 GFP_KERNEL);
5075 if (!mboxq) {
5076 rc = -ENOMEM;
5077 goto out_free_bsmbx;
5078 }
5079
James Smartfedd3b72011-02-16 12:39:24 -05005080 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
James Smartcb5172e2010-03-15 11:25:07 -04005081 lpfc_supported_pages(mboxq);
5082 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smartfedd3b72011-02-16 12:39:24 -05005083 if (!rc) {
5084 mqe = &mboxq->u.mqe;
5085 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
5086 LPFC_MAX_SUPPORTED_PAGES);
5087 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
5088 switch (pn_page[i]) {
5089 case LPFC_SLI4_PARAMETERS:
5090 phba->sli4_hba.pc_sli4_params.supported = 1;
5091 break;
5092 default:
5093 break;
5094 }
5095 }
5096 /* Read the port's SLI4 Parameters capabilities if supported. */
5097 if (phba->sli4_hba.pc_sli4_params.supported)
5098 rc = lpfc_pc_sli4_params_get(phba, mboxq);
5099 if (rc) {
5100 mempool_free(mboxq, phba->mbox_mem_pool);
5101 rc = -EIO;
5102 goto out_free_bsmbx;
James Smartcb5172e2010-03-15 11:25:07 -04005103 }
5104 }
James Smartfedd3b72011-02-16 12:39:24 -05005105 /*
5106 * Get sli4 parameters that override parameters from Port capabilities.
James Smart6d368e52011-05-24 11:44:12 -04005107 * If this call fails, it isn't critical unless the SLI4 parameters come
5108 * back in conflict.
James Smartfedd3b72011-02-16 12:39:24 -05005109 */
James Smart6d368e52011-05-24 11:44:12 -04005110 rc = lpfc_get_sli4_parameters(phba, mboxq);
5111 if (rc) {
5112 if (phba->sli4_hba.extents_in_use &&
5113 phba->sli4_hba.rpi_hdrs_in_use) {
5114 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5115 "2999 Unsupported SLI4 Parameters "
5116 "Extents and RPI headers enabled.\n");
5117 goto out_free_bsmbx;
5118 }
5119 }
James Smartcb5172e2010-03-15 11:25:07 -04005120 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart5350d872011-10-10 21:33:49 -04005121 /* Verify all the SLI4 queues */
5122 rc = lpfc_sli4_queue_verify(phba);
James Smartda0436e2009-05-22 14:51:39 -04005123 if (rc)
5124 goto out_free_bsmbx;
5125
5126 /* Create driver internal CQE event pool */
5127 rc = lpfc_sli4_cq_event_pool_create(phba);
5128 if (rc)
James Smart5350d872011-10-10 21:33:49 -04005129 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04005130
James Smart8a9d2e82012-05-09 21:16:12 -04005131 /* Initialize sgl lists per host */
5132 lpfc_init_sgl_list(phba);
5133
5134 /* Allocate and initialize active sgl array */
James Smartda0436e2009-05-22 14:51:39 -04005135 rc = lpfc_init_active_sgl_array(phba);
5136 if (rc) {
5137 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5138 "1430 Failed to initialize sgl list.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04005139 goto out_destroy_cq_event_pool;
James Smartda0436e2009-05-22 14:51:39 -04005140 }
James Smartda0436e2009-05-22 14:51:39 -04005141 rc = lpfc_sli4_init_rpi_hdrs(phba);
5142 if (rc) {
5143 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5144 "1432 Failed to initialize rpi headers.\n");
5145 goto out_free_active_sgl;
5146 }
5147
James Smarta93ff372010-10-22 11:06:08 -04005148 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
James Smart0c9ab6f2010-02-26 14:15:57 -05005149 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
5150 phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
5151 GFP_KERNEL);
5152 if (!phba->fcf.fcf_rr_bmask) {
5153 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5154 "2759 Failed allocate memory for FCF round "
5155 "robin failover bmask\n");
James Smart05580562011-05-24 11:40:48 -04005156 rc = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -05005157 goto out_remove_rpi_hdrs;
5158 }
5159
James Smart67d12732012-08-03 12:36:13 -04005160 phba->sli4_hba.fcp_eq_hdl =
5161 kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
5162 phba->cfg_fcp_io_channel), GFP_KERNEL);
5163 if (!phba->sli4_hba.fcp_eq_hdl) {
5164 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5165 "2572 Failed allocate memory for "
5166 "fast-path per-EQ handle array\n");
5167 rc = -ENOMEM;
5168 goto out_free_fcf_rr_bmask;
James Smartda0436e2009-05-22 14:51:39 -04005169 }
5170
5171 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
James Smart82c3e9b2012-09-29 11:29:50 -04005172 phba->cfg_fcp_io_channel), GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005173 if (!phba->sli4_hba.msix_entries) {
5174 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5175 "2573 Failed allocate memory for msi-x "
5176 "interrupt vector entries\n");
James Smart05580562011-05-24 11:40:48 -04005177 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04005178 goto out_free_fcp_eq_hdl;
5179 }
5180
James Smart912e3ac2011-05-24 11:42:11 -04005181 /*
5182 * Enable sr-iov virtual functions if supported and configured
5183 * through the module parameter.
5184 */
5185 if (phba->cfg_sriov_nr_virtfn > 0) {
5186 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
5187 phba->cfg_sriov_nr_virtfn);
5188 if (rc) {
5189 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5190 "3020 Requested number of SR-IOV "
5191 "virtual functions (%d) is not "
5192 "supported\n",
5193 phba->cfg_sriov_nr_virtfn);
5194 phba->cfg_sriov_nr_virtfn = 0;
5195 }
5196 }
5197
James Smart5248a742011-07-22 18:37:06 -04005198 return 0;
James Smartda0436e2009-05-22 14:51:39 -04005199
5200out_free_fcp_eq_hdl:
5201 kfree(phba->sli4_hba.fcp_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05005202out_free_fcf_rr_bmask:
5203 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04005204out_remove_rpi_hdrs:
5205 lpfc_sli4_remove_rpi_hdrs(phba);
5206out_free_active_sgl:
5207 lpfc_free_active_sgl(phba);
James Smartda0436e2009-05-22 14:51:39 -04005208out_destroy_cq_event_pool:
5209 lpfc_sli4_cq_event_pool_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04005210out_free_bsmbx:
5211 lpfc_destroy_bootstrap_mbox(phba);
5212out_free_mem:
5213 lpfc_mem_free(phba);
5214 return rc;
5215}
5216
5217/**
5218 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
5219 * @phba: pointer to lpfc hba data structure.
5220 *
5221 * This routine is invoked to unset the driver internal resources set up
5222 * specific for supporting the SLI-4 HBA device it attached to.
5223 **/
5224static void
5225lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
5226{
5227 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
5228
James Smartda0436e2009-05-22 14:51:39 -04005229 /* Free memory allocated for msi-x interrupt vector entries */
5230 kfree(phba->sli4_hba.msix_entries);
5231
5232 /* Free memory allocated for fast-path work queue handles */
5233 kfree(phba->sli4_hba.fcp_eq_hdl);
5234
5235 /* Free the allocated rpi headers. */
5236 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04005237 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04005238
James Smart0c9ab6f2010-02-26 14:15:57 -05005239 /* Free eligible FCF index bmask */
5240 kfree(phba->fcf.fcf_rr_bmask);
5241
James Smartda0436e2009-05-22 14:51:39 -04005242 /* Free the ELS sgl list */
5243 lpfc_free_active_sgl(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04005244 lpfc_free_els_sgl_list(phba);
James Smartda0436e2009-05-22 14:51:39 -04005245
James Smartda0436e2009-05-22 14:51:39 -04005246 /* Free the completion queue EQ event pool */
5247 lpfc_sli4_cq_event_release_all(phba);
5248 lpfc_sli4_cq_event_pool_destroy(phba);
5249
James Smart6d368e52011-05-24 11:44:12 -04005250 /* Release resource identifiers. */
5251 lpfc_sli4_dealloc_resource_identifiers(phba);
5252
James Smartda0436e2009-05-22 14:51:39 -04005253 /* Free the bsmbx region. */
5254 lpfc_destroy_bootstrap_mbox(phba);
5255
5256 /* Free the SLI Layer memory with SLI4 HBAs */
5257 lpfc_mem_free_all(phba);
5258
5259 /* Free the current connect table */
5260 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04005261 &phba->fcf_conn_rec_list, list) {
5262 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04005263 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04005264 }
James Smartda0436e2009-05-22 14:51:39 -04005265
5266 return;
5267}
5268
5269/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005270 * lpfc_init_api_table_setup - Set up init api function jump table
James Smart3772a992009-05-22 14:50:54 -04005271 * @phba: The hba struct for which this call is being executed.
5272 * @dev_grp: The HBA PCI-Device group number.
5273 *
5274 * This routine sets up the device INIT interface API function jump table
5275 * in @phba struct.
5276 *
5277 * Returns: 0 - success, -ENODEV - failure.
5278 **/
5279int
5280lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5281{
James Smart84d1b002010-02-12 14:42:33 -05005282 phba->lpfc_hba_init_link = lpfc_hba_init_link;
5283 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart7f860592011-03-11 16:05:52 -05005284 phba->lpfc_selective_reset = lpfc_selective_reset;
James Smart3772a992009-05-22 14:50:54 -04005285 switch (dev_grp) {
5286 case LPFC_PCI_DEV_LP:
5287 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
5288 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
5289 phba->lpfc_stop_port = lpfc_stop_port_s3;
5290 break;
James Smartda0436e2009-05-22 14:51:39 -04005291 case LPFC_PCI_DEV_OC:
5292 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
5293 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
5294 phba->lpfc_stop_port = lpfc_stop_port_s4;
5295 break;
James Smart3772a992009-05-22 14:50:54 -04005296 default:
5297 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5298 "1431 Invalid HBA PCI-device group: 0x%x\n",
5299 dev_grp);
5300 return -ENODEV;
5301 break;
5302 }
5303 return 0;
5304}
5305
5306/**
5307 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
5308 * @phba: pointer to lpfc hba data structure.
5309 *
5310 * This routine is invoked to set up the driver internal resources before the
5311 * device specific resource setup to support the HBA device it attached to.
5312 *
5313 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005314 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005315 * other values - error
5316 **/
5317static int
5318lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
5319{
5320 /*
5321 * Driver resources common to all SLI revisions
5322 */
James Smartea2151b2008-09-07 11:52:10 -04005323 atomic_set(&phba->fast_event_count, 0);
James Smart2e0fef82007-06-17 19:56:36 -05005324 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005325
James Smarte47c9092008-02-08 18:49:26 -05005326 /* Initialize ndlp management spinlock */
5327 spin_lock_init(&phba->ndlp_lock);
5328
James Smart2e0fef82007-06-17 19:56:36 -05005329 INIT_LIST_HEAD(&phba->port_list);
James Smart3772a992009-05-22 14:50:54 -04005330 INIT_LIST_HEAD(&phba->work_list);
James Smart84774a42008-08-24 21:50:06 -04005331 init_waitqueue_head(&phba->wait_4_mlo_m_q);
dea31012005-04-17 16:05:31 -05005332
James Smart3772a992009-05-22 14:50:54 -04005333 /* Initialize the wait queue head for the kernel thread */
5334 init_waitqueue_head(&phba->work_waitq);
James Smart858c9f62007-06-17 19:56:39 -05005335
James Smart3772a992009-05-22 14:50:54 -04005336 /* Initialize the scsi buffer list used by driver for scsi IO */
5337 spin_lock_init(&phba->scsi_buf_list_lock);
5338 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
dea31012005-04-17 16:05:31 -05005339
James Smart3772a992009-05-22 14:50:54 -04005340 /* Initialize the fabric iocb list */
5341 INIT_LIST_HEAD(&phba->fabric_iocb_list);
dea31012005-04-17 16:05:31 -05005342
James Smart3772a992009-05-22 14:50:54 -04005343 /* Initialize list to save ELS buffers */
5344 INIT_LIST_HEAD(&phba->elsbuf);
dea31012005-04-17 16:05:31 -05005345
James Smart3772a992009-05-22 14:50:54 -04005346 /* Initialize FCF connection rec list */
5347 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
dea31012005-04-17 16:05:31 -05005348
James Smart3772a992009-05-22 14:50:54 -04005349 return 0;
5350}
dea31012005-04-17 16:05:31 -05005351
James Smart3772a992009-05-22 14:50:54 -04005352/**
5353 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
5354 * @phba: pointer to lpfc hba data structure.
5355 *
5356 * This routine is invoked to set up the driver internal resources after the
5357 * device specific resource setup to support the HBA device it attached to.
5358 *
5359 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005360 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005361 * other values - error
5362 **/
5363static int
5364lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
5365{
5366 int error;
5367
5368 /* Startup the kernel thread for this host adapter. */
5369 phba->worker_thread = kthread_run(lpfc_do_work, phba,
5370 "lpfc_worker_%d", phba->brd_no);
5371 if (IS_ERR(phba->worker_thread)) {
5372 error = PTR_ERR(phba->worker_thread);
5373 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005374 }
5375
James Smart3772a992009-05-22 14:50:54 -04005376 return 0;
5377}
5378
5379/**
5380 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
5381 * @phba: pointer to lpfc hba data structure.
5382 *
5383 * This routine is invoked to unset the driver internal resources set up after
5384 * the device specific resource setup for supporting the HBA device it
5385 * attached to.
5386 **/
5387static void
5388lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
5389{
5390 /* Stop kernel worker thread */
5391 kthread_stop(phba->worker_thread);
5392}
5393
5394/**
5395 * lpfc_free_iocb_list - Free iocb list.
5396 * @phba: pointer to lpfc hba data structure.
5397 *
5398 * This routine is invoked to free the driver's IOCB list and memory.
5399 **/
5400static void
5401lpfc_free_iocb_list(struct lpfc_hba *phba)
5402{
5403 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
5404
5405 spin_lock_irq(&phba->hbalock);
5406 list_for_each_entry_safe(iocbq_entry, iocbq_next,
5407 &phba->lpfc_iocb_list, list) {
5408 list_del(&iocbq_entry->list);
5409 kfree(iocbq_entry);
5410 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005411 }
James Smart3772a992009-05-22 14:50:54 -04005412 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005413
James Smart3772a992009-05-22 14:50:54 -04005414 return;
5415}
dea31012005-04-17 16:05:31 -05005416
James Smart3772a992009-05-22 14:50:54 -04005417/**
5418 * lpfc_init_iocb_list - Allocate and initialize iocb list.
5419 * @phba: pointer to lpfc hba data structure.
5420 *
5421 * This routine is invoked to allocate and initizlize the driver's IOCB
5422 * list and set up the IOCB tag array accordingly.
5423 *
5424 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005425 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005426 * other values - error
5427 **/
5428static int
5429lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
5430{
5431 struct lpfc_iocbq *iocbq_entry = NULL;
5432 uint16_t iotag;
5433 int i;
dea31012005-04-17 16:05:31 -05005434
5435 /* Initialize and populate the iocb list per host. */
5436 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04005437 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07005438 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05005439 if (iocbq_entry == NULL) {
5440 printk(KERN_ERR "%s: only allocated %d iocbs of "
5441 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07005442 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05005443 goto out_free_iocbq;
5444 }
5445
James Bottomley604a3e32005-10-29 10:28:33 -05005446 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
5447 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04005448 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05005449 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04005450 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05005451 goto out_free_iocbq;
5452 }
James Smart6d368e52011-05-24 11:44:12 -04005453 iocbq_entry->sli4_lxritag = NO_XRI;
James Smart3772a992009-05-22 14:50:54 -04005454 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05005455
5456 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005457 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
5458 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05005459 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005460 }
5461
James Smart3772a992009-05-22 14:50:54 -04005462 return 0;
5463
5464out_free_iocbq:
5465 lpfc_free_iocb_list(phba);
5466
5467 return -ENOMEM;
5468}
5469
5470/**
James Smart8a9d2e82012-05-09 21:16:12 -04005471 * lpfc_free_sgl_list - Free a given sgl list.
James Smartda0436e2009-05-22 14:51:39 -04005472 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -04005473 * @sglq_list: pointer to the head of sgl list.
James Smartda0436e2009-05-22 14:51:39 -04005474 *
James Smart8a9d2e82012-05-09 21:16:12 -04005475 * This routine is invoked to free a give sgl list and memory.
James Smartda0436e2009-05-22 14:51:39 -04005476 **/
James Smart8a9d2e82012-05-09 21:16:12 -04005477void
5478lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
James Smartda0436e2009-05-22 14:51:39 -04005479{
5480 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart8a9d2e82012-05-09 21:16:12 -04005481
5482 list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
5483 list_del(&sglq_entry->list);
5484 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
5485 kfree(sglq_entry);
5486 }
5487}
5488
5489/**
5490 * lpfc_free_els_sgl_list - Free els sgl list.
5491 * @phba: pointer to lpfc hba data structure.
5492 *
5493 * This routine is invoked to free the driver's els sgl list and memory.
5494 **/
5495static void
5496lpfc_free_els_sgl_list(struct lpfc_hba *phba)
5497{
James Smartda0436e2009-05-22 14:51:39 -04005498 LIST_HEAD(sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04005499
James Smart8a9d2e82012-05-09 21:16:12 -04005500 /* Retrieve all els sgls from driver list */
James Smartda0436e2009-05-22 14:51:39 -04005501 spin_lock_irq(&phba->hbalock);
5502 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
5503 spin_unlock_irq(&phba->hbalock);
5504
James Smart8a9d2e82012-05-09 21:16:12 -04005505 /* Now free the sgl list */
5506 lpfc_free_sgl_list(phba, &sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04005507}
5508
5509/**
5510 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
5511 * @phba: pointer to lpfc hba data structure.
5512 *
5513 * This routine is invoked to allocate the driver's active sgl memory.
5514 * This array will hold the sglq_entry's for active IOs.
5515 **/
5516static int
5517lpfc_init_active_sgl_array(struct lpfc_hba *phba)
5518{
5519 int size;
5520 size = sizeof(struct lpfc_sglq *);
5521 size *= phba->sli4_hba.max_cfg_param.max_xri;
5522
5523 phba->sli4_hba.lpfc_sglq_active_list =
5524 kzalloc(size, GFP_KERNEL);
5525 if (!phba->sli4_hba.lpfc_sglq_active_list)
5526 return -ENOMEM;
5527 return 0;
5528}
5529
5530/**
5531 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
5532 * @phba: pointer to lpfc hba data structure.
5533 *
5534 * This routine is invoked to walk through the array of active sglq entries
5535 * and free all of the resources.
5536 * This is just a place holder for now.
5537 **/
5538static void
5539lpfc_free_active_sgl(struct lpfc_hba *phba)
5540{
5541 kfree(phba->sli4_hba.lpfc_sglq_active_list);
5542}
5543
5544/**
5545 * lpfc_init_sgl_list - Allocate and initialize sgl list.
5546 * @phba: pointer to lpfc hba data structure.
5547 *
5548 * This routine is invoked to allocate and initizlize the driver's sgl
5549 * list and set up the sgl xritag tag array accordingly.
5550 *
James Smartda0436e2009-05-22 14:51:39 -04005551 **/
James Smart8a9d2e82012-05-09 21:16:12 -04005552static void
James Smartda0436e2009-05-22 14:51:39 -04005553lpfc_init_sgl_list(struct lpfc_hba *phba)
5554{
James Smartda0436e2009-05-22 14:51:39 -04005555 /* Initialize and populate the sglq list per host/VF. */
5556 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
5557 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
5558
James Smart8a9d2e82012-05-09 21:16:12 -04005559 /* els xri-sgl book keeping */
5560 phba->sli4_hba.els_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04005561
James Smart8a9d2e82012-05-09 21:16:12 -04005562 /* scsi xri-buffer book keeping */
James Smartda0436e2009-05-22 14:51:39 -04005563 phba->sli4_hba.scsi_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04005564}
5565
5566/**
5567 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
5568 * @phba: pointer to lpfc hba data structure.
5569 *
5570 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -04005571 * port for those SLI4 ports that do not support extents. This routine
James Smartda0436e2009-05-22 14:51:39 -04005572 * posts a PAGE_SIZE memory region to the port to hold up to
James Smart88a2cfb2011-07-22 18:36:33 -04005573 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
5574 * and should be called only when interrupts are disabled.
James Smartda0436e2009-05-22 14:51:39 -04005575 *
5576 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005577 * 0 - successful
James Smart88a2cfb2011-07-22 18:36:33 -04005578 * -ERROR - otherwise.
James Smartda0436e2009-05-22 14:51:39 -04005579 **/
5580int
5581lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
5582{
5583 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04005584 struct lpfc_rpi_hdr *rpi_hdr;
5585
5586 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
James Smartff78d8f2011-12-13 13:21:35 -05005587 if (!phba->sli4_hba.rpi_hdrs_in_use)
James Smart6d368e52011-05-24 11:44:12 -04005588 return rc;
James Smart6d368e52011-05-24 11:44:12 -04005589 if (phba->sli4_hba.extents_in_use)
5590 return -EIO;
James Smartda0436e2009-05-22 14:51:39 -04005591
5592 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
5593 if (!rpi_hdr) {
5594 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5595 "0391 Error during rpi post operation\n");
5596 lpfc_sli4_remove_rpis(phba);
5597 rc = -ENODEV;
5598 }
5599
5600 return rc;
5601}
5602
5603/**
5604 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
5605 * @phba: pointer to lpfc hba data structure.
5606 *
5607 * This routine is invoked to allocate a single 4KB memory region to
5608 * support rpis and stores them in the phba. This single region
5609 * provides support for up to 64 rpis. The region is used globally
5610 * by the device.
5611 *
5612 * Returns:
5613 * A valid rpi hdr on success.
5614 * A NULL pointer on any failure.
5615 **/
5616struct lpfc_rpi_hdr *
5617lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
5618{
5619 uint16_t rpi_limit, curr_rpi_range;
5620 struct lpfc_dmabuf *dmabuf;
5621 struct lpfc_rpi_hdr *rpi_hdr;
James Smart9589b0622011-04-16 11:03:17 -04005622 uint32_t rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04005623
James Smart6d368e52011-05-24 11:44:12 -04005624 /*
5625 * If the SLI4 port supports extents, posting the rpi header isn't
5626 * required. Set the expected maximum count and let the actual value
5627 * get set when extents are fully allocated.
5628 */
5629 if (!phba->sli4_hba.rpi_hdrs_in_use)
5630 return NULL;
5631 if (phba->sli4_hba.extents_in_use)
5632 return NULL;
5633
5634 /* The limit on the logical index is just the max_rpi count. */
James Smartda0436e2009-05-22 14:51:39 -04005635 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
James Smart6d368e52011-05-24 11:44:12 -04005636 phba->sli4_hba.max_cfg_param.max_rpi - 1;
James Smartda0436e2009-05-22 14:51:39 -04005637
5638 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04005639 /*
5640 * Establish the starting RPI in this header block. The starting
5641 * rpi is normalized to a zero base because the physical rpi is
5642 * port based.
5643 */
James Smart97f2ecf2012-03-01 22:35:23 -05005644 curr_rpi_range = phba->sli4_hba.next_rpi;
James Smartda0436e2009-05-22 14:51:39 -04005645 spin_unlock_irq(&phba->hbalock);
5646
5647 /*
5648 * The port has a limited number of rpis. The increment here
5649 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
5650 * and to allow the full max_rpi range per port.
5651 */
5652 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
James Smart9589b0622011-04-16 11:03:17 -04005653 rpi_count = rpi_limit - curr_rpi_range;
5654 else
5655 rpi_count = LPFC_RPI_HDR_COUNT;
James Smartda0436e2009-05-22 14:51:39 -04005656
James Smart6d368e52011-05-24 11:44:12 -04005657 if (!rpi_count)
5658 return NULL;
James Smartda0436e2009-05-22 14:51:39 -04005659 /*
5660 * First allocate the protocol header region for the port. The
5661 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
5662 */
5663 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5664 if (!dmabuf)
5665 return NULL;
5666
5667 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5668 LPFC_HDR_TEMPLATE_SIZE,
5669 &dmabuf->phys,
5670 GFP_KERNEL);
5671 if (!dmabuf->virt) {
5672 rpi_hdr = NULL;
5673 goto err_free_dmabuf;
5674 }
5675
5676 memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
5677 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
5678 rpi_hdr = NULL;
5679 goto err_free_coherent;
5680 }
5681
5682 /* Save the rpi header data for cleanup later. */
5683 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
5684 if (!rpi_hdr)
5685 goto err_free_coherent;
5686
5687 rpi_hdr->dmabuf = dmabuf;
5688 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
5689 rpi_hdr->page_count = 1;
5690 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04005691
5692 /* The rpi_hdr stores the logical index only. */
5693 rpi_hdr->start_rpi = curr_rpi_range;
James Smartda0436e2009-05-22 14:51:39 -04005694 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
5695
5696 /*
James Smart6d368e52011-05-24 11:44:12 -04005697 * The next_rpi stores the next logical module-64 rpi value used
5698 * to post physical rpis in subsequent rpi postings.
James Smartda0436e2009-05-22 14:51:39 -04005699 */
James Smart9589b0622011-04-16 11:03:17 -04005700 phba->sli4_hba.next_rpi += rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04005701 spin_unlock_irq(&phba->hbalock);
5702 return rpi_hdr;
5703
5704 err_free_coherent:
5705 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
5706 dmabuf->virt, dmabuf->phys);
5707 err_free_dmabuf:
5708 kfree(dmabuf);
5709 return NULL;
5710}
5711
5712/**
5713 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
5714 * @phba: pointer to lpfc hba data structure.
5715 *
5716 * This routine is invoked to remove all memory resources allocated
James Smart6d368e52011-05-24 11:44:12 -04005717 * to support rpis for SLI4 ports not supporting extents. This routine
5718 * presumes the caller has released all rpis consumed by fabric or port
5719 * logins and is prepared to have the header pages removed.
James Smartda0436e2009-05-22 14:51:39 -04005720 **/
5721void
5722lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
5723{
5724 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
5725
James Smart6d368e52011-05-24 11:44:12 -04005726 if (!phba->sli4_hba.rpi_hdrs_in_use)
5727 goto exit;
5728
James Smartda0436e2009-05-22 14:51:39 -04005729 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
5730 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
5731 list_del(&rpi_hdr->list);
5732 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
5733 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
5734 kfree(rpi_hdr->dmabuf);
5735 kfree(rpi_hdr);
5736 }
James Smart6d368e52011-05-24 11:44:12 -04005737 exit:
5738 /* There are no rpis available to the port now. */
5739 phba->sli4_hba.next_rpi = 0;
James Smartda0436e2009-05-22 14:51:39 -04005740}
5741
5742/**
James Smart3772a992009-05-22 14:50:54 -04005743 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
5744 * @pdev: pointer to pci device data structure.
5745 *
5746 * This routine is invoked to allocate the driver hba data structure for an
5747 * HBA device. If the allocation is successful, the phba reference to the
5748 * PCI device data structure is set.
5749 *
5750 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005751 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04005752 * NULL - error
5753 **/
5754static struct lpfc_hba *
5755lpfc_hba_alloc(struct pci_dev *pdev)
5756{
5757 struct lpfc_hba *phba;
5758
5759 /* Allocate memory for HBA structure */
5760 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
5761 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02005762 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04005763 return NULL;
5764 }
5765
5766 /* Set reference to PCI device in HBA structure */
5767 phba->pcidev = pdev;
5768
5769 /* Assign an unused board number */
5770 phba->brd_no = lpfc_get_instance();
5771 if (phba->brd_no < 0) {
5772 kfree(phba);
5773 return NULL;
5774 }
5775
James Smart4fede782010-01-26 23:08:55 -05005776 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04005777 INIT_LIST_HEAD(&phba->ct_ev_waiters);
5778
James Smart3772a992009-05-22 14:50:54 -04005779 return phba;
5780}
5781
5782/**
5783 * lpfc_hba_free - Free driver hba data structure with a device.
5784 * @phba: pointer to lpfc hba data structure.
5785 *
5786 * This routine is invoked to free the driver hba data structure with an
5787 * HBA device.
5788 **/
5789static void
5790lpfc_hba_free(struct lpfc_hba *phba)
5791{
5792 /* Release the driver assigned board number */
5793 idr_remove(&lpfc_hba_index, phba->brd_no);
5794
James Smart2a76a282012-08-03 12:35:54 -04005795 /* Free memory allocated with sli rings */
5796 kfree(phba->sli.ring);
5797 phba->sli.ring = NULL;
5798
James Smart3772a992009-05-22 14:50:54 -04005799 kfree(phba);
5800 return;
5801}
5802
5803/**
5804 * lpfc_create_shost - Create hba physical port with associated scsi host.
5805 * @phba: pointer to lpfc hba data structure.
5806 *
5807 * This routine is invoked to create HBA physical port and associate a SCSI
5808 * host with it.
5809 *
5810 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005811 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005812 * other values - error
5813 **/
5814static int
5815lpfc_create_shost(struct lpfc_hba *phba)
5816{
5817 struct lpfc_vport *vport;
5818 struct Scsi_Host *shost;
5819
5820 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05005821 phba->fc_edtov = FF_DEF_EDTOV;
5822 phba->fc_ratov = FF_DEF_RATOV;
5823 phba->fc_altov = FF_DEF_ALTOV;
5824 phba->fc_arbtov = FF_DEF_ARBTOV;
5825
James Smartd7c47992010-06-08 18:31:54 -04005826 atomic_set(&phba->sdev_cnt, 0);
James Smart3de2a652007-08-02 11:09:59 -04005827 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05005828 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04005829 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05005830
5831 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05005832 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05005833 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04005834 /* Put reference to SCSI host to driver's device private data */
5835 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05005836
James Smart3772a992009-05-22 14:50:54 -04005837 return 0;
5838}
dea31012005-04-17 16:05:31 -05005839
James Smart3772a992009-05-22 14:50:54 -04005840/**
5841 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
5842 * @phba: pointer to lpfc hba data structure.
5843 *
5844 * This routine is invoked to destroy HBA physical port and the associated
5845 * SCSI host.
5846 **/
5847static void
5848lpfc_destroy_shost(struct lpfc_hba *phba)
5849{
5850 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04005851
James Smart3772a992009-05-22 14:50:54 -04005852 /* Destroy physical port that associated with the SCSI host */
5853 destroy_port(vport);
5854
5855 return;
5856}
5857
5858/**
5859 * lpfc_setup_bg - Setup Block guard structures and debug areas.
5860 * @phba: pointer to lpfc hba data structure.
5861 * @shost: the shost to be used to detect Block guard settings.
5862 *
5863 * This routine sets up the local Block guard protocol settings for @shost.
5864 * This routine also allocates memory for debugging bg buffers.
5865 **/
5866static void
5867lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
5868{
James Smartbbeb79b2012-06-12 13:54:27 -04005869 uint32_t old_mask;
5870 uint32_t old_guard;
5871
James Smart3772a992009-05-22 14:50:54 -04005872 int pagecnt = 10;
5873 if (lpfc_prot_mask && lpfc_prot_guard) {
5874 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5875 "1478 Registering BlockGuard with the "
5876 "SCSI layer\n");
James Smartbbeb79b2012-06-12 13:54:27 -04005877
5878 old_mask = lpfc_prot_mask;
5879 old_guard = lpfc_prot_guard;
5880
5881 /* Only allow supported values */
5882 lpfc_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
5883 SHOST_DIX_TYPE0_PROTECTION |
5884 SHOST_DIX_TYPE1_PROTECTION);
5885 lpfc_prot_guard &= (SHOST_DIX_GUARD_IP | SHOST_DIX_GUARD_CRC);
5886
5887 /* DIF Type 1 protection for profiles AST1/C1 is end to end */
5888 if (lpfc_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
5889 lpfc_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
5890
5891 if (lpfc_prot_mask && lpfc_prot_guard) {
5892 if ((old_mask != lpfc_prot_mask) ||
5893 (old_guard != lpfc_prot_guard))
5894 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5895 "1475 Registering BlockGuard with the "
5896 "SCSI layer: mask %d guard %d\n",
5897 lpfc_prot_mask, lpfc_prot_guard);
5898
5899 scsi_host_set_prot(shost, lpfc_prot_mask);
5900 scsi_host_set_guard(shost, lpfc_prot_guard);
5901 } else
5902 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5903 "1479 Not Registering BlockGuard with the SCSI "
5904 "layer, Bad protection parameters: %d %d\n",
5905 old_mask, old_guard);
James Smart98c9ea52007-10-27 13:37:33 -04005906 }
James Smartbbeb79b2012-06-12 13:54:27 -04005907
James Smart81301a92008-12-04 22:39:46 -05005908 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05005909 while (pagecnt) {
5910 spin_lock_init(&_dump_buf_lock);
5911 _dump_buf_data =
5912 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
5913 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04005914 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5915 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04005916 "_dump_buf_data at 0x%p\n",
5917 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05005918 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04005919 memset(_dump_buf_data, 0,
5920 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05005921 break;
James Smart3772a992009-05-22 14:50:54 -04005922 } else
James Smart81301a92008-12-04 22:39:46 -05005923 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05005924 }
James Smart81301a92008-12-04 22:39:46 -05005925 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04005926 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5927 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04005928 "memory for hexdump\n");
5929 } else
James Smart6a9c52c2009-10-02 15:16:51 -04005930 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5931 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05005932 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05005933 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05005934 while (pagecnt) {
5935 _dump_buf_dif =
5936 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
5937 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04005938 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5939 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04005940 "_dump_buf_dif at 0x%p\n",
5941 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05005942 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04005943 memset(_dump_buf_dif, 0,
5944 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05005945 break;
James Smart3772a992009-05-22 14:50:54 -04005946 } else
James Smart81301a92008-12-04 22:39:46 -05005947 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05005948 }
James Smart81301a92008-12-04 22:39:46 -05005949 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04005950 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5951 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04005952 "memory for hexdump\n");
5953 } else
James Smart6a9c52c2009-10-02 15:16:51 -04005954 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5955 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04005956 _dump_buf_dif);
5957}
5958
5959/**
5960 * lpfc_post_init_setup - Perform necessary device post initialization setup.
5961 * @phba: pointer to lpfc hba data structure.
5962 *
5963 * This routine is invoked to perform all the necessary post initialization
5964 * setup for the device.
5965 **/
5966static void
5967lpfc_post_init_setup(struct lpfc_hba *phba)
5968{
5969 struct Scsi_Host *shost;
5970 struct lpfc_adapter_event_header adapter_event;
5971
5972 /* Get the default values for Model Name and Description */
5973 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
5974
5975 /*
5976 * hba setup may have changed the hba_queue_depth so we need to
5977 * adjust the value of can_queue.
5978 */
5979 shost = pci_get_drvdata(phba->pcidev);
5980 shost->can_queue = phba->cfg_hba_queue_depth - 10;
5981 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
5982 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05005983
5984 lpfc_host_attrib_init(shost);
5985
James Smart2e0fef82007-06-17 19:56:36 -05005986 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
5987 spin_lock_irq(shost->host_lock);
5988 lpfc_poll_start_timer(phba);
5989 spin_unlock_irq(shost->host_lock);
5990 }
James Smart8f6d98d2006-08-01 07:34:00 -04005991
James Smart93996272008-08-24 21:50:30 -04005992 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5993 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04005994 /* Send board arrival event to upper layer */
5995 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
5996 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
5997 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04005998 sizeof(adapter_event),
5999 (char *) &adapter_event,
6000 LPFC_NL_VENDOR_ID);
6001 return;
6002}
6003
6004/**
6005 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
6006 * @phba: pointer to lpfc hba data structure.
6007 *
6008 * This routine is invoked to set up the PCI device memory space for device
6009 * with SLI-3 interface spec.
6010 *
6011 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006012 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006013 * other values - error
6014 **/
6015static int
6016lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
6017{
6018 struct pci_dev *pdev;
6019 unsigned long bar0map_len, bar2map_len;
6020 int i, hbq_count;
6021 void *ptr;
6022 int error = -ENODEV;
6023
6024 /* Obtain PCI device reference */
6025 if (!phba->pcidev)
6026 return error;
6027 else
6028 pdev = phba->pcidev;
6029
6030 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05006031 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
6032 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
6033 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
6034 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smart3772a992009-05-22 14:50:54 -04006035 return error;
Michael Reed8e685972009-09-18 12:02:05 -05006036 }
6037 }
James Smart3772a992009-05-22 14:50:54 -04006038
6039 /* Get the bus address of Bar0 and Bar2 and the number of bytes
6040 * required by each mapping.
6041 */
6042 phba->pci_bar0_map = pci_resource_start(pdev, 0);
6043 bar0map_len = pci_resource_len(pdev, 0);
6044
6045 phba->pci_bar2_map = pci_resource_start(pdev, 2);
6046 bar2map_len = pci_resource_len(pdev, 2);
6047
6048 /* Map HBA SLIM to a kernel virtual address. */
6049 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
6050 if (!phba->slim_memmap_p) {
6051 dev_printk(KERN_ERR, &pdev->dev,
6052 "ioremap failed for SLIM memory.\n");
6053 goto out;
6054 }
6055
6056 /* Map HBA Control Registers to a kernel virtual address. */
6057 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
6058 if (!phba->ctrl_regs_memmap_p) {
6059 dev_printk(KERN_ERR, &pdev->dev,
6060 "ioremap failed for HBA control registers.\n");
6061 goto out_iounmap_slim;
6062 }
6063
6064 /* Allocate memory for SLI-2 structures */
6065 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
6066 SLI2_SLIM_SIZE,
6067 &phba->slim2p.phys,
6068 GFP_KERNEL);
6069 if (!phba->slim2p.virt)
6070 goto out_iounmap;
6071
6072 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
6073 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04006074 phba->mbox_ext = (phba->slim2p.virt +
6075 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04006076 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
6077 phba->IOCBs = (phba->slim2p.virt +
6078 offsetof(struct lpfc_sli2_slim, IOCBs));
6079
6080 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
6081 lpfc_sli_hbq_size(),
6082 &phba->hbqslimp.phys,
6083 GFP_KERNEL);
6084 if (!phba->hbqslimp.virt)
6085 goto out_free_slim;
6086
6087 hbq_count = lpfc_sli_hbq_count();
6088 ptr = phba->hbqslimp.virt;
6089 for (i = 0; i < hbq_count; ++i) {
6090 phba->hbqs[i].hbq_virt = ptr;
6091 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
6092 ptr += (lpfc_hbq_defs[i]->entry_count *
6093 sizeof(struct lpfc_hbq_entry));
6094 }
6095 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
6096 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
6097
6098 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
6099
6100 INIT_LIST_HEAD(&phba->rb_pend_list);
6101
6102 phba->MBslimaddr = phba->slim_memmap_p;
6103 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
6104 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
6105 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
6106 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04006107
dea31012005-04-17 16:05:31 -05006108 return 0;
6109
dea31012005-04-17 16:05:31 -05006110out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04006111 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6112 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05006113out_iounmap:
6114 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05006115out_iounmap_slim:
dea31012005-04-17 16:05:31 -05006116 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05006117out:
6118 return error;
6119}
6120
James Smarte59058c2008-08-24 21:49:00 -04006121/**
James Smart3772a992009-05-22 14:50:54 -04006122 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
6123 * @phba: pointer to lpfc hba data structure.
6124 *
6125 * This routine is invoked to unset the PCI device memory space for device
6126 * with SLI-3 interface spec.
6127 **/
6128static void
6129lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
6130{
6131 struct pci_dev *pdev;
6132
6133 /* Obtain PCI device reference */
6134 if (!phba->pcidev)
6135 return;
6136 else
6137 pdev = phba->pcidev;
6138
6139 /* Free coherent DMA memory allocated */
6140 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
6141 phba->hbqslimp.virt, phba->hbqslimp.phys);
6142 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6143 phba->slim2p.virt, phba->slim2p.phys);
6144
6145 /* I/O memory unmap */
6146 iounmap(phba->ctrl_regs_memmap_p);
6147 iounmap(phba->slim_memmap_p);
6148
6149 return;
6150}
6151
6152/**
James Smartda0436e2009-05-22 14:51:39 -04006153 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
6154 * @phba: pointer to lpfc hba data structure.
6155 *
6156 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
6157 * done and check status.
6158 *
6159 * Return 0 if successful, otherwise -ENODEV.
6160 **/
6161int
6162lpfc_sli4_post_status_check(struct lpfc_hba *phba)
6163{
James Smart2fcee4b2010-12-15 17:57:46 -05006164 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
6165 struct lpfc_register reg_data;
6166 int i, port_error = 0;
6167 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04006168
James Smart9940b972011-03-11 16:06:12 -05006169 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
6170 memset(&reg_data, 0, sizeof(reg_data));
James Smart2fcee4b2010-12-15 17:57:46 -05006171 if (!phba->sli4_hba.PSMPHRregaddr)
James Smartda0436e2009-05-22 14:51:39 -04006172 return -ENODEV;
6173
James Smartda0436e2009-05-22 14:51:39 -04006174 /* Wait up to 30 seconds for the SLI Port POST done and ready */
6175 for (i = 0; i < 3000; i++) {
James Smart9940b972011-03-11 16:06:12 -05006176 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
6177 &portsmphr_reg.word0) ||
6178 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
James Smart2fcee4b2010-12-15 17:57:46 -05006179 /* Port has a fatal POST error, break out */
James Smartda0436e2009-05-22 14:51:39 -04006180 port_error = -ENODEV;
6181 break;
6182 }
James Smart2fcee4b2010-12-15 17:57:46 -05006183 if (LPFC_POST_STAGE_PORT_READY ==
6184 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
James Smartda0436e2009-05-22 14:51:39 -04006185 break;
James Smartda0436e2009-05-22 14:51:39 -04006186 msleep(10);
6187 }
6188
James Smart2fcee4b2010-12-15 17:57:46 -05006189 /*
6190 * If there was a port error during POST, then don't proceed with
6191 * other register reads as the data may not be valid. Just exit.
6192 */
6193 if (port_error) {
James Smartda0436e2009-05-22 14:51:39 -04006194 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05006195 "1408 Port Failed POST - portsmphr=0x%x, "
6196 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
6197 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
6198 portsmphr_reg.word0,
6199 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
6200 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
6201 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
6202 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
6203 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
6204 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
6205 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
6206 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
6207 } else {
James Smart28baac72010-02-12 14:42:03 -05006208 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05006209 "2534 Device Info: SLIFamily=0x%x, "
6210 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
6211 "SLIHint_2=0x%x, FT=0x%x\n",
James Smart28baac72010-02-12 14:42:03 -05006212 bf_get(lpfc_sli_intf_sli_family,
6213 &phba->sli4_hba.sli_intf),
6214 bf_get(lpfc_sli_intf_slirev,
6215 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05006216 bf_get(lpfc_sli_intf_if_type,
James Smart28baac72010-02-12 14:42:03 -05006217 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05006218 bf_get(lpfc_sli_intf_sli_hint1,
6219 &phba->sli4_hba.sli_intf),
6220 bf_get(lpfc_sli_intf_sli_hint2,
6221 &phba->sli4_hba.sli_intf),
6222 bf_get(lpfc_sli_intf_func_type,
James Smart28baac72010-02-12 14:42:03 -05006223 &phba->sli4_hba.sli_intf));
James Smart2fcee4b2010-12-15 17:57:46 -05006224 /*
6225 * Check for other Port errors during the initialization
6226 * process. Fail the load if the port did not come up
6227 * correctly.
6228 */
6229 if_type = bf_get(lpfc_sli_intf_if_type,
6230 &phba->sli4_hba.sli_intf);
6231 switch (if_type) {
6232 case LPFC_SLI_INTF_IF_TYPE_0:
6233 phba->sli4_hba.ue_mask_lo =
6234 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
6235 phba->sli4_hba.ue_mask_hi =
6236 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
6237 uerrlo_reg.word0 =
6238 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
6239 uerrhi_reg.word0 =
6240 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
6241 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
6242 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
6243 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6244 "1422 Unrecoverable Error "
6245 "Detected during POST "
6246 "uerr_lo_reg=0x%x, "
6247 "uerr_hi_reg=0x%x, "
6248 "ue_mask_lo_reg=0x%x, "
6249 "ue_mask_hi_reg=0x%x\n",
6250 uerrlo_reg.word0,
6251 uerrhi_reg.word0,
6252 phba->sli4_hba.ue_mask_lo,
6253 phba->sli4_hba.ue_mask_hi);
6254 port_error = -ENODEV;
6255 }
6256 break;
6257 case LPFC_SLI_INTF_IF_TYPE_2:
6258 /* Final checks. The port status should be clean. */
James Smart9940b972011-03-11 16:06:12 -05006259 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
6260 &reg_data.word0) ||
James Smart05580562011-05-24 11:40:48 -04006261 (bf_get(lpfc_sliport_status_err, &reg_data) &&
6262 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
James Smart2fcee4b2010-12-15 17:57:46 -05006263 phba->work_status[0] =
6264 readl(phba->sli4_hba.u.if_type2.
6265 ERR1regaddr);
6266 phba->work_status[1] =
6267 readl(phba->sli4_hba.u.if_type2.
6268 ERR2regaddr);
6269 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05006270 "2888 Unrecoverable port error "
6271 "following POST: port status reg "
6272 "0x%x, port_smphr reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05006273 "error 1=0x%x, error 2=0x%x\n",
6274 reg_data.word0,
6275 portsmphr_reg.word0,
6276 phba->work_status[0],
6277 phba->work_status[1]);
6278 port_error = -ENODEV;
6279 }
6280 break;
6281 case LPFC_SLI_INTF_IF_TYPE_1:
6282 default:
6283 break;
6284 }
James Smart28baac72010-02-12 14:42:03 -05006285 }
James Smartda0436e2009-05-22 14:51:39 -04006286 return port_error;
6287}
6288
6289/**
6290 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
6291 * @phba: pointer to lpfc hba data structure.
James Smart2fcee4b2010-12-15 17:57:46 -05006292 * @if_type: The SLI4 interface type getting configured.
James Smartda0436e2009-05-22 14:51:39 -04006293 *
6294 * This routine is invoked to set up SLI4 BAR0 PCI config space register
6295 * memory map.
6296 **/
6297static void
James Smart2fcee4b2010-12-15 17:57:46 -05006298lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04006299{
James Smart2fcee4b2010-12-15 17:57:46 -05006300 switch (if_type) {
6301 case LPFC_SLI_INTF_IF_TYPE_0:
6302 phba->sli4_hba.u.if_type0.UERRLOregaddr =
6303 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
6304 phba->sli4_hba.u.if_type0.UERRHIregaddr =
6305 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
6306 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
6307 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
6308 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
6309 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
6310 phba->sli4_hba.SLIINTFregaddr =
6311 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
6312 break;
6313 case LPFC_SLI_INTF_IF_TYPE_2:
6314 phba->sli4_hba.u.if_type2.ERR1regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006315 phba->sli4_hba.conf_regs_memmap_p +
6316 LPFC_CTL_PORT_ER1_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006317 phba->sli4_hba.u.if_type2.ERR2regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006318 phba->sli4_hba.conf_regs_memmap_p +
6319 LPFC_CTL_PORT_ER2_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006320 phba->sli4_hba.u.if_type2.CTRLregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006321 phba->sli4_hba.conf_regs_memmap_p +
6322 LPFC_CTL_PORT_CTL_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006323 phba->sli4_hba.u.if_type2.STATUSregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006324 phba->sli4_hba.conf_regs_memmap_p +
6325 LPFC_CTL_PORT_STA_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006326 phba->sli4_hba.SLIINTFregaddr =
6327 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
6328 phba->sli4_hba.PSMPHRregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006329 phba->sli4_hba.conf_regs_memmap_p +
6330 LPFC_CTL_PORT_SEM_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006331 phba->sli4_hba.RQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05006332 phba->sli4_hba.conf_regs_memmap_p +
6333 LPFC_ULP0_RQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05006334 phba->sli4_hba.WQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05006335 phba->sli4_hba.conf_regs_memmap_p +
6336 LPFC_ULP0_WQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05006337 phba->sli4_hba.EQCQDBregaddr =
6338 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
6339 phba->sli4_hba.MQDBregaddr =
6340 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
6341 phba->sli4_hba.BMBXregaddr =
6342 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
6343 break;
6344 case LPFC_SLI_INTF_IF_TYPE_1:
6345 default:
6346 dev_printk(KERN_ERR, &phba->pcidev->dev,
6347 "FATAL - unsupported SLI4 interface type - %d\n",
6348 if_type);
6349 break;
6350 }
James Smartda0436e2009-05-22 14:51:39 -04006351}
6352
6353/**
6354 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
6355 * @phba: pointer to lpfc hba data structure.
6356 *
6357 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
6358 * memory map.
6359 **/
6360static void
6361lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
6362{
James Smart2fcee4b2010-12-15 17:57:46 -05006363 phba->sli4_hba.PSMPHRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
6364 LPFC_SLIPORT_IF0_SMPHR;
James Smartda0436e2009-05-22 14:51:39 -04006365 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006366 LPFC_HST_ISR0;
James Smartda0436e2009-05-22 14:51:39 -04006367 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006368 LPFC_HST_IMR0;
James Smartda0436e2009-05-22 14:51:39 -04006369 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006370 LPFC_HST_ISCR0;
James Smartda0436e2009-05-22 14:51:39 -04006371}
6372
6373/**
6374 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
6375 * @phba: pointer to lpfc hba data structure.
6376 * @vf: virtual function number
6377 *
6378 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
6379 * based on the given viftual function number, @vf.
6380 *
6381 * Return 0 if successful, otherwise -ENODEV.
6382 **/
6383static int
6384lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
6385{
6386 if (vf > LPFC_VIR_FUNC_MAX)
6387 return -ENODEV;
6388
6389 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05006390 vf * LPFC_VFR_PAGE_SIZE +
6391 LPFC_ULP0_RQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04006392 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05006393 vf * LPFC_VFR_PAGE_SIZE +
6394 LPFC_ULP0_WQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04006395 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6396 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
6397 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6398 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
6399 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6400 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
6401 return 0;
6402}
6403
6404/**
6405 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
6406 * @phba: pointer to lpfc hba data structure.
6407 *
6408 * This routine is invoked to create the bootstrap mailbox
6409 * region consistent with the SLI-4 interface spec. This
6410 * routine allocates all memory necessary to communicate
6411 * mailbox commands to the port and sets up all alignment
6412 * needs. No locks are expected to be held when calling
6413 * this routine.
6414 *
6415 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006416 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04006417 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04006418 **/
6419static int
6420lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
6421{
6422 uint32_t bmbx_size;
6423 struct lpfc_dmabuf *dmabuf;
6424 struct dma_address *dma_address;
6425 uint32_t pa_addr;
6426 uint64_t phys_addr;
6427
6428 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
6429 if (!dmabuf)
6430 return -ENOMEM;
6431
6432 /*
6433 * The bootstrap mailbox region is comprised of 2 parts
6434 * plus an alignment restriction of 16 bytes.
6435 */
6436 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
6437 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
6438 bmbx_size,
6439 &dmabuf->phys,
6440 GFP_KERNEL);
6441 if (!dmabuf->virt) {
6442 kfree(dmabuf);
6443 return -ENOMEM;
6444 }
6445 memset(dmabuf->virt, 0, bmbx_size);
6446
6447 /*
6448 * Initialize the bootstrap mailbox pointers now so that the register
6449 * operations are simple later. The mailbox dma address is required
6450 * to be 16-byte aligned. Also align the virtual memory as each
6451 * maibox is copied into the bmbx mailbox region before issuing the
6452 * command to the port.
6453 */
6454 phba->sli4_hba.bmbx.dmabuf = dmabuf;
6455 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
6456
6457 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
6458 LPFC_ALIGN_16_BYTE);
6459 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
6460 LPFC_ALIGN_16_BYTE);
6461
6462 /*
6463 * Set the high and low physical addresses now. The SLI4 alignment
6464 * requirement is 16 bytes and the mailbox is posted to the port
6465 * as two 30-bit addresses. The other data is a bit marking whether
6466 * the 30-bit address is the high or low address.
6467 * Upcast bmbx aphys to 64bits so shift instruction compiles
6468 * clean on 32 bit machines.
6469 */
6470 dma_address = &phba->sli4_hba.bmbx.dma_address;
6471 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
6472 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
6473 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
6474 LPFC_BMBX_BIT1_ADDR_HI);
6475
6476 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
6477 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
6478 LPFC_BMBX_BIT1_ADDR_LO);
6479 return 0;
6480}
6481
6482/**
6483 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
6484 * @phba: pointer to lpfc hba data structure.
6485 *
6486 * This routine is invoked to teardown the bootstrap mailbox
6487 * region and release all host resources. This routine requires
6488 * the caller to ensure all mailbox commands recovered, no
6489 * additional mailbox comands are sent, and interrupts are disabled
6490 * before calling this routine.
6491 *
6492 **/
6493static void
6494lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
6495{
6496 dma_free_coherent(&phba->pcidev->dev,
6497 phba->sli4_hba.bmbx.bmbx_size,
6498 phba->sli4_hba.bmbx.dmabuf->virt,
6499 phba->sli4_hba.bmbx.dmabuf->phys);
6500
6501 kfree(phba->sli4_hba.bmbx.dmabuf);
6502 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
6503}
6504
6505/**
6506 * lpfc_sli4_read_config - Get the config parameters.
6507 * @phba: pointer to lpfc hba data structure.
6508 *
6509 * This routine is invoked to read the configuration parameters from the HBA.
6510 * The configuration parameters are used to set the base and maximum values
6511 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
6512 * allocation for the port.
6513 *
6514 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006515 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006516 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006517 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006518 **/
James Smartff78d8f2011-12-13 13:21:35 -05006519int
James Smartda0436e2009-05-22 14:51:39 -04006520lpfc_sli4_read_config(struct lpfc_hba *phba)
6521{
6522 LPFC_MBOXQ_t *pmb;
6523 struct lpfc_mbx_read_config *rd_config;
James Smart912e3ac2011-05-24 11:42:11 -04006524 union lpfc_sli4_cfg_shdr *shdr;
6525 uint32_t shdr_status, shdr_add_status;
6526 struct lpfc_mbx_get_func_cfg *get_func_cfg;
6527 struct lpfc_rsrc_desc_fcfcoe *desc;
James Smart8aa134a2012-08-14 14:25:29 -04006528 char *pdesc_0;
James Smart912e3ac2011-05-24 11:42:11 -04006529 uint32_t desc_count;
James Smart8aa134a2012-08-14 14:25:29 -04006530 int length, i, rc = 0, rc2;
James Smartda0436e2009-05-22 14:51:39 -04006531
6532 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6533 if (!pmb) {
6534 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6535 "2011 Unable to allocate memory for issuing "
6536 "SLI_CONFIG_SPECIAL mailbox command\n");
6537 return -ENOMEM;
6538 }
6539
6540 lpfc_read_config(phba, pmb);
6541
6542 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6543 if (rc != MBX_SUCCESS) {
6544 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6545 "2012 Mailbox failed , mbxCmd x%x "
6546 "READ_CONFIG, mbxStatus x%x\n",
6547 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6548 bf_get(lpfc_mqe_status, &pmb->u.mqe));
6549 rc = -EIO;
6550 } else {
6551 rd_config = &pmb->u.mqe.un.rd_config;
James Smartff78d8f2011-12-13 13:21:35 -05006552 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
6553 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
6554 phba->sli4_hba.lnk_info.lnk_tp =
6555 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
6556 phba->sli4_hba.lnk_info.lnk_no =
6557 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
6558 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6559 "3081 lnk_type:%d, lnk_numb:%d\n",
6560 phba->sli4_hba.lnk_info.lnk_tp,
6561 phba->sli4_hba.lnk_info.lnk_no);
6562 } else
6563 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6564 "3082 Mailbox (x%x) returned ldv:x0\n",
6565 bf_get(lpfc_mqe_command, &pmb->u.mqe));
James Smart6d368e52011-05-24 11:44:12 -04006566 phba->sli4_hba.extents_in_use =
6567 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04006568 phba->sli4_hba.max_cfg_param.max_xri =
6569 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
6570 phba->sli4_hba.max_cfg_param.xri_base =
6571 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
6572 phba->sli4_hba.max_cfg_param.max_vpi =
6573 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
6574 phba->sli4_hba.max_cfg_param.vpi_base =
6575 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
6576 phba->sli4_hba.max_cfg_param.max_rpi =
6577 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
6578 phba->sli4_hba.max_cfg_param.rpi_base =
6579 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
6580 phba->sli4_hba.max_cfg_param.max_vfi =
6581 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
6582 phba->sli4_hba.max_cfg_param.vfi_base =
6583 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
6584 phba->sli4_hba.max_cfg_param.max_fcfi =
6585 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04006586 phba->sli4_hba.max_cfg_param.max_eq =
6587 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
6588 phba->sli4_hba.max_cfg_param.max_rq =
6589 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
6590 phba->sli4_hba.max_cfg_param.max_wq =
6591 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
6592 phba->sli4_hba.max_cfg_param.max_cq =
6593 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
6594 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
6595 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
6596 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
6597 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
James Smart5ffc2662009-11-18 15:39:44 -05006598 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
6599 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04006600 phba->max_vports = phba->max_vpi;
6601 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart6d368e52011-05-24 11:44:12 -04006602 "2003 cfg params Extents? %d "
6603 "XRI(B:%d M:%d), "
James Smartda0436e2009-05-22 14:51:39 -04006604 "VPI(B:%d M:%d) "
6605 "VFI(B:%d M:%d) "
6606 "RPI(B:%d M:%d) "
James Smart6d368e52011-05-24 11:44:12 -04006607 "FCFI(Count:%d)\n",
6608 phba->sli4_hba.extents_in_use,
James Smartda0436e2009-05-22 14:51:39 -04006609 phba->sli4_hba.max_cfg_param.xri_base,
6610 phba->sli4_hba.max_cfg_param.max_xri,
6611 phba->sli4_hba.max_cfg_param.vpi_base,
6612 phba->sli4_hba.max_cfg_param.max_vpi,
6613 phba->sli4_hba.max_cfg_param.vfi_base,
6614 phba->sli4_hba.max_cfg_param.max_vfi,
6615 phba->sli4_hba.max_cfg_param.rpi_base,
6616 phba->sli4_hba.max_cfg_param.max_rpi,
James Smartda0436e2009-05-22 14:51:39 -04006617 phba->sli4_hba.max_cfg_param.max_fcfi);
6618 }
James Smart912e3ac2011-05-24 11:42:11 -04006619
6620 if (rc)
6621 goto read_cfg_out;
James Smartda0436e2009-05-22 14:51:39 -04006622
6623 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smartb92938b2010-06-07 15:24:12 -04006624 if (phba->cfg_hba_queue_depth >
6625 (phba->sli4_hba.max_cfg_param.max_xri -
6626 lpfc_sli4_get_els_iocb_cnt(phba)))
James Smartda0436e2009-05-22 14:51:39 -04006627 phba->cfg_hba_queue_depth =
James Smartb92938b2010-06-07 15:24:12 -04006628 phba->sli4_hba.max_cfg_param.max_xri -
6629 lpfc_sli4_get_els_iocb_cnt(phba);
James Smart912e3ac2011-05-24 11:42:11 -04006630
6631 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
6632 LPFC_SLI_INTF_IF_TYPE_2)
6633 goto read_cfg_out;
6634
6635 /* get the pf# and vf# for SLI4 if_type 2 port */
6636 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
6637 sizeof(struct lpfc_sli4_cfg_mhdr));
6638 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
6639 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
6640 length, LPFC_SLI4_MBX_EMBED);
6641
James Smart8aa134a2012-08-14 14:25:29 -04006642 rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart912e3ac2011-05-24 11:42:11 -04006643 shdr = (union lpfc_sli4_cfg_shdr *)
6644 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
6645 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6646 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart8aa134a2012-08-14 14:25:29 -04006647 if (rc2 || shdr_status || shdr_add_status) {
James Smart912e3ac2011-05-24 11:42:11 -04006648 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6649 "3026 Mailbox failed , mbxCmd x%x "
6650 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
6651 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6652 bf_get(lpfc_mqe_status, &pmb->u.mqe));
James Smart912e3ac2011-05-24 11:42:11 -04006653 goto read_cfg_out;
6654 }
6655
6656 /* search for fc_fcoe resrouce descriptor */
6657 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
6658 desc_count = get_func_cfg->func_cfg.rsrc_desc_count;
6659
James Smart8aa134a2012-08-14 14:25:29 -04006660 pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
6661 desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
6662 length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
6663 if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
6664 length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
6665 else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
6666 goto read_cfg_out;
6667
James Smart912e3ac2011-05-24 11:42:11 -04006668 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
James Smart8aa134a2012-08-14 14:25:29 -04006669 desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
James Smart912e3ac2011-05-24 11:42:11 -04006670 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
James Smart8aa134a2012-08-14 14:25:29 -04006671 bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
James Smart912e3ac2011-05-24 11:42:11 -04006672 phba->sli4_hba.iov.pf_number =
6673 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
6674 phba->sli4_hba.iov.vf_number =
6675 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
6676 break;
6677 }
6678 }
6679
6680 if (i < LPFC_RSRC_DESC_MAX_NUM)
6681 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6682 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
6683 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
6684 phba->sli4_hba.iov.vf_number);
James Smart8aa134a2012-08-14 14:25:29 -04006685 else
James Smart912e3ac2011-05-24 11:42:11 -04006686 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6687 "3028 GET_FUNCTION_CONFIG: failed to find "
6688 "Resrouce Descriptor:x%x\n",
6689 LPFC_RSRC_DESC_TYPE_FCFCOE);
James Smart912e3ac2011-05-24 11:42:11 -04006690
6691read_cfg_out:
6692 mempool_free(pmb, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04006693 return rc;
6694}
6695
6696/**
James Smart2fcee4b2010-12-15 17:57:46 -05006697 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
James Smartda0436e2009-05-22 14:51:39 -04006698 * @phba: pointer to lpfc hba data structure.
6699 *
James Smart2fcee4b2010-12-15 17:57:46 -05006700 * This routine is invoked to setup the port-side endian order when
6701 * the port if_type is 0. This routine has no function for other
6702 * if_types.
James Smartda0436e2009-05-22 14:51:39 -04006703 *
6704 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006705 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006706 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006707 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006708 **/
6709static int
6710lpfc_setup_endian_order(struct lpfc_hba *phba)
6711{
6712 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05006713 uint32_t if_type, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04006714 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
6715 HOST_ENDIAN_HIGH_WORD1};
6716
James Smart2fcee4b2010-12-15 17:57:46 -05006717 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
6718 switch (if_type) {
6719 case LPFC_SLI_INTF_IF_TYPE_0:
6720 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6721 GFP_KERNEL);
6722 if (!mboxq) {
6723 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6724 "0492 Unable to allocate memory for "
6725 "issuing SLI_CONFIG_SPECIAL mailbox "
6726 "command\n");
6727 return -ENOMEM;
6728 }
James Smartda0436e2009-05-22 14:51:39 -04006729
James Smart2fcee4b2010-12-15 17:57:46 -05006730 /*
6731 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
6732 * two words to contain special data values and no other data.
6733 */
6734 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
6735 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
6736 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6737 if (rc != MBX_SUCCESS) {
6738 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6739 "0493 SLI_CONFIG_SPECIAL mailbox "
6740 "failed with status x%x\n",
6741 rc);
6742 rc = -EIO;
6743 }
6744 mempool_free(mboxq, phba->mbox_mem_pool);
6745 break;
6746 case LPFC_SLI_INTF_IF_TYPE_2:
6747 case LPFC_SLI_INTF_IF_TYPE_1:
6748 default:
6749 break;
James Smartda0436e2009-05-22 14:51:39 -04006750 }
James Smartda0436e2009-05-22 14:51:39 -04006751 return rc;
6752}
6753
6754/**
James Smart5350d872011-10-10 21:33:49 -04006755 * lpfc_sli4_queue_verify - Verify and update EQ and CQ counts
James Smartda0436e2009-05-22 14:51:39 -04006756 * @phba: pointer to lpfc hba data structure.
6757 *
James Smart5350d872011-10-10 21:33:49 -04006758 * This routine is invoked to check the user settable queue counts for EQs and
6759 * CQs. after this routine is called the counts will be set to valid values that
6760 * adhere to the constraints of the system's interrupt vectors and the port's
6761 * queue resources.
James Smartda0436e2009-05-22 14:51:39 -04006762 *
6763 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006764 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006765 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04006766 **/
6767static int
James Smart5350d872011-10-10 21:33:49 -04006768lpfc_sli4_queue_verify(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04006769{
James Smart67d12732012-08-03 12:36:13 -04006770 int cfg_fcp_io_channel;
James Smart90695ee2012-08-14 14:25:36 -04006771 uint32_t cpu;
6772 uint32_t i = 0;
6773
James Smartda0436e2009-05-22 14:51:39 -04006774
6775 /*
James Smart67d12732012-08-03 12:36:13 -04006776 * Sanity check for configured queue parameters against the run-time
James Smartda0436e2009-05-22 14:51:39 -04006777 * device parameters
6778 */
6779
James Smart67d12732012-08-03 12:36:13 -04006780 /* Sanity check on HBA EQ parameters */
6781 cfg_fcp_io_channel = phba->cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04006782
James Smart90695ee2012-08-14 14:25:36 -04006783 /* It doesn't make sense to have more io channels then CPUs */
6784 for_each_online_cpu(cpu) {
6785 i++;
6786 }
6787 if (i < cfg_fcp_io_channel) {
James Smart82c3e9b2012-09-29 11:29:50 -04006788 lpfc_printf_log(phba,
6789 KERN_ERR, LOG_INIT,
James Smart90695ee2012-08-14 14:25:36 -04006790 "3188 Reducing IO channels to match number of "
6791 "CPUs: from %d to %d\n", cfg_fcp_io_channel, i);
6792 cfg_fcp_io_channel = i;
6793 }
6794
James Smart67d12732012-08-03 12:36:13 -04006795 if (cfg_fcp_io_channel >
6796 phba->sli4_hba.max_cfg_param.max_eq) {
James Smart82c3e9b2012-09-29 11:29:50 -04006797 if (phba->sli4_hba.max_cfg_param.max_eq <
6798 LPFC_FCP_IO_CHAN_MIN) {
James Smartda0436e2009-05-22 14:51:39 -04006799 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6800 "2574 Not enough EQs (%d) from the "
6801 "pci function for supporting FCP "
6802 "EQs (%d)\n",
6803 phba->sli4_hba.max_cfg_param.max_eq,
James Smart67d12732012-08-03 12:36:13 -04006804 phba->cfg_fcp_io_channel);
James Smartda0436e2009-05-22 14:51:39 -04006805 goto out_error;
6806 }
James Smart82c3e9b2012-09-29 11:29:50 -04006807 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6808 "2575 Reducing IO channels to match number of "
6809 "available EQs: from %d to %d\n",
6810 cfg_fcp_io_channel,
6811 phba->sli4_hba.max_cfg_param.max_eq);
6812 cfg_fcp_io_channel = phba->sli4_hba.max_cfg_param.max_eq;
James Smartda0436e2009-05-22 14:51:39 -04006813 }
James Smart67d12732012-08-03 12:36:13 -04006814
6815 /* Eventually cfg_fcp_eq_count / cfg_fcp_wq_count will be depricated */
6816
James Smartda0436e2009-05-22 14:51:39 -04006817 /* The actual number of FCP event queues adopted */
James Smart67d12732012-08-03 12:36:13 -04006818 phba->cfg_fcp_eq_count = cfg_fcp_io_channel;
6819 phba->cfg_fcp_wq_count = cfg_fcp_io_channel;
6820 phba->cfg_fcp_io_channel = cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04006821
James Smartda0436e2009-05-22 14:51:39 -04006822 /* Get EQ depth from module parameter, fake the default for now */
6823 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
6824 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
6825
James Smart5350d872011-10-10 21:33:49 -04006826 /* Get CQ depth from module parameter, fake the default for now */
6827 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
6828 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
6829
6830 return 0;
6831out_error:
6832 return -ENOMEM;
6833}
6834
6835/**
6836 * lpfc_sli4_queue_create - Create all the SLI4 queues
6837 * @phba: pointer to lpfc hba data structure.
6838 *
6839 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
6840 * operation. For each SLI4 queue type, the parameters such as queue entry
6841 * count (queue depth) shall be taken from the module parameter. For now,
6842 * we just use some constant number as place holder.
6843 *
6844 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07006845 * 0 - successful
James Smart5350d872011-10-10 21:33:49 -04006846 * -ENOMEM - No availble memory
6847 * -EIO - The mailbox failed to complete successfully.
6848 **/
6849int
6850lpfc_sli4_queue_create(struct lpfc_hba *phba)
6851{
6852 struct lpfc_queue *qdesc;
James Smart67d12732012-08-03 12:36:13 -04006853 int idx;
James Smart5350d872011-10-10 21:33:49 -04006854
6855 /*
James Smart67d12732012-08-03 12:36:13 -04006856 * Create HBA Record arrays.
James Smart5350d872011-10-10 21:33:49 -04006857 */
James Smart67d12732012-08-03 12:36:13 -04006858 if (!phba->cfg_fcp_io_channel)
6859 return -ERANGE;
James Smart5350d872011-10-10 21:33:49 -04006860
James Smart67d12732012-08-03 12:36:13 -04006861 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
6862 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
6863 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
6864 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
6865 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
6866 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
6867
6868 phba->sli4_hba.hba_eq = kzalloc((sizeof(struct lpfc_queue *) *
6869 phba->cfg_fcp_io_channel), GFP_KERNEL);
6870 if (!phba->sli4_hba.hba_eq) {
James Smartda0436e2009-05-22 14:51:39 -04006871 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04006872 "2576 Failed allocate memory for "
6873 "fast-path EQ record array\n");
James Smartda0436e2009-05-22 14:51:39 -04006874 goto out_error;
6875 }
James Smart67d12732012-08-03 12:36:13 -04006876
6877 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
6878 phba->cfg_fcp_io_channel), GFP_KERNEL);
6879 if (!phba->sli4_hba.fcp_cq) {
6880 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6881 "2577 Failed allocate memory for fast-path "
6882 "CQ record array\n");
6883 goto out_error;
6884 }
6885
6886 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
6887 phba->cfg_fcp_io_channel), GFP_KERNEL);
6888 if (!phba->sli4_hba.fcp_wq) {
6889 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6890 "2578 Failed allocate memory for fast-path "
6891 "WQ record array\n");
6892 goto out_error;
6893 }
James Smartda0436e2009-05-22 14:51:39 -04006894
James Smart5350d872011-10-10 21:33:49 -04006895 /*
James Smart67d12732012-08-03 12:36:13 -04006896 * Since the first EQ can have multiple CQs associated with it,
6897 * this array is used to quickly see if we have a FCP fast-path
6898 * CQ match.
James Smart5350d872011-10-10 21:33:49 -04006899 */
James Smart67d12732012-08-03 12:36:13 -04006900 phba->sli4_hba.fcp_cq_map = kzalloc((sizeof(uint16_t) *
6901 phba->cfg_fcp_io_channel), GFP_KERNEL);
6902 if (!phba->sli4_hba.fcp_cq_map) {
6903 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6904 "2545 Failed allocate memory for fast-path "
6905 "CQ map\n");
6906 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04006907 }
James Smart67d12732012-08-03 12:36:13 -04006908
6909 /*
6910 * Create HBA Event Queues (EQs). The cfg_fcp_io_channel specifies
6911 * how many EQs to create.
6912 */
6913 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
6914
6915 /* Create EQs */
James Smartda0436e2009-05-22 14:51:39 -04006916 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
6917 phba->sli4_hba.eq_ecount);
6918 if (!qdesc) {
6919 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04006920 "0497 Failed allocate EQ (%d)\n", idx);
6921 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04006922 }
James Smart67d12732012-08-03 12:36:13 -04006923 phba->sli4_hba.hba_eq[idx] = qdesc;
6924
6925 /* Create Fast Path FCP CQs */
6926 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6927 phba->sli4_hba.cq_ecount);
6928 if (!qdesc) {
6929 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6930 "0499 Failed allocate fast-path FCP "
6931 "CQ (%d)\n", idx);
6932 goto out_error;
6933 }
6934 phba->sli4_hba.fcp_cq[idx] = qdesc;
6935
6936 /* Create Fast Path FCP WQs */
6937 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
6938 phba->sli4_hba.wq_ecount);
6939 if (!qdesc) {
6940 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6941 "0503 Failed allocate fast-path FCP "
6942 "WQ (%d)\n", idx);
6943 goto out_error;
6944 }
6945 phba->sli4_hba.fcp_wq[idx] = qdesc;
James Smartda0436e2009-05-22 14:51:39 -04006946 }
6947
James Smart67d12732012-08-03 12:36:13 -04006948
James Smartda0436e2009-05-22 14:51:39 -04006949 /*
James Smart67d12732012-08-03 12:36:13 -04006950 * Create Slow Path Completion Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04006951 */
6952
James Smartda0436e2009-05-22 14:51:39 -04006953 /* Create slow-path Mailbox Command Complete Queue */
6954 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6955 phba->sli4_hba.cq_ecount);
6956 if (!qdesc) {
6957 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6958 "0500 Failed allocate slow-path mailbox CQ\n");
James Smart67d12732012-08-03 12:36:13 -04006959 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04006960 }
6961 phba->sli4_hba.mbx_cq = qdesc;
6962
6963 /* Create slow-path ELS Complete Queue */
6964 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6965 phba->sli4_hba.cq_ecount);
6966 if (!qdesc) {
6967 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6968 "0501 Failed allocate slow-path ELS CQ\n");
James Smart67d12732012-08-03 12:36:13 -04006969 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04006970 }
6971 phba->sli4_hba.els_cq = qdesc;
6972
James Smartda0436e2009-05-22 14:51:39 -04006973
James Smart5350d872011-10-10 21:33:49 -04006974 /*
James Smart67d12732012-08-03 12:36:13 -04006975 * Create Slow Path Work Queues (WQs)
James Smart5350d872011-10-10 21:33:49 -04006976 */
James Smartda0436e2009-05-22 14:51:39 -04006977
6978 /* Create Mailbox Command Queue */
James Smartda0436e2009-05-22 14:51:39 -04006979
6980 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
6981 phba->sli4_hba.mq_ecount);
6982 if (!qdesc) {
6983 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6984 "0505 Failed allocate slow-path MQ\n");
James Smart67d12732012-08-03 12:36:13 -04006985 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04006986 }
6987 phba->sli4_hba.mbx_wq = qdesc;
6988
6989 /*
James Smart67d12732012-08-03 12:36:13 -04006990 * Create ELS Work Queues
James Smartda0436e2009-05-22 14:51:39 -04006991 */
James Smartda0436e2009-05-22 14:51:39 -04006992
6993 /* Create slow-path ELS Work Queue */
6994 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
6995 phba->sli4_hba.wq_ecount);
6996 if (!qdesc) {
6997 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6998 "0504 Failed allocate slow-path ELS WQ\n");
James Smart67d12732012-08-03 12:36:13 -04006999 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007000 }
7001 phba->sli4_hba.els_wq = qdesc;
7002
James Smartda0436e2009-05-22 14:51:39 -04007003 /*
7004 * Create Receive Queue (RQ)
7005 */
James Smartda0436e2009-05-22 14:51:39 -04007006
7007 /* Create Receive Queue for header */
7008 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
7009 phba->sli4_hba.rq_ecount);
7010 if (!qdesc) {
7011 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7012 "0506 Failed allocate receive HRQ\n");
James Smart67d12732012-08-03 12:36:13 -04007013 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007014 }
7015 phba->sli4_hba.hdr_rq = qdesc;
7016
7017 /* Create Receive Queue for data */
7018 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
7019 phba->sli4_hba.rq_ecount);
7020 if (!qdesc) {
7021 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7022 "0507 Failed allocate receive DRQ\n");
James Smart67d12732012-08-03 12:36:13 -04007023 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007024 }
7025 phba->sli4_hba.dat_rq = qdesc;
7026
7027 return 0;
7028
James Smartda0436e2009-05-22 14:51:39 -04007029out_error:
James Smart67d12732012-08-03 12:36:13 -04007030 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007031 return -ENOMEM;
7032}
7033
7034/**
7035 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
7036 * @phba: pointer to lpfc hba data structure.
7037 *
7038 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
7039 * operation.
7040 *
7041 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007042 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007043 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007044 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007045 **/
James Smart5350d872011-10-10 21:33:49 -04007046void
James Smartda0436e2009-05-22 14:51:39 -04007047lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
7048{
James Smart67d12732012-08-03 12:36:13 -04007049 int idx;
7050
7051 if (phba->sli4_hba.hba_eq != NULL) {
7052 /* Release HBA event queue */
7053 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7054 if (phba->sli4_hba.hba_eq[idx] != NULL) {
7055 lpfc_sli4_queue_free(
7056 phba->sli4_hba.hba_eq[idx]);
7057 phba->sli4_hba.hba_eq[idx] = NULL;
7058 }
7059 }
7060 kfree(phba->sli4_hba.hba_eq);
7061 phba->sli4_hba.hba_eq = NULL;
7062 }
7063
7064 if (phba->sli4_hba.fcp_cq != NULL) {
7065 /* Release FCP completion queue */
7066 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7067 if (phba->sli4_hba.fcp_cq[idx] != NULL) {
7068 lpfc_sli4_queue_free(
7069 phba->sli4_hba.fcp_cq[idx]);
7070 phba->sli4_hba.fcp_cq[idx] = NULL;
7071 }
7072 }
7073 kfree(phba->sli4_hba.fcp_cq);
7074 phba->sli4_hba.fcp_cq = NULL;
7075 }
7076
7077 if (phba->sli4_hba.fcp_wq != NULL) {
7078 /* Release FCP work queue */
7079 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7080 if (phba->sli4_hba.fcp_wq[idx] != NULL) {
7081 lpfc_sli4_queue_free(
7082 phba->sli4_hba.fcp_wq[idx]);
7083 phba->sli4_hba.fcp_wq[idx] = NULL;
7084 }
7085 }
7086 kfree(phba->sli4_hba.fcp_wq);
7087 phba->sli4_hba.fcp_wq = NULL;
7088 }
7089
James Smart962bc512013-01-03 15:44:00 -05007090 if (phba->pci_bar0_memmap_p) {
7091 iounmap(phba->pci_bar0_memmap_p);
7092 phba->pci_bar0_memmap_p = NULL;
7093 }
7094 if (phba->pci_bar2_memmap_p) {
7095 iounmap(phba->pci_bar2_memmap_p);
7096 phba->pci_bar2_memmap_p = NULL;
7097 }
7098 if (phba->pci_bar4_memmap_p) {
7099 iounmap(phba->pci_bar4_memmap_p);
7100 phba->pci_bar4_memmap_p = NULL;
7101 }
7102
James Smart67d12732012-08-03 12:36:13 -04007103 /* Release FCP CQ mapping array */
7104 if (phba->sli4_hba.fcp_cq_map != NULL) {
7105 kfree(phba->sli4_hba.fcp_cq_map);
7106 phba->sli4_hba.fcp_cq_map = NULL;
7107 }
James Smartda0436e2009-05-22 14:51:39 -04007108
7109 /* Release mailbox command work queue */
James Smart67d12732012-08-03 12:36:13 -04007110 if (phba->sli4_hba.mbx_wq != NULL) {
7111 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
7112 phba->sli4_hba.mbx_wq = NULL;
7113 }
James Smartda0436e2009-05-22 14:51:39 -04007114
7115 /* Release ELS work queue */
James Smart67d12732012-08-03 12:36:13 -04007116 if (phba->sli4_hba.els_wq != NULL) {
7117 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
7118 phba->sli4_hba.els_wq = NULL;
7119 }
James Smartda0436e2009-05-22 14:51:39 -04007120
7121 /* Release unsolicited receive queue */
James Smart67d12732012-08-03 12:36:13 -04007122 if (phba->sli4_hba.hdr_rq != NULL) {
7123 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
7124 phba->sli4_hba.hdr_rq = NULL;
7125 }
7126 if (phba->sli4_hba.dat_rq != NULL) {
7127 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
7128 phba->sli4_hba.dat_rq = NULL;
7129 }
James Smartda0436e2009-05-22 14:51:39 -04007130
James Smartda0436e2009-05-22 14:51:39 -04007131 /* Release ELS complete queue */
James Smart67d12732012-08-03 12:36:13 -04007132 if (phba->sli4_hba.els_cq != NULL) {
7133 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
7134 phba->sli4_hba.els_cq = NULL;
7135 }
James Smartda0436e2009-05-22 14:51:39 -04007136
7137 /* Release mailbox command complete queue */
James Smart67d12732012-08-03 12:36:13 -04007138 if (phba->sli4_hba.mbx_cq != NULL) {
7139 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
7140 phba->sli4_hba.mbx_cq = NULL;
7141 }
James Smartda0436e2009-05-22 14:51:39 -04007142
7143 return;
7144}
7145
7146/**
7147 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
7148 * @phba: pointer to lpfc hba data structure.
7149 *
7150 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
7151 * operation.
7152 *
7153 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007154 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007155 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007156 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007157 **/
7158int
7159lpfc_sli4_queue_setup(struct lpfc_hba *phba)
7160{
James Smart2a76a282012-08-03 12:35:54 -04007161 struct lpfc_sli *psli = &phba->sli;
7162 struct lpfc_sli_ring *pring;
James Smartda0436e2009-05-22 14:51:39 -04007163 int rc = -ENOMEM;
7164 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
7165 int fcp_cq_index = 0;
James Smart962bc512013-01-03 15:44:00 -05007166 uint32_t shdr_status, shdr_add_status;
7167 union lpfc_sli4_cfg_shdr *shdr;
7168 LPFC_MBOXQ_t *mboxq;
7169 uint32_t length;
7170
7171 /* Check for dual-ULP support */
7172 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7173 if (!mboxq) {
7174 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7175 "3249 Unable to allocate memory for "
7176 "QUERY_FW_CFG mailbox command\n");
7177 return -ENOMEM;
7178 }
7179 length = (sizeof(struct lpfc_mbx_query_fw_config) -
7180 sizeof(struct lpfc_sli4_cfg_mhdr));
7181 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7182 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
7183 length, LPFC_SLI4_MBX_EMBED);
7184
7185 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7186
7187 shdr = (union lpfc_sli4_cfg_shdr *)
7188 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7189 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7190 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7191 if (shdr_status || shdr_add_status || rc) {
7192 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7193 "3250 QUERY_FW_CFG mailbox failed with status "
7194 "x%x add_status x%x, mbx status x%x\n",
7195 shdr_status, shdr_add_status, rc);
7196 if (rc != MBX_TIMEOUT)
7197 mempool_free(mboxq, phba->mbox_mem_pool);
7198 rc = -ENXIO;
7199 goto out_error;
7200 }
7201
7202 phba->sli4_hba.fw_func_mode =
7203 mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
7204 phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
7205 phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
7206 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7207 "3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
7208 "ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
7209 phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
7210
7211 if (rc != MBX_TIMEOUT)
7212 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04007213
7214 /*
James Smart67d12732012-08-03 12:36:13 -04007215 * Set up HBA Event Queues (EQs)
James Smartda0436e2009-05-22 14:51:39 -04007216 */
7217
James Smart67d12732012-08-03 12:36:13 -04007218 /* Set up HBA event queue */
7219 if (phba->cfg_fcp_io_channel && !phba->sli4_hba.hba_eq) {
James Smart2e90f4b2011-12-13 13:22:37 -05007220 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7221 "3147 Fast-path EQs not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007222 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007223 goto out_error;
James Smart2e90f4b2011-12-13 13:22:37 -05007224 }
James Smart67d12732012-08-03 12:36:13 -04007225 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
7226 if (!phba->sli4_hba.hba_eq[fcp_eqidx]) {
James Smartda0436e2009-05-22 14:51:39 -04007227 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7228 "0522 Fast-path EQ (%d) not "
7229 "allocated\n", fcp_eqidx);
James Smart1b511972011-12-13 13:23:09 -05007230 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007231 goto out_destroy_hba_eq;
James Smartda0436e2009-05-22 14:51:39 -04007232 }
James Smart67d12732012-08-03 12:36:13 -04007233 rc = lpfc_eq_create(phba, phba->sli4_hba.hba_eq[fcp_eqidx],
James Smartbf8dae82012-08-03 12:36:24 -04007234 (phba->cfg_fcp_imax / phba->cfg_fcp_io_channel));
James Smartda0436e2009-05-22 14:51:39 -04007235 if (rc) {
7236 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7237 "0523 Failed setup of fast-path EQ "
7238 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
James Smart67d12732012-08-03 12:36:13 -04007239 goto out_destroy_hba_eq;
James Smartda0436e2009-05-22 14:51:39 -04007240 }
7241 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04007242 "2584 HBA EQ setup: "
James Smartda0436e2009-05-22 14:51:39 -04007243 "queue[%d]-id=%d\n", fcp_eqidx,
James Smart67d12732012-08-03 12:36:13 -04007244 phba->sli4_hba.hba_eq[fcp_eqidx]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007245 }
7246
James Smart67d12732012-08-03 12:36:13 -04007247 /* Set up fast-path FCP Response Complete Queue */
7248 if (!phba->sli4_hba.fcp_cq) {
7249 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7250 "3148 Fast-path FCP CQ array not "
7251 "allocated\n");
7252 rc = -ENOMEM;
7253 goto out_destroy_hba_eq;
7254 }
7255
7256 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_io_channel; fcp_cqidx++) {
7257 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
7258 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7259 "0526 Fast-path FCP CQ (%d) not "
7260 "allocated\n", fcp_cqidx);
7261 rc = -ENOMEM;
7262 goto out_destroy_fcp_cq;
7263 }
7264 rc = lpfc_cq_create(phba, phba->sli4_hba.fcp_cq[fcp_cqidx],
7265 phba->sli4_hba.hba_eq[fcp_cqidx], LPFC_WCQ, LPFC_FCP);
7266 if (rc) {
7267 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7268 "0527 Failed setup of fast-path FCP "
7269 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
7270 goto out_destroy_fcp_cq;
7271 }
7272
7273 /* Setup fcp_cq_map for fast lookup */
7274 phba->sli4_hba.fcp_cq_map[fcp_cqidx] =
7275 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id;
7276
7277 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7278 "2588 FCP CQ setup: cq[%d]-id=%d, "
7279 "parent seq[%d]-id=%d\n",
7280 fcp_cqidx,
7281 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
7282 fcp_cqidx,
7283 phba->sli4_hba.hba_eq[fcp_cqidx]->queue_id);
7284 }
7285
7286 /* Set up fast-path FCP Work Queue */
7287 if (!phba->sli4_hba.fcp_wq) {
7288 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7289 "3149 Fast-path FCP WQ array not "
7290 "allocated\n");
7291 rc = -ENOMEM;
7292 goto out_destroy_fcp_cq;
7293 }
7294
7295 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_io_channel; fcp_wqidx++) {
7296 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
7297 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7298 "0534 Fast-path FCP WQ (%d) not "
7299 "allocated\n", fcp_wqidx);
7300 rc = -ENOMEM;
7301 goto out_destroy_fcp_wq;
7302 }
7303 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
7304 phba->sli4_hba.fcp_cq[fcp_wqidx],
7305 LPFC_FCP);
7306 if (rc) {
7307 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7308 "0535 Failed setup of fast-path FCP "
7309 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
7310 goto out_destroy_fcp_wq;
7311 }
7312
7313 /* Bind this WQ to the next FCP ring */
7314 pring = &psli->ring[MAX_SLI3_CONFIGURED_RINGS + fcp_wqidx];
7315 pring->sli.sli4.wqp = (void *)phba->sli4_hba.fcp_wq[fcp_wqidx];
7316 phba->sli4_hba.fcp_cq[fcp_wqidx]->pring = pring;
7317
7318 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7319 "2591 FCP WQ setup: wq[%d]-id=%d, "
7320 "parent cq[%d]-id=%d\n",
7321 fcp_wqidx,
7322 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
7323 fcp_cq_index,
7324 phba->sli4_hba.fcp_cq[fcp_wqidx]->queue_id);
7325 }
James Smartda0436e2009-05-22 14:51:39 -04007326 /*
7327 * Set up Complete Queues (CQs)
7328 */
7329
7330 /* Set up slow-path MBOX Complete Queue as the first CQ */
7331 if (!phba->sli4_hba.mbx_cq) {
7332 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7333 "0528 Mailbox CQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007334 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007335 goto out_destroy_fcp_wq;
James Smartda0436e2009-05-22 14:51:39 -04007336 }
James Smart67d12732012-08-03 12:36:13 -04007337 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq,
7338 phba->sli4_hba.hba_eq[0], LPFC_MCQ, LPFC_MBOX);
James Smartda0436e2009-05-22 14:51:39 -04007339 if (rc) {
7340 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7341 "0529 Failed setup of slow-path mailbox CQ: "
7342 "rc = 0x%x\n", rc);
James Smart67d12732012-08-03 12:36:13 -04007343 goto out_destroy_fcp_wq;
James Smartda0436e2009-05-22 14:51:39 -04007344 }
7345 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7346 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
7347 phba->sli4_hba.mbx_cq->queue_id,
James Smart67d12732012-08-03 12:36:13 -04007348 phba->sli4_hba.hba_eq[0]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007349
7350 /* Set up slow-path ELS Complete Queue */
7351 if (!phba->sli4_hba.els_cq) {
7352 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7353 "0530 ELS CQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007354 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04007355 goto out_destroy_mbx_cq;
7356 }
James Smart67d12732012-08-03 12:36:13 -04007357 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq,
7358 phba->sli4_hba.hba_eq[0], LPFC_WCQ, LPFC_ELS);
James Smartda0436e2009-05-22 14:51:39 -04007359 if (rc) {
7360 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7361 "0531 Failed setup of slow-path ELS CQ: "
7362 "rc = 0x%x\n", rc);
7363 goto out_destroy_mbx_cq;
7364 }
7365 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7366 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
7367 phba->sli4_hba.els_cq->queue_id,
James Smart67d12732012-08-03 12:36:13 -04007368 phba->sli4_hba.hba_eq[0]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007369
7370 /*
7371 * Set up all the Work Queues (WQs)
7372 */
7373
7374 /* Set up Mailbox Command Queue */
7375 if (!phba->sli4_hba.mbx_wq) {
7376 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7377 "0538 Slow-path MQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007378 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007379 goto out_destroy_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04007380 }
7381 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
7382 phba->sli4_hba.mbx_cq, LPFC_MBOX);
7383 if (rc) {
7384 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7385 "0539 Failed setup of slow-path MQ: "
7386 "rc = 0x%x\n", rc);
James Smart67d12732012-08-03 12:36:13 -04007387 goto out_destroy_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04007388 }
7389 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7390 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
7391 phba->sli4_hba.mbx_wq->queue_id,
7392 phba->sli4_hba.mbx_cq->queue_id);
7393
7394 /* Set up slow-path ELS Work Queue */
7395 if (!phba->sli4_hba.els_wq) {
7396 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7397 "0536 Slow-path ELS WQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007398 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04007399 goto out_destroy_mbx_wq;
7400 }
7401 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
7402 phba->sli4_hba.els_cq, LPFC_ELS);
7403 if (rc) {
7404 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7405 "0537 Failed setup of slow-path ELS WQ: "
7406 "rc = 0x%x\n", rc);
7407 goto out_destroy_mbx_wq;
7408 }
James Smart2a76a282012-08-03 12:35:54 -04007409
7410 /* Bind this WQ to the ELS ring */
7411 pring = &psli->ring[LPFC_ELS_RING];
7412 pring->sli.sli4.wqp = (void *)phba->sli4_hba.els_wq;
7413 phba->sli4_hba.els_cq->pring = pring;
7414
James Smartda0436e2009-05-22 14:51:39 -04007415 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7416 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
7417 phba->sli4_hba.els_wq->queue_id,
7418 phba->sli4_hba.els_cq->queue_id);
7419
James Smartda0436e2009-05-22 14:51:39 -04007420 /*
7421 * Create Receive Queue (RQ)
7422 */
7423 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
7424 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7425 "0540 Receive Queue not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007426 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007427 goto out_destroy_els_wq;
James Smartda0436e2009-05-22 14:51:39 -04007428 }
James Smart73d91e52011-10-10 21:32:10 -04007429
7430 lpfc_rq_adjust_repost(phba, phba->sli4_hba.hdr_rq, LPFC_ELS_HBQ);
7431 lpfc_rq_adjust_repost(phba, phba->sli4_hba.dat_rq, LPFC_ELS_HBQ);
7432
James Smartda0436e2009-05-22 14:51:39 -04007433 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04007434 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04007435 if (rc) {
7436 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7437 "0541 Failed setup of Receive Queue: "
7438 "rc = 0x%x\n", rc);
7439 goto out_destroy_fcp_wq;
7440 }
James Smart73d91e52011-10-10 21:32:10 -04007441
James Smartda0436e2009-05-22 14:51:39 -04007442 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7443 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
7444 "parent cq-id=%d\n",
7445 phba->sli4_hba.hdr_rq->queue_id,
7446 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04007447 phba->sli4_hba.els_cq->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007448 return 0;
7449
James Smart2e90f4b2011-12-13 13:22:37 -05007450out_destroy_els_wq:
James Smartda0436e2009-05-22 14:51:39 -04007451 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7452out_destroy_mbx_wq:
7453 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
James Smart2e90f4b2011-12-13 13:22:37 -05007454out_destroy_els_cq:
James Smartda0436e2009-05-22 14:51:39 -04007455 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
7456out_destroy_mbx_cq:
7457 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
James Smart67d12732012-08-03 12:36:13 -04007458out_destroy_fcp_wq:
7459 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
7460 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
7461out_destroy_fcp_cq:
7462 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
7463 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
7464out_destroy_hba_eq:
James Smartda0436e2009-05-22 14:51:39 -04007465 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
James Smart67d12732012-08-03 12:36:13 -04007466 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[fcp_eqidx]);
James Smartda0436e2009-05-22 14:51:39 -04007467out_error:
7468 return rc;
7469}
7470
7471/**
7472 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
7473 * @phba: pointer to lpfc hba data structure.
7474 *
7475 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
7476 * operation.
7477 *
7478 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007479 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007480 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007481 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007482 **/
7483void
7484lpfc_sli4_queue_unset(struct lpfc_hba *phba)
7485{
7486 int fcp_qidx;
7487
7488 /* Unset mailbox command work queue */
7489 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
7490 /* Unset ELS work queue */
7491 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7492 /* Unset unsolicited receive queue */
7493 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
7494 /* Unset FCP work queue */
James Smart67d12732012-08-03 12:36:13 -04007495 if (phba->sli4_hba.fcp_wq) {
7496 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
7497 fcp_qidx++)
7498 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
7499 }
James Smartda0436e2009-05-22 14:51:39 -04007500 /* Unset mailbox command complete queue */
7501 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
7502 /* Unset ELS complete queue */
7503 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
James Smartda0436e2009-05-22 14:51:39 -04007504 /* Unset FCP response complete queue */
James Smart2e90f4b2011-12-13 13:22:37 -05007505 if (phba->sli4_hba.fcp_cq) {
James Smart67d12732012-08-03 12:36:13 -04007506 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
7507 fcp_qidx++)
James Smart2e90f4b2011-12-13 13:22:37 -05007508 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05007509 }
James Smartda0436e2009-05-22 14:51:39 -04007510 /* Unset fast-path event queue */
James Smart67d12732012-08-03 12:36:13 -04007511 if (phba->sli4_hba.hba_eq) {
7512 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
James Smart2e90f4b2011-12-13 13:22:37 -05007513 fcp_qidx++)
James Smart67d12732012-08-03 12:36:13 -04007514 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[fcp_qidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05007515 }
James Smartda0436e2009-05-22 14:51:39 -04007516}
7517
7518/**
7519 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
7520 * @phba: pointer to lpfc hba data structure.
7521 *
7522 * This routine is invoked to allocate and set up a pool of completion queue
7523 * events. The body of the completion queue event is a completion queue entry
7524 * CQE. For now, this pool is used for the interrupt service routine to queue
7525 * the following HBA completion queue events for the worker thread to process:
7526 * - Mailbox asynchronous events
7527 * - Receive queue completion unsolicited events
7528 * Later, this can be used for all the slow-path events.
7529 *
7530 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007531 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007532 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04007533 **/
7534static int
7535lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
7536{
7537 struct lpfc_cq_event *cq_event;
7538 int i;
7539
7540 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
7541 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
7542 if (!cq_event)
7543 goto out_pool_create_fail;
7544 list_add_tail(&cq_event->list,
7545 &phba->sli4_hba.sp_cqe_event_pool);
7546 }
7547 return 0;
7548
7549out_pool_create_fail:
7550 lpfc_sli4_cq_event_pool_destroy(phba);
7551 return -ENOMEM;
7552}
7553
7554/**
7555 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
7556 * @phba: pointer to lpfc hba data structure.
7557 *
7558 * This routine is invoked to free the pool of completion queue events at
7559 * driver unload time. Note that, it is the responsibility of the driver
7560 * cleanup routine to free all the outstanding completion-queue events
7561 * allocated from this pool back into the pool before invoking this routine
7562 * to destroy the pool.
7563 **/
7564static void
7565lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
7566{
7567 struct lpfc_cq_event *cq_event, *next_cq_event;
7568
7569 list_for_each_entry_safe(cq_event, next_cq_event,
7570 &phba->sli4_hba.sp_cqe_event_pool, list) {
7571 list_del(&cq_event->list);
7572 kfree(cq_event);
7573 }
7574}
7575
7576/**
7577 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
7578 * @phba: pointer to lpfc hba data structure.
7579 *
7580 * This routine is the lock free version of the API invoked to allocate a
7581 * completion-queue event from the free pool.
7582 *
7583 * Return: Pointer to the newly allocated completion-queue event if successful
7584 * NULL otherwise.
7585 **/
7586struct lpfc_cq_event *
7587__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
7588{
7589 struct lpfc_cq_event *cq_event = NULL;
7590
7591 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
7592 struct lpfc_cq_event, list);
7593 return cq_event;
7594}
7595
7596/**
7597 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
7598 * @phba: pointer to lpfc hba data structure.
7599 *
7600 * This routine is the lock version of the API invoked to allocate a
7601 * completion-queue event from the free pool.
7602 *
7603 * Return: Pointer to the newly allocated completion-queue event if successful
7604 * NULL otherwise.
7605 **/
7606struct lpfc_cq_event *
7607lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
7608{
7609 struct lpfc_cq_event *cq_event;
7610 unsigned long iflags;
7611
7612 spin_lock_irqsave(&phba->hbalock, iflags);
7613 cq_event = __lpfc_sli4_cq_event_alloc(phba);
7614 spin_unlock_irqrestore(&phba->hbalock, iflags);
7615 return cq_event;
7616}
7617
7618/**
7619 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
7620 * @phba: pointer to lpfc hba data structure.
7621 * @cq_event: pointer to the completion queue event to be freed.
7622 *
7623 * This routine is the lock free version of the API invoked to release a
7624 * completion-queue event back into the free pool.
7625 **/
7626void
7627__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
7628 struct lpfc_cq_event *cq_event)
7629{
7630 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
7631}
7632
7633/**
7634 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
7635 * @phba: pointer to lpfc hba data structure.
7636 * @cq_event: pointer to the completion queue event to be freed.
7637 *
7638 * This routine is the lock version of the API invoked to release a
7639 * completion-queue event back into the free pool.
7640 **/
7641void
7642lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
7643 struct lpfc_cq_event *cq_event)
7644{
7645 unsigned long iflags;
7646 spin_lock_irqsave(&phba->hbalock, iflags);
7647 __lpfc_sli4_cq_event_release(phba, cq_event);
7648 spin_unlock_irqrestore(&phba->hbalock, iflags);
7649}
7650
7651/**
7652 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
7653 * @phba: pointer to lpfc hba data structure.
7654 *
7655 * This routine is to free all the pending completion-queue events to the
7656 * back into the free pool for device reset.
7657 **/
7658static void
7659lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
7660{
7661 LIST_HEAD(cqelist);
7662 struct lpfc_cq_event *cqe;
7663 unsigned long iflags;
7664
7665 /* Retrieve all the pending WCQEs from pending WCQE lists */
7666 spin_lock_irqsave(&phba->hbalock, iflags);
7667 /* Pending FCP XRI abort events */
7668 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
7669 &cqelist);
7670 /* Pending ELS XRI abort events */
7671 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
7672 &cqelist);
7673 /* Pending asynnc events */
7674 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
7675 &cqelist);
7676 spin_unlock_irqrestore(&phba->hbalock, iflags);
7677
7678 while (!list_empty(&cqelist)) {
7679 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
7680 lpfc_sli4_cq_event_release(phba, cqe);
7681 }
7682}
7683
7684/**
7685 * lpfc_pci_function_reset - Reset pci function.
7686 * @phba: pointer to lpfc hba data structure.
7687 *
7688 * This routine is invoked to request a PCI function reset. It will destroys
7689 * all resources assigned to the PCI function which originates this request.
7690 *
7691 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007692 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007693 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007694 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007695 **/
7696int
7697lpfc_pci_function_reset(struct lpfc_hba *phba)
7698{
7699 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05007700 uint32_t rc = 0, if_type;
James Smartda0436e2009-05-22 14:51:39 -04007701 uint32_t shdr_status, shdr_add_status;
James Smart2fcee4b2010-12-15 17:57:46 -05007702 uint32_t rdy_chk, num_resets = 0, reset_again = 0;
James Smartda0436e2009-05-22 14:51:39 -04007703 union lpfc_sli4_cfg_shdr *shdr;
James Smart2fcee4b2010-12-15 17:57:46 -05007704 struct lpfc_register reg_data;
James Smart2b81f942012-03-01 22:37:18 -05007705 uint16_t devid;
James Smartda0436e2009-05-22 14:51:39 -04007706
James Smart2fcee4b2010-12-15 17:57:46 -05007707 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7708 switch (if_type) {
7709 case LPFC_SLI_INTF_IF_TYPE_0:
7710 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
7711 GFP_KERNEL);
7712 if (!mboxq) {
7713 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7714 "0494 Unable to allocate memory for "
7715 "issuing SLI_FUNCTION_RESET mailbox "
7716 "command\n");
7717 return -ENOMEM;
7718 }
7719
7720 /* Setup PCI function reset mailbox-ioctl command */
7721 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7722 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
7723 LPFC_SLI4_MBX_EMBED);
7724 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7725 shdr = (union lpfc_sli4_cfg_shdr *)
7726 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7727 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7728 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
7729 &shdr->response);
7730 if (rc != MBX_TIMEOUT)
7731 mempool_free(mboxq, phba->mbox_mem_pool);
7732 if (shdr_status || shdr_add_status || rc) {
7733 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7734 "0495 SLI_FUNCTION_RESET mailbox "
7735 "failed with status x%x add_status x%x,"
7736 " mbx status x%x\n",
7737 shdr_status, shdr_add_status, rc);
7738 rc = -ENXIO;
7739 }
7740 break;
7741 case LPFC_SLI_INTF_IF_TYPE_2:
7742 for (num_resets = 0;
7743 num_resets < MAX_IF_TYPE_2_RESETS;
7744 num_resets++) {
7745 reg_data.word0 = 0;
7746 bf_set(lpfc_sliport_ctrl_end, &reg_data,
7747 LPFC_SLIPORT_LITTLE_ENDIAN);
7748 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
7749 LPFC_SLIPORT_INIT_PORT);
7750 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
7751 CTRLregaddr);
James Smart8fcb8ac2012-03-01 22:35:58 -05007752 /* flush */
James Smart2b81f942012-03-01 22:37:18 -05007753 pci_read_config_word(phba->pcidev,
7754 PCI_DEVICE_ID, &devid);
James Smart2fcee4b2010-12-15 17:57:46 -05007755 /*
7756 * Poll the Port Status Register and wait for RDY for
7757 * up to 10 seconds. If the port doesn't respond, treat
7758 * it as an error. If the port responds with RN, start
7759 * the loop again.
7760 */
7761 for (rdy_chk = 0; rdy_chk < 1000; rdy_chk++) {
James Smart73d91e52011-10-10 21:32:10 -04007762 msleep(10);
James Smart9940b972011-03-11 16:06:12 -05007763 if (lpfc_readl(phba->sli4_hba.u.if_type2.
7764 STATUSregaddr, &reg_data.word0)) {
7765 rc = -ENODEV;
James Smart73d91e52011-10-10 21:32:10 -04007766 goto out;
James Smart9940b972011-03-11 16:06:12 -05007767 }
James Smart6b5151f2012-01-18 16:24:06 -05007768 if (bf_get(lpfc_sliport_status_rn, &reg_data))
7769 reset_again++;
James Smart2fcee4b2010-12-15 17:57:46 -05007770 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
7771 break;
James Smart2fcee4b2010-12-15 17:57:46 -05007772 }
7773
7774 /*
7775 * If the port responds to the init request with
7776 * reset needed, delay for a bit and restart the loop.
7777 */
James Smart6b5151f2012-01-18 16:24:06 -05007778 if (reset_again && (rdy_chk < 1000)) {
James Smart2fcee4b2010-12-15 17:57:46 -05007779 msleep(10);
7780 reset_again = 0;
7781 continue;
7782 }
7783
7784 /* Detect any port errors. */
James Smart2fcee4b2010-12-15 17:57:46 -05007785 if ((bf_get(lpfc_sliport_status_err, &reg_data)) ||
7786 (rdy_chk >= 1000)) {
7787 phba->work_status[0] = readl(
7788 phba->sli4_hba.u.if_type2.ERR1regaddr);
7789 phba->work_status[1] = readl(
7790 phba->sli4_hba.u.if_type2.ERR2regaddr);
7791 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05007792 "2890 Port error detected during port "
James Smart8a9d2e82012-05-09 21:16:12 -04007793 "reset(%d): wait_tmo:%d ms, "
7794 "port status reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05007795 "error 1=0x%x, error 2=0x%x\n",
James Smart8a9d2e82012-05-09 21:16:12 -04007796 num_resets, rdy_chk*10,
7797 reg_data.word0,
James Smart2fcee4b2010-12-15 17:57:46 -05007798 phba->work_status[0],
7799 phba->work_status[1]);
7800 rc = -ENODEV;
7801 }
7802
7803 /*
7804 * Terminate the outer loop provided the Port indicated
7805 * ready within 10 seconds.
7806 */
7807 if (rdy_chk < 1000)
7808 break;
7809 }
James Smart05580562011-05-24 11:40:48 -04007810 /* delay driver action following IF_TYPE_2 function reset */
7811 msleep(100);
James Smart2fcee4b2010-12-15 17:57:46 -05007812 break;
7813 case LPFC_SLI_INTF_IF_TYPE_1:
7814 default:
7815 break;
James Smartda0436e2009-05-22 14:51:39 -04007816 }
7817
James Smart73d91e52011-10-10 21:32:10 -04007818out:
James Smart2fcee4b2010-12-15 17:57:46 -05007819 /* Catch the not-ready port failure after a port reset. */
James Smart229adb02013-04-17 20:16:51 -04007820 if (num_resets >= MAX_IF_TYPE_2_RESETS) {
7821 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7822 "3317 HBA not functional: IP Reset Failed "
7823 "after (%d) retries, try: "
7824 "echo fw_reset > board_mode\n", num_resets);
James Smart2fcee4b2010-12-15 17:57:46 -05007825 rc = -ENODEV;
James Smart229adb02013-04-17 20:16:51 -04007826 }
James Smart2fcee4b2010-12-15 17:57:46 -05007827
James Smartda0436e2009-05-22 14:51:39 -04007828 return rc;
7829}
7830
7831/**
James Smartda0436e2009-05-22 14:51:39 -04007832 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
7833 * @phba: pointer to lpfc hba data structure.
7834 *
7835 * This routine is invoked to set up the PCI device memory space for device
7836 * with SLI-4 interface spec.
7837 *
7838 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007839 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007840 * other values - error
7841 **/
7842static int
7843lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
7844{
7845 struct pci_dev *pdev;
7846 unsigned long bar0map_len, bar1map_len, bar2map_len;
7847 int error = -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -05007848 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04007849
7850 /* Obtain PCI device reference */
7851 if (!phba->pcidev)
7852 return error;
7853 else
7854 pdev = phba->pcidev;
7855
7856 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05007857 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
7858 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
7859 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
7860 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smartda0436e2009-05-22 14:51:39 -04007861 return error;
Michael Reed8e685972009-09-18 12:02:05 -05007862 }
7863 }
James Smartda0436e2009-05-22 14:51:39 -04007864
James Smart2fcee4b2010-12-15 17:57:46 -05007865 /*
7866 * The BARs and register set definitions and offset locations are
7867 * dependent on the if_type.
7868 */
7869 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
7870 &phba->sli4_hba.sli_intf.word0)) {
7871 return error;
7872 }
7873
7874 /* There is no SLI3 failback for SLI4 devices. */
7875 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
7876 LPFC_SLI_INTF_VALID) {
7877 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7878 "2894 SLI_INTF reg contents invalid "
7879 "sli_intf reg 0x%x\n",
7880 phba->sli4_hba.sli_intf.word0);
7881 return error;
7882 }
7883
7884 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7885 /*
7886 * Get the bus address of SLI4 device Bar regions and the
7887 * number of bytes required by each mapping. The mapping of the
7888 * particular PCI BARs regions is dependent on the type of
7889 * SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -04007890 */
James Smart1dfb5a42010-02-12 14:40:50 -05007891 if (pci_resource_start(pdev, 0)) {
7892 phba->pci_bar0_map = pci_resource_start(pdev, 0);
7893 bar0map_len = pci_resource_len(pdev, 0);
James Smart2fcee4b2010-12-15 17:57:46 -05007894
7895 /*
7896 * Map SLI4 PCI Config Space Register base to a kernel virtual
7897 * addr
7898 */
7899 phba->sli4_hba.conf_regs_memmap_p =
7900 ioremap(phba->pci_bar0_map, bar0map_len);
7901 if (!phba->sli4_hba.conf_regs_memmap_p) {
7902 dev_printk(KERN_ERR, &pdev->dev,
7903 "ioremap failed for SLI4 PCI config "
7904 "registers.\n");
7905 goto out;
7906 }
7907 /* Set up BAR0 PCI config space register memory map */
7908 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smart1dfb5a42010-02-12 14:40:50 -05007909 } else {
7910 phba->pci_bar0_map = pci_resource_start(pdev, 1);
7911 bar0map_len = pci_resource_len(pdev, 1);
James Smart2fcee4b2010-12-15 17:57:46 -05007912 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
7913 dev_printk(KERN_ERR, &pdev->dev,
7914 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
7915 goto out;
7916 }
7917 phba->sli4_hba.conf_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04007918 ioremap(phba->pci_bar0_map, bar0map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05007919 if (!phba->sli4_hba.conf_regs_memmap_p) {
7920 dev_printk(KERN_ERR, &pdev->dev,
7921 "ioremap failed for SLI4 PCI config "
7922 "registers.\n");
7923 goto out;
7924 }
7925 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smartda0436e2009-05-22 14:51:39 -04007926 }
7927
James Smartc31098c2011-04-16 11:03:33 -04007928 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
7929 (pci_resource_start(pdev, 2))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007930 /*
7931 * Map SLI4 if type 0 HBA Control Register base to a kernel
7932 * virtual address and setup the registers.
7933 */
7934 phba->pci_bar1_map = pci_resource_start(pdev, 2);
7935 bar1map_len = pci_resource_len(pdev, 2);
7936 phba->sli4_hba.ctrl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04007937 ioremap(phba->pci_bar1_map, bar1map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05007938 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
7939 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04007940 "ioremap failed for SLI4 HBA control registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05007941 goto out_iounmap_conf;
7942 }
7943 lpfc_sli4_bar1_register_memmap(phba);
James Smartda0436e2009-05-22 14:51:39 -04007944 }
7945
James Smartc31098c2011-04-16 11:03:33 -04007946 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
7947 (pci_resource_start(pdev, 4))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007948 /*
7949 * Map SLI4 if type 0 HBA Doorbell Register base to a kernel
7950 * virtual address and setup the registers.
7951 */
7952 phba->pci_bar2_map = pci_resource_start(pdev, 4);
7953 bar2map_len = pci_resource_len(pdev, 4);
7954 phba->sli4_hba.drbl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04007955 ioremap(phba->pci_bar2_map, bar2map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05007956 if (!phba->sli4_hba.drbl_regs_memmap_p) {
7957 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04007958 "ioremap failed for SLI4 HBA doorbell registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05007959 goto out_iounmap_ctrl;
7960 }
7961 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
7962 if (error)
7963 goto out_iounmap_all;
James Smartda0436e2009-05-22 14:51:39 -04007964 }
7965
James Smartda0436e2009-05-22 14:51:39 -04007966 return 0;
7967
7968out_iounmap_all:
7969 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
7970out_iounmap_ctrl:
7971 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
7972out_iounmap_conf:
7973 iounmap(phba->sli4_hba.conf_regs_memmap_p);
7974out:
7975 return error;
7976}
7977
7978/**
7979 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
7980 * @phba: pointer to lpfc hba data structure.
7981 *
7982 * This routine is invoked to unset the PCI device memory space for device
7983 * with SLI-4 interface spec.
7984 **/
7985static void
7986lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
7987{
James Smart2e90f4b2011-12-13 13:22:37 -05007988 uint32_t if_type;
7989 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smartda0436e2009-05-22 14:51:39 -04007990
James Smart2e90f4b2011-12-13 13:22:37 -05007991 switch (if_type) {
7992 case LPFC_SLI_INTF_IF_TYPE_0:
7993 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
7994 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
7995 iounmap(phba->sli4_hba.conf_regs_memmap_p);
7996 break;
7997 case LPFC_SLI_INTF_IF_TYPE_2:
7998 iounmap(phba->sli4_hba.conf_regs_memmap_p);
7999 break;
8000 case LPFC_SLI_INTF_IF_TYPE_1:
8001 default:
8002 dev_printk(KERN_ERR, &phba->pcidev->dev,
8003 "FATAL - unsupported SLI4 interface type - %d\n",
8004 if_type);
8005 break;
8006 }
James Smartda0436e2009-05-22 14:51:39 -04008007}
8008
8009/**
James Smart3772a992009-05-22 14:50:54 -04008010 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
8011 * @phba: pointer to lpfc hba data structure.
8012 *
8013 * This routine is invoked to enable the MSI-X interrupt vectors to device
8014 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
8015 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
8016 * invoked, enables either all or nothing, depending on the current
8017 * availability of PCI vector resources. The device driver is responsible
8018 * for calling the individual request_irq() to register each MSI-X vector
8019 * with a interrupt handler, which is done in this function. Note that
8020 * later when device is unloading, the driver should always call free_irq()
8021 * on all MSI-X vectors it has done request_irq() on before calling
8022 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
8023 * will be left with MSI-X enabled and leaks its vectors.
8024 *
8025 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008026 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008027 * other values - error
8028 **/
8029static int
8030lpfc_sli_enable_msix(struct lpfc_hba *phba)
8031{
8032 int rc, i;
8033 LPFC_MBOXQ_t *pmb;
8034
8035 /* Set up MSI-X multi-message vectors */
8036 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8037 phba->msix_entries[i].entry = i;
8038
8039 /* Configure MSI-X capability structure */
8040 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
8041 ARRAY_SIZE(phba->msix_entries));
8042 if (rc) {
8043 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8044 "0420 PCI enable MSI-X failed (%d)\n", rc);
8045 goto msi_fail_out;
8046 }
8047 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8048 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8049 "0477 MSI-X entry[%d]: vector=x%x "
8050 "message=%d\n", i,
8051 phba->msix_entries[i].vector,
8052 phba->msix_entries[i].entry);
8053 /*
8054 * Assign MSI-X vectors to interrupt handlers
8055 */
8056
8057 /* vector-0 is associated to slow-path handler */
8058 rc = request_irq(phba->msix_entries[0].vector,
8059 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
8060 LPFC_SP_DRIVER_HANDLER_NAME, phba);
8061 if (rc) {
8062 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8063 "0421 MSI-X slow-path request_irq failed "
8064 "(%d)\n", rc);
8065 goto msi_fail_out;
8066 }
8067
8068 /* vector-1 is associated to fast-path handler */
8069 rc = request_irq(phba->msix_entries[1].vector,
8070 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
8071 LPFC_FP_DRIVER_HANDLER_NAME, phba);
8072
8073 if (rc) {
8074 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8075 "0429 MSI-X fast-path request_irq failed "
8076 "(%d)\n", rc);
8077 goto irq_fail_out;
8078 }
8079
8080 /*
8081 * Configure HBA MSI-X attention conditions to messages
8082 */
8083 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8084
8085 if (!pmb) {
8086 rc = -ENOMEM;
8087 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8088 "0474 Unable to allocate memory for issuing "
8089 "MBOX_CONFIG_MSI command\n");
8090 goto mem_fail_out;
8091 }
8092 rc = lpfc_config_msi(phba, pmb);
8093 if (rc)
8094 goto mbx_fail_out;
8095 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
8096 if (rc != MBX_SUCCESS) {
8097 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
8098 "0351 Config MSI mailbox command failed, "
8099 "mbxCmd x%x, mbxStatus x%x\n",
8100 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
8101 goto mbx_fail_out;
8102 }
8103
8104 /* Free memory allocated for mailbox command */
8105 mempool_free(pmb, phba->mbox_mem_pool);
8106 return rc;
8107
8108mbx_fail_out:
8109 /* Free memory allocated for mailbox command */
8110 mempool_free(pmb, phba->mbox_mem_pool);
8111
8112mem_fail_out:
8113 /* free the irq already requested */
8114 free_irq(phba->msix_entries[1].vector, phba);
8115
8116irq_fail_out:
8117 /* free the irq already requested */
8118 free_irq(phba->msix_entries[0].vector, phba);
8119
8120msi_fail_out:
8121 /* Unconfigure MSI-X capability structure */
8122 pci_disable_msix(phba->pcidev);
8123 return rc;
8124}
8125
8126/**
8127 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
8128 * @phba: pointer to lpfc hba data structure.
8129 *
8130 * This routine is invoked to release the MSI-X vectors and then disable the
8131 * MSI-X interrupt mode to device with SLI-3 interface spec.
8132 **/
8133static void
8134lpfc_sli_disable_msix(struct lpfc_hba *phba)
8135{
8136 int i;
8137
8138 /* Free up MSI-X multi-message vectors */
8139 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8140 free_irq(phba->msix_entries[i].vector, phba);
8141 /* Disable MSI-X */
8142 pci_disable_msix(phba->pcidev);
8143
8144 return;
8145}
8146
8147/**
8148 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
8149 * @phba: pointer to lpfc hba data structure.
8150 *
8151 * This routine is invoked to enable the MSI interrupt mode to device with
8152 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
8153 * enable the MSI vector. The device driver is responsible for calling the
8154 * request_irq() to register MSI vector with a interrupt the handler, which
8155 * is done in this function.
8156 *
8157 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008158 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008159 * other values - error
8160 */
8161static int
8162lpfc_sli_enable_msi(struct lpfc_hba *phba)
8163{
8164 int rc;
8165
8166 rc = pci_enable_msi(phba->pcidev);
8167 if (!rc)
8168 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8169 "0462 PCI enable MSI mode success.\n");
8170 else {
8171 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8172 "0471 PCI enable MSI mode failed (%d)\n", rc);
8173 return rc;
8174 }
8175
8176 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
8177 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8178 if (rc) {
8179 pci_disable_msi(phba->pcidev);
8180 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8181 "0478 MSI request_irq failed (%d)\n", rc);
8182 }
8183 return rc;
8184}
8185
8186/**
8187 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
8188 * @phba: pointer to lpfc hba data structure.
8189 *
8190 * This routine is invoked to disable the MSI interrupt mode to device with
8191 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
8192 * done request_irq() on before calling pci_disable_msi(). Failure to do so
8193 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
8194 * its vector.
8195 */
8196static void
8197lpfc_sli_disable_msi(struct lpfc_hba *phba)
8198{
8199 free_irq(phba->pcidev->irq, phba);
8200 pci_disable_msi(phba->pcidev);
8201 return;
8202}
8203
8204/**
8205 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
8206 * @phba: pointer to lpfc hba data structure.
8207 *
8208 * This routine is invoked to enable device interrupt and associate driver's
8209 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
8210 * spec. Depends on the interrupt mode configured to the driver, the driver
8211 * will try to fallback from the configured interrupt mode to an interrupt
8212 * mode which is supported by the platform, kernel, and device in the order
8213 * of:
8214 * MSI-X -> MSI -> IRQ.
8215 *
8216 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008217 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008218 * other values - error
8219 **/
8220static uint32_t
8221lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
8222{
8223 uint32_t intr_mode = LPFC_INTR_ERROR;
8224 int retval;
8225
8226 if (cfg_mode == 2) {
8227 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
8228 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
8229 if (!retval) {
8230 /* Now, try to enable MSI-X interrupt mode */
8231 retval = lpfc_sli_enable_msix(phba);
8232 if (!retval) {
8233 /* Indicate initialization to MSI-X mode */
8234 phba->intr_type = MSIX;
8235 intr_mode = 2;
8236 }
8237 }
8238 }
8239
8240 /* Fallback to MSI if MSI-X initialization failed */
8241 if (cfg_mode >= 1 && phba->intr_type == NONE) {
8242 retval = lpfc_sli_enable_msi(phba);
8243 if (!retval) {
8244 /* Indicate initialization to MSI mode */
8245 phba->intr_type = MSI;
8246 intr_mode = 1;
8247 }
8248 }
8249
8250 /* Fallback to INTx if both MSI-X/MSI initalization failed */
8251 if (phba->intr_type == NONE) {
8252 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
8253 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8254 if (!retval) {
8255 /* Indicate initialization to INTx mode */
8256 phba->intr_type = INTx;
8257 intr_mode = 0;
8258 }
8259 }
8260 return intr_mode;
8261}
8262
8263/**
8264 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
8265 * @phba: pointer to lpfc hba data structure.
8266 *
8267 * This routine is invoked to disable device interrupt and disassociate the
8268 * driver's interrupt handler(s) from interrupt vector(s) to device with
8269 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
8270 * release the interrupt vector(s) for the message signaled interrupt.
8271 **/
8272static void
8273lpfc_sli_disable_intr(struct lpfc_hba *phba)
8274{
8275 /* Disable the currently initialized interrupt mode */
8276 if (phba->intr_type == MSIX)
8277 lpfc_sli_disable_msix(phba);
8278 else if (phba->intr_type == MSI)
8279 lpfc_sli_disable_msi(phba);
8280 else if (phba->intr_type == INTx)
8281 free_irq(phba->pcidev->irq, phba);
8282
8283 /* Reset interrupt management states */
8284 phba->intr_type = NONE;
8285 phba->sli.slistat.sli_intr = 0;
8286
8287 return;
8288}
8289
8290/**
James Smartda0436e2009-05-22 14:51:39 -04008291 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
8292 * @phba: pointer to lpfc hba data structure.
8293 *
8294 * This routine is invoked to enable the MSI-X interrupt vectors to device
8295 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
8296 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
8297 * enables either all or nothing, depending on the current availability of
8298 * PCI vector resources. The device driver is responsible for calling the
8299 * individual request_irq() to register each MSI-X vector with a interrupt
8300 * handler, which is done in this function. Note that later when device is
8301 * unloading, the driver should always call free_irq() on all MSI-X vectors
8302 * it has done request_irq() on before calling pci_disable_msix(). Failure
8303 * to do so results in a BUG_ON() and a device will be left with MSI-X
8304 * enabled and leaks its vectors.
8305 *
8306 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008307 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008308 * other values - error
8309 **/
8310static int
8311lpfc_sli4_enable_msix(struct lpfc_hba *phba)
8312{
James Smart75baf692010-06-08 18:31:21 -04008313 int vectors, rc, index;
James Smartda0436e2009-05-22 14:51:39 -04008314
8315 /* Set up MSI-X multi-message vectors */
James Smart82c3e9b2012-09-29 11:29:50 -04008316 for (index = 0; index < phba->cfg_fcp_io_channel; index++)
James Smartda0436e2009-05-22 14:51:39 -04008317 phba->sli4_hba.msix_entries[index].entry = index;
8318
8319 /* Configure MSI-X capability structure */
James Smart82c3e9b2012-09-29 11:29:50 -04008320 vectors = phba->cfg_fcp_io_channel;
James Smart75baf692010-06-08 18:31:21 -04008321enable_msix_vectors:
James Smartda0436e2009-05-22 14:51:39 -04008322 rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
James Smart75baf692010-06-08 18:31:21 -04008323 vectors);
8324 if (rc > 1) {
8325 vectors = rc;
8326 goto enable_msix_vectors;
8327 } else if (rc) {
James Smartda0436e2009-05-22 14:51:39 -04008328 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8329 "0484 PCI enable MSI-X failed (%d)\n", rc);
8330 goto msi_fail_out;
8331 }
James Smart75baf692010-06-08 18:31:21 -04008332
James Smartda0436e2009-05-22 14:51:39 -04008333 /* Log MSI-X vector assignment */
James Smart75baf692010-06-08 18:31:21 -04008334 for (index = 0; index < vectors; index++)
James Smartda0436e2009-05-22 14:51:39 -04008335 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8336 "0489 MSI-X entry[%d]: vector=x%x "
8337 "message=%d\n", index,
8338 phba->sli4_hba.msix_entries[index].vector,
8339 phba->sli4_hba.msix_entries[index].entry);
James Smart67d12732012-08-03 12:36:13 -04008340
James Smartda0436e2009-05-22 14:51:39 -04008341 /*
8342 * Assign MSI-X vectors to interrupt handlers
8343 */
James Smart67d12732012-08-03 12:36:13 -04008344 for (index = 0; index < vectors; index++) {
James Smart4305f182012-08-03 12:36:33 -04008345 memset(&phba->sli4_hba.handler_name[index], 0, 16);
8346 sprintf((char *)&phba->sli4_hba.handler_name[index],
8347 LPFC_DRIVER_HANDLER_NAME"%d", index);
James Smartda0436e2009-05-22 14:51:39 -04008348
James Smart67d12732012-08-03 12:36:13 -04008349 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8350 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
James Smartba20c852012-08-03 12:36:52 -04008351 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].fcp_eq_in_use, 1);
James Smartda0436e2009-05-22 14:51:39 -04008352 rc = request_irq(phba->sli4_hba.msix_entries[index].vector,
James Smart67d12732012-08-03 12:36:13 -04008353 &lpfc_sli4_hba_intr_handler, IRQF_SHARED,
James Smart4305f182012-08-03 12:36:33 -04008354 (char *)&phba->sli4_hba.handler_name[index],
James Smart67d12732012-08-03 12:36:13 -04008355 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -04008356 if (rc) {
8357 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8358 "0486 MSI-X fast-path (%d) "
8359 "request_irq failed (%d)\n", index, rc);
8360 goto cfg_fail_out;
8361 }
8362 }
8363
James Smart82c3e9b2012-09-29 11:29:50 -04008364 if (vectors != phba->cfg_fcp_io_channel) {
8365 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8366 "3238 Reducing IO channels to match number of "
8367 "MSI-X vectors, requested %d got %d\n",
8368 phba->cfg_fcp_io_channel, vectors);
8369 phba->cfg_fcp_io_channel = vectors;
8370 }
James Smartda0436e2009-05-22 14:51:39 -04008371 return rc;
8372
8373cfg_fail_out:
8374 /* free the irq already requested */
James Smart67d12732012-08-03 12:36:13 -04008375 for (--index; index >= 0; index--)
8376 free_irq(phba->sli4_hba.msix_entries[index].vector,
8377 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -04008378
8379msi_fail_out:
8380 /* Unconfigure MSI-X capability structure */
8381 pci_disable_msix(phba->pcidev);
8382 return rc;
8383}
8384
8385/**
8386 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
8387 * @phba: pointer to lpfc hba data structure.
8388 *
8389 * This routine is invoked to release the MSI-X vectors and then disable the
8390 * MSI-X interrupt mode to device with SLI-4 interface spec.
8391 **/
8392static void
8393lpfc_sli4_disable_msix(struct lpfc_hba *phba)
8394{
8395 int index;
8396
8397 /* Free up MSI-X multi-message vectors */
James Smart82c3e9b2012-09-29 11:29:50 -04008398 for (index = 0; index < phba->cfg_fcp_io_channel; index++)
James Smartda0436e2009-05-22 14:51:39 -04008399 free_irq(phba->sli4_hba.msix_entries[index].vector,
James Smart67d12732012-08-03 12:36:13 -04008400 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smart75baf692010-06-08 18:31:21 -04008401
James Smartda0436e2009-05-22 14:51:39 -04008402 /* Disable MSI-X */
8403 pci_disable_msix(phba->pcidev);
8404
8405 return;
8406}
8407
8408/**
8409 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
8410 * @phba: pointer to lpfc hba data structure.
8411 *
8412 * This routine is invoked to enable the MSI interrupt mode to device with
8413 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
8414 * to enable the MSI vector. The device driver is responsible for calling
8415 * the request_irq() to register MSI vector with a interrupt the handler,
8416 * which is done in this function.
8417 *
8418 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008419 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008420 * other values - error
8421 **/
8422static int
8423lpfc_sli4_enable_msi(struct lpfc_hba *phba)
8424{
8425 int rc, index;
8426
8427 rc = pci_enable_msi(phba->pcidev);
8428 if (!rc)
8429 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8430 "0487 PCI enable MSI mode success.\n");
8431 else {
8432 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8433 "0488 PCI enable MSI mode failed (%d)\n", rc);
8434 return rc;
8435 }
8436
8437 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
8438 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8439 if (rc) {
8440 pci_disable_msi(phba->pcidev);
8441 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8442 "0490 MSI request_irq failed (%d)\n", rc);
James Smart75baf692010-06-08 18:31:21 -04008443 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008444 }
8445
James Smart67d12732012-08-03 12:36:13 -04008446 for (index = 0; index < phba->cfg_fcp_io_channel; index++) {
James Smartda0436e2009-05-22 14:51:39 -04008447 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8448 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
8449 }
8450
James Smart75baf692010-06-08 18:31:21 -04008451 return 0;
James Smartda0436e2009-05-22 14:51:39 -04008452}
8453
8454/**
8455 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
8456 * @phba: pointer to lpfc hba data structure.
8457 *
8458 * This routine is invoked to disable the MSI interrupt mode to device with
8459 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
8460 * done request_irq() on before calling pci_disable_msi(). Failure to do so
8461 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
8462 * its vector.
8463 **/
8464static void
8465lpfc_sli4_disable_msi(struct lpfc_hba *phba)
8466{
8467 free_irq(phba->pcidev->irq, phba);
8468 pci_disable_msi(phba->pcidev);
8469 return;
8470}
8471
8472/**
8473 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
8474 * @phba: pointer to lpfc hba data structure.
8475 *
8476 * This routine is invoked to enable device interrupt and associate driver's
8477 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
8478 * interface spec. Depends on the interrupt mode configured to the driver,
8479 * the driver will try to fallback from the configured interrupt mode to an
8480 * interrupt mode which is supported by the platform, kernel, and device in
8481 * the order of:
8482 * MSI-X -> MSI -> IRQ.
8483 *
8484 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008485 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008486 * other values - error
8487 **/
8488static uint32_t
8489lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
8490{
8491 uint32_t intr_mode = LPFC_INTR_ERROR;
8492 int retval, index;
8493
8494 if (cfg_mode == 2) {
8495 /* Preparation before conf_msi mbox cmd */
8496 retval = 0;
8497 if (!retval) {
8498 /* Now, try to enable MSI-X interrupt mode */
8499 retval = lpfc_sli4_enable_msix(phba);
8500 if (!retval) {
8501 /* Indicate initialization to MSI-X mode */
8502 phba->intr_type = MSIX;
8503 intr_mode = 2;
8504 }
8505 }
8506 }
8507
8508 /* Fallback to MSI if MSI-X initialization failed */
8509 if (cfg_mode >= 1 && phba->intr_type == NONE) {
8510 retval = lpfc_sli4_enable_msi(phba);
8511 if (!retval) {
8512 /* Indicate initialization to MSI mode */
8513 phba->intr_type = MSI;
8514 intr_mode = 1;
8515 }
8516 }
8517
8518 /* Fallback to INTx if both MSI-X/MSI initalization failed */
8519 if (phba->intr_type == NONE) {
8520 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
8521 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8522 if (!retval) {
8523 /* Indicate initialization to INTx mode */
8524 phba->intr_type = INTx;
8525 intr_mode = 0;
James Smart67d12732012-08-03 12:36:13 -04008526 for (index = 0; index < phba->cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04008527 index++) {
8528 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8529 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
James Smartba20c852012-08-03 12:36:52 -04008530 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].
8531 fcp_eq_in_use, 1);
James Smartda0436e2009-05-22 14:51:39 -04008532 }
8533 }
8534 }
8535 return intr_mode;
8536}
8537
8538/**
8539 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
8540 * @phba: pointer to lpfc hba data structure.
8541 *
8542 * This routine is invoked to disable device interrupt and disassociate
8543 * the driver's interrupt handler(s) from interrupt vector(s) to device
8544 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
8545 * will release the interrupt vector(s) for the message signaled interrupt.
8546 **/
8547static void
8548lpfc_sli4_disable_intr(struct lpfc_hba *phba)
8549{
8550 /* Disable the currently initialized interrupt mode */
8551 if (phba->intr_type == MSIX)
8552 lpfc_sli4_disable_msix(phba);
8553 else if (phba->intr_type == MSI)
8554 lpfc_sli4_disable_msi(phba);
8555 else if (phba->intr_type == INTx)
8556 free_irq(phba->pcidev->irq, phba);
8557
8558 /* Reset interrupt management states */
8559 phba->intr_type = NONE;
8560 phba->sli.slistat.sli_intr = 0;
8561
8562 return;
8563}
8564
8565/**
James Smart3772a992009-05-22 14:50:54 -04008566 * lpfc_unset_hba - Unset SLI3 hba device initialization
8567 * @phba: pointer to lpfc hba data structure.
8568 *
8569 * This routine is invoked to unset the HBA device initialization steps to
8570 * a device with SLI-3 interface spec.
8571 **/
8572static void
8573lpfc_unset_hba(struct lpfc_hba *phba)
8574{
8575 struct lpfc_vport *vport = phba->pport;
8576 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
8577
8578 spin_lock_irq(shost->host_lock);
8579 vport->load_flag |= FC_UNLOADING;
8580 spin_unlock_irq(shost->host_lock);
8581
James Smart72859902012-01-18 16:25:38 -05008582 kfree(phba->vpi_bmask);
8583 kfree(phba->vpi_ids);
8584
James Smart3772a992009-05-22 14:50:54 -04008585 lpfc_stop_hba_timers(phba);
8586
8587 phba->pport->work_port_events = 0;
8588
8589 lpfc_sli_hba_down(phba);
8590
8591 lpfc_sli_brdrestart(phba);
8592
8593 lpfc_sli_disable_intr(phba);
8594
8595 return;
8596}
8597
8598/**
James Smart5af5eee2010-10-22 11:06:38 -04008599 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
8600 * @phba: Pointer to HBA context object.
8601 *
8602 * This function is called in the SLI4 code path to wait for completion
8603 * of device's XRIs exchange busy. It will check the XRI exchange busy
8604 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
8605 * that, it will check the XRI exchange busy on outstanding FCP and ELS
8606 * I/Os every 30 seconds, log error message, and wait forever. Only when
8607 * all XRI exchange busy complete, the driver unload shall proceed with
8608 * invoking the function reset ioctl mailbox command to the CNA and the
8609 * the rest of the driver unload resource release.
8610 **/
8611static void
8612lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
8613{
8614 int wait_time = 0;
8615 int fcp_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
8616 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
8617
8618 while (!fcp_xri_cmpl || !els_xri_cmpl) {
8619 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
8620 if (!fcp_xri_cmpl)
8621 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8622 "2877 FCP XRI exchange busy "
8623 "wait time: %d seconds.\n",
8624 wait_time/1000);
8625 if (!els_xri_cmpl)
8626 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8627 "2878 ELS XRI exchange busy "
8628 "wait time: %d seconds.\n",
8629 wait_time/1000);
8630 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
8631 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
8632 } else {
8633 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
8634 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
8635 }
8636 fcp_xri_cmpl =
8637 list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
8638 els_xri_cmpl =
8639 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
8640 }
8641}
8642
8643/**
James Smartda0436e2009-05-22 14:51:39 -04008644 * lpfc_sli4_hba_unset - Unset the fcoe hba
8645 * @phba: Pointer to HBA context object.
8646 *
8647 * This function is called in the SLI4 code path to reset the HBA's FCoE
8648 * function. The caller is not required to hold any lock. This routine
8649 * issues PCI function reset mailbox command to reset the FCoE function.
8650 * At the end of the function, it calls lpfc_hba_down_post function to
8651 * free any pending commands.
8652 **/
8653static void
8654lpfc_sli4_hba_unset(struct lpfc_hba *phba)
8655{
8656 int wait_cnt = 0;
8657 LPFC_MBOXQ_t *mboxq;
James Smart912e3ac2011-05-24 11:42:11 -04008658 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -04008659
8660 lpfc_stop_hba_timers(phba);
8661 phba->sli4_hba.intr_enable = 0;
8662
8663 /*
8664 * Gracefully wait out the potential current outstanding asynchronous
8665 * mailbox command.
8666 */
8667
8668 /* First, block any pending async mailbox command from posted */
8669 spin_lock_irq(&phba->hbalock);
8670 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
8671 spin_unlock_irq(&phba->hbalock);
8672 /* Now, trying to wait it out if we can */
8673 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8674 msleep(10);
8675 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
8676 break;
8677 }
8678 /* Forcefully release the outstanding mailbox command if timed out */
8679 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8680 spin_lock_irq(&phba->hbalock);
8681 mboxq = phba->sli.mbox_active;
8682 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8683 __lpfc_mbox_cmpl_put(phba, mboxq);
8684 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8685 phba->sli.mbox_active = NULL;
8686 spin_unlock_irq(&phba->hbalock);
8687 }
8688
James Smart5af5eee2010-10-22 11:06:38 -04008689 /* Abort all iocbs associated with the hba */
8690 lpfc_sli_hba_iocb_abort(phba);
8691
8692 /* Wait for completion of device XRI exchange busy */
8693 lpfc_sli4_xri_exchange_busy_wait(phba);
8694
James Smartda0436e2009-05-22 14:51:39 -04008695 /* Disable PCI subsystem interrupt */
8696 lpfc_sli4_disable_intr(phba);
8697
James Smart912e3ac2011-05-24 11:42:11 -04008698 /* Disable SR-IOV if enabled */
8699 if (phba->cfg_sriov_nr_virtfn)
8700 pci_disable_sriov(pdev);
8701
James Smartda0436e2009-05-22 14:51:39 -04008702 /* Stop kthread signal shall trigger work_done one more time */
8703 kthread_stop(phba->worker_thread);
8704
James Smart3677a3a2010-09-29 11:19:14 -04008705 /* Reset SLI4 HBA FCoE function */
8706 lpfc_pci_function_reset(phba);
James Smart5350d872011-10-10 21:33:49 -04008707 lpfc_sli4_queue_destroy(phba);
James Smart3677a3a2010-09-29 11:19:14 -04008708
James Smartda0436e2009-05-22 14:51:39 -04008709 /* Stop the SLI4 device port */
8710 phba->pport->work_port_events = 0;
8711}
8712
James Smart28baac72010-02-12 14:42:03 -05008713 /**
8714 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
8715 * @phba: Pointer to HBA context object.
8716 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
8717 *
8718 * This function is called in the SLI4 code path to read the port's
8719 * sli4 capabilities.
8720 *
8721 * This function may be be called from any context that can block-wait
8722 * for the completion. The expectation is that this routine is called
8723 * typically from probe_one or from the online routine.
8724 **/
8725int
8726lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8727{
8728 int rc;
8729 struct lpfc_mqe *mqe;
8730 struct lpfc_pc_sli4_params *sli4_params;
8731 uint32_t mbox_tmo;
8732
8733 rc = 0;
8734 mqe = &mboxq->u.mqe;
8735
8736 /* Read the port's SLI4 Parameters port capabilities */
James Smartfedd3b72011-02-16 12:39:24 -05008737 lpfc_pc_sli4_params(mboxq);
James Smart28baac72010-02-12 14:42:03 -05008738 if (!phba->sli4_hba.intr_enable)
8739 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8740 else {
James Smarta183a152011-10-10 21:32:43 -04008741 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart28baac72010-02-12 14:42:03 -05008742 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
8743 }
8744
8745 if (unlikely(rc))
8746 return 1;
8747
8748 sli4_params = &phba->sli4_hba.pc_sli4_params;
8749 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
8750 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
8751 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
8752 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
8753 &mqe->un.sli4_params);
8754 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
8755 &mqe->un.sli4_params);
8756 sli4_params->proto_types = mqe->un.sli4_params.word3;
8757 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
8758 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
8759 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
8760 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
8761 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
8762 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
8763 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
8764 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
8765 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
8766 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
8767 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
8768 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
8769 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
8770 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
8771 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
8772 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
8773 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
8774 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
8775 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
8776 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
James Smart05580562011-05-24 11:40:48 -04008777
8778 /* Make sure that sge_supp_len can be handled by the driver */
8779 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
8780 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
8781
James Smart28baac72010-02-12 14:42:03 -05008782 return rc;
8783}
8784
James Smartda0436e2009-05-22 14:51:39 -04008785/**
James Smartfedd3b72011-02-16 12:39:24 -05008786 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
8787 * @phba: Pointer to HBA context object.
8788 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
8789 *
8790 * This function is called in the SLI4 code path to read the port's
8791 * sli4 capabilities.
8792 *
8793 * This function may be be called from any context that can block-wait
8794 * for the completion. The expectation is that this routine is called
8795 * typically from probe_one or from the online routine.
8796 **/
8797int
8798lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8799{
8800 int rc;
8801 struct lpfc_mqe *mqe = &mboxq->u.mqe;
8802 struct lpfc_pc_sli4_params *sli4_params;
James Smarta183a152011-10-10 21:32:43 -04008803 uint32_t mbox_tmo;
James Smartfedd3b72011-02-16 12:39:24 -05008804 int length;
8805 struct lpfc_sli4_parameters *mbx_sli4_parameters;
8806
James Smart6d368e52011-05-24 11:44:12 -04008807 /*
8808 * By default, the driver assumes the SLI4 port requires RPI
8809 * header postings. The SLI4_PARAM response will correct this
8810 * assumption.
8811 */
8812 phba->sli4_hba.rpi_hdrs_in_use = 1;
8813
James Smartfedd3b72011-02-16 12:39:24 -05008814 /* Read the port's SLI4 Config Parameters */
8815 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
8816 sizeof(struct lpfc_sli4_cfg_mhdr));
8817 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
8818 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
8819 length, LPFC_SLI4_MBX_EMBED);
8820 if (!phba->sli4_hba.intr_enable)
8821 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -04008822 else {
8823 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
8824 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
8825 }
James Smartfedd3b72011-02-16 12:39:24 -05008826 if (unlikely(rc))
8827 return rc;
8828 sli4_params = &phba->sli4_hba.pc_sli4_params;
8829 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
8830 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
8831 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
8832 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
8833 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
8834 mbx_sli4_parameters);
8835 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
8836 mbx_sli4_parameters);
8837 if (bf_get(cfg_phwq, mbx_sli4_parameters))
8838 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
8839 else
8840 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
8841 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
8842 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
8843 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
8844 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
8845 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
8846 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
8847 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
8848 mbx_sli4_parameters);
8849 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
8850 mbx_sli4_parameters);
James Smart6d368e52011-05-24 11:44:12 -04008851 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
8852 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
James Smart05580562011-05-24 11:40:48 -04008853
8854 /* Make sure that sge_supp_len can be handled by the driver */
8855 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
8856 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
8857
James Smartfedd3b72011-02-16 12:39:24 -05008858 return 0;
8859}
8860
8861/**
James Smart3772a992009-05-22 14:50:54 -04008862 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
8863 * @pdev: pointer to PCI device
8864 * @pid: pointer to PCI device identifier
8865 *
8866 * This routine is to be called to attach a device with SLI-3 interface spec
8867 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
8868 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
8869 * information of the device and driver to see if the driver state that it can
8870 * support this kind of device. If the match is successful, the driver core
8871 * invokes this routine. If this routine determines it can claim the HBA, it
8872 * does all the initialization that it needs to do to handle the HBA properly.
8873 *
8874 * Return code
8875 * 0 - driver can claim the device
8876 * negative value - driver can not claim the device
8877 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008878static int
James Smart3772a992009-05-22 14:50:54 -04008879lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
8880{
8881 struct lpfc_hba *phba;
8882 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04008883 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -04008884 int error;
8885 uint32_t cfg_mode, intr_mode;
8886
8887 /* Allocate memory for HBA structure */
8888 phba = lpfc_hba_alloc(pdev);
8889 if (!phba)
8890 return -ENOMEM;
8891
8892 /* Perform generic PCI device enabling operation */
8893 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -04008894 if (error)
James Smart3772a992009-05-22 14:50:54 -04008895 goto out_free_phba;
James Smart3772a992009-05-22 14:50:54 -04008896
8897 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
8898 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
8899 if (error)
8900 goto out_disable_pci_dev;
8901
8902 /* Set up SLI-3 specific device PCI memory space */
8903 error = lpfc_sli_pci_mem_setup(phba);
8904 if (error) {
8905 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8906 "1402 Failed to set up pci memory space.\n");
8907 goto out_disable_pci_dev;
8908 }
8909
8910 /* Set up phase-1 common device driver resources */
8911 error = lpfc_setup_driver_resource_phase1(phba);
8912 if (error) {
8913 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8914 "1403 Failed to set up driver resource.\n");
8915 goto out_unset_pci_mem_s3;
8916 }
8917
8918 /* Set up SLI-3 specific device driver resources */
8919 error = lpfc_sli_driver_resource_setup(phba);
8920 if (error) {
8921 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8922 "1404 Failed to set up driver resource.\n");
8923 goto out_unset_pci_mem_s3;
8924 }
8925
8926 /* Initialize and populate the iocb list per host */
8927 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
8928 if (error) {
8929 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8930 "1405 Failed to initialize iocb list.\n");
8931 goto out_unset_driver_resource_s3;
8932 }
8933
8934 /* Set up common device driver resources */
8935 error = lpfc_setup_driver_resource_phase2(phba);
8936 if (error) {
8937 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8938 "1406 Failed to set up driver resource.\n");
8939 goto out_free_iocb_list;
8940 }
8941
James Smart079b5c92011-08-21 21:48:49 -04008942 /* Get the default values for Model Name and Description */
8943 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
8944
James Smart3772a992009-05-22 14:50:54 -04008945 /* Create SCSI host to the physical port */
8946 error = lpfc_create_shost(phba);
8947 if (error) {
8948 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8949 "1407 Failed to create scsi host.\n");
8950 goto out_unset_driver_resource;
8951 }
8952
8953 /* Configure sysfs attributes */
8954 vport = phba->pport;
8955 error = lpfc_alloc_sysfs_attr(vport);
8956 if (error) {
8957 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8958 "1476 Failed to allocate sysfs attr\n");
8959 goto out_destroy_shost;
8960 }
8961
James Smart6669f9b2009-10-02 15:16:45 -04008962 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -04008963 /* Now, trying to enable interrupt and bring up the device */
8964 cfg_mode = phba->cfg_use_msi;
8965 while (true) {
8966 /* Put device to a known state before enabling interrupt */
8967 lpfc_stop_port(phba);
8968 /* Configure and enable interrupt */
8969 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
8970 if (intr_mode == LPFC_INTR_ERROR) {
8971 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8972 "0431 Failed to enable interrupt.\n");
8973 error = -ENODEV;
8974 goto out_free_sysfs_attr;
8975 }
8976 /* SLI-3 HBA setup */
8977 if (lpfc_sli_hba_setup(phba)) {
8978 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8979 "1477 Failed to set up hba\n");
8980 error = -ENODEV;
8981 goto out_remove_device;
8982 }
8983
8984 /* Wait 50ms for the interrupts of previous mailbox commands */
8985 msleep(50);
8986 /* Check active interrupts on message signaled interrupts */
8987 if (intr_mode == 0 ||
8988 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
8989 /* Log the current active interrupt mode */
8990 phba->intr_mode = intr_mode;
8991 lpfc_log_intr_mode(phba, intr_mode);
8992 break;
8993 } else {
8994 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8995 "0447 Configure interrupt mode (%d) "
8996 "failed active interrupt test.\n",
8997 intr_mode);
8998 /* Disable the current interrupt mode */
8999 lpfc_sli_disable_intr(phba);
9000 /* Try next level of interrupt mode */
9001 cfg_mode = --intr_mode;
9002 }
9003 }
9004
9005 /* Perform post initialization setup */
9006 lpfc_post_init_setup(phba);
9007
9008 /* Check if there are static vports to be created. */
9009 lpfc_create_static_vport(phba);
9010
9011 return 0;
9012
9013out_remove_device:
9014 lpfc_unset_hba(phba);
9015out_free_sysfs_attr:
9016 lpfc_free_sysfs_attr(vport);
9017out_destroy_shost:
9018 lpfc_destroy_shost(phba);
9019out_unset_driver_resource:
9020 lpfc_unset_driver_resource_phase2(phba);
9021out_free_iocb_list:
9022 lpfc_free_iocb_list(phba);
9023out_unset_driver_resource_s3:
9024 lpfc_sli_driver_resource_unset(phba);
9025out_unset_pci_mem_s3:
9026 lpfc_sli_pci_mem_unset(phba);
9027out_disable_pci_dev:
9028 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04009029 if (shost)
9030 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -04009031out_free_phba:
9032 lpfc_hba_free(phba);
9033 return error;
9034}
9035
9036/**
9037 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -04009038 * @pdev: pointer to PCI device
9039 *
James Smart3772a992009-05-22 14:50:54 -04009040 * This routine is to be called to disattach a device with SLI-3 interface
9041 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
9042 * removed from PCI bus, it performs all the necessary cleanup for the HBA
9043 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -04009044 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009045static void
James Smart3772a992009-05-22 14:50:54 -04009046lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -05009047{
James Smart2e0fef82007-06-17 19:56:36 -05009048 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9049 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -05009050 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -05009051 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -05009052 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05009053 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
9054
James Smart549e55c2007-08-02 11:09:51 -04009055 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04009056 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04009057 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009058
James Smart858c9f62007-06-17 19:56:39 -05009059 lpfc_free_sysfs_attr(vport);
9060
James Smarteada2722008-12-04 22:39:13 -05009061 /* Release all the vports against this physical port */
9062 vports = lpfc_create_vport_work_array(phba);
9063 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -04009064 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
9065 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
9066 continue;
James Smarteada2722008-12-04 22:39:13 -05009067 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -04009068 }
James Smarteada2722008-12-04 22:39:13 -05009069 lpfc_destroy_vport_work_array(phba, vports);
9070
9071 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -05009072 fc_remove_host(shost);
9073 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04009074 lpfc_cleanup(vport);
9075
James Smart2e0fef82007-06-17 19:56:36 -05009076 /*
9077 * Bring down the SLI Layer. This step disable all interrupts,
9078 * clears the rings, discards all mailbox commands, and resets
9079 * the HBA.
9080 */
James Smarta257bf92009-04-06 18:48:10 -04009081
Justin P. Mattock48e34d02010-12-30 15:07:58 -08009082 /* HBA interrupt will be disabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -05009083 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -04009084 /* Stop kthread signal shall trigger work_done one more time */
9085 kthread_stop(phba->worker_thread);
9086 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -05009087 lpfc_sli_brdrestart(phba);
9088
James Smart72859902012-01-18 16:25:38 -05009089 kfree(phba->vpi_bmask);
9090 kfree(phba->vpi_ids);
9091
James Smart3772a992009-05-22 14:50:54 -04009092 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05009093 spin_lock_irq(&phba->hbalock);
9094 list_del_init(&vport->listentry);
9095 spin_unlock_irq(&phba->hbalock);
9096
James Smart858c9f62007-06-17 19:56:39 -05009097 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05009098
James Smart912e3ac2011-05-24 11:42:11 -04009099 /* Disable SR-IOV if enabled */
9100 if (phba->cfg_sriov_nr_virtfn)
9101 pci_disable_sriov(pdev);
9102
James Smart5b75da22008-12-04 22:39:35 -05009103 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009104 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -05009105
9106 pci_set_drvdata(pdev, NULL);
James Smart858c9f62007-06-17 19:56:39 -05009107 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05009108
9109 /*
9110 * Call scsi_free before mem_free since scsi bufs are released to their
9111 * corresponding pools here.
9112 */
9113 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -04009114 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -05009115
James Smart34b02dc2008-08-24 21:49:55 -04009116 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
9117 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -05009118
James Smart2e0fef82007-06-17 19:56:36 -05009119 /* Free resources associated with SLI2 interface */
9120 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -04009121 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -05009122
9123 /* unmap adapter SLIM and Control Registers */
9124 iounmap(phba->ctrl_regs_memmap_p);
9125 iounmap(phba->slim_memmap_p);
9126
James Smart3772a992009-05-22 14:50:54 -04009127 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -05009128
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05009129 pci_release_selected_regions(pdev, bars);
James Smart2e0fef82007-06-17 19:56:36 -05009130 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05009131}
9132
Linas Vepstas8d63f372007-02-14 14:28:36 -06009133/**
James Smart3772a992009-05-22 14:50:54 -04009134 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05009135 * @pdev: pointer to PCI device
9136 * @msg: power management message
9137 *
James Smart3772a992009-05-22 14:50:54 -04009138 * This routine is to be called from the kernel's PCI subsystem to support
9139 * system Power Management (PM) to device with SLI-3 interface spec. When
9140 * PM invokes this method, it quiesces the device by stopping the driver's
9141 * worker thread for the device, turning off device's interrupt and DMA,
9142 * and bring the device offline. Note that as the driver implements the
9143 * minimum PM requirements to a power-aware driver's PM support for the
9144 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
9145 * to the suspend() method call will be treated as SUSPEND and the driver will
9146 * fully reinitialize its device during resume() method call, the driver will
9147 * set device to PCI_D3hot state in PCI config space instead of setting it
9148 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -05009149 *
9150 * Return code
James Smart3772a992009-05-22 14:50:54 -04009151 * 0 - driver suspended the device
9152 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05009153 **/
9154static int
James Smart3772a992009-05-22 14:50:54 -04009155lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -05009156{
9157 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9158 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9159
9160 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9161 "0473 PCI device Power Management suspend.\n");
9162
9163 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -04009164 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3a55b532008-12-04 22:38:54 -05009165 lpfc_offline(phba);
9166 kthread_stop(phba->worker_thread);
9167
9168 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -04009169 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -05009170
9171 /* Save device state to PCI config space */
9172 pci_save_state(pdev);
9173 pci_set_power_state(pdev, PCI_D3hot);
9174
9175 return 0;
9176}
9177
9178/**
James Smart3772a992009-05-22 14:50:54 -04009179 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05009180 * @pdev: pointer to PCI device
9181 *
James Smart3772a992009-05-22 14:50:54 -04009182 * This routine is to be called from the kernel's PCI subsystem to support
9183 * system Power Management (PM) to device with SLI-3 interface spec. When PM
9184 * invokes this method, it restores the device's PCI config space state and
9185 * fully reinitializes the device and brings it online. Note that as the
9186 * driver implements the minimum PM requirements to a power-aware driver's
9187 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
9188 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
9189 * driver will fully reinitialize its device during resume() method call,
9190 * the device will be set to PCI_D0 directly in PCI config space before
9191 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -05009192 *
9193 * Return code
James Smart3772a992009-05-22 14:50:54 -04009194 * 0 - driver suspended the device
9195 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05009196 **/
9197static int
James Smart3772a992009-05-22 14:50:54 -04009198lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -05009199{
9200 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9201 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -05009202 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05009203 int error;
9204
9205 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9206 "0452 PCI device Power Management resume.\n");
9207
9208 /* Restore device state from PCI config space */
9209 pci_set_power_state(pdev, PCI_D0);
9210 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -04009211
James Smart1dfb5a42010-02-12 14:40:50 -05009212 /*
9213 * As the new kernel behavior of pci_restore_state() API call clears
9214 * device saved_state flag, need to save the restored state again.
9215 */
9216 pci_save_state(pdev);
9217
James Smart3a55b532008-12-04 22:38:54 -05009218 if (pdev->is_busmaster)
9219 pci_set_master(pdev);
9220
9221 /* Startup the kernel thread for this host adapter. */
9222 phba->worker_thread = kthread_run(lpfc_do_work, phba,
9223 "lpfc_worker_%d", phba->brd_no);
9224 if (IS_ERR(phba->worker_thread)) {
9225 error = PTR_ERR(phba->worker_thread);
9226 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9227 "0434 PM resume failed to start worker "
9228 "thread: error=x%x.\n", error);
9229 return error;
9230 }
9231
James Smart5b75da22008-12-04 22:39:35 -05009232 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009233 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05009234 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -05009235 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -05009236 "0430 PM resume Failed to enable interrupt\n");
9237 return -EIO;
9238 } else
9239 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05009240
9241 /* Restart HBA and bring it online */
9242 lpfc_sli_brdrestart(phba);
9243 lpfc_online(phba);
9244
James Smart5b75da22008-12-04 22:39:35 -05009245 /* Log the current active interrupt mode */
9246 lpfc_log_intr_mode(phba, phba->intr_mode);
9247
James Smart3a55b532008-12-04 22:38:54 -05009248 return 0;
9249}
9250
9251/**
James Smart891478a2009-11-18 15:40:23 -05009252 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
9253 * @phba: pointer to lpfc hba data structure.
9254 *
9255 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -04009256 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -05009257 **/
9258static void
9259lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
9260{
James Smarte2af0d22010-03-15 11:25:32 -04009261 struct lpfc_sli *psli = &phba->sli;
9262 struct lpfc_sli_ring *pring;
9263
James Smart891478a2009-11-18 15:40:23 -05009264 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9265 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -04009266
9267 /*
9268 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
9269 * and let the SCSI mid-layer to retry them to recover.
9270 */
9271 pring = &psli->ring[psli->fcp_ring];
9272 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart891478a2009-11-18 15:40:23 -05009273}
9274
9275/**
James Smart0d878412009-10-02 15:16:56 -04009276 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
9277 * @phba: pointer to lpfc hba data structure.
9278 *
9279 * This routine is called to prepare the SLI3 device for PCI slot reset. It
9280 * disables the device interrupt and pci device, and aborts the internal FCP
9281 * pending I/Os.
9282 **/
9283static void
9284lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
9285{
James Smart0d878412009-10-02 15:16:56 -04009286 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05009287 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -04009288
James Smart75baf692010-06-08 18:31:21 -04009289 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -04009290 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -04009291
James Smarte2af0d22010-03-15 11:25:32 -04009292 /* Block all SCSI devices' I/Os on the host */
9293 lpfc_scsi_dev_block(phba);
9294
9295 /* stop all timers */
9296 lpfc_stop_hba_timers(phba);
9297
James Smart0d878412009-10-02 15:16:56 -04009298 /* Disable interrupt and pci device */
9299 lpfc_sli_disable_intr(phba);
9300 pci_disable_device(phba->pcidev);
James Smart75baf692010-06-08 18:31:21 -04009301
James Smarte2af0d22010-03-15 11:25:32 -04009302 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
9303 lpfc_sli_flush_fcp_rings(phba);
James Smart0d878412009-10-02 15:16:56 -04009304}
9305
9306/**
9307 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
9308 * @phba: pointer to lpfc hba data structure.
9309 *
9310 * This routine is called to prepare the SLI3 device for PCI slot permanently
9311 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
9312 * pending I/Os.
9313 **/
9314static void
James Smart75baf692010-06-08 18:31:21 -04009315lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
James Smart0d878412009-10-02 15:16:56 -04009316{
9317 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05009318 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -04009319 /* Block all SCSI devices' I/Os on the host */
9320 lpfc_scsi_dev_block(phba);
9321
9322 /* stop all timers */
9323 lpfc_stop_hba_timers(phba);
9324
James Smart0d878412009-10-02 15:16:56 -04009325 /* Clean up all driver's outstanding SCSI I/Os */
9326 lpfc_sli_flush_fcp_rings(phba);
9327}
9328
9329/**
James Smart3772a992009-05-22 14:50:54 -04009330 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -04009331 * @pdev: pointer to PCI device.
9332 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009333 *
James Smart3772a992009-05-22 14:50:54 -04009334 * This routine is called from the PCI subsystem for I/O error handling to
9335 * device with SLI-3 interface spec. This function is called by the PCI
9336 * subsystem after a PCI bus error affecting this device has been detected.
9337 * When this function is invoked, it will need to stop all the I/Os and
9338 * interrupt(s) to the device. Once that is done, it will return
9339 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
9340 * as desired.
James Smarte59058c2008-08-24 21:49:00 -04009341 *
9342 * Return codes
James Smart0d878412009-10-02 15:16:56 -04009343 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -04009344 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
9345 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -04009346 **/
James Smart3772a992009-05-22 14:50:54 -04009347static pci_ers_result_t
9348lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009349{
James Smart51ef4c22007-08-02 11:10:31 -04009350 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9351 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009352
James Smart0d878412009-10-02 15:16:56 -04009353 switch (state) {
9354 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -05009355 /* Non-fatal error, prepare for recovery */
9356 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -04009357 return PCI_ERS_RESULT_CAN_RECOVER;
9358 case pci_channel_io_frozen:
9359 /* Fatal error, prepare for slot reset */
9360 lpfc_sli_prep_dev_for_reset(phba);
9361 return PCI_ERS_RESULT_NEED_RESET;
9362 case pci_channel_io_perm_failure:
9363 /* Permanent failure, prepare for device down */
James Smart75baf692010-06-08 18:31:21 -04009364 lpfc_sli_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009365 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -04009366 default:
9367 /* Unknown state, prepare and request slot reset */
9368 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9369 "0472 Unknown PCI error state: x%x\n", state);
9370 lpfc_sli_prep_dev_for_reset(phba);
9371 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -04009372 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06009373}
9374
9375/**
James Smart3772a992009-05-22 14:50:54 -04009376 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -04009377 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009378 *
James Smart3772a992009-05-22 14:50:54 -04009379 * This routine is called from the PCI subsystem for error handling to
9380 * device with SLI-3 interface spec. This is called after PCI bus has been
9381 * reset to restart the PCI card from scratch, as if from a cold-boot.
9382 * During the PCI subsystem error recovery, after driver returns
9383 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
9384 * recovery and then call this routine before calling the .resume method
9385 * to recover the device. This function will initialize the HBA device,
9386 * enable the interrupt, but it will just put the HBA to offline state
9387 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -04009388 *
9389 * Return codes
James Smart3772a992009-05-22 14:50:54 -04009390 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
9391 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -06009392 */
James Smart3772a992009-05-22 14:50:54 -04009393static pci_ers_result_t
9394lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009395{
James Smart51ef4c22007-08-02 11:10:31 -04009396 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9397 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009398 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -05009399 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009400
9401 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +11009402 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -06009403 printk(KERN_ERR "lpfc: Cannot re-enable "
9404 "PCI device after reset.\n");
9405 return PCI_ERS_RESULT_DISCONNECT;
9406 }
9407
James Smart97207482008-12-04 22:39:19 -05009408 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05009409
9410 /*
9411 * As the new kernel behavior of pci_restore_state() API call clears
9412 * device saved_state flag, need to save the restored state again.
9413 */
9414 pci_save_state(pdev);
9415
James Smart97207482008-12-04 22:39:19 -05009416 if (pdev->is_busmaster)
9417 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009418
James Smart92d7f7b2007-06-17 19:56:38 -05009419 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04009420 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05009421 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009422
James Smart5b75da22008-12-04 22:39:35 -05009423 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009424 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05009425 if (intr_mode == LPFC_INTR_ERROR) {
9426 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9427 "0427 Cannot re-enable interrupt after "
9428 "slot reset.\n");
9429 return PCI_ERS_RESULT_DISCONNECT;
9430 } else
9431 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009432
James Smart75baf692010-06-08 18:31:21 -04009433 /* Take device offline, it will perform cleanup */
James Smart618a5232012-06-12 13:54:36 -04009434 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009435 lpfc_offline(phba);
9436 lpfc_sli_brdrestart(phba);
9437
James Smart5b75da22008-12-04 22:39:35 -05009438 /* Log the current active interrupt mode */
9439 lpfc_log_intr_mode(phba, phba->intr_mode);
9440
Linas Vepstas8d63f372007-02-14 14:28:36 -06009441 return PCI_ERS_RESULT_RECOVERED;
9442}
9443
9444/**
James Smart3772a992009-05-22 14:50:54 -04009445 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -04009446 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -06009447 *
James Smart3772a992009-05-22 14:50:54 -04009448 * This routine is called from the PCI subsystem for error handling to device
9449 * with SLI-3 interface spec. It is called when kernel error recovery tells
9450 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
9451 * error recovery. After this call, traffic can start to flow from this device
9452 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009453 */
James Smart3772a992009-05-22 14:50:54 -04009454static void
9455lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009456{
James Smart51ef4c22007-08-02 11:10:31 -04009457 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9458 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009459
James Smarte2af0d22010-03-15 11:25:32 -04009460 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -04009461 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -04009462
9463 /* Clean up Advanced Error Reporting (AER) if needed */
9464 if (phba->hba_flag & HBA_AER_ENABLED)
9465 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009466}
9467
James Smart3772a992009-05-22 14:50:54 -04009468/**
James Smartda0436e2009-05-22 14:51:39 -04009469 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
9470 * @phba: pointer to lpfc hba data structure.
9471 *
9472 * returns the number of ELS/CT IOCBs to reserve
9473 **/
9474int
9475lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
9476{
9477 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
9478
James Smartf1126682009-06-10 17:22:44 -04009479 if (phba->sli_rev == LPFC_SLI_REV4) {
9480 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -04009481 return 10;
James Smartf1126682009-06-10 17:22:44 -04009482 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -04009483 return 25;
James Smartf1126682009-06-10 17:22:44 -04009484 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -04009485 return 50;
James Smartf1126682009-06-10 17:22:44 -04009486 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -04009487 return 100;
James Smart8a9d2e82012-05-09 21:16:12 -04009488 else if (max_xri <= 1536)
James Smart6a9c52c2009-10-02 15:16:51 -04009489 return 150;
James Smart8a9d2e82012-05-09 21:16:12 -04009490 else if (max_xri <= 2048)
9491 return 200;
9492 else
9493 return 250;
James Smartf1126682009-06-10 17:22:44 -04009494 } else
9495 return 0;
James Smartda0436e2009-05-22 14:51:39 -04009496}
9497
9498/**
James Smart52d52442011-05-24 11:42:45 -04009499 * lpfc_write_firmware - attempt to write a firmware image to the port
James Smart52d52442011-05-24 11:42:45 -04009500 * @fw: pointer to firmware image returned from request_firmware.
James Smartce396282012-09-29 11:30:56 -04009501 * @phba: pointer to lpfc hba data structure.
James Smart52d52442011-05-24 11:42:45 -04009502 *
James Smart52d52442011-05-24 11:42:45 -04009503 **/
James Smartce396282012-09-29 11:30:56 -04009504static void
9505lpfc_write_firmware(const struct firmware *fw, void *context)
James Smart52d52442011-05-24 11:42:45 -04009506{
James Smartce396282012-09-29 11:30:56 -04009507 struct lpfc_hba *phba = (struct lpfc_hba *)context;
James Smart6b5151f2012-01-18 16:24:06 -05009508 char fwrev[FW_REV_STR_SIZE];
James Smartce396282012-09-29 11:30:56 -04009509 struct lpfc_grp_hdr *image;
James Smart52d52442011-05-24 11:42:45 -04009510 struct list_head dma_buffer_list;
9511 int i, rc = 0;
9512 struct lpfc_dmabuf *dmabuf, *next;
9513 uint32_t offset = 0, temp_offset = 0;
9514
James Smartc71ab862012-10-31 14:44:33 -04009515 /* It can be null in no-wait mode, sanity check */
James Smartce396282012-09-29 11:30:56 -04009516 if (!fw) {
9517 rc = -ENXIO;
9518 goto out;
9519 }
9520 image = (struct lpfc_grp_hdr *)fw->data;
9521
James Smart52d52442011-05-24 11:42:45 -04009522 INIT_LIST_HEAD(&dma_buffer_list);
James Smart079b5c92011-08-21 21:48:49 -04009523 if ((be32_to_cpu(image->magic_number) != LPFC_GROUP_OJECT_MAGIC_NUM) ||
9524 (bf_get_be32(lpfc_grp_hdr_file_type, image) !=
9525 LPFC_FILE_TYPE_GROUP) ||
9526 (bf_get_be32(lpfc_grp_hdr_id, image) != LPFC_FILE_ID_GROUP) ||
9527 (be32_to_cpu(image->size) != fw->size)) {
James Smart52d52442011-05-24 11:42:45 -04009528 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9529 "3022 Invalid FW image found. "
James Smart079b5c92011-08-21 21:48:49 -04009530 "Magic:%x Type:%x ID:%x\n",
9531 be32_to_cpu(image->magic_number),
9532 bf_get_be32(lpfc_grp_hdr_file_type, image),
9533 bf_get_be32(lpfc_grp_hdr_id, image));
James Smartce396282012-09-29 11:30:56 -04009534 rc = -EINVAL;
9535 goto release_out;
James Smart52d52442011-05-24 11:42:45 -04009536 }
9537 lpfc_decode_firmware_rev(phba, fwrev, 1);
James Smart88a2cfb2011-07-22 18:36:33 -04009538 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
James Smart52d52442011-05-24 11:42:45 -04009539 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartce396282012-09-29 11:30:56 -04009540 "3023 Updating Firmware, Current Version:%s "
James Smart52d52442011-05-24 11:42:45 -04009541 "New Version:%s\n",
James Smart88a2cfb2011-07-22 18:36:33 -04009542 fwrev, image->revision);
James Smart52d52442011-05-24 11:42:45 -04009543 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
9544 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
9545 GFP_KERNEL);
9546 if (!dmabuf) {
9547 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -04009548 goto release_out;
James Smart52d52442011-05-24 11:42:45 -04009549 }
9550 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
9551 SLI4_PAGE_SIZE,
9552 &dmabuf->phys,
9553 GFP_KERNEL);
9554 if (!dmabuf->virt) {
9555 kfree(dmabuf);
9556 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -04009557 goto release_out;
James Smart52d52442011-05-24 11:42:45 -04009558 }
9559 list_add_tail(&dmabuf->list, &dma_buffer_list);
9560 }
9561 while (offset < fw->size) {
9562 temp_offset = offset;
9563 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
James Smart079b5c92011-08-21 21:48:49 -04009564 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
James Smart52d52442011-05-24 11:42:45 -04009565 memcpy(dmabuf->virt,
9566 fw->data + temp_offset,
James Smart079b5c92011-08-21 21:48:49 -04009567 fw->size - temp_offset);
9568 temp_offset = fw->size;
James Smart52d52442011-05-24 11:42:45 -04009569 break;
9570 }
James Smart52d52442011-05-24 11:42:45 -04009571 memcpy(dmabuf->virt, fw->data + temp_offset,
9572 SLI4_PAGE_SIZE);
James Smart88a2cfb2011-07-22 18:36:33 -04009573 temp_offset += SLI4_PAGE_SIZE;
James Smart52d52442011-05-24 11:42:45 -04009574 }
9575 rc = lpfc_wr_object(phba, &dma_buffer_list,
9576 (fw->size - offset), &offset);
James Smartce396282012-09-29 11:30:56 -04009577 if (rc)
9578 goto release_out;
James Smart52d52442011-05-24 11:42:45 -04009579 }
9580 rc = offset;
9581 }
James Smartce396282012-09-29 11:30:56 -04009582
9583release_out:
James Smart52d52442011-05-24 11:42:45 -04009584 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
9585 list_del(&dmabuf->list);
9586 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
9587 dmabuf->virt, dmabuf->phys);
9588 kfree(dmabuf);
9589 }
James Smartce396282012-09-29 11:30:56 -04009590 release_firmware(fw);
9591out:
9592 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartc71ab862012-10-31 14:44:33 -04009593 "3024 Firmware update done: %d.\n", rc);
James Smartce396282012-09-29 11:30:56 -04009594 return;
James Smart52d52442011-05-24 11:42:45 -04009595}
9596
9597/**
James Smartc71ab862012-10-31 14:44:33 -04009598 * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
9599 * @phba: pointer to lpfc hba data structure.
9600 *
9601 * This routine is called to perform Linux generic firmware upgrade on device
9602 * that supports such feature.
9603 **/
9604int
9605lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
9606{
9607 uint8_t file_name[ELX_MODEL_NAME_SIZE];
9608 int ret;
9609 const struct firmware *fw;
9610
9611 /* Only supported on SLI4 interface type 2 for now */
9612 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
9613 LPFC_SLI_INTF_IF_TYPE_2)
9614 return -EPERM;
9615
9616 snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
9617
9618 if (fw_upgrade == INT_FW_UPGRADE) {
9619 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
9620 file_name, &phba->pcidev->dev,
9621 GFP_KERNEL, (void *)phba,
9622 lpfc_write_firmware);
9623 } else if (fw_upgrade == RUN_FW_UPGRADE) {
9624 ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
9625 if (!ret)
9626 lpfc_write_firmware(fw, (void *)phba);
9627 } else {
9628 ret = -EINVAL;
9629 }
9630
9631 return ret;
9632}
9633
9634/**
James Smartda0436e2009-05-22 14:51:39 -04009635 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
9636 * @pdev: pointer to PCI device
9637 * @pid: pointer to PCI device identifier
9638 *
9639 * This routine is called from the kernel's PCI subsystem to device with
9640 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
9641 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
9642 * information of the device and driver to see if the driver state that it
9643 * can support this kind of device. If the match is successful, the driver
9644 * core invokes this routine. If this routine determines it can claim the HBA,
9645 * it does all the initialization that it needs to do to handle the HBA
9646 * properly.
9647 *
9648 * Return code
9649 * 0 - driver can claim the device
9650 * negative value - driver can not claim the device
9651 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009652static int
James Smartda0436e2009-05-22 14:51:39 -04009653lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
9654{
9655 struct lpfc_hba *phba;
9656 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04009657 struct Scsi_Host *shost = NULL;
James Smartce396282012-09-29 11:30:56 -04009658 int error, ret;
James Smartda0436e2009-05-22 14:51:39 -04009659 uint32_t cfg_mode, intr_mode;
James Smart67d12732012-08-03 12:36:13 -04009660 int adjusted_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04009661
9662 /* Allocate memory for HBA structure */
9663 phba = lpfc_hba_alloc(pdev);
9664 if (!phba)
9665 return -ENOMEM;
9666
9667 /* Perform generic PCI device enabling operation */
9668 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -04009669 if (error)
James Smartda0436e2009-05-22 14:51:39 -04009670 goto out_free_phba;
James Smartda0436e2009-05-22 14:51:39 -04009671
9672 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
9673 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
9674 if (error)
9675 goto out_disable_pci_dev;
9676
9677 /* Set up SLI-4 specific device PCI memory space */
9678 error = lpfc_sli4_pci_mem_setup(phba);
9679 if (error) {
9680 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9681 "1410 Failed to set up pci memory space.\n");
9682 goto out_disable_pci_dev;
9683 }
9684
9685 /* Set up phase-1 common device driver resources */
9686 error = lpfc_setup_driver_resource_phase1(phba);
9687 if (error) {
9688 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9689 "1411 Failed to set up driver resource.\n");
9690 goto out_unset_pci_mem_s4;
9691 }
9692
9693 /* Set up SLI-4 Specific device driver resources */
9694 error = lpfc_sli4_driver_resource_setup(phba);
9695 if (error) {
9696 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9697 "1412 Failed to set up driver resource.\n");
9698 goto out_unset_pci_mem_s4;
9699 }
9700
9701 /* Initialize and populate the iocb list per host */
James Smart2a9bf3d2010-06-07 15:24:45 -04009702
9703 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9704 "2821 initialize iocb list %d.\n",
9705 phba->cfg_iocb_cnt*1024);
9706 error = lpfc_init_iocb_list(phba, phba->cfg_iocb_cnt*1024);
9707
James Smartda0436e2009-05-22 14:51:39 -04009708 if (error) {
9709 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9710 "1413 Failed to initialize iocb list.\n");
9711 goto out_unset_driver_resource_s4;
9712 }
9713
James Smart19ca7602010-11-20 23:11:55 -05009714 INIT_LIST_HEAD(&phba->active_rrq_list);
James Smart7d791df2011-07-22 18:37:52 -04009715 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
James Smart19ca7602010-11-20 23:11:55 -05009716
James Smartda0436e2009-05-22 14:51:39 -04009717 /* Set up common device driver resources */
9718 error = lpfc_setup_driver_resource_phase2(phba);
9719 if (error) {
9720 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9721 "1414 Failed to set up driver resource.\n");
9722 goto out_free_iocb_list;
9723 }
9724
James Smart079b5c92011-08-21 21:48:49 -04009725 /* Get the default values for Model Name and Description */
9726 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
9727
James Smartda0436e2009-05-22 14:51:39 -04009728 /* Create SCSI host to the physical port */
9729 error = lpfc_create_shost(phba);
9730 if (error) {
9731 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9732 "1415 Failed to create scsi host.\n");
9733 goto out_unset_driver_resource;
9734 }
9735
9736 /* Configure sysfs attributes */
9737 vport = phba->pport;
9738 error = lpfc_alloc_sysfs_attr(vport);
9739 if (error) {
9740 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9741 "1416 Failed to allocate sysfs attr\n");
9742 goto out_destroy_shost;
9743 }
9744
James Smart6669f9b2009-10-02 15:16:45 -04009745 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -04009746 /* Now, trying to enable interrupt and bring up the device */
9747 cfg_mode = phba->cfg_use_msi;
James Smartda0436e2009-05-22 14:51:39 -04009748
James Smart7b15db32013-01-03 15:43:29 -05009749 /* Put device to a known state before enabling interrupt */
9750 lpfc_stop_port(phba);
9751 /* Configure and enable interrupt */
9752 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
9753 if (intr_mode == LPFC_INTR_ERROR) {
9754 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9755 "0426 Failed to enable interrupt.\n");
9756 error = -ENODEV;
9757 goto out_free_sysfs_attr;
James Smartda0436e2009-05-22 14:51:39 -04009758 }
James Smart7b15db32013-01-03 15:43:29 -05009759 /* Default to single EQ for non-MSI-X */
9760 if (phba->intr_type != MSIX)
9761 adjusted_fcp_io_channel = 1;
9762 else
9763 adjusted_fcp_io_channel = phba->cfg_fcp_io_channel;
9764 phba->cfg_fcp_io_channel = adjusted_fcp_io_channel;
9765 /* Set up SLI-4 HBA */
9766 if (lpfc_sli4_hba_setup(phba)) {
9767 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9768 "1421 Failed to set up hba\n");
9769 error = -ENODEV;
9770 goto out_disable_intr;
9771 }
9772
9773 /* Log the current active interrupt mode */
9774 phba->intr_mode = intr_mode;
9775 lpfc_log_intr_mode(phba, intr_mode);
James Smartda0436e2009-05-22 14:51:39 -04009776
9777 /* Perform post initialization setup */
9778 lpfc_post_init_setup(phba);
9779
James Smartc71ab862012-10-31 14:44:33 -04009780 /* check for firmware upgrade or downgrade */
9781 if (phba->cfg_request_firmware_upgrade)
9782 ret = lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
James Smart52d52442011-05-24 11:42:45 -04009783
James Smart1c6834a2009-07-19 10:01:26 -04009784 /* Check if there are static vports to be created. */
9785 lpfc_create_static_vport(phba);
James Smartda0436e2009-05-22 14:51:39 -04009786 return 0;
9787
9788out_disable_intr:
9789 lpfc_sli4_disable_intr(phba);
9790out_free_sysfs_attr:
9791 lpfc_free_sysfs_attr(vport);
9792out_destroy_shost:
9793 lpfc_destroy_shost(phba);
9794out_unset_driver_resource:
9795 lpfc_unset_driver_resource_phase2(phba);
9796out_free_iocb_list:
9797 lpfc_free_iocb_list(phba);
9798out_unset_driver_resource_s4:
9799 lpfc_sli4_driver_resource_unset(phba);
9800out_unset_pci_mem_s4:
9801 lpfc_sli4_pci_mem_unset(phba);
9802out_disable_pci_dev:
9803 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04009804 if (shost)
9805 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -04009806out_free_phba:
9807 lpfc_hba_free(phba);
9808 return error;
9809}
9810
9811/**
9812 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
9813 * @pdev: pointer to PCI device
9814 *
9815 * This routine is called from the kernel's PCI subsystem to device with
9816 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
9817 * removed from PCI bus, it performs all the necessary cleanup for the HBA
9818 * device to be removed from the PCI subsystem properly.
9819 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009820static void
James Smartda0436e2009-05-22 14:51:39 -04009821lpfc_pci_remove_one_s4(struct pci_dev *pdev)
9822{
9823 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9824 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
9825 struct lpfc_vport **vports;
9826 struct lpfc_hba *phba = vport->phba;
9827 int i;
9828
9829 /* Mark the device unloading flag */
9830 spin_lock_irq(&phba->hbalock);
9831 vport->load_flag |= FC_UNLOADING;
9832 spin_unlock_irq(&phba->hbalock);
9833
9834 /* Free the HBA sysfs attributes */
9835 lpfc_free_sysfs_attr(vport);
9836
9837 /* Release all the vports against this physical port */
9838 vports = lpfc_create_vport_work_array(phba);
9839 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -04009840 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
9841 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
9842 continue;
James Smartda0436e2009-05-22 14:51:39 -04009843 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -04009844 }
James Smartda0436e2009-05-22 14:51:39 -04009845 lpfc_destroy_vport_work_array(phba, vports);
9846
9847 /* Remove FC host and then SCSI host with the physical port */
9848 fc_remove_host(shost);
9849 scsi_remove_host(shost);
9850
9851 /* Perform cleanup on the physical port */
9852 lpfc_cleanup(vport);
9853
9854 /*
9855 * Bring down the SLI Layer. This step disables all interrupts,
9856 * clears the rings, discards all mailbox commands, and resets
9857 * the HBA FCoE function.
9858 */
9859 lpfc_debugfs_terminate(vport);
9860 lpfc_sli4_hba_unset(phba);
9861
9862 spin_lock_irq(&phba->hbalock);
9863 list_del_init(&vport->listentry);
9864 spin_unlock_irq(&phba->hbalock);
9865
James Smart3677a3a2010-09-29 11:19:14 -04009866 /* Perform scsi free before driver resource_unset since scsi
James Smartda0436e2009-05-22 14:51:39 -04009867 * buffers are released to their corresponding pools here.
9868 */
9869 lpfc_scsi_free(phba);
James Smart67d12732012-08-03 12:36:13 -04009870
James Smartda0436e2009-05-22 14:51:39 -04009871 lpfc_sli4_driver_resource_unset(phba);
9872
9873 /* Unmap adapter Control and Doorbell registers */
9874 lpfc_sli4_pci_mem_unset(phba);
9875
9876 /* Release PCI resources and disable device's PCI function */
9877 scsi_host_put(shost);
9878 lpfc_disable_pci_dev(phba);
9879
9880 /* Finally, free the driver's device data structure */
9881 lpfc_hba_free(phba);
9882
9883 return;
9884}
9885
9886/**
9887 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
9888 * @pdev: pointer to PCI device
9889 * @msg: power management message
9890 *
9891 * This routine is called from the kernel's PCI subsystem to support system
9892 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
9893 * this method, it quiesces the device by stopping the driver's worker
9894 * thread for the device, turning off device's interrupt and DMA, and bring
9895 * the device offline. Note that as the driver implements the minimum PM
9896 * requirements to a power-aware driver's PM support for suspend/resume -- all
9897 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
9898 * method call will be treated as SUSPEND and the driver will fully
9899 * reinitialize its device during resume() method call, the driver will set
9900 * device to PCI_D3hot state in PCI config space instead of setting it
9901 * according to the @msg provided by the PM.
9902 *
9903 * Return code
9904 * 0 - driver suspended the device
9905 * Error otherwise
9906 **/
9907static int
9908lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
9909{
9910 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9911 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9912
9913 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart75baf692010-06-08 18:31:21 -04009914 "2843 PCI device Power Management suspend.\n");
James Smartda0436e2009-05-22 14:51:39 -04009915
9916 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -04009917 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smartda0436e2009-05-22 14:51:39 -04009918 lpfc_offline(phba);
9919 kthread_stop(phba->worker_thread);
9920
9921 /* Disable interrupt from device */
9922 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -04009923 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04009924
9925 /* Save device state to PCI config space */
9926 pci_save_state(pdev);
9927 pci_set_power_state(pdev, PCI_D3hot);
9928
9929 return 0;
9930}
9931
9932/**
9933 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
9934 * @pdev: pointer to PCI device
9935 *
9936 * This routine is called from the kernel's PCI subsystem to support system
9937 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
9938 * this method, it restores the device's PCI config space state and fully
9939 * reinitializes the device and brings it online. Note that as the driver
9940 * implements the minimum PM requirements to a power-aware driver's PM for
9941 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
9942 * to the suspend() method call will be treated as SUSPEND and the driver
9943 * will fully reinitialize its device during resume() method call, the device
9944 * will be set to PCI_D0 directly in PCI config space before restoring the
9945 * state.
9946 *
9947 * Return code
9948 * 0 - driver suspended the device
9949 * Error otherwise
9950 **/
9951static int
9952lpfc_pci_resume_one_s4(struct pci_dev *pdev)
9953{
9954 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9955 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9956 uint32_t intr_mode;
9957 int error;
9958
9959 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9960 "0292 PCI device Power Management resume.\n");
9961
9962 /* Restore device state from PCI config space */
9963 pci_set_power_state(pdev, PCI_D0);
9964 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05009965
9966 /*
9967 * As the new kernel behavior of pci_restore_state() API call clears
9968 * device saved_state flag, need to save the restored state again.
9969 */
9970 pci_save_state(pdev);
9971
James Smartda0436e2009-05-22 14:51:39 -04009972 if (pdev->is_busmaster)
9973 pci_set_master(pdev);
9974
9975 /* Startup the kernel thread for this host adapter. */
9976 phba->worker_thread = kthread_run(lpfc_do_work, phba,
9977 "lpfc_worker_%d", phba->brd_no);
9978 if (IS_ERR(phba->worker_thread)) {
9979 error = PTR_ERR(phba->worker_thread);
9980 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9981 "0293 PM resume failed to start worker "
9982 "thread: error=x%x.\n", error);
9983 return error;
9984 }
9985
9986 /* Configure and enable interrupt */
9987 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
9988 if (intr_mode == LPFC_INTR_ERROR) {
9989 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9990 "0294 PM resume Failed to enable interrupt\n");
9991 return -EIO;
9992 } else
9993 phba->intr_mode = intr_mode;
9994
9995 /* Restart HBA and bring it online */
9996 lpfc_sli_brdrestart(phba);
9997 lpfc_online(phba);
9998
9999 /* Log the current active interrupt mode */
10000 lpfc_log_intr_mode(phba, phba->intr_mode);
10001
10002 return 0;
10003}
10004
10005/**
James Smart75baf692010-06-08 18:31:21 -040010006 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
10007 * @phba: pointer to lpfc hba data structure.
10008 *
10009 * This routine is called to prepare the SLI4 device for PCI slot recover. It
10010 * aborts all the outstanding SCSI I/Os to the pci device.
10011 **/
10012static void
10013lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
10014{
10015 struct lpfc_sli *psli = &phba->sli;
10016 struct lpfc_sli_ring *pring;
10017
10018 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10019 "2828 PCI channel I/O abort preparing for recovery\n");
10020 /*
10021 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
10022 * and let the SCSI mid-layer to retry them to recover.
10023 */
10024 pring = &psli->ring[psli->fcp_ring];
10025 lpfc_sli_abort_iocb_ring(phba, pring);
10026}
10027
10028/**
10029 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
10030 * @phba: pointer to lpfc hba data structure.
10031 *
10032 * This routine is called to prepare the SLI4 device for PCI slot reset. It
10033 * disables the device interrupt and pci device, and aborts the internal FCP
10034 * pending I/Os.
10035 **/
10036static void
10037lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
10038{
10039 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10040 "2826 PCI channel disable preparing for reset\n");
10041
10042 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040010043 lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
James Smart75baf692010-06-08 18:31:21 -040010044
10045 /* Block all SCSI devices' I/Os on the host */
10046 lpfc_scsi_dev_block(phba);
10047
10048 /* stop all timers */
10049 lpfc_stop_hba_timers(phba);
10050
10051 /* Disable interrupt and pci device */
10052 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040010053 lpfc_sli4_queue_destroy(phba);
James Smart75baf692010-06-08 18:31:21 -040010054 pci_disable_device(phba->pcidev);
10055
10056 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
10057 lpfc_sli_flush_fcp_rings(phba);
10058}
10059
10060/**
10061 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
10062 * @phba: pointer to lpfc hba data structure.
10063 *
10064 * This routine is called to prepare the SLI4 device for PCI slot permanently
10065 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
10066 * pending I/Os.
10067 **/
10068static void
10069lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
10070{
10071 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10072 "2827 PCI channel permanent disable for failure\n");
10073
10074 /* Block all SCSI devices' I/Os on the host */
10075 lpfc_scsi_dev_block(phba);
10076
10077 /* stop all timers */
10078 lpfc_stop_hba_timers(phba);
10079
10080 /* Clean up all driver's outstanding SCSI I/Os */
10081 lpfc_sli_flush_fcp_rings(phba);
10082}
10083
10084/**
James Smartda0436e2009-05-22 14:51:39 -040010085 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
10086 * @pdev: pointer to PCI device.
10087 * @state: the current PCI connection state.
10088 *
10089 * This routine is called from the PCI subsystem for error handling to device
10090 * with SLI-4 interface spec. This function is called by the PCI subsystem
10091 * after a PCI bus error affecting this device has been detected. When this
10092 * function is invoked, it will need to stop all the I/Os and interrupt(s)
10093 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
10094 * for the PCI subsystem to perform proper recovery as desired.
10095 *
10096 * Return codes
10097 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10098 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10099 **/
10100static pci_ers_result_t
10101lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
10102{
James Smart75baf692010-06-08 18:31:21 -040010103 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10104 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10105
10106 switch (state) {
10107 case pci_channel_io_normal:
10108 /* Non-fatal error, prepare for recovery */
10109 lpfc_sli4_prep_dev_for_recover(phba);
10110 return PCI_ERS_RESULT_CAN_RECOVER;
10111 case pci_channel_io_frozen:
10112 /* Fatal error, prepare for slot reset */
10113 lpfc_sli4_prep_dev_for_reset(phba);
10114 return PCI_ERS_RESULT_NEED_RESET;
10115 case pci_channel_io_perm_failure:
10116 /* Permanent failure, prepare for device down */
10117 lpfc_sli4_prep_dev_for_perm_failure(phba);
10118 return PCI_ERS_RESULT_DISCONNECT;
10119 default:
10120 /* Unknown state, prepare and request slot reset */
10121 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10122 "2825 Unknown PCI error state: x%x\n", state);
10123 lpfc_sli4_prep_dev_for_reset(phba);
10124 return PCI_ERS_RESULT_NEED_RESET;
10125 }
James Smartda0436e2009-05-22 14:51:39 -040010126}
10127
10128/**
10129 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
10130 * @pdev: pointer to PCI device.
10131 *
10132 * This routine is called from the PCI subsystem for error handling to device
10133 * with SLI-4 interface spec. It is called after PCI bus has been reset to
10134 * restart the PCI card from scratch, as if from a cold-boot. During the
10135 * PCI subsystem error recovery, after the driver returns
10136 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
10137 * recovery and then call this routine before calling the .resume method to
10138 * recover the device. This function will initialize the HBA device, enable
10139 * the interrupt, but it will just put the HBA to offline state without
10140 * passing any I/O traffic.
10141 *
10142 * Return codes
10143 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10144 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10145 */
10146static pci_ers_result_t
10147lpfc_io_slot_reset_s4(struct pci_dev *pdev)
10148{
James Smart75baf692010-06-08 18:31:21 -040010149 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10150 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10151 struct lpfc_sli *psli = &phba->sli;
10152 uint32_t intr_mode;
10153
10154 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
10155 if (pci_enable_device_mem(pdev)) {
10156 printk(KERN_ERR "lpfc: Cannot re-enable "
10157 "PCI device after reset.\n");
10158 return PCI_ERS_RESULT_DISCONNECT;
10159 }
10160
10161 pci_restore_state(pdev);
James Smart0a96e972011-07-22 18:37:28 -040010162
10163 /*
10164 * As the new kernel behavior of pci_restore_state() API call clears
10165 * device saved_state flag, need to save the restored state again.
10166 */
10167 pci_save_state(pdev);
10168
James Smart75baf692010-06-08 18:31:21 -040010169 if (pdev->is_busmaster)
10170 pci_set_master(pdev);
10171
10172 spin_lock_irq(&phba->hbalock);
10173 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
10174 spin_unlock_irq(&phba->hbalock);
10175
10176 /* Configure and enable interrupt */
10177 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
10178 if (intr_mode == LPFC_INTR_ERROR) {
10179 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10180 "2824 Cannot re-enable interrupt after "
10181 "slot reset.\n");
10182 return PCI_ERS_RESULT_DISCONNECT;
10183 } else
10184 phba->intr_mode = intr_mode;
10185
10186 /* Log the current active interrupt mode */
10187 lpfc_log_intr_mode(phba, phba->intr_mode);
10188
James Smartda0436e2009-05-22 14:51:39 -040010189 return PCI_ERS_RESULT_RECOVERED;
10190}
10191
10192/**
10193 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
10194 * @pdev: pointer to PCI device
10195 *
10196 * This routine is called from the PCI subsystem for error handling to device
10197 * with SLI-4 interface spec. It is called when kernel error recovery tells
10198 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
10199 * error recovery. After this call, traffic can start to flow from this device
10200 * again.
10201 **/
10202static void
10203lpfc_io_resume_s4(struct pci_dev *pdev)
10204{
James Smart75baf692010-06-08 18:31:21 -040010205 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10206 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10207
10208 /*
10209 * In case of slot reset, as function reset is performed through
10210 * mailbox command which needs DMA to be enabled, this operation
10211 * has to be moved to the io resume phase. Taking device offline
10212 * will perform the necessary cleanup.
10213 */
10214 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
10215 /* Perform device reset */
James Smart618a5232012-06-12 13:54:36 -040010216 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040010217 lpfc_offline(phba);
10218 lpfc_sli_brdrestart(phba);
10219 /* Bring the device back online */
10220 lpfc_online(phba);
10221 }
10222
10223 /* Clean up Advanced Error Reporting (AER) if needed */
10224 if (phba->hba_flag & HBA_AER_ENABLED)
10225 pci_cleanup_aer_uncorrect_error_status(pdev);
James Smartda0436e2009-05-22 14:51:39 -040010226}
10227
10228/**
James Smart3772a992009-05-22 14:50:54 -040010229 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
10230 * @pdev: pointer to PCI device
10231 * @pid: pointer to PCI device identifier
10232 *
10233 * This routine is to be registered to the kernel's PCI subsystem. When an
10234 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
10235 * at PCI device-specific information of the device and driver to see if the
10236 * driver state that it can support this kind of device. If the match is
10237 * successful, the driver core invokes this routine. This routine dispatches
10238 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
10239 * do all the initialization that it needs to do to handle the HBA device
10240 * properly.
10241 *
10242 * Return code
10243 * 0 - driver can claim the device
10244 * negative value - driver can not claim the device
10245 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010246static int
James Smart3772a992009-05-22 14:50:54 -040010247lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
10248{
10249 int rc;
James Smart8fa38512009-07-19 10:01:03 -040010250 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -040010251
James Smart28baac72010-02-12 14:42:03 -050010252 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -040010253 return -ENODEV;
10254
James Smart8fa38512009-07-19 10:01:03 -040010255 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -050010256 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -040010257 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040010258 else
James Smart3772a992009-05-22 14:50:54 -040010259 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040010260
James Smart3772a992009-05-22 14:50:54 -040010261 return rc;
10262}
10263
10264/**
10265 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
10266 * @pdev: pointer to PCI device
10267 *
10268 * This routine is to be registered to the kernel's PCI subsystem. When an
10269 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
10270 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
10271 * remove routine, which will perform all the necessary cleanup for the
10272 * device to be removed from the PCI subsystem properly.
10273 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010274static void
James Smart3772a992009-05-22 14:50:54 -040010275lpfc_pci_remove_one(struct pci_dev *pdev)
10276{
10277 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10278 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10279
10280 switch (phba->pci_dev_grp) {
10281 case LPFC_PCI_DEV_LP:
10282 lpfc_pci_remove_one_s3(pdev);
10283 break;
James Smartda0436e2009-05-22 14:51:39 -040010284 case LPFC_PCI_DEV_OC:
10285 lpfc_pci_remove_one_s4(pdev);
10286 break;
James Smart3772a992009-05-22 14:50:54 -040010287 default:
10288 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10289 "1424 Invalid PCI device group: 0x%x\n",
10290 phba->pci_dev_grp);
10291 break;
10292 }
10293 return;
10294}
10295
10296/**
10297 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
10298 * @pdev: pointer to PCI device
10299 * @msg: power management message
10300 *
10301 * This routine is to be registered to the kernel's PCI subsystem to support
10302 * system Power Management (PM). When PM invokes this method, it dispatches
10303 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
10304 * suspend the device.
10305 *
10306 * Return code
10307 * 0 - driver suspended the device
10308 * Error otherwise
10309 **/
10310static int
10311lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
10312{
10313 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10314 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10315 int rc = -ENODEV;
10316
10317 switch (phba->pci_dev_grp) {
10318 case LPFC_PCI_DEV_LP:
10319 rc = lpfc_pci_suspend_one_s3(pdev, msg);
10320 break;
James Smartda0436e2009-05-22 14:51:39 -040010321 case LPFC_PCI_DEV_OC:
10322 rc = lpfc_pci_suspend_one_s4(pdev, msg);
10323 break;
James Smart3772a992009-05-22 14:50:54 -040010324 default:
10325 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10326 "1425 Invalid PCI device group: 0x%x\n",
10327 phba->pci_dev_grp);
10328 break;
10329 }
10330 return rc;
10331}
10332
10333/**
10334 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
10335 * @pdev: pointer to PCI device
10336 *
10337 * This routine is to be registered to the kernel's PCI subsystem to support
10338 * system Power Management (PM). When PM invokes this method, it dispatches
10339 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
10340 * resume the device.
10341 *
10342 * Return code
10343 * 0 - driver suspended the device
10344 * Error otherwise
10345 **/
10346static int
10347lpfc_pci_resume_one(struct pci_dev *pdev)
10348{
10349 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10350 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10351 int rc = -ENODEV;
10352
10353 switch (phba->pci_dev_grp) {
10354 case LPFC_PCI_DEV_LP:
10355 rc = lpfc_pci_resume_one_s3(pdev);
10356 break;
James Smartda0436e2009-05-22 14:51:39 -040010357 case LPFC_PCI_DEV_OC:
10358 rc = lpfc_pci_resume_one_s4(pdev);
10359 break;
James Smart3772a992009-05-22 14:50:54 -040010360 default:
10361 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10362 "1426 Invalid PCI device group: 0x%x\n",
10363 phba->pci_dev_grp);
10364 break;
10365 }
10366 return rc;
10367}
10368
10369/**
10370 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
10371 * @pdev: pointer to PCI device.
10372 * @state: the current PCI connection state.
10373 *
10374 * This routine is registered to the PCI subsystem for error handling. This
10375 * function is called by the PCI subsystem after a PCI bus error affecting
10376 * this device has been detected. When this routine is invoked, it dispatches
10377 * the action to the proper SLI-3 or SLI-4 device error detected handling
10378 * routine, which will perform the proper error detected operation.
10379 *
10380 * Return codes
10381 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10382 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10383 **/
10384static pci_ers_result_t
10385lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
10386{
10387 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10388 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10389 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
10390
10391 switch (phba->pci_dev_grp) {
10392 case LPFC_PCI_DEV_LP:
10393 rc = lpfc_io_error_detected_s3(pdev, state);
10394 break;
James Smartda0436e2009-05-22 14:51:39 -040010395 case LPFC_PCI_DEV_OC:
10396 rc = lpfc_io_error_detected_s4(pdev, state);
10397 break;
James Smart3772a992009-05-22 14:50:54 -040010398 default:
10399 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10400 "1427 Invalid PCI device group: 0x%x\n",
10401 phba->pci_dev_grp);
10402 break;
10403 }
10404 return rc;
10405}
10406
10407/**
10408 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
10409 * @pdev: pointer to PCI device.
10410 *
10411 * This routine is registered to the PCI subsystem for error handling. This
10412 * function is called after PCI bus has been reset to restart the PCI card
10413 * from scratch, as if from a cold-boot. When this routine is invoked, it
10414 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
10415 * routine, which will perform the proper device reset.
10416 *
10417 * Return codes
10418 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10419 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10420 **/
10421static pci_ers_result_t
10422lpfc_io_slot_reset(struct pci_dev *pdev)
10423{
10424 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10425 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10426 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
10427
10428 switch (phba->pci_dev_grp) {
10429 case LPFC_PCI_DEV_LP:
10430 rc = lpfc_io_slot_reset_s3(pdev);
10431 break;
James Smartda0436e2009-05-22 14:51:39 -040010432 case LPFC_PCI_DEV_OC:
10433 rc = lpfc_io_slot_reset_s4(pdev);
10434 break;
James Smart3772a992009-05-22 14:50:54 -040010435 default:
10436 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10437 "1428 Invalid PCI device group: 0x%x\n",
10438 phba->pci_dev_grp);
10439 break;
10440 }
10441 return rc;
10442}
10443
10444/**
10445 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
10446 * @pdev: pointer to PCI device
10447 *
10448 * This routine is registered to the PCI subsystem for error handling. It
10449 * is called when kernel error recovery tells the lpfc driver that it is
10450 * OK to resume normal PCI operation after PCI bus error recovery. When
10451 * this routine is invoked, it dispatches the action to the proper SLI-3
10452 * or SLI-4 device io_resume routine, which will resume the device operation.
10453 **/
10454static void
10455lpfc_io_resume(struct pci_dev *pdev)
10456{
10457 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10458 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10459
10460 switch (phba->pci_dev_grp) {
10461 case LPFC_PCI_DEV_LP:
10462 lpfc_io_resume_s3(pdev);
10463 break;
James Smartda0436e2009-05-22 14:51:39 -040010464 case LPFC_PCI_DEV_OC:
10465 lpfc_io_resume_s4(pdev);
10466 break;
James Smart3772a992009-05-22 14:50:54 -040010467 default:
10468 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10469 "1429 Invalid PCI device group: 0x%x\n",
10470 phba->pci_dev_grp);
10471 break;
10472 }
10473 return;
10474}
10475
James Smart3ef6d242012-01-18 16:23:48 -050010476/**
10477 * lpfc_mgmt_open - method called when 'lpfcmgmt' is opened from userspace
10478 * @inode: pointer to the inode representing the lpfcmgmt device
10479 * @filep: pointer to the file representing the open lpfcmgmt device
10480 *
10481 * This routine puts a reference count on the lpfc module whenever the
10482 * character device is opened
10483 **/
10484static int
10485lpfc_mgmt_open(struct inode *inode, struct file *filep)
10486{
10487 try_module_get(THIS_MODULE);
10488 return 0;
10489}
10490
10491/**
10492 * lpfc_mgmt_release - method called when 'lpfcmgmt' is closed in userspace
10493 * @inode: pointer to the inode representing the lpfcmgmt device
10494 * @filep: pointer to the file representing the open lpfcmgmt device
10495 *
10496 * This routine removes a reference count from the lpfc module when the
10497 * character device is closed
10498 **/
10499static int
10500lpfc_mgmt_release(struct inode *inode, struct file *filep)
10501{
10502 module_put(THIS_MODULE);
10503 return 0;
10504}
10505
dea31012005-04-17 16:05:31 -050010506static struct pci_device_id lpfc_id_table[] = {
10507 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
10508 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -040010509 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
10510 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010511 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
10512 PCI_ANY_ID, PCI_ANY_ID, },
10513 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
10514 PCI_ANY_ID, PCI_ANY_ID, },
10515 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
10516 PCI_ANY_ID, PCI_ANY_ID, },
10517 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
10518 PCI_ANY_ID, PCI_ANY_ID, },
10519 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
10520 PCI_ANY_ID, PCI_ANY_ID, },
10521 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
10522 PCI_ANY_ID, PCI_ANY_ID, },
10523 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
10524 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050010525 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
10526 PCI_ANY_ID, PCI_ANY_ID, },
10527 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
10528 PCI_ANY_ID, PCI_ANY_ID, },
10529 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
10530 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010531 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
10532 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050010533 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
10534 PCI_ANY_ID, PCI_ANY_ID, },
10535 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
10536 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010537 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
10538 PCI_ANY_ID, PCI_ANY_ID, },
10539 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
10540 PCI_ANY_ID, PCI_ANY_ID, },
10541 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
10542 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -040010543 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
10544 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050010545 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
10546 PCI_ANY_ID, PCI_ANY_ID, },
10547 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
10548 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010549 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
10550 PCI_ANY_ID, PCI_ANY_ID, },
10551 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
10552 PCI_ANY_ID, PCI_ANY_ID, },
10553 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
10554 PCI_ANY_ID, PCI_ANY_ID, },
10555 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
10556 PCI_ANY_ID, PCI_ANY_ID, },
10557 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
10558 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050010559 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
10560 PCI_ANY_ID, PCI_ANY_ID, },
10561 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
10562 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -040010563 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
10564 PCI_ANY_ID, PCI_ANY_ID, },
10565 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
10566 PCI_ANY_ID, PCI_ANY_ID, },
10567 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
10568 PCI_ANY_ID, PCI_ANY_ID, },
10569 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
10570 PCI_ANY_ID, PCI_ANY_ID, },
10571 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
10572 PCI_ANY_ID, PCI_ANY_ID, },
10573 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
10574 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -040010575 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
10576 PCI_ANY_ID, PCI_ANY_ID, },
10577 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
10578 PCI_ANY_ID, PCI_ANY_ID, },
10579 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
10580 PCI_ANY_ID, PCI_ANY_ID, },
James Smart3772a992009-05-22 14:50:54 -040010581 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
10582 PCI_ANY_ID, PCI_ANY_ID, },
James Smarta747c9c2009-11-18 15:41:10 -050010583 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
10584 PCI_ANY_ID, PCI_ANY_ID, },
10585 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
James Smart6669f9b2009-10-02 15:16:45 -040010586 PCI_ANY_ID, PCI_ANY_ID, },
James Smart98fc5dd2010-06-07 15:24:29 -040010587 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BALIUS,
10588 PCI_ANY_ID, PCI_ANY_ID, },
James Smart085c6472010-11-20 23:11:37 -050010589 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC,
10590 PCI_ANY_ID, PCI_ANY_ID, },
10591 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE,
10592 PCI_ANY_ID, PCI_ANY_ID, },
James Smartc0c11512011-05-24 11:41:34 -040010593 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC_VF,
10594 PCI_ANY_ID, PCI_ANY_ID, },
10595 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE_VF,
10596 PCI_ANY_ID, PCI_ANY_ID, },
James Smartf8cafd32012-08-03 12:42:51 -040010597 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SKYHAWK,
10598 PCI_ANY_ID, PCI_ANY_ID, },
10599 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SKYHAWK_VF,
10600 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010601 { 0 }
10602};
10603
10604MODULE_DEVICE_TABLE(pci, lpfc_id_table);
10605
Stephen Hemmingera55b2d22012-09-07 09:33:16 -070010606static const struct pci_error_handlers lpfc_err_handler = {
Linas Vepstas8d63f372007-02-14 14:28:36 -060010607 .error_detected = lpfc_io_error_detected,
10608 .slot_reset = lpfc_io_slot_reset,
10609 .resume = lpfc_io_resume,
10610};
10611
dea31012005-04-17 16:05:31 -050010612static struct pci_driver lpfc_driver = {
10613 .name = LPFC_DRIVER_NAME,
10614 .id_table = lpfc_id_table,
10615 .probe = lpfc_pci_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010616 .remove = lpfc_pci_remove_one,
James Smart3a55b532008-12-04 22:38:54 -050010617 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -040010618 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -050010619 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -050010620};
10621
James Smart3ef6d242012-01-18 16:23:48 -050010622static const struct file_operations lpfc_mgmt_fop = {
10623 .open = lpfc_mgmt_open,
10624 .release = lpfc_mgmt_release,
10625};
10626
10627static struct miscdevice lpfc_mgmt_dev = {
10628 .minor = MISC_DYNAMIC_MINOR,
10629 .name = "lpfcmgmt",
10630 .fops = &lpfc_mgmt_fop,
10631};
10632
James Smarte59058c2008-08-24 21:49:00 -040010633/**
James Smart3621a712009-04-06 18:47:14 -040010634 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -040010635 *
10636 * This routine is to be invoked when the lpfc module is loaded into the
10637 * kernel. The special kernel macro module_init() is used to indicate the
10638 * role of this routine to the kernel as lpfc module entry point.
10639 *
10640 * Return codes
10641 * 0 - successful
10642 * -ENOMEM - FC attach transport failed
10643 * all others - failed
10644 */
dea31012005-04-17 16:05:31 -050010645static int __init
10646lpfc_init(void)
10647{
10648 int error = 0;
10649
10650 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010651 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -050010652
James Smart3ef6d242012-01-18 16:23:48 -050010653 error = misc_register(&lpfc_mgmt_dev);
10654 if (error)
10655 printk(KERN_ERR "Could not register lpfcmgmt device, "
10656 "misc_register returned with status %d", error);
10657
James Smart7ee5d432007-10-27 13:37:17 -040010658 if (lpfc_enable_npiv) {
10659 lpfc_transport_functions.vport_create = lpfc_vport_create;
10660 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
10661 }
dea31012005-04-17 16:05:31 -050010662 lpfc_transport_template =
10663 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -040010664 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -050010665 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -040010666 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -040010667 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -040010668 fc_attach_transport(&lpfc_vport_transport_functions);
10669 if (lpfc_vport_transport_template == NULL) {
10670 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040010671 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -040010672 }
James Smart7ee5d432007-10-27 13:37:17 -040010673 }
dea31012005-04-17 16:05:31 -050010674 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -050010675 if (error) {
dea31012005-04-17 16:05:31 -050010676 fc_release_transport(lpfc_transport_template);
James Smartd7c255b2008-08-24 21:50:00 -040010677 if (lpfc_enable_npiv)
10678 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -050010679 }
dea31012005-04-17 16:05:31 -050010680
10681 return error;
10682}
10683
James Smarte59058c2008-08-24 21:49:00 -040010684/**
James Smart3621a712009-04-06 18:47:14 -040010685 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -040010686 *
10687 * This routine is invoked when the lpfc module is removed from the kernel.
10688 * The special kernel macro module_exit() is used to indicate the role of
10689 * this routine to the kernel as lpfc module exit point.
10690 */
dea31012005-04-17 16:05:31 -050010691static void __exit
10692lpfc_exit(void)
10693{
James Smart3ef6d242012-01-18 16:23:48 -050010694 misc_deregister(&lpfc_mgmt_dev);
dea31012005-04-17 16:05:31 -050010695 pci_unregister_driver(&lpfc_driver);
10696 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040010697 if (lpfc_enable_npiv)
10698 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -050010699 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040010700 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
10701 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050010702 (1L << _dump_buf_data_order), _dump_buf_data);
10703 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
10704 }
10705
10706 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -040010707 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
10708 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050010709 (1L << _dump_buf_dif_order), _dump_buf_dif);
10710 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
10711 }
dea31012005-04-17 16:05:31 -050010712}
10713
10714module_init(lpfc_init);
10715module_exit(lpfc_exit);
10716MODULE_LICENSE("GPL");
10717MODULE_DESCRIPTION(LPFC_MODULE_DESC);
10718MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
10719MODULE_VERSION("0:" LPFC_DRIVER_VERSION);