blob: b28f4e7c3bc63ce3a1073bb40d6b68429db30dad [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smartd4379ac2012-03-01 22:37:07 -05004 * Copyright (C) 2004-2012 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/delay.h>
24#include <linux/dma-mapping.h>
25#include <linux/idr.h>
26#include <linux/interrupt.h>
Paul Gortmakeracf3368f2011-05-27 09:47:43 -040027#include <linux/module.h>
dea31012005-04-17 16:05:31 -050028#include <linux/kthread.h>
29#include <linux/pci.h>
30#include <linux/spinlock.h>
James Smart92d7f7b2007-06-17 19:56:38 -050031#include <linux/ctype.h>
James Smart0d878412009-10-02 15:16:56 -040032#include <linux/aer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
James Smart52d52442011-05-24 11:42:45 -040034#include <linux/firmware.h>
James Smart3ef6d242012-01-18 16:23:48 -050035#include <linux/miscdevice.h>
dea31012005-04-17 16:05:31 -050036
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040037#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050038#include <scsi/scsi_device.h>
39#include <scsi/scsi_host.h>
40#include <scsi/scsi_transport_fc.h>
41
James Smartda0436e2009-05-22 14:51:39 -040042#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050043#include "lpfc_hw.h"
44#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040045#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040046#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050047#include "lpfc_disc.h"
48#include "lpfc_scsi.h"
49#include "lpfc.h"
50#include "lpfc_logmsg.h"
51#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050052#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050053#include "lpfc_version.h"
54
James Smart81301a92008-12-04 22:39:46 -050055char *_dump_buf_data;
56unsigned long _dump_buf_data_order;
57char *_dump_buf_dif;
58unsigned long _dump_buf_dif_order;
59spinlock_t _dump_buf_lock;
60
dea31012005-04-17 16:05:31 -050061static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
62static int lpfc_post_rcv_buf(struct lpfc_hba *);
James Smart5350d872011-10-10 21:33:49 -040063static int lpfc_sli4_queue_verify(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040064static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
65static int lpfc_setup_endian_order(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040066static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
James Smart8a9d2e82012-05-09 21:16:12 -040067static void lpfc_free_els_sgl_list(struct lpfc_hba *);
68static void lpfc_init_sgl_list(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040069static int lpfc_init_active_sgl_array(struct lpfc_hba *);
70static void lpfc_free_active_sgl(struct lpfc_hba *);
71static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
72static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
73static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
74static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
75static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
James Smart618a5232012-06-12 13:54:36 -040076static void lpfc_sli4_disable_intr(struct lpfc_hba *);
77static uint32_t lpfc_sli4_enable_intr(struct lpfc_hba *, uint32_t);
dea31012005-04-17 16:05:31 -050078
79static struct scsi_transport_template *lpfc_transport_template = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050080static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea31012005-04-17 16:05:31 -050081static DEFINE_IDR(lpfc_hba_index);
82
James Smarte59058c2008-08-24 21:49:00 -040083/**
James Smart3621a712009-04-06 18:47:14 -040084 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
James Smarte59058c2008-08-24 21:49:00 -040085 * @phba: pointer to lpfc hba data structure.
86 *
87 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
88 * mailbox command. It retrieves the revision information from the HBA and
89 * collects the Vital Product Data (VPD) about the HBA for preparing the
90 * configuration of the HBA.
91 *
92 * Return codes:
93 * 0 - success.
94 * -ERESTART - requests the SLI layer to reset the HBA and try again.
95 * Any other value - indicates an error.
96 **/
dea31012005-04-17 16:05:31 -050097int
James Smart2e0fef82007-06-17 19:56:36 -050098lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050099{
100 lpfc_vpd_t *vp = &phba->vpd;
101 int i = 0, rc;
102 LPFC_MBOXQ_t *pmb;
103 MAILBOX_t *mb;
104 char *lpfc_vpd_data = NULL;
105 uint16_t offset = 0;
106 static char licensed[56] =
107 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -0400108 static int init_key = 1;
dea31012005-04-17 16:05:31 -0500109
110 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
111 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500112 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500113 return -ENOMEM;
114 }
115
James Smart04c68492009-05-22 14:52:52 -0400116 mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -0500117 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500118
119 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -0400120 if (init_key) {
121 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -0500122
James Smart65a29c12006-07-06 15:50:50 -0400123 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
124 *ptext = cpu_to_be32(*ptext);
125 init_key = 0;
126 }
dea31012005-04-17 16:05:31 -0500127
128 lpfc_read_nv(phba, pmb);
129 memset((char*)mb->un.varRDnvp.rsvd3, 0,
130 sizeof (mb->un.varRDnvp.rsvd3));
131 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
132 sizeof (licensed));
133
134 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
135
136 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500137 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smarte8b62012007-08-02 11:10:09 -0400138 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500139 "error, mbxCmd x%x READ_NVPARM, "
140 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500141 mb->mbxCommand, mb->mbxStatus);
142 mempool_free(pmb, phba->mbox_mem_pool);
143 return -ERESTART;
144 }
145 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500146 sizeof(phba->wwnn));
147 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
148 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500149 }
150
James Smart92d7f7b2007-06-17 19:56:38 -0500151 phba->sli3_options = 0x0;
152
dea31012005-04-17 16:05:31 -0500153 /* Setup and issue mailbox READ REV command */
154 lpfc_read_rev(phba, pmb);
155 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
156 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500157 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400158 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500159 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500160 mb->mbxCommand, mb->mbxStatus);
161 mempool_free( pmb, phba->mbox_mem_pool);
162 return -ERESTART;
163 }
164
James Smart92d7f7b2007-06-17 19:56:38 -0500165
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500166 /*
167 * The value of rr must be 1 since the driver set the cv field to 1.
168 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500169 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500170 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500171 vp->rev.rBit = 0;
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500172 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400173 "0440 Adapter failed to init, READ_REV has "
174 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500175 mempool_free(pmb, phba->mbox_mem_pool);
176 return -ERESTART;
dea31012005-04-17 16:05:31 -0500177 }
178
James Smart495a7142008-06-14 22:52:59 -0400179 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
180 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -0500181 return -EINVAL;
James Smart495a7142008-06-14 22:52:59 -0400182 }
James Smarted957682007-06-17 19:56:37 -0500183
dea31012005-04-17 16:05:31 -0500184 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500185 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500186 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500187 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
188 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
189 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
190 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500191 vp->rev.biuRev = mb->un.varRdRev.biuRev;
192 vp->rev.smRev = mb->un.varRdRev.smRev;
193 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
194 vp->rev.endecRev = mb->un.varRdRev.endecRev;
195 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
196 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
197 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
198 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
199 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
200 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
201
James Smart92d7f7b2007-06-17 19:56:38 -0500202 /* If the sli feature level is less then 9, we must
203 * tear down all RPIs and VPIs on link down if NPIV
204 * is enabled.
205 */
206 if (vp->rev.feaLevelHigh < 9)
207 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
208
dea31012005-04-17 16:05:31 -0500209 if (lpfc_is_LC_HBA(phba->pcidev->device))
210 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
211 sizeof (phba->RandomData));
212
dea31012005-04-17 16:05:31 -0500213 /* Get adapter VPD information */
dea31012005-04-17 16:05:31 -0500214 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
215 if (!lpfc_vpd_data)
James Smartd7c255b2008-08-24 21:50:00 -0400216 goto out_free_mbox;
dea31012005-04-17 16:05:31 -0500217 do {
James Smarta0c87cb2009-07-19 10:01:10 -0400218 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
dea31012005-04-17 16:05:31 -0500219 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
220
221 if (rc != MBX_SUCCESS) {
222 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400223 "0441 VPD not present on adapter, "
dea31012005-04-17 16:05:31 -0500224 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500225 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500226 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500227 }
James Smart04c68492009-05-22 14:52:52 -0400228 /* dump mem may return a zero when finished or we got a
229 * mailbox error, either way we are done.
230 */
231 if (mb->un.varDmp.word_cnt == 0)
232 break;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500233 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
234 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
James Smartd7c255b2008-08-24 21:50:00 -0400235 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
236 lpfc_vpd_data + offset,
James Smart92d7f7b2007-06-17 19:56:38 -0500237 mb->un.varDmp.word_cnt);
dea31012005-04-17 16:05:31 -0500238 offset += mb->un.varDmp.word_cnt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500239 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
240 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500241
242 kfree(lpfc_vpd_data);
dea31012005-04-17 16:05:31 -0500243out_free_mbox:
244 mempool_free(pmb, phba->mbox_mem_pool);
245 return 0;
246}
247
James Smarte59058c2008-08-24 21:49:00 -0400248/**
James Smart3621a712009-04-06 18:47:14 -0400249 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
James Smarte59058c2008-08-24 21:49:00 -0400250 * @phba: pointer to lpfc hba data structure.
251 * @pmboxq: pointer to the driver internal queue element for mailbox command.
252 *
253 * This is the completion handler for driver's configuring asynchronous event
254 * mailbox command to the device. If the mailbox command returns successfully,
255 * it will set internal async event support flag to 1; otherwise, it will
256 * set internal async event support flag to 0.
257 **/
James Smart57127f12007-10-27 13:37:05 -0400258static void
259lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
260{
James Smart04c68492009-05-22 14:52:52 -0400261 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
James Smart57127f12007-10-27 13:37:05 -0400262 phba->temp_sensor_support = 1;
263 else
264 phba->temp_sensor_support = 0;
265 mempool_free(pmboxq, phba->mbox_mem_pool);
266 return;
267}
268
James Smarte59058c2008-08-24 21:49:00 -0400269/**
James Smart3621a712009-04-06 18:47:14 -0400270 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
James Smart97207482008-12-04 22:39:19 -0500271 * @phba: pointer to lpfc hba data structure.
272 * @pmboxq: pointer to the driver internal queue element for mailbox command.
273 *
274 * This is the completion handler for dump mailbox command for getting
275 * wake up parameters. When this command complete, the response contain
276 * Option rom version of the HBA. This function translate the version number
277 * into a human readable string and store it in OptionROMVersion.
278 **/
279static void
280lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
281{
282 struct prog_id *prg;
283 uint32_t prog_id_word;
284 char dist = ' ';
285 /* character array used for decoding dist type. */
286 char dist_char[] = "nabx";
287
James Smart04c68492009-05-22 14:52:52 -0400288 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
James Smart9f1e1b52008-12-04 22:39:40 -0500289 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500290 return;
James Smart9f1e1b52008-12-04 22:39:40 -0500291 }
James Smart97207482008-12-04 22:39:19 -0500292
293 prg = (struct prog_id *) &prog_id_word;
294
295 /* word 7 contain option rom version */
James Smart04c68492009-05-22 14:52:52 -0400296 prog_id_word = pmboxq->u.mb.un.varWords[7];
James Smart97207482008-12-04 22:39:19 -0500297
298 /* Decode the Option rom version word to a readable string */
299 if (prg->dist < 4)
300 dist = dist_char[prg->dist];
301
302 if ((prg->dist == 3) && (prg->num == 0))
303 sprintf(phba->OptionROMVersion, "%d.%d%d",
304 prg->ver, prg->rev, prg->lev);
305 else
306 sprintf(phba->OptionROMVersion, "%d.%d%d%c%d",
307 prg->ver, prg->rev, prg->lev,
308 dist, prg->num);
James Smart9f1e1b52008-12-04 22:39:40 -0500309 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500310 return;
311}
312
313/**
James Smart05580562011-05-24 11:40:48 -0400314 * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
315 * cfg_soft_wwnn, cfg_soft_wwpn
316 * @vport: pointer to lpfc vport data structure.
317 *
318 *
319 * Return codes
320 * None.
321 **/
322void
323lpfc_update_vport_wwn(struct lpfc_vport *vport)
324{
325 /* If the soft name exists then update it using the service params */
326 if (vport->phba->cfg_soft_wwnn)
327 u64_to_wwn(vport->phba->cfg_soft_wwnn,
328 vport->fc_sparam.nodeName.u.wwn);
329 if (vport->phba->cfg_soft_wwpn)
330 u64_to_wwn(vport->phba->cfg_soft_wwpn,
331 vport->fc_sparam.portName.u.wwn);
332
333 /*
334 * If the name is empty or there exists a soft name
335 * then copy the service params name, otherwise use the fc name
336 */
337 if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
338 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
339 sizeof(struct lpfc_name));
340 else
341 memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
342 sizeof(struct lpfc_name));
343
344 if (vport->fc_portname.u.wwn[0] == 0 || vport->phba->cfg_soft_wwpn)
345 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
346 sizeof(struct lpfc_name));
347 else
348 memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
349 sizeof(struct lpfc_name));
350}
351
352/**
James Smart3621a712009-04-06 18:47:14 -0400353 * lpfc_config_port_post - Perform lpfc initialization after config port
James Smarte59058c2008-08-24 21:49:00 -0400354 * @phba: pointer to lpfc hba data structure.
355 *
356 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
357 * command call. It performs all internal resource and state setups on the
358 * port: post IOCB buffers, enable appropriate host interrupt attentions,
359 * ELS ring timers, etc.
360 *
361 * Return codes
362 * 0 - success.
363 * Any other value - error.
364 **/
dea31012005-04-17 16:05:31 -0500365int
James Smart2e0fef82007-06-17 19:56:36 -0500366lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500367{
James Smart2e0fef82007-06-17 19:56:36 -0500368 struct lpfc_vport *vport = phba->pport;
James Smarta257bf92009-04-06 18:48:10 -0400369 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -0500370 LPFC_MBOXQ_t *pmb;
371 MAILBOX_t *mb;
372 struct lpfc_dmabuf *mp;
373 struct lpfc_sli *psli = &phba->sli;
374 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500375 int i, j;
376 int rc;
dea31012005-04-17 16:05:31 -0500377
James Smart7af67052007-10-27 13:38:11 -0400378 spin_lock_irq(&phba->hbalock);
379 /*
380 * If the Config port completed correctly the HBA is not
381 * over heated any more.
382 */
383 if (phba->over_temp_state == HBA_OVER_TEMP)
384 phba->over_temp_state = HBA_NORMAL_TEMP;
385 spin_unlock_irq(&phba->hbalock);
386
dea31012005-04-17 16:05:31 -0500387 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
388 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500389 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500390 return -ENOMEM;
391 }
James Smart04c68492009-05-22 14:52:52 -0400392 mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500393
dea31012005-04-17 16:05:31 -0500394 /* Get login parameters for NID. */
James Smart9f1177a2010-02-26 14:12:57 -0500395 rc = lpfc_read_sparam(phba, pmb, 0);
396 if (rc) {
397 mempool_free(pmb, phba->mbox_mem_pool);
398 return -ENOMEM;
399 }
400
James Smarted957682007-06-17 19:56:37 -0500401 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500402 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500403 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400404 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500405 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500406 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500407 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500408 mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart9f1177a2010-02-26 14:12:57 -0500409 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500410 lpfc_mbuf_free(phba, mp->virt, mp->phys);
411 kfree(mp);
412 return -EIO;
413 }
414
415 mp = (struct lpfc_dmabuf *) pmb->context1;
416
James Smart2e0fef82007-06-17 19:56:36 -0500417 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500418 lpfc_mbuf_free(phba, mp->virt, mp->phys);
419 kfree(mp);
420 pmb->context1 = NULL;
James Smart05580562011-05-24 11:40:48 -0400421 lpfc_update_vport_wwn(vport);
James Smarta257bf92009-04-06 18:48:10 -0400422
423 /* Update the fc_host data structures with new wwn. */
424 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
425 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart21e9a0a2009-05-22 14:53:21 -0400426 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smarta257bf92009-04-06 18:48:10 -0400427
dea31012005-04-17 16:05:31 -0500428 /* If no serial number in VPD data, use low 6 bytes of WWNN */
429 /* This should be consolidated into parse_vpd ? - mr */
430 if (phba->SerialNumber[0] == 0) {
431 uint8_t *outptr;
432
James Smart2e0fef82007-06-17 19:56:36 -0500433 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500434 for (i = 0; i < 12; i++) {
435 status = *outptr++;
436 j = ((status & 0xf0) >> 4);
437 if (j <= 9)
438 phba->SerialNumber[i] =
439 (char)((uint8_t) 0x30 + (uint8_t) j);
440 else
441 phba->SerialNumber[i] =
442 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
443 i++;
444 j = (status & 0xf);
445 if (j <= 9)
446 phba->SerialNumber[i] =
447 (char)((uint8_t) 0x30 + (uint8_t) j);
448 else
449 phba->SerialNumber[i] =
450 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
451 }
452 }
453
dea31012005-04-17 16:05:31 -0500454 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500455 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500456 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500457 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400458 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500459 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500460 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500461 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500462 mempool_free( pmb, phba->mbox_mem_pool);
463 return -EIO;
464 }
465
James Smarta0c87cb2009-07-19 10:01:10 -0400466 /* Check if the port is disabled */
467 lpfc_sli_read_link_ste(phba);
468
dea31012005-04-17 16:05:31 -0500469 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
470 if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
471 phba->cfg_hba_queue_depth =
James Smartf1126682009-06-10 17:22:44 -0400472 (mb->un.varRdConfig.max_xri + 1) -
473 lpfc_sli4_get_els_iocb_cnt(phba);
dea31012005-04-17 16:05:31 -0500474
475 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500476
477 /* Get the default values for Model Name and Description */
478 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
479
James Smart2e0fef82007-06-17 19:56:36 -0500480 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500481
James Smart0b727fe2007-10-27 13:37:25 -0400482 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smart7e56aa22012-08-03 12:35:34 -0400483 if (psli->ring[psli->extra_ring].sli.sli3.cmdringaddr)
James Smarta4bc3372006-12-02 13:34:16 -0500484 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
James Smart7e56aa22012-08-03 12:35:34 -0400485 if (psli->ring[psli->fcp_ring].sli.sli3.cmdringaddr)
dea31012005-04-17 16:05:31 -0500486 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
James Smart7e56aa22012-08-03 12:35:34 -0400487 if (psli->ring[psli->next_ring].sli.sli3.cmdringaddr)
dea31012005-04-17 16:05:31 -0500488 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
489
490 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500491 if (phba->sli_rev != 3)
492 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500493
James Smart93996272008-08-24 21:50:30 -0400494 /*
495 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
496 */
497 if (phba->intr_type == MSIX) {
498 rc = lpfc_config_msi(phba, pmb);
499 if (rc) {
500 mempool_free(pmb, phba->mbox_mem_pool);
501 return -EIO;
502 }
503 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
504 if (rc != MBX_SUCCESS) {
505 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
506 "0352 Config MSI mailbox command "
507 "failed, mbxCmd x%x, mbxStatus x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400508 pmb->u.mb.mbxCommand,
509 pmb->u.mb.mbxStatus);
James Smart93996272008-08-24 21:50:30 -0400510 mempool_free(pmb, phba->mbox_mem_pool);
511 return -EIO;
512 }
513 }
514
James Smart04c68492009-05-22 14:52:52 -0400515 spin_lock_irq(&phba->hbalock);
James Smart93996272008-08-24 21:50:30 -0400516 /* Initialize ERATT handling flag */
517 phba->hba_flag &= ~HBA_ERATT_HANDLED;
518
dea31012005-04-17 16:05:31 -0500519 /* Enable appropriate host interrupts */
James Smart9940b972011-03-11 16:06:12 -0500520 if (lpfc_readl(phba->HCregaddr, &status)) {
521 spin_unlock_irq(&phba->hbalock);
522 return -EIO;
523 }
dea31012005-04-17 16:05:31 -0500524 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
525 if (psli->num_rings > 0)
526 status |= HC_R0INT_ENA;
527 if (psli->num_rings > 1)
528 status |= HC_R1INT_ENA;
529 if (psli->num_rings > 2)
530 status |= HC_R2INT_ENA;
531 if (psli->num_rings > 3)
532 status |= HC_R3INT_ENA;
533
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500534 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
535 (phba->cfg_poll & DISABLE_FCP_RING_INT))
James Smart93996272008-08-24 21:50:30 -0400536 status &= ~(HC_R0INT_ENA);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500537
dea31012005-04-17 16:05:31 -0500538 writel(status, phba->HCregaddr);
539 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500540 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500541
James Smart93996272008-08-24 21:50:30 -0400542 /* Set up ring-0 (ELS) timer */
543 timeout = phba->fc_ratov * 2;
James Smart2e0fef82007-06-17 19:56:36 -0500544 mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
James Smart93996272008-08-24 21:50:30 -0400545 /* Set up heart beat (HB) timer */
James Smart858c9f62007-06-17 19:56:39 -0500546 mod_timer(&phba->hb_tmofunc, jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
547 phba->hb_outstanding = 0;
548 phba->last_completion_time = jiffies;
James Smart93996272008-08-24 21:50:30 -0400549 /* Set up error attention (ERATT) polling timer */
550 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
dea31012005-04-17 16:05:31 -0500551
James Smarta0c87cb2009-07-19 10:01:10 -0400552 if (phba->hba_flag & LINK_DISABLED) {
553 lpfc_printf_log(phba,
554 KERN_ERR, LOG_INIT,
555 "2598 Adapter Link is disabled.\n");
556 lpfc_down_link(phba, pmb);
557 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
558 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
559 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
560 lpfc_printf_log(phba,
561 KERN_ERR, LOG_INIT,
562 "2599 Adapter failed to issue DOWN_LINK"
563 " mbox command rc 0x%x\n", rc);
564
565 mempool_free(pmb, phba->mbox_mem_pool);
566 return -EIO;
567 }
James Smarte40a02c2010-02-26 14:13:54 -0500568 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart026abb82011-12-13 13:20:45 -0500569 mempool_free(pmb, phba->mbox_mem_pool);
570 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
571 if (rc)
572 return rc;
dea31012005-04-17 16:05:31 -0500573 }
574 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400575 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500576 if (!pmb) {
577 phba->link_state = LPFC_HBA_ERROR;
578 return -ENOMEM;
579 }
580
James Smart57127f12007-10-27 13:37:05 -0400581 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
582 pmb->mbox_cmpl = lpfc_config_async_cmpl;
583 pmb->vport = phba->pport;
584 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500585
James Smart57127f12007-10-27 13:37:05 -0400586 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
587 lpfc_printf_log(phba,
588 KERN_ERR,
589 LOG_INIT,
590 "0456 Adapter failed to issue "
James Smarte4e74272009-07-19 10:01:38 -0400591 "ASYNCEVT_ENABLE mbox status x%x\n",
James Smart57127f12007-10-27 13:37:05 -0400592 rc);
593 mempool_free(pmb, phba->mbox_mem_pool);
594 }
James Smart97207482008-12-04 22:39:19 -0500595
596 /* Get Option rom version */
597 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500598 if (!pmb) {
599 phba->link_state = LPFC_HBA_ERROR;
600 return -ENOMEM;
601 }
602
James Smart97207482008-12-04 22:39:19 -0500603 lpfc_dump_wakeup_param(phba, pmb);
604 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
605 pmb->vport = phba->pport;
606 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
607
608 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
609 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
James Smarte4e74272009-07-19 10:01:38 -0400610 "to get Option ROM version status x%x\n", rc);
James Smart97207482008-12-04 22:39:19 -0500611 mempool_free(pmb, phba->mbox_mem_pool);
612 }
613
James Smartd7c255b2008-08-24 21:50:00 -0400614 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400615}
616
James Smarte59058c2008-08-24 21:49:00 -0400617/**
James Smart84d1b002010-02-12 14:42:33 -0500618 * lpfc_hba_init_link - Initialize the FC link
619 * @phba: pointer to lpfc hba data structure.
James Smart6e7288d2010-06-07 15:23:35 -0400620 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500621 *
622 * This routine will issue the INIT_LINK mailbox command call.
623 * It is available to other drivers through the lpfc_hba data
624 * structure for use as a delayed link up mechanism with the
625 * module parameter lpfc_suppress_link_up.
626 *
627 * Return code
628 * 0 - success
629 * Any other value - error
630 **/
631int
James Smart6e7288d2010-06-07 15:23:35 -0400632lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500633{
James Smart1b511972011-12-13 13:23:09 -0500634 return lpfc_hba_init_link_fc_topology(phba, phba->cfg_topology, flag);
635}
636
637/**
638 * lpfc_hba_init_link_fc_topology - Initialize FC link with desired topology
639 * @phba: pointer to lpfc hba data structure.
640 * @fc_topology: desired fc topology.
641 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
642 *
643 * This routine will issue the INIT_LINK mailbox command call.
644 * It is available to other drivers through the lpfc_hba data
645 * structure for use as a delayed link up mechanism with the
646 * module parameter lpfc_suppress_link_up.
647 *
648 * Return code
649 * 0 - success
650 * Any other value - error
651 **/
652int
653lpfc_hba_init_link_fc_topology(struct lpfc_hba *phba, uint32_t fc_topology,
654 uint32_t flag)
655{
James Smart84d1b002010-02-12 14:42:33 -0500656 struct lpfc_vport *vport = phba->pport;
657 LPFC_MBOXQ_t *pmb;
658 MAILBOX_t *mb;
659 int rc;
660
661 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
662 if (!pmb) {
663 phba->link_state = LPFC_HBA_ERROR;
664 return -ENOMEM;
665 }
666 mb = &pmb->u.mb;
667 pmb->vport = vport;
668
James Smart026abb82011-12-13 13:20:45 -0500669 if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_MAX) ||
670 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G) &&
671 !(phba->lmt & LMT_1Gb)) ||
672 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G) &&
673 !(phba->lmt & LMT_2Gb)) ||
674 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G) &&
675 !(phba->lmt & LMT_4Gb)) ||
676 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G) &&
677 !(phba->lmt & LMT_8Gb)) ||
678 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G) &&
679 !(phba->lmt & LMT_10Gb)) ||
680 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G) &&
681 !(phba->lmt & LMT_16Gb))) {
682 /* Reset link speed to auto */
683 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
684 "1302 Invalid speed for this board:%d "
685 "Reset link speed to auto.\n",
686 phba->cfg_link_speed);
687 phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
688 }
James Smart1b511972011-12-13 13:23:09 -0500689 lpfc_init_link(phba, pmb, fc_topology, phba->cfg_link_speed);
James Smart84d1b002010-02-12 14:42:33 -0500690 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart1b511972011-12-13 13:23:09 -0500691 if (phba->sli_rev < LPFC_SLI_REV4)
692 lpfc_set_loopback_flag(phba);
James Smart6e7288d2010-06-07 15:23:35 -0400693 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart76a95d72010-11-20 23:11:48 -0500694 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
James Smart84d1b002010-02-12 14:42:33 -0500695 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
696 "0498 Adapter failed to init, mbxCmd x%x "
697 "INIT_LINK, mbxStatus x%x\n",
698 mb->mbxCommand, mb->mbxStatus);
James Smart76a95d72010-11-20 23:11:48 -0500699 if (phba->sli_rev <= LPFC_SLI_REV3) {
700 /* Clear all interrupt enable conditions */
701 writel(0, phba->HCregaddr);
702 readl(phba->HCregaddr); /* flush */
703 /* Clear all pending interrupts */
704 writel(0xffffffff, phba->HAregaddr);
705 readl(phba->HAregaddr); /* flush */
706 }
James Smart84d1b002010-02-12 14:42:33 -0500707 phba->link_state = LPFC_HBA_ERROR;
James Smart6e7288d2010-06-07 15:23:35 -0400708 if (rc != MBX_BUSY || flag == MBX_POLL)
James Smart84d1b002010-02-12 14:42:33 -0500709 mempool_free(pmb, phba->mbox_mem_pool);
710 return -EIO;
711 }
James Smarte40a02c2010-02-26 14:13:54 -0500712 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
James Smart6e7288d2010-06-07 15:23:35 -0400713 if (flag == MBX_POLL)
714 mempool_free(pmb, phba->mbox_mem_pool);
James Smart84d1b002010-02-12 14:42:33 -0500715
716 return 0;
717}
718
719/**
720 * lpfc_hba_down_link - this routine downs the FC link
James Smart6e7288d2010-06-07 15:23:35 -0400721 * @phba: pointer to lpfc hba data structure.
722 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500723 *
724 * This routine will issue the DOWN_LINK mailbox command call.
725 * It is available to other drivers through the lpfc_hba data
726 * structure for use to stop the link.
727 *
728 * Return code
729 * 0 - success
730 * Any other value - error
731 **/
732int
James Smart6e7288d2010-06-07 15:23:35 -0400733lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500734{
735 LPFC_MBOXQ_t *pmb;
736 int rc;
737
738 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
739 if (!pmb) {
740 phba->link_state = LPFC_HBA_ERROR;
741 return -ENOMEM;
742 }
743
744 lpfc_printf_log(phba,
745 KERN_ERR, LOG_INIT,
746 "0491 Adapter Link is disabled.\n");
747 lpfc_down_link(phba, pmb);
748 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6e7288d2010-06-07 15:23:35 -0400749 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart84d1b002010-02-12 14:42:33 -0500750 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
751 lpfc_printf_log(phba,
752 KERN_ERR, LOG_INIT,
753 "2522 Adapter failed to issue DOWN_LINK"
754 " mbox command rc 0x%x\n", rc);
755
756 mempool_free(pmb, phba->mbox_mem_pool);
757 return -EIO;
758 }
James Smart6e7288d2010-06-07 15:23:35 -0400759 if (flag == MBX_POLL)
760 mempool_free(pmb, phba->mbox_mem_pool);
761
James Smart84d1b002010-02-12 14:42:33 -0500762 return 0;
763}
764
765/**
James Smart3621a712009-04-06 18:47:14 -0400766 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400767 * @phba: pointer to lpfc HBA data structure.
768 *
769 * This routine will do LPFC uninitialization before the HBA is reset when
770 * bringing down the SLI Layer.
771 *
772 * Return codes
773 * 0 - success.
774 * Any other value - error.
775 **/
dea31012005-04-17 16:05:31 -0500776int
James Smart2e0fef82007-06-17 19:56:36 -0500777lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500778{
James Smart1b32f6a2008-02-08 18:49:39 -0500779 struct lpfc_vport **vports;
780 int i;
James Smart3772a992009-05-22 14:50:54 -0400781
782 if (phba->sli_rev <= LPFC_SLI_REV3) {
783 /* Disable interrupts */
784 writel(0, phba->HCregaddr);
785 readl(phba->HCregaddr); /* flush */
786 }
dea31012005-04-17 16:05:31 -0500787
James Smart1b32f6a2008-02-08 18:49:39 -0500788 if (phba->pport->load_flag & FC_UNLOADING)
789 lpfc_cleanup_discovery_resources(phba->pport);
790 else {
791 vports = lpfc_create_vport_work_array(phba);
792 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400793 for (i = 0; i <= phba->max_vports &&
794 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500795 lpfc_cleanup_discovery_resources(vports[i]);
796 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500797 }
798 return 0;
dea31012005-04-17 16:05:31 -0500799}
800
James Smarte59058c2008-08-24 21:49:00 -0400801/**
James Smart3772a992009-05-22 14:50:54 -0400802 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400803 * @phba: pointer to lpfc HBA data structure.
804 *
805 * This routine will do uninitialization after the HBA is reset when bring
806 * down the SLI Layer.
807 *
808 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200809 * 0 - success.
James Smarte59058c2008-08-24 21:49:00 -0400810 * Any other value - error.
811 **/
James Smart3772a992009-05-22 14:50:54 -0400812static int
813lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -0500814{
815 struct lpfc_sli *psli = &phba->sli;
816 struct lpfc_sli_ring *pring;
817 struct lpfc_dmabuf *mp, *next_mp;
James Smart09372822008-01-11 01:52:54 -0500818 LIST_HEAD(completions);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500819 int i;
820
James Smart92d7f7b2007-06-17 19:56:38 -0500821 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
822 lpfc_sli_hbqbuf_free_all(phba);
823 else {
824 /* Cleanup preposted buffers on the ELS ring */
825 pring = &psli->ring[LPFC_ELS_RING];
826 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
827 list_del(&mp->list);
828 pring->postbufq_cnt--;
829 lpfc_mbuf_free(phba, mp->virt, mp->phys);
830 kfree(mp);
831 }
Jamie Wellnitz41415862006-02-28 19:25:27 -0500832 }
833
James Smart09372822008-01-11 01:52:54 -0500834 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500835 for (i = 0; i < psli->num_rings; i++) {
836 pring = &psli->ring[i];
James Smart09372822008-01-11 01:52:54 -0500837
838 /* At this point in time the HBA is either reset or DOA. Either
839 * way, nothing should be on txcmplq as it will NEVER complete.
840 */
841 list_splice_init(&pring->txcmplq, &completions);
James Smart09372822008-01-11 01:52:54 -0500842 spin_unlock_irq(&phba->hbalock);
843
James Smarta257bf92009-04-06 18:48:10 -0400844 /* Cancel all the IOCBs from the completions list */
845 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
846 IOERR_SLI_ABORTED);
James Smart09372822008-01-11 01:52:54 -0500847
Jamie Wellnitz41415862006-02-28 19:25:27 -0500848 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart09372822008-01-11 01:52:54 -0500849 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500850 }
James Smart09372822008-01-11 01:52:54 -0500851 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500852
853 return 0;
854}
James Smart5af5eee2010-10-22 11:06:38 -0400855
James Smartda0436e2009-05-22 14:51:39 -0400856/**
857 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
858 * @phba: pointer to lpfc HBA data structure.
859 *
860 * This routine will do uninitialization after the HBA is reset when bring
861 * down the SLI Layer.
862 *
863 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200864 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400865 * Any other value - error.
866 **/
867static int
868lpfc_hba_down_post_s4(struct lpfc_hba *phba)
869{
870 struct lpfc_scsi_buf *psb, *psb_next;
871 LIST_HEAD(aborts);
872 int ret;
873 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500874 struct lpfc_sglq *sglq_entry = NULL;
875
James Smartda0436e2009-05-22 14:51:39 -0400876 ret = lpfc_hba_down_post_s3(phba);
877 if (ret)
878 return ret;
879 /* At this point in time the HBA is either reset or DOA. Either
880 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
881 * on the lpfc_sgl_list so that it can either be freed if the
882 * driver is unloading or reposted if the driver is restarting
883 * the port.
884 */
885 spin_lock_irq(&phba->hbalock); /* required for lpfc_sgl_list and */
886 /* scsl_buf_list */
887 /* abts_sgl_list_lock required because worker thread uses this
888 * list.
889 */
890 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -0500891 list_for_each_entry(sglq_entry,
892 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
893 sglq_entry->state = SGL_FREED;
894
James Smartda0436e2009-05-22 14:51:39 -0400895 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
896 &phba->sli4_hba.lpfc_sgl_list);
897 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
898 /* abts_scsi_buf_list_lock required because worker thread uses this
899 * list.
900 */
901 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
902 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
903 &aborts);
904 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
905 spin_unlock_irq(&phba->hbalock);
906
907 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
908 psb->pCmd = NULL;
909 psb->status = IOSTAT_SUCCESS;
910 }
911 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
912 list_splice(&aborts, &phba->lpfc_scsi_buf_list);
913 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
914 return 0;
915}
916
917/**
918 * lpfc_hba_down_post - Wrapper func for hba down post routine
919 * @phba: pointer to lpfc HBA data structure.
920 *
921 * This routine wraps the actual SLI3 or SLI4 routine for performing
922 * uninitialization after the HBA is reset when bring down the SLI Layer.
923 *
924 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200925 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400926 * Any other value - error.
927 **/
928int
929lpfc_hba_down_post(struct lpfc_hba *phba)
930{
931 return (*phba->lpfc_hba_down_post)(phba);
932}
Jamie Wellnitz41415862006-02-28 19:25:27 -0500933
James Smarte59058c2008-08-24 21:49:00 -0400934/**
James Smart3621a712009-04-06 18:47:14 -0400935 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -0400936 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
937 *
938 * This is the HBA-timer timeout handler registered to the lpfc driver. When
939 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
940 * work-port-events bitmap and the worker thread is notified. This timeout
941 * event will be used by the worker thread to invoke the actual timeout
942 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
943 * be performed in the timeout handler and the HBA timeout event bit shall
944 * be cleared by the worker thread after it has taken the event bitmap out.
945 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100946static void
James Smart858c9f62007-06-17 19:56:39 -0500947lpfc_hb_timeout(unsigned long ptr)
948{
949 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400950 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -0500951 unsigned long iflag;
952
953 phba = (struct lpfc_hba *)ptr;
James Smart93996272008-08-24 21:50:30 -0400954
955 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -0500956 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -0400957 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
958 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -0500959 phba->pport->work_port_events |= WORKER_HB_TMO;
960 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
961
James Smart93996272008-08-24 21:50:30 -0400962 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -0400963 if (!tmo_posted)
964 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -0500965 return;
966}
967
James Smarte59058c2008-08-24 21:49:00 -0400968/**
James Smart19ca7602010-11-20 23:11:55 -0500969 * lpfc_rrq_timeout - The RRQ-timer timeout handler
970 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
971 *
972 * This is the RRQ-timer timeout handler registered to the lpfc driver. When
973 * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
974 * work-port-events bitmap and the worker thread is notified. This timeout
975 * event will be used by the worker thread to invoke the actual timeout
976 * handler routine, lpfc_rrq_handler. Any periodical operations will
977 * be performed in the timeout handler and the RRQ timeout event bit shall
978 * be cleared by the worker thread after it has taken the event bitmap out.
979 **/
980static void
981lpfc_rrq_timeout(unsigned long ptr)
982{
983 struct lpfc_hba *phba;
James Smart19ca7602010-11-20 23:11:55 -0500984 unsigned long iflag;
985
986 phba = (struct lpfc_hba *)ptr;
987 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -0500988 phba->hba_flag |= HBA_RRQ_ACTIVE;
James Smart19ca7602010-11-20 23:11:55 -0500989 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -0500990 lpfc_worker_wake_up(phba);
James Smart19ca7602010-11-20 23:11:55 -0500991}
992
993/**
James Smart3621a712009-04-06 18:47:14 -0400994 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -0400995 * @phba: pointer to lpfc hba data structure.
996 * @pmboxq: pointer to the driver internal queue element for mailbox command.
997 *
998 * This is the callback function to the lpfc heart-beat mailbox command.
999 * If configured, the lpfc driver issues the heart-beat mailbox command to
1000 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
1001 * heart-beat mailbox command is issued, the driver shall set up heart-beat
1002 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
1003 * heart-beat outstanding state. Once the mailbox command comes back and
1004 * no error conditions detected, the heart-beat mailbox command timer is
1005 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1006 * state is cleared for the next heart-beat. If the timer expired with the
1007 * heart-beat outstanding state set, the driver will put the HBA offline.
1008 **/
James Smart858c9f62007-06-17 19:56:39 -05001009static void
1010lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1011{
1012 unsigned long drvr_flag;
1013
1014 spin_lock_irqsave(&phba->hbalock, drvr_flag);
1015 phba->hb_outstanding = 0;
1016 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1017
James Smart93996272008-08-24 21:50:30 -04001018 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -05001019 mempool_free(pmboxq, phba->mbox_mem_pool);
1020 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1021 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -04001022 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -05001023 mod_timer(&phba->hb_tmofunc,
1024 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1025 return;
1026}
1027
James Smarte59058c2008-08-24 21:49:00 -04001028/**
James Smart3621a712009-04-06 18:47:14 -04001029 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001030 * @phba: pointer to lpfc hba data structure.
1031 *
1032 * This is the actual HBA-timer timeout handler to be invoked by the worker
1033 * thread whenever the HBA timer fired and HBA-timeout event posted. This
1034 * handler performs any periodic operations needed for the device. If such
1035 * periodic event has already been attended to either in the interrupt handler
1036 * or by processing slow-ring or fast-ring events within the HBA-timer
1037 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1038 * the timer for the next timeout period. If lpfc heart-beat mailbox command
1039 * is configured and there is no heart-beat mailbox command outstanding, a
1040 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1041 * has been a heart-beat mailbox command outstanding, the HBA shall be put
1042 * to offline.
1043 **/
James Smart858c9f62007-06-17 19:56:39 -05001044void
1045lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1046{
James Smart45ed1192009-10-02 15:17:02 -04001047 struct lpfc_vport **vports;
James Smart858c9f62007-06-17 19:56:39 -05001048 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -05001049 struct lpfc_dmabuf *buf_ptr;
James Smart45ed1192009-10-02 15:17:02 -04001050 int retval, i;
James Smart858c9f62007-06-17 19:56:39 -05001051 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -05001052 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -05001053
James Smart45ed1192009-10-02 15:17:02 -04001054 vports = lpfc_create_vport_work_array(phba);
1055 if (vports != NULL)
1056 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
1057 lpfc_rcv_seq_check_edtov(vports[i]);
1058 lpfc_destroy_vport_work_array(phba, vports);
1059
James Smart858c9f62007-06-17 19:56:39 -05001060 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -04001061 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -05001062 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1063 return;
1064
1065 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -05001066
1067 if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
1068 jiffies)) {
1069 spin_unlock_irq(&phba->pport->work_port_lock);
1070 if (!phba->hb_outstanding)
1071 mod_timer(&phba->hb_tmofunc,
1072 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1073 else
1074 mod_timer(&phba->hb_tmofunc,
1075 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
1076 return;
1077 }
1078 spin_unlock_irq(&phba->pport->work_port_lock);
1079
James Smart0ff10d42008-01-11 01:52:36 -05001080 if (phba->elsbuf_cnt &&
1081 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1082 spin_lock_irq(&phba->hbalock);
1083 list_splice_init(&phba->elsbuf, &completions);
1084 phba->elsbuf_cnt = 0;
1085 phba->elsbuf_prev_cnt = 0;
1086 spin_unlock_irq(&phba->hbalock);
1087
1088 while (!list_empty(&completions)) {
1089 list_remove_head(&completions, buf_ptr,
1090 struct lpfc_dmabuf, list);
1091 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1092 kfree(buf_ptr);
1093 }
1094 }
1095 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1096
James Smart858c9f62007-06-17 19:56:39 -05001097 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -05001098 if (phba->cfg_enable_hba_heartbeat) {
1099 if (!phba->hb_outstanding) {
James Smartbc739052010-08-04 16:11:18 -04001100 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1101 (list_empty(&psli->mboxq))) {
1102 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1103 GFP_KERNEL);
1104 if (!pmboxq) {
1105 mod_timer(&phba->hb_tmofunc,
1106 jiffies +
1107 HZ * LPFC_HB_MBOX_INTERVAL);
1108 return;
1109 }
James Smart13815c82008-01-11 01:52:48 -05001110
James Smartbc739052010-08-04 16:11:18 -04001111 lpfc_heart_beat(phba, pmboxq);
1112 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1113 pmboxq->vport = phba->pport;
1114 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1115 MBX_NOWAIT);
James Smart13815c82008-01-11 01:52:48 -05001116
James Smartbc739052010-08-04 16:11:18 -04001117 if (retval != MBX_BUSY &&
1118 retval != MBX_SUCCESS) {
1119 mempool_free(pmboxq,
1120 phba->mbox_mem_pool);
1121 mod_timer(&phba->hb_tmofunc,
1122 jiffies +
1123 HZ * LPFC_HB_MBOX_INTERVAL);
1124 return;
1125 }
1126 phba->skipped_hb = 0;
1127 phba->hb_outstanding = 1;
1128 } else if (time_before_eq(phba->last_completion_time,
1129 phba->skipped_hb)) {
1130 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1131 "2857 Last completion time not "
1132 " updated in %d ms\n",
1133 jiffies_to_msecs(jiffies
1134 - phba->last_completion_time));
1135 } else
1136 phba->skipped_hb = jiffies;
1137
James Smart858c9f62007-06-17 19:56:39 -05001138 mod_timer(&phba->hb_tmofunc,
James Smart13815c82008-01-11 01:52:48 -05001139 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
James Smart858c9f62007-06-17 19:56:39 -05001140 return;
James Smart13815c82008-01-11 01:52:48 -05001141 } else {
1142 /*
1143 * If heart beat timeout called with hb_outstanding set
James Smartdcf2a4e2010-09-29 11:18:53 -04001144 * we need to give the hb mailbox cmd a chance to
1145 * complete or TMO.
James Smart13815c82008-01-11 01:52:48 -05001146 */
James Smartdcf2a4e2010-09-29 11:18:53 -04001147 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1148 "0459 Adapter heartbeat still out"
1149 "standing:last compl time was %d ms.\n",
1150 jiffies_to_msecs(jiffies
1151 - phba->last_completion_time));
1152 mod_timer(&phba->hb_tmofunc,
1153 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
James Smart858c9f62007-06-17 19:56:39 -05001154 }
James Smart858c9f62007-06-17 19:56:39 -05001155 }
1156}
1157
James Smarte59058c2008-08-24 21:49:00 -04001158/**
James Smart3621a712009-04-06 18:47:14 -04001159 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -04001160 * @phba: pointer to lpfc hba data structure.
1161 *
1162 * This routine is called to bring the HBA offline when HBA hardware error
1163 * other than Port Error 6 has been detected.
1164 **/
James Smart09372822008-01-11 01:52:54 -05001165static void
1166lpfc_offline_eratt(struct lpfc_hba *phba)
1167{
1168 struct lpfc_sli *psli = &phba->sli;
1169
1170 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001171 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -05001172 spin_unlock_irq(&phba->hbalock);
James Smart618a5232012-06-12 13:54:36 -04001173 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart09372822008-01-11 01:52:54 -05001174
1175 lpfc_offline(phba);
1176 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001177 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001178 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001179 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001180 lpfc_hba_down_post(phba);
1181 lpfc_sli_brdready(phba, HS_MBRDY);
1182 lpfc_unblock_mgmt_io(phba);
1183 phba->link_state = LPFC_HBA_ERROR;
1184 return;
1185}
1186
James Smarte59058c2008-08-24 21:49:00 -04001187/**
James Smartda0436e2009-05-22 14:51:39 -04001188 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1189 * @phba: pointer to lpfc hba data structure.
1190 *
1191 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1192 * other than Port Error 6 has been detected.
1193 **/
1194static void
1195lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1196{
James Smart618a5232012-06-12 13:54:36 -04001197 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smartda0436e2009-05-22 14:51:39 -04001198 lpfc_offline(phba);
1199 lpfc_sli4_brdreset(phba);
1200 lpfc_hba_down_post(phba);
1201 lpfc_sli4_post_status_check(phba);
1202 lpfc_unblock_mgmt_io(phba);
1203 phba->link_state = LPFC_HBA_ERROR;
1204}
1205
1206/**
James Smarta257bf92009-04-06 18:48:10 -04001207 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1208 * @phba: pointer to lpfc hba data structure.
1209 *
1210 * This routine is invoked to handle the deferred HBA hardware error
1211 * conditions. This type of error is indicated by HBA by setting ER1
1212 * and another ER bit in the host status register. The driver will
1213 * wait until the ER1 bit clears before handling the error condition.
1214 **/
1215static void
1216lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1217{
1218 uint32_t old_host_status = phba->work_hs;
1219 struct lpfc_sli_ring *pring;
1220 struct lpfc_sli *psli = &phba->sli;
1221
James Smartf4b4c682009-05-22 14:53:12 -04001222 /* If the pci channel is offline, ignore possible errors,
1223 * since we cannot communicate with the pci card anyway.
1224 */
1225 if (pci_channel_offline(phba->pcidev)) {
1226 spin_lock_irq(&phba->hbalock);
1227 phba->hba_flag &= ~DEFER_ERATT;
1228 spin_unlock_irq(&phba->hbalock);
1229 return;
1230 }
1231
James Smarta257bf92009-04-06 18:48:10 -04001232 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1233 "0479 Deferred Adapter Hardware Error "
1234 "Data: x%x x%x x%x\n",
1235 phba->work_hs,
1236 phba->work_status[0], phba->work_status[1]);
1237
1238 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001239 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001240 spin_unlock_irq(&phba->hbalock);
1241
1242
1243 /*
1244 * Firmware stops when it triggred erratt. That could cause the I/Os
1245 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1246 * SCSI layer retry it after re-establishing link.
1247 */
1248 pring = &psli->ring[psli->fcp_ring];
1249 lpfc_sli_abort_iocb_ring(phba, pring);
1250
1251 /*
1252 * There was a firmware error. Take the hba offline and then
1253 * attempt to restart it.
1254 */
James Smart618a5232012-06-12 13:54:36 -04001255 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smarta257bf92009-04-06 18:48:10 -04001256 lpfc_offline(phba);
1257
1258 /* Wait for the ER1 bit to clear.*/
1259 while (phba->work_hs & HS_FFER1) {
1260 msleep(100);
James Smart9940b972011-03-11 16:06:12 -05001261 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1262 phba->work_hs = UNPLUG_ERR ;
1263 break;
1264 }
James Smarta257bf92009-04-06 18:48:10 -04001265 /* If driver is unloading let the worker thread continue */
1266 if (phba->pport->load_flag & FC_UNLOADING) {
1267 phba->work_hs = 0;
1268 break;
1269 }
1270 }
1271
1272 /*
1273 * This is to ptrotect against a race condition in which
1274 * first write to the host attention register clear the
1275 * host status register.
1276 */
1277 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1278 phba->work_hs = old_host_status & ~HS_FFER1;
1279
James Smart3772a992009-05-22 14:50:54 -04001280 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001281 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001282 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001283 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1284 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1285}
1286
James Smart3772a992009-05-22 14:50:54 -04001287static void
1288lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1289{
1290 struct lpfc_board_event_header board_event;
1291 struct Scsi_Host *shost;
1292
1293 board_event.event_type = FC_REG_BOARD_EVENT;
1294 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1295 shost = lpfc_shost_from_vport(phba->pport);
1296 fc_host_post_vendor_event(shost, fc_get_event_number(),
1297 sizeof(board_event),
1298 (char *) &board_event,
1299 LPFC_NL_VENDOR_ID);
1300}
1301
James Smarta257bf92009-04-06 18:48:10 -04001302/**
James Smart3772a992009-05-22 14:50:54 -04001303 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001304 * @phba: pointer to lpfc hba data structure.
1305 *
1306 * This routine is invoked to handle the following HBA hardware error
1307 * conditions:
1308 * 1 - HBA error attention interrupt
1309 * 2 - DMA ring index out of range
1310 * 3 - Mailbox command came back as unknown
1311 **/
James Smart3772a992009-05-22 14:50:54 -04001312static void
1313lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001314{
James Smart2e0fef82007-06-17 19:56:36 -05001315 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001316 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001317 struct lpfc_sli_ring *pring;
James Smartd2873e42006-08-18 17:46:43 -04001318 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001319 unsigned long temperature;
1320 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001321 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001322
Linas Vepstas8d63f372007-02-14 14:28:36 -06001323 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001324 * since we cannot communicate with the pci card anyway.
1325 */
1326 if (pci_channel_offline(phba->pcidev)) {
1327 spin_lock_irq(&phba->hbalock);
1328 phba->hba_flag &= ~DEFER_ERATT;
1329 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001330 return;
James Smart3772a992009-05-22 14:50:54 -04001331 }
1332
James Smart13815c82008-01-11 01:52:48 -05001333 /* If resets are disabled then leave the HBA alone and return */
1334 if (!phba->cfg_enable_hba_reset)
1335 return;
dea31012005-04-17 16:05:31 -05001336
James Smartea2151b2008-09-07 11:52:10 -04001337 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001338 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001339
James Smarta257bf92009-04-06 18:48:10 -04001340 if (phba->hba_flag & DEFER_ERATT)
1341 lpfc_handle_deferred_eratt(phba);
1342
James Smartdcf2a4e2010-09-29 11:18:53 -04001343 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1344 if (phba->work_hs & HS_FFER6)
1345 /* Re-establishing Link */
1346 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1347 "1301 Re-establishing Link "
1348 "Data: x%x x%x x%x\n",
1349 phba->work_hs, phba->work_status[0],
1350 phba->work_status[1]);
1351 if (phba->work_hs & HS_FFER8)
1352 /* Device Zeroization */
1353 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1354 "2861 Host Authentication device "
1355 "zeroization Data:x%x x%x x%x\n",
1356 phba->work_hs, phba->work_status[0],
1357 phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001358
James Smart92d7f7b2007-06-17 19:56:38 -05001359 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001360 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001361 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001362
1363 /*
1364 * Firmware stops when it triggled erratt with HS_FFER6.
1365 * That could cause the I/Os dropped by the firmware.
1366 * Error iocb (I/O) on txcmplq and let the SCSI layer
1367 * retry it after re-establishing link.
1368 */
1369 pring = &psli->ring[psli->fcp_ring];
1370 lpfc_sli_abort_iocb_ring(phba, pring);
1371
dea31012005-04-17 16:05:31 -05001372 /*
1373 * There was a firmware error. Take the hba offline and then
1374 * attempt to restart it.
1375 */
James Smart618a5232012-06-12 13:54:36 -04001376 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
dea31012005-04-17 16:05:31 -05001377 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001378 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001379 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001380 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001381 return;
1382 }
James Smart46fa3112007-04-25 09:51:45 -04001383 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001384 } else if (phba->work_hs & HS_CRIT_TEMP) {
1385 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1386 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1387 temp_event_data.event_code = LPFC_CRIT_TEMP;
1388 temp_event_data.data = (uint32_t)temperature;
1389
1390 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001391 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001392 "(%ld), taking this port offline "
1393 "Data: x%x x%x x%x\n",
1394 temperature, phba->work_hs,
1395 phba->work_status[0], phba->work_status[1]);
1396
1397 shost = lpfc_shost_from_vport(phba->pport);
1398 fc_host_post_vendor_event(shost, fc_get_event_number(),
1399 sizeof(temp_event_data),
1400 (char *) &temp_event_data,
1401 SCSI_NL_VID_TYPE_PCI
1402 | PCI_VENDOR_ID_EMULEX);
1403
James Smart7af67052007-10-27 13:38:11 -04001404 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001405 phba->over_temp_state = HBA_OVER_TEMP;
1406 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001407 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001408
dea31012005-04-17 16:05:31 -05001409 } else {
1410 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001411 * failure is a value other than FFER6. Do not call the offline
1412 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001413 */
1414 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001415 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001416 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001417 phba->work_hs,
dea31012005-04-17 16:05:31 -05001418 phba->work_status[0], phba->work_status[1]);
1419
James Smartd2873e42006-08-18 17:46:43 -04001420 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001421 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001422 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001423 sizeof(event_data), (char *) &event_data,
1424 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1425
James Smart09372822008-01-11 01:52:54 -05001426 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001427 }
James Smart93996272008-08-24 21:50:30 -04001428 return;
dea31012005-04-17 16:05:31 -05001429}
1430
James Smarte59058c2008-08-24 21:49:00 -04001431/**
James Smart618a5232012-06-12 13:54:36 -04001432 * lpfc_sli4_port_sta_fn_reset - The SLI4 function reset due to port status reg
1433 * @phba: pointer to lpfc hba data structure.
1434 * @mbx_action: flag for mailbox shutdown action.
1435 *
1436 * This routine is invoked to perform an SLI4 port PCI function reset in
1437 * response to port status register polling attention. It waits for port
1438 * status register (ERR, RDY, RN) bits before proceeding with function reset.
1439 * During this process, interrupt vectors are freed and later requested
1440 * for handling possible port resource change.
1441 **/
1442static int
1443lpfc_sli4_port_sta_fn_reset(struct lpfc_hba *phba, int mbx_action)
1444{
1445 int rc;
1446 uint32_t intr_mode;
1447
1448 /*
1449 * On error status condition, driver need to wait for port
1450 * ready before performing reset.
1451 */
1452 rc = lpfc_sli4_pdev_status_reg_wait(phba);
1453 if (!rc) {
1454 /* need reset: attempt for port recovery */
1455 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1456 "2887 Reset Needed: Attempting Port "
1457 "Recovery...\n");
1458 lpfc_offline_prep(phba, mbx_action);
1459 lpfc_offline(phba);
1460 /* release interrupt for possible resource change */
1461 lpfc_sli4_disable_intr(phba);
1462 lpfc_sli_brdrestart(phba);
1463 /* request and enable interrupt */
1464 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
1465 if (intr_mode == LPFC_INTR_ERROR) {
1466 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1467 "3175 Failed to enable interrupt\n");
1468 return -EIO;
1469 } else {
1470 phba->intr_mode = intr_mode;
1471 }
1472 rc = lpfc_online(phba);
1473 if (rc == 0)
1474 lpfc_unblock_mgmt_io(phba);
1475 }
1476 return rc;
1477}
1478
1479/**
James Smartda0436e2009-05-22 14:51:39 -04001480 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1481 * @phba: pointer to lpfc hba data structure.
1482 *
1483 * This routine is invoked to handle the SLI4 HBA hardware error attention
1484 * conditions.
1485 **/
1486static void
1487lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1488{
1489 struct lpfc_vport *vport = phba->pport;
1490 uint32_t event_data;
1491 struct Scsi_Host *shost;
James Smart2fcee4b2010-12-15 17:57:46 -05001492 uint32_t if_type;
James Smart2e90f4b2011-12-13 13:22:37 -05001493 struct lpfc_register portstat_reg = {0};
1494 uint32_t reg_err1, reg_err2;
1495 uint32_t uerrlo_reg, uemasklo_reg;
1496 uint32_t pci_rd_rc1, pci_rd_rc2;
James Smart73d91e52011-10-10 21:32:10 -04001497 int rc;
James Smartda0436e2009-05-22 14:51:39 -04001498
1499 /* If the pci channel is offline, ignore possible errors, since
1500 * we cannot communicate with the pci card anyway.
1501 */
1502 if (pci_channel_offline(phba->pcidev))
1503 return;
1504 /* If resets are disabled then leave the HBA alone and return */
1505 if (!phba->cfg_enable_hba_reset)
1506 return;
1507
James Smart2fcee4b2010-12-15 17:57:46 -05001508 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1509 switch (if_type) {
1510 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart2e90f4b2011-12-13 13:22:37 -05001511 pci_rd_rc1 = lpfc_readl(
1512 phba->sli4_hba.u.if_type0.UERRLOregaddr,
1513 &uerrlo_reg);
1514 pci_rd_rc2 = lpfc_readl(
1515 phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
1516 &uemasklo_reg);
1517 /* consider PCI bus read error as pci_channel_offline */
1518 if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
1519 return;
James Smart2fcee4b2010-12-15 17:57:46 -05001520 lpfc_sli4_offline_eratt(phba);
1521 break;
1522 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart2e90f4b2011-12-13 13:22:37 -05001523 pci_rd_rc1 = lpfc_readl(
1524 phba->sli4_hba.u.if_type2.STATUSregaddr,
1525 &portstat_reg.word0);
1526 /* consider PCI bus read error as pci_channel_offline */
James Smart6b5151f2012-01-18 16:24:06 -05001527 if (pci_rd_rc1 == -EIO) {
1528 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1529 "3151 PCI bus read access failure: x%x\n",
1530 readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
James Smart2e90f4b2011-12-13 13:22:37 -05001531 return;
James Smart6b5151f2012-01-18 16:24:06 -05001532 }
James Smart2e90f4b2011-12-13 13:22:37 -05001533 reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
1534 reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
James Smart2fcee4b2010-12-15 17:57:46 -05001535 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
1536 /* TODO: Register for Overtemp async events. */
1537 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1538 "2889 Port Overtemperature event, "
James Smart026abb82011-12-13 13:20:45 -05001539 "taking port offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001540 spin_lock_irq(&phba->hbalock);
1541 phba->over_temp_state = HBA_OVER_TEMP;
1542 spin_unlock_irq(&phba->hbalock);
1543 lpfc_sli4_offline_eratt(phba);
James Smart2e90f4b2011-12-13 13:22:37 -05001544 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001545 }
James Smart2e90f4b2011-12-13 13:22:37 -05001546 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1547 reg_err2 == SLIPORT_ERR2_REG_FW_RESTART)
1548 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1549 "3143 Port Down: Firmware Restarted\n");
1550 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1551 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1552 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1553 "3144 Port Down: Debug Dump\n");
1554 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1555 reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
1556 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1557 "3145 Port Down: Provisioning\n");
James Smart618a5232012-06-12 13:54:36 -04001558
1559 /* Check port status register for function reset */
1560 rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT);
1561 if (rc == 0) {
1562 /* don't report event on forced debug dump */
1563 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1564 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1565 return;
1566 else
1567 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001568 }
James Smart618a5232012-06-12 13:54:36 -04001569 /* fall through for not able to recover */
James Smart6b5151f2012-01-18 16:24:06 -05001570 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1571 "3152 Unrecoverable error, bring the port "
1572 "offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001573 lpfc_sli4_offline_eratt(phba);
1574 break;
1575 case LPFC_SLI_INTF_IF_TYPE_1:
1576 default:
1577 break;
1578 }
James Smart2e90f4b2011-12-13 13:22:37 -05001579 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1580 "3123 Report dump event to upper layer\n");
1581 /* Send an internal error event to mgmt application */
1582 lpfc_board_errevt_to_mgmt(phba);
1583
1584 event_data = FC_REG_DUMP_EVENT;
1585 shost = lpfc_shost_from_vport(vport);
1586 fc_host_post_vendor_event(shost, fc_get_event_number(),
1587 sizeof(event_data), (char *) &event_data,
1588 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
James Smartda0436e2009-05-22 14:51:39 -04001589}
1590
1591/**
1592 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1593 * @phba: pointer to lpfc HBA data structure.
1594 *
1595 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1596 * routine from the API jump table function pointer from the lpfc_hba struct.
1597 *
1598 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001599 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001600 * Any other value - error.
1601 **/
1602void
1603lpfc_handle_eratt(struct lpfc_hba *phba)
1604{
1605 (*phba->lpfc_handle_eratt)(phba);
1606}
1607
1608/**
James Smart3621a712009-04-06 18:47:14 -04001609 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04001610 * @phba: pointer to lpfc hba data structure.
1611 *
1612 * This routine is invoked from the worker thread to handle a HBA host
1613 * attention link event.
1614 **/
dea31012005-04-17 16:05:31 -05001615void
James Smart2e0fef82007-06-17 19:56:36 -05001616lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001617{
James Smart2e0fef82007-06-17 19:56:36 -05001618 struct lpfc_vport *vport = phba->pport;
1619 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001620 LPFC_MBOXQ_t *pmb;
1621 volatile uint32_t control;
1622 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05001623 int rc = 0;
dea31012005-04-17 16:05:31 -05001624
1625 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001626 if (!pmb) {
1627 rc = 1;
dea31012005-04-17 16:05:31 -05001628 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05001629 }
dea31012005-04-17 16:05:31 -05001630
1631 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001632 if (!mp) {
1633 rc = 2;
dea31012005-04-17 16:05:31 -05001634 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05001635 }
dea31012005-04-17 16:05:31 -05001636
1637 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05001638 if (!mp->virt) {
1639 rc = 3;
dea31012005-04-17 16:05:31 -05001640 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05001641 }
dea31012005-04-17 16:05:31 -05001642
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05001643 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04001644 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05001645
1646 psli->slistat.link_event++;
James Smart76a95d72010-11-20 23:11:48 -05001647 lpfc_read_topology(phba, pmb, mp);
1648 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smart2e0fef82007-06-17 19:56:36 -05001649 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04001650 /* Block ELS IOCBs until we have processed this mbox command */
1651 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04001652 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05001653 if (rc == MBX_NOT_FINISHED) {
1654 rc = 4;
James Smart14691152006-12-02 13:34:28 -05001655 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05001656 }
dea31012005-04-17 16:05:31 -05001657
1658 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05001659 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001660 writel(HA_LATT, phba->HAregaddr);
1661 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001662 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001663
1664 return;
1665
James Smart14691152006-12-02 13:34:28 -05001666lpfc_handle_latt_free_mbuf:
James Smart0d2b6b82008-06-14 22:52:47 -04001667 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05001668 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001669lpfc_handle_latt_free_mp:
1670 kfree(mp);
1671lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04001672 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001673lpfc_handle_latt_err_exit:
1674 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001675 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001676 psli->sli_flag |= LPFC_PROCESS_LA;
1677 control = readl(phba->HCregaddr);
1678 control |= HC_LAINT_ENA;
1679 writel(control, phba->HCregaddr);
1680 readl(phba->HCregaddr); /* flush */
1681
1682 /* Clear Link Attention in HA REG */
1683 writel(HA_LATT, phba->HAregaddr);
1684 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001685 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001686 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001687 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001688
James Smart09372822008-01-11 01:52:54 -05001689 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1690 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05001691
1692 return;
1693}
1694
James Smarte59058c2008-08-24 21:49:00 -04001695/**
James Smart3621a712009-04-06 18:47:14 -04001696 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04001697 * @phba: pointer to lpfc hba data structure.
1698 * @vpd: pointer to the vital product data.
1699 * @len: length of the vital product data in bytes.
1700 *
1701 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1702 * an array of characters. In this routine, the ModelName, ProgramType, and
1703 * ModelDesc, etc. fields of the phba data structure will be populated.
1704 *
1705 * Return codes
1706 * 0 - pointer to the VPD passed in is NULL
1707 * 1 - success
1708 **/
James Smart3772a992009-05-22 14:50:54 -04001709int
James Smart2e0fef82007-06-17 19:56:36 -05001710lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05001711{
1712 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05001713 int Length;
dea31012005-04-17 16:05:31 -05001714 int i, j;
1715 int finished = 0;
1716 int index = 0;
1717
1718 if (!vpd)
1719 return 0;
1720
1721 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05001722 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001723 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05001724 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1725 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001726 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05001727 switch (vpd[index]) {
1728 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001729 case 0x91:
dea31012005-04-17 16:05:31 -05001730 index += 1;
1731 lenlo = vpd[index];
1732 index += 1;
1733 lenhi = vpd[index];
1734 index += 1;
1735 i = ((((unsigned short)lenhi) << 8) + lenlo);
1736 index += i;
1737 break;
1738 case 0x90:
1739 index += 1;
1740 lenlo = vpd[index];
1741 index += 1;
1742 lenhi = vpd[index];
1743 index += 1;
1744 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001745 if (Length > len - index)
1746 Length = len - index;
dea31012005-04-17 16:05:31 -05001747 while (Length > 0) {
1748 /* Look for Serial Number */
1749 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1750 index += 2;
1751 i = vpd[index];
1752 index += 1;
1753 j = 0;
1754 Length -= (3+i);
1755 while(i--) {
1756 phba->SerialNumber[j++] = vpd[index++];
1757 if (j == 31)
1758 break;
1759 }
1760 phba->SerialNumber[j] = 0;
1761 continue;
1762 }
1763 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1764 phba->vpd_flag |= VPD_MODEL_DESC;
1765 index += 2;
1766 i = vpd[index];
1767 index += 1;
1768 j = 0;
1769 Length -= (3+i);
1770 while(i--) {
1771 phba->ModelDesc[j++] = vpd[index++];
1772 if (j == 255)
1773 break;
1774 }
1775 phba->ModelDesc[j] = 0;
1776 continue;
1777 }
1778 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1779 phba->vpd_flag |= VPD_MODEL_NAME;
1780 index += 2;
1781 i = vpd[index];
1782 index += 1;
1783 j = 0;
1784 Length -= (3+i);
1785 while(i--) {
1786 phba->ModelName[j++] = vpd[index++];
1787 if (j == 79)
1788 break;
1789 }
1790 phba->ModelName[j] = 0;
1791 continue;
1792 }
1793 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1794 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1795 index += 2;
1796 i = vpd[index];
1797 index += 1;
1798 j = 0;
1799 Length -= (3+i);
1800 while(i--) {
1801 phba->ProgramType[j++] = vpd[index++];
1802 if (j == 255)
1803 break;
1804 }
1805 phba->ProgramType[j] = 0;
1806 continue;
1807 }
1808 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1809 phba->vpd_flag |= VPD_PORT;
1810 index += 2;
1811 i = vpd[index];
1812 index += 1;
1813 j = 0;
1814 Length -= (3+i);
1815 while(i--) {
James Smartcd1c8302011-10-10 21:33:25 -04001816 if ((phba->sli_rev == LPFC_SLI_REV4) &&
1817 (phba->sli4_hba.pport_name_sta ==
1818 LPFC_SLI4_PPNAME_GET)) {
1819 j++;
1820 index++;
1821 } else
1822 phba->Port[j++] = vpd[index++];
1823 if (j == 19)
1824 break;
dea31012005-04-17 16:05:31 -05001825 }
James Smartcd1c8302011-10-10 21:33:25 -04001826 if ((phba->sli_rev != LPFC_SLI_REV4) ||
1827 (phba->sli4_hba.pport_name_sta ==
1828 LPFC_SLI4_PPNAME_NON))
1829 phba->Port[j] = 0;
dea31012005-04-17 16:05:31 -05001830 continue;
1831 }
1832 else {
1833 index += 2;
1834 i = vpd[index];
1835 index += 1;
1836 index += i;
1837 Length -= (3 + i);
1838 }
1839 }
1840 finished = 0;
1841 break;
1842 case 0x78:
1843 finished = 1;
1844 break;
1845 default:
1846 index ++;
1847 break;
1848 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001849 }
dea31012005-04-17 16:05:31 -05001850
1851 return(1);
1852}
1853
James Smarte59058c2008-08-24 21:49:00 -04001854/**
James Smart3621a712009-04-06 18:47:14 -04001855 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04001856 * @phba: pointer to lpfc hba data structure.
1857 * @mdp: pointer to the data structure to hold the derived model name.
1858 * @descp: pointer to the data structure to hold the derived description.
1859 *
1860 * This routine retrieves HBA's description based on its registered PCI device
1861 * ID. The @descp passed into this function points to an array of 256 chars. It
1862 * shall be returned with the model name, maximum speed, and the host bus type.
1863 * The @mdp passed into this function points to an array of 80 chars. When the
1864 * function returns, the @mdp will be filled with the model name.
1865 **/
dea31012005-04-17 16:05:31 -05001866static void
James Smart2e0fef82007-06-17 19:56:36 -05001867lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05001868{
1869 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05001870 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001871 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04001872 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04001873 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001874 struct {
James Smarta747c9c2009-11-18 15:41:10 -05001875 char *name;
1876 char *bus;
1877 char *function;
1878 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001879
1880 if (mdp && mdp[0] != '\0'
1881 && descp && descp[0] != '\0')
1882 return;
1883
James Smartc0c11512011-05-24 11:41:34 -04001884 if (phba->lmt & LMT_16Gb)
1885 max_speed = 16;
1886 else if (phba->lmt & LMT_10Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001887 max_speed = 10;
1888 else if (phba->lmt & LMT_8Gb)
1889 max_speed = 8;
1890 else if (phba->lmt & LMT_4Gb)
1891 max_speed = 4;
1892 else if (phba->lmt & LMT_2Gb)
1893 max_speed = 2;
James Smart4169d862012-09-29 11:32:09 -04001894 else if (phba->lmt & LMT_1Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001895 max_speed = 1;
James Smart4169d862012-09-29 11:32:09 -04001896 else
1897 max_speed = 0;
dea31012005-04-17 16:05:31 -05001898
1899 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05001900
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001901 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001902 case PCI_DEVICE_ID_FIREFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001903 m = (typeof(m)){"LP6000", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001904 break;
dea31012005-04-17 16:05:31 -05001905 case PCI_DEVICE_ID_SUPERFLY:
1906 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smarta747c9c2009-11-18 15:41:10 -05001907 m = (typeof(m)){"LP7000", "PCI",
1908 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001909 else
James Smarta747c9c2009-11-18 15:41:10 -05001910 m = (typeof(m)){"LP7000E", "PCI",
1911 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001912 break;
1913 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001914 m = (typeof(m)){"LP8000", "PCI",
1915 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001916 break;
1917 case PCI_DEVICE_ID_CENTAUR:
1918 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smarta747c9c2009-11-18 15:41:10 -05001919 m = (typeof(m)){"LP9002", "PCI",
1920 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001921 else
James Smarta747c9c2009-11-18 15:41:10 -05001922 m = (typeof(m)){"LP9000", "PCI",
1923 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001924 break;
1925 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001926 m = (typeof(m)){"LP952", "PCI",
1927 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001928 break;
1929 case PCI_DEVICE_ID_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05001930 m = (typeof(m)){"LP9802", "PCI-X",
1931 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001932 break;
1933 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05001934 m = (typeof(m)){"LP10000", "PCI-X",
1935 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001936 break;
1937 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05001938 m = (typeof(m)){"LPX1000", "PCI-X",
1939 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001940 break;
1941 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001942 m = (typeof(m)){"LP982", "PCI-X",
1943 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001944 break;
1945 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001946 m = (typeof(m)){"LP1050", "PCI-X",
1947 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001948 break;
1949 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05001950 m = (typeof(m)){"LP11000", "PCI-X2",
1951 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001952 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001953 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001954 m = (typeof(m)){"LP11000-SP", "PCI-X2",
1955 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001956 break;
1957 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001958 m = (typeof(m)){"LP11002-SP", "PCI-X2",
1959 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001960 break;
1961 case PCI_DEVICE_ID_NEPTUNE:
James Smarta747c9c2009-11-18 15:41:10 -05001962 m = (typeof(m)){"LPe1000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001963 break;
1964 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001965 m = (typeof(m)){"LPe1000-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001966 break;
1967 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001968 m = (typeof(m)){"LPe1002-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001969 break;
dea31012005-04-17 16:05:31 -05001970 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05001971 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001972 break;
1973 case PCI_DEVICE_ID_BSMB:
James Smarta747c9c2009-11-18 15:41:10 -05001974 m = (typeof(m)){"LP111", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001975 break;
1976 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05001977 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001978 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001979 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001980 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001981 break;
1982 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001983 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04001984 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001985 break;
dea31012005-04-17 16:05:31 -05001986 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05001987 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001988 break;
1989 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05001990 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001991 break;
1992 case PCI_DEVICE_ID_LP101:
James Smarta747c9c2009-11-18 15:41:10 -05001993 m = (typeof(m)){"LP101", "PCI-X", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001994 break;
1995 case PCI_DEVICE_ID_LP10000S:
James Smarta747c9c2009-11-18 15:41:10 -05001996 m = (typeof(m)){"LP10000-S", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001997 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001998 case PCI_DEVICE_ID_LP11000S:
James Smarta747c9c2009-11-18 15:41:10 -05001999 m = (typeof(m)){"LP11000-S", "PCI-X2", "Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05002000 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002001 case PCI_DEVICE_ID_LPE11000S:
James Smarta747c9c2009-11-18 15:41:10 -05002002 m = (typeof(m)){"LPe11000-S", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002003 break;
James Smartb87eab32007-04-25 09:53:28 -04002004 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05002005 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002006 break;
2007 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05002008 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002009 break;
2010 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05002011 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002012 break;
2013 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002014 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002015 break;
2016 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002017 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002018 break;
2019 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05002020 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002021 break;
James Smart84774a42008-08-24 21:50:06 -04002022 case PCI_DEVICE_ID_HORNET:
James Smarta747c9c2009-11-18 15:41:10 -05002023 m = (typeof(m)){"LP21000", "PCIe", "FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002024 GE = 1;
2025 break;
2026 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05002027 m = (typeof(m)){"LPev12000", "PCIe IOV",
2028 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002029 break;
2030 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05002031 m = (typeof(m)){"LPev12000", "PCIe IOV",
2032 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002033 break;
2034 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05002035 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
2036 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002037 break;
James Smartda0436e2009-05-22 14:51:39 -04002038 case PCI_DEVICE_ID_TIGERSHARK:
2039 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002040 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04002041 break;
James Smarta747c9c2009-11-18 15:41:10 -05002042 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04002043 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002044 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2045 break;
2046 case PCI_DEVICE_ID_FALCON:
2047 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2048 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04002049 break;
James Smart98fc5dd2010-06-07 15:24:29 -04002050 case PCI_DEVICE_ID_BALIUS:
2051 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
2052 "Fibre Channel Adapter"};
2053 break;
James Smart085c6472010-11-20 23:11:37 -05002054 case PCI_DEVICE_ID_LANCER_FC:
James Smartc0c11512011-05-24 11:41:34 -04002055 case PCI_DEVICE_ID_LANCER_FC_VF:
2056 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
James Smart085c6472010-11-20 23:11:37 -05002057 break;
2058 case PCI_DEVICE_ID_LANCER_FCOE:
James Smartc0c11512011-05-24 11:41:34 -04002059 case PCI_DEVICE_ID_LANCER_FCOE_VF:
James Smart085c6472010-11-20 23:11:37 -05002060 oneConnect = 1;
James Smart079b5c92011-08-21 21:48:49 -04002061 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
James Smart085c6472010-11-20 23:11:37 -05002062 break;
James Smartf8cafd32012-08-03 12:42:51 -04002063 case PCI_DEVICE_ID_SKYHAWK:
2064 case PCI_DEVICE_ID_SKYHAWK_VF:
2065 oneConnect = 1;
2066 m = (typeof(m)){"OCe14000", "PCIe", "FCoE"};
2067 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002068 default:
James Smarta747c9c2009-11-18 15:41:10 -05002069 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002070 break;
dea31012005-04-17 16:05:31 -05002071 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002072
2073 if (mdp && mdp[0] == '\0')
2074 snprintf(mdp, 79,"%s", m.name);
James Smartc0c11512011-05-24 11:41:34 -04002075 /*
2076 * oneConnect hba requires special processing, they are all initiators
James Smartda0436e2009-05-22 14:51:39 -04002077 * and we put the port number on the end
2078 */
2079 if (descp && descp[0] == '\0') {
2080 if (oneConnect)
2081 snprintf(descp, 255,
James Smart4169d862012-09-29 11:32:09 -04002082 "Emulex OneConnect %s, %s Initiator %s",
James Smarta747c9c2009-11-18 15:41:10 -05002083 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04002084 phba->Port);
James Smart4169d862012-09-29 11:32:09 -04002085 else if (max_speed == 0)
2086 snprintf(descp, 255,
2087 "Emulex %s %s %s ",
2088 m.name, m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002089 else
2090 snprintf(descp, 255,
2091 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05002092 m.name, max_speed, (GE) ? "GE" : "Gb",
2093 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002094 }
dea31012005-04-17 16:05:31 -05002095}
2096
James Smarte59058c2008-08-24 21:49:00 -04002097/**
James Smart3621a712009-04-06 18:47:14 -04002098 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04002099 * @phba: pointer to lpfc hba data structure.
2100 * @pring: pointer to a IOCB ring.
2101 * @cnt: the number of IOCBs to be posted to the IOCB ring.
2102 *
2103 * This routine posts a given number of IOCBs with the associated DMA buffer
2104 * descriptors specified by the cnt argument to the given IOCB ring.
2105 *
2106 * Return codes
2107 * The number of IOCBs NOT able to be posted to the IOCB ring.
2108 **/
dea31012005-04-17 16:05:31 -05002109int
James Smart495a7142008-06-14 22:52:59 -04002110lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05002111{
2112 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002113 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05002114 struct lpfc_dmabuf *mp1, *mp2;
2115
2116 cnt += pring->missbufcnt;
2117
2118 /* While there are buffers to post */
2119 while (cnt > 0) {
2120 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002121 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002122 if (iocb == NULL) {
2123 pring->missbufcnt = cnt;
2124 return cnt;
2125 }
dea31012005-04-17 16:05:31 -05002126 icmd = &iocb->iocb;
2127
2128 /* 2 buffers can be posted per command */
2129 /* Allocate buffer to post */
2130 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2131 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04002132 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2133 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002134 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002135 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002136 pring->missbufcnt = cnt;
2137 return cnt;
2138 }
2139
2140 INIT_LIST_HEAD(&mp1->list);
2141 /* Allocate buffer to post */
2142 if (cnt > 1) {
2143 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2144 if (mp2)
2145 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2146 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04002147 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002148 kfree(mp2);
dea31012005-04-17 16:05:31 -05002149 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2150 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002151 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002152 pring->missbufcnt = cnt;
2153 return cnt;
2154 }
2155
2156 INIT_LIST_HEAD(&mp2->list);
2157 } else {
2158 mp2 = NULL;
2159 }
2160
2161 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2162 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2163 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2164 icmd->ulpBdeCount = 1;
2165 cnt--;
2166 if (mp2) {
2167 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2168 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2169 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2170 cnt--;
2171 icmd->ulpBdeCount = 2;
2172 }
2173
2174 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2175 icmd->ulpLe = 1;
2176
James Smart3772a992009-05-22 14:50:54 -04002177 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2178 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05002179 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2180 kfree(mp1);
2181 cnt++;
2182 if (mp2) {
2183 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2184 kfree(mp2);
2185 cnt++;
2186 }
James Bottomley604a3e32005-10-29 10:28:33 -05002187 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002188 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05002189 return cnt;
2190 }
dea31012005-04-17 16:05:31 -05002191 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05002192 if (mp2)
dea31012005-04-17 16:05:31 -05002193 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05002194 }
2195 pring->missbufcnt = 0;
2196 return 0;
2197}
2198
James Smarte59058c2008-08-24 21:49:00 -04002199/**
James Smart3621a712009-04-06 18:47:14 -04002200 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04002201 * @phba: pointer to lpfc hba data structure.
2202 *
2203 * This routine posts initial receive IOCB buffers to the ELS ring. The
2204 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
2205 * set to 64 IOCBs.
2206 *
2207 * Return codes
2208 * 0 - success (currently always success)
2209 **/
dea31012005-04-17 16:05:31 -05002210static int
James Smart2e0fef82007-06-17 19:56:36 -05002211lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002212{
2213 struct lpfc_sli *psli = &phba->sli;
2214
2215 /* Ring 0, ELS / CT buffers */
James Smart495a7142008-06-14 22:52:59 -04002216 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05002217 /* Ring 2 - FCP no buffers needed */
2218
2219 return 0;
2220}
2221
2222#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2223
James Smarte59058c2008-08-24 21:49:00 -04002224/**
James Smart3621a712009-04-06 18:47:14 -04002225 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04002226 * @HashResultPointer: pointer to an array as hash table.
2227 *
2228 * This routine sets up the initial values to the array of hash table entries
2229 * for the LC HBAs.
2230 **/
dea31012005-04-17 16:05:31 -05002231static void
2232lpfc_sha_init(uint32_t * HashResultPointer)
2233{
2234 HashResultPointer[0] = 0x67452301;
2235 HashResultPointer[1] = 0xEFCDAB89;
2236 HashResultPointer[2] = 0x98BADCFE;
2237 HashResultPointer[3] = 0x10325476;
2238 HashResultPointer[4] = 0xC3D2E1F0;
2239}
2240
James Smarte59058c2008-08-24 21:49:00 -04002241/**
James Smart3621a712009-04-06 18:47:14 -04002242 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04002243 * @HashResultPointer: pointer to an initial/result hash table.
2244 * @HashWorkingPointer: pointer to an working hash table.
2245 *
2246 * This routine iterates an initial hash table pointed by @HashResultPointer
2247 * with the values from the working hash table pointeed by @HashWorkingPointer.
2248 * The results are putting back to the initial hash table, returned through
2249 * the @HashResultPointer as the result hash table.
2250 **/
dea31012005-04-17 16:05:31 -05002251static void
2252lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2253{
2254 int t;
2255 uint32_t TEMP;
2256 uint32_t A, B, C, D, E;
2257 t = 16;
2258 do {
2259 HashWorkingPointer[t] =
2260 S(1,
2261 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2262 8] ^
2263 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2264 } while (++t <= 79);
2265 t = 0;
2266 A = HashResultPointer[0];
2267 B = HashResultPointer[1];
2268 C = HashResultPointer[2];
2269 D = HashResultPointer[3];
2270 E = HashResultPointer[4];
2271
2272 do {
2273 if (t < 20) {
2274 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2275 } else if (t < 40) {
2276 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2277 } else if (t < 60) {
2278 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2279 } else {
2280 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2281 }
2282 TEMP += S(5, A) + E + HashWorkingPointer[t];
2283 E = D;
2284 D = C;
2285 C = S(30, B);
2286 B = A;
2287 A = TEMP;
2288 } while (++t <= 79);
2289
2290 HashResultPointer[0] += A;
2291 HashResultPointer[1] += B;
2292 HashResultPointer[2] += C;
2293 HashResultPointer[3] += D;
2294 HashResultPointer[4] += E;
2295
2296}
2297
James Smarte59058c2008-08-24 21:49:00 -04002298/**
James Smart3621a712009-04-06 18:47:14 -04002299 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002300 * @RandomChallenge: pointer to the entry of host challenge random number array.
2301 * @HashWorking: pointer to the entry of the working hash array.
2302 *
2303 * This routine calculates the working hash array referred by @HashWorking
2304 * from the challenge random numbers associated with the host, referred by
2305 * @RandomChallenge. The result is put into the entry of the working hash
2306 * array and returned by reference through @HashWorking.
2307 **/
dea31012005-04-17 16:05:31 -05002308static void
2309lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2310{
2311 *HashWorking = (*RandomChallenge ^ *HashWorking);
2312}
2313
James Smarte59058c2008-08-24 21:49:00 -04002314/**
James Smart3621a712009-04-06 18:47:14 -04002315 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002316 * @phba: pointer to lpfc hba data structure.
2317 * @hbainit: pointer to an array of unsigned 32-bit integers.
2318 *
2319 * This routine performs the special handling for LC HBA initialization.
2320 **/
dea31012005-04-17 16:05:31 -05002321void
2322lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2323{
2324 int t;
2325 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002326 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002327
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002328 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002329 if (!HashWorking)
2330 return;
2331
dea31012005-04-17 16:05:31 -05002332 HashWorking[0] = HashWorking[78] = *pwwnn++;
2333 HashWorking[1] = HashWorking[79] = *pwwnn;
2334
2335 for (t = 0; t < 7; t++)
2336 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2337
2338 lpfc_sha_init(hbainit);
2339 lpfc_sha_iterate(hbainit, HashWorking);
2340 kfree(HashWorking);
2341}
2342
James Smarte59058c2008-08-24 21:49:00 -04002343/**
James Smart3621a712009-04-06 18:47:14 -04002344 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002345 * @vport: pointer to a virtual N_Port data structure.
2346 *
2347 * This routine performs the necessary cleanups before deleting the @vport.
2348 * It invokes the discovery state machine to perform necessary state
2349 * transitions and to release the ndlps associated with the @vport. Note,
2350 * the physical port is treated as @vport 0.
2351 **/
James Smart87af33f2007-10-27 13:37:43 -04002352void
James Smart2e0fef82007-06-17 19:56:36 -05002353lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002354{
James Smart87af33f2007-10-27 13:37:43 -04002355 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002356 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002357 int i = 0;
dea31012005-04-17 16:05:31 -05002358
James Smart87af33f2007-10-27 13:37:43 -04002359 if (phba->link_state > LPFC_LINK_DOWN)
2360 lpfc_port_link_failure(vport);
2361
2362 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002363 if (!NLP_CHK_NODE_ACT(ndlp)) {
2364 ndlp = lpfc_enable_node(vport, ndlp,
2365 NLP_STE_UNUSED_NODE);
2366 if (!ndlp)
2367 continue;
2368 spin_lock_irq(&phba->ndlp_lock);
2369 NLP_SET_FREE_REQ(ndlp);
2370 spin_unlock_irq(&phba->ndlp_lock);
2371 /* Trigger the release of the ndlp memory */
2372 lpfc_nlp_put(ndlp);
2373 continue;
2374 }
2375 spin_lock_irq(&phba->ndlp_lock);
2376 if (NLP_CHK_FREE_REQ(ndlp)) {
2377 /* The ndlp should not be in memory free mode already */
2378 spin_unlock_irq(&phba->ndlp_lock);
2379 continue;
2380 } else
2381 /* Indicate request for freeing ndlp memory */
2382 NLP_SET_FREE_REQ(ndlp);
2383 spin_unlock_irq(&phba->ndlp_lock);
2384
James Smart58da1ff2008-04-07 10:15:56 -04002385 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2386 ndlp->nlp_DID == Fabric_DID) {
2387 /* Just free up ndlp with Fabric_DID for vports */
2388 lpfc_nlp_put(ndlp);
2389 continue;
2390 }
2391
James Smarteff4a012012-01-18 16:25:25 -05002392 /* take care of nodes in unused state before the state
2393 * machine taking action.
2394 */
2395 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
2396 lpfc_nlp_put(ndlp);
2397 continue;
2398 }
2399
James Smart87af33f2007-10-27 13:37:43 -04002400 if (ndlp->nlp_type & NLP_FABRIC)
2401 lpfc_disc_state_machine(vport, ndlp, NULL,
2402 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002403
James Smart87af33f2007-10-27 13:37:43 -04002404 lpfc_disc_state_machine(vport, ndlp, NULL,
2405 NLP_EVT_DEVICE_RM);
2406 }
2407
James Smarta8adb832007-10-27 13:37:53 -04002408 /* At this point, ALL ndlp's should be gone
2409 * because of the previous NLP_EVT_DEVICE_RM.
2410 * Lets wait for this to happen, if needed.
2411 */
James Smart87af33f2007-10-27 13:37:43 -04002412 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002413 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002414 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002415 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002416 list_for_each_entry_safe(ndlp, next_ndlp,
2417 &vport->fc_nodes, nlp_listp) {
2418 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2419 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002420 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002421 "usgmap:x%x refcnt:%d\n",
2422 ndlp->nlp_DID, (void *)ndlp,
2423 ndlp->nlp_usg_map,
2424 atomic_read(
2425 &ndlp->kref.refcount));
2426 }
James Smarta8adb832007-10-27 13:37:53 -04002427 break;
James Smart87af33f2007-10-27 13:37:43 -04002428 }
James Smarta8adb832007-10-27 13:37:53 -04002429
2430 /* Wait for any activity on ndlps to settle */
2431 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002432 }
James Smart1151e3e2011-02-16 12:39:35 -05002433 lpfc_cleanup_vports_rrqs(vport, NULL);
dea31012005-04-17 16:05:31 -05002434}
2435
James Smarte59058c2008-08-24 21:49:00 -04002436/**
James Smart3621a712009-04-06 18:47:14 -04002437 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002438 * @vport: pointer to a virtual N_Port data structure.
2439 *
2440 * This routine stops all the timers associated with a @vport. This function
2441 * is invoked before disabling or deleting a @vport. Note that the physical
2442 * port is treated as @vport 0.
2443 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002444void
2445lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002446{
James Smart92d7f7b2007-06-17 19:56:38 -05002447 del_timer_sync(&vport->els_tmofunc);
2448 del_timer_sync(&vport->fc_fdmitmo);
James Smart92494142011-02-16 12:39:44 -05002449 del_timer_sync(&vport->delayed_disc_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002450 lpfc_can_disctmo(vport);
2451 return;
dea31012005-04-17 16:05:31 -05002452}
2453
James Smarte59058c2008-08-24 21:49:00 -04002454/**
James Smartecfd03c2010-02-12 14:41:27 -05002455 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2456 * @phba: pointer to lpfc hba data structure.
2457 *
2458 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2459 * caller of this routine should already hold the host lock.
2460 **/
2461void
2462__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2463{
James Smart5ac6b302010-10-22 11:05:36 -04002464 /* Clear pending FCF rediscovery wait flag */
2465 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2466
James Smartecfd03c2010-02-12 14:41:27 -05002467 /* Now, try to stop the timer */
2468 del_timer(&phba->fcf.redisc_wait);
2469}
2470
2471/**
2472 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2473 * @phba: pointer to lpfc hba data structure.
2474 *
2475 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2476 * checks whether the FCF rediscovery wait timer is pending with the host
2477 * lock held before proceeding with disabling the timer and clearing the
2478 * wait timer pendig flag.
2479 **/
2480void
2481lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2482{
2483 spin_lock_irq(&phba->hbalock);
2484 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2485 /* FCF rediscovery timer already fired or stopped */
2486 spin_unlock_irq(&phba->hbalock);
2487 return;
2488 }
2489 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart5ac6b302010-10-22 11:05:36 -04002490 /* Clear failover in progress flags */
2491 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05002492 spin_unlock_irq(&phba->hbalock);
2493}
2494
2495/**
James Smart3772a992009-05-22 14:50:54 -04002496 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002497 * @phba: pointer to lpfc hba data structure.
2498 *
2499 * This routine stops all the timers associated with a HBA. This function is
2500 * invoked before either putting a HBA offline or unloading the driver.
2501 **/
James Smart3772a992009-05-22 14:50:54 -04002502void
2503lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002504{
James Smart51ef4c22007-08-02 11:10:31 -04002505 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05002506 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002507 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002508 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002509 del_timer_sync(&phba->hb_tmofunc);
James Smart1151e3e2011-02-16 12:39:35 -05002510 if (phba->sli_rev == LPFC_SLI_REV4) {
2511 del_timer_sync(&phba->rrq_tmr);
2512 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2513 }
James Smart3772a992009-05-22 14:50:54 -04002514 phba->hb_outstanding = 0;
2515
2516 switch (phba->pci_dev_grp) {
2517 case LPFC_PCI_DEV_LP:
2518 /* Stop any LightPulse device specific driver timers */
2519 del_timer_sync(&phba->fcp_poll_timer);
2520 break;
2521 case LPFC_PCI_DEV_OC:
2522 /* Stop any OneConnect device sepcific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05002523 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04002524 break;
2525 default:
2526 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2527 "0297 Invalid device group (x%x)\n",
2528 phba->pci_dev_grp);
2529 break;
2530 }
James Smart2e0fef82007-06-17 19:56:36 -05002531 return;
dea31012005-04-17 16:05:31 -05002532}
2533
James Smarte59058c2008-08-24 21:49:00 -04002534/**
James Smart3621a712009-04-06 18:47:14 -04002535 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002536 * @phba: pointer to lpfc hba data structure.
2537 *
2538 * This routine marks a HBA's management interface as blocked. Once the HBA's
2539 * management interface is marked as blocked, all the user space access to
2540 * the HBA, whether they are from sysfs interface or libdfc interface will
2541 * all be blocked. The HBA is set to block the management interface when the
2542 * driver prepares the HBA interface for online or offline.
2543 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002544static void
James Smart618a5232012-06-12 13:54:36 -04002545lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
Adrian Bunka6ababd2007-11-05 18:07:33 +01002546{
2547 unsigned long iflag;
James Smart6e7288d2010-06-07 15:23:35 -04002548 uint8_t actcmd = MBX_HEARTBEAT;
2549 unsigned long timeout;
2550
Adrian Bunka6ababd2007-11-05 18:07:33 +01002551 spin_lock_irqsave(&phba->hbalock, iflag);
2552 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
James Smart618a5232012-06-12 13:54:36 -04002553 spin_unlock_irqrestore(&phba->hbalock, iflag);
2554 if (mbx_action == LPFC_MBX_NO_WAIT)
2555 return;
2556 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
2557 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002558 if (phba->sli.mbox_active) {
James Smart6e7288d2010-06-07 15:23:35 -04002559 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
James Smarta183a152011-10-10 21:32:43 -04002560 /* Determine how long we might wait for the active mailbox
2561 * command to be gracefully completed by firmware.
2562 */
2563 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
2564 phba->sli.mbox_active) * 1000) + jiffies;
2565 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002566 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002567
James Smart6e7288d2010-06-07 15:23:35 -04002568 /* Wait for the outstnading mailbox command to complete */
2569 while (phba->sli.mbox_active) {
2570 /* Check active mailbox complete status every 2ms */
2571 msleep(2);
2572 if (time_after(jiffies, timeout)) {
2573 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2574 "2813 Mgmt IO is Blocked %x "
2575 "- mbox cmd %x still active\n",
2576 phba->sli.sli_flag, actcmd);
2577 break;
2578 }
2579 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002580}
2581
James Smarte59058c2008-08-24 21:49:00 -04002582/**
James Smart6b5151f2012-01-18 16:24:06 -05002583 * lpfc_sli4_node_prep - Assign RPIs for active nodes.
2584 * @phba: pointer to lpfc hba data structure.
2585 *
2586 * Allocate RPIs for all active remote nodes. This is needed whenever
2587 * an SLI4 adapter is reset and the driver is not unloading. Its purpose
2588 * is to fixup the temporary rpi assignments.
2589 **/
2590void
2591lpfc_sli4_node_prep(struct lpfc_hba *phba)
2592{
2593 struct lpfc_nodelist *ndlp, *next_ndlp;
2594 struct lpfc_vport **vports;
2595 int i;
2596
2597 if (phba->sli_rev != LPFC_SLI_REV4)
2598 return;
2599
2600 vports = lpfc_create_vport_work_array(phba);
2601 if (vports != NULL) {
2602 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2603 if (vports[i]->load_flag & FC_UNLOADING)
2604 continue;
2605
2606 list_for_each_entry_safe(ndlp, next_ndlp,
2607 &vports[i]->fc_nodes,
2608 nlp_listp) {
2609 if (NLP_CHK_NODE_ACT(ndlp))
2610 ndlp->nlp_rpi =
2611 lpfc_sli4_alloc_rpi(phba);
2612 }
2613 }
2614 }
2615 lpfc_destroy_vport_work_array(phba, vports);
2616}
2617
2618/**
James Smart3621a712009-04-06 18:47:14 -04002619 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04002620 * @phba: pointer to lpfc hba data structure.
2621 *
2622 * This routine initializes the HBA and brings a HBA online. During this
2623 * process, the management interface is blocked to prevent user space access
2624 * to the HBA interfering with the driver initialization.
2625 *
2626 * Return codes
2627 * 0 - successful
2628 * 1 - failed
2629 **/
dea31012005-04-17 16:05:31 -05002630int
James Smart2e0fef82007-06-17 19:56:36 -05002631lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002632{
Julia Lawall372bd282008-12-16 16:15:08 +01002633 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04002634 struct lpfc_vport **vports;
2635 int i;
James Smart2e0fef82007-06-17 19:56:36 -05002636
dea31012005-04-17 16:05:31 -05002637 if (!phba)
2638 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01002639 vport = phba->pport;
dea31012005-04-17 16:05:31 -05002640
James Smart2e0fef82007-06-17 19:56:36 -05002641 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05002642 return 0;
2643
James Smarted957682007-06-17 19:56:37 -05002644 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002645 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05002646
James Smart618a5232012-06-12 13:54:36 -04002647 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05002648
James Smart46fa3112007-04-25 09:51:45 -04002649 if (!lpfc_sli_queue_setup(phba)) {
2650 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002651 return 1;
James Smart46fa3112007-04-25 09:51:45 -04002652 }
2653
James Smartda0436e2009-05-22 14:51:39 -04002654 if (phba->sli_rev == LPFC_SLI_REV4) {
2655 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2656 lpfc_unblock_mgmt_io(phba);
2657 return 1;
2658 }
2659 } else {
2660 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2661 lpfc_unblock_mgmt_io(phba);
2662 return 1;
2663 }
James Smart46fa3112007-04-25 09:51:45 -04002664 }
dea31012005-04-17 16:05:31 -05002665
James Smart549e55c2007-08-02 11:09:51 -04002666 vports = lpfc_create_vport_work_array(phba);
2667 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002668 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002669 struct Scsi_Host *shost;
2670 shost = lpfc_shost_from_vport(vports[i]);
2671 spin_lock_irq(shost->host_lock);
2672 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2673 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2674 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart1c6834a2009-07-19 10:01:26 -04002675 if (phba->sli_rev == LPFC_SLI_REV4)
2676 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart549e55c2007-08-02 11:09:51 -04002677 spin_unlock_irq(shost->host_lock);
2678 }
James Smart09372822008-01-11 01:52:54 -05002679 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002680
James Smart46fa3112007-04-25 09:51:45 -04002681 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002682 return 0;
2683}
2684
James Smarte59058c2008-08-24 21:49:00 -04002685/**
James Smart3621a712009-04-06 18:47:14 -04002686 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04002687 * @phba: pointer to lpfc hba data structure.
2688 *
2689 * This routine marks a HBA's management interface as not blocked. Once the
2690 * HBA's management interface is marked as not blocked, all the user space
2691 * access to the HBA, whether they are from sysfs interface or libdfc
2692 * interface will be allowed. The HBA is set to block the management interface
2693 * when the driver prepares the HBA interface for online or offline and then
2694 * set to unblock the management interface afterwards.
2695 **/
James Smart46fa3112007-04-25 09:51:45 -04002696void
James Smart46fa3112007-04-25 09:51:45 -04002697lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2698{
2699 unsigned long iflag;
2700
James Smart2e0fef82007-06-17 19:56:36 -05002701 spin_lock_irqsave(&phba->hbalock, iflag);
2702 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2703 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04002704}
2705
James Smarte59058c2008-08-24 21:49:00 -04002706/**
James Smart3621a712009-04-06 18:47:14 -04002707 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04002708 * @phba: pointer to lpfc hba data structure.
2709 *
2710 * This routine is invoked to prepare a HBA to be brought offline. It performs
2711 * unregistration login to all the nodes on all vports and flushes the mailbox
2712 * queue to make it ready to be brought offline.
2713 **/
James Smart46fa3112007-04-25 09:51:45 -04002714void
James Smart618a5232012-06-12 13:54:36 -04002715lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
James Smart46fa3112007-04-25 09:51:45 -04002716{
James Smart2e0fef82007-06-17 19:56:36 -05002717 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04002718 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04002719 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05002720 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04002721 int i;
dea31012005-04-17 16:05:31 -05002722
James Smart2e0fef82007-06-17 19:56:36 -05002723 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002724 return;
dea31012005-04-17 16:05:31 -05002725
James Smart618a5232012-06-12 13:54:36 -04002726 lpfc_block_mgmt_io(phba, mbx_action);
dea31012005-04-17 16:05:31 -05002727
2728 lpfc_linkdown(phba);
2729
James Smart87af33f2007-10-27 13:37:43 -04002730 /* Issue an unreg_login to all nodes on all vports */
2731 vports = lpfc_create_vport_work_array(phba);
2732 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04002733 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04002734 if (vports[i]->load_flag & FC_UNLOADING)
2735 continue;
James Smart72100cc2010-02-12 14:43:01 -05002736 shost = lpfc_shost_from_vport(vports[i]);
2737 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002738 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002739 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2740 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05002741 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002742
James Smart87af33f2007-10-27 13:37:43 -04002743 shost = lpfc_shost_from_vport(vports[i]);
2744 list_for_each_entry_safe(ndlp, next_ndlp,
2745 &vports[i]->fc_nodes,
2746 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002747 if (!NLP_CHK_NODE_ACT(ndlp))
2748 continue;
James Smart87af33f2007-10-27 13:37:43 -04002749 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2750 continue;
2751 if (ndlp->nlp_type & NLP_FABRIC) {
2752 lpfc_disc_state_machine(vports[i], ndlp,
2753 NULL, NLP_EVT_DEVICE_RECOVERY);
2754 lpfc_disc_state_machine(vports[i], ndlp,
2755 NULL, NLP_EVT_DEVICE_RM);
2756 }
2757 spin_lock_irq(shost->host_lock);
2758 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart401ee0c2012-03-01 22:35:34 -05002759 spin_unlock_irq(shost->host_lock);
James Smart6b5151f2012-01-18 16:24:06 -05002760 /*
2761 * Whenever an SLI4 port goes offline, free the
James Smart401ee0c2012-03-01 22:35:34 -05002762 * RPI. Get a new RPI when the adapter port
2763 * comes back online.
James Smart6b5151f2012-01-18 16:24:06 -05002764 */
2765 if (phba->sli_rev == LPFC_SLI_REV4)
2766 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smart87af33f2007-10-27 13:37:43 -04002767 lpfc_unreg_rpi(vports[i], ndlp);
2768 }
2769 }
2770 }
James Smart09372822008-01-11 01:52:54 -05002771 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002772
James Smart618a5232012-06-12 13:54:36 -04002773 lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
James Smart46fa3112007-04-25 09:51:45 -04002774}
2775
James Smarte59058c2008-08-24 21:49:00 -04002776/**
James Smart3621a712009-04-06 18:47:14 -04002777 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04002778 * @phba: pointer to lpfc hba data structure.
2779 *
2780 * This routine actually brings a HBA offline. It stops all the timers
2781 * associated with the HBA, brings down the SLI layer, and eventually
2782 * marks the HBA as in offline state for the upper layer protocol.
2783 **/
James Smart46fa3112007-04-25 09:51:45 -04002784void
James Smart2e0fef82007-06-17 19:56:36 -05002785lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04002786{
James Smart549e55c2007-08-02 11:09:51 -04002787 struct Scsi_Host *shost;
2788 struct lpfc_vport **vports;
2789 int i;
James Smart46fa3112007-04-25 09:51:45 -04002790
James Smart549e55c2007-08-02 11:09:51 -04002791 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002792 return;
James Smart688a8862006-07-06 15:49:56 -04002793
James Smartda0436e2009-05-22 14:51:39 -04002794 /* stop port and all timers associated with this hba */
2795 lpfc_stop_port(phba);
James Smart51ef4c22007-08-02 11:10:31 -04002796 vports = lpfc_create_vport_work_array(phba);
2797 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002798 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04002799 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05002800 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05002801 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002802 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05002803 /* Bring down the SLI Layer and cleanup. The HBA is offline
2804 now. */
2805 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002806 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04002807 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002808 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04002809 vports = lpfc_create_vport_work_array(phba);
2810 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002811 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002812 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04002813 spin_lock_irq(shost->host_lock);
2814 vports[i]->work_port_events = 0;
2815 vports[i]->fc_flag |= FC_OFFLINE_MODE;
2816 spin_unlock_irq(shost->host_lock);
2817 }
James Smart09372822008-01-11 01:52:54 -05002818 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002819}
2820
James Smarte59058c2008-08-24 21:49:00 -04002821/**
James Smart3621a712009-04-06 18:47:14 -04002822 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04002823 * @phba: pointer to lpfc hba data structure.
2824 *
2825 * This routine is to free all the SCSI buffers and IOCBs from the driver
2826 * list back to kernel. It is called from lpfc_pci_remove_one to free
2827 * the internal resources before the device is removed from the system.
James Smarte59058c2008-08-24 21:49:00 -04002828 **/
James Smart8a9d2e82012-05-09 21:16:12 -04002829static void
James Smart2e0fef82007-06-17 19:56:36 -05002830lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002831{
2832 struct lpfc_scsi_buf *sb, *sb_next;
2833 struct lpfc_iocbq *io, *io_next;
2834
James Smart2e0fef82007-06-17 19:56:36 -05002835 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002836 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smart1c6f4ef52009-11-18 15:40:49 -05002837 spin_lock(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05002838 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
2839 list_del(&sb->list);
2840 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05002841 sb->dma_handle);
dea31012005-04-17 16:05:31 -05002842 kfree(sb);
2843 phba->total_scsi_bufs--;
2844 }
James Smart1c6f4ef52009-11-18 15:40:49 -05002845 spin_unlock(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05002846
2847 /* Release all the lpfc_iocbq entries maintained by this host. */
2848 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2849 list_del(&io->list);
2850 kfree(io);
2851 phba->total_iocbq_bufs--;
2852 }
James Smart6d368e52011-05-24 11:44:12 -04002853
James Smart2e0fef82007-06-17 19:56:36 -05002854 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04002855}
2856
2857/**
2858 * lpfc_sli4_xri_sgl_update - update xri-sgl sizing and mapping
2859 * @phba: pointer to lpfc hba data structure.
2860 *
2861 * This routine first calculates the sizes of the current els and allocated
2862 * scsi sgl lists, and then goes through all sgls to updates the physical
2863 * XRIs assigned due to port function reset. During port initialization, the
2864 * current els and allocated scsi sgl lists are 0s.
2865 *
2866 * Return codes
2867 * 0 - successful (for now, it always returns 0)
2868 **/
2869int
2870lpfc_sli4_xri_sgl_update(struct lpfc_hba *phba)
2871{
2872 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
2873 struct lpfc_scsi_buf *psb = NULL, *psb_next = NULL;
2874 uint16_t i, lxri, xri_cnt, els_xri_cnt, scsi_xri_cnt;
2875 LIST_HEAD(els_sgl_list);
2876 LIST_HEAD(scsi_sgl_list);
2877 int rc;
2878
2879 /*
2880 * update on pci function's els xri-sgl list
2881 */
2882 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
2883 if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
2884 /* els xri-sgl expanded */
2885 xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
2886 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2887 "3157 ELS xri-sgl count increased from "
2888 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
2889 els_xri_cnt);
2890 /* allocate the additional els sgls */
2891 for (i = 0; i < xri_cnt; i++) {
2892 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
2893 GFP_KERNEL);
2894 if (sglq_entry == NULL) {
2895 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2896 "2562 Failure to allocate an "
2897 "ELS sgl entry:%d\n", i);
2898 rc = -ENOMEM;
2899 goto out_free_mem;
2900 }
2901 sglq_entry->buff_type = GEN_BUFF_TYPE;
2902 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
2903 &sglq_entry->phys);
2904 if (sglq_entry->virt == NULL) {
2905 kfree(sglq_entry);
2906 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2907 "2563 Failure to allocate an "
2908 "ELS mbuf:%d\n", i);
2909 rc = -ENOMEM;
2910 goto out_free_mem;
2911 }
2912 sglq_entry->sgl = sglq_entry->virt;
2913 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
2914 sglq_entry->state = SGL_FREED;
2915 list_add_tail(&sglq_entry->list, &els_sgl_list);
2916 }
2917 spin_lock(&phba->hbalock);
2918 list_splice_init(&els_sgl_list, &phba->sli4_hba.lpfc_sgl_list);
2919 spin_unlock(&phba->hbalock);
2920 } else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
2921 /* els xri-sgl shrinked */
2922 xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
2923 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2924 "3158 ELS xri-sgl count decreased from "
2925 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
2926 els_xri_cnt);
2927 spin_lock_irq(&phba->hbalock);
2928 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &els_sgl_list);
2929 spin_unlock_irq(&phba->hbalock);
2930 /* release extra els sgls from list */
2931 for (i = 0; i < xri_cnt; i++) {
2932 list_remove_head(&els_sgl_list,
2933 sglq_entry, struct lpfc_sglq, list);
2934 if (sglq_entry) {
2935 lpfc_mbuf_free(phba, sglq_entry->virt,
2936 sglq_entry->phys);
2937 kfree(sglq_entry);
2938 }
2939 }
2940 spin_lock_irq(&phba->hbalock);
2941 list_splice_init(&els_sgl_list, &phba->sli4_hba.lpfc_sgl_list);
2942 spin_unlock_irq(&phba->hbalock);
2943 } else
2944 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2945 "3163 ELS xri-sgl count unchanged: %d\n",
2946 els_xri_cnt);
2947 phba->sli4_hba.els_xri_cnt = els_xri_cnt;
2948
2949 /* update xris to els sgls on the list */
2950 sglq_entry = NULL;
2951 sglq_entry_next = NULL;
2952 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
2953 &phba->sli4_hba.lpfc_sgl_list, list) {
2954 lxri = lpfc_sli4_next_xritag(phba);
2955 if (lxri == NO_XRI) {
2956 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2957 "2400 Failed to allocate xri for "
2958 "ELS sgl\n");
2959 rc = -ENOMEM;
2960 goto out_free_mem;
2961 }
2962 sglq_entry->sli4_lxritag = lxri;
2963 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
2964 }
2965
2966 /*
2967 * update on pci function's allocated scsi xri-sgl list
2968 */
2969 phba->total_scsi_bufs = 0;
2970
2971 /* maximum number of xris available for scsi buffers */
2972 phba->sli4_hba.scsi_xri_max = phba->sli4_hba.max_cfg_param.max_xri -
2973 els_xri_cnt;
2974
2975 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2976 "2401 Current allocated SCSI xri-sgl count:%d, "
2977 "maximum SCSI xri count:%d\n",
2978 phba->sli4_hba.scsi_xri_cnt,
2979 phba->sli4_hba.scsi_xri_max);
2980
2981 spin_lock_irq(&phba->scsi_buf_list_lock);
2982 list_splice_init(&phba->lpfc_scsi_buf_list, &scsi_sgl_list);
2983 spin_unlock_irq(&phba->scsi_buf_list_lock);
2984
2985 if (phba->sli4_hba.scsi_xri_cnt > phba->sli4_hba.scsi_xri_max) {
2986 /* max scsi xri shrinked below the allocated scsi buffers */
2987 scsi_xri_cnt = phba->sli4_hba.scsi_xri_cnt -
2988 phba->sli4_hba.scsi_xri_max;
2989 /* release the extra allocated scsi buffers */
2990 for (i = 0; i < scsi_xri_cnt; i++) {
2991 list_remove_head(&scsi_sgl_list, psb,
2992 struct lpfc_scsi_buf, list);
2993 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, psb->data,
2994 psb->dma_handle);
2995 kfree(psb);
2996 }
2997 spin_lock_irq(&phba->scsi_buf_list_lock);
2998 phba->sli4_hba.scsi_xri_cnt -= scsi_xri_cnt;
2999 spin_unlock_irq(&phba->scsi_buf_list_lock);
3000 }
3001
3002 /* update xris associated to remaining allocated scsi buffers */
3003 psb = NULL;
3004 psb_next = NULL;
3005 list_for_each_entry_safe(psb, psb_next, &scsi_sgl_list, list) {
3006 lxri = lpfc_sli4_next_xritag(phba);
3007 if (lxri == NO_XRI) {
3008 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3009 "2560 Failed to allocate xri for "
3010 "scsi buffer\n");
3011 rc = -ENOMEM;
3012 goto out_free_mem;
3013 }
3014 psb->cur_iocbq.sli4_lxritag = lxri;
3015 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3016 }
3017 spin_lock(&phba->scsi_buf_list_lock);
3018 list_splice_init(&scsi_sgl_list, &phba->lpfc_scsi_buf_list);
3019 spin_unlock(&phba->scsi_buf_list_lock);
3020
dea31012005-04-17 16:05:31 -05003021 return 0;
James Smart8a9d2e82012-05-09 21:16:12 -04003022
3023out_free_mem:
3024 lpfc_free_els_sgl_list(phba);
3025 lpfc_scsi_free(phba);
3026 return rc;
dea31012005-04-17 16:05:31 -05003027}
3028
James Smarte59058c2008-08-24 21:49:00 -04003029/**
James Smart3621a712009-04-06 18:47:14 -04003030 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04003031 * @phba: pointer to lpfc hba data structure.
3032 * @instance: a unique integer ID to this FC port.
3033 * @dev: pointer to the device data structure.
3034 *
3035 * This routine creates a FC port for the upper layer protocol. The FC port
3036 * can be created on top of either a physical port or a virtual port provided
3037 * by the HBA. This routine also allocates a SCSI host data structure (shost)
3038 * and associates the FC port created before adding the shost into the SCSI
3039 * layer.
3040 *
3041 * Return codes
3042 * @vport - pointer to the virtual N_Port data structure.
3043 * NULL - port create failed.
3044 **/
James Smart2e0fef82007-06-17 19:56:36 -05003045struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04003046lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04003047{
James Smart2e0fef82007-06-17 19:56:36 -05003048 struct lpfc_vport *vport;
3049 struct Scsi_Host *shost;
3050 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04003051
James Smart3de2a652007-08-02 11:09:59 -04003052 if (dev != &phba->pcidev->dev)
3053 shost = scsi_host_alloc(&lpfc_vport_template,
3054 sizeof(struct lpfc_vport));
3055 else
3056 shost = scsi_host_alloc(&lpfc_template,
3057 sizeof(struct lpfc_vport));
James Smart2e0fef82007-06-17 19:56:36 -05003058 if (!shost)
3059 goto out;
James Smart47a86172007-04-25 09:53:22 -04003060
James Smart2e0fef82007-06-17 19:56:36 -05003061 vport = (struct lpfc_vport *) shost->hostdata;
3062 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05003063 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05003064 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05003065 vport->fc_rscn_flush = 0;
James Smart47a86172007-04-25 09:53:22 -04003066
James Smart3de2a652007-08-02 11:09:59 -04003067 lpfc_get_vport_cfgparam(vport);
James Smart2e0fef82007-06-17 19:56:36 -05003068 shost->unique_id = instance;
3069 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04003070 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05003071 shost->this_id = -1;
3072 shost->max_cmd_len = 16;
James Smartda0436e2009-05-22 14:51:39 -04003073 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart28baac72010-02-12 14:42:03 -05003074 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04003075 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smartda0436e2009-05-22 14:51:39 -04003076 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
3077 }
James Smart81301a92008-12-04 22:39:46 -05003078
James Smart47a86172007-04-25 09:53:22 -04003079 /*
James Smart2e0fef82007-06-17 19:56:36 -05003080 * Set initial can_queue value since 0 is no longer supported and
3081 * scsi_add_host will fail. This will be adjusted later based on the
3082 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04003083 */
James Smart2e0fef82007-06-17 19:56:36 -05003084 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04003085 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05003086 shost->transportt = lpfc_vport_transport_template;
3087 vport->port_type = LPFC_NPIV_PORT;
3088 } else {
3089 shost->transportt = lpfc_transport_template;
3090 vport->port_type = LPFC_PHYSICAL_PORT;
3091 }
James Smart47a86172007-04-25 09:53:22 -04003092
James Smart2e0fef82007-06-17 19:56:36 -05003093 /* Initialize all internally managed lists. */
3094 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04003095 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05003096 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04003097
James Smart2e0fef82007-06-17 19:56:36 -05003098 init_timer(&vport->fc_disctmo);
3099 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05003100 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04003101
James Smart2e0fef82007-06-17 19:56:36 -05003102 init_timer(&vport->fc_fdmitmo);
3103 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
James Smart92d7f7b2007-06-17 19:56:38 -05003104 vport->fc_fdmitmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04003105
James Smart2e0fef82007-06-17 19:56:36 -05003106 init_timer(&vport->els_tmofunc);
3107 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05003108 vport->els_tmofunc.data = (unsigned long)vport;
James Smart92494142011-02-16 12:39:44 -05003109
3110 init_timer(&vport->delayed_disc_tmo);
3111 vport->delayed_disc_tmo.function = lpfc_delayed_disc_tmo;
3112 vport->delayed_disc_tmo.data = (unsigned long)vport;
3113
James Bottomleyd139b9b2009-11-05 13:33:12 -06003114 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05003115 if (error)
3116 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04003117
James Smart549e55c2007-08-02 11:09:51 -04003118 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003119 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04003120 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003121 return vport;
James Smart47a86172007-04-25 09:53:22 -04003122
James Smart2e0fef82007-06-17 19:56:36 -05003123out_put_shost:
3124 scsi_host_put(shost);
3125out:
3126 return NULL;
James Smart47a86172007-04-25 09:53:22 -04003127}
3128
James Smarte59058c2008-08-24 21:49:00 -04003129/**
James Smart3621a712009-04-06 18:47:14 -04003130 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04003131 * @vport: pointer to an lpfc virtual N_Port data structure.
3132 *
3133 * This routine destroys a FC port from the upper layer protocol. All the
3134 * resources associated with the port are released.
3135 **/
James Smart2e0fef82007-06-17 19:56:36 -05003136void
3137destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04003138{
James Smart92d7f7b2007-06-17 19:56:38 -05003139 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3140 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003141
James Smart858c9f62007-06-17 19:56:39 -05003142 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05003143 fc_remove_host(shost);
3144 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04003145
James Smart92d7f7b2007-06-17 19:56:38 -05003146 spin_lock_irq(&phba->hbalock);
3147 list_del_init(&vport->listentry);
3148 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04003149
James Smart92d7f7b2007-06-17 19:56:38 -05003150 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04003151 return;
James Smart47a86172007-04-25 09:53:22 -04003152}
3153
James Smarte59058c2008-08-24 21:49:00 -04003154/**
James Smart3621a712009-04-06 18:47:14 -04003155 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04003156 *
3157 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
3158 * uses the kernel idr facility to perform the task.
3159 *
3160 * Return codes:
3161 * instance - a unique integer ID allocated as the new instance.
3162 * -1 - lpfc get instance failed.
3163 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003164int
3165lpfc_get_instance(void)
3166{
Tejun Heoab516032013-02-27 17:04:44 -08003167 int ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003168
Tejun Heoab516032013-02-27 17:04:44 -08003169 ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
3170 return ret < 0 ? -1 : ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003171}
3172
James Smarte59058c2008-08-24 21:49:00 -04003173/**
James Smart3621a712009-04-06 18:47:14 -04003174 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04003175 * @shost: pointer to SCSI host data structure.
3176 * @time: elapsed time of the scan in jiffies.
3177 *
3178 * This routine is called by the SCSI layer with a SCSI host to determine
3179 * whether the scan host is finished.
3180 *
3181 * Note: there is no scan_start function as adapter initialization will have
3182 * asynchronously kicked off the link initialization.
3183 *
3184 * Return codes
3185 * 0 - SCSI host scan is not over yet.
3186 * 1 - SCSI host scan is over.
3187 **/
James Smart47a86172007-04-25 09:53:22 -04003188int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
3189{
James Smart2e0fef82007-06-17 19:56:36 -05003190 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3191 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05003192 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04003193
James Smart858c9f62007-06-17 19:56:39 -05003194 spin_lock_irq(shost->host_lock);
3195
James Smart51ef4c22007-08-02 11:10:31 -04003196 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05003197 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003198 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05003199 }
James Smart2e0fef82007-06-17 19:56:36 -05003200 if (time >= 30 * HZ) {
3201 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003202 "0461 Scanning longer than 30 "
3203 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003204 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003205 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003206 }
3207 if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
3208 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003209 "0465 Link down longer than 15 "
3210 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003211 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05003212 goto finished;
James Smart47a86172007-04-25 09:53:22 -04003213 }
3214
James Smart2e0fef82007-06-17 19:56:36 -05003215 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05003216 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003217 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05003218 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003219 if (vport->fc_map_cnt == 0 && time < 2 * HZ)
James Smart858c9f62007-06-17 19:56:39 -05003220 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003221 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05003222 goto finished;
3223
3224 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003225
3226finished:
James Smart858c9f62007-06-17 19:56:39 -05003227 spin_unlock_irq(shost->host_lock);
3228 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05003229}
3230
James Smarte59058c2008-08-24 21:49:00 -04003231/**
James Smart3621a712009-04-06 18:47:14 -04003232 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04003233 * @shost: pointer to SCSI host data structure.
3234 *
3235 * This routine initializes a given SCSI host attributes on a FC port. The
3236 * SCSI host can be either on top of a physical port or a virtual port.
3237 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003238void lpfc_host_attrib_init(struct Scsi_Host *shost)
3239{
3240 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3241 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003242 /*
James Smart2e0fef82007-06-17 19:56:36 -05003243 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04003244 */
3245
James Smart2e0fef82007-06-17 19:56:36 -05003246 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
3247 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04003248 fc_host_supported_classes(shost) = FC_COS_CLASS3;
3249
3250 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003251 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003252 fc_host_supported_fc4s(shost)[2] = 1;
3253 fc_host_supported_fc4s(shost)[7] = 1;
3254
James Smart92d7f7b2007-06-17 19:56:38 -05003255 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
3256 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04003257
3258 fc_host_supported_speeds(shost) = 0;
James Smart88a2cfb2011-07-22 18:36:33 -04003259 if (phba->lmt & LMT_16Gb)
3260 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
James Smart47a86172007-04-25 09:53:22 -04003261 if (phba->lmt & LMT_10Gb)
3262 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04003263 if (phba->lmt & LMT_8Gb)
3264 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04003265 if (phba->lmt & LMT_4Gb)
3266 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
3267 if (phba->lmt & LMT_2Gb)
3268 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
3269 if (phba->lmt & LMT_1Gb)
3270 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
3271
3272 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05003273 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
3274 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04003275
Mike Christie0af5d702010-09-15 16:52:31 -05003276 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
3277
James Smart47a86172007-04-25 09:53:22 -04003278 /* This value is also unchanging */
3279 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003280 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003281 fc_host_active_fc4s(shost)[2] = 1;
3282 fc_host_active_fc4s(shost)[7] = 1;
3283
James Smart92d7f7b2007-06-17 19:56:38 -05003284 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04003285 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04003286 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04003287 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04003288}
dea31012005-04-17 16:05:31 -05003289
James Smarte59058c2008-08-24 21:49:00 -04003290/**
James Smartda0436e2009-05-22 14:51:39 -04003291 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04003292 * @phba: pointer to lpfc hba data structure.
3293 *
James Smartda0436e2009-05-22 14:51:39 -04003294 * This routine is invoked to stop an SLI3 device port, it stops the device
3295 * from generating interrupts and stops the device driver's timers for the
3296 * device.
James Smarte59058c2008-08-24 21:49:00 -04003297 **/
James Smartdb2378e2008-02-08 18:49:51 -05003298static void
James Smartda0436e2009-05-22 14:51:39 -04003299lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05003300{
James Smartda0436e2009-05-22 14:51:39 -04003301 /* Clear all interrupt enable conditions */
3302 writel(0, phba->HCregaddr);
3303 readl(phba->HCregaddr); /* flush */
3304 /* Clear all pending interrupts */
3305 writel(0xffffffff, phba->HAregaddr);
3306 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04003307
James Smartda0436e2009-05-22 14:51:39 -04003308 /* Reset some HBA SLI setup states */
3309 lpfc_stop_hba_timers(phba);
3310 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05003311}
3312
James Smarte59058c2008-08-24 21:49:00 -04003313/**
James Smartda0436e2009-05-22 14:51:39 -04003314 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05003315 * @phba: pointer to lpfc hba data structure.
3316 *
James Smartda0436e2009-05-22 14:51:39 -04003317 * This routine is invoked to stop an SLI4 device port, it stops the device
3318 * from generating interrupts and stops the device driver's timers for the
3319 * device.
3320 **/
3321static void
3322lpfc_stop_port_s4(struct lpfc_hba *phba)
3323{
3324 /* Reset some HBA SLI4 setup states */
3325 lpfc_stop_hba_timers(phba);
3326 phba->pport->work_port_events = 0;
3327 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04003328}
3329
3330/**
3331 * lpfc_stop_port - Wrapper function for stopping hba port
3332 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05003333 *
James Smartda0436e2009-05-22 14:51:39 -04003334 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
3335 * the API jump table function pointer from the lpfc_hba struct.
3336 **/
3337void
3338lpfc_stop_port(struct lpfc_hba *phba)
3339{
3340 phba->lpfc_stop_port(phba);
3341}
3342
3343/**
James Smartecfd03c2010-02-12 14:41:27 -05003344 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
3345 * @phba: Pointer to hba for which this call is being executed.
3346 *
3347 * This routine starts the timer waiting for the FCF rediscovery to complete.
3348 **/
3349void
3350lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
3351{
3352 unsigned long fcf_redisc_wait_tmo =
3353 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
3354 /* Start fcf rediscovery wait period timer */
3355 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
3356 spin_lock_irq(&phba->hbalock);
3357 /* Allow action to new fcf asynchronous event */
3358 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
3359 /* Mark the FCF rediscovery pending state */
3360 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
3361 spin_unlock_irq(&phba->hbalock);
3362}
3363
3364/**
3365 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
3366 * @ptr: Map to lpfc_hba data structure pointer.
3367 *
3368 * This routine is invoked when waiting for FCF table rediscover has been
3369 * timed out. If new FCF record(s) has (have) been discovered during the
3370 * wait period, a new FCF event shall be added to the FCOE async event
3371 * list, and then worker thread shall be waked up for processing from the
3372 * worker thread context.
3373 **/
3374void
3375lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
3376{
3377 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
3378
3379 /* Don't send FCF rediscovery event if timer cancelled */
3380 spin_lock_irq(&phba->hbalock);
3381 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
3382 spin_unlock_irq(&phba->hbalock);
3383 return;
3384 }
3385 /* Clear FCF rediscovery timer pending flag */
3386 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
3387 /* FCF rediscovery event to worker thread */
3388 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
3389 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003390 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04003391 "2776 FCF rediscover quiescent timer expired\n");
James Smartecfd03c2010-02-12 14:41:27 -05003392 /* wake up worker thread */
3393 lpfc_worker_wake_up(phba);
3394}
3395
3396/**
James Smartda0436e2009-05-22 14:51:39 -04003397 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
3398 * @phba: pointer to lpfc hba data structure.
3399 * @acqe_link: pointer to the async link completion queue entry.
3400 *
3401 * This routine is to parse the SLI4 link-attention link fault code and
3402 * translate it into the base driver's read link attention mailbox command
3403 * status.
3404 *
3405 * Return: Link-attention status in terms of base driver's coding.
3406 **/
3407static uint16_t
3408lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
3409 struct lpfc_acqe_link *acqe_link)
3410{
3411 uint16_t latt_fault;
3412
3413 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
3414 case LPFC_ASYNC_LINK_FAULT_NONE:
3415 case LPFC_ASYNC_LINK_FAULT_LOCAL:
3416 case LPFC_ASYNC_LINK_FAULT_REMOTE:
3417 latt_fault = 0;
3418 break;
3419 default:
3420 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3421 "0398 Invalid link fault code: x%x\n",
3422 bf_get(lpfc_acqe_link_fault, acqe_link));
3423 latt_fault = MBXERR_ERROR;
3424 break;
3425 }
3426 return latt_fault;
3427}
3428
3429/**
3430 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
3431 * @phba: pointer to lpfc hba data structure.
3432 * @acqe_link: pointer to the async link completion queue entry.
3433 *
3434 * This routine is to parse the SLI4 link attention type and translate it
3435 * into the base driver's link attention type coding.
3436 *
3437 * Return: Link attention type in terms of base driver's coding.
3438 **/
3439static uint8_t
3440lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
3441 struct lpfc_acqe_link *acqe_link)
3442{
3443 uint8_t att_type;
3444
3445 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
3446 case LPFC_ASYNC_LINK_STATUS_DOWN:
3447 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
James Smart76a95d72010-11-20 23:11:48 -05003448 att_type = LPFC_ATT_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04003449 break;
3450 case LPFC_ASYNC_LINK_STATUS_UP:
3451 /* Ignore physical link up events - wait for logical link up */
James Smart76a95d72010-11-20 23:11:48 -05003452 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003453 break;
3454 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
James Smart76a95d72010-11-20 23:11:48 -05003455 att_type = LPFC_ATT_LINK_UP;
James Smartda0436e2009-05-22 14:51:39 -04003456 break;
3457 default:
3458 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3459 "0399 Invalid link attention type: x%x\n",
3460 bf_get(lpfc_acqe_link_status, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003461 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003462 break;
3463 }
3464 return att_type;
3465}
3466
3467/**
3468 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
3469 * @phba: pointer to lpfc hba data structure.
3470 * @acqe_link: pointer to the async link completion queue entry.
3471 *
3472 * This routine is to parse the SLI4 link-attention link speed and translate
3473 * it into the base driver's link-attention link speed coding.
3474 *
3475 * Return: Link-attention link speed in terms of base driver's coding.
3476 **/
3477static uint8_t
3478lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
3479 struct lpfc_acqe_link *acqe_link)
3480{
3481 uint8_t link_speed;
3482
3483 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
3484 case LPFC_ASYNC_LINK_SPEED_ZERO:
James Smartda0436e2009-05-22 14:51:39 -04003485 case LPFC_ASYNC_LINK_SPEED_10MBPS:
James Smartda0436e2009-05-22 14:51:39 -04003486 case LPFC_ASYNC_LINK_SPEED_100MBPS:
James Smart76a95d72010-11-20 23:11:48 -05003487 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003488 break;
3489 case LPFC_ASYNC_LINK_SPEED_1GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003490 link_speed = LPFC_LINK_SPEED_1GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003491 break;
3492 case LPFC_ASYNC_LINK_SPEED_10GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003493 link_speed = LPFC_LINK_SPEED_10GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003494 break;
3495 default:
3496 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3497 "0483 Invalid link-attention link speed: x%x\n",
3498 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003499 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003500 break;
3501 }
3502 return link_speed;
3503}
3504
3505/**
James Smart8b68cd52012-09-29 11:32:37 -04003506 * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
3507 * @phba: pointer to lpfc hba data structure.
3508 *
3509 * This routine is to get an SLI3 FC port's link speed in Mbps.
3510 *
3511 * Return: link speed in terms of Mbps.
3512 **/
3513uint32_t
3514lpfc_sli_port_speed_get(struct lpfc_hba *phba)
3515{
3516 uint32_t link_speed;
3517
3518 if (!lpfc_is_link_up(phba))
3519 return 0;
3520
3521 switch (phba->fc_linkspeed) {
3522 case LPFC_LINK_SPEED_1GHZ:
3523 link_speed = 1000;
3524 break;
3525 case LPFC_LINK_SPEED_2GHZ:
3526 link_speed = 2000;
3527 break;
3528 case LPFC_LINK_SPEED_4GHZ:
3529 link_speed = 4000;
3530 break;
3531 case LPFC_LINK_SPEED_8GHZ:
3532 link_speed = 8000;
3533 break;
3534 case LPFC_LINK_SPEED_10GHZ:
3535 link_speed = 10000;
3536 break;
3537 case LPFC_LINK_SPEED_16GHZ:
3538 link_speed = 16000;
3539 break;
3540 default:
3541 link_speed = 0;
3542 }
3543 return link_speed;
3544}
3545
3546/**
3547 * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
3548 * @phba: pointer to lpfc hba data structure.
3549 * @evt_code: asynchronous event code.
3550 * @speed_code: asynchronous event link speed code.
3551 *
3552 * This routine is to parse the giving SLI4 async event link speed code into
3553 * value of Mbps for the link speed.
3554 *
3555 * Return: link speed in terms of Mbps.
3556 **/
3557static uint32_t
3558lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
3559 uint8_t speed_code)
3560{
3561 uint32_t port_speed;
3562
3563 switch (evt_code) {
3564 case LPFC_TRAILER_CODE_LINK:
3565 switch (speed_code) {
3566 case LPFC_EVT_CODE_LINK_NO_LINK:
3567 port_speed = 0;
3568 break;
3569 case LPFC_EVT_CODE_LINK_10_MBIT:
3570 port_speed = 10;
3571 break;
3572 case LPFC_EVT_CODE_LINK_100_MBIT:
3573 port_speed = 100;
3574 break;
3575 case LPFC_EVT_CODE_LINK_1_GBIT:
3576 port_speed = 1000;
3577 break;
3578 case LPFC_EVT_CODE_LINK_10_GBIT:
3579 port_speed = 10000;
3580 break;
3581 default:
3582 port_speed = 0;
3583 }
3584 break;
3585 case LPFC_TRAILER_CODE_FC:
3586 switch (speed_code) {
3587 case LPFC_EVT_CODE_FC_NO_LINK:
3588 port_speed = 0;
3589 break;
3590 case LPFC_EVT_CODE_FC_1_GBAUD:
3591 port_speed = 1000;
3592 break;
3593 case LPFC_EVT_CODE_FC_2_GBAUD:
3594 port_speed = 2000;
3595 break;
3596 case LPFC_EVT_CODE_FC_4_GBAUD:
3597 port_speed = 4000;
3598 break;
3599 case LPFC_EVT_CODE_FC_8_GBAUD:
3600 port_speed = 8000;
3601 break;
3602 case LPFC_EVT_CODE_FC_10_GBAUD:
3603 port_speed = 10000;
3604 break;
3605 case LPFC_EVT_CODE_FC_16_GBAUD:
3606 port_speed = 16000;
3607 break;
3608 default:
3609 port_speed = 0;
3610 }
3611 break;
3612 default:
3613 port_speed = 0;
3614 }
3615 return port_speed;
3616}
3617
3618/**
James Smart70f3c072010-12-15 17:57:33 -05003619 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
James Smartda0436e2009-05-22 14:51:39 -04003620 * @phba: pointer to lpfc hba data structure.
3621 * @acqe_link: pointer to the async link completion queue entry.
3622 *
James Smart70f3c072010-12-15 17:57:33 -05003623 * This routine is to handle the SLI4 asynchronous FCoE link event.
James Smartda0436e2009-05-22 14:51:39 -04003624 **/
3625static void
3626lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3627 struct lpfc_acqe_link *acqe_link)
3628{
3629 struct lpfc_dmabuf *mp;
3630 LPFC_MBOXQ_t *pmb;
3631 MAILBOX_t *mb;
James Smart76a95d72010-11-20 23:11:48 -05003632 struct lpfc_mbx_read_top *la;
James Smartda0436e2009-05-22 14:51:39 -04003633 uint8_t att_type;
James Smart76a95d72010-11-20 23:11:48 -05003634 int rc;
James Smartda0436e2009-05-22 14:51:39 -04003635
3636 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05003637 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
James Smartda0436e2009-05-22 14:51:39 -04003638 return;
James Smart32b97932009-07-19 10:01:21 -04003639 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003640 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3641 if (!pmb) {
3642 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3643 "0395 The mboxq allocation failed\n");
3644 return;
3645 }
3646 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3647 if (!mp) {
3648 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3649 "0396 The lpfc_dmabuf allocation failed\n");
3650 goto out_free_pmb;
3651 }
3652 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3653 if (!mp->virt) {
3654 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3655 "0397 The mbuf allocation failed\n");
3656 goto out_free_dmabuf;
3657 }
3658
3659 /* Cleanup any outstanding ELS commands */
3660 lpfc_els_flush_all_cmd(phba);
3661
3662 /* Block ELS IOCBs until we have done process link event */
3663 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3664
3665 /* Update link event statistics */
3666 phba->sli.slistat.link_event++;
3667
James Smart76a95d72010-11-20 23:11:48 -05003668 /* Create lpfc_handle_latt mailbox command from link ACQE */
3669 lpfc_read_topology(phba, pmb, mp);
3670 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smartda0436e2009-05-22 14:51:39 -04003671 pmb->vport = phba->pport;
3672
James Smartda0436e2009-05-22 14:51:39 -04003673 /* Keep the link status for extra SLI4 state machine reference */
3674 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04003675 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
3676 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smartda0436e2009-05-22 14:51:39 -04003677 phba->sli4_hba.link_state.duplex =
3678 bf_get(lpfc_acqe_link_duplex, acqe_link);
3679 phba->sli4_hba.link_state.status =
3680 bf_get(lpfc_acqe_link_status, acqe_link);
James Smart70f3c072010-12-15 17:57:33 -05003681 phba->sli4_hba.link_state.type =
3682 bf_get(lpfc_acqe_link_type, acqe_link);
3683 phba->sli4_hba.link_state.number =
3684 bf_get(lpfc_acqe_link_number, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04003685 phba->sli4_hba.link_state.fault =
3686 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05003687 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04003688 bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
3689
James Smart70f3c072010-12-15 17:57:33 -05003690 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartc31098c2011-04-16 11:03:33 -04003691 "2900 Async FC/FCoE Link event - Speed:%dGBit "
3692 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
3693 "Logical speed:%dMbps Fault:%d\n",
James Smart70f3c072010-12-15 17:57:33 -05003694 phba->sli4_hba.link_state.speed,
3695 phba->sli4_hba.link_state.topology,
3696 phba->sli4_hba.link_state.status,
3697 phba->sli4_hba.link_state.type,
3698 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04003699 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05003700 phba->sli4_hba.link_state.fault);
James Smart76a95d72010-11-20 23:11:48 -05003701 /*
3702 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
3703 * topology info. Note: Optional for non FC-AL ports.
3704 */
3705 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3706 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3707 if (rc == MBX_NOT_FINISHED)
3708 goto out_free_dmabuf;
3709 return;
3710 }
3711 /*
3712 * For FCoE Mode: fill in all the topology information we need and call
3713 * the READ_TOPOLOGY completion routine to continue without actually
3714 * sending the READ_TOPOLOGY mailbox command to the port.
3715 */
3716 /* Parse and translate status field */
3717 mb = &pmb->u.mb;
3718 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
3719
3720 /* Parse and translate link attention fields */
3721 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
3722 la->eventTag = acqe_link->event_tag;
3723 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
3724 bf_set(lpfc_mbx_read_top_link_spd, la,
3725 lpfc_sli4_parse_latt_link_speed(phba, acqe_link));
3726
3727 /* Fake the the following irrelvant fields */
3728 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
3729 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
3730 bf_set(lpfc_mbx_read_top_il, la, 0);
3731 bf_set(lpfc_mbx_read_top_pb, la, 0);
3732 bf_set(lpfc_mbx_read_top_fa, la, 0);
3733 bf_set(lpfc_mbx_read_top_mm, la, 0);
James Smartda0436e2009-05-22 14:51:39 -04003734
3735 /* Invoke the lpfc_handle_latt mailbox command callback function */
James Smart76a95d72010-11-20 23:11:48 -05003736 lpfc_mbx_cmpl_read_topology(phba, pmb);
James Smartda0436e2009-05-22 14:51:39 -04003737
3738 return;
3739
3740out_free_dmabuf:
3741 kfree(mp);
3742out_free_pmb:
3743 mempool_free(pmb, phba->mbox_mem_pool);
3744}
3745
3746/**
James Smart70f3c072010-12-15 17:57:33 -05003747 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
3748 * @phba: pointer to lpfc hba data structure.
3749 * @acqe_fc: pointer to the async fc completion queue entry.
3750 *
3751 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
3752 * that the event was received and then issue a read_topology mailbox command so
3753 * that the rest of the driver will treat it the same as SLI3.
3754 **/
3755static void
3756lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
3757{
3758 struct lpfc_dmabuf *mp;
3759 LPFC_MBOXQ_t *pmb;
3760 int rc;
3761
3762 if (bf_get(lpfc_trailer_type, acqe_fc) !=
3763 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
3764 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3765 "2895 Non FC link Event detected.(%d)\n",
3766 bf_get(lpfc_trailer_type, acqe_fc));
3767 return;
3768 }
3769 /* Keep the link status for extra SLI4 state machine reference */
3770 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04003771 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
3772 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
James Smart70f3c072010-12-15 17:57:33 -05003773 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
3774 phba->sli4_hba.link_state.topology =
3775 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
3776 phba->sli4_hba.link_state.status =
3777 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
3778 phba->sli4_hba.link_state.type =
3779 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
3780 phba->sli4_hba.link_state.number =
3781 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
3782 phba->sli4_hba.link_state.fault =
3783 bf_get(lpfc_acqe_link_fault, acqe_fc);
3784 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04003785 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
James Smart70f3c072010-12-15 17:57:33 -05003786 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3787 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
3788 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
3789 "%dMbps Fault:%d\n",
3790 phba->sli4_hba.link_state.speed,
3791 phba->sli4_hba.link_state.topology,
3792 phba->sli4_hba.link_state.status,
3793 phba->sli4_hba.link_state.type,
3794 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04003795 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05003796 phba->sli4_hba.link_state.fault);
3797 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3798 if (!pmb) {
3799 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3800 "2897 The mboxq allocation failed\n");
3801 return;
3802 }
3803 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3804 if (!mp) {
3805 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3806 "2898 The lpfc_dmabuf allocation failed\n");
3807 goto out_free_pmb;
3808 }
3809 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3810 if (!mp->virt) {
3811 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3812 "2899 The mbuf allocation failed\n");
3813 goto out_free_dmabuf;
3814 }
3815
3816 /* Cleanup any outstanding ELS commands */
3817 lpfc_els_flush_all_cmd(phba);
3818
3819 /* Block ELS IOCBs until we have done process link event */
3820 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3821
3822 /* Update link event statistics */
3823 phba->sli.slistat.link_event++;
3824
3825 /* Create lpfc_handle_latt mailbox command from link ACQE */
3826 lpfc_read_topology(phba, pmb, mp);
3827 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
3828 pmb->vport = phba->pport;
3829
3830 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3831 if (rc == MBX_NOT_FINISHED)
3832 goto out_free_dmabuf;
3833 return;
3834
3835out_free_dmabuf:
3836 kfree(mp);
3837out_free_pmb:
3838 mempool_free(pmb, phba->mbox_mem_pool);
3839}
3840
3841/**
3842 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
3843 * @phba: pointer to lpfc hba data structure.
3844 * @acqe_fc: pointer to the async SLI completion queue entry.
3845 *
3846 * This routine is to handle the SLI4 asynchronous SLI events.
3847 **/
3848static void
3849lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
3850{
James Smart4b8bae02012-06-12 13:55:07 -04003851 char port_name;
James Smart8c1312e2012-10-31 14:45:09 -04003852 char message[128];
James Smart4b8bae02012-06-12 13:55:07 -04003853 uint8_t status;
3854 struct lpfc_acqe_misconfigured_event *misconfigured;
3855
3856 /* special case misconfigured event as it contains data for all ports */
3857 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3858 LPFC_SLI_INTF_IF_TYPE_2) ||
3859 (bf_get(lpfc_trailer_type, acqe_sli) !=
3860 LPFC_SLI_EVENT_TYPE_MISCONFIGURED)) {
3861 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3862 "2901 Async SLI event - Event Data1:x%08x Event Data2:"
3863 "x%08x SLI Event Type:%d\n",
3864 acqe_sli->event_data1, acqe_sli->event_data2,
3865 bf_get(lpfc_trailer_type, acqe_sli));
3866 return;
3867 }
3868
3869 port_name = phba->Port[0];
3870 if (port_name == 0x00)
3871 port_name = '?'; /* get port name is empty */
3872
3873 misconfigured = (struct lpfc_acqe_misconfigured_event *)
3874 &acqe_sli->event_data1;
3875
3876 /* fetch the status for this port */
3877 switch (phba->sli4_hba.lnk_info.lnk_no) {
3878 case LPFC_LINK_NUMBER_0:
3879 status = bf_get(lpfc_sli_misconfigured_port0,
3880 &misconfigured->theEvent);
3881 break;
3882 case LPFC_LINK_NUMBER_1:
3883 status = bf_get(lpfc_sli_misconfigured_port1,
3884 &misconfigured->theEvent);
3885 break;
3886 case LPFC_LINK_NUMBER_2:
3887 status = bf_get(lpfc_sli_misconfigured_port2,
3888 &misconfigured->theEvent);
3889 break;
3890 case LPFC_LINK_NUMBER_3:
3891 status = bf_get(lpfc_sli_misconfigured_port3,
3892 &misconfigured->theEvent);
3893 break;
3894 default:
3895 status = ~LPFC_SLI_EVENT_STATUS_VALID;
3896 break;
3897 }
3898
3899 switch (status) {
3900 case LPFC_SLI_EVENT_STATUS_VALID:
3901 return; /* no message if the sfp is okay */
3902 case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
James Smart292098b2012-09-29 11:31:41 -04003903 sprintf(message, "Optics faulted/incorrectly installed/not " \
3904 "installed - Reseat optics, if issue not "
3905 "resolved, replace.");
James Smart4b8bae02012-06-12 13:55:07 -04003906 break;
3907 case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
3908 sprintf(message,
James Smart292098b2012-09-29 11:31:41 -04003909 "Optics of two types installed - Remove one optic or " \
3910 "install matching pair of optics.");
James Smart4b8bae02012-06-12 13:55:07 -04003911 break;
3912 case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
James Smart292098b2012-09-29 11:31:41 -04003913 sprintf(message, "Incompatible optics - Replace with " \
3914 "compatible optics for card to function.");
James Smart4b8bae02012-06-12 13:55:07 -04003915 break;
3916 default:
3917 /* firmware is reporting a status we don't know about */
3918 sprintf(message, "Unknown event status x%02x", status);
3919 break;
3920 }
3921
3922 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3923 "3176 Misconfigured Physical Port - "
3924 "Port Name %c %s\n", port_name, message);
James Smart70f3c072010-12-15 17:57:33 -05003925}
3926
3927/**
James Smartfc2b9892010-02-26 14:15:29 -05003928 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
3929 * @vport: pointer to vport data structure.
3930 *
3931 * This routine is to perform Clear Virtual Link (CVL) on a vport in
3932 * response to a CVL event.
3933 *
3934 * Return the pointer to the ndlp with the vport if successful, otherwise
3935 * return NULL.
3936 **/
3937static struct lpfc_nodelist *
3938lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
3939{
3940 struct lpfc_nodelist *ndlp;
3941 struct Scsi_Host *shost;
3942 struct lpfc_hba *phba;
3943
3944 if (!vport)
3945 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05003946 phba = vport->phba;
3947 if (!phba)
3948 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04003949 ndlp = lpfc_findnode_did(vport, Fabric_DID);
3950 if (!ndlp) {
3951 /* Cannot find existing Fabric ndlp, so allocate a new one */
3952 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
3953 if (!ndlp)
3954 return 0;
3955 lpfc_nlp_init(vport, ndlp, Fabric_DID);
3956 /* Set the node type */
3957 ndlp->nlp_type |= NLP_FABRIC;
3958 /* Put ndlp onto node list */
3959 lpfc_enqueue_node(vport, ndlp);
3960 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
3961 /* re-setup ndlp without removing from node list */
3962 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
3963 if (!ndlp)
3964 return 0;
3965 }
James Smart63e801c2010-11-20 23:14:19 -05003966 if ((phba->pport->port_state < LPFC_FLOGI) &&
3967 (phba->pport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05003968 return NULL;
3969 /* If virtual link is not yet instantiated ignore CVL */
James Smart63e801c2010-11-20 23:14:19 -05003970 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
3971 && (vport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05003972 return NULL;
3973 shost = lpfc_shost_from_vport(vport);
3974 if (!shost)
3975 return NULL;
3976 lpfc_linkdown_port(vport);
3977 lpfc_cleanup_pending_mbox(vport);
3978 spin_lock_irq(shost->host_lock);
3979 vport->fc_flag |= FC_VPORT_CVL_RCVD;
3980 spin_unlock_irq(shost->host_lock);
3981
3982 return ndlp;
3983}
3984
3985/**
3986 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
3987 * @vport: pointer to lpfc hba data structure.
3988 *
3989 * This routine is to perform Clear Virtual Link (CVL) on all vports in
3990 * response to a FCF dead event.
3991 **/
3992static void
3993lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
3994{
3995 struct lpfc_vport **vports;
3996 int i;
3997
3998 vports = lpfc_create_vport_work_array(phba);
3999 if (vports)
4000 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
4001 lpfc_sli4_perform_vport_cvl(vports[i]);
4002 lpfc_destroy_vport_work_array(phba, vports);
4003}
4004
4005/**
James Smart76a95d72010-11-20 23:11:48 -05004006 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
James Smartda0436e2009-05-22 14:51:39 -04004007 * @phba: pointer to lpfc hba data structure.
4008 * @acqe_link: pointer to the async fcoe completion queue entry.
4009 *
4010 * This routine is to handle the SLI4 asynchronous fcoe event.
4011 **/
4012static void
James Smart76a95d72010-11-20 23:11:48 -05004013lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
James Smart70f3c072010-12-15 17:57:33 -05004014 struct lpfc_acqe_fip *acqe_fip)
James Smartda0436e2009-05-22 14:51:39 -04004015{
James Smart70f3c072010-12-15 17:57:33 -05004016 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004017 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04004018 struct lpfc_vport *vport;
4019 struct lpfc_nodelist *ndlp;
4020 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05004021 int active_vlink_present;
4022 struct lpfc_vport **vports;
4023 int i;
James Smartda0436e2009-05-22 14:51:39 -04004024
James Smart70f3c072010-12-15 17:57:33 -05004025 phba->fc_eventTag = acqe_fip->event_tag;
4026 phba->fcoe_eventtag = acqe_fip->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004027 switch (event_type) {
James Smart70f3c072010-12-15 17:57:33 -05004028 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
4029 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
4030 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
James Smart999d8132010-03-15 11:24:56 -04004031 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4032 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004033 "2546 New FCF event, evt_tag:x%x, "
4034 "index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004035 acqe_fip->event_tag,
4036 acqe_fip->index);
James Smart999d8132010-03-15 11:24:56 -04004037 else
4038 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
4039 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004040 "2788 FCF param modified event, "
4041 "evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004042 acqe_fip->event_tag,
4043 acqe_fip->index);
James Smart38b92ef2010-08-04 16:11:39 -04004044 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004045 /*
4046 * During period of FCF discovery, read the FCF
4047 * table record indexed by the event to update
James Smarta93ff372010-10-22 11:06:08 -04004048 * FCF roundrobin failover eligible FCF bmask.
James Smart0c9ab6f2010-02-26 14:15:57 -05004049 */
4050 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4051 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004052 "2779 Read FCF (x%x) for updating "
4053 "roundrobin FCF failover bmask\n",
James Smart70f3c072010-12-15 17:57:33 -05004054 acqe_fip->index);
4055 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004056 }
James Smart38b92ef2010-08-04 16:11:39 -04004057
4058 /* If the FCF discovery is in progress, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04004059 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04004060 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart38b92ef2010-08-04 16:11:39 -04004061 spin_unlock_irq(&phba->hbalock);
4062 break;
4063 }
4064 /* If fast FCF failover rescan event is pending, do nothing */
4065 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
4066 spin_unlock_irq(&phba->hbalock);
4067 break;
4068 }
4069
4070 /* If the FCF has been in discovered state, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04004071 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
4072 spin_unlock_irq(&phba->hbalock);
4073 break;
4074 }
4075 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04004076
James Smart0c9ab6f2010-02-26 14:15:57 -05004077 /* Otherwise, scan the entire FCF table and re-discover SAN */
4078 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004079 "2770 Start FCF table scan per async FCF "
4080 "event, evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004081 acqe_fip->event_tag, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004082 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
4083 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04004084 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05004085 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4086 "2547 Issue FCF scan read FCF mailbox "
James Smarta93ff372010-10-22 11:06:08 -04004087 "command failed (x%x)\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004088 break;
4089
James Smart70f3c072010-12-15 17:57:33 -05004090 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
James Smartda0436e2009-05-22 14:51:39 -04004091 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04004092 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004093 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
4094 acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04004095 break;
4096
James Smart70f3c072010-12-15 17:57:33 -05004097 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
James Smart80c17842012-03-01 22:35:45 -05004098 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05004099 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004100 "2549 FCF (x%x) disconnected from network, "
James Smart70f3c072010-12-15 17:57:33 -05004101 "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart38b92ef2010-08-04 16:11:39 -04004102 /*
4103 * If we are in the middle of FCF failover process, clear
4104 * the corresponding FCF bit in the roundrobin bitmap.
James Smartda0436e2009-05-22 14:51:39 -04004105 */
James Smartfc2b9892010-02-26 14:15:29 -05004106 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004107 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05004108 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004109 /* Update FLOGI FCF failover eligible FCF bmask */
James Smart70f3c072010-12-15 17:57:33 -05004110 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004111 break;
4112 }
James Smart38b92ef2010-08-04 16:11:39 -04004113 spin_unlock_irq(&phba->hbalock);
4114
4115 /* If the event is not for currently used fcf do nothing */
James Smart70f3c072010-12-15 17:57:33 -05004116 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
James Smart38b92ef2010-08-04 16:11:39 -04004117 break;
4118
4119 /*
4120 * Otherwise, request the port to rediscover the entire FCF
4121 * table for a fast recovery from case that the current FCF
4122 * is no longer valid as we are not in the middle of FCF
4123 * failover process already.
4124 */
4125 spin_lock_irq(&phba->hbalock);
James Smartfc2b9892010-02-26 14:15:29 -05004126 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05004127 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004128 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04004129
James Smart0c9ab6f2010-02-26 14:15:57 -05004130 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
4131 "2771 Start FCF fast failover process due to "
4132 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
James Smart70f3c072010-12-15 17:57:33 -05004133 "\n", acqe_fip->event_tag, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004134 rc = lpfc_sli4_redisc_fcf_table(phba);
4135 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004136 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4137 LOG_DISCOVERY,
4138 "2772 Issue FCF rediscover mabilbox "
4139 "command failed, fail through to FCF "
4140 "dead event\n");
James Smartfc2b9892010-02-26 14:15:29 -05004141 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004142 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004143 spin_unlock_irq(&phba->hbalock);
4144 /*
4145 * Last resort will fail over by treating this
4146 * as a link down to FCF registration.
4147 */
4148 lpfc_sli4_fcf_dead_failthrough(phba);
James Smart38b92ef2010-08-04 16:11:39 -04004149 } else {
4150 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04004151 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart38b92ef2010-08-04 16:11:39 -04004152 /*
4153 * Handling fast FCF failover to a DEAD FCF event is
4154 * considered equalivant to receiving CVL to all vports.
James Smartfc2b9892010-02-26 14:15:29 -05004155 */
4156 lpfc_sli4_perform_all_vport_cvl(phba);
James Smart38b92ef2010-08-04 16:11:39 -04004157 }
James Smartda0436e2009-05-22 14:51:39 -04004158 break;
James Smart70f3c072010-12-15 17:57:33 -05004159 case LPFC_FIP_EVENT_TYPE_CVL:
James Smart80c17842012-03-01 22:35:45 -05004160 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05004161 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6669f9b2009-10-02 15:16:45 -04004162 "2718 Clear Virtual Link Received for VPI 0x%x"
James Smart70f3c072010-12-15 17:57:33 -05004163 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart6d368e52011-05-24 11:44:12 -04004164
James Smart6669f9b2009-10-02 15:16:45 -04004165 vport = lpfc_find_vport_by_vpid(phba,
James Smart5248a742011-07-22 18:37:06 -04004166 acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004167 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004168 if (!ndlp)
4169 break;
James Smart695a8142010-01-26 23:08:03 -05004170 active_vlink_present = 0;
4171
4172 vports = lpfc_create_vport_work_array(phba);
4173 if (vports) {
4174 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
4175 i++) {
4176 if ((!(vports[i]->fc_flag &
4177 FC_VPORT_CVL_RCVD)) &&
4178 (vports[i]->port_state > LPFC_FDISC)) {
4179 active_vlink_present = 1;
4180 break;
4181 }
4182 }
4183 lpfc_destroy_vport_work_array(phba, vports);
4184 }
4185
4186 if (active_vlink_present) {
4187 /*
4188 * If there are other active VLinks present,
4189 * re-instantiate the Vlink using FDISC.
4190 */
James Smart6669f9b2009-10-02 15:16:45 -04004191 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
James Smartfc2b9892010-02-26 14:15:29 -05004192 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004193 spin_lock_irq(shost->host_lock);
4194 ndlp->nlp_flag |= NLP_DELAY_TMO;
4195 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05004196 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
4197 vport->port_state = LPFC_FDISC;
4198 } else {
James Smartecfd03c2010-02-12 14:41:27 -05004199 /*
4200 * Otherwise, we request port to rediscover
4201 * the entire FCF table for a fast recovery
4202 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05004203 * is no longer valid if we are not already
4204 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05004205 */
James Smartfc2b9892010-02-26 14:15:29 -05004206 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004207 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05004208 spin_unlock_irq(&phba->hbalock);
4209 break;
4210 }
4211 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05004212 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004213 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004214 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4215 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004216 "2773 Start FCF failover per CVL, "
James Smart70f3c072010-12-15 17:57:33 -05004217 "evt_tag:x%x\n", acqe_fip->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05004218 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05004219 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004220 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4221 LOG_DISCOVERY,
4222 "2774 Issue FCF rediscover "
4223 "mabilbox command failed, "
4224 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05004225 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004226 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004227 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05004228 /*
4229 * Last resort will be re-try on the
4230 * the current registered FCF entry.
4231 */
4232 lpfc_retry_pport_discovery(phba);
James Smart38b92ef2010-08-04 16:11:39 -04004233 } else
4234 /*
4235 * Reset FCF roundrobin bmask for new
4236 * discovery.
4237 */
James Smart7d791df2011-07-22 18:37:52 -04004238 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart6669f9b2009-10-02 15:16:45 -04004239 }
4240 break;
James Smartda0436e2009-05-22 14:51:39 -04004241 default:
4242 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4243 "0288 Unknown FCoE event type 0x%x event tag "
James Smart70f3c072010-12-15 17:57:33 -05004244 "0x%x\n", event_type, acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04004245 break;
4246 }
4247}
4248
4249/**
4250 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
4251 * @phba: pointer to lpfc hba data structure.
4252 * @acqe_link: pointer to the async dcbx completion queue entry.
4253 *
4254 * This routine is to handle the SLI4 asynchronous dcbx event.
4255 **/
4256static void
4257lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
4258 struct lpfc_acqe_dcbx *acqe_dcbx)
4259{
James Smart4d9ab992009-10-02 15:16:39 -04004260 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004261 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4262 "0290 The SLI4 DCBX asynchronous event is not "
4263 "handled yet\n");
4264}
4265
4266/**
James Smartb19a0612010-04-06 14:48:51 -04004267 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
4268 * @phba: pointer to lpfc hba data structure.
4269 * @acqe_link: pointer to the async grp5 completion queue entry.
4270 *
4271 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
4272 * is an asynchronous notified of a logical link speed change. The Port
4273 * reports the logical link speed in units of 10Mbps.
4274 **/
4275static void
4276lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
4277 struct lpfc_acqe_grp5 *acqe_grp5)
4278{
4279 uint16_t prev_ll_spd;
4280
4281 phba->fc_eventTag = acqe_grp5->event_tag;
4282 phba->fcoe_eventtag = acqe_grp5->event_tag;
4283 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
4284 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04004285 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
James Smartb19a0612010-04-06 14:48:51 -04004286 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4287 "2789 GRP5 Async Event: Updating logical link speed "
James Smart8b68cd52012-09-29 11:32:37 -04004288 "from %dMbps to %dMbps\n", prev_ll_spd,
4289 phba->sli4_hba.link_state.logical_speed);
James Smartb19a0612010-04-06 14:48:51 -04004290}
4291
4292/**
James Smartda0436e2009-05-22 14:51:39 -04004293 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
4294 * @phba: pointer to lpfc hba data structure.
4295 *
4296 * This routine is invoked by the worker thread to process all the pending
4297 * SLI4 asynchronous events.
4298 **/
4299void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
4300{
4301 struct lpfc_cq_event *cq_event;
4302
4303 /* First, declare the async event has been handled */
4304 spin_lock_irq(&phba->hbalock);
4305 phba->hba_flag &= ~ASYNC_EVENT;
4306 spin_unlock_irq(&phba->hbalock);
4307 /* Now, handle all the async events */
4308 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
4309 /* Get the first event from the head of the event queue */
4310 spin_lock_irq(&phba->hbalock);
4311 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
4312 cq_event, struct lpfc_cq_event, list);
4313 spin_unlock_irq(&phba->hbalock);
4314 /* Process the asynchronous event */
4315 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
4316 case LPFC_TRAILER_CODE_LINK:
4317 lpfc_sli4_async_link_evt(phba,
4318 &cq_event->cqe.acqe_link);
4319 break;
4320 case LPFC_TRAILER_CODE_FCOE:
James Smart70f3c072010-12-15 17:57:33 -05004321 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004322 break;
4323 case LPFC_TRAILER_CODE_DCBX:
4324 lpfc_sli4_async_dcbx_evt(phba,
4325 &cq_event->cqe.acqe_dcbx);
4326 break;
James Smartb19a0612010-04-06 14:48:51 -04004327 case LPFC_TRAILER_CODE_GRP5:
4328 lpfc_sli4_async_grp5_evt(phba,
4329 &cq_event->cqe.acqe_grp5);
4330 break;
James Smart70f3c072010-12-15 17:57:33 -05004331 case LPFC_TRAILER_CODE_FC:
4332 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
4333 break;
4334 case LPFC_TRAILER_CODE_SLI:
4335 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
4336 break;
James Smartda0436e2009-05-22 14:51:39 -04004337 default:
4338 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4339 "1804 Invalid asynchrous event code: "
4340 "x%x\n", bf_get(lpfc_trailer_code,
4341 &cq_event->cqe.mcqe_cmpl));
4342 break;
4343 }
4344 /* Free the completion event processed to the free pool */
4345 lpfc_sli4_cq_event_release(phba, cq_event);
4346 }
4347}
4348
4349/**
James Smartecfd03c2010-02-12 14:41:27 -05004350 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
4351 * @phba: pointer to lpfc hba data structure.
4352 *
4353 * This routine is invoked by the worker thread to process FCF table
4354 * rediscovery pending completion event.
4355 **/
4356void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
4357{
4358 int rc;
4359
4360 spin_lock_irq(&phba->hbalock);
4361 /* Clear FCF rediscovery timeout event */
4362 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
4363 /* Clear driver fast failover FCF record flag */
4364 phba->fcf.failover_rec.flag = 0;
4365 /* Set state for FCF fast failover */
4366 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
4367 spin_unlock_irq(&phba->hbalock);
4368
4369 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05004370 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004371 "2777 Start post-quiescent FCF table scan\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05004372 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05004373 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05004374 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4375 "2747 Issue FCF scan read FCF mailbox "
4376 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05004377}
4378
4379/**
James Smartda0436e2009-05-22 14:51:39 -04004380 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
4381 * @phba: pointer to lpfc hba data structure.
4382 * @dev_grp: The HBA PCI-Device group number.
4383 *
4384 * This routine is invoked to set up the per HBA PCI-Device group function
4385 * API jump table entries.
4386 *
4387 * Return: 0 if success, otherwise -ENODEV
4388 **/
4389int
4390lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05004391{
4392 int rc;
4393
James Smartda0436e2009-05-22 14:51:39 -04004394 /* Set up lpfc PCI-device group */
4395 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05004396
James Smartda0436e2009-05-22 14:51:39 -04004397 /* The LPFC_PCI_DEV_OC uses SLI4 */
4398 if (dev_grp == LPFC_PCI_DEV_OC)
4399 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05004400
James Smartda0436e2009-05-22 14:51:39 -04004401 /* Set up device INIT API function jump table */
4402 rc = lpfc_init_api_table_setup(phba, dev_grp);
4403 if (rc)
4404 return -ENODEV;
4405 /* Set up SCSI API function jump table */
4406 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
4407 if (rc)
4408 return -ENODEV;
4409 /* Set up SLI API function jump table */
4410 rc = lpfc_sli_api_table_setup(phba, dev_grp);
4411 if (rc)
4412 return -ENODEV;
4413 /* Set up MBOX API function jump table */
4414 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
4415 if (rc)
4416 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004417
James Smartda0436e2009-05-22 14:51:39 -04004418 return 0;
James Smart5b75da22008-12-04 22:39:35 -05004419}
4420
4421/**
James Smart3621a712009-04-06 18:47:14 -04004422 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05004423 * @phba: pointer to lpfc hba data structure.
4424 * @intr_mode: active interrupt mode adopted.
4425 *
4426 * This routine it invoked to log the currently used active interrupt mode
4427 * to the device.
James Smart3772a992009-05-22 14:50:54 -04004428 **/
4429static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05004430{
4431 switch (intr_mode) {
4432 case 0:
4433 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4434 "0470 Enable INTx interrupt mode.\n");
4435 break;
4436 case 1:
4437 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4438 "0481 Enabled MSI interrupt mode.\n");
4439 break;
4440 case 2:
4441 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4442 "0480 Enabled MSI-X interrupt mode.\n");
4443 break;
4444 default:
4445 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4446 "0482 Illegal interrupt mode.\n");
4447 break;
4448 }
4449 return;
4450}
4451
James Smart5b75da22008-12-04 22:39:35 -05004452/**
James Smart3772a992009-05-22 14:50:54 -04004453 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05004454 * @phba: pointer to lpfc hba data structure.
4455 *
James Smart3772a992009-05-22 14:50:54 -04004456 * This routine is invoked to enable the PCI device that is common to all
4457 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004458 *
4459 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004460 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004461 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05004462 **/
James Smart3772a992009-05-22 14:50:54 -04004463static int
4464lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004465{
James Smart3772a992009-05-22 14:50:54 -04004466 struct pci_dev *pdev;
James Smart079b5c92011-08-21 21:48:49 -04004467 int bars = 0;
James Smart5b75da22008-12-04 22:39:35 -05004468
James Smart3772a992009-05-22 14:50:54 -04004469 /* Obtain PCI device reference */
4470 if (!phba->pcidev)
4471 goto out_error;
4472 else
4473 pdev = phba->pcidev;
4474 /* Select PCI BARs */
4475 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4476 /* Enable PCI device */
4477 if (pci_enable_device_mem(pdev))
4478 goto out_error;
4479 /* Request PCI resource for the device */
4480 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
4481 goto out_disable_device;
4482 /* Set up device as PCI master and save state for EEH */
4483 pci_set_master(pdev);
4484 pci_try_set_mwi(pdev);
4485 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05004486
James Smart05580562011-05-24 11:40:48 -04004487 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
4488 if (pci_find_capability(pdev, PCI_CAP_ID_EXP))
4489 pdev->needs_freset = 1;
4490
James Smart3772a992009-05-22 14:50:54 -04004491 return 0;
James Smart5b75da22008-12-04 22:39:35 -05004492
James Smart3772a992009-05-22 14:50:54 -04004493out_disable_device:
4494 pci_disable_device(pdev);
4495out_error:
James Smart079b5c92011-08-21 21:48:49 -04004496 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4497 "1401 Failed to enable pci device, bars:x%x\n", bars);
James Smart3772a992009-05-22 14:50:54 -04004498 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004499}
4500
4501/**
James Smart3772a992009-05-22 14:50:54 -04004502 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05004503 * @phba: pointer to lpfc hba data structure.
4504 *
James Smart3772a992009-05-22 14:50:54 -04004505 * This routine is invoked to disable the PCI device that is common to all
4506 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004507 **/
4508static void
James Smart3772a992009-05-22 14:50:54 -04004509lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004510{
James Smart3772a992009-05-22 14:50:54 -04004511 struct pci_dev *pdev;
4512 int bars;
James Smart5b75da22008-12-04 22:39:35 -05004513
James Smart3772a992009-05-22 14:50:54 -04004514 /* Obtain PCI device reference */
4515 if (!phba->pcidev)
4516 return;
4517 else
4518 pdev = phba->pcidev;
4519 /* Select PCI BARs */
4520 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4521 /* Release PCI resource and disable PCI device */
4522 pci_release_selected_regions(pdev, bars);
4523 pci_disable_device(pdev);
4524 /* Null out PCI private reference to driver */
4525 pci_set_drvdata(pdev, NULL);
James Smart5b75da22008-12-04 22:39:35 -05004526
4527 return;
4528}
4529
4530/**
James Smart3772a992009-05-22 14:50:54 -04004531 * lpfc_reset_hba - Reset a hba
4532 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04004533 *
James Smart3772a992009-05-22 14:50:54 -04004534 * This routine is invoked to reset a hba device. It brings the HBA
4535 * offline, performs a board restart, and then brings the board back
4536 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
4537 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04004538 **/
James Smart3772a992009-05-22 14:50:54 -04004539void
4540lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004541{
James Smart3772a992009-05-22 14:50:54 -04004542 /* If resets are disabled then set error state and return. */
4543 if (!phba->cfg_enable_hba_reset) {
4544 phba->link_state = LPFC_HBA_ERROR;
4545 return;
4546 }
James Smart618a5232012-06-12 13:54:36 -04004547 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3772a992009-05-22 14:50:54 -04004548 lpfc_offline(phba);
4549 lpfc_sli_brdrestart(phba);
4550 lpfc_online(phba);
4551 lpfc_unblock_mgmt_io(phba);
4552}
dea31012005-04-17 16:05:31 -05004553
James Smart3772a992009-05-22 14:50:54 -04004554/**
James Smart0a96e972011-07-22 18:37:28 -04004555 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
4556 * @phba: pointer to lpfc hba data structure.
4557 *
4558 * This function enables the PCI SR-IOV virtual functions to a physical
4559 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4560 * enable the number of virtual functions to the physical function. As
4561 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4562 * API call does not considered as an error condition for most of the device.
4563 **/
4564uint16_t
4565lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
4566{
4567 struct pci_dev *pdev = phba->pcidev;
4568 uint16_t nr_virtfn;
4569 int pos;
4570
James Smart0a96e972011-07-22 18:37:28 -04004571 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
4572 if (pos == 0)
4573 return 0;
4574
4575 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
4576 return nr_virtfn;
4577}
4578
4579/**
James Smart912e3ac2011-05-24 11:42:11 -04004580 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
4581 * @phba: pointer to lpfc hba data structure.
4582 * @nr_vfn: number of virtual functions to be enabled.
4583 *
4584 * This function enables the PCI SR-IOV virtual functions to a physical
4585 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4586 * enable the number of virtual functions to the physical function. As
4587 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4588 * API call does not considered as an error condition for most of the device.
4589 **/
4590int
4591lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
4592{
4593 struct pci_dev *pdev = phba->pcidev;
James Smart0a96e972011-07-22 18:37:28 -04004594 uint16_t max_nr_vfn;
James Smart912e3ac2011-05-24 11:42:11 -04004595 int rc;
4596
James Smart0a96e972011-07-22 18:37:28 -04004597 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
4598 if (nr_vfn > max_nr_vfn) {
4599 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4600 "3057 Requested vfs (%d) greater than "
4601 "supported vfs (%d)", nr_vfn, max_nr_vfn);
4602 return -EINVAL;
4603 }
4604
James Smart912e3ac2011-05-24 11:42:11 -04004605 rc = pci_enable_sriov(pdev, nr_vfn);
4606 if (rc) {
4607 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4608 "2806 Failed to enable sriov on this device "
4609 "with vfn number nr_vf:%d, rc:%d\n",
4610 nr_vfn, rc);
4611 } else
4612 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4613 "2807 Successful enable sriov on this device "
4614 "with vfn number nr_vf:%d\n", nr_vfn);
4615 return rc;
4616}
4617
4618/**
James Smart3772a992009-05-22 14:50:54 -04004619 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
4620 * @phba: pointer to lpfc hba data structure.
4621 *
4622 * This routine is invoked to set up the driver internal resources specific to
4623 * support the SLI-3 HBA device it attached to.
4624 *
4625 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004626 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004627 * other values - error
4628 **/
4629static int
4630lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
4631{
4632 struct lpfc_sli *psli;
James Smart912e3ac2011-05-24 11:42:11 -04004633 int rc;
dea31012005-04-17 16:05:31 -05004634
James Smart3772a992009-05-22 14:50:54 -04004635 /*
4636 * Initialize timers used by driver
4637 */
dea31012005-04-17 16:05:31 -05004638
James Smart3772a992009-05-22 14:50:54 -04004639 /* Heartbeat timer */
4640 init_timer(&phba->hb_tmofunc);
4641 phba->hb_tmofunc.function = lpfc_hb_timeout;
4642 phba->hb_tmofunc.data = (unsigned long)phba;
4643
4644 psli = &phba->sli;
4645 /* MBOX heartbeat timer */
4646 init_timer(&psli->mbox_tmo);
4647 psli->mbox_tmo.function = lpfc_mbox_timeout;
4648 psli->mbox_tmo.data = (unsigned long) phba;
4649 /* FCP polling mode timer */
4650 init_timer(&phba->fcp_poll_timer);
4651 phba->fcp_poll_timer.function = lpfc_poll_timeout;
4652 phba->fcp_poll_timer.data = (unsigned long) phba;
4653 /* Fabric block timer */
4654 init_timer(&phba->fabric_block_timer);
4655 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4656 phba->fabric_block_timer.data = (unsigned long) phba;
4657 /* EA polling mode timer */
4658 init_timer(&phba->eratt_poll);
4659 phba->eratt_poll.function = lpfc_poll_eratt;
4660 phba->eratt_poll.data = (unsigned long) phba;
4661
4662 /* Host attention work mask setup */
4663 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
4664 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
4665
4666 /* Get all the module params for configuring this host */
4667 lpfc_get_cfgparam(phba);
James Smart49198b32010-04-06 15:04:33 -04004668 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
4669 phba->menlo_flag |= HBA_MENLO_SUPPORT;
4670 /* check for menlo minimum sg count */
4671 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
4672 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
4673 }
4674
James Smart2a76a282012-08-03 12:35:54 -04004675 if (!phba->sli.ring)
4676 phba->sli.ring = (struct lpfc_sli_ring *)
4677 kzalloc(LPFC_SLI3_MAX_RING *
4678 sizeof(struct lpfc_sli_ring), GFP_KERNEL);
4679 if (!phba->sli.ring)
4680 return -ENOMEM;
4681
James Smart3772a992009-05-22 14:50:54 -04004682 /*
4683 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
4684 * used to create the sg_dma_buf_pool must be dynamically calculated.
4685 * 2 segments are added since the IOCB needs a command and response bde.
4686 */
4687 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4688 sizeof(struct fcp_rsp) +
4689 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
4690
4691 if (phba->cfg_enable_bg) {
4692 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT;
4693 phba->cfg_sg_dma_buf_size +=
4694 phba->cfg_prot_sg_seg_cnt * sizeof(struct ulp_bde64);
4695 }
4696
4697 /* Also reinitialize the host templates with new values. */
4698 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4699 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4700
4701 phba->max_vpi = LPFC_MAX_VPI;
4702 /* This will be set to correct value after config_port mbox */
4703 phba->max_vports = 0;
4704
4705 /*
4706 * Initialize the SLI Layer to run with lpfc HBAs.
4707 */
4708 lpfc_sli_setup(phba);
4709 lpfc_sli_queue_setup(phba);
4710
4711 /* Allocate device driver memory */
4712 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
4713 return -ENOMEM;
4714
James Smart912e3ac2011-05-24 11:42:11 -04004715 /*
4716 * Enable sr-iov virtual functions if supported and configured
4717 * through the module parameter.
4718 */
4719 if (phba->cfg_sriov_nr_virtfn > 0) {
4720 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
4721 phba->cfg_sriov_nr_virtfn);
4722 if (rc) {
4723 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4724 "2808 Requested number of SR-IOV "
4725 "virtual functions (%d) is not "
4726 "supported\n",
4727 phba->cfg_sriov_nr_virtfn);
4728 phba->cfg_sriov_nr_virtfn = 0;
4729 }
4730 }
4731
James Smart3772a992009-05-22 14:50:54 -04004732 return 0;
4733}
4734
4735/**
4736 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
4737 * @phba: pointer to lpfc hba data structure.
4738 *
4739 * This routine is invoked to unset the driver internal resources set up
4740 * specific for supporting the SLI-3 HBA device it attached to.
4741 **/
4742static void
4743lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
4744{
4745 /* Free device driver memory allocated */
4746 lpfc_mem_free_all(phba);
4747
4748 return;
4749}
4750
4751/**
James Smartda0436e2009-05-22 14:51:39 -04004752 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
4753 * @phba: pointer to lpfc hba data structure.
4754 *
4755 * This routine is invoked to set up the driver internal resources specific to
4756 * support the SLI-4 HBA device it attached to.
4757 *
4758 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004759 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004760 * other values - error
4761 **/
4762static int
4763lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
4764{
4765 struct lpfc_sli *psli;
James Smart28baac72010-02-12 14:42:03 -05004766 LPFC_MBOXQ_t *mboxq;
4767 int rc, i, hbq_count, buf_size, dma_buf_size, max_buf_size;
4768 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
4769 struct lpfc_mqe *mqe;
James Smart085c6472010-11-20 23:11:37 -05004770 int longs, sli_family;
James Smartacd68592012-01-18 16:25:09 -05004771 int sges_per_segment;
James Smartda0436e2009-05-22 14:51:39 -04004772
4773 /* Before proceed, wait for POST done and device ready */
4774 rc = lpfc_sli4_post_status_check(phba);
4775 if (rc)
4776 return -ENODEV;
4777
4778 /*
4779 * Initialize timers used by driver
4780 */
4781
4782 /* Heartbeat timer */
4783 init_timer(&phba->hb_tmofunc);
4784 phba->hb_tmofunc.function = lpfc_hb_timeout;
4785 phba->hb_tmofunc.data = (unsigned long)phba;
James Smart19ca7602010-11-20 23:11:55 -05004786 init_timer(&phba->rrq_tmr);
4787 phba->rrq_tmr.function = lpfc_rrq_timeout;
4788 phba->rrq_tmr.data = (unsigned long)phba;
James Smartda0436e2009-05-22 14:51:39 -04004789
4790 psli = &phba->sli;
4791 /* MBOX heartbeat timer */
4792 init_timer(&psli->mbox_tmo);
4793 psli->mbox_tmo.function = lpfc_mbox_timeout;
4794 psli->mbox_tmo.data = (unsigned long) phba;
4795 /* Fabric block timer */
4796 init_timer(&phba->fabric_block_timer);
4797 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4798 phba->fabric_block_timer.data = (unsigned long) phba;
4799 /* EA polling mode timer */
4800 init_timer(&phba->eratt_poll);
4801 phba->eratt_poll.function = lpfc_poll_eratt;
4802 phba->eratt_poll.data = (unsigned long) phba;
James Smartecfd03c2010-02-12 14:41:27 -05004803 /* FCF rediscover timer */
4804 init_timer(&phba->fcf.redisc_wait);
4805 phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
4806 phba->fcf.redisc_wait.data = (unsigned long)phba;
4807
James Smartda0436e2009-05-22 14:51:39 -04004808 /*
James Smart7ad20aa2011-05-24 11:44:28 -04004809 * Control structure for handling external multi-buffer mailbox
4810 * command pass-through.
4811 */
4812 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
4813 sizeof(struct lpfc_mbox_ext_buf_ctx));
4814 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
4815
4816 /*
James Smartda0436e2009-05-22 14:51:39 -04004817 * We need to do a READ_CONFIG mailbox command here before
4818 * calling lpfc_get_cfgparam. For VFs this will report the
4819 * MAX_XRI, MAX_VPI, MAX_RPI, MAX_IOCB, and MAX_VFI settings.
4820 * All of the resources allocated
4821 * for this Port are tied to these values.
4822 */
4823 /* Get all the module params for configuring this host */
4824 lpfc_get_cfgparam(phba);
4825 phba->max_vpi = LPFC_MAX_VPI;
James Smart67d12732012-08-03 12:36:13 -04004826
4827 /* Eventually cfg_fcp_eq_count / cfg_fcp_wq_count will be depricated */
4828 phba->cfg_fcp_io_channel = phba->cfg_fcp_eq_count;
4829
James Smartda0436e2009-05-22 14:51:39 -04004830 /* This will be set to correct value after the read_config mbox */
4831 phba->max_vports = 0;
4832
4833 /* Program the default value of vlan_id and fc_map */
4834 phba->valid_vlan = 0;
4835 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4836 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4837 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4838
James Smartacd68592012-01-18 16:25:09 -05004839 /* With BlockGuard we can have multiple SGEs per Data Segemnt */
4840 sges_per_segment = 1;
4841 if (phba->cfg_enable_bg)
4842 sges_per_segment = 2;
4843
James Smartda0436e2009-05-22 14:51:39 -04004844 /*
James Smart2a76a282012-08-03 12:35:54 -04004845 * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
4846 * we will associate a new ring, for each FCP fastpath EQ/CQ/WQ tuple.
4847 */
4848 if (!phba->sli.ring)
4849 phba->sli.ring = kzalloc(
James Smart67d12732012-08-03 12:36:13 -04004850 (LPFC_SLI3_MAX_RING + phba->cfg_fcp_io_channel) *
James Smart2a76a282012-08-03 12:35:54 -04004851 sizeof(struct lpfc_sli_ring), GFP_KERNEL);
4852 if (!phba->sli.ring)
4853 return -ENOMEM;
4854 /*
James Smartda0436e2009-05-22 14:51:39 -04004855 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
4856 * used to create the sg_dma_buf_pool must be dynamically calculated.
4857 * 2 segments are added since the IOCB needs a command and response bde.
4858 * To insure that the scsi sgl does not cross a 4k page boundary only
James Smart28baac72010-02-12 14:42:03 -05004859 * sgl sizes of must be a power of 2.
James Smartda0436e2009-05-22 14:51:39 -04004860 */
James Smart28baac72010-02-12 14:42:03 -05004861 buf_size = (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp) +
James Smartacd68592012-01-18 16:25:09 -05004862 (((phba->cfg_sg_seg_cnt * sges_per_segment) + 2) *
4863 sizeof(struct sli4_sge)));
James Smart085c6472010-11-20 23:11:37 -05004864
4865 sli_family = bf_get(lpfc_sli_intf_sli_family, &phba->sli4_hba.sli_intf);
4866 max_buf_size = LPFC_SLI4_MAX_BUF_SIZE;
4867 switch (sli_family) {
4868 case LPFC_SLI_INTF_FAMILY_BE2:
4869 case LPFC_SLI_INTF_FAMILY_BE3:
4870 /* There is a single hint for BE - 2 pages per BPL. */
4871 if (bf_get(lpfc_sli_intf_sli_hint1, &phba->sli4_hba.sli_intf) ==
4872 LPFC_SLI_INTF_SLI_HINT1_1)
4873 max_buf_size = LPFC_SLI4_FL1_MAX_BUF_SIZE;
4874 break;
4875 case LPFC_SLI_INTF_FAMILY_LNCR_A0:
4876 case LPFC_SLI_INTF_FAMILY_LNCR_B0:
4877 default:
4878 break;
4879 }
James Smartacd68592012-01-18 16:25:09 -05004880
James Smart28baac72010-02-12 14:42:03 -05004881 for (dma_buf_size = LPFC_SLI4_MIN_BUF_SIZE;
4882 dma_buf_size < max_buf_size && buf_size > dma_buf_size;
4883 dma_buf_size = dma_buf_size << 1)
4884 ;
4885 if (dma_buf_size == max_buf_size)
4886 phba->cfg_sg_seg_cnt = (dma_buf_size -
4887 sizeof(struct fcp_cmnd) - sizeof(struct fcp_rsp) -
4888 (2 * sizeof(struct sli4_sge))) /
4889 sizeof(struct sli4_sge);
4890 phba->cfg_sg_dma_buf_size = dma_buf_size;
James Smartda0436e2009-05-22 14:51:39 -04004891
4892 /* Initialize buffer queue management fields */
4893 hbq_count = lpfc_sli_hbq_count();
4894 for (i = 0; i < hbq_count; ++i)
4895 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
4896 INIT_LIST_HEAD(&phba->rb_pend_list);
4897 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
4898 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
4899
4900 /*
4901 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
4902 */
4903 /* Initialize the Abort scsi buffer list used by driver */
4904 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
4905 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
4906 /* This abort list used by worker thread */
4907 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
4908
4909 /*
James Smart6d368e52011-05-24 11:44:12 -04004910 * Initialize driver internal slow-path work queues
James Smartda0436e2009-05-22 14:51:39 -04004911 */
4912
4913 /* Driver internel slow-path CQ Event pool */
4914 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
4915 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04004916 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04004917 /* Asynchronous event CQ Event work queue list */
4918 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
4919 /* Fast-path XRI aborted CQ Event work queue list */
4920 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
4921 /* Slow-path XRI aborted CQ Event work queue list */
4922 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
4923 /* Receive queue CQ Event work queue list */
4924 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
4925
James Smart6d368e52011-05-24 11:44:12 -04004926 /* Initialize extent block lists. */
4927 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
4928 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
4929 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
4930 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
4931
James Smartda0436e2009-05-22 14:51:39 -04004932 /* Initialize the driver internal SLI layer lists. */
4933 lpfc_sli_setup(phba);
4934 lpfc_sli_queue_setup(phba);
4935
4936 /* Allocate device driver memory */
4937 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
4938 if (rc)
4939 return -ENOMEM;
4940
James Smart2fcee4b2010-12-15 17:57:46 -05004941 /* IF Type 2 ports get initialized now. */
4942 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
4943 LPFC_SLI_INTF_IF_TYPE_2) {
4944 rc = lpfc_pci_function_reset(phba);
4945 if (unlikely(rc))
4946 return -ENODEV;
4947 }
4948
James Smartda0436e2009-05-22 14:51:39 -04004949 /* Create the bootstrap mailbox command */
4950 rc = lpfc_create_bootstrap_mbox(phba);
4951 if (unlikely(rc))
4952 goto out_free_mem;
4953
4954 /* Set up the host's endian order with the device. */
4955 rc = lpfc_setup_endian_order(phba);
4956 if (unlikely(rc))
4957 goto out_free_bsmbx;
4958
4959 /* Set up the hba's configuration parameters. */
4960 rc = lpfc_sli4_read_config(phba);
4961 if (unlikely(rc))
4962 goto out_free_bsmbx;
4963
James Smart2fcee4b2010-12-15 17:57:46 -05004964 /* IF Type 0 ports get initialized now. */
4965 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
4966 LPFC_SLI_INTF_IF_TYPE_0) {
4967 rc = lpfc_pci_function_reset(phba);
4968 if (unlikely(rc))
4969 goto out_free_bsmbx;
4970 }
James Smartda0436e2009-05-22 14:51:39 -04004971
James Smartcb5172e2010-03-15 11:25:07 -04004972 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
4973 GFP_KERNEL);
4974 if (!mboxq) {
4975 rc = -ENOMEM;
4976 goto out_free_bsmbx;
4977 }
4978
James Smartfedd3b72011-02-16 12:39:24 -05004979 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
James Smartcb5172e2010-03-15 11:25:07 -04004980 lpfc_supported_pages(mboxq);
4981 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smartfedd3b72011-02-16 12:39:24 -05004982 if (!rc) {
4983 mqe = &mboxq->u.mqe;
4984 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
4985 LPFC_MAX_SUPPORTED_PAGES);
4986 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
4987 switch (pn_page[i]) {
4988 case LPFC_SLI4_PARAMETERS:
4989 phba->sli4_hba.pc_sli4_params.supported = 1;
4990 break;
4991 default:
4992 break;
4993 }
4994 }
4995 /* Read the port's SLI4 Parameters capabilities if supported. */
4996 if (phba->sli4_hba.pc_sli4_params.supported)
4997 rc = lpfc_pc_sli4_params_get(phba, mboxq);
4998 if (rc) {
4999 mempool_free(mboxq, phba->mbox_mem_pool);
5000 rc = -EIO;
5001 goto out_free_bsmbx;
James Smartcb5172e2010-03-15 11:25:07 -04005002 }
5003 }
James Smartfedd3b72011-02-16 12:39:24 -05005004 /*
5005 * Get sli4 parameters that override parameters from Port capabilities.
James Smart6d368e52011-05-24 11:44:12 -04005006 * If this call fails, it isn't critical unless the SLI4 parameters come
5007 * back in conflict.
James Smartfedd3b72011-02-16 12:39:24 -05005008 */
James Smart6d368e52011-05-24 11:44:12 -04005009 rc = lpfc_get_sli4_parameters(phba, mboxq);
5010 if (rc) {
5011 if (phba->sli4_hba.extents_in_use &&
5012 phba->sli4_hba.rpi_hdrs_in_use) {
5013 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5014 "2999 Unsupported SLI4 Parameters "
5015 "Extents and RPI headers enabled.\n");
5016 goto out_free_bsmbx;
5017 }
5018 }
James Smartcb5172e2010-03-15 11:25:07 -04005019 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart5350d872011-10-10 21:33:49 -04005020 /* Verify all the SLI4 queues */
5021 rc = lpfc_sli4_queue_verify(phba);
James Smartda0436e2009-05-22 14:51:39 -04005022 if (rc)
5023 goto out_free_bsmbx;
5024
5025 /* Create driver internal CQE event pool */
5026 rc = lpfc_sli4_cq_event_pool_create(phba);
5027 if (rc)
James Smart5350d872011-10-10 21:33:49 -04005028 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04005029
James Smart8a9d2e82012-05-09 21:16:12 -04005030 /* Initialize sgl lists per host */
5031 lpfc_init_sgl_list(phba);
5032
5033 /* Allocate and initialize active sgl array */
James Smartda0436e2009-05-22 14:51:39 -04005034 rc = lpfc_init_active_sgl_array(phba);
5035 if (rc) {
5036 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5037 "1430 Failed to initialize sgl list.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04005038 goto out_destroy_cq_event_pool;
James Smartda0436e2009-05-22 14:51:39 -04005039 }
James Smartda0436e2009-05-22 14:51:39 -04005040 rc = lpfc_sli4_init_rpi_hdrs(phba);
5041 if (rc) {
5042 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5043 "1432 Failed to initialize rpi headers.\n");
5044 goto out_free_active_sgl;
5045 }
5046
James Smarta93ff372010-10-22 11:06:08 -04005047 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
James Smart0c9ab6f2010-02-26 14:15:57 -05005048 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
5049 phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
5050 GFP_KERNEL);
5051 if (!phba->fcf.fcf_rr_bmask) {
5052 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5053 "2759 Failed allocate memory for FCF round "
5054 "robin failover bmask\n");
James Smart05580562011-05-24 11:40:48 -04005055 rc = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -05005056 goto out_remove_rpi_hdrs;
5057 }
5058
James Smart67d12732012-08-03 12:36:13 -04005059 phba->sli4_hba.fcp_eq_hdl =
5060 kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
5061 phba->cfg_fcp_io_channel), GFP_KERNEL);
5062 if (!phba->sli4_hba.fcp_eq_hdl) {
5063 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5064 "2572 Failed allocate memory for "
5065 "fast-path per-EQ handle array\n");
5066 rc = -ENOMEM;
5067 goto out_free_fcf_rr_bmask;
James Smartda0436e2009-05-22 14:51:39 -04005068 }
5069
5070 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
James Smart82c3e9b2012-09-29 11:29:50 -04005071 phba->cfg_fcp_io_channel), GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005072 if (!phba->sli4_hba.msix_entries) {
5073 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5074 "2573 Failed allocate memory for msi-x "
5075 "interrupt vector entries\n");
James Smart05580562011-05-24 11:40:48 -04005076 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04005077 goto out_free_fcp_eq_hdl;
5078 }
5079
James Smart912e3ac2011-05-24 11:42:11 -04005080 /*
5081 * Enable sr-iov virtual functions if supported and configured
5082 * through the module parameter.
5083 */
5084 if (phba->cfg_sriov_nr_virtfn > 0) {
5085 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
5086 phba->cfg_sriov_nr_virtfn);
5087 if (rc) {
5088 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5089 "3020 Requested number of SR-IOV "
5090 "virtual functions (%d) is not "
5091 "supported\n",
5092 phba->cfg_sriov_nr_virtfn);
5093 phba->cfg_sriov_nr_virtfn = 0;
5094 }
5095 }
5096
James Smart5248a742011-07-22 18:37:06 -04005097 return 0;
James Smartda0436e2009-05-22 14:51:39 -04005098
5099out_free_fcp_eq_hdl:
5100 kfree(phba->sli4_hba.fcp_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05005101out_free_fcf_rr_bmask:
5102 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04005103out_remove_rpi_hdrs:
5104 lpfc_sli4_remove_rpi_hdrs(phba);
5105out_free_active_sgl:
5106 lpfc_free_active_sgl(phba);
James Smartda0436e2009-05-22 14:51:39 -04005107out_destroy_cq_event_pool:
5108 lpfc_sli4_cq_event_pool_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04005109out_free_bsmbx:
5110 lpfc_destroy_bootstrap_mbox(phba);
5111out_free_mem:
5112 lpfc_mem_free(phba);
5113 return rc;
5114}
5115
5116/**
5117 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
5118 * @phba: pointer to lpfc hba data structure.
5119 *
5120 * This routine is invoked to unset the driver internal resources set up
5121 * specific for supporting the SLI-4 HBA device it attached to.
5122 **/
5123static void
5124lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
5125{
5126 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
5127
James Smartda0436e2009-05-22 14:51:39 -04005128 /* Free memory allocated for msi-x interrupt vector entries */
5129 kfree(phba->sli4_hba.msix_entries);
5130
5131 /* Free memory allocated for fast-path work queue handles */
5132 kfree(phba->sli4_hba.fcp_eq_hdl);
5133
5134 /* Free the allocated rpi headers. */
5135 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04005136 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04005137
James Smart0c9ab6f2010-02-26 14:15:57 -05005138 /* Free eligible FCF index bmask */
5139 kfree(phba->fcf.fcf_rr_bmask);
5140
James Smartda0436e2009-05-22 14:51:39 -04005141 /* Free the ELS sgl list */
5142 lpfc_free_active_sgl(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04005143 lpfc_free_els_sgl_list(phba);
James Smartda0436e2009-05-22 14:51:39 -04005144
James Smartda0436e2009-05-22 14:51:39 -04005145 /* Free the completion queue EQ event pool */
5146 lpfc_sli4_cq_event_release_all(phba);
5147 lpfc_sli4_cq_event_pool_destroy(phba);
5148
James Smart6d368e52011-05-24 11:44:12 -04005149 /* Release resource identifiers. */
5150 lpfc_sli4_dealloc_resource_identifiers(phba);
5151
James Smartda0436e2009-05-22 14:51:39 -04005152 /* Free the bsmbx region. */
5153 lpfc_destroy_bootstrap_mbox(phba);
5154
5155 /* Free the SLI Layer memory with SLI4 HBAs */
5156 lpfc_mem_free_all(phba);
5157
5158 /* Free the current connect table */
5159 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04005160 &phba->fcf_conn_rec_list, list) {
5161 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04005162 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04005163 }
James Smartda0436e2009-05-22 14:51:39 -04005164
5165 return;
5166}
5167
5168/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005169 * lpfc_init_api_table_setup - Set up init api function jump table
James Smart3772a992009-05-22 14:50:54 -04005170 * @phba: The hba struct for which this call is being executed.
5171 * @dev_grp: The HBA PCI-Device group number.
5172 *
5173 * This routine sets up the device INIT interface API function jump table
5174 * in @phba struct.
5175 *
5176 * Returns: 0 - success, -ENODEV - failure.
5177 **/
5178int
5179lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5180{
James Smart84d1b002010-02-12 14:42:33 -05005181 phba->lpfc_hba_init_link = lpfc_hba_init_link;
5182 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart7f860592011-03-11 16:05:52 -05005183 phba->lpfc_selective_reset = lpfc_selective_reset;
James Smart3772a992009-05-22 14:50:54 -04005184 switch (dev_grp) {
5185 case LPFC_PCI_DEV_LP:
5186 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
5187 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
5188 phba->lpfc_stop_port = lpfc_stop_port_s3;
5189 break;
James Smartda0436e2009-05-22 14:51:39 -04005190 case LPFC_PCI_DEV_OC:
5191 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
5192 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
5193 phba->lpfc_stop_port = lpfc_stop_port_s4;
5194 break;
James Smart3772a992009-05-22 14:50:54 -04005195 default:
5196 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5197 "1431 Invalid HBA PCI-device group: 0x%x\n",
5198 dev_grp);
5199 return -ENODEV;
5200 break;
5201 }
5202 return 0;
5203}
5204
5205/**
5206 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
5207 * @phba: pointer to lpfc hba data structure.
5208 *
5209 * This routine is invoked to set up the driver internal resources before the
5210 * device specific resource setup to support the HBA device it attached to.
5211 *
5212 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005213 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005214 * other values - error
5215 **/
5216static int
5217lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
5218{
5219 /*
5220 * Driver resources common to all SLI revisions
5221 */
James Smartea2151b2008-09-07 11:52:10 -04005222 atomic_set(&phba->fast_event_count, 0);
James Smart2e0fef82007-06-17 19:56:36 -05005223 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005224
James Smarte47c9092008-02-08 18:49:26 -05005225 /* Initialize ndlp management spinlock */
5226 spin_lock_init(&phba->ndlp_lock);
5227
James Smart2e0fef82007-06-17 19:56:36 -05005228 INIT_LIST_HEAD(&phba->port_list);
James Smart3772a992009-05-22 14:50:54 -04005229 INIT_LIST_HEAD(&phba->work_list);
James Smart84774a42008-08-24 21:50:06 -04005230 init_waitqueue_head(&phba->wait_4_mlo_m_q);
dea31012005-04-17 16:05:31 -05005231
James Smart3772a992009-05-22 14:50:54 -04005232 /* Initialize the wait queue head for the kernel thread */
5233 init_waitqueue_head(&phba->work_waitq);
James Smart858c9f62007-06-17 19:56:39 -05005234
James Smart3772a992009-05-22 14:50:54 -04005235 /* Initialize the scsi buffer list used by driver for scsi IO */
5236 spin_lock_init(&phba->scsi_buf_list_lock);
5237 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
dea31012005-04-17 16:05:31 -05005238
James Smart3772a992009-05-22 14:50:54 -04005239 /* Initialize the fabric iocb list */
5240 INIT_LIST_HEAD(&phba->fabric_iocb_list);
dea31012005-04-17 16:05:31 -05005241
James Smart3772a992009-05-22 14:50:54 -04005242 /* Initialize list to save ELS buffers */
5243 INIT_LIST_HEAD(&phba->elsbuf);
dea31012005-04-17 16:05:31 -05005244
James Smart3772a992009-05-22 14:50:54 -04005245 /* Initialize FCF connection rec list */
5246 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
dea31012005-04-17 16:05:31 -05005247
James Smart3772a992009-05-22 14:50:54 -04005248 return 0;
5249}
dea31012005-04-17 16:05:31 -05005250
James Smart3772a992009-05-22 14:50:54 -04005251/**
5252 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
5253 * @phba: pointer to lpfc hba data structure.
5254 *
5255 * This routine is invoked to set up the driver internal resources after the
5256 * device specific resource setup to support the HBA device it attached to.
5257 *
5258 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005259 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005260 * other values - error
5261 **/
5262static int
5263lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
5264{
5265 int error;
5266
5267 /* Startup the kernel thread for this host adapter. */
5268 phba->worker_thread = kthread_run(lpfc_do_work, phba,
5269 "lpfc_worker_%d", phba->brd_no);
5270 if (IS_ERR(phba->worker_thread)) {
5271 error = PTR_ERR(phba->worker_thread);
5272 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005273 }
5274
James Smart3772a992009-05-22 14:50:54 -04005275 return 0;
5276}
5277
5278/**
5279 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
5280 * @phba: pointer to lpfc hba data structure.
5281 *
5282 * This routine is invoked to unset the driver internal resources set up after
5283 * the device specific resource setup for supporting the HBA device it
5284 * attached to.
5285 **/
5286static void
5287lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
5288{
5289 /* Stop kernel worker thread */
5290 kthread_stop(phba->worker_thread);
5291}
5292
5293/**
5294 * lpfc_free_iocb_list - Free iocb list.
5295 * @phba: pointer to lpfc hba data structure.
5296 *
5297 * This routine is invoked to free the driver's IOCB list and memory.
5298 **/
5299static void
5300lpfc_free_iocb_list(struct lpfc_hba *phba)
5301{
5302 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
5303
5304 spin_lock_irq(&phba->hbalock);
5305 list_for_each_entry_safe(iocbq_entry, iocbq_next,
5306 &phba->lpfc_iocb_list, list) {
5307 list_del(&iocbq_entry->list);
5308 kfree(iocbq_entry);
5309 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005310 }
James Smart3772a992009-05-22 14:50:54 -04005311 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005312
James Smart3772a992009-05-22 14:50:54 -04005313 return;
5314}
dea31012005-04-17 16:05:31 -05005315
James Smart3772a992009-05-22 14:50:54 -04005316/**
5317 * lpfc_init_iocb_list - Allocate and initialize iocb list.
5318 * @phba: pointer to lpfc hba data structure.
5319 *
5320 * This routine is invoked to allocate and initizlize the driver's IOCB
5321 * list and set up the IOCB tag array accordingly.
5322 *
5323 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005324 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005325 * other values - error
5326 **/
5327static int
5328lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
5329{
5330 struct lpfc_iocbq *iocbq_entry = NULL;
5331 uint16_t iotag;
5332 int i;
dea31012005-04-17 16:05:31 -05005333
5334 /* Initialize and populate the iocb list per host. */
5335 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04005336 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07005337 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05005338 if (iocbq_entry == NULL) {
5339 printk(KERN_ERR "%s: only allocated %d iocbs of "
5340 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07005341 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05005342 goto out_free_iocbq;
5343 }
5344
James Bottomley604a3e32005-10-29 10:28:33 -05005345 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
5346 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04005347 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05005348 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04005349 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05005350 goto out_free_iocbq;
5351 }
James Smart6d368e52011-05-24 11:44:12 -04005352 iocbq_entry->sli4_lxritag = NO_XRI;
James Smart3772a992009-05-22 14:50:54 -04005353 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05005354
5355 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005356 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
5357 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05005358 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005359 }
5360
James Smart3772a992009-05-22 14:50:54 -04005361 return 0;
5362
5363out_free_iocbq:
5364 lpfc_free_iocb_list(phba);
5365
5366 return -ENOMEM;
5367}
5368
5369/**
James Smart8a9d2e82012-05-09 21:16:12 -04005370 * lpfc_free_sgl_list - Free a given sgl list.
James Smartda0436e2009-05-22 14:51:39 -04005371 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -04005372 * @sglq_list: pointer to the head of sgl list.
James Smartda0436e2009-05-22 14:51:39 -04005373 *
James Smart8a9d2e82012-05-09 21:16:12 -04005374 * This routine is invoked to free a give sgl list and memory.
James Smartda0436e2009-05-22 14:51:39 -04005375 **/
James Smart8a9d2e82012-05-09 21:16:12 -04005376void
5377lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
James Smartda0436e2009-05-22 14:51:39 -04005378{
5379 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart8a9d2e82012-05-09 21:16:12 -04005380
5381 list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
5382 list_del(&sglq_entry->list);
5383 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
5384 kfree(sglq_entry);
5385 }
5386}
5387
5388/**
5389 * lpfc_free_els_sgl_list - Free els sgl list.
5390 * @phba: pointer to lpfc hba data structure.
5391 *
5392 * This routine is invoked to free the driver's els sgl list and memory.
5393 **/
5394static void
5395lpfc_free_els_sgl_list(struct lpfc_hba *phba)
5396{
James Smartda0436e2009-05-22 14:51:39 -04005397 LIST_HEAD(sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04005398
James Smart8a9d2e82012-05-09 21:16:12 -04005399 /* Retrieve all els sgls from driver list */
James Smartda0436e2009-05-22 14:51:39 -04005400 spin_lock_irq(&phba->hbalock);
5401 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
5402 spin_unlock_irq(&phba->hbalock);
5403
James Smart8a9d2e82012-05-09 21:16:12 -04005404 /* Now free the sgl list */
5405 lpfc_free_sgl_list(phba, &sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04005406}
5407
5408/**
5409 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
5410 * @phba: pointer to lpfc hba data structure.
5411 *
5412 * This routine is invoked to allocate the driver's active sgl memory.
5413 * This array will hold the sglq_entry's for active IOs.
5414 **/
5415static int
5416lpfc_init_active_sgl_array(struct lpfc_hba *phba)
5417{
5418 int size;
5419 size = sizeof(struct lpfc_sglq *);
5420 size *= phba->sli4_hba.max_cfg_param.max_xri;
5421
5422 phba->sli4_hba.lpfc_sglq_active_list =
5423 kzalloc(size, GFP_KERNEL);
5424 if (!phba->sli4_hba.lpfc_sglq_active_list)
5425 return -ENOMEM;
5426 return 0;
5427}
5428
5429/**
5430 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
5431 * @phba: pointer to lpfc hba data structure.
5432 *
5433 * This routine is invoked to walk through the array of active sglq entries
5434 * and free all of the resources.
5435 * This is just a place holder for now.
5436 **/
5437static void
5438lpfc_free_active_sgl(struct lpfc_hba *phba)
5439{
5440 kfree(phba->sli4_hba.lpfc_sglq_active_list);
5441}
5442
5443/**
5444 * lpfc_init_sgl_list - Allocate and initialize sgl list.
5445 * @phba: pointer to lpfc hba data structure.
5446 *
5447 * This routine is invoked to allocate and initizlize the driver's sgl
5448 * list and set up the sgl xritag tag array accordingly.
5449 *
James Smartda0436e2009-05-22 14:51:39 -04005450 **/
James Smart8a9d2e82012-05-09 21:16:12 -04005451static void
James Smartda0436e2009-05-22 14:51:39 -04005452lpfc_init_sgl_list(struct lpfc_hba *phba)
5453{
James Smartda0436e2009-05-22 14:51:39 -04005454 /* Initialize and populate the sglq list per host/VF. */
5455 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
5456 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
5457
James Smart8a9d2e82012-05-09 21:16:12 -04005458 /* els xri-sgl book keeping */
5459 phba->sli4_hba.els_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04005460
James Smart8a9d2e82012-05-09 21:16:12 -04005461 /* scsi xri-buffer book keeping */
James Smartda0436e2009-05-22 14:51:39 -04005462 phba->sli4_hba.scsi_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04005463}
5464
5465/**
5466 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
5467 * @phba: pointer to lpfc hba data structure.
5468 *
5469 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -04005470 * port for those SLI4 ports that do not support extents. This routine
James Smartda0436e2009-05-22 14:51:39 -04005471 * posts a PAGE_SIZE memory region to the port to hold up to
James Smart88a2cfb2011-07-22 18:36:33 -04005472 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
5473 * and should be called only when interrupts are disabled.
James Smartda0436e2009-05-22 14:51:39 -04005474 *
5475 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005476 * 0 - successful
James Smart88a2cfb2011-07-22 18:36:33 -04005477 * -ERROR - otherwise.
James Smartda0436e2009-05-22 14:51:39 -04005478 **/
5479int
5480lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
5481{
5482 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04005483 struct lpfc_rpi_hdr *rpi_hdr;
5484
5485 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
James Smartff78d8f2011-12-13 13:21:35 -05005486 if (!phba->sli4_hba.rpi_hdrs_in_use)
James Smart6d368e52011-05-24 11:44:12 -04005487 return rc;
James Smart6d368e52011-05-24 11:44:12 -04005488 if (phba->sli4_hba.extents_in_use)
5489 return -EIO;
James Smartda0436e2009-05-22 14:51:39 -04005490
5491 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
5492 if (!rpi_hdr) {
5493 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5494 "0391 Error during rpi post operation\n");
5495 lpfc_sli4_remove_rpis(phba);
5496 rc = -ENODEV;
5497 }
5498
5499 return rc;
5500}
5501
5502/**
5503 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
5504 * @phba: pointer to lpfc hba data structure.
5505 *
5506 * This routine is invoked to allocate a single 4KB memory region to
5507 * support rpis and stores them in the phba. This single region
5508 * provides support for up to 64 rpis. The region is used globally
5509 * by the device.
5510 *
5511 * Returns:
5512 * A valid rpi hdr on success.
5513 * A NULL pointer on any failure.
5514 **/
5515struct lpfc_rpi_hdr *
5516lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
5517{
5518 uint16_t rpi_limit, curr_rpi_range;
5519 struct lpfc_dmabuf *dmabuf;
5520 struct lpfc_rpi_hdr *rpi_hdr;
James Smart9589b0622011-04-16 11:03:17 -04005521 uint32_t rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04005522
James Smart6d368e52011-05-24 11:44:12 -04005523 /*
5524 * If the SLI4 port supports extents, posting the rpi header isn't
5525 * required. Set the expected maximum count and let the actual value
5526 * get set when extents are fully allocated.
5527 */
5528 if (!phba->sli4_hba.rpi_hdrs_in_use)
5529 return NULL;
5530 if (phba->sli4_hba.extents_in_use)
5531 return NULL;
5532
5533 /* The limit on the logical index is just the max_rpi count. */
James Smartda0436e2009-05-22 14:51:39 -04005534 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
James Smart6d368e52011-05-24 11:44:12 -04005535 phba->sli4_hba.max_cfg_param.max_rpi - 1;
James Smartda0436e2009-05-22 14:51:39 -04005536
5537 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04005538 /*
5539 * Establish the starting RPI in this header block. The starting
5540 * rpi is normalized to a zero base because the physical rpi is
5541 * port based.
5542 */
James Smart97f2ecf2012-03-01 22:35:23 -05005543 curr_rpi_range = phba->sli4_hba.next_rpi;
James Smartda0436e2009-05-22 14:51:39 -04005544 spin_unlock_irq(&phba->hbalock);
5545
5546 /*
5547 * The port has a limited number of rpis. The increment here
5548 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
5549 * and to allow the full max_rpi range per port.
5550 */
5551 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
James Smart9589b0622011-04-16 11:03:17 -04005552 rpi_count = rpi_limit - curr_rpi_range;
5553 else
5554 rpi_count = LPFC_RPI_HDR_COUNT;
James Smartda0436e2009-05-22 14:51:39 -04005555
James Smart6d368e52011-05-24 11:44:12 -04005556 if (!rpi_count)
5557 return NULL;
James Smartda0436e2009-05-22 14:51:39 -04005558 /*
5559 * First allocate the protocol header region for the port. The
5560 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
5561 */
5562 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5563 if (!dmabuf)
5564 return NULL;
5565
5566 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5567 LPFC_HDR_TEMPLATE_SIZE,
5568 &dmabuf->phys,
5569 GFP_KERNEL);
5570 if (!dmabuf->virt) {
5571 rpi_hdr = NULL;
5572 goto err_free_dmabuf;
5573 }
5574
5575 memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
5576 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
5577 rpi_hdr = NULL;
5578 goto err_free_coherent;
5579 }
5580
5581 /* Save the rpi header data for cleanup later. */
5582 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
5583 if (!rpi_hdr)
5584 goto err_free_coherent;
5585
5586 rpi_hdr->dmabuf = dmabuf;
5587 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
5588 rpi_hdr->page_count = 1;
5589 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04005590
5591 /* The rpi_hdr stores the logical index only. */
5592 rpi_hdr->start_rpi = curr_rpi_range;
James Smartda0436e2009-05-22 14:51:39 -04005593 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
5594
5595 /*
James Smart6d368e52011-05-24 11:44:12 -04005596 * The next_rpi stores the next logical module-64 rpi value used
5597 * to post physical rpis in subsequent rpi postings.
James Smartda0436e2009-05-22 14:51:39 -04005598 */
James Smart9589b0622011-04-16 11:03:17 -04005599 phba->sli4_hba.next_rpi += rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04005600 spin_unlock_irq(&phba->hbalock);
5601 return rpi_hdr;
5602
5603 err_free_coherent:
5604 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
5605 dmabuf->virt, dmabuf->phys);
5606 err_free_dmabuf:
5607 kfree(dmabuf);
5608 return NULL;
5609}
5610
5611/**
5612 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
5613 * @phba: pointer to lpfc hba data structure.
5614 *
5615 * This routine is invoked to remove all memory resources allocated
James Smart6d368e52011-05-24 11:44:12 -04005616 * to support rpis for SLI4 ports not supporting extents. This routine
5617 * presumes the caller has released all rpis consumed by fabric or port
5618 * logins and is prepared to have the header pages removed.
James Smartda0436e2009-05-22 14:51:39 -04005619 **/
5620void
5621lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
5622{
5623 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
5624
James Smart6d368e52011-05-24 11:44:12 -04005625 if (!phba->sli4_hba.rpi_hdrs_in_use)
5626 goto exit;
5627
James Smartda0436e2009-05-22 14:51:39 -04005628 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
5629 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
5630 list_del(&rpi_hdr->list);
5631 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
5632 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
5633 kfree(rpi_hdr->dmabuf);
5634 kfree(rpi_hdr);
5635 }
James Smart6d368e52011-05-24 11:44:12 -04005636 exit:
5637 /* There are no rpis available to the port now. */
5638 phba->sli4_hba.next_rpi = 0;
James Smartda0436e2009-05-22 14:51:39 -04005639}
5640
5641/**
James Smart3772a992009-05-22 14:50:54 -04005642 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
5643 * @pdev: pointer to pci device data structure.
5644 *
5645 * This routine is invoked to allocate the driver hba data structure for an
5646 * HBA device. If the allocation is successful, the phba reference to the
5647 * PCI device data structure is set.
5648 *
5649 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005650 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04005651 * NULL - error
5652 **/
5653static struct lpfc_hba *
5654lpfc_hba_alloc(struct pci_dev *pdev)
5655{
5656 struct lpfc_hba *phba;
5657
5658 /* Allocate memory for HBA structure */
5659 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
5660 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02005661 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04005662 return NULL;
5663 }
5664
5665 /* Set reference to PCI device in HBA structure */
5666 phba->pcidev = pdev;
5667
5668 /* Assign an unused board number */
5669 phba->brd_no = lpfc_get_instance();
5670 if (phba->brd_no < 0) {
5671 kfree(phba);
5672 return NULL;
5673 }
5674
James Smart4fede782010-01-26 23:08:55 -05005675 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04005676 INIT_LIST_HEAD(&phba->ct_ev_waiters);
5677
James Smart3772a992009-05-22 14:50:54 -04005678 return phba;
5679}
5680
5681/**
5682 * lpfc_hba_free - Free driver hba data structure with a device.
5683 * @phba: pointer to lpfc hba data structure.
5684 *
5685 * This routine is invoked to free the driver hba data structure with an
5686 * HBA device.
5687 **/
5688static void
5689lpfc_hba_free(struct lpfc_hba *phba)
5690{
5691 /* Release the driver assigned board number */
5692 idr_remove(&lpfc_hba_index, phba->brd_no);
5693
James Smart2a76a282012-08-03 12:35:54 -04005694 /* Free memory allocated with sli rings */
5695 kfree(phba->sli.ring);
5696 phba->sli.ring = NULL;
5697
James Smart3772a992009-05-22 14:50:54 -04005698 kfree(phba);
5699 return;
5700}
5701
5702/**
5703 * lpfc_create_shost - Create hba physical port with associated scsi host.
5704 * @phba: pointer to lpfc hba data structure.
5705 *
5706 * This routine is invoked to create HBA physical port and associate a SCSI
5707 * host with it.
5708 *
5709 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005710 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005711 * other values - error
5712 **/
5713static int
5714lpfc_create_shost(struct lpfc_hba *phba)
5715{
5716 struct lpfc_vport *vport;
5717 struct Scsi_Host *shost;
5718
5719 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05005720 phba->fc_edtov = FF_DEF_EDTOV;
5721 phba->fc_ratov = FF_DEF_RATOV;
5722 phba->fc_altov = FF_DEF_ALTOV;
5723 phba->fc_arbtov = FF_DEF_ARBTOV;
5724
James Smartd7c47992010-06-08 18:31:54 -04005725 atomic_set(&phba->sdev_cnt, 0);
James Smart3de2a652007-08-02 11:09:59 -04005726 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05005727 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04005728 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05005729
5730 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05005731 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05005732 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04005733 /* Put reference to SCSI host to driver's device private data */
5734 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05005735
James Smart3772a992009-05-22 14:50:54 -04005736 return 0;
5737}
dea31012005-04-17 16:05:31 -05005738
James Smart3772a992009-05-22 14:50:54 -04005739/**
5740 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
5741 * @phba: pointer to lpfc hba data structure.
5742 *
5743 * This routine is invoked to destroy HBA physical port and the associated
5744 * SCSI host.
5745 **/
5746static void
5747lpfc_destroy_shost(struct lpfc_hba *phba)
5748{
5749 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04005750
James Smart3772a992009-05-22 14:50:54 -04005751 /* Destroy physical port that associated with the SCSI host */
5752 destroy_port(vport);
5753
5754 return;
5755}
5756
5757/**
5758 * lpfc_setup_bg - Setup Block guard structures and debug areas.
5759 * @phba: pointer to lpfc hba data structure.
5760 * @shost: the shost to be used to detect Block guard settings.
5761 *
5762 * This routine sets up the local Block guard protocol settings for @shost.
5763 * This routine also allocates memory for debugging bg buffers.
5764 **/
5765static void
5766lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
5767{
James Smartbbeb79b2012-06-12 13:54:27 -04005768 uint32_t old_mask;
5769 uint32_t old_guard;
5770
James Smart3772a992009-05-22 14:50:54 -04005771 int pagecnt = 10;
5772 if (lpfc_prot_mask && lpfc_prot_guard) {
5773 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5774 "1478 Registering BlockGuard with the "
5775 "SCSI layer\n");
James Smartbbeb79b2012-06-12 13:54:27 -04005776
5777 old_mask = lpfc_prot_mask;
5778 old_guard = lpfc_prot_guard;
5779
5780 /* Only allow supported values */
5781 lpfc_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
5782 SHOST_DIX_TYPE0_PROTECTION |
5783 SHOST_DIX_TYPE1_PROTECTION);
5784 lpfc_prot_guard &= (SHOST_DIX_GUARD_IP | SHOST_DIX_GUARD_CRC);
5785
5786 /* DIF Type 1 protection for profiles AST1/C1 is end to end */
5787 if (lpfc_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
5788 lpfc_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
5789
5790 if (lpfc_prot_mask && lpfc_prot_guard) {
5791 if ((old_mask != lpfc_prot_mask) ||
5792 (old_guard != lpfc_prot_guard))
5793 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5794 "1475 Registering BlockGuard with the "
5795 "SCSI layer: mask %d guard %d\n",
5796 lpfc_prot_mask, lpfc_prot_guard);
5797
5798 scsi_host_set_prot(shost, lpfc_prot_mask);
5799 scsi_host_set_guard(shost, lpfc_prot_guard);
5800 } else
5801 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5802 "1479 Not Registering BlockGuard with the SCSI "
5803 "layer, Bad protection parameters: %d %d\n",
5804 old_mask, old_guard);
James Smart98c9ea52007-10-27 13:37:33 -04005805 }
James Smartbbeb79b2012-06-12 13:54:27 -04005806
James Smart81301a92008-12-04 22:39:46 -05005807 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05005808 while (pagecnt) {
5809 spin_lock_init(&_dump_buf_lock);
5810 _dump_buf_data =
5811 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
5812 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04005813 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5814 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04005815 "_dump_buf_data at 0x%p\n",
5816 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05005817 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04005818 memset(_dump_buf_data, 0,
5819 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05005820 break;
James Smart3772a992009-05-22 14:50:54 -04005821 } else
James Smart81301a92008-12-04 22:39:46 -05005822 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05005823 }
James Smart81301a92008-12-04 22:39:46 -05005824 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04005825 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5826 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04005827 "memory for hexdump\n");
5828 } else
James Smart6a9c52c2009-10-02 15:16:51 -04005829 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5830 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05005831 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05005832 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05005833 while (pagecnt) {
5834 _dump_buf_dif =
5835 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
5836 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04005837 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5838 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04005839 "_dump_buf_dif at 0x%p\n",
5840 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05005841 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04005842 memset(_dump_buf_dif, 0,
5843 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05005844 break;
James Smart3772a992009-05-22 14:50:54 -04005845 } else
James Smart81301a92008-12-04 22:39:46 -05005846 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05005847 }
James Smart81301a92008-12-04 22:39:46 -05005848 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04005849 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5850 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04005851 "memory for hexdump\n");
5852 } else
James Smart6a9c52c2009-10-02 15:16:51 -04005853 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5854 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04005855 _dump_buf_dif);
5856}
5857
5858/**
5859 * lpfc_post_init_setup - Perform necessary device post initialization setup.
5860 * @phba: pointer to lpfc hba data structure.
5861 *
5862 * This routine is invoked to perform all the necessary post initialization
5863 * setup for the device.
5864 **/
5865static void
5866lpfc_post_init_setup(struct lpfc_hba *phba)
5867{
5868 struct Scsi_Host *shost;
5869 struct lpfc_adapter_event_header adapter_event;
5870
5871 /* Get the default values for Model Name and Description */
5872 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
5873
5874 /*
5875 * hba setup may have changed the hba_queue_depth so we need to
5876 * adjust the value of can_queue.
5877 */
5878 shost = pci_get_drvdata(phba->pcidev);
5879 shost->can_queue = phba->cfg_hba_queue_depth - 10;
5880 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
5881 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05005882
5883 lpfc_host_attrib_init(shost);
5884
James Smart2e0fef82007-06-17 19:56:36 -05005885 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
5886 spin_lock_irq(shost->host_lock);
5887 lpfc_poll_start_timer(phba);
5888 spin_unlock_irq(shost->host_lock);
5889 }
James Smart8f6d98d2006-08-01 07:34:00 -04005890
James Smart93996272008-08-24 21:50:30 -04005891 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5892 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04005893 /* Send board arrival event to upper layer */
5894 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
5895 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
5896 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04005897 sizeof(adapter_event),
5898 (char *) &adapter_event,
5899 LPFC_NL_VENDOR_ID);
5900 return;
5901}
5902
5903/**
5904 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
5905 * @phba: pointer to lpfc hba data structure.
5906 *
5907 * This routine is invoked to set up the PCI device memory space for device
5908 * with SLI-3 interface spec.
5909 *
5910 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005911 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005912 * other values - error
5913 **/
5914static int
5915lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
5916{
5917 struct pci_dev *pdev;
5918 unsigned long bar0map_len, bar2map_len;
5919 int i, hbq_count;
5920 void *ptr;
5921 int error = -ENODEV;
5922
5923 /* Obtain PCI device reference */
5924 if (!phba->pcidev)
5925 return error;
5926 else
5927 pdev = phba->pcidev;
5928
5929 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05005930 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
5931 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
5932 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
5933 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smart3772a992009-05-22 14:50:54 -04005934 return error;
Michael Reed8e685972009-09-18 12:02:05 -05005935 }
5936 }
James Smart3772a992009-05-22 14:50:54 -04005937
5938 /* Get the bus address of Bar0 and Bar2 and the number of bytes
5939 * required by each mapping.
5940 */
5941 phba->pci_bar0_map = pci_resource_start(pdev, 0);
5942 bar0map_len = pci_resource_len(pdev, 0);
5943
5944 phba->pci_bar2_map = pci_resource_start(pdev, 2);
5945 bar2map_len = pci_resource_len(pdev, 2);
5946
5947 /* Map HBA SLIM to a kernel virtual address. */
5948 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
5949 if (!phba->slim_memmap_p) {
5950 dev_printk(KERN_ERR, &pdev->dev,
5951 "ioremap failed for SLIM memory.\n");
5952 goto out;
5953 }
5954
5955 /* Map HBA Control Registers to a kernel virtual address. */
5956 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
5957 if (!phba->ctrl_regs_memmap_p) {
5958 dev_printk(KERN_ERR, &pdev->dev,
5959 "ioremap failed for HBA control registers.\n");
5960 goto out_iounmap_slim;
5961 }
5962
5963 /* Allocate memory for SLI-2 structures */
5964 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
5965 SLI2_SLIM_SIZE,
5966 &phba->slim2p.phys,
5967 GFP_KERNEL);
5968 if (!phba->slim2p.virt)
5969 goto out_iounmap;
5970
5971 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
5972 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04005973 phba->mbox_ext = (phba->slim2p.virt +
5974 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04005975 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
5976 phba->IOCBs = (phba->slim2p.virt +
5977 offsetof(struct lpfc_sli2_slim, IOCBs));
5978
5979 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
5980 lpfc_sli_hbq_size(),
5981 &phba->hbqslimp.phys,
5982 GFP_KERNEL);
5983 if (!phba->hbqslimp.virt)
5984 goto out_free_slim;
5985
5986 hbq_count = lpfc_sli_hbq_count();
5987 ptr = phba->hbqslimp.virt;
5988 for (i = 0; i < hbq_count; ++i) {
5989 phba->hbqs[i].hbq_virt = ptr;
5990 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
5991 ptr += (lpfc_hbq_defs[i]->entry_count *
5992 sizeof(struct lpfc_hbq_entry));
5993 }
5994 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
5995 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
5996
5997 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
5998
5999 INIT_LIST_HEAD(&phba->rb_pend_list);
6000
6001 phba->MBslimaddr = phba->slim_memmap_p;
6002 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
6003 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
6004 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
6005 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04006006
dea31012005-04-17 16:05:31 -05006007 return 0;
6008
dea31012005-04-17 16:05:31 -05006009out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04006010 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6011 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05006012out_iounmap:
6013 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05006014out_iounmap_slim:
dea31012005-04-17 16:05:31 -05006015 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05006016out:
6017 return error;
6018}
6019
James Smarte59058c2008-08-24 21:49:00 -04006020/**
James Smart3772a992009-05-22 14:50:54 -04006021 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
6022 * @phba: pointer to lpfc hba data structure.
6023 *
6024 * This routine is invoked to unset the PCI device memory space for device
6025 * with SLI-3 interface spec.
6026 **/
6027static void
6028lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
6029{
6030 struct pci_dev *pdev;
6031
6032 /* Obtain PCI device reference */
6033 if (!phba->pcidev)
6034 return;
6035 else
6036 pdev = phba->pcidev;
6037
6038 /* Free coherent DMA memory allocated */
6039 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
6040 phba->hbqslimp.virt, phba->hbqslimp.phys);
6041 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6042 phba->slim2p.virt, phba->slim2p.phys);
6043
6044 /* I/O memory unmap */
6045 iounmap(phba->ctrl_regs_memmap_p);
6046 iounmap(phba->slim_memmap_p);
6047
6048 return;
6049}
6050
6051/**
James Smartda0436e2009-05-22 14:51:39 -04006052 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
6053 * @phba: pointer to lpfc hba data structure.
6054 *
6055 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
6056 * done and check status.
6057 *
6058 * Return 0 if successful, otherwise -ENODEV.
6059 **/
6060int
6061lpfc_sli4_post_status_check(struct lpfc_hba *phba)
6062{
James Smart2fcee4b2010-12-15 17:57:46 -05006063 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
6064 struct lpfc_register reg_data;
6065 int i, port_error = 0;
6066 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04006067
James Smart9940b972011-03-11 16:06:12 -05006068 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
6069 memset(&reg_data, 0, sizeof(reg_data));
James Smart2fcee4b2010-12-15 17:57:46 -05006070 if (!phba->sli4_hba.PSMPHRregaddr)
James Smartda0436e2009-05-22 14:51:39 -04006071 return -ENODEV;
6072
James Smartda0436e2009-05-22 14:51:39 -04006073 /* Wait up to 30 seconds for the SLI Port POST done and ready */
6074 for (i = 0; i < 3000; i++) {
James Smart9940b972011-03-11 16:06:12 -05006075 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
6076 &portsmphr_reg.word0) ||
6077 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
James Smart2fcee4b2010-12-15 17:57:46 -05006078 /* Port has a fatal POST error, break out */
James Smartda0436e2009-05-22 14:51:39 -04006079 port_error = -ENODEV;
6080 break;
6081 }
James Smart2fcee4b2010-12-15 17:57:46 -05006082 if (LPFC_POST_STAGE_PORT_READY ==
6083 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
James Smartda0436e2009-05-22 14:51:39 -04006084 break;
James Smartda0436e2009-05-22 14:51:39 -04006085 msleep(10);
6086 }
6087
James Smart2fcee4b2010-12-15 17:57:46 -05006088 /*
6089 * If there was a port error during POST, then don't proceed with
6090 * other register reads as the data may not be valid. Just exit.
6091 */
6092 if (port_error) {
James Smartda0436e2009-05-22 14:51:39 -04006093 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05006094 "1408 Port Failed POST - portsmphr=0x%x, "
6095 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
6096 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
6097 portsmphr_reg.word0,
6098 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
6099 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
6100 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
6101 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
6102 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
6103 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
6104 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
6105 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
6106 } else {
James Smart28baac72010-02-12 14:42:03 -05006107 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05006108 "2534 Device Info: SLIFamily=0x%x, "
6109 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
6110 "SLIHint_2=0x%x, FT=0x%x\n",
James Smart28baac72010-02-12 14:42:03 -05006111 bf_get(lpfc_sli_intf_sli_family,
6112 &phba->sli4_hba.sli_intf),
6113 bf_get(lpfc_sli_intf_slirev,
6114 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05006115 bf_get(lpfc_sli_intf_if_type,
James Smart28baac72010-02-12 14:42:03 -05006116 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05006117 bf_get(lpfc_sli_intf_sli_hint1,
6118 &phba->sli4_hba.sli_intf),
6119 bf_get(lpfc_sli_intf_sli_hint2,
6120 &phba->sli4_hba.sli_intf),
6121 bf_get(lpfc_sli_intf_func_type,
James Smart28baac72010-02-12 14:42:03 -05006122 &phba->sli4_hba.sli_intf));
James Smart2fcee4b2010-12-15 17:57:46 -05006123 /*
6124 * Check for other Port errors during the initialization
6125 * process. Fail the load if the port did not come up
6126 * correctly.
6127 */
6128 if_type = bf_get(lpfc_sli_intf_if_type,
6129 &phba->sli4_hba.sli_intf);
6130 switch (if_type) {
6131 case LPFC_SLI_INTF_IF_TYPE_0:
6132 phba->sli4_hba.ue_mask_lo =
6133 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
6134 phba->sli4_hba.ue_mask_hi =
6135 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
6136 uerrlo_reg.word0 =
6137 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
6138 uerrhi_reg.word0 =
6139 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
6140 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
6141 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
6142 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6143 "1422 Unrecoverable Error "
6144 "Detected during POST "
6145 "uerr_lo_reg=0x%x, "
6146 "uerr_hi_reg=0x%x, "
6147 "ue_mask_lo_reg=0x%x, "
6148 "ue_mask_hi_reg=0x%x\n",
6149 uerrlo_reg.word0,
6150 uerrhi_reg.word0,
6151 phba->sli4_hba.ue_mask_lo,
6152 phba->sli4_hba.ue_mask_hi);
6153 port_error = -ENODEV;
6154 }
6155 break;
6156 case LPFC_SLI_INTF_IF_TYPE_2:
6157 /* Final checks. The port status should be clean. */
James Smart9940b972011-03-11 16:06:12 -05006158 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
6159 &reg_data.word0) ||
James Smart05580562011-05-24 11:40:48 -04006160 (bf_get(lpfc_sliport_status_err, &reg_data) &&
6161 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
James Smart2fcee4b2010-12-15 17:57:46 -05006162 phba->work_status[0] =
6163 readl(phba->sli4_hba.u.if_type2.
6164 ERR1regaddr);
6165 phba->work_status[1] =
6166 readl(phba->sli4_hba.u.if_type2.
6167 ERR2regaddr);
6168 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05006169 "2888 Unrecoverable port error "
6170 "following POST: port status reg "
6171 "0x%x, port_smphr reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05006172 "error 1=0x%x, error 2=0x%x\n",
6173 reg_data.word0,
6174 portsmphr_reg.word0,
6175 phba->work_status[0],
6176 phba->work_status[1]);
6177 port_error = -ENODEV;
6178 }
6179 break;
6180 case LPFC_SLI_INTF_IF_TYPE_1:
6181 default:
6182 break;
6183 }
James Smart28baac72010-02-12 14:42:03 -05006184 }
James Smartda0436e2009-05-22 14:51:39 -04006185 return port_error;
6186}
6187
6188/**
6189 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
6190 * @phba: pointer to lpfc hba data structure.
James Smart2fcee4b2010-12-15 17:57:46 -05006191 * @if_type: The SLI4 interface type getting configured.
James Smartda0436e2009-05-22 14:51:39 -04006192 *
6193 * This routine is invoked to set up SLI4 BAR0 PCI config space register
6194 * memory map.
6195 **/
6196static void
James Smart2fcee4b2010-12-15 17:57:46 -05006197lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04006198{
James Smart2fcee4b2010-12-15 17:57:46 -05006199 switch (if_type) {
6200 case LPFC_SLI_INTF_IF_TYPE_0:
6201 phba->sli4_hba.u.if_type0.UERRLOregaddr =
6202 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
6203 phba->sli4_hba.u.if_type0.UERRHIregaddr =
6204 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
6205 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
6206 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
6207 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
6208 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
6209 phba->sli4_hba.SLIINTFregaddr =
6210 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
6211 break;
6212 case LPFC_SLI_INTF_IF_TYPE_2:
6213 phba->sli4_hba.u.if_type2.ERR1regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006214 phba->sli4_hba.conf_regs_memmap_p +
6215 LPFC_CTL_PORT_ER1_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006216 phba->sli4_hba.u.if_type2.ERR2regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006217 phba->sli4_hba.conf_regs_memmap_p +
6218 LPFC_CTL_PORT_ER2_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006219 phba->sli4_hba.u.if_type2.CTRLregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006220 phba->sli4_hba.conf_regs_memmap_p +
6221 LPFC_CTL_PORT_CTL_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006222 phba->sli4_hba.u.if_type2.STATUSregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006223 phba->sli4_hba.conf_regs_memmap_p +
6224 LPFC_CTL_PORT_STA_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006225 phba->sli4_hba.SLIINTFregaddr =
6226 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
6227 phba->sli4_hba.PSMPHRregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006228 phba->sli4_hba.conf_regs_memmap_p +
6229 LPFC_CTL_PORT_SEM_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006230 phba->sli4_hba.RQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05006231 phba->sli4_hba.conf_regs_memmap_p +
6232 LPFC_ULP0_RQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05006233 phba->sli4_hba.WQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05006234 phba->sli4_hba.conf_regs_memmap_p +
6235 LPFC_ULP0_WQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05006236 phba->sli4_hba.EQCQDBregaddr =
6237 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
6238 phba->sli4_hba.MQDBregaddr =
6239 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
6240 phba->sli4_hba.BMBXregaddr =
6241 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
6242 break;
6243 case LPFC_SLI_INTF_IF_TYPE_1:
6244 default:
6245 dev_printk(KERN_ERR, &phba->pcidev->dev,
6246 "FATAL - unsupported SLI4 interface type - %d\n",
6247 if_type);
6248 break;
6249 }
James Smartda0436e2009-05-22 14:51:39 -04006250}
6251
6252/**
6253 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
6254 * @phba: pointer to lpfc hba data structure.
6255 *
6256 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
6257 * memory map.
6258 **/
6259static void
6260lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
6261{
James Smart2fcee4b2010-12-15 17:57:46 -05006262 phba->sli4_hba.PSMPHRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
6263 LPFC_SLIPORT_IF0_SMPHR;
James Smartda0436e2009-05-22 14:51:39 -04006264 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006265 LPFC_HST_ISR0;
James Smartda0436e2009-05-22 14:51:39 -04006266 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006267 LPFC_HST_IMR0;
James Smartda0436e2009-05-22 14:51:39 -04006268 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006269 LPFC_HST_ISCR0;
James Smartda0436e2009-05-22 14:51:39 -04006270}
6271
6272/**
6273 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
6274 * @phba: pointer to lpfc hba data structure.
6275 * @vf: virtual function number
6276 *
6277 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
6278 * based on the given viftual function number, @vf.
6279 *
6280 * Return 0 if successful, otherwise -ENODEV.
6281 **/
6282static int
6283lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
6284{
6285 if (vf > LPFC_VIR_FUNC_MAX)
6286 return -ENODEV;
6287
6288 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05006289 vf * LPFC_VFR_PAGE_SIZE +
6290 LPFC_ULP0_RQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04006291 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05006292 vf * LPFC_VFR_PAGE_SIZE +
6293 LPFC_ULP0_WQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04006294 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6295 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
6296 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6297 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
6298 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6299 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
6300 return 0;
6301}
6302
6303/**
6304 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
6305 * @phba: pointer to lpfc hba data structure.
6306 *
6307 * This routine is invoked to create the bootstrap mailbox
6308 * region consistent with the SLI-4 interface spec. This
6309 * routine allocates all memory necessary to communicate
6310 * mailbox commands to the port and sets up all alignment
6311 * needs. No locks are expected to be held when calling
6312 * this routine.
6313 *
6314 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006315 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04006316 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04006317 **/
6318static int
6319lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
6320{
6321 uint32_t bmbx_size;
6322 struct lpfc_dmabuf *dmabuf;
6323 struct dma_address *dma_address;
6324 uint32_t pa_addr;
6325 uint64_t phys_addr;
6326
6327 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
6328 if (!dmabuf)
6329 return -ENOMEM;
6330
6331 /*
6332 * The bootstrap mailbox region is comprised of 2 parts
6333 * plus an alignment restriction of 16 bytes.
6334 */
6335 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
6336 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
6337 bmbx_size,
6338 &dmabuf->phys,
6339 GFP_KERNEL);
6340 if (!dmabuf->virt) {
6341 kfree(dmabuf);
6342 return -ENOMEM;
6343 }
6344 memset(dmabuf->virt, 0, bmbx_size);
6345
6346 /*
6347 * Initialize the bootstrap mailbox pointers now so that the register
6348 * operations are simple later. The mailbox dma address is required
6349 * to be 16-byte aligned. Also align the virtual memory as each
6350 * maibox is copied into the bmbx mailbox region before issuing the
6351 * command to the port.
6352 */
6353 phba->sli4_hba.bmbx.dmabuf = dmabuf;
6354 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
6355
6356 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
6357 LPFC_ALIGN_16_BYTE);
6358 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
6359 LPFC_ALIGN_16_BYTE);
6360
6361 /*
6362 * Set the high and low physical addresses now. The SLI4 alignment
6363 * requirement is 16 bytes and the mailbox is posted to the port
6364 * as two 30-bit addresses. The other data is a bit marking whether
6365 * the 30-bit address is the high or low address.
6366 * Upcast bmbx aphys to 64bits so shift instruction compiles
6367 * clean on 32 bit machines.
6368 */
6369 dma_address = &phba->sli4_hba.bmbx.dma_address;
6370 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
6371 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
6372 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
6373 LPFC_BMBX_BIT1_ADDR_HI);
6374
6375 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
6376 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
6377 LPFC_BMBX_BIT1_ADDR_LO);
6378 return 0;
6379}
6380
6381/**
6382 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
6383 * @phba: pointer to lpfc hba data structure.
6384 *
6385 * This routine is invoked to teardown the bootstrap mailbox
6386 * region and release all host resources. This routine requires
6387 * the caller to ensure all mailbox commands recovered, no
6388 * additional mailbox comands are sent, and interrupts are disabled
6389 * before calling this routine.
6390 *
6391 **/
6392static void
6393lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
6394{
6395 dma_free_coherent(&phba->pcidev->dev,
6396 phba->sli4_hba.bmbx.bmbx_size,
6397 phba->sli4_hba.bmbx.dmabuf->virt,
6398 phba->sli4_hba.bmbx.dmabuf->phys);
6399
6400 kfree(phba->sli4_hba.bmbx.dmabuf);
6401 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
6402}
6403
6404/**
6405 * lpfc_sli4_read_config - Get the config parameters.
6406 * @phba: pointer to lpfc hba data structure.
6407 *
6408 * This routine is invoked to read the configuration parameters from the HBA.
6409 * The configuration parameters are used to set the base and maximum values
6410 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
6411 * allocation for the port.
6412 *
6413 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006414 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006415 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006416 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006417 **/
James Smartff78d8f2011-12-13 13:21:35 -05006418int
James Smartda0436e2009-05-22 14:51:39 -04006419lpfc_sli4_read_config(struct lpfc_hba *phba)
6420{
6421 LPFC_MBOXQ_t *pmb;
6422 struct lpfc_mbx_read_config *rd_config;
James Smart912e3ac2011-05-24 11:42:11 -04006423 union lpfc_sli4_cfg_shdr *shdr;
6424 uint32_t shdr_status, shdr_add_status;
6425 struct lpfc_mbx_get_func_cfg *get_func_cfg;
6426 struct lpfc_rsrc_desc_fcfcoe *desc;
James Smart8aa134a2012-08-14 14:25:29 -04006427 char *pdesc_0;
James Smart912e3ac2011-05-24 11:42:11 -04006428 uint32_t desc_count;
James Smart8aa134a2012-08-14 14:25:29 -04006429 int length, i, rc = 0, rc2;
James Smartda0436e2009-05-22 14:51:39 -04006430
6431 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6432 if (!pmb) {
6433 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6434 "2011 Unable to allocate memory for issuing "
6435 "SLI_CONFIG_SPECIAL mailbox command\n");
6436 return -ENOMEM;
6437 }
6438
6439 lpfc_read_config(phba, pmb);
6440
6441 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6442 if (rc != MBX_SUCCESS) {
6443 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6444 "2012 Mailbox failed , mbxCmd x%x "
6445 "READ_CONFIG, mbxStatus x%x\n",
6446 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6447 bf_get(lpfc_mqe_status, &pmb->u.mqe));
6448 rc = -EIO;
6449 } else {
6450 rd_config = &pmb->u.mqe.un.rd_config;
James Smartff78d8f2011-12-13 13:21:35 -05006451 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
6452 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
6453 phba->sli4_hba.lnk_info.lnk_tp =
6454 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
6455 phba->sli4_hba.lnk_info.lnk_no =
6456 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
6457 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6458 "3081 lnk_type:%d, lnk_numb:%d\n",
6459 phba->sli4_hba.lnk_info.lnk_tp,
6460 phba->sli4_hba.lnk_info.lnk_no);
6461 } else
6462 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6463 "3082 Mailbox (x%x) returned ldv:x0\n",
6464 bf_get(lpfc_mqe_command, &pmb->u.mqe));
James Smart6d368e52011-05-24 11:44:12 -04006465 phba->sli4_hba.extents_in_use =
6466 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04006467 phba->sli4_hba.max_cfg_param.max_xri =
6468 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
6469 phba->sli4_hba.max_cfg_param.xri_base =
6470 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
6471 phba->sli4_hba.max_cfg_param.max_vpi =
6472 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
6473 phba->sli4_hba.max_cfg_param.vpi_base =
6474 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
6475 phba->sli4_hba.max_cfg_param.max_rpi =
6476 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
6477 phba->sli4_hba.max_cfg_param.rpi_base =
6478 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
6479 phba->sli4_hba.max_cfg_param.max_vfi =
6480 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
6481 phba->sli4_hba.max_cfg_param.vfi_base =
6482 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
6483 phba->sli4_hba.max_cfg_param.max_fcfi =
6484 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04006485 phba->sli4_hba.max_cfg_param.max_eq =
6486 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
6487 phba->sli4_hba.max_cfg_param.max_rq =
6488 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
6489 phba->sli4_hba.max_cfg_param.max_wq =
6490 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
6491 phba->sli4_hba.max_cfg_param.max_cq =
6492 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
6493 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
6494 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
6495 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
6496 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
James Smart5ffc2662009-11-18 15:39:44 -05006497 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
6498 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04006499 phba->max_vports = phba->max_vpi;
6500 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart6d368e52011-05-24 11:44:12 -04006501 "2003 cfg params Extents? %d "
6502 "XRI(B:%d M:%d), "
James Smartda0436e2009-05-22 14:51:39 -04006503 "VPI(B:%d M:%d) "
6504 "VFI(B:%d M:%d) "
6505 "RPI(B:%d M:%d) "
James Smart6d368e52011-05-24 11:44:12 -04006506 "FCFI(Count:%d)\n",
6507 phba->sli4_hba.extents_in_use,
James Smartda0436e2009-05-22 14:51:39 -04006508 phba->sli4_hba.max_cfg_param.xri_base,
6509 phba->sli4_hba.max_cfg_param.max_xri,
6510 phba->sli4_hba.max_cfg_param.vpi_base,
6511 phba->sli4_hba.max_cfg_param.max_vpi,
6512 phba->sli4_hba.max_cfg_param.vfi_base,
6513 phba->sli4_hba.max_cfg_param.max_vfi,
6514 phba->sli4_hba.max_cfg_param.rpi_base,
6515 phba->sli4_hba.max_cfg_param.max_rpi,
James Smartda0436e2009-05-22 14:51:39 -04006516 phba->sli4_hba.max_cfg_param.max_fcfi);
6517 }
James Smart912e3ac2011-05-24 11:42:11 -04006518
6519 if (rc)
6520 goto read_cfg_out;
James Smartda0436e2009-05-22 14:51:39 -04006521
6522 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smartb92938b2010-06-07 15:24:12 -04006523 if (phba->cfg_hba_queue_depth >
6524 (phba->sli4_hba.max_cfg_param.max_xri -
6525 lpfc_sli4_get_els_iocb_cnt(phba)))
James Smartda0436e2009-05-22 14:51:39 -04006526 phba->cfg_hba_queue_depth =
James Smartb92938b2010-06-07 15:24:12 -04006527 phba->sli4_hba.max_cfg_param.max_xri -
6528 lpfc_sli4_get_els_iocb_cnt(phba);
James Smart912e3ac2011-05-24 11:42:11 -04006529
6530 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
6531 LPFC_SLI_INTF_IF_TYPE_2)
6532 goto read_cfg_out;
6533
6534 /* get the pf# and vf# for SLI4 if_type 2 port */
6535 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
6536 sizeof(struct lpfc_sli4_cfg_mhdr));
6537 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
6538 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
6539 length, LPFC_SLI4_MBX_EMBED);
6540
James Smart8aa134a2012-08-14 14:25:29 -04006541 rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart912e3ac2011-05-24 11:42:11 -04006542 shdr = (union lpfc_sli4_cfg_shdr *)
6543 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
6544 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6545 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart8aa134a2012-08-14 14:25:29 -04006546 if (rc2 || shdr_status || shdr_add_status) {
James Smart912e3ac2011-05-24 11:42:11 -04006547 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6548 "3026 Mailbox failed , mbxCmd x%x "
6549 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
6550 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6551 bf_get(lpfc_mqe_status, &pmb->u.mqe));
James Smart912e3ac2011-05-24 11:42:11 -04006552 goto read_cfg_out;
6553 }
6554
6555 /* search for fc_fcoe resrouce descriptor */
6556 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
6557 desc_count = get_func_cfg->func_cfg.rsrc_desc_count;
6558
James Smart8aa134a2012-08-14 14:25:29 -04006559 pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
6560 desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
6561 length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
6562 if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
6563 length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
6564 else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
6565 goto read_cfg_out;
6566
James Smart912e3ac2011-05-24 11:42:11 -04006567 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
James Smart8aa134a2012-08-14 14:25:29 -04006568 desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
James Smart912e3ac2011-05-24 11:42:11 -04006569 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
James Smart8aa134a2012-08-14 14:25:29 -04006570 bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
James Smart912e3ac2011-05-24 11:42:11 -04006571 phba->sli4_hba.iov.pf_number =
6572 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
6573 phba->sli4_hba.iov.vf_number =
6574 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
6575 break;
6576 }
6577 }
6578
6579 if (i < LPFC_RSRC_DESC_MAX_NUM)
6580 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6581 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
6582 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
6583 phba->sli4_hba.iov.vf_number);
James Smart8aa134a2012-08-14 14:25:29 -04006584 else
James Smart912e3ac2011-05-24 11:42:11 -04006585 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6586 "3028 GET_FUNCTION_CONFIG: failed to find "
6587 "Resrouce Descriptor:x%x\n",
6588 LPFC_RSRC_DESC_TYPE_FCFCOE);
James Smart912e3ac2011-05-24 11:42:11 -04006589
6590read_cfg_out:
6591 mempool_free(pmb, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04006592 return rc;
6593}
6594
6595/**
James Smart2fcee4b2010-12-15 17:57:46 -05006596 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
James Smartda0436e2009-05-22 14:51:39 -04006597 * @phba: pointer to lpfc hba data structure.
6598 *
James Smart2fcee4b2010-12-15 17:57:46 -05006599 * This routine is invoked to setup the port-side endian order when
6600 * the port if_type is 0. This routine has no function for other
6601 * if_types.
James Smartda0436e2009-05-22 14:51:39 -04006602 *
6603 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006604 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006605 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006606 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006607 **/
6608static int
6609lpfc_setup_endian_order(struct lpfc_hba *phba)
6610{
6611 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05006612 uint32_t if_type, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04006613 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
6614 HOST_ENDIAN_HIGH_WORD1};
6615
James Smart2fcee4b2010-12-15 17:57:46 -05006616 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
6617 switch (if_type) {
6618 case LPFC_SLI_INTF_IF_TYPE_0:
6619 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6620 GFP_KERNEL);
6621 if (!mboxq) {
6622 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6623 "0492 Unable to allocate memory for "
6624 "issuing SLI_CONFIG_SPECIAL mailbox "
6625 "command\n");
6626 return -ENOMEM;
6627 }
James Smartda0436e2009-05-22 14:51:39 -04006628
James Smart2fcee4b2010-12-15 17:57:46 -05006629 /*
6630 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
6631 * two words to contain special data values and no other data.
6632 */
6633 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
6634 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
6635 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6636 if (rc != MBX_SUCCESS) {
6637 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6638 "0493 SLI_CONFIG_SPECIAL mailbox "
6639 "failed with status x%x\n",
6640 rc);
6641 rc = -EIO;
6642 }
6643 mempool_free(mboxq, phba->mbox_mem_pool);
6644 break;
6645 case LPFC_SLI_INTF_IF_TYPE_2:
6646 case LPFC_SLI_INTF_IF_TYPE_1:
6647 default:
6648 break;
James Smartda0436e2009-05-22 14:51:39 -04006649 }
James Smartda0436e2009-05-22 14:51:39 -04006650 return rc;
6651}
6652
6653/**
James Smart5350d872011-10-10 21:33:49 -04006654 * lpfc_sli4_queue_verify - Verify and update EQ and CQ counts
James Smartda0436e2009-05-22 14:51:39 -04006655 * @phba: pointer to lpfc hba data structure.
6656 *
James Smart5350d872011-10-10 21:33:49 -04006657 * This routine is invoked to check the user settable queue counts for EQs and
6658 * CQs. after this routine is called the counts will be set to valid values that
6659 * adhere to the constraints of the system's interrupt vectors and the port's
6660 * queue resources.
James Smartda0436e2009-05-22 14:51:39 -04006661 *
6662 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006663 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006664 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04006665 **/
6666static int
James Smart5350d872011-10-10 21:33:49 -04006667lpfc_sli4_queue_verify(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04006668{
James Smart67d12732012-08-03 12:36:13 -04006669 int cfg_fcp_io_channel;
James Smart90695ee2012-08-14 14:25:36 -04006670 uint32_t cpu;
6671 uint32_t i = 0;
6672
James Smartda0436e2009-05-22 14:51:39 -04006673
6674 /*
James Smart67d12732012-08-03 12:36:13 -04006675 * Sanity check for configured queue parameters against the run-time
James Smartda0436e2009-05-22 14:51:39 -04006676 * device parameters
6677 */
6678
James Smart67d12732012-08-03 12:36:13 -04006679 /* Sanity check on HBA EQ parameters */
6680 cfg_fcp_io_channel = phba->cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04006681
James Smart90695ee2012-08-14 14:25:36 -04006682 /* It doesn't make sense to have more io channels then CPUs */
6683 for_each_online_cpu(cpu) {
6684 i++;
6685 }
6686 if (i < cfg_fcp_io_channel) {
James Smart82c3e9b2012-09-29 11:29:50 -04006687 lpfc_printf_log(phba,
6688 KERN_ERR, LOG_INIT,
James Smart90695ee2012-08-14 14:25:36 -04006689 "3188 Reducing IO channels to match number of "
6690 "CPUs: from %d to %d\n", cfg_fcp_io_channel, i);
6691 cfg_fcp_io_channel = i;
6692 }
6693
James Smart67d12732012-08-03 12:36:13 -04006694 if (cfg_fcp_io_channel >
6695 phba->sli4_hba.max_cfg_param.max_eq) {
James Smart82c3e9b2012-09-29 11:29:50 -04006696 if (phba->sli4_hba.max_cfg_param.max_eq <
6697 LPFC_FCP_IO_CHAN_MIN) {
James Smartda0436e2009-05-22 14:51:39 -04006698 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6699 "2574 Not enough EQs (%d) from the "
6700 "pci function for supporting FCP "
6701 "EQs (%d)\n",
6702 phba->sli4_hba.max_cfg_param.max_eq,
James Smart67d12732012-08-03 12:36:13 -04006703 phba->cfg_fcp_io_channel);
James Smartda0436e2009-05-22 14:51:39 -04006704 goto out_error;
6705 }
James Smart82c3e9b2012-09-29 11:29:50 -04006706 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6707 "2575 Reducing IO channels to match number of "
6708 "available EQs: from %d to %d\n",
6709 cfg_fcp_io_channel,
6710 phba->sli4_hba.max_cfg_param.max_eq);
6711 cfg_fcp_io_channel = phba->sli4_hba.max_cfg_param.max_eq;
James Smartda0436e2009-05-22 14:51:39 -04006712 }
James Smart67d12732012-08-03 12:36:13 -04006713
6714 /* Eventually cfg_fcp_eq_count / cfg_fcp_wq_count will be depricated */
6715
James Smartda0436e2009-05-22 14:51:39 -04006716 /* The actual number of FCP event queues adopted */
James Smart67d12732012-08-03 12:36:13 -04006717 phba->cfg_fcp_eq_count = cfg_fcp_io_channel;
6718 phba->cfg_fcp_wq_count = cfg_fcp_io_channel;
6719 phba->cfg_fcp_io_channel = cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04006720
James Smartda0436e2009-05-22 14:51:39 -04006721 /* Get EQ depth from module parameter, fake the default for now */
6722 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
6723 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
6724
James Smart5350d872011-10-10 21:33:49 -04006725 /* Get CQ depth from module parameter, fake the default for now */
6726 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
6727 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
6728
6729 return 0;
6730out_error:
6731 return -ENOMEM;
6732}
6733
6734/**
6735 * lpfc_sli4_queue_create - Create all the SLI4 queues
6736 * @phba: pointer to lpfc hba data structure.
6737 *
6738 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
6739 * operation. For each SLI4 queue type, the parameters such as queue entry
6740 * count (queue depth) shall be taken from the module parameter. For now,
6741 * we just use some constant number as place holder.
6742 *
6743 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07006744 * 0 - successful
James Smart5350d872011-10-10 21:33:49 -04006745 * -ENOMEM - No availble memory
6746 * -EIO - The mailbox failed to complete successfully.
6747 **/
6748int
6749lpfc_sli4_queue_create(struct lpfc_hba *phba)
6750{
6751 struct lpfc_queue *qdesc;
James Smart67d12732012-08-03 12:36:13 -04006752 int idx;
James Smart5350d872011-10-10 21:33:49 -04006753
6754 /*
James Smart67d12732012-08-03 12:36:13 -04006755 * Create HBA Record arrays.
James Smart5350d872011-10-10 21:33:49 -04006756 */
James Smart67d12732012-08-03 12:36:13 -04006757 if (!phba->cfg_fcp_io_channel)
6758 return -ERANGE;
James Smart5350d872011-10-10 21:33:49 -04006759
James Smart67d12732012-08-03 12:36:13 -04006760 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
6761 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
6762 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
6763 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
6764 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
6765 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
6766
6767 phba->sli4_hba.hba_eq = kzalloc((sizeof(struct lpfc_queue *) *
6768 phba->cfg_fcp_io_channel), GFP_KERNEL);
6769 if (!phba->sli4_hba.hba_eq) {
James Smartda0436e2009-05-22 14:51:39 -04006770 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04006771 "2576 Failed allocate memory for "
6772 "fast-path EQ record array\n");
James Smartda0436e2009-05-22 14:51:39 -04006773 goto out_error;
6774 }
James Smart67d12732012-08-03 12:36:13 -04006775
6776 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
6777 phba->cfg_fcp_io_channel), GFP_KERNEL);
6778 if (!phba->sli4_hba.fcp_cq) {
6779 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6780 "2577 Failed allocate memory for fast-path "
6781 "CQ record array\n");
6782 goto out_error;
6783 }
6784
6785 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
6786 phba->cfg_fcp_io_channel), GFP_KERNEL);
6787 if (!phba->sli4_hba.fcp_wq) {
6788 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6789 "2578 Failed allocate memory for fast-path "
6790 "WQ record array\n");
6791 goto out_error;
6792 }
James Smartda0436e2009-05-22 14:51:39 -04006793
James Smart5350d872011-10-10 21:33:49 -04006794 /*
James Smart67d12732012-08-03 12:36:13 -04006795 * Since the first EQ can have multiple CQs associated with it,
6796 * this array is used to quickly see if we have a FCP fast-path
6797 * CQ match.
James Smart5350d872011-10-10 21:33:49 -04006798 */
James Smart67d12732012-08-03 12:36:13 -04006799 phba->sli4_hba.fcp_cq_map = kzalloc((sizeof(uint16_t) *
6800 phba->cfg_fcp_io_channel), GFP_KERNEL);
6801 if (!phba->sli4_hba.fcp_cq_map) {
6802 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6803 "2545 Failed allocate memory for fast-path "
6804 "CQ map\n");
6805 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04006806 }
James Smart67d12732012-08-03 12:36:13 -04006807
6808 /*
6809 * Create HBA Event Queues (EQs). The cfg_fcp_io_channel specifies
6810 * how many EQs to create.
6811 */
6812 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
6813
6814 /* Create EQs */
James Smartda0436e2009-05-22 14:51:39 -04006815 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
6816 phba->sli4_hba.eq_ecount);
6817 if (!qdesc) {
6818 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04006819 "0497 Failed allocate EQ (%d)\n", idx);
6820 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04006821 }
James Smart67d12732012-08-03 12:36:13 -04006822 phba->sli4_hba.hba_eq[idx] = qdesc;
6823
6824 /* Create Fast Path FCP CQs */
6825 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6826 phba->sli4_hba.cq_ecount);
6827 if (!qdesc) {
6828 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6829 "0499 Failed allocate fast-path FCP "
6830 "CQ (%d)\n", idx);
6831 goto out_error;
6832 }
6833 phba->sli4_hba.fcp_cq[idx] = qdesc;
6834
6835 /* Create Fast Path FCP WQs */
6836 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
6837 phba->sli4_hba.wq_ecount);
6838 if (!qdesc) {
6839 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6840 "0503 Failed allocate fast-path FCP "
6841 "WQ (%d)\n", idx);
6842 goto out_error;
6843 }
6844 phba->sli4_hba.fcp_wq[idx] = qdesc;
James Smartda0436e2009-05-22 14:51:39 -04006845 }
6846
James Smart67d12732012-08-03 12:36:13 -04006847
James Smartda0436e2009-05-22 14:51:39 -04006848 /*
James Smart67d12732012-08-03 12:36:13 -04006849 * Create Slow Path Completion Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04006850 */
6851
James Smartda0436e2009-05-22 14:51:39 -04006852 /* Create slow-path Mailbox Command Complete Queue */
6853 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6854 phba->sli4_hba.cq_ecount);
6855 if (!qdesc) {
6856 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6857 "0500 Failed allocate slow-path mailbox CQ\n");
James Smart67d12732012-08-03 12:36:13 -04006858 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04006859 }
6860 phba->sli4_hba.mbx_cq = qdesc;
6861
6862 /* Create slow-path ELS Complete Queue */
6863 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6864 phba->sli4_hba.cq_ecount);
6865 if (!qdesc) {
6866 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6867 "0501 Failed allocate slow-path ELS CQ\n");
James Smart67d12732012-08-03 12:36:13 -04006868 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04006869 }
6870 phba->sli4_hba.els_cq = qdesc;
6871
James Smartda0436e2009-05-22 14:51:39 -04006872
James Smart5350d872011-10-10 21:33:49 -04006873 /*
James Smart67d12732012-08-03 12:36:13 -04006874 * Create Slow Path Work Queues (WQs)
James Smart5350d872011-10-10 21:33:49 -04006875 */
James Smartda0436e2009-05-22 14:51:39 -04006876
6877 /* Create Mailbox Command Queue */
James Smartda0436e2009-05-22 14:51:39 -04006878
6879 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
6880 phba->sli4_hba.mq_ecount);
6881 if (!qdesc) {
6882 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6883 "0505 Failed allocate slow-path MQ\n");
James Smart67d12732012-08-03 12:36:13 -04006884 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04006885 }
6886 phba->sli4_hba.mbx_wq = qdesc;
6887
6888 /*
James Smart67d12732012-08-03 12:36:13 -04006889 * Create ELS Work Queues
James Smartda0436e2009-05-22 14:51:39 -04006890 */
James Smartda0436e2009-05-22 14:51:39 -04006891
6892 /* Create slow-path ELS Work Queue */
6893 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
6894 phba->sli4_hba.wq_ecount);
6895 if (!qdesc) {
6896 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6897 "0504 Failed allocate slow-path ELS WQ\n");
James Smart67d12732012-08-03 12:36:13 -04006898 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04006899 }
6900 phba->sli4_hba.els_wq = qdesc;
6901
James Smartda0436e2009-05-22 14:51:39 -04006902 /*
6903 * Create Receive Queue (RQ)
6904 */
James Smartda0436e2009-05-22 14:51:39 -04006905
6906 /* Create Receive Queue for header */
6907 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
6908 phba->sli4_hba.rq_ecount);
6909 if (!qdesc) {
6910 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6911 "0506 Failed allocate receive HRQ\n");
James Smart67d12732012-08-03 12:36:13 -04006912 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04006913 }
6914 phba->sli4_hba.hdr_rq = qdesc;
6915
6916 /* Create Receive Queue for data */
6917 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
6918 phba->sli4_hba.rq_ecount);
6919 if (!qdesc) {
6920 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6921 "0507 Failed allocate receive DRQ\n");
James Smart67d12732012-08-03 12:36:13 -04006922 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04006923 }
6924 phba->sli4_hba.dat_rq = qdesc;
6925
6926 return 0;
6927
James Smartda0436e2009-05-22 14:51:39 -04006928out_error:
James Smart67d12732012-08-03 12:36:13 -04006929 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04006930 return -ENOMEM;
6931}
6932
6933/**
6934 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
6935 * @phba: pointer to lpfc hba data structure.
6936 *
6937 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
6938 * operation.
6939 *
6940 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006941 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006942 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006943 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006944 **/
James Smart5350d872011-10-10 21:33:49 -04006945void
James Smartda0436e2009-05-22 14:51:39 -04006946lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
6947{
James Smart67d12732012-08-03 12:36:13 -04006948 int idx;
6949
6950 if (phba->sli4_hba.hba_eq != NULL) {
6951 /* Release HBA event queue */
6952 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
6953 if (phba->sli4_hba.hba_eq[idx] != NULL) {
6954 lpfc_sli4_queue_free(
6955 phba->sli4_hba.hba_eq[idx]);
6956 phba->sli4_hba.hba_eq[idx] = NULL;
6957 }
6958 }
6959 kfree(phba->sli4_hba.hba_eq);
6960 phba->sli4_hba.hba_eq = NULL;
6961 }
6962
6963 if (phba->sli4_hba.fcp_cq != NULL) {
6964 /* Release FCP completion queue */
6965 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
6966 if (phba->sli4_hba.fcp_cq[idx] != NULL) {
6967 lpfc_sli4_queue_free(
6968 phba->sli4_hba.fcp_cq[idx]);
6969 phba->sli4_hba.fcp_cq[idx] = NULL;
6970 }
6971 }
6972 kfree(phba->sli4_hba.fcp_cq);
6973 phba->sli4_hba.fcp_cq = NULL;
6974 }
6975
6976 if (phba->sli4_hba.fcp_wq != NULL) {
6977 /* Release FCP work queue */
6978 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
6979 if (phba->sli4_hba.fcp_wq[idx] != NULL) {
6980 lpfc_sli4_queue_free(
6981 phba->sli4_hba.fcp_wq[idx]);
6982 phba->sli4_hba.fcp_wq[idx] = NULL;
6983 }
6984 }
6985 kfree(phba->sli4_hba.fcp_wq);
6986 phba->sli4_hba.fcp_wq = NULL;
6987 }
6988
James Smart962bc512013-01-03 15:44:00 -05006989 if (phba->pci_bar0_memmap_p) {
6990 iounmap(phba->pci_bar0_memmap_p);
6991 phba->pci_bar0_memmap_p = NULL;
6992 }
6993 if (phba->pci_bar2_memmap_p) {
6994 iounmap(phba->pci_bar2_memmap_p);
6995 phba->pci_bar2_memmap_p = NULL;
6996 }
6997 if (phba->pci_bar4_memmap_p) {
6998 iounmap(phba->pci_bar4_memmap_p);
6999 phba->pci_bar4_memmap_p = NULL;
7000 }
7001
James Smart67d12732012-08-03 12:36:13 -04007002 /* Release FCP CQ mapping array */
7003 if (phba->sli4_hba.fcp_cq_map != NULL) {
7004 kfree(phba->sli4_hba.fcp_cq_map);
7005 phba->sli4_hba.fcp_cq_map = NULL;
7006 }
James Smartda0436e2009-05-22 14:51:39 -04007007
7008 /* Release mailbox command work queue */
James Smart67d12732012-08-03 12:36:13 -04007009 if (phba->sli4_hba.mbx_wq != NULL) {
7010 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
7011 phba->sli4_hba.mbx_wq = NULL;
7012 }
James Smartda0436e2009-05-22 14:51:39 -04007013
7014 /* Release ELS work queue */
James Smart67d12732012-08-03 12:36:13 -04007015 if (phba->sli4_hba.els_wq != NULL) {
7016 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
7017 phba->sli4_hba.els_wq = NULL;
7018 }
James Smartda0436e2009-05-22 14:51:39 -04007019
7020 /* Release unsolicited receive queue */
James Smart67d12732012-08-03 12:36:13 -04007021 if (phba->sli4_hba.hdr_rq != NULL) {
7022 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
7023 phba->sli4_hba.hdr_rq = NULL;
7024 }
7025 if (phba->sli4_hba.dat_rq != NULL) {
7026 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
7027 phba->sli4_hba.dat_rq = NULL;
7028 }
James Smartda0436e2009-05-22 14:51:39 -04007029
James Smartda0436e2009-05-22 14:51:39 -04007030 /* Release ELS complete queue */
James Smart67d12732012-08-03 12:36:13 -04007031 if (phba->sli4_hba.els_cq != NULL) {
7032 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
7033 phba->sli4_hba.els_cq = NULL;
7034 }
James Smartda0436e2009-05-22 14:51:39 -04007035
7036 /* Release mailbox command complete queue */
James Smart67d12732012-08-03 12:36:13 -04007037 if (phba->sli4_hba.mbx_cq != NULL) {
7038 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
7039 phba->sli4_hba.mbx_cq = NULL;
7040 }
James Smartda0436e2009-05-22 14:51:39 -04007041
7042 return;
7043}
7044
7045/**
7046 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
7047 * @phba: pointer to lpfc hba data structure.
7048 *
7049 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
7050 * operation.
7051 *
7052 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007053 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007054 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007055 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007056 **/
7057int
7058lpfc_sli4_queue_setup(struct lpfc_hba *phba)
7059{
James Smart2a76a282012-08-03 12:35:54 -04007060 struct lpfc_sli *psli = &phba->sli;
7061 struct lpfc_sli_ring *pring;
James Smartda0436e2009-05-22 14:51:39 -04007062 int rc = -ENOMEM;
7063 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
7064 int fcp_cq_index = 0;
James Smart962bc512013-01-03 15:44:00 -05007065 uint32_t shdr_status, shdr_add_status;
7066 union lpfc_sli4_cfg_shdr *shdr;
7067 LPFC_MBOXQ_t *mboxq;
7068 uint32_t length;
7069
7070 /* Check for dual-ULP support */
7071 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7072 if (!mboxq) {
7073 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7074 "3249 Unable to allocate memory for "
7075 "QUERY_FW_CFG mailbox command\n");
7076 return -ENOMEM;
7077 }
7078 length = (sizeof(struct lpfc_mbx_query_fw_config) -
7079 sizeof(struct lpfc_sli4_cfg_mhdr));
7080 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7081 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
7082 length, LPFC_SLI4_MBX_EMBED);
7083
7084 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7085
7086 shdr = (union lpfc_sli4_cfg_shdr *)
7087 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7088 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7089 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7090 if (shdr_status || shdr_add_status || rc) {
7091 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7092 "3250 QUERY_FW_CFG mailbox failed with status "
7093 "x%x add_status x%x, mbx status x%x\n",
7094 shdr_status, shdr_add_status, rc);
7095 if (rc != MBX_TIMEOUT)
7096 mempool_free(mboxq, phba->mbox_mem_pool);
7097 rc = -ENXIO;
7098 goto out_error;
7099 }
7100
7101 phba->sli4_hba.fw_func_mode =
7102 mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
7103 phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
7104 phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
7105 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7106 "3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
7107 "ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
7108 phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
7109
7110 if (rc != MBX_TIMEOUT)
7111 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04007112
7113 /*
James Smart67d12732012-08-03 12:36:13 -04007114 * Set up HBA Event Queues (EQs)
James Smartda0436e2009-05-22 14:51:39 -04007115 */
7116
James Smart67d12732012-08-03 12:36:13 -04007117 /* Set up HBA event queue */
7118 if (phba->cfg_fcp_io_channel && !phba->sli4_hba.hba_eq) {
James Smart2e90f4b2011-12-13 13:22:37 -05007119 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7120 "3147 Fast-path EQs not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007121 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007122 goto out_error;
James Smart2e90f4b2011-12-13 13:22:37 -05007123 }
James Smart67d12732012-08-03 12:36:13 -04007124 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
7125 if (!phba->sli4_hba.hba_eq[fcp_eqidx]) {
James Smartda0436e2009-05-22 14:51:39 -04007126 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7127 "0522 Fast-path EQ (%d) not "
7128 "allocated\n", fcp_eqidx);
James Smart1b511972011-12-13 13:23:09 -05007129 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007130 goto out_destroy_hba_eq;
James Smartda0436e2009-05-22 14:51:39 -04007131 }
James Smart67d12732012-08-03 12:36:13 -04007132 rc = lpfc_eq_create(phba, phba->sli4_hba.hba_eq[fcp_eqidx],
James Smartbf8dae82012-08-03 12:36:24 -04007133 (phba->cfg_fcp_imax / phba->cfg_fcp_io_channel));
James Smartda0436e2009-05-22 14:51:39 -04007134 if (rc) {
7135 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7136 "0523 Failed setup of fast-path EQ "
7137 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
James Smart67d12732012-08-03 12:36:13 -04007138 goto out_destroy_hba_eq;
James Smartda0436e2009-05-22 14:51:39 -04007139 }
7140 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04007141 "2584 HBA EQ setup: "
James Smartda0436e2009-05-22 14:51:39 -04007142 "queue[%d]-id=%d\n", fcp_eqidx,
James Smart67d12732012-08-03 12:36:13 -04007143 phba->sli4_hba.hba_eq[fcp_eqidx]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007144 }
7145
James Smart67d12732012-08-03 12:36:13 -04007146 /* Set up fast-path FCP Response Complete Queue */
7147 if (!phba->sli4_hba.fcp_cq) {
7148 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7149 "3148 Fast-path FCP CQ array not "
7150 "allocated\n");
7151 rc = -ENOMEM;
7152 goto out_destroy_hba_eq;
7153 }
7154
7155 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_io_channel; fcp_cqidx++) {
7156 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
7157 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7158 "0526 Fast-path FCP CQ (%d) not "
7159 "allocated\n", fcp_cqidx);
7160 rc = -ENOMEM;
7161 goto out_destroy_fcp_cq;
7162 }
7163 rc = lpfc_cq_create(phba, phba->sli4_hba.fcp_cq[fcp_cqidx],
7164 phba->sli4_hba.hba_eq[fcp_cqidx], LPFC_WCQ, LPFC_FCP);
7165 if (rc) {
7166 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7167 "0527 Failed setup of fast-path FCP "
7168 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
7169 goto out_destroy_fcp_cq;
7170 }
7171
7172 /* Setup fcp_cq_map for fast lookup */
7173 phba->sli4_hba.fcp_cq_map[fcp_cqidx] =
7174 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id;
7175
7176 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7177 "2588 FCP CQ setup: cq[%d]-id=%d, "
7178 "parent seq[%d]-id=%d\n",
7179 fcp_cqidx,
7180 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
7181 fcp_cqidx,
7182 phba->sli4_hba.hba_eq[fcp_cqidx]->queue_id);
7183 }
7184
7185 /* Set up fast-path FCP Work Queue */
7186 if (!phba->sli4_hba.fcp_wq) {
7187 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7188 "3149 Fast-path FCP WQ array not "
7189 "allocated\n");
7190 rc = -ENOMEM;
7191 goto out_destroy_fcp_cq;
7192 }
7193
7194 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_io_channel; fcp_wqidx++) {
7195 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
7196 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7197 "0534 Fast-path FCP WQ (%d) not "
7198 "allocated\n", fcp_wqidx);
7199 rc = -ENOMEM;
7200 goto out_destroy_fcp_wq;
7201 }
7202 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
7203 phba->sli4_hba.fcp_cq[fcp_wqidx],
7204 LPFC_FCP);
7205 if (rc) {
7206 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7207 "0535 Failed setup of fast-path FCP "
7208 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
7209 goto out_destroy_fcp_wq;
7210 }
7211
7212 /* Bind this WQ to the next FCP ring */
7213 pring = &psli->ring[MAX_SLI3_CONFIGURED_RINGS + fcp_wqidx];
7214 pring->sli.sli4.wqp = (void *)phba->sli4_hba.fcp_wq[fcp_wqidx];
7215 phba->sli4_hba.fcp_cq[fcp_wqidx]->pring = pring;
7216
7217 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7218 "2591 FCP WQ setup: wq[%d]-id=%d, "
7219 "parent cq[%d]-id=%d\n",
7220 fcp_wqidx,
7221 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
7222 fcp_cq_index,
7223 phba->sli4_hba.fcp_cq[fcp_wqidx]->queue_id);
7224 }
James Smartda0436e2009-05-22 14:51:39 -04007225 /*
7226 * Set up Complete Queues (CQs)
7227 */
7228
7229 /* Set up slow-path MBOX Complete Queue as the first CQ */
7230 if (!phba->sli4_hba.mbx_cq) {
7231 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7232 "0528 Mailbox CQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007233 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007234 goto out_destroy_fcp_wq;
James Smartda0436e2009-05-22 14:51:39 -04007235 }
James Smart67d12732012-08-03 12:36:13 -04007236 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq,
7237 phba->sli4_hba.hba_eq[0], LPFC_MCQ, LPFC_MBOX);
James Smartda0436e2009-05-22 14:51:39 -04007238 if (rc) {
7239 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7240 "0529 Failed setup of slow-path mailbox CQ: "
7241 "rc = 0x%x\n", rc);
James Smart67d12732012-08-03 12:36:13 -04007242 goto out_destroy_fcp_wq;
James Smartda0436e2009-05-22 14:51:39 -04007243 }
7244 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7245 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
7246 phba->sli4_hba.mbx_cq->queue_id,
James Smart67d12732012-08-03 12:36:13 -04007247 phba->sli4_hba.hba_eq[0]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007248
7249 /* Set up slow-path ELS Complete Queue */
7250 if (!phba->sli4_hba.els_cq) {
7251 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7252 "0530 ELS CQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007253 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04007254 goto out_destroy_mbx_cq;
7255 }
James Smart67d12732012-08-03 12:36:13 -04007256 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq,
7257 phba->sli4_hba.hba_eq[0], LPFC_WCQ, LPFC_ELS);
James Smartda0436e2009-05-22 14:51:39 -04007258 if (rc) {
7259 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7260 "0531 Failed setup of slow-path ELS CQ: "
7261 "rc = 0x%x\n", rc);
7262 goto out_destroy_mbx_cq;
7263 }
7264 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7265 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
7266 phba->sli4_hba.els_cq->queue_id,
James Smart67d12732012-08-03 12:36:13 -04007267 phba->sli4_hba.hba_eq[0]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007268
7269 /*
7270 * Set up all the Work Queues (WQs)
7271 */
7272
7273 /* Set up Mailbox Command Queue */
7274 if (!phba->sli4_hba.mbx_wq) {
7275 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7276 "0538 Slow-path MQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007277 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007278 goto out_destroy_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04007279 }
7280 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
7281 phba->sli4_hba.mbx_cq, LPFC_MBOX);
7282 if (rc) {
7283 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7284 "0539 Failed setup of slow-path MQ: "
7285 "rc = 0x%x\n", rc);
James Smart67d12732012-08-03 12:36:13 -04007286 goto out_destroy_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04007287 }
7288 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7289 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
7290 phba->sli4_hba.mbx_wq->queue_id,
7291 phba->sli4_hba.mbx_cq->queue_id);
7292
7293 /* Set up slow-path ELS Work Queue */
7294 if (!phba->sli4_hba.els_wq) {
7295 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7296 "0536 Slow-path ELS WQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007297 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04007298 goto out_destroy_mbx_wq;
7299 }
7300 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
7301 phba->sli4_hba.els_cq, LPFC_ELS);
7302 if (rc) {
7303 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7304 "0537 Failed setup of slow-path ELS WQ: "
7305 "rc = 0x%x\n", rc);
7306 goto out_destroy_mbx_wq;
7307 }
James Smart2a76a282012-08-03 12:35:54 -04007308
7309 /* Bind this WQ to the ELS ring */
7310 pring = &psli->ring[LPFC_ELS_RING];
7311 pring->sli.sli4.wqp = (void *)phba->sli4_hba.els_wq;
7312 phba->sli4_hba.els_cq->pring = pring;
7313
James Smartda0436e2009-05-22 14:51:39 -04007314 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7315 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
7316 phba->sli4_hba.els_wq->queue_id,
7317 phba->sli4_hba.els_cq->queue_id);
7318
James Smartda0436e2009-05-22 14:51:39 -04007319 /*
7320 * Create Receive Queue (RQ)
7321 */
7322 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
7323 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7324 "0540 Receive Queue not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007325 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007326 goto out_destroy_els_wq;
James Smartda0436e2009-05-22 14:51:39 -04007327 }
James Smart73d91e52011-10-10 21:32:10 -04007328
7329 lpfc_rq_adjust_repost(phba, phba->sli4_hba.hdr_rq, LPFC_ELS_HBQ);
7330 lpfc_rq_adjust_repost(phba, phba->sli4_hba.dat_rq, LPFC_ELS_HBQ);
7331
James Smartda0436e2009-05-22 14:51:39 -04007332 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04007333 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04007334 if (rc) {
7335 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7336 "0541 Failed setup of Receive Queue: "
7337 "rc = 0x%x\n", rc);
7338 goto out_destroy_fcp_wq;
7339 }
James Smart73d91e52011-10-10 21:32:10 -04007340
James Smartda0436e2009-05-22 14:51:39 -04007341 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7342 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
7343 "parent cq-id=%d\n",
7344 phba->sli4_hba.hdr_rq->queue_id,
7345 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04007346 phba->sli4_hba.els_cq->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007347 return 0;
7348
James Smart2e90f4b2011-12-13 13:22:37 -05007349out_destroy_els_wq:
James Smartda0436e2009-05-22 14:51:39 -04007350 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7351out_destroy_mbx_wq:
7352 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
James Smart2e90f4b2011-12-13 13:22:37 -05007353out_destroy_els_cq:
James Smartda0436e2009-05-22 14:51:39 -04007354 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
7355out_destroy_mbx_cq:
7356 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
James Smart67d12732012-08-03 12:36:13 -04007357out_destroy_fcp_wq:
7358 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
7359 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
7360out_destroy_fcp_cq:
7361 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
7362 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
7363out_destroy_hba_eq:
James Smartda0436e2009-05-22 14:51:39 -04007364 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
James Smart67d12732012-08-03 12:36:13 -04007365 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[fcp_eqidx]);
James Smartda0436e2009-05-22 14:51:39 -04007366out_error:
7367 return rc;
7368}
7369
7370/**
7371 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
7372 * @phba: pointer to lpfc hba data structure.
7373 *
7374 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
7375 * operation.
7376 *
7377 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007378 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007379 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007380 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007381 **/
7382void
7383lpfc_sli4_queue_unset(struct lpfc_hba *phba)
7384{
7385 int fcp_qidx;
7386
7387 /* Unset mailbox command work queue */
7388 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
7389 /* Unset ELS work queue */
7390 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7391 /* Unset unsolicited receive queue */
7392 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
7393 /* Unset FCP work queue */
James Smart67d12732012-08-03 12:36:13 -04007394 if (phba->sli4_hba.fcp_wq) {
7395 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
7396 fcp_qidx++)
7397 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
7398 }
James Smartda0436e2009-05-22 14:51:39 -04007399 /* Unset mailbox command complete queue */
7400 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
7401 /* Unset ELS complete queue */
7402 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
James Smartda0436e2009-05-22 14:51:39 -04007403 /* Unset FCP response complete queue */
James Smart2e90f4b2011-12-13 13:22:37 -05007404 if (phba->sli4_hba.fcp_cq) {
James Smart67d12732012-08-03 12:36:13 -04007405 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
7406 fcp_qidx++)
James Smart2e90f4b2011-12-13 13:22:37 -05007407 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05007408 }
James Smartda0436e2009-05-22 14:51:39 -04007409 /* Unset fast-path event queue */
James Smart67d12732012-08-03 12:36:13 -04007410 if (phba->sli4_hba.hba_eq) {
7411 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
James Smart2e90f4b2011-12-13 13:22:37 -05007412 fcp_qidx++)
James Smart67d12732012-08-03 12:36:13 -04007413 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[fcp_qidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05007414 }
James Smartda0436e2009-05-22 14:51:39 -04007415}
7416
7417/**
7418 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
7419 * @phba: pointer to lpfc hba data structure.
7420 *
7421 * This routine is invoked to allocate and set up a pool of completion queue
7422 * events. The body of the completion queue event is a completion queue entry
7423 * CQE. For now, this pool is used for the interrupt service routine to queue
7424 * the following HBA completion queue events for the worker thread to process:
7425 * - Mailbox asynchronous events
7426 * - Receive queue completion unsolicited events
7427 * Later, this can be used for all the slow-path events.
7428 *
7429 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007430 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007431 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04007432 **/
7433static int
7434lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
7435{
7436 struct lpfc_cq_event *cq_event;
7437 int i;
7438
7439 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
7440 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
7441 if (!cq_event)
7442 goto out_pool_create_fail;
7443 list_add_tail(&cq_event->list,
7444 &phba->sli4_hba.sp_cqe_event_pool);
7445 }
7446 return 0;
7447
7448out_pool_create_fail:
7449 lpfc_sli4_cq_event_pool_destroy(phba);
7450 return -ENOMEM;
7451}
7452
7453/**
7454 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
7455 * @phba: pointer to lpfc hba data structure.
7456 *
7457 * This routine is invoked to free the pool of completion queue events at
7458 * driver unload time. Note that, it is the responsibility of the driver
7459 * cleanup routine to free all the outstanding completion-queue events
7460 * allocated from this pool back into the pool before invoking this routine
7461 * to destroy the pool.
7462 **/
7463static void
7464lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
7465{
7466 struct lpfc_cq_event *cq_event, *next_cq_event;
7467
7468 list_for_each_entry_safe(cq_event, next_cq_event,
7469 &phba->sli4_hba.sp_cqe_event_pool, list) {
7470 list_del(&cq_event->list);
7471 kfree(cq_event);
7472 }
7473}
7474
7475/**
7476 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
7477 * @phba: pointer to lpfc hba data structure.
7478 *
7479 * This routine is the lock free version of the API invoked to allocate a
7480 * completion-queue event from the free pool.
7481 *
7482 * Return: Pointer to the newly allocated completion-queue event if successful
7483 * NULL otherwise.
7484 **/
7485struct lpfc_cq_event *
7486__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
7487{
7488 struct lpfc_cq_event *cq_event = NULL;
7489
7490 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
7491 struct lpfc_cq_event, list);
7492 return cq_event;
7493}
7494
7495/**
7496 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
7497 * @phba: pointer to lpfc hba data structure.
7498 *
7499 * This routine is the lock version of the API invoked to allocate a
7500 * completion-queue event from the free pool.
7501 *
7502 * Return: Pointer to the newly allocated completion-queue event if successful
7503 * NULL otherwise.
7504 **/
7505struct lpfc_cq_event *
7506lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
7507{
7508 struct lpfc_cq_event *cq_event;
7509 unsigned long iflags;
7510
7511 spin_lock_irqsave(&phba->hbalock, iflags);
7512 cq_event = __lpfc_sli4_cq_event_alloc(phba);
7513 spin_unlock_irqrestore(&phba->hbalock, iflags);
7514 return cq_event;
7515}
7516
7517/**
7518 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
7519 * @phba: pointer to lpfc hba data structure.
7520 * @cq_event: pointer to the completion queue event to be freed.
7521 *
7522 * This routine is the lock free version of the API invoked to release a
7523 * completion-queue event back into the free pool.
7524 **/
7525void
7526__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
7527 struct lpfc_cq_event *cq_event)
7528{
7529 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
7530}
7531
7532/**
7533 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
7534 * @phba: pointer to lpfc hba data structure.
7535 * @cq_event: pointer to the completion queue event to be freed.
7536 *
7537 * This routine is the lock version of the API invoked to release a
7538 * completion-queue event back into the free pool.
7539 **/
7540void
7541lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
7542 struct lpfc_cq_event *cq_event)
7543{
7544 unsigned long iflags;
7545 spin_lock_irqsave(&phba->hbalock, iflags);
7546 __lpfc_sli4_cq_event_release(phba, cq_event);
7547 spin_unlock_irqrestore(&phba->hbalock, iflags);
7548}
7549
7550/**
7551 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
7552 * @phba: pointer to lpfc hba data structure.
7553 *
7554 * This routine is to free all the pending completion-queue events to the
7555 * back into the free pool for device reset.
7556 **/
7557static void
7558lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
7559{
7560 LIST_HEAD(cqelist);
7561 struct lpfc_cq_event *cqe;
7562 unsigned long iflags;
7563
7564 /* Retrieve all the pending WCQEs from pending WCQE lists */
7565 spin_lock_irqsave(&phba->hbalock, iflags);
7566 /* Pending FCP XRI abort events */
7567 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
7568 &cqelist);
7569 /* Pending ELS XRI abort events */
7570 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
7571 &cqelist);
7572 /* Pending asynnc events */
7573 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
7574 &cqelist);
7575 spin_unlock_irqrestore(&phba->hbalock, iflags);
7576
7577 while (!list_empty(&cqelist)) {
7578 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
7579 lpfc_sli4_cq_event_release(phba, cqe);
7580 }
7581}
7582
7583/**
7584 * lpfc_pci_function_reset - Reset pci function.
7585 * @phba: pointer to lpfc hba data structure.
7586 *
7587 * This routine is invoked to request a PCI function reset. It will destroys
7588 * all resources assigned to the PCI function which originates this request.
7589 *
7590 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007591 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007592 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007593 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007594 **/
7595int
7596lpfc_pci_function_reset(struct lpfc_hba *phba)
7597{
7598 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05007599 uint32_t rc = 0, if_type;
James Smartda0436e2009-05-22 14:51:39 -04007600 uint32_t shdr_status, shdr_add_status;
James Smart2fcee4b2010-12-15 17:57:46 -05007601 uint32_t rdy_chk, num_resets = 0, reset_again = 0;
James Smartda0436e2009-05-22 14:51:39 -04007602 union lpfc_sli4_cfg_shdr *shdr;
James Smart2fcee4b2010-12-15 17:57:46 -05007603 struct lpfc_register reg_data;
James Smart2b81f942012-03-01 22:37:18 -05007604 uint16_t devid;
James Smartda0436e2009-05-22 14:51:39 -04007605
James Smart2fcee4b2010-12-15 17:57:46 -05007606 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7607 switch (if_type) {
7608 case LPFC_SLI_INTF_IF_TYPE_0:
7609 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
7610 GFP_KERNEL);
7611 if (!mboxq) {
7612 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7613 "0494 Unable to allocate memory for "
7614 "issuing SLI_FUNCTION_RESET mailbox "
7615 "command\n");
7616 return -ENOMEM;
7617 }
7618
7619 /* Setup PCI function reset mailbox-ioctl command */
7620 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7621 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
7622 LPFC_SLI4_MBX_EMBED);
7623 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7624 shdr = (union lpfc_sli4_cfg_shdr *)
7625 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7626 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7627 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
7628 &shdr->response);
7629 if (rc != MBX_TIMEOUT)
7630 mempool_free(mboxq, phba->mbox_mem_pool);
7631 if (shdr_status || shdr_add_status || rc) {
7632 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7633 "0495 SLI_FUNCTION_RESET mailbox "
7634 "failed with status x%x add_status x%x,"
7635 " mbx status x%x\n",
7636 shdr_status, shdr_add_status, rc);
7637 rc = -ENXIO;
7638 }
7639 break;
7640 case LPFC_SLI_INTF_IF_TYPE_2:
7641 for (num_resets = 0;
7642 num_resets < MAX_IF_TYPE_2_RESETS;
7643 num_resets++) {
7644 reg_data.word0 = 0;
7645 bf_set(lpfc_sliport_ctrl_end, &reg_data,
7646 LPFC_SLIPORT_LITTLE_ENDIAN);
7647 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
7648 LPFC_SLIPORT_INIT_PORT);
7649 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
7650 CTRLregaddr);
James Smart8fcb8ac2012-03-01 22:35:58 -05007651 /* flush */
James Smart2b81f942012-03-01 22:37:18 -05007652 pci_read_config_word(phba->pcidev,
7653 PCI_DEVICE_ID, &devid);
James Smart2fcee4b2010-12-15 17:57:46 -05007654 /*
7655 * Poll the Port Status Register and wait for RDY for
7656 * up to 10 seconds. If the port doesn't respond, treat
7657 * it as an error. If the port responds with RN, start
7658 * the loop again.
7659 */
7660 for (rdy_chk = 0; rdy_chk < 1000; rdy_chk++) {
James Smart73d91e52011-10-10 21:32:10 -04007661 msleep(10);
James Smart9940b972011-03-11 16:06:12 -05007662 if (lpfc_readl(phba->sli4_hba.u.if_type2.
7663 STATUSregaddr, &reg_data.word0)) {
7664 rc = -ENODEV;
James Smart73d91e52011-10-10 21:32:10 -04007665 goto out;
James Smart9940b972011-03-11 16:06:12 -05007666 }
James Smart6b5151f2012-01-18 16:24:06 -05007667 if (bf_get(lpfc_sliport_status_rn, &reg_data))
7668 reset_again++;
James Smart2fcee4b2010-12-15 17:57:46 -05007669 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
7670 break;
James Smart2fcee4b2010-12-15 17:57:46 -05007671 }
7672
7673 /*
7674 * If the port responds to the init request with
7675 * reset needed, delay for a bit and restart the loop.
7676 */
James Smart6b5151f2012-01-18 16:24:06 -05007677 if (reset_again && (rdy_chk < 1000)) {
James Smart2fcee4b2010-12-15 17:57:46 -05007678 msleep(10);
7679 reset_again = 0;
7680 continue;
7681 }
7682
7683 /* Detect any port errors. */
James Smart2fcee4b2010-12-15 17:57:46 -05007684 if ((bf_get(lpfc_sliport_status_err, &reg_data)) ||
7685 (rdy_chk >= 1000)) {
7686 phba->work_status[0] = readl(
7687 phba->sli4_hba.u.if_type2.ERR1regaddr);
7688 phba->work_status[1] = readl(
7689 phba->sli4_hba.u.if_type2.ERR2regaddr);
7690 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05007691 "2890 Port error detected during port "
James Smart8a9d2e82012-05-09 21:16:12 -04007692 "reset(%d): wait_tmo:%d ms, "
7693 "port status reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05007694 "error 1=0x%x, error 2=0x%x\n",
James Smart8a9d2e82012-05-09 21:16:12 -04007695 num_resets, rdy_chk*10,
7696 reg_data.word0,
James Smart2fcee4b2010-12-15 17:57:46 -05007697 phba->work_status[0],
7698 phba->work_status[1]);
7699 rc = -ENODEV;
7700 }
7701
7702 /*
7703 * Terminate the outer loop provided the Port indicated
7704 * ready within 10 seconds.
7705 */
7706 if (rdy_chk < 1000)
7707 break;
7708 }
James Smart05580562011-05-24 11:40:48 -04007709 /* delay driver action following IF_TYPE_2 function reset */
7710 msleep(100);
James Smart2fcee4b2010-12-15 17:57:46 -05007711 break;
7712 case LPFC_SLI_INTF_IF_TYPE_1:
7713 default:
7714 break;
James Smartda0436e2009-05-22 14:51:39 -04007715 }
7716
James Smart73d91e52011-10-10 21:32:10 -04007717out:
James Smart2fcee4b2010-12-15 17:57:46 -05007718 /* Catch the not-ready port failure after a port reset. */
7719 if (num_resets >= MAX_IF_TYPE_2_RESETS)
7720 rc = -ENODEV;
7721
James Smartda0436e2009-05-22 14:51:39 -04007722 return rc;
7723}
7724
7725/**
James Smartda0436e2009-05-22 14:51:39 -04007726 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
7727 * @phba: pointer to lpfc hba data structure.
7728 *
7729 * This routine is invoked to set up the PCI device memory space for device
7730 * with SLI-4 interface spec.
7731 *
7732 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007733 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007734 * other values - error
7735 **/
7736static int
7737lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
7738{
7739 struct pci_dev *pdev;
7740 unsigned long bar0map_len, bar1map_len, bar2map_len;
7741 int error = -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -05007742 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04007743
7744 /* Obtain PCI device reference */
7745 if (!phba->pcidev)
7746 return error;
7747 else
7748 pdev = phba->pcidev;
7749
7750 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05007751 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
7752 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
7753 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
7754 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smartda0436e2009-05-22 14:51:39 -04007755 return error;
Michael Reed8e685972009-09-18 12:02:05 -05007756 }
7757 }
James Smartda0436e2009-05-22 14:51:39 -04007758
James Smart2fcee4b2010-12-15 17:57:46 -05007759 /*
7760 * The BARs and register set definitions and offset locations are
7761 * dependent on the if_type.
7762 */
7763 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
7764 &phba->sli4_hba.sli_intf.word0)) {
7765 return error;
7766 }
7767
7768 /* There is no SLI3 failback for SLI4 devices. */
7769 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
7770 LPFC_SLI_INTF_VALID) {
7771 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7772 "2894 SLI_INTF reg contents invalid "
7773 "sli_intf reg 0x%x\n",
7774 phba->sli4_hba.sli_intf.word0);
7775 return error;
7776 }
7777
7778 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7779 /*
7780 * Get the bus address of SLI4 device Bar regions and the
7781 * number of bytes required by each mapping. The mapping of the
7782 * particular PCI BARs regions is dependent on the type of
7783 * SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -04007784 */
James Smart1dfb5a42010-02-12 14:40:50 -05007785 if (pci_resource_start(pdev, 0)) {
7786 phba->pci_bar0_map = pci_resource_start(pdev, 0);
7787 bar0map_len = pci_resource_len(pdev, 0);
James Smart2fcee4b2010-12-15 17:57:46 -05007788
7789 /*
7790 * Map SLI4 PCI Config Space Register base to a kernel virtual
7791 * addr
7792 */
7793 phba->sli4_hba.conf_regs_memmap_p =
7794 ioremap(phba->pci_bar0_map, bar0map_len);
7795 if (!phba->sli4_hba.conf_regs_memmap_p) {
7796 dev_printk(KERN_ERR, &pdev->dev,
7797 "ioremap failed for SLI4 PCI config "
7798 "registers.\n");
7799 goto out;
7800 }
7801 /* Set up BAR0 PCI config space register memory map */
7802 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smart1dfb5a42010-02-12 14:40:50 -05007803 } else {
7804 phba->pci_bar0_map = pci_resource_start(pdev, 1);
7805 bar0map_len = pci_resource_len(pdev, 1);
James Smart2fcee4b2010-12-15 17:57:46 -05007806 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
7807 dev_printk(KERN_ERR, &pdev->dev,
7808 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
7809 goto out;
7810 }
7811 phba->sli4_hba.conf_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04007812 ioremap(phba->pci_bar0_map, bar0map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05007813 if (!phba->sli4_hba.conf_regs_memmap_p) {
7814 dev_printk(KERN_ERR, &pdev->dev,
7815 "ioremap failed for SLI4 PCI config "
7816 "registers.\n");
7817 goto out;
7818 }
7819 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smartda0436e2009-05-22 14:51:39 -04007820 }
7821
James Smartc31098c2011-04-16 11:03:33 -04007822 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
7823 (pci_resource_start(pdev, 2))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007824 /*
7825 * Map SLI4 if type 0 HBA Control Register base to a kernel
7826 * virtual address and setup the registers.
7827 */
7828 phba->pci_bar1_map = pci_resource_start(pdev, 2);
7829 bar1map_len = pci_resource_len(pdev, 2);
7830 phba->sli4_hba.ctrl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04007831 ioremap(phba->pci_bar1_map, bar1map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05007832 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
7833 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04007834 "ioremap failed for SLI4 HBA control registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05007835 goto out_iounmap_conf;
7836 }
7837 lpfc_sli4_bar1_register_memmap(phba);
James Smartda0436e2009-05-22 14:51:39 -04007838 }
7839
James Smartc31098c2011-04-16 11:03:33 -04007840 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
7841 (pci_resource_start(pdev, 4))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007842 /*
7843 * Map SLI4 if type 0 HBA Doorbell Register base to a kernel
7844 * virtual address and setup the registers.
7845 */
7846 phba->pci_bar2_map = pci_resource_start(pdev, 4);
7847 bar2map_len = pci_resource_len(pdev, 4);
7848 phba->sli4_hba.drbl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04007849 ioremap(phba->pci_bar2_map, bar2map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05007850 if (!phba->sli4_hba.drbl_regs_memmap_p) {
7851 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04007852 "ioremap failed for SLI4 HBA doorbell registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05007853 goto out_iounmap_ctrl;
7854 }
7855 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
7856 if (error)
7857 goto out_iounmap_all;
James Smartda0436e2009-05-22 14:51:39 -04007858 }
7859
James Smartda0436e2009-05-22 14:51:39 -04007860 return 0;
7861
7862out_iounmap_all:
7863 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
7864out_iounmap_ctrl:
7865 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
7866out_iounmap_conf:
7867 iounmap(phba->sli4_hba.conf_regs_memmap_p);
7868out:
7869 return error;
7870}
7871
7872/**
7873 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
7874 * @phba: pointer to lpfc hba data structure.
7875 *
7876 * This routine is invoked to unset the PCI device memory space for device
7877 * with SLI-4 interface spec.
7878 **/
7879static void
7880lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
7881{
James Smart2e90f4b2011-12-13 13:22:37 -05007882 uint32_t if_type;
7883 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smartda0436e2009-05-22 14:51:39 -04007884
James Smart2e90f4b2011-12-13 13:22:37 -05007885 switch (if_type) {
7886 case LPFC_SLI_INTF_IF_TYPE_0:
7887 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
7888 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
7889 iounmap(phba->sli4_hba.conf_regs_memmap_p);
7890 break;
7891 case LPFC_SLI_INTF_IF_TYPE_2:
7892 iounmap(phba->sli4_hba.conf_regs_memmap_p);
7893 break;
7894 case LPFC_SLI_INTF_IF_TYPE_1:
7895 default:
7896 dev_printk(KERN_ERR, &phba->pcidev->dev,
7897 "FATAL - unsupported SLI4 interface type - %d\n",
7898 if_type);
7899 break;
7900 }
James Smartda0436e2009-05-22 14:51:39 -04007901}
7902
7903/**
James Smart3772a992009-05-22 14:50:54 -04007904 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
7905 * @phba: pointer to lpfc hba data structure.
7906 *
7907 * This routine is invoked to enable the MSI-X interrupt vectors to device
7908 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
7909 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
7910 * invoked, enables either all or nothing, depending on the current
7911 * availability of PCI vector resources. The device driver is responsible
7912 * for calling the individual request_irq() to register each MSI-X vector
7913 * with a interrupt handler, which is done in this function. Note that
7914 * later when device is unloading, the driver should always call free_irq()
7915 * on all MSI-X vectors it has done request_irq() on before calling
7916 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
7917 * will be left with MSI-X enabled and leaks its vectors.
7918 *
7919 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007920 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007921 * other values - error
7922 **/
7923static int
7924lpfc_sli_enable_msix(struct lpfc_hba *phba)
7925{
7926 int rc, i;
7927 LPFC_MBOXQ_t *pmb;
7928
7929 /* Set up MSI-X multi-message vectors */
7930 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
7931 phba->msix_entries[i].entry = i;
7932
7933 /* Configure MSI-X capability structure */
7934 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
7935 ARRAY_SIZE(phba->msix_entries));
7936 if (rc) {
7937 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7938 "0420 PCI enable MSI-X failed (%d)\n", rc);
7939 goto msi_fail_out;
7940 }
7941 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
7942 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7943 "0477 MSI-X entry[%d]: vector=x%x "
7944 "message=%d\n", i,
7945 phba->msix_entries[i].vector,
7946 phba->msix_entries[i].entry);
7947 /*
7948 * Assign MSI-X vectors to interrupt handlers
7949 */
7950
7951 /* vector-0 is associated to slow-path handler */
7952 rc = request_irq(phba->msix_entries[0].vector,
7953 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
7954 LPFC_SP_DRIVER_HANDLER_NAME, phba);
7955 if (rc) {
7956 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7957 "0421 MSI-X slow-path request_irq failed "
7958 "(%d)\n", rc);
7959 goto msi_fail_out;
7960 }
7961
7962 /* vector-1 is associated to fast-path handler */
7963 rc = request_irq(phba->msix_entries[1].vector,
7964 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
7965 LPFC_FP_DRIVER_HANDLER_NAME, phba);
7966
7967 if (rc) {
7968 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7969 "0429 MSI-X fast-path request_irq failed "
7970 "(%d)\n", rc);
7971 goto irq_fail_out;
7972 }
7973
7974 /*
7975 * Configure HBA MSI-X attention conditions to messages
7976 */
7977 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7978
7979 if (!pmb) {
7980 rc = -ENOMEM;
7981 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7982 "0474 Unable to allocate memory for issuing "
7983 "MBOX_CONFIG_MSI command\n");
7984 goto mem_fail_out;
7985 }
7986 rc = lpfc_config_msi(phba, pmb);
7987 if (rc)
7988 goto mbx_fail_out;
7989 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
7990 if (rc != MBX_SUCCESS) {
7991 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
7992 "0351 Config MSI mailbox command failed, "
7993 "mbxCmd x%x, mbxStatus x%x\n",
7994 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
7995 goto mbx_fail_out;
7996 }
7997
7998 /* Free memory allocated for mailbox command */
7999 mempool_free(pmb, phba->mbox_mem_pool);
8000 return rc;
8001
8002mbx_fail_out:
8003 /* Free memory allocated for mailbox command */
8004 mempool_free(pmb, phba->mbox_mem_pool);
8005
8006mem_fail_out:
8007 /* free the irq already requested */
8008 free_irq(phba->msix_entries[1].vector, phba);
8009
8010irq_fail_out:
8011 /* free the irq already requested */
8012 free_irq(phba->msix_entries[0].vector, phba);
8013
8014msi_fail_out:
8015 /* Unconfigure MSI-X capability structure */
8016 pci_disable_msix(phba->pcidev);
8017 return rc;
8018}
8019
8020/**
8021 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
8022 * @phba: pointer to lpfc hba data structure.
8023 *
8024 * This routine is invoked to release the MSI-X vectors and then disable the
8025 * MSI-X interrupt mode to device with SLI-3 interface spec.
8026 **/
8027static void
8028lpfc_sli_disable_msix(struct lpfc_hba *phba)
8029{
8030 int i;
8031
8032 /* Free up MSI-X multi-message vectors */
8033 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8034 free_irq(phba->msix_entries[i].vector, phba);
8035 /* Disable MSI-X */
8036 pci_disable_msix(phba->pcidev);
8037
8038 return;
8039}
8040
8041/**
8042 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
8043 * @phba: pointer to lpfc hba data structure.
8044 *
8045 * This routine is invoked to enable the MSI interrupt mode to device with
8046 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
8047 * enable the MSI vector. The device driver is responsible for calling the
8048 * request_irq() to register MSI vector with a interrupt the handler, which
8049 * is done in this function.
8050 *
8051 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008052 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008053 * other values - error
8054 */
8055static int
8056lpfc_sli_enable_msi(struct lpfc_hba *phba)
8057{
8058 int rc;
8059
8060 rc = pci_enable_msi(phba->pcidev);
8061 if (!rc)
8062 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8063 "0462 PCI enable MSI mode success.\n");
8064 else {
8065 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8066 "0471 PCI enable MSI mode failed (%d)\n", rc);
8067 return rc;
8068 }
8069
8070 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
8071 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8072 if (rc) {
8073 pci_disable_msi(phba->pcidev);
8074 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8075 "0478 MSI request_irq failed (%d)\n", rc);
8076 }
8077 return rc;
8078}
8079
8080/**
8081 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
8082 * @phba: pointer to lpfc hba data structure.
8083 *
8084 * This routine is invoked to disable the MSI interrupt mode to device with
8085 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
8086 * done request_irq() on before calling pci_disable_msi(). Failure to do so
8087 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
8088 * its vector.
8089 */
8090static void
8091lpfc_sli_disable_msi(struct lpfc_hba *phba)
8092{
8093 free_irq(phba->pcidev->irq, phba);
8094 pci_disable_msi(phba->pcidev);
8095 return;
8096}
8097
8098/**
8099 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
8100 * @phba: pointer to lpfc hba data structure.
8101 *
8102 * This routine is invoked to enable device interrupt and associate driver's
8103 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
8104 * spec. Depends on the interrupt mode configured to the driver, the driver
8105 * will try to fallback from the configured interrupt mode to an interrupt
8106 * mode which is supported by the platform, kernel, and device in the order
8107 * of:
8108 * MSI-X -> MSI -> IRQ.
8109 *
8110 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008111 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008112 * other values - error
8113 **/
8114static uint32_t
8115lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
8116{
8117 uint32_t intr_mode = LPFC_INTR_ERROR;
8118 int retval;
8119
8120 if (cfg_mode == 2) {
8121 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
8122 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
8123 if (!retval) {
8124 /* Now, try to enable MSI-X interrupt mode */
8125 retval = lpfc_sli_enable_msix(phba);
8126 if (!retval) {
8127 /* Indicate initialization to MSI-X mode */
8128 phba->intr_type = MSIX;
8129 intr_mode = 2;
8130 }
8131 }
8132 }
8133
8134 /* Fallback to MSI if MSI-X initialization failed */
8135 if (cfg_mode >= 1 && phba->intr_type == NONE) {
8136 retval = lpfc_sli_enable_msi(phba);
8137 if (!retval) {
8138 /* Indicate initialization to MSI mode */
8139 phba->intr_type = MSI;
8140 intr_mode = 1;
8141 }
8142 }
8143
8144 /* Fallback to INTx if both MSI-X/MSI initalization failed */
8145 if (phba->intr_type == NONE) {
8146 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
8147 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8148 if (!retval) {
8149 /* Indicate initialization to INTx mode */
8150 phba->intr_type = INTx;
8151 intr_mode = 0;
8152 }
8153 }
8154 return intr_mode;
8155}
8156
8157/**
8158 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
8159 * @phba: pointer to lpfc hba data structure.
8160 *
8161 * This routine is invoked to disable device interrupt and disassociate the
8162 * driver's interrupt handler(s) from interrupt vector(s) to device with
8163 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
8164 * release the interrupt vector(s) for the message signaled interrupt.
8165 **/
8166static void
8167lpfc_sli_disable_intr(struct lpfc_hba *phba)
8168{
8169 /* Disable the currently initialized interrupt mode */
8170 if (phba->intr_type == MSIX)
8171 lpfc_sli_disable_msix(phba);
8172 else if (phba->intr_type == MSI)
8173 lpfc_sli_disable_msi(phba);
8174 else if (phba->intr_type == INTx)
8175 free_irq(phba->pcidev->irq, phba);
8176
8177 /* Reset interrupt management states */
8178 phba->intr_type = NONE;
8179 phba->sli.slistat.sli_intr = 0;
8180
8181 return;
8182}
8183
8184/**
James Smartda0436e2009-05-22 14:51:39 -04008185 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
8186 * @phba: pointer to lpfc hba data structure.
8187 *
8188 * This routine is invoked to enable the MSI-X interrupt vectors to device
8189 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
8190 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
8191 * enables either all or nothing, depending on the current availability of
8192 * PCI vector resources. The device driver is responsible for calling the
8193 * individual request_irq() to register each MSI-X vector with a interrupt
8194 * handler, which is done in this function. Note that later when device is
8195 * unloading, the driver should always call free_irq() on all MSI-X vectors
8196 * it has done request_irq() on before calling pci_disable_msix(). Failure
8197 * to do so results in a BUG_ON() and a device will be left with MSI-X
8198 * enabled and leaks its vectors.
8199 *
8200 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008201 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008202 * other values - error
8203 **/
8204static int
8205lpfc_sli4_enable_msix(struct lpfc_hba *phba)
8206{
James Smart75baf692010-06-08 18:31:21 -04008207 int vectors, rc, index;
James Smartda0436e2009-05-22 14:51:39 -04008208
8209 /* Set up MSI-X multi-message vectors */
James Smart82c3e9b2012-09-29 11:29:50 -04008210 for (index = 0; index < phba->cfg_fcp_io_channel; index++)
James Smartda0436e2009-05-22 14:51:39 -04008211 phba->sli4_hba.msix_entries[index].entry = index;
8212
8213 /* Configure MSI-X capability structure */
James Smart82c3e9b2012-09-29 11:29:50 -04008214 vectors = phba->cfg_fcp_io_channel;
James Smart75baf692010-06-08 18:31:21 -04008215enable_msix_vectors:
James Smartda0436e2009-05-22 14:51:39 -04008216 rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
James Smart75baf692010-06-08 18:31:21 -04008217 vectors);
8218 if (rc > 1) {
8219 vectors = rc;
8220 goto enable_msix_vectors;
8221 } else if (rc) {
James Smartda0436e2009-05-22 14:51:39 -04008222 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8223 "0484 PCI enable MSI-X failed (%d)\n", rc);
8224 goto msi_fail_out;
8225 }
James Smart75baf692010-06-08 18:31:21 -04008226
James Smartda0436e2009-05-22 14:51:39 -04008227 /* Log MSI-X vector assignment */
James Smart75baf692010-06-08 18:31:21 -04008228 for (index = 0; index < vectors; index++)
James Smartda0436e2009-05-22 14:51:39 -04008229 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8230 "0489 MSI-X entry[%d]: vector=x%x "
8231 "message=%d\n", index,
8232 phba->sli4_hba.msix_entries[index].vector,
8233 phba->sli4_hba.msix_entries[index].entry);
James Smart67d12732012-08-03 12:36:13 -04008234
James Smartda0436e2009-05-22 14:51:39 -04008235 /*
8236 * Assign MSI-X vectors to interrupt handlers
8237 */
James Smart67d12732012-08-03 12:36:13 -04008238 for (index = 0; index < vectors; index++) {
James Smart4305f182012-08-03 12:36:33 -04008239 memset(&phba->sli4_hba.handler_name[index], 0, 16);
8240 sprintf((char *)&phba->sli4_hba.handler_name[index],
8241 LPFC_DRIVER_HANDLER_NAME"%d", index);
James Smartda0436e2009-05-22 14:51:39 -04008242
James Smart67d12732012-08-03 12:36:13 -04008243 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8244 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
James Smartba20c852012-08-03 12:36:52 -04008245 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].fcp_eq_in_use, 1);
James Smartda0436e2009-05-22 14:51:39 -04008246 rc = request_irq(phba->sli4_hba.msix_entries[index].vector,
James Smart67d12732012-08-03 12:36:13 -04008247 &lpfc_sli4_hba_intr_handler, IRQF_SHARED,
James Smart4305f182012-08-03 12:36:33 -04008248 (char *)&phba->sli4_hba.handler_name[index],
James Smart67d12732012-08-03 12:36:13 -04008249 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -04008250 if (rc) {
8251 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8252 "0486 MSI-X fast-path (%d) "
8253 "request_irq failed (%d)\n", index, rc);
8254 goto cfg_fail_out;
8255 }
8256 }
8257
James Smart82c3e9b2012-09-29 11:29:50 -04008258 if (vectors != phba->cfg_fcp_io_channel) {
8259 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8260 "3238 Reducing IO channels to match number of "
8261 "MSI-X vectors, requested %d got %d\n",
8262 phba->cfg_fcp_io_channel, vectors);
8263 phba->cfg_fcp_io_channel = vectors;
8264 }
James Smartda0436e2009-05-22 14:51:39 -04008265 return rc;
8266
8267cfg_fail_out:
8268 /* free the irq already requested */
James Smart67d12732012-08-03 12:36:13 -04008269 for (--index; index >= 0; index--)
8270 free_irq(phba->sli4_hba.msix_entries[index].vector,
8271 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -04008272
8273msi_fail_out:
8274 /* Unconfigure MSI-X capability structure */
8275 pci_disable_msix(phba->pcidev);
8276 return rc;
8277}
8278
8279/**
8280 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
8281 * @phba: pointer to lpfc hba data structure.
8282 *
8283 * This routine is invoked to release the MSI-X vectors and then disable the
8284 * MSI-X interrupt mode to device with SLI-4 interface spec.
8285 **/
8286static void
8287lpfc_sli4_disable_msix(struct lpfc_hba *phba)
8288{
8289 int index;
8290
8291 /* Free up MSI-X multi-message vectors */
James Smart82c3e9b2012-09-29 11:29:50 -04008292 for (index = 0; index < phba->cfg_fcp_io_channel; index++)
James Smartda0436e2009-05-22 14:51:39 -04008293 free_irq(phba->sli4_hba.msix_entries[index].vector,
James Smart67d12732012-08-03 12:36:13 -04008294 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smart75baf692010-06-08 18:31:21 -04008295
James Smartda0436e2009-05-22 14:51:39 -04008296 /* Disable MSI-X */
8297 pci_disable_msix(phba->pcidev);
8298
8299 return;
8300}
8301
8302/**
8303 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
8304 * @phba: pointer to lpfc hba data structure.
8305 *
8306 * This routine is invoked to enable the MSI interrupt mode to device with
8307 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
8308 * to enable the MSI vector. The device driver is responsible for calling
8309 * the request_irq() to register MSI vector with a interrupt the handler,
8310 * which is done in this function.
8311 *
8312 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008313 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008314 * other values - error
8315 **/
8316static int
8317lpfc_sli4_enable_msi(struct lpfc_hba *phba)
8318{
8319 int rc, index;
8320
8321 rc = pci_enable_msi(phba->pcidev);
8322 if (!rc)
8323 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8324 "0487 PCI enable MSI mode success.\n");
8325 else {
8326 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8327 "0488 PCI enable MSI mode failed (%d)\n", rc);
8328 return rc;
8329 }
8330
8331 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
8332 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8333 if (rc) {
8334 pci_disable_msi(phba->pcidev);
8335 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8336 "0490 MSI request_irq failed (%d)\n", rc);
James Smart75baf692010-06-08 18:31:21 -04008337 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008338 }
8339
James Smart67d12732012-08-03 12:36:13 -04008340 for (index = 0; index < phba->cfg_fcp_io_channel; index++) {
James Smartda0436e2009-05-22 14:51:39 -04008341 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8342 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
8343 }
8344
James Smart75baf692010-06-08 18:31:21 -04008345 return 0;
James Smartda0436e2009-05-22 14:51:39 -04008346}
8347
8348/**
8349 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
8350 * @phba: pointer to lpfc hba data structure.
8351 *
8352 * This routine is invoked to disable the MSI interrupt mode to device with
8353 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
8354 * done request_irq() on before calling pci_disable_msi(). Failure to do so
8355 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
8356 * its vector.
8357 **/
8358static void
8359lpfc_sli4_disable_msi(struct lpfc_hba *phba)
8360{
8361 free_irq(phba->pcidev->irq, phba);
8362 pci_disable_msi(phba->pcidev);
8363 return;
8364}
8365
8366/**
8367 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
8368 * @phba: pointer to lpfc hba data structure.
8369 *
8370 * This routine is invoked to enable device interrupt and associate driver's
8371 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
8372 * interface spec. Depends on the interrupt mode configured to the driver,
8373 * the driver will try to fallback from the configured interrupt mode to an
8374 * interrupt mode which is supported by the platform, kernel, and device in
8375 * the order of:
8376 * MSI-X -> MSI -> IRQ.
8377 *
8378 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008379 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008380 * other values - error
8381 **/
8382static uint32_t
8383lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
8384{
8385 uint32_t intr_mode = LPFC_INTR_ERROR;
8386 int retval, index;
8387
8388 if (cfg_mode == 2) {
8389 /* Preparation before conf_msi mbox cmd */
8390 retval = 0;
8391 if (!retval) {
8392 /* Now, try to enable MSI-X interrupt mode */
8393 retval = lpfc_sli4_enable_msix(phba);
8394 if (!retval) {
8395 /* Indicate initialization to MSI-X mode */
8396 phba->intr_type = MSIX;
8397 intr_mode = 2;
8398 }
8399 }
8400 }
8401
8402 /* Fallback to MSI if MSI-X initialization failed */
8403 if (cfg_mode >= 1 && phba->intr_type == NONE) {
8404 retval = lpfc_sli4_enable_msi(phba);
8405 if (!retval) {
8406 /* Indicate initialization to MSI mode */
8407 phba->intr_type = MSI;
8408 intr_mode = 1;
8409 }
8410 }
8411
8412 /* Fallback to INTx if both MSI-X/MSI initalization failed */
8413 if (phba->intr_type == NONE) {
8414 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
8415 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8416 if (!retval) {
8417 /* Indicate initialization to INTx mode */
8418 phba->intr_type = INTx;
8419 intr_mode = 0;
James Smart67d12732012-08-03 12:36:13 -04008420 for (index = 0; index < phba->cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04008421 index++) {
8422 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8423 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
James Smartba20c852012-08-03 12:36:52 -04008424 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].
8425 fcp_eq_in_use, 1);
James Smartda0436e2009-05-22 14:51:39 -04008426 }
8427 }
8428 }
8429 return intr_mode;
8430}
8431
8432/**
8433 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
8434 * @phba: pointer to lpfc hba data structure.
8435 *
8436 * This routine is invoked to disable device interrupt and disassociate
8437 * the driver's interrupt handler(s) from interrupt vector(s) to device
8438 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
8439 * will release the interrupt vector(s) for the message signaled interrupt.
8440 **/
8441static void
8442lpfc_sli4_disable_intr(struct lpfc_hba *phba)
8443{
8444 /* Disable the currently initialized interrupt mode */
8445 if (phba->intr_type == MSIX)
8446 lpfc_sli4_disable_msix(phba);
8447 else if (phba->intr_type == MSI)
8448 lpfc_sli4_disable_msi(phba);
8449 else if (phba->intr_type == INTx)
8450 free_irq(phba->pcidev->irq, phba);
8451
8452 /* Reset interrupt management states */
8453 phba->intr_type = NONE;
8454 phba->sli.slistat.sli_intr = 0;
8455
8456 return;
8457}
8458
8459/**
James Smart3772a992009-05-22 14:50:54 -04008460 * lpfc_unset_hba - Unset SLI3 hba device initialization
8461 * @phba: pointer to lpfc hba data structure.
8462 *
8463 * This routine is invoked to unset the HBA device initialization steps to
8464 * a device with SLI-3 interface spec.
8465 **/
8466static void
8467lpfc_unset_hba(struct lpfc_hba *phba)
8468{
8469 struct lpfc_vport *vport = phba->pport;
8470 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
8471
8472 spin_lock_irq(shost->host_lock);
8473 vport->load_flag |= FC_UNLOADING;
8474 spin_unlock_irq(shost->host_lock);
8475
James Smart72859902012-01-18 16:25:38 -05008476 kfree(phba->vpi_bmask);
8477 kfree(phba->vpi_ids);
8478
James Smart3772a992009-05-22 14:50:54 -04008479 lpfc_stop_hba_timers(phba);
8480
8481 phba->pport->work_port_events = 0;
8482
8483 lpfc_sli_hba_down(phba);
8484
8485 lpfc_sli_brdrestart(phba);
8486
8487 lpfc_sli_disable_intr(phba);
8488
8489 return;
8490}
8491
8492/**
James Smart5af5eee2010-10-22 11:06:38 -04008493 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
8494 * @phba: Pointer to HBA context object.
8495 *
8496 * This function is called in the SLI4 code path to wait for completion
8497 * of device's XRIs exchange busy. It will check the XRI exchange busy
8498 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
8499 * that, it will check the XRI exchange busy on outstanding FCP and ELS
8500 * I/Os every 30 seconds, log error message, and wait forever. Only when
8501 * all XRI exchange busy complete, the driver unload shall proceed with
8502 * invoking the function reset ioctl mailbox command to the CNA and the
8503 * the rest of the driver unload resource release.
8504 **/
8505static void
8506lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
8507{
8508 int wait_time = 0;
8509 int fcp_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
8510 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
8511
8512 while (!fcp_xri_cmpl || !els_xri_cmpl) {
8513 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
8514 if (!fcp_xri_cmpl)
8515 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8516 "2877 FCP XRI exchange busy "
8517 "wait time: %d seconds.\n",
8518 wait_time/1000);
8519 if (!els_xri_cmpl)
8520 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8521 "2878 ELS XRI exchange busy "
8522 "wait time: %d seconds.\n",
8523 wait_time/1000);
8524 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
8525 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
8526 } else {
8527 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
8528 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
8529 }
8530 fcp_xri_cmpl =
8531 list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
8532 els_xri_cmpl =
8533 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
8534 }
8535}
8536
8537/**
James Smartda0436e2009-05-22 14:51:39 -04008538 * lpfc_sli4_hba_unset - Unset the fcoe hba
8539 * @phba: Pointer to HBA context object.
8540 *
8541 * This function is called in the SLI4 code path to reset the HBA's FCoE
8542 * function. The caller is not required to hold any lock. This routine
8543 * issues PCI function reset mailbox command to reset the FCoE function.
8544 * At the end of the function, it calls lpfc_hba_down_post function to
8545 * free any pending commands.
8546 **/
8547static void
8548lpfc_sli4_hba_unset(struct lpfc_hba *phba)
8549{
8550 int wait_cnt = 0;
8551 LPFC_MBOXQ_t *mboxq;
James Smart912e3ac2011-05-24 11:42:11 -04008552 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -04008553
8554 lpfc_stop_hba_timers(phba);
8555 phba->sli4_hba.intr_enable = 0;
8556
8557 /*
8558 * Gracefully wait out the potential current outstanding asynchronous
8559 * mailbox command.
8560 */
8561
8562 /* First, block any pending async mailbox command from posted */
8563 spin_lock_irq(&phba->hbalock);
8564 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
8565 spin_unlock_irq(&phba->hbalock);
8566 /* Now, trying to wait it out if we can */
8567 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8568 msleep(10);
8569 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
8570 break;
8571 }
8572 /* Forcefully release the outstanding mailbox command if timed out */
8573 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8574 spin_lock_irq(&phba->hbalock);
8575 mboxq = phba->sli.mbox_active;
8576 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8577 __lpfc_mbox_cmpl_put(phba, mboxq);
8578 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8579 phba->sli.mbox_active = NULL;
8580 spin_unlock_irq(&phba->hbalock);
8581 }
8582
James Smart5af5eee2010-10-22 11:06:38 -04008583 /* Abort all iocbs associated with the hba */
8584 lpfc_sli_hba_iocb_abort(phba);
8585
8586 /* Wait for completion of device XRI exchange busy */
8587 lpfc_sli4_xri_exchange_busy_wait(phba);
8588
James Smartda0436e2009-05-22 14:51:39 -04008589 /* Disable PCI subsystem interrupt */
8590 lpfc_sli4_disable_intr(phba);
8591
James Smart912e3ac2011-05-24 11:42:11 -04008592 /* Disable SR-IOV if enabled */
8593 if (phba->cfg_sriov_nr_virtfn)
8594 pci_disable_sriov(pdev);
8595
James Smartda0436e2009-05-22 14:51:39 -04008596 /* Stop kthread signal shall trigger work_done one more time */
8597 kthread_stop(phba->worker_thread);
8598
James Smart3677a3a2010-09-29 11:19:14 -04008599 /* Reset SLI4 HBA FCoE function */
8600 lpfc_pci_function_reset(phba);
James Smart5350d872011-10-10 21:33:49 -04008601 lpfc_sli4_queue_destroy(phba);
James Smart3677a3a2010-09-29 11:19:14 -04008602
James Smartda0436e2009-05-22 14:51:39 -04008603 /* Stop the SLI4 device port */
8604 phba->pport->work_port_events = 0;
8605}
8606
James Smart28baac72010-02-12 14:42:03 -05008607 /**
8608 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
8609 * @phba: Pointer to HBA context object.
8610 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
8611 *
8612 * This function is called in the SLI4 code path to read the port's
8613 * sli4 capabilities.
8614 *
8615 * This function may be be called from any context that can block-wait
8616 * for the completion. The expectation is that this routine is called
8617 * typically from probe_one or from the online routine.
8618 **/
8619int
8620lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8621{
8622 int rc;
8623 struct lpfc_mqe *mqe;
8624 struct lpfc_pc_sli4_params *sli4_params;
8625 uint32_t mbox_tmo;
8626
8627 rc = 0;
8628 mqe = &mboxq->u.mqe;
8629
8630 /* Read the port's SLI4 Parameters port capabilities */
James Smartfedd3b72011-02-16 12:39:24 -05008631 lpfc_pc_sli4_params(mboxq);
James Smart28baac72010-02-12 14:42:03 -05008632 if (!phba->sli4_hba.intr_enable)
8633 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8634 else {
James Smarta183a152011-10-10 21:32:43 -04008635 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart28baac72010-02-12 14:42:03 -05008636 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
8637 }
8638
8639 if (unlikely(rc))
8640 return 1;
8641
8642 sli4_params = &phba->sli4_hba.pc_sli4_params;
8643 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
8644 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
8645 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
8646 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
8647 &mqe->un.sli4_params);
8648 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
8649 &mqe->un.sli4_params);
8650 sli4_params->proto_types = mqe->un.sli4_params.word3;
8651 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
8652 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
8653 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
8654 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
8655 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
8656 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
8657 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
8658 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
8659 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
8660 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
8661 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
8662 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
8663 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
8664 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
8665 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
8666 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
8667 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
8668 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
8669 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
8670 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
James Smart05580562011-05-24 11:40:48 -04008671
8672 /* Make sure that sge_supp_len can be handled by the driver */
8673 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
8674 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
8675
James Smart28baac72010-02-12 14:42:03 -05008676 return rc;
8677}
8678
James Smartda0436e2009-05-22 14:51:39 -04008679/**
James Smartfedd3b72011-02-16 12:39:24 -05008680 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
8681 * @phba: Pointer to HBA context object.
8682 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
8683 *
8684 * This function is called in the SLI4 code path to read the port's
8685 * sli4 capabilities.
8686 *
8687 * This function may be be called from any context that can block-wait
8688 * for the completion. The expectation is that this routine is called
8689 * typically from probe_one or from the online routine.
8690 **/
8691int
8692lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8693{
8694 int rc;
8695 struct lpfc_mqe *mqe = &mboxq->u.mqe;
8696 struct lpfc_pc_sli4_params *sli4_params;
James Smarta183a152011-10-10 21:32:43 -04008697 uint32_t mbox_tmo;
James Smartfedd3b72011-02-16 12:39:24 -05008698 int length;
8699 struct lpfc_sli4_parameters *mbx_sli4_parameters;
8700
James Smart6d368e52011-05-24 11:44:12 -04008701 /*
8702 * By default, the driver assumes the SLI4 port requires RPI
8703 * header postings. The SLI4_PARAM response will correct this
8704 * assumption.
8705 */
8706 phba->sli4_hba.rpi_hdrs_in_use = 1;
8707
James Smartfedd3b72011-02-16 12:39:24 -05008708 /* Read the port's SLI4 Config Parameters */
8709 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
8710 sizeof(struct lpfc_sli4_cfg_mhdr));
8711 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
8712 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
8713 length, LPFC_SLI4_MBX_EMBED);
8714 if (!phba->sli4_hba.intr_enable)
8715 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -04008716 else {
8717 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
8718 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
8719 }
James Smartfedd3b72011-02-16 12:39:24 -05008720 if (unlikely(rc))
8721 return rc;
8722 sli4_params = &phba->sli4_hba.pc_sli4_params;
8723 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
8724 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
8725 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
8726 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
8727 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
8728 mbx_sli4_parameters);
8729 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
8730 mbx_sli4_parameters);
8731 if (bf_get(cfg_phwq, mbx_sli4_parameters))
8732 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
8733 else
8734 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
8735 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
8736 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
8737 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
8738 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
8739 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
8740 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
8741 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
8742 mbx_sli4_parameters);
8743 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
8744 mbx_sli4_parameters);
James Smart6d368e52011-05-24 11:44:12 -04008745 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
8746 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
James Smart05580562011-05-24 11:40:48 -04008747
8748 /* Make sure that sge_supp_len can be handled by the driver */
8749 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
8750 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
8751
James Smartfedd3b72011-02-16 12:39:24 -05008752 return 0;
8753}
8754
8755/**
James Smart3772a992009-05-22 14:50:54 -04008756 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
8757 * @pdev: pointer to PCI device
8758 * @pid: pointer to PCI device identifier
8759 *
8760 * This routine is to be called to attach a device with SLI-3 interface spec
8761 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
8762 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
8763 * information of the device and driver to see if the driver state that it can
8764 * support this kind of device. If the match is successful, the driver core
8765 * invokes this routine. If this routine determines it can claim the HBA, it
8766 * does all the initialization that it needs to do to handle the HBA properly.
8767 *
8768 * Return code
8769 * 0 - driver can claim the device
8770 * negative value - driver can not claim the device
8771 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008772static int
James Smart3772a992009-05-22 14:50:54 -04008773lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
8774{
8775 struct lpfc_hba *phba;
8776 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04008777 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -04008778 int error;
8779 uint32_t cfg_mode, intr_mode;
8780
8781 /* Allocate memory for HBA structure */
8782 phba = lpfc_hba_alloc(pdev);
8783 if (!phba)
8784 return -ENOMEM;
8785
8786 /* Perform generic PCI device enabling operation */
8787 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -04008788 if (error)
James Smart3772a992009-05-22 14:50:54 -04008789 goto out_free_phba;
James Smart3772a992009-05-22 14:50:54 -04008790
8791 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
8792 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
8793 if (error)
8794 goto out_disable_pci_dev;
8795
8796 /* Set up SLI-3 specific device PCI memory space */
8797 error = lpfc_sli_pci_mem_setup(phba);
8798 if (error) {
8799 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8800 "1402 Failed to set up pci memory space.\n");
8801 goto out_disable_pci_dev;
8802 }
8803
8804 /* Set up phase-1 common device driver resources */
8805 error = lpfc_setup_driver_resource_phase1(phba);
8806 if (error) {
8807 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8808 "1403 Failed to set up driver resource.\n");
8809 goto out_unset_pci_mem_s3;
8810 }
8811
8812 /* Set up SLI-3 specific device driver resources */
8813 error = lpfc_sli_driver_resource_setup(phba);
8814 if (error) {
8815 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8816 "1404 Failed to set up driver resource.\n");
8817 goto out_unset_pci_mem_s3;
8818 }
8819
8820 /* Initialize and populate the iocb list per host */
8821 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
8822 if (error) {
8823 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8824 "1405 Failed to initialize iocb list.\n");
8825 goto out_unset_driver_resource_s3;
8826 }
8827
8828 /* Set up common device driver resources */
8829 error = lpfc_setup_driver_resource_phase2(phba);
8830 if (error) {
8831 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8832 "1406 Failed to set up driver resource.\n");
8833 goto out_free_iocb_list;
8834 }
8835
James Smart079b5c92011-08-21 21:48:49 -04008836 /* Get the default values for Model Name and Description */
8837 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
8838
James Smart3772a992009-05-22 14:50:54 -04008839 /* Create SCSI host to the physical port */
8840 error = lpfc_create_shost(phba);
8841 if (error) {
8842 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8843 "1407 Failed to create scsi host.\n");
8844 goto out_unset_driver_resource;
8845 }
8846
8847 /* Configure sysfs attributes */
8848 vport = phba->pport;
8849 error = lpfc_alloc_sysfs_attr(vport);
8850 if (error) {
8851 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8852 "1476 Failed to allocate sysfs attr\n");
8853 goto out_destroy_shost;
8854 }
8855
James Smart6669f9b2009-10-02 15:16:45 -04008856 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -04008857 /* Now, trying to enable interrupt and bring up the device */
8858 cfg_mode = phba->cfg_use_msi;
8859 while (true) {
8860 /* Put device to a known state before enabling interrupt */
8861 lpfc_stop_port(phba);
8862 /* Configure and enable interrupt */
8863 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
8864 if (intr_mode == LPFC_INTR_ERROR) {
8865 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8866 "0431 Failed to enable interrupt.\n");
8867 error = -ENODEV;
8868 goto out_free_sysfs_attr;
8869 }
8870 /* SLI-3 HBA setup */
8871 if (lpfc_sli_hba_setup(phba)) {
8872 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8873 "1477 Failed to set up hba\n");
8874 error = -ENODEV;
8875 goto out_remove_device;
8876 }
8877
8878 /* Wait 50ms for the interrupts of previous mailbox commands */
8879 msleep(50);
8880 /* Check active interrupts on message signaled interrupts */
8881 if (intr_mode == 0 ||
8882 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
8883 /* Log the current active interrupt mode */
8884 phba->intr_mode = intr_mode;
8885 lpfc_log_intr_mode(phba, intr_mode);
8886 break;
8887 } else {
8888 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8889 "0447 Configure interrupt mode (%d) "
8890 "failed active interrupt test.\n",
8891 intr_mode);
8892 /* Disable the current interrupt mode */
8893 lpfc_sli_disable_intr(phba);
8894 /* Try next level of interrupt mode */
8895 cfg_mode = --intr_mode;
8896 }
8897 }
8898
8899 /* Perform post initialization setup */
8900 lpfc_post_init_setup(phba);
8901
8902 /* Check if there are static vports to be created. */
8903 lpfc_create_static_vport(phba);
8904
8905 return 0;
8906
8907out_remove_device:
8908 lpfc_unset_hba(phba);
8909out_free_sysfs_attr:
8910 lpfc_free_sysfs_attr(vport);
8911out_destroy_shost:
8912 lpfc_destroy_shost(phba);
8913out_unset_driver_resource:
8914 lpfc_unset_driver_resource_phase2(phba);
8915out_free_iocb_list:
8916 lpfc_free_iocb_list(phba);
8917out_unset_driver_resource_s3:
8918 lpfc_sli_driver_resource_unset(phba);
8919out_unset_pci_mem_s3:
8920 lpfc_sli_pci_mem_unset(phba);
8921out_disable_pci_dev:
8922 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04008923 if (shost)
8924 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -04008925out_free_phba:
8926 lpfc_hba_free(phba);
8927 return error;
8928}
8929
8930/**
8931 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -04008932 * @pdev: pointer to PCI device
8933 *
James Smart3772a992009-05-22 14:50:54 -04008934 * This routine is to be called to disattach a device with SLI-3 interface
8935 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
8936 * removed from PCI bus, it performs all the necessary cleanup for the HBA
8937 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -04008938 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008939static void
James Smart3772a992009-05-22 14:50:54 -04008940lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -05008941{
James Smart2e0fef82007-06-17 19:56:36 -05008942 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8943 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -05008944 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -05008945 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -05008946 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05008947 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
8948
James Smart549e55c2007-08-02 11:09:51 -04008949 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04008950 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04008951 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008952
James Smart858c9f62007-06-17 19:56:39 -05008953 lpfc_free_sysfs_attr(vport);
8954
James Smarteada2722008-12-04 22:39:13 -05008955 /* Release all the vports against this physical port */
8956 vports = lpfc_create_vport_work_array(phba);
8957 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -04008958 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
8959 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
8960 continue;
James Smarteada2722008-12-04 22:39:13 -05008961 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -04008962 }
James Smarteada2722008-12-04 22:39:13 -05008963 lpfc_destroy_vport_work_array(phba, vports);
8964
8965 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -05008966 fc_remove_host(shost);
8967 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04008968 lpfc_cleanup(vport);
8969
James Smart2e0fef82007-06-17 19:56:36 -05008970 /*
8971 * Bring down the SLI Layer. This step disable all interrupts,
8972 * clears the rings, discards all mailbox commands, and resets
8973 * the HBA.
8974 */
James Smarta257bf92009-04-06 18:48:10 -04008975
Justin P. Mattock48e34d02010-12-30 15:07:58 -08008976 /* HBA interrupt will be disabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -05008977 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -04008978 /* Stop kthread signal shall trigger work_done one more time */
8979 kthread_stop(phba->worker_thread);
8980 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -05008981 lpfc_sli_brdrestart(phba);
8982
James Smart72859902012-01-18 16:25:38 -05008983 kfree(phba->vpi_bmask);
8984 kfree(phba->vpi_ids);
8985
James Smart3772a992009-05-22 14:50:54 -04008986 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05008987 spin_lock_irq(&phba->hbalock);
8988 list_del_init(&vport->listentry);
8989 spin_unlock_irq(&phba->hbalock);
8990
James Smart858c9f62007-06-17 19:56:39 -05008991 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05008992
James Smart912e3ac2011-05-24 11:42:11 -04008993 /* Disable SR-IOV if enabled */
8994 if (phba->cfg_sriov_nr_virtfn)
8995 pci_disable_sriov(pdev);
8996
James Smart5b75da22008-12-04 22:39:35 -05008997 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04008998 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -05008999
9000 pci_set_drvdata(pdev, NULL);
James Smart858c9f62007-06-17 19:56:39 -05009001 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05009002
9003 /*
9004 * Call scsi_free before mem_free since scsi bufs are released to their
9005 * corresponding pools here.
9006 */
9007 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -04009008 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -05009009
James Smart34b02dc2008-08-24 21:49:55 -04009010 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
9011 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -05009012
James Smart2e0fef82007-06-17 19:56:36 -05009013 /* Free resources associated with SLI2 interface */
9014 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -04009015 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -05009016
9017 /* unmap adapter SLIM and Control Registers */
9018 iounmap(phba->ctrl_regs_memmap_p);
9019 iounmap(phba->slim_memmap_p);
9020
James Smart3772a992009-05-22 14:50:54 -04009021 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -05009022
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05009023 pci_release_selected_regions(pdev, bars);
James Smart2e0fef82007-06-17 19:56:36 -05009024 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05009025}
9026
Linas Vepstas8d63f372007-02-14 14:28:36 -06009027/**
James Smart3772a992009-05-22 14:50:54 -04009028 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05009029 * @pdev: pointer to PCI device
9030 * @msg: power management message
9031 *
James Smart3772a992009-05-22 14:50:54 -04009032 * This routine is to be called from the kernel's PCI subsystem to support
9033 * system Power Management (PM) to device with SLI-3 interface spec. When
9034 * PM invokes this method, it quiesces the device by stopping the driver's
9035 * worker thread for the device, turning off device's interrupt and DMA,
9036 * and bring the device offline. Note that as the driver implements the
9037 * minimum PM requirements to a power-aware driver's PM support for the
9038 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
9039 * to the suspend() method call will be treated as SUSPEND and the driver will
9040 * fully reinitialize its device during resume() method call, the driver will
9041 * set device to PCI_D3hot state in PCI config space instead of setting it
9042 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -05009043 *
9044 * Return code
James Smart3772a992009-05-22 14:50:54 -04009045 * 0 - driver suspended the device
9046 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05009047 **/
9048static int
James Smart3772a992009-05-22 14:50:54 -04009049lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -05009050{
9051 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9052 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9053
9054 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9055 "0473 PCI device Power Management suspend.\n");
9056
9057 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -04009058 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3a55b532008-12-04 22:38:54 -05009059 lpfc_offline(phba);
9060 kthread_stop(phba->worker_thread);
9061
9062 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -04009063 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -05009064
9065 /* Save device state to PCI config space */
9066 pci_save_state(pdev);
9067 pci_set_power_state(pdev, PCI_D3hot);
9068
9069 return 0;
9070}
9071
9072/**
James Smart3772a992009-05-22 14:50:54 -04009073 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05009074 * @pdev: pointer to PCI device
9075 *
James Smart3772a992009-05-22 14:50:54 -04009076 * This routine is to be called from the kernel's PCI subsystem to support
9077 * system Power Management (PM) to device with SLI-3 interface spec. When PM
9078 * invokes this method, it restores the device's PCI config space state and
9079 * fully reinitializes the device and brings it online. Note that as the
9080 * driver implements the minimum PM requirements to a power-aware driver's
9081 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
9082 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
9083 * driver will fully reinitialize its device during resume() method call,
9084 * the device will be set to PCI_D0 directly in PCI config space before
9085 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -05009086 *
9087 * Return code
James Smart3772a992009-05-22 14:50:54 -04009088 * 0 - driver suspended the device
9089 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05009090 **/
9091static int
James Smart3772a992009-05-22 14:50:54 -04009092lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -05009093{
9094 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9095 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -05009096 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05009097 int error;
9098
9099 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9100 "0452 PCI device Power Management resume.\n");
9101
9102 /* Restore device state from PCI config space */
9103 pci_set_power_state(pdev, PCI_D0);
9104 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -04009105
James Smart1dfb5a42010-02-12 14:40:50 -05009106 /*
9107 * As the new kernel behavior of pci_restore_state() API call clears
9108 * device saved_state flag, need to save the restored state again.
9109 */
9110 pci_save_state(pdev);
9111
James Smart3a55b532008-12-04 22:38:54 -05009112 if (pdev->is_busmaster)
9113 pci_set_master(pdev);
9114
9115 /* Startup the kernel thread for this host adapter. */
9116 phba->worker_thread = kthread_run(lpfc_do_work, phba,
9117 "lpfc_worker_%d", phba->brd_no);
9118 if (IS_ERR(phba->worker_thread)) {
9119 error = PTR_ERR(phba->worker_thread);
9120 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9121 "0434 PM resume failed to start worker "
9122 "thread: error=x%x.\n", error);
9123 return error;
9124 }
9125
James Smart5b75da22008-12-04 22:39:35 -05009126 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009127 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05009128 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -05009129 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -05009130 "0430 PM resume Failed to enable interrupt\n");
9131 return -EIO;
9132 } else
9133 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05009134
9135 /* Restart HBA and bring it online */
9136 lpfc_sli_brdrestart(phba);
9137 lpfc_online(phba);
9138
James Smart5b75da22008-12-04 22:39:35 -05009139 /* Log the current active interrupt mode */
9140 lpfc_log_intr_mode(phba, phba->intr_mode);
9141
James Smart3a55b532008-12-04 22:38:54 -05009142 return 0;
9143}
9144
9145/**
James Smart891478a2009-11-18 15:40:23 -05009146 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
9147 * @phba: pointer to lpfc hba data structure.
9148 *
9149 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -04009150 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -05009151 **/
9152static void
9153lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
9154{
James Smarte2af0d22010-03-15 11:25:32 -04009155 struct lpfc_sli *psli = &phba->sli;
9156 struct lpfc_sli_ring *pring;
9157
James Smart891478a2009-11-18 15:40:23 -05009158 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9159 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -04009160
9161 /*
9162 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
9163 * and let the SCSI mid-layer to retry them to recover.
9164 */
9165 pring = &psli->ring[psli->fcp_ring];
9166 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart891478a2009-11-18 15:40:23 -05009167}
9168
9169/**
James Smart0d878412009-10-02 15:16:56 -04009170 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
9171 * @phba: pointer to lpfc hba data structure.
9172 *
9173 * This routine is called to prepare the SLI3 device for PCI slot reset. It
9174 * disables the device interrupt and pci device, and aborts the internal FCP
9175 * pending I/Os.
9176 **/
9177static void
9178lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
9179{
James Smart0d878412009-10-02 15:16:56 -04009180 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05009181 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -04009182
James Smart75baf692010-06-08 18:31:21 -04009183 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -04009184 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -04009185
James Smarte2af0d22010-03-15 11:25:32 -04009186 /* Block all SCSI devices' I/Os on the host */
9187 lpfc_scsi_dev_block(phba);
9188
9189 /* stop all timers */
9190 lpfc_stop_hba_timers(phba);
9191
James Smart0d878412009-10-02 15:16:56 -04009192 /* Disable interrupt and pci device */
9193 lpfc_sli_disable_intr(phba);
9194 pci_disable_device(phba->pcidev);
James Smart75baf692010-06-08 18:31:21 -04009195
James Smarte2af0d22010-03-15 11:25:32 -04009196 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
9197 lpfc_sli_flush_fcp_rings(phba);
James Smart0d878412009-10-02 15:16:56 -04009198}
9199
9200/**
9201 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
9202 * @phba: pointer to lpfc hba data structure.
9203 *
9204 * This routine is called to prepare the SLI3 device for PCI slot permanently
9205 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
9206 * pending I/Os.
9207 **/
9208static void
James Smart75baf692010-06-08 18:31:21 -04009209lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
James Smart0d878412009-10-02 15:16:56 -04009210{
9211 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05009212 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -04009213 /* Block all SCSI devices' I/Os on the host */
9214 lpfc_scsi_dev_block(phba);
9215
9216 /* stop all timers */
9217 lpfc_stop_hba_timers(phba);
9218
James Smart0d878412009-10-02 15:16:56 -04009219 /* Clean up all driver's outstanding SCSI I/Os */
9220 lpfc_sli_flush_fcp_rings(phba);
9221}
9222
9223/**
James Smart3772a992009-05-22 14:50:54 -04009224 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -04009225 * @pdev: pointer to PCI device.
9226 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009227 *
James Smart3772a992009-05-22 14:50:54 -04009228 * This routine is called from the PCI subsystem for I/O error handling to
9229 * device with SLI-3 interface spec. This function is called by the PCI
9230 * subsystem after a PCI bus error affecting this device has been detected.
9231 * When this function is invoked, it will need to stop all the I/Os and
9232 * interrupt(s) to the device. Once that is done, it will return
9233 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
9234 * as desired.
James Smarte59058c2008-08-24 21:49:00 -04009235 *
9236 * Return codes
James Smart0d878412009-10-02 15:16:56 -04009237 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -04009238 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
9239 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -04009240 **/
James Smart3772a992009-05-22 14:50:54 -04009241static pci_ers_result_t
9242lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009243{
James Smart51ef4c22007-08-02 11:10:31 -04009244 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9245 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009246
James Smart0d878412009-10-02 15:16:56 -04009247 switch (state) {
9248 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -05009249 /* Non-fatal error, prepare for recovery */
9250 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -04009251 return PCI_ERS_RESULT_CAN_RECOVER;
9252 case pci_channel_io_frozen:
9253 /* Fatal error, prepare for slot reset */
9254 lpfc_sli_prep_dev_for_reset(phba);
9255 return PCI_ERS_RESULT_NEED_RESET;
9256 case pci_channel_io_perm_failure:
9257 /* Permanent failure, prepare for device down */
James Smart75baf692010-06-08 18:31:21 -04009258 lpfc_sli_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009259 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -04009260 default:
9261 /* Unknown state, prepare and request slot reset */
9262 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9263 "0472 Unknown PCI error state: x%x\n", state);
9264 lpfc_sli_prep_dev_for_reset(phba);
9265 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -04009266 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06009267}
9268
9269/**
James Smart3772a992009-05-22 14:50:54 -04009270 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -04009271 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009272 *
James Smart3772a992009-05-22 14:50:54 -04009273 * This routine is called from the PCI subsystem for error handling to
9274 * device with SLI-3 interface spec. This is called after PCI bus has been
9275 * reset to restart the PCI card from scratch, as if from a cold-boot.
9276 * During the PCI subsystem error recovery, after driver returns
9277 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
9278 * recovery and then call this routine before calling the .resume method
9279 * to recover the device. This function will initialize the HBA device,
9280 * enable the interrupt, but it will just put the HBA to offline state
9281 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -04009282 *
9283 * Return codes
James Smart3772a992009-05-22 14:50:54 -04009284 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
9285 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -06009286 */
James Smart3772a992009-05-22 14:50:54 -04009287static pci_ers_result_t
9288lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009289{
James Smart51ef4c22007-08-02 11:10:31 -04009290 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9291 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009292 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -05009293 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009294
9295 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +11009296 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -06009297 printk(KERN_ERR "lpfc: Cannot re-enable "
9298 "PCI device after reset.\n");
9299 return PCI_ERS_RESULT_DISCONNECT;
9300 }
9301
James Smart97207482008-12-04 22:39:19 -05009302 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05009303
9304 /*
9305 * As the new kernel behavior of pci_restore_state() API call clears
9306 * device saved_state flag, need to save the restored state again.
9307 */
9308 pci_save_state(pdev);
9309
James Smart97207482008-12-04 22:39:19 -05009310 if (pdev->is_busmaster)
9311 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009312
James Smart92d7f7b2007-06-17 19:56:38 -05009313 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04009314 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05009315 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009316
James Smart5b75da22008-12-04 22:39:35 -05009317 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009318 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05009319 if (intr_mode == LPFC_INTR_ERROR) {
9320 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9321 "0427 Cannot re-enable interrupt after "
9322 "slot reset.\n");
9323 return PCI_ERS_RESULT_DISCONNECT;
9324 } else
9325 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009326
James Smart75baf692010-06-08 18:31:21 -04009327 /* Take device offline, it will perform cleanup */
James Smart618a5232012-06-12 13:54:36 -04009328 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009329 lpfc_offline(phba);
9330 lpfc_sli_brdrestart(phba);
9331
James Smart5b75da22008-12-04 22:39:35 -05009332 /* Log the current active interrupt mode */
9333 lpfc_log_intr_mode(phba, phba->intr_mode);
9334
Linas Vepstas8d63f372007-02-14 14:28:36 -06009335 return PCI_ERS_RESULT_RECOVERED;
9336}
9337
9338/**
James Smart3772a992009-05-22 14:50:54 -04009339 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -04009340 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -06009341 *
James Smart3772a992009-05-22 14:50:54 -04009342 * This routine is called from the PCI subsystem for error handling to device
9343 * with SLI-3 interface spec. It is called when kernel error recovery tells
9344 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
9345 * error recovery. After this call, traffic can start to flow from this device
9346 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009347 */
James Smart3772a992009-05-22 14:50:54 -04009348static void
9349lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009350{
James Smart51ef4c22007-08-02 11:10:31 -04009351 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9352 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009353
James Smarte2af0d22010-03-15 11:25:32 -04009354 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -04009355 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -04009356
9357 /* Clean up Advanced Error Reporting (AER) if needed */
9358 if (phba->hba_flag & HBA_AER_ENABLED)
9359 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009360}
9361
James Smart3772a992009-05-22 14:50:54 -04009362/**
James Smartda0436e2009-05-22 14:51:39 -04009363 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
9364 * @phba: pointer to lpfc hba data structure.
9365 *
9366 * returns the number of ELS/CT IOCBs to reserve
9367 **/
9368int
9369lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
9370{
9371 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
9372
James Smartf1126682009-06-10 17:22:44 -04009373 if (phba->sli_rev == LPFC_SLI_REV4) {
9374 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -04009375 return 10;
James Smartf1126682009-06-10 17:22:44 -04009376 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -04009377 return 25;
James Smartf1126682009-06-10 17:22:44 -04009378 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -04009379 return 50;
James Smartf1126682009-06-10 17:22:44 -04009380 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -04009381 return 100;
James Smart8a9d2e82012-05-09 21:16:12 -04009382 else if (max_xri <= 1536)
James Smart6a9c52c2009-10-02 15:16:51 -04009383 return 150;
James Smart8a9d2e82012-05-09 21:16:12 -04009384 else if (max_xri <= 2048)
9385 return 200;
9386 else
9387 return 250;
James Smartf1126682009-06-10 17:22:44 -04009388 } else
9389 return 0;
James Smartda0436e2009-05-22 14:51:39 -04009390}
9391
9392/**
James Smart52d52442011-05-24 11:42:45 -04009393 * lpfc_write_firmware - attempt to write a firmware image to the port
James Smart52d52442011-05-24 11:42:45 -04009394 * @fw: pointer to firmware image returned from request_firmware.
James Smartce396282012-09-29 11:30:56 -04009395 * @phba: pointer to lpfc hba data structure.
James Smart52d52442011-05-24 11:42:45 -04009396 *
James Smart52d52442011-05-24 11:42:45 -04009397 **/
James Smartce396282012-09-29 11:30:56 -04009398static void
9399lpfc_write_firmware(const struct firmware *fw, void *context)
James Smart52d52442011-05-24 11:42:45 -04009400{
James Smartce396282012-09-29 11:30:56 -04009401 struct lpfc_hba *phba = (struct lpfc_hba *)context;
James Smart6b5151f2012-01-18 16:24:06 -05009402 char fwrev[FW_REV_STR_SIZE];
James Smartce396282012-09-29 11:30:56 -04009403 struct lpfc_grp_hdr *image;
James Smart52d52442011-05-24 11:42:45 -04009404 struct list_head dma_buffer_list;
9405 int i, rc = 0;
9406 struct lpfc_dmabuf *dmabuf, *next;
9407 uint32_t offset = 0, temp_offset = 0;
9408
James Smartc71ab862012-10-31 14:44:33 -04009409 /* It can be null in no-wait mode, sanity check */
James Smartce396282012-09-29 11:30:56 -04009410 if (!fw) {
9411 rc = -ENXIO;
9412 goto out;
9413 }
9414 image = (struct lpfc_grp_hdr *)fw->data;
9415
James Smart52d52442011-05-24 11:42:45 -04009416 INIT_LIST_HEAD(&dma_buffer_list);
James Smart079b5c92011-08-21 21:48:49 -04009417 if ((be32_to_cpu(image->magic_number) != LPFC_GROUP_OJECT_MAGIC_NUM) ||
9418 (bf_get_be32(lpfc_grp_hdr_file_type, image) !=
9419 LPFC_FILE_TYPE_GROUP) ||
9420 (bf_get_be32(lpfc_grp_hdr_id, image) != LPFC_FILE_ID_GROUP) ||
9421 (be32_to_cpu(image->size) != fw->size)) {
James Smart52d52442011-05-24 11:42:45 -04009422 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9423 "3022 Invalid FW image found. "
James Smart079b5c92011-08-21 21:48:49 -04009424 "Magic:%x Type:%x ID:%x\n",
9425 be32_to_cpu(image->magic_number),
9426 bf_get_be32(lpfc_grp_hdr_file_type, image),
9427 bf_get_be32(lpfc_grp_hdr_id, image));
James Smartce396282012-09-29 11:30:56 -04009428 rc = -EINVAL;
9429 goto release_out;
James Smart52d52442011-05-24 11:42:45 -04009430 }
9431 lpfc_decode_firmware_rev(phba, fwrev, 1);
James Smart88a2cfb2011-07-22 18:36:33 -04009432 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
James Smart52d52442011-05-24 11:42:45 -04009433 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartce396282012-09-29 11:30:56 -04009434 "3023 Updating Firmware, Current Version:%s "
James Smart52d52442011-05-24 11:42:45 -04009435 "New Version:%s\n",
James Smart88a2cfb2011-07-22 18:36:33 -04009436 fwrev, image->revision);
James Smart52d52442011-05-24 11:42:45 -04009437 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
9438 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
9439 GFP_KERNEL);
9440 if (!dmabuf) {
9441 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -04009442 goto release_out;
James Smart52d52442011-05-24 11:42:45 -04009443 }
9444 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
9445 SLI4_PAGE_SIZE,
9446 &dmabuf->phys,
9447 GFP_KERNEL);
9448 if (!dmabuf->virt) {
9449 kfree(dmabuf);
9450 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -04009451 goto release_out;
James Smart52d52442011-05-24 11:42:45 -04009452 }
9453 list_add_tail(&dmabuf->list, &dma_buffer_list);
9454 }
9455 while (offset < fw->size) {
9456 temp_offset = offset;
9457 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
James Smart079b5c92011-08-21 21:48:49 -04009458 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
James Smart52d52442011-05-24 11:42:45 -04009459 memcpy(dmabuf->virt,
9460 fw->data + temp_offset,
James Smart079b5c92011-08-21 21:48:49 -04009461 fw->size - temp_offset);
9462 temp_offset = fw->size;
James Smart52d52442011-05-24 11:42:45 -04009463 break;
9464 }
James Smart52d52442011-05-24 11:42:45 -04009465 memcpy(dmabuf->virt, fw->data + temp_offset,
9466 SLI4_PAGE_SIZE);
James Smart88a2cfb2011-07-22 18:36:33 -04009467 temp_offset += SLI4_PAGE_SIZE;
James Smart52d52442011-05-24 11:42:45 -04009468 }
9469 rc = lpfc_wr_object(phba, &dma_buffer_list,
9470 (fw->size - offset), &offset);
James Smartce396282012-09-29 11:30:56 -04009471 if (rc)
9472 goto release_out;
James Smart52d52442011-05-24 11:42:45 -04009473 }
9474 rc = offset;
9475 }
James Smartce396282012-09-29 11:30:56 -04009476
9477release_out:
James Smart52d52442011-05-24 11:42:45 -04009478 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
9479 list_del(&dmabuf->list);
9480 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
9481 dmabuf->virt, dmabuf->phys);
9482 kfree(dmabuf);
9483 }
James Smartce396282012-09-29 11:30:56 -04009484 release_firmware(fw);
9485out:
9486 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartc71ab862012-10-31 14:44:33 -04009487 "3024 Firmware update done: %d.\n", rc);
James Smartce396282012-09-29 11:30:56 -04009488 return;
James Smart52d52442011-05-24 11:42:45 -04009489}
9490
9491/**
James Smartc71ab862012-10-31 14:44:33 -04009492 * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
9493 * @phba: pointer to lpfc hba data structure.
9494 *
9495 * This routine is called to perform Linux generic firmware upgrade on device
9496 * that supports such feature.
9497 **/
9498int
9499lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
9500{
9501 uint8_t file_name[ELX_MODEL_NAME_SIZE];
9502 int ret;
9503 const struct firmware *fw;
9504
9505 /* Only supported on SLI4 interface type 2 for now */
9506 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
9507 LPFC_SLI_INTF_IF_TYPE_2)
9508 return -EPERM;
9509
9510 snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
9511
9512 if (fw_upgrade == INT_FW_UPGRADE) {
9513 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
9514 file_name, &phba->pcidev->dev,
9515 GFP_KERNEL, (void *)phba,
9516 lpfc_write_firmware);
9517 } else if (fw_upgrade == RUN_FW_UPGRADE) {
9518 ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
9519 if (!ret)
9520 lpfc_write_firmware(fw, (void *)phba);
9521 } else {
9522 ret = -EINVAL;
9523 }
9524
9525 return ret;
9526}
9527
9528/**
James Smartda0436e2009-05-22 14:51:39 -04009529 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
9530 * @pdev: pointer to PCI device
9531 * @pid: pointer to PCI device identifier
9532 *
9533 * This routine is called from the kernel's PCI subsystem to device with
9534 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
9535 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
9536 * information of the device and driver to see if the driver state that it
9537 * can support this kind of device. If the match is successful, the driver
9538 * core invokes this routine. If this routine determines it can claim the HBA,
9539 * it does all the initialization that it needs to do to handle the HBA
9540 * properly.
9541 *
9542 * Return code
9543 * 0 - driver can claim the device
9544 * negative value - driver can not claim the device
9545 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009546static int
James Smartda0436e2009-05-22 14:51:39 -04009547lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
9548{
9549 struct lpfc_hba *phba;
9550 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04009551 struct Scsi_Host *shost = NULL;
James Smartce396282012-09-29 11:30:56 -04009552 int error, ret;
James Smartda0436e2009-05-22 14:51:39 -04009553 uint32_t cfg_mode, intr_mode;
James Smart67d12732012-08-03 12:36:13 -04009554 int adjusted_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04009555
9556 /* Allocate memory for HBA structure */
9557 phba = lpfc_hba_alloc(pdev);
9558 if (!phba)
9559 return -ENOMEM;
9560
9561 /* Perform generic PCI device enabling operation */
9562 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -04009563 if (error)
James Smartda0436e2009-05-22 14:51:39 -04009564 goto out_free_phba;
James Smartda0436e2009-05-22 14:51:39 -04009565
9566 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
9567 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
9568 if (error)
9569 goto out_disable_pci_dev;
9570
9571 /* Set up SLI-4 specific device PCI memory space */
9572 error = lpfc_sli4_pci_mem_setup(phba);
9573 if (error) {
9574 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9575 "1410 Failed to set up pci memory space.\n");
9576 goto out_disable_pci_dev;
9577 }
9578
9579 /* Set up phase-1 common device driver resources */
9580 error = lpfc_setup_driver_resource_phase1(phba);
9581 if (error) {
9582 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9583 "1411 Failed to set up driver resource.\n");
9584 goto out_unset_pci_mem_s4;
9585 }
9586
9587 /* Set up SLI-4 Specific device driver resources */
9588 error = lpfc_sli4_driver_resource_setup(phba);
9589 if (error) {
9590 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9591 "1412 Failed to set up driver resource.\n");
9592 goto out_unset_pci_mem_s4;
9593 }
9594
9595 /* Initialize and populate the iocb list per host */
James Smart2a9bf3d2010-06-07 15:24:45 -04009596
9597 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9598 "2821 initialize iocb list %d.\n",
9599 phba->cfg_iocb_cnt*1024);
9600 error = lpfc_init_iocb_list(phba, phba->cfg_iocb_cnt*1024);
9601
James Smartda0436e2009-05-22 14:51:39 -04009602 if (error) {
9603 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9604 "1413 Failed to initialize iocb list.\n");
9605 goto out_unset_driver_resource_s4;
9606 }
9607
James Smart19ca7602010-11-20 23:11:55 -05009608 INIT_LIST_HEAD(&phba->active_rrq_list);
James Smart7d791df2011-07-22 18:37:52 -04009609 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
James Smart19ca7602010-11-20 23:11:55 -05009610
James Smartda0436e2009-05-22 14:51:39 -04009611 /* Set up common device driver resources */
9612 error = lpfc_setup_driver_resource_phase2(phba);
9613 if (error) {
9614 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9615 "1414 Failed to set up driver resource.\n");
9616 goto out_free_iocb_list;
9617 }
9618
James Smart079b5c92011-08-21 21:48:49 -04009619 /* Get the default values for Model Name and Description */
9620 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
9621
James Smartda0436e2009-05-22 14:51:39 -04009622 /* Create SCSI host to the physical port */
9623 error = lpfc_create_shost(phba);
9624 if (error) {
9625 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9626 "1415 Failed to create scsi host.\n");
9627 goto out_unset_driver_resource;
9628 }
9629
9630 /* Configure sysfs attributes */
9631 vport = phba->pport;
9632 error = lpfc_alloc_sysfs_attr(vport);
9633 if (error) {
9634 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9635 "1416 Failed to allocate sysfs attr\n");
9636 goto out_destroy_shost;
9637 }
9638
James Smart6669f9b2009-10-02 15:16:45 -04009639 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -04009640 /* Now, trying to enable interrupt and bring up the device */
9641 cfg_mode = phba->cfg_use_msi;
James Smartda0436e2009-05-22 14:51:39 -04009642
James Smart7b15db32013-01-03 15:43:29 -05009643 /* Put device to a known state before enabling interrupt */
9644 lpfc_stop_port(phba);
9645 /* Configure and enable interrupt */
9646 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
9647 if (intr_mode == LPFC_INTR_ERROR) {
9648 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9649 "0426 Failed to enable interrupt.\n");
9650 error = -ENODEV;
9651 goto out_free_sysfs_attr;
James Smartda0436e2009-05-22 14:51:39 -04009652 }
James Smart7b15db32013-01-03 15:43:29 -05009653 /* Default to single EQ for non-MSI-X */
9654 if (phba->intr_type != MSIX)
9655 adjusted_fcp_io_channel = 1;
9656 else
9657 adjusted_fcp_io_channel = phba->cfg_fcp_io_channel;
9658 phba->cfg_fcp_io_channel = adjusted_fcp_io_channel;
9659 /* Set up SLI-4 HBA */
9660 if (lpfc_sli4_hba_setup(phba)) {
9661 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9662 "1421 Failed to set up hba\n");
9663 error = -ENODEV;
9664 goto out_disable_intr;
9665 }
9666
9667 /* Log the current active interrupt mode */
9668 phba->intr_mode = intr_mode;
9669 lpfc_log_intr_mode(phba, intr_mode);
James Smartda0436e2009-05-22 14:51:39 -04009670
9671 /* Perform post initialization setup */
9672 lpfc_post_init_setup(phba);
9673
James Smartc71ab862012-10-31 14:44:33 -04009674 /* check for firmware upgrade or downgrade */
9675 if (phba->cfg_request_firmware_upgrade)
9676 ret = lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
James Smart52d52442011-05-24 11:42:45 -04009677
James Smart1c6834a2009-07-19 10:01:26 -04009678 /* Check if there are static vports to be created. */
9679 lpfc_create_static_vport(phba);
James Smartda0436e2009-05-22 14:51:39 -04009680 return 0;
9681
9682out_disable_intr:
9683 lpfc_sli4_disable_intr(phba);
9684out_free_sysfs_attr:
9685 lpfc_free_sysfs_attr(vport);
9686out_destroy_shost:
9687 lpfc_destroy_shost(phba);
9688out_unset_driver_resource:
9689 lpfc_unset_driver_resource_phase2(phba);
9690out_free_iocb_list:
9691 lpfc_free_iocb_list(phba);
9692out_unset_driver_resource_s4:
9693 lpfc_sli4_driver_resource_unset(phba);
9694out_unset_pci_mem_s4:
9695 lpfc_sli4_pci_mem_unset(phba);
9696out_disable_pci_dev:
9697 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04009698 if (shost)
9699 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -04009700out_free_phba:
9701 lpfc_hba_free(phba);
9702 return error;
9703}
9704
9705/**
9706 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
9707 * @pdev: pointer to PCI device
9708 *
9709 * This routine is called from the kernel's PCI subsystem to device with
9710 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
9711 * removed from PCI bus, it performs all the necessary cleanup for the HBA
9712 * device to be removed from the PCI subsystem properly.
9713 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009714static void
James Smartda0436e2009-05-22 14:51:39 -04009715lpfc_pci_remove_one_s4(struct pci_dev *pdev)
9716{
9717 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9718 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
9719 struct lpfc_vport **vports;
9720 struct lpfc_hba *phba = vport->phba;
9721 int i;
9722
9723 /* Mark the device unloading flag */
9724 spin_lock_irq(&phba->hbalock);
9725 vport->load_flag |= FC_UNLOADING;
9726 spin_unlock_irq(&phba->hbalock);
9727
9728 /* Free the HBA sysfs attributes */
9729 lpfc_free_sysfs_attr(vport);
9730
9731 /* Release all the vports against this physical port */
9732 vports = lpfc_create_vport_work_array(phba);
9733 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -04009734 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
9735 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
9736 continue;
James Smartda0436e2009-05-22 14:51:39 -04009737 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -04009738 }
James Smartda0436e2009-05-22 14:51:39 -04009739 lpfc_destroy_vport_work_array(phba, vports);
9740
9741 /* Remove FC host and then SCSI host with the physical port */
9742 fc_remove_host(shost);
9743 scsi_remove_host(shost);
9744
9745 /* Perform cleanup on the physical port */
9746 lpfc_cleanup(vport);
9747
9748 /*
9749 * Bring down the SLI Layer. This step disables all interrupts,
9750 * clears the rings, discards all mailbox commands, and resets
9751 * the HBA FCoE function.
9752 */
9753 lpfc_debugfs_terminate(vport);
9754 lpfc_sli4_hba_unset(phba);
9755
9756 spin_lock_irq(&phba->hbalock);
9757 list_del_init(&vport->listentry);
9758 spin_unlock_irq(&phba->hbalock);
9759
James Smart3677a3a2010-09-29 11:19:14 -04009760 /* Perform scsi free before driver resource_unset since scsi
James Smartda0436e2009-05-22 14:51:39 -04009761 * buffers are released to their corresponding pools here.
9762 */
9763 lpfc_scsi_free(phba);
James Smart67d12732012-08-03 12:36:13 -04009764
James Smartda0436e2009-05-22 14:51:39 -04009765 lpfc_sli4_driver_resource_unset(phba);
9766
9767 /* Unmap adapter Control and Doorbell registers */
9768 lpfc_sli4_pci_mem_unset(phba);
9769
9770 /* Release PCI resources and disable device's PCI function */
9771 scsi_host_put(shost);
9772 lpfc_disable_pci_dev(phba);
9773
9774 /* Finally, free the driver's device data structure */
9775 lpfc_hba_free(phba);
9776
9777 return;
9778}
9779
9780/**
9781 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
9782 * @pdev: pointer to PCI device
9783 * @msg: power management message
9784 *
9785 * This routine is called from the kernel's PCI subsystem to support system
9786 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
9787 * this method, it quiesces the device by stopping the driver's worker
9788 * thread for the device, turning off device's interrupt and DMA, and bring
9789 * the device offline. Note that as the driver implements the minimum PM
9790 * requirements to a power-aware driver's PM support for suspend/resume -- all
9791 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
9792 * method call will be treated as SUSPEND and the driver will fully
9793 * reinitialize its device during resume() method call, the driver will set
9794 * device to PCI_D3hot state in PCI config space instead of setting it
9795 * according to the @msg provided by the PM.
9796 *
9797 * Return code
9798 * 0 - driver suspended the device
9799 * Error otherwise
9800 **/
9801static int
9802lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
9803{
9804 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9805 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9806
9807 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart75baf692010-06-08 18:31:21 -04009808 "2843 PCI device Power Management suspend.\n");
James Smartda0436e2009-05-22 14:51:39 -04009809
9810 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -04009811 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smartda0436e2009-05-22 14:51:39 -04009812 lpfc_offline(phba);
9813 kthread_stop(phba->worker_thread);
9814
9815 /* Disable interrupt from device */
9816 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -04009817 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04009818
9819 /* Save device state to PCI config space */
9820 pci_save_state(pdev);
9821 pci_set_power_state(pdev, PCI_D3hot);
9822
9823 return 0;
9824}
9825
9826/**
9827 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
9828 * @pdev: pointer to PCI device
9829 *
9830 * This routine is called from the kernel's PCI subsystem to support system
9831 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
9832 * this method, it restores the device's PCI config space state and fully
9833 * reinitializes the device and brings it online. Note that as the driver
9834 * implements the minimum PM requirements to a power-aware driver's PM for
9835 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
9836 * to the suspend() method call will be treated as SUSPEND and the driver
9837 * will fully reinitialize its device during resume() method call, the device
9838 * will be set to PCI_D0 directly in PCI config space before restoring the
9839 * state.
9840 *
9841 * Return code
9842 * 0 - driver suspended the device
9843 * Error otherwise
9844 **/
9845static int
9846lpfc_pci_resume_one_s4(struct pci_dev *pdev)
9847{
9848 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9849 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9850 uint32_t intr_mode;
9851 int error;
9852
9853 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9854 "0292 PCI device Power Management resume.\n");
9855
9856 /* Restore device state from PCI config space */
9857 pci_set_power_state(pdev, PCI_D0);
9858 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05009859
9860 /*
9861 * As the new kernel behavior of pci_restore_state() API call clears
9862 * device saved_state flag, need to save the restored state again.
9863 */
9864 pci_save_state(pdev);
9865
James Smartda0436e2009-05-22 14:51:39 -04009866 if (pdev->is_busmaster)
9867 pci_set_master(pdev);
9868
9869 /* Startup the kernel thread for this host adapter. */
9870 phba->worker_thread = kthread_run(lpfc_do_work, phba,
9871 "lpfc_worker_%d", phba->brd_no);
9872 if (IS_ERR(phba->worker_thread)) {
9873 error = PTR_ERR(phba->worker_thread);
9874 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9875 "0293 PM resume failed to start worker "
9876 "thread: error=x%x.\n", error);
9877 return error;
9878 }
9879
9880 /* Configure and enable interrupt */
9881 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
9882 if (intr_mode == LPFC_INTR_ERROR) {
9883 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9884 "0294 PM resume Failed to enable interrupt\n");
9885 return -EIO;
9886 } else
9887 phba->intr_mode = intr_mode;
9888
9889 /* Restart HBA and bring it online */
9890 lpfc_sli_brdrestart(phba);
9891 lpfc_online(phba);
9892
9893 /* Log the current active interrupt mode */
9894 lpfc_log_intr_mode(phba, phba->intr_mode);
9895
9896 return 0;
9897}
9898
9899/**
James Smart75baf692010-06-08 18:31:21 -04009900 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
9901 * @phba: pointer to lpfc hba data structure.
9902 *
9903 * This routine is called to prepare the SLI4 device for PCI slot recover. It
9904 * aborts all the outstanding SCSI I/Os to the pci device.
9905 **/
9906static void
9907lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
9908{
9909 struct lpfc_sli *psli = &phba->sli;
9910 struct lpfc_sli_ring *pring;
9911
9912 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9913 "2828 PCI channel I/O abort preparing for recovery\n");
9914 /*
9915 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
9916 * and let the SCSI mid-layer to retry them to recover.
9917 */
9918 pring = &psli->ring[psli->fcp_ring];
9919 lpfc_sli_abort_iocb_ring(phba, pring);
9920}
9921
9922/**
9923 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
9924 * @phba: pointer to lpfc hba data structure.
9925 *
9926 * This routine is called to prepare the SLI4 device for PCI slot reset. It
9927 * disables the device interrupt and pci device, and aborts the internal FCP
9928 * pending I/Os.
9929 **/
9930static void
9931lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
9932{
9933 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9934 "2826 PCI channel disable preparing for reset\n");
9935
9936 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -04009937 lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
James Smart75baf692010-06-08 18:31:21 -04009938
9939 /* Block all SCSI devices' I/Os on the host */
9940 lpfc_scsi_dev_block(phba);
9941
9942 /* stop all timers */
9943 lpfc_stop_hba_timers(phba);
9944
9945 /* Disable interrupt and pci device */
9946 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -04009947 lpfc_sli4_queue_destroy(phba);
James Smart75baf692010-06-08 18:31:21 -04009948 pci_disable_device(phba->pcidev);
9949
9950 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
9951 lpfc_sli_flush_fcp_rings(phba);
9952}
9953
9954/**
9955 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
9956 * @phba: pointer to lpfc hba data structure.
9957 *
9958 * This routine is called to prepare the SLI4 device for PCI slot permanently
9959 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
9960 * pending I/Os.
9961 **/
9962static void
9963lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
9964{
9965 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9966 "2827 PCI channel permanent disable for failure\n");
9967
9968 /* Block all SCSI devices' I/Os on the host */
9969 lpfc_scsi_dev_block(phba);
9970
9971 /* stop all timers */
9972 lpfc_stop_hba_timers(phba);
9973
9974 /* Clean up all driver's outstanding SCSI I/Os */
9975 lpfc_sli_flush_fcp_rings(phba);
9976}
9977
9978/**
James Smartda0436e2009-05-22 14:51:39 -04009979 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
9980 * @pdev: pointer to PCI device.
9981 * @state: the current PCI connection state.
9982 *
9983 * This routine is called from the PCI subsystem for error handling to device
9984 * with SLI-4 interface spec. This function is called by the PCI subsystem
9985 * after a PCI bus error affecting this device has been detected. When this
9986 * function is invoked, it will need to stop all the I/Os and interrupt(s)
9987 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
9988 * for the PCI subsystem to perform proper recovery as desired.
9989 *
9990 * Return codes
9991 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
9992 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
9993 **/
9994static pci_ers_result_t
9995lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
9996{
James Smart75baf692010-06-08 18:31:21 -04009997 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9998 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9999
10000 switch (state) {
10001 case pci_channel_io_normal:
10002 /* Non-fatal error, prepare for recovery */
10003 lpfc_sli4_prep_dev_for_recover(phba);
10004 return PCI_ERS_RESULT_CAN_RECOVER;
10005 case pci_channel_io_frozen:
10006 /* Fatal error, prepare for slot reset */
10007 lpfc_sli4_prep_dev_for_reset(phba);
10008 return PCI_ERS_RESULT_NEED_RESET;
10009 case pci_channel_io_perm_failure:
10010 /* Permanent failure, prepare for device down */
10011 lpfc_sli4_prep_dev_for_perm_failure(phba);
10012 return PCI_ERS_RESULT_DISCONNECT;
10013 default:
10014 /* Unknown state, prepare and request slot reset */
10015 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10016 "2825 Unknown PCI error state: x%x\n", state);
10017 lpfc_sli4_prep_dev_for_reset(phba);
10018 return PCI_ERS_RESULT_NEED_RESET;
10019 }
James Smartda0436e2009-05-22 14:51:39 -040010020}
10021
10022/**
10023 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
10024 * @pdev: pointer to PCI device.
10025 *
10026 * This routine is called from the PCI subsystem for error handling to device
10027 * with SLI-4 interface spec. It is called after PCI bus has been reset to
10028 * restart the PCI card from scratch, as if from a cold-boot. During the
10029 * PCI subsystem error recovery, after the driver returns
10030 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
10031 * recovery and then call this routine before calling the .resume method to
10032 * recover the device. This function will initialize the HBA device, enable
10033 * the interrupt, but it will just put the HBA to offline state without
10034 * passing any I/O traffic.
10035 *
10036 * Return codes
10037 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10038 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10039 */
10040static pci_ers_result_t
10041lpfc_io_slot_reset_s4(struct pci_dev *pdev)
10042{
James Smart75baf692010-06-08 18:31:21 -040010043 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10044 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10045 struct lpfc_sli *psli = &phba->sli;
10046 uint32_t intr_mode;
10047
10048 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
10049 if (pci_enable_device_mem(pdev)) {
10050 printk(KERN_ERR "lpfc: Cannot re-enable "
10051 "PCI device after reset.\n");
10052 return PCI_ERS_RESULT_DISCONNECT;
10053 }
10054
10055 pci_restore_state(pdev);
James Smart0a96e972011-07-22 18:37:28 -040010056
10057 /*
10058 * As the new kernel behavior of pci_restore_state() API call clears
10059 * device saved_state flag, need to save the restored state again.
10060 */
10061 pci_save_state(pdev);
10062
James Smart75baf692010-06-08 18:31:21 -040010063 if (pdev->is_busmaster)
10064 pci_set_master(pdev);
10065
10066 spin_lock_irq(&phba->hbalock);
10067 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
10068 spin_unlock_irq(&phba->hbalock);
10069
10070 /* Configure and enable interrupt */
10071 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
10072 if (intr_mode == LPFC_INTR_ERROR) {
10073 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10074 "2824 Cannot re-enable interrupt after "
10075 "slot reset.\n");
10076 return PCI_ERS_RESULT_DISCONNECT;
10077 } else
10078 phba->intr_mode = intr_mode;
10079
10080 /* Log the current active interrupt mode */
10081 lpfc_log_intr_mode(phba, phba->intr_mode);
10082
James Smartda0436e2009-05-22 14:51:39 -040010083 return PCI_ERS_RESULT_RECOVERED;
10084}
10085
10086/**
10087 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
10088 * @pdev: pointer to PCI device
10089 *
10090 * This routine is called from the PCI subsystem for error handling to device
10091 * with SLI-4 interface spec. It is called when kernel error recovery tells
10092 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
10093 * error recovery. After this call, traffic can start to flow from this device
10094 * again.
10095 **/
10096static void
10097lpfc_io_resume_s4(struct pci_dev *pdev)
10098{
James Smart75baf692010-06-08 18:31:21 -040010099 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10100 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10101
10102 /*
10103 * In case of slot reset, as function reset is performed through
10104 * mailbox command which needs DMA to be enabled, this operation
10105 * has to be moved to the io resume phase. Taking device offline
10106 * will perform the necessary cleanup.
10107 */
10108 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
10109 /* Perform device reset */
James Smart618a5232012-06-12 13:54:36 -040010110 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040010111 lpfc_offline(phba);
10112 lpfc_sli_brdrestart(phba);
10113 /* Bring the device back online */
10114 lpfc_online(phba);
10115 }
10116
10117 /* Clean up Advanced Error Reporting (AER) if needed */
10118 if (phba->hba_flag & HBA_AER_ENABLED)
10119 pci_cleanup_aer_uncorrect_error_status(pdev);
James Smartda0436e2009-05-22 14:51:39 -040010120}
10121
10122/**
James Smart3772a992009-05-22 14:50:54 -040010123 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
10124 * @pdev: pointer to PCI device
10125 * @pid: pointer to PCI device identifier
10126 *
10127 * This routine is to be registered to the kernel's PCI subsystem. When an
10128 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
10129 * at PCI device-specific information of the device and driver to see if the
10130 * driver state that it can support this kind of device. If the match is
10131 * successful, the driver core invokes this routine. This routine dispatches
10132 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
10133 * do all the initialization that it needs to do to handle the HBA device
10134 * properly.
10135 *
10136 * Return code
10137 * 0 - driver can claim the device
10138 * negative value - driver can not claim the device
10139 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010140static int
James Smart3772a992009-05-22 14:50:54 -040010141lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
10142{
10143 int rc;
James Smart8fa38512009-07-19 10:01:03 -040010144 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -040010145
James Smart28baac72010-02-12 14:42:03 -050010146 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -040010147 return -ENODEV;
10148
James Smart8fa38512009-07-19 10:01:03 -040010149 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -050010150 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -040010151 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040010152 else
James Smart3772a992009-05-22 14:50:54 -040010153 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040010154
James Smart3772a992009-05-22 14:50:54 -040010155 return rc;
10156}
10157
10158/**
10159 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
10160 * @pdev: pointer to PCI device
10161 *
10162 * This routine is to be registered to the kernel's PCI subsystem. When an
10163 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
10164 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
10165 * remove routine, which will perform all the necessary cleanup for the
10166 * device to be removed from the PCI subsystem properly.
10167 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010168static void
James Smart3772a992009-05-22 14:50:54 -040010169lpfc_pci_remove_one(struct pci_dev *pdev)
10170{
10171 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10172 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10173
10174 switch (phba->pci_dev_grp) {
10175 case LPFC_PCI_DEV_LP:
10176 lpfc_pci_remove_one_s3(pdev);
10177 break;
James Smartda0436e2009-05-22 14:51:39 -040010178 case LPFC_PCI_DEV_OC:
10179 lpfc_pci_remove_one_s4(pdev);
10180 break;
James Smart3772a992009-05-22 14:50:54 -040010181 default:
10182 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10183 "1424 Invalid PCI device group: 0x%x\n",
10184 phba->pci_dev_grp);
10185 break;
10186 }
10187 return;
10188}
10189
10190/**
10191 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
10192 * @pdev: pointer to PCI device
10193 * @msg: power management message
10194 *
10195 * This routine is to be registered to the kernel's PCI subsystem to support
10196 * system Power Management (PM). When PM invokes this method, it dispatches
10197 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
10198 * suspend the device.
10199 *
10200 * Return code
10201 * 0 - driver suspended the device
10202 * Error otherwise
10203 **/
10204static int
10205lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
10206{
10207 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10208 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10209 int rc = -ENODEV;
10210
10211 switch (phba->pci_dev_grp) {
10212 case LPFC_PCI_DEV_LP:
10213 rc = lpfc_pci_suspend_one_s3(pdev, msg);
10214 break;
James Smartda0436e2009-05-22 14:51:39 -040010215 case LPFC_PCI_DEV_OC:
10216 rc = lpfc_pci_suspend_one_s4(pdev, msg);
10217 break;
James Smart3772a992009-05-22 14:50:54 -040010218 default:
10219 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10220 "1425 Invalid PCI device group: 0x%x\n",
10221 phba->pci_dev_grp);
10222 break;
10223 }
10224 return rc;
10225}
10226
10227/**
10228 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
10229 * @pdev: pointer to PCI device
10230 *
10231 * This routine is to be registered to the kernel's PCI subsystem to support
10232 * system Power Management (PM). When PM invokes this method, it dispatches
10233 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
10234 * resume the device.
10235 *
10236 * Return code
10237 * 0 - driver suspended the device
10238 * Error otherwise
10239 **/
10240static int
10241lpfc_pci_resume_one(struct pci_dev *pdev)
10242{
10243 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10244 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10245 int rc = -ENODEV;
10246
10247 switch (phba->pci_dev_grp) {
10248 case LPFC_PCI_DEV_LP:
10249 rc = lpfc_pci_resume_one_s3(pdev);
10250 break;
James Smartda0436e2009-05-22 14:51:39 -040010251 case LPFC_PCI_DEV_OC:
10252 rc = lpfc_pci_resume_one_s4(pdev);
10253 break;
James Smart3772a992009-05-22 14:50:54 -040010254 default:
10255 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10256 "1426 Invalid PCI device group: 0x%x\n",
10257 phba->pci_dev_grp);
10258 break;
10259 }
10260 return rc;
10261}
10262
10263/**
10264 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
10265 * @pdev: pointer to PCI device.
10266 * @state: the current PCI connection state.
10267 *
10268 * This routine is registered to the PCI subsystem for error handling. This
10269 * function is called by the PCI subsystem after a PCI bus error affecting
10270 * this device has been detected. When this routine is invoked, it dispatches
10271 * the action to the proper SLI-3 or SLI-4 device error detected handling
10272 * routine, which will perform the proper error detected operation.
10273 *
10274 * Return codes
10275 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10276 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10277 **/
10278static pci_ers_result_t
10279lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
10280{
10281 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10282 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10283 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
10284
10285 switch (phba->pci_dev_grp) {
10286 case LPFC_PCI_DEV_LP:
10287 rc = lpfc_io_error_detected_s3(pdev, state);
10288 break;
James Smartda0436e2009-05-22 14:51:39 -040010289 case LPFC_PCI_DEV_OC:
10290 rc = lpfc_io_error_detected_s4(pdev, state);
10291 break;
James Smart3772a992009-05-22 14:50:54 -040010292 default:
10293 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10294 "1427 Invalid PCI device group: 0x%x\n",
10295 phba->pci_dev_grp);
10296 break;
10297 }
10298 return rc;
10299}
10300
10301/**
10302 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
10303 * @pdev: pointer to PCI device.
10304 *
10305 * This routine is registered to the PCI subsystem for error handling. This
10306 * function is called after PCI bus has been reset to restart the PCI card
10307 * from scratch, as if from a cold-boot. When this routine is invoked, it
10308 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
10309 * routine, which will perform the proper device reset.
10310 *
10311 * Return codes
10312 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10313 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10314 **/
10315static pci_ers_result_t
10316lpfc_io_slot_reset(struct pci_dev *pdev)
10317{
10318 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10319 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10320 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
10321
10322 switch (phba->pci_dev_grp) {
10323 case LPFC_PCI_DEV_LP:
10324 rc = lpfc_io_slot_reset_s3(pdev);
10325 break;
James Smartda0436e2009-05-22 14:51:39 -040010326 case LPFC_PCI_DEV_OC:
10327 rc = lpfc_io_slot_reset_s4(pdev);
10328 break;
James Smart3772a992009-05-22 14:50:54 -040010329 default:
10330 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10331 "1428 Invalid PCI device group: 0x%x\n",
10332 phba->pci_dev_grp);
10333 break;
10334 }
10335 return rc;
10336}
10337
10338/**
10339 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
10340 * @pdev: pointer to PCI device
10341 *
10342 * This routine is registered to the PCI subsystem for error handling. It
10343 * is called when kernel error recovery tells the lpfc driver that it is
10344 * OK to resume normal PCI operation after PCI bus error recovery. When
10345 * this routine is invoked, it dispatches the action to the proper SLI-3
10346 * or SLI-4 device io_resume routine, which will resume the device operation.
10347 **/
10348static void
10349lpfc_io_resume(struct pci_dev *pdev)
10350{
10351 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10352 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10353
10354 switch (phba->pci_dev_grp) {
10355 case LPFC_PCI_DEV_LP:
10356 lpfc_io_resume_s3(pdev);
10357 break;
James Smartda0436e2009-05-22 14:51:39 -040010358 case LPFC_PCI_DEV_OC:
10359 lpfc_io_resume_s4(pdev);
10360 break;
James Smart3772a992009-05-22 14:50:54 -040010361 default:
10362 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10363 "1429 Invalid PCI device group: 0x%x\n",
10364 phba->pci_dev_grp);
10365 break;
10366 }
10367 return;
10368}
10369
James Smart3ef6d242012-01-18 16:23:48 -050010370/**
10371 * lpfc_mgmt_open - method called when 'lpfcmgmt' is opened from userspace
10372 * @inode: pointer to the inode representing the lpfcmgmt device
10373 * @filep: pointer to the file representing the open lpfcmgmt device
10374 *
10375 * This routine puts a reference count on the lpfc module whenever the
10376 * character device is opened
10377 **/
10378static int
10379lpfc_mgmt_open(struct inode *inode, struct file *filep)
10380{
10381 try_module_get(THIS_MODULE);
10382 return 0;
10383}
10384
10385/**
10386 * lpfc_mgmt_release - method called when 'lpfcmgmt' is closed in userspace
10387 * @inode: pointer to the inode representing the lpfcmgmt device
10388 * @filep: pointer to the file representing the open lpfcmgmt device
10389 *
10390 * This routine removes a reference count from the lpfc module when the
10391 * character device is closed
10392 **/
10393static int
10394lpfc_mgmt_release(struct inode *inode, struct file *filep)
10395{
10396 module_put(THIS_MODULE);
10397 return 0;
10398}
10399
dea31012005-04-17 16:05:31 -050010400static struct pci_device_id lpfc_id_table[] = {
10401 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
10402 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -040010403 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
10404 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010405 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
10406 PCI_ANY_ID, PCI_ANY_ID, },
10407 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
10408 PCI_ANY_ID, PCI_ANY_ID, },
10409 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
10410 PCI_ANY_ID, PCI_ANY_ID, },
10411 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
10412 PCI_ANY_ID, PCI_ANY_ID, },
10413 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
10414 PCI_ANY_ID, PCI_ANY_ID, },
10415 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
10416 PCI_ANY_ID, PCI_ANY_ID, },
10417 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
10418 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050010419 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
10420 PCI_ANY_ID, PCI_ANY_ID, },
10421 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
10422 PCI_ANY_ID, PCI_ANY_ID, },
10423 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
10424 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010425 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
10426 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050010427 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
10428 PCI_ANY_ID, PCI_ANY_ID, },
10429 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
10430 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010431 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
10432 PCI_ANY_ID, PCI_ANY_ID, },
10433 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
10434 PCI_ANY_ID, PCI_ANY_ID, },
10435 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
10436 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -040010437 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
10438 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050010439 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
10440 PCI_ANY_ID, PCI_ANY_ID, },
10441 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
10442 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010443 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
10444 PCI_ANY_ID, PCI_ANY_ID, },
10445 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
10446 PCI_ANY_ID, PCI_ANY_ID, },
10447 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
10448 PCI_ANY_ID, PCI_ANY_ID, },
10449 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
10450 PCI_ANY_ID, PCI_ANY_ID, },
10451 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
10452 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050010453 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
10454 PCI_ANY_ID, PCI_ANY_ID, },
10455 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
10456 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -040010457 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
10458 PCI_ANY_ID, PCI_ANY_ID, },
10459 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
10460 PCI_ANY_ID, PCI_ANY_ID, },
10461 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
10462 PCI_ANY_ID, PCI_ANY_ID, },
10463 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
10464 PCI_ANY_ID, PCI_ANY_ID, },
10465 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
10466 PCI_ANY_ID, PCI_ANY_ID, },
10467 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
10468 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -040010469 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
10470 PCI_ANY_ID, PCI_ANY_ID, },
10471 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
10472 PCI_ANY_ID, PCI_ANY_ID, },
10473 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
10474 PCI_ANY_ID, PCI_ANY_ID, },
James Smart3772a992009-05-22 14:50:54 -040010475 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
10476 PCI_ANY_ID, PCI_ANY_ID, },
James Smarta747c9c2009-11-18 15:41:10 -050010477 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
10478 PCI_ANY_ID, PCI_ANY_ID, },
10479 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
James Smart6669f9b2009-10-02 15:16:45 -040010480 PCI_ANY_ID, PCI_ANY_ID, },
James Smart98fc5dd2010-06-07 15:24:29 -040010481 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BALIUS,
10482 PCI_ANY_ID, PCI_ANY_ID, },
James Smart085c6472010-11-20 23:11:37 -050010483 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC,
10484 PCI_ANY_ID, PCI_ANY_ID, },
10485 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE,
10486 PCI_ANY_ID, PCI_ANY_ID, },
James Smartc0c11512011-05-24 11:41:34 -040010487 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC_VF,
10488 PCI_ANY_ID, PCI_ANY_ID, },
10489 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE_VF,
10490 PCI_ANY_ID, PCI_ANY_ID, },
James Smartf8cafd32012-08-03 12:42:51 -040010491 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SKYHAWK,
10492 PCI_ANY_ID, PCI_ANY_ID, },
10493 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SKYHAWK_VF,
10494 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010495 { 0 }
10496};
10497
10498MODULE_DEVICE_TABLE(pci, lpfc_id_table);
10499
Stephen Hemmingera55b2d22012-09-07 09:33:16 -070010500static const struct pci_error_handlers lpfc_err_handler = {
Linas Vepstas8d63f372007-02-14 14:28:36 -060010501 .error_detected = lpfc_io_error_detected,
10502 .slot_reset = lpfc_io_slot_reset,
10503 .resume = lpfc_io_resume,
10504};
10505
dea31012005-04-17 16:05:31 -050010506static struct pci_driver lpfc_driver = {
10507 .name = LPFC_DRIVER_NAME,
10508 .id_table = lpfc_id_table,
10509 .probe = lpfc_pci_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010510 .remove = lpfc_pci_remove_one,
James Smart3a55b532008-12-04 22:38:54 -050010511 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -040010512 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -050010513 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -050010514};
10515
James Smart3ef6d242012-01-18 16:23:48 -050010516static const struct file_operations lpfc_mgmt_fop = {
10517 .open = lpfc_mgmt_open,
10518 .release = lpfc_mgmt_release,
10519};
10520
10521static struct miscdevice lpfc_mgmt_dev = {
10522 .minor = MISC_DYNAMIC_MINOR,
10523 .name = "lpfcmgmt",
10524 .fops = &lpfc_mgmt_fop,
10525};
10526
James Smarte59058c2008-08-24 21:49:00 -040010527/**
James Smart3621a712009-04-06 18:47:14 -040010528 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -040010529 *
10530 * This routine is to be invoked when the lpfc module is loaded into the
10531 * kernel. The special kernel macro module_init() is used to indicate the
10532 * role of this routine to the kernel as lpfc module entry point.
10533 *
10534 * Return codes
10535 * 0 - successful
10536 * -ENOMEM - FC attach transport failed
10537 * all others - failed
10538 */
dea31012005-04-17 16:05:31 -050010539static int __init
10540lpfc_init(void)
10541{
10542 int error = 0;
10543
10544 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010545 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -050010546
James Smart3ef6d242012-01-18 16:23:48 -050010547 error = misc_register(&lpfc_mgmt_dev);
10548 if (error)
10549 printk(KERN_ERR "Could not register lpfcmgmt device, "
10550 "misc_register returned with status %d", error);
10551
James Smart7ee5d432007-10-27 13:37:17 -040010552 if (lpfc_enable_npiv) {
10553 lpfc_transport_functions.vport_create = lpfc_vport_create;
10554 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
10555 }
dea31012005-04-17 16:05:31 -050010556 lpfc_transport_template =
10557 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -040010558 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -050010559 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -040010560 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -040010561 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -040010562 fc_attach_transport(&lpfc_vport_transport_functions);
10563 if (lpfc_vport_transport_template == NULL) {
10564 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040010565 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -040010566 }
James Smart7ee5d432007-10-27 13:37:17 -040010567 }
dea31012005-04-17 16:05:31 -050010568 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -050010569 if (error) {
dea31012005-04-17 16:05:31 -050010570 fc_release_transport(lpfc_transport_template);
James Smartd7c255b2008-08-24 21:50:00 -040010571 if (lpfc_enable_npiv)
10572 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -050010573 }
dea31012005-04-17 16:05:31 -050010574
10575 return error;
10576}
10577
James Smarte59058c2008-08-24 21:49:00 -040010578/**
James Smart3621a712009-04-06 18:47:14 -040010579 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -040010580 *
10581 * This routine is invoked when the lpfc module is removed from the kernel.
10582 * The special kernel macro module_exit() is used to indicate the role of
10583 * this routine to the kernel as lpfc module exit point.
10584 */
dea31012005-04-17 16:05:31 -050010585static void __exit
10586lpfc_exit(void)
10587{
James Smart3ef6d242012-01-18 16:23:48 -050010588 misc_deregister(&lpfc_mgmt_dev);
dea31012005-04-17 16:05:31 -050010589 pci_unregister_driver(&lpfc_driver);
10590 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040010591 if (lpfc_enable_npiv)
10592 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -050010593 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040010594 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
10595 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050010596 (1L << _dump_buf_data_order), _dump_buf_data);
10597 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
10598 }
10599
10600 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -040010601 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
10602 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050010603 (1L << _dump_buf_dif_order), _dump_buf_dif);
10604 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
10605 }
dea31012005-04-17 16:05:31 -050010606}
10607
10608module_init(lpfc_init);
10609module_exit(lpfc_exit);
10610MODULE_LICENSE("GPL");
10611MODULE_DESCRIPTION(LPFC_MODULE_DESC);
10612MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
10613MODULE_VERSION("0:" LPFC_DRIVER_VERSION);