blob: 9efe5f82a6a10b62fb6f8c2b37235f98eb768a33 [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smartd4379ac2012-03-01 22:37:07 -05004 * Copyright (C) 2004-2012 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/delay.h>
24#include <linux/dma-mapping.h>
25#include <linux/idr.h>
26#include <linux/interrupt.h>
Paul Gortmakeracf3368f2011-05-27 09:47:43 -040027#include <linux/module.h>
dea31012005-04-17 16:05:31 -050028#include <linux/kthread.h>
29#include <linux/pci.h>
30#include <linux/spinlock.h>
James Smart92d7f7b2007-06-17 19:56:38 -050031#include <linux/ctype.h>
James Smart0d878412009-10-02 15:16:56 -040032#include <linux/aer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
James Smart52d52442011-05-24 11:42:45 -040034#include <linux/firmware.h>
James Smart3ef6d242012-01-18 16:23:48 -050035#include <linux/miscdevice.h>
dea31012005-04-17 16:05:31 -050036
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040037#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050038#include <scsi/scsi_device.h>
39#include <scsi/scsi_host.h>
40#include <scsi/scsi_transport_fc.h>
41
James Smartda0436e2009-05-22 14:51:39 -040042#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050043#include "lpfc_hw.h"
44#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040045#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040046#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050047#include "lpfc_disc.h"
48#include "lpfc_scsi.h"
49#include "lpfc.h"
50#include "lpfc_logmsg.h"
51#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050052#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050053#include "lpfc_version.h"
54
James Smart81301a92008-12-04 22:39:46 -050055char *_dump_buf_data;
56unsigned long _dump_buf_data_order;
57char *_dump_buf_dif;
58unsigned long _dump_buf_dif_order;
59spinlock_t _dump_buf_lock;
60
dea31012005-04-17 16:05:31 -050061static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
62static int lpfc_post_rcv_buf(struct lpfc_hba *);
James Smart5350d872011-10-10 21:33:49 -040063static int lpfc_sli4_queue_verify(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040064static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
65static int lpfc_setup_endian_order(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040066static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
James Smart8a9d2e82012-05-09 21:16:12 -040067static void lpfc_free_els_sgl_list(struct lpfc_hba *);
68static void lpfc_init_sgl_list(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040069static int lpfc_init_active_sgl_array(struct lpfc_hba *);
70static void lpfc_free_active_sgl(struct lpfc_hba *);
71static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
72static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
73static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
74static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
75static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
James Smart618a5232012-06-12 13:54:36 -040076static void lpfc_sli4_disable_intr(struct lpfc_hba *);
77static uint32_t lpfc_sli4_enable_intr(struct lpfc_hba *, uint32_t);
dea31012005-04-17 16:05:31 -050078
79static struct scsi_transport_template *lpfc_transport_template = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050080static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea31012005-04-17 16:05:31 -050081static DEFINE_IDR(lpfc_hba_index);
82
James Smarte59058c2008-08-24 21:49:00 -040083/**
James Smart3621a712009-04-06 18:47:14 -040084 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
James Smarte59058c2008-08-24 21:49:00 -040085 * @phba: pointer to lpfc hba data structure.
86 *
87 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
88 * mailbox command. It retrieves the revision information from the HBA and
89 * collects the Vital Product Data (VPD) about the HBA for preparing the
90 * configuration of the HBA.
91 *
92 * Return codes:
93 * 0 - success.
94 * -ERESTART - requests the SLI layer to reset the HBA and try again.
95 * Any other value - indicates an error.
96 **/
dea31012005-04-17 16:05:31 -050097int
James Smart2e0fef82007-06-17 19:56:36 -050098lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050099{
100 lpfc_vpd_t *vp = &phba->vpd;
101 int i = 0, rc;
102 LPFC_MBOXQ_t *pmb;
103 MAILBOX_t *mb;
104 char *lpfc_vpd_data = NULL;
105 uint16_t offset = 0;
106 static char licensed[56] =
107 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -0400108 static int init_key = 1;
dea31012005-04-17 16:05:31 -0500109
110 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
111 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500112 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500113 return -ENOMEM;
114 }
115
James Smart04c68492009-05-22 14:52:52 -0400116 mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -0500117 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500118
119 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -0400120 if (init_key) {
121 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -0500122
James Smart65a29c12006-07-06 15:50:50 -0400123 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
124 *ptext = cpu_to_be32(*ptext);
125 init_key = 0;
126 }
dea31012005-04-17 16:05:31 -0500127
128 lpfc_read_nv(phba, pmb);
129 memset((char*)mb->un.varRDnvp.rsvd3, 0,
130 sizeof (mb->un.varRDnvp.rsvd3));
131 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
132 sizeof (licensed));
133
134 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
135
136 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500137 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smarte8b62012007-08-02 11:10:09 -0400138 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500139 "error, mbxCmd x%x READ_NVPARM, "
140 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500141 mb->mbxCommand, mb->mbxStatus);
142 mempool_free(pmb, phba->mbox_mem_pool);
143 return -ERESTART;
144 }
145 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500146 sizeof(phba->wwnn));
147 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
148 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500149 }
150
James Smart92d7f7b2007-06-17 19:56:38 -0500151 phba->sli3_options = 0x0;
152
dea31012005-04-17 16:05:31 -0500153 /* Setup and issue mailbox READ REV command */
154 lpfc_read_rev(phba, pmb);
155 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
156 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500157 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400158 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500159 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500160 mb->mbxCommand, mb->mbxStatus);
161 mempool_free( pmb, phba->mbox_mem_pool);
162 return -ERESTART;
163 }
164
James Smart92d7f7b2007-06-17 19:56:38 -0500165
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500166 /*
167 * The value of rr must be 1 since the driver set the cv field to 1.
168 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500169 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500170 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500171 vp->rev.rBit = 0;
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500172 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400173 "0440 Adapter failed to init, READ_REV has "
174 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500175 mempool_free(pmb, phba->mbox_mem_pool);
176 return -ERESTART;
dea31012005-04-17 16:05:31 -0500177 }
178
James Smart495a7142008-06-14 22:52:59 -0400179 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
180 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -0500181 return -EINVAL;
James Smart495a7142008-06-14 22:52:59 -0400182 }
James Smarted957682007-06-17 19:56:37 -0500183
dea31012005-04-17 16:05:31 -0500184 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500185 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500186 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500187 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
188 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
189 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
190 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500191 vp->rev.biuRev = mb->un.varRdRev.biuRev;
192 vp->rev.smRev = mb->un.varRdRev.smRev;
193 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
194 vp->rev.endecRev = mb->un.varRdRev.endecRev;
195 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
196 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
197 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
198 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
199 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
200 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
201
James Smart92d7f7b2007-06-17 19:56:38 -0500202 /* If the sli feature level is less then 9, we must
203 * tear down all RPIs and VPIs on link down if NPIV
204 * is enabled.
205 */
206 if (vp->rev.feaLevelHigh < 9)
207 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
208
dea31012005-04-17 16:05:31 -0500209 if (lpfc_is_LC_HBA(phba->pcidev->device))
210 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
211 sizeof (phba->RandomData));
212
dea31012005-04-17 16:05:31 -0500213 /* Get adapter VPD information */
dea31012005-04-17 16:05:31 -0500214 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
215 if (!lpfc_vpd_data)
James Smartd7c255b2008-08-24 21:50:00 -0400216 goto out_free_mbox;
dea31012005-04-17 16:05:31 -0500217 do {
James Smarta0c87cb2009-07-19 10:01:10 -0400218 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
dea31012005-04-17 16:05:31 -0500219 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
220
221 if (rc != MBX_SUCCESS) {
222 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400223 "0441 VPD not present on adapter, "
dea31012005-04-17 16:05:31 -0500224 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500225 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500226 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500227 }
James Smart04c68492009-05-22 14:52:52 -0400228 /* dump mem may return a zero when finished or we got a
229 * mailbox error, either way we are done.
230 */
231 if (mb->un.varDmp.word_cnt == 0)
232 break;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500233 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
234 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
James Smartd7c255b2008-08-24 21:50:00 -0400235 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
236 lpfc_vpd_data + offset,
James Smart92d7f7b2007-06-17 19:56:38 -0500237 mb->un.varDmp.word_cnt);
dea31012005-04-17 16:05:31 -0500238 offset += mb->un.varDmp.word_cnt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500239 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
240 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500241
242 kfree(lpfc_vpd_data);
dea31012005-04-17 16:05:31 -0500243out_free_mbox:
244 mempool_free(pmb, phba->mbox_mem_pool);
245 return 0;
246}
247
James Smarte59058c2008-08-24 21:49:00 -0400248/**
James Smart3621a712009-04-06 18:47:14 -0400249 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
James Smarte59058c2008-08-24 21:49:00 -0400250 * @phba: pointer to lpfc hba data structure.
251 * @pmboxq: pointer to the driver internal queue element for mailbox command.
252 *
253 * This is the completion handler for driver's configuring asynchronous event
254 * mailbox command to the device. If the mailbox command returns successfully,
255 * it will set internal async event support flag to 1; otherwise, it will
256 * set internal async event support flag to 0.
257 **/
James Smart57127f12007-10-27 13:37:05 -0400258static void
259lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
260{
James Smart04c68492009-05-22 14:52:52 -0400261 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
James Smart57127f12007-10-27 13:37:05 -0400262 phba->temp_sensor_support = 1;
263 else
264 phba->temp_sensor_support = 0;
265 mempool_free(pmboxq, phba->mbox_mem_pool);
266 return;
267}
268
James Smarte59058c2008-08-24 21:49:00 -0400269/**
James Smart3621a712009-04-06 18:47:14 -0400270 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
James Smart97207482008-12-04 22:39:19 -0500271 * @phba: pointer to lpfc hba data structure.
272 * @pmboxq: pointer to the driver internal queue element for mailbox command.
273 *
274 * This is the completion handler for dump mailbox command for getting
275 * wake up parameters. When this command complete, the response contain
276 * Option rom version of the HBA. This function translate the version number
277 * into a human readable string and store it in OptionROMVersion.
278 **/
279static void
280lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
281{
282 struct prog_id *prg;
283 uint32_t prog_id_word;
284 char dist = ' ';
285 /* character array used for decoding dist type. */
286 char dist_char[] = "nabx";
287
James Smart04c68492009-05-22 14:52:52 -0400288 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
James Smart9f1e1b52008-12-04 22:39:40 -0500289 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500290 return;
James Smart9f1e1b52008-12-04 22:39:40 -0500291 }
James Smart97207482008-12-04 22:39:19 -0500292
293 prg = (struct prog_id *) &prog_id_word;
294
295 /* word 7 contain option rom version */
James Smart04c68492009-05-22 14:52:52 -0400296 prog_id_word = pmboxq->u.mb.un.varWords[7];
James Smart97207482008-12-04 22:39:19 -0500297
298 /* Decode the Option rom version word to a readable string */
299 if (prg->dist < 4)
300 dist = dist_char[prg->dist];
301
302 if ((prg->dist == 3) && (prg->num == 0))
303 sprintf(phba->OptionROMVersion, "%d.%d%d",
304 prg->ver, prg->rev, prg->lev);
305 else
306 sprintf(phba->OptionROMVersion, "%d.%d%d%c%d",
307 prg->ver, prg->rev, prg->lev,
308 dist, prg->num);
James Smart9f1e1b52008-12-04 22:39:40 -0500309 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500310 return;
311}
312
313/**
James Smart05580562011-05-24 11:40:48 -0400314 * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
315 * cfg_soft_wwnn, cfg_soft_wwpn
316 * @vport: pointer to lpfc vport data structure.
317 *
318 *
319 * Return codes
320 * None.
321 **/
322void
323lpfc_update_vport_wwn(struct lpfc_vport *vport)
324{
325 /* If the soft name exists then update it using the service params */
326 if (vport->phba->cfg_soft_wwnn)
327 u64_to_wwn(vport->phba->cfg_soft_wwnn,
328 vport->fc_sparam.nodeName.u.wwn);
329 if (vport->phba->cfg_soft_wwpn)
330 u64_to_wwn(vport->phba->cfg_soft_wwpn,
331 vport->fc_sparam.portName.u.wwn);
332
333 /*
334 * If the name is empty or there exists a soft name
335 * then copy the service params name, otherwise use the fc name
336 */
337 if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
338 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
339 sizeof(struct lpfc_name));
340 else
341 memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
342 sizeof(struct lpfc_name));
343
344 if (vport->fc_portname.u.wwn[0] == 0 || vport->phba->cfg_soft_wwpn)
345 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
346 sizeof(struct lpfc_name));
347 else
348 memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
349 sizeof(struct lpfc_name));
350}
351
352/**
James Smart3621a712009-04-06 18:47:14 -0400353 * lpfc_config_port_post - Perform lpfc initialization after config port
James Smarte59058c2008-08-24 21:49:00 -0400354 * @phba: pointer to lpfc hba data structure.
355 *
356 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
357 * command call. It performs all internal resource and state setups on the
358 * port: post IOCB buffers, enable appropriate host interrupt attentions,
359 * ELS ring timers, etc.
360 *
361 * Return codes
362 * 0 - success.
363 * Any other value - error.
364 **/
dea31012005-04-17 16:05:31 -0500365int
James Smart2e0fef82007-06-17 19:56:36 -0500366lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500367{
James Smart2e0fef82007-06-17 19:56:36 -0500368 struct lpfc_vport *vport = phba->pport;
James Smarta257bf92009-04-06 18:48:10 -0400369 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -0500370 LPFC_MBOXQ_t *pmb;
371 MAILBOX_t *mb;
372 struct lpfc_dmabuf *mp;
373 struct lpfc_sli *psli = &phba->sli;
374 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500375 int i, j;
376 int rc;
dea31012005-04-17 16:05:31 -0500377
James Smart7af67052007-10-27 13:38:11 -0400378 spin_lock_irq(&phba->hbalock);
379 /*
380 * If the Config port completed correctly the HBA is not
381 * over heated any more.
382 */
383 if (phba->over_temp_state == HBA_OVER_TEMP)
384 phba->over_temp_state = HBA_NORMAL_TEMP;
385 spin_unlock_irq(&phba->hbalock);
386
dea31012005-04-17 16:05:31 -0500387 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
388 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500389 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500390 return -ENOMEM;
391 }
James Smart04c68492009-05-22 14:52:52 -0400392 mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500393
dea31012005-04-17 16:05:31 -0500394 /* Get login parameters for NID. */
James Smart9f1177a2010-02-26 14:12:57 -0500395 rc = lpfc_read_sparam(phba, pmb, 0);
396 if (rc) {
397 mempool_free(pmb, phba->mbox_mem_pool);
398 return -ENOMEM;
399 }
400
James Smarted957682007-06-17 19:56:37 -0500401 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500402 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500403 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400404 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500405 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500406 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500407 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500408 mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart9f1177a2010-02-26 14:12:57 -0500409 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500410 lpfc_mbuf_free(phba, mp->virt, mp->phys);
411 kfree(mp);
412 return -EIO;
413 }
414
415 mp = (struct lpfc_dmabuf *) pmb->context1;
416
James Smart2e0fef82007-06-17 19:56:36 -0500417 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500418 lpfc_mbuf_free(phba, mp->virt, mp->phys);
419 kfree(mp);
420 pmb->context1 = NULL;
James Smart05580562011-05-24 11:40:48 -0400421 lpfc_update_vport_wwn(vport);
James Smarta257bf92009-04-06 18:48:10 -0400422
423 /* Update the fc_host data structures with new wwn. */
424 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
425 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart21e9a0a2009-05-22 14:53:21 -0400426 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smarta257bf92009-04-06 18:48:10 -0400427
dea31012005-04-17 16:05:31 -0500428 /* If no serial number in VPD data, use low 6 bytes of WWNN */
429 /* This should be consolidated into parse_vpd ? - mr */
430 if (phba->SerialNumber[0] == 0) {
431 uint8_t *outptr;
432
James Smart2e0fef82007-06-17 19:56:36 -0500433 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500434 for (i = 0; i < 12; i++) {
435 status = *outptr++;
436 j = ((status & 0xf0) >> 4);
437 if (j <= 9)
438 phba->SerialNumber[i] =
439 (char)((uint8_t) 0x30 + (uint8_t) j);
440 else
441 phba->SerialNumber[i] =
442 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
443 i++;
444 j = (status & 0xf);
445 if (j <= 9)
446 phba->SerialNumber[i] =
447 (char)((uint8_t) 0x30 + (uint8_t) j);
448 else
449 phba->SerialNumber[i] =
450 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
451 }
452 }
453
dea31012005-04-17 16:05:31 -0500454 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500455 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500456 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500457 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400458 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500459 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500460 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500461 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500462 mempool_free( pmb, phba->mbox_mem_pool);
463 return -EIO;
464 }
465
James Smarta0c87cb2009-07-19 10:01:10 -0400466 /* Check if the port is disabled */
467 lpfc_sli_read_link_ste(phba);
468
dea31012005-04-17 16:05:31 -0500469 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
470 if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
471 phba->cfg_hba_queue_depth =
James Smartf1126682009-06-10 17:22:44 -0400472 (mb->un.varRdConfig.max_xri + 1) -
473 lpfc_sli4_get_els_iocb_cnt(phba);
dea31012005-04-17 16:05:31 -0500474
475 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500476
477 /* Get the default values for Model Name and Description */
478 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
479
James Smart2e0fef82007-06-17 19:56:36 -0500480 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500481
James Smart0b727fe2007-10-27 13:37:25 -0400482 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smart7e56aa22012-08-03 12:35:34 -0400483 if (psli->ring[psli->extra_ring].sli.sli3.cmdringaddr)
James Smarta4bc3372006-12-02 13:34:16 -0500484 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
James Smart7e56aa22012-08-03 12:35:34 -0400485 if (psli->ring[psli->fcp_ring].sli.sli3.cmdringaddr)
dea31012005-04-17 16:05:31 -0500486 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
James Smart7e56aa22012-08-03 12:35:34 -0400487 if (psli->ring[psli->next_ring].sli.sli3.cmdringaddr)
dea31012005-04-17 16:05:31 -0500488 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
489
490 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500491 if (phba->sli_rev != 3)
492 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500493
James Smart93996272008-08-24 21:50:30 -0400494 /*
495 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
496 */
497 if (phba->intr_type == MSIX) {
498 rc = lpfc_config_msi(phba, pmb);
499 if (rc) {
500 mempool_free(pmb, phba->mbox_mem_pool);
501 return -EIO;
502 }
503 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
504 if (rc != MBX_SUCCESS) {
505 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
506 "0352 Config MSI mailbox command "
507 "failed, mbxCmd x%x, mbxStatus x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400508 pmb->u.mb.mbxCommand,
509 pmb->u.mb.mbxStatus);
James Smart93996272008-08-24 21:50:30 -0400510 mempool_free(pmb, phba->mbox_mem_pool);
511 return -EIO;
512 }
513 }
514
James Smart04c68492009-05-22 14:52:52 -0400515 spin_lock_irq(&phba->hbalock);
James Smart93996272008-08-24 21:50:30 -0400516 /* Initialize ERATT handling flag */
517 phba->hba_flag &= ~HBA_ERATT_HANDLED;
518
dea31012005-04-17 16:05:31 -0500519 /* Enable appropriate host interrupts */
James Smart9940b972011-03-11 16:06:12 -0500520 if (lpfc_readl(phba->HCregaddr, &status)) {
521 spin_unlock_irq(&phba->hbalock);
522 return -EIO;
523 }
dea31012005-04-17 16:05:31 -0500524 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
525 if (psli->num_rings > 0)
526 status |= HC_R0INT_ENA;
527 if (psli->num_rings > 1)
528 status |= HC_R1INT_ENA;
529 if (psli->num_rings > 2)
530 status |= HC_R2INT_ENA;
531 if (psli->num_rings > 3)
532 status |= HC_R3INT_ENA;
533
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500534 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
535 (phba->cfg_poll & DISABLE_FCP_RING_INT))
James Smart93996272008-08-24 21:50:30 -0400536 status &= ~(HC_R0INT_ENA);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500537
dea31012005-04-17 16:05:31 -0500538 writel(status, phba->HCregaddr);
539 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500540 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500541
James Smart93996272008-08-24 21:50:30 -0400542 /* Set up ring-0 (ELS) timer */
543 timeout = phba->fc_ratov * 2;
James Smart2e0fef82007-06-17 19:56:36 -0500544 mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
James Smart93996272008-08-24 21:50:30 -0400545 /* Set up heart beat (HB) timer */
James Smart858c9f62007-06-17 19:56:39 -0500546 mod_timer(&phba->hb_tmofunc, jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
547 phba->hb_outstanding = 0;
548 phba->last_completion_time = jiffies;
James Smart93996272008-08-24 21:50:30 -0400549 /* Set up error attention (ERATT) polling timer */
550 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
dea31012005-04-17 16:05:31 -0500551
James Smarta0c87cb2009-07-19 10:01:10 -0400552 if (phba->hba_flag & LINK_DISABLED) {
553 lpfc_printf_log(phba,
554 KERN_ERR, LOG_INIT,
555 "2598 Adapter Link is disabled.\n");
556 lpfc_down_link(phba, pmb);
557 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
558 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
559 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
560 lpfc_printf_log(phba,
561 KERN_ERR, LOG_INIT,
562 "2599 Adapter failed to issue DOWN_LINK"
563 " mbox command rc 0x%x\n", rc);
564
565 mempool_free(pmb, phba->mbox_mem_pool);
566 return -EIO;
567 }
James Smarte40a02c2010-02-26 14:13:54 -0500568 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart026abb82011-12-13 13:20:45 -0500569 mempool_free(pmb, phba->mbox_mem_pool);
570 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
571 if (rc)
572 return rc;
dea31012005-04-17 16:05:31 -0500573 }
574 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400575 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500576 if (!pmb) {
577 phba->link_state = LPFC_HBA_ERROR;
578 return -ENOMEM;
579 }
580
James Smart57127f12007-10-27 13:37:05 -0400581 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
582 pmb->mbox_cmpl = lpfc_config_async_cmpl;
583 pmb->vport = phba->pport;
584 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500585
James Smart57127f12007-10-27 13:37:05 -0400586 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
587 lpfc_printf_log(phba,
588 KERN_ERR,
589 LOG_INIT,
590 "0456 Adapter failed to issue "
James Smarte4e74272009-07-19 10:01:38 -0400591 "ASYNCEVT_ENABLE mbox status x%x\n",
James Smart57127f12007-10-27 13:37:05 -0400592 rc);
593 mempool_free(pmb, phba->mbox_mem_pool);
594 }
James Smart97207482008-12-04 22:39:19 -0500595
596 /* Get Option rom version */
597 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500598 if (!pmb) {
599 phba->link_state = LPFC_HBA_ERROR;
600 return -ENOMEM;
601 }
602
James Smart97207482008-12-04 22:39:19 -0500603 lpfc_dump_wakeup_param(phba, pmb);
604 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
605 pmb->vport = phba->pport;
606 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
607
608 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
609 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
James Smarte4e74272009-07-19 10:01:38 -0400610 "to get Option ROM version status x%x\n", rc);
James Smart97207482008-12-04 22:39:19 -0500611 mempool_free(pmb, phba->mbox_mem_pool);
612 }
613
James Smartd7c255b2008-08-24 21:50:00 -0400614 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400615}
616
James Smarte59058c2008-08-24 21:49:00 -0400617/**
James Smart84d1b002010-02-12 14:42:33 -0500618 * lpfc_hba_init_link - Initialize the FC link
619 * @phba: pointer to lpfc hba data structure.
James Smart6e7288d2010-06-07 15:23:35 -0400620 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500621 *
622 * This routine will issue the INIT_LINK mailbox command call.
623 * It is available to other drivers through the lpfc_hba data
624 * structure for use as a delayed link up mechanism with the
625 * module parameter lpfc_suppress_link_up.
626 *
627 * Return code
628 * 0 - success
629 * Any other value - error
630 **/
631int
James Smart6e7288d2010-06-07 15:23:35 -0400632lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500633{
James Smart1b511972011-12-13 13:23:09 -0500634 return lpfc_hba_init_link_fc_topology(phba, phba->cfg_topology, flag);
635}
636
637/**
638 * lpfc_hba_init_link_fc_topology - Initialize FC link with desired topology
639 * @phba: pointer to lpfc hba data structure.
640 * @fc_topology: desired fc topology.
641 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
642 *
643 * This routine will issue the INIT_LINK mailbox command call.
644 * It is available to other drivers through the lpfc_hba data
645 * structure for use as a delayed link up mechanism with the
646 * module parameter lpfc_suppress_link_up.
647 *
648 * Return code
649 * 0 - success
650 * Any other value - error
651 **/
652int
653lpfc_hba_init_link_fc_topology(struct lpfc_hba *phba, uint32_t fc_topology,
654 uint32_t flag)
655{
James Smart84d1b002010-02-12 14:42:33 -0500656 struct lpfc_vport *vport = phba->pport;
657 LPFC_MBOXQ_t *pmb;
658 MAILBOX_t *mb;
659 int rc;
660
661 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
662 if (!pmb) {
663 phba->link_state = LPFC_HBA_ERROR;
664 return -ENOMEM;
665 }
666 mb = &pmb->u.mb;
667 pmb->vport = vport;
668
James Smart026abb82011-12-13 13:20:45 -0500669 if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_MAX) ||
670 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G) &&
671 !(phba->lmt & LMT_1Gb)) ||
672 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G) &&
673 !(phba->lmt & LMT_2Gb)) ||
674 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G) &&
675 !(phba->lmt & LMT_4Gb)) ||
676 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G) &&
677 !(phba->lmt & LMT_8Gb)) ||
678 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G) &&
679 !(phba->lmt & LMT_10Gb)) ||
680 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G) &&
681 !(phba->lmt & LMT_16Gb))) {
682 /* Reset link speed to auto */
683 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
684 "1302 Invalid speed for this board:%d "
685 "Reset link speed to auto.\n",
686 phba->cfg_link_speed);
687 phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
688 }
James Smart1b511972011-12-13 13:23:09 -0500689 lpfc_init_link(phba, pmb, fc_topology, phba->cfg_link_speed);
James Smart84d1b002010-02-12 14:42:33 -0500690 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart1b511972011-12-13 13:23:09 -0500691 if (phba->sli_rev < LPFC_SLI_REV4)
692 lpfc_set_loopback_flag(phba);
James Smart6e7288d2010-06-07 15:23:35 -0400693 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart76a95d72010-11-20 23:11:48 -0500694 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
James Smart84d1b002010-02-12 14:42:33 -0500695 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
696 "0498 Adapter failed to init, mbxCmd x%x "
697 "INIT_LINK, mbxStatus x%x\n",
698 mb->mbxCommand, mb->mbxStatus);
James Smart76a95d72010-11-20 23:11:48 -0500699 if (phba->sli_rev <= LPFC_SLI_REV3) {
700 /* Clear all interrupt enable conditions */
701 writel(0, phba->HCregaddr);
702 readl(phba->HCregaddr); /* flush */
703 /* Clear all pending interrupts */
704 writel(0xffffffff, phba->HAregaddr);
705 readl(phba->HAregaddr); /* flush */
706 }
James Smart84d1b002010-02-12 14:42:33 -0500707 phba->link_state = LPFC_HBA_ERROR;
James Smart6e7288d2010-06-07 15:23:35 -0400708 if (rc != MBX_BUSY || flag == MBX_POLL)
James Smart84d1b002010-02-12 14:42:33 -0500709 mempool_free(pmb, phba->mbox_mem_pool);
710 return -EIO;
711 }
James Smarte40a02c2010-02-26 14:13:54 -0500712 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
James Smart6e7288d2010-06-07 15:23:35 -0400713 if (flag == MBX_POLL)
714 mempool_free(pmb, phba->mbox_mem_pool);
James Smart84d1b002010-02-12 14:42:33 -0500715
716 return 0;
717}
718
719/**
720 * lpfc_hba_down_link - this routine downs the FC link
James Smart6e7288d2010-06-07 15:23:35 -0400721 * @phba: pointer to lpfc hba data structure.
722 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500723 *
724 * This routine will issue the DOWN_LINK mailbox command call.
725 * It is available to other drivers through the lpfc_hba data
726 * structure for use to stop the link.
727 *
728 * Return code
729 * 0 - success
730 * Any other value - error
731 **/
732int
James Smart6e7288d2010-06-07 15:23:35 -0400733lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500734{
735 LPFC_MBOXQ_t *pmb;
736 int rc;
737
738 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
739 if (!pmb) {
740 phba->link_state = LPFC_HBA_ERROR;
741 return -ENOMEM;
742 }
743
744 lpfc_printf_log(phba,
745 KERN_ERR, LOG_INIT,
746 "0491 Adapter Link is disabled.\n");
747 lpfc_down_link(phba, pmb);
748 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6e7288d2010-06-07 15:23:35 -0400749 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart84d1b002010-02-12 14:42:33 -0500750 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
751 lpfc_printf_log(phba,
752 KERN_ERR, LOG_INIT,
753 "2522 Adapter failed to issue DOWN_LINK"
754 " mbox command rc 0x%x\n", rc);
755
756 mempool_free(pmb, phba->mbox_mem_pool);
757 return -EIO;
758 }
James Smart6e7288d2010-06-07 15:23:35 -0400759 if (flag == MBX_POLL)
760 mempool_free(pmb, phba->mbox_mem_pool);
761
James Smart84d1b002010-02-12 14:42:33 -0500762 return 0;
763}
764
765/**
James Smart3621a712009-04-06 18:47:14 -0400766 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400767 * @phba: pointer to lpfc HBA data structure.
768 *
769 * This routine will do LPFC uninitialization before the HBA is reset when
770 * bringing down the SLI Layer.
771 *
772 * Return codes
773 * 0 - success.
774 * Any other value - error.
775 **/
dea31012005-04-17 16:05:31 -0500776int
James Smart2e0fef82007-06-17 19:56:36 -0500777lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500778{
James Smart1b32f6a2008-02-08 18:49:39 -0500779 struct lpfc_vport **vports;
780 int i;
James Smart3772a992009-05-22 14:50:54 -0400781
782 if (phba->sli_rev <= LPFC_SLI_REV3) {
783 /* Disable interrupts */
784 writel(0, phba->HCregaddr);
785 readl(phba->HCregaddr); /* flush */
786 }
dea31012005-04-17 16:05:31 -0500787
James Smart1b32f6a2008-02-08 18:49:39 -0500788 if (phba->pport->load_flag & FC_UNLOADING)
789 lpfc_cleanup_discovery_resources(phba->pport);
790 else {
791 vports = lpfc_create_vport_work_array(phba);
792 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400793 for (i = 0; i <= phba->max_vports &&
794 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500795 lpfc_cleanup_discovery_resources(vports[i]);
796 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500797 }
798 return 0;
dea31012005-04-17 16:05:31 -0500799}
800
James Smarte59058c2008-08-24 21:49:00 -0400801/**
James Smart3772a992009-05-22 14:50:54 -0400802 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400803 * @phba: pointer to lpfc HBA data structure.
804 *
805 * This routine will do uninitialization after the HBA is reset when bring
806 * down the SLI Layer.
807 *
808 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200809 * 0 - success.
James Smarte59058c2008-08-24 21:49:00 -0400810 * Any other value - error.
811 **/
James Smart3772a992009-05-22 14:50:54 -0400812static int
813lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -0500814{
815 struct lpfc_sli *psli = &phba->sli;
816 struct lpfc_sli_ring *pring;
817 struct lpfc_dmabuf *mp, *next_mp;
James Smart09372822008-01-11 01:52:54 -0500818 LIST_HEAD(completions);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500819 int i;
820
James Smart92d7f7b2007-06-17 19:56:38 -0500821 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
822 lpfc_sli_hbqbuf_free_all(phba);
823 else {
824 /* Cleanup preposted buffers on the ELS ring */
825 pring = &psli->ring[LPFC_ELS_RING];
826 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
827 list_del(&mp->list);
828 pring->postbufq_cnt--;
829 lpfc_mbuf_free(phba, mp->virt, mp->phys);
830 kfree(mp);
831 }
Jamie Wellnitz41415862006-02-28 19:25:27 -0500832 }
833
James Smart09372822008-01-11 01:52:54 -0500834 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500835 for (i = 0; i < psli->num_rings; i++) {
836 pring = &psli->ring[i];
James Smart09372822008-01-11 01:52:54 -0500837
838 /* At this point in time the HBA is either reset or DOA. Either
839 * way, nothing should be on txcmplq as it will NEVER complete.
840 */
841 list_splice_init(&pring->txcmplq, &completions);
842 pring->txcmplq_cnt = 0;
843 spin_unlock_irq(&phba->hbalock);
844
James Smarta257bf92009-04-06 18:48:10 -0400845 /* Cancel all the IOCBs from the completions list */
846 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
847 IOERR_SLI_ABORTED);
James Smart09372822008-01-11 01:52:54 -0500848
Jamie Wellnitz41415862006-02-28 19:25:27 -0500849 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart09372822008-01-11 01:52:54 -0500850 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500851 }
James Smart09372822008-01-11 01:52:54 -0500852 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500853
854 return 0;
855}
James Smart5af5eee2010-10-22 11:06:38 -0400856
James Smartda0436e2009-05-22 14:51:39 -0400857/**
858 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
859 * @phba: pointer to lpfc HBA data structure.
860 *
861 * This routine will do uninitialization after the HBA is reset when bring
862 * down the SLI Layer.
863 *
864 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200865 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400866 * Any other value - error.
867 **/
868static int
869lpfc_hba_down_post_s4(struct lpfc_hba *phba)
870{
871 struct lpfc_scsi_buf *psb, *psb_next;
872 LIST_HEAD(aborts);
873 int ret;
874 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500875 struct lpfc_sglq *sglq_entry = NULL;
876
James Smartda0436e2009-05-22 14:51:39 -0400877 ret = lpfc_hba_down_post_s3(phba);
878 if (ret)
879 return ret;
880 /* At this point in time the HBA is either reset or DOA. Either
881 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
882 * on the lpfc_sgl_list so that it can either be freed if the
883 * driver is unloading or reposted if the driver is restarting
884 * the port.
885 */
886 spin_lock_irq(&phba->hbalock); /* required for lpfc_sgl_list and */
887 /* scsl_buf_list */
888 /* abts_sgl_list_lock required because worker thread uses this
889 * list.
890 */
891 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -0500892 list_for_each_entry(sglq_entry,
893 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
894 sglq_entry->state = SGL_FREED;
895
James Smartda0436e2009-05-22 14:51:39 -0400896 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
897 &phba->sli4_hba.lpfc_sgl_list);
898 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
899 /* abts_scsi_buf_list_lock required because worker thread uses this
900 * list.
901 */
902 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
903 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
904 &aborts);
905 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
906 spin_unlock_irq(&phba->hbalock);
907
908 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
909 psb->pCmd = NULL;
910 psb->status = IOSTAT_SUCCESS;
911 }
912 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
913 list_splice(&aborts, &phba->lpfc_scsi_buf_list);
914 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
915 return 0;
916}
917
918/**
919 * lpfc_hba_down_post - Wrapper func for hba down post routine
920 * @phba: pointer to lpfc HBA data structure.
921 *
922 * This routine wraps the actual SLI3 or SLI4 routine for performing
923 * uninitialization after the HBA is reset when bring down the SLI Layer.
924 *
925 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200926 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400927 * Any other value - error.
928 **/
929int
930lpfc_hba_down_post(struct lpfc_hba *phba)
931{
932 return (*phba->lpfc_hba_down_post)(phba);
933}
Jamie Wellnitz41415862006-02-28 19:25:27 -0500934
James Smarte59058c2008-08-24 21:49:00 -0400935/**
James Smart3621a712009-04-06 18:47:14 -0400936 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -0400937 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
938 *
939 * This is the HBA-timer timeout handler registered to the lpfc driver. When
940 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
941 * work-port-events bitmap and the worker thread is notified. This timeout
942 * event will be used by the worker thread to invoke the actual timeout
943 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
944 * be performed in the timeout handler and the HBA timeout event bit shall
945 * be cleared by the worker thread after it has taken the event bitmap out.
946 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100947static void
James Smart858c9f62007-06-17 19:56:39 -0500948lpfc_hb_timeout(unsigned long ptr)
949{
950 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400951 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -0500952 unsigned long iflag;
953
954 phba = (struct lpfc_hba *)ptr;
James Smart93996272008-08-24 21:50:30 -0400955
956 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -0500957 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -0400958 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
959 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -0500960 phba->pport->work_port_events |= WORKER_HB_TMO;
961 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
962
James Smart93996272008-08-24 21:50:30 -0400963 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -0400964 if (!tmo_posted)
965 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -0500966 return;
967}
968
James Smarte59058c2008-08-24 21:49:00 -0400969/**
James Smart19ca7602010-11-20 23:11:55 -0500970 * lpfc_rrq_timeout - The RRQ-timer timeout handler
971 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
972 *
973 * This is the RRQ-timer timeout handler registered to the lpfc driver. When
974 * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
975 * work-port-events bitmap and the worker thread is notified. This timeout
976 * event will be used by the worker thread to invoke the actual timeout
977 * handler routine, lpfc_rrq_handler. Any periodical operations will
978 * be performed in the timeout handler and the RRQ timeout event bit shall
979 * be cleared by the worker thread after it has taken the event bitmap out.
980 **/
981static void
982lpfc_rrq_timeout(unsigned long ptr)
983{
984 struct lpfc_hba *phba;
James Smart19ca7602010-11-20 23:11:55 -0500985 unsigned long iflag;
986
987 phba = (struct lpfc_hba *)ptr;
988 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -0500989 phba->hba_flag |= HBA_RRQ_ACTIVE;
James Smart19ca7602010-11-20 23:11:55 -0500990 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -0500991 lpfc_worker_wake_up(phba);
James Smart19ca7602010-11-20 23:11:55 -0500992}
993
994/**
James Smart3621a712009-04-06 18:47:14 -0400995 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -0400996 * @phba: pointer to lpfc hba data structure.
997 * @pmboxq: pointer to the driver internal queue element for mailbox command.
998 *
999 * This is the callback function to the lpfc heart-beat mailbox command.
1000 * If configured, the lpfc driver issues the heart-beat mailbox command to
1001 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
1002 * heart-beat mailbox command is issued, the driver shall set up heart-beat
1003 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
1004 * heart-beat outstanding state. Once the mailbox command comes back and
1005 * no error conditions detected, the heart-beat mailbox command timer is
1006 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1007 * state is cleared for the next heart-beat. If the timer expired with the
1008 * heart-beat outstanding state set, the driver will put the HBA offline.
1009 **/
James Smart858c9f62007-06-17 19:56:39 -05001010static void
1011lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1012{
1013 unsigned long drvr_flag;
1014
1015 spin_lock_irqsave(&phba->hbalock, drvr_flag);
1016 phba->hb_outstanding = 0;
1017 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1018
James Smart93996272008-08-24 21:50:30 -04001019 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -05001020 mempool_free(pmboxq, phba->mbox_mem_pool);
1021 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1022 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -04001023 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -05001024 mod_timer(&phba->hb_tmofunc,
1025 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1026 return;
1027}
1028
James Smarte59058c2008-08-24 21:49:00 -04001029/**
James Smart3621a712009-04-06 18:47:14 -04001030 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001031 * @phba: pointer to lpfc hba data structure.
1032 *
1033 * This is the actual HBA-timer timeout handler to be invoked by the worker
1034 * thread whenever the HBA timer fired and HBA-timeout event posted. This
1035 * handler performs any periodic operations needed for the device. If such
1036 * periodic event has already been attended to either in the interrupt handler
1037 * or by processing slow-ring or fast-ring events within the HBA-timer
1038 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1039 * the timer for the next timeout period. If lpfc heart-beat mailbox command
1040 * is configured and there is no heart-beat mailbox command outstanding, a
1041 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1042 * has been a heart-beat mailbox command outstanding, the HBA shall be put
1043 * to offline.
1044 **/
James Smart858c9f62007-06-17 19:56:39 -05001045void
1046lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1047{
James Smart45ed1192009-10-02 15:17:02 -04001048 struct lpfc_vport **vports;
James Smart858c9f62007-06-17 19:56:39 -05001049 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -05001050 struct lpfc_dmabuf *buf_ptr;
James Smart45ed1192009-10-02 15:17:02 -04001051 int retval, i;
James Smart858c9f62007-06-17 19:56:39 -05001052 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -05001053 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -05001054
James Smart45ed1192009-10-02 15:17:02 -04001055 vports = lpfc_create_vport_work_array(phba);
1056 if (vports != NULL)
1057 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
1058 lpfc_rcv_seq_check_edtov(vports[i]);
1059 lpfc_destroy_vport_work_array(phba, vports);
1060
James Smart858c9f62007-06-17 19:56:39 -05001061 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -04001062 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -05001063 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1064 return;
1065
1066 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -05001067
1068 if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
1069 jiffies)) {
1070 spin_unlock_irq(&phba->pport->work_port_lock);
1071 if (!phba->hb_outstanding)
1072 mod_timer(&phba->hb_tmofunc,
1073 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1074 else
1075 mod_timer(&phba->hb_tmofunc,
1076 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
1077 return;
1078 }
1079 spin_unlock_irq(&phba->pport->work_port_lock);
1080
James Smart0ff10d42008-01-11 01:52:36 -05001081 if (phba->elsbuf_cnt &&
1082 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1083 spin_lock_irq(&phba->hbalock);
1084 list_splice_init(&phba->elsbuf, &completions);
1085 phba->elsbuf_cnt = 0;
1086 phba->elsbuf_prev_cnt = 0;
1087 spin_unlock_irq(&phba->hbalock);
1088
1089 while (!list_empty(&completions)) {
1090 list_remove_head(&completions, buf_ptr,
1091 struct lpfc_dmabuf, list);
1092 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1093 kfree(buf_ptr);
1094 }
1095 }
1096 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1097
James Smart858c9f62007-06-17 19:56:39 -05001098 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -05001099 if (phba->cfg_enable_hba_heartbeat) {
1100 if (!phba->hb_outstanding) {
James Smartbc739052010-08-04 16:11:18 -04001101 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1102 (list_empty(&psli->mboxq))) {
1103 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1104 GFP_KERNEL);
1105 if (!pmboxq) {
1106 mod_timer(&phba->hb_tmofunc,
1107 jiffies +
1108 HZ * LPFC_HB_MBOX_INTERVAL);
1109 return;
1110 }
James Smart13815c82008-01-11 01:52:48 -05001111
James Smartbc739052010-08-04 16:11:18 -04001112 lpfc_heart_beat(phba, pmboxq);
1113 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1114 pmboxq->vport = phba->pport;
1115 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1116 MBX_NOWAIT);
James Smart13815c82008-01-11 01:52:48 -05001117
James Smartbc739052010-08-04 16:11:18 -04001118 if (retval != MBX_BUSY &&
1119 retval != MBX_SUCCESS) {
1120 mempool_free(pmboxq,
1121 phba->mbox_mem_pool);
1122 mod_timer(&phba->hb_tmofunc,
1123 jiffies +
1124 HZ * LPFC_HB_MBOX_INTERVAL);
1125 return;
1126 }
1127 phba->skipped_hb = 0;
1128 phba->hb_outstanding = 1;
1129 } else if (time_before_eq(phba->last_completion_time,
1130 phba->skipped_hb)) {
1131 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1132 "2857 Last completion time not "
1133 " updated in %d ms\n",
1134 jiffies_to_msecs(jiffies
1135 - phba->last_completion_time));
1136 } else
1137 phba->skipped_hb = jiffies;
1138
James Smart858c9f62007-06-17 19:56:39 -05001139 mod_timer(&phba->hb_tmofunc,
James Smart13815c82008-01-11 01:52:48 -05001140 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
James Smart858c9f62007-06-17 19:56:39 -05001141 return;
James Smart13815c82008-01-11 01:52:48 -05001142 } else {
1143 /*
1144 * If heart beat timeout called with hb_outstanding set
James Smartdcf2a4e2010-09-29 11:18:53 -04001145 * we need to give the hb mailbox cmd a chance to
1146 * complete or TMO.
James Smart13815c82008-01-11 01:52:48 -05001147 */
James Smartdcf2a4e2010-09-29 11:18:53 -04001148 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1149 "0459 Adapter heartbeat still out"
1150 "standing:last compl time was %d ms.\n",
1151 jiffies_to_msecs(jiffies
1152 - phba->last_completion_time));
1153 mod_timer(&phba->hb_tmofunc,
1154 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
James Smart858c9f62007-06-17 19:56:39 -05001155 }
James Smart858c9f62007-06-17 19:56:39 -05001156 }
1157}
1158
James Smarte59058c2008-08-24 21:49:00 -04001159/**
James Smart3621a712009-04-06 18:47:14 -04001160 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -04001161 * @phba: pointer to lpfc hba data structure.
1162 *
1163 * This routine is called to bring the HBA offline when HBA hardware error
1164 * other than Port Error 6 has been detected.
1165 **/
James Smart09372822008-01-11 01:52:54 -05001166static void
1167lpfc_offline_eratt(struct lpfc_hba *phba)
1168{
1169 struct lpfc_sli *psli = &phba->sli;
1170
1171 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001172 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -05001173 spin_unlock_irq(&phba->hbalock);
James Smart618a5232012-06-12 13:54:36 -04001174 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart09372822008-01-11 01:52:54 -05001175
1176 lpfc_offline(phba);
1177 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001178 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001179 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001180 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001181 lpfc_hba_down_post(phba);
1182 lpfc_sli_brdready(phba, HS_MBRDY);
1183 lpfc_unblock_mgmt_io(phba);
1184 phba->link_state = LPFC_HBA_ERROR;
1185 return;
1186}
1187
James Smarte59058c2008-08-24 21:49:00 -04001188/**
James Smartda0436e2009-05-22 14:51:39 -04001189 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1190 * @phba: pointer to lpfc hba data structure.
1191 *
1192 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1193 * other than Port Error 6 has been detected.
1194 **/
1195static void
1196lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1197{
James Smart618a5232012-06-12 13:54:36 -04001198 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smartda0436e2009-05-22 14:51:39 -04001199 lpfc_offline(phba);
1200 lpfc_sli4_brdreset(phba);
1201 lpfc_hba_down_post(phba);
1202 lpfc_sli4_post_status_check(phba);
1203 lpfc_unblock_mgmt_io(phba);
1204 phba->link_state = LPFC_HBA_ERROR;
1205}
1206
1207/**
James Smarta257bf92009-04-06 18:48:10 -04001208 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1209 * @phba: pointer to lpfc hba data structure.
1210 *
1211 * This routine is invoked to handle the deferred HBA hardware error
1212 * conditions. This type of error is indicated by HBA by setting ER1
1213 * and another ER bit in the host status register. The driver will
1214 * wait until the ER1 bit clears before handling the error condition.
1215 **/
1216static void
1217lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1218{
1219 uint32_t old_host_status = phba->work_hs;
1220 struct lpfc_sli_ring *pring;
1221 struct lpfc_sli *psli = &phba->sli;
1222
James Smartf4b4c682009-05-22 14:53:12 -04001223 /* If the pci channel is offline, ignore possible errors,
1224 * since we cannot communicate with the pci card anyway.
1225 */
1226 if (pci_channel_offline(phba->pcidev)) {
1227 spin_lock_irq(&phba->hbalock);
1228 phba->hba_flag &= ~DEFER_ERATT;
1229 spin_unlock_irq(&phba->hbalock);
1230 return;
1231 }
1232
James Smarta257bf92009-04-06 18:48:10 -04001233 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1234 "0479 Deferred Adapter Hardware Error "
1235 "Data: x%x x%x x%x\n",
1236 phba->work_hs,
1237 phba->work_status[0], phba->work_status[1]);
1238
1239 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001240 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001241 spin_unlock_irq(&phba->hbalock);
1242
1243
1244 /*
1245 * Firmware stops when it triggred erratt. That could cause the I/Os
1246 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1247 * SCSI layer retry it after re-establishing link.
1248 */
1249 pring = &psli->ring[psli->fcp_ring];
1250 lpfc_sli_abort_iocb_ring(phba, pring);
1251
1252 /*
1253 * There was a firmware error. Take the hba offline and then
1254 * attempt to restart it.
1255 */
James Smart618a5232012-06-12 13:54:36 -04001256 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smarta257bf92009-04-06 18:48:10 -04001257 lpfc_offline(phba);
1258
1259 /* Wait for the ER1 bit to clear.*/
1260 while (phba->work_hs & HS_FFER1) {
1261 msleep(100);
James Smart9940b972011-03-11 16:06:12 -05001262 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1263 phba->work_hs = UNPLUG_ERR ;
1264 break;
1265 }
James Smarta257bf92009-04-06 18:48:10 -04001266 /* If driver is unloading let the worker thread continue */
1267 if (phba->pport->load_flag & FC_UNLOADING) {
1268 phba->work_hs = 0;
1269 break;
1270 }
1271 }
1272
1273 /*
1274 * This is to ptrotect against a race condition in which
1275 * first write to the host attention register clear the
1276 * host status register.
1277 */
1278 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1279 phba->work_hs = old_host_status & ~HS_FFER1;
1280
James Smart3772a992009-05-22 14:50:54 -04001281 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001282 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001283 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001284 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1285 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1286}
1287
James Smart3772a992009-05-22 14:50:54 -04001288static void
1289lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1290{
1291 struct lpfc_board_event_header board_event;
1292 struct Scsi_Host *shost;
1293
1294 board_event.event_type = FC_REG_BOARD_EVENT;
1295 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1296 shost = lpfc_shost_from_vport(phba->pport);
1297 fc_host_post_vendor_event(shost, fc_get_event_number(),
1298 sizeof(board_event),
1299 (char *) &board_event,
1300 LPFC_NL_VENDOR_ID);
1301}
1302
James Smarta257bf92009-04-06 18:48:10 -04001303/**
James Smart3772a992009-05-22 14:50:54 -04001304 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001305 * @phba: pointer to lpfc hba data structure.
1306 *
1307 * This routine is invoked to handle the following HBA hardware error
1308 * conditions:
1309 * 1 - HBA error attention interrupt
1310 * 2 - DMA ring index out of range
1311 * 3 - Mailbox command came back as unknown
1312 **/
James Smart3772a992009-05-22 14:50:54 -04001313static void
1314lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001315{
James Smart2e0fef82007-06-17 19:56:36 -05001316 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001317 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001318 struct lpfc_sli_ring *pring;
James Smartd2873e42006-08-18 17:46:43 -04001319 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001320 unsigned long temperature;
1321 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001322 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001323
Linas Vepstas8d63f372007-02-14 14:28:36 -06001324 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001325 * since we cannot communicate with the pci card anyway.
1326 */
1327 if (pci_channel_offline(phba->pcidev)) {
1328 spin_lock_irq(&phba->hbalock);
1329 phba->hba_flag &= ~DEFER_ERATT;
1330 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001331 return;
James Smart3772a992009-05-22 14:50:54 -04001332 }
1333
James Smart13815c82008-01-11 01:52:48 -05001334 /* If resets are disabled then leave the HBA alone and return */
1335 if (!phba->cfg_enable_hba_reset)
1336 return;
dea31012005-04-17 16:05:31 -05001337
James Smartea2151b2008-09-07 11:52:10 -04001338 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001339 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001340
James Smarta257bf92009-04-06 18:48:10 -04001341 if (phba->hba_flag & DEFER_ERATT)
1342 lpfc_handle_deferred_eratt(phba);
1343
James Smartdcf2a4e2010-09-29 11:18:53 -04001344 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1345 if (phba->work_hs & HS_FFER6)
1346 /* Re-establishing Link */
1347 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1348 "1301 Re-establishing Link "
1349 "Data: x%x x%x x%x\n",
1350 phba->work_hs, phba->work_status[0],
1351 phba->work_status[1]);
1352 if (phba->work_hs & HS_FFER8)
1353 /* Device Zeroization */
1354 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1355 "2861 Host Authentication device "
1356 "zeroization Data:x%x x%x x%x\n",
1357 phba->work_hs, phba->work_status[0],
1358 phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001359
James Smart92d7f7b2007-06-17 19:56:38 -05001360 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001361 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001362 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001363
1364 /*
1365 * Firmware stops when it triggled erratt with HS_FFER6.
1366 * That could cause the I/Os dropped by the firmware.
1367 * Error iocb (I/O) on txcmplq and let the SCSI layer
1368 * retry it after re-establishing link.
1369 */
1370 pring = &psli->ring[psli->fcp_ring];
1371 lpfc_sli_abort_iocb_ring(phba, pring);
1372
dea31012005-04-17 16:05:31 -05001373 /*
1374 * There was a firmware error. Take the hba offline and then
1375 * attempt to restart it.
1376 */
James Smart618a5232012-06-12 13:54:36 -04001377 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
dea31012005-04-17 16:05:31 -05001378 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001379 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001380 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001381 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001382 return;
1383 }
James Smart46fa3112007-04-25 09:51:45 -04001384 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001385 } else if (phba->work_hs & HS_CRIT_TEMP) {
1386 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1387 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1388 temp_event_data.event_code = LPFC_CRIT_TEMP;
1389 temp_event_data.data = (uint32_t)temperature;
1390
1391 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001392 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001393 "(%ld), taking this port offline "
1394 "Data: x%x x%x x%x\n",
1395 temperature, phba->work_hs,
1396 phba->work_status[0], phba->work_status[1]);
1397
1398 shost = lpfc_shost_from_vport(phba->pport);
1399 fc_host_post_vendor_event(shost, fc_get_event_number(),
1400 sizeof(temp_event_data),
1401 (char *) &temp_event_data,
1402 SCSI_NL_VID_TYPE_PCI
1403 | PCI_VENDOR_ID_EMULEX);
1404
James Smart7af67052007-10-27 13:38:11 -04001405 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001406 phba->over_temp_state = HBA_OVER_TEMP;
1407 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001408 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001409
dea31012005-04-17 16:05:31 -05001410 } else {
1411 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001412 * failure is a value other than FFER6. Do not call the offline
1413 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001414 */
1415 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001416 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001417 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001418 phba->work_hs,
dea31012005-04-17 16:05:31 -05001419 phba->work_status[0], phba->work_status[1]);
1420
James Smartd2873e42006-08-18 17:46:43 -04001421 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001422 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001423 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001424 sizeof(event_data), (char *) &event_data,
1425 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1426
James Smart09372822008-01-11 01:52:54 -05001427 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001428 }
James Smart93996272008-08-24 21:50:30 -04001429 return;
dea31012005-04-17 16:05:31 -05001430}
1431
James Smarte59058c2008-08-24 21:49:00 -04001432/**
James Smart618a5232012-06-12 13:54:36 -04001433 * lpfc_sli4_port_sta_fn_reset - The SLI4 function reset due to port status reg
1434 * @phba: pointer to lpfc hba data structure.
1435 * @mbx_action: flag for mailbox shutdown action.
1436 *
1437 * This routine is invoked to perform an SLI4 port PCI function reset in
1438 * response to port status register polling attention. It waits for port
1439 * status register (ERR, RDY, RN) bits before proceeding with function reset.
1440 * During this process, interrupt vectors are freed and later requested
1441 * for handling possible port resource change.
1442 **/
1443static int
1444lpfc_sli4_port_sta_fn_reset(struct lpfc_hba *phba, int mbx_action)
1445{
1446 int rc;
1447 uint32_t intr_mode;
1448
1449 /*
1450 * On error status condition, driver need to wait for port
1451 * ready before performing reset.
1452 */
1453 rc = lpfc_sli4_pdev_status_reg_wait(phba);
1454 if (!rc) {
1455 /* need reset: attempt for port recovery */
1456 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1457 "2887 Reset Needed: Attempting Port "
1458 "Recovery...\n");
1459 lpfc_offline_prep(phba, mbx_action);
1460 lpfc_offline(phba);
1461 /* release interrupt for possible resource change */
1462 lpfc_sli4_disable_intr(phba);
1463 lpfc_sli_brdrestart(phba);
1464 /* request and enable interrupt */
1465 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
1466 if (intr_mode == LPFC_INTR_ERROR) {
1467 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1468 "3175 Failed to enable interrupt\n");
1469 return -EIO;
1470 } else {
1471 phba->intr_mode = intr_mode;
1472 }
1473 rc = lpfc_online(phba);
1474 if (rc == 0)
1475 lpfc_unblock_mgmt_io(phba);
1476 }
1477 return rc;
1478}
1479
1480/**
James Smartda0436e2009-05-22 14:51:39 -04001481 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1482 * @phba: pointer to lpfc hba data structure.
1483 *
1484 * This routine is invoked to handle the SLI4 HBA hardware error attention
1485 * conditions.
1486 **/
1487static void
1488lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1489{
1490 struct lpfc_vport *vport = phba->pport;
1491 uint32_t event_data;
1492 struct Scsi_Host *shost;
James Smart2fcee4b2010-12-15 17:57:46 -05001493 uint32_t if_type;
James Smart2e90f4b2011-12-13 13:22:37 -05001494 struct lpfc_register portstat_reg = {0};
1495 uint32_t reg_err1, reg_err2;
1496 uint32_t uerrlo_reg, uemasklo_reg;
1497 uint32_t pci_rd_rc1, pci_rd_rc2;
James Smart73d91e52011-10-10 21:32:10 -04001498 int rc;
James Smartda0436e2009-05-22 14:51:39 -04001499
1500 /* If the pci channel is offline, ignore possible errors, since
1501 * we cannot communicate with the pci card anyway.
1502 */
1503 if (pci_channel_offline(phba->pcidev))
1504 return;
1505 /* If resets are disabled then leave the HBA alone and return */
1506 if (!phba->cfg_enable_hba_reset)
1507 return;
1508
James Smart2fcee4b2010-12-15 17:57:46 -05001509 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1510 switch (if_type) {
1511 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart2e90f4b2011-12-13 13:22:37 -05001512 pci_rd_rc1 = lpfc_readl(
1513 phba->sli4_hba.u.if_type0.UERRLOregaddr,
1514 &uerrlo_reg);
1515 pci_rd_rc2 = lpfc_readl(
1516 phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
1517 &uemasklo_reg);
1518 /* consider PCI bus read error as pci_channel_offline */
1519 if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
1520 return;
James Smart2fcee4b2010-12-15 17:57:46 -05001521 lpfc_sli4_offline_eratt(phba);
1522 break;
1523 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart2e90f4b2011-12-13 13:22:37 -05001524 pci_rd_rc1 = lpfc_readl(
1525 phba->sli4_hba.u.if_type2.STATUSregaddr,
1526 &portstat_reg.word0);
1527 /* consider PCI bus read error as pci_channel_offline */
James Smart6b5151f2012-01-18 16:24:06 -05001528 if (pci_rd_rc1 == -EIO) {
1529 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1530 "3151 PCI bus read access failure: x%x\n",
1531 readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
James Smart2e90f4b2011-12-13 13:22:37 -05001532 return;
James Smart6b5151f2012-01-18 16:24:06 -05001533 }
James Smart2e90f4b2011-12-13 13:22:37 -05001534 reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
1535 reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
James Smart2fcee4b2010-12-15 17:57:46 -05001536 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
1537 /* TODO: Register for Overtemp async events. */
1538 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1539 "2889 Port Overtemperature event, "
James Smart026abb82011-12-13 13:20:45 -05001540 "taking port offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001541 spin_lock_irq(&phba->hbalock);
1542 phba->over_temp_state = HBA_OVER_TEMP;
1543 spin_unlock_irq(&phba->hbalock);
1544 lpfc_sli4_offline_eratt(phba);
James Smart2e90f4b2011-12-13 13:22:37 -05001545 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001546 }
James Smart2e90f4b2011-12-13 13:22:37 -05001547 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1548 reg_err2 == SLIPORT_ERR2_REG_FW_RESTART)
1549 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1550 "3143 Port Down: Firmware Restarted\n");
1551 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1552 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1553 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1554 "3144 Port Down: Debug Dump\n");
1555 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1556 reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
1557 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1558 "3145 Port Down: Provisioning\n");
James Smart618a5232012-06-12 13:54:36 -04001559
1560 /* Check port status register for function reset */
1561 rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT);
1562 if (rc == 0) {
1563 /* don't report event on forced debug dump */
1564 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1565 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1566 return;
1567 else
1568 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001569 }
James Smart618a5232012-06-12 13:54:36 -04001570 /* fall through for not able to recover */
James Smart6b5151f2012-01-18 16:24:06 -05001571 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1572 "3152 Unrecoverable error, bring the port "
1573 "offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001574 lpfc_sli4_offline_eratt(phba);
1575 break;
1576 case LPFC_SLI_INTF_IF_TYPE_1:
1577 default:
1578 break;
1579 }
James Smart2e90f4b2011-12-13 13:22:37 -05001580 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1581 "3123 Report dump event to upper layer\n");
1582 /* Send an internal error event to mgmt application */
1583 lpfc_board_errevt_to_mgmt(phba);
1584
1585 event_data = FC_REG_DUMP_EVENT;
1586 shost = lpfc_shost_from_vport(vport);
1587 fc_host_post_vendor_event(shost, fc_get_event_number(),
1588 sizeof(event_data), (char *) &event_data,
1589 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
James Smartda0436e2009-05-22 14:51:39 -04001590}
1591
1592/**
1593 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1594 * @phba: pointer to lpfc HBA data structure.
1595 *
1596 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1597 * routine from the API jump table function pointer from the lpfc_hba struct.
1598 *
1599 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001600 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001601 * Any other value - error.
1602 **/
1603void
1604lpfc_handle_eratt(struct lpfc_hba *phba)
1605{
1606 (*phba->lpfc_handle_eratt)(phba);
1607}
1608
1609/**
James Smart3621a712009-04-06 18:47:14 -04001610 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04001611 * @phba: pointer to lpfc hba data structure.
1612 *
1613 * This routine is invoked from the worker thread to handle a HBA host
1614 * attention link event.
1615 **/
dea31012005-04-17 16:05:31 -05001616void
James Smart2e0fef82007-06-17 19:56:36 -05001617lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001618{
James Smart2e0fef82007-06-17 19:56:36 -05001619 struct lpfc_vport *vport = phba->pport;
1620 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001621 LPFC_MBOXQ_t *pmb;
1622 volatile uint32_t control;
1623 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05001624 int rc = 0;
dea31012005-04-17 16:05:31 -05001625
1626 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001627 if (!pmb) {
1628 rc = 1;
dea31012005-04-17 16:05:31 -05001629 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05001630 }
dea31012005-04-17 16:05:31 -05001631
1632 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001633 if (!mp) {
1634 rc = 2;
dea31012005-04-17 16:05:31 -05001635 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05001636 }
dea31012005-04-17 16:05:31 -05001637
1638 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05001639 if (!mp->virt) {
1640 rc = 3;
dea31012005-04-17 16:05:31 -05001641 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05001642 }
dea31012005-04-17 16:05:31 -05001643
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05001644 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04001645 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05001646
1647 psli->slistat.link_event++;
James Smart76a95d72010-11-20 23:11:48 -05001648 lpfc_read_topology(phba, pmb, mp);
1649 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smart2e0fef82007-06-17 19:56:36 -05001650 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04001651 /* Block ELS IOCBs until we have processed this mbox command */
1652 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04001653 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05001654 if (rc == MBX_NOT_FINISHED) {
1655 rc = 4;
James Smart14691152006-12-02 13:34:28 -05001656 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05001657 }
dea31012005-04-17 16:05:31 -05001658
1659 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05001660 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001661 writel(HA_LATT, phba->HAregaddr);
1662 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001663 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001664
1665 return;
1666
James Smart14691152006-12-02 13:34:28 -05001667lpfc_handle_latt_free_mbuf:
James Smart0d2b6b82008-06-14 22:52:47 -04001668 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05001669 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001670lpfc_handle_latt_free_mp:
1671 kfree(mp);
1672lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04001673 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001674lpfc_handle_latt_err_exit:
1675 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001676 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001677 psli->sli_flag |= LPFC_PROCESS_LA;
1678 control = readl(phba->HCregaddr);
1679 control |= HC_LAINT_ENA;
1680 writel(control, phba->HCregaddr);
1681 readl(phba->HCregaddr); /* flush */
1682
1683 /* Clear Link Attention in HA REG */
1684 writel(HA_LATT, phba->HAregaddr);
1685 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001686 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001687 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001688 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001689
James Smart09372822008-01-11 01:52:54 -05001690 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1691 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05001692
1693 return;
1694}
1695
James Smarte59058c2008-08-24 21:49:00 -04001696/**
James Smart3621a712009-04-06 18:47:14 -04001697 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04001698 * @phba: pointer to lpfc hba data structure.
1699 * @vpd: pointer to the vital product data.
1700 * @len: length of the vital product data in bytes.
1701 *
1702 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1703 * an array of characters. In this routine, the ModelName, ProgramType, and
1704 * ModelDesc, etc. fields of the phba data structure will be populated.
1705 *
1706 * Return codes
1707 * 0 - pointer to the VPD passed in is NULL
1708 * 1 - success
1709 **/
James Smart3772a992009-05-22 14:50:54 -04001710int
James Smart2e0fef82007-06-17 19:56:36 -05001711lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05001712{
1713 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05001714 int Length;
dea31012005-04-17 16:05:31 -05001715 int i, j;
1716 int finished = 0;
1717 int index = 0;
1718
1719 if (!vpd)
1720 return 0;
1721
1722 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05001723 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001724 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05001725 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1726 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001727 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05001728 switch (vpd[index]) {
1729 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001730 case 0x91:
dea31012005-04-17 16:05:31 -05001731 index += 1;
1732 lenlo = vpd[index];
1733 index += 1;
1734 lenhi = vpd[index];
1735 index += 1;
1736 i = ((((unsigned short)lenhi) << 8) + lenlo);
1737 index += i;
1738 break;
1739 case 0x90:
1740 index += 1;
1741 lenlo = vpd[index];
1742 index += 1;
1743 lenhi = vpd[index];
1744 index += 1;
1745 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001746 if (Length > len - index)
1747 Length = len - index;
dea31012005-04-17 16:05:31 -05001748 while (Length > 0) {
1749 /* Look for Serial Number */
1750 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1751 index += 2;
1752 i = vpd[index];
1753 index += 1;
1754 j = 0;
1755 Length -= (3+i);
1756 while(i--) {
1757 phba->SerialNumber[j++] = vpd[index++];
1758 if (j == 31)
1759 break;
1760 }
1761 phba->SerialNumber[j] = 0;
1762 continue;
1763 }
1764 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1765 phba->vpd_flag |= VPD_MODEL_DESC;
1766 index += 2;
1767 i = vpd[index];
1768 index += 1;
1769 j = 0;
1770 Length -= (3+i);
1771 while(i--) {
1772 phba->ModelDesc[j++] = vpd[index++];
1773 if (j == 255)
1774 break;
1775 }
1776 phba->ModelDesc[j] = 0;
1777 continue;
1778 }
1779 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1780 phba->vpd_flag |= VPD_MODEL_NAME;
1781 index += 2;
1782 i = vpd[index];
1783 index += 1;
1784 j = 0;
1785 Length -= (3+i);
1786 while(i--) {
1787 phba->ModelName[j++] = vpd[index++];
1788 if (j == 79)
1789 break;
1790 }
1791 phba->ModelName[j] = 0;
1792 continue;
1793 }
1794 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1795 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1796 index += 2;
1797 i = vpd[index];
1798 index += 1;
1799 j = 0;
1800 Length -= (3+i);
1801 while(i--) {
1802 phba->ProgramType[j++] = vpd[index++];
1803 if (j == 255)
1804 break;
1805 }
1806 phba->ProgramType[j] = 0;
1807 continue;
1808 }
1809 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1810 phba->vpd_flag |= VPD_PORT;
1811 index += 2;
1812 i = vpd[index];
1813 index += 1;
1814 j = 0;
1815 Length -= (3+i);
1816 while(i--) {
James Smartcd1c8302011-10-10 21:33:25 -04001817 if ((phba->sli_rev == LPFC_SLI_REV4) &&
1818 (phba->sli4_hba.pport_name_sta ==
1819 LPFC_SLI4_PPNAME_GET)) {
1820 j++;
1821 index++;
1822 } else
1823 phba->Port[j++] = vpd[index++];
1824 if (j == 19)
1825 break;
dea31012005-04-17 16:05:31 -05001826 }
James Smartcd1c8302011-10-10 21:33:25 -04001827 if ((phba->sli_rev != LPFC_SLI_REV4) ||
1828 (phba->sli4_hba.pport_name_sta ==
1829 LPFC_SLI4_PPNAME_NON))
1830 phba->Port[j] = 0;
dea31012005-04-17 16:05:31 -05001831 continue;
1832 }
1833 else {
1834 index += 2;
1835 i = vpd[index];
1836 index += 1;
1837 index += i;
1838 Length -= (3 + i);
1839 }
1840 }
1841 finished = 0;
1842 break;
1843 case 0x78:
1844 finished = 1;
1845 break;
1846 default:
1847 index ++;
1848 break;
1849 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001850 }
dea31012005-04-17 16:05:31 -05001851
1852 return(1);
1853}
1854
James Smarte59058c2008-08-24 21:49:00 -04001855/**
James Smart3621a712009-04-06 18:47:14 -04001856 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04001857 * @phba: pointer to lpfc hba data structure.
1858 * @mdp: pointer to the data structure to hold the derived model name.
1859 * @descp: pointer to the data structure to hold the derived description.
1860 *
1861 * This routine retrieves HBA's description based on its registered PCI device
1862 * ID. The @descp passed into this function points to an array of 256 chars. It
1863 * shall be returned with the model name, maximum speed, and the host bus type.
1864 * The @mdp passed into this function points to an array of 80 chars. When the
1865 * function returns, the @mdp will be filled with the model name.
1866 **/
dea31012005-04-17 16:05:31 -05001867static void
James Smart2e0fef82007-06-17 19:56:36 -05001868lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05001869{
1870 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05001871 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001872 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04001873 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04001874 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001875 struct {
James Smarta747c9c2009-11-18 15:41:10 -05001876 char *name;
1877 char *bus;
1878 char *function;
1879 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001880
1881 if (mdp && mdp[0] != '\0'
1882 && descp && descp[0] != '\0')
1883 return;
1884
James Smartc0c11512011-05-24 11:41:34 -04001885 if (phba->lmt & LMT_16Gb)
1886 max_speed = 16;
1887 else if (phba->lmt & LMT_10Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001888 max_speed = 10;
1889 else if (phba->lmt & LMT_8Gb)
1890 max_speed = 8;
1891 else if (phba->lmt & LMT_4Gb)
1892 max_speed = 4;
1893 else if (phba->lmt & LMT_2Gb)
1894 max_speed = 2;
1895 else
1896 max_speed = 1;
dea31012005-04-17 16:05:31 -05001897
1898 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05001899
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001900 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001901 case PCI_DEVICE_ID_FIREFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001902 m = (typeof(m)){"LP6000", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001903 break;
dea31012005-04-17 16:05:31 -05001904 case PCI_DEVICE_ID_SUPERFLY:
1905 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smarta747c9c2009-11-18 15:41:10 -05001906 m = (typeof(m)){"LP7000", "PCI",
1907 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001908 else
James Smarta747c9c2009-11-18 15:41:10 -05001909 m = (typeof(m)){"LP7000E", "PCI",
1910 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001911 break;
1912 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001913 m = (typeof(m)){"LP8000", "PCI",
1914 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001915 break;
1916 case PCI_DEVICE_ID_CENTAUR:
1917 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smarta747c9c2009-11-18 15:41:10 -05001918 m = (typeof(m)){"LP9002", "PCI",
1919 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001920 else
James Smarta747c9c2009-11-18 15:41:10 -05001921 m = (typeof(m)){"LP9000", "PCI",
1922 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001923 break;
1924 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001925 m = (typeof(m)){"LP952", "PCI",
1926 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001927 break;
1928 case PCI_DEVICE_ID_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05001929 m = (typeof(m)){"LP9802", "PCI-X",
1930 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001931 break;
1932 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05001933 m = (typeof(m)){"LP10000", "PCI-X",
1934 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001935 break;
1936 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05001937 m = (typeof(m)){"LPX1000", "PCI-X",
1938 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001939 break;
1940 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001941 m = (typeof(m)){"LP982", "PCI-X",
1942 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001943 break;
1944 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001945 m = (typeof(m)){"LP1050", "PCI-X",
1946 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001947 break;
1948 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05001949 m = (typeof(m)){"LP11000", "PCI-X2",
1950 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001951 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001952 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001953 m = (typeof(m)){"LP11000-SP", "PCI-X2",
1954 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001955 break;
1956 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001957 m = (typeof(m)){"LP11002-SP", "PCI-X2",
1958 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001959 break;
1960 case PCI_DEVICE_ID_NEPTUNE:
James Smarta747c9c2009-11-18 15:41:10 -05001961 m = (typeof(m)){"LPe1000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001962 break;
1963 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001964 m = (typeof(m)){"LPe1000-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001965 break;
1966 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001967 m = (typeof(m)){"LPe1002-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001968 break;
dea31012005-04-17 16:05:31 -05001969 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05001970 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001971 break;
1972 case PCI_DEVICE_ID_BSMB:
James Smarta747c9c2009-11-18 15:41:10 -05001973 m = (typeof(m)){"LP111", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001974 break;
1975 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05001976 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001977 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001978 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001979 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001980 break;
1981 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001982 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04001983 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001984 break;
dea31012005-04-17 16:05:31 -05001985 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05001986 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001987 break;
1988 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05001989 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001990 break;
1991 case PCI_DEVICE_ID_LP101:
James Smarta747c9c2009-11-18 15:41:10 -05001992 m = (typeof(m)){"LP101", "PCI-X", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001993 break;
1994 case PCI_DEVICE_ID_LP10000S:
James Smarta747c9c2009-11-18 15:41:10 -05001995 m = (typeof(m)){"LP10000-S", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001996 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001997 case PCI_DEVICE_ID_LP11000S:
James Smarta747c9c2009-11-18 15:41:10 -05001998 m = (typeof(m)){"LP11000-S", "PCI-X2", "Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05001999 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002000 case PCI_DEVICE_ID_LPE11000S:
James Smarta747c9c2009-11-18 15:41:10 -05002001 m = (typeof(m)){"LPe11000-S", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002002 break;
James Smartb87eab32007-04-25 09:53:28 -04002003 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05002004 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002005 break;
2006 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05002007 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002008 break;
2009 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05002010 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002011 break;
2012 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002013 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002014 break;
2015 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002016 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002017 break;
2018 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05002019 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002020 break;
James Smart84774a42008-08-24 21:50:06 -04002021 case PCI_DEVICE_ID_HORNET:
James Smarta747c9c2009-11-18 15:41:10 -05002022 m = (typeof(m)){"LP21000", "PCIe", "FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002023 GE = 1;
2024 break;
2025 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05002026 m = (typeof(m)){"LPev12000", "PCIe IOV",
2027 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002028 break;
2029 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05002030 m = (typeof(m)){"LPev12000", "PCIe IOV",
2031 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002032 break;
2033 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05002034 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
2035 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002036 break;
James Smartda0436e2009-05-22 14:51:39 -04002037 case PCI_DEVICE_ID_TIGERSHARK:
2038 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002039 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04002040 break;
James Smarta747c9c2009-11-18 15:41:10 -05002041 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04002042 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002043 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2044 break;
2045 case PCI_DEVICE_ID_FALCON:
2046 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2047 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04002048 break;
James Smart98fc5dd2010-06-07 15:24:29 -04002049 case PCI_DEVICE_ID_BALIUS:
2050 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
2051 "Fibre Channel Adapter"};
2052 break;
James Smart085c6472010-11-20 23:11:37 -05002053 case PCI_DEVICE_ID_LANCER_FC:
James Smartc0c11512011-05-24 11:41:34 -04002054 case PCI_DEVICE_ID_LANCER_FC_VF:
2055 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
James Smart085c6472010-11-20 23:11:37 -05002056 break;
2057 case PCI_DEVICE_ID_LANCER_FCOE:
James Smartc0c11512011-05-24 11:41:34 -04002058 case PCI_DEVICE_ID_LANCER_FCOE_VF:
James Smart085c6472010-11-20 23:11:37 -05002059 oneConnect = 1;
James Smart079b5c92011-08-21 21:48:49 -04002060 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
James Smart085c6472010-11-20 23:11:37 -05002061 break;
James Smartf8cafd32012-08-03 12:42:51 -04002062 case PCI_DEVICE_ID_SKYHAWK:
2063 case PCI_DEVICE_ID_SKYHAWK_VF:
2064 oneConnect = 1;
2065 m = (typeof(m)){"OCe14000", "PCIe", "FCoE"};
2066 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002067 default:
James Smarta747c9c2009-11-18 15:41:10 -05002068 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002069 break;
dea31012005-04-17 16:05:31 -05002070 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002071
2072 if (mdp && mdp[0] == '\0')
2073 snprintf(mdp, 79,"%s", m.name);
James Smartc0c11512011-05-24 11:41:34 -04002074 /*
2075 * oneConnect hba requires special processing, they are all initiators
James Smartda0436e2009-05-22 14:51:39 -04002076 * and we put the port number on the end
2077 */
2078 if (descp && descp[0] == '\0') {
2079 if (oneConnect)
2080 snprintf(descp, 255,
James Smarta747c9c2009-11-18 15:41:10 -05002081 "Emulex OneConnect %s, %s Initiator, Port %s",
2082 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04002083 phba->Port);
2084 else
2085 snprintf(descp, 255,
2086 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05002087 m.name, max_speed, (GE) ? "GE" : "Gb",
2088 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002089 }
dea31012005-04-17 16:05:31 -05002090}
2091
James Smarte59058c2008-08-24 21:49:00 -04002092/**
James Smart3621a712009-04-06 18:47:14 -04002093 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04002094 * @phba: pointer to lpfc hba data structure.
2095 * @pring: pointer to a IOCB ring.
2096 * @cnt: the number of IOCBs to be posted to the IOCB ring.
2097 *
2098 * This routine posts a given number of IOCBs with the associated DMA buffer
2099 * descriptors specified by the cnt argument to the given IOCB ring.
2100 *
2101 * Return codes
2102 * The number of IOCBs NOT able to be posted to the IOCB ring.
2103 **/
dea31012005-04-17 16:05:31 -05002104int
James Smart495a7142008-06-14 22:52:59 -04002105lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05002106{
2107 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002108 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05002109 struct lpfc_dmabuf *mp1, *mp2;
2110
2111 cnt += pring->missbufcnt;
2112
2113 /* While there are buffers to post */
2114 while (cnt > 0) {
2115 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002116 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002117 if (iocb == NULL) {
2118 pring->missbufcnt = cnt;
2119 return cnt;
2120 }
dea31012005-04-17 16:05:31 -05002121 icmd = &iocb->iocb;
2122
2123 /* 2 buffers can be posted per command */
2124 /* Allocate buffer to post */
2125 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2126 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04002127 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2128 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002129 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002130 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002131 pring->missbufcnt = cnt;
2132 return cnt;
2133 }
2134
2135 INIT_LIST_HEAD(&mp1->list);
2136 /* Allocate buffer to post */
2137 if (cnt > 1) {
2138 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2139 if (mp2)
2140 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2141 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04002142 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002143 kfree(mp2);
dea31012005-04-17 16:05:31 -05002144 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2145 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002146 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002147 pring->missbufcnt = cnt;
2148 return cnt;
2149 }
2150
2151 INIT_LIST_HEAD(&mp2->list);
2152 } else {
2153 mp2 = NULL;
2154 }
2155
2156 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2157 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2158 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2159 icmd->ulpBdeCount = 1;
2160 cnt--;
2161 if (mp2) {
2162 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2163 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2164 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2165 cnt--;
2166 icmd->ulpBdeCount = 2;
2167 }
2168
2169 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2170 icmd->ulpLe = 1;
2171
James Smart3772a992009-05-22 14:50:54 -04002172 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2173 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05002174 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2175 kfree(mp1);
2176 cnt++;
2177 if (mp2) {
2178 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2179 kfree(mp2);
2180 cnt++;
2181 }
James Bottomley604a3e32005-10-29 10:28:33 -05002182 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002183 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05002184 return cnt;
2185 }
dea31012005-04-17 16:05:31 -05002186 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05002187 if (mp2)
dea31012005-04-17 16:05:31 -05002188 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05002189 }
2190 pring->missbufcnt = 0;
2191 return 0;
2192}
2193
James Smarte59058c2008-08-24 21:49:00 -04002194/**
James Smart3621a712009-04-06 18:47:14 -04002195 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04002196 * @phba: pointer to lpfc hba data structure.
2197 *
2198 * This routine posts initial receive IOCB buffers to the ELS ring. The
2199 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
2200 * set to 64 IOCBs.
2201 *
2202 * Return codes
2203 * 0 - success (currently always success)
2204 **/
dea31012005-04-17 16:05:31 -05002205static int
James Smart2e0fef82007-06-17 19:56:36 -05002206lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002207{
2208 struct lpfc_sli *psli = &phba->sli;
2209
2210 /* Ring 0, ELS / CT buffers */
James Smart495a7142008-06-14 22:52:59 -04002211 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05002212 /* Ring 2 - FCP no buffers needed */
2213
2214 return 0;
2215}
2216
2217#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2218
James Smarte59058c2008-08-24 21:49:00 -04002219/**
James Smart3621a712009-04-06 18:47:14 -04002220 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04002221 * @HashResultPointer: pointer to an array as hash table.
2222 *
2223 * This routine sets up the initial values to the array of hash table entries
2224 * for the LC HBAs.
2225 **/
dea31012005-04-17 16:05:31 -05002226static void
2227lpfc_sha_init(uint32_t * HashResultPointer)
2228{
2229 HashResultPointer[0] = 0x67452301;
2230 HashResultPointer[1] = 0xEFCDAB89;
2231 HashResultPointer[2] = 0x98BADCFE;
2232 HashResultPointer[3] = 0x10325476;
2233 HashResultPointer[4] = 0xC3D2E1F0;
2234}
2235
James Smarte59058c2008-08-24 21:49:00 -04002236/**
James Smart3621a712009-04-06 18:47:14 -04002237 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04002238 * @HashResultPointer: pointer to an initial/result hash table.
2239 * @HashWorkingPointer: pointer to an working hash table.
2240 *
2241 * This routine iterates an initial hash table pointed by @HashResultPointer
2242 * with the values from the working hash table pointeed by @HashWorkingPointer.
2243 * The results are putting back to the initial hash table, returned through
2244 * the @HashResultPointer as the result hash table.
2245 **/
dea31012005-04-17 16:05:31 -05002246static void
2247lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2248{
2249 int t;
2250 uint32_t TEMP;
2251 uint32_t A, B, C, D, E;
2252 t = 16;
2253 do {
2254 HashWorkingPointer[t] =
2255 S(1,
2256 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2257 8] ^
2258 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2259 } while (++t <= 79);
2260 t = 0;
2261 A = HashResultPointer[0];
2262 B = HashResultPointer[1];
2263 C = HashResultPointer[2];
2264 D = HashResultPointer[3];
2265 E = HashResultPointer[4];
2266
2267 do {
2268 if (t < 20) {
2269 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2270 } else if (t < 40) {
2271 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2272 } else if (t < 60) {
2273 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2274 } else {
2275 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2276 }
2277 TEMP += S(5, A) + E + HashWorkingPointer[t];
2278 E = D;
2279 D = C;
2280 C = S(30, B);
2281 B = A;
2282 A = TEMP;
2283 } while (++t <= 79);
2284
2285 HashResultPointer[0] += A;
2286 HashResultPointer[1] += B;
2287 HashResultPointer[2] += C;
2288 HashResultPointer[3] += D;
2289 HashResultPointer[4] += E;
2290
2291}
2292
James Smarte59058c2008-08-24 21:49:00 -04002293/**
James Smart3621a712009-04-06 18:47:14 -04002294 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002295 * @RandomChallenge: pointer to the entry of host challenge random number array.
2296 * @HashWorking: pointer to the entry of the working hash array.
2297 *
2298 * This routine calculates the working hash array referred by @HashWorking
2299 * from the challenge random numbers associated with the host, referred by
2300 * @RandomChallenge. The result is put into the entry of the working hash
2301 * array and returned by reference through @HashWorking.
2302 **/
dea31012005-04-17 16:05:31 -05002303static void
2304lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2305{
2306 *HashWorking = (*RandomChallenge ^ *HashWorking);
2307}
2308
James Smarte59058c2008-08-24 21:49:00 -04002309/**
James Smart3621a712009-04-06 18:47:14 -04002310 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002311 * @phba: pointer to lpfc hba data structure.
2312 * @hbainit: pointer to an array of unsigned 32-bit integers.
2313 *
2314 * This routine performs the special handling for LC HBA initialization.
2315 **/
dea31012005-04-17 16:05:31 -05002316void
2317lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2318{
2319 int t;
2320 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002321 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002322
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002323 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002324 if (!HashWorking)
2325 return;
2326
dea31012005-04-17 16:05:31 -05002327 HashWorking[0] = HashWorking[78] = *pwwnn++;
2328 HashWorking[1] = HashWorking[79] = *pwwnn;
2329
2330 for (t = 0; t < 7; t++)
2331 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2332
2333 lpfc_sha_init(hbainit);
2334 lpfc_sha_iterate(hbainit, HashWorking);
2335 kfree(HashWorking);
2336}
2337
James Smarte59058c2008-08-24 21:49:00 -04002338/**
James Smart3621a712009-04-06 18:47:14 -04002339 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002340 * @vport: pointer to a virtual N_Port data structure.
2341 *
2342 * This routine performs the necessary cleanups before deleting the @vport.
2343 * It invokes the discovery state machine to perform necessary state
2344 * transitions and to release the ndlps associated with the @vport. Note,
2345 * the physical port is treated as @vport 0.
2346 **/
James Smart87af33f2007-10-27 13:37:43 -04002347void
James Smart2e0fef82007-06-17 19:56:36 -05002348lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002349{
James Smart87af33f2007-10-27 13:37:43 -04002350 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002351 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002352 int i = 0;
dea31012005-04-17 16:05:31 -05002353
James Smart87af33f2007-10-27 13:37:43 -04002354 if (phba->link_state > LPFC_LINK_DOWN)
2355 lpfc_port_link_failure(vport);
2356
2357 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002358 if (!NLP_CHK_NODE_ACT(ndlp)) {
2359 ndlp = lpfc_enable_node(vport, ndlp,
2360 NLP_STE_UNUSED_NODE);
2361 if (!ndlp)
2362 continue;
2363 spin_lock_irq(&phba->ndlp_lock);
2364 NLP_SET_FREE_REQ(ndlp);
2365 spin_unlock_irq(&phba->ndlp_lock);
2366 /* Trigger the release of the ndlp memory */
2367 lpfc_nlp_put(ndlp);
2368 continue;
2369 }
2370 spin_lock_irq(&phba->ndlp_lock);
2371 if (NLP_CHK_FREE_REQ(ndlp)) {
2372 /* The ndlp should not be in memory free mode already */
2373 spin_unlock_irq(&phba->ndlp_lock);
2374 continue;
2375 } else
2376 /* Indicate request for freeing ndlp memory */
2377 NLP_SET_FREE_REQ(ndlp);
2378 spin_unlock_irq(&phba->ndlp_lock);
2379
James Smart58da1ff2008-04-07 10:15:56 -04002380 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2381 ndlp->nlp_DID == Fabric_DID) {
2382 /* Just free up ndlp with Fabric_DID for vports */
2383 lpfc_nlp_put(ndlp);
2384 continue;
2385 }
2386
James Smarteff4a012012-01-18 16:25:25 -05002387 /* take care of nodes in unused state before the state
2388 * machine taking action.
2389 */
2390 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
2391 lpfc_nlp_put(ndlp);
2392 continue;
2393 }
2394
James Smart87af33f2007-10-27 13:37:43 -04002395 if (ndlp->nlp_type & NLP_FABRIC)
2396 lpfc_disc_state_machine(vport, ndlp, NULL,
2397 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002398
James Smart87af33f2007-10-27 13:37:43 -04002399 lpfc_disc_state_machine(vport, ndlp, NULL,
2400 NLP_EVT_DEVICE_RM);
2401 }
2402
James Smarta8adb832007-10-27 13:37:53 -04002403 /* At this point, ALL ndlp's should be gone
2404 * because of the previous NLP_EVT_DEVICE_RM.
2405 * Lets wait for this to happen, if needed.
2406 */
James Smart87af33f2007-10-27 13:37:43 -04002407 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002408 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002409 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002410 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002411 list_for_each_entry_safe(ndlp, next_ndlp,
2412 &vport->fc_nodes, nlp_listp) {
2413 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2414 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002415 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002416 "usgmap:x%x refcnt:%d\n",
2417 ndlp->nlp_DID, (void *)ndlp,
2418 ndlp->nlp_usg_map,
2419 atomic_read(
2420 &ndlp->kref.refcount));
2421 }
James Smarta8adb832007-10-27 13:37:53 -04002422 break;
James Smart87af33f2007-10-27 13:37:43 -04002423 }
James Smarta8adb832007-10-27 13:37:53 -04002424
2425 /* Wait for any activity on ndlps to settle */
2426 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002427 }
James Smart1151e3e2011-02-16 12:39:35 -05002428 lpfc_cleanup_vports_rrqs(vport, NULL);
dea31012005-04-17 16:05:31 -05002429}
2430
James Smarte59058c2008-08-24 21:49:00 -04002431/**
James Smart3621a712009-04-06 18:47:14 -04002432 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002433 * @vport: pointer to a virtual N_Port data structure.
2434 *
2435 * This routine stops all the timers associated with a @vport. This function
2436 * is invoked before disabling or deleting a @vport. Note that the physical
2437 * port is treated as @vport 0.
2438 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002439void
2440lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002441{
James Smart92d7f7b2007-06-17 19:56:38 -05002442 del_timer_sync(&vport->els_tmofunc);
2443 del_timer_sync(&vport->fc_fdmitmo);
James Smart92494142011-02-16 12:39:44 -05002444 del_timer_sync(&vport->delayed_disc_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002445 lpfc_can_disctmo(vport);
2446 return;
dea31012005-04-17 16:05:31 -05002447}
2448
James Smarte59058c2008-08-24 21:49:00 -04002449/**
James Smartecfd03c2010-02-12 14:41:27 -05002450 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2451 * @phba: pointer to lpfc hba data structure.
2452 *
2453 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2454 * caller of this routine should already hold the host lock.
2455 **/
2456void
2457__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2458{
James Smart5ac6b302010-10-22 11:05:36 -04002459 /* Clear pending FCF rediscovery wait flag */
2460 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2461
James Smartecfd03c2010-02-12 14:41:27 -05002462 /* Now, try to stop the timer */
2463 del_timer(&phba->fcf.redisc_wait);
2464}
2465
2466/**
2467 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2468 * @phba: pointer to lpfc hba data structure.
2469 *
2470 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2471 * checks whether the FCF rediscovery wait timer is pending with the host
2472 * lock held before proceeding with disabling the timer and clearing the
2473 * wait timer pendig flag.
2474 **/
2475void
2476lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2477{
2478 spin_lock_irq(&phba->hbalock);
2479 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2480 /* FCF rediscovery timer already fired or stopped */
2481 spin_unlock_irq(&phba->hbalock);
2482 return;
2483 }
2484 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart5ac6b302010-10-22 11:05:36 -04002485 /* Clear failover in progress flags */
2486 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05002487 spin_unlock_irq(&phba->hbalock);
2488}
2489
2490/**
James Smart3772a992009-05-22 14:50:54 -04002491 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002492 * @phba: pointer to lpfc hba data structure.
2493 *
2494 * This routine stops all the timers associated with a HBA. This function is
2495 * invoked before either putting a HBA offline or unloading the driver.
2496 **/
James Smart3772a992009-05-22 14:50:54 -04002497void
2498lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002499{
James Smart51ef4c22007-08-02 11:10:31 -04002500 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05002501 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002502 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002503 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002504 del_timer_sync(&phba->hb_tmofunc);
James Smart1151e3e2011-02-16 12:39:35 -05002505 if (phba->sli_rev == LPFC_SLI_REV4) {
2506 del_timer_sync(&phba->rrq_tmr);
2507 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2508 }
James Smart3772a992009-05-22 14:50:54 -04002509 phba->hb_outstanding = 0;
2510
2511 switch (phba->pci_dev_grp) {
2512 case LPFC_PCI_DEV_LP:
2513 /* Stop any LightPulse device specific driver timers */
2514 del_timer_sync(&phba->fcp_poll_timer);
2515 break;
2516 case LPFC_PCI_DEV_OC:
2517 /* Stop any OneConnect device sepcific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05002518 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04002519 break;
2520 default:
2521 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2522 "0297 Invalid device group (x%x)\n",
2523 phba->pci_dev_grp);
2524 break;
2525 }
James Smart2e0fef82007-06-17 19:56:36 -05002526 return;
dea31012005-04-17 16:05:31 -05002527}
2528
James Smarte59058c2008-08-24 21:49:00 -04002529/**
James Smart3621a712009-04-06 18:47:14 -04002530 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002531 * @phba: pointer to lpfc hba data structure.
2532 *
2533 * This routine marks a HBA's management interface as blocked. Once the HBA's
2534 * management interface is marked as blocked, all the user space access to
2535 * the HBA, whether they are from sysfs interface or libdfc interface will
2536 * all be blocked. The HBA is set to block the management interface when the
2537 * driver prepares the HBA interface for online or offline.
2538 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002539static void
James Smart618a5232012-06-12 13:54:36 -04002540lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
Adrian Bunka6ababd2007-11-05 18:07:33 +01002541{
2542 unsigned long iflag;
James Smart6e7288d2010-06-07 15:23:35 -04002543 uint8_t actcmd = MBX_HEARTBEAT;
2544 unsigned long timeout;
2545
Adrian Bunka6ababd2007-11-05 18:07:33 +01002546 spin_lock_irqsave(&phba->hbalock, iflag);
2547 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
James Smart618a5232012-06-12 13:54:36 -04002548 spin_unlock_irqrestore(&phba->hbalock, iflag);
2549 if (mbx_action == LPFC_MBX_NO_WAIT)
2550 return;
2551 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
2552 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002553 if (phba->sli.mbox_active) {
James Smart6e7288d2010-06-07 15:23:35 -04002554 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
James Smarta183a152011-10-10 21:32:43 -04002555 /* Determine how long we might wait for the active mailbox
2556 * command to be gracefully completed by firmware.
2557 */
2558 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
2559 phba->sli.mbox_active) * 1000) + jiffies;
2560 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002561 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002562
James Smart6e7288d2010-06-07 15:23:35 -04002563 /* Wait for the outstnading mailbox command to complete */
2564 while (phba->sli.mbox_active) {
2565 /* Check active mailbox complete status every 2ms */
2566 msleep(2);
2567 if (time_after(jiffies, timeout)) {
2568 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2569 "2813 Mgmt IO is Blocked %x "
2570 "- mbox cmd %x still active\n",
2571 phba->sli.sli_flag, actcmd);
2572 break;
2573 }
2574 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002575}
2576
James Smarte59058c2008-08-24 21:49:00 -04002577/**
James Smart6b5151f2012-01-18 16:24:06 -05002578 * lpfc_sli4_node_prep - Assign RPIs for active nodes.
2579 * @phba: pointer to lpfc hba data structure.
2580 *
2581 * Allocate RPIs for all active remote nodes. This is needed whenever
2582 * an SLI4 adapter is reset and the driver is not unloading. Its purpose
2583 * is to fixup the temporary rpi assignments.
2584 **/
2585void
2586lpfc_sli4_node_prep(struct lpfc_hba *phba)
2587{
2588 struct lpfc_nodelist *ndlp, *next_ndlp;
2589 struct lpfc_vport **vports;
2590 int i;
2591
2592 if (phba->sli_rev != LPFC_SLI_REV4)
2593 return;
2594
2595 vports = lpfc_create_vport_work_array(phba);
2596 if (vports != NULL) {
2597 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2598 if (vports[i]->load_flag & FC_UNLOADING)
2599 continue;
2600
2601 list_for_each_entry_safe(ndlp, next_ndlp,
2602 &vports[i]->fc_nodes,
2603 nlp_listp) {
2604 if (NLP_CHK_NODE_ACT(ndlp))
2605 ndlp->nlp_rpi =
2606 lpfc_sli4_alloc_rpi(phba);
2607 }
2608 }
2609 }
2610 lpfc_destroy_vport_work_array(phba, vports);
2611}
2612
2613/**
James Smart3621a712009-04-06 18:47:14 -04002614 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04002615 * @phba: pointer to lpfc hba data structure.
2616 *
2617 * This routine initializes the HBA and brings a HBA online. During this
2618 * process, the management interface is blocked to prevent user space access
2619 * to the HBA interfering with the driver initialization.
2620 *
2621 * Return codes
2622 * 0 - successful
2623 * 1 - failed
2624 **/
dea31012005-04-17 16:05:31 -05002625int
James Smart2e0fef82007-06-17 19:56:36 -05002626lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002627{
Julia Lawall372bd282008-12-16 16:15:08 +01002628 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04002629 struct lpfc_vport **vports;
2630 int i;
James Smart2e0fef82007-06-17 19:56:36 -05002631
dea31012005-04-17 16:05:31 -05002632 if (!phba)
2633 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01002634 vport = phba->pport;
dea31012005-04-17 16:05:31 -05002635
James Smart2e0fef82007-06-17 19:56:36 -05002636 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05002637 return 0;
2638
James Smarted957682007-06-17 19:56:37 -05002639 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002640 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05002641
James Smart618a5232012-06-12 13:54:36 -04002642 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05002643
James Smart46fa3112007-04-25 09:51:45 -04002644 if (!lpfc_sli_queue_setup(phba)) {
2645 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002646 return 1;
James Smart46fa3112007-04-25 09:51:45 -04002647 }
2648
James Smartda0436e2009-05-22 14:51:39 -04002649 if (phba->sli_rev == LPFC_SLI_REV4) {
2650 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2651 lpfc_unblock_mgmt_io(phba);
2652 return 1;
2653 }
2654 } else {
2655 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2656 lpfc_unblock_mgmt_io(phba);
2657 return 1;
2658 }
James Smart46fa3112007-04-25 09:51:45 -04002659 }
dea31012005-04-17 16:05:31 -05002660
James Smart549e55c2007-08-02 11:09:51 -04002661 vports = lpfc_create_vport_work_array(phba);
2662 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002663 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002664 struct Scsi_Host *shost;
2665 shost = lpfc_shost_from_vport(vports[i]);
2666 spin_lock_irq(shost->host_lock);
2667 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2668 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2669 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart1c6834a2009-07-19 10:01:26 -04002670 if (phba->sli_rev == LPFC_SLI_REV4)
2671 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart549e55c2007-08-02 11:09:51 -04002672 spin_unlock_irq(shost->host_lock);
2673 }
James Smart09372822008-01-11 01:52:54 -05002674 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002675
James Smart46fa3112007-04-25 09:51:45 -04002676 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002677 return 0;
2678}
2679
James Smarte59058c2008-08-24 21:49:00 -04002680/**
James Smart3621a712009-04-06 18:47:14 -04002681 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04002682 * @phba: pointer to lpfc hba data structure.
2683 *
2684 * This routine marks a HBA's management interface as not blocked. Once the
2685 * HBA's management interface is marked as not blocked, all the user space
2686 * access to the HBA, whether they are from sysfs interface or libdfc
2687 * interface will be allowed. The HBA is set to block the management interface
2688 * when the driver prepares the HBA interface for online or offline and then
2689 * set to unblock the management interface afterwards.
2690 **/
James Smart46fa3112007-04-25 09:51:45 -04002691void
James Smart46fa3112007-04-25 09:51:45 -04002692lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2693{
2694 unsigned long iflag;
2695
James Smart2e0fef82007-06-17 19:56:36 -05002696 spin_lock_irqsave(&phba->hbalock, iflag);
2697 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2698 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04002699}
2700
James Smarte59058c2008-08-24 21:49:00 -04002701/**
James Smart3621a712009-04-06 18:47:14 -04002702 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04002703 * @phba: pointer to lpfc hba data structure.
2704 *
2705 * This routine is invoked to prepare a HBA to be brought offline. It performs
2706 * unregistration login to all the nodes on all vports and flushes the mailbox
2707 * queue to make it ready to be brought offline.
2708 **/
James Smart46fa3112007-04-25 09:51:45 -04002709void
James Smart618a5232012-06-12 13:54:36 -04002710lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
James Smart46fa3112007-04-25 09:51:45 -04002711{
James Smart2e0fef82007-06-17 19:56:36 -05002712 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04002713 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04002714 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05002715 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04002716 int i;
dea31012005-04-17 16:05:31 -05002717
James Smart2e0fef82007-06-17 19:56:36 -05002718 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002719 return;
dea31012005-04-17 16:05:31 -05002720
James Smart618a5232012-06-12 13:54:36 -04002721 lpfc_block_mgmt_io(phba, mbx_action);
dea31012005-04-17 16:05:31 -05002722
2723 lpfc_linkdown(phba);
2724
James Smart87af33f2007-10-27 13:37:43 -04002725 /* Issue an unreg_login to all nodes on all vports */
2726 vports = lpfc_create_vport_work_array(phba);
2727 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04002728 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04002729 if (vports[i]->load_flag & FC_UNLOADING)
2730 continue;
James Smart72100cc2010-02-12 14:43:01 -05002731 shost = lpfc_shost_from_vport(vports[i]);
2732 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002733 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002734 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2735 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05002736 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002737
James Smart87af33f2007-10-27 13:37:43 -04002738 shost = lpfc_shost_from_vport(vports[i]);
2739 list_for_each_entry_safe(ndlp, next_ndlp,
2740 &vports[i]->fc_nodes,
2741 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002742 if (!NLP_CHK_NODE_ACT(ndlp))
2743 continue;
James Smart87af33f2007-10-27 13:37:43 -04002744 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2745 continue;
2746 if (ndlp->nlp_type & NLP_FABRIC) {
2747 lpfc_disc_state_machine(vports[i], ndlp,
2748 NULL, NLP_EVT_DEVICE_RECOVERY);
2749 lpfc_disc_state_machine(vports[i], ndlp,
2750 NULL, NLP_EVT_DEVICE_RM);
2751 }
2752 spin_lock_irq(shost->host_lock);
2753 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart401ee0c2012-03-01 22:35:34 -05002754 spin_unlock_irq(shost->host_lock);
James Smart6b5151f2012-01-18 16:24:06 -05002755 /*
2756 * Whenever an SLI4 port goes offline, free the
James Smart401ee0c2012-03-01 22:35:34 -05002757 * RPI. Get a new RPI when the adapter port
2758 * comes back online.
James Smart6b5151f2012-01-18 16:24:06 -05002759 */
2760 if (phba->sli_rev == LPFC_SLI_REV4)
2761 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smart87af33f2007-10-27 13:37:43 -04002762 lpfc_unreg_rpi(vports[i], ndlp);
2763 }
2764 }
2765 }
James Smart09372822008-01-11 01:52:54 -05002766 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002767
James Smart618a5232012-06-12 13:54:36 -04002768 lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
James Smart46fa3112007-04-25 09:51:45 -04002769}
2770
James Smarte59058c2008-08-24 21:49:00 -04002771/**
James Smart3621a712009-04-06 18:47:14 -04002772 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04002773 * @phba: pointer to lpfc hba data structure.
2774 *
2775 * This routine actually brings a HBA offline. It stops all the timers
2776 * associated with the HBA, brings down the SLI layer, and eventually
2777 * marks the HBA as in offline state for the upper layer protocol.
2778 **/
James Smart46fa3112007-04-25 09:51:45 -04002779void
James Smart2e0fef82007-06-17 19:56:36 -05002780lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04002781{
James Smart549e55c2007-08-02 11:09:51 -04002782 struct Scsi_Host *shost;
2783 struct lpfc_vport **vports;
2784 int i;
James Smart46fa3112007-04-25 09:51:45 -04002785
James Smart549e55c2007-08-02 11:09:51 -04002786 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002787 return;
James Smart688a8862006-07-06 15:49:56 -04002788
James Smartda0436e2009-05-22 14:51:39 -04002789 /* stop port and all timers associated with this hba */
2790 lpfc_stop_port(phba);
James Smart51ef4c22007-08-02 11:10:31 -04002791 vports = lpfc_create_vport_work_array(phba);
2792 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002793 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04002794 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05002795 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05002796 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002797 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05002798 /* Bring down the SLI Layer and cleanup. The HBA is offline
2799 now. */
2800 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002801 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04002802 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002803 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04002804 vports = lpfc_create_vport_work_array(phba);
2805 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002806 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002807 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04002808 spin_lock_irq(shost->host_lock);
2809 vports[i]->work_port_events = 0;
2810 vports[i]->fc_flag |= FC_OFFLINE_MODE;
2811 spin_unlock_irq(shost->host_lock);
2812 }
James Smart09372822008-01-11 01:52:54 -05002813 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002814}
2815
James Smarte59058c2008-08-24 21:49:00 -04002816/**
James Smart3621a712009-04-06 18:47:14 -04002817 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04002818 * @phba: pointer to lpfc hba data structure.
2819 *
2820 * This routine is to free all the SCSI buffers and IOCBs from the driver
2821 * list back to kernel. It is called from lpfc_pci_remove_one to free
2822 * the internal resources before the device is removed from the system.
James Smarte59058c2008-08-24 21:49:00 -04002823 **/
James Smart8a9d2e82012-05-09 21:16:12 -04002824static void
James Smart2e0fef82007-06-17 19:56:36 -05002825lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002826{
2827 struct lpfc_scsi_buf *sb, *sb_next;
2828 struct lpfc_iocbq *io, *io_next;
2829
James Smart2e0fef82007-06-17 19:56:36 -05002830 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002831 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smart1c6f4ef52009-11-18 15:40:49 -05002832 spin_lock(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05002833 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
2834 list_del(&sb->list);
2835 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05002836 sb->dma_handle);
dea31012005-04-17 16:05:31 -05002837 kfree(sb);
2838 phba->total_scsi_bufs--;
2839 }
James Smart1c6f4ef52009-11-18 15:40:49 -05002840 spin_unlock(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05002841
2842 /* Release all the lpfc_iocbq entries maintained by this host. */
2843 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2844 list_del(&io->list);
2845 kfree(io);
2846 phba->total_iocbq_bufs--;
2847 }
James Smart6d368e52011-05-24 11:44:12 -04002848
James Smart2e0fef82007-06-17 19:56:36 -05002849 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04002850}
2851
2852/**
2853 * lpfc_sli4_xri_sgl_update - update xri-sgl sizing and mapping
2854 * @phba: pointer to lpfc hba data structure.
2855 *
2856 * This routine first calculates the sizes of the current els and allocated
2857 * scsi sgl lists, and then goes through all sgls to updates the physical
2858 * XRIs assigned due to port function reset. During port initialization, the
2859 * current els and allocated scsi sgl lists are 0s.
2860 *
2861 * Return codes
2862 * 0 - successful (for now, it always returns 0)
2863 **/
2864int
2865lpfc_sli4_xri_sgl_update(struct lpfc_hba *phba)
2866{
2867 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
2868 struct lpfc_scsi_buf *psb = NULL, *psb_next = NULL;
2869 uint16_t i, lxri, xri_cnt, els_xri_cnt, scsi_xri_cnt;
2870 LIST_HEAD(els_sgl_list);
2871 LIST_HEAD(scsi_sgl_list);
2872 int rc;
2873
2874 /*
2875 * update on pci function's els xri-sgl list
2876 */
2877 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
2878 if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
2879 /* els xri-sgl expanded */
2880 xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
2881 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2882 "3157 ELS xri-sgl count increased from "
2883 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
2884 els_xri_cnt);
2885 /* allocate the additional els sgls */
2886 for (i = 0; i < xri_cnt; i++) {
2887 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
2888 GFP_KERNEL);
2889 if (sglq_entry == NULL) {
2890 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2891 "2562 Failure to allocate an "
2892 "ELS sgl entry:%d\n", i);
2893 rc = -ENOMEM;
2894 goto out_free_mem;
2895 }
2896 sglq_entry->buff_type = GEN_BUFF_TYPE;
2897 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
2898 &sglq_entry->phys);
2899 if (sglq_entry->virt == NULL) {
2900 kfree(sglq_entry);
2901 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2902 "2563 Failure to allocate an "
2903 "ELS mbuf:%d\n", i);
2904 rc = -ENOMEM;
2905 goto out_free_mem;
2906 }
2907 sglq_entry->sgl = sglq_entry->virt;
2908 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
2909 sglq_entry->state = SGL_FREED;
2910 list_add_tail(&sglq_entry->list, &els_sgl_list);
2911 }
2912 spin_lock(&phba->hbalock);
2913 list_splice_init(&els_sgl_list, &phba->sli4_hba.lpfc_sgl_list);
2914 spin_unlock(&phba->hbalock);
2915 } else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
2916 /* els xri-sgl shrinked */
2917 xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
2918 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2919 "3158 ELS xri-sgl count decreased from "
2920 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
2921 els_xri_cnt);
2922 spin_lock_irq(&phba->hbalock);
2923 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &els_sgl_list);
2924 spin_unlock_irq(&phba->hbalock);
2925 /* release extra els sgls from list */
2926 for (i = 0; i < xri_cnt; i++) {
2927 list_remove_head(&els_sgl_list,
2928 sglq_entry, struct lpfc_sglq, list);
2929 if (sglq_entry) {
2930 lpfc_mbuf_free(phba, sglq_entry->virt,
2931 sglq_entry->phys);
2932 kfree(sglq_entry);
2933 }
2934 }
2935 spin_lock_irq(&phba->hbalock);
2936 list_splice_init(&els_sgl_list, &phba->sli4_hba.lpfc_sgl_list);
2937 spin_unlock_irq(&phba->hbalock);
2938 } else
2939 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2940 "3163 ELS xri-sgl count unchanged: %d\n",
2941 els_xri_cnt);
2942 phba->sli4_hba.els_xri_cnt = els_xri_cnt;
2943
2944 /* update xris to els sgls on the list */
2945 sglq_entry = NULL;
2946 sglq_entry_next = NULL;
2947 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
2948 &phba->sli4_hba.lpfc_sgl_list, list) {
2949 lxri = lpfc_sli4_next_xritag(phba);
2950 if (lxri == NO_XRI) {
2951 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2952 "2400 Failed to allocate xri for "
2953 "ELS sgl\n");
2954 rc = -ENOMEM;
2955 goto out_free_mem;
2956 }
2957 sglq_entry->sli4_lxritag = lxri;
2958 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
2959 }
2960
2961 /*
2962 * update on pci function's allocated scsi xri-sgl list
2963 */
2964 phba->total_scsi_bufs = 0;
2965
2966 /* maximum number of xris available for scsi buffers */
2967 phba->sli4_hba.scsi_xri_max = phba->sli4_hba.max_cfg_param.max_xri -
2968 els_xri_cnt;
2969
2970 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2971 "2401 Current allocated SCSI xri-sgl count:%d, "
2972 "maximum SCSI xri count:%d\n",
2973 phba->sli4_hba.scsi_xri_cnt,
2974 phba->sli4_hba.scsi_xri_max);
2975
2976 spin_lock_irq(&phba->scsi_buf_list_lock);
2977 list_splice_init(&phba->lpfc_scsi_buf_list, &scsi_sgl_list);
2978 spin_unlock_irq(&phba->scsi_buf_list_lock);
2979
2980 if (phba->sli4_hba.scsi_xri_cnt > phba->sli4_hba.scsi_xri_max) {
2981 /* max scsi xri shrinked below the allocated scsi buffers */
2982 scsi_xri_cnt = phba->sli4_hba.scsi_xri_cnt -
2983 phba->sli4_hba.scsi_xri_max;
2984 /* release the extra allocated scsi buffers */
2985 for (i = 0; i < scsi_xri_cnt; i++) {
2986 list_remove_head(&scsi_sgl_list, psb,
2987 struct lpfc_scsi_buf, list);
2988 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, psb->data,
2989 psb->dma_handle);
2990 kfree(psb);
2991 }
2992 spin_lock_irq(&phba->scsi_buf_list_lock);
2993 phba->sli4_hba.scsi_xri_cnt -= scsi_xri_cnt;
2994 spin_unlock_irq(&phba->scsi_buf_list_lock);
2995 }
2996
2997 /* update xris associated to remaining allocated scsi buffers */
2998 psb = NULL;
2999 psb_next = NULL;
3000 list_for_each_entry_safe(psb, psb_next, &scsi_sgl_list, list) {
3001 lxri = lpfc_sli4_next_xritag(phba);
3002 if (lxri == NO_XRI) {
3003 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3004 "2560 Failed to allocate xri for "
3005 "scsi buffer\n");
3006 rc = -ENOMEM;
3007 goto out_free_mem;
3008 }
3009 psb->cur_iocbq.sli4_lxritag = lxri;
3010 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3011 }
3012 spin_lock(&phba->scsi_buf_list_lock);
3013 list_splice_init(&scsi_sgl_list, &phba->lpfc_scsi_buf_list);
3014 spin_unlock(&phba->scsi_buf_list_lock);
3015
dea31012005-04-17 16:05:31 -05003016 return 0;
James Smart8a9d2e82012-05-09 21:16:12 -04003017
3018out_free_mem:
3019 lpfc_free_els_sgl_list(phba);
3020 lpfc_scsi_free(phba);
3021 return rc;
dea31012005-04-17 16:05:31 -05003022}
3023
James Smarte59058c2008-08-24 21:49:00 -04003024/**
James Smart3621a712009-04-06 18:47:14 -04003025 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04003026 * @phba: pointer to lpfc hba data structure.
3027 * @instance: a unique integer ID to this FC port.
3028 * @dev: pointer to the device data structure.
3029 *
3030 * This routine creates a FC port for the upper layer protocol. The FC port
3031 * can be created on top of either a physical port or a virtual port provided
3032 * by the HBA. This routine also allocates a SCSI host data structure (shost)
3033 * and associates the FC port created before adding the shost into the SCSI
3034 * layer.
3035 *
3036 * Return codes
3037 * @vport - pointer to the virtual N_Port data structure.
3038 * NULL - port create failed.
3039 **/
James Smart2e0fef82007-06-17 19:56:36 -05003040struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04003041lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04003042{
James Smart2e0fef82007-06-17 19:56:36 -05003043 struct lpfc_vport *vport;
3044 struct Scsi_Host *shost;
3045 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04003046
James Smart3de2a652007-08-02 11:09:59 -04003047 if (dev != &phba->pcidev->dev)
3048 shost = scsi_host_alloc(&lpfc_vport_template,
3049 sizeof(struct lpfc_vport));
3050 else
3051 shost = scsi_host_alloc(&lpfc_template,
3052 sizeof(struct lpfc_vport));
James Smart2e0fef82007-06-17 19:56:36 -05003053 if (!shost)
3054 goto out;
James Smart47a86172007-04-25 09:53:22 -04003055
James Smart2e0fef82007-06-17 19:56:36 -05003056 vport = (struct lpfc_vport *) shost->hostdata;
3057 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05003058 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05003059 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05003060 vport->fc_rscn_flush = 0;
James Smart47a86172007-04-25 09:53:22 -04003061
James Smart3de2a652007-08-02 11:09:59 -04003062 lpfc_get_vport_cfgparam(vport);
James Smart2e0fef82007-06-17 19:56:36 -05003063 shost->unique_id = instance;
3064 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04003065 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05003066 shost->this_id = -1;
3067 shost->max_cmd_len = 16;
James Smartda0436e2009-05-22 14:51:39 -04003068 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart28baac72010-02-12 14:42:03 -05003069 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04003070 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smartda0436e2009-05-22 14:51:39 -04003071 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
3072 }
James Smart81301a92008-12-04 22:39:46 -05003073
James Smart47a86172007-04-25 09:53:22 -04003074 /*
James Smart2e0fef82007-06-17 19:56:36 -05003075 * Set initial can_queue value since 0 is no longer supported and
3076 * scsi_add_host will fail. This will be adjusted later based on the
3077 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04003078 */
James Smart2e0fef82007-06-17 19:56:36 -05003079 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04003080 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05003081 shost->transportt = lpfc_vport_transport_template;
3082 vport->port_type = LPFC_NPIV_PORT;
3083 } else {
3084 shost->transportt = lpfc_transport_template;
3085 vport->port_type = LPFC_PHYSICAL_PORT;
3086 }
James Smart47a86172007-04-25 09:53:22 -04003087
James Smart2e0fef82007-06-17 19:56:36 -05003088 /* Initialize all internally managed lists. */
3089 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04003090 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05003091 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04003092
James Smart2e0fef82007-06-17 19:56:36 -05003093 init_timer(&vport->fc_disctmo);
3094 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05003095 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04003096
James Smart2e0fef82007-06-17 19:56:36 -05003097 init_timer(&vport->fc_fdmitmo);
3098 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
James Smart92d7f7b2007-06-17 19:56:38 -05003099 vport->fc_fdmitmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04003100
James Smart2e0fef82007-06-17 19:56:36 -05003101 init_timer(&vport->els_tmofunc);
3102 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05003103 vport->els_tmofunc.data = (unsigned long)vport;
James Smart92494142011-02-16 12:39:44 -05003104
3105 init_timer(&vport->delayed_disc_tmo);
3106 vport->delayed_disc_tmo.function = lpfc_delayed_disc_tmo;
3107 vport->delayed_disc_tmo.data = (unsigned long)vport;
3108
James Bottomleyd139b9b2009-11-05 13:33:12 -06003109 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05003110 if (error)
3111 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04003112
James Smart549e55c2007-08-02 11:09:51 -04003113 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003114 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04003115 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003116 return vport;
James Smart47a86172007-04-25 09:53:22 -04003117
James Smart2e0fef82007-06-17 19:56:36 -05003118out_put_shost:
3119 scsi_host_put(shost);
3120out:
3121 return NULL;
James Smart47a86172007-04-25 09:53:22 -04003122}
3123
James Smarte59058c2008-08-24 21:49:00 -04003124/**
James Smart3621a712009-04-06 18:47:14 -04003125 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04003126 * @vport: pointer to an lpfc virtual N_Port data structure.
3127 *
3128 * This routine destroys a FC port from the upper layer protocol. All the
3129 * resources associated with the port are released.
3130 **/
James Smart2e0fef82007-06-17 19:56:36 -05003131void
3132destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04003133{
James Smart92d7f7b2007-06-17 19:56:38 -05003134 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3135 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003136
James Smart858c9f62007-06-17 19:56:39 -05003137 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05003138 fc_remove_host(shost);
3139 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04003140
James Smart92d7f7b2007-06-17 19:56:38 -05003141 spin_lock_irq(&phba->hbalock);
3142 list_del_init(&vport->listentry);
3143 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04003144
James Smart92d7f7b2007-06-17 19:56:38 -05003145 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04003146 return;
James Smart47a86172007-04-25 09:53:22 -04003147}
3148
James Smarte59058c2008-08-24 21:49:00 -04003149/**
James Smart3621a712009-04-06 18:47:14 -04003150 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04003151 *
3152 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
3153 * uses the kernel idr facility to perform the task.
3154 *
3155 * Return codes:
3156 * instance - a unique integer ID allocated as the new instance.
3157 * -1 - lpfc get instance failed.
3158 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003159int
3160lpfc_get_instance(void)
3161{
3162 int instance = 0;
3163
3164 /* Assign an unused number */
3165 if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
3166 return -1;
3167 if (idr_get_new(&lpfc_hba_index, NULL, &instance))
3168 return -1;
3169 return instance;
3170}
3171
James Smarte59058c2008-08-24 21:49:00 -04003172/**
James Smart3621a712009-04-06 18:47:14 -04003173 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04003174 * @shost: pointer to SCSI host data structure.
3175 * @time: elapsed time of the scan in jiffies.
3176 *
3177 * This routine is called by the SCSI layer with a SCSI host to determine
3178 * whether the scan host is finished.
3179 *
3180 * Note: there is no scan_start function as adapter initialization will have
3181 * asynchronously kicked off the link initialization.
3182 *
3183 * Return codes
3184 * 0 - SCSI host scan is not over yet.
3185 * 1 - SCSI host scan is over.
3186 **/
James Smart47a86172007-04-25 09:53:22 -04003187int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
3188{
James Smart2e0fef82007-06-17 19:56:36 -05003189 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3190 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05003191 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04003192
James Smart858c9f62007-06-17 19:56:39 -05003193 spin_lock_irq(shost->host_lock);
3194
James Smart51ef4c22007-08-02 11:10:31 -04003195 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05003196 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003197 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05003198 }
James Smart2e0fef82007-06-17 19:56:36 -05003199 if (time >= 30 * HZ) {
3200 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003201 "0461 Scanning longer than 30 "
3202 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003203 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003204 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003205 }
3206 if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
3207 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003208 "0465 Link down longer than 15 "
3209 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003210 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05003211 goto finished;
James Smart47a86172007-04-25 09:53:22 -04003212 }
3213
James Smart2e0fef82007-06-17 19:56:36 -05003214 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05003215 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003216 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05003217 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003218 if (vport->fc_map_cnt == 0 && time < 2 * HZ)
James Smart858c9f62007-06-17 19:56:39 -05003219 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003220 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05003221 goto finished;
3222
3223 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003224
3225finished:
James Smart858c9f62007-06-17 19:56:39 -05003226 spin_unlock_irq(shost->host_lock);
3227 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05003228}
3229
James Smarte59058c2008-08-24 21:49:00 -04003230/**
James Smart3621a712009-04-06 18:47:14 -04003231 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04003232 * @shost: pointer to SCSI host data structure.
3233 *
3234 * This routine initializes a given SCSI host attributes on a FC port. The
3235 * SCSI host can be either on top of a physical port or a virtual port.
3236 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003237void lpfc_host_attrib_init(struct Scsi_Host *shost)
3238{
3239 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3240 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003241 /*
James Smart2e0fef82007-06-17 19:56:36 -05003242 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04003243 */
3244
James Smart2e0fef82007-06-17 19:56:36 -05003245 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
3246 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04003247 fc_host_supported_classes(shost) = FC_COS_CLASS3;
3248
3249 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003250 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003251 fc_host_supported_fc4s(shost)[2] = 1;
3252 fc_host_supported_fc4s(shost)[7] = 1;
3253
James Smart92d7f7b2007-06-17 19:56:38 -05003254 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
3255 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04003256
3257 fc_host_supported_speeds(shost) = 0;
James Smart88a2cfb2011-07-22 18:36:33 -04003258 if (phba->lmt & LMT_16Gb)
3259 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
James Smart47a86172007-04-25 09:53:22 -04003260 if (phba->lmt & LMT_10Gb)
3261 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04003262 if (phba->lmt & LMT_8Gb)
3263 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04003264 if (phba->lmt & LMT_4Gb)
3265 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
3266 if (phba->lmt & LMT_2Gb)
3267 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
3268 if (phba->lmt & LMT_1Gb)
3269 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
3270
3271 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05003272 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
3273 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04003274
Mike Christie0af5d702010-09-15 16:52:31 -05003275 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
3276
James Smart47a86172007-04-25 09:53:22 -04003277 /* This value is also unchanging */
3278 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003279 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003280 fc_host_active_fc4s(shost)[2] = 1;
3281 fc_host_active_fc4s(shost)[7] = 1;
3282
James Smart92d7f7b2007-06-17 19:56:38 -05003283 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04003284 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04003285 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04003286 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04003287}
dea31012005-04-17 16:05:31 -05003288
James Smarte59058c2008-08-24 21:49:00 -04003289/**
James Smartda0436e2009-05-22 14:51:39 -04003290 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04003291 * @phba: pointer to lpfc hba data structure.
3292 *
James Smartda0436e2009-05-22 14:51:39 -04003293 * This routine is invoked to stop an SLI3 device port, it stops the device
3294 * from generating interrupts and stops the device driver's timers for the
3295 * device.
James Smarte59058c2008-08-24 21:49:00 -04003296 **/
James Smartdb2378e2008-02-08 18:49:51 -05003297static void
James Smartda0436e2009-05-22 14:51:39 -04003298lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05003299{
James Smartda0436e2009-05-22 14:51:39 -04003300 /* Clear all interrupt enable conditions */
3301 writel(0, phba->HCregaddr);
3302 readl(phba->HCregaddr); /* flush */
3303 /* Clear all pending interrupts */
3304 writel(0xffffffff, phba->HAregaddr);
3305 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04003306
James Smartda0436e2009-05-22 14:51:39 -04003307 /* Reset some HBA SLI setup states */
3308 lpfc_stop_hba_timers(phba);
3309 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05003310}
3311
James Smarte59058c2008-08-24 21:49:00 -04003312/**
James Smartda0436e2009-05-22 14:51:39 -04003313 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05003314 * @phba: pointer to lpfc hba data structure.
3315 *
James Smartda0436e2009-05-22 14:51:39 -04003316 * This routine is invoked to stop an SLI4 device port, it stops the device
3317 * from generating interrupts and stops the device driver's timers for the
3318 * device.
3319 **/
3320static void
3321lpfc_stop_port_s4(struct lpfc_hba *phba)
3322{
3323 /* Reset some HBA SLI4 setup states */
3324 lpfc_stop_hba_timers(phba);
3325 phba->pport->work_port_events = 0;
3326 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04003327}
3328
3329/**
3330 * lpfc_stop_port - Wrapper function for stopping hba port
3331 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05003332 *
James Smartda0436e2009-05-22 14:51:39 -04003333 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
3334 * the API jump table function pointer from the lpfc_hba struct.
3335 **/
3336void
3337lpfc_stop_port(struct lpfc_hba *phba)
3338{
3339 phba->lpfc_stop_port(phba);
3340}
3341
3342/**
James Smartecfd03c2010-02-12 14:41:27 -05003343 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
3344 * @phba: Pointer to hba for which this call is being executed.
3345 *
3346 * This routine starts the timer waiting for the FCF rediscovery to complete.
3347 **/
3348void
3349lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
3350{
3351 unsigned long fcf_redisc_wait_tmo =
3352 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
3353 /* Start fcf rediscovery wait period timer */
3354 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
3355 spin_lock_irq(&phba->hbalock);
3356 /* Allow action to new fcf asynchronous event */
3357 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
3358 /* Mark the FCF rediscovery pending state */
3359 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
3360 spin_unlock_irq(&phba->hbalock);
3361}
3362
3363/**
3364 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
3365 * @ptr: Map to lpfc_hba data structure pointer.
3366 *
3367 * This routine is invoked when waiting for FCF table rediscover has been
3368 * timed out. If new FCF record(s) has (have) been discovered during the
3369 * wait period, a new FCF event shall be added to the FCOE async event
3370 * list, and then worker thread shall be waked up for processing from the
3371 * worker thread context.
3372 **/
3373void
3374lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
3375{
3376 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
3377
3378 /* Don't send FCF rediscovery event if timer cancelled */
3379 spin_lock_irq(&phba->hbalock);
3380 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
3381 spin_unlock_irq(&phba->hbalock);
3382 return;
3383 }
3384 /* Clear FCF rediscovery timer pending flag */
3385 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
3386 /* FCF rediscovery event to worker thread */
3387 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
3388 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003389 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04003390 "2776 FCF rediscover quiescent timer expired\n");
James Smartecfd03c2010-02-12 14:41:27 -05003391 /* wake up worker thread */
3392 lpfc_worker_wake_up(phba);
3393}
3394
3395/**
James Smartda0436e2009-05-22 14:51:39 -04003396 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
3397 * @phba: pointer to lpfc hba data structure.
3398 * @acqe_link: pointer to the async link completion queue entry.
3399 *
3400 * This routine is to parse the SLI4 link-attention link fault code and
3401 * translate it into the base driver's read link attention mailbox command
3402 * status.
3403 *
3404 * Return: Link-attention status in terms of base driver's coding.
3405 **/
3406static uint16_t
3407lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
3408 struct lpfc_acqe_link *acqe_link)
3409{
3410 uint16_t latt_fault;
3411
3412 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
3413 case LPFC_ASYNC_LINK_FAULT_NONE:
3414 case LPFC_ASYNC_LINK_FAULT_LOCAL:
3415 case LPFC_ASYNC_LINK_FAULT_REMOTE:
3416 latt_fault = 0;
3417 break;
3418 default:
3419 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3420 "0398 Invalid link fault code: x%x\n",
3421 bf_get(lpfc_acqe_link_fault, acqe_link));
3422 latt_fault = MBXERR_ERROR;
3423 break;
3424 }
3425 return latt_fault;
3426}
3427
3428/**
3429 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
3430 * @phba: pointer to lpfc hba data structure.
3431 * @acqe_link: pointer to the async link completion queue entry.
3432 *
3433 * This routine is to parse the SLI4 link attention type and translate it
3434 * into the base driver's link attention type coding.
3435 *
3436 * Return: Link attention type in terms of base driver's coding.
3437 **/
3438static uint8_t
3439lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
3440 struct lpfc_acqe_link *acqe_link)
3441{
3442 uint8_t att_type;
3443
3444 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
3445 case LPFC_ASYNC_LINK_STATUS_DOWN:
3446 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
James Smart76a95d72010-11-20 23:11:48 -05003447 att_type = LPFC_ATT_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04003448 break;
3449 case LPFC_ASYNC_LINK_STATUS_UP:
3450 /* Ignore physical link up events - wait for logical link up */
James Smart76a95d72010-11-20 23:11:48 -05003451 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003452 break;
3453 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
James Smart76a95d72010-11-20 23:11:48 -05003454 att_type = LPFC_ATT_LINK_UP;
James Smartda0436e2009-05-22 14:51:39 -04003455 break;
3456 default:
3457 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3458 "0399 Invalid link attention type: x%x\n",
3459 bf_get(lpfc_acqe_link_status, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003460 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003461 break;
3462 }
3463 return att_type;
3464}
3465
3466/**
3467 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
3468 * @phba: pointer to lpfc hba data structure.
3469 * @acqe_link: pointer to the async link completion queue entry.
3470 *
3471 * This routine is to parse the SLI4 link-attention link speed and translate
3472 * it into the base driver's link-attention link speed coding.
3473 *
3474 * Return: Link-attention link speed in terms of base driver's coding.
3475 **/
3476static uint8_t
3477lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
3478 struct lpfc_acqe_link *acqe_link)
3479{
3480 uint8_t link_speed;
3481
3482 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
3483 case LPFC_ASYNC_LINK_SPEED_ZERO:
James Smartda0436e2009-05-22 14:51:39 -04003484 case LPFC_ASYNC_LINK_SPEED_10MBPS:
James Smartda0436e2009-05-22 14:51:39 -04003485 case LPFC_ASYNC_LINK_SPEED_100MBPS:
James Smart76a95d72010-11-20 23:11:48 -05003486 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003487 break;
3488 case LPFC_ASYNC_LINK_SPEED_1GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003489 link_speed = LPFC_LINK_SPEED_1GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003490 break;
3491 case LPFC_ASYNC_LINK_SPEED_10GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003492 link_speed = LPFC_LINK_SPEED_10GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003493 break;
3494 default:
3495 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3496 "0483 Invalid link-attention link speed: x%x\n",
3497 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003498 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003499 break;
3500 }
3501 return link_speed;
3502}
3503
3504/**
James Smart70f3c072010-12-15 17:57:33 -05003505 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
James Smartda0436e2009-05-22 14:51:39 -04003506 * @phba: pointer to lpfc hba data structure.
3507 * @acqe_link: pointer to the async link completion queue entry.
3508 *
James Smart70f3c072010-12-15 17:57:33 -05003509 * This routine is to handle the SLI4 asynchronous FCoE link event.
James Smartda0436e2009-05-22 14:51:39 -04003510 **/
3511static void
3512lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3513 struct lpfc_acqe_link *acqe_link)
3514{
3515 struct lpfc_dmabuf *mp;
3516 LPFC_MBOXQ_t *pmb;
3517 MAILBOX_t *mb;
James Smart76a95d72010-11-20 23:11:48 -05003518 struct lpfc_mbx_read_top *la;
James Smartda0436e2009-05-22 14:51:39 -04003519 uint8_t att_type;
James Smart76a95d72010-11-20 23:11:48 -05003520 int rc;
James Smartda0436e2009-05-22 14:51:39 -04003521
3522 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05003523 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
James Smartda0436e2009-05-22 14:51:39 -04003524 return;
James Smart32b97932009-07-19 10:01:21 -04003525 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003526 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3527 if (!pmb) {
3528 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3529 "0395 The mboxq allocation failed\n");
3530 return;
3531 }
3532 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3533 if (!mp) {
3534 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3535 "0396 The lpfc_dmabuf allocation failed\n");
3536 goto out_free_pmb;
3537 }
3538 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3539 if (!mp->virt) {
3540 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3541 "0397 The mbuf allocation failed\n");
3542 goto out_free_dmabuf;
3543 }
3544
3545 /* Cleanup any outstanding ELS commands */
3546 lpfc_els_flush_all_cmd(phba);
3547
3548 /* Block ELS IOCBs until we have done process link event */
3549 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3550
3551 /* Update link event statistics */
3552 phba->sli.slistat.link_event++;
3553
James Smart76a95d72010-11-20 23:11:48 -05003554 /* Create lpfc_handle_latt mailbox command from link ACQE */
3555 lpfc_read_topology(phba, pmb, mp);
3556 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smartda0436e2009-05-22 14:51:39 -04003557 pmb->vport = phba->pport;
3558
James Smartda0436e2009-05-22 14:51:39 -04003559 /* Keep the link status for extra SLI4 state machine reference */
3560 phba->sli4_hba.link_state.speed =
3561 bf_get(lpfc_acqe_link_speed, acqe_link);
3562 phba->sli4_hba.link_state.duplex =
3563 bf_get(lpfc_acqe_link_duplex, acqe_link);
3564 phba->sli4_hba.link_state.status =
3565 bf_get(lpfc_acqe_link_status, acqe_link);
James Smart70f3c072010-12-15 17:57:33 -05003566 phba->sli4_hba.link_state.type =
3567 bf_get(lpfc_acqe_link_type, acqe_link);
3568 phba->sli4_hba.link_state.number =
3569 bf_get(lpfc_acqe_link_number, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04003570 phba->sli4_hba.link_state.fault =
3571 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05003572 phba->sli4_hba.link_state.logical_speed =
James Smart70f3c072010-12-15 17:57:33 -05003573 bf_get(lpfc_acqe_logical_link_speed, acqe_link);
3574 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartc31098c2011-04-16 11:03:33 -04003575 "2900 Async FC/FCoE Link event - Speed:%dGBit "
3576 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
3577 "Logical speed:%dMbps Fault:%d\n",
James Smart70f3c072010-12-15 17:57:33 -05003578 phba->sli4_hba.link_state.speed,
3579 phba->sli4_hba.link_state.topology,
3580 phba->sli4_hba.link_state.status,
3581 phba->sli4_hba.link_state.type,
3582 phba->sli4_hba.link_state.number,
3583 phba->sli4_hba.link_state.logical_speed * 10,
3584 phba->sli4_hba.link_state.fault);
James Smart76a95d72010-11-20 23:11:48 -05003585 /*
3586 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
3587 * topology info. Note: Optional for non FC-AL ports.
3588 */
3589 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3590 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3591 if (rc == MBX_NOT_FINISHED)
3592 goto out_free_dmabuf;
3593 return;
3594 }
3595 /*
3596 * For FCoE Mode: fill in all the topology information we need and call
3597 * the READ_TOPOLOGY completion routine to continue without actually
3598 * sending the READ_TOPOLOGY mailbox command to the port.
3599 */
3600 /* Parse and translate status field */
3601 mb = &pmb->u.mb;
3602 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
3603
3604 /* Parse and translate link attention fields */
3605 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
3606 la->eventTag = acqe_link->event_tag;
3607 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
3608 bf_set(lpfc_mbx_read_top_link_spd, la,
3609 lpfc_sli4_parse_latt_link_speed(phba, acqe_link));
3610
3611 /* Fake the the following irrelvant fields */
3612 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
3613 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
3614 bf_set(lpfc_mbx_read_top_il, la, 0);
3615 bf_set(lpfc_mbx_read_top_pb, la, 0);
3616 bf_set(lpfc_mbx_read_top_fa, la, 0);
3617 bf_set(lpfc_mbx_read_top_mm, la, 0);
James Smartda0436e2009-05-22 14:51:39 -04003618
3619 /* Invoke the lpfc_handle_latt mailbox command callback function */
James Smart76a95d72010-11-20 23:11:48 -05003620 lpfc_mbx_cmpl_read_topology(phba, pmb);
James Smartda0436e2009-05-22 14:51:39 -04003621
3622 return;
3623
3624out_free_dmabuf:
3625 kfree(mp);
3626out_free_pmb:
3627 mempool_free(pmb, phba->mbox_mem_pool);
3628}
3629
3630/**
James Smart70f3c072010-12-15 17:57:33 -05003631 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
3632 * @phba: pointer to lpfc hba data structure.
3633 * @acqe_fc: pointer to the async fc completion queue entry.
3634 *
3635 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
3636 * that the event was received and then issue a read_topology mailbox command so
3637 * that the rest of the driver will treat it the same as SLI3.
3638 **/
3639static void
3640lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
3641{
3642 struct lpfc_dmabuf *mp;
3643 LPFC_MBOXQ_t *pmb;
3644 int rc;
3645
3646 if (bf_get(lpfc_trailer_type, acqe_fc) !=
3647 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
3648 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3649 "2895 Non FC link Event detected.(%d)\n",
3650 bf_get(lpfc_trailer_type, acqe_fc));
3651 return;
3652 }
3653 /* Keep the link status for extra SLI4 state machine reference */
3654 phba->sli4_hba.link_state.speed =
3655 bf_get(lpfc_acqe_fc_la_speed, acqe_fc);
3656 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
3657 phba->sli4_hba.link_state.topology =
3658 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
3659 phba->sli4_hba.link_state.status =
3660 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
3661 phba->sli4_hba.link_state.type =
3662 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
3663 phba->sli4_hba.link_state.number =
3664 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
3665 phba->sli4_hba.link_state.fault =
3666 bf_get(lpfc_acqe_link_fault, acqe_fc);
3667 phba->sli4_hba.link_state.logical_speed =
3668 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc);
3669 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3670 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
3671 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
3672 "%dMbps Fault:%d\n",
3673 phba->sli4_hba.link_state.speed,
3674 phba->sli4_hba.link_state.topology,
3675 phba->sli4_hba.link_state.status,
3676 phba->sli4_hba.link_state.type,
3677 phba->sli4_hba.link_state.number,
3678 phba->sli4_hba.link_state.logical_speed * 10,
3679 phba->sli4_hba.link_state.fault);
3680 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3681 if (!pmb) {
3682 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3683 "2897 The mboxq allocation failed\n");
3684 return;
3685 }
3686 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3687 if (!mp) {
3688 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3689 "2898 The lpfc_dmabuf allocation failed\n");
3690 goto out_free_pmb;
3691 }
3692 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3693 if (!mp->virt) {
3694 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3695 "2899 The mbuf allocation failed\n");
3696 goto out_free_dmabuf;
3697 }
3698
3699 /* Cleanup any outstanding ELS commands */
3700 lpfc_els_flush_all_cmd(phba);
3701
3702 /* Block ELS IOCBs until we have done process link event */
3703 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3704
3705 /* Update link event statistics */
3706 phba->sli.slistat.link_event++;
3707
3708 /* Create lpfc_handle_latt mailbox command from link ACQE */
3709 lpfc_read_topology(phba, pmb, mp);
3710 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
3711 pmb->vport = phba->pport;
3712
3713 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3714 if (rc == MBX_NOT_FINISHED)
3715 goto out_free_dmabuf;
3716 return;
3717
3718out_free_dmabuf:
3719 kfree(mp);
3720out_free_pmb:
3721 mempool_free(pmb, phba->mbox_mem_pool);
3722}
3723
3724/**
3725 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
3726 * @phba: pointer to lpfc hba data structure.
3727 * @acqe_fc: pointer to the async SLI completion queue entry.
3728 *
3729 * This routine is to handle the SLI4 asynchronous SLI events.
3730 **/
3731static void
3732lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
3733{
James Smart4b8bae02012-06-12 13:55:07 -04003734 char port_name;
3735 char message[80];
3736 uint8_t status;
3737 struct lpfc_acqe_misconfigured_event *misconfigured;
3738
3739 /* special case misconfigured event as it contains data for all ports */
3740 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3741 LPFC_SLI_INTF_IF_TYPE_2) ||
3742 (bf_get(lpfc_trailer_type, acqe_sli) !=
3743 LPFC_SLI_EVENT_TYPE_MISCONFIGURED)) {
3744 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3745 "2901 Async SLI event - Event Data1:x%08x Event Data2:"
3746 "x%08x SLI Event Type:%d\n",
3747 acqe_sli->event_data1, acqe_sli->event_data2,
3748 bf_get(lpfc_trailer_type, acqe_sli));
3749 return;
3750 }
3751
3752 port_name = phba->Port[0];
3753 if (port_name == 0x00)
3754 port_name = '?'; /* get port name is empty */
3755
3756 misconfigured = (struct lpfc_acqe_misconfigured_event *)
3757 &acqe_sli->event_data1;
3758
3759 /* fetch the status for this port */
3760 switch (phba->sli4_hba.lnk_info.lnk_no) {
3761 case LPFC_LINK_NUMBER_0:
3762 status = bf_get(lpfc_sli_misconfigured_port0,
3763 &misconfigured->theEvent);
3764 break;
3765 case LPFC_LINK_NUMBER_1:
3766 status = bf_get(lpfc_sli_misconfigured_port1,
3767 &misconfigured->theEvent);
3768 break;
3769 case LPFC_LINK_NUMBER_2:
3770 status = bf_get(lpfc_sli_misconfigured_port2,
3771 &misconfigured->theEvent);
3772 break;
3773 case LPFC_LINK_NUMBER_3:
3774 status = bf_get(lpfc_sli_misconfigured_port3,
3775 &misconfigured->theEvent);
3776 break;
3777 default:
3778 status = ~LPFC_SLI_EVENT_STATUS_VALID;
3779 break;
3780 }
3781
3782 switch (status) {
3783 case LPFC_SLI_EVENT_STATUS_VALID:
3784 return; /* no message if the sfp is okay */
3785 case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
3786 sprintf(message, "Not installed");
3787 break;
3788 case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
3789 sprintf(message,
3790 "Optics of two types installed");
3791 break;
3792 case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
3793 sprintf(message, "Incompatible optics");
3794 break;
3795 default:
3796 /* firmware is reporting a status we don't know about */
3797 sprintf(message, "Unknown event status x%02x", status);
3798 break;
3799 }
3800
3801 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3802 "3176 Misconfigured Physical Port - "
3803 "Port Name %c %s\n", port_name, message);
James Smart70f3c072010-12-15 17:57:33 -05003804}
3805
3806/**
James Smartfc2b9892010-02-26 14:15:29 -05003807 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
3808 * @vport: pointer to vport data structure.
3809 *
3810 * This routine is to perform Clear Virtual Link (CVL) on a vport in
3811 * response to a CVL event.
3812 *
3813 * Return the pointer to the ndlp with the vport if successful, otherwise
3814 * return NULL.
3815 **/
3816static struct lpfc_nodelist *
3817lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
3818{
3819 struct lpfc_nodelist *ndlp;
3820 struct Scsi_Host *shost;
3821 struct lpfc_hba *phba;
3822
3823 if (!vport)
3824 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05003825 phba = vport->phba;
3826 if (!phba)
3827 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04003828 ndlp = lpfc_findnode_did(vport, Fabric_DID);
3829 if (!ndlp) {
3830 /* Cannot find existing Fabric ndlp, so allocate a new one */
3831 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
3832 if (!ndlp)
3833 return 0;
3834 lpfc_nlp_init(vport, ndlp, Fabric_DID);
3835 /* Set the node type */
3836 ndlp->nlp_type |= NLP_FABRIC;
3837 /* Put ndlp onto node list */
3838 lpfc_enqueue_node(vport, ndlp);
3839 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
3840 /* re-setup ndlp without removing from node list */
3841 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
3842 if (!ndlp)
3843 return 0;
3844 }
James Smart63e801c2010-11-20 23:14:19 -05003845 if ((phba->pport->port_state < LPFC_FLOGI) &&
3846 (phba->pport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05003847 return NULL;
3848 /* If virtual link is not yet instantiated ignore CVL */
James Smart63e801c2010-11-20 23:14:19 -05003849 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
3850 && (vport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05003851 return NULL;
3852 shost = lpfc_shost_from_vport(vport);
3853 if (!shost)
3854 return NULL;
3855 lpfc_linkdown_port(vport);
3856 lpfc_cleanup_pending_mbox(vport);
3857 spin_lock_irq(shost->host_lock);
3858 vport->fc_flag |= FC_VPORT_CVL_RCVD;
3859 spin_unlock_irq(shost->host_lock);
3860
3861 return ndlp;
3862}
3863
3864/**
3865 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
3866 * @vport: pointer to lpfc hba data structure.
3867 *
3868 * This routine is to perform Clear Virtual Link (CVL) on all vports in
3869 * response to a FCF dead event.
3870 **/
3871static void
3872lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
3873{
3874 struct lpfc_vport **vports;
3875 int i;
3876
3877 vports = lpfc_create_vport_work_array(phba);
3878 if (vports)
3879 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
3880 lpfc_sli4_perform_vport_cvl(vports[i]);
3881 lpfc_destroy_vport_work_array(phba, vports);
3882}
3883
3884/**
James Smart76a95d72010-11-20 23:11:48 -05003885 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
James Smartda0436e2009-05-22 14:51:39 -04003886 * @phba: pointer to lpfc hba data structure.
3887 * @acqe_link: pointer to the async fcoe completion queue entry.
3888 *
3889 * This routine is to handle the SLI4 asynchronous fcoe event.
3890 **/
3891static void
James Smart76a95d72010-11-20 23:11:48 -05003892lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
James Smart70f3c072010-12-15 17:57:33 -05003893 struct lpfc_acqe_fip *acqe_fip)
James Smartda0436e2009-05-22 14:51:39 -04003894{
James Smart70f3c072010-12-15 17:57:33 -05003895 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04003896 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04003897 struct lpfc_vport *vport;
3898 struct lpfc_nodelist *ndlp;
3899 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05003900 int active_vlink_present;
3901 struct lpfc_vport **vports;
3902 int i;
James Smartda0436e2009-05-22 14:51:39 -04003903
James Smart70f3c072010-12-15 17:57:33 -05003904 phba->fc_eventTag = acqe_fip->event_tag;
3905 phba->fcoe_eventtag = acqe_fip->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003906 switch (event_type) {
James Smart70f3c072010-12-15 17:57:33 -05003907 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
3908 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
3909 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
James Smart999d8132010-03-15 11:24:56 -04003910 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3911 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003912 "2546 New FCF event, evt_tag:x%x, "
3913 "index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05003914 acqe_fip->event_tag,
3915 acqe_fip->index);
James Smart999d8132010-03-15 11:24:56 -04003916 else
3917 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
3918 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003919 "2788 FCF param modified event, "
3920 "evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05003921 acqe_fip->event_tag,
3922 acqe_fip->index);
James Smart38b92ef2010-08-04 16:11:39 -04003923 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smart0c9ab6f2010-02-26 14:15:57 -05003924 /*
3925 * During period of FCF discovery, read the FCF
3926 * table record indexed by the event to update
James Smarta93ff372010-10-22 11:06:08 -04003927 * FCF roundrobin failover eligible FCF bmask.
James Smart0c9ab6f2010-02-26 14:15:57 -05003928 */
3929 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
3930 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003931 "2779 Read FCF (x%x) for updating "
3932 "roundrobin FCF failover bmask\n",
James Smart70f3c072010-12-15 17:57:33 -05003933 acqe_fip->index);
3934 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05003935 }
James Smart38b92ef2010-08-04 16:11:39 -04003936
3937 /* If the FCF discovery is in progress, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04003938 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04003939 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart38b92ef2010-08-04 16:11:39 -04003940 spin_unlock_irq(&phba->hbalock);
3941 break;
3942 }
3943 /* If fast FCF failover rescan event is pending, do nothing */
3944 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
3945 spin_unlock_irq(&phba->hbalock);
3946 break;
3947 }
3948
3949 /* If the FCF has been in discovered state, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04003950 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
3951 spin_unlock_irq(&phba->hbalock);
3952 break;
3953 }
3954 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04003955
James Smart0c9ab6f2010-02-26 14:15:57 -05003956 /* Otherwise, scan the entire FCF table and re-discover SAN */
3957 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003958 "2770 Start FCF table scan per async FCF "
3959 "event, evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05003960 acqe_fip->event_tag, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05003961 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3962 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04003963 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05003964 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3965 "2547 Issue FCF scan read FCF mailbox "
James Smarta93ff372010-10-22 11:06:08 -04003966 "command failed (x%x)\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04003967 break;
3968
James Smart70f3c072010-12-15 17:57:33 -05003969 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
James Smartda0436e2009-05-22 14:51:39 -04003970 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04003971 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05003972 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
3973 acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04003974 break;
3975
James Smart70f3c072010-12-15 17:57:33 -05003976 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
James Smart80c17842012-03-01 22:35:45 -05003977 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05003978 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003979 "2549 FCF (x%x) disconnected from network, "
James Smart70f3c072010-12-15 17:57:33 -05003980 "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart38b92ef2010-08-04 16:11:39 -04003981 /*
3982 * If we are in the middle of FCF failover process, clear
3983 * the corresponding FCF bit in the roundrobin bitmap.
James Smartda0436e2009-05-22 14:51:39 -04003984 */
James Smartfc2b9892010-02-26 14:15:29 -05003985 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003986 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05003987 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003988 /* Update FLOGI FCF failover eligible FCF bmask */
James Smart70f3c072010-12-15 17:57:33 -05003989 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05003990 break;
3991 }
James Smart38b92ef2010-08-04 16:11:39 -04003992 spin_unlock_irq(&phba->hbalock);
3993
3994 /* If the event is not for currently used fcf do nothing */
James Smart70f3c072010-12-15 17:57:33 -05003995 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
James Smart38b92ef2010-08-04 16:11:39 -04003996 break;
3997
3998 /*
3999 * Otherwise, request the port to rediscover the entire FCF
4000 * table for a fast recovery from case that the current FCF
4001 * is no longer valid as we are not in the middle of FCF
4002 * failover process already.
4003 */
4004 spin_lock_irq(&phba->hbalock);
James Smartfc2b9892010-02-26 14:15:29 -05004005 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05004006 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004007 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04004008
James Smart0c9ab6f2010-02-26 14:15:57 -05004009 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
4010 "2771 Start FCF fast failover process due to "
4011 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
James Smart70f3c072010-12-15 17:57:33 -05004012 "\n", acqe_fip->event_tag, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004013 rc = lpfc_sli4_redisc_fcf_table(phba);
4014 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004015 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4016 LOG_DISCOVERY,
4017 "2772 Issue FCF rediscover mabilbox "
4018 "command failed, fail through to FCF "
4019 "dead event\n");
James Smartfc2b9892010-02-26 14:15:29 -05004020 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004021 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004022 spin_unlock_irq(&phba->hbalock);
4023 /*
4024 * Last resort will fail over by treating this
4025 * as a link down to FCF registration.
4026 */
4027 lpfc_sli4_fcf_dead_failthrough(phba);
James Smart38b92ef2010-08-04 16:11:39 -04004028 } else {
4029 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04004030 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart38b92ef2010-08-04 16:11:39 -04004031 /*
4032 * Handling fast FCF failover to a DEAD FCF event is
4033 * considered equalivant to receiving CVL to all vports.
James Smartfc2b9892010-02-26 14:15:29 -05004034 */
4035 lpfc_sli4_perform_all_vport_cvl(phba);
James Smart38b92ef2010-08-04 16:11:39 -04004036 }
James Smartda0436e2009-05-22 14:51:39 -04004037 break;
James Smart70f3c072010-12-15 17:57:33 -05004038 case LPFC_FIP_EVENT_TYPE_CVL:
James Smart80c17842012-03-01 22:35:45 -05004039 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05004040 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6669f9b2009-10-02 15:16:45 -04004041 "2718 Clear Virtual Link Received for VPI 0x%x"
James Smart70f3c072010-12-15 17:57:33 -05004042 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart6d368e52011-05-24 11:44:12 -04004043
James Smart6669f9b2009-10-02 15:16:45 -04004044 vport = lpfc_find_vport_by_vpid(phba,
James Smart5248a742011-07-22 18:37:06 -04004045 acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004046 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004047 if (!ndlp)
4048 break;
James Smart695a8142010-01-26 23:08:03 -05004049 active_vlink_present = 0;
4050
4051 vports = lpfc_create_vport_work_array(phba);
4052 if (vports) {
4053 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
4054 i++) {
4055 if ((!(vports[i]->fc_flag &
4056 FC_VPORT_CVL_RCVD)) &&
4057 (vports[i]->port_state > LPFC_FDISC)) {
4058 active_vlink_present = 1;
4059 break;
4060 }
4061 }
4062 lpfc_destroy_vport_work_array(phba, vports);
4063 }
4064
4065 if (active_vlink_present) {
4066 /*
4067 * If there are other active VLinks present,
4068 * re-instantiate the Vlink using FDISC.
4069 */
James Smart6669f9b2009-10-02 15:16:45 -04004070 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
James Smartfc2b9892010-02-26 14:15:29 -05004071 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004072 spin_lock_irq(shost->host_lock);
4073 ndlp->nlp_flag |= NLP_DELAY_TMO;
4074 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05004075 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
4076 vport->port_state = LPFC_FDISC;
4077 } else {
James Smartecfd03c2010-02-12 14:41:27 -05004078 /*
4079 * Otherwise, we request port to rediscover
4080 * the entire FCF table for a fast recovery
4081 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05004082 * is no longer valid if we are not already
4083 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05004084 */
James Smartfc2b9892010-02-26 14:15:29 -05004085 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004086 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05004087 spin_unlock_irq(&phba->hbalock);
4088 break;
4089 }
4090 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05004091 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004092 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004093 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4094 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004095 "2773 Start FCF failover per CVL, "
James Smart70f3c072010-12-15 17:57:33 -05004096 "evt_tag:x%x\n", acqe_fip->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05004097 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05004098 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004099 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4100 LOG_DISCOVERY,
4101 "2774 Issue FCF rediscover "
4102 "mabilbox command failed, "
4103 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05004104 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004105 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004106 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05004107 /*
4108 * Last resort will be re-try on the
4109 * the current registered FCF entry.
4110 */
4111 lpfc_retry_pport_discovery(phba);
James Smart38b92ef2010-08-04 16:11:39 -04004112 } else
4113 /*
4114 * Reset FCF roundrobin bmask for new
4115 * discovery.
4116 */
James Smart7d791df2011-07-22 18:37:52 -04004117 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart6669f9b2009-10-02 15:16:45 -04004118 }
4119 break;
James Smartda0436e2009-05-22 14:51:39 -04004120 default:
4121 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4122 "0288 Unknown FCoE event type 0x%x event tag "
James Smart70f3c072010-12-15 17:57:33 -05004123 "0x%x\n", event_type, acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04004124 break;
4125 }
4126}
4127
4128/**
4129 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
4130 * @phba: pointer to lpfc hba data structure.
4131 * @acqe_link: pointer to the async dcbx completion queue entry.
4132 *
4133 * This routine is to handle the SLI4 asynchronous dcbx event.
4134 **/
4135static void
4136lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
4137 struct lpfc_acqe_dcbx *acqe_dcbx)
4138{
James Smart4d9ab992009-10-02 15:16:39 -04004139 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004140 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4141 "0290 The SLI4 DCBX asynchronous event is not "
4142 "handled yet\n");
4143}
4144
4145/**
James Smartb19a0612010-04-06 14:48:51 -04004146 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
4147 * @phba: pointer to lpfc hba data structure.
4148 * @acqe_link: pointer to the async grp5 completion queue entry.
4149 *
4150 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
4151 * is an asynchronous notified of a logical link speed change. The Port
4152 * reports the logical link speed in units of 10Mbps.
4153 **/
4154static void
4155lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
4156 struct lpfc_acqe_grp5 *acqe_grp5)
4157{
4158 uint16_t prev_ll_spd;
4159
4160 phba->fc_eventTag = acqe_grp5->event_tag;
4161 phba->fcoe_eventtag = acqe_grp5->event_tag;
4162 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
4163 phba->sli4_hba.link_state.logical_speed =
4164 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5));
4165 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4166 "2789 GRP5 Async Event: Updating logical link speed "
4167 "from %dMbps to %dMbps\n", (prev_ll_spd * 10),
4168 (phba->sli4_hba.link_state.logical_speed*10));
4169}
4170
4171/**
James Smartda0436e2009-05-22 14:51:39 -04004172 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
4173 * @phba: pointer to lpfc hba data structure.
4174 *
4175 * This routine is invoked by the worker thread to process all the pending
4176 * SLI4 asynchronous events.
4177 **/
4178void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
4179{
4180 struct lpfc_cq_event *cq_event;
4181
4182 /* First, declare the async event has been handled */
4183 spin_lock_irq(&phba->hbalock);
4184 phba->hba_flag &= ~ASYNC_EVENT;
4185 spin_unlock_irq(&phba->hbalock);
4186 /* Now, handle all the async events */
4187 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
4188 /* Get the first event from the head of the event queue */
4189 spin_lock_irq(&phba->hbalock);
4190 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
4191 cq_event, struct lpfc_cq_event, list);
4192 spin_unlock_irq(&phba->hbalock);
4193 /* Process the asynchronous event */
4194 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
4195 case LPFC_TRAILER_CODE_LINK:
4196 lpfc_sli4_async_link_evt(phba,
4197 &cq_event->cqe.acqe_link);
4198 break;
4199 case LPFC_TRAILER_CODE_FCOE:
James Smart70f3c072010-12-15 17:57:33 -05004200 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004201 break;
4202 case LPFC_TRAILER_CODE_DCBX:
4203 lpfc_sli4_async_dcbx_evt(phba,
4204 &cq_event->cqe.acqe_dcbx);
4205 break;
James Smartb19a0612010-04-06 14:48:51 -04004206 case LPFC_TRAILER_CODE_GRP5:
4207 lpfc_sli4_async_grp5_evt(phba,
4208 &cq_event->cqe.acqe_grp5);
4209 break;
James Smart70f3c072010-12-15 17:57:33 -05004210 case LPFC_TRAILER_CODE_FC:
4211 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
4212 break;
4213 case LPFC_TRAILER_CODE_SLI:
4214 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
4215 break;
James Smartda0436e2009-05-22 14:51:39 -04004216 default:
4217 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4218 "1804 Invalid asynchrous event code: "
4219 "x%x\n", bf_get(lpfc_trailer_code,
4220 &cq_event->cqe.mcqe_cmpl));
4221 break;
4222 }
4223 /* Free the completion event processed to the free pool */
4224 lpfc_sli4_cq_event_release(phba, cq_event);
4225 }
4226}
4227
4228/**
James Smartecfd03c2010-02-12 14:41:27 -05004229 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
4230 * @phba: pointer to lpfc hba data structure.
4231 *
4232 * This routine is invoked by the worker thread to process FCF table
4233 * rediscovery pending completion event.
4234 **/
4235void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
4236{
4237 int rc;
4238
4239 spin_lock_irq(&phba->hbalock);
4240 /* Clear FCF rediscovery timeout event */
4241 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
4242 /* Clear driver fast failover FCF record flag */
4243 phba->fcf.failover_rec.flag = 0;
4244 /* Set state for FCF fast failover */
4245 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
4246 spin_unlock_irq(&phba->hbalock);
4247
4248 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05004249 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004250 "2777 Start post-quiescent FCF table scan\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05004251 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05004252 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05004253 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4254 "2747 Issue FCF scan read FCF mailbox "
4255 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05004256}
4257
4258/**
James Smartda0436e2009-05-22 14:51:39 -04004259 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
4260 * @phba: pointer to lpfc hba data structure.
4261 * @dev_grp: The HBA PCI-Device group number.
4262 *
4263 * This routine is invoked to set up the per HBA PCI-Device group function
4264 * API jump table entries.
4265 *
4266 * Return: 0 if success, otherwise -ENODEV
4267 **/
4268int
4269lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05004270{
4271 int rc;
4272
James Smartda0436e2009-05-22 14:51:39 -04004273 /* Set up lpfc PCI-device group */
4274 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05004275
James Smartda0436e2009-05-22 14:51:39 -04004276 /* The LPFC_PCI_DEV_OC uses SLI4 */
4277 if (dev_grp == LPFC_PCI_DEV_OC)
4278 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05004279
James Smartda0436e2009-05-22 14:51:39 -04004280 /* Set up device INIT API function jump table */
4281 rc = lpfc_init_api_table_setup(phba, dev_grp);
4282 if (rc)
4283 return -ENODEV;
4284 /* Set up SCSI API function jump table */
4285 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
4286 if (rc)
4287 return -ENODEV;
4288 /* Set up SLI API function jump table */
4289 rc = lpfc_sli_api_table_setup(phba, dev_grp);
4290 if (rc)
4291 return -ENODEV;
4292 /* Set up MBOX API function jump table */
4293 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
4294 if (rc)
4295 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004296
James Smartda0436e2009-05-22 14:51:39 -04004297 return 0;
James Smart5b75da22008-12-04 22:39:35 -05004298}
4299
4300/**
James Smart3621a712009-04-06 18:47:14 -04004301 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05004302 * @phba: pointer to lpfc hba data structure.
4303 * @intr_mode: active interrupt mode adopted.
4304 *
4305 * This routine it invoked to log the currently used active interrupt mode
4306 * to the device.
James Smart3772a992009-05-22 14:50:54 -04004307 **/
4308static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05004309{
4310 switch (intr_mode) {
4311 case 0:
4312 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4313 "0470 Enable INTx interrupt mode.\n");
4314 break;
4315 case 1:
4316 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4317 "0481 Enabled MSI interrupt mode.\n");
4318 break;
4319 case 2:
4320 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4321 "0480 Enabled MSI-X interrupt mode.\n");
4322 break;
4323 default:
4324 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4325 "0482 Illegal interrupt mode.\n");
4326 break;
4327 }
4328 return;
4329}
4330
James Smart5b75da22008-12-04 22:39:35 -05004331/**
James Smart3772a992009-05-22 14:50:54 -04004332 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05004333 * @phba: pointer to lpfc hba data structure.
4334 *
James Smart3772a992009-05-22 14:50:54 -04004335 * This routine is invoked to enable the PCI device that is common to all
4336 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004337 *
4338 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004339 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004340 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05004341 **/
James Smart3772a992009-05-22 14:50:54 -04004342static int
4343lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004344{
James Smart3772a992009-05-22 14:50:54 -04004345 struct pci_dev *pdev;
James Smart079b5c92011-08-21 21:48:49 -04004346 int bars = 0;
James Smart5b75da22008-12-04 22:39:35 -05004347
James Smart3772a992009-05-22 14:50:54 -04004348 /* Obtain PCI device reference */
4349 if (!phba->pcidev)
4350 goto out_error;
4351 else
4352 pdev = phba->pcidev;
4353 /* Select PCI BARs */
4354 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4355 /* Enable PCI device */
4356 if (pci_enable_device_mem(pdev))
4357 goto out_error;
4358 /* Request PCI resource for the device */
4359 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
4360 goto out_disable_device;
4361 /* Set up device as PCI master and save state for EEH */
4362 pci_set_master(pdev);
4363 pci_try_set_mwi(pdev);
4364 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05004365
James Smart05580562011-05-24 11:40:48 -04004366 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
4367 if (pci_find_capability(pdev, PCI_CAP_ID_EXP))
4368 pdev->needs_freset = 1;
4369
James Smart3772a992009-05-22 14:50:54 -04004370 return 0;
James Smart5b75da22008-12-04 22:39:35 -05004371
James Smart3772a992009-05-22 14:50:54 -04004372out_disable_device:
4373 pci_disable_device(pdev);
4374out_error:
James Smart079b5c92011-08-21 21:48:49 -04004375 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4376 "1401 Failed to enable pci device, bars:x%x\n", bars);
James Smart3772a992009-05-22 14:50:54 -04004377 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004378}
4379
4380/**
James Smart3772a992009-05-22 14:50:54 -04004381 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05004382 * @phba: pointer to lpfc hba data structure.
4383 *
James Smart3772a992009-05-22 14:50:54 -04004384 * This routine is invoked to disable the PCI device that is common to all
4385 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004386 **/
4387static void
James Smart3772a992009-05-22 14:50:54 -04004388lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004389{
James Smart3772a992009-05-22 14:50:54 -04004390 struct pci_dev *pdev;
4391 int bars;
James Smart5b75da22008-12-04 22:39:35 -05004392
James Smart3772a992009-05-22 14:50:54 -04004393 /* Obtain PCI device reference */
4394 if (!phba->pcidev)
4395 return;
4396 else
4397 pdev = phba->pcidev;
4398 /* Select PCI BARs */
4399 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4400 /* Release PCI resource and disable PCI device */
4401 pci_release_selected_regions(pdev, bars);
4402 pci_disable_device(pdev);
4403 /* Null out PCI private reference to driver */
4404 pci_set_drvdata(pdev, NULL);
James Smart5b75da22008-12-04 22:39:35 -05004405
4406 return;
4407}
4408
4409/**
James Smart3772a992009-05-22 14:50:54 -04004410 * lpfc_reset_hba - Reset a hba
4411 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04004412 *
James Smart3772a992009-05-22 14:50:54 -04004413 * This routine is invoked to reset a hba device. It brings the HBA
4414 * offline, performs a board restart, and then brings the board back
4415 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
4416 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04004417 **/
James Smart3772a992009-05-22 14:50:54 -04004418void
4419lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004420{
James Smart3772a992009-05-22 14:50:54 -04004421 /* If resets are disabled then set error state and return. */
4422 if (!phba->cfg_enable_hba_reset) {
4423 phba->link_state = LPFC_HBA_ERROR;
4424 return;
4425 }
James Smart618a5232012-06-12 13:54:36 -04004426 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3772a992009-05-22 14:50:54 -04004427 lpfc_offline(phba);
4428 lpfc_sli_brdrestart(phba);
4429 lpfc_online(phba);
4430 lpfc_unblock_mgmt_io(phba);
4431}
dea31012005-04-17 16:05:31 -05004432
James Smart3772a992009-05-22 14:50:54 -04004433/**
James Smart0a96e972011-07-22 18:37:28 -04004434 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
4435 * @phba: pointer to lpfc hba data structure.
4436 *
4437 * This function enables the PCI SR-IOV virtual functions to a physical
4438 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4439 * enable the number of virtual functions to the physical function. As
4440 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4441 * API call does not considered as an error condition for most of the device.
4442 **/
4443uint16_t
4444lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
4445{
4446 struct pci_dev *pdev = phba->pcidev;
4447 uint16_t nr_virtfn;
4448 int pos;
4449
James Smart0a96e972011-07-22 18:37:28 -04004450 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
4451 if (pos == 0)
4452 return 0;
4453
4454 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
4455 return nr_virtfn;
4456}
4457
4458/**
James Smart912e3ac2011-05-24 11:42:11 -04004459 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
4460 * @phba: pointer to lpfc hba data structure.
4461 * @nr_vfn: number of virtual functions to be enabled.
4462 *
4463 * This function enables the PCI SR-IOV virtual functions to a physical
4464 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4465 * enable the number of virtual functions to the physical function. As
4466 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4467 * API call does not considered as an error condition for most of the device.
4468 **/
4469int
4470lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
4471{
4472 struct pci_dev *pdev = phba->pcidev;
James Smart0a96e972011-07-22 18:37:28 -04004473 uint16_t max_nr_vfn;
James Smart912e3ac2011-05-24 11:42:11 -04004474 int rc;
4475
James Smart0a96e972011-07-22 18:37:28 -04004476 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
4477 if (nr_vfn > max_nr_vfn) {
4478 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4479 "3057 Requested vfs (%d) greater than "
4480 "supported vfs (%d)", nr_vfn, max_nr_vfn);
4481 return -EINVAL;
4482 }
4483
James Smart912e3ac2011-05-24 11:42:11 -04004484 rc = pci_enable_sriov(pdev, nr_vfn);
4485 if (rc) {
4486 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4487 "2806 Failed to enable sriov on this device "
4488 "with vfn number nr_vf:%d, rc:%d\n",
4489 nr_vfn, rc);
4490 } else
4491 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4492 "2807 Successful enable sriov on this device "
4493 "with vfn number nr_vf:%d\n", nr_vfn);
4494 return rc;
4495}
4496
4497/**
James Smart3772a992009-05-22 14:50:54 -04004498 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
4499 * @phba: pointer to lpfc hba data structure.
4500 *
4501 * This routine is invoked to set up the driver internal resources specific to
4502 * support the SLI-3 HBA device it attached to.
4503 *
4504 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004505 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004506 * other values - error
4507 **/
4508static int
4509lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
4510{
4511 struct lpfc_sli *psli;
James Smart912e3ac2011-05-24 11:42:11 -04004512 int rc;
dea31012005-04-17 16:05:31 -05004513
James Smart3772a992009-05-22 14:50:54 -04004514 /*
4515 * Initialize timers used by driver
4516 */
dea31012005-04-17 16:05:31 -05004517
James Smart3772a992009-05-22 14:50:54 -04004518 /* Heartbeat timer */
4519 init_timer(&phba->hb_tmofunc);
4520 phba->hb_tmofunc.function = lpfc_hb_timeout;
4521 phba->hb_tmofunc.data = (unsigned long)phba;
4522
4523 psli = &phba->sli;
4524 /* MBOX heartbeat timer */
4525 init_timer(&psli->mbox_tmo);
4526 psli->mbox_tmo.function = lpfc_mbox_timeout;
4527 psli->mbox_tmo.data = (unsigned long) phba;
4528 /* FCP polling mode timer */
4529 init_timer(&phba->fcp_poll_timer);
4530 phba->fcp_poll_timer.function = lpfc_poll_timeout;
4531 phba->fcp_poll_timer.data = (unsigned long) phba;
4532 /* Fabric block timer */
4533 init_timer(&phba->fabric_block_timer);
4534 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4535 phba->fabric_block_timer.data = (unsigned long) phba;
4536 /* EA polling mode timer */
4537 init_timer(&phba->eratt_poll);
4538 phba->eratt_poll.function = lpfc_poll_eratt;
4539 phba->eratt_poll.data = (unsigned long) phba;
4540
4541 /* Host attention work mask setup */
4542 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
4543 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
4544
4545 /* Get all the module params for configuring this host */
4546 lpfc_get_cfgparam(phba);
James Smart49198b32010-04-06 15:04:33 -04004547 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
4548 phba->menlo_flag |= HBA_MENLO_SUPPORT;
4549 /* check for menlo minimum sg count */
4550 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
4551 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
4552 }
4553
James Smart3772a992009-05-22 14:50:54 -04004554 /*
4555 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
4556 * used to create the sg_dma_buf_pool must be dynamically calculated.
4557 * 2 segments are added since the IOCB needs a command and response bde.
4558 */
4559 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4560 sizeof(struct fcp_rsp) +
4561 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
4562
4563 if (phba->cfg_enable_bg) {
4564 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT;
4565 phba->cfg_sg_dma_buf_size +=
4566 phba->cfg_prot_sg_seg_cnt * sizeof(struct ulp_bde64);
4567 }
4568
4569 /* Also reinitialize the host templates with new values. */
4570 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4571 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4572
4573 phba->max_vpi = LPFC_MAX_VPI;
4574 /* This will be set to correct value after config_port mbox */
4575 phba->max_vports = 0;
4576
4577 /*
4578 * Initialize the SLI Layer to run with lpfc HBAs.
4579 */
4580 lpfc_sli_setup(phba);
4581 lpfc_sli_queue_setup(phba);
4582
4583 /* Allocate device driver memory */
4584 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
4585 return -ENOMEM;
4586
James Smart912e3ac2011-05-24 11:42:11 -04004587 /*
4588 * Enable sr-iov virtual functions if supported and configured
4589 * through the module parameter.
4590 */
4591 if (phba->cfg_sriov_nr_virtfn > 0) {
4592 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
4593 phba->cfg_sriov_nr_virtfn);
4594 if (rc) {
4595 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4596 "2808 Requested number of SR-IOV "
4597 "virtual functions (%d) is not "
4598 "supported\n",
4599 phba->cfg_sriov_nr_virtfn);
4600 phba->cfg_sriov_nr_virtfn = 0;
4601 }
4602 }
4603
James Smart3772a992009-05-22 14:50:54 -04004604 return 0;
4605}
4606
4607/**
4608 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
4609 * @phba: pointer to lpfc hba data structure.
4610 *
4611 * This routine is invoked to unset the driver internal resources set up
4612 * specific for supporting the SLI-3 HBA device it attached to.
4613 **/
4614static void
4615lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
4616{
4617 /* Free device driver memory allocated */
4618 lpfc_mem_free_all(phba);
4619
4620 return;
4621}
4622
4623/**
James Smartda0436e2009-05-22 14:51:39 -04004624 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
4625 * @phba: pointer to lpfc hba data structure.
4626 *
4627 * This routine is invoked to set up the driver internal resources specific to
4628 * support the SLI-4 HBA device it attached to.
4629 *
4630 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004631 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004632 * other values - error
4633 **/
4634static int
4635lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
4636{
4637 struct lpfc_sli *psli;
James Smart28baac72010-02-12 14:42:03 -05004638 LPFC_MBOXQ_t *mboxq;
4639 int rc, i, hbq_count, buf_size, dma_buf_size, max_buf_size;
4640 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
4641 struct lpfc_mqe *mqe;
James Smart085c6472010-11-20 23:11:37 -05004642 int longs, sli_family;
James Smartacd68592012-01-18 16:25:09 -05004643 int sges_per_segment;
James Smartda0436e2009-05-22 14:51:39 -04004644
4645 /* Before proceed, wait for POST done and device ready */
4646 rc = lpfc_sli4_post_status_check(phba);
4647 if (rc)
4648 return -ENODEV;
4649
4650 /*
4651 * Initialize timers used by driver
4652 */
4653
4654 /* Heartbeat timer */
4655 init_timer(&phba->hb_tmofunc);
4656 phba->hb_tmofunc.function = lpfc_hb_timeout;
4657 phba->hb_tmofunc.data = (unsigned long)phba;
James Smart19ca7602010-11-20 23:11:55 -05004658 init_timer(&phba->rrq_tmr);
4659 phba->rrq_tmr.function = lpfc_rrq_timeout;
4660 phba->rrq_tmr.data = (unsigned long)phba;
James Smartda0436e2009-05-22 14:51:39 -04004661
4662 psli = &phba->sli;
4663 /* MBOX heartbeat timer */
4664 init_timer(&psli->mbox_tmo);
4665 psli->mbox_tmo.function = lpfc_mbox_timeout;
4666 psli->mbox_tmo.data = (unsigned long) phba;
4667 /* Fabric block timer */
4668 init_timer(&phba->fabric_block_timer);
4669 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4670 phba->fabric_block_timer.data = (unsigned long) phba;
4671 /* EA polling mode timer */
4672 init_timer(&phba->eratt_poll);
4673 phba->eratt_poll.function = lpfc_poll_eratt;
4674 phba->eratt_poll.data = (unsigned long) phba;
James Smartecfd03c2010-02-12 14:41:27 -05004675 /* FCF rediscover timer */
4676 init_timer(&phba->fcf.redisc_wait);
4677 phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
4678 phba->fcf.redisc_wait.data = (unsigned long)phba;
4679
James Smartda0436e2009-05-22 14:51:39 -04004680 /*
James Smart7ad20aa2011-05-24 11:44:28 -04004681 * Control structure for handling external multi-buffer mailbox
4682 * command pass-through.
4683 */
4684 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
4685 sizeof(struct lpfc_mbox_ext_buf_ctx));
4686 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
4687
4688 /*
James Smartda0436e2009-05-22 14:51:39 -04004689 * We need to do a READ_CONFIG mailbox command here before
4690 * calling lpfc_get_cfgparam. For VFs this will report the
4691 * MAX_XRI, MAX_VPI, MAX_RPI, MAX_IOCB, and MAX_VFI settings.
4692 * All of the resources allocated
4693 * for this Port are tied to these values.
4694 */
4695 /* Get all the module params for configuring this host */
4696 lpfc_get_cfgparam(phba);
4697 phba->max_vpi = LPFC_MAX_VPI;
4698 /* This will be set to correct value after the read_config mbox */
4699 phba->max_vports = 0;
4700
4701 /* Program the default value of vlan_id and fc_map */
4702 phba->valid_vlan = 0;
4703 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4704 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4705 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4706
James Smartacd68592012-01-18 16:25:09 -05004707 /* With BlockGuard we can have multiple SGEs per Data Segemnt */
4708 sges_per_segment = 1;
4709 if (phba->cfg_enable_bg)
4710 sges_per_segment = 2;
4711
James Smartda0436e2009-05-22 14:51:39 -04004712 /*
4713 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
4714 * used to create the sg_dma_buf_pool must be dynamically calculated.
4715 * 2 segments are added since the IOCB needs a command and response bde.
4716 * To insure that the scsi sgl does not cross a 4k page boundary only
James Smart28baac72010-02-12 14:42:03 -05004717 * sgl sizes of must be a power of 2.
James Smartda0436e2009-05-22 14:51:39 -04004718 */
James Smart28baac72010-02-12 14:42:03 -05004719 buf_size = (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp) +
James Smartacd68592012-01-18 16:25:09 -05004720 (((phba->cfg_sg_seg_cnt * sges_per_segment) + 2) *
4721 sizeof(struct sli4_sge)));
James Smart085c6472010-11-20 23:11:37 -05004722
4723 sli_family = bf_get(lpfc_sli_intf_sli_family, &phba->sli4_hba.sli_intf);
4724 max_buf_size = LPFC_SLI4_MAX_BUF_SIZE;
4725 switch (sli_family) {
4726 case LPFC_SLI_INTF_FAMILY_BE2:
4727 case LPFC_SLI_INTF_FAMILY_BE3:
4728 /* There is a single hint for BE - 2 pages per BPL. */
4729 if (bf_get(lpfc_sli_intf_sli_hint1, &phba->sli4_hba.sli_intf) ==
4730 LPFC_SLI_INTF_SLI_HINT1_1)
4731 max_buf_size = LPFC_SLI4_FL1_MAX_BUF_SIZE;
4732 break;
4733 case LPFC_SLI_INTF_FAMILY_LNCR_A0:
4734 case LPFC_SLI_INTF_FAMILY_LNCR_B0:
4735 default:
4736 break;
4737 }
James Smartacd68592012-01-18 16:25:09 -05004738
James Smart28baac72010-02-12 14:42:03 -05004739 for (dma_buf_size = LPFC_SLI4_MIN_BUF_SIZE;
4740 dma_buf_size < max_buf_size && buf_size > dma_buf_size;
4741 dma_buf_size = dma_buf_size << 1)
4742 ;
4743 if (dma_buf_size == max_buf_size)
4744 phba->cfg_sg_seg_cnt = (dma_buf_size -
4745 sizeof(struct fcp_cmnd) - sizeof(struct fcp_rsp) -
4746 (2 * sizeof(struct sli4_sge))) /
4747 sizeof(struct sli4_sge);
4748 phba->cfg_sg_dma_buf_size = dma_buf_size;
James Smartda0436e2009-05-22 14:51:39 -04004749
4750 /* Initialize buffer queue management fields */
4751 hbq_count = lpfc_sli_hbq_count();
4752 for (i = 0; i < hbq_count; ++i)
4753 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
4754 INIT_LIST_HEAD(&phba->rb_pend_list);
4755 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
4756 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
4757
4758 /*
4759 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
4760 */
4761 /* Initialize the Abort scsi buffer list used by driver */
4762 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
4763 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
4764 /* This abort list used by worker thread */
4765 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
4766
4767 /*
James Smart6d368e52011-05-24 11:44:12 -04004768 * Initialize driver internal slow-path work queues
James Smartda0436e2009-05-22 14:51:39 -04004769 */
4770
4771 /* Driver internel slow-path CQ Event pool */
4772 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
4773 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04004774 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04004775 /* Asynchronous event CQ Event work queue list */
4776 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
4777 /* Fast-path XRI aborted CQ Event work queue list */
4778 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
4779 /* Slow-path XRI aborted CQ Event work queue list */
4780 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
4781 /* Receive queue CQ Event work queue list */
4782 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
4783
James Smart6d368e52011-05-24 11:44:12 -04004784 /* Initialize extent block lists. */
4785 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
4786 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
4787 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
4788 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
4789
James Smartda0436e2009-05-22 14:51:39 -04004790 /* Initialize the driver internal SLI layer lists. */
4791 lpfc_sli_setup(phba);
4792 lpfc_sli_queue_setup(phba);
4793
4794 /* Allocate device driver memory */
4795 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
4796 if (rc)
4797 return -ENOMEM;
4798
James Smart2fcee4b2010-12-15 17:57:46 -05004799 /* IF Type 2 ports get initialized now. */
4800 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
4801 LPFC_SLI_INTF_IF_TYPE_2) {
4802 rc = lpfc_pci_function_reset(phba);
4803 if (unlikely(rc))
4804 return -ENODEV;
4805 }
4806
James Smartda0436e2009-05-22 14:51:39 -04004807 /* Create the bootstrap mailbox command */
4808 rc = lpfc_create_bootstrap_mbox(phba);
4809 if (unlikely(rc))
4810 goto out_free_mem;
4811
4812 /* Set up the host's endian order with the device. */
4813 rc = lpfc_setup_endian_order(phba);
4814 if (unlikely(rc))
4815 goto out_free_bsmbx;
4816
4817 /* Set up the hba's configuration parameters. */
4818 rc = lpfc_sli4_read_config(phba);
4819 if (unlikely(rc))
4820 goto out_free_bsmbx;
4821
James Smart2fcee4b2010-12-15 17:57:46 -05004822 /* IF Type 0 ports get initialized now. */
4823 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
4824 LPFC_SLI_INTF_IF_TYPE_0) {
4825 rc = lpfc_pci_function_reset(phba);
4826 if (unlikely(rc))
4827 goto out_free_bsmbx;
4828 }
James Smartda0436e2009-05-22 14:51:39 -04004829
James Smartcb5172e2010-03-15 11:25:07 -04004830 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
4831 GFP_KERNEL);
4832 if (!mboxq) {
4833 rc = -ENOMEM;
4834 goto out_free_bsmbx;
4835 }
4836
James Smartfedd3b72011-02-16 12:39:24 -05004837 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
James Smartcb5172e2010-03-15 11:25:07 -04004838 lpfc_supported_pages(mboxq);
4839 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smartfedd3b72011-02-16 12:39:24 -05004840 if (!rc) {
4841 mqe = &mboxq->u.mqe;
4842 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
4843 LPFC_MAX_SUPPORTED_PAGES);
4844 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
4845 switch (pn_page[i]) {
4846 case LPFC_SLI4_PARAMETERS:
4847 phba->sli4_hba.pc_sli4_params.supported = 1;
4848 break;
4849 default:
4850 break;
4851 }
4852 }
4853 /* Read the port's SLI4 Parameters capabilities if supported. */
4854 if (phba->sli4_hba.pc_sli4_params.supported)
4855 rc = lpfc_pc_sli4_params_get(phba, mboxq);
4856 if (rc) {
4857 mempool_free(mboxq, phba->mbox_mem_pool);
4858 rc = -EIO;
4859 goto out_free_bsmbx;
James Smartcb5172e2010-03-15 11:25:07 -04004860 }
4861 }
James Smartfedd3b72011-02-16 12:39:24 -05004862 /*
4863 * Get sli4 parameters that override parameters from Port capabilities.
James Smart6d368e52011-05-24 11:44:12 -04004864 * If this call fails, it isn't critical unless the SLI4 parameters come
4865 * back in conflict.
James Smartfedd3b72011-02-16 12:39:24 -05004866 */
James Smart6d368e52011-05-24 11:44:12 -04004867 rc = lpfc_get_sli4_parameters(phba, mboxq);
4868 if (rc) {
4869 if (phba->sli4_hba.extents_in_use &&
4870 phba->sli4_hba.rpi_hdrs_in_use) {
4871 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4872 "2999 Unsupported SLI4 Parameters "
4873 "Extents and RPI headers enabled.\n");
4874 goto out_free_bsmbx;
4875 }
4876 }
James Smartcb5172e2010-03-15 11:25:07 -04004877 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart5350d872011-10-10 21:33:49 -04004878 /* Verify all the SLI4 queues */
4879 rc = lpfc_sli4_queue_verify(phba);
James Smartda0436e2009-05-22 14:51:39 -04004880 if (rc)
4881 goto out_free_bsmbx;
4882
4883 /* Create driver internal CQE event pool */
4884 rc = lpfc_sli4_cq_event_pool_create(phba);
4885 if (rc)
James Smart5350d872011-10-10 21:33:49 -04004886 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04004887
James Smart8a9d2e82012-05-09 21:16:12 -04004888 /* Initialize sgl lists per host */
4889 lpfc_init_sgl_list(phba);
4890
4891 /* Allocate and initialize active sgl array */
James Smartda0436e2009-05-22 14:51:39 -04004892 rc = lpfc_init_active_sgl_array(phba);
4893 if (rc) {
4894 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4895 "1430 Failed to initialize sgl list.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04004896 goto out_destroy_cq_event_pool;
James Smartda0436e2009-05-22 14:51:39 -04004897 }
James Smartda0436e2009-05-22 14:51:39 -04004898 rc = lpfc_sli4_init_rpi_hdrs(phba);
4899 if (rc) {
4900 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4901 "1432 Failed to initialize rpi headers.\n");
4902 goto out_free_active_sgl;
4903 }
4904
James Smarta93ff372010-10-22 11:06:08 -04004905 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
James Smart0c9ab6f2010-02-26 14:15:57 -05004906 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
4907 phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
4908 GFP_KERNEL);
4909 if (!phba->fcf.fcf_rr_bmask) {
4910 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4911 "2759 Failed allocate memory for FCF round "
4912 "robin failover bmask\n");
James Smart05580562011-05-24 11:40:48 -04004913 rc = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -05004914 goto out_remove_rpi_hdrs;
4915 }
4916
James Smart5350d872011-10-10 21:33:49 -04004917 /*
4918 * The cfg_fcp_eq_count can be zero whenever there is exactly one
4919 * interrupt vector. This is not an error
4920 */
4921 if (phba->cfg_fcp_eq_count) {
4922 phba->sli4_hba.fcp_eq_hdl =
4923 kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
James Smartda0436e2009-05-22 14:51:39 -04004924 phba->cfg_fcp_eq_count), GFP_KERNEL);
James Smart5350d872011-10-10 21:33:49 -04004925 if (!phba->sli4_hba.fcp_eq_hdl) {
4926 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4927 "2572 Failed allocate memory for "
4928 "fast-path per-EQ handle array\n");
4929 rc = -ENOMEM;
4930 goto out_free_fcf_rr_bmask;
4931 }
James Smartda0436e2009-05-22 14:51:39 -04004932 }
4933
4934 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
4935 phba->sli4_hba.cfg_eqn), GFP_KERNEL);
4936 if (!phba->sli4_hba.msix_entries) {
4937 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4938 "2573 Failed allocate memory for msi-x "
4939 "interrupt vector entries\n");
James Smart05580562011-05-24 11:40:48 -04004940 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04004941 goto out_free_fcp_eq_hdl;
4942 }
4943
James Smart912e3ac2011-05-24 11:42:11 -04004944 /*
4945 * Enable sr-iov virtual functions if supported and configured
4946 * through the module parameter.
4947 */
4948 if (phba->cfg_sriov_nr_virtfn > 0) {
4949 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
4950 phba->cfg_sriov_nr_virtfn);
4951 if (rc) {
4952 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4953 "3020 Requested number of SR-IOV "
4954 "virtual functions (%d) is not "
4955 "supported\n",
4956 phba->cfg_sriov_nr_virtfn);
4957 phba->cfg_sriov_nr_virtfn = 0;
4958 }
4959 }
4960
James Smart5248a742011-07-22 18:37:06 -04004961 return 0;
James Smartda0436e2009-05-22 14:51:39 -04004962
4963out_free_fcp_eq_hdl:
4964 kfree(phba->sli4_hba.fcp_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05004965out_free_fcf_rr_bmask:
4966 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04004967out_remove_rpi_hdrs:
4968 lpfc_sli4_remove_rpi_hdrs(phba);
4969out_free_active_sgl:
4970 lpfc_free_active_sgl(phba);
James Smartda0436e2009-05-22 14:51:39 -04004971out_destroy_cq_event_pool:
4972 lpfc_sli4_cq_event_pool_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04004973out_free_bsmbx:
4974 lpfc_destroy_bootstrap_mbox(phba);
4975out_free_mem:
4976 lpfc_mem_free(phba);
4977 return rc;
4978}
4979
4980/**
4981 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
4982 * @phba: pointer to lpfc hba data structure.
4983 *
4984 * This routine is invoked to unset the driver internal resources set up
4985 * specific for supporting the SLI-4 HBA device it attached to.
4986 **/
4987static void
4988lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
4989{
4990 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
4991
James Smartda0436e2009-05-22 14:51:39 -04004992 /* Free memory allocated for msi-x interrupt vector entries */
4993 kfree(phba->sli4_hba.msix_entries);
4994
4995 /* Free memory allocated for fast-path work queue handles */
4996 kfree(phba->sli4_hba.fcp_eq_hdl);
4997
4998 /* Free the allocated rpi headers. */
4999 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04005000 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04005001
James Smart0c9ab6f2010-02-26 14:15:57 -05005002 /* Free eligible FCF index bmask */
5003 kfree(phba->fcf.fcf_rr_bmask);
5004
James Smartda0436e2009-05-22 14:51:39 -04005005 /* Free the ELS sgl list */
5006 lpfc_free_active_sgl(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04005007 lpfc_free_els_sgl_list(phba);
James Smartda0436e2009-05-22 14:51:39 -04005008
James Smartda0436e2009-05-22 14:51:39 -04005009 /* Free the completion queue EQ event pool */
5010 lpfc_sli4_cq_event_release_all(phba);
5011 lpfc_sli4_cq_event_pool_destroy(phba);
5012
James Smart6d368e52011-05-24 11:44:12 -04005013 /* Release resource identifiers. */
5014 lpfc_sli4_dealloc_resource_identifiers(phba);
5015
James Smartda0436e2009-05-22 14:51:39 -04005016 /* Free the bsmbx region. */
5017 lpfc_destroy_bootstrap_mbox(phba);
5018
5019 /* Free the SLI Layer memory with SLI4 HBAs */
5020 lpfc_mem_free_all(phba);
5021
5022 /* Free the current connect table */
5023 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04005024 &phba->fcf_conn_rec_list, list) {
5025 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04005026 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04005027 }
James Smartda0436e2009-05-22 14:51:39 -04005028
5029 return;
5030}
5031
5032/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005033 * lpfc_init_api_table_setup - Set up init api function jump table
James Smart3772a992009-05-22 14:50:54 -04005034 * @phba: The hba struct for which this call is being executed.
5035 * @dev_grp: The HBA PCI-Device group number.
5036 *
5037 * This routine sets up the device INIT interface API function jump table
5038 * in @phba struct.
5039 *
5040 * Returns: 0 - success, -ENODEV - failure.
5041 **/
5042int
5043lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5044{
James Smart84d1b002010-02-12 14:42:33 -05005045 phba->lpfc_hba_init_link = lpfc_hba_init_link;
5046 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart7f860592011-03-11 16:05:52 -05005047 phba->lpfc_selective_reset = lpfc_selective_reset;
James Smart3772a992009-05-22 14:50:54 -04005048 switch (dev_grp) {
5049 case LPFC_PCI_DEV_LP:
5050 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
5051 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
5052 phba->lpfc_stop_port = lpfc_stop_port_s3;
5053 break;
James Smartda0436e2009-05-22 14:51:39 -04005054 case LPFC_PCI_DEV_OC:
5055 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
5056 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
5057 phba->lpfc_stop_port = lpfc_stop_port_s4;
5058 break;
James Smart3772a992009-05-22 14:50:54 -04005059 default:
5060 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5061 "1431 Invalid HBA PCI-device group: 0x%x\n",
5062 dev_grp);
5063 return -ENODEV;
5064 break;
5065 }
5066 return 0;
5067}
5068
5069/**
5070 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
5071 * @phba: pointer to lpfc hba data structure.
5072 *
5073 * This routine is invoked to set up the driver internal resources before the
5074 * device specific resource setup to support the HBA device it attached to.
5075 *
5076 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005077 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005078 * other values - error
5079 **/
5080static int
5081lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
5082{
5083 /*
5084 * Driver resources common to all SLI revisions
5085 */
James Smartea2151b2008-09-07 11:52:10 -04005086 atomic_set(&phba->fast_event_count, 0);
James Smart2e0fef82007-06-17 19:56:36 -05005087 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005088
James Smarte47c9092008-02-08 18:49:26 -05005089 /* Initialize ndlp management spinlock */
5090 spin_lock_init(&phba->ndlp_lock);
5091
James Smart2e0fef82007-06-17 19:56:36 -05005092 INIT_LIST_HEAD(&phba->port_list);
James Smart3772a992009-05-22 14:50:54 -04005093 INIT_LIST_HEAD(&phba->work_list);
James Smart84774a42008-08-24 21:50:06 -04005094 init_waitqueue_head(&phba->wait_4_mlo_m_q);
dea31012005-04-17 16:05:31 -05005095
James Smart3772a992009-05-22 14:50:54 -04005096 /* Initialize the wait queue head for the kernel thread */
5097 init_waitqueue_head(&phba->work_waitq);
James Smart858c9f62007-06-17 19:56:39 -05005098
James Smart3772a992009-05-22 14:50:54 -04005099 /* Initialize the scsi buffer list used by driver for scsi IO */
5100 spin_lock_init(&phba->scsi_buf_list_lock);
5101 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
dea31012005-04-17 16:05:31 -05005102
James Smart3772a992009-05-22 14:50:54 -04005103 /* Initialize the fabric iocb list */
5104 INIT_LIST_HEAD(&phba->fabric_iocb_list);
dea31012005-04-17 16:05:31 -05005105
James Smart3772a992009-05-22 14:50:54 -04005106 /* Initialize list to save ELS buffers */
5107 INIT_LIST_HEAD(&phba->elsbuf);
dea31012005-04-17 16:05:31 -05005108
James Smart3772a992009-05-22 14:50:54 -04005109 /* Initialize FCF connection rec list */
5110 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
dea31012005-04-17 16:05:31 -05005111
James Smart3772a992009-05-22 14:50:54 -04005112 return 0;
5113}
dea31012005-04-17 16:05:31 -05005114
James Smart3772a992009-05-22 14:50:54 -04005115/**
5116 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
5117 * @phba: pointer to lpfc hba data structure.
5118 *
5119 * This routine is invoked to set up the driver internal resources after the
5120 * device specific resource setup to support the HBA device it attached to.
5121 *
5122 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005123 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005124 * other values - error
5125 **/
5126static int
5127lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
5128{
5129 int error;
5130
5131 /* Startup the kernel thread for this host adapter. */
5132 phba->worker_thread = kthread_run(lpfc_do_work, phba,
5133 "lpfc_worker_%d", phba->brd_no);
5134 if (IS_ERR(phba->worker_thread)) {
5135 error = PTR_ERR(phba->worker_thread);
5136 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005137 }
5138
James Smart3772a992009-05-22 14:50:54 -04005139 return 0;
5140}
5141
5142/**
5143 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
5144 * @phba: pointer to lpfc hba data structure.
5145 *
5146 * This routine is invoked to unset the driver internal resources set up after
5147 * the device specific resource setup for supporting the HBA device it
5148 * attached to.
5149 **/
5150static void
5151lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
5152{
5153 /* Stop kernel worker thread */
5154 kthread_stop(phba->worker_thread);
5155}
5156
5157/**
5158 * lpfc_free_iocb_list - Free iocb list.
5159 * @phba: pointer to lpfc hba data structure.
5160 *
5161 * This routine is invoked to free the driver's IOCB list and memory.
5162 **/
5163static void
5164lpfc_free_iocb_list(struct lpfc_hba *phba)
5165{
5166 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
5167
5168 spin_lock_irq(&phba->hbalock);
5169 list_for_each_entry_safe(iocbq_entry, iocbq_next,
5170 &phba->lpfc_iocb_list, list) {
5171 list_del(&iocbq_entry->list);
5172 kfree(iocbq_entry);
5173 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005174 }
James Smart3772a992009-05-22 14:50:54 -04005175 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005176
James Smart3772a992009-05-22 14:50:54 -04005177 return;
5178}
dea31012005-04-17 16:05:31 -05005179
James Smart3772a992009-05-22 14:50:54 -04005180/**
5181 * lpfc_init_iocb_list - Allocate and initialize iocb list.
5182 * @phba: pointer to lpfc hba data structure.
5183 *
5184 * This routine is invoked to allocate and initizlize the driver's IOCB
5185 * list and set up the IOCB tag array accordingly.
5186 *
5187 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005188 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005189 * other values - error
5190 **/
5191static int
5192lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
5193{
5194 struct lpfc_iocbq *iocbq_entry = NULL;
5195 uint16_t iotag;
5196 int i;
dea31012005-04-17 16:05:31 -05005197
5198 /* Initialize and populate the iocb list per host. */
5199 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04005200 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07005201 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05005202 if (iocbq_entry == NULL) {
5203 printk(KERN_ERR "%s: only allocated %d iocbs of "
5204 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07005205 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05005206 goto out_free_iocbq;
5207 }
5208
James Bottomley604a3e32005-10-29 10:28:33 -05005209 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
5210 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04005211 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05005212 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04005213 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05005214 goto out_free_iocbq;
5215 }
James Smart6d368e52011-05-24 11:44:12 -04005216 iocbq_entry->sli4_lxritag = NO_XRI;
James Smart3772a992009-05-22 14:50:54 -04005217 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05005218
5219 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005220 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
5221 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05005222 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005223 }
5224
James Smart3772a992009-05-22 14:50:54 -04005225 return 0;
5226
5227out_free_iocbq:
5228 lpfc_free_iocb_list(phba);
5229
5230 return -ENOMEM;
5231}
5232
5233/**
James Smart8a9d2e82012-05-09 21:16:12 -04005234 * lpfc_free_sgl_list - Free a given sgl list.
James Smartda0436e2009-05-22 14:51:39 -04005235 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -04005236 * @sglq_list: pointer to the head of sgl list.
James Smartda0436e2009-05-22 14:51:39 -04005237 *
James Smart8a9d2e82012-05-09 21:16:12 -04005238 * This routine is invoked to free a give sgl list and memory.
James Smartda0436e2009-05-22 14:51:39 -04005239 **/
James Smart8a9d2e82012-05-09 21:16:12 -04005240void
5241lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
James Smartda0436e2009-05-22 14:51:39 -04005242{
5243 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart8a9d2e82012-05-09 21:16:12 -04005244
5245 list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
5246 list_del(&sglq_entry->list);
5247 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
5248 kfree(sglq_entry);
5249 }
5250}
5251
5252/**
5253 * lpfc_free_els_sgl_list - Free els sgl list.
5254 * @phba: pointer to lpfc hba data structure.
5255 *
5256 * This routine is invoked to free the driver's els sgl list and memory.
5257 **/
5258static void
5259lpfc_free_els_sgl_list(struct lpfc_hba *phba)
5260{
James Smartda0436e2009-05-22 14:51:39 -04005261 LIST_HEAD(sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04005262
James Smart8a9d2e82012-05-09 21:16:12 -04005263 /* Retrieve all els sgls from driver list */
James Smartda0436e2009-05-22 14:51:39 -04005264 spin_lock_irq(&phba->hbalock);
5265 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
5266 spin_unlock_irq(&phba->hbalock);
5267
James Smart8a9d2e82012-05-09 21:16:12 -04005268 /* Now free the sgl list */
5269 lpfc_free_sgl_list(phba, &sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04005270}
5271
5272/**
5273 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
5274 * @phba: pointer to lpfc hba data structure.
5275 *
5276 * This routine is invoked to allocate the driver's active sgl memory.
5277 * This array will hold the sglq_entry's for active IOs.
5278 **/
5279static int
5280lpfc_init_active_sgl_array(struct lpfc_hba *phba)
5281{
5282 int size;
5283 size = sizeof(struct lpfc_sglq *);
5284 size *= phba->sli4_hba.max_cfg_param.max_xri;
5285
5286 phba->sli4_hba.lpfc_sglq_active_list =
5287 kzalloc(size, GFP_KERNEL);
5288 if (!phba->sli4_hba.lpfc_sglq_active_list)
5289 return -ENOMEM;
5290 return 0;
5291}
5292
5293/**
5294 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
5295 * @phba: pointer to lpfc hba data structure.
5296 *
5297 * This routine is invoked to walk through the array of active sglq entries
5298 * and free all of the resources.
5299 * This is just a place holder for now.
5300 **/
5301static void
5302lpfc_free_active_sgl(struct lpfc_hba *phba)
5303{
5304 kfree(phba->sli4_hba.lpfc_sglq_active_list);
5305}
5306
5307/**
5308 * lpfc_init_sgl_list - Allocate and initialize sgl list.
5309 * @phba: pointer to lpfc hba data structure.
5310 *
5311 * This routine is invoked to allocate and initizlize the driver's sgl
5312 * list and set up the sgl xritag tag array accordingly.
5313 *
James Smartda0436e2009-05-22 14:51:39 -04005314 **/
James Smart8a9d2e82012-05-09 21:16:12 -04005315static void
James Smartda0436e2009-05-22 14:51:39 -04005316lpfc_init_sgl_list(struct lpfc_hba *phba)
5317{
James Smartda0436e2009-05-22 14:51:39 -04005318 /* Initialize and populate the sglq list per host/VF. */
5319 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
5320 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
5321
James Smart8a9d2e82012-05-09 21:16:12 -04005322 /* els xri-sgl book keeping */
5323 phba->sli4_hba.els_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04005324
James Smart8a9d2e82012-05-09 21:16:12 -04005325 /* scsi xri-buffer book keeping */
James Smartda0436e2009-05-22 14:51:39 -04005326 phba->sli4_hba.scsi_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04005327}
5328
5329/**
5330 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
5331 * @phba: pointer to lpfc hba data structure.
5332 *
5333 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -04005334 * port for those SLI4 ports that do not support extents. This routine
James Smartda0436e2009-05-22 14:51:39 -04005335 * posts a PAGE_SIZE memory region to the port to hold up to
James Smart88a2cfb2011-07-22 18:36:33 -04005336 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
5337 * and should be called only when interrupts are disabled.
James Smartda0436e2009-05-22 14:51:39 -04005338 *
5339 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005340 * 0 - successful
James Smart88a2cfb2011-07-22 18:36:33 -04005341 * -ERROR - otherwise.
James Smartda0436e2009-05-22 14:51:39 -04005342 **/
5343int
5344lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
5345{
5346 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04005347 struct lpfc_rpi_hdr *rpi_hdr;
5348
5349 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
James Smartff78d8f2011-12-13 13:21:35 -05005350 if (!phba->sli4_hba.rpi_hdrs_in_use)
James Smart6d368e52011-05-24 11:44:12 -04005351 return rc;
James Smart6d368e52011-05-24 11:44:12 -04005352 if (phba->sli4_hba.extents_in_use)
5353 return -EIO;
James Smartda0436e2009-05-22 14:51:39 -04005354
5355 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
5356 if (!rpi_hdr) {
5357 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5358 "0391 Error during rpi post operation\n");
5359 lpfc_sli4_remove_rpis(phba);
5360 rc = -ENODEV;
5361 }
5362
5363 return rc;
5364}
5365
5366/**
5367 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
5368 * @phba: pointer to lpfc hba data structure.
5369 *
5370 * This routine is invoked to allocate a single 4KB memory region to
5371 * support rpis and stores them in the phba. This single region
5372 * provides support for up to 64 rpis. The region is used globally
5373 * by the device.
5374 *
5375 * Returns:
5376 * A valid rpi hdr on success.
5377 * A NULL pointer on any failure.
5378 **/
5379struct lpfc_rpi_hdr *
5380lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
5381{
5382 uint16_t rpi_limit, curr_rpi_range;
5383 struct lpfc_dmabuf *dmabuf;
5384 struct lpfc_rpi_hdr *rpi_hdr;
James Smart9589b0622011-04-16 11:03:17 -04005385 uint32_t rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04005386
James Smart6d368e52011-05-24 11:44:12 -04005387 /*
5388 * If the SLI4 port supports extents, posting the rpi header isn't
5389 * required. Set the expected maximum count and let the actual value
5390 * get set when extents are fully allocated.
5391 */
5392 if (!phba->sli4_hba.rpi_hdrs_in_use)
5393 return NULL;
5394 if (phba->sli4_hba.extents_in_use)
5395 return NULL;
5396
5397 /* The limit on the logical index is just the max_rpi count. */
James Smartda0436e2009-05-22 14:51:39 -04005398 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
James Smart6d368e52011-05-24 11:44:12 -04005399 phba->sli4_hba.max_cfg_param.max_rpi - 1;
James Smartda0436e2009-05-22 14:51:39 -04005400
5401 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04005402 /*
5403 * Establish the starting RPI in this header block. The starting
5404 * rpi is normalized to a zero base because the physical rpi is
5405 * port based.
5406 */
James Smart97f2ecf2012-03-01 22:35:23 -05005407 curr_rpi_range = phba->sli4_hba.next_rpi;
James Smartda0436e2009-05-22 14:51:39 -04005408 spin_unlock_irq(&phba->hbalock);
5409
5410 /*
5411 * The port has a limited number of rpis. The increment here
5412 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
5413 * and to allow the full max_rpi range per port.
5414 */
5415 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
James Smart9589b0622011-04-16 11:03:17 -04005416 rpi_count = rpi_limit - curr_rpi_range;
5417 else
5418 rpi_count = LPFC_RPI_HDR_COUNT;
James Smartda0436e2009-05-22 14:51:39 -04005419
James Smart6d368e52011-05-24 11:44:12 -04005420 if (!rpi_count)
5421 return NULL;
James Smartda0436e2009-05-22 14:51:39 -04005422 /*
5423 * First allocate the protocol header region for the port. The
5424 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
5425 */
5426 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5427 if (!dmabuf)
5428 return NULL;
5429
5430 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5431 LPFC_HDR_TEMPLATE_SIZE,
5432 &dmabuf->phys,
5433 GFP_KERNEL);
5434 if (!dmabuf->virt) {
5435 rpi_hdr = NULL;
5436 goto err_free_dmabuf;
5437 }
5438
5439 memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
5440 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
5441 rpi_hdr = NULL;
5442 goto err_free_coherent;
5443 }
5444
5445 /* Save the rpi header data for cleanup later. */
5446 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
5447 if (!rpi_hdr)
5448 goto err_free_coherent;
5449
5450 rpi_hdr->dmabuf = dmabuf;
5451 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
5452 rpi_hdr->page_count = 1;
5453 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04005454
5455 /* The rpi_hdr stores the logical index only. */
5456 rpi_hdr->start_rpi = curr_rpi_range;
James Smartda0436e2009-05-22 14:51:39 -04005457 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
5458
5459 /*
James Smart6d368e52011-05-24 11:44:12 -04005460 * The next_rpi stores the next logical module-64 rpi value used
5461 * to post physical rpis in subsequent rpi postings.
James Smartda0436e2009-05-22 14:51:39 -04005462 */
James Smart9589b0622011-04-16 11:03:17 -04005463 phba->sli4_hba.next_rpi += rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04005464 spin_unlock_irq(&phba->hbalock);
5465 return rpi_hdr;
5466
5467 err_free_coherent:
5468 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
5469 dmabuf->virt, dmabuf->phys);
5470 err_free_dmabuf:
5471 kfree(dmabuf);
5472 return NULL;
5473}
5474
5475/**
5476 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
5477 * @phba: pointer to lpfc hba data structure.
5478 *
5479 * This routine is invoked to remove all memory resources allocated
James Smart6d368e52011-05-24 11:44:12 -04005480 * to support rpis for SLI4 ports not supporting extents. This routine
5481 * presumes the caller has released all rpis consumed by fabric or port
5482 * logins and is prepared to have the header pages removed.
James Smartda0436e2009-05-22 14:51:39 -04005483 **/
5484void
5485lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
5486{
5487 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
5488
James Smart6d368e52011-05-24 11:44:12 -04005489 if (!phba->sli4_hba.rpi_hdrs_in_use)
5490 goto exit;
5491
James Smartda0436e2009-05-22 14:51:39 -04005492 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
5493 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
5494 list_del(&rpi_hdr->list);
5495 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
5496 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
5497 kfree(rpi_hdr->dmabuf);
5498 kfree(rpi_hdr);
5499 }
James Smart6d368e52011-05-24 11:44:12 -04005500 exit:
5501 /* There are no rpis available to the port now. */
5502 phba->sli4_hba.next_rpi = 0;
James Smartda0436e2009-05-22 14:51:39 -04005503}
5504
5505/**
James Smart3772a992009-05-22 14:50:54 -04005506 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
5507 * @pdev: pointer to pci device data structure.
5508 *
5509 * This routine is invoked to allocate the driver hba data structure for an
5510 * HBA device. If the allocation is successful, the phba reference to the
5511 * PCI device data structure is set.
5512 *
5513 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005514 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04005515 * NULL - error
5516 **/
5517static struct lpfc_hba *
5518lpfc_hba_alloc(struct pci_dev *pdev)
5519{
5520 struct lpfc_hba *phba;
5521
5522 /* Allocate memory for HBA structure */
5523 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
5524 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02005525 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04005526 return NULL;
5527 }
5528
5529 /* Set reference to PCI device in HBA structure */
5530 phba->pcidev = pdev;
5531
5532 /* Assign an unused board number */
5533 phba->brd_no = lpfc_get_instance();
5534 if (phba->brd_no < 0) {
5535 kfree(phba);
5536 return NULL;
5537 }
5538
James Smart4fede782010-01-26 23:08:55 -05005539 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04005540 INIT_LIST_HEAD(&phba->ct_ev_waiters);
5541
James Smart3772a992009-05-22 14:50:54 -04005542 return phba;
5543}
5544
5545/**
5546 * lpfc_hba_free - Free driver hba data structure with a device.
5547 * @phba: pointer to lpfc hba data structure.
5548 *
5549 * This routine is invoked to free the driver hba data structure with an
5550 * HBA device.
5551 **/
5552static void
5553lpfc_hba_free(struct lpfc_hba *phba)
5554{
5555 /* Release the driver assigned board number */
5556 idr_remove(&lpfc_hba_index, phba->brd_no);
5557
5558 kfree(phba);
5559 return;
5560}
5561
5562/**
5563 * lpfc_create_shost - Create hba physical port with associated scsi host.
5564 * @phba: pointer to lpfc hba data structure.
5565 *
5566 * This routine is invoked to create HBA physical port and associate a SCSI
5567 * host with it.
5568 *
5569 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005570 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005571 * other values - error
5572 **/
5573static int
5574lpfc_create_shost(struct lpfc_hba *phba)
5575{
5576 struct lpfc_vport *vport;
5577 struct Scsi_Host *shost;
5578
5579 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05005580 phba->fc_edtov = FF_DEF_EDTOV;
5581 phba->fc_ratov = FF_DEF_RATOV;
5582 phba->fc_altov = FF_DEF_ALTOV;
5583 phba->fc_arbtov = FF_DEF_ARBTOV;
5584
James Smartd7c47992010-06-08 18:31:54 -04005585 atomic_set(&phba->sdev_cnt, 0);
James Smart3de2a652007-08-02 11:09:59 -04005586 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05005587 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04005588 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05005589
5590 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05005591 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05005592 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04005593 /* Put reference to SCSI host to driver's device private data */
5594 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05005595
James Smart3772a992009-05-22 14:50:54 -04005596 return 0;
5597}
dea31012005-04-17 16:05:31 -05005598
James Smart3772a992009-05-22 14:50:54 -04005599/**
5600 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
5601 * @phba: pointer to lpfc hba data structure.
5602 *
5603 * This routine is invoked to destroy HBA physical port and the associated
5604 * SCSI host.
5605 **/
5606static void
5607lpfc_destroy_shost(struct lpfc_hba *phba)
5608{
5609 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04005610
James Smart3772a992009-05-22 14:50:54 -04005611 /* Destroy physical port that associated with the SCSI host */
5612 destroy_port(vport);
5613
5614 return;
5615}
5616
5617/**
5618 * lpfc_setup_bg - Setup Block guard structures and debug areas.
5619 * @phba: pointer to lpfc hba data structure.
5620 * @shost: the shost to be used to detect Block guard settings.
5621 *
5622 * This routine sets up the local Block guard protocol settings for @shost.
5623 * This routine also allocates memory for debugging bg buffers.
5624 **/
5625static void
5626lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
5627{
James Smartbbeb79b2012-06-12 13:54:27 -04005628 uint32_t old_mask;
5629 uint32_t old_guard;
5630
James Smart3772a992009-05-22 14:50:54 -04005631 int pagecnt = 10;
5632 if (lpfc_prot_mask && lpfc_prot_guard) {
5633 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5634 "1478 Registering BlockGuard with the "
5635 "SCSI layer\n");
James Smartbbeb79b2012-06-12 13:54:27 -04005636
5637 old_mask = lpfc_prot_mask;
5638 old_guard = lpfc_prot_guard;
5639
5640 /* Only allow supported values */
5641 lpfc_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
5642 SHOST_DIX_TYPE0_PROTECTION |
5643 SHOST_DIX_TYPE1_PROTECTION);
5644 lpfc_prot_guard &= (SHOST_DIX_GUARD_IP | SHOST_DIX_GUARD_CRC);
5645
5646 /* DIF Type 1 protection for profiles AST1/C1 is end to end */
5647 if (lpfc_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
5648 lpfc_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
5649
5650 if (lpfc_prot_mask && lpfc_prot_guard) {
5651 if ((old_mask != lpfc_prot_mask) ||
5652 (old_guard != lpfc_prot_guard))
5653 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5654 "1475 Registering BlockGuard with the "
5655 "SCSI layer: mask %d guard %d\n",
5656 lpfc_prot_mask, lpfc_prot_guard);
5657
5658 scsi_host_set_prot(shost, lpfc_prot_mask);
5659 scsi_host_set_guard(shost, lpfc_prot_guard);
5660 } else
5661 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5662 "1479 Not Registering BlockGuard with the SCSI "
5663 "layer, Bad protection parameters: %d %d\n",
5664 old_mask, old_guard);
James Smart98c9ea52007-10-27 13:37:33 -04005665 }
James Smartbbeb79b2012-06-12 13:54:27 -04005666
James Smart81301a92008-12-04 22:39:46 -05005667 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05005668 while (pagecnt) {
5669 spin_lock_init(&_dump_buf_lock);
5670 _dump_buf_data =
5671 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
5672 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04005673 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5674 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04005675 "_dump_buf_data at 0x%p\n",
5676 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05005677 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04005678 memset(_dump_buf_data, 0,
5679 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05005680 break;
James Smart3772a992009-05-22 14:50:54 -04005681 } else
James Smart81301a92008-12-04 22:39:46 -05005682 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05005683 }
James Smart81301a92008-12-04 22:39:46 -05005684 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04005685 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5686 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04005687 "memory for hexdump\n");
5688 } else
James Smart6a9c52c2009-10-02 15:16:51 -04005689 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5690 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05005691 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05005692 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05005693 while (pagecnt) {
5694 _dump_buf_dif =
5695 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
5696 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04005697 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5698 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04005699 "_dump_buf_dif at 0x%p\n",
5700 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05005701 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04005702 memset(_dump_buf_dif, 0,
5703 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05005704 break;
James Smart3772a992009-05-22 14:50:54 -04005705 } else
James Smart81301a92008-12-04 22:39:46 -05005706 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05005707 }
James Smart81301a92008-12-04 22:39:46 -05005708 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04005709 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5710 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04005711 "memory for hexdump\n");
5712 } else
James Smart6a9c52c2009-10-02 15:16:51 -04005713 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5714 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04005715 _dump_buf_dif);
5716}
5717
5718/**
5719 * lpfc_post_init_setup - Perform necessary device post initialization setup.
5720 * @phba: pointer to lpfc hba data structure.
5721 *
5722 * This routine is invoked to perform all the necessary post initialization
5723 * setup for the device.
5724 **/
5725static void
5726lpfc_post_init_setup(struct lpfc_hba *phba)
5727{
5728 struct Scsi_Host *shost;
5729 struct lpfc_adapter_event_header adapter_event;
5730
5731 /* Get the default values for Model Name and Description */
5732 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
5733
5734 /*
5735 * hba setup may have changed the hba_queue_depth so we need to
5736 * adjust the value of can_queue.
5737 */
5738 shost = pci_get_drvdata(phba->pcidev);
5739 shost->can_queue = phba->cfg_hba_queue_depth - 10;
5740 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
5741 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05005742
5743 lpfc_host_attrib_init(shost);
5744
James Smart2e0fef82007-06-17 19:56:36 -05005745 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
5746 spin_lock_irq(shost->host_lock);
5747 lpfc_poll_start_timer(phba);
5748 spin_unlock_irq(shost->host_lock);
5749 }
James Smart8f6d98d2006-08-01 07:34:00 -04005750
James Smart93996272008-08-24 21:50:30 -04005751 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5752 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04005753 /* Send board arrival event to upper layer */
5754 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
5755 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
5756 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04005757 sizeof(adapter_event),
5758 (char *) &adapter_event,
5759 LPFC_NL_VENDOR_ID);
5760 return;
5761}
5762
5763/**
5764 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
5765 * @phba: pointer to lpfc hba data structure.
5766 *
5767 * This routine is invoked to set up the PCI device memory space for device
5768 * with SLI-3 interface spec.
5769 *
5770 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005771 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005772 * other values - error
5773 **/
5774static int
5775lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
5776{
5777 struct pci_dev *pdev;
5778 unsigned long bar0map_len, bar2map_len;
5779 int i, hbq_count;
5780 void *ptr;
5781 int error = -ENODEV;
5782
5783 /* Obtain PCI device reference */
5784 if (!phba->pcidev)
5785 return error;
5786 else
5787 pdev = phba->pcidev;
5788
5789 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05005790 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
5791 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
5792 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
5793 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smart3772a992009-05-22 14:50:54 -04005794 return error;
Michael Reed8e685972009-09-18 12:02:05 -05005795 }
5796 }
James Smart3772a992009-05-22 14:50:54 -04005797
5798 /* Get the bus address of Bar0 and Bar2 and the number of bytes
5799 * required by each mapping.
5800 */
5801 phba->pci_bar0_map = pci_resource_start(pdev, 0);
5802 bar0map_len = pci_resource_len(pdev, 0);
5803
5804 phba->pci_bar2_map = pci_resource_start(pdev, 2);
5805 bar2map_len = pci_resource_len(pdev, 2);
5806
5807 /* Map HBA SLIM to a kernel virtual address. */
5808 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
5809 if (!phba->slim_memmap_p) {
5810 dev_printk(KERN_ERR, &pdev->dev,
5811 "ioremap failed for SLIM memory.\n");
5812 goto out;
5813 }
5814
5815 /* Map HBA Control Registers to a kernel virtual address. */
5816 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
5817 if (!phba->ctrl_regs_memmap_p) {
5818 dev_printk(KERN_ERR, &pdev->dev,
5819 "ioremap failed for HBA control registers.\n");
5820 goto out_iounmap_slim;
5821 }
5822
5823 /* Allocate memory for SLI-2 structures */
5824 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
5825 SLI2_SLIM_SIZE,
5826 &phba->slim2p.phys,
5827 GFP_KERNEL);
5828 if (!phba->slim2p.virt)
5829 goto out_iounmap;
5830
5831 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
5832 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04005833 phba->mbox_ext = (phba->slim2p.virt +
5834 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04005835 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
5836 phba->IOCBs = (phba->slim2p.virt +
5837 offsetof(struct lpfc_sli2_slim, IOCBs));
5838
5839 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
5840 lpfc_sli_hbq_size(),
5841 &phba->hbqslimp.phys,
5842 GFP_KERNEL);
5843 if (!phba->hbqslimp.virt)
5844 goto out_free_slim;
5845
5846 hbq_count = lpfc_sli_hbq_count();
5847 ptr = phba->hbqslimp.virt;
5848 for (i = 0; i < hbq_count; ++i) {
5849 phba->hbqs[i].hbq_virt = ptr;
5850 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
5851 ptr += (lpfc_hbq_defs[i]->entry_count *
5852 sizeof(struct lpfc_hbq_entry));
5853 }
5854 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
5855 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
5856
5857 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
5858
5859 INIT_LIST_HEAD(&phba->rb_pend_list);
5860
5861 phba->MBslimaddr = phba->slim_memmap_p;
5862 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
5863 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
5864 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
5865 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04005866
dea31012005-04-17 16:05:31 -05005867 return 0;
5868
dea31012005-04-17 16:05:31 -05005869out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04005870 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
5871 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05005872out_iounmap:
5873 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005874out_iounmap_slim:
dea31012005-04-17 16:05:31 -05005875 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05005876out:
5877 return error;
5878}
5879
James Smarte59058c2008-08-24 21:49:00 -04005880/**
James Smart3772a992009-05-22 14:50:54 -04005881 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
5882 * @phba: pointer to lpfc hba data structure.
5883 *
5884 * This routine is invoked to unset the PCI device memory space for device
5885 * with SLI-3 interface spec.
5886 **/
5887static void
5888lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
5889{
5890 struct pci_dev *pdev;
5891
5892 /* Obtain PCI device reference */
5893 if (!phba->pcidev)
5894 return;
5895 else
5896 pdev = phba->pcidev;
5897
5898 /* Free coherent DMA memory allocated */
5899 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
5900 phba->hbqslimp.virt, phba->hbqslimp.phys);
5901 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
5902 phba->slim2p.virt, phba->slim2p.phys);
5903
5904 /* I/O memory unmap */
5905 iounmap(phba->ctrl_regs_memmap_p);
5906 iounmap(phba->slim_memmap_p);
5907
5908 return;
5909}
5910
5911/**
James Smartda0436e2009-05-22 14:51:39 -04005912 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
5913 * @phba: pointer to lpfc hba data structure.
5914 *
5915 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
5916 * done and check status.
5917 *
5918 * Return 0 if successful, otherwise -ENODEV.
5919 **/
5920int
5921lpfc_sli4_post_status_check(struct lpfc_hba *phba)
5922{
James Smart2fcee4b2010-12-15 17:57:46 -05005923 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
5924 struct lpfc_register reg_data;
5925 int i, port_error = 0;
5926 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04005927
James Smart9940b972011-03-11 16:06:12 -05005928 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
5929 memset(&reg_data, 0, sizeof(reg_data));
James Smart2fcee4b2010-12-15 17:57:46 -05005930 if (!phba->sli4_hba.PSMPHRregaddr)
James Smartda0436e2009-05-22 14:51:39 -04005931 return -ENODEV;
5932
James Smartda0436e2009-05-22 14:51:39 -04005933 /* Wait up to 30 seconds for the SLI Port POST done and ready */
5934 for (i = 0; i < 3000; i++) {
James Smart9940b972011-03-11 16:06:12 -05005935 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
5936 &portsmphr_reg.word0) ||
5937 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
James Smart2fcee4b2010-12-15 17:57:46 -05005938 /* Port has a fatal POST error, break out */
James Smartda0436e2009-05-22 14:51:39 -04005939 port_error = -ENODEV;
5940 break;
5941 }
James Smart2fcee4b2010-12-15 17:57:46 -05005942 if (LPFC_POST_STAGE_PORT_READY ==
5943 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
James Smartda0436e2009-05-22 14:51:39 -04005944 break;
James Smartda0436e2009-05-22 14:51:39 -04005945 msleep(10);
5946 }
5947
James Smart2fcee4b2010-12-15 17:57:46 -05005948 /*
5949 * If there was a port error during POST, then don't proceed with
5950 * other register reads as the data may not be valid. Just exit.
5951 */
5952 if (port_error) {
James Smartda0436e2009-05-22 14:51:39 -04005953 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05005954 "1408 Port Failed POST - portsmphr=0x%x, "
5955 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
5956 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
5957 portsmphr_reg.word0,
5958 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
5959 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
5960 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
5961 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
5962 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
5963 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
5964 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
5965 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
5966 } else {
James Smart28baac72010-02-12 14:42:03 -05005967 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05005968 "2534 Device Info: SLIFamily=0x%x, "
5969 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
5970 "SLIHint_2=0x%x, FT=0x%x\n",
James Smart28baac72010-02-12 14:42:03 -05005971 bf_get(lpfc_sli_intf_sli_family,
5972 &phba->sli4_hba.sli_intf),
5973 bf_get(lpfc_sli_intf_slirev,
5974 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05005975 bf_get(lpfc_sli_intf_if_type,
James Smart28baac72010-02-12 14:42:03 -05005976 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05005977 bf_get(lpfc_sli_intf_sli_hint1,
5978 &phba->sli4_hba.sli_intf),
5979 bf_get(lpfc_sli_intf_sli_hint2,
5980 &phba->sli4_hba.sli_intf),
5981 bf_get(lpfc_sli_intf_func_type,
James Smart28baac72010-02-12 14:42:03 -05005982 &phba->sli4_hba.sli_intf));
James Smart2fcee4b2010-12-15 17:57:46 -05005983 /*
5984 * Check for other Port errors during the initialization
5985 * process. Fail the load if the port did not come up
5986 * correctly.
5987 */
5988 if_type = bf_get(lpfc_sli_intf_if_type,
5989 &phba->sli4_hba.sli_intf);
5990 switch (if_type) {
5991 case LPFC_SLI_INTF_IF_TYPE_0:
5992 phba->sli4_hba.ue_mask_lo =
5993 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
5994 phba->sli4_hba.ue_mask_hi =
5995 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
5996 uerrlo_reg.word0 =
5997 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
5998 uerrhi_reg.word0 =
5999 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
6000 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
6001 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
6002 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6003 "1422 Unrecoverable Error "
6004 "Detected during POST "
6005 "uerr_lo_reg=0x%x, "
6006 "uerr_hi_reg=0x%x, "
6007 "ue_mask_lo_reg=0x%x, "
6008 "ue_mask_hi_reg=0x%x\n",
6009 uerrlo_reg.word0,
6010 uerrhi_reg.word0,
6011 phba->sli4_hba.ue_mask_lo,
6012 phba->sli4_hba.ue_mask_hi);
6013 port_error = -ENODEV;
6014 }
6015 break;
6016 case LPFC_SLI_INTF_IF_TYPE_2:
6017 /* Final checks. The port status should be clean. */
James Smart9940b972011-03-11 16:06:12 -05006018 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
6019 &reg_data.word0) ||
James Smart05580562011-05-24 11:40:48 -04006020 (bf_get(lpfc_sliport_status_err, &reg_data) &&
6021 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
James Smart2fcee4b2010-12-15 17:57:46 -05006022 phba->work_status[0] =
6023 readl(phba->sli4_hba.u.if_type2.
6024 ERR1regaddr);
6025 phba->work_status[1] =
6026 readl(phba->sli4_hba.u.if_type2.
6027 ERR2regaddr);
6028 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05006029 "2888 Unrecoverable port error "
6030 "following POST: port status reg "
6031 "0x%x, port_smphr reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05006032 "error 1=0x%x, error 2=0x%x\n",
6033 reg_data.word0,
6034 portsmphr_reg.word0,
6035 phba->work_status[0],
6036 phba->work_status[1]);
6037 port_error = -ENODEV;
6038 }
6039 break;
6040 case LPFC_SLI_INTF_IF_TYPE_1:
6041 default:
6042 break;
6043 }
James Smart28baac72010-02-12 14:42:03 -05006044 }
James Smartda0436e2009-05-22 14:51:39 -04006045 return port_error;
6046}
6047
6048/**
6049 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
6050 * @phba: pointer to lpfc hba data structure.
James Smart2fcee4b2010-12-15 17:57:46 -05006051 * @if_type: The SLI4 interface type getting configured.
James Smartda0436e2009-05-22 14:51:39 -04006052 *
6053 * This routine is invoked to set up SLI4 BAR0 PCI config space register
6054 * memory map.
6055 **/
6056static void
James Smart2fcee4b2010-12-15 17:57:46 -05006057lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04006058{
James Smart2fcee4b2010-12-15 17:57:46 -05006059 switch (if_type) {
6060 case LPFC_SLI_INTF_IF_TYPE_0:
6061 phba->sli4_hba.u.if_type0.UERRLOregaddr =
6062 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
6063 phba->sli4_hba.u.if_type0.UERRHIregaddr =
6064 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
6065 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
6066 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
6067 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
6068 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
6069 phba->sli4_hba.SLIINTFregaddr =
6070 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
6071 break;
6072 case LPFC_SLI_INTF_IF_TYPE_2:
6073 phba->sli4_hba.u.if_type2.ERR1regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006074 phba->sli4_hba.conf_regs_memmap_p +
6075 LPFC_CTL_PORT_ER1_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006076 phba->sli4_hba.u.if_type2.ERR2regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006077 phba->sli4_hba.conf_regs_memmap_p +
6078 LPFC_CTL_PORT_ER2_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006079 phba->sli4_hba.u.if_type2.CTRLregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006080 phba->sli4_hba.conf_regs_memmap_p +
6081 LPFC_CTL_PORT_CTL_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006082 phba->sli4_hba.u.if_type2.STATUSregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006083 phba->sli4_hba.conf_regs_memmap_p +
6084 LPFC_CTL_PORT_STA_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006085 phba->sli4_hba.SLIINTFregaddr =
6086 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
6087 phba->sli4_hba.PSMPHRregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006088 phba->sli4_hba.conf_regs_memmap_p +
6089 LPFC_CTL_PORT_SEM_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006090 phba->sli4_hba.RQDBregaddr =
6091 phba->sli4_hba.conf_regs_memmap_p + LPFC_RQ_DOORBELL;
6092 phba->sli4_hba.WQDBregaddr =
6093 phba->sli4_hba.conf_regs_memmap_p + LPFC_WQ_DOORBELL;
6094 phba->sli4_hba.EQCQDBregaddr =
6095 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
6096 phba->sli4_hba.MQDBregaddr =
6097 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
6098 phba->sli4_hba.BMBXregaddr =
6099 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
6100 break;
6101 case LPFC_SLI_INTF_IF_TYPE_1:
6102 default:
6103 dev_printk(KERN_ERR, &phba->pcidev->dev,
6104 "FATAL - unsupported SLI4 interface type - %d\n",
6105 if_type);
6106 break;
6107 }
James Smartda0436e2009-05-22 14:51:39 -04006108}
6109
6110/**
6111 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
6112 * @phba: pointer to lpfc hba data structure.
6113 *
6114 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
6115 * memory map.
6116 **/
6117static void
6118lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
6119{
James Smart2fcee4b2010-12-15 17:57:46 -05006120 phba->sli4_hba.PSMPHRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
6121 LPFC_SLIPORT_IF0_SMPHR;
James Smartda0436e2009-05-22 14:51:39 -04006122 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006123 LPFC_HST_ISR0;
James Smartda0436e2009-05-22 14:51:39 -04006124 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006125 LPFC_HST_IMR0;
James Smartda0436e2009-05-22 14:51:39 -04006126 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006127 LPFC_HST_ISCR0;
James Smartda0436e2009-05-22 14:51:39 -04006128}
6129
6130/**
6131 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
6132 * @phba: pointer to lpfc hba data structure.
6133 * @vf: virtual function number
6134 *
6135 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
6136 * based on the given viftual function number, @vf.
6137 *
6138 * Return 0 if successful, otherwise -ENODEV.
6139 **/
6140static int
6141lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
6142{
6143 if (vf > LPFC_VIR_FUNC_MAX)
6144 return -ENODEV;
6145
6146 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6147 vf * LPFC_VFR_PAGE_SIZE + LPFC_RQ_DOORBELL);
6148 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6149 vf * LPFC_VFR_PAGE_SIZE + LPFC_WQ_DOORBELL);
6150 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6151 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
6152 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6153 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
6154 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6155 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
6156 return 0;
6157}
6158
6159/**
6160 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
6161 * @phba: pointer to lpfc hba data structure.
6162 *
6163 * This routine is invoked to create the bootstrap mailbox
6164 * region consistent with the SLI-4 interface spec. This
6165 * routine allocates all memory necessary to communicate
6166 * mailbox commands to the port and sets up all alignment
6167 * needs. No locks are expected to be held when calling
6168 * this routine.
6169 *
6170 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006171 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04006172 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04006173 **/
6174static int
6175lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
6176{
6177 uint32_t bmbx_size;
6178 struct lpfc_dmabuf *dmabuf;
6179 struct dma_address *dma_address;
6180 uint32_t pa_addr;
6181 uint64_t phys_addr;
6182
6183 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
6184 if (!dmabuf)
6185 return -ENOMEM;
6186
6187 /*
6188 * The bootstrap mailbox region is comprised of 2 parts
6189 * plus an alignment restriction of 16 bytes.
6190 */
6191 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
6192 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
6193 bmbx_size,
6194 &dmabuf->phys,
6195 GFP_KERNEL);
6196 if (!dmabuf->virt) {
6197 kfree(dmabuf);
6198 return -ENOMEM;
6199 }
6200 memset(dmabuf->virt, 0, bmbx_size);
6201
6202 /*
6203 * Initialize the bootstrap mailbox pointers now so that the register
6204 * operations are simple later. The mailbox dma address is required
6205 * to be 16-byte aligned. Also align the virtual memory as each
6206 * maibox is copied into the bmbx mailbox region before issuing the
6207 * command to the port.
6208 */
6209 phba->sli4_hba.bmbx.dmabuf = dmabuf;
6210 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
6211
6212 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
6213 LPFC_ALIGN_16_BYTE);
6214 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
6215 LPFC_ALIGN_16_BYTE);
6216
6217 /*
6218 * Set the high and low physical addresses now. The SLI4 alignment
6219 * requirement is 16 bytes and the mailbox is posted to the port
6220 * as two 30-bit addresses. The other data is a bit marking whether
6221 * the 30-bit address is the high or low address.
6222 * Upcast bmbx aphys to 64bits so shift instruction compiles
6223 * clean on 32 bit machines.
6224 */
6225 dma_address = &phba->sli4_hba.bmbx.dma_address;
6226 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
6227 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
6228 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
6229 LPFC_BMBX_BIT1_ADDR_HI);
6230
6231 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
6232 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
6233 LPFC_BMBX_BIT1_ADDR_LO);
6234 return 0;
6235}
6236
6237/**
6238 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
6239 * @phba: pointer to lpfc hba data structure.
6240 *
6241 * This routine is invoked to teardown the bootstrap mailbox
6242 * region and release all host resources. This routine requires
6243 * the caller to ensure all mailbox commands recovered, no
6244 * additional mailbox comands are sent, and interrupts are disabled
6245 * before calling this routine.
6246 *
6247 **/
6248static void
6249lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
6250{
6251 dma_free_coherent(&phba->pcidev->dev,
6252 phba->sli4_hba.bmbx.bmbx_size,
6253 phba->sli4_hba.bmbx.dmabuf->virt,
6254 phba->sli4_hba.bmbx.dmabuf->phys);
6255
6256 kfree(phba->sli4_hba.bmbx.dmabuf);
6257 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
6258}
6259
6260/**
6261 * lpfc_sli4_read_config - Get the config parameters.
6262 * @phba: pointer to lpfc hba data structure.
6263 *
6264 * This routine is invoked to read the configuration parameters from the HBA.
6265 * The configuration parameters are used to set the base and maximum values
6266 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
6267 * allocation for the port.
6268 *
6269 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006270 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006271 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006272 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006273 **/
James Smartff78d8f2011-12-13 13:21:35 -05006274int
James Smartda0436e2009-05-22 14:51:39 -04006275lpfc_sli4_read_config(struct lpfc_hba *phba)
6276{
6277 LPFC_MBOXQ_t *pmb;
6278 struct lpfc_mbx_read_config *rd_config;
James Smart912e3ac2011-05-24 11:42:11 -04006279 union lpfc_sli4_cfg_shdr *shdr;
6280 uint32_t shdr_status, shdr_add_status;
6281 struct lpfc_mbx_get_func_cfg *get_func_cfg;
6282 struct lpfc_rsrc_desc_fcfcoe *desc;
6283 uint32_t desc_count;
6284 int length, i, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04006285
6286 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6287 if (!pmb) {
6288 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6289 "2011 Unable to allocate memory for issuing "
6290 "SLI_CONFIG_SPECIAL mailbox command\n");
6291 return -ENOMEM;
6292 }
6293
6294 lpfc_read_config(phba, pmb);
6295
6296 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6297 if (rc != MBX_SUCCESS) {
6298 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6299 "2012 Mailbox failed , mbxCmd x%x "
6300 "READ_CONFIG, mbxStatus x%x\n",
6301 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6302 bf_get(lpfc_mqe_status, &pmb->u.mqe));
6303 rc = -EIO;
6304 } else {
6305 rd_config = &pmb->u.mqe.un.rd_config;
James Smartff78d8f2011-12-13 13:21:35 -05006306 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
6307 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
6308 phba->sli4_hba.lnk_info.lnk_tp =
6309 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
6310 phba->sli4_hba.lnk_info.lnk_no =
6311 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
6312 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6313 "3081 lnk_type:%d, lnk_numb:%d\n",
6314 phba->sli4_hba.lnk_info.lnk_tp,
6315 phba->sli4_hba.lnk_info.lnk_no);
6316 } else
6317 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6318 "3082 Mailbox (x%x) returned ldv:x0\n",
6319 bf_get(lpfc_mqe_command, &pmb->u.mqe));
James Smart6d368e52011-05-24 11:44:12 -04006320 phba->sli4_hba.extents_in_use =
6321 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04006322 phba->sli4_hba.max_cfg_param.max_xri =
6323 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
6324 phba->sli4_hba.max_cfg_param.xri_base =
6325 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
6326 phba->sli4_hba.max_cfg_param.max_vpi =
6327 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
6328 phba->sli4_hba.max_cfg_param.vpi_base =
6329 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
6330 phba->sli4_hba.max_cfg_param.max_rpi =
6331 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
6332 phba->sli4_hba.max_cfg_param.rpi_base =
6333 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
6334 phba->sli4_hba.max_cfg_param.max_vfi =
6335 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
6336 phba->sli4_hba.max_cfg_param.vfi_base =
6337 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
6338 phba->sli4_hba.max_cfg_param.max_fcfi =
6339 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04006340 phba->sli4_hba.max_cfg_param.max_eq =
6341 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
6342 phba->sli4_hba.max_cfg_param.max_rq =
6343 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
6344 phba->sli4_hba.max_cfg_param.max_wq =
6345 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
6346 phba->sli4_hba.max_cfg_param.max_cq =
6347 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
6348 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
6349 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
6350 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
6351 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
James Smart5ffc2662009-11-18 15:39:44 -05006352 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
6353 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04006354 phba->max_vports = phba->max_vpi;
6355 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart6d368e52011-05-24 11:44:12 -04006356 "2003 cfg params Extents? %d "
6357 "XRI(B:%d M:%d), "
James Smartda0436e2009-05-22 14:51:39 -04006358 "VPI(B:%d M:%d) "
6359 "VFI(B:%d M:%d) "
6360 "RPI(B:%d M:%d) "
James Smart6d368e52011-05-24 11:44:12 -04006361 "FCFI(Count:%d)\n",
6362 phba->sli4_hba.extents_in_use,
James Smartda0436e2009-05-22 14:51:39 -04006363 phba->sli4_hba.max_cfg_param.xri_base,
6364 phba->sli4_hba.max_cfg_param.max_xri,
6365 phba->sli4_hba.max_cfg_param.vpi_base,
6366 phba->sli4_hba.max_cfg_param.max_vpi,
6367 phba->sli4_hba.max_cfg_param.vfi_base,
6368 phba->sli4_hba.max_cfg_param.max_vfi,
6369 phba->sli4_hba.max_cfg_param.rpi_base,
6370 phba->sli4_hba.max_cfg_param.max_rpi,
James Smartda0436e2009-05-22 14:51:39 -04006371 phba->sli4_hba.max_cfg_param.max_fcfi);
6372 }
James Smart912e3ac2011-05-24 11:42:11 -04006373
6374 if (rc)
6375 goto read_cfg_out;
James Smartda0436e2009-05-22 14:51:39 -04006376
6377 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smartb92938b2010-06-07 15:24:12 -04006378 if (phba->cfg_hba_queue_depth >
6379 (phba->sli4_hba.max_cfg_param.max_xri -
6380 lpfc_sli4_get_els_iocb_cnt(phba)))
James Smartda0436e2009-05-22 14:51:39 -04006381 phba->cfg_hba_queue_depth =
James Smartb92938b2010-06-07 15:24:12 -04006382 phba->sli4_hba.max_cfg_param.max_xri -
6383 lpfc_sli4_get_els_iocb_cnt(phba);
James Smart912e3ac2011-05-24 11:42:11 -04006384
6385 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
6386 LPFC_SLI_INTF_IF_TYPE_2)
6387 goto read_cfg_out;
6388
6389 /* get the pf# and vf# for SLI4 if_type 2 port */
6390 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
6391 sizeof(struct lpfc_sli4_cfg_mhdr));
6392 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
6393 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
6394 length, LPFC_SLI4_MBX_EMBED);
6395
6396 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6397 shdr = (union lpfc_sli4_cfg_shdr *)
6398 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
6399 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6400 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6401 if (rc || shdr_status || shdr_add_status) {
6402 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6403 "3026 Mailbox failed , mbxCmd x%x "
6404 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
6405 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6406 bf_get(lpfc_mqe_status, &pmb->u.mqe));
6407 rc = -EIO;
6408 goto read_cfg_out;
6409 }
6410
6411 /* search for fc_fcoe resrouce descriptor */
6412 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
6413 desc_count = get_func_cfg->func_cfg.rsrc_desc_count;
6414
6415 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
6416 desc = (struct lpfc_rsrc_desc_fcfcoe *)
6417 &get_func_cfg->func_cfg.desc[i];
6418 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
6419 bf_get(lpfc_rsrc_desc_pcie_type, desc)) {
6420 phba->sli4_hba.iov.pf_number =
6421 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
6422 phba->sli4_hba.iov.vf_number =
6423 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
6424 break;
6425 }
6426 }
6427
6428 if (i < LPFC_RSRC_DESC_MAX_NUM)
6429 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6430 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
6431 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
6432 phba->sli4_hba.iov.vf_number);
6433 else {
6434 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6435 "3028 GET_FUNCTION_CONFIG: failed to find "
6436 "Resrouce Descriptor:x%x\n",
6437 LPFC_RSRC_DESC_TYPE_FCFCOE);
6438 rc = -EIO;
6439 }
6440
6441read_cfg_out:
6442 mempool_free(pmb, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04006443 return rc;
6444}
6445
6446/**
James Smart2fcee4b2010-12-15 17:57:46 -05006447 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
James Smartda0436e2009-05-22 14:51:39 -04006448 * @phba: pointer to lpfc hba data structure.
6449 *
James Smart2fcee4b2010-12-15 17:57:46 -05006450 * This routine is invoked to setup the port-side endian order when
6451 * the port if_type is 0. This routine has no function for other
6452 * if_types.
James Smartda0436e2009-05-22 14:51:39 -04006453 *
6454 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006455 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006456 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006457 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006458 **/
6459static int
6460lpfc_setup_endian_order(struct lpfc_hba *phba)
6461{
6462 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05006463 uint32_t if_type, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04006464 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
6465 HOST_ENDIAN_HIGH_WORD1};
6466
James Smart2fcee4b2010-12-15 17:57:46 -05006467 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
6468 switch (if_type) {
6469 case LPFC_SLI_INTF_IF_TYPE_0:
6470 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6471 GFP_KERNEL);
6472 if (!mboxq) {
6473 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6474 "0492 Unable to allocate memory for "
6475 "issuing SLI_CONFIG_SPECIAL mailbox "
6476 "command\n");
6477 return -ENOMEM;
6478 }
James Smartda0436e2009-05-22 14:51:39 -04006479
James Smart2fcee4b2010-12-15 17:57:46 -05006480 /*
6481 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
6482 * two words to contain special data values and no other data.
6483 */
6484 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
6485 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
6486 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6487 if (rc != MBX_SUCCESS) {
6488 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6489 "0493 SLI_CONFIG_SPECIAL mailbox "
6490 "failed with status x%x\n",
6491 rc);
6492 rc = -EIO;
6493 }
6494 mempool_free(mboxq, phba->mbox_mem_pool);
6495 break;
6496 case LPFC_SLI_INTF_IF_TYPE_2:
6497 case LPFC_SLI_INTF_IF_TYPE_1:
6498 default:
6499 break;
James Smartda0436e2009-05-22 14:51:39 -04006500 }
James Smartda0436e2009-05-22 14:51:39 -04006501 return rc;
6502}
6503
6504/**
James Smart5350d872011-10-10 21:33:49 -04006505 * lpfc_sli4_queue_verify - Verify and update EQ and CQ counts
James Smartda0436e2009-05-22 14:51:39 -04006506 * @phba: pointer to lpfc hba data structure.
6507 *
James Smart5350d872011-10-10 21:33:49 -04006508 * This routine is invoked to check the user settable queue counts for EQs and
6509 * CQs. after this routine is called the counts will be set to valid values that
6510 * adhere to the constraints of the system's interrupt vectors and the port's
6511 * queue resources.
James Smartda0436e2009-05-22 14:51:39 -04006512 *
6513 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006514 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006515 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04006516 **/
6517static int
James Smart5350d872011-10-10 21:33:49 -04006518lpfc_sli4_queue_verify(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04006519{
James Smartda0436e2009-05-22 14:51:39 -04006520 int cfg_fcp_wq_count;
6521 int cfg_fcp_eq_count;
6522
6523 /*
6524 * Sanity check for confiugred queue parameters against the run-time
6525 * device parameters
6526 */
6527
6528 /* Sanity check on FCP fast-path WQ parameters */
6529 cfg_fcp_wq_count = phba->cfg_fcp_wq_count;
6530 if (cfg_fcp_wq_count >
6531 (phba->sli4_hba.max_cfg_param.max_wq - LPFC_SP_WQN_DEF)) {
6532 cfg_fcp_wq_count = phba->sli4_hba.max_cfg_param.max_wq -
6533 LPFC_SP_WQN_DEF;
6534 if (cfg_fcp_wq_count < LPFC_FP_WQN_MIN) {
6535 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6536 "2581 Not enough WQs (%d) from "
6537 "the pci function for supporting "
6538 "FCP WQs (%d)\n",
6539 phba->sli4_hba.max_cfg_param.max_wq,
6540 phba->cfg_fcp_wq_count);
6541 goto out_error;
6542 }
6543 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6544 "2582 Not enough WQs (%d) from the pci "
6545 "function for supporting the requested "
6546 "FCP WQs (%d), the actual FCP WQs can "
6547 "be supported: %d\n",
6548 phba->sli4_hba.max_cfg_param.max_wq,
6549 phba->cfg_fcp_wq_count, cfg_fcp_wq_count);
6550 }
6551 /* The actual number of FCP work queues adopted */
6552 phba->cfg_fcp_wq_count = cfg_fcp_wq_count;
6553
6554 /* Sanity check on FCP fast-path EQ parameters */
6555 cfg_fcp_eq_count = phba->cfg_fcp_eq_count;
6556 if (cfg_fcp_eq_count >
6557 (phba->sli4_hba.max_cfg_param.max_eq - LPFC_SP_EQN_DEF)) {
6558 cfg_fcp_eq_count = phba->sli4_hba.max_cfg_param.max_eq -
6559 LPFC_SP_EQN_DEF;
6560 if (cfg_fcp_eq_count < LPFC_FP_EQN_MIN) {
6561 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6562 "2574 Not enough EQs (%d) from the "
6563 "pci function for supporting FCP "
6564 "EQs (%d)\n",
6565 phba->sli4_hba.max_cfg_param.max_eq,
6566 phba->cfg_fcp_eq_count);
6567 goto out_error;
6568 }
6569 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6570 "2575 Not enough EQs (%d) from the pci "
6571 "function for supporting the requested "
6572 "FCP EQs (%d), the actual FCP EQs can "
6573 "be supported: %d\n",
6574 phba->sli4_hba.max_cfg_param.max_eq,
6575 phba->cfg_fcp_eq_count, cfg_fcp_eq_count);
6576 }
6577 /* It does not make sense to have more EQs than WQs */
6578 if (cfg_fcp_eq_count > phba->cfg_fcp_wq_count) {
6579 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smart6a9c52c2009-10-02 15:16:51 -04006580 "2593 The FCP EQ count(%d) cannot be greater "
6581 "than the FCP WQ count(%d), limiting the "
6582 "FCP EQ count to %d\n", cfg_fcp_eq_count,
James Smartda0436e2009-05-22 14:51:39 -04006583 phba->cfg_fcp_wq_count,
6584 phba->cfg_fcp_wq_count);
6585 cfg_fcp_eq_count = phba->cfg_fcp_wq_count;
6586 }
6587 /* The actual number of FCP event queues adopted */
6588 phba->cfg_fcp_eq_count = cfg_fcp_eq_count;
6589 /* The overall number of event queues used */
6590 phba->sli4_hba.cfg_eqn = phba->cfg_fcp_eq_count + LPFC_SP_EQN_DEF;
6591
James Smartda0436e2009-05-22 14:51:39 -04006592 /* Get EQ depth from module parameter, fake the default for now */
6593 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
6594 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
6595
James Smart5350d872011-10-10 21:33:49 -04006596 /* Get CQ depth from module parameter, fake the default for now */
6597 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
6598 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
6599
6600 return 0;
6601out_error:
6602 return -ENOMEM;
6603}
6604
6605/**
6606 * lpfc_sli4_queue_create - Create all the SLI4 queues
6607 * @phba: pointer to lpfc hba data structure.
6608 *
6609 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
6610 * operation. For each SLI4 queue type, the parameters such as queue entry
6611 * count (queue depth) shall be taken from the module parameter. For now,
6612 * we just use some constant number as place holder.
6613 *
6614 * Return codes
6615 * 0 - sucessful
6616 * -ENOMEM - No availble memory
6617 * -EIO - The mailbox failed to complete successfully.
6618 **/
6619int
6620lpfc_sli4_queue_create(struct lpfc_hba *phba)
6621{
6622 struct lpfc_queue *qdesc;
6623 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
6624
6625 /*
6626 * Create Event Queues (EQs)
6627 */
6628
James Smartda0436e2009-05-22 14:51:39 -04006629 /* Create slow path event queue */
6630 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
6631 phba->sli4_hba.eq_ecount);
6632 if (!qdesc) {
6633 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6634 "0496 Failed allocate slow-path EQ\n");
6635 goto out_error;
6636 }
6637 phba->sli4_hba.sp_eq = qdesc;
6638
James Smart5350d872011-10-10 21:33:49 -04006639 /*
6640 * Create fast-path FCP Event Queue(s). The cfg_fcp_eq_count can be
6641 * zero whenever there is exactly one interrupt vector. This is not
6642 * an error.
6643 */
6644 if (phba->cfg_fcp_eq_count) {
6645 phba->sli4_hba.fp_eq = kzalloc((sizeof(struct lpfc_queue *) *
6646 phba->cfg_fcp_eq_count), GFP_KERNEL);
6647 if (!phba->sli4_hba.fp_eq) {
6648 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6649 "2576 Failed allocate memory for "
6650 "fast-path EQ record array\n");
6651 goto out_free_sp_eq;
6652 }
James Smartda0436e2009-05-22 14:51:39 -04006653 }
6654 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
6655 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
6656 phba->sli4_hba.eq_ecount);
6657 if (!qdesc) {
6658 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6659 "0497 Failed allocate fast-path EQ\n");
6660 goto out_free_fp_eq;
6661 }
6662 phba->sli4_hba.fp_eq[fcp_eqidx] = qdesc;
6663 }
6664
6665 /*
6666 * Create Complete Queues (CQs)
6667 */
6668
James Smartda0436e2009-05-22 14:51:39 -04006669 /* Create slow-path Mailbox Command Complete Queue */
6670 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6671 phba->sli4_hba.cq_ecount);
6672 if (!qdesc) {
6673 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6674 "0500 Failed allocate slow-path mailbox CQ\n");
6675 goto out_free_fp_eq;
6676 }
6677 phba->sli4_hba.mbx_cq = qdesc;
6678
6679 /* Create slow-path ELS Complete Queue */
6680 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6681 phba->sli4_hba.cq_ecount);
6682 if (!qdesc) {
6683 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6684 "0501 Failed allocate slow-path ELS CQ\n");
6685 goto out_free_mbx_cq;
6686 }
6687 phba->sli4_hba.els_cq = qdesc;
6688
James Smartda0436e2009-05-22 14:51:39 -04006689
James Smart5350d872011-10-10 21:33:49 -04006690 /*
6691 * Create fast-path FCP Completion Queue(s), one-to-one with FCP EQs.
6692 * If there are no FCP EQs then create exactly one FCP CQ.
6693 */
6694 if (phba->cfg_fcp_eq_count)
6695 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
6696 phba->cfg_fcp_eq_count),
6697 GFP_KERNEL);
6698 else
6699 phba->sli4_hba.fcp_cq = kzalloc(sizeof(struct lpfc_queue *),
6700 GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04006701 if (!phba->sli4_hba.fcp_cq) {
6702 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6703 "2577 Failed allocate memory for fast-path "
6704 "CQ record array\n");
James Smart4d9ab992009-10-02 15:16:39 -04006705 goto out_free_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04006706 }
James Smart5350d872011-10-10 21:33:49 -04006707 fcp_cqidx = 0;
6708 do {
James Smartda0436e2009-05-22 14:51:39 -04006709 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6710 phba->sli4_hba.cq_ecount);
6711 if (!qdesc) {
6712 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6713 "0499 Failed allocate fast-path FCP "
6714 "CQ (%d)\n", fcp_cqidx);
6715 goto out_free_fcp_cq;
6716 }
6717 phba->sli4_hba.fcp_cq[fcp_cqidx] = qdesc;
James Smart5350d872011-10-10 21:33:49 -04006718 } while (++fcp_cqidx < phba->cfg_fcp_eq_count);
James Smartda0436e2009-05-22 14:51:39 -04006719
6720 /* Create Mailbox Command Queue */
6721 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
6722 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
6723
6724 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
6725 phba->sli4_hba.mq_ecount);
6726 if (!qdesc) {
6727 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6728 "0505 Failed allocate slow-path MQ\n");
6729 goto out_free_fcp_cq;
6730 }
6731 phba->sli4_hba.mbx_wq = qdesc;
6732
6733 /*
6734 * Create all the Work Queues (WQs)
6735 */
6736 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
6737 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
6738
6739 /* Create slow-path ELS Work Queue */
6740 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
6741 phba->sli4_hba.wq_ecount);
6742 if (!qdesc) {
6743 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6744 "0504 Failed allocate slow-path ELS WQ\n");
6745 goto out_free_mbx_wq;
6746 }
6747 phba->sli4_hba.els_wq = qdesc;
6748
6749 /* Create fast-path FCP Work Queue(s) */
6750 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
6751 phba->cfg_fcp_wq_count), GFP_KERNEL);
6752 if (!phba->sli4_hba.fcp_wq) {
6753 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6754 "2578 Failed allocate memory for fast-path "
6755 "WQ record array\n");
6756 goto out_free_els_wq;
6757 }
6758 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
6759 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
6760 phba->sli4_hba.wq_ecount);
6761 if (!qdesc) {
6762 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6763 "0503 Failed allocate fast-path FCP "
6764 "WQ (%d)\n", fcp_wqidx);
6765 goto out_free_fcp_wq;
6766 }
6767 phba->sli4_hba.fcp_wq[fcp_wqidx] = qdesc;
6768 }
6769
6770 /*
6771 * Create Receive Queue (RQ)
6772 */
6773 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
6774 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
6775
6776 /* Create Receive Queue for header */
6777 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
6778 phba->sli4_hba.rq_ecount);
6779 if (!qdesc) {
6780 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6781 "0506 Failed allocate receive HRQ\n");
6782 goto out_free_fcp_wq;
6783 }
6784 phba->sli4_hba.hdr_rq = qdesc;
6785
6786 /* Create Receive Queue for data */
6787 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
6788 phba->sli4_hba.rq_ecount);
6789 if (!qdesc) {
6790 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6791 "0507 Failed allocate receive DRQ\n");
6792 goto out_free_hdr_rq;
6793 }
6794 phba->sli4_hba.dat_rq = qdesc;
6795
6796 return 0;
6797
6798out_free_hdr_rq:
6799 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
6800 phba->sli4_hba.hdr_rq = NULL;
6801out_free_fcp_wq:
6802 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--) {
6803 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_wqidx]);
6804 phba->sli4_hba.fcp_wq[fcp_wqidx] = NULL;
6805 }
6806 kfree(phba->sli4_hba.fcp_wq);
James Smart2e90f4b2011-12-13 13:22:37 -05006807 phba->sli4_hba.fcp_wq = NULL;
James Smartda0436e2009-05-22 14:51:39 -04006808out_free_els_wq:
6809 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
6810 phba->sli4_hba.els_wq = NULL;
6811out_free_mbx_wq:
6812 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
6813 phba->sli4_hba.mbx_wq = NULL;
6814out_free_fcp_cq:
6815 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--) {
6816 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_cqidx]);
6817 phba->sli4_hba.fcp_cq[fcp_cqidx] = NULL;
6818 }
6819 kfree(phba->sli4_hba.fcp_cq);
James Smart2e90f4b2011-12-13 13:22:37 -05006820 phba->sli4_hba.fcp_cq = NULL;
James Smartda0436e2009-05-22 14:51:39 -04006821out_free_els_cq:
6822 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
6823 phba->sli4_hba.els_cq = NULL;
6824out_free_mbx_cq:
6825 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
6826 phba->sli4_hba.mbx_cq = NULL;
6827out_free_fp_eq:
6828 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--) {
6829 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_eqidx]);
6830 phba->sli4_hba.fp_eq[fcp_eqidx] = NULL;
6831 }
6832 kfree(phba->sli4_hba.fp_eq);
James Smart2e90f4b2011-12-13 13:22:37 -05006833 phba->sli4_hba.fp_eq = NULL;
James Smartda0436e2009-05-22 14:51:39 -04006834out_free_sp_eq:
6835 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
6836 phba->sli4_hba.sp_eq = NULL;
6837out_error:
6838 return -ENOMEM;
6839}
6840
6841/**
6842 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
6843 * @phba: pointer to lpfc hba data structure.
6844 *
6845 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
6846 * operation.
6847 *
6848 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006849 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006850 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006851 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006852 **/
James Smart5350d872011-10-10 21:33:49 -04006853void
James Smartda0436e2009-05-22 14:51:39 -04006854lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
6855{
6856 int fcp_qidx;
6857
6858 /* Release mailbox command work queue */
6859 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
6860 phba->sli4_hba.mbx_wq = NULL;
6861
6862 /* Release ELS work queue */
6863 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
6864 phba->sli4_hba.els_wq = NULL;
6865
6866 /* Release FCP work queue */
James Smart2e90f4b2011-12-13 13:22:37 -05006867 if (phba->sli4_hba.fcp_wq != NULL)
6868 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count;
6869 fcp_qidx++)
6870 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_qidx]);
James Smartda0436e2009-05-22 14:51:39 -04006871 kfree(phba->sli4_hba.fcp_wq);
6872 phba->sli4_hba.fcp_wq = NULL;
6873
6874 /* Release unsolicited receive queue */
6875 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
6876 phba->sli4_hba.hdr_rq = NULL;
6877 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
6878 phba->sli4_hba.dat_rq = NULL;
6879
James Smartda0436e2009-05-22 14:51:39 -04006880 /* Release ELS complete queue */
6881 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
6882 phba->sli4_hba.els_cq = NULL;
6883
6884 /* Release mailbox command complete queue */
6885 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
6886 phba->sli4_hba.mbx_cq = NULL;
6887
6888 /* Release FCP response complete queue */
James Smart05580562011-05-24 11:40:48 -04006889 fcp_qidx = 0;
James Smart2e90f4b2011-12-13 13:22:37 -05006890 if (phba->sli4_hba.fcp_cq != NULL)
6891 do
6892 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_qidx]);
6893 while (++fcp_qidx < phba->cfg_fcp_eq_count);
James Smartda0436e2009-05-22 14:51:39 -04006894 kfree(phba->sli4_hba.fcp_cq);
6895 phba->sli4_hba.fcp_cq = NULL;
6896
6897 /* Release fast-path event queue */
James Smart2e90f4b2011-12-13 13:22:37 -05006898 if (phba->sli4_hba.fp_eq != NULL)
6899 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count;
6900 fcp_qidx++)
6901 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_qidx]);
James Smartda0436e2009-05-22 14:51:39 -04006902 kfree(phba->sli4_hba.fp_eq);
6903 phba->sli4_hba.fp_eq = NULL;
6904
6905 /* Release slow-path event queue */
6906 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
6907 phba->sli4_hba.sp_eq = NULL;
6908
6909 return;
6910}
6911
6912/**
6913 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
6914 * @phba: pointer to lpfc hba data structure.
6915 *
6916 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
6917 * operation.
6918 *
6919 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006920 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006921 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006922 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006923 **/
6924int
6925lpfc_sli4_queue_setup(struct lpfc_hba *phba)
6926{
6927 int rc = -ENOMEM;
6928 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
6929 int fcp_cq_index = 0;
6930
6931 /*
6932 * Set up Event Queues (EQs)
6933 */
6934
6935 /* Set up slow-path event queue */
6936 if (!phba->sli4_hba.sp_eq) {
6937 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6938 "0520 Slow-path EQ not allocated\n");
6939 goto out_error;
6940 }
6941 rc = lpfc_eq_create(phba, phba->sli4_hba.sp_eq,
6942 LPFC_SP_DEF_IMAX);
6943 if (rc) {
6944 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6945 "0521 Failed setup of slow-path EQ: "
6946 "rc = 0x%x\n", rc);
6947 goto out_error;
6948 }
6949 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6950 "2583 Slow-path EQ setup: queue-id=%d\n",
6951 phba->sli4_hba.sp_eq->queue_id);
6952
6953 /* Set up fast-path event queue */
James Smart1b511972011-12-13 13:23:09 -05006954 if (phba->cfg_fcp_eq_count && !phba->sli4_hba.fp_eq) {
James Smart2e90f4b2011-12-13 13:22:37 -05006955 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6956 "3147 Fast-path EQs not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05006957 rc = -ENOMEM;
James Smart2e90f4b2011-12-13 13:22:37 -05006958 goto out_destroy_sp_eq;
6959 }
James Smartda0436e2009-05-22 14:51:39 -04006960 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
6961 if (!phba->sli4_hba.fp_eq[fcp_eqidx]) {
6962 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6963 "0522 Fast-path EQ (%d) not "
6964 "allocated\n", fcp_eqidx);
James Smart1b511972011-12-13 13:23:09 -05006965 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04006966 goto out_destroy_fp_eq;
6967 }
6968 rc = lpfc_eq_create(phba, phba->sli4_hba.fp_eq[fcp_eqidx],
6969 phba->cfg_fcp_imax);
6970 if (rc) {
6971 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6972 "0523 Failed setup of fast-path EQ "
6973 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
6974 goto out_destroy_fp_eq;
6975 }
6976 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6977 "2584 Fast-path EQ setup: "
6978 "queue[%d]-id=%d\n", fcp_eqidx,
6979 phba->sli4_hba.fp_eq[fcp_eqidx]->queue_id);
6980 }
6981
6982 /*
6983 * Set up Complete Queues (CQs)
6984 */
6985
6986 /* Set up slow-path MBOX Complete Queue as the first CQ */
6987 if (!phba->sli4_hba.mbx_cq) {
6988 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6989 "0528 Mailbox CQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05006990 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04006991 goto out_destroy_fp_eq;
6992 }
6993 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq, phba->sli4_hba.sp_eq,
6994 LPFC_MCQ, LPFC_MBOX);
6995 if (rc) {
6996 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6997 "0529 Failed setup of slow-path mailbox CQ: "
6998 "rc = 0x%x\n", rc);
6999 goto out_destroy_fp_eq;
7000 }
7001 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7002 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
7003 phba->sli4_hba.mbx_cq->queue_id,
7004 phba->sli4_hba.sp_eq->queue_id);
7005
7006 /* Set up slow-path ELS Complete Queue */
7007 if (!phba->sli4_hba.els_cq) {
7008 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7009 "0530 ELS CQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007010 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04007011 goto out_destroy_mbx_cq;
7012 }
7013 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq, phba->sli4_hba.sp_eq,
7014 LPFC_WCQ, LPFC_ELS);
7015 if (rc) {
7016 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7017 "0531 Failed setup of slow-path ELS CQ: "
7018 "rc = 0x%x\n", rc);
7019 goto out_destroy_mbx_cq;
7020 }
7021 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7022 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
7023 phba->sli4_hba.els_cq->queue_id,
7024 phba->sli4_hba.sp_eq->queue_id);
7025
James Smartda0436e2009-05-22 14:51:39 -04007026 /* Set up fast-path FCP Response Complete Queue */
James Smart2e90f4b2011-12-13 13:22:37 -05007027 if (!phba->sli4_hba.fcp_cq) {
7028 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7029 "3148 Fast-path FCP CQ array not "
7030 "allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007031 rc = -ENOMEM;
James Smart2e90f4b2011-12-13 13:22:37 -05007032 goto out_destroy_els_cq;
7033 }
James Smart05580562011-05-24 11:40:48 -04007034 fcp_cqidx = 0;
7035 do {
James Smartda0436e2009-05-22 14:51:39 -04007036 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
7037 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7038 "0526 Fast-path FCP CQ (%d) not "
7039 "allocated\n", fcp_cqidx);
James Smart1b511972011-12-13 13:23:09 -05007040 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04007041 goto out_destroy_fcp_cq;
7042 }
James Smart05580562011-05-24 11:40:48 -04007043 if (phba->cfg_fcp_eq_count)
7044 rc = lpfc_cq_create(phba,
7045 phba->sli4_hba.fcp_cq[fcp_cqidx],
7046 phba->sli4_hba.fp_eq[fcp_cqidx],
7047 LPFC_WCQ, LPFC_FCP);
7048 else
7049 rc = lpfc_cq_create(phba,
7050 phba->sli4_hba.fcp_cq[fcp_cqidx],
7051 phba->sli4_hba.sp_eq,
7052 LPFC_WCQ, LPFC_FCP);
James Smartda0436e2009-05-22 14:51:39 -04007053 if (rc) {
7054 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7055 "0527 Failed setup of fast-path FCP "
7056 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
7057 goto out_destroy_fcp_cq;
7058 }
7059 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7060 "2588 FCP CQ setup: cq[%d]-id=%d, "
James Smart05580562011-05-24 11:40:48 -04007061 "parent %seq[%d]-id=%d\n",
James Smartda0436e2009-05-22 14:51:39 -04007062 fcp_cqidx,
7063 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
James Smart05580562011-05-24 11:40:48 -04007064 (phba->cfg_fcp_eq_count) ? "" : "sp_",
James Smartda0436e2009-05-22 14:51:39 -04007065 fcp_cqidx,
James Smart05580562011-05-24 11:40:48 -04007066 (phba->cfg_fcp_eq_count) ?
7067 phba->sli4_hba.fp_eq[fcp_cqidx]->queue_id :
7068 phba->sli4_hba.sp_eq->queue_id);
7069 } while (++fcp_cqidx < phba->cfg_fcp_eq_count);
James Smartda0436e2009-05-22 14:51:39 -04007070
7071 /*
7072 * Set up all the Work Queues (WQs)
7073 */
7074
7075 /* Set up Mailbox Command Queue */
7076 if (!phba->sli4_hba.mbx_wq) {
7077 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7078 "0538 Slow-path MQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007079 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04007080 goto out_destroy_fcp_cq;
7081 }
7082 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
7083 phba->sli4_hba.mbx_cq, LPFC_MBOX);
7084 if (rc) {
7085 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7086 "0539 Failed setup of slow-path MQ: "
7087 "rc = 0x%x\n", rc);
7088 goto out_destroy_fcp_cq;
7089 }
7090 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7091 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
7092 phba->sli4_hba.mbx_wq->queue_id,
7093 phba->sli4_hba.mbx_cq->queue_id);
7094
7095 /* Set up slow-path ELS Work Queue */
7096 if (!phba->sli4_hba.els_wq) {
7097 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7098 "0536 Slow-path ELS WQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007099 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04007100 goto out_destroy_mbx_wq;
7101 }
7102 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
7103 phba->sli4_hba.els_cq, LPFC_ELS);
7104 if (rc) {
7105 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7106 "0537 Failed setup of slow-path ELS WQ: "
7107 "rc = 0x%x\n", rc);
7108 goto out_destroy_mbx_wq;
7109 }
7110 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7111 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
7112 phba->sli4_hba.els_wq->queue_id,
7113 phba->sli4_hba.els_cq->queue_id);
7114
7115 /* Set up fast-path FCP Work Queue */
James Smart2e90f4b2011-12-13 13:22:37 -05007116 if (!phba->sli4_hba.fcp_wq) {
7117 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7118 "3149 Fast-path FCP WQ array not "
7119 "allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007120 rc = -ENOMEM;
James Smart2e90f4b2011-12-13 13:22:37 -05007121 goto out_destroy_els_wq;
7122 }
James Smartda0436e2009-05-22 14:51:39 -04007123 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
7124 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
7125 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7126 "0534 Fast-path FCP WQ (%d) not "
7127 "allocated\n", fcp_wqidx);
James Smart1b511972011-12-13 13:23:09 -05007128 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04007129 goto out_destroy_fcp_wq;
7130 }
7131 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
7132 phba->sli4_hba.fcp_cq[fcp_cq_index],
7133 LPFC_FCP);
7134 if (rc) {
7135 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7136 "0535 Failed setup of fast-path FCP "
7137 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
7138 goto out_destroy_fcp_wq;
7139 }
7140 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7141 "2591 FCP WQ setup: wq[%d]-id=%d, "
7142 "parent cq[%d]-id=%d\n",
7143 fcp_wqidx,
7144 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
7145 fcp_cq_index,
7146 phba->sli4_hba.fcp_cq[fcp_cq_index]->queue_id);
7147 /* Round robin FCP Work Queue's Completion Queue assignment */
James Smart05580562011-05-24 11:40:48 -04007148 if (phba->cfg_fcp_eq_count)
7149 fcp_cq_index = ((fcp_cq_index + 1) %
7150 phba->cfg_fcp_eq_count);
James Smartda0436e2009-05-22 14:51:39 -04007151 }
7152
7153 /*
7154 * Create Receive Queue (RQ)
7155 */
7156 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
7157 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7158 "0540 Receive Queue not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007159 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04007160 goto out_destroy_fcp_wq;
7161 }
James Smart73d91e52011-10-10 21:32:10 -04007162
7163 lpfc_rq_adjust_repost(phba, phba->sli4_hba.hdr_rq, LPFC_ELS_HBQ);
7164 lpfc_rq_adjust_repost(phba, phba->sli4_hba.dat_rq, LPFC_ELS_HBQ);
7165
James Smartda0436e2009-05-22 14:51:39 -04007166 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04007167 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04007168 if (rc) {
7169 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7170 "0541 Failed setup of Receive Queue: "
7171 "rc = 0x%x\n", rc);
7172 goto out_destroy_fcp_wq;
7173 }
James Smart73d91e52011-10-10 21:32:10 -04007174
James Smartda0436e2009-05-22 14:51:39 -04007175 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7176 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
7177 "parent cq-id=%d\n",
7178 phba->sli4_hba.hdr_rq->queue_id,
7179 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04007180 phba->sli4_hba.els_cq->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007181 return 0;
7182
7183out_destroy_fcp_wq:
7184 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
7185 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05007186out_destroy_els_wq:
James Smartda0436e2009-05-22 14:51:39 -04007187 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7188out_destroy_mbx_wq:
7189 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
7190out_destroy_fcp_cq:
7191 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
7192 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05007193out_destroy_els_cq:
James Smartda0436e2009-05-22 14:51:39 -04007194 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
7195out_destroy_mbx_cq:
7196 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
7197out_destroy_fp_eq:
7198 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
7199 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_eqidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05007200out_destroy_sp_eq:
James Smartda0436e2009-05-22 14:51:39 -04007201 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
7202out_error:
7203 return rc;
7204}
7205
7206/**
7207 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
7208 * @phba: pointer to lpfc hba data structure.
7209 *
7210 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
7211 * operation.
7212 *
7213 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007214 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007215 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007216 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007217 **/
7218void
7219lpfc_sli4_queue_unset(struct lpfc_hba *phba)
7220{
7221 int fcp_qidx;
7222
7223 /* Unset mailbox command work queue */
7224 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
7225 /* Unset ELS work queue */
7226 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7227 /* Unset unsolicited receive queue */
7228 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
7229 /* Unset FCP work queue */
7230 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
7231 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
7232 /* Unset mailbox command complete queue */
7233 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
7234 /* Unset ELS complete queue */
7235 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
James Smartda0436e2009-05-22 14:51:39 -04007236 /* Unset FCP response complete queue */
James Smart2e90f4b2011-12-13 13:22:37 -05007237 if (phba->sli4_hba.fcp_cq) {
7238 fcp_qidx = 0;
7239 do {
7240 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
7241 } while (++fcp_qidx < phba->cfg_fcp_eq_count);
7242 }
James Smartda0436e2009-05-22 14:51:39 -04007243 /* Unset fast-path event queue */
James Smart2e90f4b2011-12-13 13:22:37 -05007244 if (phba->sli4_hba.fp_eq) {
7245 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count;
7246 fcp_qidx++)
7247 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_qidx]);
7248 }
James Smartda0436e2009-05-22 14:51:39 -04007249 /* Unset slow-path event queue */
7250 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
7251}
7252
7253/**
7254 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
7255 * @phba: pointer to lpfc hba data structure.
7256 *
7257 * This routine is invoked to allocate and set up a pool of completion queue
7258 * events. The body of the completion queue event is a completion queue entry
7259 * CQE. For now, this pool is used for the interrupt service routine to queue
7260 * the following HBA completion queue events for the worker thread to process:
7261 * - Mailbox asynchronous events
7262 * - Receive queue completion unsolicited events
7263 * Later, this can be used for all the slow-path events.
7264 *
7265 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007266 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007267 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04007268 **/
7269static int
7270lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
7271{
7272 struct lpfc_cq_event *cq_event;
7273 int i;
7274
7275 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
7276 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
7277 if (!cq_event)
7278 goto out_pool_create_fail;
7279 list_add_tail(&cq_event->list,
7280 &phba->sli4_hba.sp_cqe_event_pool);
7281 }
7282 return 0;
7283
7284out_pool_create_fail:
7285 lpfc_sli4_cq_event_pool_destroy(phba);
7286 return -ENOMEM;
7287}
7288
7289/**
7290 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
7291 * @phba: pointer to lpfc hba data structure.
7292 *
7293 * This routine is invoked to free the pool of completion queue events at
7294 * driver unload time. Note that, it is the responsibility of the driver
7295 * cleanup routine to free all the outstanding completion-queue events
7296 * allocated from this pool back into the pool before invoking this routine
7297 * to destroy the pool.
7298 **/
7299static void
7300lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
7301{
7302 struct lpfc_cq_event *cq_event, *next_cq_event;
7303
7304 list_for_each_entry_safe(cq_event, next_cq_event,
7305 &phba->sli4_hba.sp_cqe_event_pool, list) {
7306 list_del(&cq_event->list);
7307 kfree(cq_event);
7308 }
7309}
7310
7311/**
7312 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
7313 * @phba: pointer to lpfc hba data structure.
7314 *
7315 * This routine is the lock free version of the API invoked to allocate a
7316 * completion-queue event from the free pool.
7317 *
7318 * Return: Pointer to the newly allocated completion-queue event if successful
7319 * NULL otherwise.
7320 **/
7321struct lpfc_cq_event *
7322__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
7323{
7324 struct lpfc_cq_event *cq_event = NULL;
7325
7326 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
7327 struct lpfc_cq_event, list);
7328 return cq_event;
7329}
7330
7331/**
7332 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
7333 * @phba: pointer to lpfc hba data structure.
7334 *
7335 * This routine is the lock version of the API invoked to allocate a
7336 * completion-queue event from the free pool.
7337 *
7338 * Return: Pointer to the newly allocated completion-queue event if successful
7339 * NULL otherwise.
7340 **/
7341struct lpfc_cq_event *
7342lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
7343{
7344 struct lpfc_cq_event *cq_event;
7345 unsigned long iflags;
7346
7347 spin_lock_irqsave(&phba->hbalock, iflags);
7348 cq_event = __lpfc_sli4_cq_event_alloc(phba);
7349 spin_unlock_irqrestore(&phba->hbalock, iflags);
7350 return cq_event;
7351}
7352
7353/**
7354 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
7355 * @phba: pointer to lpfc hba data structure.
7356 * @cq_event: pointer to the completion queue event to be freed.
7357 *
7358 * This routine is the lock free version of the API invoked to release a
7359 * completion-queue event back into the free pool.
7360 **/
7361void
7362__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
7363 struct lpfc_cq_event *cq_event)
7364{
7365 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
7366}
7367
7368/**
7369 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
7370 * @phba: pointer to lpfc hba data structure.
7371 * @cq_event: pointer to the completion queue event to be freed.
7372 *
7373 * This routine is the lock version of the API invoked to release a
7374 * completion-queue event back into the free pool.
7375 **/
7376void
7377lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
7378 struct lpfc_cq_event *cq_event)
7379{
7380 unsigned long iflags;
7381 spin_lock_irqsave(&phba->hbalock, iflags);
7382 __lpfc_sli4_cq_event_release(phba, cq_event);
7383 spin_unlock_irqrestore(&phba->hbalock, iflags);
7384}
7385
7386/**
7387 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
7388 * @phba: pointer to lpfc hba data structure.
7389 *
7390 * This routine is to free all the pending completion-queue events to the
7391 * back into the free pool for device reset.
7392 **/
7393static void
7394lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
7395{
7396 LIST_HEAD(cqelist);
7397 struct lpfc_cq_event *cqe;
7398 unsigned long iflags;
7399
7400 /* Retrieve all the pending WCQEs from pending WCQE lists */
7401 spin_lock_irqsave(&phba->hbalock, iflags);
7402 /* Pending FCP XRI abort events */
7403 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
7404 &cqelist);
7405 /* Pending ELS XRI abort events */
7406 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
7407 &cqelist);
7408 /* Pending asynnc events */
7409 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
7410 &cqelist);
7411 spin_unlock_irqrestore(&phba->hbalock, iflags);
7412
7413 while (!list_empty(&cqelist)) {
7414 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
7415 lpfc_sli4_cq_event_release(phba, cqe);
7416 }
7417}
7418
7419/**
7420 * lpfc_pci_function_reset - Reset pci function.
7421 * @phba: pointer to lpfc hba data structure.
7422 *
7423 * This routine is invoked to request a PCI function reset. It will destroys
7424 * all resources assigned to the PCI function which originates this request.
7425 *
7426 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007427 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007428 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007429 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007430 **/
7431int
7432lpfc_pci_function_reset(struct lpfc_hba *phba)
7433{
7434 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05007435 uint32_t rc = 0, if_type;
James Smartda0436e2009-05-22 14:51:39 -04007436 uint32_t shdr_status, shdr_add_status;
James Smart2fcee4b2010-12-15 17:57:46 -05007437 uint32_t rdy_chk, num_resets = 0, reset_again = 0;
James Smartda0436e2009-05-22 14:51:39 -04007438 union lpfc_sli4_cfg_shdr *shdr;
James Smart2fcee4b2010-12-15 17:57:46 -05007439 struct lpfc_register reg_data;
James Smart2b81f942012-03-01 22:37:18 -05007440 uint16_t devid;
James Smartda0436e2009-05-22 14:51:39 -04007441
James Smart2fcee4b2010-12-15 17:57:46 -05007442 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7443 switch (if_type) {
7444 case LPFC_SLI_INTF_IF_TYPE_0:
7445 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
7446 GFP_KERNEL);
7447 if (!mboxq) {
7448 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7449 "0494 Unable to allocate memory for "
7450 "issuing SLI_FUNCTION_RESET mailbox "
7451 "command\n");
7452 return -ENOMEM;
7453 }
7454
7455 /* Setup PCI function reset mailbox-ioctl command */
7456 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7457 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
7458 LPFC_SLI4_MBX_EMBED);
7459 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7460 shdr = (union lpfc_sli4_cfg_shdr *)
7461 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7462 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7463 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
7464 &shdr->response);
7465 if (rc != MBX_TIMEOUT)
7466 mempool_free(mboxq, phba->mbox_mem_pool);
7467 if (shdr_status || shdr_add_status || rc) {
7468 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7469 "0495 SLI_FUNCTION_RESET mailbox "
7470 "failed with status x%x add_status x%x,"
7471 " mbx status x%x\n",
7472 shdr_status, shdr_add_status, rc);
7473 rc = -ENXIO;
7474 }
7475 break;
7476 case LPFC_SLI_INTF_IF_TYPE_2:
7477 for (num_resets = 0;
7478 num_resets < MAX_IF_TYPE_2_RESETS;
7479 num_resets++) {
7480 reg_data.word0 = 0;
7481 bf_set(lpfc_sliport_ctrl_end, &reg_data,
7482 LPFC_SLIPORT_LITTLE_ENDIAN);
7483 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
7484 LPFC_SLIPORT_INIT_PORT);
7485 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
7486 CTRLregaddr);
James Smart8fcb8ac2012-03-01 22:35:58 -05007487 /* flush */
James Smart2b81f942012-03-01 22:37:18 -05007488 pci_read_config_word(phba->pcidev,
7489 PCI_DEVICE_ID, &devid);
James Smart2fcee4b2010-12-15 17:57:46 -05007490 /*
7491 * Poll the Port Status Register and wait for RDY for
7492 * up to 10 seconds. If the port doesn't respond, treat
7493 * it as an error. If the port responds with RN, start
7494 * the loop again.
7495 */
7496 for (rdy_chk = 0; rdy_chk < 1000; rdy_chk++) {
James Smart73d91e52011-10-10 21:32:10 -04007497 msleep(10);
James Smart9940b972011-03-11 16:06:12 -05007498 if (lpfc_readl(phba->sli4_hba.u.if_type2.
7499 STATUSregaddr, &reg_data.word0)) {
7500 rc = -ENODEV;
James Smart73d91e52011-10-10 21:32:10 -04007501 goto out;
James Smart9940b972011-03-11 16:06:12 -05007502 }
James Smart6b5151f2012-01-18 16:24:06 -05007503 if (bf_get(lpfc_sliport_status_rn, &reg_data))
7504 reset_again++;
James Smart2fcee4b2010-12-15 17:57:46 -05007505 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
7506 break;
James Smart2fcee4b2010-12-15 17:57:46 -05007507 }
7508
7509 /*
7510 * If the port responds to the init request with
7511 * reset needed, delay for a bit and restart the loop.
7512 */
James Smart6b5151f2012-01-18 16:24:06 -05007513 if (reset_again && (rdy_chk < 1000)) {
James Smart2fcee4b2010-12-15 17:57:46 -05007514 msleep(10);
7515 reset_again = 0;
7516 continue;
7517 }
7518
7519 /* Detect any port errors. */
James Smart2fcee4b2010-12-15 17:57:46 -05007520 if ((bf_get(lpfc_sliport_status_err, &reg_data)) ||
7521 (rdy_chk >= 1000)) {
7522 phba->work_status[0] = readl(
7523 phba->sli4_hba.u.if_type2.ERR1regaddr);
7524 phba->work_status[1] = readl(
7525 phba->sli4_hba.u.if_type2.ERR2regaddr);
7526 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05007527 "2890 Port error detected during port "
James Smart8a9d2e82012-05-09 21:16:12 -04007528 "reset(%d): wait_tmo:%d ms, "
7529 "port status reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05007530 "error 1=0x%x, error 2=0x%x\n",
James Smart8a9d2e82012-05-09 21:16:12 -04007531 num_resets, rdy_chk*10,
7532 reg_data.word0,
James Smart2fcee4b2010-12-15 17:57:46 -05007533 phba->work_status[0],
7534 phba->work_status[1]);
7535 rc = -ENODEV;
7536 }
7537
7538 /*
7539 * Terminate the outer loop provided the Port indicated
7540 * ready within 10 seconds.
7541 */
7542 if (rdy_chk < 1000)
7543 break;
7544 }
James Smart05580562011-05-24 11:40:48 -04007545 /* delay driver action following IF_TYPE_2 function reset */
7546 msleep(100);
James Smart2fcee4b2010-12-15 17:57:46 -05007547 break;
7548 case LPFC_SLI_INTF_IF_TYPE_1:
7549 default:
7550 break;
James Smartda0436e2009-05-22 14:51:39 -04007551 }
7552
James Smart73d91e52011-10-10 21:32:10 -04007553out:
James Smart2fcee4b2010-12-15 17:57:46 -05007554 /* Catch the not-ready port failure after a port reset. */
7555 if (num_resets >= MAX_IF_TYPE_2_RESETS)
7556 rc = -ENODEV;
7557
James Smartda0436e2009-05-22 14:51:39 -04007558 return rc;
7559}
7560
7561/**
7562 * lpfc_sli4_send_nop_mbox_cmds - Send sli-4 nop mailbox commands
7563 * @phba: pointer to lpfc hba data structure.
7564 * @cnt: number of nop mailbox commands to send.
7565 *
7566 * This routine is invoked to send a number @cnt of NOP mailbox command and
7567 * wait for each command to complete.
7568 *
7569 * Return: the number of NOP mailbox command completed.
7570 **/
7571static int
7572lpfc_sli4_send_nop_mbox_cmds(struct lpfc_hba *phba, uint32_t cnt)
7573{
7574 LPFC_MBOXQ_t *mboxq;
7575 int length, cmdsent;
7576 uint32_t mbox_tmo;
7577 uint32_t rc = 0;
7578 uint32_t shdr_status, shdr_add_status;
7579 union lpfc_sli4_cfg_shdr *shdr;
7580
7581 if (cnt == 0) {
7582 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7583 "2518 Requested to send 0 NOP mailbox cmd\n");
7584 return cnt;
7585 }
7586
7587 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7588 if (!mboxq) {
7589 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7590 "2519 Unable to allocate memory for issuing "
7591 "NOP mailbox command\n");
7592 return 0;
7593 }
7594
7595 /* Set up NOP SLI4_CONFIG mailbox-ioctl command */
7596 length = (sizeof(struct lpfc_mbx_nop) -
7597 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smartda0436e2009-05-22 14:51:39 -04007598
James Smartda0436e2009-05-22 14:51:39 -04007599 for (cmdsent = 0; cmdsent < cnt; cmdsent++) {
James Smart59df3ac2012-08-03 12:34:29 -04007600 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7601 LPFC_MBOX_OPCODE_NOP, length,
7602 LPFC_SLI4_MBX_EMBED);
James Smartda0436e2009-05-22 14:51:39 -04007603 if (!phba->sli4_hba.intr_enable)
7604 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -04007605 else {
7606 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04007607 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smarta183a152011-10-10 21:32:43 -04007608 }
James Smartda0436e2009-05-22 14:51:39 -04007609 if (rc == MBX_TIMEOUT)
7610 break;
7611 /* Check return status */
7612 shdr = (union lpfc_sli4_cfg_shdr *)
7613 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7614 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7615 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
7616 &shdr->response);
7617 if (shdr_status || shdr_add_status || rc) {
7618 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7619 "2520 NOP mailbox command failed "
7620 "status x%x add_status x%x mbx "
7621 "status x%x\n", shdr_status,
7622 shdr_add_status, rc);
7623 break;
7624 }
7625 }
7626
7627 if (rc != MBX_TIMEOUT)
7628 mempool_free(mboxq, phba->mbox_mem_pool);
7629
7630 return cmdsent;
7631}
7632
7633/**
James Smartda0436e2009-05-22 14:51:39 -04007634 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
7635 * @phba: pointer to lpfc hba data structure.
7636 *
7637 * This routine is invoked to set up the PCI device memory space for device
7638 * with SLI-4 interface spec.
7639 *
7640 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007641 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007642 * other values - error
7643 **/
7644static int
7645lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
7646{
7647 struct pci_dev *pdev;
7648 unsigned long bar0map_len, bar1map_len, bar2map_len;
7649 int error = -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -05007650 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04007651
7652 /* Obtain PCI device reference */
7653 if (!phba->pcidev)
7654 return error;
7655 else
7656 pdev = phba->pcidev;
7657
7658 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05007659 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
7660 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
7661 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
7662 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smartda0436e2009-05-22 14:51:39 -04007663 return error;
Michael Reed8e685972009-09-18 12:02:05 -05007664 }
7665 }
James Smartda0436e2009-05-22 14:51:39 -04007666
James Smart2fcee4b2010-12-15 17:57:46 -05007667 /*
7668 * The BARs and register set definitions and offset locations are
7669 * dependent on the if_type.
7670 */
7671 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
7672 &phba->sli4_hba.sli_intf.word0)) {
7673 return error;
7674 }
7675
7676 /* There is no SLI3 failback for SLI4 devices. */
7677 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
7678 LPFC_SLI_INTF_VALID) {
7679 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7680 "2894 SLI_INTF reg contents invalid "
7681 "sli_intf reg 0x%x\n",
7682 phba->sli4_hba.sli_intf.word0);
7683 return error;
7684 }
7685
7686 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7687 /*
7688 * Get the bus address of SLI4 device Bar regions and the
7689 * number of bytes required by each mapping. The mapping of the
7690 * particular PCI BARs regions is dependent on the type of
7691 * SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -04007692 */
James Smart1dfb5a42010-02-12 14:40:50 -05007693 if (pci_resource_start(pdev, 0)) {
7694 phba->pci_bar0_map = pci_resource_start(pdev, 0);
7695 bar0map_len = pci_resource_len(pdev, 0);
James Smart2fcee4b2010-12-15 17:57:46 -05007696
7697 /*
7698 * Map SLI4 PCI Config Space Register base to a kernel virtual
7699 * addr
7700 */
7701 phba->sli4_hba.conf_regs_memmap_p =
7702 ioremap(phba->pci_bar0_map, bar0map_len);
7703 if (!phba->sli4_hba.conf_regs_memmap_p) {
7704 dev_printk(KERN_ERR, &pdev->dev,
7705 "ioremap failed for SLI4 PCI config "
7706 "registers.\n");
7707 goto out;
7708 }
7709 /* Set up BAR0 PCI config space register memory map */
7710 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smart1dfb5a42010-02-12 14:40:50 -05007711 } else {
7712 phba->pci_bar0_map = pci_resource_start(pdev, 1);
7713 bar0map_len = pci_resource_len(pdev, 1);
James Smart2fcee4b2010-12-15 17:57:46 -05007714 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
7715 dev_printk(KERN_ERR, &pdev->dev,
7716 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
7717 goto out;
7718 }
7719 phba->sli4_hba.conf_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04007720 ioremap(phba->pci_bar0_map, bar0map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05007721 if (!phba->sli4_hba.conf_regs_memmap_p) {
7722 dev_printk(KERN_ERR, &pdev->dev,
7723 "ioremap failed for SLI4 PCI config "
7724 "registers.\n");
7725 goto out;
7726 }
7727 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smartda0436e2009-05-22 14:51:39 -04007728 }
7729
James Smartc31098c2011-04-16 11:03:33 -04007730 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
7731 (pci_resource_start(pdev, 2))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007732 /*
7733 * Map SLI4 if type 0 HBA Control Register base to a kernel
7734 * virtual address and setup the registers.
7735 */
7736 phba->pci_bar1_map = pci_resource_start(pdev, 2);
7737 bar1map_len = pci_resource_len(pdev, 2);
7738 phba->sli4_hba.ctrl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04007739 ioremap(phba->pci_bar1_map, bar1map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05007740 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
7741 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04007742 "ioremap failed for SLI4 HBA control registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05007743 goto out_iounmap_conf;
7744 }
7745 lpfc_sli4_bar1_register_memmap(phba);
James Smartda0436e2009-05-22 14:51:39 -04007746 }
7747
James Smartc31098c2011-04-16 11:03:33 -04007748 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
7749 (pci_resource_start(pdev, 4))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007750 /*
7751 * Map SLI4 if type 0 HBA Doorbell Register base to a kernel
7752 * virtual address and setup the registers.
7753 */
7754 phba->pci_bar2_map = pci_resource_start(pdev, 4);
7755 bar2map_len = pci_resource_len(pdev, 4);
7756 phba->sli4_hba.drbl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04007757 ioremap(phba->pci_bar2_map, bar2map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05007758 if (!phba->sli4_hba.drbl_regs_memmap_p) {
7759 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04007760 "ioremap failed for SLI4 HBA doorbell registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05007761 goto out_iounmap_ctrl;
7762 }
7763 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
7764 if (error)
7765 goto out_iounmap_all;
James Smartda0436e2009-05-22 14:51:39 -04007766 }
7767
James Smartda0436e2009-05-22 14:51:39 -04007768 return 0;
7769
7770out_iounmap_all:
7771 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
7772out_iounmap_ctrl:
7773 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
7774out_iounmap_conf:
7775 iounmap(phba->sli4_hba.conf_regs_memmap_p);
7776out:
7777 return error;
7778}
7779
7780/**
7781 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
7782 * @phba: pointer to lpfc hba data structure.
7783 *
7784 * This routine is invoked to unset the PCI device memory space for device
7785 * with SLI-4 interface spec.
7786 **/
7787static void
7788lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
7789{
James Smart2e90f4b2011-12-13 13:22:37 -05007790 uint32_t if_type;
7791 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smartda0436e2009-05-22 14:51:39 -04007792
James Smart2e90f4b2011-12-13 13:22:37 -05007793 switch (if_type) {
7794 case LPFC_SLI_INTF_IF_TYPE_0:
7795 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
7796 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
7797 iounmap(phba->sli4_hba.conf_regs_memmap_p);
7798 break;
7799 case LPFC_SLI_INTF_IF_TYPE_2:
7800 iounmap(phba->sli4_hba.conf_regs_memmap_p);
7801 break;
7802 case LPFC_SLI_INTF_IF_TYPE_1:
7803 default:
7804 dev_printk(KERN_ERR, &phba->pcidev->dev,
7805 "FATAL - unsupported SLI4 interface type - %d\n",
7806 if_type);
7807 break;
7808 }
James Smartda0436e2009-05-22 14:51:39 -04007809}
7810
7811/**
James Smart3772a992009-05-22 14:50:54 -04007812 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
7813 * @phba: pointer to lpfc hba data structure.
7814 *
7815 * This routine is invoked to enable the MSI-X interrupt vectors to device
7816 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
7817 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
7818 * invoked, enables either all or nothing, depending on the current
7819 * availability of PCI vector resources. The device driver is responsible
7820 * for calling the individual request_irq() to register each MSI-X vector
7821 * with a interrupt handler, which is done in this function. Note that
7822 * later when device is unloading, the driver should always call free_irq()
7823 * on all MSI-X vectors it has done request_irq() on before calling
7824 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
7825 * will be left with MSI-X enabled and leaks its vectors.
7826 *
7827 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007828 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007829 * other values - error
7830 **/
7831static int
7832lpfc_sli_enable_msix(struct lpfc_hba *phba)
7833{
7834 int rc, i;
7835 LPFC_MBOXQ_t *pmb;
7836
7837 /* Set up MSI-X multi-message vectors */
7838 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
7839 phba->msix_entries[i].entry = i;
7840
7841 /* Configure MSI-X capability structure */
7842 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
7843 ARRAY_SIZE(phba->msix_entries));
7844 if (rc) {
7845 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7846 "0420 PCI enable MSI-X failed (%d)\n", rc);
7847 goto msi_fail_out;
7848 }
7849 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
7850 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7851 "0477 MSI-X entry[%d]: vector=x%x "
7852 "message=%d\n", i,
7853 phba->msix_entries[i].vector,
7854 phba->msix_entries[i].entry);
7855 /*
7856 * Assign MSI-X vectors to interrupt handlers
7857 */
7858
7859 /* vector-0 is associated to slow-path handler */
7860 rc = request_irq(phba->msix_entries[0].vector,
7861 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
7862 LPFC_SP_DRIVER_HANDLER_NAME, phba);
7863 if (rc) {
7864 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7865 "0421 MSI-X slow-path request_irq failed "
7866 "(%d)\n", rc);
7867 goto msi_fail_out;
7868 }
7869
7870 /* vector-1 is associated to fast-path handler */
7871 rc = request_irq(phba->msix_entries[1].vector,
7872 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
7873 LPFC_FP_DRIVER_HANDLER_NAME, phba);
7874
7875 if (rc) {
7876 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7877 "0429 MSI-X fast-path request_irq failed "
7878 "(%d)\n", rc);
7879 goto irq_fail_out;
7880 }
7881
7882 /*
7883 * Configure HBA MSI-X attention conditions to messages
7884 */
7885 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7886
7887 if (!pmb) {
7888 rc = -ENOMEM;
7889 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7890 "0474 Unable to allocate memory for issuing "
7891 "MBOX_CONFIG_MSI command\n");
7892 goto mem_fail_out;
7893 }
7894 rc = lpfc_config_msi(phba, pmb);
7895 if (rc)
7896 goto mbx_fail_out;
7897 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
7898 if (rc != MBX_SUCCESS) {
7899 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
7900 "0351 Config MSI mailbox command failed, "
7901 "mbxCmd x%x, mbxStatus x%x\n",
7902 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
7903 goto mbx_fail_out;
7904 }
7905
7906 /* Free memory allocated for mailbox command */
7907 mempool_free(pmb, phba->mbox_mem_pool);
7908 return rc;
7909
7910mbx_fail_out:
7911 /* Free memory allocated for mailbox command */
7912 mempool_free(pmb, phba->mbox_mem_pool);
7913
7914mem_fail_out:
7915 /* free the irq already requested */
7916 free_irq(phba->msix_entries[1].vector, phba);
7917
7918irq_fail_out:
7919 /* free the irq already requested */
7920 free_irq(phba->msix_entries[0].vector, phba);
7921
7922msi_fail_out:
7923 /* Unconfigure MSI-X capability structure */
7924 pci_disable_msix(phba->pcidev);
7925 return rc;
7926}
7927
7928/**
7929 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
7930 * @phba: pointer to lpfc hba data structure.
7931 *
7932 * This routine is invoked to release the MSI-X vectors and then disable the
7933 * MSI-X interrupt mode to device with SLI-3 interface spec.
7934 **/
7935static void
7936lpfc_sli_disable_msix(struct lpfc_hba *phba)
7937{
7938 int i;
7939
7940 /* Free up MSI-X multi-message vectors */
7941 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
7942 free_irq(phba->msix_entries[i].vector, phba);
7943 /* Disable MSI-X */
7944 pci_disable_msix(phba->pcidev);
7945
7946 return;
7947}
7948
7949/**
7950 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
7951 * @phba: pointer to lpfc hba data structure.
7952 *
7953 * This routine is invoked to enable the MSI interrupt mode to device with
7954 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
7955 * enable the MSI vector. The device driver is responsible for calling the
7956 * request_irq() to register MSI vector with a interrupt the handler, which
7957 * is done in this function.
7958 *
7959 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007960 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007961 * other values - error
7962 */
7963static int
7964lpfc_sli_enable_msi(struct lpfc_hba *phba)
7965{
7966 int rc;
7967
7968 rc = pci_enable_msi(phba->pcidev);
7969 if (!rc)
7970 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7971 "0462 PCI enable MSI mode success.\n");
7972 else {
7973 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7974 "0471 PCI enable MSI mode failed (%d)\n", rc);
7975 return rc;
7976 }
7977
7978 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
7979 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7980 if (rc) {
7981 pci_disable_msi(phba->pcidev);
7982 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7983 "0478 MSI request_irq failed (%d)\n", rc);
7984 }
7985 return rc;
7986}
7987
7988/**
7989 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
7990 * @phba: pointer to lpfc hba data structure.
7991 *
7992 * This routine is invoked to disable the MSI interrupt mode to device with
7993 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
7994 * done request_irq() on before calling pci_disable_msi(). Failure to do so
7995 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
7996 * its vector.
7997 */
7998static void
7999lpfc_sli_disable_msi(struct lpfc_hba *phba)
8000{
8001 free_irq(phba->pcidev->irq, phba);
8002 pci_disable_msi(phba->pcidev);
8003 return;
8004}
8005
8006/**
8007 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
8008 * @phba: pointer to lpfc hba data structure.
8009 *
8010 * This routine is invoked to enable device interrupt and associate driver's
8011 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
8012 * spec. Depends on the interrupt mode configured to the driver, the driver
8013 * will try to fallback from the configured interrupt mode to an interrupt
8014 * mode which is supported by the platform, kernel, and device in the order
8015 * of:
8016 * MSI-X -> MSI -> IRQ.
8017 *
8018 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008019 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008020 * other values - error
8021 **/
8022static uint32_t
8023lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
8024{
8025 uint32_t intr_mode = LPFC_INTR_ERROR;
8026 int retval;
8027
8028 if (cfg_mode == 2) {
8029 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
8030 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
8031 if (!retval) {
8032 /* Now, try to enable MSI-X interrupt mode */
8033 retval = lpfc_sli_enable_msix(phba);
8034 if (!retval) {
8035 /* Indicate initialization to MSI-X mode */
8036 phba->intr_type = MSIX;
8037 intr_mode = 2;
8038 }
8039 }
8040 }
8041
8042 /* Fallback to MSI if MSI-X initialization failed */
8043 if (cfg_mode >= 1 && phba->intr_type == NONE) {
8044 retval = lpfc_sli_enable_msi(phba);
8045 if (!retval) {
8046 /* Indicate initialization to MSI mode */
8047 phba->intr_type = MSI;
8048 intr_mode = 1;
8049 }
8050 }
8051
8052 /* Fallback to INTx if both MSI-X/MSI initalization failed */
8053 if (phba->intr_type == NONE) {
8054 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
8055 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8056 if (!retval) {
8057 /* Indicate initialization to INTx mode */
8058 phba->intr_type = INTx;
8059 intr_mode = 0;
8060 }
8061 }
8062 return intr_mode;
8063}
8064
8065/**
8066 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
8067 * @phba: pointer to lpfc hba data structure.
8068 *
8069 * This routine is invoked to disable device interrupt and disassociate the
8070 * driver's interrupt handler(s) from interrupt vector(s) to device with
8071 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
8072 * release the interrupt vector(s) for the message signaled interrupt.
8073 **/
8074static void
8075lpfc_sli_disable_intr(struct lpfc_hba *phba)
8076{
8077 /* Disable the currently initialized interrupt mode */
8078 if (phba->intr_type == MSIX)
8079 lpfc_sli_disable_msix(phba);
8080 else if (phba->intr_type == MSI)
8081 lpfc_sli_disable_msi(phba);
8082 else if (phba->intr_type == INTx)
8083 free_irq(phba->pcidev->irq, phba);
8084
8085 /* Reset interrupt management states */
8086 phba->intr_type = NONE;
8087 phba->sli.slistat.sli_intr = 0;
8088
8089 return;
8090}
8091
8092/**
James Smartda0436e2009-05-22 14:51:39 -04008093 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
8094 * @phba: pointer to lpfc hba data structure.
8095 *
8096 * This routine is invoked to enable the MSI-X interrupt vectors to device
8097 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
8098 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
8099 * enables either all or nothing, depending on the current availability of
8100 * PCI vector resources. The device driver is responsible for calling the
8101 * individual request_irq() to register each MSI-X vector with a interrupt
8102 * handler, which is done in this function. Note that later when device is
8103 * unloading, the driver should always call free_irq() on all MSI-X vectors
8104 * it has done request_irq() on before calling pci_disable_msix(). Failure
8105 * to do so results in a BUG_ON() and a device will be left with MSI-X
8106 * enabled and leaks its vectors.
8107 *
8108 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008109 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008110 * other values - error
8111 **/
8112static int
8113lpfc_sli4_enable_msix(struct lpfc_hba *phba)
8114{
James Smart75baf692010-06-08 18:31:21 -04008115 int vectors, rc, index;
James Smartda0436e2009-05-22 14:51:39 -04008116
8117 /* Set up MSI-X multi-message vectors */
8118 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
8119 phba->sli4_hba.msix_entries[index].entry = index;
8120
8121 /* Configure MSI-X capability structure */
James Smart75baf692010-06-08 18:31:21 -04008122 vectors = phba->sli4_hba.cfg_eqn;
8123enable_msix_vectors:
James Smartda0436e2009-05-22 14:51:39 -04008124 rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
James Smart75baf692010-06-08 18:31:21 -04008125 vectors);
8126 if (rc > 1) {
8127 vectors = rc;
8128 goto enable_msix_vectors;
8129 } else if (rc) {
James Smartda0436e2009-05-22 14:51:39 -04008130 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8131 "0484 PCI enable MSI-X failed (%d)\n", rc);
8132 goto msi_fail_out;
8133 }
James Smart75baf692010-06-08 18:31:21 -04008134
James Smartda0436e2009-05-22 14:51:39 -04008135 /* Log MSI-X vector assignment */
James Smart75baf692010-06-08 18:31:21 -04008136 for (index = 0; index < vectors; index++)
James Smartda0436e2009-05-22 14:51:39 -04008137 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8138 "0489 MSI-X entry[%d]: vector=x%x "
8139 "message=%d\n", index,
8140 phba->sli4_hba.msix_entries[index].vector,
8141 phba->sli4_hba.msix_entries[index].entry);
8142 /*
8143 * Assign MSI-X vectors to interrupt handlers
8144 */
James Smart05580562011-05-24 11:40:48 -04008145 if (vectors > 1)
8146 rc = request_irq(phba->sli4_hba.msix_entries[0].vector,
8147 &lpfc_sli4_sp_intr_handler, IRQF_SHARED,
8148 LPFC_SP_DRIVER_HANDLER_NAME, phba);
8149 else
8150 /* All Interrupts need to be handled by one EQ */
8151 rc = request_irq(phba->sli4_hba.msix_entries[0].vector,
8152 &lpfc_sli4_intr_handler, IRQF_SHARED,
8153 LPFC_DRIVER_NAME, phba);
James Smartda0436e2009-05-22 14:51:39 -04008154 if (rc) {
8155 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8156 "0485 MSI-X slow-path request_irq failed "
8157 "(%d)\n", rc);
8158 goto msi_fail_out;
8159 }
8160
8161 /* The rest of the vector(s) are associated to fast-path handler(s) */
James Smart75baf692010-06-08 18:31:21 -04008162 for (index = 1; index < vectors; index++) {
James Smartda0436e2009-05-22 14:51:39 -04008163 phba->sli4_hba.fcp_eq_hdl[index - 1].idx = index - 1;
8164 phba->sli4_hba.fcp_eq_hdl[index - 1].phba = phba;
8165 rc = request_irq(phba->sli4_hba.msix_entries[index].vector,
8166 &lpfc_sli4_fp_intr_handler, IRQF_SHARED,
8167 LPFC_FP_DRIVER_HANDLER_NAME,
8168 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
8169 if (rc) {
8170 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8171 "0486 MSI-X fast-path (%d) "
8172 "request_irq failed (%d)\n", index, rc);
8173 goto cfg_fail_out;
8174 }
8175 }
James Smart75baf692010-06-08 18:31:21 -04008176 phba->sli4_hba.msix_vec_nr = vectors;
James Smartda0436e2009-05-22 14:51:39 -04008177
8178 return rc;
8179
8180cfg_fail_out:
8181 /* free the irq already requested */
8182 for (--index; index >= 1; index--)
8183 free_irq(phba->sli4_hba.msix_entries[index - 1].vector,
8184 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
8185
8186 /* free the irq already requested */
8187 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
8188
8189msi_fail_out:
8190 /* Unconfigure MSI-X capability structure */
8191 pci_disable_msix(phba->pcidev);
8192 return rc;
8193}
8194
8195/**
8196 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
8197 * @phba: pointer to lpfc hba data structure.
8198 *
8199 * This routine is invoked to release the MSI-X vectors and then disable the
8200 * MSI-X interrupt mode to device with SLI-4 interface spec.
8201 **/
8202static void
8203lpfc_sli4_disable_msix(struct lpfc_hba *phba)
8204{
8205 int index;
8206
8207 /* Free up MSI-X multi-message vectors */
8208 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
8209
James Smart75baf692010-06-08 18:31:21 -04008210 for (index = 1; index < phba->sli4_hba.msix_vec_nr; index++)
James Smartda0436e2009-05-22 14:51:39 -04008211 free_irq(phba->sli4_hba.msix_entries[index].vector,
8212 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
James Smart75baf692010-06-08 18:31:21 -04008213
James Smartda0436e2009-05-22 14:51:39 -04008214 /* Disable MSI-X */
8215 pci_disable_msix(phba->pcidev);
8216
8217 return;
8218}
8219
8220/**
8221 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
8222 * @phba: pointer to lpfc hba data structure.
8223 *
8224 * This routine is invoked to enable the MSI interrupt mode to device with
8225 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
8226 * to enable the MSI vector. The device driver is responsible for calling
8227 * the request_irq() to register MSI vector with a interrupt the handler,
8228 * which is done in this function.
8229 *
8230 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008231 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008232 * other values - error
8233 **/
8234static int
8235lpfc_sli4_enable_msi(struct lpfc_hba *phba)
8236{
8237 int rc, index;
8238
8239 rc = pci_enable_msi(phba->pcidev);
8240 if (!rc)
8241 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8242 "0487 PCI enable MSI mode success.\n");
8243 else {
8244 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8245 "0488 PCI enable MSI mode failed (%d)\n", rc);
8246 return rc;
8247 }
8248
8249 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
8250 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8251 if (rc) {
8252 pci_disable_msi(phba->pcidev);
8253 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8254 "0490 MSI request_irq failed (%d)\n", rc);
James Smart75baf692010-06-08 18:31:21 -04008255 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008256 }
8257
8258 for (index = 0; index < phba->cfg_fcp_eq_count; index++) {
8259 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8260 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
8261 }
8262
James Smart75baf692010-06-08 18:31:21 -04008263 return 0;
James Smartda0436e2009-05-22 14:51:39 -04008264}
8265
8266/**
8267 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
8268 * @phba: pointer to lpfc hba data structure.
8269 *
8270 * This routine is invoked to disable the MSI interrupt mode to device with
8271 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
8272 * done request_irq() on before calling pci_disable_msi(). Failure to do so
8273 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
8274 * its vector.
8275 **/
8276static void
8277lpfc_sli4_disable_msi(struct lpfc_hba *phba)
8278{
8279 free_irq(phba->pcidev->irq, phba);
8280 pci_disable_msi(phba->pcidev);
8281 return;
8282}
8283
8284/**
8285 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
8286 * @phba: pointer to lpfc hba data structure.
8287 *
8288 * This routine is invoked to enable device interrupt and associate driver's
8289 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
8290 * interface spec. Depends on the interrupt mode configured to the driver,
8291 * the driver will try to fallback from the configured interrupt mode to an
8292 * interrupt mode which is supported by the platform, kernel, and device in
8293 * the order of:
8294 * MSI-X -> MSI -> IRQ.
8295 *
8296 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008297 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008298 * other values - error
8299 **/
8300static uint32_t
8301lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
8302{
8303 uint32_t intr_mode = LPFC_INTR_ERROR;
8304 int retval, index;
8305
8306 if (cfg_mode == 2) {
8307 /* Preparation before conf_msi mbox cmd */
8308 retval = 0;
8309 if (!retval) {
8310 /* Now, try to enable MSI-X interrupt mode */
8311 retval = lpfc_sli4_enable_msix(phba);
8312 if (!retval) {
8313 /* Indicate initialization to MSI-X mode */
8314 phba->intr_type = MSIX;
8315 intr_mode = 2;
8316 }
8317 }
8318 }
8319
8320 /* Fallback to MSI if MSI-X initialization failed */
8321 if (cfg_mode >= 1 && phba->intr_type == NONE) {
8322 retval = lpfc_sli4_enable_msi(phba);
8323 if (!retval) {
8324 /* Indicate initialization to MSI mode */
8325 phba->intr_type = MSI;
8326 intr_mode = 1;
8327 }
8328 }
8329
8330 /* Fallback to INTx if both MSI-X/MSI initalization failed */
8331 if (phba->intr_type == NONE) {
8332 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
8333 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8334 if (!retval) {
8335 /* Indicate initialization to INTx mode */
8336 phba->intr_type = INTx;
8337 intr_mode = 0;
8338 for (index = 0; index < phba->cfg_fcp_eq_count;
8339 index++) {
8340 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8341 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
8342 }
8343 }
8344 }
8345 return intr_mode;
8346}
8347
8348/**
8349 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
8350 * @phba: pointer to lpfc hba data structure.
8351 *
8352 * This routine is invoked to disable device interrupt and disassociate
8353 * the driver's interrupt handler(s) from interrupt vector(s) to device
8354 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
8355 * will release the interrupt vector(s) for the message signaled interrupt.
8356 **/
8357static void
8358lpfc_sli4_disable_intr(struct lpfc_hba *phba)
8359{
8360 /* Disable the currently initialized interrupt mode */
8361 if (phba->intr_type == MSIX)
8362 lpfc_sli4_disable_msix(phba);
8363 else if (phba->intr_type == MSI)
8364 lpfc_sli4_disable_msi(phba);
8365 else if (phba->intr_type == INTx)
8366 free_irq(phba->pcidev->irq, phba);
8367
8368 /* Reset interrupt management states */
8369 phba->intr_type = NONE;
8370 phba->sli.slistat.sli_intr = 0;
8371
8372 return;
8373}
8374
8375/**
James Smart3772a992009-05-22 14:50:54 -04008376 * lpfc_unset_hba - Unset SLI3 hba device initialization
8377 * @phba: pointer to lpfc hba data structure.
8378 *
8379 * This routine is invoked to unset the HBA device initialization steps to
8380 * a device with SLI-3 interface spec.
8381 **/
8382static void
8383lpfc_unset_hba(struct lpfc_hba *phba)
8384{
8385 struct lpfc_vport *vport = phba->pport;
8386 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
8387
8388 spin_lock_irq(shost->host_lock);
8389 vport->load_flag |= FC_UNLOADING;
8390 spin_unlock_irq(shost->host_lock);
8391
James Smart72859902012-01-18 16:25:38 -05008392 kfree(phba->vpi_bmask);
8393 kfree(phba->vpi_ids);
8394
James Smart3772a992009-05-22 14:50:54 -04008395 lpfc_stop_hba_timers(phba);
8396
8397 phba->pport->work_port_events = 0;
8398
8399 lpfc_sli_hba_down(phba);
8400
8401 lpfc_sli_brdrestart(phba);
8402
8403 lpfc_sli_disable_intr(phba);
8404
8405 return;
8406}
8407
8408/**
James Smartda0436e2009-05-22 14:51:39 -04008409 * lpfc_sli4_unset_hba - Unset SLI4 hba device initialization.
8410 * @phba: pointer to lpfc hba data structure.
8411 *
8412 * This routine is invoked to unset the HBA device initialization steps to
8413 * a device with SLI-4 interface spec.
8414 **/
8415static void
8416lpfc_sli4_unset_hba(struct lpfc_hba *phba)
8417{
8418 struct lpfc_vport *vport = phba->pport;
8419 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
8420
8421 spin_lock_irq(shost->host_lock);
8422 vport->load_flag |= FC_UNLOADING;
8423 spin_unlock_irq(shost->host_lock);
8424
8425 phba->pport->work_port_events = 0;
8426
James Smart3677a3a2010-09-29 11:19:14 -04008427 /* Stop the SLI4 device port */
8428 lpfc_stop_port(phba);
James Smartda0436e2009-05-22 14:51:39 -04008429
8430 lpfc_sli4_disable_intr(phba);
8431
James Smart3677a3a2010-09-29 11:19:14 -04008432 /* Reset SLI4 HBA FCoE function */
8433 lpfc_pci_function_reset(phba);
James Smart5350d872011-10-10 21:33:49 -04008434 lpfc_sli4_queue_destroy(phba);
James Smart3677a3a2010-09-29 11:19:14 -04008435
James Smartda0436e2009-05-22 14:51:39 -04008436 return;
8437}
8438
8439/**
James Smart5af5eee2010-10-22 11:06:38 -04008440 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
8441 * @phba: Pointer to HBA context object.
8442 *
8443 * This function is called in the SLI4 code path to wait for completion
8444 * of device's XRIs exchange busy. It will check the XRI exchange busy
8445 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
8446 * that, it will check the XRI exchange busy on outstanding FCP and ELS
8447 * I/Os every 30 seconds, log error message, and wait forever. Only when
8448 * all XRI exchange busy complete, the driver unload shall proceed with
8449 * invoking the function reset ioctl mailbox command to the CNA and the
8450 * the rest of the driver unload resource release.
8451 **/
8452static void
8453lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
8454{
8455 int wait_time = 0;
8456 int fcp_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
8457 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
8458
8459 while (!fcp_xri_cmpl || !els_xri_cmpl) {
8460 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
8461 if (!fcp_xri_cmpl)
8462 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8463 "2877 FCP XRI exchange busy "
8464 "wait time: %d seconds.\n",
8465 wait_time/1000);
8466 if (!els_xri_cmpl)
8467 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8468 "2878 ELS XRI exchange busy "
8469 "wait time: %d seconds.\n",
8470 wait_time/1000);
8471 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
8472 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
8473 } else {
8474 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
8475 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
8476 }
8477 fcp_xri_cmpl =
8478 list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
8479 els_xri_cmpl =
8480 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
8481 }
8482}
8483
8484/**
James Smartda0436e2009-05-22 14:51:39 -04008485 * lpfc_sli4_hba_unset - Unset the fcoe hba
8486 * @phba: Pointer to HBA context object.
8487 *
8488 * This function is called in the SLI4 code path to reset the HBA's FCoE
8489 * function. The caller is not required to hold any lock. This routine
8490 * issues PCI function reset mailbox command to reset the FCoE function.
8491 * At the end of the function, it calls lpfc_hba_down_post function to
8492 * free any pending commands.
8493 **/
8494static void
8495lpfc_sli4_hba_unset(struct lpfc_hba *phba)
8496{
8497 int wait_cnt = 0;
8498 LPFC_MBOXQ_t *mboxq;
James Smart912e3ac2011-05-24 11:42:11 -04008499 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -04008500
8501 lpfc_stop_hba_timers(phba);
8502 phba->sli4_hba.intr_enable = 0;
8503
8504 /*
8505 * Gracefully wait out the potential current outstanding asynchronous
8506 * mailbox command.
8507 */
8508
8509 /* First, block any pending async mailbox command from posted */
8510 spin_lock_irq(&phba->hbalock);
8511 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
8512 spin_unlock_irq(&phba->hbalock);
8513 /* Now, trying to wait it out if we can */
8514 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8515 msleep(10);
8516 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
8517 break;
8518 }
8519 /* Forcefully release the outstanding mailbox command if timed out */
8520 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8521 spin_lock_irq(&phba->hbalock);
8522 mboxq = phba->sli.mbox_active;
8523 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8524 __lpfc_mbox_cmpl_put(phba, mboxq);
8525 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8526 phba->sli.mbox_active = NULL;
8527 spin_unlock_irq(&phba->hbalock);
8528 }
8529
James Smart5af5eee2010-10-22 11:06:38 -04008530 /* Abort all iocbs associated with the hba */
8531 lpfc_sli_hba_iocb_abort(phba);
8532
8533 /* Wait for completion of device XRI exchange busy */
8534 lpfc_sli4_xri_exchange_busy_wait(phba);
8535
James Smartda0436e2009-05-22 14:51:39 -04008536 /* Disable PCI subsystem interrupt */
8537 lpfc_sli4_disable_intr(phba);
8538
James Smart912e3ac2011-05-24 11:42:11 -04008539 /* Disable SR-IOV if enabled */
8540 if (phba->cfg_sriov_nr_virtfn)
8541 pci_disable_sriov(pdev);
8542
James Smartda0436e2009-05-22 14:51:39 -04008543 /* Stop kthread signal shall trigger work_done one more time */
8544 kthread_stop(phba->worker_thread);
8545
James Smart3677a3a2010-09-29 11:19:14 -04008546 /* Reset SLI4 HBA FCoE function */
8547 lpfc_pci_function_reset(phba);
James Smart5350d872011-10-10 21:33:49 -04008548 lpfc_sli4_queue_destroy(phba);
James Smart3677a3a2010-09-29 11:19:14 -04008549
James Smartda0436e2009-05-22 14:51:39 -04008550 /* Stop the SLI4 device port */
8551 phba->pport->work_port_events = 0;
8552}
8553
James Smart28baac72010-02-12 14:42:03 -05008554 /**
8555 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
8556 * @phba: Pointer to HBA context object.
8557 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
8558 *
8559 * This function is called in the SLI4 code path to read the port's
8560 * sli4 capabilities.
8561 *
8562 * This function may be be called from any context that can block-wait
8563 * for the completion. The expectation is that this routine is called
8564 * typically from probe_one or from the online routine.
8565 **/
8566int
8567lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8568{
8569 int rc;
8570 struct lpfc_mqe *mqe;
8571 struct lpfc_pc_sli4_params *sli4_params;
8572 uint32_t mbox_tmo;
8573
8574 rc = 0;
8575 mqe = &mboxq->u.mqe;
8576
8577 /* Read the port's SLI4 Parameters port capabilities */
James Smartfedd3b72011-02-16 12:39:24 -05008578 lpfc_pc_sli4_params(mboxq);
James Smart28baac72010-02-12 14:42:03 -05008579 if (!phba->sli4_hba.intr_enable)
8580 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8581 else {
James Smarta183a152011-10-10 21:32:43 -04008582 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart28baac72010-02-12 14:42:03 -05008583 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
8584 }
8585
8586 if (unlikely(rc))
8587 return 1;
8588
8589 sli4_params = &phba->sli4_hba.pc_sli4_params;
8590 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
8591 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
8592 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
8593 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
8594 &mqe->un.sli4_params);
8595 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
8596 &mqe->un.sli4_params);
8597 sli4_params->proto_types = mqe->un.sli4_params.word3;
8598 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
8599 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
8600 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
8601 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
8602 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
8603 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
8604 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
8605 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
8606 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
8607 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
8608 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
8609 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
8610 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
8611 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
8612 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
8613 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
8614 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
8615 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
8616 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
8617 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
James Smart05580562011-05-24 11:40:48 -04008618
8619 /* Make sure that sge_supp_len can be handled by the driver */
8620 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
8621 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
8622
James Smart28baac72010-02-12 14:42:03 -05008623 return rc;
8624}
8625
James Smartda0436e2009-05-22 14:51:39 -04008626/**
James Smartfedd3b72011-02-16 12:39:24 -05008627 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
8628 * @phba: Pointer to HBA context object.
8629 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
8630 *
8631 * This function is called in the SLI4 code path to read the port's
8632 * sli4 capabilities.
8633 *
8634 * This function may be be called from any context that can block-wait
8635 * for the completion. The expectation is that this routine is called
8636 * typically from probe_one or from the online routine.
8637 **/
8638int
8639lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8640{
8641 int rc;
8642 struct lpfc_mqe *mqe = &mboxq->u.mqe;
8643 struct lpfc_pc_sli4_params *sli4_params;
James Smarta183a152011-10-10 21:32:43 -04008644 uint32_t mbox_tmo;
James Smartfedd3b72011-02-16 12:39:24 -05008645 int length;
8646 struct lpfc_sli4_parameters *mbx_sli4_parameters;
8647
James Smart6d368e52011-05-24 11:44:12 -04008648 /*
8649 * By default, the driver assumes the SLI4 port requires RPI
8650 * header postings. The SLI4_PARAM response will correct this
8651 * assumption.
8652 */
8653 phba->sli4_hba.rpi_hdrs_in_use = 1;
8654
James Smartfedd3b72011-02-16 12:39:24 -05008655 /* Read the port's SLI4 Config Parameters */
8656 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
8657 sizeof(struct lpfc_sli4_cfg_mhdr));
8658 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
8659 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
8660 length, LPFC_SLI4_MBX_EMBED);
8661 if (!phba->sli4_hba.intr_enable)
8662 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -04008663 else {
8664 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
8665 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
8666 }
James Smartfedd3b72011-02-16 12:39:24 -05008667 if (unlikely(rc))
8668 return rc;
8669 sli4_params = &phba->sli4_hba.pc_sli4_params;
8670 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
8671 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
8672 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
8673 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
8674 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
8675 mbx_sli4_parameters);
8676 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
8677 mbx_sli4_parameters);
8678 if (bf_get(cfg_phwq, mbx_sli4_parameters))
8679 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
8680 else
8681 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
8682 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
8683 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
8684 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
8685 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
8686 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
8687 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
8688 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
8689 mbx_sli4_parameters);
8690 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
8691 mbx_sli4_parameters);
James Smart6d368e52011-05-24 11:44:12 -04008692 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
8693 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
James Smart05580562011-05-24 11:40:48 -04008694
8695 /* Make sure that sge_supp_len can be handled by the driver */
8696 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
8697 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
8698
James Smartfedd3b72011-02-16 12:39:24 -05008699 return 0;
8700}
8701
8702/**
James Smart3772a992009-05-22 14:50:54 -04008703 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
8704 * @pdev: pointer to PCI device
8705 * @pid: pointer to PCI device identifier
8706 *
8707 * This routine is to be called to attach a device with SLI-3 interface spec
8708 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
8709 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
8710 * information of the device and driver to see if the driver state that it can
8711 * support this kind of device. If the match is successful, the driver core
8712 * invokes this routine. If this routine determines it can claim the HBA, it
8713 * does all the initialization that it needs to do to handle the HBA properly.
8714 *
8715 * Return code
8716 * 0 - driver can claim the device
8717 * negative value - driver can not claim the device
8718 **/
8719static int __devinit
8720lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
8721{
8722 struct lpfc_hba *phba;
8723 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04008724 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -04008725 int error;
8726 uint32_t cfg_mode, intr_mode;
8727
8728 /* Allocate memory for HBA structure */
8729 phba = lpfc_hba_alloc(pdev);
8730 if (!phba)
8731 return -ENOMEM;
8732
8733 /* Perform generic PCI device enabling operation */
8734 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -04008735 if (error)
James Smart3772a992009-05-22 14:50:54 -04008736 goto out_free_phba;
James Smart3772a992009-05-22 14:50:54 -04008737
8738 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
8739 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
8740 if (error)
8741 goto out_disable_pci_dev;
8742
8743 /* Set up SLI-3 specific device PCI memory space */
8744 error = lpfc_sli_pci_mem_setup(phba);
8745 if (error) {
8746 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8747 "1402 Failed to set up pci memory space.\n");
8748 goto out_disable_pci_dev;
8749 }
8750
8751 /* Set up phase-1 common device driver resources */
8752 error = lpfc_setup_driver_resource_phase1(phba);
8753 if (error) {
8754 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8755 "1403 Failed to set up driver resource.\n");
8756 goto out_unset_pci_mem_s3;
8757 }
8758
8759 /* Set up SLI-3 specific device driver resources */
8760 error = lpfc_sli_driver_resource_setup(phba);
8761 if (error) {
8762 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8763 "1404 Failed to set up driver resource.\n");
8764 goto out_unset_pci_mem_s3;
8765 }
8766
8767 /* Initialize and populate the iocb list per host */
8768 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
8769 if (error) {
8770 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8771 "1405 Failed to initialize iocb list.\n");
8772 goto out_unset_driver_resource_s3;
8773 }
8774
8775 /* Set up common device driver resources */
8776 error = lpfc_setup_driver_resource_phase2(phba);
8777 if (error) {
8778 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8779 "1406 Failed to set up driver resource.\n");
8780 goto out_free_iocb_list;
8781 }
8782
James Smart079b5c92011-08-21 21:48:49 -04008783 /* Get the default values for Model Name and Description */
8784 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
8785
James Smart3772a992009-05-22 14:50:54 -04008786 /* Create SCSI host to the physical port */
8787 error = lpfc_create_shost(phba);
8788 if (error) {
8789 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8790 "1407 Failed to create scsi host.\n");
8791 goto out_unset_driver_resource;
8792 }
8793
8794 /* Configure sysfs attributes */
8795 vport = phba->pport;
8796 error = lpfc_alloc_sysfs_attr(vport);
8797 if (error) {
8798 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8799 "1476 Failed to allocate sysfs attr\n");
8800 goto out_destroy_shost;
8801 }
8802
James Smart6669f9b2009-10-02 15:16:45 -04008803 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -04008804 /* Now, trying to enable interrupt and bring up the device */
8805 cfg_mode = phba->cfg_use_msi;
8806 while (true) {
8807 /* Put device to a known state before enabling interrupt */
8808 lpfc_stop_port(phba);
8809 /* Configure and enable interrupt */
8810 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
8811 if (intr_mode == LPFC_INTR_ERROR) {
8812 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8813 "0431 Failed to enable interrupt.\n");
8814 error = -ENODEV;
8815 goto out_free_sysfs_attr;
8816 }
8817 /* SLI-3 HBA setup */
8818 if (lpfc_sli_hba_setup(phba)) {
8819 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8820 "1477 Failed to set up hba\n");
8821 error = -ENODEV;
8822 goto out_remove_device;
8823 }
8824
8825 /* Wait 50ms for the interrupts of previous mailbox commands */
8826 msleep(50);
8827 /* Check active interrupts on message signaled interrupts */
8828 if (intr_mode == 0 ||
8829 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
8830 /* Log the current active interrupt mode */
8831 phba->intr_mode = intr_mode;
8832 lpfc_log_intr_mode(phba, intr_mode);
8833 break;
8834 } else {
8835 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8836 "0447 Configure interrupt mode (%d) "
8837 "failed active interrupt test.\n",
8838 intr_mode);
8839 /* Disable the current interrupt mode */
8840 lpfc_sli_disable_intr(phba);
8841 /* Try next level of interrupt mode */
8842 cfg_mode = --intr_mode;
8843 }
8844 }
8845
8846 /* Perform post initialization setup */
8847 lpfc_post_init_setup(phba);
8848
8849 /* Check if there are static vports to be created. */
8850 lpfc_create_static_vport(phba);
8851
8852 return 0;
8853
8854out_remove_device:
8855 lpfc_unset_hba(phba);
8856out_free_sysfs_attr:
8857 lpfc_free_sysfs_attr(vport);
8858out_destroy_shost:
8859 lpfc_destroy_shost(phba);
8860out_unset_driver_resource:
8861 lpfc_unset_driver_resource_phase2(phba);
8862out_free_iocb_list:
8863 lpfc_free_iocb_list(phba);
8864out_unset_driver_resource_s3:
8865 lpfc_sli_driver_resource_unset(phba);
8866out_unset_pci_mem_s3:
8867 lpfc_sli_pci_mem_unset(phba);
8868out_disable_pci_dev:
8869 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04008870 if (shost)
8871 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -04008872out_free_phba:
8873 lpfc_hba_free(phba);
8874 return error;
8875}
8876
8877/**
8878 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -04008879 * @pdev: pointer to PCI device
8880 *
James Smart3772a992009-05-22 14:50:54 -04008881 * This routine is to be called to disattach a device with SLI-3 interface
8882 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
8883 * removed from PCI bus, it performs all the necessary cleanup for the HBA
8884 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -04008885 **/
dea31012005-04-17 16:05:31 -05008886static void __devexit
James Smart3772a992009-05-22 14:50:54 -04008887lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -05008888{
James Smart2e0fef82007-06-17 19:56:36 -05008889 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8890 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -05008891 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -05008892 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -05008893 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05008894 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
8895
James Smart549e55c2007-08-02 11:09:51 -04008896 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04008897 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04008898 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008899
James Smart858c9f62007-06-17 19:56:39 -05008900 lpfc_free_sysfs_attr(vport);
8901
James Smarteada2722008-12-04 22:39:13 -05008902 /* Release all the vports against this physical port */
8903 vports = lpfc_create_vport_work_array(phba);
8904 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -04008905 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
8906 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
8907 continue;
James Smarteada2722008-12-04 22:39:13 -05008908 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -04008909 }
James Smarteada2722008-12-04 22:39:13 -05008910 lpfc_destroy_vport_work_array(phba, vports);
8911
8912 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -05008913 fc_remove_host(shost);
8914 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04008915 lpfc_cleanup(vport);
8916
James Smart2e0fef82007-06-17 19:56:36 -05008917 /*
8918 * Bring down the SLI Layer. This step disable all interrupts,
8919 * clears the rings, discards all mailbox commands, and resets
8920 * the HBA.
8921 */
James Smarta257bf92009-04-06 18:48:10 -04008922
Justin P. Mattock48e34d02010-12-30 15:07:58 -08008923 /* HBA interrupt will be disabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -05008924 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -04008925 /* Stop kthread signal shall trigger work_done one more time */
8926 kthread_stop(phba->worker_thread);
8927 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -05008928 lpfc_sli_brdrestart(phba);
8929
James Smart72859902012-01-18 16:25:38 -05008930 kfree(phba->vpi_bmask);
8931 kfree(phba->vpi_ids);
8932
James Smart3772a992009-05-22 14:50:54 -04008933 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05008934 spin_lock_irq(&phba->hbalock);
8935 list_del_init(&vport->listentry);
8936 spin_unlock_irq(&phba->hbalock);
8937
James Smart858c9f62007-06-17 19:56:39 -05008938 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05008939
James Smart912e3ac2011-05-24 11:42:11 -04008940 /* Disable SR-IOV if enabled */
8941 if (phba->cfg_sriov_nr_virtfn)
8942 pci_disable_sriov(pdev);
8943
James Smart5b75da22008-12-04 22:39:35 -05008944 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04008945 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -05008946
8947 pci_set_drvdata(pdev, NULL);
James Smart858c9f62007-06-17 19:56:39 -05008948 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05008949
8950 /*
8951 * Call scsi_free before mem_free since scsi bufs are released to their
8952 * corresponding pools here.
8953 */
8954 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -04008955 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -05008956
James Smart34b02dc2008-08-24 21:49:55 -04008957 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
8958 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -05008959
James Smart2e0fef82007-06-17 19:56:36 -05008960 /* Free resources associated with SLI2 interface */
8961 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -04008962 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -05008963
8964 /* unmap adapter SLIM and Control Registers */
8965 iounmap(phba->ctrl_regs_memmap_p);
8966 iounmap(phba->slim_memmap_p);
8967
James Smart3772a992009-05-22 14:50:54 -04008968 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -05008969
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05008970 pci_release_selected_regions(pdev, bars);
James Smart2e0fef82007-06-17 19:56:36 -05008971 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05008972}
8973
Linas Vepstas8d63f372007-02-14 14:28:36 -06008974/**
James Smart3772a992009-05-22 14:50:54 -04008975 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05008976 * @pdev: pointer to PCI device
8977 * @msg: power management message
8978 *
James Smart3772a992009-05-22 14:50:54 -04008979 * This routine is to be called from the kernel's PCI subsystem to support
8980 * system Power Management (PM) to device with SLI-3 interface spec. When
8981 * PM invokes this method, it quiesces the device by stopping the driver's
8982 * worker thread for the device, turning off device's interrupt and DMA,
8983 * and bring the device offline. Note that as the driver implements the
8984 * minimum PM requirements to a power-aware driver's PM support for the
8985 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
8986 * to the suspend() method call will be treated as SUSPEND and the driver will
8987 * fully reinitialize its device during resume() method call, the driver will
8988 * set device to PCI_D3hot state in PCI config space instead of setting it
8989 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -05008990 *
8991 * Return code
James Smart3772a992009-05-22 14:50:54 -04008992 * 0 - driver suspended the device
8993 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05008994 **/
8995static int
James Smart3772a992009-05-22 14:50:54 -04008996lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -05008997{
8998 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8999 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9000
9001 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9002 "0473 PCI device Power Management suspend.\n");
9003
9004 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -04009005 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3a55b532008-12-04 22:38:54 -05009006 lpfc_offline(phba);
9007 kthread_stop(phba->worker_thread);
9008
9009 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -04009010 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -05009011
9012 /* Save device state to PCI config space */
9013 pci_save_state(pdev);
9014 pci_set_power_state(pdev, PCI_D3hot);
9015
9016 return 0;
9017}
9018
9019/**
James Smart3772a992009-05-22 14:50:54 -04009020 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05009021 * @pdev: pointer to PCI device
9022 *
James Smart3772a992009-05-22 14:50:54 -04009023 * This routine is to be called from the kernel's PCI subsystem to support
9024 * system Power Management (PM) to device with SLI-3 interface spec. When PM
9025 * invokes this method, it restores the device's PCI config space state and
9026 * fully reinitializes the device and brings it online. Note that as the
9027 * driver implements the minimum PM requirements to a power-aware driver's
9028 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
9029 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
9030 * driver will fully reinitialize its device during resume() method call,
9031 * the device will be set to PCI_D0 directly in PCI config space before
9032 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -05009033 *
9034 * Return code
James Smart3772a992009-05-22 14:50:54 -04009035 * 0 - driver suspended the device
9036 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05009037 **/
9038static int
James Smart3772a992009-05-22 14:50:54 -04009039lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -05009040{
9041 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9042 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -05009043 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05009044 int error;
9045
9046 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9047 "0452 PCI device Power Management resume.\n");
9048
9049 /* Restore device state from PCI config space */
9050 pci_set_power_state(pdev, PCI_D0);
9051 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -04009052
James Smart1dfb5a42010-02-12 14:40:50 -05009053 /*
9054 * As the new kernel behavior of pci_restore_state() API call clears
9055 * device saved_state flag, need to save the restored state again.
9056 */
9057 pci_save_state(pdev);
9058
James Smart3a55b532008-12-04 22:38:54 -05009059 if (pdev->is_busmaster)
9060 pci_set_master(pdev);
9061
9062 /* Startup the kernel thread for this host adapter. */
9063 phba->worker_thread = kthread_run(lpfc_do_work, phba,
9064 "lpfc_worker_%d", phba->brd_no);
9065 if (IS_ERR(phba->worker_thread)) {
9066 error = PTR_ERR(phba->worker_thread);
9067 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9068 "0434 PM resume failed to start worker "
9069 "thread: error=x%x.\n", error);
9070 return error;
9071 }
9072
James Smart5b75da22008-12-04 22:39:35 -05009073 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009074 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05009075 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -05009076 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -05009077 "0430 PM resume Failed to enable interrupt\n");
9078 return -EIO;
9079 } else
9080 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05009081
9082 /* Restart HBA and bring it online */
9083 lpfc_sli_brdrestart(phba);
9084 lpfc_online(phba);
9085
James Smart5b75da22008-12-04 22:39:35 -05009086 /* Log the current active interrupt mode */
9087 lpfc_log_intr_mode(phba, phba->intr_mode);
9088
James Smart3a55b532008-12-04 22:38:54 -05009089 return 0;
9090}
9091
9092/**
James Smart891478a2009-11-18 15:40:23 -05009093 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
9094 * @phba: pointer to lpfc hba data structure.
9095 *
9096 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -04009097 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -05009098 **/
9099static void
9100lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
9101{
James Smarte2af0d22010-03-15 11:25:32 -04009102 struct lpfc_sli *psli = &phba->sli;
9103 struct lpfc_sli_ring *pring;
9104
James Smart891478a2009-11-18 15:40:23 -05009105 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9106 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -04009107
9108 /*
9109 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
9110 * and let the SCSI mid-layer to retry them to recover.
9111 */
9112 pring = &psli->ring[psli->fcp_ring];
9113 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart891478a2009-11-18 15:40:23 -05009114}
9115
9116/**
James Smart0d878412009-10-02 15:16:56 -04009117 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
9118 * @phba: pointer to lpfc hba data structure.
9119 *
9120 * This routine is called to prepare the SLI3 device for PCI slot reset. It
9121 * disables the device interrupt and pci device, and aborts the internal FCP
9122 * pending I/Os.
9123 **/
9124static void
9125lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
9126{
James Smart0d878412009-10-02 15:16:56 -04009127 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05009128 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -04009129
James Smart75baf692010-06-08 18:31:21 -04009130 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -04009131 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -04009132
James Smarte2af0d22010-03-15 11:25:32 -04009133 /* Block all SCSI devices' I/Os on the host */
9134 lpfc_scsi_dev_block(phba);
9135
9136 /* stop all timers */
9137 lpfc_stop_hba_timers(phba);
9138
James Smart0d878412009-10-02 15:16:56 -04009139 /* Disable interrupt and pci device */
9140 lpfc_sli_disable_intr(phba);
9141 pci_disable_device(phba->pcidev);
James Smart75baf692010-06-08 18:31:21 -04009142
James Smarte2af0d22010-03-15 11:25:32 -04009143 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
9144 lpfc_sli_flush_fcp_rings(phba);
James Smart0d878412009-10-02 15:16:56 -04009145}
9146
9147/**
9148 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
9149 * @phba: pointer to lpfc hba data structure.
9150 *
9151 * This routine is called to prepare the SLI3 device for PCI slot permanently
9152 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
9153 * pending I/Os.
9154 **/
9155static void
James Smart75baf692010-06-08 18:31:21 -04009156lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
James Smart0d878412009-10-02 15:16:56 -04009157{
9158 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05009159 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -04009160 /* Block all SCSI devices' I/Os on the host */
9161 lpfc_scsi_dev_block(phba);
9162
9163 /* stop all timers */
9164 lpfc_stop_hba_timers(phba);
9165
James Smart0d878412009-10-02 15:16:56 -04009166 /* Clean up all driver's outstanding SCSI I/Os */
9167 lpfc_sli_flush_fcp_rings(phba);
9168}
9169
9170/**
James Smart3772a992009-05-22 14:50:54 -04009171 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -04009172 * @pdev: pointer to PCI device.
9173 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009174 *
James Smart3772a992009-05-22 14:50:54 -04009175 * This routine is called from the PCI subsystem for I/O error handling to
9176 * device with SLI-3 interface spec. This function is called by the PCI
9177 * subsystem after a PCI bus error affecting this device has been detected.
9178 * When this function is invoked, it will need to stop all the I/Os and
9179 * interrupt(s) to the device. Once that is done, it will return
9180 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
9181 * as desired.
James Smarte59058c2008-08-24 21:49:00 -04009182 *
9183 * Return codes
James Smart0d878412009-10-02 15:16:56 -04009184 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -04009185 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
9186 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -04009187 **/
James Smart3772a992009-05-22 14:50:54 -04009188static pci_ers_result_t
9189lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009190{
James Smart51ef4c22007-08-02 11:10:31 -04009191 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9192 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009193
James Smart0d878412009-10-02 15:16:56 -04009194 switch (state) {
9195 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -05009196 /* Non-fatal error, prepare for recovery */
9197 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -04009198 return PCI_ERS_RESULT_CAN_RECOVER;
9199 case pci_channel_io_frozen:
9200 /* Fatal error, prepare for slot reset */
9201 lpfc_sli_prep_dev_for_reset(phba);
9202 return PCI_ERS_RESULT_NEED_RESET;
9203 case pci_channel_io_perm_failure:
9204 /* Permanent failure, prepare for device down */
James Smart75baf692010-06-08 18:31:21 -04009205 lpfc_sli_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009206 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -04009207 default:
9208 /* Unknown state, prepare and request slot reset */
9209 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9210 "0472 Unknown PCI error state: x%x\n", state);
9211 lpfc_sli_prep_dev_for_reset(phba);
9212 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -04009213 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06009214}
9215
9216/**
James Smart3772a992009-05-22 14:50:54 -04009217 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -04009218 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009219 *
James Smart3772a992009-05-22 14:50:54 -04009220 * This routine is called from the PCI subsystem for error handling to
9221 * device with SLI-3 interface spec. This is called after PCI bus has been
9222 * reset to restart the PCI card from scratch, as if from a cold-boot.
9223 * During the PCI subsystem error recovery, after driver returns
9224 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
9225 * recovery and then call this routine before calling the .resume method
9226 * to recover the device. This function will initialize the HBA device,
9227 * enable the interrupt, but it will just put the HBA to offline state
9228 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -04009229 *
9230 * Return codes
James Smart3772a992009-05-22 14:50:54 -04009231 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
9232 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -06009233 */
James Smart3772a992009-05-22 14:50:54 -04009234static pci_ers_result_t
9235lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009236{
James Smart51ef4c22007-08-02 11:10:31 -04009237 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9238 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009239 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -05009240 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009241
9242 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +11009243 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -06009244 printk(KERN_ERR "lpfc: Cannot re-enable "
9245 "PCI device after reset.\n");
9246 return PCI_ERS_RESULT_DISCONNECT;
9247 }
9248
James Smart97207482008-12-04 22:39:19 -05009249 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05009250
9251 /*
9252 * As the new kernel behavior of pci_restore_state() API call clears
9253 * device saved_state flag, need to save the restored state again.
9254 */
9255 pci_save_state(pdev);
9256
James Smart97207482008-12-04 22:39:19 -05009257 if (pdev->is_busmaster)
9258 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009259
James Smart92d7f7b2007-06-17 19:56:38 -05009260 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04009261 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05009262 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009263
James Smart5b75da22008-12-04 22:39:35 -05009264 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009265 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05009266 if (intr_mode == LPFC_INTR_ERROR) {
9267 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9268 "0427 Cannot re-enable interrupt after "
9269 "slot reset.\n");
9270 return PCI_ERS_RESULT_DISCONNECT;
9271 } else
9272 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009273
James Smart75baf692010-06-08 18:31:21 -04009274 /* Take device offline, it will perform cleanup */
James Smart618a5232012-06-12 13:54:36 -04009275 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009276 lpfc_offline(phba);
9277 lpfc_sli_brdrestart(phba);
9278
James Smart5b75da22008-12-04 22:39:35 -05009279 /* Log the current active interrupt mode */
9280 lpfc_log_intr_mode(phba, phba->intr_mode);
9281
Linas Vepstas8d63f372007-02-14 14:28:36 -06009282 return PCI_ERS_RESULT_RECOVERED;
9283}
9284
9285/**
James Smart3772a992009-05-22 14:50:54 -04009286 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -04009287 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -06009288 *
James Smart3772a992009-05-22 14:50:54 -04009289 * This routine is called from the PCI subsystem for error handling to device
9290 * with SLI-3 interface spec. It is called when kernel error recovery tells
9291 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
9292 * error recovery. After this call, traffic can start to flow from this device
9293 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009294 */
James Smart3772a992009-05-22 14:50:54 -04009295static void
9296lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009297{
James Smart51ef4c22007-08-02 11:10:31 -04009298 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9299 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009300
James Smarte2af0d22010-03-15 11:25:32 -04009301 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -04009302 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -04009303
9304 /* Clean up Advanced Error Reporting (AER) if needed */
9305 if (phba->hba_flag & HBA_AER_ENABLED)
9306 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009307}
9308
James Smart3772a992009-05-22 14:50:54 -04009309/**
James Smartda0436e2009-05-22 14:51:39 -04009310 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
9311 * @phba: pointer to lpfc hba data structure.
9312 *
9313 * returns the number of ELS/CT IOCBs to reserve
9314 **/
9315int
9316lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
9317{
9318 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
9319
James Smartf1126682009-06-10 17:22:44 -04009320 if (phba->sli_rev == LPFC_SLI_REV4) {
9321 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -04009322 return 10;
James Smartf1126682009-06-10 17:22:44 -04009323 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -04009324 return 25;
James Smartf1126682009-06-10 17:22:44 -04009325 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -04009326 return 50;
James Smartf1126682009-06-10 17:22:44 -04009327 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -04009328 return 100;
James Smart8a9d2e82012-05-09 21:16:12 -04009329 else if (max_xri <= 1536)
James Smart6a9c52c2009-10-02 15:16:51 -04009330 return 150;
James Smart8a9d2e82012-05-09 21:16:12 -04009331 else if (max_xri <= 2048)
9332 return 200;
9333 else
9334 return 250;
James Smartf1126682009-06-10 17:22:44 -04009335 } else
9336 return 0;
James Smartda0436e2009-05-22 14:51:39 -04009337}
9338
9339/**
James Smart52d52442011-05-24 11:42:45 -04009340 * lpfc_write_firmware - attempt to write a firmware image to the port
9341 * @phba: pointer to lpfc hba data structure.
9342 * @fw: pointer to firmware image returned from request_firmware.
9343 *
9344 * returns the number of bytes written if write is successful.
9345 * returns a negative error value if there were errors.
9346 * returns 0 if firmware matches currently active firmware on port.
9347 **/
9348int
9349lpfc_write_firmware(struct lpfc_hba *phba, const struct firmware *fw)
9350{
James Smart6b5151f2012-01-18 16:24:06 -05009351 char fwrev[FW_REV_STR_SIZE];
James Smart52d52442011-05-24 11:42:45 -04009352 struct lpfc_grp_hdr *image = (struct lpfc_grp_hdr *)fw->data;
9353 struct list_head dma_buffer_list;
9354 int i, rc = 0;
9355 struct lpfc_dmabuf *dmabuf, *next;
9356 uint32_t offset = 0, temp_offset = 0;
9357
9358 INIT_LIST_HEAD(&dma_buffer_list);
James Smart079b5c92011-08-21 21:48:49 -04009359 if ((be32_to_cpu(image->magic_number) != LPFC_GROUP_OJECT_MAGIC_NUM) ||
9360 (bf_get_be32(lpfc_grp_hdr_file_type, image) !=
9361 LPFC_FILE_TYPE_GROUP) ||
9362 (bf_get_be32(lpfc_grp_hdr_id, image) != LPFC_FILE_ID_GROUP) ||
9363 (be32_to_cpu(image->size) != fw->size)) {
James Smart52d52442011-05-24 11:42:45 -04009364 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9365 "3022 Invalid FW image found. "
James Smart079b5c92011-08-21 21:48:49 -04009366 "Magic:%x Type:%x ID:%x\n",
9367 be32_to_cpu(image->magic_number),
9368 bf_get_be32(lpfc_grp_hdr_file_type, image),
9369 bf_get_be32(lpfc_grp_hdr_id, image));
James Smart52d52442011-05-24 11:42:45 -04009370 return -EINVAL;
9371 }
9372 lpfc_decode_firmware_rev(phba, fwrev, 1);
James Smart88a2cfb2011-07-22 18:36:33 -04009373 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
James Smart52d52442011-05-24 11:42:45 -04009374 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9375 "3023 Updating Firmware. Current Version:%s "
9376 "New Version:%s\n",
James Smart88a2cfb2011-07-22 18:36:33 -04009377 fwrev, image->revision);
James Smart52d52442011-05-24 11:42:45 -04009378 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
9379 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
9380 GFP_KERNEL);
9381 if (!dmabuf) {
9382 rc = -ENOMEM;
9383 goto out;
9384 }
9385 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
9386 SLI4_PAGE_SIZE,
9387 &dmabuf->phys,
9388 GFP_KERNEL);
9389 if (!dmabuf->virt) {
9390 kfree(dmabuf);
9391 rc = -ENOMEM;
9392 goto out;
9393 }
9394 list_add_tail(&dmabuf->list, &dma_buffer_list);
9395 }
9396 while (offset < fw->size) {
9397 temp_offset = offset;
9398 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
James Smart079b5c92011-08-21 21:48:49 -04009399 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
James Smart52d52442011-05-24 11:42:45 -04009400 memcpy(dmabuf->virt,
9401 fw->data + temp_offset,
James Smart079b5c92011-08-21 21:48:49 -04009402 fw->size - temp_offset);
9403 temp_offset = fw->size;
James Smart52d52442011-05-24 11:42:45 -04009404 break;
9405 }
James Smart52d52442011-05-24 11:42:45 -04009406 memcpy(dmabuf->virt, fw->data + temp_offset,
9407 SLI4_PAGE_SIZE);
James Smart88a2cfb2011-07-22 18:36:33 -04009408 temp_offset += SLI4_PAGE_SIZE;
James Smart52d52442011-05-24 11:42:45 -04009409 }
9410 rc = lpfc_wr_object(phba, &dma_buffer_list,
9411 (fw->size - offset), &offset);
9412 if (rc) {
9413 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9414 "3024 Firmware update failed. "
9415 "%d\n", rc);
9416 goto out;
9417 }
9418 }
9419 rc = offset;
9420 }
9421out:
9422 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
9423 list_del(&dmabuf->list);
9424 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
9425 dmabuf->virt, dmabuf->phys);
9426 kfree(dmabuf);
9427 }
9428 return rc;
9429}
9430
9431/**
James Smartda0436e2009-05-22 14:51:39 -04009432 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
9433 * @pdev: pointer to PCI device
9434 * @pid: pointer to PCI device identifier
9435 *
9436 * This routine is called from the kernel's PCI subsystem to device with
9437 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
9438 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
9439 * information of the device and driver to see if the driver state that it
9440 * can support this kind of device. If the match is successful, the driver
9441 * core invokes this routine. If this routine determines it can claim the HBA,
9442 * it does all the initialization that it needs to do to handle the HBA
9443 * properly.
9444 *
9445 * Return code
9446 * 0 - driver can claim the device
9447 * negative value - driver can not claim the device
9448 **/
9449static int __devinit
9450lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
9451{
9452 struct lpfc_hba *phba;
9453 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04009454 struct Scsi_Host *shost = NULL;
James Smartda0436e2009-05-22 14:51:39 -04009455 int error;
9456 uint32_t cfg_mode, intr_mode;
9457 int mcnt;
James Smart05580562011-05-24 11:40:48 -04009458 int adjusted_fcp_eq_count;
James Smart52d52442011-05-24 11:42:45 -04009459 const struct firmware *fw;
9460 uint8_t file_name[16];
James Smartda0436e2009-05-22 14:51:39 -04009461
9462 /* Allocate memory for HBA structure */
9463 phba = lpfc_hba_alloc(pdev);
9464 if (!phba)
9465 return -ENOMEM;
9466
9467 /* Perform generic PCI device enabling operation */
9468 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -04009469 if (error)
James Smartda0436e2009-05-22 14:51:39 -04009470 goto out_free_phba;
James Smartda0436e2009-05-22 14:51:39 -04009471
9472 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
9473 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
9474 if (error)
9475 goto out_disable_pci_dev;
9476
9477 /* Set up SLI-4 specific device PCI memory space */
9478 error = lpfc_sli4_pci_mem_setup(phba);
9479 if (error) {
9480 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9481 "1410 Failed to set up pci memory space.\n");
9482 goto out_disable_pci_dev;
9483 }
9484
9485 /* Set up phase-1 common device driver resources */
9486 error = lpfc_setup_driver_resource_phase1(phba);
9487 if (error) {
9488 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9489 "1411 Failed to set up driver resource.\n");
9490 goto out_unset_pci_mem_s4;
9491 }
9492
9493 /* Set up SLI-4 Specific device driver resources */
9494 error = lpfc_sli4_driver_resource_setup(phba);
9495 if (error) {
9496 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9497 "1412 Failed to set up driver resource.\n");
9498 goto out_unset_pci_mem_s4;
9499 }
9500
9501 /* Initialize and populate the iocb list per host */
James Smart2a9bf3d2010-06-07 15:24:45 -04009502
9503 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9504 "2821 initialize iocb list %d.\n",
9505 phba->cfg_iocb_cnt*1024);
9506 error = lpfc_init_iocb_list(phba, phba->cfg_iocb_cnt*1024);
9507
James Smartda0436e2009-05-22 14:51:39 -04009508 if (error) {
9509 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9510 "1413 Failed to initialize iocb list.\n");
9511 goto out_unset_driver_resource_s4;
9512 }
9513
James Smart19ca7602010-11-20 23:11:55 -05009514 INIT_LIST_HEAD(&phba->active_rrq_list);
James Smart7d791df2011-07-22 18:37:52 -04009515 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
James Smart19ca7602010-11-20 23:11:55 -05009516
James Smartda0436e2009-05-22 14:51:39 -04009517 /* Set up common device driver resources */
9518 error = lpfc_setup_driver_resource_phase2(phba);
9519 if (error) {
9520 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9521 "1414 Failed to set up driver resource.\n");
9522 goto out_free_iocb_list;
9523 }
9524
James Smart079b5c92011-08-21 21:48:49 -04009525 /* Get the default values for Model Name and Description */
9526 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
9527
James Smartda0436e2009-05-22 14:51:39 -04009528 /* Create SCSI host to the physical port */
9529 error = lpfc_create_shost(phba);
9530 if (error) {
9531 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9532 "1415 Failed to create scsi host.\n");
9533 goto out_unset_driver_resource;
9534 }
9535
9536 /* Configure sysfs attributes */
9537 vport = phba->pport;
9538 error = lpfc_alloc_sysfs_attr(vport);
9539 if (error) {
9540 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9541 "1416 Failed to allocate sysfs attr\n");
9542 goto out_destroy_shost;
9543 }
9544
James Smart6669f9b2009-10-02 15:16:45 -04009545 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -04009546 /* Now, trying to enable interrupt and bring up the device */
9547 cfg_mode = phba->cfg_use_msi;
9548 while (true) {
9549 /* Put device to a known state before enabling interrupt */
9550 lpfc_stop_port(phba);
9551 /* Configure and enable interrupt */
9552 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
9553 if (intr_mode == LPFC_INTR_ERROR) {
9554 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9555 "0426 Failed to enable interrupt.\n");
9556 error = -ENODEV;
9557 goto out_free_sysfs_attr;
9558 }
James Smart05580562011-05-24 11:40:48 -04009559 /* Default to single EQ for non-MSI-X */
James Smartdef9c7a2009-12-21 17:02:28 -05009560 if (phba->intr_type != MSIX)
James Smart05580562011-05-24 11:40:48 -04009561 adjusted_fcp_eq_count = 0;
9562 else if (phba->sli4_hba.msix_vec_nr <
9563 phba->cfg_fcp_eq_count + 1)
9564 adjusted_fcp_eq_count = phba->sli4_hba.msix_vec_nr - 1;
9565 else
9566 adjusted_fcp_eq_count = phba->cfg_fcp_eq_count;
James Smart05580562011-05-24 11:40:48 -04009567 phba->cfg_fcp_eq_count = adjusted_fcp_eq_count;
James Smartda0436e2009-05-22 14:51:39 -04009568 /* Set up SLI-4 HBA */
9569 if (lpfc_sli4_hba_setup(phba)) {
9570 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9571 "1421 Failed to set up hba\n");
9572 error = -ENODEV;
9573 goto out_disable_intr;
9574 }
9575
9576 /* Send NOP mbx cmds for non-INTx mode active interrupt test */
9577 if (intr_mode != 0)
9578 mcnt = lpfc_sli4_send_nop_mbox_cmds(phba,
9579 LPFC_ACT_INTR_CNT);
9580
9581 /* Check active interrupts received only for MSI/MSI-X */
9582 if (intr_mode == 0 ||
9583 phba->sli.slistat.sli_intr >= LPFC_ACT_INTR_CNT) {
9584 /* Log the current active interrupt mode */
9585 phba->intr_mode = intr_mode;
9586 lpfc_log_intr_mode(phba, intr_mode);
9587 break;
9588 }
9589 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9590 "0451 Configure interrupt mode (%d) "
9591 "failed active interrupt test.\n",
9592 intr_mode);
James Smart2fcee4b2010-12-15 17:57:46 -05009593 /* Unset the previous SLI-4 HBA setup. */
9594 /*
9595 * TODO: Is this operation compatible with IF TYPE 2
9596 * devices? All port state is deleted and cleared.
9597 */
James Smartda0436e2009-05-22 14:51:39 -04009598 lpfc_sli4_unset_hba(phba);
9599 /* Try next level of interrupt mode */
9600 cfg_mode = --intr_mode;
9601 }
9602
9603 /* Perform post initialization setup */
9604 lpfc_post_init_setup(phba);
9605
James Smart026abb82011-12-13 13:20:45 -05009606 /* check for firmware upgrade or downgrade (if_type 2 only) */
9607 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
9608 LPFC_SLI_INTF_IF_TYPE_2) {
9609 snprintf(file_name, 16, "%s.grp", phba->ModelName);
9610 error = request_firmware(&fw, file_name, &phba->pcidev->dev);
9611 if (!error) {
9612 lpfc_write_firmware(phba, fw);
9613 release_firmware(fw);
9614 }
James Smart52d52442011-05-24 11:42:45 -04009615 }
9616
James Smart1c6834a2009-07-19 10:01:26 -04009617 /* Check if there are static vports to be created. */
9618 lpfc_create_static_vport(phba);
James Smartda0436e2009-05-22 14:51:39 -04009619 return 0;
9620
9621out_disable_intr:
9622 lpfc_sli4_disable_intr(phba);
9623out_free_sysfs_attr:
9624 lpfc_free_sysfs_attr(vport);
9625out_destroy_shost:
9626 lpfc_destroy_shost(phba);
9627out_unset_driver_resource:
9628 lpfc_unset_driver_resource_phase2(phba);
9629out_free_iocb_list:
9630 lpfc_free_iocb_list(phba);
9631out_unset_driver_resource_s4:
9632 lpfc_sli4_driver_resource_unset(phba);
9633out_unset_pci_mem_s4:
9634 lpfc_sli4_pci_mem_unset(phba);
9635out_disable_pci_dev:
9636 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04009637 if (shost)
9638 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -04009639out_free_phba:
9640 lpfc_hba_free(phba);
9641 return error;
9642}
9643
9644/**
9645 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
9646 * @pdev: pointer to PCI device
9647 *
9648 * This routine is called from the kernel's PCI subsystem to device with
9649 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
9650 * removed from PCI bus, it performs all the necessary cleanup for the HBA
9651 * device to be removed from the PCI subsystem properly.
9652 **/
9653static void __devexit
9654lpfc_pci_remove_one_s4(struct pci_dev *pdev)
9655{
9656 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9657 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
9658 struct lpfc_vport **vports;
9659 struct lpfc_hba *phba = vport->phba;
9660 int i;
9661
9662 /* Mark the device unloading flag */
9663 spin_lock_irq(&phba->hbalock);
9664 vport->load_flag |= FC_UNLOADING;
9665 spin_unlock_irq(&phba->hbalock);
9666
9667 /* Free the HBA sysfs attributes */
9668 lpfc_free_sysfs_attr(vport);
9669
9670 /* Release all the vports against this physical port */
9671 vports = lpfc_create_vport_work_array(phba);
9672 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -04009673 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
9674 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
9675 continue;
James Smartda0436e2009-05-22 14:51:39 -04009676 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -04009677 }
James Smartda0436e2009-05-22 14:51:39 -04009678 lpfc_destroy_vport_work_array(phba, vports);
9679
9680 /* Remove FC host and then SCSI host with the physical port */
9681 fc_remove_host(shost);
9682 scsi_remove_host(shost);
9683
9684 /* Perform cleanup on the physical port */
9685 lpfc_cleanup(vport);
9686
9687 /*
9688 * Bring down the SLI Layer. This step disables all interrupts,
9689 * clears the rings, discards all mailbox commands, and resets
9690 * the HBA FCoE function.
9691 */
9692 lpfc_debugfs_terminate(vport);
9693 lpfc_sli4_hba_unset(phba);
9694
9695 spin_lock_irq(&phba->hbalock);
9696 list_del_init(&vport->listentry);
9697 spin_unlock_irq(&phba->hbalock);
9698
James Smart3677a3a2010-09-29 11:19:14 -04009699 /* Perform scsi free before driver resource_unset since scsi
James Smartda0436e2009-05-22 14:51:39 -04009700 * buffers are released to their corresponding pools here.
9701 */
9702 lpfc_scsi_free(phba);
9703 lpfc_sli4_driver_resource_unset(phba);
9704
9705 /* Unmap adapter Control and Doorbell registers */
9706 lpfc_sli4_pci_mem_unset(phba);
9707
9708 /* Release PCI resources and disable device's PCI function */
9709 scsi_host_put(shost);
9710 lpfc_disable_pci_dev(phba);
9711
9712 /* Finally, free the driver's device data structure */
9713 lpfc_hba_free(phba);
9714
9715 return;
9716}
9717
9718/**
9719 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
9720 * @pdev: pointer to PCI device
9721 * @msg: power management message
9722 *
9723 * This routine is called from the kernel's PCI subsystem to support system
9724 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
9725 * this method, it quiesces the device by stopping the driver's worker
9726 * thread for the device, turning off device's interrupt and DMA, and bring
9727 * the device offline. Note that as the driver implements the minimum PM
9728 * requirements to a power-aware driver's PM support for suspend/resume -- all
9729 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
9730 * method call will be treated as SUSPEND and the driver will fully
9731 * reinitialize its device during resume() method call, the driver will set
9732 * device to PCI_D3hot state in PCI config space instead of setting it
9733 * according to the @msg provided by the PM.
9734 *
9735 * Return code
9736 * 0 - driver suspended the device
9737 * Error otherwise
9738 **/
9739static int
9740lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
9741{
9742 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9743 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9744
9745 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart75baf692010-06-08 18:31:21 -04009746 "2843 PCI device Power Management suspend.\n");
James Smartda0436e2009-05-22 14:51:39 -04009747
9748 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -04009749 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smartda0436e2009-05-22 14:51:39 -04009750 lpfc_offline(phba);
9751 kthread_stop(phba->worker_thread);
9752
9753 /* Disable interrupt from device */
9754 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -04009755 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04009756
9757 /* Save device state to PCI config space */
9758 pci_save_state(pdev);
9759 pci_set_power_state(pdev, PCI_D3hot);
9760
9761 return 0;
9762}
9763
9764/**
9765 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
9766 * @pdev: pointer to PCI device
9767 *
9768 * This routine is called from the kernel's PCI subsystem to support system
9769 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
9770 * this method, it restores the device's PCI config space state and fully
9771 * reinitializes the device and brings it online. Note that as the driver
9772 * implements the minimum PM requirements to a power-aware driver's PM for
9773 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
9774 * to the suspend() method call will be treated as SUSPEND and the driver
9775 * will fully reinitialize its device during resume() method call, the device
9776 * will be set to PCI_D0 directly in PCI config space before restoring the
9777 * state.
9778 *
9779 * Return code
9780 * 0 - driver suspended the device
9781 * Error otherwise
9782 **/
9783static int
9784lpfc_pci_resume_one_s4(struct pci_dev *pdev)
9785{
9786 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9787 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9788 uint32_t intr_mode;
9789 int error;
9790
9791 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9792 "0292 PCI device Power Management resume.\n");
9793
9794 /* Restore device state from PCI config space */
9795 pci_set_power_state(pdev, PCI_D0);
9796 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05009797
9798 /*
9799 * As the new kernel behavior of pci_restore_state() API call clears
9800 * device saved_state flag, need to save the restored state again.
9801 */
9802 pci_save_state(pdev);
9803
James Smartda0436e2009-05-22 14:51:39 -04009804 if (pdev->is_busmaster)
9805 pci_set_master(pdev);
9806
9807 /* Startup the kernel thread for this host adapter. */
9808 phba->worker_thread = kthread_run(lpfc_do_work, phba,
9809 "lpfc_worker_%d", phba->brd_no);
9810 if (IS_ERR(phba->worker_thread)) {
9811 error = PTR_ERR(phba->worker_thread);
9812 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9813 "0293 PM resume failed to start worker "
9814 "thread: error=x%x.\n", error);
9815 return error;
9816 }
9817
9818 /* Configure and enable interrupt */
9819 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
9820 if (intr_mode == LPFC_INTR_ERROR) {
9821 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9822 "0294 PM resume Failed to enable interrupt\n");
9823 return -EIO;
9824 } else
9825 phba->intr_mode = intr_mode;
9826
9827 /* Restart HBA and bring it online */
9828 lpfc_sli_brdrestart(phba);
9829 lpfc_online(phba);
9830
9831 /* Log the current active interrupt mode */
9832 lpfc_log_intr_mode(phba, phba->intr_mode);
9833
9834 return 0;
9835}
9836
9837/**
James Smart75baf692010-06-08 18:31:21 -04009838 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
9839 * @phba: pointer to lpfc hba data structure.
9840 *
9841 * This routine is called to prepare the SLI4 device for PCI slot recover. It
9842 * aborts all the outstanding SCSI I/Os to the pci device.
9843 **/
9844static void
9845lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
9846{
9847 struct lpfc_sli *psli = &phba->sli;
9848 struct lpfc_sli_ring *pring;
9849
9850 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9851 "2828 PCI channel I/O abort preparing for recovery\n");
9852 /*
9853 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
9854 * and let the SCSI mid-layer to retry them to recover.
9855 */
9856 pring = &psli->ring[psli->fcp_ring];
9857 lpfc_sli_abort_iocb_ring(phba, pring);
9858}
9859
9860/**
9861 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
9862 * @phba: pointer to lpfc hba data structure.
9863 *
9864 * This routine is called to prepare the SLI4 device for PCI slot reset. It
9865 * disables the device interrupt and pci device, and aborts the internal FCP
9866 * pending I/Os.
9867 **/
9868static void
9869lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
9870{
9871 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9872 "2826 PCI channel disable preparing for reset\n");
9873
9874 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -04009875 lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
James Smart75baf692010-06-08 18:31:21 -04009876
9877 /* Block all SCSI devices' I/Os on the host */
9878 lpfc_scsi_dev_block(phba);
9879
9880 /* stop all timers */
9881 lpfc_stop_hba_timers(phba);
9882
9883 /* Disable interrupt and pci device */
9884 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -04009885 lpfc_sli4_queue_destroy(phba);
James Smart75baf692010-06-08 18:31:21 -04009886 pci_disable_device(phba->pcidev);
9887
9888 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
9889 lpfc_sli_flush_fcp_rings(phba);
9890}
9891
9892/**
9893 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
9894 * @phba: pointer to lpfc hba data structure.
9895 *
9896 * This routine is called to prepare the SLI4 device for PCI slot permanently
9897 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
9898 * pending I/Os.
9899 **/
9900static void
9901lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
9902{
9903 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9904 "2827 PCI channel permanent disable for failure\n");
9905
9906 /* Block all SCSI devices' I/Os on the host */
9907 lpfc_scsi_dev_block(phba);
9908
9909 /* stop all timers */
9910 lpfc_stop_hba_timers(phba);
9911
9912 /* Clean up all driver's outstanding SCSI I/Os */
9913 lpfc_sli_flush_fcp_rings(phba);
9914}
9915
9916/**
James Smartda0436e2009-05-22 14:51:39 -04009917 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
9918 * @pdev: pointer to PCI device.
9919 * @state: the current PCI connection state.
9920 *
9921 * This routine is called from the PCI subsystem for error handling to device
9922 * with SLI-4 interface spec. This function is called by the PCI subsystem
9923 * after a PCI bus error affecting this device has been detected. When this
9924 * function is invoked, it will need to stop all the I/Os and interrupt(s)
9925 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
9926 * for the PCI subsystem to perform proper recovery as desired.
9927 *
9928 * Return codes
9929 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
9930 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
9931 **/
9932static pci_ers_result_t
9933lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
9934{
James Smart75baf692010-06-08 18:31:21 -04009935 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9936 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9937
9938 switch (state) {
9939 case pci_channel_io_normal:
9940 /* Non-fatal error, prepare for recovery */
9941 lpfc_sli4_prep_dev_for_recover(phba);
9942 return PCI_ERS_RESULT_CAN_RECOVER;
9943 case pci_channel_io_frozen:
9944 /* Fatal error, prepare for slot reset */
9945 lpfc_sli4_prep_dev_for_reset(phba);
9946 return PCI_ERS_RESULT_NEED_RESET;
9947 case pci_channel_io_perm_failure:
9948 /* Permanent failure, prepare for device down */
9949 lpfc_sli4_prep_dev_for_perm_failure(phba);
9950 return PCI_ERS_RESULT_DISCONNECT;
9951 default:
9952 /* Unknown state, prepare and request slot reset */
9953 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9954 "2825 Unknown PCI error state: x%x\n", state);
9955 lpfc_sli4_prep_dev_for_reset(phba);
9956 return PCI_ERS_RESULT_NEED_RESET;
9957 }
James Smartda0436e2009-05-22 14:51:39 -04009958}
9959
9960/**
9961 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
9962 * @pdev: pointer to PCI device.
9963 *
9964 * This routine is called from the PCI subsystem for error handling to device
9965 * with SLI-4 interface spec. It is called after PCI bus has been reset to
9966 * restart the PCI card from scratch, as if from a cold-boot. During the
9967 * PCI subsystem error recovery, after the driver returns
9968 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
9969 * recovery and then call this routine before calling the .resume method to
9970 * recover the device. This function will initialize the HBA device, enable
9971 * the interrupt, but it will just put the HBA to offline state without
9972 * passing any I/O traffic.
9973 *
9974 * Return codes
9975 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
9976 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
9977 */
9978static pci_ers_result_t
9979lpfc_io_slot_reset_s4(struct pci_dev *pdev)
9980{
James Smart75baf692010-06-08 18:31:21 -04009981 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9982 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9983 struct lpfc_sli *psli = &phba->sli;
9984 uint32_t intr_mode;
9985
9986 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
9987 if (pci_enable_device_mem(pdev)) {
9988 printk(KERN_ERR "lpfc: Cannot re-enable "
9989 "PCI device after reset.\n");
9990 return PCI_ERS_RESULT_DISCONNECT;
9991 }
9992
9993 pci_restore_state(pdev);
James Smart0a96e972011-07-22 18:37:28 -04009994
9995 /*
9996 * As the new kernel behavior of pci_restore_state() API call clears
9997 * device saved_state flag, need to save the restored state again.
9998 */
9999 pci_save_state(pdev);
10000
James Smart75baf692010-06-08 18:31:21 -040010001 if (pdev->is_busmaster)
10002 pci_set_master(pdev);
10003
10004 spin_lock_irq(&phba->hbalock);
10005 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
10006 spin_unlock_irq(&phba->hbalock);
10007
10008 /* Configure and enable interrupt */
10009 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
10010 if (intr_mode == LPFC_INTR_ERROR) {
10011 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10012 "2824 Cannot re-enable interrupt after "
10013 "slot reset.\n");
10014 return PCI_ERS_RESULT_DISCONNECT;
10015 } else
10016 phba->intr_mode = intr_mode;
10017
10018 /* Log the current active interrupt mode */
10019 lpfc_log_intr_mode(phba, phba->intr_mode);
10020
James Smartda0436e2009-05-22 14:51:39 -040010021 return PCI_ERS_RESULT_RECOVERED;
10022}
10023
10024/**
10025 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
10026 * @pdev: pointer to PCI device
10027 *
10028 * This routine is called from the PCI subsystem for error handling to device
10029 * with SLI-4 interface spec. It is called when kernel error recovery tells
10030 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
10031 * error recovery. After this call, traffic can start to flow from this device
10032 * again.
10033 **/
10034static void
10035lpfc_io_resume_s4(struct pci_dev *pdev)
10036{
James Smart75baf692010-06-08 18:31:21 -040010037 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10038 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10039
10040 /*
10041 * In case of slot reset, as function reset is performed through
10042 * mailbox command which needs DMA to be enabled, this operation
10043 * has to be moved to the io resume phase. Taking device offline
10044 * will perform the necessary cleanup.
10045 */
10046 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
10047 /* Perform device reset */
James Smart618a5232012-06-12 13:54:36 -040010048 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040010049 lpfc_offline(phba);
10050 lpfc_sli_brdrestart(phba);
10051 /* Bring the device back online */
10052 lpfc_online(phba);
10053 }
10054
10055 /* Clean up Advanced Error Reporting (AER) if needed */
10056 if (phba->hba_flag & HBA_AER_ENABLED)
10057 pci_cleanup_aer_uncorrect_error_status(pdev);
James Smartda0436e2009-05-22 14:51:39 -040010058}
10059
10060/**
James Smart3772a992009-05-22 14:50:54 -040010061 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
10062 * @pdev: pointer to PCI device
10063 * @pid: pointer to PCI device identifier
10064 *
10065 * This routine is to be registered to the kernel's PCI subsystem. When an
10066 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
10067 * at PCI device-specific information of the device and driver to see if the
10068 * driver state that it can support this kind of device. If the match is
10069 * successful, the driver core invokes this routine. This routine dispatches
10070 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
10071 * do all the initialization that it needs to do to handle the HBA device
10072 * properly.
10073 *
10074 * Return code
10075 * 0 - driver can claim the device
10076 * negative value - driver can not claim the device
10077 **/
10078static int __devinit
10079lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
10080{
10081 int rc;
James Smart8fa38512009-07-19 10:01:03 -040010082 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -040010083
James Smart28baac72010-02-12 14:42:03 -050010084 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -040010085 return -ENODEV;
10086
James Smart8fa38512009-07-19 10:01:03 -040010087 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -050010088 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -040010089 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040010090 else
James Smart3772a992009-05-22 14:50:54 -040010091 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040010092
James Smart3772a992009-05-22 14:50:54 -040010093 return rc;
10094}
10095
10096/**
10097 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
10098 * @pdev: pointer to PCI device
10099 *
10100 * This routine is to be registered to the kernel's PCI subsystem. When an
10101 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
10102 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
10103 * remove routine, which will perform all the necessary cleanup for the
10104 * device to be removed from the PCI subsystem properly.
10105 **/
10106static void __devexit
10107lpfc_pci_remove_one(struct pci_dev *pdev)
10108{
10109 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10110 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10111
10112 switch (phba->pci_dev_grp) {
10113 case LPFC_PCI_DEV_LP:
10114 lpfc_pci_remove_one_s3(pdev);
10115 break;
James Smartda0436e2009-05-22 14:51:39 -040010116 case LPFC_PCI_DEV_OC:
10117 lpfc_pci_remove_one_s4(pdev);
10118 break;
James Smart3772a992009-05-22 14:50:54 -040010119 default:
10120 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10121 "1424 Invalid PCI device group: 0x%x\n",
10122 phba->pci_dev_grp);
10123 break;
10124 }
10125 return;
10126}
10127
10128/**
10129 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
10130 * @pdev: pointer to PCI device
10131 * @msg: power management message
10132 *
10133 * This routine is to be registered to the kernel's PCI subsystem to support
10134 * system Power Management (PM). When PM invokes this method, it dispatches
10135 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
10136 * suspend the device.
10137 *
10138 * Return code
10139 * 0 - driver suspended the device
10140 * Error otherwise
10141 **/
10142static int
10143lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
10144{
10145 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10146 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10147 int rc = -ENODEV;
10148
10149 switch (phba->pci_dev_grp) {
10150 case LPFC_PCI_DEV_LP:
10151 rc = lpfc_pci_suspend_one_s3(pdev, msg);
10152 break;
James Smartda0436e2009-05-22 14:51:39 -040010153 case LPFC_PCI_DEV_OC:
10154 rc = lpfc_pci_suspend_one_s4(pdev, msg);
10155 break;
James Smart3772a992009-05-22 14:50:54 -040010156 default:
10157 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10158 "1425 Invalid PCI device group: 0x%x\n",
10159 phba->pci_dev_grp);
10160 break;
10161 }
10162 return rc;
10163}
10164
10165/**
10166 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
10167 * @pdev: pointer to PCI device
10168 *
10169 * This routine is to be registered to the kernel's PCI subsystem to support
10170 * system Power Management (PM). When PM invokes this method, it dispatches
10171 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
10172 * resume the device.
10173 *
10174 * Return code
10175 * 0 - driver suspended the device
10176 * Error otherwise
10177 **/
10178static int
10179lpfc_pci_resume_one(struct pci_dev *pdev)
10180{
10181 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10182 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10183 int rc = -ENODEV;
10184
10185 switch (phba->pci_dev_grp) {
10186 case LPFC_PCI_DEV_LP:
10187 rc = lpfc_pci_resume_one_s3(pdev);
10188 break;
James Smartda0436e2009-05-22 14:51:39 -040010189 case LPFC_PCI_DEV_OC:
10190 rc = lpfc_pci_resume_one_s4(pdev);
10191 break;
James Smart3772a992009-05-22 14:50:54 -040010192 default:
10193 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10194 "1426 Invalid PCI device group: 0x%x\n",
10195 phba->pci_dev_grp);
10196 break;
10197 }
10198 return rc;
10199}
10200
10201/**
10202 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
10203 * @pdev: pointer to PCI device.
10204 * @state: the current PCI connection state.
10205 *
10206 * This routine is registered to the PCI subsystem for error handling. This
10207 * function is called by the PCI subsystem after a PCI bus error affecting
10208 * this device has been detected. When this routine is invoked, it dispatches
10209 * the action to the proper SLI-3 or SLI-4 device error detected handling
10210 * routine, which will perform the proper error detected operation.
10211 *
10212 * Return codes
10213 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10214 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10215 **/
10216static pci_ers_result_t
10217lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
10218{
10219 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10220 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10221 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
10222
10223 switch (phba->pci_dev_grp) {
10224 case LPFC_PCI_DEV_LP:
10225 rc = lpfc_io_error_detected_s3(pdev, state);
10226 break;
James Smartda0436e2009-05-22 14:51:39 -040010227 case LPFC_PCI_DEV_OC:
10228 rc = lpfc_io_error_detected_s4(pdev, state);
10229 break;
James Smart3772a992009-05-22 14:50:54 -040010230 default:
10231 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10232 "1427 Invalid PCI device group: 0x%x\n",
10233 phba->pci_dev_grp);
10234 break;
10235 }
10236 return rc;
10237}
10238
10239/**
10240 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
10241 * @pdev: pointer to PCI device.
10242 *
10243 * This routine is registered to the PCI subsystem for error handling. This
10244 * function is called after PCI bus has been reset to restart the PCI card
10245 * from scratch, as if from a cold-boot. When this routine is invoked, it
10246 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
10247 * routine, which will perform the proper device reset.
10248 *
10249 * Return codes
10250 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10251 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10252 **/
10253static pci_ers_result_t
10254lpfc_io_slot_reset(struct pci_dev *pdev)
10255{
10256 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10257 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10258 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
10259
10260 switch (phba->pci_dev_grp) {
10261 case LPFC_PCI_DEV_LP:
10262 rc = lpfc_io_slot_reset_s3(pdev);
10263 break;
James Smartda0436e2009-05-22 14:51:39 -040010264 case LPFC_PCI_DEV_OC:
10265 rc = lpfc_io_slot_reset_s4(pdev);
10266 break;
James Smart3772a992009-05-22 14:50:54 -040010267 default:
10268 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10269 "1428 Invalid PCI device group: 0x%x\n",
10270 phba->pci_dev_grp);
10271 break;
10272 }
10273 return rc;
10274}
10275
10276/**
10277 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
10278 * @pdev: pointer to PCI device
10279 *
10280 * This routine is registered to the PCI subsystem for error handling. It
10281 * is called when kernel error recovery tells the lpfc driver that it is
10282 * OK to resume normal PCI operation after PCI bus error recovery. When
10283 * this routine is invoked, it dispatches the action to the proper SLI-3
10284 * or SLI-4 device io_resume routine, which will resume the device operation.
10285 **/
10286static void
10287lpfc_io_resume(struct pci_dev *pdev)
10288{
10289 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10290 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10291
10292 switch (phba->pci_dev_grp) {
10293 case LPFC_PCI_DEV_LP:
10294 lpfc_io_resume_s3(pdev);
10295 break;
James Smartda0436e2009-05-22 14:51:39 -040010296 case LPFC_PCI_DEV_OC:
10297 lpfc_io_resume_s4(pdev);
10298 break;
James Smart3772a992009-05-22 14:50:54 -040010299 default:
10300 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10301 "1429 Invalid PCI device group: 0x%x\n",
10302 phba->pci_dev_grp);
10303 break;
10304 }
10305 return;
10306}
10307
James Smart3ef6d242012-01-18 16:23:48 -050010308/**
10309 * lpfc_mgmt_open - method called when 'lpfcmgmt' is opened from userspace
10310 * @inode: pointer to the inode representing the lpfcmgmt device
10311 * @filep: pointer to the file representing the open lpfcmgmt device
10312 *
10313 * This routine puts a reference count on the lpfc module whenever the
10314 * character device is opened
10315 **/
10316static int
10317lpfc_mgmt_open(struct inode *inode, struct file *filep)
10318{
10319 try_module_get(THIS_MODULE);
10320 return 0;
10321}
10322
10323/**
10324 * lpfc_mgmt_release - method called when 'lpfcmgmt' is closed in userspace
10325 * @inode: pointer to the inode representing the lpfcmgmt device
10326 * @filep: pointer to the file representing the open lpfcmgmt device
10327 *
10328 * This routine removes a reference count from the lpfc module when the
10329 * character device is closed
10330 **/
10331static int
10332lpfc_mgmt_release(struct inode *inode, struct file *filep)
10333{
10334 module_put(THIS_MODULE);
10335 return 0;
10336}
10337
dea31012005-04-17 16:05:31 -050010338static struct pci_device_id lpfc_id_table[] = {
10339 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
10340 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -040010341 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
10342 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010343 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
10344 PCI_ANY_ID, PCI_ANY_ID, },
10345 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
10346 PCI_ANY_ID, PCI_ANY_ID, },
10347 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
10348 PCI_ANY_ID, PCI_ANY_ID, },
10349 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
10350 PCI_ANY_ID, PCI_ANY_ID, },
10351 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
10352 PCI_ANY_ID, PCI_ANY_ID, },
10353 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
10354 PCI_ANY_ID, PCI_ANY_ID, },
10355 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
10356 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050010357 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
10358 PCI_ANY_ID, PCI_ANY_ID, },
10359 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
10360 PCI_ANY_ID, PCI_ANY_ID, },
10361 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
10362 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010363 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
10364 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050010365 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
10366 PCI_ANY_ID, PCI_ANY_ID, },
10367 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
10368 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010369 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
10370 PCI_ANY_ID, PCI_ANY_ID, },
10371 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
10372 PCI_ANY_ID, PCI_ANY_ID, },
10373 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
10374 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -040010375 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
10376 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050010377 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
10378 PCI_ANY_ID, PCI_ANY_ID, },
10379 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
10380 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010381 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
10382 PCI_ANY_ID, PCI_ANY_ID, },
10383 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
10384 PCI_ANY_ID, PCI_ANY_ID, },
10385 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
10386 PCI_ANY_ID, PCI_ANY_ID, },
10387 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
10388 PCI_ANY_ID, PCI_ANY_ID, },
10389 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
10390 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050010391 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
10392 PCI_ANY_ID, PCI_ANY_ID, },
10393 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
10394 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -040010395 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
10396 PCI_ANY_ID, PCI_ANY_ID, },
10397 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
10398 PCI_ANY_ID, PCI_ANY_ID, },
10399 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
10400 PCI_ANY_ID, PCI_ANY_ID, },
10401 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
10402 PCI_ANY_ID, PCI_ANY_ID, },
10403 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
10404 PCI_ANY_ID, PCI_ANY_ID, },
10405 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
10406 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -040010407 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
10408 PCI_ANY_ID, PCI_ANY_ID, },
10409 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
10410 PCI_ANY_ID, PCI_ANY_ID, },
10411 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
10412 PCI_ANY_ID, PCI_ANY_ID, },
James Smart3772a992009-05-22 14:50:54 -040010413 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
10414 PCI_ANY_ID, PCI_ANY_ID, },
James Smarta747c9c2009-11-18 15:41:10 -050010415 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
10416 PCI_ANY_ID, PCI_ANY_ID, },
10417 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
James Smart6669f9b2009-10-02 15:16:45 -040010418 PCI_ANY_ID, PCI_ANY_ID, },
James Smart98fc5dd2010-06-07 15:24:29 -040010419 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BALIUS,
10420 PCI_ANY_ID, PCI_ANY_ID, },
James Smart085c6472010-11-20 23:11:37 -050010421 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC,
10422 PCI_ANY_ID, PCI_ANY_ID, },
10423 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE,
10424 PCI_ANY_ID, PCI_ANY_ID, },
James Smartc0c11512011-05-24 11:41:34 -040010425 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC_VF,
10426 PCI_ANY_ID, PCI_ANY_ID, },
10427 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE_VF,
10428 PCI_ANY_ID, PCI_ANY_ID, },
James Smartf8cafd32012-08-03 12:42:51 -040010429 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SKYHAWK,
10430 PCI_ANY_ID, PCI_ANY_ID, },
10431 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SKYHAWK_VF,
10432 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010433 { 0 }
10434};
10435
10436MODULE_DEVICE_TABLE(pci, lpfc_id_table);
10437
Linas Vepstas8d63f372007-02-14 14:28:36 -060010438static struct pci_error_handlers lpfc_err_handler = {
10439 .error_detected = lpfc_io_error_detected,
10440 .slot_reset = lpfc_io_slot_reset,
10441 .resume = lpfc_io_resume,
10442};
10443
dea31012005-04-17 16:05:31 -050010444static struct pci_driver lpfc_driver = {
10445 .name = LPFC_DRIVER_NAME,
10446 .id_table = lpfc_id_table,
10447 .probe = lpfc_pci_probe_one,
10448 .remove = __devexit_p(lpfc_pci_remove_one),
James Smart3a55b532008-12-04 22:38:54 -050010449 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -040010450 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -050010451 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -050010452};
10453
James Smart3ef6d242012-01-18 16:23:48 -050010454static const struct file_operations lpfc_mgmt_fop = {
10455 .open = lpfc_mgmt_open,
10456 .release = lpfc_mgmt_release,
10457};
10458
10459static struct miscdevice lpfc_mgmt_dev = {
10460 .minor = MISC_DYNAMIC_MINOR,
10461 .name = "lpfcmgmt",
10462 .fops = &lpfc_mgmt_fop,
10463};
10464
James Smarte59058c2008-08-24 21:49:00 -040010465/**
James Smart3621a712009-04-06 18:47:14 -040010466 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -040010467 *
10468 * This routine is to be invoked when the lpfc module is loaded into the
10469 * kernel. The special kernel macro module_init() is used to indicate the
10470 * role of this routine to the kernel as lpfc module entry point.
10471 *
10472 * Return codes
10473 * 0 - successful
10474 * -ENOMEM - FC attach transport failed
10475 * all others - failed
10476 */
dea31012005-04-17 16:05:31 -050010477static int __init
10478lpfc_init(void)
10479{
10480 int error = 0;
10481
10482 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010483 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -050010484
James Smart3ef6d242012-01-18 16:23:48 -050010485 error = misc_register(&lpfc_mgmt_dev);
10486 if (error)
10487 printk(KERN_ERR "Could not register lpfcmgmt device, "
10488 "misc_register returned with status %d", error);
10489
James Smart7ee5d432007-10-27 13:37:17 -040010490 if (lpfc_enable_npiv) {
10491 lpfc_transport_functions.vport_create = lpfc_vport_create;
10492 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
10493 }
dea31012005-04-17 16:05:31 -050010494 lpfc_transport_template =
10495 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -040010496 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -050010497 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -040010498 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -040010499 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -040010500 fc_attach_transport(&lpfc_vport_transport_functions);
10501 if (lpfc_vport_transport_template == NULL) {
10502 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040010503 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -040010504 }
James Smart7ee5d432007-10-27 13:37:17 -040010505 }
dea31012005-04-17 16:05:31 -050010506 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -050010507 if (error) {
dea31012005-04-17 16:05:31 -050010508 fc_release_transport(lpfc_transport_template);
James Smartd7c255b2008-08-24 21:50:00 -040010509 if (lpfc_enable_npiv)
10510 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -050010511 }
dea31012005-04-17 16:05:31 -050010512
10513 return error;
10514}
10515
James Smarte59058c2008-08-24 21:49:00 -040010516/**
James Smart3621a712009-04-06 18:47:14 -040010517 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -040010518 *
10519 * This routine is invoked when the lpfc module is removed from the kernel.
10520 * The special kernel macro module_exit() is used to indicate the role of
10521 * this routine to the kernel as lpfc module exit point.
10522 */
dea31012005-04-17 16:05:31 -050010523static void __exit
10524lpfc_exit(void)
10525{
James Smart3ef6d242012-01-18 16:23:48 -050010526 misc_deregister(&lpfc_mgmt_dev);
dea31012005-04-17 16:05:31 -050010527 pci_unregister_driver(&lpfc_driver);
10528 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040010529 if (lpfc_enable_npiv)
10530 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -050010531 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040010532 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
10533 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050010534 (1L << _dump_buf_data_order), _dump_buf_data);
10535 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
10536 }
10537
10538 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -040010539 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
10540 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050010541 (1L << _dump_buf_dif_order), _dump_buf_dif);
10542 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
10543 }
dea31012005-04-17 16:05:31 -050010544}
10545
10546module_init(lpfc_init);
10547module_exit(lpfc_exit);
10548MODULE_LICENSE("GPL");
10549MODULE_DESCRIPTION(LPFC_MODULE_DESC);
10550MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
10551MODULE_VERSION("0:" LPFC_DRIVER_VERSION);