blob: c61508d7a0a10ffcc027b74445b032f957db5cfe [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 Smart792581d2011-03-11 16:06:44 -05004 * Copyright (C) 2004-2011 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 *);
67static void lpfc_free_sgl_list(struct lpfc_hba *);
68static int lpfc_init_sgl_list(struct lpfc_hba *);
69static 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 *);
dea31012005-04-17 16:05:31 -050076
77static struct scsi_transport_template *lpfc_transport_template = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050078static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea31012005-04-17 16:05:31 -050079static DEFINE_IDR(lpfc_hba_index);
80
James Smarte59058c2008-08-24 21:49:00 -040081/**
James Smart3621a712009-04-06 18:47:14 -040082 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
James Smarte59058c2008-08-24 21:49:00 -040083 * @phba: pointer to lpfc hba data structure.
84 *
85 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
86 * mailbox command. It retrieves the revision information from the HBA and
87 * collects the Vital Product Data (VPD) about the HBA for preparing the
88 * configuration of the HBA.
89 *
90 * Return codes:
91 * 0 - success.
92 * -ERESTART - requests the SLI layer to reset the HBA and try again.
93 * Any other value - indicates an error.
94 **/
dea31012005-04-17 16:05:31 -050095int
James Smart2e0fef82007-06-17 19:56:36 -050096lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050097{
98 lpfc_vpd_t *vp = &phba->vpd;
99 int i = 0, rc;
100 LPFC_MBOXQ_t *pmb;
101 MAILBOX_t *mb;
102 char *lpfc_vpd_data = NULL;
103 uint16_t offset = 0;
104 static char licensed[56] =
105 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -0400106 static int init_key = 1;
dea31012005-04-17 16:05:31 -0500107
108 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
109 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500110 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500111 return -ENOMEM;
112 }
113
James Smart04c68492009-05-22 14:52:52 -0400114 mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -0500115 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500116
117 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -0400118 if (init_key) {
119 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -0500120
James Smart65a29c12006-07-06 15:50:50 -0400121 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
122 *ptext = cpu_to_be32(*ptext);
123 init_key = 0;
124 }
dea31012005-04-17 16:05:31 -0500125
126 lpfc_read_nv(phba, pmb);
127 memset((char*)mb->un.varRDnvp.rsvd3, 0,
128 sizeof (mb->un.varRDnvp.rsvd3));
129 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
130 sizeof (licensed));
131
132 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
133
134 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500135 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smarte8b62012007-08-02 11:10:09 -0400136 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500137 "error, mbxCmd x%x READ_NVPARM, "
138 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500139 mb->mbxCommand, mb->mbxStatus);
140 mempool_free(pmb, phba->mbox_mem_pool);
141 return -ERESTART;
142 }
143 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500144 sizeof(phba->wwnn));
145 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
146 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500147 }
148
James Smart92d7f7b2007-06-17 19:56:38 -0500149 phba->sli3_options = 0x0;
150
dea31012005-04-17 16:05:31 -0500151 /* Setup and issue mailbox READ REV command */
152 lpfc_read_rev(phba, pmb);
153 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
154 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500155 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400156 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500157 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500158 mb->mbxCommand, mb->mbxStatus);
159 mempool_free( pmb, phba->mbox_mem_pool);
160 return -ERESTART;
161 }
162
James Smart92d7f7b2007-06-17 19:56:38 -0500163
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500164 /*
165 * The value of rr must be 1 since the driver set the cv field to 1.
166 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500167 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500168 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500169 vp->rev.rBit = 0;
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500170 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400171 "0440 Adapter failed to init, READ_REV has "
172 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500173 mempool_free(pmb, phba->mbox_mem_pool);
174 return -ERESTART;
dea31012005-04-17 16:05:31 -0500175 }
176
James Smart495a7142008-06-14 22:52:59 -0400177 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
178 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -0500179 return -EINVAL;
James Smart495a7142008-06-14 22:52:59 -0400180 }
James Smarted957682007-06-17 19:56:37 -0500181
dea31012005-04-17 16:05:31 -0500182 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500183 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500184 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500185 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
186 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
187 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
188 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500189 vp->rev.biuRev = mb->un.varRdRev.biuRev;
190 vp->rev.smRev = mb->un.varRdRev.smRev;
191 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
192 vp->rev.endecRev = mb->un.varRdRev.endecRev;
193 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
194 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
195 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
196 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
197 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
198 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
199
James Smart92d7f7b2007-06-17 19:56:38 -0500200 /* If the sli feature level is less then 9, we must
201 * tear down all RPIs and VPIs on link down if NPIV
202 * is enabled.
203 */
204 if (vp->rev.feaLevelHigh < 9)
205 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
206
dea31012005-04-17 16:05:31 -0500207 if (lpfc_is_LC_HBA(phba->pcidev->device))
208 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
209 sizeof (phba->RandomData));
210
dea31012005-04-17 16:05:31 -0500211 /* Get adapter VPD information */
dea31012005-04-17 16:05:31 -0500212 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
213 if (!lpfc_vpd_data)
James Smartd7c255b2008-08-24 21:50:00 -0400214 goto out_free_mbox;
dea31012005-04-17 16:05:31 -0500215 do {
James Smarta0c87cb2009-07-19 10:01:10 -0400216 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
dea31012005-04-17 16:05:31 -0500217 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
218
219 if (rc != MBX_SUCCESS) {
220 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400221 "0441 VPD not present on adapter, "
dea31012005-04-17 16:05:31 -0500222 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500223 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500224 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500225 }
James Smart04c68492009-05-22 14:52:52 -0400226 /* dump mem may return a zero when finished or we got a
227 * mailbox error, either way we are done.
228 */
229 if (mb->un.varDmp.word_cnt == 0)
230 break;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500231 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
232 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
James Smartd7c255b2008-08-24 21:50:00 -0400233 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
234 lpfc_vpd_data + offset,
James Smart92d7f7b2007-06-17 19:56:38 -0500235 mb->un.varDmp.word_cnt);
dea31012005-04-17 16:05:31 -0500236 offset += mb->un.varDmp.word_cnt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500237 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
238 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500239
240 kfree(lpfc_vpd_data);
dea31012005-04-17 16:05:31 -0500241out_free_mbox:
242 mempool_free(pmb, phba->mbox_mem_pool);
243 return 0;
244}
245
James Smarte59058c2008-08-24 21:49:00 -0400246/**
James Smart3621a712009-04-06 18:47:14 -0400247 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
James Smarte59058c2008-08-24 21:49:00 -0400248 * @phba: pointer to lpfc hba data structure.
249 * @pmboxq: pointer to the driver internal queue element for mailbox command.
250 *
251 * This is the completion handler for driver's configuring asynchronous event
252 * mailbox command to the device. If the mailbox command returns successfully,
253 * it will set internal async event support flag to 1; otherwise, it will
254 * set internal async event support flag to 0.
255 **/
James Smart57127f12007-10-27 13:37:05 -0400256static void
257lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
258{
James Smart04c68492009-05-22 14:52:52 -0400259 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
James Smart57127f12007-10-27 13:37:05 -0400260 phba->temp_sensor_support = 1;
261 else
262 phba->temp_sensor_support = 0;
263 mempool_free(pmboxq, phba->mbox_mem_pool);
264 return;
265}
266
James Smarte59058c2008-08-24 21:49:00 -0400267/**
James Smart3621a712009-04-06 18:47:14 -0400268 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
James Smart97207482008-12-04 22:39:19 -0500269 * @phba: pointer to lpfc hba data structure.
270 * @pmboxq: pointer to the driver internal queue element for mailbox command.
271 *
272 * This is the completion handler for dump mailbox command for getting
273 * wake up parameters. When this command complete, the response contain
274 * Option rom version of the HBA. This function translate the version number
275 * into a human readable string and store it in OptionROMVersion.
276 **/
277static void
278lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
279{
280 struct prog_id *prg;
281 uint32_t prog_id_word;
282 char dist = ' ';
283 /* character array used for decoding dist type. */
284 char dist_char[] = "nabx";
285
James Smart04c68492009-05-22 14:52:52 -0400286 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
James Smart9f1e1b52008-12-04 22:39:40 -0500287 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500288 return;
James Smart9f1e1b52008-12-04 22:39:40 -0500289 }
James Smart97207482008-12-04 22:39:19 -0500290
291 prg = (struct prog_id *) &prog_id_word;
292
293 /* word 7 contain option rom version */
James Smart04c68492009-05-22 14:52:52 -0400294 prog_id_word = pmboxq->u.mb.un.varWords[7];
James Smart97207482008-12-04 22:39:19 -0500295
296 /* Decode the Option rom version word to a readable string */
297 if (prg->dist < 4)
298 dist = dist_char[prg->dist];
299
300 if ((prg->dist == 3) && (prg->num == 0))
301 sprintf(phba->OptionROMVersion, "%d.%d%d",
302 prg->ver, prg->rev, prg->lev);
303 else
304 sprintf(phba->OptionROMVersion, "%d.%d%d%c%d",
305 prg->ver, prg->rev, prg->lev,
306 dist, prg->num);
James Smart9f1e1b52008-12-04 22:39:40 -0500307 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500308 return;
309}
310
311/**
James Smart05580562011-05-24 11:40:48 -0400312 * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
313 * cfg_soft_wwnn, cfg_soft_wwpn
314 * @vport: pointer to lpfc vport data structure.
315 *
316 *
317 * Return codes
318 * None.
319 **/
320void
321lpfc_update_vport_wwn(struct lpfc_vport *vport)
322{
323 /* If the soft name exists then update it using the service params */
324 if (vport->phba->cfg_soft_wwnn)
325 u64_to_wwn(vport->phba->cfg_soft_wwnn,
326 vport->fc_sparam.nodeName.u.wwn);
327 if (vport->phba->cfg_soft_wwpn)
328 u64_to_wwn(vport->phba->cfg_soft_wwpn,
329 vport->fc_sparam.portName.u.wwn);
330
331 /*
332 * If the name is empty or there exists a soft name
333 * then copy the service params name, otherwise use the fc name
334 */
335 if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
336 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
337 sizeof(struct lpfc_name));
338 else
339 memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
340 sizeof(struct lpfc_name));
341
342 if (vport->fc_portname.u.wwn[0] == 0 || vport->phba->cfg_soft_wwpn)
343 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
344 sizeof(struct lpfc_name));
345 else
346 memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
347 sizeof(struct lpfc_name));
348}
349
350/**
James Smart3621a712009-04-06 18:47:14 -0400351 * lpfc_config_port_post - Perform lpfc initialization after config port
James Smarte59058c2008-08-24 21:49:00 -0400352 * @phba: pointer to lpfc hba data structure.
353 *
354 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
355 * command call. It performs all internal resource and state setups on the
356 * port: post IOCB buffers, enable appropriate host interrupt attentions,
357 * ELS ring timers, etc.
358 *
359 * Return codes
360 * 0 - success.
361 * Any other value - error.
362 **/
dea31012005-04-17 16:05:31 -0500363int
James Smart2e0fef82007-06-17 19:56:36 -0500364lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500365{
James Smart2e0fef82007-06-17 19:56:36 -0500366 struct lpfc_vport *vport = phba->pport;
James Smarta257bf92009-04-06 18:48:10 -0400367 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -0500368 LPFC_MBOXQ_t *pmb;
369 MAILBOX_t *mb;
370 struct lpfc_dmabuf *mp;
371 struct lpfc_sli *psli = &phba->sli;
372 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500373 int i, j;
374 int rc;
dea31012005-04-17 16:05:31 -0500375
James Smart7af67052007-10-27 13:38:11 -0400376 spin_lock_irq(&phba->hbalock);
377 /*
378 * If the Config port completed correctly the HBA is not
379 * over heated any more.
380 */
381 if (phba->over_temp_state == HBA_OVER_TEMP)
382 phba->over_temp_state = HBA_NORMAL_TEMP;
383 spin_unlock_irq(&phba->hbalock);
384
dea31012005-04-17 16:05:31 -0500385 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
386 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500387 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500388 return -ENOMEM;
389 }
James Smart04c68492009-05-22 14:52:52 -0400390 mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500391
dea31012005-04-17 16:05:31 -0500392 /* Get login parameters for NID. */
James Smart9f1177a2010-02-26 14:12:57 -0500393 rc = lpfc_read_sparam(phba, pmb, 0);
394 if (rc) {
395 mempool_free(pmb, phba->mbox_mem_pool);
396 return -ENOMEM;
397 }
398
James Smarted957682007-06-17 19:56:37 -0500399 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500400 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500401 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400402 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500403 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500404 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500405 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500406 mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart9f1177a2010-02-26 14:12:57 -0500407 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500408 lpfc_mbuf_free(phba, mp->virt, mp->phys);
409 kfree(mp);
410 return -EIO;
411 }
412
413 mp = (struct lpfc_dmabuf *) pmb->context1;
414
James Smart2e0fef82007-06-17 19:56:36 -0500415 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500416 lpfc_mbuf_free(phba, mp->virt, mp->phys);
417 kfree(mp);
418 pmb->context1 = NULL;
James Smart05580562011-05-24 11:40:48 -0400419 lpfc_update_vport_wwn(vport);
James Smarta257bf92009-04-06 18:48:10 -0400420
421 /* Update the fc_host data structures with new wwn. */
422 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
423 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart21e9a0a2009-05-22 14:53:21 -0400424 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smarta257bf92009-04-06 18:48:10 -0400425
dea31012005-04-17 16:05:31 -0500426 /* If no serial number in VPD data, use low 6 bytes of WWNN */
427 /* This should be consolidated into parse_vpd ? - mr */
428 if (phba->SerialNumber[0] == 0) {
429 uint8_t *outptr;
430
James Smart2e0fef82007-06-17 19:56:36 -0500431 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500432 for (i = 0; i < 12; i++) {
433 status = *outptr++;
434 j = ((status & 0xf0) >> 4);
435 if (j <= 9)
436 phba->SerialNumber[i] =
437 (char)((uint8_t) 0x30 + (uint8_t) j);
438 else
439 phba->SerialNumber[i] =
440 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
441 i++;
442 j = (status & 0xf);
443 if (j <= 9)
444 phba->SerialNumber[i] =
445 (char)((uint8_t) 0x30 + (uint8_t) j);
446 else
447 phba->SerialNumber[i] =
448 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
449 }
450 }
451
dea31012005-04-17 16:05:31 -0500452 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500453 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500454 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500455 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400456 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500457 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500458 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500459 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500460 mempool_free( pmb, phba->mbox_mem_pool);
461 return -EIO;
462 }
463
James Smarta0c87cb2009-07-19 10:01:10 -0400464 /* Check if the port is disabled */
465 lpfc_sli_read_link_ste(phba);
466
dea31012005-04-17 16:05:31 -0500467 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
468 if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
469 phba->cfg_hba_queue_depth =
James Smartf1126682009-06-10 17:22:44 -0400470 (mb->un.varRdConfig.max_xri + 1) -
471 lpfc_sli4_get_els_iocb_cnt(phba);
dea31012005-04-17 16:05:31 -0500472
473 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500474
475 /* Get the default values for Model Name and Description */
476 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
477
James Smart2e0fef82007-06-17 19:56:36 -0500478 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500479
James Smart0b727fe2007-10-27 13:37:25 -0400480 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smarta4bc3372006-12-02 13:34:16 -0500481 if (psli->ring[psli->extra_ring].cmdringaddr)
482 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500483 if (psli->ring[psli->fcp_ring].cmdringaddr)
484 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
485 if (psli->ring[psli->next_ring].cmdringaddr)
486 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
487
488 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500489 if (phba->sli_rev != 3)
490 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500491
James Smart93996272008-08-24 21:50:30 -0400492 /*
493 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
494 */
495 if (phba->intr_type == MSIX) {
496 rc = lpfc_config_msi(phba, pmb);
497 if (rc) {
498 mempool_free(pmb, phba->mbox_mem_pool);
499 return -EIO;
500 }
501 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
502 if (rc != MBX_SUCCESS) {
503 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
504 "0352 Config MSI mailbox command "
505 "failed, mbxCmd x%x, mbxStatus x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400506 pmb->u.mb.mbxCommand,
507 pmb->u.mb.mbxStatus);
James Smart93996272008-08-24 21:50:30 -0400508 mempool_free(pmb, phba->mbox_mem_pool);
509 return -EIO;
510 }
511 }
512
James Smart04c68492009-05-22 14:52:52 -0400513 spin_lock_irq(&phba->hbalock);
James Smart93996272008-08-24 21:50:30 -0400514 /* Initialize ERATT handling flag */
515 phba->hba_flag &= ~HBA_ERATT_HANDLED;
516
dea31012005-04-17 16:05:31 -0500517 /* Enable appropriate host interrupts */
James Smart9940b972011-03-11 16:06:12 -0500518 if (lpfc_readl(phba->HCregaddr, &status)) {
519 spin_unlock_irq(&phba->hbalock);
520 return -EIO;
521 }
dea31012005-04-17 16:05:31 -0500522 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
523 if (psli->num_rings > 0)
524 status |= HC_R0INT_ENA;
525 if (psli->num_rings > 1)
526 status |= HC_R1INT_ENA;
527 if (psli->num_rings > 2)
528 status |= HC_R2INT_ENA;
529 if (psli->num_rings > 3)
530 status |= HC_R3INT_ENA;
531
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500532 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
533 (phba->cfg_poll & DISABLE_FCP_RING_INT))
James Smart93996272008-08-24 21:50:30 -0400534 status &= ~(HC_R0INT_ENA);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500535
dea31012005-04-17 16:05:31 -0500536 writel(status, phba->HCregaddr);
537 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500538 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500539
James Smart93996272008-08-24 21:50:30 -0400540 /* Set up ring-0 (ELS) timer */
541 timeout = phba->fc_ratov * 2;
James Smart2e0fef82007-06-17 19:56:36 -0500542 mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
James Smart93996272008-08-24 21:50:30 -0400543 /* Set up heart beat (HB) timer */
James Smart858c9f62007-06-17 19:56:39 -0500544 mod_timer(&phba->hb_tmofunc, jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
545 phba->hb_outstanding = 0;
546 phba->last_completion_time = jiffies;
James Smart93996272008-08-24 21:50:30 -0400547 /* Set up error attention (ERATT) polling timer */
548 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
dea31012005-04-17 16:05:31 -0500549
James Smarta0c87cb2009-07-19 10:01:10 -0400550 if (phba->hba_flag & LINK_DISABLED) {
551 lpfc_printf_log(phba,
552 KERN_ERR, LOG_INIT,
553 "2598 Adapter Link is disabled.\n");
554 lpfc_down_link(phba, pmb);
555 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
556 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
557 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
558 lpfc_printf_log(phba,
559 KERN_ERR, LOG_INIT,
560 "2599 Adapter failed to issue DOWN_LINK"
561 " mbox command rc 0x%x\n", rc);
562
563 mempool_free(pmb, phba->mbox_mem_pool);
564 return -EIO;
565 }
James Smarte40a02c2010-02-26 14:13:54 -0500566 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart026abb82011-12-13 13:20:45 -0500567 mempool_free(pmb, phba->mbox_mem_pool);
568 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
569 if (rc)
570 return rc;
dea31012005-04-17 16:05:31 -0500571 }
572 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400573 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500574 if (!pmb) {
575 phba->link_state = LPFC_HBA_ERROR;
576 return -ENOMEM;
577 }
578
James Smart57127f12007-10-27 13:37:05 -0400579 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
580 pmb->mbox_cmpl = lpfc_config_async_cmpl;
581 pmb->vport = phba->pport;
582 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500583
James Smart57127f12007-10-27 13:37:05 -0400584 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
585 lpfc_printf_log(phba,
586 KERN_ERR,
587 LOG_INIT,
588 "0456 Adapter failed to issue "
James Smarte4e74272009-07-19 10:01:38 -0400589 "ASYNCEVT_ENABLE mbox status x%x\n",
James Smart57127f12007-10-27 13:37:05 -0400590 rc);
591 mempool_free(pmb, phba->mbox_mem_pool);
592 }
James Smart97207482008-12-04 22:39:19 -0500593
594 /* Get Option rom version */
595 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500596 if (!pmb) {
597 phba->link_state = LPFC_HBA_ERROR;
598 return -ENOMEM;
599 }
600
James Smart97207482008-12-04 22:39:19 -0500601 lpfc_dump_wakeup_param(phba, pmb);
602 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
603 pmb->vport = phba->pport;
604 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
605
606 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
607 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
James Smarte4e74272009-07-19 10:01:38 -0400608 "to get Option ROM version status x%x\n", rc);
James Smart97207482008-12-04 22:39:19 -0500609 mempool_free(pmb, phba->mbox_mem_pool);
610 }
611
James Smartd7c255b2008-08-24 21:50:00 -0400612 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400613}
614
James Smarte59058c2008-08-24 21:49:00 -0400615/**
James Smart84d1b002010-02-12 14:42:33 -0500616 * lpfc_hba_init_link - Initialize the FC link
617 * @phba: pointer to lpfc hba data structure.
James Smart6e7288d2010-06-07 15:23:35 -0400618 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500619 *
620 * This routine will issue the INIT_LINK mailbox command call.
621 * It is available to other drivers through the lpfc_hba data
622 * structure for use as a delayed link up mechanism with the
623 * module parameter lpfc_suppress_link_up.
624 *
625 * Return code
626 * 0 - success
627 * Any other value - error
628 **/
629int
James Smart6e7288d2010-06-07 15:23:35 -0400630lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500631{
James Smart1b511972011-12-13 13:23:09 -0500632 return lpfc_hba_init_link_fc_topology(phba, phba->cfg_topology, flag);
633}
634
635/**
636 * lpfc_hba_init_link_fc_topology - Initialize FC link with desired topology
637 * @phba: pointer to lpfc hba data structure.
638 * @fc_topology: desired fc topology.
639 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
640 *
641 * This routine will issue the INIT_LINK mailbox command call.
642 * It is available to other drivers through the lpfc_hba data
643 * structure for use as a delayed link up mechanism with the
644 * module parameter lpfc_suppress_link_up.
645 *
646 * Return code
647 * 0 - success
648 * Any other value - error
649 **/
650int
651lpfc_hba_init_link_fc_topology(struct lpfc_hba *phba, uint32_t fc_topology,
652 uint32_t flag)
653{
James Smart84d1b002010-02-12 14:42:33 -0500654 struct lpfc_vport *vport = phba->pport;
655 LPFC_MBOXQ_t *pmb;
656 MAILBOX_t *mb;
657 int rc;
658
659 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
660 if (!pmb) {
661 phba->link_state = LPFC_HBA_ERROR;
662 return -ENOMEM;
663 }
664 mb = &pmb->u.mb;
665 pmb->vport = vport;
666
James Smart026abb82011-12-13 13:20:45 -0500667 if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_MAX) ||
668 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G) &&
669 !(phba->lmt & LMT_1Gb)) ||
670 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G) &&
671 !(phba->lmt & LMT_2Gb)) ||
672 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G) &&
673 !(phba->lmt & LMT_4Gb)) ||
674 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G) &&
675 !(phba->lmt & LMT_8Gb)) ||
676 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G) &&
677 !(phba->lmt & LMT_10Gb)) ||
678 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G) &&
679 !(phba->lmt & LMT_16Gb))) {
680 /* Reset link speed to auto */
681 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
682 "1302 Invalid speed for this board:%d "
683 "Reset link speed to auto.\n",
684 phba->cfg_link_speed);
685 phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
686 }
James Smart1b511972011-12-13 13:23:09 -0500687 lpfc_init_link(phba, pmb, fc_topology, phba->cfg_link_speed);
James Smart84d1b002010-02-12 14:42:33 -0500688 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart1b511972011-12-13 13:23:09 -0500689 if (phba->sli_rev < LPFC_SLI_REV4)
690 lpfc_set_loopback_flag(phba);
James Smart6e7288d2010-06-07 15:23:35 -0400691 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart76a95d72010-11-20 23:11:48 -0500692 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
James Smart84d1b002010-02-12 14:42:33 -0500693 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
694 "0498 Adapter failed to init, mbxCmd x%x "
695 "INIT_LINK, mbxStatus x%x\n",
696 mb->mbxCommand, mb->mbxStatus);
James Smart76a95d72010-11-20 23:11:48 -0500697 if (phba->sli_rev <= LPFC_SLI_REV3) {
698 /* Clear all interrupt enable conditions */
699 writel(0, phba->HCregaddr);
700 readl(phba->HCregaddr); /* flush */
701 /* Clear all pending interrupts */
702 writel(0xffffffff, phba->HAregaddr);
703 readl(phba->HAregaddr); /* flush */
704 }
James Smart84d1b002010-02-12 14:42:33 -0500705 phba->link_state = LPFC_HBA_ERROR;
James Smart6e7288d2010-06-07 15:23:35 -0400706 if (rc != MBX_BUSY || flag == MBX_POLL)
James Smart84d1b002010-02-12 14:42:33 -0500707 mempool_free(pmb, phba->mbox_mem_pool);
708 return -EIO;
709 }
James Smarte40a02c2010-02-26 14:13:54 -0500710 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
James Smart6e7288d2010-06-07 15:23:35 -0400711 if (flag == MBX_POLL)
712 mempool_free(pmb, phba->mbox_mem_pool);
James Smart84d1b002010-02-12 14:42:33 -0500713
714 return 0;
715}
716
717/**
718 * lpfc_hba_down_link - this routine downs the FC link
James Smart6e7288d2010-06-07 15:23:35 -0400719 * @phba: pointer to lpfc hba data structure.
720 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500721 *
722 * This routine will issue the DOWN_LINK mailbox command call.
723 * It is available to other drivers through the lpfc_hba data
724 * structure for use to stop the link.
725 *
726 * Return code
727 * 0 - success
728 * Any other value - error
729 **/
730int
James Smart6e7288d2010-06-07 15:23:35 -0400731lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500732{
733 LPFC_MBOXQ_t *pmb;
734 int rc;
735
736 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
737 if (!pmb) {
738 phba->link_state = LPFC_HBA_ERROR;
739 return -ENOMEM;
740 }
741
742 lpfc_printf_log(phba,
743 KERN_ERR, LOG_INIT,
744 "0491 Adapter Link is disabled.\n");
745 lpfc_down_link(phba, pmb);
746 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6e7288d2010-06-07 15:23:35 -0400747 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart84d1b002010-02-12 14:42:33 -0500748 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
749 lpfc_printf_log(phba,
750 KERN_ERR, LOG_INIT,
751 "2522 Adapter failed to issue DOWN_LINK"
752 " mbox command rc 0x%x\n", rc);
753
754 mempool_free(pmb, phba->mbox_mem_pool);
755 return -EIO;
756 }
James Smart6e7288d2010-06-07 15:23:35 -0400757 if (flag == MBX_POLL)
758 mempool_free(pmb, phba->mbox_mem_pool);
759
James Smart84d1b002010-02-12 14:42:33 -0500760 return 0;
761}
762
763/**
James Smart3621a712009-04-06 18:47:14 -0400764 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400765 * @phba: pointer to lpfc HBA data structure.
766 *
767 * This routine will do LPFC uninitialization before the HBA is reset when
768 * bringing down the SLI Layer.
769 *
770 * Return codes
771 * 0 - success.
772 * Any other value - error.
773 **/
dea31012005-04-17 16:05:31 -0500774int
James Smart2e0fef82007-06-17 19:56:36 -0500775lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500776{
James Smart1b32f6a2008-02-08 18:49:39 -0500777 struct lpfc_vport **vports;
778 int i;
James Smart3772a992009-05-22 14:50:54 -0400779
780 if (phba->sli_rev <= LPFC_SLI_REV3) {
781 /* Disable interrupts */
782 writel(0, phba->HCregaddr);
783 readl(phba->HCregaddr); /* flush */
784 }
dea31012005-04-17 16:05:31 -0500785
James Smart1b32f6a2008-02-08 18:49:39 -0500786 if (phba->pport->load_flag & FC_UNLOADING)
787 lpfc_cleanup_discovery_resources(phba->pport);
788 else {
789 vports = lpfc_create_vport_work_array(phba);
790 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400791 for (i = 0; i <= phba->max_vports &&
792 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500793 lpfc_cleanup_discovery_resources(vports[i]);
794 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500795 }
796 return 0;
dea31012005-04-17 16:05:31 -0500797}
798
James Smarte59058c2008-08-24 21:49:00 -0400799/**
James Smart3772a992009-05-22 14:50:54 -0400800 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400801 * @phba: pointer to lpfc HBA data structure.
802 *
803 * This routine will do uninitialization after the HBA is reset when bring
804 * down the SLI Layer.
805 *
806 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200807 * 0 - success.
James Smarte59058c2008-08-24 21:49:00 -0400808 * Any other value - error.
809 **/
James Smart3772a992009-05-22 14:50:54 -0400810static int
811lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -0500812{
813 struct lpfc_sli *psli = &phba->sli;
814 struct lpfc_sli_ring *pring;
815 struct lpfc_dmabuf *mp, *next_mp;
James Smart09372822008-01-11 01:52:54 -0500816 LIST_HEAD(completions);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500817 int i;
818
James Smart92d7f7b2007-06-17 19:56:38 -0500819 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
820 lpfc_sli_hbqbuf_free_all(phba);
821 else {
822 /* Cleanup preposted buffers on the ELS ring */
823 pring = &psli->ring[LPFC_ELS_RING];
824 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
825 list_del(&mp->list);
826 pring->postbufq_cnt--;
827 lpfc_mbuf_free(phba, mp->virt, mp->phys);
828 kfree(mp);
829 }
Jamie Wellnitz41415862006-02-28 19:25:27 -0500830 }
831
James Smart09372822008-01-11 01:52:54 -0500832 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500833 for (i = 0; i < psli->num_rings; i++) {
834 pring = &psli->ring[i];
James Smart09372822008-01-11 01:52:54 -0500835
836 /* At this point in time the HBA is either reset or DOA. Either
837 * way, nothing should be on txcmplq as it will NEVER complete.
838 */
839 list_splice_init(&pring->txcmplq, &completions);
840 pring->txcmplq_cnt = 0;
841 spin_unlock_irq(&phba->hbalock);
842
James Smarta257bf92009-04-06 18:48:10 -0400843 /* Cancel all the IOCBs from the completions list */
844 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
845 IOERR_SLI_ABORTED);
James Smart09372822008-01-11 01:52:54 -0500846
Jamie Wellnitz41415862006-02-28 19:25:27 -0500847 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart09372822008-01-11 01:52:54 -0500848 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500849 }
James Smart09372822008-01-11 01:52:54 -0500850 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500851
852 return 0;
853}
James Smart5af5eee2010-10-22 11:06:38 -0400854
James Smartda0436e2009-05-22 14:51:39 -0400855/**
856 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
857 * @phba: pointer to lpfc HBA data structure.
858 *
859 * This routine will do uninitialization after the HBA is reset when bring
860 * down the SLI Layer.
861 *
862 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200863 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400864 * Any other value - error.
865 **/
866static int
867lpfc_hba_down_post_s4(struct lpfc_hba *phba)
868{
869 struct lpfc_scsi_buf *psb, *psb_next;
870 LIST_HEAD(aborts);
871 int ret;
872 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500873 struct lpfc_sglq *sglq_entry = NULL;
874
James Smartda0436e2009-05-22 14:51:39 -0400875 ret = lpfc_hba_down_post_s3(phba);
876 if (ret)
877 return ret;
878 /* At this point in time the HBA is either reset or DOA. Either
879 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
880 * on the lpfc_sgl_list so that it can either be freed if the
881 * driver is unloading or reposted if the driver is restarting
882 * the port.
883 */
884 spin_lock_irq(&phba->hbalock); /* required for lpfc_sgl_list and */
885 /* scsl_buf_list */
886 /* abts_sgl_list_lock required because worker thread uses this
887 * list.
888 */
889 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -0500890 list_for_each_entry(sglq_entry,
891 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
892 sglq_entry->state = SGL_FREED;
893
James Smartda0436e2009-05-22 14:51:39 -0400894 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
895 &phba->sli4_hba.lpfc_sgl_list);
896 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
897 /* abts_scsi_buf_list_lock required because worker thread uses this
898 * list.
899 */
900 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
901 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
902 &aborts);
903 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
904 spin_unlock_irq(&phba->hbalock);
905
906 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
907 psb->pCmd = NULL;
908 psb->status = IOSTAT_SUCCESS;
909 }
910 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
911 list_splice(&aborts, &phba->lpfc_scsi_buf_list);
912 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
913 return 0;
914}
915
916/**
917 * lpfc_hba_down_post - Wrapper func for hba down post routine
918 * @phba: pointer to lpfc HBA data structure.
919 *
920 * This routine wraps the actual SLI3 or SLI4 routine for performing
921 * uninitialization after the HBA is reset when bring down the SLI Layer.
922 *
923 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200924 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400925 * Any other value - error.
926 **/
927int
928lpfc_hba_down_post(struct lpfc_hba *phba)
929{
930 return (*phba->lpfc_hba_down_post)(phba);
931}
Jamie Wellnitz41415862006-02-28 19:25:27 -0500932
James Smarte59058c2008-08-24 21:49:00 -0400933/**
James Smart3621a712009-04-06 18:47:14 -0400934 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -0400935 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
936 *
937 * This is the HBA-timer timeout handler registered to the lpfc driver. When
938 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
939 * work-port-events bitmap and the worker thread is notified. This timeout
940 * event will be used by the worker thread to invoke the actual timeout
941 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
942 * be performed in the timeout handler and the HBA timeout event bit shall
943 * be cleared by the worker thread after it has taken the event bitmap out.
944 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100945static void
James Smart858c9f62007-06-17 19:56:39 -0500946lpfc_hb_timeout(unsigned long ptr)
947{
948 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400949 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -0500950 unsigned long iflag;
951
952 phba = (struct lpfc_hba *)ptr;
James Smart93996272008-08-24 21:50:30 -0400953
954 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -0500955 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -0400956 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
957 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -0500958 phba->pport->work_port_events |= WORKER_HB_TMO;
959 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
960
James Smart93996272008-08-24 21:50:30 -0400961 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -0400962 if (!tmo_posted)
963 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -0500964 return;
965}
966
James Smarte59058c2008-08-24 21:49:00 -0400967/**
James Smart19ca7602010-11-20 23:11:55 -0500968 * lpfc_rrq_timeout - The RRQ-timer timeout handler
969 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
970 *
971 * This is the RRQ-timer timeout handler registered to the lpfc driver. When
972 * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
973 * work-port-events bitmap and the worker thread is notified. This timeout
974 * event will be used by the worker thread to invoke the actual timeout
975 * handler routine, lpfc_rrq_handler. Any periodical operations will
976 * be performed in the timeout handler and the RRQ timeout event bit shall
977 * be cleared by the worker thread after it has taken the event bitmap out.
978 **/
979static void
980lpfc_rrq_timeout(unsigned long ptr)
981{
982 struct lpfc_hba *phba;
James Smart19ca7602010-11-20 23:11:55 -0500983 unsigned long iflag;
984
985 phba = (struct lpfc_hba *)ptr;
986 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -0500987 phba->hba_flag |= HBA_RRQ_ACTIVE;
James Smart19ca7602010-11-20 23:11:55 -0500988 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -0500989 lpfc_worker_wake_up(phba);
James Smart19ca7602010-11-20 23:11:55 -0500990}
991
992/**
James Smart3621a712009-04-06 18:47:14 -0400993 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -0400994 * @phba: pointer to lpfc hba data structure.
995 * @pmboxq: pointer to the driver internal queue element for mailbox command.
996 *
997 * This is the callback function to the lpfc heart-beat mailbox command.
998 * If configured, the lpfc driver issues the heart-beat mailbox command to
999 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
1000 * heart-beat mailbox command is issued, the driver shall set up heart-beat
1001 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
1002 * heart-beat outstanding state. Once the mailbox command comes back and
1003 * no error conditions detected, the heart-beat mailbox command timer is
1004 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1005 * state is cleared for the next heart-beat. If the timer expired with the
1006 * heart-beat outstanding state set, the driver will put the HBA offline.
1007 **/
James Smart858c9f62007-06-17 19:56:39 -05001008static void
1009lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1010{
1011 unsigned long drvr_flag;
1012
1013 spin_lock_irqsave(&phba->hbalock, drvr_flag);
1014 phba->hb_outstanding = 0;
1015 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1016
James Smart93996272008-08-24 21:50:30 -04001017 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -05001018 mempool_free(pmboxq, phba->mbox_mem_pool);
1019 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1020 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -04001021 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -05001022 mod_timer(&phba->hb_tmofunc,
1023 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1024 return;
1025}
1026
James Smarte59058c2008-08-24 21:49:00 -04001027/**
James Smart3621a712009-04-06 18:47:14 -04001028 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001029 * @phba: pointer to lpfc hba data structure.
1030 *
1031 * This is the actual HBA-timer timeout handler to be invoked by the worker
1032 * thread whenever the HBA timer fired and HBA-timeout event posted. This
1033 * handler performs any periodic operations needed for the device. If such
1034 * periodic event has already been attended to either in the interrupt handler
1035 * or by processing slow-ring or fast-ring events within the HBA-timer
1036 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1037 * the timer for the next timeout period. If lpfc heart-beat mailbox command
1038 * is configured and there is no heart-beat mailbox command outstanding, a
1039 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1040 * has been a heart-beat mailbox command outstanding, the HBA shall be put
1041 * to offline.
1042 **/
James Smart858c9f62007-06-17 19:56:39 -05001043void
1044lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1045{
James Smart45ed1192009-10-02 15:17:02 -04001046 struct lpfc_vport **vports;
James Smart858c9f62007-06-17 19:56:39 -05001047 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -05001048 struct lpfc_dmabuf *buf_ptr;
James Smart45ed1192009-10-02 15:17:02 -04001049 int retval, i;
James Smart858c9f62007-06-17 19:56:39 -05001050 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -05001051 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -05001052
James Smart45ed1192009-10-02 15:17:02 -04001053 vports = lpfc_create_vport_work_array(phba);
1054 if (vports != NULL)
1055 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
1056 lpfc_rcv_seq_check_edtov(vports[i]);
1057 lpfc_destroy_vport_work_array(phba, vports);
1058
James Smart858c9f62007-06-17 19:56:39 -05001059 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -04001060 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -05001061 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1062 return;
1063
1064 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -05001065
1066 if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
1067 jiffies)) {
1068 spin_unlock_irq(&phba->pport->work_port_lock);
1069 if (!phba->hb_outstanding)
1070 mod_timer(&phba->hb_tmofunc,
1071 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1072 else
1073 mod_timer(&phba->hb_tmofunc,
1074 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
1075 return;
1076 }
1077 spin_unlock_irq(&phba->pport->work_port_lock);
1078
James Smart0ff10d42008-01-11 01:52:36 -05001079 if (phba->elsbuf_cnt &&
1080 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1081 spin_lock_irq(&phba->hbalock);
1082 list_splice_init(&phba->elsbuf, &completions);
1083 phba->elsbuf_cnt = 0;
1084 phba->elsbuf_prev_cnt = 0;
1085 spin_unlock_irq(&phba->hbalock);
1086
1087 while (!list_empty(&completions)) {
1088 list_remove_head(&completions, buf_ptr,
1089 struct lpfc_dmabuf, list);
1090 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1091 kfree(buf_ptr);
1092 }
1093 }
1094 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1095
James Smart858c9f62007-06-17 19:56:39 -05001096 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -05001097 if (phba->cfg_enable_hba_heartbeat) {
1098 if (!phba->hb_outstanding) {
James Smartbc739052010-08-04 16:11:18 -04001099 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1100 (list_empty(&psli->mboxq))) {
1101 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1102 GFP_KERNEL);
1103 if (!pmboxq) {
1104 mod_timer(&phba->hb_tmofunc,
1105 jiffies +
1106 HZ * LPFC_HB_MBOX_INTERVAL);
1107 return;
1108 }
James Smart13815c82008-01-11 01:52:48 -05001109
James Smartbc739052010-08-04 16:11:18 -04001110 lpfc_heart_beat(phba, pmboxq);
1111 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1112 pmboxq->vport = phba->pport;
1113 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1114 MBX_NOWAIT);
James Smart13815c82008-01-11 01:52:48 -05001115
James Smartbc739052010-08-04 16:11:18 -04001116 if (retval != MBX_BUSY &&
1117 retval != MBX_SUCCESS) {
1118 mempool_free(pmboxq,
1119 phba->mbox_mem_pool);
1120 mod_timer(&phba->hb_tmofunc,
1121 jiffies +
1122 HZ * LPFC_HB_MBOX_INTERVAL);
1123 return;
1124 }
1125 phba->skipped_hb = 0;
1126 phba->hb_outstanding = 1;
1127 } else if (time_before_eq(phba->last_completion_time,
1128 phba->skipped_hb)) {
1129 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1130 "2857 Last completion time not "
1131 " updated in %d ms\n",
1132 jiffies_to_msecs(jiffies
1133 - phba->last_completion_time));
1134 } else
1135 phba->skipped_hb = jiffies;
1136
James Smart858c9f62007-06-17 19:56:39 -05001137 mod_timer(&phba->hb_tmofunc,
James Smart13815c82008-01-11 01:52:48 -05001138 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
James Smart858c9f62007-06-17 19:56:39 -05001139 return;
James Smart13815c82008-01-11 01:52:48 -05001140 } else {
1141 /*
1142 * If heart beat timeout called with hb_outstanding set
James Smartdcf2a4e2010-09-29 11:18:53 -04001143 * we need to give the hb mailbox cmd a chance to
1144 * complete or TMO.
James Smart13815c82008-01-11 01:52:48 -05001145 */
James Smartdcf2a4e2010-09-29 11:18:53 -04001146 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1147 "0459 Adapter heartbeat still out"
1148 "standing:last compl time was %d ms.\n",
1149 jiffies_to_msecs(jiffies
1150 - phba->last_completion_time));
1151 mod_timer(&phba->hb_tmofunc,
1152 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
James Smart858c9f62007-06-17 19:56:39 -05001153 }
James Smart858c9f62007-06-17 19:56:39 -05001154 }
1155}
1156
James Smarte59058c2008-08-24 21:49:00 -04001157/**
James Smart3621a712009-04-06 18:47:14 -04001158 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -04001159 * @phba: pointer to lpfc hba data structure.
1160 *
1161 * This routine is called to bring the HBA offline when HBA hardware error
1162 * other than Port Error 6 has been detected.
1163 **/
James Smart09372822008-01-11 01:52:54 -05001164static void
1165lpfc_offline_eratt(struct lpfc_hba *phba)
1166{
1167 struct lpfc_sli *psli = &phba->sli;
1168
1169 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001170 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -05001171 spin_unlock_irq(&phba->hbalock);
1172 lpfc_offline_prep(phba);
1173
1174 lpfc_offline(phba);
1175 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001176 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001177 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001178 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001179 lpfc_hba_down_post(phba);
1180 lpfc_sli_brdready(phba, HS_MBRDY);
1181 lpfc_unblock_mgmt_io(phba);
1182 phba->link_state = LPFC_HBA_ERROR;
1183 return;
1184}
1185
James Smarte59058c2008-08-24 21:49:00 -04001186/**
James Smartda0436e2009-05-22 14:51:39 -04001187 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1188 * @phba: pointer to lpfc hba data structure.
1189 *
1190 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1191 * other than Port Error 6 has been detected.
1192 **/
1193static void
1194lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1195{
1196 lpfc_offline_prep(phba);
1197 lpfc_offline(phba);
1198 lpfc_sli4_brdreset(phba);
1199 lpfc_hba_down_post(phba);
1200 lpfc_sli4_post_status_check(phba);
1201 lpfc_unblock_mgmt_io(phba);
1202 phba->link_state = LPFC_HBA_ERROR;
1203}
1204
1205/**
James Smarta257bf92009-04-06 18:48:10 -04001206 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1207 * @phba: pointer to lpfc hba data structure.
1208 *
1209 * This routine is invoked to handle the deferred HBA hardware error
1210 * conditions. This type of error is indicated by HBA by setting ER1
1211 * and another ER bit in the host status register. The driver will
1212 * wait until the ER1 bit clears before handling the error condition.
1213 **/
1214static void
1215lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1216{
1217 uint32_t old_host_status = phba->work_hs;
1218 struct lpfc_sli_ring *pring;
1219 struct lpfc_sli *psli = &phba->sli;
1220
James Smartf4b4c682009-05-22 14:53:12 -04001221 /* If the pci channel is offline, ignore possible errors,
1222 * since we cannot communicate with the pci card anyway.
1223 */
1224 if (pci_channel_offline(phba->pcidev)) {
1225 spin_lock_irq(&phba->hbalock);
1226 phba->hba_flag &= ~DEFER_ERATT;
1227 spin_unlock_irq(&phba->hbalock);
1228 return;
1229 }
1230
James Smarta257bf92009-04-06 18:48:10 -04001231 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1232 "0479 Deferred Adapter Hardware Error "
1233 "Data: x%x x%x x%x\n",
1234 phba->work_hs,
1235 phba->work_status[0], phba->work_status[1]);
1236
1237 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001238 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001239 spin_unlock_irq(&phba->hbalock);
1240
1241
1242 /*
1243 * Firmware stops when it triggred erratt. That could cause the I/Os
1244 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1245 * SCSI layer retry it after re-establishing link.
1246 */
1247 pring = &psli->ring[psli->fcp_ring];
1248 lpfc_sli_abort_iocb_ring(phba, pring);
1249
1250 /*
1251 * There was a firmware error. Take the hba offline and then
1252 * attempt to restart it.
1253 */
1254 lpfc_offline_prep(phba);
1255 lpfc_offline(phba);
1256
1257 /* Wait for the ER1 bit to clear.*/
1258 while (phba->work_hs & HS_FFER1) {
1259 msleep(100);
James Smart9940b972011-03-11 16:06:12 -05001260 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1261 phba->work_hs = UNPLUG_ERR ;
1262 break;
1263 }
James Smarta257bf92009-04-06 18:48:10 -04001264 /* If driver is unloading let the worker thread continue */
1265 if (phba->pport->load_flag & FC_UNLOADING) {
1266 phba->work_hs = 0;
1267 break;
1268 }
1269 }
1270
1271 /*
1272 * This is to ptrotect against a race condition in which
1273 * first write to the host attention register clear the
1274 * host status register.
1275 */
1276 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1277 phba->work_hs = old_host_status & ~HS_FFER1;
1278
James Smart3772a992009-05-22 14:50:54 -04001279 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001280 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001281 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001282 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1283 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1284}
1285
James Smart3772a992009-05-22 14:50:54 -04001286static void
1287lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1288{
1289 struct lpfc_board_event_header board_event;
1290 struct Scsi_Host *shost;
1291
1292 board_event.event_type = FC_REG_BOARD_EVENT;
1293 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1294 shost = lpfc_shost_from_vport(phba->pport);
1295 fc_host_post_vendor_event(shost, fc_get_event_number(),
1296 sizeof(board_event),
1297 (char *) &board_event,
1298 LPFC_NL_VENDOR_ID);
1299}
1300
James Smarta257bf92009-04-06 18:48:10 -04001301/**
James Smart3772a992009-05-22 14:50:54 -04001302 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001303 * @phba: pointer to lpfc hba data structure.
1304 *
1305 * This routine is invoked to handle the following HBA hardware error
1306 * conditions:
1307 * 1 - HBA error attention interrupt
1308 * 2 - DMA ring index out of range
1309 * 3 - Mailbox command came back as unknown
1310 **/
James Smart3772a992009-05-22 14:50:54 -04001311static void
1312lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001313{
James Smart2e0fef82007-06-17 19:56:36 -05001314 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001315 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001316 struct lpfc_sli_ring *pring;
James Smartd2873e42006-08-18 17:46:43 -04001317 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001318 unsigned long temperature;
1319 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001320 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001321
Linas Vepstas8d63f372007-02-14 14:28:36 -06001322 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001323 * since we cannot communicate with the pci card anyway.
1324 */
1325 if (pci_channel_offline(phba->pcidev)) {
1326 spin_lock_irq(&phba->hbalock);
1327 phba->hba_flag &= ~DEFER_ERATT;
1328 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001329 return;
James Smart3772a992009-05-22 14:50:54 -04001330 }
1331
James Smart13815c82008-01-11 01:52:48 -05001332 /* If resets are disabled then leave the HBA alone and return */
1333 if (!phba->cfg_enable_hba_reset)
1334 return;
dea31012005-04-17 16:05:31 -05001335
James Smartea2151b2008-09-07 11:52:10 -04001336 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001337 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001338
James Smarta257bf92009-04-06 18:48:10 -04001339 if (phba->hba_flag & DEFER_ERATT)
1340 lpfc_handle_deferred_eratt(phba);
1341
James Smartdcf2a4e2010-09-29 11:18:53 -04001342 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1343 if (phba->work_hs & HS_FFER6)
1344 /* Re-establishing Link */
1345 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1346 "1301 Re-establishing Link "
1347 "Data: x%x x%x x%x\n",
1348 phba->work_hs, phba->work_status[0],
1349 phba->work_status[1]);
1350 if (phba->work_hs & HS_FFER8)
1351 /* Device Zeroization */
1352 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1353 "2861 Host Authentication device "
1354 "zeroization Data:x%x x%x x%x\n",
1355 phba->work_hs, phba->work_status[0],
1356 phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001357
James Smart92d7f7b2007-06-17 19:56:38 -05001358 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001359 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001360 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001361
1362 /*
1363 * Firmware stops when it triggled erratt with HS_FFER6.
1364 * That could cause the I/Os dropped by the firmware.
1365 * Error iocb (I/O) on txcmplq and let the SCSI layer
1366 * retry it after re-establishing link.
1367 */
1368 pring = &psli->ring[psli->fcp_ring];
1369 lpfc_sli_abort_iocb_ring(phba, pring);
1370
dea31012005-04-17 16:05:31 -05001371 /*
1372 * There was a firmware error. Take the hba offline and then
1373 * attempt to restart it.
1374 */
James Smart46fa3112007-04-25 09:51:45 -04001375 lpfc_offline_prep(phba);
dea31012005-04-17 16:05:31 -05001376 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001377 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001378 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001379 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001380 return;
1381 }
James Smart46fa3112007-04-25 09:51:45 -04001382 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001383 } else if (phba->work_hs & HS_CRIT_TEMP) {
1384 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1385 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1386 temp_event_data.event_code = LPFC_CRIT_TEMP;
1387 temp_event_data.data = (uint32_t)temperature;
1388
1389 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001390 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001391 "(%ld), taking this port offline "
1392 "Data: x%x x%x x%x\n",
1393 temperature, phba->work_hs,
1394 phba->work_status[0], phba->work_status[1]);
1395
1396 shost = lpfc_shost_from_vport(phba->pport);
1397 fc_host_post_vendor_event(shost, fc_get_event_number(),
1398 sizeof(temp_event_data),
1399 (char *) &temp_event_data,
1400 SCSI_NL_VID_TYPE_PCI
1401 | PCI_VENDOR_ID_EMULEX);
1402
James Smart7af67052007-10-27 13:38:11 -04001403 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001404 phba->over_temp_state = HBA_OVER_TEMP;
1405 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001406 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001407
dea31012005-04-17 16:05:31 -05001408 } else {
1409 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001410 * failure is a value other than FFER6. Do not call the offline
1411 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001412 */
1413 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001414 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001415 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001416 phba->work_hs,
dea31012005-04-17 16:05:31 -05001417 phba->work_status[0], phba->work_status[1]);
1418
James Smartd2873e42006-08-18 17:46:43 -04001419 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001420 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001421 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001422 sizeof(event_data), (char *) &event_data,
1423 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1424
James Smart09372822008-01-11 01:52:54 -05001425 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001426 }
James Smart93996272008-08-24 21:50:30 -04001427 return;
dea31012005-04-17 16:05:31 -05001428}
1429
James Smarte59058c2008-08-24 21:49:00 -04001430/**
James Smartda0436e2009-05-22 14:51:39 -04001431 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1432 * @phba: pointer to lpfc hba data structure.
1433 *
1434 * This routine is invoked to handle the SLI4 HBA hardware error attention
1435 * conditions.
1436 **/
1437static void
1438lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1439{
1440 struct lpfc_vport *vport = phba->pport;
1441 uint32_t event_data;
1442 struct Scsi_Host *shost;
James Smart2fcee4b2010-12-15 17:57:46 -05001443 uint32_t if_type;
James Smart2e90f4b2011-12-13 13:22:37 -05001444 struct lpfc_register portstat_reg = {0};
1445 uint32_t reg_err1, reg_err2;
1446 uint32_t uerrlo_reg, uemasklo_reg;
1447 uint32_t pci_rd_rc1, pci_rd_rc2;
James Smart73d91e52011-10-10 21:32:10 -04001448 int rc;
James Smartda0436e2009-05-22 14:51:39 -04001449
1450 /* If the pci channel is offline, ignore possible errors, since
1451 * we cannot communicate with the pci card anyway.
1452 */
1453 if (pci_channel_offline(phba->pcidev))
1454 return;
1455 /* If resets are disabled then leave the HBA alone and return */
1456 if (!phba->cfg_enable_hba_reset)
1457 return;
1458
James Smart2fcee4b2010-12-15 17:57:46 -05001459 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1460 switch (if_type) {
1461 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart2e90f4b2011-12-13 13:22:37 -05001462 pci_rd_rc1 = lpfc_readl(
1463 phba->sli4_hba.u.if_type0.UERRLOregaddr,
1464 &uerrlo_reg);
1465 pci_rd_rc2 = lpfc_readl(
1466 phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
1467 &uemasklo_reg);
1468 /* consider PCI bus read error as pci_channel_offline */
1469 if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
1470 return;
James Smart2fcee4b2010-12-15 17:57:46 -05001471 lpfc_sli4_offline_eratt(phba);
1472 break;
1473 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart2e90f4b2011-12-13 13:22:37 -05001474 pci_rd_rc1 = lpfc_readl(
1475 phba->sli4_hba.u.if_type2.STATUSregaddr,
1476 &portstat_reg.word0);
1477 /* consider PCI bus read error as pci_channel_offline */
James Smart6b5151f2012-01-18 16:24:06 -05001478 if (pci_rd_rc1 == -EIO) {
1479 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1480 "3151 PCI bus read access failure: x%x\n",
1481 readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
James Smart2e90f4b2011-12-13 13:22:37 -05001482 return;
James Smart6b5151f2012-01-18 16:24:06 -05001483 }
James Smart2e90f4b2011-12-13 13:22:37 -05001484 reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
1485 reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
James Smart2fcee4b2010-12-15 17:57:46 -05001486 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
1487 /* TODO: Register for Overtemp async events. */
1488 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1489 "2889 Port Overtemperature event, "
James Smart026abb82011-12-13 13:20:45 -05001490 "taking port offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001491 spin_lock_irq(&phba->hbalock);
1492 phba->over_temp_state = HBA_OVER_TEMP;
1493 spin_unlock_irq(&phba->hbalock);
1494 lpfc_sli4_offline_eratt(phba);
James Smart2e90f4b2011-12-13 13:22:37 -05001495 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001496 }
James Smart2e90f4b2011-12-13 13:22:37 -05001497 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1498 reg_err2 == SLIPORT_ERR2_REG_FW_RESTART)
1499 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1500 "3143 Port Down: Firmware Restarted\n");
1501 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1502 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1503 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1504 "3144 Port Down: Debug Dump\n");
1505 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1506 reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
1507 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1508 "3145 Port Down: Provisioning\n");
James Smart73d91e52011-10-10 21:32:10 -04001509 /*
1510 * On error status condition, driver need to wait for port
1511 * ready before performing reset.
1512 */
1513 rc = lpfc_sli4_pdev_status_reg_wait(phba);
1514 if (!rc) {
James Smart079b5c92011-08-21 21:48:49 -04001515 /* need reset: attempt for port recovery */
James Smart2fcee4b2010-12-15 17:57:46 -05001516 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -05001517 "2887 Reset Needed: Attempting Port "
1518 "Recovery...\n");
James Smart079b5c92011-08-21 21:48:49 -04001519 lpfc_offline_prep(phba);
1520 lpfc_offline(phba);
1521 lpfc_sli_brdrestart(phba);
1522 if (lpfc_online(phba) == 0) {
1523 lpfc_unblock_mgmt_io(phba);
James Smart2e90f4b2011-12-13 13:22:37 -05001524 /* don't report event on forced debug dump */
1525 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1526 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1527 return;
1528 else
1529 break;
James Smart079b5c92011-08-21 21:48:49 -04001530 }
1531 /* fall through for not able to recover */
James Smart2fcee4b2010-12-15 17:57:46 -05001532 }
James Smart6b5151f2012-01-18 16:24:06 -05001533 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1534 "3152 Unrecoverable error, bring the port "
1535 "offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001536 lpfc_sli4_offline_eratt(phba);
1537 break;
1538 case LPFC_SLI_INTF_IF_TYPE_1:
1539 default:
1540 break;
1541 }
James Smart2e90f4b2011-12-13 13:22:37 -05001542 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1543 "3123 Report dump event to upper layer\n");
1544 /* Send an internal error event to mgmt application */
1545 lpfc_board_errevt_to_mgmt(phba);
1546
1547 event_data = FC_REG_DUMP_EVENT;
1548 shost = lpfc_shost_from_vport(vport);
1549 fc_host_post_vendor_event(shost, fc_get_event_number(),
1550 sizeof(event_data), (char *) &event_data,
1551 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
James Smartda0436e2009-05-22 14:51:39 -04001552}
1553
1554/**
1555 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1556 * @phba: pointer to lpfc HBA data structure.
1557 *
1558 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1559 * routine from the API jump table function pointer from the lpfc_hba struct.
1560 *
1561 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001562 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001563 * Any other value - error.
1564 **/
1565void
1566lpfc_handle_eratt(struct lpfc_hba *phba)
1567{
1568 (*phba->lpfc_handle_eratt)(phba);
1569}
1570
1571/**
James Smart3621a712009-04-06 18:47:14 -04001572 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04001573 * @phba: pointer to lpfc hba data structure.
1574 *
1575 * This routine is invoked from the worker thread to handle a HBA host
1576 * attention link event.
1577 **/
dea31012005-04-17 16:05:31 -05001578void
James Smart2e0fef82007-06-17 19:56:36 -05001579lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001580{
James Smart2e0fef82007-06-17 19:56:36 -05001581 struct lpfc_vport *vport = phba->pport;
1582 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001583 LPFC_MBOXQ_t *pmb;
1584 volatile uint32_t control;
1585 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05001586 int rc = 0;
dea31012005-04-17 16:05:31 -05001587
1588 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001589 if (!pmb) {
1590 rc = 1;
dea31012005-04-17 16:05:31 -05001591 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05001592 }
dea31012005-04-17 16:05:31 -05001593
1594 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001595 if (!mp) {
1596 rc = 2;
dea31012005-04-17 16:05:31 -05001597 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05001598 }
dea31012005-04-17 16:05:31 -05001599
1600 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05001601 if (!mp->virt) {
1602 rc = 3;
dea31012005-04-17 16:05:31 -05001603 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05001604 }
dea31012005-04-17 16:05:31 -05001605
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05001606 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04001607 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05001608
1609 psli->slistat.link_event++;
James Smart76a95d72010-11-20 23:11:48 -05001610 lpfc_read_topology(phba, pmb, mp);
1611 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smart2e0fef82007-06-17 19:56:36 -05001612 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04001613 /* Block ELS IOCBs until we have processed this mbox command */
1614 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04001615 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05001616 if (rc == MBX_NOT_FINISHED) {
1617 rc = 4;
James Smart14691152006-12-02 13:34:28 -05001618 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05001619 }
dea31012005-04-17 16:05:31 -05001620
1621 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05001622 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001623 writel(HA_LATT, phba->HAregaddr);
1624 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001625 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001626
1627 return;
1628
James Smart14691152006-12-02 13:34:28 -05001629lpfc_handle_latt_free_mbuf:
James Smart0d2b6b82008-06-14 22:52:47 -04001630 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05001631 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001632lpfc_handle_latt_free_mp:
1633 kfree(mp);
1634lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04001635 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001636lpfc_handle_latt_err_exit:
1637 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001638 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001639 psli->sli_flag |= LPFC_PROCESS_LA;
1640 control = readl(phba->HCregaddr);
1641 control |= HC_LAINT_ENA;
1642 writel(control, phba->HCregaddr);
1643 readl(phba->HCregaddr); /* flush */
1644
1645 /* Clear Link Attention in HA REG */
1646 writel(HA_LATT, phba->HAregaddr);
1647 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001648 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001649 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001650 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001651
James Smart09372822008-01-11 01:52:54 -05001652 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1653 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05001654
1655 return;
1656}
1657
James Smarte59058c2008-08-24 21:49:00 -04001658/**
James Smart3621a712009-04-06 18:47:14 -04001659 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04001660 * @phba: pointer to lpfc hba data structure.
1661 * @vpd: pointer to the vital product data.
1662 * @len: length of the vital product data in bytes.
1663 *
1664 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1665 * an array of characters. In this routine, the ModelName, ProgramType, and
1666 * ModelDesc, etc. fields of the phba data structure will be populated.
1667 *
1668 * Return codes
1669 * 0 - pointer to the VPD passed in is NULL
1670 * 1 - success
1671 **/
James Smart3772a992009-05-22 14:50:54 -04001672int
James Smart2e0fef82007-06-17 19:56:36 -05001673lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05001674{
1675 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05001676 int Length;
dea31012005-04-17 16:05:31 -05001677 int i, j;
1678 int finished = 0;
1679 int index = 0;
1680
1681 if (!vpd)
1682 return 0;
1683
1684 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05001685 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001686 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05001687 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1688 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001689 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05001690 switch (vpd[index]) {
1691 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001692 case 0x91:
dea31012005-04-17 16:05:31 -05001693 index += 1;
1694 lenlo = vpd[index];
1695 index += 1;
1696 lenhi = vpd[index];
1697 index += 1;
1698 i = ((((unsigned short)lenhi) << 8) + lenlo);
1699 index += i;
1700 break;
1701 case 0x90:
1702 index += 1;
1703 lenlo = vpd[index];
1704 index += 1;
1705 lenhi = vpd[index];
1706 index += 1;
1707 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001708 if (Length > len - index)
1709 Length = len - index;
dea31012005-04-17 16:05:31 -05001710 while (Length > 0) {
1711 /* Look for Serial Number */
1712 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1713 index += 2;
1714 i = vpd[index];
1715 index += 1;
1716 j = 0;
1717 Length -= (3+i);
1718 while(i--) {
1719 phba->SerialNumber[j++] = vpd[index++];
1720 if (j == 31)
1721 break;
1722 }
1723 phba->SerialNumber[j] = 0;
1724 continue;
1725 }
1726 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1727 phba->vpd_flag |= VPD_MODEL_DESC;
1728 index += 2;
1729 i = vpd[index];
1730 index += 1;
1731 j = 0;
1732 Length -= (3+i);
1733 while(i--) {
1734 phba->ModelDesc[j++] = vpd[index++];
1735 if (j == 255)
1736 break;
1737 }
1738 phba->ModelDesc[j] = 0;
1739 continue;
1740 }
1741 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1742 phba->vpd_flag |= VPD_MODEL_NAME;
1743 index += 2;
1744 i = vpd[index];
1745 index += 1;
1746 j = 0;
1747 Length -= (3+i);
1748 while(i--) {
1749 phba->ModelName[j++] = vpd[index++];
1750 if (j == 79)
1751 break;
1752 }
1753 phba->ModelName[j] = 0;
1754 continue;
1755 }
1756 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1757 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1758 index += 2;
1759 i = vpd[index];
1760 index += 1;
1761 j = 0;
1762 Length -= (3+i);
1763 while(i--) {
1764 phba->ProgramType[j++] = vpd[index++];
1765 if (j == 255)
1766 break;
1767 }
1768 phba->ProgramType[j] = 0;
1769 continue;
1770 }
1771 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1772 phba->vpd_flag |= VPD_PORT;
1773 index += 2;
1774 i = vpd[index];
1775 index += 1;
1776 j = 0;
1777 Length -= (3+i);
1778 while(i--) {
James Smartcd1c8302011-10-10 21:33:25 -04001779 if ((phba->sli_rev == LPFC_SLI_REV4) &&
1780 (phba->sli4_hba.pport_name_sta ==
1781 LPFC_SLI4_PPNAME_GET)) {
1782 j++;
1783 index++;
1784 } else
1785 phba->Port[j++] = vpd[index++];
1786 if (j == 19)
1787 break;
dea31012005-04-17 16:05:31 -05001788 }
James Smartcd1c8302011-10-10 21:33:25 -04001789 if ((phba->sli_rev != LPFC_SLI_REV4) ||
1790 (phba->sli4_hba.pport_name_sta ==
1791 LPFC_SLI4_PPNAME_NON))
1792 phba->Port[j] = 0;
dea31012005-04-17 16:05:31 -05001793 continue;
1794 }
1795 else {
1796 index += 2;
1797 i = vpd[index];
1798 index += 1;
1799 index += i;
1800 Length -= (3 + i);
1801 }
1802 }
1803 finished = 0;
1804 break;
1805 case 0x78:
1806 finished = 1;
1807 break;
1808 default:
1809 index ++;
1810 break;
1811 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001812 }
dea31012005-04-17 16:05:31 -05001813
1814 return(1);
1815}
1816
James Smarte59058c2008-08-24 21:49:00 -04001817/**
James Smart3621a712009-04-06 18:47:14 -04001818 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04001819 * @phba: pointer to lpfc hba data structure.
1820 * @mdp: pointer to the data structure to hold the derived model name.
1821 * @descp: pointer to the data structure to hold the derived description.
1822 *
1823 * This routine retrieves HBA's description based on its registered PCI device
1824 * ID. The @descp passed into this function points to an array of 256 chars. It
1825 * shall be returned with the model name, maximum speed, and the host bus type.
1826 * The @mdp passed into this function points to an array of 80 chars. When the
1827 * function returns, the @mdp will be filled with the model name.
1828 **/
dea31012005-04-17 16:05:31 -05001829static void
James Smart2e0fef82007-06-17 19:56:36 -05001830lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05001831{
1832 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05001833 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001834 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04001835 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04001836 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001837 struct {
James Smarta747c9c2009-11-18 15:41:10 -05001838 char *name;
1839 char *bus;
1840 char *function;
1841 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001842
1843 if (mdp && mdp[0] != '\0'
1844 && descp && descp[0] != '\0')
1845 return;
1846
James Smartc0c11512011-05-24 11:41:34 -04001847 if (phba->lmt & LMT_16Gb)
1848 max_speed = 16;
1849 else if (phba->lmt & LMT_10Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001850 max_speed = 10;
1851 else if (phba->lmt & LMT_8Gb)
1852 max_speed = 8;
1853 else if (phba->lmt & LMT_4Gb)
1854 max_speed = 4;
1855 else if (phba->lmt & LMT_2Gb)
1856 max_speed = 2;
1857 else
1858 max_speed = 1;
dea31012005-04-17 16:05:31 -05001859
1860 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05001861
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001862 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001863 case PCI_DEVICE_ID_FIREFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001864 m = (typeof(m)){"LP6000", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001865 break;
dea31012005-04-17 16:05:31 -05001866 case PCI_DEVICE_ID_SUPERFLY:
1867 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smarta747c9c2009-11-18 15:41:10 -05001868 m = (typeof(m)){"LP7000", "PCI",
1869 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001870 else
James Smarta747c9c2009-11-18 15:41:10 -05001871 m = (typeof(m)){"LP7000E", "PCI",
1872 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001873 break;
1874 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001875 m = (typeof(m)){"LP8000", "PCI",
1876 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001877 break;
1878 case PCI_DEVICE_ID_CENTAUR:
1879 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smarta747c9c2009-11-18 15:41:10 -05001880 m = (typeof(m)){"LP9002", "PCI",
1881 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001882 else
James Smarta747c9c2009-11-18 15:41:10 -05001883 m = (typeof(m)){"LP9000", "PCI",
1884 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001885 break;
1886 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001887 m = (typeof(m)){"LP952", "PCI",
1888 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001889 break;
1890 case PCI_DEVICE_ID_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05001891 m = (typeof(m)){"LP9802", "PCI-X",
1892 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001893 break;
1894 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05001895 m = (typeof(m)){"LP10000", "PCI-X",
1896 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001897 break;
1898 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05001899 m = (typeof(m)){"LPX1000", "PCI-X",
1900 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001901 break;
1902 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001903 m = (typeof(m)){"LP982", "PCI-X",
1904 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001905 break;
1906 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001907 m = (typeof(m)){"LP1050", "PCI-X",
1908 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001909 break;
1910 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05001911 m = (typeof(m)){"LP11000", "PCI-X2",
1912 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001913 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001914 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001915 m = (typeof(m)){"LP11000-SP", "PCI-X2",
1916 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001917 break;
1918 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001919 m = (typeof(m)){"LP11002-SP", "PCI-X2",
1920 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001921 break;
1922 case PCI_DEVICE_ID_NEPTUNE:
James Smarta747c9c2009-11-18 15:41:10 -05001923 m = (typeof(m)){"LPe1000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001924 break;
1925 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001926 m = (typeof(m)){"LPe1000-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001927 break;
1928 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001929 m = (typeof(m)){"LPe1002-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001930 break;
dea31012005-04-17 16:05:31 -05001931 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05001932 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001933 break;
1934 case PCI_DEVICE_ID_BSMB:
James Smarta747c9c2009-11-18 15:41:10 -05001935 m = (typeof(m)){"LP111", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001936 break;
1937 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05001938 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001939 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001940 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001941 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001942 break;
1943 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001944 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04001945 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001946 break;
dea31012005-04-17 16:05:31 -05001947 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05001948 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001949 break;
1950 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05001951 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001952 break;
1953 case PCI_DEVICE_ID_LP101:
James Smarta747c9c2009-11-18 15:41:10 -05001954 m = (typeof(m)){"LP101", "PCI-X", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001955 break;
1956 case PCI_DEVICE_ID_LP10000S:
James Smarta747c9c2009-11-18 15:41:10 -05001957 m = (typeof(m)){"LP10000-S", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001958 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001959 case PCI_DEVICE_ID_LP11000S:
James Smarta747c9c2009-11-18 15:41:10 -05001960 m = (typeof(m)){"LP11000-S", "PCI-X2", "Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05001961 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001962 case PCI_DEVICE_ID_LPE11000S:
James Smarta747c9c2009-11-18 15:41:10 -05001963 m = (typeof(m)){"LPe11000-S", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05001964 break;
James Smartb87eab32007-04-25 09:53:28 -04001965 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05001966 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001967 break;
1968 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05001969 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001970 break;
1971 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05001972 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001973 break;
1974 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001975 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001976 break;
1977 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001978 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001979 break;
1980 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05001981 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001982 break;
James Smart84774a42008-08-24 21:50:06 -04001983 case PCI_DEVICE_ID_HORNET:
James Smarta747c9c2009-11-18 15:41:10 -05001984 m = (typeof(m)){"LP21000", "PCIe", "FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001985 GE = 1;
1986 break;
1987 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05001988 m = (typeof(m)){"LPev12000", "PCIe IOV",
1989 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001990 break;
1991 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05001992 m = (typeof(m)){"LPev12000", "PCIe IOV",
1993 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001994 break;
1995 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05001996 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
1997 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001998 break;
James Smartda0436e2009-05-22 14:51:39 -04001999 case PCI_DEVICE_ID_TIGERSHARK:
2000 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002001 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04002002 break;
James Smarta747c9c2009-11-18 15:41:10 -05002003 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04002004 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002005 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2006 break;
2007 case PCI_DEVICE_ID_FALCON:
2008 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2009 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04002010 break;
James Smart98fc5dd2010-06-07 15:24:29 -04002011 case PCI_DEVICE_ID_BALIUS:
2012 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
2013 "Fibre Channel Adapter"};
2014 break;
James Smart085c6472010-11-20 23:11:37 -05002015 case PCI_DEVICE_ID_LANCER_FC:
James Smartc0c11512011-05-24 11:41:34 -04002016 case PCI_DEVICE_ID_LANCER_FC_VF:
2017 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
James Smart085c6472010-11-20 23:11:37 -05002018 break;
2019 case PCI_DEVICE_ID_LANCER_FCOE:
James Smartc0c11512011-05-24 11:41:34 -04002020 case PCI_DEVICE_ID_LANCER_FCOE_VF:
James Smart085c6472010-11-20 23:11:37 -05002021 oneConnect = 1;
James Smart079b5c92011-08-21 21:48:49 -04002022 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
James Smart085c6472010-11-20 23:11:37 -05002023 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002024 default:
James Smarta747c9c2009-11-18 15:41:10 -05002025 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002026 break;
dea31012005-04-17 16:05:31 -05002027 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002028
2029 if (mdp && mdp[0] == '\0')
2030 snprintf(mdp, 79,"%s", m.name);
James Smartc0c11512011-05-24 11:41:34 -04002031 /*
2032 * oneConnect hba requires special processing, they are all initiators
James Smartda0436e2009-05-22 14:51:39 -04002033 * and we put the port number on the end
2034 */
2035 if (descp && descp[0] == '\0') {
2036 if (oneConnect)
2037 snprintf(descp, 255,
James Smarta747c9c2009-11-18 15:41:10 -05002038 "Emulex OneConnect %s, %s Initiator, Port %s",
2039 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04002040 phba->Port);
2041 else
2042 snprintf(descp, 255,
2043 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05002044 m.name, max_speed, (GE) ? "GE" : "Gb",
2045 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002046 }
dea31012005-04-17 16:05:31 -05002047}
2048
James Smarte59058c2008-08-24 21:49:00 -04002049/**
James Smart3621a712009-04-06 18:47:14 -04002050 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04002051 * @phba: pointer to lpfc hba data structure.
2052 * @pring: pointer to a IOCB ring.
2053 * @cnt: the number of IOCBs to be posted to the IOCB ring.
2054 *
2055 * This routine posts a given number of IOCBs with the associated DMA buffer
2056 * descriptors specified by the cnt argument to the given IOCB ring.
2057 *
2058 * Return codes
2059 * The number of IOCBs NOT able to be posted to the IOCB ring.
2060 **/
dea31012005-04-17 16:05:31 -05002061int
James Smart495a7142008-06-14 22:52:59 -04002062lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05002063{
2064 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002065 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05002066 struct lpfc_dmabuf *mp1, *mp2;
2067
2068 cnt += pring->missbufcnt;
2069
2070 /* While there are buffers to post */
2071 while (cnt > 0) {
2072 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002073 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002074 if (iocb == NULL) {
2075 pring->missbufcnt = cnt;
2076 return cnt;
2077 }
dea31012005-04-17 16:05:31 -05002078 icmd = &iocb->iocb;
2079
2080 /* 2 buffers can be posted per command */
2081 /* Allocate buffer to post */
2082 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2083 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04002084 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2085 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002086 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002087 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002088 pring->missbufcnt = cnt;
2089 return cnt;
2090 }
2091
2092 INIT_LIST_HEAD(&mp1->list);
2093 /* Allocate buffer to post */
2094 if (cnt > 1) {
2095 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2096 if (mp2)
2097 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2098 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04002099 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002100 kfree(mp2);
dea31012005-04-17 16:05:31 -05002101 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2102 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002103 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002104 pring->missbufcnt = cnt;
2105 return cnt;
2106 }
2107
2108 INIT_LIST_HEAD(&mp2->list);
2109 } else {
2110 mp2 = NULL;
2111 }
2112
2113 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2114 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2115 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2116 icmd->ulpBdeCount = 1;
2117 cnt--;
2118 if (mp2) {
2119 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2120 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2121 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2122 cnt--;
2123 icmd->ulpBdeCount = 2;
2124 }
2125
2126 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2127 icmd->ulpLe = 1;
2128
James Smart3772a992009-05-22 14:50:54 -04002129 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2130 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05002131 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2132 kfree(mp1);
2133 cnt++;
2134 if (mp2) {
2135 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2136 kfree(mp2);
2137 cnt++;
2138 }
James Bottomley604a3e32005-10-29 10:28:33 -05002139 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002140 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05002141 return cnt;
2142 }
dea31012005-04-17 16:05:31 -05002143 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05002144 if (mp2)
dea31012005-04-17 16:05:31 -05002145 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05002146 }
2147 pring->missbufcnt = 0;
2148 return 0;
2149}
2150
James Smarte59058c2008-08-24 21:49:00 -04002151/**
James Smart3621a712009-04-06 18:47:14 -04002152 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04002153 * @phba: pointer to lpfc hba data structure.
2154 *
2155 * This routine posts initial receive IOCB buffers to the ELS ring. The
2156 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
2157 * set to 64 IOCBs.
2158 *
2159 * Return codes
2160 * 0 - success (currently always success)
2161 **/
dea31012005-04-17 16:05:31 -05002162static int
James Smart2e0fef82007-06-17 19:56:36 -05002163lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002164{
2165 struct lpfc_sli *psli = &phba->sli;
2166
2167 /* Ring 0, ELS / CT buffers */
James Smart495a7142008-06-14 22:52:59 -04002168 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05002169 /* Ring 2 - FCP no buffers needed */
2170
2171 return 0;
2172}
2173
2174#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2175
James Smarte59058c2008-08-24 21:49:00 -04002176/**
James Smart3621a712009-04-06 18:47:14 -04002177 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04002178 * @HashResultPointer: pointer to an array as hash table.
2179 *
2180 * This routine sets up the initial values to the array of hash table entries
2181 * for the LC HBAs.
2182 **/
dea31012005-04-17 16:05:31 -05002183static void
2184lpfc_sha_init(uint32_t * HashResultPointer)
2185{
2186 HashResultPointer[0] = 0x67452301;
2187 HashResultPointer[1] = 0xEFCDAB89;
2188 HashResultPointer[2] = 0x98BADCFE;
2189 HashResultPointer[3] = 0x10325476;
2190 HashResultPointer[4] = 0xC3D2E1F0;
2191}
2192
James Smarte59058c2008-08-24 21:49:00 -04002193/**
James Smart3621a712009-04-06 18:47:14 -04002194 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04002195 * @HashResultPointer: pointer to an initial/result hash table.
2196 * @HashWorkingPointer: pointer to an working hash table.
2197 *
2198 * This routine iterates an initial hash table pointed by @HashResultPointer
2199 * with the values from the working hash table pointeed by @HashWorkingPointer.
2200 * The results are putting back to the initial hash table, returned through
2201 * the @HashResultPointer as the result hash table.
2202 **/
dea31012005-04-17 16:05:31 -05002203static void
2204lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2205{
2206 int t;
2207 uint32_t TEMP;
2208 uint32_t A, B, C, D, E;
2209 t = 16;
2210 do {
2211 HashWorkingPointer[t] =
2212 S(1,
2213 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2214 8] ^
2215 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2216 } while (++t <= 79);
2217 t = 0;
2218 A = HashResultPointer[0];
2219 B = HashResultPointer[1];
2220 C = HashResultPointer[2];
2221 D = HashResultPointer[3];
2222 E = HashResultPointer[4];
2223
2224 do {
2225 if (t < 20) {
2226 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2227 } else if (t < 40) {
2228 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2229 } else if (t < 60) {
2230 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2231 } else {
2232 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2233 }
2234 TEMP += S(5, A) + E + HashWorkingPointer[t];
2235 E = D;
2236 D = C;
2237 C = S(30, B);
2238 B = A;
2239 A = TEMP;
2240 } while (++t <= 79);
2241
2242 HashResultPointer[0] += A;
2243 HashResultPointer[1] += B;
2244 HashResultPointer[2] += C;
2245 HashResultPointer[3] += D;
2246 HashResultPointer[4] += E;
2247
2248}
2249
James Smarte59058c2008-08-24 21:49:00 -04002250/**
James Smart3621a712009-04-06 18:47:14 -04002251 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002252 * @RandomChallenge: pointer to the entry of host challenge random number array.
2253 * @HashWorking: pointer to the entry of the working hash array.
2254 *
2255 * This routine calculates the working hash array referred by @HashWorking
2256 * from the challenge random numbers associated with the host, referred by
2257 * @RandomChallenge. The result is put into the entry of the working hash
2258 * array and returned by reference through @HashWorking.
2259 **/
dea31012005-04-17 16:05:31 -05002260static void
2261lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2262{
2263 *HashWorking = (*RandomChallenge ^ *HashWorking);
2264}
2265
James Smarte59058c2008-08-24 21:49:00 -04002266/**
James Smart3621a712009-04-06 18:47:14 -04002267 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002268 * @phba: pointer to lpfc hba data structure.
2269 * @hbainit: pointer to an array of unsigned 32-bit integers.
2270 *
2271 * This routine performs the special handling for LC HBA initialization.
2272 **/
dea31012005-04-17 16:05:31 -05002273void
2274lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2275{
2276 int t;
2277 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002278 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002279
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002280 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002281 if (!HashWorking)
2282 return;
2283
dea31012005-04-17 16:05:31 -05002284 HashWorking[0] = HashWorking[78] = *pwwnn++;
2285 HashWorking[1] = HashWorking[79] = *pwwnn;
2286
2287 for (t = 0; t < 7; t++)
2288 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2289
2290 lpfc_sha_init(hbainit);
2291 lpfc_sha_iterate(hbainit, HashWorking);
2292 kfree(HashWorking);
2293}
2294
James Smarte59058c2008-08-24 21:49:00 -04002295/**
James Smart3621a712009-04-06 18:47:14 -04002296 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002297 * @vport: pointer to a virtual N_Port data structure.
2298 *
2299 * This routine performs the necessary cleanups before deleting the @vport.
2300 * It invokes the discovery state machine to perform necessary state
2301 * transitions and to release the ndlps associated with the @vport. Note,
2302 * the physical port is treated as @vport 0.
2303 **/
James Smart87af33f2007-10-27 13:37:43 -04002304void
James Smart2e0fef82007-06-17 19:56:36 -05002305lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002306{
James Smart87af33f2007-10-27 13:37:43 -04002307 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002308 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002309 int i = 0;
dea31012005-04-17 16:05:31 -05002310
James Smart87af33f2007-10-27 13:37:43 -04002311 if (phba->link_state > LPFC_LINK_DOWN)
2312 lpfc_port_link_failure(vport);
2313
2314 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002315 if (!NLP_CHK_NODE_ACT(ndlp)) {
2316 ndlp = lpfc_enable_node(vport, ndlp,
2317 NLP_STE_UNUSED_NODE);
2318 if (!ndlp)
2319 continue;
2320 spin_lock_irq(&phba->ndlp_lock);
2321 NLP_SET_FREE_REQ(ndlp);
2322 spin_unlock_irq(&phba->ndlp_lock);
2323 /* Trigger the release of the ndlp memory */
2324 lpfc_nlp_put(ndlp);
2325 continue;
2326 }
2327 spin_lock_irq(&phba->ndlp_lock);
2328 if (NLP_CHK_FREE_REQ(ndlp)) {
2329 /* The ndlp should not be in memory free mode already */
2330 spin_unlock_irq(&phba->ndlp_lock);
2331 continue;
2332 } else
2333 /* Indicate request for freeing ndlp memory */
2334 NLP_SET_FREE_REQ(ndlp);
2335 spin_unlock_irq(&phba->ndlp_lock);
2336
James Smart58da1ff2008-04-07 10:15:56 -04002337 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2338 ndlp->nlp_DID == Fabric_DID) {
2339 /* Just free up ndlp with Fabric_DID for vports */
2340 lpfc_nlp_put(ndlp);
2341 continue;
2342 }
2343
James Smarteff4a012012-01-18 16:25:25 -05002344 /* take care of nodes in unused state before the state
2345 * machine taking action.
2346 */
2347 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
2348 lpfc_nlp_put(ndlp);
2349 continue;
2350 }
2351
James Smart87af33f2007-10-27 13:37:43 -04002352 if (ndlp->nlp_type & NLP_FABRIC)
2353 lpfc_disc_state_machine(vport, ndlp, NULL,
2354 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002355
James Smart87af33f2007-10-27 13:37:43 -04002356 lpfc_disc_state_machine(vport, ndlp, NULL,
2357 NLP_EVT_DEVICE_RM);
2358 }
2359
James Smarta8adb832007-10-27 13:37:53 -04002360 /* At this point, ALL ndlp's should be gone
2361 * because of the previous NLP_EVT_DEVICE_RM.
2362 * Lets wait for this to happen, if needed.
2363 */
James Smart87af33f2007-10-27 13:37:43 -04002364 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002365 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002366 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002367 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002368 list_for_each_entry_safe(ndlp, next_ndlp,
2369 &vport->fc_nodes, nlp_listp) {
2370 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2371 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002372 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002373 "usgmap:x%x refcnt:%d\n",
2374 ndlp->nlp_DID, (void *)ndlp,
2375 ndlp->nlp_usg_map,
2376 atomic_read(
2377 &ndlp->kref.refcount));
2378 }
James Smarta8adb832007-10-27 13:37:53 -04002379 break;
James Smart87af33f2007-10-27 13:37:43 -04002380 }
James Smarta8adb832007-10-27 13:37:53 -04002381
2382 /* Wait for any activity on ndlps to settle */
2383 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002384 }
James Smart1151e3e2011-02-16 12:39:35 -05002385 lpfc_cleanup_vports_rrqs(vport, NULL);
dea31012005-04-17 16:05:31 -05002386}
2387
James Smarte59058c2008-08-24 21:49:00 -04002388/**
James Smart3621a712009-04-06 18:47:14 -04002389 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002390 * @vport: pointer to a virtual N_Port data structure.
2391 *
2392 * This routine stops all the timers associated with a @vport. This function
2393 * is invoked before disabling or deleting a @vport. Note that the physical
2394 * port is treated as @vport 0.
2395 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002396void
2397lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002398{
James Smart92d7f7b2007-06-17 19:56:38 -05002399 del_timer_sync(&vport->els_tmofunc);
2400 del_timer_sync(&vport->fc_fdmitmo);
James Smart92494142011-02-16 12:39:44 -05002401 del_timer_sync(&vport->delayed_disc_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002402 lpfc_can_disctmo(vport);
2403 return;
dea31012005-04-17 16:05:31 -05002404}
2405
James Smarte59058c2008-08-24 21:49:00 -04002406/**
James Smartecfd03c2010-02-12 14:41:27 -05002407 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2408 * @phba: pointer to lpfc hba data structure.
2409 *
2410 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2411 * caller of this routine should already hold the host lock.
2412 **/
2413void
2414__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2415{
James Smart5ac6b302010-10-22 11:05:36 -04002416 /* Clear pending FCF rediscovery wait flag */
2417 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2418
James Smartecfd03c2010-02-12 14:41:27 -05002419 /* Now, try to stop the timer */
2420 del_timer(&phba->fcf.redisc_wait);
2421}
2422
2423/**
2424 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2425 * @phba: pointer to lpfc hba data structure.
2426 *
2427 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2428 * checks whether the FCF rediscovery wait timer is pending with the host
2429 * lock held before proceeding with disabling the timer and clearing the
2430 * wait timer pendig flag.
2431 **/
2432void
2433lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2434{
2435 spin_lock_irq(&phba->hbalock);
2436 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2437 /* FCF rediscovery timer already fired or stopped */
2438 spin_unlock_irq(&phba->hbalock);
2439 return;
2440 }
2441 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart5ac6b302010-10-22 11:05:36 -04002442 /* Clear failover in progress flags */
2443 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05002444 spin_unlock_irq(&phba->hbalock);
2445}
2446
2447/**
James Smart3772a992009-05-22 14:50:54 -04002448 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002449 * @phba: pointer to lpfc hba data structure.
2450 *
2451 * This routine stops all the timers associated with a HBA. This function is
2452 * invoked before either putting a HBA offline or unloading the driver.
2453 **/
James Smart3772a992009-05-22 14:50:54 -04002454void
2455lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002456{
James Smart51ef4c22007-08-02 11:10:31 -04002457 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05002458 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002459 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002460 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002461 del_timer_sync(&phba->hb_tmofunc);
James Smart1151e3e2011-02-16 12:39:35 -05002462 if (phba->sli_rev == LPFC_SLI_REV4) {
2463 del_timer_sync(&phba->rrq_tmr);
2464 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2465 }
James Smart3772a992009-05-22 14:50:54 -04002466 phba->hb_outstanding = 0;
2467
2468 switch (phba->pci_dev_grp) {
2469 case LPFC_PCI_DEV_LP:
2470 /* Stop any LightPulse device specific driver timers */
2471 del_timer_sync(&phba->fcp_poll_timer);
2472 break;
2473 case LPFC_PCI_DEV_OC:
2474 /* Stop any OneConnect device sepcific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05002475 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04002476 break;
2477 default:
2478 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2479 "0297 Invalid device group (x%x)\n",
2480 phba->pci_dev_grp);
2481 break;
2482 }
James Smart2e0fef82007-06-17 19:56:36 -05002483 return;
dea31012005-04-17 16:05:31 -05002484}
2485
James Smarte59058c2008-08-24 21:49:00 -04002486/**
James Smart3621a712009-04-06 18:47:14 -04002487 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002488 * @phba: pointer to lpfc hba data structure.
2489 *
2490 * This routine marks a HBA's management interface as blocked. Once the HBA's
2491 * management interface is marked as blocked, all the user space access to
2492 * the HBA, whether they are from sysfs interface or libdfc interface will
2493 * all be blocked. The HBA is set to block the management interface when the
2494 * driver prepares the HBA interface for online or offline.
2495 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002496static void
2497lpfc_block_mgmt_io(struct lpfc_hba * phba)
2498{
2499 unsigned long iflag;
James Smart6e7288d2010-06-07 15:23:35 -04002500 uint8_t actcmd = MBX_HEARTBEAT;
2501 unsigned long timeout;
2502
James Smarta183a152011-10-10 21:32:43 -04002503 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
Adrian Bunka6ababd2007-11-05 18:07:33 +01002504 spin_lock_irqsave(&phba->hbalock, iflag);
2505 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
James Smarta183a152011-10-10 21:32:43 -04002506 if (phba->sli.mbox_active) {
James Smart6e7288d2010-06-07 15:23:35 -04002507 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
James Smarta183a152011-10-10 21:32:43 -04002508 /* Determine how long we might wait for the active mailbox
2509 * command to be gracefully completed by firmware.
2510 */
2511 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
2512 phba->sli.mbox_active) * 1000) + jiffies;
2513 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002514 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002515
James Smart6e7288d2010-06-07 15:23:35 -04002516 /* Wait for the outstnading mailbox command to complete */
2517 while (phba->sli.mbox_active) {
2518 /* Check active mailbox complete status every 2ms */
2519 msleep(2);
2520 if (time_after(jiffies, timeout)) {
2521 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2522 "2813 Mgmt IO is Blocked %x "
2523 "- mbox cmd %x still active\n",
2524 phba->sli.sli_flag, actcmd);
2525 break;
2526 }
2527 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002528}
2529
James Smarte59058c2008-08-24 21:49:00 -04002530/**
James Smart6b5151f2012-01-18 16:24:06 -05002531 * lpfc_sli4_node_prep - Assign RPIs for active nodes.
2532 * @phba: pointer to lpfc hba data structure.
2533 *
2534 * Allocate RPIs for all active remote nodes. This is needed whenever
2535 * an SLI4 adapter is reset and the driver is not unloading. Its purpose
2536 * is to fixup the temporary rpi assignments.
2537 **/
2538void
2539lpfc_sli4_node_prep(struct lpfc_hba *phba)
2540{
2541 struct lpfc_nodelist *ndlp, *next_ndlp;
2542 struct lpfc_vport **vports;
2543 int i;
2544
2545 if (phba->sli_rev != LPFC_SLI_REV4)
2546 return;
2547
2548 vports = lpfc_create_vport_work_array(phba);
2549 if (vports != NULL) {
2550 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2551 if (vports[i]->load_flag & FC_UNLOADING)
2552 continue;
2553
2554 list_for_each_entry_safe(ndlp, next_ndlp,
2555 &vports[i]->fc_nodes,
2556 nlp_listp) {
2557 if (NLP_CHK_NODE_ACT(ndlp))
2558 ndlp->nlp_rpi =
2559 lpfc_sli4_alloc_rpi(phba);
2560 }
2561 }
2562 }
2563 lpfc_destroy_vport_work_array(phba, vports);
2564}
2565
2566/**
James Smart3621a712009-04-06 18:47:14 -04002567 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04002568 * @phba: pointer to lpfc hba data structure.
2569 *
2570 * This routine initializes the HBA and brings a HBA online. During this
2571 * process, the management interface is blocked to prevent user space access
2572 * to the HBA interfering with the driver initialization.
2573 *
2574 * Return codes
2575 * 0 - successful
2576 * 1 - failed
2577 **/
dea31012005-04-17 16:05:31 -05002578int
James Smart2e0fef82007-06-17 19:56:36 -05002579lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002580{
Julia Lawall372bd282008-12-16 16:15:08 +01002581 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04002582 struct lpfc_vport **vports;
2583 int i;
James Smart2e0fef82007-06-17 19:56:36 -05002584
dea31012005-04-17 16:05:31 -05002585 if (!phba)
2586 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01002587 vport = phba->pport;
dea31012005-04-17 16:05:31 -05002588
James Smart2e0fef82007-06-17 19:56:36 -05002589 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05002590 return 0;
2591
James Smarted957682007-06-17 19:56:37 -05002592 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002593 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05002594
James Smart46fa3112007-04-25 09:51:45 -04002595 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002596
James Smart46fa3112007-04-25 09:51:45 -04002597 if (!lpfc_sli_queue_setup(phba)) {
2598 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002599 return 1;
James Smart46fa3112007-04-25 09:51:45 -04002600 }
2601
James Smartda0436e2009-05-22 14:51:39 -04002602 if (phba->sli_rev == LPFC_SLI_REV4) {
2603 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2604 lpfc_unblock_mgmt_io(phba);
2605 return 1;
2606 }
2607 } else {
2608 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2609 lpfc_unblock_mgmt_io(phba);
2610 return 1;
2611 }
James Smart46fa3112007-04-25 09:51:45 -04002612 }
dea31012005-04-17 16:05:31 -05002613
James Smart549e55c2007-08-02 11:09:51 -04002614 vports = lpfc_create_vport_work_array(phba);
2615 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002616 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002617 struct Scsi_Host *shost;
2618 shost = lpfc_shost_from_vport(vports[i]);
2619 spin_lock_irq(shost->host_lock);
2620 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2621 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2622 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart1c6834a2009-07-19 10:01:26 -04002623 if (phba->sli_rev == LPFC_SLI_REV4)
2624 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart549e55c2007-08-02 11:09:51 -04002625 spin_unlock_irq(shost->host_lock);
2626 }
James Smart09372822008-01-11 01:52:54 -05002627 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002628
James Smart46fa3112007-04-25 09:51:45 -04002629 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002630 return 0;
2631}
2632
James Smarte59058c2008-08-24 21:49:00 -04002633/**
James Smart3621a712009-04-06 18:47:14 -04002634 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04002635 * @phba: pointer to lpfc hba data structure.
2636 *
2637 * This routine marks a HBA's management interface as not blocked. Once the
2638 * HBA's management interface is marked as not blocked, all the user space
2639 * access to the HBA, whether they are from sysfs interface or libdfc
2640 * interface will be allowed. The HBA is set to block the management interface
2641 * when the driver prepares the HBA interface for online or offline and then
2642 * set to unblock the management interface afterwards.
2643 **/
James Smart46fa3112007-04-25 09:51:45 -04002644void
James Smart46fa3112007-04-25 09:51:45 -04002645lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2646{
2647 unsigned long iflag;
2648
James Smart2e0fef82007-06-17 19:56:36 -05002649 spin_lock_irqsave(&phba->hbalock, iflag);
2650 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2651 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04002652}
2653
James Smarte59058c2008-08-24 21:49:00 -04002654/**
James Smart3621a712009-04-06 18:47:14 -04002655 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04002656 * @phba: pointer to lpfc hba data structure.
2657 *
2658 * This routine is invoked to prepare a HBA to be brought offline. It performs
2659 * unregistration login to all the nodes on all vports and flushes the mailbox
2660 * queue to make it ready to be brought offline.
2661 **/
James Smart46fa3112007-04-25 09:51:45 -04002662void
2663lpfc_offline_prep(struct lpfc_hba * phba)
2664{
James Smart2e0fef82007-06-17 19:56:36 -05002665 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04002666 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04002667 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05002668 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04002669 int i;
dea31012005-04-17 16:05:31 -05002670
James Smart2e0fef82007-06-17 19:56:36 -05002671 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002672 return;
dea31012005-04-17 16:05:31 -05002673
James Smart46fa3112007-04-25 09:51:45 -04002674 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002675
2676 lpfc_linkdown(phba);
2677
James Smart87af33f2007-10-27 13:37:43 -04002678 /* Issue an unreg_login to all nodes on all vports */
2679 vports = lpfc_create_vport_work_array(phba);
2680 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04002681 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04002682 if (vports[i]->load_flag & FC_UNLOADING)
2683 continue;
James Smart72100cc2010-02-12 14:43:01 -05002684 shost = lpfc_shost_from_vport(vports[i]);
2685 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002686 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002687 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2688 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05002689 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002690
James Smart87af33f2007-10-27 13:37:43 -04002691 shost = lpfc_shost_from_vport(vports[i]);
2692 list_for_each_entry_safe(ndlp, next_ndlp,
2693 &vports[i]->fc_nodes,
2694 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002695 if (!NLP_CHK_NODE_ACT(ndlp))
2696 continue;
James Smart87af33f2007-10-27 13:37:43 -04002697 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2698 continue;
2699 if (ndlp->nlp_type & NLP_FABRIC) {
2700 lpfc_disc_state_machine(vports[i], ndlp,
2701 NULL, NLP_EVT_DEVICE_RECOVERY);
2702 lpfc_disc_state_machine(vports[i], ndlp,
2703 NULL, NLP_EVT_DEVICE_RM);
2704 }
2705 spin_lock_irq(shost->host_lock);
2706 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart6b5151f2012-01-18 16:24:06 -05002707
2708 /*
2709 * Whenever an SLI4 port goes offline, free the
2710 * RPI. A new RPI when the adapter port comes
2711 * back online.
2712 */
2713 if (phba->sli_rev == LPFC_SLI_REV4)
2714 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
2715
James Smart87af33f2007-10-27 13:37:43 -04002716 spin_unlock_irq(shost->host_lock);
2717 lpfc_unreg_rpi(vports[i], ndlp);
2718 }
2719 }
2720 }
James Smart09372822008-01-11 01:52:54 -05002721 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002722
James Smart04c68492009-05-22 14:52:52 -04002723 lpfc_sli_mbox_sys_shutdown(phba);
James Smart46fa3112007-04-25 09:51:45 -04002724}
2725
James Smarte59058c2008-08-24 21:49:00 -04002726/**
James Smart3621a712009-04-06 18:47:14 -04002727 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04002728 * @phba: pointer to lpfc hba data structure.
2729 *
2730 * This routine actually brings a HBA offline. It stops all the timers
2731 * associated with the HBA, brings down the SLI layer, and eventually
2732 * marks the HBA as in offline state for the upper layer protocol.
2733 **/
James Smart46fa3112007-04-25 09:51:45 -04002734void
James Smart2e0fef82007-06-17 19:56:36 -05002735lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04002736{
James Smart549e55c2007-08-02 11:09:51 -04002737 struct Scsi_Host *shost;
2738 struct lpfc_vport **vports;
2739 int i;
James Smart46fa3112007-04-25 09:51:45 -04002740
James Smart549e55c2007-08-02 11:09:51 -04002741 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002742 return;
James Smart688a8862006-07-06 15:49:56 -04002743
James Smartda0436e2009-05-22 14:51:39 -04002744 /* stop port and all timers associated with this hba */
2745 lpfc_stop_port(phba);
James Smart51ef4c22007-08-02 11:10:31 -04002746 vports = lpfc_create_vport_work_array(phba);
2747 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002748 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04002749 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05002750 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05002751 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002752 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05002753 /* Bring down the SLI Layer and cleanup. The HBA is offline
2754 now. */
2755 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002756 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04002757 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002758 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04002759 vports = lpfc_create_vport_work_array(phba);
2760 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002761 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002762 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04002763 spin_lock_irq(shost->host_lock);
2764 vports[i]->work_port_events = 0;
2765 vports[i]->fc_flag |= FC_OFFLINE_MODE;
2766 spin_unlock_irq(shost->host_lock);
2767 }
James Smart09372822008-01-11 01:52:54 -05002768 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002769}
2770
James Smarte59058c2008-08-24 21:49:00 -04002771/**
James Smartff78d8f2011-12-13 13:21:35 -05002772 * lpfc_scsi_buf_update - Update the scsi_buffers that are already allocated.
2773 * @phba: pointer to lpfc hba data structure.
2774 *
2775 * This routine goes through all the scsi buffers in the system and updates the
2776 * Physical XRIs assigned to the SCSI buffer because these may change after any
2777 * firmware reset
2778 *
2779 * Return codes
2780 * 0 - successful (for now, it always returns 0)
2781 **/
2782int
2783lpfc_scsi_buf_update(struct lpfc_hba *phba)
2784{
2785 struct lpfc_scsi_buf *sb, *sb_next;
2786
2787 spin_lock_irq(&phba->hbalock);
2788 spin_lock(&phba->scsi_buf_list_lock);
James Smart41899be2012-03-01 22:34:19 -05002789 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
James Smartff78d8f2011-12-13 13:21:35 -05002790 sb->cur_iocbq.sli4_xritag =
2791 phba->sli4_hba.xri_ids[sb->cur_iocbq.sli4_lxritag];
James Smart41899be2012-03-01 22:34:19 -05002792 set_bit(sb->cur_iocbq.sli4_lxritag, phba->sli4_hba.xri_bmask);
2793 phba->sli4_hba.max_cfg_param.xri_used++;
2794 phba->sli4_hba.xri_count++;
2795 }
James Smartff78d8f2011-12-13 13:21:35 -05002796 spin_unlock(&phba->scsi_buf_list_lock);
2797 spin_unlock_irq(&phba->hbalock);
2798 return 0;
2799}
2800
2801/**
James Smart3621a712009-04-06 18:47:14 -04002802 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04002803 * @phba: pointer to lpfc hba data structure.
2804 *
2805 * This routine is to free all the SCSI buffers and IOCBs from the driver
2806 * list back to kernel. It is called from lpfc_pci_remove_one to free
2807 * the internal resources before the device is removed from the system.
2808 *
2809 * Return codes
2810 * 0 - successful (for now, it always returns 0)
2811 **/
dea31012005-04-17 16:05:31 -05002812static int
James Smart2e0fef82007-06-17 19:56:36 -05002813lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002814{
2815 struct lpfc_scsi_buf *sb, *sb_next;
2816 struct lpfc_iocbq *io, *io_next;
2817
James Smart2e0fef82007-06-17 19:56:36 -05002818 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002819 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smart1c6f4ef52009-11-18 15:40:49 -05002820 spin_lock(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05002821 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
2822 list_del(&sb->list);
2823 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05002824 sb->dma_handle);
dea31012005-04-17 16:05:31 -05002825 kfree(sb);
2826 phba->total_scsi_bufs--;
2827 }
James Smart1c6f4ef52009-11-18 15:40:49 -05002828 spin_unlock(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05002829
2830 /* Release all the lpfc_iocbq entries maintained by this host. */
2831 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2832 list_del(&io->list);
2833 kfree(io);
2834 phba->total_iocbq_bufs--;
2835 }
James Smart6d368e52011-05-24 11:44:12 -04002836
James Smart2e0fef82007-06-17 19:56:36 -05002837 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002838 return 0;
2839}
2840
James Smarte59058c2008-08-24 21:49:00 -04002841/**
James Smart3621a712009-04-06 18:47:14 -04002842 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04002843 * @phba: pointer to lpfc hba data structure.
2844 * @instance: a unique integer ID to this FC port.
2845 * @dev: pointer to the device data structure.
2846 *
2847 * This routine creates a FC port for the upper layer protocol. The FC port
2848 * can be created on top of either a physical port or a virtual port provided
2849 * by the HBA. This routine also allocates a SCSI host data structure (shost)
2850 * and associates the FC port created before adding the shost into the SCSI
2851 * layer.
2852 *
2853 * Return codes
2854 * @vport - pointer to the virtual N_Port data structure.
2855 * NULL - port create failed.
2856 **/
James Smart2e0fef82007-06-17 19:56:36 -05002857struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04002858lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04002859{
James Smart2e0fef82007-06-17 19:56:36 -05002860 struct lpfc_vport *vport;
2861 struct Scsi_Host *shost;
2862 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04002863
James Smart3de2a652007-08-02 11:09:59 -04002864 if (dev != &phba->pcidev->dev)
2865 shost = scsi_host_alloc(&lpfc_vport_template,
2866 sizeof(struct lpfc_vport));
2867 else
2868 shost = scsi_host_alloc(&lpfc_template,
2869 sizeof(struct lpfc_vport));
James Smart2e0fef82007-06-17 19:56:36 -05002870 if (!shost)
2871 goto out;
James Smart47a86172007-04-25 09:53:22 -04002872
James Smart2e0fef82007-06-17 19:56:36 -05002873 vport = (struct lpfc_vport *) shost->hostdata;
2874 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05002875 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05002876 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05002877 vport->fc_rscn_flush = 0;
James Smart47a86172007-04-25 09:53:22 -04002878
James Smart3de2a652007-08-02 11:09:59 -04002879 lpfc_get_vport_cfgparam(vport);
James Smart2e0fef82007-06-17 19:56:36 -05002880 shost->unique_id = instance;
2881 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04002882 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05002883 shost->this_id = -1;
2884 shost->max_cmd_len = 16;
James Smartda0436e2009-05-22 14:51:39 -04002885 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart28baac72010-02-12 14:42:03 -05002886 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04002887 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smartda0436e2009-05-22 14:51:39 -04002888 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
2889 }
James Smart81301a92008-12-04 22:39:46 -05002890
James Smart47a86172007-04-25 09:53:22 -04002891 /*
James Smart2e0fef82007-06-17 19:56:36 -05002892 * Set initial can_queue value since 0 is no longer supported and
2893 * scsi_add_host will fail. This will be adjusted later based on the
2894 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04002895 */
James Smart2e0fef82007-06-17 19:56:36 -05002896 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04002897 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05002898 shost->transportt = lpfc_vport_transport_template;
2899 vport->port_type = LPFC_NPIV_PORT;
2900 } else {
2901 shost->transportt = lpfc_transport_template;
2902 vport->port_type = LPFC_PHYSICAL_PORT;
2903 }
James Smart47a86172007-04-25 09:53:22 -04002904
James Smart2e0fef82007-06-17 19:56:36 -05002905 /* Initialize all internally managed lists. */
2906 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04002907 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05002908 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04002909
James Smart2e0fef82007-06-17 19:56:36 -05002910 init_timer(&vport->fc_disctmo);
2911 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05002912 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002913
James Smart2e0fef82007-06-17 19:56:36 -05002914 init_timer(&vport->fc_fdmitmo);
2915 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
James Smart92d7f7b2007-06-17 19:56:38 -05002916 vport->fc_fdmitmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002917
James Smart2e0fef82007-06-17 19:56:36 -05002918 init_timer(&vport->els_tmofunc);
2919 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05002920 vport->els_tmofunc.data = (unsigned long)vport;
James Smart92494142011-02-16 12:39:44 -05002921
2922 init_timer(&vport->delayed_disc_tmo);
2923 vport->delayed_disc_tmo.function = lpfc_delayed_disc_tmo;
2924 vport->delayed_disc_tmo.data = (unsigned long)vport;
2925
James Bottomleyd139b9b2009-11-05 13:33:12 -06002926 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05002927 if (error)
2928 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04002929
James Smart549e55c2007-08-02 11:09:51 -04002930 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002931 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04002932 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002933 return vport;
James Smart47a86172007-04-25 09:53:22 -04002934
James Smart2e0fef82007-06-17 19:56:36 -05002935out_put_shost:
2936 scsi_host_put(shost);
2937out:
2938 return NULL;
James Smart47a86172007-04-25 09:53:22 -04002939}
2940
James Smarte59058c2008-08-24 21:49:00 -04002941/**
James Smart3621a712009-04-06 18:47:14 -04002942 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04002943 * @vport: pointer to an lpfc virtual N_Port data structure.
2944 *
2945 * This routine destroys a FC port from the upper layer protocol. All the
2946 * resources associated with the port are released.
2947 **/
James Smart2e0fef82007-06-17 19:56:36 -05002948void
2949destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04002950{
James Smart92d7f7b2007-06-17 19:56:38 -05002951 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2952 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002953
James Smart858c9f62007-06-17 19:56:39 -05002954 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05002955 fc_remove_host(shost);
2956 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04002957
James Smart92d7f7b2007-06-17 19:56:38 -05002958 spin_lock_irq(&phba->hbalock);
2959 list_del_init(&vport->listentry);
2960 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04002961
James Smart92d7f7b2007-06-17 19:56:38 -05002962 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04002963 return;
James Smart47a86172007-04-25 09:53:22 -04002964}
2965
James Smarte59058c2008-08-24 21:49:00 -04002966/**
James Smart3621a712009-04-06 18:47:14 -04002967 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04002968 *
2969 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
2970 * uses the kernel idr facility to perform the task.
2971 *
2972 * Return codes:
2973 * instance - a unique integer ID allocated as the new instance.
2974 * -1 - lpfc get instance failed.
2975 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002976int
2977lpfc_get_instance(void)
2978{
2979 int instance = 0;
2980
2981 /* Assign an unused number */
2982 if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
2983 return -1;
2984 if (idr_get_new(&lpfc_hba_index, NULL, &instance))
2985 return -1;
2986 return instance;
2987}
2988
James Smarte59058c2008-08-24 21:49:00 -04002989/**
James Smart3621a712009-04-06 18:47:14 -04002990 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04002991 * @shost: pointer to SCSI host data structure.
2992 * @time: elapsed time of the scan in jiffies.
2993 *
2994 * This routine is called by the SCSI layer with a SCSI host to determine
2995 * whether the scan host is finished.
2996 *
2997 * Note: there is no scan_start function as adapter initialization will have
2998 * asynchronously kicked off the link initialization.
2999 *
3000 * Return codes
3001 * 0 - SCSI host scan is not over yet.
3002 * 1 - SCSI host scan is over.
3003 **/
James Smart47a86172007-04-25 09:53:22 -04003004int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
3005{
James Smart2e0fef82007-06-17 19:56:36 -05003006 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3007 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05003008 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04003009
James Smart858c9f62007-06-17 19:56:39 -05003010 spin_lock_irq(shost->host_lock);
3011
James Smart51ef4c22007-08-02 11:10:31 -04003012 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05003013 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003014 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05003015 }
James Smart2e0fef82007-06-17 19:56:36 -05003016 if (time >= 30 * HZ) {
3017 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003018 "0461 Scanning longer than 30 "
3019 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003020 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003021 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003022 }
3023 if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
3024 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003025 "0465 Link down longer than 15 "
3026 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003027 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05003028 goto finished;
James Smart47a86172007-04-25 09:53:22 -04003029 }
3030
James Smart2e0fef82007-06-17 19:56:36 -05003031 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05003032 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003033 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05003034 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003035 if (vport->fc_map_cnt == 0 && time < 2 * HZ)
James Smart858c9f62007-06-17 19:56:39 -05003036 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003037 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05003038 goto finished;
3039
3040 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003041
3042finished:
James Smart858c9f62007-06-17 19:56:39 -05003043 spin_unlock_irq(shost->host_lock);
3044 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05003045}
3046
James Smarte59058c2008-08-24 21:49:00 -04003047/**
James Smart3621a712009-04-06 18:47:14 -04003048 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04003049 * @shost: pointer to SCSI host data structure.
3050 *
3051 * This routine initializes a given SCSI host attributes on a FC port. The
3052 * SCSI host can be either on top of a physical port or a virtual port.
3053 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003054void lpfc_host_attrib_init(struct Scsi_Host *shost)
3055{
3056 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3057 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003058 /*
James Smart2e0fef82007-06-17 19:56:36 -05003059 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04003060 */
3061
James Smart2e0fef82007-06-17 19:56:36 -05003062 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
3063 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04003064 fc_host_supported_classes(shost) = FC_COS_CLASS3;
3065
3066 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003067 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003068 fc_host_supported_fc4s(shost)[2] = 1;
3069 fc_host_supported_fc4s(shost)[7] = 1;
3070
James Smart92d7f7b2007-06-17 19:56:38 -05003071 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
3072 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04003073
3074 fc_host_supported_speeds(shost) = 0;
James Smart88a2cfb2011-07-22 18:36:33 -04003075 if (phba->lmt & LMT_16Gb)
3076 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
James Smart47a86172007-04-25 09:53:22 -04003077 if (phba->lmt & LMT_10Gb)
3078 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04003079 if (phba->lmt & LMT_8Gb)
3080 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04003081 if (phba->lmt & LMT_4Gb)
3082 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
3083 if (phba->lmt & LMT_2Gb)
3084 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
3085 if (phba->lmt & LMT_1Gb)
3086 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
3087
3088 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05003089 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
3090 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04003091
Mike Christie0af5d702010-09-15 16:52:31 -05003092 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
3093
James Smart47a86172007-04-25 09:53:22 -04003094 /* This value is also unchanging */
3095 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003096 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003097 fc_host_active_fc4s(shost)[2] = 1;
3098 fc_host_active_fc4s(shost)[7] = 1;
3099
James Smart92d7f7b2007-06-17 19:56:38 -05003100 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04003101 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04003102 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04003103 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04003104}
dea31012005-04-17 16:05:31 -05003105
James Smarte59058c2008-08-24 21:49:00 -04003106/**
James Smartda0436e2009-05-22 14:51:39 -04003107 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04003108 * @phba: pointer to lpfc hba data structure.
3109 *
James Smartda0436e2009-05-22 14:51:39 -04003110 * This routine is invoked to stop an SLI3 device port, it stops the device
3111 * from generating interrupts and stops the device driver's timers for the
3112 * device.
James Smarte59058c2008-08-24 21:49:00 -04003113 **/
James Smartdb2378e2008-02-08 18:49:51 -05003114static void
James Smartda0436e2009-05-22 14:51:39 -04003115lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05003116{
James Smartda0436e2009-05-22 14:51:39 -04003117 /* Clear all interrupt enable conditions */
3118 writel(0, phba->HCregaddr);
3119 readl(phba->HCregaddr); /* flush */
3120 /* Clear all pending interrupts */
3121 writel(0xffffffff, phba->HAregaddr);
3122 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04003123
James Smartda0436e2009-05-22 14:51:39 -04003124 /* Reset some HBA SLI setup states */
3125 lpfc_stop_hba_timers(phba);
3126 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05003127}
3128
James Smarte59058c2008-08-24 21:49:00 -04003129/**
James Smartda0436e2009-05-22 14:51:39 -04003130 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05003131 * @phba: pointer to lpfc hba data structure.
3132 *
James Smartda0436e2009-05-22 14:51:39 -04003133 * This routine is invoked to stop an SLI4 device port, it stops the device
3134 * from generating interrupts and stops the device driver's timers for the
3135 * device.
3136 **/
3137static void
3138lpfc_stop_port_s4(struct lpfc_hba *phba)
3139{
3140 /* Reset some HBA SLI4 setup states */
3141 lpfc_stop_hba_timers(phba);
3142 phba->pport->work_port_events = 0;
3143 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04003144}
3145
3146/**
3147 * lpfc_stop_port - Wrapper function for stopping hba port
3148 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05003149 *
James Smartda0436e2009-05-22 14:51:39 -04003150 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
3151 * the API jump table function pointer from the lpfc_hba struct.
3152 **/
3153void
3154lpfc_stop_port(struct lpfc_hba *phba)
3155{
3156 phba->lpfc_stop_port(phba);
3157}
3158
3159/**
James Smartecfd03c2010-02-12 14:41:27 -05003160 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
3161 * @phba: Pointer to hba for which this call is being executed.
3162 *
3163 * This routine starts the timer waiting for the FCF rediscovery to complete.
3164 **/
3165void
3166lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
3167{
3168 unsigned long fcf_redisc_wait_tmo =
3169 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
3170 /* Start fcf rediscovery wait period timer */
3171 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
3172 spin_lock_irq(&phba->hbalock);
3173 /* Allow action to new fcf asynchronous event */
3174 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
3175 /* Mark the FCF rediscovery pending state */
3176 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
3177 spin_unlock_irq(&phba->hbalock);
3178}
3179
3180/**
3181 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
3182 * @ptr: Map to lpfc_hba data structure pointer.
3183 *
3184 * This routine is invoked when waiting for FCF table rediscover has been
3185 * timed out. If new FCF record(s) has (have) been discovered during the
3186 * wait period, a new FCF event shall be added to the FCOE async event
3187 * list, and then worker thread shall be waked up for processing from the
3188 * worker thread context.
3189 **/
3190void
3191lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
3192{
3193 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
3194
3195 /* Don't send FCF rediscovery event if timer cancelled */
3196 spin_lock_irq(&phba->hbalock);
3197 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
3198 spin_unlock_irq(&phba->hbalock);
3199 return;
3200 }
3201 /* Clear FCF rediscovery timer pending flag */
3202 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
3203 /* FCF rediscovery event to worker thread */
3204 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
3205 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003206 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04003207 "2776 FCF rediscover quiescent timer expired\n");
James Smartecfd03c2010-02-12 14:41:27 -05003208 /* wake up worker thread */
3209 lpfc_worker_wake_up(phba);
3210}
3211
3212/**
James Smartda0436e2009-05-22 14:51:39 -04003213 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
3214 * @phba: pointer to lpfc hba data structure.
3215 * @acqe_link: pointer to the async link completion queue entry.
3216 *
3217 * This routine is to parse the SLI4 link-attention link fault code and
3218 * translate it into the base driver's read link attention mailbox command
3219 * status.
3220 *
3221 * Return: Link-attention status in terms of base driver's coding.
3222 **/
3223static uint16_t
3224lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
3225 struct lpfc_acqe_link *acqe_link)
3226{
3227 uint16_t latt_fault;
3228
3229 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
3230 case LPFC_ASYNC_LINK_FAULT_NONE:
3231 case LPFC_ASYNC_LINK_FAULT_LOCAL:
3232 case LPFC_ASYNC_LINK_FAULT_REMOTE:
3233 latt_fault = 0;
3234 break;
3235 default:
3236 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3237 "0398 Invalid link fault code: x%x\n",
3238 bf_get(lpfc_acqe_link_fault, acqe_link));
3239 latt_fault = MBXERR_ERROR;
3240 break;
3241 }
3242 return latt_fault;
3243}
3244
3245/**
3246 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
3247 * @phba: pointer to lpfc hba data structure.
3248 * @acqe_link: pointer to the async link completion queue entry.
3249 *
3250 * This routine is to parse the SLI4 link attention type and translate it
3251 * into the base driver's link attention type coding.
3252 *
3253 * Return: Link attention type in terms of base driver's coding.
3254 **/
3255static uint8_t
3256lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
3257 struct lpfc_acqe_link *acqe_link)
3258{
3259 uint8_t att_type;
3260
3261 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
3262 case LPFC_ASYNC_LINK_STATUS_DOWN:
3263 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
James Smart76a95d72010-11-20 23:11:48 -05003264 att_type = LPFC_ATT_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04003265 break;
3266 case LPFC_ASYNC_LINK_STATUS_UP:
3267 /* Ignore physical link up events - wait for logical link up */
James Smart76a95d72010-11-20 23:11:48 -05003268 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003269 break;
3270 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
James Smart76a95d72010-11-20 23:11:48 -05003271 att_type = LPFC_ATT_LINK_UP;
James Smartda0436e2009-05-22 14:51:39 -04003272 break;
3273 default:
3274 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3275 "0399 Invalid link attention type: x%x\n",
3276 bf_get(lpfc_acqe_link_status, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003277 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003278 break;
3279 }
3280 return att_type;
3281}
3282
3283/**
3284 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
3285 * @phba: pointer to lpfc hba data structure.
3286 * @acqe_link: pointer to the async link completion queue entry.
3287 *
3288 * This routine is to parse the SLI4 link-attention link speed and translate
3289 * it into the base driver's link-attention link speed coding.
3290 *
3291 * Return: Link-attention link speed in terms of base driver's coding.
3292 **/
3293static uint8_t
3294lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
3295 struct lpfc_acqe_link *acqe_link)
3296{
3297 uint8_t link_speed;
3298
3299 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
3300 case LPFC_ASYNC_LINK_SPEED_ZERO:
James Smartda0436e2009-05-22 14:51:39 -04003301 case LPFC_ASYNC_LINK_SPEED_10MBPS:
James Smartda0436e2009-05-22 14:51:39 -04003302 case LPFC_ASYNC_LINK_SPEED_100MBPS:
James Smart76a95d72010-11-20 23:11:48 -05003303 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003304 break;
3305 case LPFC_ASYNC_LINK_SPEED_1GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003306 link_speed = LPFC_LINK_SPEED_1GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003307 break;
3308 case LPFC_ASYNC_LINK_SPEED_10GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003309 link_speed = LPFC_LINK_SPEED_10GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003310 break;
3311 default:
3312 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3313 "0483 Invalid link-attention link speed: x%x\n",
3314 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003315 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003316 break;
3317 }
3318 return link_speed;
3319}
3320
3321/**
James Smart70f3c072010-12-15 17:57:33 -05003322 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
James Smartda0436e2009-05-22 14:51:39 -04003323 * @phba: pointer to lpfc hba data structure.
3324 * @acqe_link: pointer to the async link completion queue entry.
3325 *
James Smart70f3c072010-12-15 17:57:33 -05003326 * This routine is to handle the SLI4 asynchronous FCoE link event.
James Smartda0436e2009-05-22 14:51:39 -04003327 **/
3328static void
3329lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3330 struct lpfc_acqe_link *acqe_link)
3331{
3332 struct lpfc_dmabuf *mp;
3333 LPFC_MBOXQ_t *pmb;
3334 MAILBOX_t *mb;
James Smart76a95d72010-11-20 23:11:48 -05003335 struct lpfc_mbx_read_top *la;
James Smartda0436e2009-05-22 14:51:39 -04003336 uint8_t att_type;
James Smart76a95d72010-11-20 23:11:48 -05003337 int rc;
James Smartda0436e2009-05-22 14:51:39 -04003338
3339 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05003340 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
James Smartda0436e2009-05-22 14:51:39 -04003341 return;
James Smart32b97932009-07-19 10:01:21 -04003342 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003343 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3344 if (!pmb) {
3345 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3346 "0395 The mboxq allocation failed\n");
3347 return;
3348 }
3349 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3350 if (!mp) {
3351 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3352 "0396 The lpfc_dmabuf allocation failed\n");
3353 goto out_free_pmb;
3354 }
3355 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3356 if (!mp->virt) {
3357 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3358 "0397 The mbuf allocation failed\n");
3359 goto out_free_dmabuf;
3360 }
3361
3362 /* Cleanup any outstanding ELS commands */
3363 lpfc_els_flush_all_cmd(phba);
3364
3365 /* Block ELS IOCBs until we have done process link event */
3366 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3367
3368 /* Update link event statistics */
3369 phba->sli.slistat.link_event++;
3370
James Smart76a95d72010-11-20 23:11:48 -05003371 /* Create lpfc_handle_latt mailbox command from link ACQE */
3372 lpfc_read_topology(phba, pmb, mp);
3373 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smartda0436e2009-05-22 14:51:39 -04003374 pmb->vport = phba->pport;
3375
James Smartda0436e2009-05-22 14:51:39 -04003376 /* Keep the link status for extra SLI4 state machine reference */
3377 phba->sli4_hba.link_state.speed =
3378 bf_get(lpfc_acqe_link_speed, acqe_link);
3379 phba->sli4_hba.link_state.duplex =
3380 bf_get(lpfc_acqe_link_duplex, acqe_link);
3381 phba->sli4_hba.link_state.status =
3382 bf_get(lpfc_acqe_link_status, acqe_link);
James Smart70f3c072010-12-15 17:57:33 -05003383 phba->sli4_hba.link_state.type =
3384 bf_get(lpfc_acqe_link_type, acqe_link);
3385 phba->sli4_hba.link_state.number =
3386 bf_get(lpfc_acqe_link_number, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04003387 phba->sli4_hba.link_state.fault =
3388 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05003389 phba->sli4_hba.link_state.logical_speed =
James Smart70f3c072010-12-15 17:57:33 -05003390 bf_get(lpfc_acqe_logical_link_speed, acqe_link);
3391 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartc31098c2011-04-16 11:03:33 -04003392 "2900 Async FC/FCoE Link event - Speed:%dGBit "
3393 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
3394 "Logical speed:%dMbps Fault:%d\n",
James Smart70f3c072010-12-15 17:57:33 -05003395 phba->sli4_hba.link_state.speed,
3396 phba->sli4_hba.link_state.topology,
3397 phba->sli4_hba.link_state.status,
3398 phba->sli4_hba.link_state.type,
3399 phba->sli4_hba.link_state.number,
3400 phba->sli4_hba.link_state.logical_speed * 10,
3401 phba->sli4_hba.link_state.fault);
James Smart76a95d72010-11-20 23:11:48 -05003402 /*
3403 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
3404 * topology info. Note: Optional for non FC-AL ports.
3405 */
3406 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3407 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3408 if (rc == MBX_NOT_FINISHED)
3409 goto out_free_dmabuf;
3410 return;
3411 }
3412 /*
3413 * For FCoE Mode: fill in all the topology information we need and call
3414 * the READ_TOPOLOGY completion routine to continue without actually
3415 * sending the READ_TOPOLOGY mailbox command to the port.
3416 */
3417 /* Parse and translate status field */
3418 mb = &pmb->u.mb;
3419 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
3420
3421 /* Parse and translate link attention fields */
3422 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
3423 la->eventTag = acqe_link->event_tag;
3424 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
3425 bf_set(lpfc_mbx_read_top_link_spd, la,
3426 lpfc_sli4_parse_latt_link_speed(phba, acqe_link));
3427
3428 /* Fake the the following irrelvant fields */
3429 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
3430 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
3431 bf_set(lpfc_mbx_read_top_il, la, 0);
3432 bf_set(lpfc_mbx_read_top_pb, la, 0);
3433 bf_set(lpfc_mbx_read_top_fa, la, 0);
3434 bf_set(lpfc_mbx_read_top_mm, la, 0);
James Smartda0436e2009-05-22 14:51:39 -04003435
3436 /* Invoke the lpfc_handle_latt mailbox command callback function */
James Smart76a95d72010-11-20 23:11:48 -05003437 lpfc_mbx_cmpl_read_topology(phba, pmb);
James Smartda0436e2009-05-22 14:51:39 -04003438
3439 return;
3440
3441out_free_dmabuf:
3442 kfree(mp);
3443out_free_pmb:
3444 mempool_free(pmb, phba->mbox_mem_pool);
3445}
3446
3447/**
James Smart70f3c072010-12-15 17:57:33 -05003448 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
3449 * @phba: pointer to lpfc hba data structure.
3450 * @acqe_fc: pointer to the async fc completion queue entry.
3451 *
3452 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
3453 * that the event was received and then issue a read_topology mailbox command so
3454 * that the rest of the driver will treat it the same as SLI3.
3455 **/
3456static void
3457lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
3458{
3459 struct lpfc_dmabuf *mp;
3460 LPFC_MBOXQ_t *pmb;
3461 int rc;
3462
3463 if (bf_get(lpfc_trailer_type, acqe_fc) !=
3464 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
3465 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3466 "2895 Non FC link Event detected.(%d)\n",
3467 bf_get(lpfc_trailer_type, acqe_fc));
3468 return;
3469 }
3470 /* Keep the link status for extra SLI4 state machine reference */
3471 phba->sli4_hba.link_state.speed =
3472 bf_get(lpfc_acqe_fc_la_speed, acqe_fc);
3473 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
3474 phba->sli4_hba.link_state.topology =
3475 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
3476 phba->sli4_hba.link_state.status =
3477 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
3478 phba->sli4_hba.link_state.type =
3479 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
3480 phba->sli4_hba.link_state.number =
3481 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
3482 phba->sli4_hba.link_state.fault =
3483 bf_get(lpfc_acqe_link_fault, acqe_fc);
3484 phba->sli4_hba.link_state.logical_speed =
3485 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc);
3486 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3487 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
3488 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
3489 "%dMbps Fault:%d\n",
3490 phba->sli4_hba.link_state.speed,
3491 phba->sli4_hba.link_state.topology,
3492 phba->sli4_hba.link_state.status,
3493 phba->sli4_hba.link_state.type,
3494 phba->sli4_hba.link_state.number,
3495 phba->sli4_hba.link_state.logical_speed * 10,
3496 phba->sli4_hba.link_state.fault);
3497 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3498 if (!pmb) {
3499 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3500 "2897 The mboxq allocation failed\n");
3501 return;
3502 }
3503 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3504 if (!mp) {
3505 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3506 "2898 The lpfc_dmabuf allocation failed\n");
3507 goto out_free_pmb;
3508 }
3509 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3510 if (!mp->virt) {
3511 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3512 "2899 The mbuf allocation failed\n");
3513 goto out_free_dmabuf;
3514 }
3515
3516 /* Cleanup any outstanding ELS commands */
3517 lpfc_els_flush_all_cmd(phba);
3518
3519 /* Block ELS IOCBs until we have done process link event */
3520 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3521
3522 /* Update link event statistics */
3523 phba->sli.slistat.link_event++;
3524
3525 /* Create lpfc_handle_latt mailbox command from link ACQE */
3526 lpfc_read_topology(phba, pmb, mp);
3527 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
3528 pmb->vport = phba->pport;
3529
3530 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3531 if (rc == MBX_NOT_FINISHED)
3532 goto out_free_dmabuf;
3533 return;
3534
3535out_free_dmabuf:
3536 kfree(mp);
3537out_free_pmb:
3538 mempool_free(pmb, phba->mbox_mem_pool);
3539}
3540
3541/**
3542 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
3543 * @phba: pointer to lpfc hba data structure.
3544 * @acqe_fc: pointer to the async SLI completion queue entry.
3545 *
3546 * This routine is to handle the SLI4 asynchronous SLI events.
3547 **/
3548static void
3549lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
3550{
3551 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3552 "2901 Async SLI event - Event Data1:x%08x Event Data2:"
3553 "x%08x SLI Event Type:%d",
3554 acqe_sli->event_data1, acqe_sli->event_data2,
3555 bf_get(lpfc_trailer_type, acqe_sli));
3556 return;
3557}
3558
3559/**
James Smartfc2b9892010-02-26 14:15:29 -05003560 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
3561 * @vport: pointer to vport data structure.
3562 *
3563 * This routine is to perform Clear Virtual Link (CVL) on a vport in
3564 * response to a CVL event.
3565 *
3566 * Return the pointer to the ndlp with the vport if successful, otherwise
3567 * return NULL.
3568 **/
3569static struct lpfc_nodelist *
3570lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
3571{
3572 struct lpfc_nodelist *ndlp;
3573 struct Scsi_Host *shost;
3574 struct lpfc_hba *phba;
3575
3576 if (!vport)
3577 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05003578 phba = vport->phba;
3579 if (!phba)
3580 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04003581 ndlp = lpfc_findnode_did(vport, Fabric_DID);
3582 if (!ndlp) {
3583 /* Cannot find existing Fabric ndlp, so allocate a new one */
3584 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
3585 if (!ndlp)
3586 return 0;
3587 lpfc_nlp_init(vport, ndlp, Fabric_DID);
3588 /* Set the node type */
3589 ndlp->nlp_type |= NLP_FABRIC;
3590 /* Put ndlp onto node list */
3591 lpfc_enqueue_node(vport, ndlp);
3592 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
3593 /* re-setup ndlp without removing from node list */
3594 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
3595 if (!ndlp)
3596 return 0;
3597 }
James Smart63e801c2010-11-20 23:14:19 -05003598 if ((phba->pport->port_state < LPFC_FLOGI) &&
3599 (phba->pport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05003600 return NULL;
3601 /* If virtual link is not yet instantiated ignore CVL */
James Smart63e801c2010-11-20 23:14:19 -05003602 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
3603 && (vport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05003604 return NULL;
3605 shost = lpfc_shost_from_vport(vport);
3606 if (!shost)
3607 return NULL;
3608 lpfc_linkdown_port(vport);
3609 lpfc_cleanup_pending_mbox(vport);
3610 spin_lock_irq(shost->host_lock);
3611 vport->fc_flag |= FC_VPORT_CVL_RCVD;
3612 spin_unlock_irq(shost->host_lock);
3613
3614 return ndlp;
3615}
3616
3617/**
3618 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
3619 * @vport: pointer to lpfc hba data structure.
3620 *
3621 * This routine is to perform Clear Virtual Link (CVL) on all vports in
3622 * response to a FCF dead event.
3623 **/
3624static void
3625lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
3626{
3627 struct lpfc_vport **vports;
3628 int i;
3629
3630 vports = lpfc_create_vport_work_array(phba);
3631 if (vports)
3632 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
3633 lpfc_sli4_perform_vport_cvl(vports[i]);
3634 lpfc_destroy_vport_work_array(phba, vports);
3635}
3636
3637/**
James Smart76a95d72010-11-20 23:11:48 -05003638 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
James Smartda0436e2009-05-22 14:51:39 -04003639 * @phba: pointer to lpfc hba data structure.
3640 * @acqe_link: pointer to the async fcoe completion queue entry.
3641 *
3642 * This routine is to handle the SLI4 asynchronous fcoe event.
3643 **/
3644static void
James Smart76a95d72010-11-20 23:11:48 -05003645lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
James Smart70f3c072010-12-15 17:57:33 -05003646 struct lpfc_acqe_fip *acqe_fip)
James Smartda0436e2009-05-22 14:51:39 -04003647{
James Smart70f3c072010-12-15 17:57:33 -05003648 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04003649 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04003650 struct lpfc_vport *vport;
3651 struct lpfc_nodelist *ndlp;
3652 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05003653 int active_vlink_present;
3654 struct lpfc_vport **vports;
3655 int i;
James Smartda0436e2009-05-22 14:51:39 -04003656
James Smart70f3c072010-12-15 17:57:33 -05003657 phba->fc_eventTag = acqe_fip->event_tag;
3658 phba->fcoe_eventtag = acqe_fip->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003659 switch (event_type) {
James Smart70f3c072010-12-15 17:57:33 -05003660 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
3661 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
3662 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
James Smart999d8132010-03-15 11:24:56 -04003663 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3664 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003665 "2546 New FCF event, evt_tag:x%x, "
3666 "index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05003667 acqe_fip->event_tag,
3668 acqe_fip->index);
James Smart999d8132010-03-15 11:24:56 -04003669 else
3670 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
3671 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003672 "2788 FCF param modified event, "
3673 "evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05003674 acqe_fip->event_tag,
3675 acqe_fip->index);
James Smart38b92ef2010-08-04 16:11:39 -04003676 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smart0c9ab6f2010-02-26 14:15:57 -05003677 /*
3678 * During period of FCF discovery, read the FCF
3679 * table record indexed by the event to update
James Smarta93ff372010-10-22 11:06:08 -04003680 * FCF roundrobin failover eligible FCF bmask.
James Smart0c9ab6f2010-02-26 14:15:57 -05003681 */
3682 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
3683 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003684 "2779 Read FCF (x%x) for updating "
3685 "roundrobin FCF failover bmask\n",
James Smart70f3c072010-12-15 17:57:33 -05003686 acqe_fip->index);
3687 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05003688 }
James Smart38b92ef2010-08-04 16:11:39 -04003689
3690 /* If the FCF discovery is in progress, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04003691 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04003692 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart38b92ef2010-08-04 16:11:39 -04003693 spin_unlock_irq(&phba->hbalock);
3694 break;
3695 }
3696 /* If fast FCF failover rescan event is pending, do nothing */
3697 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
3698 spin_unlock_irq(&phba->hbalock);
3699 break;
3700 }
3701
3702 /* If the FCF has been in discovered state, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04003703 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
3704 spin_unlock_irq(&phba->hbalock);
3705 break;
3706 }
3707 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04003708
James Smart0c9ab6f2010-02-26 14:15:57 -05003709 /* Otherwise, scan the entire FCF table and re-discover SAN */
3710 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003711 "2770 Start FCF table scan per async FCF "
3712 "event, evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05003713 acqe_fip->event_tag, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05003714 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3715 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04003716 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05003717 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3718 "2547 Issue FCF scan read FCF mailbox "
James Smarta93ff372010-10-22 11:06:08 -04003719 "command failed (x%x)\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04003720 break;
3721
James Smart70f3c072010-12-15 17:57:33 -05003722 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
James Smartda0436e2009-05-22 14:51:39 -04003723 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04003724 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05003725 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
3726 acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04003727 break;
3728
James Smart70f3c072010-12-15 17:57:33 -05003729 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
James Smart0c9ab6f2010-02-26 14:15:57 -05003730 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003731 "2549 FCF (x%x) disconnected from network, "
James Smart70f3c072010-12-15 17:57:33 -05003732 "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart38b92ef2010-08-04 16:11:39 -04003733 /*
3734 * If we are in the middle of FCF failover process, clear
3735 * the corresponding FCF bit in the roundrobin bitmap.
James Smartda0436e2009-05-22 14:51:39 -04003736 */
James Smartfc2b9892010-02-26 14:15:29 -05003737 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003738 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05003739 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003740 /* Update FLOGI FCF failover eligible FCF bmask */
James Smart70f3c072010-12-15 17:57:33 -05003741 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05003742 break;
3743 }
James Smart38b92ef2010-08-04 16:11:39 -04003744 spin_unlock_irq(&phba->hbalock);
3745
3746 /* If the event is not for currently used fcf do nothing */
James Smart70f3c072010-12-15 17:57:33 -05003747 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
James Smart38b92ef2010-08-04 16:11:39 -04003748 break;
3749
3750 /*
3751 * Otherwise, request the port to rediscover the entire FCF
3752 * table for a fast recovery from case that the current FCF
3753 * is no longer valid as we are not in the middle of FCF
3754 * failover process already.
3755 */
3756 spin_lock_irq(&phba->hbalock);
James Smartfc2b9892010-02-26 14:15:29 -05003757 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05003758 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003759 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04003760
James Smart0c9ab6f2010-02-26 14:15:57 -05003761 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3762 "2771 Start FCF fast failover process due to "
3763 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
James Smart70f3c072010-12-15 17:57:33 -05003764 "\n", acqe_fip->event_tag, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05003765 rc = lpfc_sli4_redisc_fcf_table(phba);
3766 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05003767 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3768 LOG_DISCOVERY,
3769 "2772 Issue FCF rediscover mabilbox "
3770 "command failed, fail through to FCF "
3771 "dead event\n");
James Smartfc2b9892010-02-26 14:15:29 -05003772 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003773 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003774 spin_unlock_irq(&phba->hbalock);
3775 /*
3776 * Last resort will fail over by treating this
3777 * as a link down to FCF registration.
3778 */
3779 lpfc_sli4_fcf_dead_failthrough(phba);
James Smart38b92ef2010-08-04 16:11:39 -04003780 } else {
3781 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04003782 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart38b92ef2010-08-04 16:11:39 -04003783 /*
3784 * Handling fast FCF failover to a DEAD FCF event is
3785 * considered equalivant to receiving CVL to all vports.
James Smartfc2b9892010-02-26 14:15:29 -05003786 */
3787 lpfc_sli4_perform_all_vport_cvl(phba);
James Smart38b92ef2010-08-04 16:11:39 -04003788 }
James Smartda0436e2009-05-22 14:51:39 -04003789 break;
James Smart70f3c072010-12-15 17:57:33 -05003790 case LPFC_FIP_EVENT_TYPE_CVL:
James Smart0c9ab6f2010-02-26 14:15:57 -05003791 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6669f9b2009-10-02 15:16:45 -04003792 "2718 Clear Virtual Link Received for VPI 0x%x"
James Smart70f3c072010-12-15 17:57:33 -05003793 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart6d368e52011-05-24 11:44:12 -04003794
James Smart6669f9b2009-10-02 15:16:45 -04003795 vport = lpfc_find_vport_by_vpid(phba,
James Smart5248a742011-07-22 18:37:06 -04003796 acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05003797 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04003798 if (!ndlp)
3799 break;
James Smart695a8142010-01-26 23:08:03 -05003800 active_vlink_present = 0;
3801
3802 vports = lpfc_create_vport_work_array(phba);
3803 if (vports) {
3804 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
3805 i++) {
3806 if ((!(vports[i]->fc_flag &
3807 FC_VPORT_CVL_RCVD)) &&
3808 (vports[i]->port_state > LPFC_FDISC)) {
3809 active_vlink_present = 1;
3810 break;
3811 }
3812 }
3813 lpfc_destroy_vport_work_array(phba, vports);
3814 }
3815
3816 if (active_vlink_present) {
3817 /*
3818 * If there are other active VLinks present,
3819 * re-instantiate the Vlink using FDISC.
3820 */
James Smart6669f9b2009-10-02 15:16:45 -04003821 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
James Smartfc2b9892010-02-26 14:15:29 -05003822 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04003823 spin_lock_irq(shost->host_lock);
3824 ndlp->nlp_flag |= NLP_DELAY_TMO;
3825 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05003826 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
3827 vport->port_state = LPFC_FDISC;
3828 } else {
James Smartecfd03c2010-02-12 14:41:27 -05003829 /*
3830 * Otherwise, we request port to rediscover
3831 * the entire FCF table for a fast recovery
3832 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05003833 * is no longer valid if we are not already
3834 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05003835 */
James Smartfc2b9892010-02-26 14:15:29 -05003836 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003837 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05003838 spin_unlock_irq(&phba->hbalock);
3839 break;
3840 }
3841 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05003842 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003843 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003844 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
3845 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003846 "2773 Start FCF failover per CVL, "
James Smart70f3c072010-12-15 17:57:33 -05003847 "evt_tag:x%x\n", acqe_fip->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05003848 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05003849 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05003850 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3851 LOG_DISCOVERY,
3852 "2774 Issue FCF rediscover "
3853 "mabilbox command failed, "
3854 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05003855 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003856 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003857 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05003858 /*
3859 * Last resort will be re-try on the
3860 * the current registered FCF entry.
3861 */
3862 lpfc_retry_pport_discovery(phba);
James Smart38b92ef2010-08-04 16:11:39 -04003863 } else
3864 /*
3865 * Reset FCF roundrobin bmask for new
3866 * discovery.
3867 */
James Smart7d791df2011-07-22 18:37:52 -04003868 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart6669f9b2009-10-02 15:16:45 -04003869 }
3870 break;
James Smartda0436e2009-05-22 14:51:39 -04003871 default:
3872 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3873 "0288 Unknown FCoE event type 0x%x event tag "
James Smart70f3c072010-12-15 17:57:33 -05003874 "0x%x\n", event_type, acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04003875 break;
3876 }
3877}
3878
3879/**
3880 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
3881 * @phba: pointer to lpfc hba data structure.
3882 * @acqe_link: pointer to the async dcbx completion queue entry.
3883 *
3884 * This routine is to handle the SLI4 asynchronous dcbx event.
3885 **/
3886static void
3887lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
3888 struct lpfc_acqe_dcbx *acqe_dcbx)
3889{
James Smart4d9ab992009-10-02 15:16:39 -04003890 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003891 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3892 "0290 The SLI4 DCBX asynchronous event is not "
3893 "handled yet\n");
3894}
3895
3896/**
James Smartb19a0612010-04-06 14:48:51 -04003897 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
3898 * @phba: pointer to lpfc hba data structure.
3899 * @acqe_link: pointer to the async grp5 completion queue entry.
3900 *
3901 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
3902 * is an asynchronous notified of a logical link speed change. The Port
3903 * reports the logical link speed in units of 10Mbps.
3904 **/
3905static void
3906lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
3907 struct lpfc_acqe_grp5 *acqe_grp5)
3908{
3909 uint16_t prev_ll_spd;
3910
3911 phba->fc_eventTag = acqe_grp5->event_tag;
3912 phba->fcoe_eventtag = acqe_grp5->event_tag;
3913 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
3914 phba->sli4_hba.link_state.logical_speed =
3915 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5));
3916 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3917 "2789 GRP5 Async Event: Updating logical link speed "
3918 "from %dMbps to %dMbps\n", (prev_ll_spd * 10),
3919 (phba->sli4_hba.link_state.logical_speed*10));
3920}
3921
3922/**
James Smartda0436e2009-05-22 14:51:39 -04003923 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
3924 * @phba: pointer to lpfc hba data structure.
3925 *
3926 * This routine is invoked by the worker thread to process all the pending
3927 * SLI4 asynchronous events.
3928 **/
3929void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
3930{
3931 struct lpfc_cq_event *cq_event;
3932
3933 /* First, declare the async event has been handled */
3934 spin_lock_irq(&phba->hbalock);
3935 phba->hba_flag &= ~ASYNC_EVENT;
3936 spin_unlock_irq(&phba->hbalock);
3937 /* Now, handle all the async events */
3938 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
3939 /* Get the first event from the head of the event queue */
3940 spin_lock_irq(&phba->hbalock);
3941 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
3942 cq_event, struct lpfc_cq_event, list);
3943 spin_unlock_irq(&phba->hbalock);
3944 /* Process the asynchronous event */
3945 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
3946 case LPFC_TRAILER_CODE_LINK:
3947 lpfc_sli4_async_link_evt(phba,
3948 &cq_event->cqe.acqe_link);
3949 break;
3950 case LPFC_TRAILER_CODE_FCOE:
James Smart70f3c072010-12-15 17:57:33 -05003951 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04003952 break;
3953 case LPFC_TRAILER_CODE_DCBX:
3954 lpfc_sli4_async_dcbx_evt(phba,
3955 &cq_event->cqe.acqe_dcbx);
3956 break;
James Smartb19a0612010-04-06 14:48:51 -04003957 case LPFC_TRAILER_CODE_GRP5:
3958 lpfc_sli4_async_grp5_evt(phba,
3959 &cq_event->cqe.acqe_grp5);
3960 break;
James Smart70f3c072010-12-15 17:57:33 -05003961 case LPFC_TRAILER_CODE_FC:
3962 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
3963 break;
3964 case LPFC_TRAILER_CODE_SLI:
3965 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
3966 break;
James Smartda0436e2009-05-22 14:51:39 -04003967 default:
3968 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3969 "1804 Invalid asynchrous event code: "
3970 "x%x\n", bf_get(lpfc_trailer_code,
3971 &cq_event->cqe.mcqe_cmpl));
3972 break;
3973 }
3974 /* Free the completion event processed to the free pool */
3975 lpfc_sli4_cq_event_release(phba, cq_event);
3976 }
3977}
3978
3979/**
James Smartecfd03c2010-02-12 14:41:27 -05003980 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
3981 * @phba: pointer to lpfc hba data structure.
3982 *
3983 * This routine is invoked by the worker thread to process FCF table
3984 * rediscovery pending completion event.
3985 **/
3986void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
3987{
3988 int rc;
3989
3990 spin_lock_irq(&phba->hbalock);
3991 /* Clear FCF rediscovery timeout event */
3992 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
3993 /* Clear driver fast failover FCF record flag */
3994 phba->fcf.failover_rec.flag = 0;
3995 /* Set state for FCF fast failover */
3996 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
3997 spin_unlock_irq(&phba->hbalock);
3998
3999 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05004000 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004001 "2777 Start post-quiescent FCF table scan\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05004002 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05004003 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05004004 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4005 "2747 Issue FCF scan read FCF mailbox "
4006 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05004007}
4008
4009/**
James Smartda0436e2009-05-22 14:51:39 -04004010 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
4011 * @phba: pointer to lpfc hba data structure.
4012 * @dev_grp: The HBA PCI-Device group number.
4013 *
4014 * This routine is invoked to set up the per HBA PCI-Device group function
4015 * API jump table entries.
4016 *
4017 * Return: 0 if success, otherwise -ENODEV
4018 **/
4019int
4020lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05004021{
4022 int rc;
4023
James Smartda0436e2009-05-22 14:51:39 -04004024 /* Set up lpfc PCI-device group */
4025 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05004026
James Smartda0436e2009-05-22 14:51:39 -04004027 /* The LPFC_PCI_DEV_OC uses SLI4 */
4028 if (dev_grp == LPFC_PCI_DEV_OC)
4029 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05004030
James Smartda0436e2009-05-22 14:51:39 -04004031 /* Set up device INIT API function jump table */
4032 rc = lpfc_init_api_table_setup(phba, dev_grp);
4033 if (rc)
4034 return -ENODEV;
4035 /* Set up SCSI API function jump table */
4036 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
4037 if (rc)
4038 return -ENODEV;
4039 /* Set up SLI API function jump table */
4040 rc = lpfc_sli_api_table_setup(phba, dev_grp);
4041 if (rc)
4042 return -ENODEV;
4043 /* Set up MBOX API function jump table */
4044 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
4045 if (rc)
4046 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004047
James Smartda0436e2009-05-22 14:51:39 -04004048 return 0;
James Smart5b75da22008-12-04 22:39:35 -05004049}
4050
4051/**
James Smart3621a712009-04-06 18:47:14 -04004052 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05004053 * @phba: pointer to lpfc hba data structure.
4054 * @intr_mode: active interrupt mode adopted.
4055 *
4056 * This routine it invoked to log the currently used active interrupt mode
4057 * to the device.
James Smart3772a992009-05-22 14:50:54 -04004058 **/
4059static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05004060{
4061 switch (intr_mode) {
4062 case 0:
4063 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4064 "0470 Enable INTx interrupt mode.\n");
4065 break;
4066 case 1:
4067 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4068 "0481 Enabled MSI interrupt mode.\n");
4069 break;
4070 case 2:
4071 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4072 "0480 Enabled MSI-X interrupt mode.\n");
4073 break;
4074 default:
4075 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4076 "0482 Illegal interrupt mode.\n");
4077 break;
4078 }
4079 return;
4080}
4081
James Smart5b75da22008-12-04 22:39:35 -05004082/**
James Smart3772a992009-05-22 14:50:54 -04004083 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05004084 * @phba: pointer to lpfc hba data structure.
4085 *
James Smart3772a992009-05-22 14:50:54 -04004086 * This routine is invoked to enable the PCI device that is common to all
4087 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004088 *
4089 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004090 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004091 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05004092 **/
James Smart3772a992009-05-22 14:50:54 -04004093static int
4094lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004095{
James Smart3772a992009-05-22 14:50:54 -04004096 struct pci_dev *pdev;
James Smart079b5c92011-08-21 21:48:49 -04004097 int bars = 0;
James Smart5b75da22008-12-04 22:39:35 -05004098
James Smart3772a992009-05-22 14:50:54 -04004099 /* Obtain PCI device reference */
4100 if (!phba->pcidev)
4101 goto out_error;
4102 else
4103 pdev = phba->pcidev;
4104 /* Select PCI BARs */
4105 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4106 /* Enable PCI device */
4107 if (pci_enable_device_mem(pdev))
4108 goto out_error;
4109 /* Request PCI resource for the device */
4110 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
4111 goto out_disable_device;
4112 /* Set up device as PCI master and save state for EEH */
4113 pci_set_master(pdev);
4114 pci_try_set_mwi(pdev);
4115 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05004116
James Smart05580562011-05-24 11:40:48 -04004117 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
4118 if (pci_find_capability(pdev, PCI_CAP_ID_EXP))
4119 pdev->needs_freset = 1;
4120
James Smart3772a992009-05-22 14:50:54 -04004121 return 0;
James Smart5b75da22008-12-04 22:39:35 -05004122
James Smart3772a992009-05-22 14:50:54 -04004123out_disable_device:
4124 pci_disable_device(pdev);
4125out_error:
James Smart079b5c92011-08-21 21:48:49 -04004126 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4127 "1401 Failed to enable pci device, bars:x%x\n", bars);
James Smart3772a992009-05-22 14:50:54 -04004128 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004129}
4130
4131/**
James Smart3772a992009-05-22 14:50:54 -04004132 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05004133 * @phba: pointer to lpfc hba data structure.
4134 *
James Smart3772a992009-05-22 14:50:54 -04004135 * This routine is invoked to disable the PCI device that is common to all
4136 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004137 **/
4138static void
James Smart3772a992009-05-22 14:50:54 -04004139lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004140{
James Smart3772a992009-05-22 14:50:54 -04004141 struct pci_dev *pdev;
4142 int bars;
James Smart5b75da22008-12-04 22:39:35 -05004143
James Smart3772a992009-05-22 14:50:54 -04004144 /* Obtain PCI device reference */
4145 if (!phba->pcidev)
4146 return;
4147 else
4148 pdev = phba->pcidev;
4149 /* Select PCI BARs */
4150 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4151 /* Release PCI resource and disable PCI device */
4152 pci_release_selected_regions(pdev, bars);
4153 pci_disable_device(pdev);
4154 /* Null out PCI private reference to driver */
4155 pci_set_drvdata(pdev, NULL);
James Smart5b75da22008-12-04 22:39:35 -05004156
4157 return;
4158}
4159
4160/**
James Smart3772a992009-05-22 14:50:54 -04004161 * lpfc_reset_hba - Reset a hba
4162 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04004163 *
James Smart3772a992009-05-22 14:50:54 -04004164 * This routine is invoked to reset a hba device. It brings the HBA
4165 * offline, performs a board restart, and then brings the board back
4166 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
4167 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04004168 **/
James Smart3772a992009-05-22 14:50:54 -04004169void
4170lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004171{
James Smart3772a992009-05-22 14:50:54 -04004172 /* If resets are disabled then set error state and return. */
4173 if (!phba->cfg_enable_hba_reset) {
4174 phba->link_state = LPFC_HBA_ERROR;
4175 return;
4176 }
4177 lpfc_offline_prep(phba);
4178 lpfc_offline(phba);
4179 lpfc_sli_brdrestart(phba);
4180 lpfc_online(phba);
4181 lpfc_unblock_mgmt_io(phba);
4182}
dea31012005-04-17 16:05:31 -05004183
James Smart3772a992009-05-22 14:50:54 -04004184/**
James Smart0a96e972011-07-22 18:37:28 -04004185 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
4186 * @phba: pointer to lpfc hba data structure.
4187 *
4188 * This function enables the PCI SR-IOV virtual functions to a physical
4189 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4190 * enable the number of virtual functions to the physical function. As
4191 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4192 * API call does not considered as an error condition for most of the device.
4193 **/
4194uint16_t
4195lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
4196{
4197 struct pci_dev *pdev = phba->pcidev;
4198 uint16_t nr_virtfn;
4199 int pos;
4200
James Smart0a96e972011-07-22 18:37:28 -04004201 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
4202 if (pos == 0)
4203 return 0;
4204
4205 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
4206 return nr_virtfn;
4207}
4208
4209/**
James Smart912e3ac2011-05-24 11:42:11 -04004210 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
4211 * @phba: pointer to lpfc hba data structure.
4212 * @nr_vfn: number of virtual functions to be enabled.
4213 *
4214 * This function enables the PCI SR-IOV virtual functions to a physical
4215 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4216 * enable the number of virtual functions to the physical function. As
4217 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4218 * API call does not considered as an error condition for most of the device.
4219 **/
4220int
4221lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
4222{
4223 struct pci_dev *pdev = phba->pcidev;
James Smart0a96e972011-07-22 18:37:28 -04004224 uint16_t max_nr_vfn;
James Smart912e3ac2011-05-24 11:42:11 -04004225 int rc;
4226
James Smart0a96e972011-07-22 18:37:28 -04004227 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
4228 if (nr_vfn > max_nr_vfn) {
4229 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4230 "3057 Requested vfs (%d) greater than "
4231 "supported vfs (%d)", nr_vfn, max_nr_vfn);
4232 return -EINVAL;
4233 }
4234
James Smart912e3ac2011-05-24 11:42:11 -04004235 rc = pci_enable_sriov(pdev, nr_vfn);
4236 if (rc) {
4237 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4238 "2806 Failed to enable sriov on this device "
4239 "with vfn number nr_vf:%d, rc:%d\n",
4240 nr_vfn, rc);
4241 } else
4242 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4243 "2807 Successful enable sriov on this device "
4244 "with vfn number nr_vf:%d\n", nr_vfn);
4245 return rc;
4246}
4247
4248/**
James Smart3772a992009-05-22 14:50:54 -04004249 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
4250 * @phba: pointer to lpfc hba data structure.
4251 *
4252 * This routine is invoked to set up the driver internal resources specific to
4253 * support the SLI-3 HBA device it attached to.
4254 *
4255 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004256 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004257 * other values - error
4258 **/
4259static int
4260lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
4261{
4262 struct lpfc_sli *psli;
James Smart912e3ac2011-05-24 11:42:11 -04004263 int rc;
dea31012005-04-17 16:05:31 -05004264
James Smart3772a992009-05-22 14:50:54 -04004265 /*
4266 * Initialize timers used by driver
4267 */
dea31012005-04-17 16:05:31 -05004268
James Smart3772a992009-05-22 14:50:54 -04004269 /* Heartbeat timer */
4270 init_timer(&phba->hb_tmofunc);
4271 phba->hb_tmofunc.function = lpfc_hb_timeout;
4272 phba->hb_tmofunc.data = (unsigned long)phba;
4273
4274 psli = &phba->sli;
4275 /* MBOX heartbeat timer */
4276 init_timer(&psli->mbox_tmo);
4277 psli->mbox_tmo.function = lpfc_mbox_timeout;
4278 psli->mbox_tmo.data = (unsigned long) phba;
4279 /* FCP polling mode timer */
4280 init_timer(&phba->fcp_poll_timer);
4281 phba->fcp_poll_timer.function = lpfc_poll_timeout;
4282 phba->fcp_poll_timer.data = (unsigned long) phba;
4283 /* Fabric block timer */
4284 init_timer(&phba->fabric_block_timer);
4285 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4286 phba->fabric_block_timer.data = (unsigned long) phba;
4287 /* EA polling mode timer */
4288 init_timer(&phba->eratt_poll);
4289 phba->eratt_poll.function = lpfc_poll_eratt;
4290 phba->eratt_poll.data = (unsigned long) phba;
4291
4292 /* Host attention work mask setup */
4293 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
4294 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
4295
4296 /* Get all the module params for configuring this host */
4297 lpfc_get_cfgparam(phba);
James Smart49198b32010-04-06 15:04:33 -04004298 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
4299 phba->menlo_flag |= HBA_MENLO_SUPPORT;
4300 /* check for menlo minimum sg count */
4301 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
4302 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
4303 }
4304
James Smart3772a992009-05-22 14:50:54 -04004305 /*
4306 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
4307 * used to create the sg_dma_buf_pool must be dynamically calculated.
4308 * 2 segments are added since the IOCB needs a command and response bde.
4309 */
4310 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4311 sizeof(struct fcp_rsp) +
4312 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
4313
4314 if (phba->cfg_enable_bg) {
4315 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT;
4316 phba->cfg_sg_dma_buf_size +=
4317 phba->cfg_prot_sg_seg_cnt * sizeof(struct ulp_bde64);
4318 }
4319
4320 /* Also reinitialize the host templates with new values. */
4321 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4322 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4323
4324 phba->max_vpi = LPFC_MAX_VPI;
4325 /* This will be set to correct value after config_port mbox */
4326 phba->max_vports = 0;
4327
4328 /*
4329 * Initialize the SLI Layer to run with lpfc HBAs.
4330 */
4331 lpfc_sli_setup(phba);
4332 lpfc_sli_queue_setup(phba);
4333
4334 /* Allocate device driver memory */
4335 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
4336 return -ENOMEM;
4337
James Smart912e3ac2011-05-24 11:42:11 -04004338 /*
4339 * Enable sr-iov virtual functions if supported and configured
4340 * through the module parameter.
4341 */
4342 if (phba->cfg_sriov_nr_virtfn > 0) {
4343 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
4344 phba->cfg_sriov_nr_virtfn);
4345 if (rc) {
4346 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4347 "2808 Requested number of SR-IOV "
4348 "virtual functions (%d) is not "
4349 "supported\n",
4350 phba->cfg_sriov_nr_virtfn);
4351 phba->cfg_sriov_nr_virtfn = 0;
4352 }
4353 }
4354
James Smart3772a992009-05-22 14:50:54 -04004355 return 0;
4356}
4357
4358/**
4359 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
4360 * @phba: pointer to lpfc hba data structure.
4361 *
4362 * This routine is invoked to unset the driver internal resources set up
4363 * specific for supporting the SLI-3 HBA device it attached to.
4364 **/
4365static void
4366lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
4367{
4368 /* Free device driver memory allocated */
4369 lpfc_mem_free_all(phba);
4370
4371 return;
4372}
4373
4374/**
James Smartda0436e2009-05-22 14:51:39 -04004375 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
4376 * @phba: pointer to lpfc hba data structure.
4377 *
4378 * This routine is invoked to set up the driver internal resources specific to
4379 * support the SLI-4 HBA device it attached to.
4380 *
4381 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004382 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004383 * other values - error
4384 **/
4385static int
4386lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
4387{
4388 struct lpfc_sli *psli;
James Smart28baac72010-02-12 14:42:03 -05004389 LPFC_MBOXQ_t *mboxq;
4390 int rc, i, hbq_count, buf_size, dma_buf_size, max_buf_size;
4391 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
4392 struct lpfc_mqe *mqe;
James Smart085c6472010-11-20 23:11:37 -05004393 int longs, sli_family;
James Smartacd68592012-01-18 16:25:09 -05004394 int sges_per_segment;
James Smartda0436e2009-05-22 14:51:39 -04004395
4396 /* Before proceed, wait for POST done and device ready */
4397 rc = lpfc_sli4_post_status_check(phba);
4398 if (rc)
4399 return -ENODEV;
4400
4401 /*
4402 * Initialize timers used by driver
4403 */
4404
4405 /* Heartbeat timer */
4406 init_timer(&phba->hb_tmofunc);
4407 phba->hb_tmofunc.function = lpfc_hb_timeout;
4408 phba->hb_tmofunc.data = (unsigned long)phba;
James Smart19ca7602010-11-20 23:11:55 -05004409 init_timer(&phba->rrq_tmr);
4410 phba->rrq_tmr.function = lpfc_rrq_timeout;
4411 phba->rrq_tmr.data = (unsigned long)phba;
James Smartda0436e2009-05-22 14:51:39 -04004412
4413 psli = &phba->sli;
4414 /* MBOX heartbeat timer */
4415 init_timer(&psli->mbox_tmo);
4416 psli->mbox_tmo.function = lpfc_mbox_timeout;
4417 psli->mbox_tmo.data = (unsigned long) phba;
4418 /* Fabric block timer */
4419 init_timer(&phba->fabric_block_timer);
4420 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4421 phba->fabric_block_timer.data = (unsigned long) phba;
4422 /* EA polling mode timer */
4423 init_timer(&phba->eratt_poll);
4424 phba->eratt_poll.function = lpfc_poll_eratt;
4425 phba->eratt_poll.data = (unsigned long) phba;
James Smartecfd03c2010-02-12 14:41:27 -05004426 /* FCF rediscover timer */
4427 init_timer(&phba->fcf.redisc_wait);
4428 phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
4429 phba->fcf.redisc_wait.data = (unsigned long)phba;
4430
James Smartda0436e2009-05-22 14:51:39 -04004431 /*
James Smart7ad20aa2011-05-24 11:44:28 -04004432 * Control structure for handling external multi-buffer mailbox
4433 * command pass-through.
4434 */
4435 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
4436 sizeof(struct lpfc_mbox_ext_buf_ctx));
4437 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
4438
4439 /*
James Smartda0436e2009-05-22 14:51:39 -04004440 * We need to do a READ_CONFIG mailbox command here before
4441 * calling lpfc_get_cfgparam. For VFs this will report the
4442 * MAX_XRI, MAX_VPI, MAX_RPI, MAX_IOCB, and MAX_VFI settings.
4443 * All of the resources allocated
4444 * for this Port are tied to these values.
4445 */
4446 /* Get all the module params for configuring this host */
4447 lpfc_get_cfgparam(phba);
4448 phba->max_vpi = LPFC_MAX_VPI;
4449 /* This will be set to correct value after the read_config mbox */
4450 phba->max_vports = 0;
4451
4452 /* Program the default value of vlan_id and fc_map */
4453 phba->valid_vlan = 0;
4454 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4455 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4456 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4457
James Smartacd68592012-01-18 16:25:09 -05004458 /* With BlockGuard we can have multiple SGEs per Data Segemnt */
4459 sges_per_segment = 1;
4460 if (phba->cfg_enable_bg)
4461 sges_per_segment = 2;
4462
James Smartda0436e2009-05-22 14:51:39 -04004463 /*
4464 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
4465 * used to create the sg_dma_buf_pool must be dynamically calculated.
4466 * 2 segments are added since the IOCB needs a command and response bde.
4467 * To insure that the scsi sgl does not cross a 4k page boundary only
James Smart28baac72010-02-12 14:42:03 -05004468 * sgl sizes of must be a power of 2.
James Smartda0436e2009-05-22 14:51:39 -04004469 */
James Smart28baac72010-02-12 14:42:03 -05004470 buf_size = (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp) +
James Smartacd68592012-01-18 16:25:09 -05004471 (((phba->cfg_sg_seg_cnt * sges_per_segment) + 2) *
4472 sizeof(struct sli4_sge)));
James Smart085c6472010-11-20 23:11:37 -05004473
4474 sli_family = bf_get(lpfc_sli_intf_sli_family, &phba->sli4_hba.sli_intf);
4475 max_buf_size = LPFC_SLI4_MAX_BUF_SIZE;
4476 switch (sli_family) {
4477 case LPFC_SLI_INTF_FAMILY_BE2:
4478 case LPFC_SLI_INTF_FAMILY_BE3:
4479 /* There is a single hint for BE - 2 pages per BPL. */
4480 if (bf_get(lpfc_sli_intf_sli_hint1, &phba->sli4_hba.sli_intf) ==
4481 LPFC_SLI_INTF_SLI_HINT1_1)
4482 max_buf_size = LPFC_SLI4_FL1_MAX_BUF_SIZE;
4483 break;
4484 case LPFC_SLI_INTF_FAMILY_LNCR_A0:
4485 case LPFC_SLI_INTF_FAMILY_LNCR_B0:
4486 default:
4487 break;
4488 }
James Smartacd68592012-01-18 16:25:09 -05004489
James Smart28baac72010-02-12 14:42:03 -05004490 for (dma_buf_size = LPFC_SLI4_MIN_BUF_SIZE;
4491 dma_buf_size < max_buf_size && buf_size > dma_buf_size;
4492 dma_buf_size = dma_buf_size << 1)
4493 ;
4494 if (dma_buf_size == max_buf_size)
4495 phba->cfg_sg_seg_cnt = (dma_buf_size -
4496 sizeof(struct fcp_cmnd) - sizeof(struct fcp_rsp) -
4497 (2 * sizeof(struct sli4_sge))) /
4498 sizeof(struct sli4_sge);
4499 phba->cfg_sg_dma_buf_size = dma_buf_size;
James Smartda0436e2009-05-22 14:51:39 -04004500
4501 /* Initialize buffer queue management fields */
4502 hbq_count = lpfc_sli_hbq_count();
4503 for (i = 0; i < hbq_count; ++i)
4504 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
4505 INIT_LIST_HEAD(&phba->rb_pend_list);
4506 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
4507 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
4508
4509 /*
4510 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
4511 */
4512 /* Initialize the Abort scsi buffer list used by driver */
4513 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
4514 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
4515 /* This abort list used by worker thread */
4516 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
4517
4518 /*
James Smart6d368e52011-05-24 11:44:12 -04004519 * Initialize driver internal slow-path work queues
James Smartda0436e2009-05-22 14:51:39 -04004520 */
4521
4522 /* Driver internel slow-path CQ Event pool */
4523 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
4524 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04004525 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04004526 /* Asynchronous event CQ Event work queue list */
4527 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
4528 /* Fast-path XRI aborted CQ Event work queue list */
4529 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
4530 /* Slow-path XRI aborted CQ Event work queue list */
4531 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
4532 /* Receive queue CQ Event work queue list */
4533 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
4534
James Smart6d368e52011-05-24 11:44:12 -04004535 /* Initialize extent block lists. */
4536 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
4537 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
4538 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
4539 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
4540
James Smartda0436e2009-05-22 14:51:39 -04004541 /* Initialize the driver internal SLI layer lists. */
4542 lpfc_sli_setup(phba);
4543 lpfc_sli_queue_setup(phba);
4544
4545 /* Allocate device driver memory */
4546 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
4547 if (rc)
4548 return -ENOMEM;
4549
James Smart2fcee4b2010-12-15 17:57:46 -05004550 /* IF Type 2 ports get initialized now. */
4551 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
4552 LPFC_SLI_INTF_IF_TYPE_2) {
4553 rc = lpfc_pci_function_reset(phba);
4554 if (unlikely(rc))
4555 return -ENODEV;
4556 }
4557
James Smartda0436e2009-05-22 14:51:39 -04004558 /* Create the bootstrap mailbox command */
4559 rc = lpfc_create_bootstrap_mbox(phba);
4560 if (unlikely(rc))
4561 goto out_free_mem;
4562
4563 /* Set up the host's endian order with the device. */
4564 rc = lpfc_setup_endian_order(phba);
4565 if (unlikely(rc))
4566 goto out_free_bsmbx;
4567
4568 /* Set up the hba's configuration parameters. */
4569 rc = lpfc_sli4_read_config(phba);
4570 if (unlikely(rc))
4571 goto out_free_bsmbx;
4572
James Smart2fcee4b2010-12-15 17:57:46 -05004573 /* IF Type 0 ports get initialized now. */
4574 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
4575 LPFC_SLI_INTF_IF_TYPE_0) {
4576 rc = lpfc_pci_function_reset(phba);
4577 if (unlikely(rc))
4578 goto out_free_bsmbx;
4579 }
James Smartda0436e2009-05-22 14:51:39 -04004580
James Smartcb5172e2010-03-15 11:25:07 -04004581 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
4582 GFP_KERNEL);
4583 if (!mboxq) {
4584 rc = -ENOMEM;
4585 goto out_free_bsmbx;
4586 }
4587
James Smartfedd3b72011-02-16 12:39:24 -05004588 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
James Smartcb5172e2010-03-15 11:25:07 -04004589 lpfc_supported_pages(mboxq);
4590 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smartfedd3b72011-02-16 12:39:24 -05004591 if (!rc) {
4592 mqe = &mboxq->u.mqe;
4593 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
4594 LPFC_MAX_SUPPORTED_PAGES);
4595 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
4596 switch (pn_page[i]) {
4597 case LPFC_SLI4_PARAMETERS:
4598 phba->sli4_hba.pc_sli4_params.supported = 1;
4599 break;
4600 default:
4601 break;
4602 }
4603 }
4604 /* Read the port's SLI4 Parameters capabilities if supported. */
4605 if (phba->sli4_hba.pc_sli4_params.supported)
4606 rc = lpfc_pc_sli4_params_get(phba, mboxq);
4607 if (rc) {
4608 mempool_free(mboxq, phba->mbox_mem_pool);
4609 rc = -EIO;
4610 goto out_free_bsmbx;
James Smartcb5172e2010-03-15 11:25:07 -04004611 }
4612 }
James Smartfedd3b72011-02-16 12:39:24 -05004613 /*
4614 * Get sli4 parameters that override parameters from Port capabilities.
James Smart6d368e52011-05-24 11:44:12 -04004615 * If this call fails, it isn't critical unless the SLI4 parameters come
4616 * back in conflict.
James Smartfedd3b72011-02-16 12:39:24 -05004617 */
James Smart6d368e52011-05-24 11:44:12 -04004618 rc = lpfc_get_sli4_parameters(phba, mboxq);
4619 if (rc) {
4620 if (phba->sli4_hba.extents_in_use &&
4621 phba->sli4_hba.rpi_hdrs_in_use) {
4622 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4623 "2999 Unsupported SLI4 Parameters "
4624 "Extents and RPI headers enabled.\n");
4625 goto out_free_bsmbx;
4626 }
4627 }
James Smartcb5172e2010-03-15 11:25:07 -04004628 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart5350d872011-10-10 21:33:49 -04004629 /* Verify all the SLI4 queues */
4630 rc = lpfc_sli4_queue_verify(phba);
James Smartda0436e2009-05-22 14:51:39 -04004631 if (rc)
4632 goto out_free_bsmbx;
4633
4634 /* Create driver internal CQE event pool */
4635 rc = lpfc_sli4_cq_event_pool_create(phba);
4636 if (rc)
James Smart5350d872011-10-10 21:33:49 -04004637 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04004638
4639 /* Initialize and populate the iocb list per host */
4640 rc = lpfc_init_sgl_list(phba);
4641 if (rc) {
4642 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4643 "1400 Failed to initialize sgl list.\n");
4644 goto out_destroy_cq_event_pool;
4645 }
4646 rc = lpfc_init_active_sgl_array(phba);
4647 if (rc) {
4648 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4649 "1430 Failed to initialize sgl list.\n");
4650 goto out_free_sgl_list;
4651 }
James Smartda0436e2009-05-22 14:51:39 -04004652 rc = lpfc_sli4_init_rpi_hdrs(phba);
4653 if (rc) {
4654 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4655 "1432 Failed to initialize rpi headers.\n");
4656 goto out_free_active_sgl;
4657 }
4658
James Smarta93ff372010-10-22 11:06:08 -04004659 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
James Smart0c9ab6f2010-02-26 14:15:57 -05004660 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
4661 phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
4662 GFP_KERNEL);
4663 if (!phba->fcf.fcf_rr_bmask) {
4664 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4665 "2759 Failed allocate memory for FCF round "
4666 "robin failover bmask\n");
James Smart05580562011-05-24 11:40:48 -04004667 rc = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -05004668 goto out_remove_rpi_hdrs;
4669 }
4670
James Smart5350d872011-10-10 21:33:49 -04004671 /*
4672 * The cfg_fcp_eq_count can be zero whenever there is exactly one
4673 * interrupt vector. This is not an error
4674 */
4675 if (phba->cfg_fcp_eq_count) {
4676 phba->sli4_hba.fcp_eq_hdl =
4677 kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
James Smartda0436e2009-05-22 14:51:39 -04004678 phba->cfg_fcp_eq_count), GFP_KERNEL);
James Smart5350d872011-10-10 21:33:49 -04004679 if (!phba->sli4_hba.fcp_eq_hdl) {
4680 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4681 "2572 Failed allocate memory for "
4682 "fast-path per-EQ handle array\n");
4683 rc = -ENOMEM;
4684 goto out_free_fcf_rr_bmask;
4685 }
James Smartda0436e2009-05-22 14:51:39 -04004686 }
4687
4688 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
4689 phba->sli4_hba.cfg_eqn), GFP_KERNEL);
4690 if (!phba->sli4_hba.msix_entries) {
4691 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4692 "2573 Failed allocate memory for msi-x "
4693 "interrupt vector entries\n");
James Smart05580562011-05-24 11:40:48 -04004694 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04004695 goto out_free_fcp_eq_hdl;
4696 }
4697
James Smart912e3ac2011-05-24 11:42:11 -04004698 /*
4699 * Enable sr-iov virtual functions if supported and configured
4700 * through the module parameter.
4701 */
4702 if (phba->cfg_sriov_nr_virtfn > 0) {
4703 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
4704 phba->cfg_sriov_nr_virtfn);
4705 if (rc) {
4706 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4707 "3020 Requested number of SR-IOV "
4708 "virtual functions (%d) is not "
4709 "supported\n",
4710 phba->cfg_sriov_nr_virtfn);
4711 phba->cfg_sriov_nr_virtfn = 0;
4712 }
4713 }
4714
James Smart5248a742011-07-22 18:37:06 -04004715 return 0;
James Smartda0436e2009-05-22 14:51:39 -04004716
4717out_free_fcp_eq_hdl:
4718 kfree(phba->sli4_hba.fcp_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05004719out_free_fcf_rr_bmask:
4720 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04004721out_remove_rpi_hdrs:
4722 lpfc_sli4_remove_rpi_hdrs(phba);
4723out_free_active_sgl:
4724 lpfc_free_active_sgl(phba);
4725out_free_sgl_list:
4726 lpfc_free_sgl_list(phba);
4727out_destroy_cq_event_pool:
4728 lpfc_sli4_cq_event_pool_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04004729out_free_bsmbx:
4730 lpfc_destroy_bootstrap_mbox(phba);
4731out_free_mem:
4732 lpfc_mem_free(phba);
4733 return rc;
4734}
4735
4736/**
4737 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
4738 * @phba: pointer to lpfc hba data structure.
4739 *
4740 * This routine is invoked to unset the driver internal resources set up
4741 * specific for supporting the SLI-4 HBA device it attached to.
4742 **/
4743static void
4744lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
4745{
4746 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
4747
James Smartda0436e2009-05-22 14:51:39 -04004748 /* Free memory allocated for msi-x interrupt vector entries */
4749 kfree(phba->sli4_hba.msix_entries);
4750
4751 /* Free memory allocated for fast-path work queue handles */
4752 kfree(phba->sli4_hba.fcp_eq_hdl);
4753
4754 /* Free the allocated rpi headers. */
4755 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04004756 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04004757
James Smart0c9ab6f2010-02-26 14:15:57 -05004758 /* Free eligible FCF index bmask */
4759 kfree(phba->fcf.fcf_rr_bmask);
4760
James Smartda0436e2009-05-22 14:51:39 -04004761 /* Free the ELS sgl list */
4762 lpfc_free_active_sgl(phba);
4763 lpfc_free_sgl_list(phba);
4764
4765 /* Free the SCSI sgl management array */
4766 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
4767
James Smartda0436e2009-05-22 14:51:39 -04004768 /* Free the completion queue EQ event pool */
4769 lpfc_sli4_cq_event_release_all(phba);
4770 lpfc_sli4_cq_event_pool_destroy(phba);
4771
James Smart6d368e52011-05-24 11:44:12 -04004772 /* Release resource identifiers. */
4773 lpfc_sli4_dealloc_resource_identifiers(phba);
4774
James Smartda0436e2009-05-22 14:51:39 -04004775 /* Free the bsmbx region. */
4776 lpfc_destroy_bootstrap_mbox(phba);
4777
4778 /* Free the SLI Layer memory with SLI4 HBAs */
4779 lpfc_mem_free_all(phba);
4780
4781 /* Free the current connect table */
4782 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04004783 &phba->fcf_conn_rec_list, list) {
4784 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04004785 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04004786 }
James Smartda0436e2009-05-22 14:51:39 -04004787
4788 return;
4789}
4790
4791/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004792 * lpfc_init_api_table_setup - Set up init api function jump table
James Smart3772a992009-05-22 14:50:54 -04004793 * @phba: The hba struct for which this call is being executed.
4794 * @dev_grp: The HBA PCI-Device group number.
4795 *
4796 * This routine sets up the device INIT interface API function jump table
4797 * in @phba struct.
4798 *
4799 * Returns: 0 - success, -ENODEV - failure.
4800 **/
4801int
4802lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4803{
James Smart84d1b002010-02-12 14:42:33 -05004804 phba->lpfc_hba_init_link = lpfc_hba_init_link;
4805 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart7f860592011-03-11 16:05:52 -05004806 phba->lpfc_selective_reset = lpfc_selective_reset;
James Smart3772a992009-05-22 14:50:54 -04004807 switch (dev_grp) {
4808 case LPFC_PCI_DEV_LP:
4809 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
4810 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
4811 phba->lpfc_stop_port = lpfc_stop_port_s3;
4812 break;
James Smartda0436e2009-05-22 14:51:39 -04004813 case LPFC_PCI_DEV_OC:
4814 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
4815 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
4816 phba->lpfc_stop_port = lpfc_stop_port_s4;
4817 break;
James Smart3772a992009-05-22 14:50:54 -04004818 default:
4819 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4820 "1431 Invalid HBA PCI-device group: 0x%x\n",
4821 dev_grp);
4822 return -ENODEV;
4823 break;
4824 }
4825 return 0;
4826}
4827
4828/**
4829 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
4830 * @phba: pointer to lpfc hba data structure.
4831 *
4832 * This routine is invoked to set up the driver internal resources before the
4833 * device specific resource setup to support the HBA device it attached to.
4834 *
4835 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004836 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004837 * other values - error
4838 **/
4839static int
4840lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
4841{
4842 /*
4843 * Driver resources common to all SLI revisions
4844 */
James Smartea2151b2008-09-07 11:52:10 -04004845 atomic_set(&phba->fast_event_count, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004846 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004847
James Smarte47c9092008-02-08 18:49:26 -05004848 /* Initialize ndlp management spinlock */
4849 spin_lock_init(&phba->ndlp_lock);
4850
James Smart2e0fef82007-06-17 19:56:36 -05004851 INIT_LIST_HEAD(&phba->port_list);
James Smart3772a992009-05-22 14:50:54 -04004852 INIT_LIST_HEAD(&phba->work_list);
James Smart84774a42008-08-24 21:50:06 -04004853 init_waitqueue_head(&phba->wait_4_mlo_m_q);
dea31012005-04-17 16:05:31 -05004854
James Smart3772a992009-05-22 14:50:54 -04004855 /* Initialize the wait queue head for the kernel thread */
4856 init_waitqueue_head(&phba->work_waitq);
James Smart858c9f62007-06-17 19:56:39 -05004857
James Smart3772a992009-05-22 14:50:54 -04004858 /* Initialize the scsi buffer list used by driver for scsi IO */
4859 spin_lock_init(&phba->scsi_buf_list_lock);
4860 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
dea31012005-04-17 16:05:31 -05004861
James Smart3772a992009-05-22 14:50:54 -04004862 /* Initialize the fabric iocb list */
4863 INIT_LIST_HEAD(&phba->fabric_iocb_list);
dea31012005-04-17 16:05:31 -05004864
James Smart3772a992009-05-22 14:50:54 -04004865 /* Initialize list to save ELS buffers */
4866 INIT_LIST_HEAD(&phba->elsbuf);
dea31012005-04-17 16:05:31 -05004867
James Smart3772a992009-05-22 14:50:54 -04004868 /* Initialize FCF connection rec list */
4869 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
dea31012005-04-17 16:05:31 -05004870
James Smart3772a992009-05-22 14:50:54 -04004871 return 0;
4872}
dea31012005-04-17 16:05:31 -05004873
James Smart3772a992009-05-22 14:50:54 -04004874/**
4875 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
4876 * @phba: pointer to lpfc hba data structure.
4877 *
4878 * This routine is invoked to set up the driver internal resources after the
4879 * device specific resource setup to support the HBA device it attached to.
4880 *
4881 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004882 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004883 * other values - error
4884 **/
4885static int
4886lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
4887{
4888 int error;
4889
4890 /* Startup the kernel thread for this host adapter. */
4891 phba->worker_thread = kthread_run(lpfc_do_work, phba,
4892 "lpfc_worker_%d", phba->brd_no);
4893 if (IS_ERR(phba->worker_thread)) {
4894 error = PTR_ERR(phba->worker_thread);
4895 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05004896 }
4897
James Smart3772a992009-05-22 14:50:54 -04004898 return 0;
4899}
4900
4901/**
4902 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
4903 * @phba: pointer to lpfc hba data structure.
4904 *
4905 * This routine is invoked to unset the driver internal resources set up after
4906 * the device specific resource setup for supporting the HBA device it
4907 * attached to.
4908 **/
4909static void
4910lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
4911{
4912 /* Stop kernel worker thread */
4913 kthread_stop(phba->worker_thread);
4914}
4915
4916/**
4917 * lpfc_free_iocb_list - Free iocb list.
4918 * @phba: pointer to lpfc hba data structure.
4919 *
4920 * This routine is invoked to free the driver's IOCB list and memory.
4921 **/
4922static void
4923lpfc_free_iocb_list(struct lpfc_hba *phba)
4924{
4925 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
4926
4927 spin_lock_irq(&phba->hbalock);
4928 list_for_each_entry_safe(iocbq_entry, iocbq_next,
4929 &phba->lpfc_iocb_list, list) {
4930 list_del(&iocbq_entry->list);
4931 kfree(iocbq_entry);
4932 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05004933 }
James Smart3772a992009-05-22 14:50:54 -04004934 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004935
James Smart3772a992009-05-22 14:50:54 -04004936 return;
4937}
dea31012005-04-17 16:05:31 -05004938
James Smart3772a992009-05-22 14:50:54 -04004939/**
4940 * lpfc_init_iocb_list - Allocate and initialize iocb list.
4941 * @phba: pointer to lpfc hba data structure.
4942 *
4943 * This routine is invoked to allocate and initizlize the driver's IOCB
4944 * list and set up the IOCB tag array accordingly.
4945 *
4946 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004947 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004948 * other values - error
4949 **/
4950static int
4951lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
4952{
4953 struct lpfc_iocbq *iocbq_entry = NULL;
4954 uint16_t iotag;
4955 int i;
dea31012005-04-17 16:05:31 -05004956
4957 /* Initialize and populate the iocb list per host. */
4958 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04004959 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07004960 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05004961 if (iocbq_entry == NULL) {
4962 printk(KERN_ERR "%s: only allocated %d iocbs of "
4963 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07004964 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05004965 goto out_free_iocbq;
4966 }
4967
James Bottomley604a3e32005-10-29 10:28:33 -05004968 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
4969 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04004970 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05004971 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04004972 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05004973 goto out_free_iocbq;
4974 }
James Smart6d368e52011-05-24 11:44:12 -04004975 iocbq_entry->sli4_lxritag = NO_XRI;
James Smart3772a992009-05-22 14:50:54 -04004976 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05004977
4978 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004979 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
4980 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05004981 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004982 }
4983
James Smart3772a992009-05-22 14:50:54 -04004984 return 0;
4985
4986out_free_iocbq:
4987 lpfc_free_iocb_list(phba);
4988
4989 return -ENOMEM;
4990}
4991
4992/**
James Smartda0436e2009-05-22 14:51:39 -04004993 * lpfc_free_sgl_list - Free sgl list.
4994 * @phba: pointer to lpfc hba data structure.
4995 *
4996 * This routine is invoked to free the driver's sgl list and memory.
4997 **/
4998static void
4999lpfc_free_sgl_list(struct lpfc_hba *phba)
5000{
5001 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
5002 LIST_HEAD(sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04005003
5004 spin_lock_irq(&phba->hbalock);
5005 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
5006 spin_unlock_irq(&phba->hbalock);
5007
5008 list_for_each_entry_safe(sglq_entry, sglq_next,
5009 &sglq_list, list) {
5010 list_del(&sglq_entry->list);
5011 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
5012 kfree(sglq_entry);
5013 phba->sli4_hba.total_sglq_bufs--;
5014 }
James Smartda0436e2009-05-22 14:51:39 -04005015 kfree(phba->sli4_hba.lpfc_els_sgl_array);
5016}
5017
5018/**
5019 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
5020 * @phba: pointer to lpfc hba data structure.
5021 *
5022 * This routine is invoked to allocate the driver's active sgl memory.
5023 * This array will hold the sglq_entry's for active IOs.
5024 **/
5025static int
5026lpfc_init_active_sgl_array(struct lpfc_hba *phba)
5027{
5028 int size;
5029 size = sizeof(struct lpfc_sglq *);
5030 size *= phba->sli4_hba.max_cfg_param.max_xri;
5031
5032 phba->sli4_hba.lpfc_sglq_active_list =
5033 kzalloc(size, GFP_KERNEL);
5034 if (!phba->sli4_hba.lpfc_sglq_active_list)
5035 return -ENOMEM;
5036 return 0;
5037}
5038
5039/**
5040 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
5041 * @phba: pointer to lpfc hba data structure.
5042 *
5043 * This routine is invoked to walk through the array of active sglq entries
5044 * and free all of the resources.
5045 * This is just a place holder for now.
5046 **/
5047static void
5048lpfc_free_active_sgl(struct lpfc_hba *phba)
5049{
5050 kfree(phba->sli4_hba.lpfc_sglq_active_list);
5051}
5052
5053/**
5054 * lpfc_init_sgl_list - Allocate and initialize sgl list.
5055 * @phba: pointer to lpfc hba data structure.
5056 *
5057 * This routine is invoked to allocate and initizlize the driver's sgl
5058 * list and set up the sgl xritag tag array accordingly.
5059 *
5060 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005061 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005062 * other values - error
5063 **/
5064static int
5065lpfc_init_sgl_list(struct lpfc_hba *phba)
5066{
5067 struct lpfc_sglq *sglq_entry = NULL;
5068 int i;
5069 int els_xri_cnt;
5070
5071 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
5072 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart6d368e52011-05-24 11:44:12 -04005073 "2400 ELS XRI count %d.\n",
James Smartda0436e2009-05-22 14:51:39 -04005074 els_xri_cnt);
5075 /* Initialize and populate the sglq list per host/VF. */
5076 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
5077 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
5078
5079 /* Sanity check on XRI management */
5080 if (phba->sli4_hba.max_cfg_param.max_xri <= els_xri_cnt) {
5081 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5082 "2562 No room left for SCSI XRI allocation: "
5083 "max_xri=%d, els_xri=%d\n",
5084 phba->sli4_hba.max_cfg_param.max_xri,
5085 els_xri_cnt);
5086 return -ENOMEM;
5087 }
5088
5089 /* Allocate memory for the ELS XRI management array */
5090 phba->sli4_hba.lpfc_els_sgl_array =
5091 kzalloc((sizeof(struct lpfc_sglq *) * els_xri_cnt),
5092 GFP_KERNEL);
5093
5094 if (!phba->sli4_hba.lpfc_els_sgl_array) {
5095 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5096 "2401 Failed to allocate memory for ELS "
5097 "XRI management array of size %d.\n",
5098 els_xri_cnt);
5099 return -ENOMEM;
5100 }
5101
5102 /* Keep the SCSI XRI into the XRI management array */
5103 phba->sli4_hba.scsi_xri_max =
5104 phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
5105 phba->sli4_hba.scsi_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04005106 phba->sli4_hba.lpfc_scsi_psb_array =
5107 kzalloc((sizeof(struct lpfc_scsi_buf *) *
5108 phba->sli4_hba.scsi_xri_max), GFP_KERNEL);
5109
5110 if (!phba->sli4_hba.lpfc_scsi_psb_array) {
5111 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5112 "2563 Failed to allocate memory for SCSI "
5113 "XRI management array of size %d.\n",
5114 phba->sli4_hba.scsi_xri_max);
5115 kfree(phba->sli4_hba.lpfc_els_sgl_array);
5116 return -ENOMEM;
5117 }
5118
5119 for (i = 0; i < els_xri_cnt; i++) {
5120 sglq_entry = kzalloc(sizeof(struct lpfc_sglq), GFP_KERNEL);
5121 if (sglq_entry == NULL) {
5122 printk(KERN_ERR "%s: only allocated %d sgls of "
5123 "expected %d count. Unloading driver.\n",
5124 __func__, i, els_xri_cnt);
5125 goto out_free_mem;
5126 }
5127
James Smartda0436e2009-05-22 14:51:39 -04005128 sglq_entry->buff_type = GEN_BUFF_TYPE;
5129 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0, &sglq_entry->phys);
5130 if (sglq_entry->virt == NULL) {
5131 kfree(sglq_entry);
5132 printk(KERN_ERR "%s: failed to allocate mbuf.\n"
5133 "Unloading driver.\n", __func__);
5134 goto out_free_mem;
5135 }
5136 sglq_entry->sgl = sglq_entry->virt;
5137 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
5138
5139 /* The list order is used by later block SGL registraton */
5140 spin_lock_irq(&phba->hbalock);
James Smart0f65ff62010-02-26 14:14:23 -05005141 sglq_entry->state = SGL_FREED;
James Smartda0436e2009-05-22 14:51:39 -04005142 list_add_tail(&sglq_entry->list, &phba->sli4_hba.lpfc_sgl_list);
5143 phba->sli4_hba.lpfc_els_sgl_array[i] = sglq_entry;
5144 phba->sli4_hba.total_sglq_bufs++;
5145 spin_unlock_irq(&phba->hbalock);
5146 }
5147 return 0;
5148
5149out_free_mem:
5150 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
5151 lpfc_free_sgl_list(phba);
5152 return -ENOMEM;
5153}
5154
5155/**
5156 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
5157 * @phba: pointer to lpfc hba data structure.
5158 *
5159 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -04005160 * port for those SLI4 ports that do not support extents. This routine
James Smartda0436e2009-05-22 14:51:39 -04005161 * posts a PAGE_SIZE memory region to the port to hold up to
James Smart88a2cfb2011-07-22 18:36:33 -04005162 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
5163 * and should be called only when interrupts are disabled.
James Smartda0436e2009-05-22 14:51:39 -04005164 *
5165 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005166 * 0 - successful
James Smart88a2cfb2011-07-22 18:36:33 -04005167 * -ERROR - otherwise.
James Smartda0436e2009-05-22 14:51:39 -04005168 **/
5169int
5170lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
5171{
5172 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04005173 struct lpfc_rpi_hdr *rpi_hdr;
5174
5175 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
James Smartff78d8f2011-12-13 13:21:35 -05005176 if (!phba->sli4_hba.rpi_hdrs_in_use)
James Smart6d368e52011-05-24 11:44:12 -04005177 return rc;
James Smart6d368e52011-05-24 11:44:12 -04005178 if (phba->sli4_hba.extents_in_use)
5179 return -EIO;
James Smartda0436e2009-05-22 14:51:39 -04005180
5181 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
5182 if (!rpi_hdr) {
5183 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5184 "0391 Error during rpi post operation\n");
5185 lpfc_sli4_remove_rpis(phba);
5186 rc = -ENODEV;
5187 }
5188
5189 return rc;
5190}
5191
5192/**
5193 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
5194 * @phba: pointer to lpfc hba data structure.
5195 *
5196 * This routine is invoked to allocate a single 4KB memory region to
5197 * support rpis and stores them in the phba. This single region
5198 * provides support for up to 64 rpis. The region is used globally
5199 * by the device.
5200 *
5201 * Returns:
5202 * A valid rpi hdr on success.
5203 * A NULL pointer on any failure.
5204 **/
5205struct lpfc_rpi_hdr *
5206lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
5207{
5208 uint16_t rpi_limit, curr_rpi_range;
5209 struct lpfc_dmabuf *dmabuf;
5210 struct lpfc_rpi_hdr *rpi_hdr;
James Smart9589b0622011-04-16 11:03:17 -04005211 uint32_t rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04005212
James Smart6d368e52011-05-24 11:44:12 -04005213 /*
5214 * If the SLI4 port supports extents, posting the rpi header isn't
5215 * required. Set the expected maximum count and let the actual value
5216 * get set when extents are fully allocated.
5217 */
5218 if (!phba->sli4_hba.rpi_hdrs_in_use)
5219 return NULL;
5220 if (phba->sli4_hba.extents_in_use)
5221 return NULL;
5222
5223 /* The limit on the logical index is just the max_rpi count. */
James Smartda0436e2009-05-22 14:51:39 -04005224 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
James Smart6d368e52011-05-24 11:44:12 -04005225 phba->sli4_hba.max_cfg_param.max_rpi - 1;
James Smartda0436e2009-05-22 14:51:39 -04005226
5227 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04005228 /*
5229 * Establish the starting RPI in this header block. The starting
5230 * rpi is normalized to a zero base because the physical rpi is
5231 * port based.
5232 */
5233 curr_rpi_range = phba->sli4_hba.next_rpi -
5234 phba->sli4_hba.max_cfg_param.rpi_base;
James Smartda0436e2009-05-22 14:51:39 -04005235 spin_unlock_irq(&phba->hbalock);
5236
5237 /*
5238 * The port has a limited number of rpis. The increment here
5239 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
5240 * and to allow the full max_rpi range per port.
5241 */
5242 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
James Smart9589b0622011-04-16 11:03:17 -04005243 rpi_count = rpi_limit - curr_rpi_range;
5244 else
5245 rpi_count = LPFC_RPI_HDR_COUNT;
James Smartda0436e2009-05-22 14:51:39 -04005246
James Smart6d368e52011-05-24 11:44:12 -04005247 if (!rpi_count)
5248 return NULL;
James Smartda0436e2009-05-22 14:51:39 -04005249 /*
5250 * First allocate the protocol header region for the port. The
5251 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
5252 */
5253 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5254 if (!dmabuf)
5255 return NULL;
5256
5257 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5258 LPFC_HDR_TEMPLATE_SIZE,
5259 &dmabuf->phys,
5260 GFP_KERNEL);
5261 if (!dmabuf->virt) {
5262 rpi_hdr = NULL;
5263 goto err_free_dmabuf;
5264 }
5265
5266 memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
5267 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
5268 rpi_hdr = NULL;
5269 goto err_free_coherent;
5270 }
5271
5272 /* Save the rpi header data for cleanup later. */
5273 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
5274 if (!rpi_hdr)
5275 goto err_free_coherent;
5276
5277 rpi_hdr->dmabuf = dmabuf;
5278 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
5279 rpi_hdr->page_count = 1;
5280 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04005281
5282 /* The rpi_hdr stores the logical index only. */
5283 rpi_hdr->start_rpi = curr_rpi_range;
James Smartda0436e2009-05-22 14:51:39 -04005284 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
5285
5286 /*
James Smart6d368e52011-05-24 11:44:12 -04005287 * The next_rpi stores the next logical module-64 rpi value used
5288 * to post physical rpis in subsequent rpi postings.
James Smartda0436e2009-05-22 14:51:39 -04005289 */
James Smart9589b0622011-04-16 11:03:17 -04005290 phba->sli4_hba.next_rpi += rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04005291 spin_unlock_irq(&phba->hbalock);
5292 return rpi_hdr;
5293
5294 err_free_coherent:
5295 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
5296 dmabuf->virt, dmabuf->phys);
5297 err_free_dmabuf:
5298 kfree(dmabuf);
5299 return NULL;
5300}
5301
5302/**
5303 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
5304 * @phba: pointer to lpfc hba data structure.
5305 *
5306 * This routine is invoked to remove all memory resources allocated
James Smart6d368e52011-05-24 11:44:12 -04005307 * to support rpis for SLI4 ports not supporting extents. This routine
5308 * presumes the caller has released all rpis consumed by fabric or port
5309 * logins and is prepared to have the header pages removed.
James Smartda0436e2009-05-22 14:51:39 -04005310 **/
5311void
5312lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
5313{
5314 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
5315
James Smart6d368e52011-05-24 11:44:12 -04005316 if (!phba->sli4_hba.rpi_hdrs_in_use)
5317 goto exit;
5318
James Smartda0436e2009-05-22 14:51:39 -04005319 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
5320 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
5321 list_del(&rpi_hdr->list);
5322 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
5323 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
5324 kfree(rpi_hdr->dmabuf);
5325 kfree(rpi_hdr);
5326 }
James Smart6d368e52011-05-24 11:44:12 -04005327 exit:
5328 /* There are no rpis available to the port now. */
5329 phba->sli4_hba.next_rpi = 0;
James Smartda0436e2009-05-22 14:51:39 -04005330}
5331
5332/**
James Smart3772a992009-05-22 14:50:54 -04005333 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
5334 * @pdev: pointer to pci device data structure.
5335 *
5336 * This routine is invoked to allocate the driver hba data structure for an
5337 * HBA device. If the allocation is successful, the phba reference to the
5338 * PCI device data structure is set.
5339 *
5340 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005341 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04005342 * NULL - error
5343 **/
5344static struct lpfc_hba *
5345lpfc_hba_alloc(struct pci_dev *pdev)
5346{
5347 struct lpfc_hba *phba;
5348
5349 /* Allocate memory for HBA structure */
5350 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
5351 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02005352 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04005353 return NULL;
5354 }
5355
5356 /* Set reference to PCI device in HBA structure */
5357 phba->pcidev = pdev;
5358
5359 /* Assign an unused board number */
5360 phba->brd_no = lpfc_get_instance();
5361 if (phba->brd_no < 0) {
5362 kfree(phba);
5363 return NULL;
5364 }
5365
James Smart4fede782010-01-26 23:08:55 -05005366 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04005367 INIT_LIST_HEAD(&phba->ct_ev_waiters);
5368
James Smart3772a992009-05-22 14:50:54 -04005369 return phba;
5370}
5371
5372/**
5373 * lpfc_hba_free - Free driver hba data structure with a device.
5374 * @phba: pointer to lpfc hba data structure.
5375 *
5376 * This routine is invoked to free the driver hba data structure with an
5377 * HBA device.
5378 **/
5379static void
5380lpfc_hba_free(struct lpfc_hba *phba)
5381{
5382 /* Release the driver assigned board number */
5383 idr_remove(&lpfc_hba_index, phba->brd_no);
5384
5385 kfree(phba);
5386 return;
5387}
5388
5389/**
5390 * lpfc_create_shost - Create hba physical port with associated scsi host.
5391 * @phba: pointer to lpfc hba data structure.
5392 *
5393 * This routine is invoked to create HBA physical port and associate a SCSI
5394 * host with it.
5395 *
5396 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005397 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005398 * other values - error
5399 **/
5400static int
5401lpfc_create_shost(struct lpfc_hba *phba)
5402{
5403 struct lpfc_vport *vport;
5404 struct Scsi_Host *shost;
5405
5406 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05005407 phba->fc_edtov = FF_DEF_EDTOV;
5408 phba->fc_ratov = FF_DEF_RATOV;
5409 phba->fc_altov = FF_DEF_ALTOV;
5410 phba->fc_arbtov = FF_DEF_ARBTOV;
5411
James Smartd7c47992010-06-08 18:31:54 -04005412 atomic_set(&phba->sdev_cnt, 0);
James Smart3de2a652007-08-02 11:09:59 -04005413 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05005414 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04005415 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05005416
5417 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05005418 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05005419 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04005420 /* Put reference to SCSI host to driver's device private data */
5421 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05005422
James Smart3772a992009-05-22 14:50:54 -04005423 return 0;
5424}
dea31012005-04-17 16:05:31 -05005425
James Smart3772a992009-05-22 14:50:54 -04005426/**
5427 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
5428 * @phba: pointer to lpfc hba data structure.
5429 *
5430 * This routine is invoked to destroy HBA physical port and the associated
5431 * SCSI host.
5432 **/
5433static void
5434lpfc_destroy_shost(struct lpfc_hba *phba)
5435{
5436 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04005437
James Smart3772a992009-05-22 14:50:54 -04005438 /* Destroy physical port that associated with the SCSI host */
5439 destroy_port(vport);
5440
5441 return;
5442}
5443
5444/**
5445 * lpfc_setup_bg - Setup Block guard structures and debug areas.
5446 * @phba: pointer to lpfc hba data structure.
5447 * @shost: the shost to be used to detect Block guard settings.
5448 *
5449 * This routine sets up the local Block guard protocol settings for @shost.
5450 * This routine also allocates memory for debugging bg buffers.
5451 **/
5452static void
5453lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
5454{
5455 int pagecnt = 10;
5456 if (lpfc_prot_mask && lpfc_prot_guard) {
5457 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5458 "1478 Registering BlockGuard with the "
5459 "SCSI layer\n");
5460 scsi_host_set_prot(shost, lpfc_prot_mask);
5461 scsi_host_set_guard(shost, lpfc_prot_guard);
James Smart98c9ea52007-10-27 13:37:33 -04005462 }
James Smart81301a92008-12-04 22:39:46 -05005463 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05005464 while (pagecnt) {
5465 spin_lock_init(&_dump_buf_lock);
5466 _dump_buf_data =
5467 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
5468 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04005469 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5470 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04005471 "_dump_buf_data at 0x%p\n",
5472 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05005473 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04005474 memset(_dump_buf_data, 0,
5475 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05005476 break;
James Smart3772a992009-05-22 14:50:54 -04005477 } else
James Smart81301a92008-12-04 22:39:46 -05005478 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05005479 }
James Smart81301a92008-12-04 22:39:46 -05005480 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04005481 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5482 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04005483 "memory for hexdump\n");
5484 } else
James Smart6a9c52c2009-10-02 15:16:51 -04005485 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5486 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05005487 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05005488 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05005489 while (pagecnt) {
5490 _dump_buf_dif =
5491 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
5492 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04005493 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5494 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04005495 "_dump_buf_dif at 0x%p\n",
5496 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05005497 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04005498 memset(_dump_buf_dif, 0,
5499 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05005500 break;
James Smart3772a992009-05-22 14:50:54 -04005501 } else
James Smart81301a92008-12-04 22:39:46 -05005502 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05005503 }
James Smart81301a92008-12-04 22:39:46 -05005504 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04005505 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5506 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04005507 "memory for hexdump\n");
5508 } else
James Smart6a9c52c2009-10-02 15:16:51 -04005509 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5510 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04005511 _dump_buf_dif);
5512}
5513
5514/**
5515 * lpfc_post_init_setup - Perform necessary device post initialization setup.
5516 * @phba: pointer to lpfc hba data structure.
5517 *
5518 * This routine is invoked to perform all the necessary post initialization
5519 * setup for the device.
5520 **/
5521static void
5522lpfc_post_init_setup(struct lpfc_hba *phba)
5523{
5524 struct Scsi_Host *shost;
5525 struct lpfc_adapter_event_header adapter_event;
5526
5527 /* Get the default values for Model Name and Description */
5528 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
5529
5530 /*
5531 * hba setup may have changed the hba_queue_depth so we need to
5532 * adjust the value of can_queue.
5533 */
5534 shost = pci_get_drvdata(phba->pcidev);
5535 shost->can_queue = phba->cfg_hba_queue_depth - 10;
5536 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
5537 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05005538
5539 lpfc_host_attrib_init(shost);
5540
James Smart2e0fef82007-06-17 19:56:36 -05005541 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
5542 spin_lock_irq(shost->host_lock);
5543 lpfc_poll_start_timer(phba);
5544 spin_unlock_irq(shost->host_lock);
5545 }
James Smart8f6d98d2006-08-01 07:34:00 -04005546
James Smart93996272008-08-24 21:50:30 -04005547 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5548 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04005549 /* Send board arrival event to upper layer */
5550 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
5551 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
5552 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04005553 sizeof(adapter_event),
5554 (char *) &adapter_event,
5555 LPFC_NL_VENDOR_ID);
5556 return;
5557}
5558
5559/**
5560 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
5561 * @phba: pointer to lpfc hba data structure.
5562 *
5563 * This routine is invoked to set up the PCI device memory space for device
5564 * with SLI-3 interface spec.
5565 *
5566 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005567 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005568 * other values - error
5569 **/
5570static int
5571lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
5572{
5573 struct pci_dev *pdev;
5574 unsigned long bar0map_len, bar2map_len;
5575 int i, hbq_count;
5576 void *ptr;
5577 int error = -ENODEV;
5578
5579 /* Obtain PCI device reference */
5580 if (!phba->pcidev)
5581 return error;
5582 else
5583 pdev = phba->pcidev;
5584
5585 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05005586 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
5587 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
5588 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
5589 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smart3772a992009-05-22 14:50:54 -04005590 return error;
Michael Reed8e685972009-09-18 12:02:05 -05005591 }
5592 }
James Smart3772a992009-05-22 14:50:54 -04005593
5594 /* Get the bus address of Bar0 and Bar2 and the number of bytes
5595 * required by each mapping.
5596 */
5597 phba->pci_bar0_map = pci_resource_start(pdev, 0);
5598 bar0map_len = pci_resource_len(pdev, 0);
5599
5600 phba->pci_bar2_map = pci_resource_start(pdev, 2);
5601 bar2map_len = pci_resource_len(pdev, 2);
5602
5603 /* Map HBA SLIM to a kernel virtual address. */
5604 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
5605 if (!phba->slim_memmap_p) {
5606 dev_printk(KERN_ERR, &pdev->dev,
5607 "ioremap failed for SLIM memory.\n");
5608 goto out;
5609 }
5610
5611 /* Map HBA Control Registers to a kernel virtual address. */
5612 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
5613 if (!phba->ctrl_regs_memmap_p) {
5614 dev_printk(KERN_ERR, &pdev->dev,
5615 "ioremap failed for HBA control registers.\n");
5616 goto out_iounmap_slim;
5617 }
5618
5619 /* Allocate memory for SLI-2 structures */
5620 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
5621 SLI2_SLIM_SIZE,
5622 &phba->slim2p.phys,
5623 GFP_KERNEL);
5624 if (!phba->slim2p.virt)
5625 goto out_iounmap;
5626
5627 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
5628 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04005629 phba->mbox_ext = (phba->slim2p.virt +
5630 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04005631 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
5632 phba->IOCBs = (phba->slim2p.virt +
5633 offsetof(struct lpfc_sli2_slim, IOCBs));
5634
5635 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
5636 lpfc_sli_hbq_size(),
5637 &phba->hbqslimp.phys,
5638 GFP_KERNEL);
5639 if (!phba->hbqslimp.virt)
5640 goto out_free_slim;
5641
5642 hbq_count = lpfc_sli_hbq_count();
5643 ptr = phba->hbqslimp.virt;
5644 for (i = 0; i < hbq_count; ++i) {
5645 phba->hbqs[i].hbq_virt = ptr;
5646 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
5647 ptr += (lpfc_hbq_defs[i]->entry_count *
5648 sizeof(struct lpfc_hbq_entry));
5649 }
5650 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
5651 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
5652
5653 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
5654
5655 INIT_LIST_HEAD(&phba->rb_pend_list);
5656
5657 phba->MBslimaddr = phba->slim_memmap_p;
5658 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
5659 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
5660 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
5661 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04005662
dea31012005-04-17 16:05:31 -05005663 return 0;
5664
dea31012005-04-17 16:05:31 -05005665out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04005666 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
5667 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05005668out_iounmap:
5669 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005670out_iounmap_slim:
dea31012005-04-17 16:05:31 -05005671 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05005672out:
5673 return error;
5674}
5675
James Smarte59058c2008-08-24 21:49:00 -04005676/**
James Smart3772a992009-05-22 14:50:54 -04005677 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
5678 * @phba: pointer to lpfc hba data structure.
5679 *
5680 * This routine is invoked to unset the PCI device memory space for device
5681 * with SLI-3 interface spec.
5682 **/
5683static void
5684lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
5685{
5686 struct pci_dev *pdev;
5687
5688 /* Obtain PCI device reference */
5689 if (!phba->pcidev)
5690 return;
5691 else
5692 pdev = phba->pcidev;
5693
5694 /* Free coherent DMA memory allocated */
5695 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
5696 phba->hbqslimp.virt, phba->hbqslimp.phys);
5697 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
5698 phba->slim2p.virt, phba->slim2p.phys);
5699
5700 /* I/O memory unmap */
5701 iounmap(phba->ctrl_regs_memmap_p);
5702 iounmap(phba->slim_memmap_p);
5703
5704 return;
5705}
5706
5707/**
James Smartda0436e2009-05-22 14:51:39 -04005708 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
5709 * @phba: pointer to lpfc hba data structure.
5710 *
5711 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
5712 * done and check status.
5713 *
5714 * Return 0 if successful, otherwise -ENODEV.
5715 **/
5716int
5717lpfc_sli4_post_status_check(struct lpfc_hba *phba)
5718{
James Smart2fcee4b2010-12-15 17:57:46 -05005719 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
5720 struct lpfc_register reg_data;
5721 int i, port_error = 0;
5722 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04005723
James Smart9940b972011-03-11 16:06:12 -05005724 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
5725 memset(&reg_data, 0, sizeof(reg_data));
James Smart2fcee4b2010-12-15 17:57:46 -05005726 if (!phba->sli4_hba.PSMPHRregaddr)
James Smartda0436e2009-05-22 14:51:39 -04005727 return -ENODEV;
5728
James Smartda0436e2009-05-22 14:51:39 -04005729 /* Wait up to 30 seconds for the SLI Port POST done and ready */
5730 for (i = 0; i < 3000; i++) {
James Smart9940b972011-03-11 16:06:12 -05005731 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
5732 &portsmphr_reg.word0) ||
5733 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
James Smart2fcee4b2010-12-15 17:57:46 -05005734 /* Port has a fatal POST error, break out */
James Smartda0436e2009-05-22 14:51:39 -04005735 port_error = -ENODEV;
5736 break;
5737 }
James Smart2fcee4b2010-12-15 17:57:46 -05005738 if (LPFC_POST_STAGE_PORT_READY ==
5739 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
James Smartda0436e2009-05-22 14:51:39 -04005740 break;
James Smartda0436e2009-05-22 14:51:39 -04005741 msleep(10);
5742 }
5743
James Smart2fcee4b2010-12-15 17:57:46 -05005744 /*
5745 * If there was a port error during POST, then don't proceed with
5746 * other register reads as the data may not be valid. Just exit.
5747 */
5748 if (port_error) {
James Smartda0436e2009-05-22 14:51:39 -04005749 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05005750 "1408 Port Failed POST - portsmphr=0x%x, "
5751 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
5752 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
5753 portsmphr_reg.word0,
5754 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
5755 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
5756 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
5757 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
5758 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
5759 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
5760 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
5761 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
5762 } else {
James Smart28baac72010-02-12 14:42:03 -05005763 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05005764 "2534 Device Info: SLIFamily=0x%x, "
5765 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
5766 "SLIHint_2=0x%x, FT=0x%x\n",
James Smart28baac72010-02-12 14:42:03 -05005767 bf_get(lpfc_sli_intf_sli_family,
5768 &phba->sli4_hba.sli_intf),
5769 bf_get(lpfc_sli_intf_slirev,
5770 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05005771 bf_get(lpfc_sli_intf_if_type,
James Smart28baac72010-02-12 14:42:03 -05005772 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05005773 bf_get(lpfc_sli_intf_sli_hint1,
5774 &phba->sli4_hba.sli_intf),
5775 bf_get(lpfc_sli_intf_sli_hint2,
5776 &phba->sli4_hba.sli_intf),
5777 bf_get(lpfc_sli_intf_func_type,
James Smart28baac72010-02-12 14:42:03 -05005778 &phba->sli4_hba.sli_intf));
James Smart2fcee4b2010-12-15 17:57:46 -05005779 /*
5780 * Check for other Port errors during the initialization
5781 * process. Fail the load if the port did not come up
5782 * correctly.
5783 */
5784 if_type = bf_get(lpfc_sli_intf_if_type,
5785 &phba->sli4_hba.sli_intf);
5786 switch (if_type) {
5787 case LPFC_SLI_INTF_IF_TYPE_0:
5788 phba->sli4_hba.ue_mask_lo =
5789 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
5790 phba->sli4_hba.ue_mask_hi =
5791 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
5792 uerrlo_reg.word0 =
5793 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
5794 uerrhi_reg.word0 =
5795 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
5796 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
5797 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
5798 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5799 "1422 Unrecoverable Error "
5800 "Detected during POST "
5801 "uerr_lo_reg=0x%x, "
5802 "uerr_hi_reg=0x%x, "
5803 "ue_mask_lo_reg=0x%x, "
5804 "ue_mask_hi_reg=0x%x\n",
5805 uerrlo_reg.word0,
5806 uerrhi_reg.word0,
5807 phba->sli4_hba.ue_mask_lo,
5808 phba->sli4_hba.ue_mask_hi);
5809 port_error = -ENODEV;
5810 }
5811 break;
5812 case LPFC_SLI_INTF_IF_TYPE_2:
5813 /* Final checks. The port status should be clean. */
James Smart9940b972011-03-11 16:06:12 -05005814 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
5815 &reg_data.word0) ||
James Smart05580562011-05-24 11:40:48 -04005816 (bf_get(lpfc_sliport_status_err, &reg_data) &&
5817 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
James Smart2fcee4b2010-12-15 17:57:46 -05005818 phba->work_status[0] =
5819 readl(phba->sli4_hba.u.if_type2.
5820 ERR1regaddr);
5821 phba->work_status[1] =
5822 readl(phba->sli4_hba.u.if_type2.
5823 ERR2regaddr);
5824 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5825 "2888 Port Error Detected "
5826 "during POST: "
5827 "port status reg 0x%x, "
5828 "port_smphr reg 0x%x, "
5829 "error 1=0x%x, error 2=0x%x\n",
5830 reg_data.word0,
5831 portsmphr_reg.word0,
5832 phba->work_status[0],
5833 phba->work_status[1]);
5834 port_error = -ENODEV;
5835 }
5836 break;
5837 case LPFC_SLI_INTF_IF_TYPE_1:
5838 default:
5839 break;
5840 }
James Smart28baac72010-02-12 14:42:03 -05005841 }
James Smartda0436e2009-05-22 14:51:39 -04005842 return port_error;
5843}
5844
5845/**
5846 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
5847 * @phba: pointer to lpfc hba data structure.
James Smart2fcee4b2010-12-15 17:57:46 -05005848 * @if_type: The SLI4 interface type getting configured.
James Smartda0436e2009-05-22 14:51:39 -04005849 *
5850 * This routine is invoked to set up SLI4 BAR0 PCI config space register
5851 * memory map.
5852 **/
5853static void
James Smart2fcee4b2010-12-15 17:57:46 -05005854lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04005855{
James Smart2fcee4b2010-12-15 17:57:46 -05005856 switch (if_type) {
5857 case LPFC_SLI_INTF_IF_TYPE_0:
5858 phba->sli4_hba.u.if_type0.UERRLOregaddr =
5859 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
5860 phba->sli4_hba.u.if_type0.UERRHIregaddr =
5861 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
5862 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
5863 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
5864 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
5865 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
5866 phba->sli4_hba.SLIINTFregaddr =
5867 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
5868 break;
5869 case LPFC_SLI_INTF_IF_TYPE_2:
5870 phba->sli4_hba.u.if_type2.ERR1regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04005871 phba->sli4_hba.conf_regs_memmap_p +
5872 LPFC_CTL_PORT_ER1_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05005873 phba->sli4_hba.u.if_type2.ERR2regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04005874 phba->sli4_hba.conf_regs_memmap_p +
5875 LPFC_CTL_PORT_ER2_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05005876 phba->sli4_hba.u.if_type2.CTRLregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04005877 phba->sli4_hba.conf_regs_memmap_p +
5878 LPFC_CTL_PORT_CTL_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05005879 phba->sli4_hba.u.if_type2.STATUSregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04005880 phba->sli4_hba.conf_regs_memmap_p +
5881 LPFC_CTL_PORT_STA_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05005882 phba->sli4_hba.SLIINTFregaddr =
5883 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
5884 phba->sli4_hba.PSMPHRregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04005885 phba->sli4_hba.conf_regs_memmap_p +
5886 LPFC_CTL_PORT_SEM_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05005887 phba->sli4_hba.RQDBregaddr =
5888 phba->sli4_hba.conf_regs_memmap_p + LPFC_RQ_DOORBELL;
5889 phba->sli4_hba.WQDBregaddr =
5890 phba->sli4_hba.conf_regs_memmap_p + LPFC_WQ_DOORBELL;
5891 phba->sli4_hba.EQCQDBregaddr =
5892 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
5893 phba->sli4_hba.MQDBregaddr =
5894 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
5895 phba->sli4_hba.BMBXregaddr =
5896 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
5897 break;
5898 case LPFC_SLI_INTF_IF_TYPE_1:
5899 default:
5900 dev_printk(KERN_ERR, &phba->pcidev->dev,
5901 "FATAL - unsupported SLI4 interface type - %d\n",
5902 if_type);
5903 break;
5904 }
James Smartda0436e2009-05-22 14:51:39 -04005905}
5906
5907/**
5908 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
5909 * @phba: pointer to lpfc hba data structure.
5910 *
5911 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
5912 * memory map.
5913 **/
5914static void
5915lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
5916{
James Smart2fcee4b2010-12-15 17:57:46 -05005917 phba->sli4_hba.PSMPHRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5918 LPFC_SLIPORT_IF0_SMPHR;
James Smartda0436e2009-05-22 14:51:39 -04005919 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05005920 LPFC_HST_ISR0;
James Smartda0436e2009-05-22 14:51:39 -04005921 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05005922 LPFC_HST_IMR0;
James Smartda0436e2009-05-22 14:51:39 -04005923 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05005924 LPFC_HST_ISCR0;
James Smartda0436e2009-05-22 14:51:39 -04005925}
5926
5927/**
5928 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
5929 * @phba: pointer to lpfc hba data structure.
5930 * @vf: virtual function number
5931 *
5932 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
5933 * based on the given viftual function number, @vf.
5934 *
5935 * Return 0 if successful, otherwise -ENODEV.
5936 **/
5937static int
5938lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
5939{
5940 if (vf > LPFC_VIR_FUNC_MAX)
5941 return -ENODEV;
5942
5943 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5944 vf * LPFC_VFR_PAGE_SIZE + LPFC_RQ_DOORBELL);
5945 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5946 vf * LPFC_VFR_PAGE_SIZE + LPFC_WQ_DOORBELL);
5947 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5948 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
5949 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5950 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
5951 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5952 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
5953 return 0;
5954}
5955
5956/**
5957 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
5958 * @phba: pointer to lpfc hba data structure.
5959 *
5960 * This routine is invoked to create the bootstrap mailbox
5961 * region consistent with the SLI-4 interface spec. This
5962 * routine allocates all memory necessary to communicate
5963 * mailbox commands to the port and sets up all alignment
5964 * needs. No locks are expected to be held when calling
5965 * this routine.
5966 *
5967 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005968 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005969 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04005970 **/
5971static int
5972lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
5973{
5974 uint32_t bmbx_size;
5975 struct lpfc_dmabuf *dmabuf;
5976 struct dma_address *dma_address;
5977 uint32_t pa_addr;
5978 uint64_t phys_addr;
5979
5980 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5981 if (!dmabuf)
5982 return -ENOMEM;
5983
5984 /*
5985 * The bootstrap mailbox region is comprised of 2 parts
5986 * plus an alignment restriction of 16 bytes.
5987 */
5988 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
5989 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5990 bmbx_size,
5991 &dmabuf->phys,
5992 GFP_KERNEL);
5993 if (!dmabuf->virt) {
5994 kfree(dmabuf);
5995 return -ENOMEM;
5996 }
5997 memset(dmabuf->virt, 0, bmbx_size);
5998
5999 /*
6000 * Initialize the bootstrap mailbox pointers now so that the register
6001 * operations are simple later. The mailbox dma address is required
6002 * to be 16-byte aligned. Also align the virtual memory as each
6003 * maibox is copied into the bmbx mailbox region before issuing the
6004 * command to the port.
6005 */
6006 phba->sli4_hba.bmbx.dmabuf = dmabuf;
6007 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
6008
6009 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
6010 LPFC_ALIGN_16_BYTE);
6011 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
6012 LPFC_ALIGN_16_BYTE);
6013
6014 /*
6015 * Set the high and low physical addresses now. The SLI4 alignment
6016 * requirement is 16 bytes and the mailbox is posted to the port
6017 * as two 30-bit addresses. The other data is a bit marking whether
6018 * the 30-bit address is the high or low address.
6019 * Upcast bmbx aphys to 64bits so shift instruction compiles
6020 * clean on 32 bit machines.
6021 */
6022 dma_address = &phba->sli4_hba.bmbx.dma_address;
6023 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
6024 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
6025 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
6026 LPFC_BMBX_BIT1_ADDR_HI);
6027
6028 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
6029 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
6030 LPFC_BMBX_BIT1_ADDR_LO);
6031 return 0;
6032}
6033
6034/**
6035 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
6036 * @phba: pointer to lpfc hba data structure.
6037 *
6038 * This routine is invoked to teardown the bootstrap mailbox
6039 * region and release all host resources. This routine requires
6040 * the caller to ensure all mailbox commands recovered, no
6041 * additional mailbox comands are sent, and interrupts are disabled
6042 * before calling this routine.
6043 *
6044 **/
6045static void
6046lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
6047{
6048 dma_free_coherent(&phba->pcidev->dev,
6049 phba->sli4_hba.bmbx.bmbx_size,
6050 phba->sli4_hba.bmbx.dmabuf->virt,
6051 phba->sli4_hba.bmbx.dmabuf->phys);
6052
6053 kfree(phba->sli4_hba.bmbx.dmabuf);
6054 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
6055}
6056
6057/**
6058 * lpfc_sli4_read_config - Get the config parameters.
6059 * @phba: pointer to lpfc hba data structure.
6060 *
6061 * This routine is invoked to read the configuration parameters from the HBA.
6062 * The configuration parameters are used to set the base and maximum values
6063 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
6064 * allocation for the port.
6065 *
6066 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006067 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006068 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006069 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006070 **/
James Smartff78d8f2011-12-13 13:21:35 -05006071int
James Smartda0436e2009-05-22 14:51:39 -04006072lpfc_sli4_read_config(struct lpfc_hba *phba)
6073{
6074 LPFC_MBOXQ_t *pmb;
6075 struct lpfc_mbx_read_config *rd_config;
James Smart912e3ac2011-05-24 11:42:11 -04006076 union lpfc_sli4_cfg_shdr *shdr;
6077 uint32_t shdr_status, shdr_add_status;
6078 struct lpfc_mbx_get_func_cfg *get_func_cfg;
6079 struct lpfc_rsrc_desc_fcfcoe *desc;
6080 uint32_t desc_count;
6081 int length, i, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04006082
6083 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6084 if (!pmb) {
6085 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6086 "2011 Unable to allocate memory for issuing "
6087 "SLI_CONFIG_SPECIAL mailbox command\n");
6088 return -ENOMEM;
6089 }
6090
6091 lpfc_read_config(phba, pmb);
6092
6093 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6094 if (rc != MBX_SUCCESS) {
6095 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6096 "2012 Mailbox failed , mbxCmd x%x "
6097 "READ_CONFIG, mbxStatus x%x\n",
6098 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6099 bf_get(lpfc_mqe_status, &pmb->u.mqe));
6100 rc = -EIO;
6101 } else {
6102 rd_config = &pmb->u.mqe.un.rd_config;
James Smartff78d8f2011-12-13 13:21:35 -05006103 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
6104 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
6105 phba->sli4_hba.lnk_info.lnk_tp =
6106 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
6107 phba->sli4_hba.lnk_info.lnk_no =
6108 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
6109 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6110 "3081 lnk_type:%d, lnk_numb:%d\n",
6111 phba->sli4_hba.lnk_info.lnk_tp,
6112 phba->sli4_hba.lnk_info.lnk_no);
6113 } else
6114 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6115 "3082 Mailbox (x%x) returned ldv:x0\n",
6116 bf_get(lpfc_mqe_command, &pmb->u.mqe));
James Smart6d368e52011-05-24 11:44:12 -04006117 phba->sli4_hba.extents_in_use =
6118 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04006119 phba->sli4_hba.max_cfg_param.max_xri =
6120 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
6121 phba->sli4_hba.max_cfg_param.xri_base =
6122 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
6123 phba->sli4_hba.max_cfg_param.max_vpi =
6124 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
6125 phba->sli4_hba.max_cfg_param.vpi_base =
6126 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
6127 phba->sli4_hba.max_cfg_param.max_rpi =
6128 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
6129 phba->sli4_hba.max_cfg_param.rpi_base =
6130 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
6131 phba->sli4_hba.max_cfg_param.max_vfi =
6132 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
6133 phba->sli4_hba.max_cfg_param.vfi_base =
6134 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
6135 phba->sli4_hba.max_cfg_param.max_fcfi =
6136 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04006137 phba->sli4_hba.max_cfg_param.max_eq =
6138 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
6139 phba->sli4_hba.max_cfg_param.max_rq =
6140 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
6141 phba->sli4_hba.max_cfg_param.max_wq =
6142 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
6143 phba->sli4_hba.max_cfg_param.max_cq =
6144 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
6145 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
6146 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
6147 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
6148 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
6149 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
James Smart5ffc2662009-11-18 15:39:44 -05006150 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
6151 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04006152 phba->max_vports = phba->max_vpi;
6153 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart6d368e52011-05-24 11:44:12 -04006154 "2003 cfg params Extents? %d "
6155 "XRI(B:%d M:%d), "
James Smartda0436e2009-05-22 14:51:39 -04006156 "VPI(B:%d M:%d) "
6157 "VFI(B:%d M:%d) "
6158 "RPI(B:%d M:%d) "
James Smart6d368e52011-05-24 11:44:12 -04006159 "FCFI(Count:%d)\n",
6160 phba->sli4_hba.extents_in_use,
James Smartda0436e2009-05-22 14:51:39 -04006161 phba->sli4_hba.max_cfg_param.xri_base,
6162 phba->sli4_hba.max_cfg_param.max_xri,
6163 phba->sli4_hba.max_cfg_param.vpi_base,
6164 phba->sli4_hba.max_cfg_param.max_vpi,
6165 phba->sli4_hba.max_cfg_param.vfi_base,
6166 phba->sli4_hba.max_cfg_param.max_vfi,
6167 phba->sli4_hba.max_cfg_param.rpi_base,
6168 phba->sli4_hba.max_cfg_param.max_rpi,
James Smartda0436e2009-05-22 14:51:39 -04006169 phba->sli4_hba.max_cfg_param.max_fcfi);
6170 }
James Smart912e3ac2011-05-24 11:42:11 -04006171
6172 if (rc)
6173 goto read_cfg_out;
James Smartda0436e2009-05-22 14:51:39 -04006174
6175 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smartb92938b2010-06-07 15:24:12 -04006176 if (phba->cfg_hba_queue_depth >
6177 (phba->sli4_hba.max_cfg_param.max_xri -
6178 lpfc_sli4_get_els_iocb_cnt(phba)))
James Smartda0436e2009-05-22 14:51:39 -04006179 phba->cfg_hba_queue_depth =
James Smartb92938b2010-06-07 15:24:12 -04006180 phba->sli4_hba.max_cfg_param.max_xri -
6181 lpfc_sli4_get_els_iocb_cnt(phba);
James Smart912e3ac2011-05-24 11:42:11 -04006182
6183 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
6184 LPFC_SLI_INTF_IF_TYPE_2)
6185 goto read_cfg_out;
6186
6187 /* get the pf# and vf# for SLI4 if_type 2 port */
6188 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
6189 sizeof(struct lpfc_sli4_cfg_mhdr));
6190 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
6191 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
6192 length, LPFC_SLI4_MBX_EMBED);
6193
6194 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6195 shdr = (union lpfc_sli4_cfg_shdr *)
6196 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
6197 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6198 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6199 if (rc || shdr_status || shdr_add_status) {
6200 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6201 "3026 Mailbox failed , mbxCmd x%x "
6202 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
6203 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6204 bf_get(lpfc_mqe_status, &pmb->u.mqe));
6205 rc = -EIO;
6206 goto read_cfg_out;
6207 }
6208
6209 /* search for fc_fcoe resrouce descriptor */
6210 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
6211 desc_count = get_func_cfg->func_cfg.rsrc_desc_count;
6212
6213 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
6214 desc = (struct lpfc_rsrc_desc_fcfcoe *)
6215 &get_func_cfg->func_cfg.desc[i];
6216 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
6217 bf_get(lpfc_rsrc_desc_pcie_type, desc)) {
6218 phba->sli4_hba.iov.pf_number =
6219 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
6220 phba->sli4_hba.iov.vf_number =
6221 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
6222 break;
6223 }
6224 }
6225
6226 if (i < LPFC_RSRC_DESC_MAX_NUM)
6227 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6228 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
6229 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
6230 phba->sli4_hba.iov.vf_number);
6231 else {
6232 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6233 "3028 GET_FUNCTION_CONFIG: failed to find "
6234 "Resrouce Descriptor:x%x\n",
6235 LPFC_RSRC_DESC_TYPE_FCFCOE);
6236 rc = -EIO;
6237 }
6238
6239read_cfg_out:
6240 mempool_free(pmb, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04006241 return rc;
6242}
6243
6244/**
James Smart2fcee4b2010-12-15 17:57:46 -05006245 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
James Smartda0436e2009-05-22 14:51:39 -04006246 * @phba: pointer to lpfc hba data structure.
6247 *
James Smart2fcee4b2010-12-15 17:57:46 -05006248 * This routine is invoked to setup the port-side endian order when
6249 * the port if_type is 0. This routine has no function for other
6250 * if_types.
James Smartda0436e2009-05-22 14:51:39 -04006251 *
6252 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006253 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006254 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006255 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006256 **/
6257static int
6258lpfc_setup_endian_order(struct lpfc_hba *phba)
6259{
6260 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05006261 uint32_t if_type, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04006262 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
6263 HOST_ENDIAN_HIGH_WORD1};
6264
James Smart2fcee4b2010-12-15 17:57:46 -05006265 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
6266 switch (if_type) {
6267 case LPFC_SLI_INTF_IF_TYPE_0:
6268 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6269 GFP_KERNEL);
6270 if (!mboxq) {
6271 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6272 "0492 Unable to allocate memory for "
6273 "issuing SLI_CONFIG_SPECIAL mailbox "
6274 "command\n");
6275 return -ENOMEM;
6276 }
James Smartda0436e2009-05-22 14:51:39 -04006277
James Smart2fcee4b2010-12-15 17:57:46 -05006278 /*
6279 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
6280 * two words to contain special data values and no other data.
6281 */
6282 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
6283 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
6284 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6285 if (rc != MBX_SUCCESS) {
6286 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6287 "0493 SLI_CONFIG_SPECIAL mailbox "
6288 "failed with status x%x\n",
6289 rc);
6290 rc = -EIO;
6291 }
6292 mempool_free(mboxq, phba->mbox_mem_pool);
6293 break;
6294 case LPFC_SLI_INTF_IF_TYPE_2:
6295 case LPFC_SLI_INTF_IF_TYPE_1:
6296 default:
6297 break;
James Smartda0436e2009-05-22 14:51:39 -04006298 }
James Smartda0436e2009-05-22 14:51:39 -04006299 return rc;
6300}
6301
6302/**
James Smart5350d872011-10-10 21:33:49 -04006303 * lpfc_sli4_queue_verify - Verify and update EQ and CQ counts
James Smartda0436e2009-05-22 14:51:39 -04006304 * @phba: pointer to lpfc hba data structure.
6305 *
James Smart5350d872011-10-10 21:33:49 -04006306 * This routine is invoked to check the user settable queue counts for EQs and
6307 * CQs. after this routine is called the counts will be set to valid values that
6308 * adhere to the constraints of the system's interrupt vectors and the port's
6309 * queue resources.
James Smartda0436e2009-05-22 14:51:39 -04006310 *
6311 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006312 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006313 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04006314 **/
6315static int
James Smart5350d872011-10-10 21:33:49 -04006316lpfc_sli4_queue_verify(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04006317{
James Smartda0436e2009-05-22 14:51:39 -04006318 int cfg_fcp_wq_count;
6319 int cfg_fcp_eq_count;
6320
6321 /*
6322 * Sanity check for confiugred queue parameters against the run-time
6323 * device parameters
6324 */
6325
6326 /* Sanity check on FCP fast-path WQ parameters */
6327 cfg_fcp_wq_count = phba->cfg_fcp_wq_count;
6328 if (cfg_fcp_wq_count >
6329 (phba->sli4_hba.max_cfg_param.max_wq - LPFC_SP_WQN_DEF)) {
6330 cfg_fcp_wq_count = phba->sli4_hba.max_cfg_param.max_wq -
6331 LPFC_SP_WQN_DEF;
6332 if (cfg_fcp_wq_count < LPFC_FP_WQN_MIN) {
6333 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6334 "2581 Not enough WQs (%d) from "
6335 "the pci function for supporting "
6336 "FCP WQs (%d)\n",
6337 phba->sli4_hba.max_cfg_param.max_wq,
6338 phba->cfg_fcp_wq_count);
6339 goto out_error;
6340 }
6341 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6342 "2582 Not enough WQs (%d) from the pci "
6343 "function for supporting the requested "
6344 "FCP WQs (%d), the actual FCP WQs can "
6345 "be supported: %d\n",
6346 phba->sli4_hba.max_cfg_param.max_wq,
6347 phba->cfg_fcp_wq_count, cfg_fcp_wq_count);
6348 }
6349 /* The actual number of FCP work queues adopted */
6350 phba->cfg_fcp_wq_count = cfg_fcp_wq_count;
6351
6352 /* Sanity check on FCP fast-path EQ parameters */
6353 cfg_fcp_eq_count = phba->cfg_fcp_eq_count;
6354 if (cfg_fcp_eq_count >
6355 (phba->sli4_hba.max_cfg_param.max_eq - LPFC_SP_EQN_DEF)) {
6356 cfg_fcp_eq_count = phba->sli4_hba.max_cfg_param.max_eq -
6357 LPFC_SP_EQN_DEF;
6358 if (cfg_fcp_eq_count < LPFC_FP_EQN_MIN) {
6359 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6360 "2574 Not enough EQs (%d) from the "
6361 "pci function for supporting FCP "
6362 "EQs (%d)\n",
6363 phba->sli4_hba.max_cfg_param.max_eq,
6364 phba->cfg_fcp_eq_count);
6365 goto out_error;
6366 }
6367 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6368 "2575 Not enough EQs (%d) from the pci "
6369 "function for supporting the requested "
6370 "FCP EQs (%d), the actual FCP EQs can "
6371 "be supported: %d\n",
6372 phba->sli4_hba.max_cfg_param.max_eq,
6373 phba->cfg_fcp_eq_count, cfg_fcp_eq_count);
6374 }
6375 /* It does not make sense to have more EQs than WQs */
6376 if (cfg_fcp_eq_count > phba->cfg_fcp_wq_count) {
6377 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smart6a9c52c2009-10-02 15:16:51 -04006378 "2593 The FCP EQ count(%d) cannot be greater "
6379 "than the FCP WQ count(%d), limiting the "
6380 "FCP EQ count to %d\n", cfg_fcp_eq_count,
James Smartda0436e2009-05-22 14:51:39 -04006381 phba->cfg_fcp_wq_count,
6382 phba->cfg_fcp_wq_count);
6383 cfg_fcp_eq_count = phba->cfg_fcp_wq_count;
6384 }
6385 /* The actual number of FCP event queues adopted */
6386 phba->cfg_fcp_eq_count = cfg_fcp_eq_count;
6387 /* The overall number of event queues used */
6388 phba->sli4_hba.cfg_eqn = phba->cfg_fcp_eq_count + LPFC_SP_EQN_DEF;
6389
James Smartda0436e2009-05-22 14:51:39 -04006390 /* Get EQ depth from module parameter, fake the default for now */
6391 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
6392 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
6393
James Smart5350d872011-10-10 21:33:49 -04006394 /* Get CQ depth from module parameter, fake the default for now */
6395 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
6396 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
6397
6398 return 0;
6399out_error:
6400 return -ENOMEM;
6401}
6402
6403/**
6404 * lpfc_sli4_queue_create - Create all the SLI4 queues
6405 * @phba: pointer to lpfc hba data structure.
6406 *
6407 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
6408 * operation. For each SLI4 queue type, the parameters such as queue entry
6409 * count (queue depth) shall be taken from the module parameter. For now,
6410 * we just use some constant number as place holder.
6411 *
6412 * Return codes
6413 * 0 - sucessful
6414 * -ENOMEM - No availble memory
6415 * -EIO - The mailbox failed to complete successfully.
6416 **/
6417int
6418lpfc_sli4_queue_create(struct lpfc_hba *phba)
6419{
6420 struct lpfc_queue *qdesc;
6421 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
6422
6423 /*
6424 * Create Event Queues (EQs)
6425 */
6426
James Smartda0436e2009-05-22 14:51:39 -04006427 /* Create slow path event queue */
6428 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
6429 phba->sli4_hba.eq_ecount);
6430 if (!qdesc) {
6431 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6432 "0496 Failed allocate slow-path EQ\n");
6433 goto out_error;
6434 }
6435 phba->sli4_hba.sp_eq = qdesc;
6436
James Smart5350d872011-10-10 21:33:49 -04006437 /*
6438 * Create fast-path FCP Event Queue(s). The cfg_fcp_eq_count can be
6439 * zero whenever there is exactly one interrupt vector. This is not
6440 * an error.
6441 */
6442 if (phba->cfg_fcp_eq_count) {
6443 phba->sli4_hba.fp_eq = kzalloc((sizeof(struct lpfc_queue *) *
6444 phba->cfg_fcp_eq_count), GFP_KERNEL);
6445 if (!phba->sli4_hba.fp_eq) {
6446 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6447 "2576 Failed allocate memory for "
6448 "fast-path EQ record array\n");
6449 goto out_free_sp_eq;
6450 }
James Smartda0436e2009-05-22 14:51:39 -04006451 }
6452 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
6453 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
6454 phba->sli4_hba.eq_ecount);
6455 if (!qdesc) {
6456 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6457 "0497 Failed allocate fast-path EQ\n");
6458 goto out_free_fp_eq;
6459 }
6460 phba->sli4_hba.fp_eq[fcp_eqidx] = qdesc;
6461 }
6462
6463 /*
6464 * Create Complete Queues (CQs)
6465 */
6466
James Smartda0436e2009-05-22 14:51:39 -04006467 /* Create slow-path Mailbox Command Complete Queue */
6468 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6469 phba->sli4_hba.cq_ecount);
6470 if (!qdesc) {
6471 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6472 "0500 Failed allocate slow-path mailbox CQ\n");
6473 goto out_free_fp_eq;
6474 }
6475 phba->sli4_hba.mbx_cq = qdesc;
6476
6477 /* Create slow-path ELS Complete Queue */
6478 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6479 phba->sli4_hba.cq_ecount);
6480 if (!qdesc) {
6481 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6482 "0501 Failed allocate slow-path ELS CQ\n");
6483 goto out_free_mbx_cq;
6484 }
6485 phba->sli4_hba.els_cq = qdesc;
6486
James Smartda0436e2009-05-22 14:51:39 -04006487
James Smart5350d872011-10-10 21:33:49 -04006488 /*
6489 * Create fast-path FCP Completion Queue(s), one-to-one with FCP EQs.
6490 * If there are no FCP EQs then create exactly one FCP CQ.
6491 */
6492 if (phba->cfg_fcp_eq_count)
6493 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
6494 phba->cfg_fcp_eq_count),
6495 GFP_KERNEL);
6496 else
6497 phba->sli4_hba.fcp_cq = kzalloc(sizeof(struct lpfc_queue *),
6498 GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04006499 if (!phba->sli4_hba.fcp_cq) {
6500 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6501 "2577 Failed allocate memory for fast-path "
6502 "CQ record array\n");
James Smart4d9ab992009-10-02 15:16:39 -04006503 goto out_free_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04006504 }
James Smart5350d872011-10-10 21:33:49 -04006505 fcp_cqidx = 0;
6506 do {
James Smartda0436e2009-05-22 14:51:39 -04006507 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6508 phba->sli4_hba.cq_ecount);
6509 if (!qdesc) {
6510 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6511 "0499 Failed allocate fast-path FCP "
6512 "CQ (%d)\n", fcp_cqidx);
6513 goto out_free_fcp_cq;
6514 }
6515 phba->sli4_hba.fcp_cq[fcp_cqidx] = qdesc;
James Smart5350d872011-10-10 21:33:49 -04006516 } while (++fcp_cqidx < phba->cfg_fcp_eq_count);
James Smartda0436e2009-05-22 14:51:39 -04006517
6518 /* Create Mailbox Command Queue */
6519 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
6520 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
6521
6522 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
6523 phba->sli4_hba.mq_ecount);
6524 if (!qdesc) {
6525 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6526 "0505 Failed allocate slow-path MQ\n");
6527 goto out_free_fcp_cq;
6528 }
6529 phba->sli4_hba.mbx_wq = qdesc;
6530
6531 /*
6532 * Create all the Work Queues (WQs)
6533 */
6534 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
6535 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
6536
6537 /* Create slow-path ELS Work Queue */
6538 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
6539 phba->sli4_hba.wq_ecount);
6540 if (!qdesc) {
6541 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6542 "0504 Failed allocate slow-path ELS WQ\n");
6543 goto out_free_mbx_wq;
6544 }
6545 phba->sli4_hba.els_wq = qdesc;
6546
6547 /* Create fast-path FCP Work Queue(s) */
6548 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
6549 phba->cfg_fcp_wq_count), GFP_KERNEL);
6550 if (!phba->sli4_hba.fcp_wq) {
6551 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6552 "2578 Failed allocate memory for fast-path "
6553 "WQ record array\n");
6554 goto out_free_els_wq;
6555 }
6556 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
6557 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
6558 phba->sli4_hba.wq_ecount);
6559 if (!qdesc) {
6560 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6561 "0503 Failed allocate fast-path FCP "
6562 "WQ (%d)\n", fcp_wqidx);
6563 goto out_free_fcp_wq;
6564 }
6565 phba->sli4_hba.fcp_wq[fcp_wqidx] = qdesc;
6566 }
6567
6568 /*
6569 * Create Receive Queue (RQ)
6570 */
6571 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
6572 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
6573
6574 /* Create Receive Queue for header */
6575 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
6576 phba->sli4_hba.rq_ecount);
6577 if (!qdesc) {
6578 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6579 "0506 Failed allocate receive HRQ\n");
6580 goto out_free_fcp_wq;
6581 }
6582 phba->sli4_hba.hdr_rq = qdesc;
6583
6584 /* Create Receive Queue for data */
6585 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
6586 phba->sli4_hba.rq_ecount);
6587 if (!qdesc) {
6588 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6589 "0507 Failed allocate receive DRQ\n");
6590 goto out_free_hdr_rq;
6591 }
6592 phba->sli4_hba.dat_rq = qdesc;
6593
6594 return 0;
6595
6596out_free_hdr_rq:
6597 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
6598 phba->sli4_hba.hdr_rq = NULL;
6599out_free_fcp_wq:
6600 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--) {
6601 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_wqidx]);
6602 phba->sli4_hba.fcp_wq[fcp_wqidx] = NULL;
6603 }
6604 kfree(phba->sli4_hba.fcp_wq);
James Smart2e90f4b2011-12-13 13:22:37 -05006605 phba->sli4_hba.fcp_wq = NULL;
James Smartda0436e2009-05-22 14:51:39 -04006606out_free_els_wq:
6607 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
6608 phba->sli4_hba.els_wq = NULL;
6609out_free_mbx_wq:
6610 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
6611 phba->sli4_hba.mbx_wq = NULL;
6612out_free_fcp_cq:
6613 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--) {
6614 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_cqidx]);
6615 phba->sli4_hba.fcp_cq[fcp_cqidx] = NULL;
6616 }
6617 kfree(phba->sli4_hba.fcp_cq);
James Smart2e90f4b2011-12-13 13:22:37 -05006618 phba->sli4_hba.fcp_cq = NULL;
James Smartda0436e2009-05-22 14:51:39 -04006619out_free_els_cq:
6620 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
6621 phba->sli4_hba.els_cq = NULL;
6622out_free_mbx_cq:
6623 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
6624 phba->sli4_hba.mbx_cq = NULL;
6625out_free_fp_eq:
6626 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--) {
6627 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_eqidx]);
6628 phba->sli4_hba.fp_eq[fcp_eqidx] = NULL;
6629 }
6630 kfree(phba->sli4_hba.fp_eq);
James Smart2e90f4b2011-12-13 13:22:37 -05006631 phba->sli4_hba.fp_eq = NULL;
James Smartda0436e2009-05-22 14:51:39 -04006632out_free_sp_eq:
6633 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
6634 phba->sli4_hba.sp_eq = NULL;
6635out_error:
6636 return -ENOMEM;
6637}
6638
6639/**
6640 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
6641 * @phba: pointer to lpfc hba data structure.
6642 *
6643 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
6644 * operation.
6645 *
6646 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006647 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006648 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006649 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006650 **/
James Smart5350d872011-10-10 21:33:49 -04006651void
James Smartda0436e2009-05-22 14:51:39 -04006652lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
6653{
6654 int fcp_qidx;
6655
6656 /* Release mailbox command work queue */
6657 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
6658 phba->sli4_hba.mbx_wq = NULL;
6659
6660 /* Release ELS work queue */
6661 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
6662 phba->sli4_hba.els_wq = NULL;
6663
6664 /* Release FCP work queue */
James Smart2e90f4b2011-12-13 13:22:37 -05006665 if (phba->sli4_hba.fcp_wq != NULL)
6666 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count;
6667 fcp_qidx++)
6668 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_qidx]);
James Smartda0436e2009-05-22 14:51:39 -04006669 kfree(phba->sli4_hba.fcp_wq);
6670 phba->sli4_hba.fcp_wq = NULL;
6671
6672 /* Release unsolicited receive queue */
6673 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
6674 phba->sli4_hba.hdr_rq = NULL;
6675 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
6676 phba->sli4_hba.dat_rq = NULL;
6677
James Smartda0436e2009-05-22 14:51:39 -04006678 /* Release ELS complete queue */
6679 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
6680 phba->sli4_hba.els_cq = NULL;
6681
6682 /* Release mailbox command complete queue */
6683 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
6684 phba->sli4_hba.mbx_cq = NULL;
6685
6686 /* Release FCP response complete queue */
James Smart05580562011-05-24 11:40:48 -04006687 fcp_qidx = 0;
James Smart2e90f4b2011-12-13 13:22:37 -05006688 if (phba->sli4_hba.fcp_cq != NULL)
6689 do
6690 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_qidx]);
6691 while (++fcp_qidx < phba->cfg_fcp_eq_count);
James Smartda0436e2009-05-22 14:51:39 -04006692 kfree(phba->sli4_hba.fcp_cq);
6693 phba->sli4_hba.fcp_cq = NULL;
6694
6695 /* Release fast-path event queue */
James Smart2e90f4b2011-12-13 13:22:37 -05006696 if (phba->sli4_hba.fp_eq != NULL)
6697 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count;
6698 fcp_qidx++)
6699 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_qidx]);
James Smartda0436e2009-05-22 14:51:39 -04006700 kfree(phba->sli4_hba.fp_eq);
6701 phba->sli4_hba.fp_eq = NULL;
6702
6703 /* Release slow-path event queue */
6704 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
6705 phba->sli4_hba.sp_eq = NULL;
6706
6707 return;
6708}
6709
6710/**
6711 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
6712 * @phba: pointer to lpfc hba data structure.
6713 *
6714 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
6715 * operation.
6716 *
6717 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006718 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006719 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006720 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006721 **/
6722int
6723lpfc_sli4_queue_setup(struct lpfc_hba *phba)
6724{
6725 int rc = -ENOMEM;
6726 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
6727 int fcp_cq_index = 0;
6728
6729 /*
6730 * Set up Event Queues (EQs)
6731 */
6732
6733 /* Set up slow-path event queue */
6734 if (!phba->sli4_hba.sp_eq) {
6735 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6736 "0520 Slow-path EQ not allocated\n");
6737 goto out_error;
6738 }
6739 rc = lpfc_eq_create(phba, phba->sli4_hba.sp_eq,
6740 LPFC_SP_DEF_IMAX);
6741 if (rc) {
6742 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6743 "0521 Failed setup of slow-path EQ: "
6744 "rc = 0x%x\n", rc);
6745 goto out_error;
6746 }
6747 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6748 "2583 Slow-path EQ setup: queue-id=%d\n",
6749 phba->sli4_hba.sp_eq->queue_id);
6750
6751 /* Set up fast-path event queue */
James Smart1b511972011-12-13 13:23:09 -05006752 if (phba->cfg_fcp_eq_count && !phba->sli4_hba.fp_eq) {
James Smart2e90f4b2011-12-13 13:22:37 -05006753 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6754 "3147 Fast-path EQs not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05006755 rc = -ENOMEM;
James Smart2e90f4b2011-12-13 13:22:37 -05006756 goto out_destroy_sp_eq;
6757 }
James Smartda0436e2009-05-22 14:51:39 -04006758 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
6759 if (!phba->sli4_hba.fp_eq[fcp_eqidx]) {
6760 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6761 "0522 Fast-path EQ (%d) not "
6762 "allocated\n", fcp_eqidx);
James Smart1b511972011-12-13 13:23:09 -05006763 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04006764 goto out_destroy_fp_eq;
6765 }
6766 rc = lpfc_eq_create(phba, phba->sli4_hba.fp_eq[fcp_eqidx],
6767 phba->cfg_fcp_imax);
6768 if (rc) {
6769 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6770 "0523 Failed setup of fast-path EQ "
6771 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
6772 goto out_destroy_fp_eq;
6773 }
6774 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6775 "2584 Fast-path EQ setup: "
6776 "queue[%d]-id=%d\n", fcp_eqidx,
6777 phba->sli4_hba.fp_eq[fcp_eqidx]->queue_id);
6778 }
6779
6780 /*
6781 * Set up Complete Queues (CQs)
6782 */
6783
6784 /* Set up slow-path MBOX Complete Queue as the first CQ */
6785 if (!phba->sli4_hba.mbx_cq) {
6786 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6787 "0528 Mailbox CQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05006788 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04006789 goto out_destroy_fp_eq;
6790 }
6791 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq, phba->sli4_hba.sp_eq,
6792 LPFC_MCQ, LPFC_MBOX);
6793 if (rc) {
6794 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6795 "0529 Failed setup of slow-path mailbox CQ: "
6796 "rc = 0x%x\n", rc);
6797 goto out_destroy_fp_eq;
6798 }
6799 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6800 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
6801 phba->sli4_hba.mbx_cq->queue_id,
6802 phba->sli4_hba.sp_eq->queue_id);
6803
6804 /* Set up slow-path ELS Complete Queue */
6805 if (!phba->sli4_hba.els_cq) {
6806 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6807 "0530 ELS CQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05006808 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04006809 goto out_destroy_mbx_cq;
6810 }
6811 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq, phba->sli4_hba.sp_eq,
6812 LPFC_WCQ, LPFC_ELS);
6813 if (rc) {
6814 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6815 "0531 Failed setup of slow-path ELS CQ: "
6816 "rc = 0x%x\n", rc);
6817 goto out_destroy_mbx_cq;
6818 }
6819 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6820 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
6821 phba->sli4_hba.els_cq->queue_id,
6822 phba->sli4_hba.sp_eq->queue_id);
6823
James Smartda0436e2009-05-22 14:51:39 -04006824 /* Set up fast-path FCP Response Complete Queue */
James Smart2e90f4b2011-12-13 13:22:37 -05006825 if (!phba->sli4_hba.fcp_cq) {
6826 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6827 "3148 Fast-path FCP CQ array not "
6828 "allocated\n");
James Smart1b511972011-12-13 13:23:09 -05006829 rc = -ENOMEM;
James Smart2e90f4b2011-12-13 13:22:37 -05006830 goto out_destroy_els_cq;
6831 }
James Smart05580562011-05-24 11:40:48 -04006832 fcp_cqidx = 0;
6833 do {
James Smartda0436e2009-05-22 14:51:39 -04006834 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
6835 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6836 "0526 Fast-path FCP CQ (%d) not "
6837 "allocated\n", fcp_cqidx);
James Smart1b511972011-12-13 13:23:09 -05006838 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04006839 goto out_destroy_fcp_cq;
6840 }
James Smart05580562011-05-24 11:40:48 -04006841 if (phba->cfg_fcp_eq_count)
6842 rc = lpfc_cq_create(phba,
6843 phba->sli4_hba.fcp_cq[fcp_cqidx],
6844 phba->sli4_hba.fp_eq[fcp_cqidx],
6845 LPFC_WCQ, LPFC_FCP);
6846 else
6847 rc = lpfc_cq_create(phba,
6848 phba->sli4_hba.fcp_cq[fcp_cqidx],
6849 phba->sli4_hba.sp_eq,
6850 LPFC_WCQ, LPFC_FCP);
James Smartda0436e2009-05-22 14:51:39 -04006851 if (rc) {
6852 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6853 "0527 Failed setup of fast-path FCP "
6854 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
6855 goto out_destroy_fcp_cq;
6856 }
6857 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6858 "2588 FCP CQ setup: cq[%d]-id=%d, "
James Smart05580562011-05-24 11:40:48 -04006859 "parent %seq[%d]-id=%d\n",
James Smartda0436e2009-05-22 14:51:39 -04006860 fcp_cqidx,
6861 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
James Smart05580562011-05-24 11:40:48 -04006862 (phba->cfg_fcp_eq_count) ? "" : "sp_",
James Smartda0436e2009-05-22 14:51:39 -04006863 fcp_cqidx,
James Smart05580562011-05-24 11:40:48 -04006864 (phba->cfg_fcp_eq_count) ?
6865 phba->sli4_hba.fp_eq[fcp_cqidx]->queue_id :
6866 phba->sli4_hba.sp_eq->queue_id);
6867 } while (++fcp_cqidx < phba->cfg_fcp_eq_count);
James Smartda0436e2009-05-22 14:51:39 -04006868
6869 /*
6870 * Set up all the Work Queues (WQs)
6871 */
6872
6873 /* Set up Mailbox Command Queue */
6874 if (!phba->sli4_hba.mbx_wq) {
6875 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6876 "0538 Slow-path MQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05006877 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04006878 goto out_destroy_fcp_cq;
6879 }
6880 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
6881 phba->sli4_hba.mbx_cq, LPFC_MBOX);
6882 if (rc) {
6883 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6884 "0539 Failed setup of slow-path MQ: "
6885 "rc = 0x%x\n", rc);
6886 goto out_destroy_fcp_cq;
6887 }
6888 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6889 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
6890 phba->sli4_hba.mbx_wq->queue_id,
6891 phba->sli4_hba.mbx_cq->queue_id);
6892
6893 /* Set up slow-path ELS Work Queue */
6894 if (!phba->sli4_hba.els_wq) {
6895 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6896 "0536 Slow-path ELS WQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05006897 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04006898 goto out_destroy_mbx_wq;
6899 }
6900 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
6901 phba->sli4_hba.els_cq, LPFC_ELS);
6902 if (rc) {
6903 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6904 "0537 Failed setup of slow-path ELS WQ: "
6905 "rc = 0x%x\n", rc);
6906 goto out_destroy_mbx_wq;
6907 }
6908 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6909 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
6910 phba->sli4_hba.els_wq->queue_id,
6911 phba->sli4_hba.els_cq->queue_id);
6912
6913 /* Set up fast-path FCP Work Queue */
James Smart2e90f4b2011-12-13 13:22:37 -05006914 if (!phba->sli4_hba.fcp_wq) {
6915 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6916 "3149 Fast-path FCP WQ array not "
6917 "allocated\n");
James Smart1b511972011-12-13 13:23:09 -05006918 rc = -ENOMEM;
James Smart2e90f4b2011-12-13 13:22:37 -05006919 goto out_destroy_els_wq;
6920 }
James Smartda0436e2009-05-22 14:51:39 -04006921 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
6922 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
6923 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6924 "0534 Fast-path FCP WQ (%d) not "
6925 "allocated\n", fcp_wqidx);
James Smart1b511972011-12-13 13:23:09 -05006926 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04006927 goto out_destroy_fcp_wq;
6928 }
6929 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
6930 phba->sli4_hba.fcp_cq[fcp_cq_index],
6931 LPFC_FCP);
6932 if (rc) {
6933 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6934 "0535 Failed setup of fast-path FCP "
6935 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
6936 goto out_destroy_fcp_wq;
6937 }
6938 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6939 "2591 FCP WQ setup: wq[%d]-id=%d, "
6940 "parent cq[%d]-id=%d\n",
6941 fcp_wqidx,
6942 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
6943 fcp_cq_index,
6944 phba->sli4_hba.fcp_cq[fcp_cq_index]->queue_id);
6945 /* Round robin FCP Work Queue's Completion Queue assignment */
James Smart05580562011-05-24 11:40:48 -04006946 if (phba->cfg_fcp_eq_count)
6947 fcp_cq_index = ((fcp_cq_index + 1) %
6948 phba->cfg_fcp_eq_count);
James Smartda0436e2009-05-22 14:51:39 -04006949 }
6950
6951 /*
6952 * Create Receive Queue (RQ)
6953 */
6954 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
6955 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6956 "0540 Receive Queue not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05006957 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04006958 goto out_destroy_fcp_wq;
6959 }
James Smart73d91e52011-10-10 21:32:10 -04006960
6961 lpfc_rq_adjust_repost(phba, phba->sli4_hba.hdr_rq, LPFC_ELS_HBQ);
6962 lpfc_rq_adjust_repost(phba, phba->sli4_hba.dat_rq, LPFC_ELS_HBQ);
6963
James Smartda0436e2009-05-22 14:51:39 -04006964 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04006965 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04006966 if (rc) {
6967 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6968 "0541 Failed setup of Receive Queue: "
6969 "rc = 0x%x\n", rc);
6970 goto out_destroy_fcp_wq;
6971 }
James Smart73d91e52011-10-10 21:32:10 -04006972
James Smartda0436e2009-05-22 14:51:39 -04006973 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6974 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
6975 "parent cq-id=%d\n",
6976 phba->sli4_hba.hdr_rq->queue_id,
6977 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04006978 phba->sli4_hba.els_cq->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04006979 return 0;
6980
6981out_destroy_fcp_wq:
6982 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
6983 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05006984out_destroy_els_wq:
James Smartda0436e2009-05-22 14:51:39 -04006985 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
6986out_destroy_mbx_wq:
6987 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
6988out_destroy_fcp_cq:
6989 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
6990 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05006991out_destroy_els_cq:
James Smartda0436e2009-05-22 14:51:39 -04006992 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
6993out_destroy_mbx_cq:
6994 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
6995out_destroy_fp_eq:
6996 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
6997 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_eqidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05006998out_destroy_sp_eq:
James Smartda0436e2009-05-22 14:51:39 -04006999 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
7000out_error:
7001 return rc;
7002}
7003
7004/**
7005 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
7006 * @phba: pointer to lpfc hba data structure.
7007 *
7008 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
7009 * operation.
7010 *
7011 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007012 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007013 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007014 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007015 **/
7016void
7017lpfc_sli4_queue_unset(struct lpfc_hba *phba)
7018{
7019 int fcp_qidx;
7020
7021 /* Unset mailbox command work queue */
7022 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
7023 /* Unset ELS work queue */
7024 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7025 /* Unset unsolicited receive queue */
7026 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
7027 /* Unset FCP work queue */
7028 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
7029 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
7030 /* Unset mailbox command complete queue */
7031 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
7032 /* Unset ELS complete queue */
7033 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
James Smartda0436e2009-05-22 14:51:39 -04007034 /* Unset FCP response complete queue */
James Smart2e90f4b2011-12-13 13:22:37 -05007035 if (phba->sli4_hba.fcp_cq) {
7036 fcp_qidx = 0;
7037 do {
7038 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
7039 } while (++fcp_qidx < phba->cfg_fcp_eq_count);
7040 }
James Smartda0436e2009-05-22 14:51:39 -04007041 /* Unset fast-path event queue */
James Smart2e90f4b2011-12-13 13:22:37 -05007042 if (phba->sli4_hba.fp_eq) {
7043 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count;
7044 fcp_qidx++)
7045 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_qidx]);
7046 }
James Smartda0436e2009-05-22 14:51:39 -04007047 /* Unset slow-path event queue */
7048 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
7049}
7050
7051/**
7052 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
7053 * @phba: pointer to lpfc hba data structure.
7054 *
7055 * This routine is invoked to allocate and set up a pool of completion queue
7056 * events. The body of the completion queue event is a completion queue entry
7057 * CQE. For now, this pool is used for the interrupt service routine to queue
7058 * the following HBA completion queue events for the worker thread to process:
7059 * - Mailbox asynchronous events
7060 * - Receive queue completion unsolicited events
7061 * Later, this can be used for all the slow-path events.
7062 *
7063 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007064 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007065 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04007066 **/
7067static int
7068lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
7069{
7070 struct lpfc_cq_event *cq_event;
7071 int i;
7072
7073 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
7074 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
7075 if (!cq_event)
7076 goto out_pool_create_fail;
7077 list_add_tail(&cq_event->list,
7078 &phba->sli4_hba.sp_cqe_event_pool);
7079 }
7080 return 0;
7081
7082out_pool_create_fail:
7083 lpfc_sli4_cq_event_pool_destroy(phba);
7084 return -ENOMEM;
7085}
7086
7087/**
7088 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
7089 * @phba: pointer to lpfc hba data structure.
7090 *
7091 * This routine is invoked to free the pool of completion queue events at
7092 * driver unload time. Note that, it is the responsibility of the driver
7093 * cleanup routine to free all the outstanding completion-queue events
7094 * allocated from this pool back into the pool before invoking this routine
7095 * to destroy the pool.
7096 **/
7097static void
7098lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
7099{
7100 struct lpfc_cq_event *cq_event, *next_cq_event;
7101
7102 list_for_each_entry_safe(cq_event, next_cq_event,
7103 &phba->sli4_hba.sp_cqe_event_pool, list) {
7104 list_del(&cq_event->list);
7105 kfree(cq_event);
7106 }
7107}
7108
7109/**
7110 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
7111 * @phba: pointer to lpfc hba data structure.
7112 *
7113 * This routine is the lock free version of the API invoked to allocate a
7114 * completion-queue event from the free pool.
7115 *
7116 * Return: Pointer to the newly allocated completion-queue event if successful
7117 * NULL otherwise.
7118 **/
7119struct lpfc_cq_event *
7120__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
7121{
7122 struct lpfc_cq_event *cq_event = NULL;
7123
7124 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
7125 struct lpfc_cq_event, list);
7126 return cq_event;
7127}
7128
7129/**
7130 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
7131 * @phba: pointer to lpfc hba data structure.
7132 *
7133 * This routine is the lock version of the API invoked to allocate a
7134 * completion-queue event from the free pool.
7135 *
7136 * Return: Pointer to the newly allocated completion-queue event if successful
7137 * NULL otherwise.
7138 **/
7139struct lpfc_cq_event *
7140lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
7141{
7142 struct lpfc_cq_event *cq_event;
7143 unsigned long iflags;
7144
7145 spin_lock_irqsave(&phba->hbalock, iflags);
7146 cq_event = __lpfc_sli4_cq_event_alloc(phba);
7147 spin_unlock_irqrestore(&phba->hbalock, iflags);
7148 return cq_event;
7149}
7150
7151/**
7152 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
7153 * @phba: pointer to lpfc hba data structure.
7154 * @cq_event: pointer to the completion queue event to be freed.
7155 *
7156 * This routine is the lock free version of the API invoked to release a
7157 * completion-queue event back into the free pool.
7158 **/
7159void
7160__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
7161 struct lpfc_cq_event *cq_event)
7162{
7163 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
7164}
7165
7166/**
7167 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
7168 * @phba: pointer to lpfc hba data structure.
7169 * @cq_event: pointer to the completion queue event to be freed.
7170 *
7171 * This routine is the lock version of the API invoked to release a
7172 * completion-queue event back into the free pool.
7173 **/
7174void
7175lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
7176 struct lpfc_cq_event *cq_event)
7177{
7178 unsigned long iflags;
7179 spin_lock_irqsave(&phba->hbalock, iflags);
7180 __lpfc_sli4_cq_event_release(phba, cq_event);
7181 spin_unlock_irqrestore(&phba->hbalock, iflags);
7182}
7183
7184/**
7185 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
7186 * @phba: pointer to lpfc hba data structure.
7187 *
7188 * This routine is to free all the pending completion-queue events to the
7189 * back into the free pool for device reset.
7190 **/
7191static void
7192lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
7193{
7194 LIST_HEAD(cqelist);
7195 struct lpfc_cq_event *cqe;
7196 unsigned long iflags;
7197
7198 /* Retrieve all the pending WCQEs from pending WCQE lists */
7199 spin_lock_irqsave(&phba->hbalock, iflags);
7200 /* Pending FCP XRI abort events */
7201 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
7202 &cqelist);
7203 /* Pending ELS XRI abort events */
7204 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
7205 &cqelist);
7206 /* Pending asynnc events */
7207 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
7208 &cqelist);
7209 spin_unlock_irqrestore(&phba->hbalock, iflags);
7210
7211 while (!list_empty(&cqelist)) {
7212 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
7213 lpfc_sli4_cq_event_release(phba, cqe);
7214 }
7215}
7216
7217/**
7218 * lpfc_pci_function_reset - Reset pci function.
7219 * @phba: pointer to lpfc hba data structure.
7220 *
7221 * This routine is invoked to request a PCI function reset. It will destroys
7222 * all resources assigned to the PCI function which originates this request.
7223 *
7224 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007225 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007226 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007227 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007228 **/
7229int
7230lpfc_pci_function_reset(struct lpfc_hba *phba)
7231{
7232 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05007233 uint32_t rc = 0, if_type;
James Smartda0436e2009-05-22 14:51:39 -04007234 uint32_t shdr_status, shdr_add_status;
James Smart2fcee4b2010-12-15 17:57:46 -05007235 uint32_t rdy_chk, num_resets = 0, reset_again = 0;
James Smartda0436e2009-05-22 14:51:39 -04007236 union lpfc_sli4_cfg_shdr *shdr;
James Smart2fcee4b2010-12-15 17:57:46 -05007237 struct lpfc_register reg_data;
James Smartda0436e2009-05-22 14:51:39 -04007238
James Smart2fcee4b2010-12-15 17:57:46 -05007239 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7240 switch (if_type) {
7241 case LPFC_SLI_INTF_IF_TYPE_0:
7242 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
7243 GFP_KERNEL);
7244 if (!mboxq) {
7245 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7246 "0494 Unable to allocate memory for "
7247 "issuing SLI_FUNCTION_RESET mailbox "
7248 "command\n");
7249 return -ENOMEM;
7250 }
7251
7252 /* Setup PCI function reset mailbox-ioctl command */
7253 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7254 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
7255 LPFC_SLI4_MBX_EMBED);
7256 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7257 shdr = (union lpfc_sli4_cfg_shdr *)
7258 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7259 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7260 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
7261 &shdr->response);
7262 if (rc != MBX_TIMEOUT)
7263 mempool_free(mboxq, phba->mbox_mem_pool);
7264 if (shdr_status || shdr_add_status || rc) {
7265 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7266 "0495 SLI_FUNCTION_RESET mailbox "
7267 "failed with status x%x add_status x%x,"
7268 " mbx status x%x\n",
7269 shdr_status, shdr_add_status, rc);
7270 rc = -ENXIO;
7271 }
7272 break;
7273 case LPFC_SLI_INTF_IF_TYPE_2:
7274 for (num_resets = 0;
7275 num_resets < MAX_IF_TYPE_2_RESETS;
7276 num_resets++) {
7277 reg_data.word0 = 0;
7278 bf_set(lpfc_sliport_ctrl_end, &reg_data,
7279 LPFC_SLIPORT_LITTLE_ENDIAN);
7280 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
7281 LPFC_SLIPORT_INIT_PORT);
7282 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
7283 CTRLregaddr);
7284
7285 /*
7286 * Poll the Port Status Register and wait for RDY for
7287 * up to 10 seconds. If the port doesn't respond, treat
7288 * it as an error. If the port responds with RN, start
7289 * the loop again.
7290 */
7291 for (rdy_chk = 0; rdy_chk < 1000; rdy_chk++) {
James Smart73d91e52011-10-10 21:32:10 -04007292 msleep(10);
James Smart9940b972011-03-11 16:06:12 -05007293 if (lpfc_readl(phba->sli4_hba.u.if_type2.
7294 STATUSregaddr, &reg_data.word0)) {
7295 rc = -ENODEV;
James Smart73d91e52011-10-10 21:32:10 -04007296 goto out;
James Smart9940b972011-03-11 16:06:12 -05007297 }
James Smart6b5151f2012-01-18 16:24:06 -05007298 if (bf_get(lpfc_sliport_status_rn, &reg_data))
7299 reset_again++;
James Smart2fcee4b2010-12-15 17:57:46 -05007300 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
7301 break;
James Smart2fcee4b2010-12-15 17:57:46 -05007302 }
7303
7304 /*
7305 * If the port responds to the init request with
7306 * reset needed, delay for a bit and restart the loop.
7307 */
James Smart6b5151f2012-01-18 16:24:06 -05007308 if (reset_again && (rdy_chk < 1000)) {
James Smart2fcee4b2010-12-15 17:57:46 -05007309 msleep(10);
7310 reset_again = 0;
7311 continue;
7312 }
7313
7314 /* Detect any port errors. */
James Smart2fcee4b2010-12-15 17:57:46 -05007315 if ((bf_get(lpfc_sliport_status_err, &reg_data)) ||
7316 (rdy_chk >= 1000)) {
7317 phba->work_status[0] = readl(
7318 phba->sli4_hba.u.if_type2.ERR1regaddr);
7319 phba->work_status[1] = readl(
7320 phba->sli4_hba.u.if_type2.ERR2regaddr);
7321 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7322 "2890 Port Error Detected "
7323 "during Port Reset: "
7324 "port status reg 0x%x, "
7325 "error 1=0x%x, error 2=0x%x\n",
7326 reg_data.word0,
7327 phba->work_status[0],
7328 phba->work_status[1]);
7329 rc = -ENODEV;
7330 }
7331
7332 /*
7333 * Terminate the outer loop provided the Port indicated
7334 * ready within 10 seconds.
7335 */
7336 if (rdy_chk < 1000)
7337 break;
7338 }
James Smart05580562011-05-24 11:40:48 -04007339 /* delay driver action following IF_TYPE_2 function reset */
7340 msleep(100);
James Smart2fcee4b2010-12-15 17:57:46 -05007341 break;
7342 case LPFC_SLI_INTF_IF_TYPE_1:
7343 default:
7344 break;
James Smartda0436e2009-05-22 14:51:39 -04007345 }
7346
James Smart73d91e52011-10-10 21:32:10 -04007347out:
James Smart2fcee4b2010-12-15 17:57:46 -05007348 /* Catch the not-ready port failure after a port reset. */
7349 if (num_resets >= MAX_IF_TYPE_2_RESETS)
7350 rc = -ENODEV;
7351
James Smartda0436e2009-05-22 14:51:39 -04007352 return rc;
7353}
7354
7355/**
7356 * lpfc_sli4_send_nop_mbox_cmds - Send sli-4 nop mailbox commands
7357 * @phba: pointer to lpfc hba data structure.
7358 * @cnt: number of nop mailbox commands to send.
7359 *
7360 * This routine is invoked to send a number @cnt of NOP mailbox command and
7361 * wait for each command to complete.
7362 *
7363 * Return: the number of NOP mailbox command completed.
7364 **/
7365static int
7366lpfc_sli4_send_nop_mbox_cmds(struct lpfc_hba *phba, uint32_t cnt)
7367{
7368 LPFC_MBOXQ_t *mboxq;
7369 int length, cmdsent;
7370 uint32_t mbox_tmo;
7371 uint32_t rc = 0;
7372 uint32_t shdr_status, shdr_add_status;
7373 union lpfc_sli4_cfg_shdr *shdr;
7374
7375 if (cnt == 0) {
7376 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7377 "2518 Requested to send 0 NOP mailbox cmd\n");
7378 return cnt;
7379 }
7380
7381 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7382 if (!mboxq) {
7383 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7384 "2519 Unable to allocate memory for issuing "
7385 "NOP mailbox command\n");
7386 return 0;
7387 }
7388
7389 /* Set up NOP SLI4_CONFIG mailbox-ioctl command */
7390 length = (sizeof(struct lpfc_mbx_nop) -
7391 sizeof(struct lpfc_sli4_cfg_mhdr));
7392 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7393 LPFC_MBOX_OPCODE_NOP, length, LPFC_SLI4_MBX_EMBED);
7394
James Smartda0436e2009-05-22 14:51:39 -04007395 for (cmdsent = 0; cmdsent < cnt; cmdsent++) {
7396 if (!phba->sli4_hba.intr_enable)
7397 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -04007398 else {
7399 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04007400 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smarta183a152011-10-10 21:32:43 -04007401 }
James Smartda0436e2009-05-22 14:51:39 -04007402 if (rc == MBX_TIMEOUT)
7403 break;
7404 /* Check return status */
7405 shdr = (union lpfc_sli4_cfg_shdr *)
7406 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7407 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7408 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
7409 &shdr->response);
7410 if (shdr_status || shdr_add_status || rc) {
7411 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7412 "2520 NOP mailbox command failed "
7413 "status x%x add_status x%x mbx "
7414 "status x%x\n", shdr_status,
7415 shdr_add_status, rc);
7416 break;
7417 }
7418 }
7419
7420 if (rc != MBX_TIMEOUT)
7421 mempool_free(mboxq, phba->mbox_mem_pool);
7422
7423 return cmdsent;
7424}
7425
7426/**
James Smartda0436e2009-05-22 14:51:39 -04007427 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
7428 * @phba: pointer to lpfc hba data structure.
7429 *
7430 * This routine is invoked to set up the PCI device memory space for device
7431 * with SLI-4 interface spec.
7432 *
7433 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007434 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007435 * other values - error
7436 **/
7437static int
7438lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
7439{
7440 struct pci_dev *pdev;
7441 unsigned long bar0map_len, bar1map_len, bar2map_len;
7442 int error = -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -05007443 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04007444
7445 /* Obtain PCI device reference */
7446 if (!phba->pcidev)
7447 return error;
7448 else
7449 pdev = phba->pcidev;
7450
7451 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05007452 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
7453 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
7454 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
7455 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smartda0436e2009-05-22 14:51:39 -04007456 return error;
Michael Reed8e685972009-09-18 12:02:05 -05007457 }
7458 }
James Smartda0436e2009-05-22 14:51:39 -04007459
James Smart2fcee4b2010-12-15 17:57:46 -05007460 /*
7461 * The BARs and register set definitions and offset locations are
7462 * dependent on the if_type.
7463 */
7464 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
7465 &phba->sli4_hba.sli_intf.word0)) {
7466 return error;
7467 }
7468
7469 /* There is no SLI3 failback for SLI4 devices. */
7470 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
7471 LPFC_SLI_INTF_VALID) {
7472 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7473 "2894 SLI_INTF reg contents invalid "
7474 "sli_intf reg 0x%x\n",
7475 phba->sli4_hba.sli_intf.word0);
7476 return error;
7477 }
7478
7479 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7480 /*
7481 * Get the bus address of SLI4 device Bar regions and the
7482 * number of bytes required by each mapping. The mapping of the
7483 * particular PCI BARs regions is dependent on the type of
7484 * SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -04007485 */
James Smart1dfb5a42010-02-12 14:40:50 -05007486 if (pci_resource_start(pdev, 0)) {
7487 phba->pci_bar0_map = pci_resource_start(pdev, 0);
7488 bar0map_len = pci_resource_len(pdev, 0);
James Smart2fcee4b2010-12-15 17:57:46 -05007489
7490 /*
7491 * Map SLI4 PCI Config Space Register base to a kernel virtual
7492 * addr
7493 */
7494 phba->sli4_hba.conf_regs_memmap_p =
7495 ioremap(phba->pci_bar0_map, bar0map_len);
7496 if (!phba->sli4_hba.conf_regs_memmap_p) {
7497 dev_printk(KERN_ERR, &pdev->dev,
7498 "ioremap failed for SLI4 PCI config "
7499 "registers.\n");
7500 goto out;
7501 }
7502 /* Set up BAR0 PCI config space register memory map */
7503 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smart1dfb5a42010-02-12 14:40:50 -05007504 } else {
7505 phba->pci_bar0_map = pci_resource_start(pdev, 1);
7506 bar0map_len = pci_resource_len(pdev, 1);
James Smart2fcee4b2010-12-15 17:57:46 -05007507 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
7508 dev_printk(KERN_ERR, &pdev->dev,
7509 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
7510 goto out;
7511 }
7512 phba->sli4_hba.conf_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04007513 ioremap(phba->pci_bar0_map, bar0map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05007514 if (!phba->sli4_hba.conf_regs_memmap_p) {
7515 dev_printk(KERN_ERR, &pdev->dev,
7516 "ioremap failed for SLI4 PCI config "
7517 "registers.\n");
7518 goto out;
7519 }
7520 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smartda0436e2009-05-22 14:51:39 -04007521 }
7522
James Smartc31098c2011-04-16 11:03:33 -04007523 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
7524 (pci_resource_start(pdev, 2))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007525 /*
7526 * Map SLI4 if type 0 HBA Control Register base to a kernel
7527 * virtual address and setup the registers.
7528 */
7529 phba->pci_bar1_map = pci_resource_start(pdev, 2);
7530 bar1map_len = pci_resource_len(pdev, 2);
7531 phba->sli4_hba.ctrl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04007532 ioremap(phba->pci_bar1_map, bar1map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05007533 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
7534 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04007535 "ioremap failed for SLI4 HBA control registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05007536 goto out_iounmap_conf;
7537 }
7538 lpfc_sli4_bar1_register_memmap(phba);
James Smartda0436e2009-05-22 14:51:39 -04007539 }
7540
James Smartc31098c2011-04-16 11:03:33 -04007541 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
7542 (pci_resource_start(pdev, 4))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007543 /*
7544 * Map SLI4 if type 0 HBA Doorbell Register base to a kernel
7545 * virtual address and setup the registers.
7546 */
7547 phba->pci_bar2_map = pci_resource_start(pdev, 4);
7548 bar2map_len = pci_resource_len(pdev, 4);
7549 phba->sli4_hba.drbl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04007550 ioremap(phba->pci_bar2_map, bar2map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05007551 if (!phba->sli4_hba.drbl_regs_memmap_p) {
7552 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04007553 "ioremap failed for SLI4 HBA doorbell registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05007554 goto out_iounmap_ctrl;
7555 }
7556 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
7557 if (error)
7558 goto out_iounmap_all;
James Smartda0436e2009-05-22 14:51:39 -04007559 }
7560
James Smartda0436e2009-05-22 14:51:39 -04007561 return 0;
7562
7563out_iounmap_all:
7564 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
7565out_iounmap_ctrl:
7566 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
7567out_iounmap_conf:
7568 iounmap(phba->sli4_hba.conf_regs_memmap_p);
7569out:
7570 return error;
7571}
7572
7573/**
7574 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
7575 * @phba: pointer to lpfc hba data structure.
7576 *
7577 * This routine is invoked to unset the PCI device memory space for device
7578 * with SLI-4 interface spec.
7579 **/
7580static void
7581lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
7582{
James Smart2e90f4b2011-12-13 13:22:37 -05007583 uint32_t if_type;
7584 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smartda0436e2009-05-22 14:51:39 -04007585
James Smart2e90f4b2011-12-13 13:22:37 -05007586 switch (if_type) {
7587 case LPFC_SLI_INTF_IF_TYPE_0:
7588 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
7589 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
7590 iounmap(phba->sli4_hba.conf_regs_memmap_p);
7591 break;
7592 case LPFC_SLI_INTF_IF_TYPE_2:
7593 iounmap(phba->sli4_hba.conf_regs_memmap_p);
7594 break;
7595 case LPFC_SLI_INTF_IF_TYPE_1:
7596 default:
7597 dev_printk(KERN_ERR, &phba->pcidev->dev,
7598 "FATAL - unsupported SLI4 interface type - %d\n",
7599 if_type);
7600 break;
7601 }
James Smartda0436e2009-05-22 14:51:39 -04007602}
7603
7604/**
James Smart3772a992009-05-22 14:50:54 -04007605 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
7606 * @phba: pointer to lpfc hba data structure.
7607 *
7608 * This routine is invoked to enable the MSI-X interrupt vectors to device
7609 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
7610 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
7611 * invoked, enables either all or nothing, depending on the current
7612 * availability of PCI vector resources. The device driver is responsible
7613 * for calling the individual request_irq() to register each MSI-X vector
7614 * with a interrupt handler, which is done in this function. Note that
7615 * later when device is unloading, the driver should always call free_irq()
7616 * on all MSI-X vectors it has done request_irq() on before calling
7617 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
7618 * will be left with MSI-X enabled and leaks its vectors.
7619 *
7620 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007621 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007622 * other values - error
7623 **/
7624static int
7625lpfc_sli_enable_msix(struct lpfc_hba *phba)
7626{
7627 int rc, i;
7628 LPFC_MBOXQ_t *pmb;
7629
7630 /* Set up MSI-X multi-message vectors */
7631 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
7632 phba->msix_entries[i].entry = i;
7633
7634 /* Configure MSI-X capability structure */
7635 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
7636 ARRAY_SIZE(phba->msix_entries));
7637 if (rc) {
7638 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7639 "0420 PCI enable MSI-X failed (%d)\n", rc);
7640 goto msi_fail_out;
7641 }
7642 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
7643 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7644 "0477 MSI-X entry[%d]: vector=x%x "
7645 "message=%d\n", i,
7646 phba->msix_entries[i].vector,
7647 phba->msix_entries[i].entry);
7648 /*
7649 * Assign MSI-X vectors to interrupt handlers
7650 */
7651
7652 /* vector-0 is associated to slow-path handler */
7653 rc = request_irq(phba->msix_entries[0].vector,
7654 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
7655 LPFC_SP_DRIVER_HANDLER_NAME, phba);
7656 if (rc) {
7657 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7658 "0421 MSI-X slow-path request_irq failed "
7659 "(%d)\n", rc);
7660 goto msi_fail_out;
7661 }
7662
7663 /* vector-1 is associated to fast-path handler */
7664 rc = request_irq(phba->msix_entries[1].vector,
7665 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
7666 LPFC_FP_DRIVER_HANDLER_NAME, phba);
7667
7668 if (rc) {
7669 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7670 "0429 MSI-X fast-path request_irq failed "
7671 "(%d)\n", rc);
7672 goto irq_fail_out;
7673 }
7674
7675 /*
7676 * Configure HBA MSI-X attention conditions to messages
7677 */
7678 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7679
7680 if (!pmb) {
7681 rc = -ENOMEM;
7682 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7683 "0474 Unable to allocate memory for issuing "
7684 "MBOX_CONFIG_MSI command\n");
7685 goto mem_fail_out;
7686 }
7687 rc = lpfc_config_msi(phba, pmb);
7688 if (rc)
7689 goto mbx_fail_out;
7690 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
7691 if (rc != MBX_SUCCESS) {
7692 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
7693 "0351 Config MSI mailbox command failed, "
7694 "mbxCmd x%x, mbxStatus x%x\n",
7695 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
7696 goto mbx_fail_out;
7697 }
7698
7699 /* Free memory allocated for mailbox command */
7700 mempool_free(pmb, phba->mbox_mem_pool);
7701 return rc;
7702
7703mbx_fail_out:
7704 /* Free memory allocated for mailbox command */
7705 mempool_free(pmb, phba->mbox_mem_pool);
7706
7707mem_fail_out:
7708 /* free the irq already requested */
7709 free_irq(phba->msix_entries[1].vector, phba);
7710
7711irq_fail_out:
7712 /* free the irq already requested */
7713 free_irq(phba->msix_entries[0].vector, phba);
7714
7715msi_fail_out:
7716 /* Unconfigure MSI-X capability structure */
7717 pci_disable_msix(phba->pcidev);
7718 return rc;
7719}
7720
7721/**
7722 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
7723 * @phba: pointer to lpfc hba data structure.
7724 *
7725 * This routine is invoked to release the MSI-X vectors and then disable the
7726 * MSI-X interrupt mode to device with SLI-3 interface spec.
7727 **/
7728static void
7729lpfc_sli_disable_msix(struct lpfc_hba *phba)
7730{
7731 int i;
7732
7733 /* Free up MSI-X multi-message vectors */
7734 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
7735 free_irq(phba->msix_entries[i].vector, phba);
7736 /* Disable MSI-X */
7737 pci_disable_msix(phba->pcidev);
7738
7739 return;
7740}
7741
7742/**
7743 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
7744 * @phba: pointer to lpfc hba data structure.
7745 *
7746 * This routine is invoked to enable the MSI interrupt mode to device with
7747 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
7748 * enable the MSI vector. The device driver is responsible for calling the
7749 * request_irq() to register MSI vector with a interrupt the handler, which
7750 * is done in this function.
7751 *
7752 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007753 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007754 * other values - error
7755 */
7756static int
7757lpfc_sli_enable_msi(struct lpfc_hba *phba)
7758{
7759 int rc;
7760
7761 rc = pci_enable_msi(phba->pcidev);
7762 if (!rc)
7763 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7764 "0462 PCI enable MSI mode success.\n");
7765 else {
7766 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7767 "0471 PCI enable MSI mode failed (%d)\n", rc);
7768 return rc;
7769 }
7770
7771 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
7772 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7773 if (rc) {
7774 pci_disable_msi(phba->pcidev);
7775 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7776 "0478 MSI request_irq failed (%d)\n", rc);
7777 }
7778 return rc;
7779}
7780
7781/**
7782 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
7783 * @phba: pointer to lpfc hba data structure.
7784 *
7785 * This routine is invoked to disable the MSI interrupt mode to device with
7786 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
7787 * done request_irq() on before calling pci_disable_msi(). Failure to do so
7788 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
7789 * its vector.
7790 */
7791static void
7792lpfc_sli_disable_msi(struct lpfc_hba *phba)
7793{
7794 free_irq(phba->pcidev->irq, phba);
7795 pci_disable_msi(phba->pcidev);
7796 return;
7797}
7798
7799/**
7800 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
7801 * @phba: pointer to lpfc hba data structure.
7802 *
7803 * This routine is invoked to enable device interrupt and associate driver's
7804 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
7805 * spec. Depends on the interrupt mode configured to the driver, the driver
7806 * will try to fallback from the configured interrupt mode to an interrupt
7807 * mode which is supported by the platform, kernel, and device in the order
7808 * of:
7809 * MSI-X -> MSI -> IRQ.
7810 *
7811 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007812 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007813 * other values - error
7814 **/
7815static uint32_t
7816lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
7817{
7818 uint32_t intr_mode = LPFC_INTR_ERROR;
7819 int retval;
7820
7821 if (cfg_mode == 2) {
7822 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
7823 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
7824 if (!retval) {
7825 /* Now, try to enable MSI-X interrupt mode */
7826 retval = lpfc_sli_enable_msix(phba);
7827 if (!retval) {
7828 /* Indicate initialization to MSI-X mode */
7829 phba->intr_type = MSIX;
7830 intr_mode = 2;
7831 }
7832 }
7833 }
7834
7835 /* Fallback to MSI if MSI-X initialization failed */
7836 if (cfg_mode >= 1 && phba->intr_type == NONE) {
7837 retval = lpfc_sli_enable_msi(phba);
7838 if (!retval) {
7839 /* Indicate initialization to MSI mode */
7840 phba->intr_type = MSI;
7841 intr_mode = 1;
7842 }
7843 }
7844
7845 /* Fallback to INTx if both MSI-X/MSI initalization failed */
7846 if (phba->intr_type == NONE) {
7847 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
7848 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7849 if (!retval) {
7850 /* Indicate initialization to INTx mode */
7851 phba->intr_type = INTx;
7852 intr_mode = 0;
7853 }
7854 }
7855 return intr_mode;
7856}
7857
7858/**
7859 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
7860 * @phba: pointer to lpfc hba data structure.
7861 *
7862 * This routine is invoked to disable device interrupt and disassociate the
7863 * driver's interrupt handler(s) from interrupt vector(s) to device with
7864 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
7865 * release the interrupt vector(s) for the message signaled interrupt.
7866 **/
7867static void
7868lpfc_sli_disable_intr(struct lpfc_hba *phba)
7869{
7870 /* Disable the currently initialized interrupt mode */
7871 if (phba->intr_type == MSIX)
7872 lpfc_sli_disable_msix(phba);
7873 else if (phba->intr_type == MSI)
7874 lpfc_sli_disable_msi(phba);
7875 else if (phba->intr_type == INTx)
7876 free_irq(phba->pcidev->irq, phba);
7877
7878 /* Reset interrupt management states */
7879 phba->intr_type = NONE;
7880 phba->sli.slistat.sli_intr = 0;
7881
7882 return;
7883}
7884
7885/**
James Smartda0436e2009-05-22 14:51:39 -04007886 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
7887 * @phba: pointer to lpfc hba data structure.
7888 *
7889 * This routine is invoked to enable the MSI-X interrupt vectors to device
7890 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
7891 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
7892 * enables either all or nothing, depending on the current availability of
7893 * PCI vector resources. The device driver is responsible for calling the
7894 * individual request_irq() to register each MSI-X vector with a interrupt
7895 * handler, which is done in this function. Note that later when device is
7896 * unloading, the driver should always call free_irq() on all MSI-X vectors
7897 * it has done request_irq() on before calling pci_disable_msix(). Failure
7898 * to do so results in a BUG_ON() and a device will be left with MSI-X
7899 * enabled and leaks its vectors.
7900 *
7901 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007902 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007903 * other values - error
7904 **/
7905static int
7906lpfc_sli4_enable_msix(struct lpfc_hba *phba)
7907{
James Smart75baf692010-06-08 18:31:21 -04007908 int vectors, rc, index;
James Smartda0436e2009-05-22 14:51:39 -04007909
7910 /* Set up MSI-X multi-message vectors */
7911 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
7912 phba->sli4_hba.msix_entries[index].entry = index;
7913
7914 /* Configure MSI-X capability structure */
James Smart75baf692010-06-08 18:31:21 -04007915 vectors = phba->sli4_hba.cfg_eqn;
7916enable_msix_vectors:
James Smartda0436e2009-05-22 14:51:39 -04007917 rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
James Smart75baf692010-06-08 18:31:21 -04007918 vectors);
7919 if (rc > 1) {
7920 vectors = rc;
7921 goto enable_msix_vectors;
7922 } else if (rc) {
James Smartda0436e2009-05-22 14:51:39 -04007923 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7924 "0484 PCI enable MSI-X failed (%d)\n", rc);
7925 goto msi_fail_out;
7926 }
James Smart75baf692010-06-08 18:31:21 -04007927
James Smartda0436e2009-05-22 14:51:39 -04007928 /* Log MSI-X vector assignment */
James Smart75baf692010-06-08 18:31:21 -04007929 for (index = 0; index < vectors; index++)
James Smartda0436e2009-05-22 14:51:39 -04007930 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7931 "0489 MSI-X entry[%d]: vector=x%x "
7932 "message=%d\n", index,
7933 phba->sli4_hba.msix_entries[index].vector,
7934 phba->sli4_hba.msix_entries[index].entry);
7935 /*
7936 * Assign MSI-X vectors to interrupt handlers
7937 */
James Smart05580562011-05-24 11:40:48 -04007938 if (vectors > 1)
7939 rc = request_irq(phba->sli4_hba.msix_entries[0].vector,
7940 &lpfc_sli4_sp_intr_handler, IRQF_SHARED,
7941 LPFC_SP_DRIVER_HANDLER_NAME, phba);
7942 else
7943 /* All Interrupts need to be handled by one EQ */
7944 rc = request_irq(phba->sli4_hba.msix_entries[0].vector,
7945 &lpfc_sli4_intr_handler, IRQF_SHARED,
7946 LPFC_DRIVER_NAME, phba);
James Smartda0436e2009-05-22 14:51:39 -04007947 if (rc) {
7948 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7949 "0485 MSI-X slow-path request_irq failed "
7950 "(%d)\n", rc);
7951 goto msi_fail_out;
7952 }
7953
7954 /* The rest of the vector(s) are associated to fast-path handler(s) */
James Smart75baf692010-06-08 18:31:21 -04007955 for (index = 1; index < vectors; index++) {
James Smartda0436e2009-05-22 14:51:39 -04007956 phba->sli4_hba.fcp_eq_hdl[index - 1].idx = index - 1;
7957 phba->sli4_hba.fcp_eq_hdl[index - 1].phba = phba;
7958 rc = request_irq(phba->sli4_hba.msix_entries[index].vector,
7959 &lpfc_sli4_fp_intr_handler, IRQF_SHARED,
7960 LPFC_FP_DRIVER_HANDLER_NAME,
7961 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
7962 if (rc) {
7963 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7964 "0486 MSI-X fast-path (%d) "
7965 "request_irq failed (%d)\n", index, rc);
7966 goto cfg_fail_out;
7967 }
7968 }
James Smart75baf692010-06-08 18:31:21 -04007969 phba->sli4_hba.msix_vec_nr = vectors;
James Smartda0436e2009-05-22 14:51:39 -04007970
7971 return rc;
7972
7973cfg_fail_out:
7974 /* free the irq already requested */
7975 for (--index; index >= 1; index--)
7976 free_irq(phba->sli4_hba.msix_entries[index - 1].vector,
7977 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
7978
7979 /* free the irq already requested */
7980 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
7981
7982msi_fail_out:
7983 /* Unconfigure MSI-X capability structure */
7984 pci_disable_msix(phba->pcidev);
7985 return rc;
7986}
7987
7988/**
7989 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
7990 * @phba: pointer to lpfc hba data structure.
7991 *
7992 * This routine is invoked to release the MSI-X vectors and then disable the
7993 * MSI-X interrupt mode to device with SLI-4 interface spec.
7994 **/
7995static void
7996lpfc_sli4_disable_msix(struct lpfc_hba *phba)
7997{
7998 int index;
7999
8000 /* Free up MSI-X multi-message vectors */
8001 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
8002
James Smart75baf692010-06-08 18:31:21 -04008003 for (index = 1; index < phba->sli4_hba.msix_vec_nr; index++)
James Smartda0436e2009-05-22 14:51:39 -04008004 free_irq(phba->sli4_hba.msix_entries[index].vector,
8005 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
James Smart75baf692010-06-08 18:31:21 -04008006
James Smartda0436e2009-05-22 14:51:39 -04008007 /* Disable MSI-X */
8008 pci_disable_msix(phba->pcidev);
8009
8010 return;
8011}
8012
8013/**
8014 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
8015 * @phba: pointer to lpfc hba data structure.
8016 *
8017 * This routine is invoked to enable the MSI interrupt mode to device with
8018 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
8019 * to enable the MSI vector. The device driver is responsible for calling
8020 * the request_irq() to register MSI vector with a interrupt the handler,
8021 * which is done in this function.
8022 *
8023 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008024 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008025 * other values - error
8026 **/
8027static int
8028lpfc_sli4_enable_msi(struct lpfc_hba *phba)
8029{
8030 int rc, index;
8031
8032 rc = pci_enable_msi(phba->pcidev);
8033 if (!rc)
8034 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8035 "0487 PCI enable MSI mode success.\n");
8036 else {
8037 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8038 "0488 PCI enable MSI mode failed (%d)\n", rc);
8039 return rc;
8040 }
8041
8042 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
8043 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8044 if (rc) {
8045 pci_disable_msi(phba->pcidev);
8046 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8047 "0490 MSI request_irq failed (%d)\n", rc);
James Smart75baf692010-06-08 18:31:21 -04008048 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008049 }
8050
8051 for (index = 0; index < phba->cfg_fcp_eq_count; index++) {
8052 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8053 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
8054 }
8055
James Smart75baf692010-06-08 18:31:21 -04008056 return 0;
James Smartda0436e2009-05-22 14:51:39 -04008057}
8058
8059/**
8060 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
8061 * @phba: pointer to lpfc hba data structure.
8062 *
8063 * This routine is invoked to disable the MSI interrupt mode to device with
8064 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
8065 * done request_irq() on before calling pci_disable_msi(). Failure to do so
8066 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
8067 * its vector.
8068 **/
8069static void
8070lpfc_sli4_disable_msi(struct lpfc_hba *phba)
8071{
8072 free_irq(phba->pcidev->irq, phba);
8073 pci_disable_msi(phba->pcidev);
8074 return;
8075}
8076
8077/**
8078 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
8079 * @phba: pointer to lpfc hba data structure.
8080 *
8081 * This routine is invoked to enable device interrupt and associate driver's
8082 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
8083 * interface spec. Depends on the interrupt mode configured to the driver,
8084 * the driver will try to fallback from the configured interrupt mode to an
8085 * interrupt mode which is supported by the platform, kernel, and device in
8086 * the order of:
8087 * MSI-X -> MSI -> IRQ.
8088 *
8089 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008090 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008091 * other values - error
8092 **/
8093static uint32_t
8094lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
8095{
8096 uint32_t intr_mode = LPFC_INTR_ERROR;
8097 int retval, index;
8098
8099 if (cfg_mode == 2) {
8100 /* Preparation before conf_msi mbox cmd */
8101 retval = 0;
8102 if (!retval) {
8103 /* Now, try to enable MSI-X interrupt mode */
8104 retval = lpfc_sli4_enable_msix(phba);
8105 if (!retval) {
8106 /* Indicate initialization to MSI-X mode */
8107 phba->intr_type = MSIX;
8108 intr_mode = 2;
8109 }
8110 }
8111 }
8112
8113 /* Fallback to MSI if MSI-X initialization failed */
8114 if (cfg_mode >= 1 && phba->intr_type == NONE) {
8115 retval = lpfc_sli4_enable_msi(phba);
8116 if (!retval) {
8117 /* Indicate initialization to MSI mode */
8118 phba->intr_type = MSI;
8119 intr_mode = 1;
8120 }
8121 }
8122
8123 /* Fallback to INTx if both MSI-X/MSI initalization failed */
8124 if (phba->intr_type == NONE) {
8125 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
8126 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8127 if (!retval) {
8128 /* Indicate initialization to INTx mode */
8129 phba->intr_type = INTx;
8130 intr_mode = 0;
8131 for (index = 0; index < phba->cfg_fcp_eq_count;
8132 index++) {
8133 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8134 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
8135 }
8136 }
8137 }
8138 return intr_mode;
8139}
8140
8141/**
8142 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
8143 * @phba: pointer to lpfc hba data structure.
8144 *
8145 * This routine is invoked to disable device interrupt and disassociate
8146 * the driver's interrupt handler(s) from interrupt vector(s) to device
8147 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
8148 * will release the interrupt vector(s) for the message signaled interrupt.
8149 **/
8150static void
8151lpfc_sli4_disable_intr(struct lpfc_hba *phba)
8152{
8153 /* Disable the currently initialized interrupt mode */
8154 if (phba->intr_type == MSIX)
8155 lpfc_sli4_disable_msix(phba);
8156 else if (phba->intr_type == MSI)
8157 lpfc_sli4_disable_msi(phba);
8158 else if (phba->intr_type == INTx)
8159 free_irq(phba->pcidev->irq, phba);
8160
8161 /* Reset interrupt management states */
8162 phba->intr_type = NONE;
8163 phba->sli.slistat.sli_intr = 0;
8164
8165 return;
8166}
8167
8168/**
James Smart3772a992009-05-22 14:50:54 -04008169 * lpfc_unset_hba - Unset SLI3 hba device initialization
8170 * @phba: pointer to lpfc hba data structure.
8171 *
8172 * This routine is invoked to unset the HBA device initialization steps to
8173 * a device with SLI-3 interface spec.
8174 **/
8175static void
8176lpfc_unset_hba(struct lpfc_hba *phba)
8177{
8178 struct lpfc_vport *vport = phba->pport;
8179 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
8180
8181 spin_lock_irq(shost->host_lock);
8182 vport->load_flag |= FC_UNLOADING;
8183 spin_unlock_irq(shost->host_lock);
8184
James Smart72859902012-01-18 16:25:38 -05008185 kfree(phba->vpi_bmask);
8186 kfree(phba->vpi_ids);
8187
James Smart3772a992009-05-22 14:50:54 -04008188 lpfc_stop_hba_timers(phba);
8189
8190 phba->pport->work_port_events = 0;
8191
8192 lpfc_sli_hba_down(phba);
8193
8194 lpfc_sli_brdrestart(phba);
8195
8196 lpfc_sli_disable_intr(phba);
8197
8198 return;
8199}
8200
8201/**
James Smartda0436e2009-05-22 14:51:39 -04008202 * lpfc_sli4_unset_hba - Unset SLI4 hba device initialization.
8203 * @phba: pointer to lpfc hba data structure.
8204 *
8205 * This routine is invoked to unset the HBA device initialization steps to
8206 * a device with SLI-4 interface spec.
8207 **/
8208static void
8209lpfc_sli4_unset_hba(struct lpfc_hba *phba)
8210{
8211 struct lpfc_vport *vport = phba->pport;
8212 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
8213
8214 spin_lock_irq(shost->host_lock);
8215 vport->load_flag |= FC_UNLOADING;
8216 spin_unlock_irq(shost->host_lock);
8217
8218 phba->pport->work_port_events = 0;
8219
James Smart3677a3a2010-09-29 11:19:14 -04008220 /* Stop the SLI4 device port */
8221 lpfc_stop_port(phba);
James Smartda0436e2009-05-22 14:51:39 -04008222
8223 lpfc_sli4_disable_intr(phba);
8224
James Smart3677a3a2010-09-29 11:19:14 -04008225 /* Reset SLI4 HBA FCoE function */
8226 lpfc_pci_function_reset(phba);
James Smart5350d872011-10-10 21:33:49 -04008227 lpfc_sli4_queue_destroy(phba);
James Smart3677a3a2010-09-29 11:19:14 -04008228
James Smartda0436e2009-05-22 14:51:39 -04008229 return;
8230}
8231
8232/**
James Smart5af5eee2010-10-22 11:06:38 -04008233 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
8234 * @phba: Pointer to HBA context object.
8235 *
8236 * This function is called in the SLI4 code path to wait for completion
8237 * of device's XRIs exchange busy. It will check the XRI exchange busy
8238 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
8239 * that, it will check the XRI exchange busy on outstanding FCP and ELS
8240 * I/Os every 30 seconds, log error message, and wait forever. Only when
8241 * all XRI exchange busy complete, the driver unload shall proceed with
8242 * invoking the function reset ioctl mailbox command to the CNA and the
8243 * the rest of the driver unload resource release.
8244 **/
8245static void
8246lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
8247{
8248 int wait_time = 0;
8249 int fcp_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
8250 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
8251
8252 while (!fcp_xri_cmpl || !els_xri_cmpl) {
8253 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
8254 if (!fcp_xri_cmpl)
8255 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8256 "2877 FCP XRI exchange busy "
8257 "wait time: %d seconds.\n",
8258 wait_time/1000);
8259 if (!els_xri_cmpl)
8260 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8261 "2878 ELS XRI exchange busy "
8262 "wait time: %d seconds.\n",
8263 wait_time/1000);
8264 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
8265 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
8266 } else {
8267 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
8268 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
8269 }
8270 fcp_xri_cmpl =
8271 list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
8272 els_xri_cmpl =
8273 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
8274 }
8275}
8276
8277/**
James Smartda0436e2009-05-22 14:51:39 -04008278 * lpfc_sli4_hba_unset - Unset the fcoe hba
8279 * @phba: Pointer to HBA context object.
8280 *
8281 * This function is called in the SLI4 code path to reset the HBA's FCoE
8282 * function. The caller is not required to hold any lock. This routine
8283 * issues PCI function reset mailbox command to reset the FCoE function.
8284 * At the end of the function, it calls lpfc_hba_down_post function to
8285 * free any pending commands.
8286 **/
8287static void
8288lpfc_sli4_hba_unset(struct lpfc_hba *phba)
8289{
8290 int wait_cnt = 0;
8291 LPFC_MBOXQ_t *mboxq;
James Smart912e3ac2011-05-24 11:42:11 -04008292 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -04008293
8294 lpfc_stop_hba_timers(phba);
8295 phba->sli4_hba.intr_enable = 0;
8296
8297 /*
8298 * Gracefully wait out the potential current outstanding asynchronous
8299 * mailbox command.
8300 */
8301
8302 /* First, block any pending async mailbox command from posted */
8303 spin_lock_irq(&phba->hbalock);
8304 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
8305 spin_unlock_irq(&phba->hbalock);
8306 /* Now, trying to wait it out if we can */
8307 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8308 msleep(10);
8309 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
8310 break;
8311 }
8312 /* Forcefully release the outstanding mailbox command if timed out */
8313 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8314 spin_lock_irq(&phba->hbalock);
8315 mboxq = phba->sli.mbox_active;
8316 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8317 __lpfc_mbox_cmpl_put(phba, mboxq);
8318 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8319 phba->sli.mbox_active = NULL;
8320 spin_unlock_irq(&phba->hbalock);
8321 }
8322
James Smart5af5eee2010-10-22 11:06:38 -04008323 /* Abort all iocbs associated with the hba */
8324 lpfc_sli_hba_iocb_abort(phba);
8325
8326 /* Wait for completion of device XRI exchange busy */
8327 lpfc_sli4_xri_exchange_busy_wait(phba);
8328
James Smartda0436e2009-05-22 14:51:39 -04008329 /* Disable PCI subsystem interrupt */
8330 lpfc_sli4_disable_intr(phba);
8331
James Smart912e3ac2011-05-24 11:42:11 -04008332 /* Disable SR-IOV if enabled */
8333 if (phba->cfg_sriov_nr_virtfn)
8334 pci_disable_sriov(pdev);
8335
James Smartda0436e2009-05-22 14:51:39 -04008336 /* Stop kthread signal shall trigger work_done one more time */
8337 kthread_stop(phba->worker_thread);
8338
James Smart3677a3a2010-09-29 11:19:14 -04008339 /* Reset SLI4 HBA FCoE function */
8340 lpfc_pci_function_reset(phba);
James Smart5350d872011-10-10 21:33:49 -04008341 lpfc_sli4_queue_destroy(phba);
James Smart3677a3a2010-09-29 11:19:14 -04008342
James Smartda0436e2009-05-22 14:51:39 -04008343 /* Stop the SLI4 device port */
8344 phba->pport->work_port_events = 0;
8345}
8346
James Smart28baac72010-02-12 14:42:03 -05008347 /**
8348 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
8349 * @phba: Pointer to HBA context object.
8350 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
8351 *
8352 * This function is called in the SLI4 code path to read the port's
8353 * sli4 capabilities.
8354 *
8355 * This function may be be called from any context that can block-wait
8356 * for the completion. The expectation is that this routine is called
8357 * typically from probe_one or from the online routine.
8358 **/
8359int
8360lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8361{
8362 int rc;
8363 struct lpfc_mqe *mqe;
8364 struct lpfc_pc_sli4_params *sli4_params;
8365 uint32_t mbox_tmo;
8366
8367 rc = 0;
8368 mqe = &mboxq->u.mqe;
8369
8370 /* Read the port's SLI4 Parameters port capabilities */
James Smartfedd3b72011-02-16 12:39:24 -05008371 lpfc_pc_sli4_params(mboxq);
James Smart28baac72010-02-12 14:42:03 -05008372 if (!phba->sli4_hba.intr_enable)
8373 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8374 else {
James Smarta183a152011-10-10 21:32:43 -04008375 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart28baac72010-02-12 14:42:03 -05008376 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
8377 }
8378
8379 if (unlikely(rc))
8380 return 1;
8381
8382 sli4_params = &phba->sli4_hba.pc_sli4_params;
8383 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
8384 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
8385 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
8386 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
8387 &mqe->un.sli4_params);
8388 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
8389 &mqe->un.sli4_params);
8390 sli4_params->proto_types = mqe->un.sli4_params.word3;
8391 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
8392 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
8393 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
8394 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
8395 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
8396 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
8397 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
8398 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
8399 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
8400 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
8401 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
8402 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
8403 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
8404 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
8405 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
8406 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
8407 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
8408 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
8409 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
8410 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
James Smart05580562011-05-24 11:40:48 -04008411
8412 /* Make sure that sge_supp_len can be handled by the driver */
8413 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
8414 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
8415
James Smart28baac72010-02-12 14:42:03 -05008416 return rc;
8417}
8418
James Smartda0436e2009-05-22 14:51:39 -04008419/**
James Smartfedd3b72011-02-16 12:39:24 -05008420 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
8421 * @phba: Pointer to HBA context object.
8422 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
8423 *
8424 * This function is called in the SLI4 code path to read the port's
8425 * sli4 capabilities.
8426 *
8427 * This function may be be called from any context that can block-wait
8428 * for the completion. The expectation is that this routine is called
8429 * typically from probe_one or from the online routine.
8430 **/
8431int
8432lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8433{
8434 int rc;
8435 struct lpfc_mqe *mqe = &mboxq->u.mqe;
8436 struct lpfc_pc_sli4_params *sli4_params;
James Smarta183a152011-10-10 21:32:43 -04008437 uint32_t mbox_tmo;
James Smartfedd3b72011-02-16 12:39:24 -05008438 int length;
8439 struct lpfc_sli4_parameters *mbx_sli4_parameters;
8440
James Smart6d368e52011-05-24 11:44:12 -04008441 /*
8442 * By default, the driver assumes the SLI4 port requires RPI
8443 * header postings. The SLI4_PARAM response will correct this
8444 * assumption.
8445 */
8446 phba->sli4_hba.rpi_hdrs_in_use = 1;
8447
James Smartfedd3b72011-02-16 12:39:24 -05008448 /* Read the port's SLI4 Config Parameters */
8449 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
8450 sizeof(struct lpfc_sli4_cfg_mhdr));
8451 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
8452 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
8453 length, LPFC_SLI4_MBX_EMBED);
8454 if (!phba->sli4_hba.intr_enable)
8455 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -04008456 else {
8457 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
8458 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
8459 }
James Smartfedd3b72011-02-16 12:39:24 -05008460 if (unlikely(rc))
8461 return rc;
8462 sli4_params = &phba->sli4_hba.pc_sli4_params;
8463 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
8464 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
8465 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
8466 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
8467 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
8468 mbx_sli4_parameters);
8469 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
8470 mbx_sli4_parameters);
8471 if (bf_get(cfg_phwq, mbx_sli4_parameters))
8472 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
8473 else
8474 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
8475 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
8476 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
8477 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
8478 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
8479 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
8480 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
8481 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
8482 mbx_sli4_parameters);
8483 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
8484 mbx_sli4_parameters);
James Smart6d368e52011-05-24 11:44:12 -04008485 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
8486 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
James Smart05580562011-05-24 11:40:48 -04008487
8488 /* Make sure that sge_supp_len can be handled by the driver */
8489 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
8490 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
8491
James Smartfedd3b72011-02-16 12:39:24 -05008492 return 0;
8493}
8494
8495/**
James Smart3772a992009-05-22 14:50:54 -04008496 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
8497 * @pdev: pointer to PCI device
8498 * @pid: pointer to PCI device identifier
8499 *
8500 * This routine is to be called to attach a device with SLI-3 interface spec
8501 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
8502 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
8503 * information of the device and driver to see if the driver state that it can
8504 * support this kind of device. If the match is successful, the driver core
8505 * invokes this routine. If this routine determines it can claim the HBA, it
8506 * does all the initialization that it needs to do to handle the HBA properly.
8507 *
8508 * Return code
8509 * 0 - driver can claim the device
8510 * negative value - driver can not claim the device
8511 **/
8512static int __devinit
8513lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
8514{
8515 struct lpfc_hba *phba;
8516 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04008517 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -04008518 int error;
8519 uint32_t cfg_mode, intr_mode;
8520
8521 /* Allocate memory for HBA structure */
8522 phba = lpfc_hba_alloc(pdev);
8523 if (!phba)
8524 return -ENOMEM;
8525
8526 /* Perform generic PCI device enabling operation */
8527 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -04008528 if (error)
James Smart3772a992009-05-22 14:50:54 -04008529 goto out_free_phba;
James Smart3772a992009-05-22 14:50:54 -04008530
8531 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
8532 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
8533 if (error)
8534 goto out_disable_pci_dev;
8535
8536 /* Set up SLI-3 specific device PCI memory space */
8537 error = lpfc_sli_pci_mem_setup(phba);
8538 if (error) {
8539 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8540 "1402 Failed to set up pci memory space.\n");
8541 goto out_disable_pci_dev;
8542 }
8543
8544 /* Set up phase-1 common device driver resources */
8545 error = lpfc_setup_driver_resource_phase1(phba);
8546 if (error) {
8547 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8548 "1403 Failed to set up driver resource.\n");
8549 goto out_unset_pci_mem_s3;
8550 }
8551
8552 /* Set up SLI-3 specific device driver resources */
8553 error = lpfc_sli_driver_resource_setup(phba);
8554 if (error) {
8555 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8556 "1404 Failed to set up driver resource.\n");
8557 goto out_unset_pci_mem_s3;
8558 }
8559
8560 /* Initialize and populate the iocb list per host */
8561 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
8562 if (error) {
8563 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8564 "1405 Failed to initialize iocb list.\n");
8565 goto out_unset_driver_resource_s3;
8566 }
8567
8568 /* Set up common device driver resources */
8569 error = lpfc_setup_driver_resource_phase2(phba);
8570 if (error) {
8571 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8572 "1406 Failed to set up driver resource.\n");
8573 goto out_free_iocb_list;
8574 }
8575
James Smart079b5c92011-08-21 21:48:49 -04008576 /* Get the default values for Model Name and Description */
8577 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
8578
James Smart3772a992009-05-22 14:50:54 -04008579 /* Create SCSI host to the physical port */
8580 error = lpfc_create_shost(phba);
8581 if (error) {
8582 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8583 "1407 Failed to create scsi host.\n");
8584 goto out_unset_driver_resource;
8585 }
8586
8587 /* Configure sysfs attributes */
8588 vport = phba->pport;
8589 error = lpfc_alloc_sysfs_attr(vport);
8590 if (error) {
8591 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8592 "1476 Failed to allocate sysfs attr\n");
8593 goto out_destroy_shost;
8594 }
8595
James Smart6669f9b2009-10-02 15:16:45 -04008596 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -04008597 /* Now, trying to enable interrupt and bring up the device */
8598 cfg_mode = phba->cfg_use_msi;
8599 while (true) {
8600 /* Put device to a known state before enabling interrupt */
8601 lpfc_stop_port(phba);
8602 /* Configure and enable interrupt */
8603 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
8604 if (intr_mode == LPFC_INTR_ERROR) {
8605 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8606 "0431 Failed to enable interrupt.\n");
8607 error = -ENODEV;
8608 goto out_free_sysfs_attr;
8609 }
8610 /* SLI-3 HBA setup */
8611 if (lpfc_sli_hba_setup(phba)) {
8612 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8613 "1477 Failed to set up hba\n");
8614 error = -ENODEV;
8615 goto out_remove_device;
8616 }
8617
8618 /* Wait 50ms for the interrupts of previous mailbox commands */
8619 msleep(50);
8620 /* Check active interrupts on message signaled interrupts */
8621 if (intr_mode == 0 ||
8622 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
8623 /* Log the current active interrupt mode */
8624 phba->intr_mode = intr_mode;
8625 lpfc_log_intr_mode(phba, intr_mode);
8626 break;
8627 } else {
8628 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8629 "0447 Configure interrupt mode (%d) "
8630 "failed active interrupt test.\n",
8631 intr_mode);
8632 /* Disable the current interrupt mode */
8633 lpfc_sli_disable_intr(phba);
8634 /* Try next level of interrupt mode */
8635 cfg_mode = --intr_mode;
8636 }
8637 }
8638
8639 /* Perform post initialization setup */
8640 lpfc_post_init_setup(phba);
8641
8642 /* Check if there are static vports to be created. */
8643 lpfc_create_static_vport(phba);
8644
8645 return 0;
8646
8647out_remove_device:
8648 lpfc_unset_hba(phba);
8649out_free_sysfs_attr:
8650 lpfc_free_sysfs_attr(vport);
8651out_destroy_shost:
8652 lpfc_destroy_shost(phba);
8653out_unset_driver_resource:
8654 lpfc_unset_driver_resource_phase2(phba);
8655out_free_iocb_list:
8656 lpfc_free_iocb_list(phba);
8657out_unset_driver_resource_s3:
8658 lpfc_sli_driver_resource_unset(phba);
8659out_unset_pci_mem_s3:
8660 lpfc_sli_pci_mem_unset(phba);
8661out_disable_pci_dev:
8662 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04008663 if (shost)
8664 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -04008665out_free_phba:
8666 lpfc_hba_free(phba);
8667 return error;
8668}
8669
8670/**
8671 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -04008672 * @pdev: pointer to PCI device
8673 *
James Smart3772a992009-05-22 14:50:54 -04008674 * This routine is to be called to disattach a device with SLI-3 interface
8675 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
8676 * removed from PCI bus, it performs all the necessary cleanup for the HBA
8677 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -04008678 **/
dea31012005-04-17 16:05:31 -05008679static void __devexit
James Smart3772a992009-05-22 14:50:54 -04008680lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -05008681{
James Smart2e0fef82007-06-17 19:56:36 -05008682 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8683 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -05008684 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -05008685 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -05008686 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05008687 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
8688
James Smart549e55c2007-08-02 11:09:51 -04008689 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04008690 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04008691 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008692
James Smart858c9f62007-06-17 19:56:39 -05008693 lpfc_free_sysfs_attr(vport);
8694
James Smarteada2722008-12-04 22:39:13 -05008695 /* Release all the vports against this physical port */
8696 vports = lpfc_create_vport_work_array(phba);
8697 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -04008698 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
James Smarteada2722008-12-04 22:39:13 -05008699 fc_vport_terminate(vports[i]->fc_vport);
8700 lpfc_destroy_vport_work_array(phba, vports);
8701
8702 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -05008703 fc_remove_host(shost);
8704 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04008705 lpfc_cleanup(vport);
8706
James Smart2e0fef82007-06-17 19:56:36 -05008707 /*
8708 * Bring down the SLI Layer. This step disable all interrupts,
8709 * clears the rings, discards all mailbox commands, and resets
8710 * the HBA.
8711 */
James Smarta257bf92009-04-06 18:48:10 -04008712
Justin P. Mattock48e34d02010-12-30 15:07:58 -08008713 /* HBA interrupt will be disabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -05008714 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -04008715 /* Stop kthread signal shall trigger work_done one more time */
8716 kthread_stop(phba->worker_thread);
8717 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -05008718 lpfc_sli_brdrestart(phba);
8719
James Smart72859902012-01-18 16:25:38 -05008720 kfree(phba->vpi_bmask);
8721 kfree(phba->vpi_ids);
8722
James Smart3772a992009-05-22 14:50:54 -04008723 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05008724 spin_lock_irq(&phba->hbalock);
8725 list_del_init(&vport->listentry);
8726 spin_unlock_irq(&phba->hbalock);
8727
James Smart858c9f62007-06-17 19:56:39 -05008728 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05008729
James Smart912e3ac2011-05-24 11:42:11 -04008730 /* Disable SR-IOV if enabled */
8731 if (phba->cfg_sriov_nr_virtfn)
8732 pci_disable_sriov(pdev);
8733
James Smart5b75da22008-12-04 22:39:35 -05008734 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04008735 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -05008736
8737 pci_set_drvdata(pdev, NULL);
James Smart858c9f62007-06-17 19:56:39 -05008738 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05008739
8740 /*
8741 * Call scsi_free before mem_free since scsi bufs are released to their
8742 * corresponding pools here.
8743 */
8744 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -04008745 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -05008746
James Smart34b02dc2008-08-24 21:49:55 -04008747 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
8748 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -05008749
James Smart2e0fef82007-06-17 19:56:36 -05008750 /* Free resources associated with SLI2 interface */
8751 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -04008752 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -05008753
8754 /* unmap adapter SLIM and Control Registers */
8755 iounmap(phba->ctrl_regs_memmap_p);
8756 iounmap(phba->slim_memmap_p);
8757
James Smart3772a992009-05-22 14:50:54 -04008758 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -05008759
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05008760 pci_release_selected_regions(pdev, bars);
James Smart2e0fef82007-06-17 19:56:36 -05008761 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05008762}
8763
Linas Vepstas8d63f372007-02-14 14:28:36 -06008764/**
James Smart3772a992009-05-22 14:50:54 -04008765 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05008766 * @pdev: pointer to PCI device
8767 * @msg: power management message
8768 *
James Smart3772a992009-05-22 14:50:54 -04008769 * This routine is to be called from the kernel's PCI subsystem to support
8770 * system Power Management (PM) to device with SLI-3 interface spec. When
8771 * PM invokes this method, it quiesces the device by stopping the driver's
8772 * worker thread for the device, turning off device's interrupt and DMA,
8773 * and bring the device offline. Note that as the driver implements the
8774 * minimum PM requirements to a power-aware driver's PM support for the
8775 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
8776 * to the suspend() method call will be treated as SUSPEND and the driver will
8777 * fully reinitialize its device during resume() method call, the driver will
8778 * set device to PCI_D3hot state in PCI config space instead of setting it
8779 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -05008780 *
8781 * Return code
James Smart3772a992009-05-22 14:50:54 -04008782 * 0 - driver suspended the device
8783 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05008784 **/
8785static int
James Smart3772a992009-05-22 14:50:54 -04008786lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -05008787{
8788 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8789 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8790
8791 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8792 "0473 PCI device Power Management suspend.\n");
8793
8794 /* Bring down the device */
8795 lpfc_offline_prep(phba);
8796 lpfc_offline(phba);
8797 kthread_stop(phba->worker_thread);
8798
8799 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -04008800 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -05008801
8802 /* Save device state to PCI config space */
8803 pci_save_state(pdev);
8804 pci_set_power_state(pdev, PCI_D3hot);
8805
8806 return 0;
8807}
8808
8809/**
James Smart3772a992009-05-22 14:50:54 -04008810 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05008811 * @pdev: pointer to PCI device
8812 *
James Smart3772a992009-05-22 14:50:54 -04008813 * This routine is to be called from the kernel's PCI subsystem to support
8814 * system Power Management (PM) to device with SLI-3 interface spec. When PM
8815 * invokes this method, it restores the device's PCI config space state and
8816 * fully reinitializes the device and brings it online. Note that as the
8817 * driver implements the minimum PM requirements to a power-aware driver's
8818 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
8819 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
8820 * driver will fully reinitialize its device during resume() method call,
8821 * the device will be set to PCI_D0 directly in PCI config space before
8822 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -05008823 *
8824 * Return code
James Smart3772a992009-05-22 14:50:54 -04008825 * 0 - driver suspended the device
8826 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05008827 **/
8828static int
James Smart3772a992009-05-22 14:50:54 -04008829lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -05008830{
8831 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8832 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -05008833 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05008834 int error;
8835
8836 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8837 "0452 PCI device Power Management resume.\n");
8838
8839 /* Restore device state from PCI config space */
8840 pci_set_power_state(pdev, PCI_D0);
8841 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -04008842
James Smart1dfb5a42010-02-12 14:40:50 -05008843 /*
8844 * As the new kernel behavior of pci_restore_state() API call clears
8845 * device saved_state flag, need to save the restored state again.
8846 */
8847 pci_save_state(pdev);
8848
James Smart3a55b532008-12-04 22:38:54 -05008849 if (pdev->is_busmaster)
8850 pci_set_master(pdev);
8851
8852 /* Startup the kernel thread for this host adapter. */
8853 phba->worker_thread = kthread_run(lpfc_do_work, phba,
8854 "lpfc_worker_%d", phba->brd_no);
8855 if (IS_ERR(phba->worker_thread)) {
8856 error = PTR_ERR(phba->worker_thread);
8857 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8858 "0434 PM resume failed to start worker "
8859 "thread: error=x%x.\n", error);
8860 return error;
8861 }
8862
James Smart5b75da22008-12-04 22:39:35 -05008863 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04008864 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05008865 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -05008866 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -05008867 "0430 PM resume Failed to enable interrupt\n");
8868 return -EIO;
8869 } else
8870 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05008871
8872 /* Restart HBA and bring it online */
8873 lpfc_sli_brdrestart(phba);
8874 lpfc_online(phba);
8875
James Smart5b75da22008-12-04 22:39:35 -05008876 /* Log the current active interrupt mode */
8877 lpfc_log_intr_mode(phba, phba->intr_mode);
8878
James Smart3a55b532008-12-04 22:38:54 -05008879 return 0;
8880}
8881
8882/**
James Smart891478a2009-11-18 15:40:23 -05008883 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
8884 * @phba: pointer to lpfc hba data structure.
8885 *
8886 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -04008887 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -05008888 **/
8889static void
8890lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
8891{
James Smarte2af0d22010-03-15 11:25:32 -04008892 struct lpfc_sli *psli = &phba->sli;
8893 struct lpfc_sli_ring *pring;
8894
James Smart891478a2009-11-18 15:40:23 -05008895 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8896 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -04008897
8898 /*
8899 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
8900 * and let the SCSI mid-layer to retry them to recover.
8901 */
8902 pring = &psli->ring[psli->fcp_ring];
8903 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart891478a2009-11-18 15:40:23 -05008904}
8905
8906/**
James Smart0d878412009-10-02 15:16:56 -04008907 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
8908 * @phba: pointer to lpfc hba data structure.
8909 *
8910 * This routine is called to prepare the SLI3 device for PCI slot reset. It
8911 * disables the device interrupt and pci device, and aborts the internal FCP
8912 * pending I/Os.
8913 **/
8914static void
8915lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
8916{
James Smart0d878412009-10-02 15:16:56 -04008917 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05008918 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -04008919
James Smart75baf692010-06-08 18:31:21 -04008920 /* Block any management I/Os to the device */
8921 lpfc_block_mgmt_io(phba);
8922
James Smarte2af0d22010-03-15 11:25:32 -04008923 /* Block all SCSI devices' I/Os on the host */
8924 lpfc_scsi_dev_block(phba);
8925
8926 /* stop all timers */
8927 lpfc_stop_hba_timers(phba);
8928
James Smart0d878412009-10-02 15:16:56 -04008929 /* Disable interrupt and pci device */
8930 lpfc_sli_disable_intr(phba);
8931 pci_disable_device(phba->pcidev);
James Smart75baf692010-06-08 18:31:21 -04008932
James Smarte2af0d22010-03-15 11:25:32 -04008933 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
8934 lpfc_sli_flush_fcp_rings(phba);
James Smart0d878412009-10-02 15:16:56 -04008935}
8936
8937/**
8938 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
8939 * @phba: pointer to lpfc hba data structure.
8940 *
8941 * This routine is called to prepare the SLI3 device for PCI slot permanently
8942 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
8943 * pending I/Os.
8944 **/
8945static void
James Smart75baf692010-06-08 18:31:21 -04008946lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
James Smart0d878412009-10-02 15:16:56 -04008947{
8948 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05008949 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -04008950 /* Block all SCSI devices' I/Os on the host */
8951 lpfc_scsi_dev_block(phba);
8952
8953 /* stop all timers */
8954 lpfc_stop_hba_timers(phba);
8955
James Smart0d878412009-10-02 15:16:56 -04008956 /* Clean up all driver's outstanding SCSI I/Os */
8957 lpfc_sli_flush_fcp_rings(phba);
8958}
8959
8960/**
James Smart3772a992009-05-22 14:50:54 -04008961 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -04008962 * @pdev: pointer to PCI device.
8963 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -06008964 *
James Smart3772a992009-05-22 14:50:54 -04008965 * This routine is called from the PCI subsystem for I/O error handling to
8966 * device with SLI-3 interface spec. This function is called by the PCI
8967 * subsystem after a PCI bus error affecting this device has been detected.
8968 * When this function is invoked, it will need to stop all the I/Os and
8969 * interrupt(s) to the device. Once that is done, it will return
8970 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
8971 * as desired.
James Smarte59058c2008-08-24 21:49:00 -04008972 *
8973 * Return codes
James Smart0d878412009-10-02 15:16:56 -04008974 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -04008975 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
8976 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -04008977 **/
James Smart3772a992009-05-22 14:50:54 -04008978static pci_ers_result_t
8979lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -06008980{
James Smart51ef4c22007-08-02 11:10:31 -04008981 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8982 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06008983
James Smart0d878412009-10-02 15:16:56 -04008984 switch (state) {
8985 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -05008986 /* Non-fatal error, prepare for recovery */
8987 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -04008988 return PCI_ERS_RESULT_CAN_RECOVER;
8989 case pci_channel_io_frozen:
8990 /* Fatal error, prepare for slot reset */
8991 lpfc_sli_prep_dev_for_reset(phba);
8992 return PCI_ERS_RESULT_NEED_RESET;
8993 case pci_channel_io_perm_failure:
8994 /* Permanent failure, prepare for device down */
James Smart75baf692010-06-08 18:31:21 -04008995 lpfc_sli_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06008996 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -04008997 default:
8998 /* Unknown state, prepare and request slot reset */
8999 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9000 "0472 Unknown PCI error state: x%x\n", state);
9001 lpfc_sli_prep_dev_for_reset(phba);
9002 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -04009003 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06009004}
9005
9006/**
James Smart3772a992009-05-22 14:50:54 -04009007 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -04009008 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009009 *
James Smart3772a992009-05-22 14:50:54 -04009010 * This routine is called from the PCI subsystem for error handling to
9011 * device with SLI-3 interface spec. This is called after PCI bus has been
9012 * reset to restart the PCI card from scratch, as if from a cold-boot.
9013 * During the PCI subsystem error recovery, after driver returns
9014 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
9015 * recovery and then call this routine before calling the .resume method
9016 * to recover the device. This function will initialize the HBA device,
9017 * enable the interrupt, but it will just put the HBA to offline state
9018 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -04009019 *
9020 * Return codes
James Smart3772a992009-05-22 14:50:54 -04009021 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
9022 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -06009023 */
James Smart3772a992009-05-22 14:50:54 -04009024static pci_ers_result_t
9025lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009026{
James Smart51ef4c22007-08-02 11:10:31 -04009027 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9028 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009029 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -05009030 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009031
9032 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +11009033 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -06009034 printk(KERN_ERR "lpfc: Cannot re-enable "
9035 "PCI device after reset.\n");
9036 return PCI_ERS_RESULT_DISCONNECT;
9037 }
9038
James Smart97207482008-12-04 22:39:19 -05009039 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05009040
9041 /*
9042 * As the new kernel behavior of pci_restore_state() API call clears
9043 * device saved_state flag, need to save the restored state again.
9044 */
9045 pci_save_state(pdev);
9046
James Smart97207482008-12-04 22:39:19 -05009047 if (pdev->is_busmaster)
9048 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009049
James Smart92d7f7b2007-06-17 19:56:38 -05009050 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04009051 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05009052 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009053
James Smart5b75da22008-12-04 22:39:35 -05009054 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009055 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05009056 if (intr_mode == LPFC_INTR_ERROR) {
9057 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9058 "0427 Cannot re-enable interrupt after "
9059 "slot reset.\n");
9060 return PCI_ERS_RESULT_DISCONNECT;
9061 } else
9062 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009063
James Smart75baf692010-06-08 18:31:21 -04009064 /* Take device offline, it will perform cleanup */
9065 lpfc_offline_prep(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009066 lpfc_offline(phba);
9067 lpfc_sli_brdrestart(phba);
9068
James Smart5b75da22008-12-04 22:39:35 -05009069 /* Log the current active interrupt mode */
9070 lpfc_log_intr_mode(phba, phba->intr_mode);
9071
Linas Vepstas8d63f372007-02-14 14:28:36 -06009072 return PCI_ERS_RESULT_RECOVERED;
9073}
9074
9075/**
James Smart3772a992009-05-22 14:50:54 -04009076 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -04009077 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -06009078 *
James Smart3772a992009-05-22 14:50:54 -04009079 * This routine is called from the PCI subsystem for error handling to device
9080 * with SLI-3 interface spec. It is called when kernel error recovery tells
9081 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
9082 * error recovery. After this call, traffic can start to flow from this device
9083 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009084 */
James Smart3772a992009-05-22 14:50:54 -04009085static void
9086lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009087{
James Smart51ef4c22007-08-02 11:10:31 -04009088 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9089 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009090
James Smarte2af0d22010-03-15 11:25:32 -04009091 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -04009092 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -04009093
9094 /* Clean up Advanced Error Reporting (AER) if needed */
9095 if (phba->hba_flag & HBA_AER_ENABLED)
9096 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009097}
9098
James Smart3772a992009-05-22 14:50:54 -04009099/**
James Smartda0436e2009-05-22 14:51:39 -04009100 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
9101 * @phba: pointer to lpfc hba data structure.
9102 *
9103 * returns the number of ELS/CT IOCBs to reserve
9104 **/
9105int
9106lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
9107{
9108 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
9109
James Smartf1126682009-06-10 17:22:44 -04009110 if (phba->sli_rev == LPFC_SLI_REV4) {
9111 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -04009112 return 10;
James Smartf1126682009-06-10 17:22:44 -04009113 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -04009114 return 25;
James Smartf1126682009-06-10 17:22:44 -04009115 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -04009116 return 50;
James Smartf1126682009-06-10 17:22:44 -04009117 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -04009118 return 100;
James Smartf1126682009-06-10 17:22:44 -04009119 else
James Smart6a9c52c2009-10-02 15:16:51 -04009120 return 150;
James Smartf1126682009-06-10 17:22:44 -04009121 } else
9122 return 0;
James Smartda0436e2009-05-22 14:51:39 -04009123}
9124
9125/**
James Smart52d52442011-05-24 11:42:45 -04009126 * lpfc_write_firmware - attempt to write a firmware image to the port
9127 * @phba: pointer to lpfc hba data structure.
9128 * @fw: pointer to firmware image returned from request_firmware.
9129 *
9130 * returns the number of bytes written if write is successful.
9131 * returns a negative error value if there were errors.
9132 * returns 0 if firmware matches currently active firmware on port.
9133 **/
9134int
9135lpfc_write_firmware(struct lpfc_hba *phba, const struct firmware *fw)
9136{
James Smart6b5151f2012-01-18 16:24:06 -05009137 char fwrev[FW_REV_STR_SIZE];
James Smart52d52442011-05-24 11:42:45 -04009138 struct lpfc_grp_hdr *image = (struct lpfc_grp_hdr *)fw->data;
9139 struct list_head dma_buffer_list;
9140 int i, rc = 0;
9141 struct lpfc_dmabuf *dmabuf, *next;
9142 uint32_t offset = 0, temp_offset = 0;
9143
9144 INIT_LIST_HEAD(&dma_buffer_list);
James Smart079b5c92011-08-21 21:48:49 -04009145 if ((be32_to_cpu(image->magic_number) != LPFC_GROUP_OJECT_MAGIC_NUM) ||
9146 (bf_get_be32(lpfc_grp_hdr_file_type, image) !=
9147 LPFC_FILE_TYPE_GROUP) ||
9148 (bf_get_be32(lpfc_grp_hdr_id, image) != LPFC_FILE_ID_GROUP) ||
9149 (be32_to_cpu(image->size) != fw->size)) {
James Smart52d52442011-05-24 11:42:45 -04009150 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9151 "3022 Invalid FW image found. "
James Smart079b5c92011-08-21 21:48:49 -04009152 "Magic:%x Type:%x ID:%x\n",
9153 be32_to_cpu(image->magic_number),
9154 bf_get_be32(lpfc_grp_hdr_file_type, image),
9155 bf_get_be32(lpfc_grp_hdr_id, image));
James Smart52d52442011-05-24 11:42:45 -04009156 return -EINVAL;
9157 }
9158 lpfc_decode_firmware_rev(phba, fwrev, 1);
James Smart88a2cfb2011-07-22 18:36:33 -04009159 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
James Smart52d52442011-05-24 11:42:45 -04009160 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9161 "3023 Updating Firmware. Current Version:%s "
9162 "New Version:%s\n",
James Smart88a2cfb2011-07-22 18:36:33 -04009163 fwrev, image->revision);
James Smart52d52442011-05-24 11:42:45 -04009164 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
9165 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
9166 GFP_KERNEL);
9167 if (!dmabuf) {
9168 rc = -ENOMEM;
9169 goto out;
9170 }
9171 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
9172 SLI4_PAGE_SIZE,
9173 &dmabuf->phys,
9174 GFP_KERNEL);
9175 if (!dmabuf->virt) {
9176 kfree(dmabuf);
9177 rc = -ENOMEM;
9178 goto out;
9179 }
9180 list_add_tail(&dmabuf->list, &dma_buffer_list);
9181 }
9182 while (offset < fw->size) {
9183 temp_offset = offset;
9184 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
James Smart079b5c92011-08-21 21:48:49 -04009185 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
James Smart52d52442011-05-24 11:42:45 -04009186 memcpy(dmabuf->virt,
9187 fw->data + temp_offset,
James Smart079b5c92011-08-21 21:48:49 -04009188 fw->size - temp_offset);
9189 temp_offset = fw->size;
James Smart52d52442011-05-24 11:42:45 -04009190 break;
9191 }
James Smart52d52442011-05-24 11:42:45 -04009192 memcpy(dmabuf->virt, fw->data + temp_offset,
9193 SLI4_PAGE_SIZE);
James Smart88a2cfb2011-07-22 18:36:33 -04009194 temp_offset += SLI4_PAGE_SIZE;
James Smart52d52442011-05-24 11:42:45 -04009195 }
9196 rc = lpfc_wr_object(phba, &dma_buffer_list,
9197 (fw->size - offset), &offset);
9198 if (rc) {
9199 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9200 "3024 Firmware update failed. "
9201 "%d\n", rc);
9202 goto out;
9203 }
9204 }
9205 rc = offset;
9206 }
9207out:
9208 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
9209 list_del(&dmabuf->list);
9210 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
9211 dmabuf->virt, dmabuf->phys);
9212 kfree(dmabuf);
9213 }
9214 return rc;
9215}
9216
9217/**
James Smartda0436e2009-05-22 14:51:39 -04009218 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
9219 * @pdev: pointer to PCI device
9220 * @pid: pointer to PCI device identifier
9221 *
9222 * This routine is called from the kernel's PCI subsystem to device with
9223 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
9224 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
9225 * information of the device and driver to see if the driver state that it
9226 * can support this kind of device. If the match is successful, the driver
9227 * core invokes this routine. If this routine determines it can claim the HBA,
9228 * it does all the initialization that it needs to do to handle the HBA
9229 * properly.
9230 *
9231 * Return code
9232 * 0 - driver can claim the device
9233 * negative value - driver can not claim the device
9234 **/
9235static int __devinit
9236lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
9237{
9238 struct lpfc_hba *phba;
9239 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04009240 struct Scsi_Host *shost = NULL;
James Smartda0436e2009-05-22 14:51:39 -04009241 int error;
9242 uint32_t cfg_mode, intr_mode;
9243 int mcnt;
James Smart05580562011-05-24 11:40:48 -04009244 int adjusted_fcp_eq_count;
James Smart52d52442011-05-24 11:42:45 -04009245 const struct firmware *fw;
9246 uint8_t file_name[16];
James Smartda0436e2009-05-22 14:51:39 -04009247
9248 /* Allocate memory for HBA structure */
9249 phba = lpfc_hba_alloc(pdev);
9250 if (!phba)
9251 return -ENOMEM;
9252
9253 /* Perform generic PCI device enabling operation */
9254 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -04009255 if (error)
James Smartda0436e2009-05-22 14:51:39 -04009256 goto out_free_phba;
James Smartda0436e2009-05-22 14:51:39 -04009257
9258 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
9259 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
9260 if (error)
9261 goto out_disable_pci_dev;
9262
9263 /* Set up SLI-4 specific device PCI memory space */
9264 error = lpfc_sli4_pci_mem_setup(phba);
9265 if (error) {
9266 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9267 "1410 Failed to set up pci memory space.\n");
9268 goto out_disable_pci_dev;
9269 }
9270
9271 /* Set up phase-1 common device driver resources */
9272 error = lpfc_setup_driver_resource_phase1(phba);
9273 if (error) {
9274 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9275 "1411 Failed to set up driver resource.\n");
9276 goto out_unset_pci_mem_s4;
9277 }
9278
9279 /* Set up SLI-4 Specific device driver resources */
9280 error = lpfc_sli4_driver_resource_setup(phba);
9281 if (error) {
9282 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9283 "1412 Failed to set up driver resource.\n");
9284 goto out_unset_pci_mem_s4;
9285 }
9286
9287 /* Initialize and populate the iocb list per host */
James Smart2a9bf3d2010-06-07 15:24:45 -04009288
9289 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9290 "2821 initialize iocb list %d.\n",
9291 phba->cfg_iocb_cnt*1024);
9292 error = lpfc_init_iocb_list(phba, phba->cfg_iocb_cnt*1024);
9293
James Smartda0436e2009-05-22 14:51:39 -04009294 if (error) {
9295 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9296 "1413 Failed to initialize iocb list.\n");
9297 goto out_unset_driver_resource_s4;
9298 }
9299
James Smart19ca7602010-11-20 23:11:55 -05009300 INIT_LIST_HEAD(&phba->active_rrq_list);
James Smart7d791df2011-07-22 18:37:52 -04009301 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
James Smart19ca7602010-11-20 23:11:55 -05009302
James Smartda0436e2009-05-22 14:51:39 -04009303 /* Set up common device driver resources */
9304 error = lpfc_setup_driver_resource_phase2(phba);
9305 if (error) {
9306 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9307 "1414 Failed to set up driver resource.\n");
9308 goto out_free_iocb_list;
9309 }
9310
James Smart079b5c92011-08-21 21:48:49 -04009311 /* Get the default values for Model Name and Description */
9312 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
9313
James Smartda0436e2009-05-22 14:51:39 -04009314 /* Create SCSI host to the physical port */
9315 error = lpfc_create_shost(phba);
9316 if (error) {
9317 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9318 "1415 Failed to create scsi host.\n");
9319 goto out_unset_driver_resource;
9320 }
9321
9322 /* Configure sysfs attributes */
9323 vport = phba->pport;
9324 error = lpfc_alloc_sysfs_attr(vport);
9325 if (error) {
9326 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9327 "1416 Failed to allocate sysfs attr\n");
9328 goto out_destroy_shost;
9329 }
9330
James Smart6669f9b2009-10-02 15:16:45 -04009331 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -04009332 /* Now, trying to enable interrupt and bring up the device */
9333 cfg_mode = phba->cfg_use_msi;
9334 while (true) {
9335 /* Put device to a known state before enabling interrupt */
9336 lpfc_stop_port(phba);
9337 /* Configure and enable interrupt */
9338 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
9339 if (intr_mode == LPFC_INTR_ERROR) {
9340 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9341 "0426 Failed to enable interrupt.\n");
9342 error = -ENODEV;
9343 goto out_free_sysfs_attr;
9344 }
James Smart05580562011-05-24 11:40:48 -04009345 /* Default to single EQ for non-MSI-X */
James Smartdef9c7a2009-12-21 17:02:28 -05009346 if (phba->intr_type != MSIX)
James Smart05580562011-05-24 11:40:48 -04009347 adjusted_fcp_eq_count = 0;
9348 else if (phba->sli4_hba.msix_vec_nr <
9349 phba->cfg_fcp_eq_count + 1)
9350 adjusted_fcp_eq_count = phba->sli4_hba.msix_vec_nr - 1;
9351 else
9352 adjusted_fcp_eq_count = phba->cfg_fcp_eq_count;
James Smart05580562011-05-24 11:40:48 -04009353 phba->cfg_fcp_eq_count = adjusted_fcp_eq_count;
James Smartda0436e2009-05-22 14:51:39 -04009354 /* Set up SLI-4 HBA */
9355 if (lpfc_sli4_hba_setup(phba)) {
9356 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9357 "1421 Failed to set up hba\n");
9358 error = -ENODEV;
9359 goto out_disable_intr;
9360 }
9361
9362 /* Send NOP mbx cmds for non-INTx mode active interrupt test */
9363 if (intr_mode != 0)
9364 mcnt = lpfc_sli4_send_nop_mbox_cmds(phba,
9365 LPFC_ACT_INTR_CNT);
9366
9367 /* Check active interrupts received only for MSI/MSI-X */
9368 if (intr_mode == 0 ||
9369 phba->sli.slistat.sli_intr >= LPFC_ACT_INTR_CNT) {
9370 /* Log the current active interrupt mode */
9371 phba->intr_mode = intr_mode;
9372 lpfc_log_intr_mode(phba, intr_mode);
9373 break;
9374 }
9375 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9376 "0451 Configure interrupt mode (%d) "
9377 "failed active interrupt test.\n",
9378 intr_mode);
James Smart2fcee4b2010-12-15 17:57:46 -05009379 /* Unset the previous SLI-4 HBA setup. */
9380 /*
9381 * TODO: Is this operation compatible with IF TYPE 2
9382 * devices? All port state is deleted and cleared.
9383 */
James Smartda0436e2009-05-22 14:51:39 -04009384 lpfc_sli4_unset_hba(phba);
9385 /* Try next level of interrupt mode */
9386 cfg_mode = --intr_mode;
9387 }
9388
9389 /* Perform post initialization setup */
9390 lpfc_post_init_setup(phba);
9391
James Smart026abb82011-12-13 13:20:45 -05009392 /* check for firmware upgrade or downgrade (if_type 2 only) */
9393 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
9394 LPFC_SLI_INTF_IF_TYPE_2) {
9395 snprintf(file_name, 16, "%s.grp", phba->ModelName);
9396 error = request_firmware(&fw, file_name, &phba->pcidev->dev);
9397 if (!error) {
9398 lpfc_write_firmware(phba, fw);
9399 release_firmware(fw);
9400 }
James Smart52d52442011-05-24 11:42:45 -04009401 }
9402
James Smart1c6834a2009-07-19 10:01:26 -04009403 /* Check if there are static vports to be created. */
9404 lpfc_create_static_vport(phba);
James Smartda0436e2009-05-22 14:51:39 -04009405 return 0;
9406
9407out_disable_intr:
9408 lpfc_sli4_disable_intr(phba);
9409out_free_sysfs_attr:
9410 lpfc_free_sysfs_attr(vport);
9411out_destroy_shost:
9412 lpfc_destroy_shost(phba);
9413out_unset_driver_resource:
9414 lpfc_unset_driver_resource_phase2(phba);
9415out_free_iocb_list:
9416 lpfc_free_iocb_list(phba);
9417out_unset_driver_resource_s4:
9418 lpfc_sli4_driver_resource_unset(phba);
9419out_unset_pci_mem_s4:
9420 lpfc_sli4_pci_mem_unset(phba);
9421out_disable_pci_dev:
9422 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04009423 if (shost)
9424 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -04009425out_free_phba:
9426 lpfc_hba_free(phba);
9427 return error;
9428}
9429
9430/**
9431 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
9432 * @pdev: pointer to PCI device
9433 *
9434 * This routine is called from the kernel's PCI subsystem to device with
9435 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
9436 * removed from PCI bus, it performs all the necessary cleanup for the HBA
9437 * device to be removed from the PCI subsystem properly.
9438 **/
9439static void __devexit
9440lpfc_pci_remove_one_s4(struct pci_dev *pdev)
9441{
9442 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9443 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
9444 struct lpfc_vport **vports;
9445 struct lpfc_hba *phba = vport->phba;
9446 int i;
9447
9448 /* Mark the device unloading flag */
9449 spin_lock_irq(&phba->hbalock);
9450 vport->load_flag |= FC_UNLOADING;
9451 spin_unlock_irq(&phba->hbalock);
9452
9453 /* Free the HBA sysfs attributes */
9454 lpfc_free_sysfs_attr(vport);
9455
9456 /* Release all the vports against this physical port */
9457 vports = lpfc_create_vport_work_array(phba);
9458 if (vports != NULL)
9459 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
9460 fc_vport_terminate(vports[i]->fc_vport);
9461 lpfc_destroy_vport_work_array(phba, vports);
9462
9463 /* Remove FC host and then SCSI host with the physical port */
9464 fc_remove_host(shost);
9465 scsi_remove_host(shost);
9466
9467 /* Perform cleanup on the physical port */
9468 lpfc_cleanup(vport);
9469
9470 /*
9471 * Bring down the SLI Layer. This step disables all interrupts,
9472 * clears the rings, discards all mailbox commands, and resets
9473 * the HBA FCoE function.
9474 */
9475 lpfc_debugfs_terminate(vport);
9476 lpfc_sli4_hba_unset(phba);
9477
9478 spin_lock_irq(&phba->hbalock);
9479 list_del_init(&vport->listentry);
9480 spin_unlock_irq(&phba->hbalock);
9481
James Smart3677a3a2010-09-29 11:19:14 -04009482 /* Perform scsi free before driver resource_unset since scsi
James Smartda0436e2009-05-22 14:51:39 -04009483 * buffers are released to their corresponding pools here.
9484 */
9485 lpfc_scsi_free(phba);
9486 lpfc_sli4_driver_resource_unset(phba);
9487
9488 /* Unmap adapter Control and Doorbell registers */
9489 lpfc_sli4_pci_mem_unset(phba);
9490
9491 /* Release PCI resources and disable device's PCI function */
9492 scsi_host_put(shost);
9493 lpfc_disable_pci_dev(phba);
9494
9495 /* Finally, free the driver's device data structure */
9496 lpfc_hba_free(phba);
9497
9498 return;
9499}
9500
9501/**
9502 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
9503 * @pdev: pointer to PCI device
9504 * @msg: power management message
9505 *
9506 * This routine is called from the kernel's PCI subsystem to support system
9507 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
9508 * this method, it quiesces the device by stopping the driver's worker
9509 * thread for the device, turning off device's interrupt and DMA, and bring
9510 * the device offline. Note that as the driver implements the minimum PM
9511 * requirements to a power-aware driver's PM support for suspend/resume -- all
9512 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
9513 * method call will be treated as SUSPEND and the driver will fully
9514 * reinitialize its device during resume() method call, the driver will set
9515 * device to PCI_D3hot state in PCI config space instead of setting it
9516 * according to the @msg provided by the PM.
9517 *
9518 * Return code
9519 * 0 - driver suspended the device
9520 * Error otherwise
9521 **/
9522static int
9523lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
9524{
9525 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9526 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9527
9528 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart75baf692010-06-08 18:31:21 -04009529 "2843 PCI device Power Management suspend.\n");
James Smartda0436e2009-05-22 14:51:39 -04009530
9531 /* Bring down the device */
9532 lpfc_offline_prep(phba);
9533 lpfc_offline(phba);
9534 kthread_stop(phba->worker_thread);
9535
9536 /* Disable interrupt from device */
9537 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -04009538 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04009539
9540 /* Save device state to PCI config space */
9541 pci_save_state(pdev);
9542 pci_set_power_state(pdev, PCI_D3hot);
9543
9544 return 0;
9545}
9546
9547/**
9548 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
9549 * @pdev: pointer to PCI device
9550 *
9551 * This routine is called from the kernel's PCI subsystem to support system
9552 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
9553 * this method, it restores the device's PCI config space state and fully
9554 * reinitializes the device and brings it online. Note that as the driver
9555 * implements the minimum PM requirements to a power-aware driver's PM for
9556 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
9557 * to the suspend() method call will be treated as SUSPEND and the driver
9558 * will fully reinitialize its device during resume() method call, the device
9559 * will be set to PCI_D0 directly in PCI config space before restoring the
9560 * state.
9561 *
9562 * Return code
9563 * 0 - driver suspended the device
9564 * Error otherwise
9565 **/
9566static int
9567lpfc_pci_resume_one_s4(struct pci_dev *pdev)
9568{
9569 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9570 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9571 uint32_t intr_mode;
9572 int error;
9573
9574 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9575 "0292 PCI device Power Management resume.\n");
9576
9577 /* Restore device state from PCI config space */
9578 pci_set_power_state(pdev, PCI_D0);
9579 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05009580
9581 /*
9582 * As the new kernel behavior of pci_restore_state() API call clears
9583 * device saved_state flag, need to save the restored state again.
9584 */
9585 pci_save_state(pdev);
9586
James Smartda0436e2009-05-22 14:51:39 -04009587 if (pdev->is_busmaster)
9588 pci_set_master(pdev);
9589
9590 /* Startup the kernel thread for this host adapter. */
9591 phba->worker_thread = kthread_run(lpfc_do_work, phba,
9592 "lpfc_worker_%d", phba->brd_no);
9593 if (IS_ERR(phba->worker_thread)) {
9594 error = PTR_ERR(phba->worker_thread);
9595 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9596 "0293 PM resume failed to start worker "
9597 "thread: error=x%x.\n", error);
9598 return error;
9599 }
9600
9601 /* Configure and enable interrupt */
9602 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
9603 if (intr_mode == LPFC_INTR_ERROR) {
9604 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9605 "0294 PM resume Failed to enable interrupt\n");
9606 return -EIO;
9607 } else
9608 phba->intr_mode = intr_mode;
9609
9610 /* Restart HBA and bring it online */
9611 lpfc_sli_brdrestart(phba);
9612 lpfc_online(phba);
9613
9614 /* Log the current active interrupt mode */
9615 lpfc_log_intr_mode(phba, phba->intr_mode);
9616
9617 return 0;
9618}
9619
9620/**
James Smart75baf692010-06-08 18:31:21 -04009621 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
9622 * @phba: pointer to lpfc hba data structure.
9623 *
9624 * This routine is called to prepare the SLI4 device for PCI slot recover. It
9625 * aborts all the outstanding SCSI I/Os to the pci device.
9626 **/
9627static void
9628lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
9629{
9630 struct lpfc_sli *psli = &phba->sli;
9631 struct lpfc_sli_ring *pring;
9632
9633 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9634 "2828 PCI channel I/O abort preparing for recovery\n");
9635 /*
9636 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
9637 * and let the SCSI mid-layer to retry them to recover.
9638 */
9639 pring = &psli->ring[psli->fcp_ring];
9640 lpfc_sli_abort_iocb_ring(phba, pring);
9641}
9642
9643/**
9644 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
9645 * @phba: pointer to lpfc hba data structure.
9646 *
9647 * This routine is called to prepare the SLI4 device for PCI slot reset. It
9648 * disables the device interrupt and pci device, and aborts the internal FCP
9649 * pending I/Os.
9650 **/
9651static void
9652lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
9653{
9654 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9655 "2826 PCI channel disable preparing for reset\n");
9656
9657 /* Block any management I/Os to the device */
9658 lpfc_block_mgmt_io(phba);
9659
9660 /* Block all SCSI devices' I/Os on the host */
9661 lpfc_scsi_dev_block(phba);
9662
9663 /* stop all timers */
9664 lpfc_stop_hba_timers(phba);
9665
9666 /* Disable interrupt and pci device */
9667 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -04009668 lpfc_sli4_queue_destroy(phba);
James Smart75baf692010-06-08 18:31:21 -04009669 pci_disable_device(phba->pcidev);
9670
9671 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
9672 lpfc_sli_flush_fcp_rings(phba);
9673}
9674
9675/**
9676 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
9677 * @phba: pointer to lpfc hba data structure.
9678 *
9679 * This routine is called to prepare the SLI4 device for PCI slot permanently
9680 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
9681 * pending I/Os.
9682 **/
9683static void
9684lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
9685{
9686 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9687 "2827 PCI channel permanent disable for failure\n");
9688
9689 /* Block all SCSI devices' I/Os on the host */
9690 lpfc_scsi_dev_block(phba);
9691
9692 /* stop all timers */
9693 lpfc_stop_hba_timers(phba);
9694
9695 /* Clean up all driver's outstanding SCSI I/Os */
9696 lpfc_sli_flush_fcp_rings(phba);
9697}
9698
9699/**
James Smartda0436e2009-05-22 14:51:39 -04009700 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
9701 * @pdev: pointer to PCI device.
9702 * @state: the current PCI connection state.
9703 *
9704 * This routine is called from the PCI subsystem for error handling to device
9705 * with SLI-4 interface spec. This function is called by the PCI subsystem
9706 * after a PCI bus error affecting this device has been detected. When this
9707 * function is invoked, it will need to stop all the I/Os and interrupt(s)
9708 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
9709 * for the PCI subsystem to perform proper recovery as desired.
9710 *
9711 * Return codes
9712 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
9713 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
9714 **/
9715static pci_ers_result_t
9716lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
9717{
James Smart75baf692010-06-08 18:31:21 -04009718 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9719 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9720
9721 switch (state) {
9722 case pci_channel_io_normal:
9723 /* Non-fatal error, prepare for recovery */
9724 lpfc_sli4_prep_dev_for_recover(phba);
9725 return PCI_ERS_RESULT_CAN_RECOVER;
9726 case pci_channel_io_frozen:
9727 /* Fatal error, prepare for slot reset */
9728 lpfc_sli4_prep_dev_for_reset(phba);
9729 return PCI_ERS_RESULT_NEED_RESET;
9730 case pci_channel_io_perm_failure:
9731 /* Permanent failure, prepare for device down */
9732 lpfc_sli4_prep_dev_for_perm_failure(phba);
9733 return PCI_ERS_RESULT_DISCONNECT;
9734 default:
9735 /* Unknown state, prepare and request slot reset */
9736 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9737 "2825 Unknown PCI error state: x%x\n", state);
9738 lpfc_sli4_prep_dev_for_reset(phba);
9739 return PCI_ERS_RESULT_NEED_RESET;
9740 }
James Smartda0436e2009-05-22 14:51:39 -04009741}
9742
9743/**
9744 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
9745 * @pdev: pointer to PCI device.
9746 *
9747 * This routine is called from the PCI subsystem for error handling to device
9748 * with SLI-4 interface spec. It is called after PCI bus has been reset to
9749 * restart the PCI card from scratch, as if from a cold-boot. During the
9750 * PCI subsystem error recovery, after the driver returns
9751 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
9752 * recovery and then call this routine before calling the .resume method to
9753 * recover the device. This function will initialize the HBA device, enable
9754 * the interrupt, but it will just put the HBA to offline state without
9755 * passing any I/O traffic.
9756 *
9757 * Return codes
9758 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
9759 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
9760 */
9761static pci_ers_result_t
9762lpfc_io_slot_reset_s4(struct pci_dev *pdev)
9763{
James Smart75baf692010-06-08 18:31:21 -04009764 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9765 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9766 struct lpfc_sli *psli = &phba->sli;
9767 uint32_t intr_mode;
9768
9769 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
9770 if (pci_enable_device_mem(pdev)) {
9771 printk(KERN_ERR "lpfc: Cannot re-enable "
9772 "PCI device after reset.\n");
9773 return PCI_ERS_RESULT_DISCONNECT;
9774 }
9775
9776 pci_restore_state(pdev);
James Smart0a96e972011-07-22 18:37:28 -04009777
9778 /*
9779 * As the new kernel behavior of pci_restore_state() API call clears
9780 * device saved_state flag, need to save the restored state again.
9781 */
9782 pci_save_state(pdev);
9783
James Smart75baf692010-06-08 18:31:21 -04009784 if (pdev->is_busmaster)
9785 pci_set_master(pdev);
9786
9787 spin_lock_irq(&phba->hbalock);
9788 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
9789 spin_unlock_irq(&phba->hbalock);
9790
9791 /* Configure and enable interrupt */
9792 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
9793 if (intr_mode == LPFC_INTR_ERROR) {
9794 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9795 "2824 Cannot re-enable interrupt after "
9796 "slot reset.\n");
9797 return PCI_ERS_RESULT_DISCONNECT;
9798 } else
9799 phba->intr_mode = intr_mode;
9800
9801 /* Log the current active interrupt mode */
9802 lpfc_log_intr_mode(phba, phba->intr_mode);
9803
James Smartda0436e2009-05-22 14:51:39 -04009804 return PCI_ERS_RESULT_RECOVERED;
9805}
9806
9807/**
9808 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
9809 * @pdev: pointer to PCI device
9810 *
9811 * This routine is called from the PCI subsystem for error handling to device
9812 * with SLI-4 interface spec. It is called when kernel error recovery tells
9813 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
9814 * error recovery. After this call, traffic can start to flow from this device
9815 * again.
9816 **/
9817static void
9818lpfc_io_resume_s4(struct pci_dev *pdev)
9819{
James Smart75baf692010-06-08 18:31:21 -04009820 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9821 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9822
9823 /*
9824 * In case of slot reset, as function reset is performed through
9825 * mailbox command which needs DMA to be enabled, this operation
9826 * has to be moved to the io resume phase. Taking device offline
9827 * will perform the necessary cleanup.
9828 */
9829 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
9830 /* Perform device reset */
9831 lpfc_offline_prep(phba);
9832 lpfc_offline(phba);
9833 lpfc_sli_brdrestart(phba);
9834 /* Bring the device back online */
9835 lpfc_online(phba);
9836 }
9837
9838 /* Clean up Advanced Error Reporting (AER) if needed */
9839 if (phba->hba_flag & HBA_AER_ENABLED)
9840 pci_cleanup_aer_uncorrect_error_status(pdev);
James Smartda0436e2009-05-22 14:51:39 -04009841}
9842
9843/**
James Smart3772a992009-05-22 14:50:54 -04009844 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
9845 * @pdev: pointer to PCI device
9846 * @pid: pointer to PCI device identifier
9847 *
9848 * This routine is to be registered to the kernel's PCI subsystem. When an
9849 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
9850 * at PCI device-specific information of the device and driver to see if the
9851 * driver state that it can support this kind of device. If the match is
9852 * successful, the driver core invokes this routine. This routine dispatches
9853 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
9854 * do all the initialization that it needs to do to handle the HBA device
9855 * properly.
9856 *
9857 * Return code
9858 * 0 - driver can claim the device
9859 * negative value - driver can not claim the device
9860 **/
9861static int __devinit
9862lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
9863{
9864 int rc;
James Smart8fa38512009-07-19 10:01:03 -04009865 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -04009866
James Smart28baac72010-02-12 14:42:03 -05009867 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -04009868 return -ENODEV;
9869
James Smart8fa38512009-07-19 10:01:03 -04009870 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -05009871 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -04009872 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04009873 else
James Smart3772a992009-05-22 14:50:54 -04009874 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04009875
James Smart3772a992009-05-22 14:50:54 -04009876 return rc;
9877}
9878
9879/**
9880 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
9881 * @pdev: pointer to PCI device
9882 *
9883 * This routine is to be registered to the kernel's PCI subsystem. When an
9884 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
9885 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
9886 * remove routine, which will perform all the necessary cleanup for the
9887 * device to be removed from the PCI subsystem properly.
9888 **/
9889static void __devexit
9890lpfc_pci_remove_one(struct pci_dev *pdev)
9891{
9892 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9893 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9894
9895 switch (phba->pci_dev_grp) {
9896 case LPFC_PCI_DEV_LP:
9897 lpfc_pci_remove_one_s3(pdev);
9898 break;
James Smartda0436e2009-05-22 14:51:39 -04009899 case LPFC_PCI_DEV_OC:
9900 lpfc_pci_remove_one_s4(pdev);
9901 break;
James Smart3772a992009-05-22 14:50:54 -04009902 default:
9903 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9904 "1424 Invalid PCI device group: 0x%x\n",
9905 phba->pci_dev_grp);
9906 break;
9907 }
9908 return;
9909}
9910
9911/**
9912 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
9913 * @pdev: pointer to PCI device
9914 * @msg: power management message
9915 *
9916 * This routine is to be registered to the kernel's PCI subsystem to support
9917 * system Power Management (PM). When PM invokes this method, it dispatches
9918 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
9919 * suspend the device.
9920 *
9921 * Return code
9922 * 0 - driver suspended the device
9923 * Error otherwise
9924 **/
9925static int
9926lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
9927{
9928 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9929 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9930 int rc = -ENODEV;
9931
9932 switch (phba->pci_dev_grp) {
9933 case LPFC_PCI_DEV_LP:
9934 rc = lpfc_pci_suspend_one_s3(pdev, msg);
9935 break;
James Smartda0436e2009-05-22 14:51:39 -04009936 case LPFC_PCI_DEV_OC:
9937 rc = lpfc_pci_suspend_one_s4(pdev, msg);
9938 break;
James Smart3772a992009-05-22 14:50:54 -04009939 default:
9940 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9941 "1425 Invalid PCI device group: 0x%x\n",
9942 phba->pci_dev_grp);
9943 break;
9944 }
9945 return rc;
9946}
9947
9948/**
9949 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
9950 * @pdev: pointer to PCI device
9951 *
9952 * This routine is to be registered to the kernel's PCI subsystem to support
9953 * system Power Management (PM). When PM invokes this method, it dispatches
9954 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
9955 * resume the device.
9956 *
9957 * Return code
9958 * 0 - driver suspended the device
9959 * Error otherwise
9960 **/
9961static int
9962lpfc_pci_resume_one(struct pci_dev *pdev)
9963{
9964 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9965 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9966 int rc = -ENODEV;
9967
9968 switch (phba->pci_dev_grp) {
9969 case LPFC_PCI_DEV_LP:
9970 rc = lpfc_pci_resume_one_s3(pdev);
9971 break;
James Smartda0436e2009-05-22 14:51:39 -04009972 case LPFC_PCI_DEV_OC:
9973 rc = lpfc_pci_resume_one_s4(pdev);
9974 break;
James Smart3772a992009-05-22 14:50:54 -04009975 default:
9976 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9977 "1426 Invalid PCI device group: 0x%x\n",
9978 phba->pci_dev_grp);
9979 break;
9980 }
9981 return rc;
9982}
9983
9984/**
9985 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
9986 * @pdev: pointer to PCI device.
9987 * @state: the current PCI connection state.
9988 *
9989 * This routine is registered to the PCI subsystem for error handling. This
9990 * function is called by the PCI subsystem after a PCI bus error affecting
9991 * this device has been detected. When this routine is invoked, it dispatches
9992 * the action to the proper SLI-3 or SLI-4 device error detected handling
9993 * routine, which will perform the proper error detected operation.
9994 *
9995 * Return codes
9996 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
9997 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
9998 **/
9999static pci_ers_result_t
10000lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
10001{
10002 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10003 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10004 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
10005
10006 switch (phba->pci_dev_grp) {
10007 case LPFC_PCI_DEV_LP:
10008 rc = lpfc_io_error_detected_s3(pdev, state);
10009 break;
James Smartda0436e2009-05-22 14:51:39 -040010010 case LPFC_PCI_DEV_OC:
10011 rc = lpfc_io_error_detected_s4(pdev, state);
10012 break;
James Smart3772a992009-05-22 14:50:54 -040010013 default:
10014 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10015 "1427 Invalid PCI device group: 0x%x\n",
10016 phba->pci_dev_grp);
10017 break;
10018 }
10019 return rc;
10020}
10021
10022/**
10023 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
10024 * @pdev: pointer to PCI device.
10025 *
10026 * This routine is registered to the PCI subsystem for error handling. This
10027 * function is called after PCI bus has been reset to restart the PCI card
10028 * from scratch, as if from a cold-boot. When this routine is invoked, it
10029 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
10030 * routine, which will perform the proper device reset.
10031 *
10032 * Return codes
10033 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10034 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10035 **/
10036static pci_ers_result_t
10037lpfc_io_slot_reset(struct pci_dev *pdev)
10038{
10039 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10040 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10041 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
10042
10043 switch (phba->pci_dev_grp) {
10044 case LPFC_PCI_DEV_LP:
10045 rc = lpfc_io_slot_reset_s3(pdev);
10046 break;
James Smartda0436e2009-05-22 14:51:39 -040010047 case LPFC_PCI_DEV_OC:
10048 rc = lpfc_io_slot_reset_s4(pdev);
10049 break;
James Smart3772a992009-05-22 14:50:54 -040010050 default:
10051 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10052 "1428 Invalid PCI device group: 0x%x\n",
10053 phba->pci_dev_grp);
10054 break;
10055 }
10056 return rc;
10057}
10058
10059/**
10060 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
10061 * @pdev: pointer to PCI device
10062 *
10063 * This routine is registered to the PCI subsystem for error handling. It
10064 * is called when kernel error recovery tells the lpfc driver that it is
10065 * OK to resume normal PCI operation after PCI bus error recovery. When
10066 * this routine is invoked, it dispatches the action to the proper SLI-3
10067 * or SLI-4 device io_resume routine, which will resume the device operation.
10068 **/
10069static void
10070lpfc_io_resume(struct pci_dev *pdev)
10071{
10072 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10073 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10074
10075 switch (phba->pci_dev_grp) {
10076 case LPFC_PCI_DEV_LP:
10077 lpfc_io_resume_s3(pdev);
10078 break;
James Smartda0436e2009-05-22 14:51:39 -040010079 case LPFC_PCI_DEV_OC:
10080 lpfc_io_resume_s4(pdev);
10081 break;
James Smart3772a992009-05-22 14:50:54 -040010082 default:
10083 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10084 "1429 Invalid PCI device group: 0x%x\n",
10085 phba->pci_dev_grp);
10086 break;
10087 }
10088 return;
10089}
10090
James Smart3ef6d242012-01-18 16:23:48 -050010091/**
10092 * lpfc_mgmt_open - method called when 'lpfcmgmt' is opened from userspace
10093 * @inode: pointer to the inode representing the lpfcmgmt device
10094 * @filep: pointer to the file representing the open lpfcmgmt device
10095 *
10096 * This routine puts a reference count on the lpfc module whenever the
10097 * character device is opened
10098 **/
10099static int
10100lpfc_mgmt_open(struct inode *inode, struct file *filep)
10101{
10102 try_module_get(THIS_MODULE);
10103 return 0;
10104}
10105
10106/**
10107 * lpfc_mgmt_release - method called when 'lpfcmgmt' is closed in userspace
10108 * @inode: pointer to the inode representing the lpfcmgmt device
10109 * @filep: pointer to the file representing the open lpfcmgmt device
10110 *
10111 * This routine removes a reference count from the lpfc module when the
10112 * character device is closed
10113 **/
10114static int
10115lpfc_mgmt_release(struct inode *inode, struct file *filep)
10116{
10117 module_put(THIS_MODULE);
10118 return 0;
10119}
10120
dea31012005-04-17 16:05:31 -050010121static struct pci_device_id lpfc_id_table[] = {
10122 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
10123 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -040010124 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
10125 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010126 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
10127 PCI_ANY_ID, PCI_ANY_ID, },
10128 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
10129 PCI_ANY_ID, PCI_ANY_ID, },
10130 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
10131 PCI_ANY_ID, PCI_ANY_ID, },
10132 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
10133 PCI_ANY_ID, PCI_ANY_ID, },
10134 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
10135 PCI_ANY_ID, PCI_ANY_ID, },
10136 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
10137 PCI_ANY_ID, PCI_ANY_ID, },
10138 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
10139 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050010140 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
10141 PCI_ANY_ID, PCI_ANY_ID, },
10142 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
10143 PCI_ANY_ID, PCI_ANY_ID, },
10144 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
10145 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010146 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
10147 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050010148 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
10149 PCI_ANY_ID, PCI_ANY_ID, },
10150 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
10151 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010152 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
10153 PCI_ANY_ID, PCI_ANY_ID, },
10154 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
10155 PCI_ANY_ID, PCI_ANY_ID, },
10156 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
10157 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -040010158 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
10159 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050010160 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
10161 PCI_ANY_ID, PCI_ANY_ID, },
10162 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
10163 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010164 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
10165 PCI_ANY_ID, PCI_ANY_ID, },
10166 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
10167 PCI_ANY_ID, PCI_ANY_ID, },
10168 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
10169 PCI_ANY_ID, PCI_ANY_ID, },
10170 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
10171 PCI_ANY_ID, PCI_ANY_ID, },
10172 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
10173 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050010174 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
10175 PCI_ANY_ID, PCI_ANY_ID, },
10176 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
10177 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -040010178 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
10179 PCI_ANY_ID, PCI_ANY_ID, },
10180 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
10181 PCI_ANY_ID, PCI_ANY_ID, },
10182 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
10183 PCI_ANY_ID, PCI_ANY_ID, },
10184 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
10185 PCI_ANY_ID, PCI_ANY_ID, },
10186 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
10187 PCI_ANY_ID, PCI_ANY_ID, },
10188 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
10189 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -040010190 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
10191 PCI_ANY_ID, PCI_ANY_ID, },
10192 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
10193 PCI_ANY_ID, PCI_ANY_ID, },
10194 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
10195 PCI_ANY_ID, PCI_ANY_ID, },
James Smart3772a992009-05-22 14:50:54 -040010196 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
10197 PCI_ANY_ID, PCI_ANY_ID, },
James Smarta747c9c2009-11-18 15:41:10 -050010198 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
10199 PCI_ANY_ID, PCI_ANY_ID, },
10200 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
James Smart6669f9b2009-10-02 15:16:45 -040010201 PCI_ANY_ID, PCI_ANY_ID, },
James Smart98fc5dd2010-06-07 15:24:29 -040010202 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BALIUS,
10203 PCI_ANY_ID, PCI_ANY_ID, },
James Smart085c6472010-11-20 23:11:37 -050010204 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC,
10205 PCI_ANY_ID, PCI_ANY_ID, },
10206 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE,
10207 PCI_ANY_ID, PCI_ANY_ID, },
James Smartc0c11512011-05-24 11:41:34 -040010208 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC_VF,
10209 PCI_ANY_ID, PCI_ANY_ID, },
10210 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE_VF,
10211 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010212 { 0 }
10213};
10214
10215MODULE_DEVICE_TABLE(pci, lpfc_id_table);
10216
Linas Vepstas8d63f372007-02-14 14:28:36 -060010217static struct pci_error_handlers lpfc_err_handler = {
10218 .error_detected = lpfc_io_error_detected,
10219 .slot_reset = lpfc_io_slot_reset,
10220 .resume = lpfc_io_resume,
10221};
10222
dea31012005-04-17 16:05:31 -050010223static struct pci_driver lpfc_driver = {
10224 .name = LPFC_DRIVER_NAME,
10225 .id_table = lpfc_id_table,
10226 .probe = lpfc_pci_probe_one,
10227 .remove = __devexit_p(lpfc_pci_remove_one),
James Smart3a55b532008-12-04 22:38:54 -050010228 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -040010229 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -050010230 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -050010231};
10232
James Smart3ef6d242012-01-18 16:23:48 -050010233static const struct file_operations lpfc_mgmt_fop = {
10234 .open = lpfc_mgmt_open,
10235 .release = lpfc_mgmt_release,
10236};
10237
10238static struct miscdevice lpfc_mgmt_dev = {
10239 .minor = MISC_DYNAMIC_MINOR,
10240 .name = "lpfcmgmt",
10241 .fops = &lpfc_mgmt_fop,
10242};
10243
James Smarte59058c2008-08-24 21:49:00 -040010244/**
James Smart3621a712009-04-06 18:47:14 -040010245 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -040010246 *
10247 * This routine is to be invoked when the lpfc module is loaded into the
10248 * kernel. The special kernel macro module_init() is used to indicate the
10249 * role of this routine to the kernel as lpfc module entry point.
10250 *
10251 * Return codes
10252 * 0 - successful
10253 * -ENOMEM - FC attach transport failed
10254 * all others - failed
10255 */
dea31012005-04-17 16:05:31 -050010256static int __init
10257lpfc_init(void)
10258{
10259 int error = 0;
10260
10261 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010262 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -050010263
James Smart3ef6d242012-01-18 16:23:48 -050010264 error = misc_register(&lpfc_mgmt_dev);
10265 if (error)
10266 printk(KERN_ERR "Could not register lpfcmgmt device, "
10267 "misc_register returned with status %d", error);
10268
James Smart7ee5d432007-10-27 13:37:17 -040010269 if (lpfc_enable_npiv) {
10270 lpfc_transport_functions.vport_create = lpfc_vport_create;
10271 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
10272 }
dea31012005-04-17 16:05:31 -050010273 lpfc_transport_template =
10274 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -040010275 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -050010276 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -040010277 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -040010278 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -040010279 fc_attach_transport(&lpfc_vport_transport_functions);
10280 if (lpfc_vport_transport_template == NULL) {
10281 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040010282 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -040010283 }
James Smart7ee5d432007-10-27 13:37:17 -040010284 }
dea31012005-04-17 16:05:31 -050010285 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -050010286 if (error) {
dea31012005-04-17 16:05:31 -050010287 fc_release_transport(lpfc_transport_template);
James Smartd7c255b2008-08-24 21:50:00 -040010288 if (lpfc_enable_npiv)
10289 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -050010290 }
dea31012005-04-17 16:05:31 -050010291
10292 return error;
10293}
10294
James Smarte59058c2008-08-24 21:49:00 -040010295/**
James Smart3621a712009-04-06 18:47:14 -040010296 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -040010297 *
10298 * This routine is invoked when the lpfc module is removed from the kernel.
10299 * The special kernel macro module_exit() is used to indicate the role of
10300 * this routine to the kernel as lpfc module exit point.
10301 */
dea31012005-04-17 16:05:31 -050010302static void __exit
10303lpfc_exit(void)
10304{
James Smart3ef6d242012-01-18 16:23:48 -050010305 misc_deregister(&lpfc_mgmt_dev);
dea31012005-04-17 16:05:31 -050010306 pci_unregister_driver(&lpfc_driver);
10307 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040010308 if (lpfc_enable_npiv)
10309 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -050010310 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040010311 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
10312 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050010313 (1L << _dump_buf_data_order), _dump_buf_data);
10314 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
10315 }
10316
10317 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -040010318 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
10319 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050010320 (1L << _dump_buf_dif_order), _dump_buf_dif);
10321 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
10322 }
dea31012005-04-17 16:05:31 -050010323}
10324
10325module_init(lpfc_init);
10326module_exit(lpfc_exit);
10327MODULE_LICENSE("GPL");
10328MODULE_DESCRIPTION(LPFC_MODULE_DESC);
10329MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
10330MODULE_VERSION("0:" LPFC_DRIVER_VERSION);