blob: d9628770f11e3466a0fd5b835f9760299e1442d2 [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 Gortmakeracf33682011-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 Smart87af33f2007-10-27 13:37:43 -04002344 if (ndlp->nlp_type & NLP_FABRIC)
2345 lpfc_disc_state_machine(vport, ndlp, NULL,
2346 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002347
James Smart87af33f2007-10-27 13:37:43 -04002348 lpfc_disc_state_machine(vport, ndlp, NULL,
2349 NLP_EVT_DEVICE_RM);
James Smart495a7142008-06-14 22:52:59 -04002350
James Smart87af33f2007-10-27 13:37:43 -04002351 }
2352
James Smarta8adb832007-10-27 13:37:53 -04002353 /* At this point, ALL ndlp's should be gone
2354 * because of the previous NLP_EVT_DEVICE_RM.
2355 * Lets wait for this to happen, if needed.
2356 */
James Smart87af33f2007-10-27 13:37:43 -04002357 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002358 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002359 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002360 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002361 list_for_each_entry_safe(ndlp, next_ndlp,
2362 &vport->fc_nodes, nlp_listp) {
2363 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2364 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002365 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002366 "usgmap:x%x refcnt:%d\n",
2367 ndlp->nlp_DID, (void *)ndlp,
2368 ndlp->nlp_usg_map,
2369 atomic_read(
2370 &ndlp->kref.refcount));
2371 }
James Smarta8adb832007-10-27 13:37:53 -04002372 break;
James Smart87af33f2007-10-27 13:37:43 -04002373 }
James Smarta8adb832007-10-27 13:37:53 -04002374
2375 /* Wait for any activity on ndlps to settle */
2376 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002377 }
James Smart1151e3e2011-02-16 12:39:35 -05002378 lpfc_cleanup_vports_rrqs(vport, NULL);
dea31012005-04-17 16:05:31 -05002379}
2380
James Smarte59058c2008-08-24 21:49:00 -04002381/**
James Smart3621a712009-04-06 18:47:14 -04002382 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002383 * @vport: pointer to a virtual N_Port data structure.
2384 *
2385 * This routine stops all the timers associated with a @vport. This function
2386 * is invoked before disabling or deleting a @vport. Note that the physical
2387 * port is treated as @vport 0.
2388 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002389void
2390lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002391{
James Smart92d7f7b2007-06-17 19:56:38 -05002392 del_timer_sync(&vport->els_tmofunc);
2393 del_timer_sync(&vport->fc_fdmitmo);
James Smart92494142011-02-16 12:39:44 -05002394 del_timer_sync(&vport->delayed_disc_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002395 lpfc_can_disctmo(vport);
2396 return;
dea31012005-04-17 16:05:31 -05002397}
2398
James Smarte59058c2008-08-24 21:49:00 -04002399/**
James Smartecfd03c2010-02-12 14:41:27 -05002400 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2401 * @phba: pointer to lpfc hba data structure.
2402 *
2403 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2404 * caller of this routine should already hold the host lock.
2405 **/
2406void
2407__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2408{
James Smart5ac6b302010-10-22 11:05:36 -04002409 /* Clear pending FCF rediscovery wait flag */
2410 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2411
James Smartecfd03c2010-02-12 14:41:27 -05002412 /* Now, try to stop the timer */
2413 del_timer(&phba->fcf.redisc_wait);
2414}
2415
2416/**
2417 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2418 * @phba: pointer to lpfc hba data structure.
2419 *
2420 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2421 * checks whether the FCF rediscovery wait timer is pending with the host
2422 * lock held before proceeding with disabling the timer and clearing the
2423 * wait timer pendig flag.
2424 **/
2425void
2426lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2427{
2428 spin_lock_irq(&phba->hbalock);
2429 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2430 /* FCF rediscovery timer already fired or stopped */
2431 spin_unlock_irq(&phba->hbalock);
2432 return;
2433 }
2434 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart5ac6b302010-10-22 11:05:36 -04002435 /* Clear failover in progress flags */
2436 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05002437 spin_unlock_irq(&phba->hbalock);
2438}
2439
2440/**
James Smart3772a992009-05-22 14:50:54 -04002441 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002442 * @phba: pointer to lpfc hba data structure.
2443 *
2444 * This routine stops all the timers associated with a HBA. This function is
2445 * invoked before either putting a HBA offline or unloading the driver.
2446 **/
James Smart3772a992009-05-22 14:50:54 -04002447void
2448lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002449{
James Smart51ef4c22007-08-02 11:10:31 -04002450 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05002451 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002452 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002453 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002454 del_timer_sync(&phba->hb_tmofunc);
James Smart1151e3e2011-02-16 12:39:35 -05002455 if (phba->sli_rev == LPFC_SLI_REV4) {
2456 del_timer_sync(&phba->rrq_tmr);
2457 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2458 }
James Smart3772a992009-05-22 14:50:54 -04002459 phba->hb_outstanding = 0;
2460
2461 switch (phba->pci_dev_grp) {
2462 case LPFC_PCI_DEV_LP:
2463 /* Stop any LightPulse device specific driver timers */
2464 del_timer_sync(&phba->fcp_poll_timer);
2465 break;
2466 case LPFC_PCI_DEV_OC:
2467 /* Stop any OneConnect device sepcific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05002468 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04002469 break;
2470 default:
2471 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2472 "0297 Invalid device group (x%x)\n",
2473 phba->pci_dev_grp);
2474 break;
2475 }
James Smart2e0fef82007-06-17 19:56:36 -05002476 return;
dea31012005-04-17 16:05:31 -05002477}
2478
James Smarte59058c2008-08-24 21:49:00 -04002479/**
James Smart3621a712009-04-06 18:47:14 -04002480 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002481 * @phba: pointer to lpfc hba data structure.
2482 *
2483 * This routine marks a HBA's management interface as blocked. Once the HBA's
2484 * management interface is marked as blocked, all the user space access to
2485 * the HBA, whether they are from sysfs interface or libdfc interface will
2486 * all be blocked. The HBA is set to block the management interface when the
2487 * driver prepares the HBA interface for online or offline.
2488 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002489static void
2490lpfc_block_mgmt_io(struct lpfc_hba * phba)
2491{
2492 unsigned long iflag;
James Smart6e7288d2010-06-07 15:23:35 -04002493 uint8_t actcmd = MBX_HEARTBEAT;
2494 unsigned long timeout;
2495
James Smarta183a152011-10-10 21:32:43 -04002496 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
Adrian Bunka6ababd2007-11-05 18:07:33 +01002497 spin_lock_irqsave(&phba->hbalock, iflag);
2498 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
James Smarta183a152011-10-10 21:32:43 -04002499 if (phba->sli.mbox_active) {
James Smart6e7288d2010-06-07 15:23:35 -04002500 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
James Smarta183a152011-10-10 21:32:43 -04002501 /* Determine how long we might wait for the active mailbox
2502 * command to be gracefully completed by firmware.
2503 */
2504 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
2505 phba->sli.mbox_active) * 1000) + jiffies;
2506 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002507 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002508
James Smart6e7288d2010-06-07 15:23:35 -04002509 /* Wait for the outstnading mailbox command to complete */
2510 while (phba->sli.mbox_active) {
2511 /* Check active mailbox complete status every 2ms */
2512 msleep(2);
2513 if (time_after(jiffies, timeout)) {
2514 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2515 "2813 Mgmt IO is Blocked %x "
2516 "- mbox cmd %x still active\n",
2517 phba->sli.sli_flag, actcmd);
2518 break;
2519 }
2520 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002521}
2522
James Smarte59058c2008-08-24 21:49:00 -04002523/**
James Smart6b5151f2012-01-18 16:24:06 -05002524 * lpfc_sli4_node_prep - Assign RPIs for active nodes.
2525 * @phba: pointer to lpfc hba data structure.
2526 *
2527 * Allocate RPIs for all active remote nodes. This is needed whenever
2528 * an SLI4 adapter is reset and the driver is not unloading. Its purpose
2529 * is to fixup the temporary rpi assignments.
2530 **/
2531void
2532lpfc_sli4_node_prep(struct lpfc_hba *phba)
2533{
2534 struct lpfc_nodelist *ndlp, *next_ndlp;
2535 struct lpfc_vport **vports;
2536 int i;
2537
2538 if (phba->sli_rev != LPFC_SLI_REV4)
2539 return;
2540
2541 vports = lpfc_create_vport_work_array(phba);
2542 if (vports != NULL) {
2543 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2544 if (vports[i]->load_flag & FC_UNLOADING)
2545 continue;
2546
2547 list_for_each_entry_safe(ndlp, next_ndlp,
2548 &vports[i]->fc_nodes,
2549 nlp_listp) {
2550 if (NLP_CHK_NODE_ACT(ndlp))
2551 ndlp->nlp_rpi =
2552 lpfc_sli4_alloc_rpi(phba);
2553 }
2554 }
2555 }
2556 lpfc_destroy_vport_work_array(phba, vports);
2557}
2558
2559/**
James Smart3621a712009-04-06 18:47:14 -04002560 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04002561 * @phba: pointer to lpfc hba data structure.
2562 *
2563 * This routine initializes the HBA and brings a HBA online. During this
2564 * process, the management interface is blocked to prevent user space access
2565 * to the HBA interfering with the driver initialization.
2566 *
2567 * Return codes
2568 * 0 - successful
2569 * 1 - failed
2570 **/
dea31012005-04-17 16:05:31 -05002571int
James Smart2e0fef82007-06-17 19:56:36 -05002572lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002573{
Julia Lawall372bd282008-12-16 16:15:08 +01002574 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04002575 struct lpfc_vport **vports;
2576 int i;
James Smart2e0fef82007-06-17 19:56:36 -05002577
dea31012005-04-17 16:05:31 -05002578 if (!phba)
2579 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01002580 vport = phba->pport;
dea31012005-04-17 16:05:31 -05002581
James Smart2e0fef82007-06-17 19:56:36 -05002582 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05002583 return 0;
2584
James Smarted957682007-06-17 19:56:37 -05002585 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002586 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05002587
James Smart46fa3112007-04-25 09:51:45 -04002588 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002589
James Smart46fa3112007-04-25 09:51:45 -04002590 if (!lpfc_sli_queue_setup(phba)) {
2591 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002592 return 1;
James Smart46fa3112007-04-25 09:51:45 -04002593 }
2594
James Smartda0436e2009-05-22 14:51:39 -04002595 if (phba->sli_rev == LPFC_SLI_REV4) {
2596 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2597 lpfc_unblock_mgmt_io(phba);
2598 return 1;
2599 }
2600 } else {
2601 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2602 lpfc_unblock_mgmt_io(phba);
2603 return 1;
2604 }
James Smart46fa3112007-04-25 09:51:45 -04002605 }
dea31012005-04-17 16:05:31 -05002606
James Smart549e55c2007-08-02 11:09:51 -04002607 vports = lpfc_create_vport_work_array(phba);
2608 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002609 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002610 struct Scsi_Host *shost;
2611 shost = lpfc_shost_from_vport(vports[i]);
2612 spin_lock_irq(shost->host_lock);
2613 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2614 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2615 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart1c6834a2009-07-19 10:01:26 -04002616 if (phba->sli_rev == LPFC_SLI_REV4)
2617 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart549e55c2007-08-02 11:09:51 -04002618 spin_unlock_irq(shost->host_lock);
2619 }
James Smart09372822008-01-11 01:52:54 -05002620 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002621
James Smart46fa3112007-04-25 09:51:45 -04002622 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002623 return 0;
2624}
2625
James Smarte59058c2008-08-24 21:49:00 -04002626/**
James Smart3621a712009-04-06 18:47:14 -04002627 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04002628 * @phba: pointer to lpfc hba data structure.
2629 *
2630 * This routine marks a HBA's management interface as not blocked. Once the
2631 * HBA's management interface is marked as not blocked, all the user space
2632 * access to the HBA, whether they are from sysfs interface or libdfc
2633 * interface will be allowed. The HBA is set to block the management interface
2634 * when the driver prepares the HBA interface for online or offline and then
2635 * set to unblock the management interface afterwards.
2636 **/
James Smart46fa3112007-04-25 09:51:45 -04002637void
James Smart46fa3112007-04-25 09:51:45 -04002638lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2639{
2640 unsigned long iflag;
2641
James Smart2e0fef82007-06-17 19:56:36 -05002642 spin_lock_irqsave(&phba->hbalock, iflag);
2643 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2644 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04002645}
2646
James Smarte59058c2008-08-24 21:49:00 -04002647/**
James Smart3621a712009-04-06 18:47:14 -04002648 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04002649 * @phba: pointer to lpfc hba data structure.
2650 *
2651 * This routine is invoked to prepare a HBA to be brought offline. It performs
2652 * unregistration login to all the nodes on all vports and flushes the mailbox
2653 * queue to make it ready to be brought offline.
2654 **/
James Smart46fa3112007-04-25 09:51:45 -04002655void
2656lpfc_offline_prep(struct lpfc_hba * phba)
2657{
James Smart2e0fef82007-06-17 19:56:36 -05002658 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04002659 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04002660 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05002661 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04002662 int i;
dea31012005-04-17 16:05:31 -05002663
James Smart2e0fef82007-06-17 19:56:36 -05002664 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002665 return;
dea31012005-04-17 16:05:31 -05002666
James Smart46fa3112007-04-25 09:51:45 -04002667 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002668
2669 lpfc_linkdown(phba);
2670
James Smart87af33f2007-10-27 13:37:43 -04002671 /* Issue an unreg_login to all nodes on all vports */
2672 vports = lpfc_create_vport_work_array(phba);
2673 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04002674 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04002675 if (vports[i]->load_flag & FC_UNLOADING)
2676 continue;
James Smart72100cc2010-02-12 14:43:01 -05002677 shost = lpfc_shost_from_vport(vports[i]);
2678 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002679 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002680 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2681 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05002682 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002683
James Smart87af33f2007-10-27 13:37:43 -04002684 shost = lpfc_shost_from_vport(vports[i]);
2685 list_for_each_entry_safe(ndlp, next_ndlp,
2686 &vports[i]->fc_nodes,
2687 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002688 if (!NLP_CHK_NODE_ACT(ndlp))
2689 continue;
James Smart87af33f2007-10-27 13:37:43 -04002690 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2691 continue;
2692 if (ndlp->nlp_type & NLP_FABRIC) {
2693 lpfc_disc_state_machine(vports[i], ndlp,
2694 NULL, NLP_EVT_DEVICE_RECOVERY);
2695 lpfc_disc_state_machine(vports[i], ndlp,
2696 NULL, NLP_EVT_DEVICE_RM);
2697 }
2698 spin_lock_irq(shost->host_lock);
2699 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart6b5151f2012-01-18 16:24:06 -05002700
2701 /*
2702 * Whenever an SLI4 port goes offline, free the
2703 * RPI. A new RPI when the adapter port comes
2704 * back online.
2705 */
2706 if (phba->sli_rev == LPFC_SLI_REV4)
2707 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
2708
James Smart87af33f2007-10-27 13:37:43 -04002709 spin_unlock_irq(shost->host_lock);
2710 lpfc_unreg_rpi(vports[i], ndlp);
2711 }
2712 }
2713 }
James Smart09372822008-01-11 01:52:54 -05002714 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002715
James Smart04c68492009-05-22 14:52:52 -04002716 lpfc_sli_mbox_sys_shutdown(phba);
James Smart46fa3112007-04-25 09:51:45 -04002717}
2718
James Smarte59058c2008-08-24 21:49:00 -04002719/**
James Smart3621a712009-04-06 18:47:14 -04002720 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04002721 * @phba: pointer to lpfc hba data structure.
2722 *
2723 * This routine actually brings a HBA offline. It stops all the timers
2724 * associated with the HBA, brings down the SLI layer, and eventually
2725 * marks the HBA as in offline state for the upper layer protocol.
2726 **/
James Smart46fa3112007-04-25 09:51:45 -04002727void
James Smart2e0fef82007-06-17 19:56:36 -05002728lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04002729{
James Smart549e55c2007-08-02 11:09:51 -04002730 struct Scsi_Host *shost;
2731 struct lpfc_vport **vports;
2732 int i;
James Smart46fa3112007-04-25 09:51:45 -04002733
James Smart549e55c2007-08-02 11:09:51 -04002734 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002735 return;
James Smart688a8862006-07-06 15:49:56 -04002736
James Smartda0436e2009-05-22 14:51:39 -04002737 /* stop port and all timers associated with this hba */
2738 lpfc_stop_port(phba);
James Smart51ef4c22007-08-02 11:10:31 -04002739 vports = lpfc_create_vport_work_array(phba);
2740 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002741 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04002742 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05002743 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05002744 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002745 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05002746 /* Bring down the SLI Layer and cleanup. The HBA is offline
2747 now. */
2748 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002749 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04002750 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002751 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04002752 vports = lpfc_create_vport_work_array(phba);
2753 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002754 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002755 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04002756 spin_lock_irq(shost->host_lock);
2757 vports[i]->work_port_events = 0;
2758 vports[i]->fc_flag |= FC_OFFLINE_MODE;
2759 spin_unlock_irq(shost->host_lock);
2760 }
James Smart09372822008-01-11 01:52:54 -05002761 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002762}
2763
James Smarte59058c2008-08-24 21:49:00 -04002764/**
James Smartff78d8f2011-12-13 13:21:35 -05002765 * lpfc_scsi_buf_update - Update the scsi_buffers that are already allocated.
2766 * @phba: pointer to lpfc hba data structure.
2767 *
2768 * This routine goes through all the scsi buffers in the system and updates the
2769 * Physical XRIs assigned to the SCSI buffer because these may change after any
2770 * firmware reset
2771 *
2772 * Return codes
2773 * 0 - successful (for now, it always returns 0)
2774 **/
2775int
2776lpfc_scsi_buf_update(struct lpfc_hba *phba)
2777{
2778 struct lpfc_scsi_buf *sb, *sb_next;
2779
2780 spin_lock_irq(&phba->hbalock);
2781 spin_lock(&phba->scsi_buf_list_lock);
2782 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list)
2783 sb->cur_iocbq.sli4_xritag =
2784 phba->sli4_hba.xri_ids[sb->cur_iocbq.sli4_lxritag];
2785 spin_unlock(&phba->scsi_buf_list_lock);
2786 spin_unlock_irq(&phba->hbalock);
2787 return 0;
2788}
2789
2790/**
James Smart3621a712009-04-06 18:47:14 -04002791 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04002792 * @phba: pointer to lpfc hba data structure.
2793 *
2794 * This routine is to free all the SCSI buffers and IOCBs from the driver
2795 * list back to kernel. It is called from lpfc_pci_remove_one to free
2796 * the internal resources before the device is removed from the system.
2797 *
2798 * Return codes
2799 * 0 - successful (for now, it always returns 0)
2800 **/
dea31012005-04-17 16:05:31 -05002801static int
James Smart2e0fef82007-06-17 19:56:36 -05002802lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002803{
2804 struct lpfc_scsi_buf *sb, *sb_next;
2805 struct lpfc_iocbq *io, *io_next;
2806
James Smart2e0fef82007-06-17 19:56:36 -05002807 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002808 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smart1c6f4ef52009-11-18 15:40:49 -05002809 spin_lock(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05002810 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
2811 list_del(&sb->list);
2812 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05002813 sb->dma_handle);
dea31012005-04-17 16:05:31 -05002814 kfree(sb);
2815 phba->total_scsi_bufs--;
2816 }
James Smart1c6f4ef52009-11-18 15:40:49 -05002817 spin_unlock(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05002818
2819 /* Release all the lpfc_iocbq entries maintained by this host. */
2820 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2821 list_del(&io->list);
2822 kfree(io);
2823 phba->total_iocbq_bufs--;
2824 }
James Smart6d368e52011-05-24 11:44:12 -04002825
James Smart2e0fef82007-06-17 19:56:36 -05002826 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002827 return 0;
2828}
2829
James Smarte59058c2008-08-24 21:49:00 -04002830/**
James Smart3621a712009-04-06 18:47:14 -04002831 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04002832 * @phba: pointer to lpfc hba data structure.
2833 * @instance: a unique integer ID to this FC port.
2834 * @dev: pointer to the device data structure.
2835 *
2836 * This routine creates a FC port for the upper layer protocol. The FC port
2837 * can be created on top of either a physical port or a virtual port provided
2838 * by the HBA. This routine also allocates a SCSI host data structure (shost)
2839 * and associates the FC port created before adding the shost into the SCSI
2840 * layer.
2841 *
2842 * Return codes
2843 * @vport - pointer to the virtual N_Port data structure.
2844 * NULL - port create failed.
2845 **/
James Smart2e0fef82007-06-17 19:56:36 -05002846struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04002847lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04002848{
James Smart2e0fef82007-06-17 19:56:36 -05002849 struct lpfc_vport *vport;
2850 struct Scsi_Host *shost;
2851 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04002852
James Smart3de2a652007-08-02 11:09:59 -04002853 if (dev != &phba->pcidev->dev)
2854 shost = scsi_host_alloc(&lpfc_vport_template,
2855 sizeof(struct lpfc_vport));
2856 else
2857 shost = scsi_host_alloc(&lpfc_template,
2858 sizeof(struct lpfc_vport));
James Smart2e0fef82007-06-17 19:56:36 -05002859 if (!shost)
2860 goto out;
James Smart47a86172007-04-25 09:53:22 -04002861
James Smart2e0fef82007-06-17 19:56:36 -05002862 vport = (struct lpfc_vport *) shost->hostdata;
2863 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05002864 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05002865 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05002866 vport->fc_rscn_flush = 0;
James Smart47a86172007-04-25 09:53:22 -04002867
James Smart3de2a652007-08-02 11:09:59 -04002868 lpfc_get_vport_cfgparam(vport);
James Smart2e0fef82007-06-17 19:56:36 -05002869 shost->unique_id = instance;
2870 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04002871 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05002872 shost->this_id = -1;
2873 shost->max_cmd_len = 16;
James Smartda0436e2009-05-22 14:51:39 -04002874 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart28baac72010-02-12 14:42:03 -05002875 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04002876 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smartda0436e2009-05-22 14:51:39 -04002877 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
2878 }
James Smart81301a92008-12-04 22:39:46 -05002879
James Smart47a86172007-04-25 09:53:22 -04002880 /*
James Smart2e0fef82007-06-17 19:56:36 -05002881 * Set initial can_queue value since 0 is no longer supported and
2882 * scsi_add_host will fail. This will be adjusted later based on the
2883 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04002884 */
James Smart2e0fef82007-06-17 19:56:36 -05002885 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04002886 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05002887 shost->transportt = lpfc_vport_transport_template;
2888 vport->port_type = LPFC_NPIV_PORT;
2889 } else {
2890 shost->transportt = lpfc_transport_template;
2891 vport->port_type = LPFC_PHYSICAL_PORT;
2892 }
James Smart47a86172007-04-25 09:53:22 -04002893
James Smart2e0fef82007-06-17 19:56:36 -05002894 /* Initialize all internally managed lists. */
2895 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04002896 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05002897 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04002898
James Smart2e0fef82007-06-17 19:56:36 -05002899 init_timer(&vport->fc_disctmo);
2900 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05002901 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002902
James Smart2e0fef82007-06-17 19:56:36 -05002903 init_timer(&vport->fc_fdmitmo);
2904 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
James Smart92d7f7b2007-06-17 19:56:38 -05002905 vport->fc_fdmitmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002906
James Smart2e0fef82007-06-17 19:56:36 -05002907 init_timer(&vport->els_tmofunc);
2908 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05002909 vport->els_tmofunc.data = (unsigned long)vport;
James Smart92494142011-02-16 12:39:44 -05002910
2911 init_timer(&vport->delayed_disc_tmo);
2912 vport->delayed_disc_tmo.function = lpfc_delayed_disc_tmo;
2913 vport->delayed_disc_tmo.data = (unsigned long)vport;
2914
James Bottomleyd139b9b2009-11-05 13:33:12 -06002915 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05002916 if (error)
2917 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04002918
James Smart549e55c2007-08-02 11:09:51 -04002919 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002920 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04002921 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002922 return vport;
James Smart47a86172007-04-25 09:53:22 -04002923
James Smart2e0fef82007-06-17 19:56:36 -05002924out_put_shost:
2925 scsi_host_put(shost);
2926out:
2927 return NULL;
James Smart47a86172007-04-25 09:53:22 -04002928}
2929
James Smarte59058c2008-08-24 21:49:00 -04002930/**
James Smart3621a712009-04-06 18:47:14 -04002931 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04002932 * @vport: pointer to an lpfc virtual N_Port data structure.
2933 *
2934 * This routine destroys a FC port from the upper layer protocol. All the
2935 * resources associated with the port are released.
2936 **/
James Smart2e0fef82007-06-17 19:56:36 -05002937void
2938destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04002939{
James Smart92d7f7b2007-06-17 19:56:38 -05002940 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2941 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002942
James Smart858c9f62007-06-17 19:56:39 -05002943 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05002944 fc_remove_host(shost);
2945 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04002946
James Smart92d7f7b2007-06-17 19:56:38 -05002947 spin_lock_irq(&phba->hbalock);
2948 list_del_init(&vport->listentry);
2949 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04002950
James Smart92d7f7b2007-06-17 19:56:38 -05002951 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04002952 return;
James Smart47a86172007-04-25 09:53:22 -04002953}
2954
James Smarte59058c2008-08-24 21:49:00 -04002955/**
James Smart3621a712009-04-06 18:47:14 -04002956 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04002957 *
2958 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
2959 * uses the kernel idr facility to perform the task.
2960 *
2961 * Return codes:
2962 * instance - a unique integer ID allocated as the new instance.
2963 * -1 - lpfc get instance failed.
2964 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002965int
2966lpfc_get_instance(void)
2967{
2968 int instance = 0;
2969
2970 /* Assign an unused number */
2971 if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
2972 return -1;
2973 if (idr_get_new(&lpfc_hba_index, NULL, &instance))
2974 return -1;
2975 return instance;
2976}
2977
James Smarte59058c2008-08-24 21:49:00 -04002978/**
James Smart3621a712009-04-06 18:47:14 -04002979 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04002980 * @shost: pointer to SCSI host data structure.
2981 * @time: elapsed time of the scan in jiffies.
2982 *
2983 * This routine is called by the SCSI layer with a SCSI host to determine
2984 * whether the scan host is finished.
2985 *
2986 * Note: there is no scan_start function as adapter initialization will have
2987 * asynchronously kicked off the link initialization.
2988 *
2989 * Return codes
2990 * 0 - SCSI host scan is not over yet.
2991 * 1 - SCSI host scan is over.
2992 **/
James Smart47a86172007-04-25 09:53:22 -04002993int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2994{
James Smart2e0fef82007-06-17 19:56:36 -05002995 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2996 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05002997 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04002998
James Smart858c9f62007-06-17 19:56:39 -05002999 spin_lock_irq(shost->host_lock);
3000
James Smart51ef4c22007-08-02 11:10:31 -04003001 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05003002 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003003 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05003004 }
James Smart2e0fef82007-06-17 19:56:36 -05003005 if (time >= 30 * HZ) {
3006 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003007 "0461 Scanning longer than 30 "
3008 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003009 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003010 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003011 }
3012 if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
3013 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003014 "0465 Link down longer than 15 "
3015 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003016 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05003017 goto finished;
James Smart47a86172007-04-25 09:53:22 -04003018 }
3019
James Smart2e0fef82007-06-17 19:56:36 -05003020 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05003021 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003022 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05003023 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003024 if (vport->fc_map_cnt == 0 && time < 2 * HZ)
James Smart858c9f62007-06-17 19:56:39 -05003025 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003026 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05003027 goto finished;
3028
3029 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003030
3031finished:
James Smart858c9f62007-06-17 19:56:39 -05003032 spin_unlock_irq(shost->host_lock);
3033 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05003034}
3035
James Smarte59058c2008-08-24 21:49:00 -04003036/**
James Smart3621a712009-04-06 18:47:14 -04003037 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04003038 * @shost: pointer to SCSI host data structure.
3039 *
3040 * This routine initializes a given SCSI host attributes on a FC port. The
3041 * SCSI host can be either on top of a physical port or a virtual port.
3042 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003043void lpfc_host_attrib_init(struct Scsi_Host *shost)
3044{
3045 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3046 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003047 /*
James Smart2e0fef82007-06-17 19:56:36 -05003048 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04003049 */
3050
James Smart2e0fef82007-06-17 19:56:36 -05003051 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
3052 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04003053 fc_host_supported_classes(shost) = FC_COS_CLASS3;
3054
3055 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003056 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003057 fc_host_supported_fc4s(shost)[2] = 1;
3058 fc_host_supported_fc4s(shost)[7] = 1;
3059
James Smart92d7f7b2007-06-17 19:56:38 -05003060 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
3061 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04003062
3063 fc_host_supported_speeds(shost) = 0;
James Smart88a2cfb2011-07-22 18:36:33 -04003064 if (phba->lmt & LMT_16Gb)
3065 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
James Smart47a86172007-04-25 09:53:22 -04003066 if (phba->lmt & LMT_10Gb)
3067 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04003068 if (phba->lmt & LMT_8Gb)
3069 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04003070 if (phba->lmt & LMT_4Gb)
3071 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
3072 if (phba->lmt & LMT_2Gb)
3073 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
3074 if (phba->lmt & LMT_1Gb)
3075 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
3076
3077 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05003078 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
3079 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04003080
Mike Christie0af5d702010-09-15 16:52:31 -05003081 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
3082
James Smart47a86172007-04-25 09:53:22 -04003083 /* This value is also unchanging */
3084 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003085 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003086 fc_host_active_fc4s(shost)[2] = 1;
3087 fc_host_active_fc4s(shost)[7] = 1;
3088
James Smart92d7f7b2007-06-17 19:56:38 -05003089 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04003090 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04003091 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04003092 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04003093}
dea31012005-04-17 16:05:31 -05003094
James Smarte59058c2008-08-24 21:49:00 -04003095/**
James Smartda0436e2009-05-22 14:51:39 -04003096 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04003097 * @phba: pointer to lpfc hba data structure.
3098 *
James Smartda0436e2009-05-22 14:51:39 -04003099 * This routine is invoked to stop an SLI3 device port, it stops the device
3100 * from generating interrupts and stops the device driver's timers for the
3101 * device.
James Smarte59058c2008-08-24 21:49:00 -04003102 **/
James Smartdb2378e2008-02-08 18:49:51 -05003103static void
James Smartda0436e2009-05-22 14:51:39 -04003104lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05003105{
James Smartda0436e2009-05-22 14:51:39 -04003106 /* Clear all interrupt enable conditions */
3107 writel(0, phba->HCregaddr);
3108 readl(phba->HCregaddr); /* flush */
3109 /* Clear all pending interrupts */
3110 writel(0xffffffff, phba->HAregaddr);
3111 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04003112
James Smartda0436e2009-05-22 14:51:39 -04003113 /* Reset some HBA SLI setup states */
3114 lpfc_stop_hba_timers(phba);
3115 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05003116}
3117
James Smarte59058c2008-08-24 21:49:00 -04003118/**
James Smartda0436e2009-05-22 14:51:39 -04003119 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05003120 * @phba: pointer to lpfc hba data structure.
3121 *
James Smartda0436e2009-05-22 14:51:39 -04003122 * This routine is invoked to stop an SLI4 device port, it stops the device
3123 * from generating interrupts and stops the device driver's timers for the
3124 * device.
3125 **/
3126static void
3127lpfc_stop_port_s4(struct lpfc_hba *phba)
3128{
3129 /* Reset some HBA SLI4 setup states */
3130 lpfc_stop_hba_timers(phba);
3131 phba->pport->work_port_events = 0;
3132 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04003133}
3134
3135/**
3136 * lpfc_stop_port - Wrapper function for stopping hba port
3137 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05003138 *
James Smartda0436e2009-05-22 14:51:39 -04003139 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
3140 * the API jump table function pointer from the lpfc_hba struct.
3141 **/
3142void
3143lpfc_stop_port(struct lpfc_hba *phba)
3144{
3145 phba->lpfc_stop_port(phba);
3146}
3147
3148/**
James Smartecfd03c2010-02-12 14:41:27 -05003149 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
3150 * @phba: Pointer to hba for which this call is being executed.
3151 *
3152 * This routine starts the timer waiting for the FCF rediscovery to complete.
3153 **/
3154void
3155lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
3156{
3157 unsigned long fcf_redisc_wait_tmo =
3158 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
3159 /* Start fcf rediscovery wait period timer */
3160 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
3161 spin_lock_irq(&phba->hbalock);
3162 /* Allow action to new fcf asynchronous event */
3163 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
3164 /* Mark the FCF rediscovery pending state */
3165 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
3166 spin_unlock_irq(&phba->hbalock);
3167}
3168
3169/**
3170 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
3171 * @ptr: Map to lpfc_hba data structure pointer.
3172 *
3173 * This routine is invoked when waiting for FCF table rediscover has been
3174 * timed out. If new FCF record(s) has (have) been discovered during the
3175 * wait period, a new FCF event shall be added to the FCOE async event
3176 * list, and then worker thread shall be waked up for processing from the
3177 * worker thread context.
3178 **/
3179void
3180lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
3181{
3182 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
3183
3184 /* Don't send FCF rediscovery event if timer cancelled */
3185 spin_lock_irq(&phba->hbalock);
3186 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
3187 spin_unlock_irq(&phba->hbalock);
3188 return;
3189 }
3190 /* Clear FCF rediscovery timer pending flag */
3191 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
3192 /* FCF rediscovery event to worker thread */
3193 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
3194 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003195 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04003196 "2776 FCF rediscover quiescent timer expired\n");
James Smartecfd03c2010-02-12 14:41:27 -05003197 /* wake up worker thread */
3198 lpfc_worker_wake_up(phba);
3199}
3200
3201/**
James Smartda0436e2009-05-22 14:51:39 -04003202 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
3203 * @phba: pointer to lpfc hba data structure.
3204 * @acqe_link: pointer to the async link completion queue entry.
3205 *
3206 * This routine is to parse the SLI4 link-attention link fault code and
3207 * translate it into the base driver's read link attention mailbox command
3208 * status.
3209 *
3210 * Return: Link-attention status in terms of base driver's coding.
3211 **/
3212static uint16_t
3213lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
3214 struct lpfc_acqe_link *acqe_link)
3215{
3216 uint16_t latt_fault;
3217
3218 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
3219 case LPFC_ASYNC_LINK_FAULT_NONE:
3220 case LPFC_ASYNC_LINK_FAULT_LOCAL:
3221 case LPFC_ASYNC_LINK_FAULT_REMOTE:
3222 latt_fault = 0;
3223 break;
3224 default:
3225 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3226 "0398 Invalid link fault code: x%x\n",
3227 bf_get(lpfc_acqe_link_fault, acqe_link));
3228 latt_fault = MBXERR_ERROR;
3229 break;
3230 }
3231 return latt_fault;
3232}
3233
3234/**
3235 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
3236 * @phba: pointer to lpfc hba data structure.
3237 * @acqe_link: pointer to the async link completion queue entry.
3238 *
3239 * This routine is to parse the SLI4 link attention type and translate it
3240 * into the base driver's link attention type coding.
3241 *
3242 * Return: Link attention type in terms of base driver's coding.
3243 **/
3244static uint8_t
3245lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
3246 struct lpfc_acqe_link *acqe_link)
3247{
3248 uint8_t att_type;
3249
3250 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
3251 case LPFC_ASYNC_LINK_STATUS_DOWN:
3252 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
James Smart76a95d72010-11-20 23:11:48 -05003253 att_type = LPFC_ATT_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04003254 break;
3255 case LPFC_ASYNC_LINK_STATUS_UP:
3256 /* Ignore physical link up events - wait for logical link up */
James Smart76a95d72010-11-20 23:11:48 -05003257 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003258 break;
3259 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
James Smart76a95d72010-11-20 23:11:48 -05003260 att_type = LPFC_ATT_LINK_UP;
James Smartda0436e2009-05-22 14:51:39 -04003261 break;
3262 default:
3263 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3264 "0399 Invalid link attention type: x%x\n",
3265 bf_get(lpfc_acqe_link_status, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003266 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003267 break;
3268 }
3269 return att_type;
3270}
3271
3272/**
3273 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
3274 * @phba: pointer to lpfc hba data structure.
3275 * @acqe_link: pointer to the async link completion queue entry.
3276 *
3277 * This routine is to parse the SLI4 link-attention link speed and translate
3278 * it into the base driver's link-attention link speed coding.
3279 *
3280 * Return: Link-attention link speed in terms of base driver's coding.
3281 **/
3282static uint8_t
3283lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
3284 struct lpfc_acqe_link *acqe_link)
3285{
3286 uint8_t link_speed;
3287
3288 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
3289 case LPFC_ASYNC_LINK_SPEED_ZERO:
James Smartda0436e2009-05-22 14:51:39 -04003290 case LPFC_ASYNC_LINK_SPEED_10MBPS:
James Smartda0436e2009-05-22 14:51:39 -04003291 case LPFC_ASYNC_LINK_SPEED_100MBPS:
James Smart76a95d72010-11-20 23:11:48 -05003292 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003293 break;
3294 case LPFC_ASYNC_LINK_SPEED_1GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003295 link_speed = LPFC_LINK_SPEED_1GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003296 break;
3297 case LPFC_ASYNC_LINK_SPEED_10GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003298 link_speed = LPFC_LINK_SPEED_10GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003299 break;
3300 default:
3301 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3302 "0483 Invalid link-attention link speed: x%x\n",
3303 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003304 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003305 break;
3306 }
3307 return link_speed;
3308}
3309
3310/**
James Smart70f3c072010-12-15 17:57:33 -05003311 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
James Smartda0436e2009-05-22 14:51:39 -04003312 * @phba: pointer to lpfc hba data structure.
3313 * @acqe_link: pointer to the async link completion queue entry.
3314 *
James Smart70f3c072010-12-15 17:57:33 -05003315 * This routine is to handle the SLI4 asynchronous FCoE link event.
James Smartda0436e2009-05-22 14:51:39 -04003316 **/
3317static void
3318lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3319 struct lpfc_acqe_link *acqe_link)
3320{
3321 struct lpfc_dmabuf *mp;
3322 LPFC_MBOXQ_t *pmb;
3323 MAILBOX_t *mb;
James Smart76a95d72010-11-20 23:11:48 -05003324 struct lpfc_mbx_read_top *la;
James Smartda0436e2009-05-22 14:51:39 -04003325 uint8_t att_type;
James Smart76a95d72010-11-20 23:11:48 -05003326 int rc;
James Smartda0436e2009-05-22 14:51:39 -04003327
3328 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05003329 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
James Smartda0436e2009-05-22 14:51:39 -04003330 return;
James Smart32b97932009-07-19 10:01:21 -04003331 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003332 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3333 if (!pmb) {
3334 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3335 "0395 The mboxq allocation failed\n");
3336 return;
3337 }
3338 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3339 if (!mp) {
3340 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3341 "0396 The lpfc_dmabuf allocation failed\n");
3342 goto out_free_pmb;
3343 }
3344 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3345 if (!mp->virt) {
3346 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3347 "0397 The mbuf allocation failed\n");
3348 goto out_free_dmabuf;
3349 }
3350
3351 /* Cleanup any outstanding ELS commands */
3352 lpfc_els_flush_all_cmd(phba);
3353
3354 /* Block ELS IOCBs until we have done process link event */
3355 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3356
3357 /* Update link event statistics */
3358 phba->sli.slistat.link_event++;
3359
James Smart76a95d72010-11-20 23:11:48 -05003360 /* Create lpfc_handle_latt mailbox command from link ACQE */
3361 lpfc_read_topology(phba, pmb, mp);
3362 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smartda0436e2009-05-22 14:51:39 -04003363 pmb->vport = phba->pport;
3364
James Smartda0436e2009-05-22 14:51:39 -04003365 /* Keep the link status for extra SLI4 state machine reference */
3366 phba->sli4_hba.link_state.speed =
3367 bf_get(lpfc_acqe_link_speed, acqe_link);
3368 phba->sli4_hba.link_state.duplex =
3369 bf_get(lpfc_acqe_link_duplex, acqe_link);
3370 phba->sli4_hba.link_state.status =
3371 bf_get(lpfc_acqe_link_status, acqe_link);
James Smart70f3c072010-12-15 17:57:33 -05003372 phba->sli4_hba.link_state.type =
3373 bf_get(lpfc_acqe_link_type, acqe_link);
3374 phba->sli4_hba.link_state.number =
3375 bf_get(lpfc_acqe_link_number, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04003376 phba->sli4_hba.link_state.fault =
3377 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05003378 phba->sli4_hba.link_state.logical_speed =
James Smart70f3c072010-12-15 17:57:33 -05003379 bf_get(lpfc_acqe_logical_link_speed, acqe_link);
3380 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartc31098c2011-04-16 11:03:33 -04003381 "2900 Async FC/FCoE Link event - Speed:%dGBit "
3382 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
3383 "Logical speed:%dMbps Fault:%d\n",
James Smart70f3c072010-12-15 17:57:33 -05003384 phba->sli4_hba.link_state.speed,
3385 phba->sli4_hba.link_state.topology,
3386 phba->sli4_hba.link_state.status,
3387 phba->sli4_hba.link_state.type,
3388 phba->sli4_hba.link_state.number,
3389 phba->sli4_hba.link_state.logical_speed * 10,
3390 phba->sli4_hba.link_state.fault);
James Smart76a95d72010-11-20 23:11:48 -05003391 /*
3392 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
3393 * topology info. Note: Optional for non FC-AL ports.
3394 */
3395 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3396 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3397 if (rc == MBX_NOT_FINISHED)
3398 goto out_free_dmabuf;
3399 return;
3400 }
3401 /*
3402 * For FCoE Mode: fill in all the topology information we need and call
3403 * the READ_TOPOLOGY completion routine to continue without actually
3404 * sending the READ_TOPOLOGY mailbox command to the port.
3405 */
3406 /* Parse and translate status field */
3407 mb = &pmb->u.mb;
3408 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
3409
3410 /* Parse and translate link attention fields */
3411 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
3412 la->eventTag = acqe_link->event_tag;
3413 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
3414 bf_set(lpfc_mbx_read_top_link_spd, la,
3415 lpfc_sli4_parse_latt_link_speed(phba, acqe_link));
3416
3417 /* Fake the the following irrelvant fields */
3418 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
3419 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
3420 bf_set(lpfc_mbx_read_top_il, la, 0);
3421 bf_set(lpfc_mbx_read_top_pb, la, 0);
3422 bf_set(lpfc_mbx_read_top_fa, la, 0);
3423 bf_set(lpfc_mbx_read_top_mm, la, 0);
James Smartda0436e2009-05-22 14:51:39 -04003424
3425 /* Invoke the lpfc_handle_latt mailbox command callback function */
James Smart76a95d72010-11-20 23:11:48 -05003426 lpfc_mbx_cmpl_read_topology(phba, pmb);
James Smartda0436e2009-05-22 14:51:39 -04003427
3428 return;
3429
3430out_free_dmabuf:
3431 kfree(mp);
3432out_free_pmb:
3433 mempool_free(pmb, phba->mbox_mem_pool);
3434}
3435
3436/**
James Smart70f3c072010-12-15 17:57:33 -05003437 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
3438 * @phba: pointer to lpfc hba data structure.
3439 * @acqe_fc: pointer to the async fc completion queue entry.
3440 *
3441 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
3442 * that the event was received and then issue a read_topology mailbox command so
3443 * that the rest of the driver will treat it the same as SLI3.
3444 **/
3445static void
3446lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
3447{
3448 struct lpfc_dmabuf *mp;
3449 LPFC_MBOXQ_t *pmb;
3450 int rc;
3451
3452 if (bf_get(lpfc_trailer_type, acqe_fc) !=
3453 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
3454 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3455 "2895 Non FC link Event detected.(%d)\n",
3456 bf_get(lpfc_trailer_type, acqe_fc));
3457 return;
3458 }
3459 /* Keep the link status for extra SLI4 state machine reference */
3460 phba->sli4_hba.link_state.speed =
3461 bf_get(lpfc_acqe_fc_la_speed, acqe_fc);
3462 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
3463 phba->sli4_hba.link_state.topology =
3464 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
3465 phba->sli4_hba.link_state.status =
3466 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
3467 phba->sli4_hba.link_state.type =
3468 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
3469 phba->sli4_hba.link_state.number =
3470 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
3471 phba->sli4_hba.link_state.fault =
3472 bf_get(lpfc_acqe_link_fault, acqe_fc);
3473 phba->sli4_hba.link_state.logical_speed =
3474 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc);
3475 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3476 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
3477 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
3478 "%dMbps Fault:%d\n",
3479 phba->sli4_hba.link_state.speed,
3480 phba->sli4_hba.link_state.topology,
3481 phba->sli4_hba.link_state.status,
3482 phba->sli4_hba.link_state.type,
3483 phba->sli4_hba.link_state.number,
3484 phba->sli4_hba.link_state.logical_speed * 10,
3485 phba->sli4_hba.link_state.fault);
3486 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3487 if (!pmb) {
3488 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3489 "2897 The mboxq allocation failed\n");
3490 return;
3491 }
3492 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3493 if (!mp) {
3494 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3495 "2898 The lpfc_dmabuf allocation failed\n");
3496 goto out_free_pmb;
3497 }
3498 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3499 if (!mp->virt) {
3500 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3501 "2899 The mbuf allocation failed\n");
3502 goto out_free_dmabuf;
3503 }
3504
3505 /* Cleanup any outstanding ELS commands */
3506 lpfc_els_flush_all_cmd(phba);
3507
3508 /* Block ELS IOCBs until we have done process link event */
3509 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3510
3511 /* Update link event statistics */
3512 phba->sli.slistat.link_event++;
3513
3514 /* Create lpfc_handle_latt mailbox command from link ACQE */
3515 lpfc_read_topology(phba, pmb, mp);
3516 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
3517 pmb->vport = phba->pport;
3518
3519 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3520 if (rc == MBX_NOT_FINISHED)
3521 goto out_free_dmabuf;
3522 return;
3523
3524out_free_dmabuf:
3525 kfree(mp);
3526out_free_pmb:
3527 mempool_free(pmb, phba->mbox_mem_pool);
3528}
3529
3530/**
3531 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
3532 * @phba: pointer to lpfc hba data structure.
3533 * @acqe_fc: pointer to the async SLI completion queue entry.
3534 *
3535 * This routine is to handle the SLI4 asynchronous SLI events.
3536 **/
3537static void
3538lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
3539{
3540 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3541 "2901 Async SLI event - Event Data1:x%08x Event Data2:"
3542 "x%08x SLI Event Type:%d",
3543 acqe_sli->event_data1, acqe_sli->event_data2,
3544 bf_get(lpfc_trailer_type, acqe_sli));
3545 return;
3546}
3547
3548/**
James Smartfc2b9892010-02-26 14:15:29 -05003549 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
3550 * @vport: pointer to vport data structure.
3551 *
3552 * This routine is to perform Clear Virtual Link (CVL) on a vport in
3553 * response to a CVL event.
3554 *
3555 * Return the pointer to the ndlp with the vport if successful, otherwise
3556 * return NULL.
3557 **/
3558static struct lpfc_nodelist *
3559lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
3560{
3561 struct lpfc_nodelist *ndlp;
3562 struct Scsi_Host *shost;
3563 struct lpfc_hba *phba;
3564
3565 if (!vport)
3566 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05003567 phba = vport->phba;
3568 if (!phba)
3569 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04003570 ndlp = lpfc_findnode_did(vport, Fabric_DID);
3571 if (!ndlp) {
3572 /* Cannot find existing Fabric ndlp, so allocate a new one */
3573 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
3574 if (!ndlp)
3575 return 0;
3576 lpfc_nlp_init(vport, ndlp, Fabric_DID);
3577 /* Set the node type */
3578 ndlp->nlp_type |= NLP_FABRIC;
3579 /* Put ndlp onto node list */
3580 lpfc_enqueue_node(vport, ndlp);
3581 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
3582 /* re-setup ndlp without removing from node list */
3583 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
3584 if (!ndlp)
3585 return 0;
3586 }
James Smart63e801c2010-11-20 23:14:19 -05003587 if ((phba->pport->port_state < LPFC_FLOGI) &&
3588 (phba->pport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05003589 return NULL;
3590 /* If virtual link is not yet instantiated ignore CVL */
James Smart63e801c2010-11-20 23:14:19 -05003591 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
3592 && (vport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05003593 return NULL;
3594 shost = lpfc_shost_from_vport(vport);
3595 if (!shost)
3596 return NULL;
3597 lpfc_linkdown_port(vport);
3598 lpfc_cleanup_pending_mbox(vport);
3599 spin_lock_irq(shost->host_lock);
3600 vport->fc_flag |= FC_VPORT_CVL_RCVD;
3601 spin_unlock_irq(shost->host_lock);
3602
3603 return ndlp;
3604}
3605
3606/**
3607 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
3608 * @vport: pointer to lpfc hba data structure.
3609 *
3610 * This routine is to perform Clear Virtual Link (CVL) on all vports in
3611 * response to a FCF dead event.
3612 **/
3613static void
3614lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
3615{
3616 struct lpfc_vport **vports;
3617 int i;
3618
3619 vports = lpfc_create_vport_work_array(phba);
3620 if (vports)
3621 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
3622 lpfc_sli4_perform_vport_cvl(vports[i]);
3623 lpfc_destroy_vport_work_array(phba, vports);
3624}
3625
3626/**
James Smart76a95d72010-11-20 23:11:48 -05003627 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
James Smartda0436e2009-05-22 14:51:39 -04003628 * @phba: pointer to lpfc hba data structure.
3629 * @acqe_link: pointer to the async fcoe completion queue entry.
3630 *
3631 * This routine is to handle the SLI4 asynchronous fcoe event.
3632 **/
3633static void
James Smart76a95d72010-11-20 23:11:48 -05003634lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
James Smart70f3c072010-12-15 17:57:33 -05003635 struct lpfc_acqe_fip *acqe_fip)
James Smartda0436e2009-05-22 14:51:39 -04003636{
James Smart70f3c072010-12-15 17:57:33 -05003637 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04003638 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04003639 struct lpfc_vport *vport;
3640 struct lpfc_nodelist *ndlp;
3641 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05003642 int active_vlink_present;
3643 struct lpfc_vport **vports;
3644 int i;
James Smartda0436e2009-05-22 14:51:39 -04003645
James Smart70f3c072010-12-15 17:57:33 -05003646 phba->fc_eventTag = acqe_fip->event_tag;
3647 phba->fcoe_eventtag = acqe_fip->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003648 switch (event_type) {
James Smart70f3c072010-12-15 17:57:33 -05003649 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
3650 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
3651 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
James Smart999d8132010-03-15 11:24:56 -04003652 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3653 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003654 "2546 New FCF event, evt_tag:x%x, "
3655 "index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05003656 acqe_fip->event_tag,
3657 acqe_fip->index);
James Smart999d8132010-03-15 11:24:56 -04003658 else
3659 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
3660 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003661 "2788 FCF param modified event, "
3662 "evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05003663 acqe_fip->event_tag,
3664 acqe_fip->index);
James Smart38b92ef2010-08-04 16:11:39 -04003665 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smart0c9ab6f2010-02-26 14:15:57 -05003666 /*
3667 * During period of FCF discovery, read the FCF
3668 * table record indexed by the event to update
James Smarta93ff372010-10-22 11:06:08 -04003669 * FCF roundrobin failover eligible FCF bmask.
James Smart0c9ab6f2010-02-26 14:15:57 -05003670 */
3671 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
3672 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003673 "2779 Read FCF (x%x) for updating "
3674 "roundrobin FCF failover bmask\n",
James Smart70f3c072010-12-15 17:57:33 -05003675 acqe_fip->index);
3676 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05003677 }
James Smart38b92ef2010-08-04 16:11:39 -04003678
3679 /* If the FCF discovery is in progress, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04003680 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04003681 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart38b92ef2010-08-04 16:11:39 -04003682 spin_unlock_irq(&phba->hbalock);
3683 break;
3684 }
3685 /* If fast FCF failover rescan event is pending, do nothing */
3686 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
3687 spin_unlock_irq(&phba->hbalock);
3688 break;
3689 }
3690
3691 /* If the FCF has been in discovered state, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04003692 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
3693 spin_unlock_irq(&phba->hbalock);
3694 break;
3695 }
3696 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04003697
James Smart0c9ab6f2010-02-26 14:15:57 -05003698 /* Otherwise, scan the entire FCF table and re-discover SAN */
3699 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003700 "2770 Start FCF table scan per async FCF "
3701 "event, evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05003702 acqe_fip->event_tag, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05003703 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3704 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04003705 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05003706 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3707 "2547 Issue FCF scan read FCF mailbox "
James Smarta93ff372010-10-22 11:06:08 -04003708 "command failed (x%x)\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04003709 break;
3710
James Smart70f3c072010-12-15 17:57:33 -05003711 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
James Smartda0436e2009-05-22 14:51:39 -04003712 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04003713 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05003714 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
3715 acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04003716 break;
3717
James Smart70f3c072010-12-15 17:57:33 -05003718 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
James Smart0c9ab6f2010-02-26 14:15:57 -05003719 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003720 "2549 FCF (x%x) disconnected from network, "
James Smart70f3c072010-12-15 17:57:33 -05003721 "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart38b92ef2010-08-04 16:11:39 -04003722 /*
3723 * If we are in the middle of FCF failover process, clear
3724 * the corresponding FCF bit in the roundrobin bitmap.
James Smartda0436e2009-05-22 14:51:39 -04003725 */
James Smartfc2b9892010-02-26 14:15:29 -05003726 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003727 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05003728 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003729 /* Update FLOGI FCF failover eligible FCF bmask */
James Smart70f3c072010-12-15 17:57:33 -05003730 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05003731 break;
3732 }
James Smart38b92ef2010-08-04 16:11:39 -04003733 spin_unlock_irq(&phba->hbalock);
3734
3735 /* If the event is not for currently used fcf do nothing */
James Smart70f3c072010-12-15 17:57:33 -05003736 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
James Smart38b92ef2010-08-04 16:11:39 -04003737 break;
3738
3739 /*
3740 * Otherwise, request the port to rediscover the entire FCF
3741 * table for a fast recovery from case that the current FCF
3742 * is no longer valid as we are not in the middle of FCF
3743 * failover process already.
3744 */
3745 spin_lock_irq(&phba->hbalock);
James Smartfc2b9892010-02-26 14:15:29 -05003746 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05003747 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003748 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04003749
James Smart0c9ab6f2010-02-26 14:15:57 -05003750 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3751 "2771 Start FCF fast failover process due to "
3752 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
James Smart70f3c072010-12-15 17:57:33 -05003753 "\n", acqe_fip->event_tag, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05003754 rc = lpfc_sli4_redisc_fcf_table(phba);
3755 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05003756 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3757 LOG_DISCOVERY,
3758 "2772 Issue FCF rediscover mabilbox "
3759 "command failed, fail through to FCF "
3760 "dead event\n");
James Smartfc2b9892010-02-26 14:15:29 -05003761 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003762 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003763 spin_unlock_irq(&phba->hbalock);
3764 /*
3765 * Last resort will fail over by treating this
3766 * as a link down to FCF registration.
3767 */
3768 lpfc_sli4_fcf_dead_failthrough(phba);
James Smart38b92ef2010-08-04 16:11:39 -04003769 } else {
3770 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04003771 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart38b92ef2010-08-04 16:11:39 -04003772 /*
3773 * Handling fast FCF failover to a DEAD FCF event is
3774 * considered equalivant to receiving CVL to all vports.
James Smartfc2b9892010-02-26 14:15:29 -05003775 */
3776 lpfc_sli4_perform_all_vport_cvl(phba);
James Smart38b92ef2010-08-04 16:11:39 -04003777 }
James Smartda0436e2009-05-22 14:51:39 -04003778 break;
James Smart70f3c072010-12-15 17:57:33 -05003779 case LPFC_FIP_EVENT_TYPE_CVL:
James Smart0c9ab6f2010-02-26 14:15:57 -05003780 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6669f9b2009-10-02 15:16:45 -04003781 "2718 Clear Virtual Link Received for VPI 0x%x"
James Smart70f3c072010-12-15 17:57:33 -05003782 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart6d368e52011-05-24 11:44:12 -04003783
James Smart6669f9b2009-10-02 15:16:45 -04003784 vport = lpfc_find_vport_by_vpid(phba,
James Smart5248a742011-07-22 18:37:06 -04003785 acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05003786 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04003787 if (!ndlp)
3788 break;
James Smart695a8142010-01-26 23:08:03 -05003789 active_vlink_present = 0;
3790
3791 vports = lpfc_create_vport_work_array(phba);
3792 if (vports) {
3793 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
3794 i++) {
3795 if ((!(vports[i]->fc_flag &
3796 FC_VPORT_CVL_RCVD)) &&
3797 (vports[i]->port_state > LPFC_FDISC)) {
3798 active_vlink_present = 1;
3799 break;
3800 }
3801 }
3802 lpfc_destroy_vport_work_array(phba, vports);
3803 }
3804
3805 if (active_vlink_present) {
3806 /*
3807 * If there are other active VLinks present,
3808 * re-instantiate the Vlink using FDISC.
3809 */
James Smart6669f9b2009-10-02 15:16:45 -04003810 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
James Smartfc2b9892010-02-26 14:15:29 -05003811 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04003812 spin_lock_irq(shost->host_lock);
3813 ndlp->nlp_flag |= NLP_DELAY_TMO;
3814 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05003815 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
3816 vport->port_state = LPFC_FDISC;
3817 } else {
James Smartecfd03c2010-02-12 14:41:27 -05003818 /*
3819 * Otherwise, we request port to rediscover
3820 * the entire FCF table for a fast recovery
3821 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05003822 * is no longer valid if we are not already
3823 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05003824 */
James Smartfc2b9892010-02-26 14:15:29 -05003825 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003826 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05003827 spin_unlock_irq(&phba->hbalock);
3828 break;
3829 }
3830 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05003831 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003832 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003833 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
3834 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003835 "2773 Start FCF failover per CVL, "
James Smart70f3c072010-12-15 17:57:33 -05003836 "evt_tag:x%x\n", acqe_fip->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05003837 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05003838 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05003839 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3840 LOG_DISCOVERY,
3841 "2774 Issue FCF rediscover "
3842 "mabilbox command failed, "
3843 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05003844 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003845 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003846 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05003847 /*
3848 * Last resort will be re-try on the
3849 * the current registered FCF entry.
3850 */
3851 lpfc_retry_pport_discovery(phba);
James Smart38b92ef2010-08-04 16:11:39 -04003852 } else
3853 /*
3854 * Reset FCF roundrobin bmask for new
3855 * discovery.
3856 */
James Smart7d791df2011-07-22 18:37:52 -04003857 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart6669f9b2009-10-02 15:16:45 -04003858 }
3859 break;
James Smartda0436e2009-05-22 14:51:39 -04003860 default:
3861 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3862 "0288 Unknown FCoE event type 0x%x event tag "
James Smart70f3c072010-12-15 17:57:33 -05003863 "0x%x\n", event_type, acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04003864 break;
3865 }
3866}
3867
3868/**
3869 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
3870 * @phba: pointer to lpfc hba data structure.
3871 * @acqe_link: pointer to the async dcbx completion queue entry.
3872 *
3873 * This routine is to handle the SLI4 asynchronous dcbx event.
3874 **/
3875static void
3876lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
3877 struct lpfc_acqe_dcbx *acqe_dcbx)
3878{
James Smart4d9ab992009-10-02 15:16:39 -04003879 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003880 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3881 "0290 The SLI4 DCBX asynchronous event is not "
3882 "handled yet\n");
3883}
3884
3885/**
James Smartb19a0612010-04-06 14:48:51 -04003886 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
3887 * @phba: pointer to lpfc hba data structure.
3888 * @acqe_link: pointer to the async grp5 completion queue entry.
3889 *
3890 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
3891 * is an asynchronous notified of a logical link speed change. The Port
3892 * reports the logical link speed in units of 10Mbps.
3893 **/
3894static void
3895lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
3896 struct lpfc_acqe_grp5 *acqe_grp5)
3897{
3898 uint16_t prev_ll_spd;
3899
3900 phba->fc_eventTag = acqe_grp5->event_tag;
3901 phba->fcoe_eventtag = acqe_grp5->event_tag;
3902 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
3903 phba->sli4_hba.link_state.logical_speed =
3904 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5));
3905 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3906 "2789 GRP5 Async Event: Updating logical link speed "
3907 "from %dMbps to %dMbps\n", (prev_ll_spd * 10),
3908 (phba->sli4_hba.link_state.logical_speed*10));
3909}
3910
3911/**
James Smartda0436e2009-05-22 14:51:39 -04003912 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
3913 * @phba: pointer to lpfc hba data structure.
3914 *
3915 * This routine is invoked by the worker thread to process all the pending
3916 * SLI4 asynchronous events.
3917 **/
3918void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
3919{
3920 struct lpfc_cq_event *cq_event;
3921
3922 /* First, declare the async event has been handled */
3923 spin_lock_irq(&phba->hbalock);
3924 phba->hba_flag &= ~ASYNC_EVENT;
3925 spin_unlock_irq(&phba->hbalock);
3926 /* Now, handle all the async events */
3927 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
3928 /* Get the first event from the head of the event queue */
3929 spin_lock_irq(&phba->hbalock);
3930 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
3931 cq_event, struct lpfc_cq_event, list);
3932 spin_unlock_irq(&phba->hbalock);
3933 /* Process the asynchronous event */
3934 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
3935 case LPFC_TRAILER_CODE_LINK:
3936 lpfc_sli4_async_link_evt(phba,
3937 &cq_event->cqe.acqe_link);
3938 break;
3939 case LPFC_TRAILER_CODE_FCOE:
James Smart70f3c072010-12-15 17:57:33 -05003940 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04003941 break;
3942 case LPFC_TRAILER_CODE_DCBX:
3943 lpfc_sli4_async_dcbx_evt(phba,
3944 &cq_event->cqe.acqe_dcbx);
3945 break;
James Smartb19a0612010-04-06 14:48:51 -04003946 case LPFC_TRAILER_CODE_GRP5:
3947 lpfc_sli4_async_grp5_evt(phba,
3948 &cq_event->cqe.acqe_grp5);
3949 break;
James Smart70f3c072010-12-15 17:57:33 -05003950 case LPFC_TRAILER_CODE_FC:
3951 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
3952 break;
3953 case LPFC_TRAILER_CODE_SLI:
3954 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
3955 break;
James Smartda0436e2009-05-22 14:51:39 -04003956 default:
3957 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3958 "1804 Invalid asynchrous event code: "
3959 "x%x\n", bf_get(lpfc_trailer_code,
3960 &cq_event->cqe.mcqe_cmpl));
3961 break;
3962 }
3963 /* Free the completion event processed to the free pool */
3964 lpfc_sli4_cq_event_release(phba, cq_event);
3965 }
3966}
3967
3968/**
James Smartecfd03c2010-02-12 14:41:27 -05003969 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
3970 * @phba: pointer to lpfc hba data structure.
3971 *
3972 * This routine is invoked by the worker thread to process FCF table
3973 * rediscovery pending completion event.
3974 **/
3975void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
3976{
3977 int rc;
3978
3979 spin_lock_irq(&phba->hbalock);
3980 /* Clear FCF rediscovery timeout event */
3981 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
3982 /* Clear driver fast failover FCF record flag */
3983 phba->fcf.failover_rec.flag = 0;
3984 /* Set state for FCF fast failover */
3985 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
3986 spin_unlock_irq(&phba->hbalock);
3987
3988 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05003989 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003990 "2777 Start post-quiescent FCF table scan\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05003991 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05003992 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05003993 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3994 "2747 Issue FCF scan read FCF mailbox "
3995 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05003996}
3997
3998/**
James Smartda0436e2009-05-22 14:51:39 -04003999 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
4000 * @phba: pointer to lpfc hba data structure.
4001 * @dev_grp: The HBA PCI-Device group number.
4002 *
4003 * This routine is invoked to set up the per HBA PCI-Device group function
4004 * API jump table entries.
4005 *
4006 * Return: 0 if success, otherwise -ENODEV
4007 **/
4008int
4009lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05004010{
4011 int rc;
4012
James Smartda0436e2009-05-22 14:51:39 -04004013 /* Set up lpfc PCI-device group */
4014 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05004015
James Smartda0436e2009-05-22 14:51:39 -04004016 /* The LPFC_PCI_DEV_OC uses SLI4 */
4017 if (dev_grp == LPFC_PCI_DEV_OC)
4018 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05004019
James Smartda0436e2009-05-22 14:51:39 -04004020 /* Set up device INIT API function jump table */
4021 rc = lpfc_init_api_table_setup(phba, dev_grp);
4022 if (rc)
4023 return -ENODEV;
4024 /* Set up SCSI API function jump table */
4025 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
4026 if (rc)
4027 return -ENODEV;
4028 /* Set up SLI API function jump table */
4029 rc = lpfc_sli_api_table_setup(phba, dev_grp);
4030 if (rc)
4031 return -ENODEV;
4032 /* Set up MBOX API function jump table */
4033 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
4034 if (rc)
4035 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004036
James Smartda0436e2009-05-22 14:51:39 -04004037 return 0;
James Smart5b75da22008-12-04 22:39:35 -05004038}
4039
4040/**
James Smart3621a712009-04-06 18:47:14 -04004041 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05004042 * @phba: pointer to lpfc hba data structure.
4043 * @intr_mode: active interrupt mode adopted.
4044 *
4045 * This routine it invoked to log the currently used active interrupt mode
4046 * to the device.
James Smart3772a992009-05-22 14:50:54 -04004047 **/
4048static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05004049{
4050 switch (intr_mode) {
4051 case 0:
4052 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4053 "0470 Enable INTx interrupt mode.\n");
4054 break;
4055 case 1:
4056 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4057 "0481 Enabled MSI interrupt mode.\n");
4058 break;
4059 case 2:
4060 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4061 "0480 Enabled MSI-X interrupt mode.\n");
4062 break;
4063 default:
4064 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4065 "0482 Illegal interrupt mode.\n");
4066 break;
4067 }
4068 return;
4069}
4070
James Smart5b75da22008-12-04 22:39:35 -05004071/**
James Smart3772a992009-05-22 14:50:54 -04004072 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05004073 * @phba: pointer to lpfc hba data structure.
4074 *
James Smart3772a992009-05-22 14:50:54 -04004075 * This routine is invoked to enable the PCI device that is common to all
4076 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004077 *
4078 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004079 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004080 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05004081 **/
James Smart3772a992009-05-22 14:50:54 -04004082static int
4083lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004084{
James Smart3772a992009-05-22 14:50:54 -04004085 struct pci_dev *pdev;
James Smart079b5c92011-08-21 21:48:49 -04004086 int bars = 0;
James Smart5b75da22008-12-04 22:39:35 -05004087
James Smart3772a992009-05-22 14:50:54 -04004088 /* Obtain PCI device reference */
4089 if (!phba->pcidev)
4090 goto out_error;
4091 else
4092 pdev = phba->pcidev;
4093 /* Select PCI BARs */
4094 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4095 /* Enable PCI device */
4096 if (pci_enable_device_mem(pdev))
4097 goto out_error;
4098 /* Request PCI resource for the device */
4099 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
4100 goto out_disable_device;
4101 /* Set up device as PCI master and save state for EEH */
4102 pci_set_master(pdev);
4103 pci_try_set_mwi(pdev);
4104 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05004105
James Smart05580562011-05-24 11:40:48 -04004106 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
4107 if (pci_find_capability(pdev, PCI_CAP_ID_EXP))
4108 pdev->needs_freset = 1;
4109
James Smart3772a992009-05-22 14:50:54 -04004110 return 0;
James Smart5b75da22008-12-04 22:39:35 -05004111
James Smart3772a992009-05-22 14:50:54 -04004112out_disable_device:
4113 pci_disable_device(pdev);
4114out_error:
James Smart079b5c92011-08-21 21:48:49 -04004115 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4116 "1401 Failed to enable pci device, bars:x%x\n", bars);
James Smart3772a992009-05-22 14:50:54 -04004117 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004118}
4119
4120/**
James Smart3772a992009-05-22 14:50:54 -04004121 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05004122 * @phba: pointer to lpfc hba data structure.
4123 *
James Smart3772a992009-05-22 14:50:54 -04004124 * This routine is invoked to disable the PCI device that is common to all
4125 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004126 **/
4127static void
James Smart3772a992009-05-22 14:50:54 -04004128lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004129{
James Smart3772a992009-05-22 14:50:54 -04004130 struct pci_dev *pdev;
4131 int bars;
James Smart5b75da22008-12-04 22:39:35 -05004132
James Smart3772a992009-05-22 14:50:54 -04004133 /* Obtain PCI device reference */
4134 if (!phba->pcidev)
4135 return;
4136 else
4137 pdev = phba->pcidev;
4138 /* Select PCI BARs */
4139 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4140 /* Release PCI resource and disable PCI device */
4141 pci_release_selected_regions(pdev, bars);
4142 pci_disable_device(pdev);
4143 /* Null out PCI private reference to driver */
4144 pci_set_drvdata(pdev, NULL);
James Smart5b75da22008-12-04 22:39:35 -05004145
4146 return;
4147}
4148
4149/**
James Smart3772a992009-05-22 14:50:54 -04004150 * lpfc_reset_hba - Reset a hba
4151 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04004152 *
James Smart3772a992009-05-22 14:50:54 -04004153 * This routine is invoked to reset a hba device. It brings the HBA
4154 * offline, performs a board restart, and then brings the board back
4155 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
4156 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04004157 **/
James Smart3772a992009-05-22 14:50:54 -04004158void
4159lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004160{
James Smart3772a992009-05-22 14:50:54 -04004161 /* If resets are disabled then set error state and return. */
4162 if (!phba->cfg_enable_hba_reset) {
4163 phba->link_state = LPFC_HBA_ERROR;
4164 return;
4165 }
4166 lpfc_offline_prep(phba);
4167 lpfc_offline(phba);
4168 lpfc_sli_brdrestart(phba);
4169 lpfc_online(phba);
4170 lpfc_unblock_mgmt_io(phba);
4171}
dea31012005-04-17 16:05:31 -05004172
James Smart3772a992009-05-22 14:50:54 -04004173/**
James Smart0a96e972011-07-22 18:37:28 -04004174 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
4175 * @phba: pointer to lpfc hba data structure.
4176 *
4177 * This function enables the PCI SR-IOV virtual functions to a physical
4178 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4179 * enable the number of virtual functions to the physical function. As
4180 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4181 * API call does not considered as an error condition for most of the device.
4182 **/
4183uint16_t
4184lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
4185{
4186 struct pci_dev *pdev = phba->pcidev;
4187 uint16_t nr_virtfn;
4188 int pos;
4189
James Smart0a96e972011-07-22 18:37:28 -04004190 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
4191 if (pos == 0)
4192 return 0;
4193
4194 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
4195 return nr_virtfn;
4196}
4197
4198/**
James Smart912e3ac2011-05-24 11:42:11 -04004199 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
4200 * @phba: pointer to lpfc hba data structure.
4201 * @nr_vfn: number of virtual functions to be enabled.
4202 *
4203 * This function enables the PCI SR-IOV virtual functions to a physical
4204 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4205 * enable the number of virtual functions to the physical function. As
4206 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4207 * API call does not considered as an error condition for most of the device.
4208 **/
4209int
4210lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
4211{
4212 struct pci_dev *pdev = phba->pcidev;
James Smart0a96e972011-07-22 18:37:28 -04004213 uint16_t max_nr_vfn;
James Smart912e3ac2011-05-24 11:42:11 -04004214 int rc;
4215
James Smart0a96e972011-07-22 18:37:28 -04004216 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
4217 if (nr_vfn > max_nr_vfn) {
4218 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4219 "3057 Requested vfs (%d) greater than "
4220 "supported vfs (%d)", nr_vfn, max_nr_vfn);
4221 return -EINVAL;
4222 }
4223
James Smart912e3ac2011-05-24 11:42:11 -04004224 rc = pci_enable_sriov(pdev, nr_vfn);
4225 if (rc) {
4226 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4227 "2806 Failed to enable sriov on this device "
4228 "with vfn number nr_vf:%d, rc:%d\n",
4229 nr_vfn, rc);
4230 } else
4231 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4232 "2807 Successful enable sriov on this device "
4233 "with vfn number nr_vf:%d\n", nr_vfn);
4234 return rc;
4235}
4236
4237/**
James Smart3772a992009-05-22 14:50:54 -04004238 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
4239 * @phba: pointer to lpfc hba data structure.
4240 *
4241 * This routine is invoked to set up the driver internal resources specific to
4242 * support the SLI-3 HBA device it attached to.
4243 *
4244 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004245 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004246 * other values - error
4247 **/
4248static int
4249lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
4250{
4251 struct lpfc_sli *psli;
James Smart912e3ac2011-05-24 11:42:11 -04004252 int rc;
dea31012005-04-17 16:05:31 -05004253
James Smart3772a992009-05-22 14:50:54 -04004254 /*
4255 * Initialize timers used by driver
4256 */
dea31012005-04-17 16:05:31 -05004257
James Smart3772a992009-05-22 14:50:54 -04004258 /* Heartbeat timer */
4259 init_timer(&phba->hb_tmofunc);
4260 phba->hb_tmofunc.function = lpfc_hb_timeout;
4261 phba->hb_tmofunc.data = (unsigned long)phba;
4262
4263 psli = &phba->sli;
4264 /* MBOX heartbeat timer */
4265 init_timer(&psli->mbox_tmo);
4266 psli->mbox_tmo.function = lpfc_mbox_timeout;
4267 psli->mbox_tmo.data = (unsigned long) phba;
4268 /* FCP polling mode timer */
4269 init_timer(&phba->fcp_poll_timer);
4270 phba->fcp_poll_timer.function = lpfc_poll_timeout;
4271 phba->fcp_poll_timer.data = (unsigned long) phba;
4272 /* Fabric block timer */
4273 init_timer(&phba->fabric_block_timer);
4274 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4275 phba->fabric_block_timer.data = (unsigned long) phba;
4276 /* EA polling mode timer */
4277 init_timer(&phba->eratt_poll);
4278 phba->eratt_poll.function = lpfc_poll_eratt;
4279 phba->eratt_poll.data = (unsigned long) phba;
4280
4281 /* Host attention work mask setup */
4282 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
4283 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
4284
4285 /* Get all the module params for configuring this host */
4286 lpfc_get_cfgparam(phba);
James Smart49198b32010-04-06 15:04:33 -04004287 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
4288 phba->menlo_flag |= HBA_MENLO_SUPPORT;
4289 /* check for menlo minimum sg count */
4290 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
4291 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
4292 }
4293
James Smart3772a992009-05-22 14:50:54 -04004294 /*
4295 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
4296 * used to create the sg_dma_buf_pool must be dynamically calculated.
4297 * 2 segments are added since the IOCB needs a command and response bde.
4298 */
4299 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4300 sizeof(struct fcp_rsp) +
4301 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
4302
4303 if (phba->cfg_enable_bg) {
4304 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT;
4305 phba->cfg_sg_dma_buf_size +=
4306 phba->cfg_prot_sg_seg_cnt * sizeof(struct ulp_bde64);
4307 }
4308
4309 /* Also reinitialize the host templates with new values. */
4310 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4311 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4312
4313 phba->max_vpi = LPFC_MAX_VPI;
4314 /* This will be set to correct value after config_port mbox */
4315 phba->max_vports = 0;
4316
4317 /*
4318 * Initialize the SLI Layer to run with lpfc HBAs.
4319 */
4320 lpfc_sli_setup(phba);
4321 lpfc_sli_queue_setup(phba);
4322
4323 /* Allocate device driver memory */
4324 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
4325 return -ENOMEM;
4326
James Smart912e3ac2011-05-24 11:42:11 -04004327 /*
4328 * Enable sr-iov virtual functions if supported and configured
4329 * through the module parameter.
4330 */
4331 if (phba->cfg_sriov_nr_virtfn > 0) {
4332 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
4333 phba->cfg_sriov_nr_virtfn);
4334 if (rc) {
4335 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4336 "2808 Requested number of SR-IOV "
4337 "virtual functions (%d) is not "
4338 "supported\n",
4339 phba->cfg_sriov_nr_virtfn);
4340 phba->cfg_sriov_nr_virtfn = 0;
4341 }
4342 }
4343
James Smart3772a992009-05-22 14:50:54 -04004344 return 0;
4345}
4346
4347/**
4348 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
4349 * @phba: pointer to lpfc hba data structure.
4350 *
4351 * This routine is invoked to unset the driver internal resources set up
4352 * specific for supporting the SLI-3 HBA device it attached to.
4353 **/
4354static void
4355lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
4356{
4357 /* Free device driver memory allocated */
4358 lpfc_mem_free_all(phba);
4359
4360 return;
4361}
4362
4363/**
James Smartda0436e2009-05-22 14:51:39 -04004364 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
4365 * @phba: pointer to lpfc hba data structure.
4366 *
4367 * This routine is invoked to set up the driver internal resources specific to
4368 * support the SLI-4 HBA device it attached to.
4369 *
4370 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004371 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004372 * other values - error
4373 **/
4374static int
4375lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
4376{
4377 struct lpfc_sli *psli;
James Smart28baac72010-02-12 14:42:03 -05004378 LPFC_MBOXQ_t *mboxq;
4379 int rc, i, hbq_count, buf_size, dma_buf_size, max_buf_size;
4380 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
4381 struct lpfc_mqe *mqe;
James Smart085c6472010-11-20 23:11:37 -05004382 int longs, sli_family;
James Smartacd68592012-01-18 16:25:09 -05004383 int sges_per_segment;
James Smartda0436e2009-05-22 14:51:39 -04004384
4385 /* Before proceed, wait for POST done and device ready */
4386 rc = lpfc_sli4_post_status_check(phba);
4387 if (rc)
4388 return -ENODEV;
4389
4390 /*
4391 * Initialize timers used by driver
4392 */
4393
4394 /* Heartbeat timer */
4395 init_timer(&phba->hb_tmofunc);
4396 phba->hb_tmofunc.function = lpfc_hb_timeout;
4397 phba->hb_tmofunc.data = (unsigned long)phba;
James Smart19ca7602010-11-20 23:11:55 -05004398 init_timer(&phba->rrq_tmr);
4399 phba->rrq_tmr.function = lpfc_rrq_timeout;
4400 phba->rrq_tmr.data = (unsigned long)phba;
James Smartda0436e2009-05-22 14:51:39 -04004401
4402 psli = &phba->sli;
4403 /* MBOX heartbeat timer */
4404 init_timer(&psli->mbox_tmo);
4405 psli->mbox_tmo.function = lpfc_mbox_timeout;
4406 psli->mbox_tmo.data = (unsigned long) phba;
4407 /* Fabric block timer */
4408 init_timer(&phba->fabric_block_timer);
4409 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4410 phba->fabric_block_timer.data = (unsigned long) phba;
4411 /* EA polling mode timer */
4412 init_timer(&phba->eratt_poll);
4413 phba->eratt_poll.function = lpfc_poll_eratt;
4414 phba->eratt_poll.data = (unsigned long) phba;
James Smartecfd03c2010-02-12 14:41:27 -05004415 /* FCF rediscover timer */
4416 init_timer(&phba->fcf.redisc_wait);
4417 phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
4418 phba->fcf.redisc_wait.data = (unsigned long)phba;
4419
James Smartda0436e2009-05-22 14:51:39 -04004420 /*
James Smart7ad20aa2011-05-24 11:44:28 -04004421 * Control structure for handling external multi-buffer mailbox
4422 * command pass-through.
4423 */
4424 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
4425 sizeof(struct lpfc_mbox_ext_buf_ctx));
4426 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
4427
4428 /*
James Smartda0436e2009-05-22 14:51:39 -04004429 * We need to do a READ_CONFIG mailbox command here before
4430 * calling lpfc_get_cfgparam. For VFs this will report the
4431 * MAX_XRI, MAX_VPI, MAX_RPI, MAX_IOCB, and MAX_VFI settings.
4432 * All of the resources allocated
4433 * for this Port are tied to these values.
4434 */
4435 /* Get all the module params for configuring this host */
4436 lpfc_get_cfgparam(phba);
4437 phba->max_vpi = LPFC_MAX_VPI;
4438 /* This will be set to correct value after the read_config mbox */
4439 phba->max_vports = 0;
4440
4441 /* Program the default value of vlan_id and fc_map */
4442 phba->valid_vlan = 0;
4443 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4444 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4445 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4446
James Smartacd68592012-01-18 16:25:09 -05004447 /* With BlockGuard we can have multiple SGEs per Data Segemnt */
4448 sges_per_segment = 1;
4449 if (phba->cfg_enable_bg)
4450 sges_per_segment = 2;
4451
James Smartda0436e2009-05-22 14:51:39 -04004452 /*
4453 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
4454 * used to create the sg_dma_buf_pool must be dynamically calculated.
4455 * 2 segments are added since the IOCB needs a command and response bde.
4456 * To insure that the scsi sgl does not cross a 4k page boundary only
James Smart28baac72010-02-12 14:42:03 -05004457 * sgl sizes of must be a power of 2.
James Smartda0436e2009-05-22 14:51:39 -04004458 */
James Smart28baac72010-02-12 14:42:03 -05004459 buf_size = (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp) +
James Smartacd68592012-01-18 16:25:09 -05004460 (((phba->cfg_sg_seg_cnt * sges_per_segment) + 2) *
4461 sizeof(struct sli4_sge)));
James Smart085c6472010-11-20 23:11:37 -05004462
4463 sli_family = bf_get(lpfc_sli_intf_sli_family, &phba->sli4_hba.sli_intf);
4464 max_buf_size = LPFC_SLI4_MAX_BUF_SIZE;
4465 switch (sli_family) {
4466 case LPFC_SLI_INTF_FAMILY_BE2:
4467 case LPFC_SLI_INTF_FAMILY_BE3:
4468 /* There is a single hint for BE - 2 pages per BPL. */
4469 if (bf_get(lpfc_sli_intf_sli_hint1, &phba->sli4_hba.sli_intf) ==
4470 LPFC_SLI_INTF_SLI_HINT1_1)
4471 max_buf_size = LPFC_SLI4_FL1_MAX_BUF_SIZE;
4472 break;
4473 case LPFC_SLI_INTF_FAMILY_LNCR_A0:
4474 case LPFC_SLI_INTF_FAMILY_LNCR_B0:
4475 default:
4476 break;
4477 }
James Smartacd68592012-01-18 16:25:09 -05004478
James Smart28baac72010-02-12 14:42:03 -05004479 for (dma_buf_size = LPFC_SLI4_MIN_BUF_SIZE;
4480 dma_buf_size < max_buf_size && buf_size > dma_buf_size;
4481 dma_buf_size = dma_buf_size << 1)
4482 ;
4483 if (dma_buf_size == max_buf_size)
4484 phba->cfg_sg_seg_cnt = (dma_buf_size -
4485 sizeof(struct fcp_cmnd) - sizeof(struct fcp_rsp) -
4486 (2 * sizeof(struct sli4_sge))) /
4487 sizeof(struct sli4_sge);
4488 phba->cfg_sg_dma_buf_size = dma_buf_size;
James Smartda0436e2009-05-22 14:51:39 -04004489
4490 /* Initialize buffer queue management fields */
4491 hbq_count = lpfc_sli_hbq_count();
4492 for (i = 0; i < hbq_count; ++i)
4493 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
4494 INIT_LIST_HEAD(&phba->rb_pend_list);
4495 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
4496 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
4497
4498 /*
4499 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
4500 */
4501 /* Initialize the Abort scsi buffer list used by driver */
4502 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
4503 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
4504 /* This abort list used by worker thread */
4505 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
4506
4507 /*
James Smart6d368e52011-05-24 11:44:12 -04004508 * Initialize driver internal slow-path work queues
James Smartda0436e2009-05-22 14:51:39 -04004509 */
4510
4511 /* Driver internel slow-path CQ Event pool */
4512 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
4513 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04004514 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04004515 /* Asynchronous event CQ Event work queue list */
4516 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
4517 /* Fast-path XRI aborted CQ Event work queue list */
4518 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
4519 /* Slow-path XRI aborted CQ Event work queue list */
4520 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
4521 /* Receive queue CQ Event work queue list */
4522 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
4523
James Smart6d368e52011-05-24 11:44:12 -04004524 /* Initialize extent block lists. */
4525 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
4526 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
4527 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
4528 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
4529
James Smartda0436e2009-05-22 14:51:39 -04004530 /* Initialize the driver internal SLI layer lists. */
4531 lpfc_sli_setup(phba);
4532 lpfc_sli_queue_setup(phba);
4533
4534 /* Allocate device driver memory */
4535 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
4536 if (rc)
4537 return -ENOMEM;
4538
James Smart2fcee4b2010-12-15 17:57:46 -05004539 /* IF Type 2 ports get initialized now. */
4540 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
4541 LPFC_SLI_INTF_IF_TYPE_2) {
4542 rc = lpfc_pci_function_reset(phba);
4543 if (unlikely(rc))
4544 return -ENODEV;
4545 }
4546
James Smartda0436e2009-05-22 14:51:39 -04004547 /* Create the bootstrap mailbox command */
4548 rc = lpfc_create_bootstrap_mbox(phba);
4549 if (unlikely(rc))
4550 goto out_free_mem;
4551
4552 /* Set up the host's endian order with the device. */
4553 rc = lpfc_setup_endian_order(phba);
4554 if (unlikely(rc))
4555 goto out_free_bsmbx;
4556
4557 /* Set up the hba's configuration parameters. */
4558 rc = lpfc_sli4_read_config(phba);
4559 if (unlikely(rc))
4560 goto out_free_bsmbx;
4561
James Smart2fcee4b2010-12-15 17:57:46 -05004562 /* IF Type 0 ports get initialized now. */
4563 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
4564 LPFC_SLI_INTF_IF_TYPE_0) {
4565 rc = lpfc_pci_function_reset(phba);
4566 if (unlikely(rc))
4567 goto out_free_bsmbx;
4568 }
James Smartda0436e2009-05-22 14:51:39 -04004569
James Smartcb5172e2010-03-15 11:25:07 -04004570 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
4571 GFP_KERNEL);
4572 if (!mboxq) {
4573 rc = -ENOMEM;
4574 goto out_free_bsmbx;
4575 }
4576
James Smartfedd3b72011-02-16 12:39:24 -05004577 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
James Smartcb5172e2010-03-15 11:25:07 -04004578 lpfc_supported_pages(mboxq);
4579 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smartfedd3b72011-02-16 12:39:24 -05004580 if (!rc) {
4581 mqe = &mboxq->u.mqe;
4582 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
4583 LPFC_MAX_SUPPORTED_PAGES);
4584 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
4585 switch (pn_page[i]) {
4586 case LPFC_SLI4_PARAMETERS:
4587 phba->sli4_hba.pc_sli4_params.supported = 1;
4588 break;
4589 default:
4590 break;
4591 }
4592 }
4593 /* Read the port's SLI4 Parameters capabilities if supported. */
4594 if (phba->sli4_hba.pc_sli4_params.supported)
4595 rc = lpfc_pc_sli4_params_get(phba, mboxq);
4596 if (rc) {
4597 mempool_free(mboxq, phba->mbox_mem_pool);
4598 rc = -EIO;
4599 goto out_free_bsmbx;
James Smartcb5172e2010-03-15 11:25:07 -04004600 }
4601 }
James Smartfedd3b72011-02-16 12:39:24 -05004602 /*
4603 * Get sli4 parameters that override parameters from Port capabilities.
James Smart6d368e52011-05-24 11:44:12 -04004604 * If this call fails, it isn't critical unless the SLI4 parameters come
4605 * back in conflict.
James Smartfedd3b72011-02-16 12:39:24 -05004606 */
James Smart6d368e52011-05-24 11:44:12 -04004607 rc = lpfc_get_sli4_parameters(phba, mboxq);
4608 if (rc) {
4609 if (phba->sli4_hba.extents_in_use &&
4610 phba->sli4_hba.rpi_hdrs_in_use) {
4611 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4612 "2999 Unsupported SLI4 Parameters "
4613 "Extents and RPI headers enabled.\n");
4614 goto out_free_bsmbx;
4615 }
4616 }
James Smartcb5172e2010-03-15 11:25:07 -04004617 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart5350d872011-10-10 21:33:49 -04004618 /* Verify all the SLI4 queues */
4619 rc = lpfc_sli4_queue_verify(phba);
James Smartda0436e2009-05-22 14:51:39 -04004620 if (rc)
4621 goto out_free_bsmbx;
4622
4623 /* Create driver internal CQE event pool */
4624 rc = lpfc_sli4_cq_event_pool_create(phba);
4625 if (rc)
James Smart5350d872011-10-10 21:33:49 -04004626 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04004627
4628 /* Initialize and populate the iocb list per host */
4629 rc = lpfc_init_sgl_list(phba);
4630 if (rc) {
4631 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4632 "1400 Failed to initialize sgl list.\n");
4633 goto out_destroy_cq_event_pool;
4634 }
4635 rc = lpfc_init_active_sgl_array(phba);
4636 if (rc) {
4637 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4638 "1430 Failed to initialize sgl list.\n");
4639 goto out_free_sgl_list;
4640 }
James Smartda0436e2009-05-22 14:51:39 -04004641 rc = lpfc_sli4_init_rpi_hdrs(phba);
4642 if (rc) {
4643 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4644 "1432 Failed to initialize rpi headers.\n");
4645 goto out_free_active_sgl;
4646 }
4647
James Smarta93ff372010-10-22 11:06:08 -04004648 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
James Smart0c9ab6f2010-02-26 14:15:57 -05004649 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
4650 phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
4651 GFP_KERNEL);
4652 if (!phba->fcf.fcf_rr_bmask) {
4653 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4654 "2759 Failed allocate memory for FCF round "
4655 "robin failover bmask\n");
James Smart05580562011-05-24 11:40:48 -04004656 rc = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -05004657 goto out_remove_rpi_hdrs;
4658 }
4659
James Smart5350d872011-10-10 21:33:49 -04004660 /*
4661 * The cfg_fcp_eq_count can be zero whenever there is exactly one
4662 * interrupt vector. This is not an error
4663 */
4664 if (phba->cfg_fcp_eq_count) {
4665 phba->sli4_hba.fcp_eq_hdl =
4666 kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
James Smartda0436e2009-05-22 14:51:39 -04004667 phba->cfg_fcp_eq_count), GFP_KERNEL);
James Smart5350d872011-10-10 21:33:49 -04004668 if (!phba->sli4_hba.fcp_eq_hdl) {
4669 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4670 "2572 Failed allocate memory for "
4671 "fast-path per-EQ handle array\n");
4672 rc = -ENOMEM;
4673 goto out_free_fcf_rr_bmask;
4674 }
James Smartda0436e2009-05-22 14:51:39 -04004675 }
4676
4677 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
4678 phba->sli4_hba.cfg_eqn), GFP_KERNEL);
4679 if (!phba->sli4_hba.msix_entries) {
4680 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4681 "2573 Failed allocate memory for msi-x "
4682 "interrupt vector entries\n");
James Smart05580562011-05-24 11:40:48 -04004683 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04004684 goto out_free_fcp_eq_hdl;
4685 }
4686
James Smart912e3ac2011-05-24 11:42:11 -04004687 /*
4688 * Enable sr-iov virtual functions if supported and configured
4689 * through the module parameter.
4690 */
4691 if (phba->cfg_sriov_nr_virtfn > 0) {
4692 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
4693 phba->cfg_sriov_nr_virtfn);
4694 if (rc) {
4695 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4696 "3020 Requested number of SR-IOV "
4697 "virtual functions (%d) is not "
4698 "supported\n",
4699 phba->cfg_sriov_nr_virtfn);
4700 phba->cfg_sriov_nr_virtfn = 0;
4701 }
4702 }
4703
James Smart5248a742011-07-22 18:37:06 -04004704 return 0;
James Smartda0436e2009-05-22 14:51:39 -04004705
4706out_free_fcp_eq_hdl:
4707 kfree(phba->sli4_hba.fcp_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05004708out_free_fcf_rr_bmask:
4709 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04004710out_remove_rpi_hdrs:
4711 lpfc_sli4_remove_rpi_hdrs(phba);
4712out_free_active_sgl:
4713 lpfc_free_active_sgl(phba);
4714out_free_sgl_list:
4715 lpfc_free_sgl_list(phba);
4716out_destroy_cq_event_pool:
4717 lpfc_sli4_cq_event_pool_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04004718out_free_bsmbx:
4719 lpfc_destroy_bootstrap_mbox(phba);
4720out_free_mem:
4721 lpfc_mem_free(phba);
4722 return rc;
4723}
4724
4725/**
4726 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
4727 * @phba: pointer to lpfc hba data structure.
4728 *
4729 * This routine is invoked to unset the driver internal resources set up
4730 * specific for supporting the SLI-4 HBA device it attached to.
4731 **/
4732static void
4733lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
4734{
4735 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
4736
James Smartda0436e2009-05-22 14:51:39 -04004737 /* Free memory allocated for msi-x interrupt vector entries */
4738 kfree(phba->sli4_hba.msix_entries);
4739
4740 /* Free memory allocated for fast-path work queue handles */
4741 kfree(phba->sli4_hba.fcp_eq_hdl);
4742
4743 /* Free the allocated rpi headers. */
4744 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04004745 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04004746
James Smart0c9ab6f2010-02-26 14:15:57 -05004747 /* Free eligible FCF index bmask */
4748 kfree(phba->fcf.fcf_rr_bmask);
4749
James Smartda0436e2009-05-22 14:51:39 -04004750 /* Free the ELS sgl list */
4751 lpfc_free_active_sgl(phba);
4752 lpfc_free_sgl_list(phba);
4753
4754 /* Free the SCSI sgl management array */
4755 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
4756
James Smartda0436e2009-05-22 14:51:39 -04004757 /* Free the completion queue EQ event pool */
4758 lpfc_sli4_cq_event_release_all(phba);
4759 lpfc_sli4_cq_event_pool_destroy(phba);
4760
James Smart6d368e52011-05-24 11:44:12 -04004761 /* Release resource identifiers. */
4762 lpfc_sli4_dealloc_resource_identifiers(phba);
4763
James Smartda0436e2009-05-22 14:51:39 -04004764 /* Free the bsmbx region. */
4765 lpfc_destroy_bootstrap_mbox(phba);
4766
4767 /* Free the SLI Layer memory with SLI4 HBAs */
4768 lpfc_mem_free_all(phba);
4769
4770 /* Free the current connect table */
4771 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04004772 &phba->fcf_conn_rec_list, list) {
4773 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04004774 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04004775 }
James Smartda0436e2009-05-22 14:51:39 -04004776
4777 return;
4778}
4779
4780/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004781 * lpfc_init_api_table_setup - Set up init api function jump table
James Smart3772a992009-05-22 14:50:54 -04004782 * @phba: The hba struct for which this call is being executed.
4783 * @dev_grp: The HBA PCI-Device group number.
4784 *
4785 * This routine sets up the device INIT interface API function jump table
4786 * in @phba struct.
4787 *
4788 * Returns: 0 - success, -ENODEV - failure.
4789 **/
4790int
4791lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4792{
James Smart84d1b002010-02-12 14:42:33 -05004793 phba->lpfc_hba_init_link = lpfc_hba_init_link;
4794 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart7f860592011-03-11 16:05:52 -05004795 phba->lpfc_selective_reset = lpfc_selective_reset;
James Smart3772a992009-05-22 14:50:54 -04004796 switch (dev_grp) {
4797 case LPFC_PCI_DEV_LP:
4798 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
4799 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
4800 phba->lpfc_stop_port = lpfc_stop_port_s3;
4801 break;
James Smartda0436e2009-05-22 14:51:39 -04004802 case LPFC_PCI_DEV_OC:
4803 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
4804 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
4805 phba->lpfc_stop_port = lpfc_stop_port_s4;
4806 break;
James Smart3772a992009-05-22 14:50:54 -04004807 default:
4808 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4809 "1431 Invalid HBA PCI-device group: 0x%x\n",
4810 dev_grp);
4811 return -ENODEV;
4812 break;
4813 }
4814 return 0;
4815}
4816
4817/**
4818 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
4819 * @phba: pointer to lpfc hba data structure.
4820 *
4821 * This routine is invoked to set up the driver internal resources before the
4822 * device specific resource setup to support the HBA device it attached to.
4823 *
4824 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004825 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004826 * other values - error
4827 **/
4828static int
4829lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
4830{
4831 /*
4832 * Driver resources common to all SLI revisions
4833 */
James Smartea2151b2008-09-07 11:52:10 -04004834 atomic_set(&phba->fast_event_count, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004835 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004836
James Smarte47c9092008-02-08 18:49:26 -05004837 /* Initialize ndlp management spinlock */
4838 spin_lock_init(&phba->ndlp_lock);
4839
James Smart2e0fef82007-06-17 19:56:36 -05004840 INIT_LIST_HEAD(&phba->port_list);
James Smart3772a992009-05-22 14:50:54 -04004841 INIT_LIST_HEAD(&phba->work_list);
James Smart84774a42008-08-24 21:50:06 -04004842 init_waitqueue_head(&phba->wait_4_mlo_m_q);
dea31012005-04-17 16:05:31 -05004843
James Smart3772a992009-05-22 14:50:54 -04004844 /* Initialize the wait queue head for the kernel thread */
4845 init_waitqueue_head(&phba->work_waitq);
James Smart858c9f62007-06-17 19:56:39 -05004846
James Smart3772a992009-05-22 14:50:54 -04004847 /* Initialize the scsi buffer list used by driver for scsi IO */
4848 spin_lock_init(&phba->scsi_buf_list_lock);
4849 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
dea31012005-04-17 16:05:31 -05004850
James Smart3772a992009-05-22 14:50:54 -04004851 /* Initialize the fabric iocb list */
4852 INIT_LIST_HEAD(&phba->fabric_iocb_list);
dea31012005-04-17 16:05:31 -05004853
James Smart3772a992009-05-22 14:50:54 -04004854 /* Initialize list to save ELS buffers */
4855 INIT_LIST_HEAD(&phba->elsbuf);
dea31012005-04-17 16:05:31 -05004856
James Smart3772a992009-05-22 14:50:54 -04004857 /* Initialize FCF connection rec list */
4858 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
dea31012005-04-17 16:05:31 -05004859
James Smart3772a992009-05-22 14:50:54 -04004860 return 0;
4861}
dea31012005-04-17 16:05:31 -05004862
James Smart3772a992009-05-22 14:50:54 -04004863/**
4864 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
4865 * @phba: pointer to lpfc hba data structure.
4866 *
4867 * This routine is invoked to set up the driver internal resources after the
4868 * device specific resource setup to support the HBA device it attached to.
4869 *
4870 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004871 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004872 * other values - error
4873 **/
4874static int
4875lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
4876{
4877 int error;
4878
4879 /* Startup the kernel thread for this host adapter. */
4880 phba->worker_thread = kthread_run(lpfc_do_work, phba,
4881 "lpfc_worker_%d", phba->brd_no);
4882 if (IS_ERR(phba->worker_thread)) {
4883 error = PTR_ERR(phba->worker_thread);
4884 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05004885 }
4886
James Smart3772a992009-05-22 14:50:54 -04004887 return 0;
4888}
4889
4890/**
4891 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
4892 * @phba: pointer to lpfc hba data structure.
4893 *
4894 * This routine is invoked to unset the driver internal resources set up after
4895 * the device specific resource setup for supporting the HBA device it
4896 * attached to.
4897 **/
4898static void
4899lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
4900{
4901 /* Stop kernel worker thread */
4902 kthread_stop(phba->worker_thread);
4903}
4904
4905/**
4906 * lpfc_free_iocb_list - Free iocb list.
4907 * @phba: pointer to lpfc hba data structure.
4908 *
4909 * This routine is invoked to free the driver's IOCB list and memory.
4910 **/
4911static void
4912lpfc_free_iocb_list(struct lpfc_hba *phba)
4913{
4914 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
4915
4916 spin_lock_irq(&phba->hbalock);
4917 list_for_each_entry_safe(iocbq_entry, iocbq_next,
4918 &phba->lpfc_iocb_list, list) {
4919 list_del(&iocbq_entry->list);
4920 kfree(iocbq_entry);
4921 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05004922 }
James Smart3772a992009-05-22 14:50:54 -04004923 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004924
James Smart3772a992009-05-22 14:50:54 -04004925 return;
4926}
dea31012005-04-17 16:05:31 -05004927
James Smart3772a992009-05-22 14:50:54 -04004928/**
4929 * lpfc_init_iocb_list - Allocate and initialize iocb list.
4930 * @phba: pointer to lpfc hba data structure.
4931 *
4932 * This routine is invoked to allocate and initizlize the driver's IOCB
4933 * list and set up the IOCB tag array accordingly.
4934 *
4935 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004936 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004937 * other values - error
4938 **/
4939static int
4940lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
4941{
4942 struct lpfc_iocbq *iocbq_entry = NULL;
4943 uint16_t iotag;
4944 int i;
dea31012005-04-17 16:05:31 -05004945
4946 /* Initialize and populate the iocb list per host. */
4947 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04004948 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07004949 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05004950 if (iocbq_entry == NULL) {
4951 printk(KERN_ERR "%s: only allocated %d iocbs of "
4952 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07004953 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05004954 goto out_free_iocbq;
4955 }
4956
James Bottomley604a3e32005-10-29 10:28:33 -05004957 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
4958 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04004959 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05004960 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04004961 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05004962 goto out_free_iocbq;
4963 }
James Smart6d368e52011-05-24 11:44:12 -04004964 iocbq_entry->sli4_lxritag = NO_XRI;
James Smart3772a992009-05-22 14:50:54 -04004965 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05004966
4967 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004968 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
4969 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05004970 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004971 }
4972
James Smart3772a992009-05-22 14:50:54 -04004973 return 0;
4974
4975out_free_iocbq:
4976 lpfc_free_iocb_list(phba);
4977
4978 return -ENOMEM;
4979}
4980
4981/**
James Smartda0436e2009-05-22 14:51:39 -04004982 * lpfc_free_sgl_list - Free sgl list.
4983 * @phba: pointer to lpfc hba data structure.
4984 *
4985 * This routine is invoked to free the driver's sgl list and memory.
4986 **/
4987static void
4988lpfc_free_sgl_list(struct lpfc_hba *phba)
4989{
4990 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
4991 LIST_HEAD(sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04004992
4993 spin_lock_irq(&phba->hbalock);
4994 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
4995 spin_unlock_irq(&phba->hbalock);
4996
4997 list_for_each_entry_safe(sglq_entry, sglq_next,
4998 &sglq_list, list) {
4999 list_del(&sglq_entry->list);
5000 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
5001 kfree(sglq_entry);
5002 phba->sli4_hba.total_sglq_bufs--;
5003 }
James Smartda0436e2009-05-22 14:51:39 -04005004 kfree(phba->sli4_hba.lpfc_els_sgl_array);
5005}
5006
5007/**
5008 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
5009 * @phba: pointer to lpfc hba data structure.
5010 *
5011 * This routine is invoked to allocate the driver's active sgl memory.
5012 * This array will hold the sglq_entry's for active IOs.
5013 **/
5014static int
5015lpfc_init_active_sgl_array(struct lpfc_hba *phba)
5016{
5017 int size;
5018 size = sizeof(struct lpfc_sglq *);
5019 size *= phba->sli4_hba.max_cfg_param.max_xri;
5020
5021 phba->sli4_hba.lpfc_sglq_active_list =
5022 kzalloc(size, GFP_KERNEL);
5023 if (!phba->sli4_hba.lpfc_sglq_active_list)
5024 return -ENOMEM;
5025 return 0;
5026}
5027
5028/**
5029 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
5030 * @phba: pointer to lpfc hba data structure.
5031 *
5032 * This routine is invoked to walk through the array of active sglq entries
5033 * and free all of the resources.
5034 * This is just a place holder for now.
5035 **/
5036static void
5037lpfc_free_active_sgl(struct lpfc_hba *phba)
5038{
5039 kfree(phba->sli4_hba.lpfc_sglq_active_list);
5040}
5041
5042/**
5043 * lpfc_init_sgl_list - Allocate and initialize sgl list.
5044 * @phba: pointer to lpfc hba data structure.
5045 *
5046 * This routine is invoked to allocate and initizlize the driver's sgl
5047 * list and set up the sgl xritag tag array accordingly.
5048 *
5049 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005050 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005051 * other values - error
5052 **/
5053static int
5054lpfc_init_sgl_list(struct lpfc_hba *phba)
5055{
5056 struct lpfc_sglq *sglq_entry = NULL;
5057 int i;
5058 int els_xri_cnt;
5059
5060 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
5061 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart6d368e52011-05-24 11:44:12 -04005062 "2400 ELS XRI count %d.\n",
James Smartda0436e2009-05-22 14:51:39 -04005063 els_xri_cnt);
5064 /* Initialize and populate the sglq list per host/VF. */
5065 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
5066 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
5067
5068 /* Sanity check on XRI management */
5069 if (phba->sli4_hba.max_cfg_param.max_xri <= els_xri_cnt) {
5070 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5071 "2562 No room left for SCSI XRI allocation: "
5072 "max_xri=%d, els_xri=%d\n",
5073 phba->sli4_hba.max_cfg_param.max_xri,
5074 els_xri_cnt);
5075 return -ENOMEM;
5076 }
5077
5078 /* Allocate memory for the ELS XRI management array */
5079 phba->sli4_hba.lpfc_els_sgl_array =
5080 kzalloc((sizeof(struct lpfc_sglq *) * els_xri_cnt),
5081 GFP_KERNEL);
5082
5083 if (!phba->sli4_hba.lpfc_els_sgl_array) {
5084 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5085 "2401 Failed to allocate memory for ELS "
5086 "XRI management array of size %d.\n",
5087 els_xri_cnt);
5088 return -ENOMEM;
5089 }
5090
5091 /* Keep the SCSI XRI into the XRI management array */
5092 phba->sli4_hba.scsi_xri_max =
5093 phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
5094 phba->sli4_hba.scsi_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04005095 phba->sli4_hba.lpfc_scsi_psb_array =
5096 kzalloc((sizeof(struct lpfc_scsi_buf *) *
5097 phba->sli4_hba.scsi_xri_max), GFP_KERNEL);
5098
5099 if (!phba->sli4_hba.lpfc_scsi_psb_array) {
5100 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5101 "2563 Failed to allocate memory for SCSI "
5102 "XRI management array of size %d.\n",
5103 phba->sli4_hba.scsi_xri_max);
5104 kfree(phba->sli4_hba.lpfc_els_sgl_array);
5105 return -ENOMEM;
5106 }
5107
5108 for (i = 0; i < els_xri_cnt; i++) {
5109 sglq_entry = kzalloc(sizeof(struct lpfc_sglq), GFP_KERNEL);
5110 if (sglq_entry == NULL) {
5111 printk(KERN_ERR "%s: only allocated %d sgls of "
5112 "expected %d count. Unloading driver.\n",
5113 __func__, i, els_xri_cnt);
5114 goto out_free_mem;
5115 }
5116
James Smartda0436e2009-05-22 14:51:39 -04005117 sglq_entry->buff_type = GEN_BUFF_TYPE;
5118 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0, &sglq_entry->phys);
5119 if (sglq_entry->virt == NULL) {
5120 kfree(sglq_entry);
5121 printk(KERN_ERR "%s: failed to allocate mbuf.\n"
5122 "Unloading driver.\n", __func__);
5123 goto out_free_mem;
5124 }
5125 sglq_entry->sgl = sglq_entry->virt;
5126 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
5127
5128 /* The list order is used by later block SGL registraton */
5129 spin_lock_irq(&phba->hbalock);
James Smart0f65ff62010-02-26 14:14:23 -05005130 sglq_entry->state = SGL_FREED;
James Smartda0436e2009-05-22 14:51:39 -04005131 list_add_tail(&sglq_entry->list, &phba->sli4_hba.lpfc_sgl_list);
5132 phba->sli4_hba.lpfc_els_sgl_array[i] = sglq_entry;
5133 phba->sli4_hba.total_sglq_bufs++;
5134 spin_unlock_irq(&phba->hbalock);
5135 }
5136 return 0;
5137
5138out_free_mem:
5139 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
5140 lpfc_free_sgl_list(phba);
5141 return -ENOMEM;
5142}
5143
5144/**
5145 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
5146 * @phba: pointer to lpfc hba data structure.
5147 *
5148 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -04005149 * port for those SLI4 ports that do not support extents. This routine
James Smartda0436e2009-05-22 14:51:39 -04005150 * posts a PAGE_SIZE memory region to the port to hold up to
James Smart88a2cfb2011-07-22 18:36:33 -04005151 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
5152 * and should be called only when interrupts are disabled.
James Smartda0436e2009-05-22 14:51:39 -04005153 *
5154 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005155 * 0 - successful
James Smart88a2cfb2011-07-22 18:36:33 -04005156 * -ERROR - otherwise.
James Smartda0436e2009-05-22 14:51:39 -04005157 **/
5158int
5159lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
5160{
5161 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04005162 struct lpfc_rpi_hdr *rpi_hdr;
5163
5164 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
James Smartff78d8f2011-12-13 13:21:35 -05005165 if (!phba->sli4_hba.rpi_hdrs_in_use)
James Smart6d368e52011-05-24 11:44:12 -04005166 return rc;
James Smart6d368e52011-05-24 11:44:12 -04005167 if (phba->sli4_hba.extents_in_use)
5168 return -EIO;
James Smartda0436e2009-05-22 14:51:39 -04005169
5170 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
5171 if (!rpi_hdr) {
5172 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5173 "0391 Error during rpi post operation\n");
5174 lpfc_sli4_remove_rpis(phba);
5175 rc = -ENODEV;
5176 }
5177
5178 return rc;
5179}
5180
5181/**
5182 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
5183 * @phba: pointer to lpfc hba data structure.
5184 *
5185 * This routine is invoked to allocate a single 4KB memory region to
5186 * support rpis and stores them in the phba. This single region
5187 * provides support for up to 64 rpis. The region is used globally
5188 * by the device.
5189 *
5190 * Returns:
5191 * A valid rpi hdr on success.
5192 * A NULL pointer on any failure.
5193 **/
5194struct lpfc_rpi_hdr *
5195lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
5196{
5197 uint16_t rpi_limit, curr_rpi_range;
5198 struct lpfc_dmabuf *dmabuf;
5199 struct lpfc_rpi_hdr *rpi_hdr;
James Smart9589b062011-04-16 11:03:17 -04005200 uint32_t rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04005201
James Smart6d368e52011-05-24 11:44:12 -04005202 /*
5203 * If the SLI4 port supports extents, posting the rpi header isn't
5204 * required. Set the expected maximum count and let the actual value
5205 * get set when extents are fully allocated.
5206 */
5207 if (!phba->sli4_hba.rpi_hdrs_in_use)
5208 return NULL;
5209 if (phba->sli4_hba.extents_in_use)
5210 return NULL;
5211
5212 /* The limit on the logical index is just the max_rpi count. */
James Smartda0436e2009-05-22 14:51:39 -04005213 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
James Smart6d368e52011-05-24 11:44:12 -04005214 phba->sli4_hba.max_cfg_param.max_rpi - 1;
James Smartda0436e2009-05-22 14:51:39 -04005215
5216 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04005217 /*
5218 * Establish the starting RPI in this header block. The starting
5219 * rpi is normalized to a zero base because the physical rpi is
5220 * port based.
5221 */
5222 curr_rpi_range = phba->sli4_hba.next_rpi -
5223 phba->sli4_hba.max_cfg_param.rpi_base;
James Smartda0436e2009-05-22 14:51:39 -04005224 spin_unlock_irq(&phba->hbalock);
5225
5226 /*
5227 * The port has a limited number of rpis. The increment here
5228 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
5229 * and to allow the full max_rpi range per port.
5230 */
5231 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
James Smart9589b062011-04-16 11:03:17 -04005232 rpi_count = rpi_limit - curr_rpi_range;
5233 else
5234 rpi_count = LPFC_RPI_HDR_COUNT;
James Smartda0436e2009-05-22 14:51:39 -04005235
James Smart6d368e52011-05-24 11:44:12 -04005236 if (!rpi_count)
5237 return NULL;
James Smartda0436e2009-05-22 14:51:39 -04005238 /*
5239 * First allocate the protocol header region for the port. The
5240 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
5241 */
5242 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5243 if (!dmabuf)
5244 return NULL;
5245
5246 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5247 LPFC_HDR_TEMPLATE_SIZE,
5248 &dmabuf->phys,
5249 GFP_KERNEL);
5250 if (!dmabuf->virt) {
5251 rpi_hdr = NULL;
5252 goto err_free_dmabuf;
5253 }
5254
5255 memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
5256 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
5257 rpi_hdr = NULL;
5258 goto err_free_coherent;
5259 }
5260
5261 /* Save the rpi header data for cleanup later. */
5262 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
5263 if (!rpi_hdr)
5264 goto err_free_coherent;
5265
5266 rpi_hdr->dmabuf = dmabuf;
5267 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
5268 rpi_hdr->page_count = 1;
5269 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04005270
5271 /* The rpi_hdr stores the logical index only. */
5272 rpi_hdr->start_rpi = curr_rpi_range;
James Smartda0436e2009-05-22 14:51:39 -04005273 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
5274
5275 /*
James Smart6d368e52011-05-24 11:44:12 -04005276 * The next_rpi stores the next logical module-64 rpi value used
5277 * to post physical rpis in subsequent rpi postings.
James Smartda0436e2009-05-22 14:51:39 -04005278 */
James Smart9589b062011-04-16 11:03:17 -04005279 phba->sli4_hba.next_rpi += rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04005280 spin_unlock_irq(&phba->hbalock);
5281 return rpi_hdr;
5282
5283 err_free_coherent:
5284 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
5285 dmabuf->virt, dmabuf->phys);
5286 err_free_dmabuf:
5287 kfree(dmabuf);
5288 return NULL;
5289}
5290
5291/**
5292 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
5293 * @phba: pointer to lpfc hba data structure.
5294 *
5295 * This routine is invoked to remove all memory resources allocated
James Smart6d368e52011-05-24 11:44:12 -04005296 * to support rpis for SLI4 ports not supporting extents. This routine
5297 * presumes the caller has released all rpis consumed by fabric or port
5298 * logins and is prepared to have the header pages removed.
James Smartda0436e2009-05-22 14:51:39 -04005299 **/
5300void
5301lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
5302{
5303 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
5304
James Smart6d368e52011-05-24 11:44:12 -04005305 if (!phba->sli4_hba.rpi_hdrs_in_use)
5306 goto exit;
5307
James Smartda0436e2009-05-22 14:51:39 -04005308 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
5309 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
5310 list_del(&rpi_hdr->list);
5311 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
5312 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
5313 kfree(rpi_hdr->dmabuf);
5314 kfree(rpi_hdr);
5315 }
James Smart6d368e52011-05-24 11:44:12 -04005316 exit:
5317 /* There are no rpis available to the port now. */
5318 phba->sli4_hba.next_rpi = 0;
James Smartda0436e2009-05-22 14:51:39 -04005319}
5320
5321/**
James Smart3772a992009-05-22 14:50:54 -04005322 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
5323 * @pdev: pointer to pci device data structure.
5324 *
5325 * This routine is invoked to allocate the driver hba data structure for an
5326 * HBA device. If the allocation is successful, the phba reference to the
5327 * PCI device data structure is set.
5328 *
5329 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005330 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04005331 * NULL - error
5332 **/
5333static struct lpfc_hba *
5334lpfc_hba_alloc(struct pci_dev *pdev)
5335{
5336 struct lpfc_hba *phba;
5337
5338 /* Allocate memory for HBA structure */
5339 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
5340 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02005341 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04005342 return NULL;
5343 }
5344
5345 /* Set reference to PCI device in HBA structure */
5346 phba->pcidev = pdev;
5347
5348 /* Assign an unused board number */
5349 phba->brd_no = lpfc_get_instance();
5350 if (phba->brd_no < 0) {
5351 kfree(phba);
5352 return NULL;
5353 }
5354
James Smart4fede782010-01-26 23:08:55 -05005355 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04005356 INIT_LIST_HEAD(&phba->ct_ev_waiters);
5357
James Smart3772a992009-05-22 14:50:54 -04005358 return phba;
5359}
5360
5361/**
5362 * lpfc_hba_free - Free driver hba data structure with a device.
5363 * @phba: pointer to lpfc hba data structure.
5364 *
5365 * This routine is invoked to free the driver hba data structure with an
5366 * HBA device.
5367 **/
5368static void
5369lpfc_hba_free(struct lpfc_hba *phba)
5370{
5371 /* Release the driver assigned board number */
5372 idr_remove(&lpfc_hba_index, phba->brd_no);
5373
5374 kfree(phba);
5375 return;
5376}
5377
5378/**
5379 * lpfc_create_shost - Create hba physical port with associated scsi host.
5380 * @phba: pointer to lpfc hba data structure.
5381 *
5382 * This routine is invoked to create HBA physical port and associate a SCSI
5383 * host with it.
5384 *
5385 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005386 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005387 * other values - error
5388 **/
5389static int
5390lpfc_create_shost(struct lpfc_hba *phba)
5391{
5392 struct lpfc_vport *vport;
5393 struct Scsi_Host *shost;
5394
5395 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05005396 phba->fc_edtov = FF_DEF_EDTOV;
5397 phba->fc_ratov = FF_DEF_RATOV;
5398 phba->fc_altov = FF_DEF_ALTOV;
5399 phba->fc_arbtov = FF_DEF_ARBTOV;
5400
James Smartd7c47992010-06-08 18:31:54 -04005401 atomic_set(&phba->sdev_cnt, 0);
James Smart3de2a652007-08-02 11:09:59 -04005402 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05005403 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04005404 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05005405
5406 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05005407 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05005408 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04005409 /* Put reference to SCSI host to driver's device private data */
5410 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05005411
James Smart3772a992009-05-22 14:50:54 -04005412 return 0;
5413}
dea31012005-04-17 16:05:31 -05005414
James Smart3772a992009-05-22 14:50:54 -04005415/**
5416 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
5417 * @phba: pointer to lpfc hba data structure.
5418 *
5419 * This routine is invoked to destroy HBA physical port and the associated
5420 * SCSI host.
5421 **/
5422static void
5423lpfc_destroy_shost(struct lpfc_hba *phba)
5424{
5425 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04005426
James Smart3772a992009-05-22 14:50:54 -04005427 /* Destroy physical port that associated with the SCSI host */
5428 destroy_port(vport);
5429
5430 return;
5431}
5432
5433/**
5434 * lpfc_setup_bg - Setup Block guard structures and debug areas.
5435 * @phba: pointer to lpfc hba data structure.
5436 * @shost: the shost to be used to detect Block guard settings.
5437 *
5438 * This routine sets up the local Block guard protocol settings for @shost.
5439 * This routine also allocates memory for debugging bg buffers.
5440 **/
5441static void
5442lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
5443{
5444 int pagecnt = 10;
5445 if (lpfc_prot_mask && lpfc_prot_guard) {
5446 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5447 "1478 Registering BlockGuard with the "
5448 "SCSI layer\n");
5449 scsi_host_set_prot(shost, lpfc_prot_mask);
5450 scsi_host_set_guard(shost, lpfc_prot_guard);
James Smart98c9ea52007-10-27 13:37:33 -04005451 }
James Smart81301a92008-12-04 22:39:46 -05005452 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05005453 while (pagecnt) {
5454 spin_lock_init(&_dump_buf_lock);
5455 _dump_buf_data =
5456 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
5457 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04005458 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5459 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04005460 "_dump_buf_data at 0x%p\n",
5461 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05005462 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04005463 memset(_dump_buf_data, 0,
5464 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05005465 break;
James Smart3772a992009-05-22 14:50:54 -04005466 } else
James Smart81301a92008-12-04 22:39:46 -05005467 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05005468 }
James Smart81301a92008-12-04 22:39:46 -05005469 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04005470 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5471 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04005472 "memory for hexdump\n");
5473 } else
James Smart6a9c52c2009-10-02 15:16:51 -04005474 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5475 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05005476 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05005477 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05005478 while (pagecnt) {
5479 _dump_buf_dif =
5480 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
5481 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04005482 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5483 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04005484 "_dump_buf_dif at 0x%p\n",
5485 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05005486 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04005487 memset(_dump_buf_dif, 0,
5488 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05005489 break;
James Smart3772a992009-05-22 14:50:54 -04005490 } else
James Smart81301a92008-12-04 22:39:46 -05005491 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05005492 }
James Smart81301a92008-12-04 22:39:46 -05005493 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04005494 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5495 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04005496 "memory for hexdump\n");
5497 } else
James Smart6a9c52c2009-10-02 15:16:51 -04005498 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5499 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04005500 _dump_buf_dif);
5501}
5502
5503/**
5504 * lpfc_post_init_setup - Perform necessary device post initialization setup.
5505 * @phba: pointer to lpfc hba data structure.
5506 *
5507 * This routine is invoked to perform all the necessary post initialization
5508 * setup for the device.
5509 **/
5510static void
5511lpfc_post_init_setup(struct lpfc_hba *phba)
5512{
5513 struct Scsi_Host *shost;
5514 struct lpfc_adapter_event_header adapter_event;
5515
5516 /* Get the default values for Model Name and Description */
5517 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
5518
5519 /*
5520 * hba setup may have changed the hba_queue_depth so we need to
5521 * adjust the value of can_queue.
5522 */
5523 shost = pci_get_drvdata(phba->pcidev);
5524 shost->can_queue = phba->cfg_hba_queue_depth - 10;
5525 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
5526 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05005527
5528 lpfc_host_attrib_init(shost);
5529
James Smart2e0fef82007-06-17 19:56:36 -05005530 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
5531 spin_lock_irq(shost->host_lock);
5532 lpfc_poll_start_timer(phba);
5533 spin_unlock_irq(shost->host_lock);
5534 }
James Smart8f6d98d2006-08-01 07:34:00 -04005535
James Smart93996272008-08-24 21:50:30 -04005536 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5537 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04005538 /* Send board arrival event to upper layer */
5539 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
5540 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
5541 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04005542 sizeof(adapter_event),
5543 (char *) &adapter_event,
5544 LPFC_NL_VENDOR_ID);
5545 return;
5546}
5547
5548/**
5549 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
5550 * @phba: pointer to lpfc hba data structure.
5551 *
5552 * This routine is invoked to set up the PCI device memory space for device
5553 * with SLI-3 interface spec.
5554 *
5555 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005556 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005557 * other values - error
5558 **/
5559static int
5560lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
5561{
5562 struct pci_dev *pdev;
5563 unsigned long bar0map_len, bar2map_len;
5564 int i, hbq_count;
5565 void *ptr;
5566 int error = -ENODEV;
5567
5568 /* Obtain PCI device reference */
5569 if (!phba->pcidev)
5570 return error;
5571 else
5572 pdev = phba->pcidev;
5573
5574 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05005575 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
5576 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
5577 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
5578 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smart3772a992009-05-22 14:50:54 -04005579 return error;
Michael Reed8e685972009-09-18 12:02:05 -05005580 }
5581 }
James Smart3772a992009-05-22 14:50:54 -04005582
5583 /* Get the bus address of Bar0 and Bar2 and the number of bytes
5584 * required by each mapping.
5585 */
5586 phba->pci_bar0_map = pci_resource_start(pdev, 0);
5587 bar0map_len = pci_resource_len(pdev, 0);
5588
5589 phba->pci_bar2_map = pci_resource_start(pdev, 2);
5590 bar2map_len = pci_resource_len(pdev, 2);
5591
5592 /* Map HBA SLIM to a kernel virtual address. */
5593 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
5594 if (!phba->slim_memmap_p) {
5595 dev_printk(KERN_ERR, &pdev->dev,
5596 "ioremap failed for SLIM memory.\n");
5597 goto out;
5598 }
5599
5600 /* Map HBA Control Registers to a kernel virtual address. */
5601 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
5602 if (!phba->ctrl_regs_memmap_p) {
5603 dev_printk(KERN_ERR, &pdev->dev,
5604 "ioremap failed for HBA control registers.\n");
5605 goto out_iounmap_slim;
5606 }
5607
5608 /* Allocate memory for SLI-2 structures */
5609 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
5610 SLI2_SLIM_SIZE,
5611 &phba->slim2p.phys,
5612 GFP_KERNEL);
5613 if (!phba->slim2p.virt)
5614 goto out_iounmap;
5615
5616 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
5617 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04005618 phba->mbox_ext = (phba->slim2p.virt +
5619 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04005620 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
5621 phba->IOCBs = (phba->slim2p.virt +
5622 offsetof(struct lpfc_sli2_slim, IOCBs));
5623
5624 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
5625 lpfc_sli_hbq_size(),
5626 &phba->hbqslimp.phys,
5627 GFP_KERNEL);
5628 if (!phba->hbqslimp.virt)
5629 goto out_free_slim;
5630
5631 hbq_count = lpfc_sli_hbq_count();
5632 ptr = phba->hbqslimp.virt;
5633 for (i = 0; i < hbq_count; ++i) {
5634 phba->hbqs[i].hbq_virt = ptr;
5635 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
5636 ptr += (lpfc_hbq_defs[i]->entry_count *
5637 sizeof(struct lpfc_hbq_entry));
5638 }
5639 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
5640 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
5641
5642 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
5643
5644 INIT_LIST_HEAD(&phba->rb_pend_list);
5645
5646 phba->MBslimaddr = phba->slim_memmap_p;
5647 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
5648 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
5649 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
5650 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04005651
dea31012005-04-17 16:05:31 -05005652 return 0;
5653
dea31012005-04-17 16:05:31 -05005654out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04005655 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
5656 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05005657out_iounmap:
5658 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005659out_iounmap_slim:
dea31012005-04-17 16:05:31 -05005660 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05005661out:
5662 return error;
5663}
5664
James Smarte59058c2008-08-24 21:49:00 -04005665/**
James Smart3772a992009-05-22 14:50:54 -04005666 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
5667 * @phba: pointer to lpfc hba data structure.
5668 *
5669 * This routine is invoked to unset the PCI device memory space for device
5670 * with SLI-3 interface spec.
5671 **/
5672static void
5673lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
5674{
5675 struct pci_dev *pdev;
5676
5677 /* Obtain PCI device reference */
5678 if (!phba->pcidev)
5679 return;
5680 else
5681 pdev = phba->pcidev;
5682
5683 /* Free coherent DMA memory allocated */
5684 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
5685 phba->hbqslimp.virt, phba->hbqslimp.phys);
5686 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
5687 phba->slim2p.virt, phba->slim2p.phys);
5688
5689 /* I/O memory unmap */
5690 iounmap(phba->ctrl_regs_memmap_p);
5691 iounmap(phba->slim_memmap_p);
5692
5693 return;
5694}
5695
5696/**
James Smartda0436e2009-05-22 14:51:39 -04005697 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
5698 * @phba: pointer to lpfc hba data structure.
5699 *
5700 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
5701 * done and check status.
5702 *
5703 * Return 0 if successful, otherwise -ENODEV.
5704 **/
5705int
5706lpfc_sli4_post_status_check(struct lpfc_hba *phba)
5707{
James Smart2fcee4b2010-12-15 17:57:46 -05005708 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
5709 struct lpfc_register reg_data;
5710 int i, port_error = 0;
5711 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04005712
James Smart9940b972011-03-11 16:06:12 -05005713 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
5714 memset(&reg_data, 0, sizeof(reg_data));
James Smart2fcee4b2010-12-15 17:57:46 -05005715 if (!phba->sli4_hba.PSMPHRregaddr)
James Smartda0436e2009-05-22 14:51:39 -04005716 return -ENODEV;
5717
James Smartda0436e2009-05-22 14:51:39 -04005718 /* Wait up to 30 seconds for the SLI Port POST done and ready */
5719 for (i = 0; i < 3000; i++) {
James Smart9940b972011-03-11 16:06:12 -05005720 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
5721 &portsmphr_reg.word0) ||
5722 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
James Smart2fcee4b2010-12-15 17:57:46 -05005723 /* Port has a fatal POST error, break out */
James Smartda0436e2009-05-22 14:51:39 -04005724 port_error = -ENODEV;
5725 break;
5726 }
James Smart2fcee4b2010-12-15 17:57:46 -05005727 if (LPFC_POST_STAGE_PORT_READY ==
5728 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
James Smartda0436e2009-05-22 14:51:39 -04005729 break;
James Smartda0436e2009-05-22 14:51:39 -04005730 msleep(10);
5731 }
5732
James Smart2fcee4b2010-12-15 17:57:46 -05005733 /*
5734 * If there was a port error during POST, then don't proceed with
5735 * other register reads as the data may not be valid. Just exit.
5736 */
5737 if (port_error) {
James Smartda0436e2009-05-22 14:51:39 -04005738 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05005739 "1408 Port Failed POST - portsmphr=0x%x, "
5740 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
5741 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
5742 portsmphr_reg.word0,
5743 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
5744 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
5745 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
5746 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
5747 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
5748 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
5749 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
5750 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
5751 } else {
James Smart28baac72010-02-12 14:42:03 -05005752 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05005753 "2534 Device Info: SLIFamily=0x%x, "
5754 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
5755 "SLIHint_2=0x%x, FT=0x%x\n",
James Smart28baac72010-02-12 14:42:03 -05005756 bf_get(lpfc_sli_intf_sli_family,
5757 &phba->sli4_hba.sli_intf),
5758 bf_get(lpfc_sli_intf_slirev,
5759 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05005760 bf_get(lpfc_sli_intf_if_type,
James Smart28baac72010-02-12 14:42:03 -05005761 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05005762 bf_get(lpfc_sli_intf_sli_hint1,
5763 &phba->sli4_hba.sli_intf),
5764 bf_get(lpfc_sli_intf_sli_hint2,
5765 &phba->sli4_hba.sli_intf),
5766 bf_get(lpfc_sli_intf_func_type,
James Smart28baac72010-02-12 14:42:03 -05005767 &phba->sli4_hba.sli_intf));
James Smart2fcee4b2010-12-15 17:57:46 -05005768 /*
5769 * Check for other Port errors during the initialization
5770 * process. Fail the load if the port did not come up
5771 * correctly.
5772 */
5773 if_type = bf_get(lpfc_sli_intf_if_type,
5774 &phba->sli4_hba.sli_intf);
5775 switch (if_type) {
5776 case LPFC_SLI_INTF_IF_TYPE_0:
5777 phba->sli4_hba.ue_mask_lo =
5778 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
5779 phba->sli4_hba.ue_mask_hi =
5780 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
5781 uerrlo_reg.word0 =
5782 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
5783 uerrhi_reg.word0 =
5784 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
5785 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
5786 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
5787 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5788 "1422 Unrecoverable Error "
5789 "Detected during POST "
5790 "uerr_lo_reg=0x%x, "
5791 "uerr_hi_reg=0x%x, "
5792 "ue_mask_lo_reg=0x%x, "
5793 "ue_mask_hi_reg=0x%x\n",
5794 uerrlo_reg.word0,
5795 uerrhi_reg.word0,
5796 phba->sli4_hba.ue_mask_lo,
5797 phba->sli4_hba.ue_mask_hi);
5798 port_error = -ENODEV;
5799 }
5800 break;
5801 case LPFC_SLI_INTF_IF_TYPE_2:
5802 /* Final checks. The port status should be clean. */
James Smart9940b972011-03-11 16:06:12 -05005803 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
5804 &reg_data.word0) ||
James Smart05580562011-05-24 11:40:48 -04005805 (bf_get(lpfc_sliport_status_err, &reg_data) &&
5806 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
James Smart2fcee4b2010-12-15 17:57:46 -05005807 phba->work_status[0] =
5808 readl(phba->sli4_hba.u.if_type2.
5809 ERR1regaddr);
5810 phba->work_status[1] =
5811 readl(phba->sli4_hba.u.if_type2.
5812 ERR2regaddr);
5813 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5814 "2888 Port Error Detected "
5815 "during POST: "
5816 "port status reg 0x%x, "
5817 "port_smphr reg 0x%x, "
5818 "error 1=0x%x, error 2=0x%x\n",
5819 reg_data.word0,
5820 portsmphr_reg.word0,
5821 phba->work_status[0],
5822 phba->work_status[1]);
5823 port_error = -ENODEV;
5824 }
5825 break;
5826 case LPFC_SLI_INTF_IF_TYPE_1:
5827 default:
5828 break;
5829 }
James Smart28baac72010-02-12 14:42:03 -05005830 }
James Smartda0436e2009-05-22 14:51:39 -04005831 return port_error;
5832}
5833
5834/**
5835 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
5836 * @phba: pointer to lpfc hba data structure.
James Smart2fcee4b2010-12-15 17:57:46 -05005837 * @if_type: The SLI4 interface type getting configured.
James Smartda0436e2009-05-22 14:51:39 -04005838 *
5839 * This routine is invoked to set up SLI4 BAR0 PCI config space register
5840 * memory map.
5841 **/
5842static void
James Smart2fcee4b2010-12-15 17:57:46 -05005843lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04005844{
James Smart2fcee4b2010-12-15 17:57:46 -05005845 switch (if_type) {
5846 case LPFC_SLI_INTF_IF_TYPE_0:
5847 phba->sli4_hba.u.if_type0.UERRLOregaddr =
5848 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
5849 phba->sli4_hba.u.if_type0.UERRHIregaddr =
5850 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
5851 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
5852 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
5853 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
5854 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
5855 phba->sli4_hba.SLIINTFregaddr =
5856 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
5857 break;
5858 case LPFC_SLI_INTF_IF_TYPE_2:
5859 phba->sli4_hba.u.if_type2.ERR1regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04005860 phba->sli4_hba.conf_regs_memmap_p +
5861 LPFC_CTL_PORT_ER1_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05005862 phba->sli4_hba.u.if_type2.ERR2regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04005863 phba->sli4_hba.conf_regs_memmap_p +
5864 LPFC_CTL_PORT_ER2_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05005865 phba->sli4_hba.u.if_type2.CTRLregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04005866 phba->sli4_hba.conf_regs_memmap_p +
5867 LPFC_CTL_PORT_CTL_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05005868 phba->sli4_hba.u.if_type2.STATUSregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04005869 phba->sli4_hba.conf_regs_memmap_p +
5870 LPFC_CTL_PORT_STA_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05005871 phba->sli4_hba.SLIINTFregaddr =
5872 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
5873 phba->sli4_hba.PSMPHRregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04005874 phba->sli4_hba.conf_regs_memmap_p +
5875 LPFC_CTL_PORT_SEM_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05005876 phba->sli4_hba.RQDBregaddr =
5877 phba->sli4_hba.conf_regs_memmap_p + LPFC_RQ_DOORBELL;
5878 phba->sli4_hba.WQDBregaddr =
5879 phba->sli4_hba.conf_regs_memmap_p + LPFC_WQ_DOORBELL;
5880 phba->sli4_hba.EQCQDBregaddr =
5881 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
5882 phba->sli4_hba.MQDBregaddr =
5883 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
5884 phba->sli4_hba.BMBXregaddr =
5885 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
5886 break;
5887 case LPFC_SLI_INTF_IF_TYPE_1:
5888 default:
5889 dev_printk(KERN_ERR, &phba->pcidev->dev,
5890 "FATAL - unsupported SLI4 interface type - %d\n",
5891 if_type);
5892 break;
5893 }
James Smartda0436e2009-05-22 14:51:39 -04005894}
5895
5896/**
5897 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
5898 * @phba: pointer to lpfc hba data structure.
5899 *
5900 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
5901 * memory map.
5902 **/
5903static void
5904lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
5905{
James Smart2fcee4b2010-12-15 17:57:46 -05005906 phba->sli4_hba.PSMPHRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5907 LPFC_SLIPORT_IF0_SMPHR;
James Smartda0436e2009-05-22 14:51:39 -04005908 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05005909 LPFC_HST_ISR0;
James Smartda0436e2009-05-22 14:51:39 -04005910 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05005911 LPFC_HST_IMR0;
James Smartda0436e2009-05-22 14:51:39 -04005912 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05005913 LPFC_HST_ISCR0;
James Smartda0436e2009-05-22 14:51:39 -04005914}
5915
5916/**
5917 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
5918 * @phba: pointer to lpfc hba data structure.
5919 * @vf: virtual function number
5920 *
5921 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
5922 * based on the given viftual function number, @vf.
5923 *
5924 * Return 0 if successful, otherwise -ENODEV.
5925 **/
5926static int
5927lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
5928{
5929 if (vf > LPFC_VIR_FUNC_MAX)
5930 return -ENODEV;
5931
5932 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5933 vf * LPFC_VFR_PAGE_SIZE + LPFC_RQ_DOORBELL);
5934 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5935 vf * LPFC_VFR_PAGE_SIZE + LPFC_WQ_DOORBELL);
5936 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5937 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
5938 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5939 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
5940 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5941 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
5942 return 0;
5943}
5944
5945/**
5946 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
5947 * @phba: pointer to lpfc hba data structure.
5948 *
5949 * This routine is invoked to create the bootstrap mailbox
5950 * region consistent with the SLI-4 interface spec. This
5951 * routine allocates all memory necessary to communicate
5952 * mailbox commands to the port and sets up all alignment
5953 * needs. No locks are expected to be held when calling
5954 * this routine.
5955 *
5956 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005957 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005958 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04005959 **/
5960static int
5961lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
5962{
5963 uint32_t bmbx_size;
5964 struct lpfc_dmabuf *dmabuf;
5965 struct dma_address *dma_address;
5966 uint32_t pa_addr;
5967 uint64_t phys_addr;
5968
5969 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5970 if (!dmabuf)
5971 return -ENOMEM;
5972
5973 /*
5974 * The bootstrap mailbox region is comprised of 2 parts
5975 * plus an alignment restriction of 16 bytes.
5976 */
5977 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
5978 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5979 bmbx_size,
5980 &dmabuf->phys,
5981 GFP_KERNEL);
5982 if (!dmabuf->virt) {
5983 kfree(dmabuf);
5984 return -ENOMEM;
5985 }
5986 memset(dmabuf->virt, 0, bmbx_size);
5987
5988 /*
5989 * Initialize the bootstrap mailbox pointers now so that the register
5990 * operations are simple later. The mailbox dma address is required
5991 * to be 16-byte aligned. Also align the virtual memory as each
5992 * maibox is copied into the bmbx mailbox region before issuing the
5993 * command to the port.
5994 */
5995 phba->sli4_hba.bmbx.dmabuf = dmabuf;
5996 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
5997
5998 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
5999 LPFC_ALIGN_16_BYTE);
6000 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
6001 LPFC_ALIGN_16_BYTE);
6002
6003 /*
6004 * Set the high and low physical addresses now. The SLI4 alignment
6005 * requirement is 16 bytes and the mailbox is posted to the port
6006 * as two 30-bit addresses. The other data is a bit marking whether
6007 * the 30-bit address is the high or low address.
6008 * Upcast bmbx aphys to 64bits so shift instruction compiles
6009 * clean on 32 bit machines.
6010 */
6011 dma_address = &phba->sli4_hba.bmbx.dma_address;
6012 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
6013 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
6014 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
6015 LPFC_BMBX_BIT1_ADDR_HI);
6016
6017 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
6018 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
6019 LPFC_BMBX_BIT1_ADDR_LO);
6020 return 0;
6021}
6022
6023/**
6024 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
6025 * @phba: pointer to lpfc hba data structure.
6026 *
6027 * This routine is invoked to teardown the bootstrap mailbox
6028 * region and release all host resources. This routine requires
6029 * the caller to ensure all mailbox commands recovered, no
6030 * additional mailbox comands are sent, and interrupts are disabled
6031 * before calling this routine.
6032 *
6033 **/
6034static void
6035lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
6036{
6037 dma_free_coherent(&phba->pcidev->dev,
6038 phba->sli4_hba.bmbx.bmbx_size,
6039 phba->sli4_hba.bmbx.dmabuf->virt,
6040 phba->sli4_hba.bmbx.dmabuf->phys);
6041
6042 kfree(phba->sli4_hba.bmbx.dmabuf);
6043 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
6044}
6045
6046/**
6047 * lpfc_sli4_read_config - Get the config parameters.
6048 * @phba: pointer to lpfc hba data structure.
6049 *
6050 * This routine is invoked to read the configuration parameters from the HBA.
6051 * The configuration parameters are used to set the base and maximum values
6052 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
6053 * allocation for the port.
6054 *
6055 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006056 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006057 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006058 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006059 **/
James Smartff78d8f2011-12-13 13:21:35 -05006060int
James Smartda0436e2009-05-22 14:51:39 -04006061lpfc_sli4_read_config(struct lpfc_hba *phba)
6062{
6063 LPFC_MBOXQ_t *pmb;
6064 struct lpfc_mbx_read_config *rd_config;
James Smart912e3ac2011-05-24 11:42:11 -04006065 union lpfc_sli4_cfg_shdr *shdr;
6066 uint32_t shdr_status, shdr_add_status;
6067 struct lpfc_mbx_get_func_cfg *get_func_cfg;
6068 struct lpfc_rsrc_desc_fcfcoe *desc;
6069 uint32_t desc_count;
6070 int length, i, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04006071
6072 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6073 if (!pmb) {
6074 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6075 "2011 Unable to allocate memory for issuing "
6076 "SLI_CONFIG_SPECIAL mailbox command\n");
6077 return -ENOMEM;
6078 }
6079
6080 lpfc_read_config(phba, pmb);
6081
6082 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6083 if (rc != MBX_SUCCESS) {
6084 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6085 "2012 Mailbox failed , mbxCmd x%x "
6086 "READ_CONFIG, mbxStatus x%x\n",
6087 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6088 bf_get(lpfc_mqe_status, &pmb->u.mqe));
6089 rc = -EIO;
6090 } else {
6091 rd_config = &pmb->u.mqe.un.rd_config;
James Smartff78d8f2011-12-13 13:21:35 -05006092 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
6093 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
6094 phba->sli4_hba.lnk_info.lnk_tp =
6095 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
6096 phba->sli4_hba.lnk_info.lnk_no =
6097 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
6098 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6099 "3081 lnk_type:%d, lnk_numb:%d\n",
6100 phba->sli4_hba.lnk_info.lnk_tp,
6101 phba->sli4_hba.lnk_info.lnk_no);
6102 } else
6103 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6104 "3082 Mailbox (x%x) returned ldv:x0\n",
6105 bf_get(lpfc_mqe_command, &pmb->u.mqe));
James Smart6d368e52011-05-24 11:44:12 -04006106 phba->sli4_hba.extents_in_use =
6107 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04006108 phba->sli4_hba.max_cfg_param.max_xri =
6109 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
6110 phba->sli4_hba.max_cfg_param.xri_base =
6111 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
6112 phba->sli4_hba.max_cfg_param.max_vpi =
6113 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
6114 phba->sli4_hba.max_cfg_param.vpi_base =
6115 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
6116 phba->sli4_hba.max_cfg_param.max_rpi =
6117 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
6118 phba->sli4_hba.max_cfg_param.rpi_base =
6119 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
6120 phba->sli4_hba.max_cfg_param.max_vfi =
6121 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
6122 phba->sli4_hba.max_cfg_param.vfi_base =
6123 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
6124 phba->sli4_hba.max_cfg_param.max_fcfi =
6125 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04006126 phba->sli4_hba.max_cfg_param.max_eq =
6127 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
6128 phba->sli4_hba.max_cfg_param.max_rq =
6129 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
6130 phba->sli4_hba.max_cfg_param.max_wq =
6131 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
6132 phba->sli4_hba.max_cfg_param.max_cq =
6133 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
6134 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
6135 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
6136 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
6137 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
6138 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
James Smart5ffc2662009-11-18 15:39:44 -05006139 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
6140 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04006141 phba->max_vports = phba->max_vpi;
6142 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart6d368e52011-05-24 11:44:12 -04006143 "2003 cfg params Extents? %d "
6144 "XRI(B:%d M:%d), "
James Smartda0436e2009-05-22 14:51:39 -04006145 "VPI(B:%d M:%d) "
6146 "VFI(B:%d M:%d) "
6147 "RPI(B:%d M:%d) "
James Smart6d368e52011-05-24 11:44:12 -04006148 "FCFI(Count:%d)\n",
6149 phba->sli4_hba.extents_in_use,
James Smartda0436e2009-05-22 14:51:39 -04006150 phba->sli4_hba.max_cfg_param.xri_base,
6151 phba->sli4_hba.max_cfg_param.max_xri,
6152 phba->sli4_hba.max_cfg_param.vpi_base,
6153 phba->sli4_hba.max_cfg_param.max_vpi,
6154 phba->sli4_hba.max_cfg_param.vfi_base,
6155 phba->sli4_hba.max_cfg_param.max_vfi,
6156 phba->sli4_hba.max_cfg_param.rpi_base,
6157 phba->sli4_hba.max_cfg_param.max_rpi,
James Smartda0436e2009-05-22 14:51:39 -04006158 phba->sli4_hba.max_cfg_param.max_fcfi);
6159 }
James Smart912e3ac2011-05-24 11:42:11 -04006160
6161 if (rc)
6162 goto read_cfg_out;
James Smartda0436e2009-05-22 14:51:39 -04006163
6164 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smartb92938b2010-06-07 15:24:12 -04006165 if (phba->cfg_hba_queue_depth >
6166 (phba->sli4_hba.max_cfg_param.max_xri -
6167 lpfc_sli4_get_els_iocb_cnt(phba)))
James Smartda0436e2009-05-22 14:51:39 -04006168 phba->cfg_hba_queue_depth =
James Smartb92938b2010-06-07 15:24:12 -04006169 phba->sli4_hba.max_cfg_param.max_xri -
6170 lpfc_sli4_get_els_iocb_cnt(phba);
James Smart912e3ac2011-05-24 11:42:11 -04006171
6172 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
6173 LPFC_SLI_INTF_IF_TYPE_2)
6174 goto read_cfg_out;
6175
6176 /* get the pf# and vf# for SLI4 if_type 2 port */
6177 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
6178 sizeof(struct lpfc_sli4_cfg_mhdr));
6179 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
6180 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
6181 length, LPFC_SLI4_MBX_EMBED);
6182
6183 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6184 shdr = (union lpfc_sli4_cfg_shdr *)
6185 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
6186 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6187 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6188 if (rc || shdr_status || shdr_add_status) {
6189 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6190 "3026 Mailbox failed , mbxCmd x%x "
6191 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
6192 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6193 bf_get(lpfc_mqe_status, &pmb->u.mqe));
6194 rc = -EIO;
6195 goto read_cfg_out;
6196 }
6197
6198 /* search for fc_fcoe resrouce descriptor */
6199 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
6200 desc_count = get_func_cfg->func_cfg.rsrc_desc_count;
6201
6202 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
6203 desc = (struct lpfc_rsrc_desc_fcfcoe *)
6204 &get_func_cfg->func_cfg.desc[i];
6205 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
6206 bf_get(lpfc_rsrc_desc_pcie_type, desc)) {
6207 phba->sli4_hba.iov.pf_number =
6208 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
6209 phba->sli4_hba.iov.vf_number =
6210 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
6211 break;
6212 }
6213 }
6214
6215 if (i < LPFC_RSRC_DESC_MAX_NUM)
6216 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6217 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
6218 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
6219 phba->sli4_hba.iov.vf_number);
6220 else {
6221 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6222 "3028 GET_FUNCTION_CONFIG: failed to find "
6223 "Resrouce Descriptor:x%x\n",
6224 LPFC_RSRC_DESC_TYPE_FCFCOE);
6225 rc = -EIO;
6226 }
6227
6228read_cfg_out:
6229 mempool_free(pmb, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04006230 return rc;
6231}
6232
6233/**
James Smart2fcee4b2010-12-15 17:57:46 -05006234 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
James Smartda0436e2009-05-22 14:51:39 -04006235 * @phba: pointer to lpfc hba data structure.
6236 *
James Smart2fcee4b2010-12-15 17:57:46 -05006237 * This routine is invoked to setup the port-side endian order when
6238 * the port if_type is 0. This routine has no function for other
6239 * if_types.
James Smartda0436e2009-05-22 14:51:39 -04006240 *
6241 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006242 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006243 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006244 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006245 **/
6246static int
6247lpfc_setup_endian_order(struct lpfc_hba *phba)
6248{
6249 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05006250 uint32_t if_type, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04006251 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
6252 HOST_ENDIAN_HIGH_WORD1};
6253
James Smart2fcee4b2010-12-15 17:57:46 -05006254 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
6255 switch (if_type) {
6256 case LPFC_SLI_INTF_IF_TYPE_0:
6257 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6258 GFP_KERNEL);
6259 if (!mboxq) {
6260 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6261 "0492 Unable to allocate memory for "
6262 "issuing SLI_CONFIG_SPECIAL mailbox "
6263 "command\n");
6264 return -ENOMEM;
6265 }
James Smartda0436e2009-05-22 14:51:39 -04006266
James Smart2fcee4b2010-12-15 17:57:46 -05006267 /*
6268 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
6269 * two words to contain special data values and no other data.
6270 */
6271 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
6272 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
6273 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6274 if (rc != MBX_SUCCESS) {
6275 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6276 "0493 SLI_CONFIG_SPECIAL mailbox "
6277 "failed with status x%x\n",
6278 rc);
6279 rc = -EIO;
6280 }
6281 mempool_free(mboxq, phba->mbox_mem_pool);
6282 break;
6283 case LPFC_SLI_INTF_IF_TYPE_2:
6284 case LPFC_SLI_INTF_IF_TYPE_1:
6285 default:
6286 break;
James Smartda0436e2009-05-22 14:51:39 -04006287 }
James Smartda0436e2009-05-22 14:51:39 -04006288 return rc;
6289}
6290
6291/**
James Smart5350d872011-10-10 21:33:49 -04006292 * lpfc_sli4_queue_verify - Verify and update EQ and CQ counts
James Smartda0436e2009-05-22 14:51:39 -04006293 * @phba: pointer to lpfc hba data structure.
6294 *
James Smart5350d872011-10-10 21:33:49 -04006295 * This routine is invoked to check the user settable queue counts for EQs and
6296 * CQs. after this routine is called the counts will be set to valid values that
6297 * adhere to the constraints of the system's interrupt vectors and the port's
6298 * queue resources.
James Smartda0436e2009-05-22 14:51:39 -04006299 *
6300 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006301 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006302 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04006303 **/
6304static int
James Smart5350d872011-10-10 21:33:49 -04006305lpfc_sli4_queue_verify(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04006306{
James Smartda0436e2009-05-22 14:51:39 -04006307 int cfg_fcp_wq_count;
6308 int cfg_fcp_eq_count;
6309
6310 /*
6311 * Sanity check for confiugred queue parameters against the run-time
6312 * device parameters
6313 */
6314
6315 /* Sanity check on FCP fast-path WQ parameters */
6316 cfg_fcp_wq_count = phba->cfg_fcp_wq_count;
6317 if (cfg_fcp_wq_count >
6318 (phba->sli4_hba.max_cfg_param.max_wq - LPFC_SP_WQN_DEF)) {
6319 cfg_fcp_wq_count = phba->sli4_hba.max_cfg_param.max_wq -
6320 LPFC_SP_WQN_DEF;
6321 if (cfg_fcp_wq_count < LPFC_FP_WQN_MIN) {
6322 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6323 "2581 Not enough WQs (%d) from "
6324 "the pci function for supporting "
6325 "FCP WQs (%d)\n",
6326 phba->sli4_hba.max_cfg_param.max_wq,
6327 phba->cfg_fcp_wq_count);
6328 goto out_error;
6329 }
6330 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6331 "2582 Not enough WQs (%d) from the pci "
6332 "function for supporting the requested "
6333 "FCP WQs (%d), the actual FCP WQs can "
6334 "be supported: %d\n",
6335 phba->sli4_hba.max_cfg_param.max_wq,
6336 phba->cfg_fcp_wq_count, cfg_fcp_wq_count);
6337 }
6338 /* The actual number of FCP work queues adopted */
6339 phba->cfg_fcp_wq_count = cfg_fcp_wq_count;
6340
6341 /* Sanity check on FCP fast-path EQ parameters */
6342 cfg_fcp_eq_count = phba->cfg_fcp_eq_count;
6343 if (cfg_fcp_eq_count >
6344 (phba->sli4_hba.max_cfg_param.max_eq - LPFC_SP_EQN_DEF)) {
6345 cfg_fcp_eq_count = phba->sli4_hba.max_cfg_param.max_eq -
6346 LPFC_SP_EQN_DEF;
6347 if (cfg_fcp_eq_count < LPFC_FP_EQN_MIN) {
6348 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6349 "2574 Not enough EQs (%d) from the "
6350 "pci function for supporting FCP "
6351 "EQs (%d)\n",
6352 phba->sli4_hba.max_cfg_param.max_eq,
6353 phba->cfg_fcp_eq_count);
6354 goto out_error;
6355 }
6356 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6357 "2575 Not enough EQs (%d) from the pci "
6358 "function for supporting the requested "
6359 "FCP EQs (%d), the actual FCP EQs can "
6360 "be supported: %d\n",
6361 phba->sli4_hba.max_cfg_param.max_eq,
6362 phba->cfg_fcp_eq_count, cfg_fcp_eq_count);
6363 }
6364 /* It does not make sense to have more EQs than WQs */
6365 if (cfg_fcp_eq_count > phba->cfg_fcp_wq_count) {
6366 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smart6a9c52c2009-10-02 15:16:51 -04006367 "2593 The FCP EQ count(%d) cannot be greater "
6368 "than the FCP WQ count(%d), limiting the "
6369 "FCP EQ count to %d\n", cfg_fcp_eq_count,
James Smartda0436e2009-05-22 14:51:39 -04006370 phba->cfg_fcp_wq_count,
6371 phba->cfg_fcp_wq_count);
6372 cfg_fcp_eq_count = phba->cfg_fcp_wq_count;
6373 }
6374 /* The actual number of FCP event queues adopted */
6375 phba->cfg_fcp_eq_count = cfg_fcp_eq_count;
6376 /* The overall number of event queues used */
6377 phba->sli4_hba.cfg_eqn = phba->cfg_fcp_eq_count + LPFC_SP_EQN_DEF;
6378
James Smartda0436e2009-05-22 14:51:39 -04006379 /* Get EQ depth from module parameter, fake the default for now */
6380 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
6381 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
6382
James Smart5350d872011-10-10 21:33:49 -04006383 /* Get CQ depth from module parameter, fake the default for now */
6384 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
6385 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
6386
6387 return 0;
6388out_error:
6389 return -ENOMEM;
6390}
6391
6392/**
6393 * lpfc_sli4_queue_create - Create all the SLI4 queues
6394 * @phba: pointer to lpfc hba data structure.
6395 *
6396 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
6397 * operation. For each SLI4 queue type, the parameters such as queue entry
6398 * count (queue depth) shall be taken from the module parameter. For now,
6399 * we just use some constant number as place holder.
6400 *
6401 * Return codes
6402 * 0 - sucessful
6403 * -ENOMEM - No availble memory
6404 * -EIO - The mailbox failed to complete successfully.
6405 **/
6406int
6407lpfc_sli4_queue_create(struct lpfc_hba *phba)
6408{
6409 struct lpfc_queue *qdesc;
6410 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
6411
6412 /*
6413 * Create Event Queues (EQs)
6414 */
6415
James Smartda0436e2009-05-22 14:51:39 -04006416 /* Create slow path event queue */
6417 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
6418 phba->sli4_hba.eq_ecount);
6419 if (!qdesc) {
6420 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6421 "0496 Failed allocate slow-path EQ\n");
6422 goto out_error;
6423 }
6424 phba->sli4_hba.sp_eq = qdesc;
6425
James Smart5350d872011-10-10 21:33:49 -04006426 /*
6427 * Create fast-path FCP Event Queue(s). The cfg_fcp_eq_count can be
6428 * zero whenever there is exactly one interrupt vector. This is not
6429 * an error.
6430 */
6431 if (phba->cfg_fcp_eq_count) {
6432 phba->sli4_hba.fp_eq = kzalloc((sizeof(struct lpfc_queue *) *
6433 phba->cfg_fcp_eq_count), GFP_KERNEL);
6434 if (!phba->sli4_hba.fp_eq) {
6435 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6436 "2576 Failed allocate memory for "
6437 "fast-path EQ record array\n");
6438 goto out_free_sp_eq;
6439 }
James Smartda0436e2009-05-22 14:51:39 -04006440 }
6441 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
6442 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
6443 phba->sli4_hba.eq_ecount);
6444 if (!qdesc) {
6445 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6446 "0497 Failed allocate fast-path EQ\n");
6447 goto out_free_fp_eq;
6448 }
6449 phba->sli4_hba.fp_eq[fcp_eqidx] = qdesc;
6450 }
6451
6452 /*
6453 * Create Complete Queues (CQs)
6454 */
6455
James Smartda0436e2009-05-22 14:51:39 -04006456 /* Create slow-path Mailbox Command Complete Queue */
6457 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6458 phba->sli4_hba.cq_ecount);
6459 if (!qdesc) {
6460 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6461 "0500 Failed allocate slow-path mailbox CQ\n");
6462 goto out_free_fp_eq;
6463 }
6464 phba->sli4_hba.mbx_cq = qdesc;
6465
6466 /* Create slow-path ELS Complete Queue */
6467 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6468 phba->sli4_hba.cq_ecount);
6469 if (!qdesc) {
6470 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6471 "0501 Failed allocate slow-path ELS CQ\n");
6472 goto out_free_mbx_cq;
6473 }
6474 phba->sli4_hba.els_cq = qdesc;
6475
James Smartda0436e2009-05-22 14:51:39 -04006476
James Smart5350d872011-10-10 21:33:49 -04006477 /*
6478 * Create fast-path FCP Completion Queue(s), one-to-one with FCP EQs.
6479 * If there are no FCP EQs then create exactly one FCP CQ.
6480 */
6481 if (phba->cfg_fcp_eq_count)
6482 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
6483 phba->cfg_fcp_eq_count),
6484 GFP_KERNEL);
6485 else
6486 phba->sli4_hba.fcp_cq = kzalloc(sizeof(struct lpfc_queue *),
6487 GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04006488 if (!phba->sli4_hba.fcp_cq) {
6489 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6490 "2577 Failed allocate memory for fast-path "
6491 "CQ record array\n");
James Smart4d9ab992009-10-02 15:16:39 -04006492 goto out_free_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04006493 }
James Smart5350d872011-10-10 21:33:49 -04006494 fcp_cqidx = 0;
6495 do {
James Smartda0436e2009-05-22 14:51:39 -04006496 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6497 phba->sli4_hba.cq_ecount);
6498 if (!qdesc) {
6499 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6500 "0499 Failed allocate fast-path FCP "
6501 "CQ (%d)\n", fcp_cqidx);
6502 goto out_free_fcp_cq;
6503 }
6504 phba->sli4_hba.fcp_cq[fcp_cqidx] = qdesc;
James Smart5350d872011-10-10 21:33:49 -04006505 } while (++fcp_cqidx < phba->cfg_fcp_eq_count);
James Smartda0436e2009-05-22 14:51:39 -04006506
6507 /* Create Mailbox Command Queue */
6508 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
6509 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
6510
6511 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
6512 phba->sli4_hba.mq_ecount);
6513 if (!qdesc) {
6514 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6515 "0505 Failed allocate slow-path MQ\n");
6516 goto out_free_fcp_cq;
6517 }
6518 phba->sli4_hba.mbx_wq = qdesc;
6519
6520 /*
6521 * Create all the Work Queues (WQs)
6522 */
6523 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
6524 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
6525
6526 /* Create slow-path ELS Work Queue */
6527 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
6528 phba->sli4_hba.wq_ecount);
6529 if (!qdesc) {
6530 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6531 "0504 Failed allocate slow-path ELS WQ\n");
6532 goto out_free_mbx_wq;
6533 }
6534 phba->sli4_hba.els_wq = qdesc;
6535
6536 /* Create fast-path FCP Work Queue(s) */
6537 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
6538 phba->cfg_fcp_wq_count), GFP_KERNEL);
6539 if (!phba->sli4_hba.fcp_wq) {
6540 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6541 "2578 Failed allocate memory for fast-path "
6542 "WQ record array\n");
6543 goto out_free_els_wq;
6544 }
6545 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
6546 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
6547 phba->sli4_hba.wq_ecount);
6548 if (!qdesc) {
6549 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6550 "0503 Failed allocate fast-path FCP "
6551 "WQ (%d)\n", fcp_wqidx);
6552 goto out_free_fcp_wq;
6553 }
6554 phba->sli4_hba.fcp_wq[fcp_wqidx] = qdesc;
6555 }
6556
6557 /*
6558 * Create Receive Queue (RQ)
6559 */
6560 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
6561 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
6562
6563 /* Create Receive Queue for header */
6564 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
6565 phba->sli4_hba.rq_ecount);
6566 if (!qdesc) {
6567 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6568 "0506 Failed allocate receive HRQ\n");
6569 goto out_free_fcp_wq;
6570 }
6571 phba->sli4_hba.hdr_rq = qdesc;
6572
6573 /* Create Receive Queue for data */
6574 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
6575 phba->sli4_hba.rq_ecount);
6576 if (!qdesc) {
6577 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6578 "0507 Failed allocate receive DRQ\n");
6579 goto out_free_hdr_rq;
6580 }
6581 phba->sli4_hba.dat_rq = qdesc;
6582
6583 return 0;
6584
6585out_free_hdr_rq:
6586 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
6587 phba->sli4_hba.hdr_rq = NULL;
6588out_free_fcp_wq:
6589 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--) {
6590 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_wqidx]);
6591 phba->sli4_hba.fcp_wq[fcp_wqidx] = NULL;
6592 }
6593 kfree(phba->sli4_hba.fcp_wq);
James Smart2e90f4b2011-12-13 13:22:37 -05006594 phba->sli4_hba.fcp_wq = NULL;
James Smartda0436e2009-05-22 14:51:39 -04006595out_free_els_wq:
6596 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
6597 phba->sli4_hba.els_wq = NULL;
6598out_free_mbx_wq:
6599 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
6600 phba->sli4_hba.mbx_wq = NULL;
6601out_free_fcp_cq:
6602 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--) {
6603 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_cqidx]);
6604 phba->sli4_hba.fcp_cq[fcp_cqidx] = NULL;
6605 }
6606 kfree(phba->sli4_hba.fcp_cq);
James Smart2e90f4b2011-12-13 13:22:37 -05006607 phba->sli4_hba.fcp_cq = NULL;
James Smartda0436e2009-05-22 14:51:39 -04006608out_free_els_cq:
6609 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
6610 phba->sli4_hba.els_cq = NULL;
6611out_free_mbx_cq:
6612 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
6613 phba->sli4_hba.mbx_cq = NULL;
6614out_free_fp_eq:
6615 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--) {
6616 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_eqidx]);
6617 phba->sli4_hba.fp_eq[fcp_eqidx] = NULL;
6618 }
6619 kfree(phba->sli4_hba.fp_eq);
James Smart2e90f4b2011-12-13 13:22:37 -05006620 phba->sli4_hba.fp_eq = NULL;
James Smartda0436e2009-05-22 14:51:39 -04006621out_free_sp_eq:
6622 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
6623 phba->sli4_hba.sp_eq = NULL;
6624out_error:
6625 return -ENOMEM;
6626}
6627
6628/**
6629 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
6630 * @phba: pointer to lpfc hba data structure.
6631 *
6632 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
6633 * operation.
6634 *
6635 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006636 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006637 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006638 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006639 **/
James Smart5350d872011-10-10 21:33:49 -04006640void
James Smartda0436e2009-05-22 14:51:39 -04006641lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
6642{
6643 int fcp_qidx;
6644
6645 /* Release mailbox command work queue */
6646 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
6647 phba->sli4_hba.mbx_wq = NULL;
6648
6649 /* Release ELS work queue */
6650 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
6651 phba->sli4_hba.els_wq = NULL;
6652
6653 /* Release FCP work queue */
James Smart2e90f4b2011-12-13 13:22:37 -05006654 if (phba->sli4_hba.fcp_wq != NULL)
6655 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count;
6656 fcp_qidx++)
6657 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_qidx]);
James Smartda0436e2009-05-22 14:51:39 -04006658 kfree(phba->sli4_hba.fcp_wq);
6659 phba->sli4_hba.fcp_wq = NULL;
6660
6661 /* Release unsolicited receive queue */
6662 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
6663 phba->sli4_hba.hdr_rq = NULL;
6664 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
6665 phba->sli4_hba.dat_rq = NULL;
6666
James Smartda0436e2009-05-22 14:51:39 -04006667 /* Release ELS complete queue */
6668 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
6669 phba->sli4_hba.els_cq = NULL;
6670
6671 /* Release mailbox command complete queue */
6672 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
6673 phba->sli4_hba.mbx_cq = NULL;
6674
6675 /* Release FCP response complete queue */
James Smart05580562011-05-24 11:40:48 -04006676 fcp_qidx = 0;
James Smart2e90f4b2011-12-13 13:22:37 -05006677 if (phba->sli4_hba.fcp_cq != NULL)
6678 do
6679 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_qidx]);
6680 while (++fcp_qidx < phba->cfg_fcp_eq_count);
James Smartda0436e2009-05-22 14:51:39 -04006681 kfree(phba->sli4_hba.fcp_cq);
6682 phba->sli4_hba.fcp_cq = NULL;
6683
6684 /* Release fast-path event queue */
James Smart2e90f4b2011-12-13 13:22:37 -05006685 if (phba->sli4_hba.fp_eq != NULL)
6686 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count;
6687 fcp_qidx++)
6688 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_qidx]);
James Smartda0436e2009-05-22 14:51:39 -04006689 kfree(phba->sli4_hba.fp_eq);
6690 phba->sli4_hba.fp_eq = NULL;
6691
6692 /* Release slow-path event queue */
6693 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
6694 phba->sli4_hba.sp_eq = NULL;
6695
6696 return;
6697}
6698
6699/**
6700 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
6701 * @phba: pointer to lpfc hba data structure.
6702 *
6703 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
6704 * operation.
6705 *
6706 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006707 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006708 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006709 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006710 **/
6711int
6712lpfc_sli4_queue_setup(struct lpfc_hba *phba)
6713{
6714 int rc = -ENOMEM;
6715 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
6716 int fcp_cq_index = 0;
6717
6718 /*
6719 * Set up Event Queues (EQs)
6720 */
6721
6722 /* Set up slow-path event queue */
6723 if (!phba->sli4_hba.sp_eq) {
6724 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6725 "0520 Slow-path EQ not allocated\n");
6726 goto out_error;
6727 }
6728 rc = lpfc_eq_create(phba, phba->sli4_hba.sp_eq,
6729 LPFC_SP_DEF_IMAX);
6730 if (rc) {
6731 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6732 "0521 Failed setup of slow-path EQ: "
6733 "rc = 0x%x\n", rc);
6734 goto out_error;
6735 }
6736 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6737 "2583 Slow-path EQ setup: queue-id=%d\n",
6738 phba->sli4_hba.sp_eq->queue_id);
6739
6740 /* Set up fast-path event queue */
James Smart1b511972011-12-13 13:23:09 -05006741 if (phba->cfg_fcp_eq_count && !phba->sli4_hba.fp_eq) {
James Smart2e90f4b2011-12-13 13:22:37 -05006742 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6743 "3147 Fast-path EQs not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05006744 rc = -ENOMEM;
James Smart2e90f4b2011-12-13 13:22:37 -05006745 goto out_destroy_sp_eq;
6746 }
James Smartda0436e2009-05-22 14:51:39 -04006747 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
6748 if (!phba->sli4_hba.fp_eq[fcp_eqidx]) {
6749 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6750 "0522 Fast-path EQ (%d) not "
6751 "allocated\n", fcp_eqidx);
James Smart1b511972011-12-13 13:23:09 -05006752 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04006753 goto out_destroy_fp_eq;
6754 }
6755 rc = lpfc_eq_create(phba, phba->sli4_hba.fp_eq[fcp_eqidx],
6756 phba->cfg_fcp_imax);
6757 if (rc) {
6758 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6759 "0523 Failed setup of fast-path EQ "
6760 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
6761 goto out_destroy_fp_eq;
6762 }
6763 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6764 "2584 Fast-path EQ setup: "
6765 "queue[%d]-id=%d\n", fcp_eqidx,
6766 phba->sli4_hba.fp_eq[fcp_eqidx]->queue_id);
6767 }
6768
6769 /*
6770 * Set up Complete Queues (CQs)
6771 */
6772
6773 /* Set up slow-path MBOX Complete Queue as the first CQ */
6774 if (!phba->sli4_hba.mbx_cq) {
6775 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6776 "0528 Mailbox CQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05006777 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04006778 goto out_destroy_fp_eq;
6779 }
6780 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq, phba->sli4_hba.sp_eq,
6781 LPFC_MCQ, LPFC_MBOX);
6782 if (rc) {
6783 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6784 "0529 Failed setup of slow-path mailbox CQ: "
6785 "rc = 0x%x\n", rc);
6786 goto out_destroy_fp_eq;
6787 }
6788 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6789 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
6790 phba->sli4_hba.mbx_cq->queue_id,
6791 phba->sli4_hba.sp_eq->queue_id);
6792
6793 /* Set up slow-path ELS Complete Queue */
6794 if (!phba->sli4_hba.els_cq) {
6795 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6796 "0530 ELS CQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05006797 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04006798 goto out_destroy_mbx_cq;
6799 }
6800 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq, phba->sli4_hba.sp_eq,
6801 LPFC_WCQ, LPFC_ELS);
6802 if (rc) {
6803 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6804 "0531 Failed setup of slow-path ELS CQ: "
6805 "rc = 0x%x\n", rc);
6806 goto out_destroy_mbx_cq;
6807 }
6808 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6809 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
6810 phba->sli4_hba.els_cq->queue_id,
6811 phba->sli4_hba.sp_eq->queue_id);
6812
James Smartda0436e2009-05-22 14:51:39 -04006813 /* Set up fast-path FCP Response Complete Queue */
James Smart2e90f4b2011-12-13 13:22:37 -05006814 if (!phba->sli4_hba.fcp_cq) {
6815 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6816 "3148 Fast-path FCP CQ array not "
6817 "allocated\n");
James Smart1b511972011-12-13 13:23:09 -05006818 rc = -ENOMEM;
James Smart2e90f4b2011-12-13 13:22:37 -05006819 goto out_destroy_els_cq;
6820 }
James Smart05580562011-05-24 11:40:48 -04006821 fcp_cqidx = 0;
6822 do {
James Smartda0436e2009-05-22 14:51:39 -04006823 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
6824 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6825 "0526 Fast-path FCP CQ (%d) not "
6826 "allocated\n", fcp_cqidx);
James Smart1b511972011-12-13 13:23:09 -05006827 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04006828 goto out_destroy_fcp_cq;
6829 }
James Smart05580562011-05-24 11:40:48 -04006830 if (phba->cfg_fcp_eq_count)
6831 rc = lpfc_cq_create(phba,
6832 phba->sli4_hba.fcp_cq[fcp_cqidx],
6833 phba->sli4_hba.fp_eq[fcp_cqidx],
6834 LPFC_WCQ, LPFC_FCP);
6835 else
6836 rc = lpfc_cq_create(phba,
6837 phba->sli4_hba.fcp_cq[fcp_cqidx],
6838 phba->sli4_hba.sp_eq,
6839 LPFC_WCQ, LPFC_FCP);
James Smartda0436e2009-05-22 14:51:39 -04006840 if (rc) {
6841 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6842 "0527 Failed setup of fast-path FCP "
6843 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
6844 goto out_destroy_fcp_cq;
6845 }
6846 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6847 "2588 FCP CQ setup: cq[%d]-id=%d, "
James Smart05580562011-05-24 11:40:48 -04006848 "parent %seq[%d]-id=%d\n",
James Smartda0436e2009-05-22 14:51:39 -04006849 fcp_cqidx,
6850 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
James Smart05580562011-05-24 11:40:48 -04006851 (phba->cfg_fcp_eq_count) ? "" : "sp_",
James Smartda0436e2009-05-22 14:51:39 -04006852 fcp_cqidx,
James Smart05580562011-05-24 11:40:48 -04006853 (phba->cfg_fcp_eq_count) ?
6854 phba->sli4_hba.fp_eq[fcp_cqidx]->queue_id :
6855 phba->sli4_hba.sp_eq->queue_id);
6856 } while (++fcp_cqidx < phba->cfg_fcp_eq_count);
James Smartda0436e2009-05-22 14:51:39 -04006857
6858 /*
6859 * Set up all the Work Queues (WQs)
6860 */
6861
6862 /* Set up Mailbox Command Queue */
6863 if (!phba->sli4_hba.mbx_wq) {
6864 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6865 "0538 Slow-path MQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05006866 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04006867 goto out_destroy_fcp_cq;
6868 }
6869 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
6870 phba->sli4_hba.mbx_cq, LPFC_MBOX);
6871 if (rc) {
6872 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6873 "0539 Failed setup of slow-path MQ: "
6874 "rc = 0x%x\n", rc);
6875 goto out_destroy_fcp_cq;
6876 }
6877 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6878 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
6879 phba->sli4_hba.mbx_wq->queue_id,
6880 phba->sli4_hba.mbx_cq->queue_id);
6881
6882 /* Set up slow-path ELS Work Queue */
6883 if (!phba->sli4_hba.els_wq) {
6884 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6885 "0536 Slow-path ELS WQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05006886 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04006887 goto out_destroy_mbx_wq;
6888 }
6889 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
6890 phba->sli4_hba.els_cq, LPFC_ELS);
6891 if (rc) {
6892 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6893 "0537 Failed setup of slow-path ELS WQ: "
6894 "rc = 0x%x\n", rc);
6895 goto out_destroy_mbx_wq;
6896 }
6897 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6898 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
6899 phba->sli4_hba.els_wq->queue_id,
6900 phba->sli4_hba.els_cq->queue_id);
6901
6902 /* Set up fast-path FCP Work Queue */
James Smart2e90f4b2011-12-13 13:22:37 -05006903 if (!phba->sli4_hba.fcp_wq) {
6904 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6905 "3149 Fast-path FCP WQ array not "
6906 "allocated\n");
James Smart1b511972011-12-13 13:23:09 -05006907 rc = -ENOMEM;
James Smart2e90f4b2011-12-13 13:22:37 -05006908 goto out_destroy_els_wq;
6909 }
James Smartda0436e2009-05-22 14:51:39 -04006910 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
6911 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
6912 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6913 "0534 Fast-path FCP WQ (%d) not "
6914 "allocated\n", fcp_wqidx);
James Smart1b511972011-12-13 13:23:09 -05006915 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04006916 goto out_destroy_fcp_wq;
6917 }
6918 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
6919 phba->sli4_hba.fcp_cq[fcp_cq_index],
6920 LPFC_FCP);
6921 if (rc) {
6922 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6923 "0535 Failed setup of fast-path FCP "
6924 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
6925 goto out_destroy_fcp_wq;
6926 }
6927 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6928 "2591 FCP WQ setup: wq[%d]-id=%d, "
6929 "parent cq[%d]-id=%d\n",
6930 fcp_wqidx,
6931 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
6932 fcp_cq_index,
6933 phba->sli4_hba.fcp_cq[fcp_cq_index]->queue_id);
6934 /* Round robin FCP Work Queue's Completion Queue assignment */
James Smart05580562011-05-24 11:40:48 -04006935 if (phba->cfg_fcp_eq_count)
6936 fcp_cq_index = ((fcp_cq_index + 1) %
6937 phba->cfg_fcp_eq_count);
James Smartda0436e2009-05-22 14:51:39 -04006938 }
6939
6940 /*
6941 * Create Receive Queue (RQ)
6942 */
6943 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
6944 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6945 "0540 Receive Queue not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05006946 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04006947 goto out_destroy_fcp_wq;
6948 }
James Smart73d91e52011-10-10 21:32:10 -04006949
6950 lpfc_rq_adjust_repost(phba, phba->sli4_hba.hdr_rq, LPFC_ELS_HBQ);
6951 lpfc_rq_adjust_repost(phba, phba->sli4_hba.dat_rq, LPFC_ELS_HBQ);
6952
James Smartda0436e2009-05-22 14:51:39 -04006953 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04006954 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04006955 if (rc) {
6956 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6957 "0541 Failed setup of Receive Queue: "
6958 "rc = 0x%x\n", rc);
6959 goto out_destroy_fcp_wq;
6960 }
James Smart73d91e52011-10-10 21:32:10 -04006961
James Smartda0436e2009-05-22 14:51:39 -04006962 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6963 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
6964 "parent cq-id=%d\n",
6965 phba->sli4_hba.hdr_rq->queue_id,
6966 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04006967 phba->sli4_hba.els_cq->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04006968 return 0;
6969
6970out_destroy_fcp_wq:
6971 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
6972 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05006973out_destroy_els_wq:
James Smartda0436e2009-05-22 14:51:39 -04006974 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
6975out_destroy_mbx_wq:
6976 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
6977out_destroy_fcp_cq:
6978 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
6979 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05006980out_destroy_els_cq:
James Smartda0436e2009-05-22 14:51:39 -04006981 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
6982out_destroy_mbx_cq:
6983 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
6984out_destroy_fp_eq:
6985 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
6986 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_eqidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05006987out_destroy_sp_eq:
James Smartda0436e2009-05-22 14:51:39 -04006988 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
6989out_error:
6990 return rc;
6991}
6992
6993/**
6994 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
6995 * @phba: pointer to lpfc hba data structure.
6996 *
6997 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
6998 * operation.
6999 *
7000 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007001 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007002 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007003 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007004 **/
7005void
7006lpfc_sli4_queue_unset(struct lpfc_hba *phba)
7007{
7008 int fcp_qidx;
7009
7010 /* Unset mailbox command work queue */
7011 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
7012 /* Unset ELS work queue */
7013 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7014 /* Unset unsolicited receive queue */
7015 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
7016 /* Unset FCP work queue */
7017 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
7018 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
7019 /* Unset mailbox command complete queue */
7020 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
7021 /* Unset ELS complete queue */
7022 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
James Smartda0436e2009-05-22 14:51:39 -04007023 /* Unset FCP response complete queue */
James Smart2e90f4b2011-12-13 13:22:37 -05007024 if (phba->sli4_hba.fcp_cq) {
7025 fcp_qidx = 0;
7026 do {
7027 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
7028 } while (++fcp_qidx < phba->cfg_fcp_eq_count);
7029 }
James Smartda0436e2009-05-22 14:51:39 -04007030 /* Unset fast-path event queue */
James Smart2e90f4b2011-12-13 13:22:37 -05007031 if (phba->sli4_hba.fp_eq) {
7032 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count;
7033 fcp_qidx++)
7034 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_qidx]);
7035 }
James Smartda0436e2009-05-22 14:51:39 -04007036 /* Unset slow-path event queue */
7037 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
7038}
7039
7040/**
7041 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
7042 * @phba: pointer to lpfc hba data structure.
7043 *
7044 * This routine is invoked to allocate and set up a pool of completion queue
7045 * events. The body of the completion queue event is a completion queue entry
7046 * CQE. For now, this pool is used for the interrupt service routine to queue
7047 * the following HBA completion queue events for the worker thread to process:
7048 * - Mailbox asynchronous events
7049 * - Receive queue completion unsolicited events
7050 * Later, this can be used for all the slow-path events.
7051 *
7052 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007053 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007054 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04007055 **/
7056static int
7057lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
7058{
7059 struct lpfc_cq_event *cq_event;
7060 int i;
7061
7062 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
7063 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
7064 if (!cq_event)
7065 goto out_pool_create_fail;
7066 list_add_tail(&cq_event->list,
7067 &phba->sli4_hba.sp_cqe_event_pool);
7068 }
7069 return 0;
7070
7071out_pool_create_fail:
7072 lpfc_sli4_cq_event_pool_destroy(phba);
7073 return -ENOMEM;
7074}
7075
7076/**
7077 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
7078 * @phba: pointer to lpfc hba data structure.
7079 *
7080 * This routine is invoked to free the pool of completion queue events at
7081 * driver unload time. Note that, it is the responsibility of the driver
7082 * cleanup routine to free all the outstanding completion-queue events
7083 * allocated from this pool back into the pool before invoking this routine
7084 * to destroy the pool.
7085 **/
7086static void
7087lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
7088{
7089 struct lpfc_cq_event *cq_event, *next_cq_event;
7090
7091 list_for_each_entry_safe(cq_event, next_cq_event,
7092 &phba->sli4_hba.sp_cqe_event_pool, list) {
7093 list_del(&cq_event->list);
7094 kfree(cq_event);
7095 }
7096}
7097
7098/**
7099 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
7100 * @phba: pointer to lpfc hba data structure.
7101 *
7102 * This routine is the lock free version of the API invoked to allocate a
7103 * completion-queue event from the free pool.
7104 *
7105 * Return: Pointer to the newly allocated completion-queue event if successful
7106 * NULL otherwise.
7107 **/
7108struct lpfc_cq_event *
7109__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
7110{
7111 struct lpfc_cq_event *cq_event = NULL;
7112
7113 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
7114 struct lpfc_cq_event, list);
7115 return cq_event;
7116}
7117
7118/**
7119 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
7120 * @phba: pointer to lpfc hba data structure.
7121 *
7122 * This routine is the lock version of the API invoked to allocate a
7123 * completion-queue event from the free pool.
7124 *
7125 * Return: Pointer to the newly allocated completion-queue event if successful
7126 * NULL otherwise.
7127 **/
7128struct lpfc_cq_event *
7129lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
7130{
7131 struct lpfc_cq_event *cq_event;
7132 unsigned long iflags;
7133
7134 spin_lock_irqsave(&phba->hbalock, iflags);
7135 cq_event = __lpfc_sli4_cq_event_alloc(phba);
7136 spin_unlock_irqrestore(&phba->hbalock, iflags);
7137 return cq_event;
7138}
7139
7140/**
7141 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
7142 * @phba: pointer to lpfc hba data structure.
7143 * @cq_event: pointer to the completion queue event to be freed.
7144 *
7145 * This routine is the lock free version of the API invoked to release a
7146 * completion-queue event back into the free pool.
7147 **/
7148void
7149__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
7150 struct lpfc_cq_event *cq_event)
7151{
7152 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
7153}
7154
7155/**
7156 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
7157 * @phba: pointer to lpfc hba data structure.
7158 * @cq_event: pointer to the completion queue event to be freed.
7159 *
7160 * This routine is the lock version of the API invoked to release a
7161 * completion-queue event back into the free pool.
7162 **/
7163void
7164lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
7165 struct lpfc_cq_event *cq_event)
7166{
7167 unsigned long iflags;
7168 spin_lock_irqsave(&phba->hbalock, iflags);
7169 __lpfc_sli4_cq_event_release(phba, cq_event);
7170 spin_unlock_irqrestore(&phba->hbalock, iflags);
7171}
7172
7173/**
7174 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
7175 * @phba: pointer to lpfc hba data structure.
7176 *
7177 * This routine is to free all the pending completion-queue events to the
7178 * back into the free pool for device reset.
7179 **/
7180static void
7181lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
7182{
7183 LIST_HEAD(cqelist);
7184 struct lpfc_cq_event *cqe;
7185 unsigned long iflags;
7186
7187 /* Retrieve all the pending WCQEs from pending WCQE lists */
7188 spin_lock_irqsave(&phba->hbalock, iflags);
7189 /* Pending FCP XRI abort events */
7190 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
7191 &cqelist);
7192 /* Pending ELS XRI abort events */
7193 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
7194 &cqelist);
7195 /* Pending asynnc events */
7196 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
7197 &cqelist);
7198 spin_unlock_irqrestore(&phba->hbalock, iflags);
7199
7200 while (!list_empty(&cqelist)) {
7201 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
7202 lpfc_sli4_cq_event_release(phba, cqe);
7203 }
7204}
7205
7206/**
7207 * lpfc_pci_function_reset - Reset pci function.
7208 * @phba: pointer to lpfc hba data structure.
7209 *
7210 * This routine is invoked to request a PCI function reset. It will destroys
7211 * all resources assigned to the PCI function which originates this request.
7212 *
7213 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007214 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007215 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007216 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007217 **/
7218int
7219lpfc_pci_function_reset(struct lpfc_hba *phba)
7220{
7221 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05007222 uint32_t rc = 0, if_type;
James Smartda0436e2009-05-22 14:51:39 -04007223 uint32_t shdr_status, shdr_add_status;
James Smart2fcee4b2010-12-15 17:57:46 -05007224 uint32_t rdy_chk, num_resets = 0, reset_again = 0;
James Smartda0436e2009-05-22 14:51:39 -04007225 union lpfc_sli4_cfg_shdr *shdr;
James Smart2fcee4b2010-12-15 17:57:46 -05007226 struct lpfc_register reg_data;
James Smartda0436e2009-05-22 14:51:39 -04007227
James Smart2fcee4b2010-12-15 17:57:46 -05007228 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7229 switch (if_type) {
7230 case LPFC_SLI_INTF_IF_TYPE_0:
7231 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
7232 GFP_KERNEL);
7233 if (!mboxq) {
7234 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7235 "0494 Unable to allocate memory for "
7236 "issuing SLI_FUNCTION_RESET mailbox "
7237 "command\n");
7238 return -ENOMEM;
7239 }
7240
7241 /* Setup PCI function reset mailbox-ioctl command */
7242 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7243 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
7244 LPFC_SLI4_MBX_EMBED);
7245 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7246 shdr = (union lpfc_sli4_cfg_shdr *)
7247 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7248 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7249 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
7250 &shdr->response);
7251 if (rc != MBX_TIMEOUT)
7252 mempool_free(mboxq, phba->mbox_mem_pool);
7253 if (shdr_status || shdr_add_status || rc) {
7254 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7255 "0495 SLI_FUNCTION_RESET mailbox "
7256 "failed with status x%x add_status x%x,"
7257 " mbx status x%x\n",
7258 shdr_status, shdr_add_status, rc);
7259 rc = -ENXIO;
7260 }
7261 break;
7262 case LPFC_SLI_INTF_IF_TYPE_2:
7263 for (num_resets = 0;
7264 num_resets < MAX_IF_TYPE_2_RESETS;
7265 num_resets++) {
7266 reg_data.word0 = 0;
7267 bf_set(lpfc_sliport_ctrl_end, &reg_data,
7268 LPFC_SLIPORT_LITTLE_ENDIAN);
7269 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
7270 LPFC_SLIPORT_INIT_PORT);
7271 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
7272 CTRLregaddr);
7273
7274 /*
7275 * Poll the Port Status Register and wait for RDY for
7276 * up to 10 seconds. If the port doesn't respond, treat
7277 * it as an error. If the port responds with RN, start
7278 * the loop again.
7279 */
7280 for (rdy_chk = 0; rdy_chk < 1000; rdy_chk++) {
James Smart73d91e52011-10-10 21:32:10 -04007281 msleep(10);
James Smart9940b972011-03-11 16:06:12 -05007282 if (lpfc_readl(phba->sli4_hba.u.if_type2.
7283 STATUSregaddr, &reg_data.word0)) {
7284 rc = -ENODEV;
James Smart73d91e52011-10-10 21:32:10 -04007285 goto out;
James Smart9940b972011-03-11 16:06:12 -05007286 }
James Smart6b5151f2012-01-18 16:24:06 -05007287 if (bf_get(lpfc_sliport_status_rn, &reg_data))
7288 reset_again++;
James Smart2fcee4b2010-12-15 17:57:46 -05007289 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
7290 break;
James Smart2fcee4b2010-12-15 17:57:46 -05007291 }
7292
7293 /*
7294 * If the port responds to the init request with
7295 * reset needed, delay for a bit and restart the loop.
7296 */
James Smart6b5151f2012-01-18 16:24:06 -05007297 if (reset_again && (rdy_chk < 1000)) {
James Smart2fcee4b2010-12-15 17:57:46 -05007298 msleep(10);
7299 reset_again = 0;
7300 continue;
7301 }
7302
7303 /* Detect any port errors. */
James Smart2fcee4b2010-12-15 17:57:46 -05007304 if ((bf_get(lpfc_sliport_status_err, &reg_data)) ||
7305 (rdy_chk >= 1000)) {
7306 phba->work_status[0] = readl(
7307 phba->sli4_hba.u.if_type2.ERR1regaddr);
7308 phba->work_status[1] = readl(
7309 phba->sli4_hba.u.if_type2.ERR2regaddr);
7310 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7311 "2890 Port Error Detected "
7312 "during Port Reset: "
7313 "port status reg 0x%x, "
7314 "error 1=0x%x, error 2=0x%x\n",
7315 reg_data.word0,
7316 phba->work_status[0],
7317 phba->work_status[1]);
7318 rc = -ENODEV;
7319 }
7320
7321 /*
7322 * Terminate the outer loop provided the Port indicated
7323 * ready within 10 seconds.
7324 */
7325 if (rdy_chk < 1000)
7326 break;
7327 }
James Smart05580562011-05-24 11:40:48 -04007328 /* delay driver action following IF_TYPE_2 function reset */
7329 msleep(100);
James Smart2fcee4b2010-12-15 17:57:46 -05007330 break;
7331 case LPFC_SLI_INTF_IF_TYPE_1:
7332 default:
7333 break;
James Smartda0436e2009-05-22 14:51:39 -04007334 }
7335
James Smart73d91e52011-10-10 21:32:10 -04007336out:
James Smart2fcee4b2010-12-15 17:57:46 -05007337 /* Catch the not-ready port failure after a port reset. */
7338 if (num_resets >= MAX_IF_TYPE_2_RESETS)
7339 rc = -ENODEV;
7340
James Smartda0436e2009-05-22 14:51:39 -04007341 return rc;
7342}
7343
7344/**
7345 * lpfc_sli4_send_nop_mbox_cmds - Send sli-4 nop mailbox commands
7346 * @phba: pointer to lpfc hba data structure.
7347 * @cnt: number of nop mailbox commands to send.
7348 *
7349 * This routine is invoked to send a number @cnt of NOP mailbox command and
7350 * wait for each command to complete.
7351 *
7352 * Return: the number of NOP mailbox command completed.
7353 **/
7354static int
7355lpfc_sli4_send_nop_mbox_cmds(struct lpfc_hba *phba, uint32_t cnt)
7356{
7357 LPFC_MBOXQ_t *mboxq;
7358 int length, cmdsent;
7359 uint32_t mbox_tmo;
7360 uint32_t rc = 0;
7361 uint32_t shdr_status, shdr_add_status;
7362 union lpfc_sli4_cfg_shdr *shdr;
7363
7364 if (cnt == 0) {
7365 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7366 "2518 Requested to send 0 NOP mailbox cmd\n");
7367 return cnt;
7368 }
7369
7370 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7371 if (!mboxq) {
7372 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7373 "2519 Unable to allocate memory for issuing "
7374 "NOP mailbox command\n");
7375 return 0;
7376 }
7377
7378 /* Set up NOP SLI4_CONFIG mailbox-ioctl command */
7379 length = (sizeof(struct lpfc_mbx_nop) -
7380 sizeof(struct lpfc_sli4_cfg_mhdr));
7381 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7382 LPFC_MBOX_OPCODE_NOP, length, LPFC_SLI4_MBX_EMBED);
7383
James Smartda0436e2009-05-22 14:51:39 -04007384 for (cmdsent = 0; cmdsent < cnt; cmdsent++) {
7385 if (!phba->sli4_hba.intr_enable)
7386 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -04007387 else {
7388 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04007389 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smarta183a152011-10-10 21:32:43 -04007390 }
James Smartda0436e2009-05-22 14:51:39 -04007391 if (rc == MBX_TIMEOUT)
7392 break;
7393 /* Check return status */
7394 shdr = (union lpfc_sli4_cfg_shdr *)
7395 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7396 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7397 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
7398 &shdr->response);
7399 if (shdr_status || shdr_add_status || rc) {
7400 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7401 "2520 NOP mailbox command failed "
7402 "status x%x add_status x%x mbx "
7403 "status x%x\n", shdr_status,
7404 shdr_add_status, rc);
7405 break;
7406 }
7407 }
7408
7409 if (rc != MBX_TIMEOUT)
7410 mempool_free(mboxq, phba->mbox_mem_pool);
7411
7412 return cmdsent;
7413}
7414
7415/**
James Smartda0436e2009-05-22 14:51:39 -04007416 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
7417 * @phba: pointer to lpfc hba data structure.
7418 *
7419 * This routine is invoked to set up the PCI device memory space for device
7420 * with SLI-4 interface spec.
7421 *
7422 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007423 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007424 * other values - error
7425 **/
7426static int
7427lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
7428{
7429 struct pci_dev *pdev;
7430 unsigned long bar0map_len, bar1map_len, bar2map_len;
7431 int error = -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -05007432 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04007433
7434 /* Obtain PCI device reference */
7435 if (!phba->pcidev)
7436 return error;
7437 else
7438 pdev = phba->pcidev;
7439
7440 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05007441 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
7442 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
7443 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
7444 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smartda0436e2009-05-22 14:51:39 -04007445 return error;
Michael Reed8e685972009-09-18 12:02:05 -05007446 }
7447 }
James Smartda0436e2009-05-22 14:51:39 -04007448
James Smart2fcee4b2010-12-15 17:57:46 -05007449 /*
7450 * The BARs and register set definitions and offset locations are
7451 * dependent on the if_type.
7452 */
7453 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
7454 &phba->sli4_hba.sli_intf.word0)) {
7455 return error;
7456 }
7457
7458 /* There is no SLI3 failback for SLI4 devices. */
7459 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
7460 LPFC_SLI_INTF_VALID) {
7461 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7462 "2894 SLI_INTF reg contents invalid "
7463 "sli_intf reg 0x%x\n",
7464 phba->sli4_hba.sli_intf.word0);
7465 return error;
7466 }
7467
7468 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7469 /*
7470 * Get the bus address of SLI4 device Bar regions and the
7471 * number of bytes required by each mapping. The mapping of the
7472 * particular PCI BARs regions is dependent on the type of
7473 * SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -04007474 */
James Smart1dfb5a42010-02-12 14:40:50 -05007475 if (pci_resource_start(pdev, 0)) {
7476 phba->pci_bar0_map = pci_resource_start(pdev, 0);
7477 bar0map_len = pci_resource_len(pdev, 0);
James Smart2fcee4b2010-12-15 17:57:46 -05007478
7479 /*
7480 * Map SLI4 PCI Config Space Register base to a kernel virtual
7481 * addr
7482 */
7483 phba->sli4_hba.conf_regs_memmap_p =
7484 ioremap(phba->pci_bar0_map, bar0map_len);
7485 if (!phba->sli4_hba.conf_regs_memmap_p) {
7486 dev_printk(KERN_ERR, &pdev->dev,
7487 "ioremap failed for SLI4 PCI config "
7488 "registers.\n");
7489 goto out;
7490 }
7491 /* Set up BAR0 PCI config space register memory map */
7492 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smart1dfb5a42010-02-12 14:40:50 -05007493 } else {
7494 phba->pci_bar0_map = pci_resource_start(pdev, 1);
7495 bar0map_len = pci_resource_len(pdev, 1);
James Smart2fcee4b2010-12-15 17:57:46 -05007496 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
7497 dev_printk(KERN_ERR, &pdev->dev,
7498 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
7499 goto out;
7500 }
7501 phba->sli4_hba.conf_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04007502 ioremap(phba->pci_bar0_map, bar0map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05007503 if (!phba->sli4_hba.conf_regs_memmap_p) {
7504 dev_printk(KERN_ERR, &pdev->dev,
7505 "ioremap failed for SLI4 PCI config "
7506 "registers.\n");
7507 goto out;
7508 }
7509 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smartda0436e2009-05-22 14:51:39 -04007510 }
7511
James Smartc31098c2011-04-16 11:03:33 -04007512 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
7513 (pci_resource_start(pdev, 2))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007514 /*
7515 * Map SLI4 if type 0 HBA Control Register base to a kernel
7516 * virtual address and setup the registers.
7517 */
7518 phba->pci_bar1_map = pci_resource_start(pdev, 2);
7519 bar1map_len = pci_resource_len(pdev, 2);
7520 phba->sli4_hba.ctrl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04007521 ioremap(phba->pci_bar1_map, bar1map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05007522 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
7523 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04007524 "ioremap failed for SLI4 HBA control registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05007525 goto out_iounmap_conf;
7526 }
7527 lpfc_sli4_bar1_register_memmap(phba);
James Smartda0436e2009-05-22 14:51:39 -04007528 }
7529
James Smartc31098c2011-04-16 11:03:33 -04007530 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
7531 (pci_resource_start(pdev, 4))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007532 /*
7533 * Map SLI4 if type 0 HBA Doorbell Register base to a kernel
7534 * virtual address and setup the registers.
7535 */
7536 phba->pci_bar2_map = pci_resource_start(pdev, 4);
7537 bar2map_len = pci_resource_len(pdev, 4);
7538 phba->sli4_hba.drbl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04007539 ioremap(phba->pci_bar2_map, bar2map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05007540 if (!phba->sli4_hba.drbl_regs_memmap_p) {
7541 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04007542 "ioremap failed for SLI4 HBA doorbell registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05007543 goto out_iounmap_ctrl;
7544 }
7545 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
7546 if (error)
7547 goto out_iounmap_all;
James Smartda0436e2009-05-22 14:51:39 -04007548 }
7549
James Smartda0436e2009-05-22 14:51:39 -04007550 return 0;
7551
7552out_iounmap_all:
7553 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
7554out_iounmap_ctrl:
7555 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
7556out_iounmap_conf:
7557 iounmap(phba->sli4_hba.conf_regs_memmap_p);
7558out:
7559 return error;
7560}
7561
7562/**
7563 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
7564 * @phba: pointer to lpfc hba data structure.
7565 *
7566 * This routine is invoked to unset the PCI device memory space for device
7567 * with SLI-4 interface spec.
7568 **/
7569static void
7570lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
7571{
James Smart2e90f4b2011-12-13 13:22:37 -05007572 uint32_t if_type;
7573 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smartda0436e2009-05-22 14:51:39 -04007574
James Smart2e90f4b2011-12-13 13:22:37 -05007575 switch (if_type) {
7576 case LPFC_SLI_INTF_IF_TYPE_0:
7577 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
7578 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
7579 iounmap(phba->sli4_hba.conf_regs_memmap_p);
7580 break;
7581 case LPFC_SLI_INTF_IF_TYPE_2:
7582 iounmap(phba->sli4_hba.conf_regs_memmap_p);
7583 break;
7584 case LPFC_SLI_INTF_IF_TYPE_1:
7585 default:
7586 dev_printk(KERN_ERR, &phba->pcidev->dev,
7587 "FATAL - unsupported SLI4 interface type - %d\n",
7588 if_type);
7589 break;
7590 }
James Smartda0436e2009-05-22 14:51:39 -04007591}
7592
7593/**
James Smart3772a992009-05-22 14:50:54 -04007594 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
7595 * @phba: pointer to lpfc hba data structure.
7596 *
7597 * This routine is invoked to enable the MSI-X interrupt vectors to device
7598 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
7599 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
7600 * invoked, enables either all or nothing, depending on the current
7601 * availability of PCI vector resources. The device driver is responsible
7602 * for calling the individual request_irq() to register each MSI-X vector
7603 * with a interrupt handler, which is done in this function. Note that
7604 * later when device is unloading, the driver should always call free_irq()
7605 * on all MSI-X vectors it has done request_irq() on before calling
7606 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
7607 * will be left with MSI-X enabled and leaks its vectors.
7608 *
7609 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007610 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007611 * other values - error
7612 **/
7613static int
7614lpfc_sli_enable_msix(struct lpfc_hba *phba)
7615{
7616 int rc, i;
7617 LPFC_MBOXQ_t *pmb;
7618
7619 /* Set up MSI-X multi-message vectors */
7620 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
7621 phba->msix_entries[i].entry = i;
7622
7623 /* Configure MSI-X capability structure */
7624 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
7625 ARRAY_SIZE(phba->msix_entries));
7626 if (rc) {
7627 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7628 "0420 PCI enable MSI-X failed (%d)\n", rc);
7629 goto msi_fail_out;
7630 }
7631 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
7632 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7633 "0477 MSI-X entry[%d]: vector=x%x "
7634 "message=%d\n", i,
7635 phba->msix_entries[i].vector,
7636 phba->msix_entries[i].entry);
7637 /*
7638 * Assign MSI-X vectors to interrupt handlers
7639 */
7640
7641 /* vector-0 is associated to slow-path handler */
7642 rc = request_irq(phba->msix_entries[0].vector,
7643 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
7644 LPFC_SP_DRIVER_HANDLER_NAME, phba);
7645 if (rc) {
7646 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7647 "0421 MSI-X slow-path request_irq failed "
7648 "(%d)\n", rc);
7649 goto msi_fail_out;
7650 }
7651
7652 /* vector-1 is associated to fast-path handler */
7653 rc = request_irq(phba->msix_entries[1].vector,
7654 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
7655 LPFC_FP_DRIVER_HANDLER_NAME, phba);
7656
7657 if (rc) {
7658 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7659 "0429 MSI-X fast-path request_irq failed "
7660 "(%d)\n", rc);
7661 goto irq_fail_out;
7662 }
7663
7664 /*
7665 * Configure HBA MSI-X attention conditions to messages
7666 */
7667 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7668
7669 if (!pmb) {
7670 rc = -ENOMEM;
7671 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7672 "0474 Unable to allocate memory for issuing "
7673 "MBOX_CONFIG_MSI command\n");
7674 goto mem_fail_out;
7675 }
7676 rc = lpfc_config_msi(phba, pmb);
7677 if (rc)
7678 goto mbx_fail_out;
7679 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
7680 if (rc != MBX_SUCCESS) {
7681 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
7682 "0351 Config MSI mailbox command failed, "
7683 "mbxCmd x%x, mbxStatus x%x\n",
7684 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
7685 goto mbx_fail_out;
7686 }
7687
7688 /* Free memory allocated for mailbox command */
7689 mempool_free(pmb, phba->mbox_mem_pool);
7690 return rc;
7691
7692mbx_fail_out:
7693 /* Free memory allocated for mailbox command */
7694 mempool_free(pmb, phba->mbox_mem_pool);
7695
7696mem_fail_out:
7697 /* free the irq already requested */
7698 free_irq(phba->msix_entries[1].vector, phba);
7699
7700irq_fail_out:
7701 /* free the irq already requested */
7702 free_irq(phba->msix_entries[0].vector, phba);
7703
7704msi_fail_out:
7705 /* Unconfigure MSI-X capability structure */
7706 pci_disable_msix(phba->pcidev);
7707 return rc;
7708}
7709
7710/**
7711 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
7712 * @phba: pointer to lpfc hba data structure.
7713 *
7714 * This routine is invoked to release the MSI-X vectors and then disable the
7715 * MSI-X interrupt mode to device with SLI-3 interface spec.
7716 **/
7717static void
7718lpfc_sli_disable_msix(struct lpfc_hba *phba)
7719{
7720 int i;
7721
7722 /* Free up MSI-X multi-message vectors */
7723 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
7724 free_irq(phba->msix_entries[i].vector, phba);
7725 /* Disable MSI-X */
7726 pci_disable_msix(phba->pcidev);
7727
7728 return;
7729}
7730
7731/**
7732 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
7733 * @phba: pointer to lpfc hba data structure.
7734 *
7735 * This routine is invoked to enable the MSI interrupt mode to device with
7736 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
7737 * enable the MSI vector. The device driver is responsible for calling the
7738 * request_irq() to register MSI vector with a interrupt the handler, which
7739 * is done in this function.
7740 *
7741 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007742 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007743 * other values - error
7744 */
7745static int
7746lpfc_sli_enable_msi(struct lpfc_hba *phba)
7747{
7748 int rc;
7749
7750 rc = pci_enable_msi(phba->pcidev);
7751 if (!rc)
7752 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7753 "0462 PCI enable MSI mode success.\n");
7754 else {
7755 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7756 "0471 PCI enable MSI mode failed (%d)\n", rc);
7757 return rc;
7758 }
7759
7760 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
7761 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7762 if (rc) {
7763 pci_disable_msi(phba->pcidev);
7764 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7765 "0478 MSI request_irq failed (%d)\n", rc);
7766 }
7767 return rc;
7768}
7769
7770/**
7771 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
7772 * @phba: pointer to lpfc hba data structure.
7773 *
7774 * This routine is invoked to disable the MSI interrupt mode to device with
7775 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
7776 * done request_irq() on before calling pci_disable_msi(). Failure to do so
7777 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
7778 * its vector.
7779 */
7780static void
7781lpfc_sli_disable_msi(struct lpfc_hba *phba)
7782{
7783 free_irq(phba->pcidev->irq, phba);
7784 pci_disable_msi(phba->pcidev);
7785 return;
7786}
7787
7788/**
7789 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
7790 * @phba: pointer to lpfc hba data structure.
7791 *
7792 * This routine is invoked to enable device interrupt and associate driver's
7793 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
7794 * spec. Depends on the interrupt mode configured to the driver, the driver
7795 * will try to fallback from the configured interrupt mode to an interrupt
7796 * mode which is supported by the platform, kernel, and device in the order
7797 * of:
7798 * MSI-X -> MSI -> IRQ.
7799 *
7800 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007801 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007802 * other values - error
7803 **/
7804static uint32_t
7805lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
7806{
7807 uint32_t intr_mode = LPFC_INTR_ERROR;
7808 int retval;
7809
7810 if (cfg_mode == 2) {
7811 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
7812 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
7813 if (!retval) {
7814 /* Now, try to enable MSI-X interrupt mode */
7815 retval = lpfc_sli_enable_msix(phba);
7816 if (!retval) {
7817 /* Indicate initialization to MSI-X mode */
7818 phba->intr_type = MSIX;
7819 intr_mode = 2;
7820 }
7821 }
7822 }
7823
7824 /* Fallback to MSI if MSI-X initialization failed */
7825 if (cfg_mode >= 1 && phba->intr_type == NONE) {
7826 retval = lpfc_sli_enable_msi(phba);
7827 if (!retval) {
7828 /* Indicate initialization to MSI mode */
7829 phba->intr_type = MSI;
7830 intr_mode = 1;
7831 }
7832 }
7833
7834 /* Fallback to INTx if both MSI-X/MSI initalization failed */
7835 if (phba->intr_type == NONE) {
7836 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
7837 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7838 if (!retval) {
7839 /* Indicate initialization to INTx mode */
7840 phba->intr_type = INTx;
7841 intr_mode = 0;
7842 }
7843 }
7844 return intr_mode;
7845}
7846
7847/**
7848 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
7849 * @phba: pointer to lpfc hba data structure.
7850 *
7851 * This routine is invoked to disable device interrupt and disassociate the
7852 * driver's interrupt handler(s) from interrupt vector(s) to device with
7853 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
7854 * release the interrupt vector(s) for the message signaled interrupt.
7855 **/
7856static void
7857lpfc_sli_disable_intr(struct lpfc_hba *phba)
7858{
7859 /* Disable the currently initialized interrupt mode */
7860 if (phba->intr_type == MSIX)
7861 lpfc_sli_disable_msix(phba);
7862 else if (phba->intr_type == MSI)
7863 lpfc_sli_disable_msi(phba);
7864 else if (phba->intr_type == INTx)
7865 free_irq(phba->pcidev->irq, phba);
7866
7867 /* Reset interrupt management states */
7868 phba->intr_type = NONE;
7869 phba->sli.slistat.sli_intr = 0;
7870
7871 return;
7872}
7873
7874/**
James Smartda0436e2009-05-22 14:51:39 -04007875 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
7876 * @phba: pointer to lpfc hba data structure.
7877 *
7878 * This routine is invoked to enable the MSI-X interrupt vectors to device
7879 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
7880 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
7881 * enables either all or nothing, depending on the current availability of
7882 * PCI vector resources. The device driver is responsible for calling the
7883 * individual request_irq() to register each MSI-X vector with a interrupt
7884 * handler, which is done in this function. Note that later when device is
7885 * unloading, the driver should always call free_irq() on all MSI-X vectors
7886 * it has done request_irq() on before calling pci_disable_msix(). Failure
7887 * to do so results in a BUG_ON() and a device will be left with MSI-X
7888 * enabled and leaks its vectors.
7889 *
7890 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007891 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007892 * other values - error
7893 **/
7894static int
7895lpfc_sli4_enable_msix(struct lpfc_hba *phba)
7896{
James Smart75baf692010-06-08 18:31:21 -04007897 int vectors, rc, index;
James Smartda0436e2009-05-22 14:51:39 -04007898
7899 /* Set up MSI-X multi-message vectors */
7900 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
7901 phba->sli4_hba.msix_entries[index].entry = index;
7902
7903 /* Configure MSI-X capability structure */
James Smart75baf692010-06-08 18:31:21 -04007904 vectors = phba->sli4_hba.cfg_eqn;
7905enable_msix_vectors:
James Smartda0436e2009-05-22 14:51:39 -04007906 rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
James Smart75baf692010-06-08 18:31:21 -04007907 vectors);
7908 if (rc > 1) {
7909 vectors = rc;
7910 goto enable_msix_vectors;
7911 } else if (rc) {
James Smartda0436e2009-05-22 14:51:39 -04007912 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7913 "0484 PCI enable MSI-X failed (%d)\n", rc);
7914 goto msi_fail_out;
7915 }
James Smart75baf692010-06-08 18:31:21 -04007916
James Smartda0436e2009-05-22 14:51:39 -04007917 /* Log MSI-X vector assignment */
James Smart75baf692010-06-08 18:31:21 -04007918 for (index = 0; index < vectors; index++)
James Smartda0436e2009-05-22 14:51:39 -04007919 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7920 "0489 MSI-X entry[%d]: vector=x%x "
7921 "message=%d\n", index,
7922 phba->sli4_hba.msix_entries[index].vector,
7923 phba->sli4_hba.msix_entries[index].entry);
7924 /*
7925 * Assign MSI-X vectors to interrupt handlers
7926 */
James Smart05580562011-05-24 11:40:48 -04007927 if (vectors > 1)
7928 rc = request_irq(phba->sli4_hba.msix_entries[0].vector,
7929 &lpfc_sli4_sp_intr_handler, IRQF_SHARED,
7930 LPFC_SP_DRIVER_HANDLER_NAME, phba);
7931 else
7932 /* All Interrupts need to be handled by one EQ */
7933 rc = request_irq(phba->sli4_hba.msix_entries[0].vector,
7934 &lpfc_sli4_intr_handler, IRQF_SHARED,
7935 LPFC_DRIVER_NAME, phba);
James Smartda0436e2009-05-22 14:51:39 -04007936 if (rc) {
7937 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7938 "0485 MSI-X slow-path request_irq failed "
7939 "(%d)\n", rc);
7940 goto msi_fail_out;
7941 }
7942
7943 /* The rest of the vector(s) are associated to fast-path handler(s) */
James Smart75baf692010-06-08 18:31:21 -04007944 for (index = 1; index < vectors; index++) {
James Smartda0436e2009-05-22 14:51:39 -04007945 phba->sli4_hba.fcp_eq_hdl[index - 1].idx = index - 1;
7946 phba->sli4_hba.fcp_eq_hdl[index - 1].phba = phba;
7947 rc = request_irq(phba->sli4_hba.msix_entries[index].vector,
7948 &lpfc_sli4_fp_intr_handler, IRQF_SHARED,
7949 LPFC_FP_DRIVER_HANDLER_NAME,
7950 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
7951 if (rc) {
7952 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7953 "0486 MSI-X fast-path (%d) "
7954 "request_irq failed (%d)\n", index, rc);
7955 goto cfg_fail_out;
7956 }
7957 }
James Smart75baf692010-06-08 18:31:21 -04007958 phba->sli4_hba.msix_vec_nr = vectors;
James Smartda0436e2009-05-22 14:51:39 -04007959
7960 return rc;
7961
7962cfg_fail_out:
7963 /* free the irq already requested */
7964 for (--index; index >= 1; index--)
7965 free_irq(phba->sli4_hba.msix_entries[index - 1].vector,
7966 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
7967
7968 /* free the irq already requested */
7969 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
7970
7971msi_fail_out:
7972 /* Unconfigure MSI-X capability structure */
7973 pci_disable_msix(phba->pcidev);
7974 return rc;
7975}
7976
7977/**
7978 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
7979 * @phba: pointer to lpfc hba data structure.
7980 *
7981 * This routine is invoked to release the MSI-X vectors and then disable the
7982 * MSI-X interrupt mode to device with SLI-4 interface spec.
7983 **/
7984static void
7985lpfc_sli4_disable_msix(struct lpfc_hba *phba)
7986{
7987 int index;
7988
7989 /* Free up MSI-X multi-message vectors */
7990 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
7991
James Smart75baf692010-06-08 18:31:21 -04007992 for (index = 1; index < phba->sli4_hba.msix_vec_nr; index++)
James Smartda0436e2009-05-22 14:51:39 -04007993 free_irq(phba->sli4_hba.msix_entries[index].vector,
7994 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
James Smart75baf692010-06-08 18:31:21 -04007995
James Smartda0436e2009-05-22 14:51:39 -04007996 /* Disable MSI-X */
7997 pci_disable_msix(phba->pcidev);
7998
7999 return;
8000}
8001
8002/**
8003 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
8004 * @phba: pointer to lpfc hba data structure.
8005 *
8006 * This routine is invoked to enable the MSI interrupt mode to device with
8007 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
8008 * to enable the MSI vector. The device driver is responsible for calling
8009 * the request_irq() to register MSI vector with a interrupt the handler,
8010 * which is done in this function.
8011 *
8012 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008013 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008014 * other values - error
8015 **/
8016static int
8017lpfc_sli4_enable_msi(struct lpfc_hba *phba)
8018{
8019 int rc, index;
8020
8021 rc = pci_enable_msi(phba->pcidev);
8022 if (!rc)
8023 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8024 "0487 PCI enable MSI mode success.\n");
8025 else {
8026 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8027 "0488 PCI enable MSI mode failed (%d)\n", rc);
8028 return rc;
8029 }
8030
8031 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
8032 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8033 if (rc) {
8034 pci_disable_msi(phba->pcidev);
8035 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8036 "0490 MSI request_irq failed (%d)\n", rc);
James Smart75baf692010-06-08 18:31:21 -04008037 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008038 }
8039
8040 for (index = 0; index < phba->cfg_fcp_eq_count; index++) {
8041 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8042 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
8043 }
8044
James Smart75baf692010-06-08 18:31:21 -04008045 return 0;
James Smartda0436e2009-05-22 14:51:39 -04008046}
8047
8048/**
8049 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
8050 * @phba: pointer to lpfc hba data structure.
8051 *
8052 * This routine is invoked to disable the MSI interrupt mode to device with
8053 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
8054 * done request_irq() on before calling pci_disable_msi(). Failure to do so
8055 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
8056 * its vector.
8057 **/
8058static void
8059lpfc_sli4_disable_msi(struct lpfc_hba *phba)
8060{
8061 free_irq(phba->pcidev->irq, phba);
8062 pci_disable_msi(phba->pcidev);
8063 return;
8064}
8065
8066/**
8067 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
8068 * @phba: pointer to lpfc hba data structure.
8069 *
8070 * This routine is invoked to enable device interrupt and associate driver's
8071 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
8072 * interface spec. Depends on the interrupt mode configured to the driver,
8073 * the driver will try to fallback from the configured interrupt mode to an
8074 * interrupt mode which is supported by the platform, kernel, and device in
8075 * the order of:
8076 * MSI-X -> MSI -> IRQ.
8077 *
8078 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008079 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008080 * other values - error
8081 **/
8082static uint32_t
8083lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
8084{
8085 uint32_t intr_mode = LPFC_INTR_ERROR;
8086 int retval, index;
8087
8088 if (cfg_mode == 2) {
8089 /* Preparation before conf_msi mbox cmd */
8090 retval = 0;
8091 if (!retval) {
8092 /* Now, try to enable MSI-X interrupt mode */
8093 retval = lpfc_sli4_enable_msix(phba);
8094 if (!retval) {
8095 /* Indicate initialization to MSI-X mode */
8096 phba->intr_type = MSIX;
8097 intr_mode = 2;
8098 }
8099 }
8100 }
8101
8102 /* Fallback to MSI if MSI-X initialization failed */
8103 if (cfg_mode >= 1 && phba->intr_type == NONE) {
8104 retval = lpfc_sli4_enable_msi(phba);
8105 if (!retval) {
8106 /* Indicate initialization to MSI mode */
8107 phba->intr_type = MSI;
8108 intr_mode = 1;
8109 }
8110 }
8111
8112 /* Fallback to INTx if both MSI-X/MSI initalization failed */
8113 if (phba->intr_type == NONE) {
8114 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
8115 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8116 if (!retval) {
8117 /* Indicate initialization to INTx mode */
8118 phba->intr_type = INTx;
8119 intr_mode = 0;
8120 for (index = 0; index < phba->cfg_fcp_eq_count;
8121 index++) {
8122 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8123 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
8124 }
8125 }
8126 }
8127 return intr_mode;
8128}
8129
8130/**
8131 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
8132 * @phba: pointer to lpfc hba data structure.
8133 *
8134 * This routine is invoked to disable device interrupt and disassociate
8135 * the driver's interrupt handler(s) from interrupt vector(s) to device
8136 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
8137 * will release the interrupt vector(s) for the message signaled interrupt.
8138 **/
8139static void
8140lpfc_sli4_disable_intr(struct lpfc_hba *phba)
8141{
8142 /* Disable the currently initialized interrupt mode */
8143 if (phba->intr_type == MSIX)
8144 lpfc_sli4_disable_msix(phba);
8145 else if (phba->intr_type == MSI)
8146 lpfc_sli4_disable_msi(phba);
8147 else if (phba->intr_type == INTx)
8148 free_irq(phba->pcidev->irq, phba);
8149
8150 /* Reset interrupt management states */
8151 phba->intr_type = NONE;
8152 phba->sli.slistat.sli_intr = 0;
8153
8154 return;
8155}
8156
8157/**
James Smart3772a992009-05-22 14:50:54 -04008158 * lpfc_unset_hba - Unset SLI3 hba device initialization
8159 * @phba: pointer to lpfc hba data structure.
8160 *
8161 * This routine is invoked to unset the HBA device initialization steps to
8162 * a device with SLI-3 interface spec.
8163 **/
8164static void
8165lpfc_unset_hba(struct lpfc_hba *phba)
8166{
8167 struct lpfc_vport *vport = phba->pport;
8168 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
8169
8170 spin_lock_irq(shost->host_lock);
8171 vport->load_flag |= FC_UNLOADING;
8172 spin_unlock_irq(shost->host_lock);
8173
8174 lpfc_stop_hba_timers(phba);
8175
8176 phba->pport->work_port_events = 0;
8177
8178 lpfc_sli_hba_down(phba);
8179
8180 lpfc_sli_brdrestart(phba);
8181
8182 lpfc_sli_disable_intr(phba);
8183
8184 return;
8185}
8186
8187/**
James Smartda0436e2009-05-22 14:51:39 -04008188 * lpfc_sli4_unset_hba - Unset SLI4 hba device initialization.
8189 * @phba: pointer to lpfc hba data structure.
8190 *
8191 * This routine is invoked to unset the HBA device initialization steps to
8192 * a device with SLI-4 interface spec.
8193 **/
8194static void
8195lpfc_sli4_unset_hba(struct lpfc_hba *phba)
8196{
8197 struct lpfc_vport *vport = phba->pport;
8198 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
8199
8200 spin_lock_irq(shost->host_lock);
8201 vport->load_flag |= FC_UNLOADING;
8202 spin_unlock_irq(shost->host_lock);
8203
8204 phba->pport->work_port_events = 0;
8205
James Smart3677a3a2010-09-29 11:19:14 -04008206 /* Stop the SLI4 device port */
8207 lpfc_stop_port(phba);
James Smartda0436e2009-05-22 14:51:39 -04008208
8209 lpfc_sli4_disable_intr(phba);
8210
James Smart3677a3a2010-09-29 11:19:14 -04008211 /* Reset SLI4 HBA FCoE function */
8212 lpfc_pci_function_reset(phba);
James Smart5350d872011-10-10 21:33:49 -04008213 lpfc_sli4_queue_destroy(phba);
James Smart3677a3a2010-09-29 11:19:14 -04008214
James Smartda0436e2009-05-22 14:51:39 -04008215 return;
8216}
8217
8218/**
James Smart5af5eee2010-10-22 11:06:38 -04008219 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
8220 * @phba: Pointer to HBA context object.
8221 *
8222 * This function is called in the SLI4 code path to wait for completion
8223 * of device's XRIs exchange busy. It will check the XRI exchange busy
8224 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
8225 * that, it will check the XRI exchange busy on outstanding FCP and ELS
8226 * I/Os every 30 seconds, log error message, and wait forever. Only when
8227 * all XRI exchange busy complete, the driver unload shall proceed with
8228 * invoking the function reset ioctl mailbox command to the CNA and the
8229 * the rest of the driver unload resource release.
8230 **/
8231static void
8232lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
8233{
8234 int wait_time = 0;
8235 int fcp_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
8236 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
8237
8238 while (!fcp_xri_cmpl || !els_xri_cmpl) {
8239 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
8240 if (!fcp_xri_cmpl)
8241 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8242 "2877 FCP XRI exchange busy "
8243 "wait time: %d seconds.\n",
8244 wait_time/1000);
8245 if (!els_xri_cmpl)
8246 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8247 "2878 ELS XRI exchange busy "
8248 "wait time: %d seconds.\n",
8249 wait_time/1000);
8250 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
8251 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
8252 } else {
8253 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
8254 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
8255 }
8256 fcp_xri_cmpl =
8257 list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
8258 els_xri_cmpl =
8259 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
8260 }
8261}
8262
8263/**
James Smartda0436e2009-05-22 14:51:39 -04008264 * lpfc_sli4_hba_unset - Unset the fcoe hba
8265 * @phba: Pointer to HBA context object.
8266 *
8267 * This function is called in the SLI4 code path to reset the HBA's FCoE
8268 * function. The caller is not required to hold any lock. This routine
8269 * issues PCI function reset mailbox command to reset the FCoE function.
8270 * At the end of the function, it calls lpfc_hba_down_post function to
8271 * free any pending commands.
8272 **/
8273static void
8274lpfc_sli4_hba_unset(struct lpfc_hba *phba)
8275{
8276 int wait_cnt = 0;
8277 LPFC_MBOXQ_t *mboxq;
James Smart912e3ac2011-05-24 11:42:11 -04008278 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -04008279
8280 lpfc_stop_hba_timers(phba);
8281 phba->sli4_hba.intr_enable = 0;
8282
8283 /*
8284 * Gracefully wait out the potential current outstanding asynchronous
8285 * mailbox command.
8286 */
8287
8288 /* First, block any pending async mailbox command from posted */
8289 spin_lock_irq(&phba->hbalock);
8290 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
8291 spin_unlock_irq(&phba->hbalock);
8292 /* Now, trying to wait it out if we can */
8293 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8294 msleep(10);
8295 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
8296 break;
8297 }
8298 /* Forcefully release the outstanding mailbox command if timed out */
8299 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8300 spin_lock_irq(&phba->hbalock);
8301 mboxq = phba->sli.mbox_active;
8302 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8303 __lpfc_mbox_cmpl_put(phba, mboxq);
8304 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8305 phba->sli.mbox_active = NULL;
8306 spin_unlock_irq(&phba->hbalock);
8307 }
8308
James Smart5af5eee2010-10-22 11:06:38 -04008309 /* Abort all iocbs associated with the hba */
8310 lpfc_sli_hba_iocb_abort(phba);
8311
8312 /* Wait for completion of device XRI exchange busy */
8313 lpfc_sli4_xri_exchange_busy_wait(phba);
8314
James Smartda0436e2009-05-22 14:51:39 -04008315 /* Disable PCI subsystem interrupt */
8316 lpfc_sli4_disable_intr(phba);
8317
James Smart912e3ac2011-05-24 11:42:11 -04008318 /* Disable SR-IOV if enabled */
8319 if (phba->cfg_sriov_nr_virtfn)
8320 pci_disable_sriov(pdev);
8321
James Smartda0436e2009-05-22 14:51:39 -04008322 /* Stop kthread signal shall trigger work_done one more time */
8323 kthread_stop(phba->worker_thread);
8324
James Smart3677a3a2010-09-29 11:19:14 -04008325 /* Reset SLI4 HBA FCoE function */
8326 lpfc_pci_function_reset(phba);
James Smart5350d872011-10-10 21:33:49 -04008327 lpfc_sli4_queue_destroy(phba);
James Smart3677a3a2010-09-29 11:19:14 -04008328
James Smartda0436e2009-05-22 14:51:39 -04008329 /* Stop the SLI4 device port */
8330 phba->pport->work_port_events = 0;
8331}
8332
James Smart28baac72010-02-12 14:42:03 -05008333 /**
8334 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
8335 * @phba: Pointer to HBA context object.
8336 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
8337 *
8338 * This function is called in the SLI4 code path to read the port's
8339 * sli4 capabilities.
8340 *
8341 * This function may be be called from any context that can block-wait
8342 * for the completion. The expectation is that this routine is called
8343 * typically from probe_one or from the online routine.
8344 **/
8345int
8346lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8347{
8348 int rc;
8349 struct lpfc_mqe *mqe;
8350 struct lpfc_pc_sli4_params *sli4_params;
8351 uint32_t mbox_tmo;
8352
8353 rc = 0;
8354 mqe = &mboxq->u.mqe;
8355
8356 /* Read the port's SLI4 Parameters port capabilities */
James Smartfedd3b72011-02-16 12:39:24 -05008357 lpfc_pc_sli4_params(mboxq);
James Smart28baac72010-02-12 14:42:03 -05008358 if (!phba->sli4_hba.intr_enable)
8359 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8360 else {
James Smarta183a152011-10-10 21:32:43 -04008361 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart28baac72010-02-12 14:42:03 -05008362 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
8363 }
8364
8365 if (unlikely(rc))
8366 return 1;
8367
8368 sli4_params = &phba->sli4_hba.pc_sli4_params;
8369 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
8370 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
8371 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
8372 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
8373 &mqe->un.sli4_params);
8374 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
8375 &mqe->un.sli4_params);
8376 sli4_params->proto_types = mqe->un.sli4_params.word3;
8377 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
8378 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
8379 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
8380 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
8381 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
8382 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
8383 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
8384 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
8385 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
8386 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
8387 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
8388 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
8389 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
8390 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
8391 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
8392 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
8393 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
8394 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
8395 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
8396 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
James Smart05580562011-05-24 11:40:48 -04008397
8398 /* Make sure that sge_supp_len can be handled by the driver */
8399 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
8400 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
8401
James Smart28baac72010-02-12 14:42:03 -05008402 return rc;
8403}
8404
James Smartda0436e2009-05-22 14:51:39 -04008405/**
James Smartfedd3b72011-02-16 12:39:24 -05008406 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
8407 * @phba: Pointer to HBA context object.
8408 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
8409 *
8410 * This function is called in the SLI4 code path to read the port's
8411 * sli4 capabilities.
8412 *
8413 * This function may be be called from any context that can block-wait
8414 * for the completion. The expectation is that this routine is called
8415 * typically from probe_one or from the online routine.
8416 **/
8417int
8418lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8419{
8420 int rc;
8421 struct lpfc_mqe *mqe = &mboxq->u.mqe;
8422 struct lpfc_pc_sli4_params *sli4_params;
James Smarta183a152011-10-10 21:32:43 -04008423 uint32_t mbox_tmo;
James Smartfedd3b72011-02-16 12:39:24 -05008424 int length;
8425 struct lpfc_sli4_parameters *mbx_sli4_parameters;
8426
James Smart6d368e52011-05-24 11:44:12 -04008427 /*
8428 * By default, the driver assumes the SLI4 port requires RPI
8429 * header postings. The SLI4_PARAM response will correct this
8430 * assumption.
8431 */
8432 phba->sli4_hba.rpi_hdrs_in_use = 1;
8433
James Smartfedd3b72011-02-16 12:39:24 -05008434 /* Read the port's SLI4 Config Parameters */
8435 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
8436 sizeof(struct lpfc_sli4_cfg_mhdr));
8437 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
8438 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
8439 length, LPFC_SLI4_MBX_EMBED);
8440 if (!phba->sli4_hba.intr_enable)
8441 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -04008442 else {
8443 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
8444 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
8445 }
James Smartfedd3b72011-02-16 12:39:24 -05008446 if (unlikely(rc))
8447 return rc;
8448 sli4_params = &phba->sli4_hba.pc_sli4_params;
8449 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
8450 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
8451 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
8452 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
8453 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
8454 mbx_sli4_parameters);
8455 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
8456 mbx_sli4_parameters);
8457 if (bf_get(cfg_phwq, mbx_sli4_parameters))
8458 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
8459 else
8460 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
8461 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
8462 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
8463 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
8464 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
8465 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
8466 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
8467 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
8468 mbx_sli4_parameters);
8469 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
8470 mbx_sli4_parameters);
James Smart6d368e52011-05-24 11:44:12 -04008471 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
8472 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
James Smart05580562011-05-24 11:40:48 -04008473
8474 /* Make sure that sge_supp_len can be handled by the driver */
8475 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
8476 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
8477
James Smartfedd3b72011-02-16 12:39:24 -05008478 return 0;
8479}
8480
8481/**
James Smart3772a992009-05-22 14:50:54 -04008482 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
8483 * @pdev: pointer to PCI device
8484 * @pid: pointer to PCI device identifier
8485 *
8486 * This routine is to be called to attach a device with SLI-3 interface spec
8487 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
8488 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
8489 * information of the device and driver to see if the driver state that it can
8490 * support this kind of device. If the match is successful, the driver core
8491 * invokes this routine. If this routine determines it can claim the HBA, it
8492 * does all the initialization that it needs to do to handle the HBA properly.
8493 *
8494 * Return code
8495 * 0 - driver can claim the device
8496 * negative value - driver can not claim the device
8497 **/
8498static int __devinit
8499lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
8500{
8501 struct lpfc_hba *phba;
8502 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04008503 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -04008504 int error;
8505 uint32_t cfg_mode, intr_mode;
8506
8507 /* Allocate memory for HBA structure */
8508 phba = lpfc_hba_alloc(pdev);
8509 if (!phba)
8510 return -ENOMEM;
8511
8512 /* Perform generic PCI device enabling operation */
8513 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -04008514 if (error)
James Smart3772a992009-05-22 14:50:54 -04008515 goto out_free_phba;
James Smart3772a992009-05-22 14:50:54 -04008516
8517 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
8518 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
8519 if (error)
8520 goto out_disable_pci_dev;
8521
8522 /* Set up SLI-3 specific device PCI memory space */
8523 error = lpfc_sli_pci_mem_setup(phba);
8524 if (error) {
8525 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8526 "1402 Failed to set up pci memory space.\n");
8527 goto out_disable_pci_dev;
8528 }
8529
8530 /* Set up phase-1 common device driver resources */
8531 error = lpfc_setup_driver_resource_phase1(phba);
8532 if (error) {
8533 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8534 "1403 Failed to set up driver resource.\n");
8535 goto out_unset_pci_mem_s3;
8536 }
8537
8538 /* Set up SLI-3 specific device driver resources */
8539 error = lpfc_sli_driver_resource_setup(phba);
8540 if (error) {
8541 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8542 "1404 Failed to set up driver resource.\n");
8543 goto out_unset_pci_mem_s3;
8544 }
8545
8546 /* Initialize and populate the iocb list per host */
8547 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
8548 if (error) {
8549 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8550 "1405 Failed to initialize iocb list.\n");
8551 goto out_unset_driver_resource_s3;
8552 }
8553
8554 /* Set up common device driver resources */
8555 error = lpfc_setup_driver_resource_phase2(phba);
8556 if (error) {
8557 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8558 "1406 Failed to set up driver resource.\n");
8559 goto out_free_iocb_list;
8560 }
8561
James Smart079b5c92011-08-21 21:48:49 -04008562 /* Get the default values for Model Name and Description */
8563 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
8564
James Smart3772a992009-05-22 14:50:54 -04008565 /* Create SCSI host to the physical port */
8566 error = lpfc_create_shost(phba);
8567 if (error) {
8568 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8569 "1407 Failed to create scsi host.\n");
8570 goto out_unset_driver_resource;
8571 }
8572
8573 /* Configure sysfs attributes */
8574 vport = phba->pport;
8575 error = lpfc_alloc_sysfs_attr(vport);
8576 if (error) {
8577 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8578 "1476 Failed to allocate sysfs attr\n");
8579 goto out_destroy_shost;
8580 }
8581
James Smart6669f9b2009-10-02 15:16:45 -04008582 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -04008583 /* Now, trying to enable interrupt and bring up the device */
8584 cfg_mode = phba->cfg_use_msi;
8585 while (true) {
8586 /* Put device to a known state before enabling interrupt */
8587 lpfc_stop_port(phba);
8588 /* Configure and enable interrupt */
8589 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
8590 if (intr_mode == LPFC_INTR_ERROR) {
8591 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8592 "0431 Failed to enable interrupt.\n");
8593 error = -ENODEV;
8594 goto out_free_sysfs_attr;
8595 }
8596 /* SLI-3 HBA setup */
8597 if (lpfc_sli_hba_setup(phba)) {
8598 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8599 "1477 Failed to set up hba\n");
8600 error = -ENODEV;
8601 goto out_remove_device;
8602 }
8603
8604 /* Wait 50ms for the interrupts of previous mailbox commands */
8605 msleep(50);
8606 /* Check active interrupts on message signaled interrupts */
8607 if (intr_mode == 0 ||
8608 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
8609 /* Log the current active interrupt mode */
8610 phba->intr_mode = intr_mode;
8611 lpfc_log_intr_mode(phba, intr_mode);
8612 break;
8613 } else {
8614 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8615 "0447 Configure interrupt mode (%d) "
8616 "failed active interrupt test.\n",
8617 intr_mode);
8618 /* Disable the current interrupt mode */
8619 lpfc_sli_disable_intr(phba);
8620 /* Try next level of interrupt mode */
8621 cfg_mode = --intr_mode;
8622 }
8623 }
8624
8625 /* Perform post initialization setup */
8626 lpfc_post_init_setup(phba);
8627
8628 /* Check if there are static vports to be created. */
8629 lpfc_create_static_vport(phba);
8630
8631 return 0;
8632
8633out_remove_device:
8634 lpfc_unset_hba(phba);
8635out_free_sysfs_attr:
8636 lpfc_free_sysfs_attr(vport);
8637out_destroy_shost:
8638 lpfc_destroy_shost(phba);
8639out_unset_driver_resource:
8640 lpfc_unset_driver_resource_phase2(phba);
8641out_free_iocb_list:
8642 lpfc_free_iocb_list(phba);
8643out_unset_driver_resource_s3:
8644 lpfc_sli_driver_resource_unset(phba);
8645out_unset_pci_mem_s3:
8646 lpfc_sli_pci_mem_unset(phba);
8647out_disable_pci_dev:
8648 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04008649 if (shost)
8650 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -04008651out_free_phba:
8652 lpfc_hba_free(phba);
8653 return error;
8654}
8655
8656/**
8657 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -04008658 * @pdev: pointer to PCI device
8659 *
James Smart3772a992009-05-22 14:50:54 -04008660 * This routine is to be called to disattach a device with SLI-3 interface
8661 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
8662 * removed from PCI bus, it performs all the necessary cleanup for the HBA
8663 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -04008664 **/
dea31012005-04-17 16:05:31 -05008665static void __devexit
James Smart3772a992009-05-22 14:50:54 -04008666lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -05008667{
James Smart2e0fef82007-06-17 19:56:36 -05008668 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8669 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -05008670 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -05008671 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -05008672 int i;
Tomohiro Kusumi8a4df122008-01-11 01:53:00 -05008673 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
8674
James Smart549e55c2007-08-02 11:09:51 -04008675 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04008676 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04008677 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008678
James Smart858c9f62007-06-17 19:56:39 -05008679 lpfc_free_sysfs_attr(vport);
8680
James Smarteada2722008-12-04 22:39:13 -05008681 /* Release all the vports against this physical port */
8682 vports = lpfc_create_vport_work_array(phba);
8683 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -04008684 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
James Smarteada2722008-12-04 22:39:13 -05008685 fc_vport_terminate(vports[i]->fc_vport);
8686 lpfc_destroy_vport_work_array(phba, vports);
8687
8688 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -05008689 fc_remove_host(shost);
8690 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04008691 lpfc_cleanup(vport);
8692
James Smart2e0fef82007-06-17 19:56:36 -05008693 /*
8694 * Bring down the SLI Layer. This step disable all interrupts,
8695 * clears the rings, discards all mailbox commands, and resets
8696 * the HBA.
8697 */
James Smarta257bf92009-04-06 18:48:10 -04008698
Justin P. Mattock48e34d02010-12-30 15:07:58 -08008699 /* HBA interrupt will be disabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -05008700 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -04008701 /* Stop kthread signal shall trigger work_done one more time */
8702 kthread_stop(phba->worker_thread);
8703 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -05008704 lpfc_sli_brdrestart(phba);
8705
James Smart3772a992009-05-22 14:50:54 -04008706 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05008707 spin_lock_irq(&phba->hbalock);
8708 list_del_init(&vport->listentry);
8709 spin_unlock_irq(&phba->hbalock);
8710
James Smart858c9f62007-06-17 19:56:39 -05008711 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05008712
James Smart912e3ac2011-05-24 11:42:11 -04008713 /* Disable SR-IOV if enabled */
8714 if (phba->cfg_sriov_nr_virtfn)
8715 pci_disable_sriov(pdev);
8716
James Smart5b75da22008-12-04 22:39:35 -05008717 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04008718 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -05008719
8720 pci_set_drvdata(pdev, NULL);
James Smart858c9f62007-06-17 19:56:39 -05008721 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05008722
8723 /*
8724 * Call scsi_free before mem_free since scsi bufs are released to their
8725 * corresponding pools here.
8726 */
8727 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -04008728 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -05008729
James Smart34b02dc2008-08-24 21:49:55 -04008730 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
8731 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -05008732
James Smart2e0fef82007-06-17 19:56:36 -05008733 /* Free resources associated with SLI2 interface */
8734 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -04008735 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -05008736
8737 /* unmap adapter SLIM and Control Registers */
8738 iounmap(phba->ctrl_regs_memmap_p);
8739 iounmap(phba->slim_memmap_p);
8740
James Smart3772a992009-05-22 14:50:54 -04008741 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -05008742
Tomohiro Kusumi8a4df122008-01-11 01:53:00 -05008743 pci_release_selected_regions(pdev, bars);
James Smart2e0fef82007-06-17 19:56:36 -05008744 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05008745}
8746
Linas Vepstas8d63f372007-02-14 14:28:36 -06008747/**
James Smart3772a992009-05-22 14:50:54 -04008748 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05008749 * @pdev: pointer to PCI device
8750 * @msg: power management message
8751 *
James Smart3772a992009-05-22 14:50:54 -04008752 * This routine is to be called from the kernel's PCI subsystem to support
8753 * system Power Management (PM) to device with SLI-3 interface spec. When
8754 * PM invokes this method, it quiesces the device by stopping the driver's
8755 * worker thread for the device, turning off device's interrupt and DMA,
8756 * and bring the device offline. Note that as the driver implements the
8757 * minimum PM requirements to a power-aware driver's PM support for the
8758 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
8759 * to the suspend() method call will be treated as SUSPEND and the driver will
8760 * fully reinitialize its device during resume() method call, the driver will
8761 * set device to PCI_D3hot state in PCI config space instead of setting it
8762 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -05008763 *
8764 * Return code
James Smart3772a992009-05-22 14:50:54 -04008765 * 0 - driver suspended the device
8766 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05008767 **/
8768static int
James Smart3772a992009-05-22 14:50:54 -04008769lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -05008770{
8771 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8772 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8773
8774 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8775 "0473 PCI device Power Management suspend.\n");
8776
8777 /* Bring down the device */
8778 lpfc_offline_prep(phba);
8779 lpfc_offline(phba);
8780 kthread_stop(phba->worker_thread);
8781
8782 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -04008783 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -05008784
8785 /* Save device state to PCI config space */
8786 pci_save_state(pdev);
8787 pci_set_power_state(pdev, PCI_D3hot);
8788
8789 return 0;
8790}
8791
8792/**
James Smart3772a992009-05-22 14:50:54 -04008793 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05008794 * @pdev: pointer to PCI device
8795 *
James Smart3772a992009-05-22 14:50:54 -04008796 * This routine is to be called from the kernel's PCI subsystem to support
8797 * system Power Management (PM) to device with SLI-3 interface spec. When PM
8798 * invokes this method, it restores the device's PCI config space state and
8799 * fully reinitializes the device and brings it online. Note that as the
8800 * driver implements the minimum PM requirements to a power-aware driver's
8801 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
8802 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
8803 * driver will fully reinitialize its device during resume() method call,
8804 * the device will be set to PCI_D0 directly in PCI config space before
8805 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -05008806 *
8807 * Return code
James Smart3772a992009-05-22 14:50:54 -04008808 * 0 - driver suspended the device
8809 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05008810 **/
8811static int
James Smart3772a992009-05-22 14:50:54 -04008812lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -05008813{
8814 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8815 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -05008816 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05008817 int error;
8818
8819 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8820 "0452 PCI device Power Management resume.\n");
8821
8822 /* Restore device state from PCI config space */
8823 pci_set_power_state(pdev, PCI_D0);
8824 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -04008825
James Smart1dfb5a42010-02-12 14:40:50 -05008826 /*
8827 * As the new kernel behavior of pci_restore_state() API call clears
8828 * device saved_state flag, need to save the restored state again.
8829 */
8830 pci_save_state(pdev);
8831
James Smart3a55b532008-12-04 22:38:54 -05008832 if (pdev->is_busmaster)
8833 pci_set_master(pdev);
8834
8835 /* Startup the kernel thread for this host adapter. */
8836 phba->worker_thread = kthread_run(lpfc_do_work, phba,
8837 "lpfc_worker_%d", phba->brd_no);
8838 if (IS_ERR(phba->worker_thread)) {
8839 error = PTR_ERR(phba->worker_thread);
8840 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8841 "0434 PM resume failed to start worker "
8842 "thread: error=x%x.\n", error);
8843 return error;
8844 }
8845
James Smart5b75da22008-12-04 22:39:35 -05008846 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04008847 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05008848 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -05008849 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -05008850 "0430 PM resume Failed to enable interrupt\n");
8851 return -EIO;
8852 } else
8853 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05008854
8855 /* Restart HBA and bring it online */
8856 lpfc_sli_brdrestart(phba);
8857 lpfc_online(phba);
8858
James Smart5b75da22008-12-04 22:39:35 -05008859 /* Log the current active interrupt mode */
8860 lpfc_log_intr_mode(phba, phba->intr_mode);
8861
James Smart3a55b532008-12-04 22:38:54 -05008862 return 0;
8863}
8864
8865/**
James Smart891478a2009-11-18 15:40:23 -05008866 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
8867 * @phba: pointer to lpfc hba data structure.
8868 *
8869 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -04008870 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -05008871 **/
8872static void
8873lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
8874{
James Smarte2af0d22010-03-15 11:25:32 -04008875 struct lpfc_sli *psli = &phba->sli;
8876 struct lpfc_sli_ring *pring;
8877
James Smart891478a2009-11-18 15:40:23 -05008878 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8879 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -04008880
8881 /*
8882 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
8883 * and let the SCSI mid-layer to retry them to recover.
8884 */
8885 pring = &psli->ring[psli->fcp_ring];
8886 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart891478a2009-11-18 15:40:23 -05008887}
8888
8889/**
James Smart0d878412009-10-02 15:16:56 -04008890 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
8891 * @phba: pointer to lpfc hba data structure.
8892 *
8893 * This routine is called to prepare the SLI3 device for PCI slot reset. It
8894 * disables the device interrupt and pci device, and aborts the internal FCP
8895 * pending I/Os.
8896 **/
8897static void
8898lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
8899{
James Smart0d878412009-10-02 15:16:56 -04008900 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05008901 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -04008902
James Smart75baf692010-06-08 18:31:21 -04008903 /* Block any management I/Os to the device */
8904 lpfc_block_mgmt_io(phba);
8905
James Smarte2af0d22010-03-15 11:25:32 -04008906 /* Block all SCSI devices' I/Os on the host */
8907 lpfc_scsi_dev_block(phba);
8908
8909 /* stop all timers */
8910 lpfc_stop_hba_timers(phba);
8911
James Smart0d878412009-10-02 15:16:56 -04008912 /* Disable interrupt and pci device */
8913 lpfc_sli_disable_intr(phba);
8914 pci_disable_device(phba->pcidev);
James Smart75baf692010-06-08 18:31:21 -04008915
James Smarte2af0d22010-03-15 11:25:32 -04008916 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
8917 lpfc_sli_flush_fcp_rings(phba);
James Smart0d878412009-10-02 15:16:56 -04008918}
8919
8920/**
8921 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
8922 * @phba: pointer to lpfc hba data structure.
8923 *
8924 * This routine is called to prepare the SLI3 device for PCI slot permanently
8925 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
8926 * pending I/Os.
8927 **/
8928static void
James Smart75baf692010-06-08 18:31:21 -04008929lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
James Smart0d878412009-10-02 15:16:56 -04008930{
8931 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05008932 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -04008933 /* Block all SCSI devices' I/Os on the host */
8934 lpfc_scsi_dev_block(phba);
8935
8936 /* stop all timers */
8937 lpfc_stop_hba_timers(phba);
8938
James Smart0d878412009-10-02 15:16:56 -04008939 /* Clean up all driver's outstanding SCSI I/Os */
8940 lpfc_sli_flush_fcp_rings(phba);
8941}
8942
8943/**
James Smart3772a992009-05-22 14:50:54 -04008944 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -04008945 * @pdev: pointer to PCI device.
8946 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -06008947 *
James Smart3772a992009-05-22 14:50:54 -04008948 * This routine is called from the PCI subsystem for I/O error handling to
8949 * device with SLI-3 interface spec. This function is called by the PCI
8950 * subsystem after a PCI bus error affecting this device has been detected.
8951 * When this function is invoked, it will need to stop all the I/Os and
8952 * interrupt(s) to the device. Once that is done, it will return
8953 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
8954 * as desired.
James Smarte59058c2008-08-24 21:49:00 -04008955 *
8956 * Return codes
James Smart0d878412009-10-02 15:16:56 -04008957 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -04008958 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
8959 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -04008960 **/
James Smart3772a992009-05-22 14:50:54 -04008961static pci_ers_result_t
8962lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -06008963{
James Smart51ef4c22007-08-02 11:10:31 -04008964 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8965 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06008966
James Smart0d878412009-10-02 15:16:56 -04008967 switch (state) {
8968 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -05008969 /* Non-fatal error, prepare for recovery */
8970 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -04008971 return PCI_ERS_RESULT_CAN_RECOVER;
8972 case pci_channel_io_frozen:
8973 /* Fatal error, prepare for slot reset */
8974 lpfc_sli_prep_dev_for_reset(phba);
8975 return PCI_ERS_RESULT_NEED_RESET;
8976 case pci_channel_io_perm_failure:
8977 /* Permanent failure, prepare for device down */
James Smart75baf692010-06-08 18:31:21 -04008978 lpfc_sli_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06008979 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -04008980 default:
8981 /* Unknown state, prepare and request slot reset */
8982 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8983 "0472 Unknown PCI error state: x%x\n", state);
8984 lpfc_sli_prep_dev_for_reset(phba);
8985 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -04008986 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06008987}
8988
8989/**
James Smart3772a992009-05-22 14:50:54 -04008990 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -04008991 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -06008992 *
James Smart3772a992009-05-22 14:50:54 -04008993 * This routine is called from the PCI subsystem for error handling to
8994 * device with SLI-3 interface spec. This is called after PCI bus has been
8995 * reset to restart the PCI card from scratch, as if from a cold-boot.
8996 * During the PCI subsystem error recovery, after driver returns
8997 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
8998 * recovery and then call this routine before calling the .resume method
8999 * to recover the device. This function will initialize the HBA device,
9000 * enable the interrupt, but it will just put the HBA to offline state
9001 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -04009002 *
9003 * Return codes
James Smart3772a992009-05-22 14:50:54 -04009004 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
9005 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -06009006 */
James Smart3772a992009-05-22 14:50:54 -04009007static pci_ers_result_t
9008lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009009{
James Smart51ef4c22007-08-02 11:10:31 -04009010 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9011 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009012 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -05009013 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009014
9015 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +11009016 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -06009017 printk(KERN_ERR "lpfc: Cannot re-enable "
9018 "PCI device after reset.\n");
9019 return PCI_ERS_RESULT_DISCONNECT;
9020 }
9021
James Smart97207482008-12-04 22:39:19 -05009022 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05009023
9024 /*
9025 * As the new kernel behavior of pci_restore_state() API call clears
9026 * device saved_state flag, need to save the restored state again.
9027 */
9028 pci_save_state(pdev);
9029
James Smart97207482008-12-04 22:39:19 -05009030 if (pdev->is_busmaster)
9031 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009032
James Smart92d7f7b2007-06-17 19:56:38 -05009033 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04009034 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05009035 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009036
James Smart5b75da22008-12-04 22:39:35 -05009037 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009038 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05009039 if (intr_mode == LPFC_INTR_ERROR) {
9040 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9041 "0427 Cannot re-enable interrupt after "
9042 "slot reset.\n");
9043 return PCI_ERS_RESULT_DISCONNECT;
9044 } else
9045 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009046
James Smart75baf692010-06-08 18:31:21 -04009047 /* Take device offline, it will perform cleanup */
9048 lpfc_offline_prep(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009049 lpfc_offline(phba);
9050 lpfc_sli_brdrestart(phba);
9051
James Smart5b75da22008-12-04 22:39:35 -05009052 /* Log the current active interrupt mode */
9053 lpfc_log_intr_mode(phba, phba->intr_mode);
9054
Linas Vepstas8d63f372007-02-14 14:28:36 -06009055 return PCI_ERS_RESULT_RECOVERED;
9056}
9057
9058/**
James Smart3772a992009-05-22 14:50:54 -04009059 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -04009060 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -06009061 *
James Smart3772a992009-05-22 14:50:54 -04009062 * This routine is called from the PCI subsystem for error handling to device
9063 * with SLI-3 interface spec. It is called when kernel error recovery tells
9064 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
9065 * error recovery. After this call, traffic can start to flow from this device
9066 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009067 */
James Smart3772a992009-05-22 14:50:54 -04009068static void
9069lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009070{
James Smart51ef4c22007-08-02 11:10:31 -04009071 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9072 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009073
James Smarte2af0d22010-03-15 11:25:32 -04009074 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -04009075 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -04009076
9077 /* Clean up Advanced Error Reporting (AER) if needed */
9078 if (phba->hba_flag & HBA_AER_ENABLED)
9079 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009080}
9081
James Smart3772a992009-05-22 14:50:54 -04009082/**
James Smartda0436e2009-05-22 14:51:39 -04009083 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
9084 * @phba: pointer to lpfc hba data structure.
9085 *
9086 * returns the number of ELS/CT IOCBs to reserve
9087 **/
9088int
9089lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
9090{
9091 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
9092
James Smartf1126682009-06-10 17:22:44 -04009093 if (phba->sli_rev == LPFC_SLI_REV4) {
9094 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -04009095 return 10;
James Smartf1126682009-06-10 17:22:44 -04009096 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -04009097 return 25;
James Smartf1126682009-06-10 17:22:44 -04009098 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -04009099 return 50;
James Smartf1126682009-06-10 17:22:44 -04009100 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -04009101 return 100;
James Smartf1126682009-06-10 17:22:44 -04009102 else
James Smart6a9c52c2009-10-02 15:16:51 -04009103 return 150;
James Smartf1126682009-06-10 17:22:44 -04009104 } else
9105 return 0;
James Smartda0436e2009-05-22 14:51:39 -04009106}
9107
9108/**
James Smart52d52442011-05-24 11:42:45 -04009109 * lpfc_write_firmware - attempt to write a firmware image to the port
9110 * @phba: pointer to lpfc hba data structure.
9111 * @fw: pointer to firmware image returned from request_firmware.
9112 *
9113 * returns the number of bytes written if write is successful.
9114 * returns a negative error value if there were errors.
9115 * returns 0 if firmware matches currently active firmware on port.
9116 **/
9117int
9118lpfc_write_firmware(struct lpfc_hba *phba, const struct firmware *fw)
9119{
James Smart6b5151f2012-01-18 16:24:06 -05009120 char fwrev[FW_REV_STR_SIZE];
James Smart52d52442011-05-24 11:42:45 -04009121 struct lpfc_grp_hdr *image = (struct lpfc_grp_hdr *)fw->data;
9122 struct list_head dma_buffer_list;
9123 int i, rc = 0;
9124 struct lpfc_dmabuf *dmabuf, *next;
9125 uint32_t offset = 0, temp_offset = 0;
9126
9127 INIT_LIST_HEAD(&dma_buffer_list);
James Smart079b5c92011-08-21 21:48:49 -04009128 if ((be32_to_cpu(image->magic_number) != LPFC_GROUP_OJECT_MAGIC_NUM) ||
9129 (bf_get_be32(lpfc_grp_hdr_file_type, image) !=
9130 LPFC_FILE_TYPE_GROUP) ||
9131 (bf_get_be32(lpfc_grp_hdr_id, image) != LPFC_FILE_ID_GROUP) ||
9132 (be32_to_cpu(image->size) != fw->size)) {
James Smart52d52442011-05-24 11:42:45 -04009133 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9134 "3022 Invalid FW image found. "
James Smart079b5c92011-08-21 21:48:49 -04009135 "Magic:%x Type:%x ID:%x\n",
9136 be32_to_cpu(image->magic_number),
9137 bf_get_be32(lpfc_grp_hdr_file_type, image),
9138 bf_get_be32(lpfc_grp_hdr_id, image));
James Smart52d52442011-05-24 11:42:45 -04009139 return -EINVAL;
9140 }
9141 lpfc_decode_firmware_rev(phba, fwrev, 1);
James Smart88a2cfb2011-07-22 18:36:33 -04009142 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
James Smart52d52442011-05-24 11:42:45 -04009143 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9144 "3023 Updating Firmware. Current Version:%s "
9145 "New Version:%s\n",
James Smart88a2cfb2011-07-22 18:36:33 -04009146 fwrev, image->revision);
James Smart52d52442011-05-24 11:42:45 -04009147 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
9148 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
9149 GFP_KERNEL);
9150 if (!dmabuf) {
9151 rc = -ENOMEM;
9152 goto out;
9153 }
9154 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
9155 SLI4_PAGE_SIZE,
9156 &dmabuf->phys,
9157 GFP_KERNEL);
9158 if (!dmabuf->virt) {
9159 kfree(dmabuf);
9160 rc = -ENOMEM;
9161 goto out;
9162 }
9163 list_add_tail(&dmabuf->list, &dma_buffer_list);
9164 }
9165 while (offset < fw->size) {
9166 temp_offset = offset;
9167 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
James Smart079b5c92011-08-21 21:48:49 -04009168 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
James Smart52d52442011-05-24 11:42:45 -04009169 memcpy(dmabuf->virt,
9170 fw->data + temp_offset,
James Smart079b5c92011-08-21 21:48:49 -04009171 fw->size - temp_offset);
9172 temp_offset = fw->size;
James Smart52d52442011-05-24 11:42:45 -04009173 break;
9174 }
James Smart52d52442011-05-24 11:42:45 -04009175 memcpy(dmabuf->virt, fw->data + temp_offset,
9176 SLI4_PAGE_SIZE);
James Smart88a2cfb2011-07-22 18:36:33 -04009177 temp_offset += SLI4_PAGE_SIZE;
James Smart52d52442011-05-24 11:42:45 -04009178 }
9179 rc = lpfc_wr_object(phba, &dma_buffer_list,
9180 (fw->size - offset), &offset);
9181 if (rc) {
9182 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9183 "3024 Firmware update failed. "
9184 "%d\n", rc);
9185 goto out;
9186 }
9187 }
9188 rc = offset;
9189 }
9190out:
9191 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
9192 list_del(&dmabuf->list);
9193 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
9194 dmabuf->virt, dmabuf->phys);
9195 kfree(dmabuf);
9196 }
9197 return rc;
9198}
9199
9200/**
James Smartda0436e2009-05-22 14:51:39 -04009201 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
9202 * @pdev: pointer to PCI device
9203 * @pid: pointer to PCI device identifier
9204 *
9205 * This routine is called from the kernel's PCI subsystem to device with
9206 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
9207 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
9208 * information of the device and driver to see if the driver state that it
9209 * can support this kind of device. If the match is successful, the driver
9210 * core invokes this routine. If this routine determines it can claim the HBA,
9211 * it does all the initialization that it needs to do to handle the HBA
9212 * properly.
9213 *
9214 * Return code
9215 * 0 - driver can claim the device
9216 * negative value - driver can not claim the device
9217 **/
9218static int __devinit
9219lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
9220{
9221 struct lpfc_hba *phba;
9222 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04009223 struct Scsi_Host *shost = NULL;
James Smartda0436e2009-05-22 14:51:39 -04009224 int error;
9225 uint32_t cfg_mode, intr_mode;
9226 int mcnt;
James Smart05580562011-05-24 11:40:48 -04009227 int adjusted_fcp_eq_count;
James Smart52d52442011-05-24 11:42:45 -04009228 const struct firmware *fw;
9229 uint8_t file_name[16];
James Smartda0436e2009-05-22 14:51:39 -04009230
9231 /* Allocate memory for HBA structure */
9232 phba = lpfc_hba_alloc(pdev);
9233 if (!phba)
9234 return -ENOMEM;
9235
9236 /* Perform generic PCI device enabling operation */
9237 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -04009238 if (error)
James Smartda0436e2009-05-22 14:51:39 -04009239 goto out_free_phba;
James Smartda0436e2009-05-22 14:51:39 -04009240
9241 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
9242 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
9243 if (error)
9244 goto out_disable_pci_dev;
9245
9246 /* Set up SLI-4 specific device PCI memory space */
9247 error = lpfc_sli4_pci_mem_setup(phba);
9248 if (error) {
9249 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9250 "1410 Failed to set up pci memory space.\n");
9251 goto out_disable_pci_dev;
9252 }
9253
9254 /* Set up phase-1 common device driver resources */
9255 error = lpfc_setup_driver_resource_phase1(phba);
9256 if (error) {
9257 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9258 "1411 Failed to set up driver resource.\n");
9259 goto out_unset_pci_mem_s4;
9260 }
9261
9262 /* Set up SLI-4 Specific device driver resources */
9263 error = lpfc_sli4_driver_resource_setup(phba);
9264 if (error) {
9265 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9266 "1412 Failed to set up driver resource.\n");
9267 goto out_unset_pci_mem_s4;
9268 }
9269
9270 /* Initialize and populate the iocb list per host */
James Smart2a9bf3d2010-06-07 15:24:45 -04009271
9272 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9273 "2821 initialize iocb list %d.\n",
9274 phba->cfg_iocb_cnt*1024);
9275 error = lpfc_init_iocb_list(phba, phba->cfg_iocb_cnt*1024);
9276
James Smartda0436e2009-05-22 14:51:39 -04009277 if (error) {
9278 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9279 "1413 Failed to initialize iocb list.\n");
9280 goto out_unset_driver_resource_s4;
9281 }
9282
James Smart19ca7602010-11-20 23:11:55 -05009283 INIT_LIST_HEAD(&phba->active_rrq_list);
James Smart7d791df2011-07-22 18:37:52 -04009284 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
James Smart19ca7602010-11-20 23:11:55 -05009285
James Smartda0436e2009-05-22 14:51:39 -04009286 /* Set up common device driver resources */
9287 error = lpfc_setup_driver_resource_phase2(phba);
9288 if (error) {
9289 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9290 "1414 Failed to set up driver resource.\n");
9291 goto out_free_iocb_list;
9292 }
9293
James Smart079b5c92011-08-21 21:48:49 -04009294 /* Get the default values for Model Name and Description */
9295 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
9296
James Smartda0436e2009-05-22 14:51:39 -04009297 /* Create SCSI host to the physical port */
9298 error = lpfc_create_shost(phba);
9299 if (error) {
9300 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9301 "1415 Failed to create scsi host.\n");
9302 goto out_unset_driver_resource;
9303 }
9304
9305 /* Configure sysfs attributes */
9306 vport = phba->pport;
9307 error = lpfc_alloc_sysfs_attr(vport);
9308 if (error) {
9309 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9310 "1416 Failed to allocate sysfs attr\n");
9311 goto out_destroy_shost;
9312 }
9313
James Smart6669f9b2009-10-02 15:16:45 -04009314 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -04009315 /* Now, trying to enable interrupt and bring up the device */
9316 cfg_mode = phba->cfg_use_msi;
9317 while (true) {
9318 /* Put device to a known state before enabling interrupt */
9319 lpfc_stop_port(phba);
9320 /* Configure and enable interrupt */
9321 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
9322 if (intr_mode == LPFC_INTR_ERROR) {
9323 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9324 "0426 Failed to enable interrupt.\n");
9325 error = -ENODEV;
9326 goto out_free_sysfs_attr;
9327 }
James Smart05580562011-05-24 11:40:48 -04009328 /* Default to single EQ for non-MSI-X */
James Smartdef9c7a2009-12-21 17:02:28 -05009329 if (phba->intr_type != MSIX)
James Smart05580562011-05-24 11:40:48 -04009330 adjusted_fcp_eq_count = 0;
9331 else if (phba->sli4_hba.msix_vec_nr <
9332 phba->cfg_fcp_eq_count + 1)
9333 adjusted_fcp_eq_count = phba->sli4_hba.msix_vec_nr - 1;
9334 else
9335 adjusted_fcp_eq_count = phba->cfg_fcp_eq_count;
James Smart05580562011-05-24 11:40:48 -04009336 phba->cfg_fcp_eq_count = adjusted_fcp_eq_count;
James Smartda0436e2009-05-22 14:51:39 -04009337 /* Set up SLI-4 HBA */
9338 if (lpfc_sli4_hba_setup(phba)) {
9339 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9340 "1421 Failed to set up hba\n");
9341 error = -ENODEV;
9342 goto out_disable_intr;
9343 }
9344
9345 /* Send NOP mbx cmds for non-INTx mode active interrupt test */
9346 if (intr_mode != 0)
9347 mcnt = lpfc_sli4_send_nop_mbox_cmds(phba,
9348 LPFC_ACT_INTR_CNT);
9349
9350 /* Check active interrupts received only for MSI/MSI-X */
9351 if (intr_mode == 0 ||
9352 phba->sli.slistat.sli_intr >= LPFC_ACT_INTR_CNT) {
9353 /* Log the current active interrupt mode */
9354 phba->intr_mode = intr_mode;
9355 lpfc_log_intr_mode(phba, intr_mode);
9356 break;
9357 }
9358 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9359 "0451 Configure interrupt mode (%d) "
9360 "failed active interrupt test.\n",
9361 intr_mode);
James Smart2fcee4b2010-12-15 17:57:46 -05009362 /* Unset the previous SLI-4 HBA setup. */
9363 /*
9364 * TODO: Is this operation compatible with IF TYPE 2
9365 * devices? All port state is deleted and cleared.
9366 */
James Smartda0436e2009-05-22 14:51:39 -04009367 lpfc_sli4_unset_hba(phba);
9368 /* Try next level of interrupt mode */
9369 cfg_mode = --intr_mode;
9370 }
9371
9372 /* Perform post initialization setup */
9373 lpfc_post_init_setup(phba);
9374
James Smart026abb82011-12-13 13:20:45 -05009375 /* check for firmware upgrade or downgrade (if_type 2 only) */
9376 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
9377 LPFC_SLI_INTF_IF_TYPE_2) {
9378 snprintf(file_name, 16, "%s.grp", phba->ModelName);
9379 error = request_firmware(&fw, file_name, &phba->pcidev->dev);
9380 if (!error) {
9381 lpfc_write_firmware(phba, fw);
9382 release_firmware(fw);
9383 }
James Smart52d52442011-05-24 11:42:45 -04009384 }
9385
James Smart1c6834a2009-07-19 10:01:26 -04009386 /* Check if there are static vports to be created. */
9387 lpfc_create_static_vport(phba);
James Smartda0436e2009-05-22 14:51:39 -04009388 return 0;
9389
9390out_disable_intr:
9391 lpfc_sli4_disable_intr(phba);
9392out_free_sysfs_attr:
9393 lpfc_free_sysfs_attr(vport);
9394out_destroy_shost:
9395 lpfc_destroy_shost(phba);
9396out_unset_driver_resource:
9397 lpfc_unset_driver_resource_phase2(phba);
9398out_free_iocb_list:
9399 lpfc_free_iocb_list(phba);
9400out_unset_driver_resource_s4:
9401 lpfc_sli4_driver_resource_unset(phba);
9402out_unset_pci_mem_s4:
9403 lpfc_sli4_pci_mem_unset(phba);
9404out_disable_pci_dev:
9405 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04009406 if (shost)
9407 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -04009408out_free_phba:
9409 lpfc_hba_free(phba);
9410 return error;
9411}
9412
9413/**
9414 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
9415 * @pdev: pointer to PCI device
9416 *
9417 * This routine is called from the kernel's PCI subsystem to device with
9418 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
9419 * removed from PCI bus, it performs all the necessary cleanup for the HBA
9420 * device to be removed from the PCI subsystem properly.
9421 **/
9422static void __devexit
9423lpfc_pci_remove_one_s4(struct pci_dev *pdev)
9424{
9425 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9426 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
9427 struct lpfc_vport **vports;
9428 struct lpfc_hba *phba = vport->phba;
9429 int i;
9430
9431 /* Mark the device unloading flag */
9432 spin_lock_irq(&phba->hbalock);
9433 vport->load_flag |= FC_UNLOADING;
9434 spin_unlock_irq(&phba->hbalock);
9435
9436 /* Free the HBA sysfs attributes */
9437 lpfc_free_sysfs_attr(vport);
9438
9439 /* Release all the vports against this physical port */
9440 vports = lpfc_create_vport_work_array(phba);
9441 if (vports != NULL)
9442 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
9443 fc_vport_terminate(vports[i]->fc_vport);
9444 lpfc_destroy_vport_work_array(phba, vports);
9445
9446 /* Remove FC host and then SCSI host with the physical port */
9447 fc_remove_host(shost);
9448 scsi_remove_host(shost);
9449
9450 /* Perform cleanup on the physical port */
9451 lpfc_cleanup(vport);
9452
9453 /*
9454 * Bring down the SLI Layer. This step disables all interrupts,
9455 * clears the rings, discards all mailbox commands, and resets
9456 * the HBA FCoE function.
9457 */
9458 lpfc_debugfs_terminate(vport);
9459 lpfc_sli4_hba_unset(phba);
9460
9461 spin_lock_irq(&phba->hbalock);
9462 list_del_init(&vport->listentry);
9463 spin_unlock_irq(&phba->hbalock);
9464
James Smart3677a3a2010-09-29 11:19:14 -04009465 /* Perform scsi free before driver resource_unset since scsi
James Smartda0436e2009-05-22 14:51:39 -04009466 * buffers are released to their corresponding pools here.
9467 */
9468 lpfc_scsi_free(phba);
9469 lpfc_sli4_driver_resource_unset(phba);
9470
9471 /* Unmap adapter Control and Doorbell registers */
9472 lpfc_sli4_pci_mem_unset(phba);
9473
9474 /* Release PCI resources and disable device's PCI function */
9475 scsi_host_put(shost);
9476 lpfc_disable_pci_dev(phba);
9477
9478 /* Finally, free the driver's device data structure */
9479 lpfc_hba_free(phba);
9480
9481 return;
9482}
9483
9484/**
9485 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
9486 * @pdev: pointer to PCI device
9487 * @msg: power management message
9488 *
9489 * This routine is called from the kernel's PCI subsystem to support system
9490 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
9491 * this method, it quiesces the device by stopping the driver's worker
9492 * thread for the device, turning off device's interrupt and DMA, and bring
9493 * the device offline. Note that as the driver implements the minimum PM
9494 * requirements to a power-aware driver's PM support for suspend/resume -- all
9495 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
9496 * method call will be treated as SUSPEND and the driver will fully
9497 * reinitialize its device during resume() method call, the driver will set
9498 * device to PCI_D3hot state in PCI config space instead of setting it
9499 * according to the @msg provided by the PM.
9500 *
9501 * Return code
9502 * 0 - driver suspended the device
9503 * Error otherwise
9504 **/
9505static int
9506lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
9507{
9508 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9509 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9510
9511 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart75baf692010-06-08 18:31:21 -04009512 "2843 PCI device Power Management suspend.\n");
James Smartda0436e2009-05-22 14:51:39 -04009513
9514 /* Bring down the device */
9515 lpfc_offline_prep(phba);
9516 lpfc_offline(phba);
9517 kthread_stop(phba->worker_thread);
9518
9519 /* Disable interrupt from device */
9520 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -04009521 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04009522
9523 /* Save device state to PCI config space */
9524 pci_save_state(pdev);
9525 pci_set_power_state(pdev, PCI_D3hot);
9526
9527 return 0;
9528}
9529
9530/**
9531 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
9532 * @pdev: pointer to PCI device
9533 *
9534 * This routine is called from the kernel's PCI subsystem to support system
9535 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
9536 * this method, it restores the device's PCI config space state and fully
9537 * reinitializes the device and brings it online. Note that as the driver
9538 * implements the minimum PM requirements to a power-aware driver's PM for
9539 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
9540 * to the suspend() method call will be treated as SUSPEND and the driver
9541 * will fully reinitialize its device during resume() method call, the device
9542 * will be set to PCI_D0 directly in PCI config space before restoring the
9543 * state.
9544 *
9545 * Return code
9546 * 0 - driver suspended the device
9547 * Error otherwise
9548 **/
9549static int
9550lpfc_pci_resume_one_s4(struct pci_dev *pdev)
9551{
9552 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9553 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9554 uint32_t intr_mode;
9555 int error;
9556
9557 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9558 "0292 PCI device Power Management resume.\n");
9559
9560 /* Restore device state from PCI config space */
9561 pci_set_power_state(pdev, PCI_D0);
9562 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05009563
9564 /*
9565 * As the new kernel behavior of pci_restore_state() API call clears
9566 * device saved_state flag, need to save the restored state again.
9567 */
9568 pci_save_state(pdev);
9569
James Smartda0436e2009-05-22 14:51:39 -04009570 if (pdev->is_busmaster)
9571 pci_set_master(pdev);
9572
9573 /* Startup the kernel thread for this host adapter. */
9574 phba->worker_thread = kthread_run(lpfc_do_work, phba,
9575 "lpfc_worker_%d", phba->brd_no);
9576 if (IS_ERR(phba->worker_thread)) {
9577 error = PTR_ERR(phba->worker_thread);
9578 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9579 "0293 PM resume failed to start worker "
9580 "thread: error=x%x.\n", error);
9581 return error;
9582 }
9583
9584 /* Configure and enable interrupt */
9585 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
9586 if (intr_mode == LPFC_INTR_ERROR) {
9587 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9588 "0294 PM resume Failed to enable interrupt\n");
9589 return -EIO;
9590 } else
9591 phba->intr_mode = intr_mode;
9592
9593 /* Restart HBA and bring it online */
9594 lpfc_sli_brdrestart(phba);
9595 lpfc_online(phba);
9596
9597 /* Log the current active interrupt mode */
9598 lpfc_log_intr_mode(phba, phba->intr_mode);
9599
9600 return 0;
9601}
9602
9603/**
James Smart75baf692010-06-08 18:31:21 -04009604 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
9605 * @phba: pointer to lpfc hba data structure.
9606 *
9607 * This routine is called to prepare the SLI4 device for PCI slot recover. It
9608 * aborts all the outstanding SCSI I/Os to the pci device.
9609 **/
9610static void
9611lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
9612{
9613 struct lpfc_sli *psli = &phba->sli;
9614 struct lpfc_sli_ring *pring;
9615
9616 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9617 "2828 PCI channel I/O abort preparing for recovery\n");
9618 /*
9619 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
9620 * and let the SCSI mid-layer to retry them to recover.
9621 */
9622 pring = &psli->ring[psli->fcp_ring];
9623 lpfc_sli_abort_iocb_ring(phba, pring);
9624}
9625
9626/**
9627 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
9628 * @phba: pointer to lpfc hba data structure.
9629 *
9630 * This routine is called to prepare the SLI4 device for PCI slot reset. It
9631 * disables the device interrupt and pci device, and aborts the internal FCP
9632 * pending I/Os.
9633 **/
9634static void
9635lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
9636{
9637 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9638 "2826 PCI channel disable preparing for reset\n");
9639
9640 /* Block any management I/Os to the device */
9641 lpfc_block_mgmt_io(phba);
9642
9643 /* Block all SCSI devices' I/Os on the host */
9644 lpfc_scsi_dev_block(phba);
9645
9646 /* stop all timers */
9647 lpfc_stop_hba_timers(phba);
9648
9649 /* Disable interrupt and pci device */
9650 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -04009651 lpfc_sli4_queue_destroy(phba);
James Smart75baf692010-06-08 18:31:21 -04009652 pci_disable_device(phba->pcidev);
9653
9654 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
9655 lpfc_sli_flush_fcp_rings(phba);
9656}
9657
9658/**
9659 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
9660 * @phba: pointer to lpfc hba data structure.
9661 *
9662 * This routine is called to prepare the SLI4 device for PCI slot permanently
9663 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
9664 * pending I/Os.
9665 **/
9666static void
9667lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
9668{
9669 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9670 "2827 PCI channel permanent disable for failure\n");
9671
9672 /* Block all SCSI devices' I/Os on the host */
9673 lpfc_scsi_dev_block(phba);
9674
9675 /* stop all timers */
9676 lpfc_stop_hba_timers(phba);
9677
9678 /* Clean up all driver's outstanding SCSI I/Os */
9679 lpfc_sli_flush_fcp_rings(phba);
9680}
9681
9682/**
James Smartda0436e2009-05-22 14:51:39 -04009683 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
9684 * @pdev: pointer to PCI device.
9685 * @state: the current PCI connection state.
9686 *
9687 * This routine is called from the PCI subsystem for error handling to device
9688 * with SLI-4 interface spec. This function is called by the PCI subsystem
9689 * after a PCI bus error affecting this device has been detected. When this
9690 * function is invoked, it will need to stop all the I/Os and interrupt(s)
9691 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
9692 * for the PCI subsystem to perform proper recovery as desired.
9693 *
9694 * Return codes
9695 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
9696 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
9697 **/
9698static pci_ers_result_t
9699lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
9700{
James Smart75baf692010-06-08 18:31:21 -04009701 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9702 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9703
9704 switch (state) {
9705 case pci_channel_io_normal:
9706 /* Non-fatal error, prepare for recovery */
9707 lpfc_sli4_prep_dev_for_recover(phba);
9708 return PCI_ERS_RESULT_CAN_RECOVER;
9709 case pci_channel_io_frozen:
9710 /* Fatal error, prepare for slot reset */
9711 lpfc_sli4_prep_dev_for_reset(phba);
9712 return PCI_ERS_RESULT_NEED_RESET;
9713 case pci_channel_io_perm_failure:
9714 /* Permanent failure, prepare for device down */
9715 lpfc_sli4_prep_dev_for_perm_failure(phba);
9716 return PCI_ERS_RESULT_DISCONNECT;
9717 default:
9718 /* Unknown state, prepare and request slot reset */
9719 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9720 "2825 Unknown PCI error state: x%x\n", state);
9721 lpfc_sli4_prep_dev_for_reset(phba);
9722 return PCI_ERS_RESULT_NEED_RESET;
9723 }
James Smartda0436e2009-05-22 14:51:39 -04009724}
9725
9726/**
9727 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
9728 * @pdev: pointer to PCI device.
9729 *
9730 * This routine is called from the PCI subsystem for error handling to device
9731 * with SLI-4 interface spec. It is called after PCI bus has been reset to
9732 * restart the PCI card from scratch, as if from a cold-boot. During the
9733 * PCI subsystem error recovery, after the driver returns
9734 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
9735 * recovery and then call this routine before calling the .resume method to
9736 * recover the device. This function will initialize the HBA device, enable
9737 * the interrupt, but it will just put the HBA to offline state without
9738 * passing any I/O traffic.
9739 *
9740 * Return codes
9741 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
9742 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
9743 */
9744static pci_ers_result_t
9745lpfc_io_slot_reset_s4(struct pci_dev *pdev)
9746{
James Smart75baf692010-06-08 18:31:21 -04009747 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9748 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9749 struct lpfc_sli *psli = &phba->sli;
9750 uint32_t intr_mode;
9751
9752 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
9753 if (pci_enable_device_mem(pdev)) {
9754 printk(KERN_ERR "lpfc: Cannot re-enable "
9755 "PCI device after reset.\n");
9756 return PCI_ERS_RESULT_DISCONNECT;
9757 }
9758
9759 pci_restore_state(pdev);
James Smart0a96e972011-07-22 18:37:28 -04009760
9761 /*
9762 * As the new kernel behavior of pci_restore_state() API call clears
9763 * device saved_state flag, need to save the restored state again.
9764 */
9765 pci_save_state(pdev);
9766
James Smart75baf692010-06-08 18:31:21 -04009767 if (pdev->is_busmaster)
9768 pci_set_master(pdev);
9769
9770 spin_lock_irq(&phba->hbalock);
9771 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
9772 spin_unlock_irq(&phba->hbalock);
9773
9774 /* Configure and enable interrupt */
9775 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
9776 if (intr_mode == LPFC_INTR_ERROR) {
9777 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9778 "2824 Cannot re-enable interrupt after "
9779 "slot reset.\n");
9780 return PCI_ERS_RESULT_DISCONNECT;
9781 } else
9782 phba->intr_mode = intr_mode;
9783
9784 /* Log the current active interrupt mode */
9785 lpfc_log_intr_mode(phba, phba->intr_mode);
9786
James Smartda0436e2009-05-22 14:51:39 -04009787 return PCI_ERS_RESULT_RECOVERED;
9788}
9789
9790/**
9791 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
9792 * @pdev: pointer to PCI device
9793 *
9794 * This routine is called from the PCI subsystem for error handling to device
9795 * with SLI-4 interface spec. It is called when kernel error recovery tells
9796 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
9797 * error recovery. After this call, traffic can start to flow from this device
9798 * again.
9799 **/
9800static void
9801lpfc_io_resume_s4(struct pci_dev *pdev)
9802{
James Smart75baf692010-06-08 18:31:21 -04009803 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9804 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9805
9806 /*
9807 * In case of slot reset, as function reset is performed through
9808 * mailbox command which needs DMA to be enabled, this operation
9809 * has to be moved to the io resume phase. Taking device offline
9810 * will perform the necessary cleanup.
9811 */
9812 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
9813 /* Perform device reset */
9814 lpfc_offline_prep(phba);
9815 lpfc_offline(phba);
9816 lpfc_sli_brdrestart(phba);
9817 /* Bring the device back online */
9818 lpfc_online(phba);
9819 }
9820
9821 /* Clean up Advanced Error Reporting (AER) if needed */
9822 if (phba->hba_flag & HBA_AER_ENABLED)
9823 pci_cleanup_aer_uncorrect_error_status(pdev);
James Smartda0436e2009-05-22 14:51:39 -04009824}
9825
9826/**
James Smart3772a992009-05-22 14:50:54 -04009827 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
9828 * @pdev: pointer to PCI device
9829 * @pid: pointer to PCI device identifier
9830 *
9831 * This routine is to be registered to the kernel's PCI subsystem. When an
9832 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
9833 * at PCI device-specific information of the device and driver to see if the
9834 * driver state that it can support this kind of device. If the match is
9835 * successful, the driver core invokes this routine. This routine dispatches
9836 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
9837 * do all the initialization that it needs to do to handle the HBA device
9838 * properly.
9839 *
9840 * Return code
9841 * 0 - driver can claim the device
9842 * negative value - driver can not claim the device
9843 **/
9844static int __devinit
9845lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
9846{
9847 int rc;
James Smart8fa38512009-07-19 10:01:03 -04009848 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -04009849
James Smart28baac72010-02-12 14:42:03 -05009850 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -04009851 return -ENODEV;
9852
James Smart8fa38512009-07-19 10:01:03 -04009853 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -05009854 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -04009855 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04009856 else
James Smart3772a992009-05-22 14:50:54 -04009857 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04009858
James Smart3772a992009-05-22 14:50:54 -04009859 return rc;
9860}
9861
9862/**
9863 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
9864 * @pdev: pointer to PCI device
9865 *
9866 * This routine is to be registered to the kernel's PCI subsystem. When an
9867 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
9868 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
9869 * remove routine, which will perform all the necessary cleanup for the
9870 * device to be removed from the PCI subsystem properly.
9871 **/
9872static void __devexit
9873lpfc_pci_remove_one(struct pci_dev *pdev)
9874{
9875 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9876 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9877
9878 switch (phba->pci_dev_grp) {
9879 case LPFC_PCI_DEV_LP:
9880 lpfc_pci_remove_one_s3(pdev);
9881 break;
James Smartda0436e2009-05-22 14:51:39 -04009882 case LPFC_PCI_DEV_OC:
9883 lpfc_pci_remove_one_s4(pdev);
9884 break;
James Smart3772a992009-05-22 14:50:54 -04009885 default:
9886 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9887 "1424 Invalid PCI device group: 0x%x\n",
9888 phba->pci_dev_grp);
9889 break;
9890 }
9891 return;
9892}
9893
9894/**
9895 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
9896 * @pdev: pointer to PCI device
9897 * @msg: power management message
9898 *
9899 * This routine is to be registered to the kernel's PCI subsystem to support
9900 * system Power Management (PM). When PM invokes this method, it dispatches
9901 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
9902 * suspend the device.
9903 *
9904 * Return code
9905 * 0 - driver suspended the device
9906 * Error otherwise
9907 **/
9908static int
9909lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
9910{
9911 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9912 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9913 int rc = -ENODEV;
9914
9915 switch (phba->pci_dev_grp) {
9916 case LPFC_PCI_DEV_LP:
9917 rc = lpfc_pci_suspend_one_s3(pdev, msg);
9918 break;
James Smartda0436e2009-05-22 14:51:39 -04009919 case LPFC_PCI_DEV_OC:
9920 rc = lpfc_pci_suspend_one_s4(pdev, msg);
9921 break;
James Smart3772a992009-05-22 14:50:54 -04009922 default:
9923 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9924 "1425 Invalid PCI device group: 0x%x\n",
9925 phba->pci_dev_grp);
9926 break;
9927 }
9928 return rc;
9929}
9930
9931/**
9932 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
9933 * @pdev: pointer to PCI device
9934 *
9935 * This routine is to be registered to the kernel's PCI subsystem to support
9936 * system Power Management (PM). When PM invokes this method, it dispatches
9937 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
9938 * resume the device.
9939 *
9940 * Return code
9941 * 0 - driver suspended the device
9942 * Error otherwise
9943 **/
9944static int
9945lpfc_pci_resume_one(struct pci_dev *pdev)
9946{
9947 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9948 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9949 int rc = -ENODEV;
9950
9951 switch (phba->pci_dev_grp) {
9952 case LPFC_PCI_DEV_LP:
9953 rc = lpfc_pci_resume_one_s3(pdev);
9954 break;
James Smartda0436e2009-05-22 14:51:39 -04009955 case LPFC_PCI_DEV_OC:
9956 rc = lpfc_pci_resume_one_s4(pdev);
9957 break;
James Smart3772a992009-05-22 14:50:54 -04009958 default:
9959 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9960 "1426 Invalid PCI device group: 0x%x\n",
9961 phba->pci_dev_grp);
9962 break;
9963 }
9964 return rc;
9965}
9966
9967/**
9968 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
9969 * @pdev: pointer to PCI device.
9970 * @state: the current PCI connection state.
9971 *
9972 * This routine is registered to the PCI subsystem for error handling. This
9973 * function is called by the PCI subsystem after a PCI bus error affecting
9974 * this device has been detected. When this routine is invoked, it dispatches
9975 * the action to the proper SLI-3 or SLI-4 device error detected handling
9976 * routine, which will perform the proper error detected operation.
9977 *
9978 * Return codes
9979 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
9980 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
9981 **/
9982static pci_ers_result_t
9983lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
9984{
9985 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9986 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9987 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
9988
9989 switch (phba->pci_dev_grp) {
9990 case LPFC_PCI_DEV_LP:
9991 rc = lpfc_io_error_detected_s3(pdev, state);
9992 break;
James Smartda0436e2009-05-22 14:51:39 -04009993 case LPFC_PCI_DEV_OC:
9994 rc = lpfc_io_error_detected_s4(pdev, state);
9995 break;
James Smart3772a992009-05-22 14:50:54 -04009996 default:
9997 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9998 "1427 Invalid PCI device group: 0x%x\n",
9999 phba->pci_dev_grp);
10000 break;
10001 }
10002 return rc;
10003}
10004
10005/**
10006 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
10007 * @pdev: pointer to PCI device.
10008 *
10009 * This routine is registered to the PCI subsystem for error handling. This
10010 * function is called after PCI bus has been reset to restart the PCI card
10011 * from scratch, as if from a cold-boot. When this routine is invoked, it
10012 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
10013 * routine, which will perform the proper device reset.
10014 *
10015 * Return codes
10016 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10017 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10018 **/
10019static pci_ers_result_t
10020lpfc_io_slot_reset(struct pci_dev *pdev)
10021{
10022 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10023 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10024 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
10025
10026 switch (phba->pci_dev_grp) {
10027 case LPFC_PCI_DEV_LP:
10028 rc = lpfc_io_slot_reset_s3(pdev);
10029 break;
James Smartda0436e2009-05-22 14:51:39 -040010030 case LPFC_PCI_DEV_OC:
10031 rc = lpfc_io_slot_reset_s4(pdev);
10032 break;
James Smart3772a992009-05-22 14:50:54 -040010033 default:
10034 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10035 "1428 Invalid PCI device group: 0x%x\n",
10036 phba->pci_dev_grp);
10037 break;
10038 }
10039 return rc;
10040}
10041
10042/**
10043 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
10044 * @pdev: pointer to PCI device
10045 *
10046 * This routine is registered to the PCI subsystem for error handling. It
10047 * is called when kernel error recovery tells the lpfc driver that it is
10048 * OK to resume normal PCI operation after PCI bus error recovery. When
10049 * this routine is invoked, it dispatches the action to the proper SLI-3
10050 * or SLI-4 device io_resume routine, which will resume the device operation.
10051 **/
10052static void
10053lpfc_io_resume(struct pci_dev *pdev)
10054{
10055 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10056 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10057
10058 switch (phba->pci_dev_grp) {
10059 case LPFC_PCI_DEV_LP:
10060 lpfc_io_resume_s3(pdev);
10061 break;
James Smartda0436e2009-05-22 14:51:39 -040010062 case LPFC_PCI_DEV_OC:
10063 lpfc_io_resume_s4(pdev);
10064 break;
James Smart3772a992009-05-22 14:50:54 -040010065 default:
10066 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10067 "1429 Invalid PCI device group: 0x%x\n",
10068 phba->pci_dev_grp);
10069 break;
10070 }
10071 return;
10072}
10073
James Smart3ef6d242012-01-18 16:23:48 -050010074/**
10075 * lpfc_mgmt_open - method called when 'lpfcmgmt' is opened from userspace
10076 * @inode: pointer to the inode representing the lpfcmgmt device
10077 * @filep: pointer to the file representing the open lpfcmgmt device
10078 *
10079 * This routine puts a reference count on the lpfc module whenever the
10080 * character device is opened
10081 **/
10082static int
10083lpfc_mgmt_open(struct inode *inode, struct file *filep)
10084{
10085 try_module_get(THIS_MODULE);
10086 return 0;
10087}
10088
10089/**
10090 * lpfc_mgmt_release - method called when 'lpfcmgmt' is closed in userspace
10091 * @inode: pointer to the inode representing the lpfcmgmt device
10092 * @filep: pointer to the file representing the open lpfcmgmt device
10093 *
10094 * This routine removes a reference count from the lpfc module when the
10095 * character device is closed
10096 **/
10097static int
10098lpfc_mgmt_release(struct inode *inode, struct file *filep)
10099{
10100 module_put(THIS_MODULE);
10101 return 0;
10102}
10103
dea31012005-04-17 16:05:31 -050010104static struct pci_device_id lpfc_id_table[] = {
10105 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
10106 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -040010107 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
10108 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010109 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
10110 PCI_ANY_ID, PCI_ANY_ID, },
10111 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
10112 PCI_ANY_ID, PCI_ANY_ID, },
10113 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
10114 PCI_ANY_ID, PCI_ANY_ID, },
10115 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
10116 PCI_ANY_ID, PCI_ANY_ID, },
10117 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
10118 PCI_ANY_ID, PCI_ANY_ID, },
10119 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
10120 PCI_ANY_ID, PCI_ANY_ID, },
10121 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
10122 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050010123 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
10124 PCI_ANY_ID, PCI_ANY_ID, },
10125 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
10126 PCI_ANY_ID, PCI_ANY_ID, },
10127 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
10128 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010129 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
10130 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050010131 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
10132 PCI_ANY_ID, PCI_ANY_ID, },
10133 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
10134 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010135 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
10136 PCI_ANY_ID, PCI_ANY_ID, },
10137 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
10138 PCI_ANY_ID, PCI_ANY_ID, },
10139 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
10140 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -040010141 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
10142 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050010143 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
10144 PCI_ANY_ID, PCI_ANY_ID, },
10145 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
10146 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010147 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
10148 PCI_ANY_ID, PCI_ANY_ID, },
10149 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
10150 PCI_ANY_ID, PCI_ANY_ID, },
10151 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
10152 PCI_ANY_ID, PCI_ANY_ID, },
10153 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
10154 PCI_ANY_ID, PCI_ANY_ID, },
10155 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
10156 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050010157 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
10158 PCI_ANY_ID, PCI_ANY_ID, },
10159 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
10160 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -040010161 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
10162 PCI_ANY_ID, PCI_ANY_ID, },
10163 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
10164 PCI_ANY_ID, PCI_ANY_ID, },
10165 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
10166 PCI_ANY_ID, PCI_ANY_ID, },
10167 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
10168 PCI_ANY_ID, PCI_ANY_ID, },
10169 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
10170 PCI_ANY_ID, PCI_ANY_ID, },
10171 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
10172 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -040010173 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
10174 PCI_ANY_ID, PCI_ANY_ID, },
10175 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
10176 PCI_ANY_ID, PCI_ANY_ID, },
10177 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
10178 PCI_ANY_ID, PCI_ANY_ID, },
James Smart3772a992009-05-22 14:50:54 -040010179 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
10180 PCI_ANY_ID, PCI_ANY_ID, },
James Smarta747c9c2009-11-18 15:41:10 -050010181 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
10182 PCI_ANY_ID, PCI_ANY_ID, },
10183 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
James Smart6669f9b2009-10-02 15:16:45 -040010184 PCI_ANY_ID, PCI_ANY_ID, },
James Smart98fc5dd2010-06-07 15:24:29 -040010185 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BALIUS,
10186 PCI_ANY_ID, PCI_ANY_ID, },
James Smart085c6472010-11-20 23:11:37 -050010187 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC,
10188 PCI_ANY_ID, PCI_ANY_ID, },
10189 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE,
10190 PCI_ANY_ID, PCI_ANY_ID, },
James Smartc0c11512011-05-24 11:41:34 -040010191 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC_VF,
10192 PCI_ANY_ID, PCI_ANY_ID, },
10193 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE_VF,
10194 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010195 { 0 }
10196};
10197
10198MODULE_DEVICE_TABLE(pci, lpfc_id_table);
10199
Linas Vepstas8d63f372007-02-14 14:28:36 -060010200static struct pci_error_handlers lpfc_err_handler = {
10201 .error_detected = lpfc_io_error_detected,
10202 .slot_reset = lpfc_io_slot_reset,
10203 .resume = lpfc_io_resume,
10204};
10205
dea31012005-04-17 16:05:31 -050010206static struct pci_driver lpfc_driver = {
10207 .name = LPFC_DRIVER_NAME,
10208 .id_table = lpfc_id_table,
10209 .probe = lpfc_pci_probe_one,
10210 .remove = __devexit_p(lpfc_pci_remove_one),
James Smart3a55b532008-12-04 22:38:54 -050010211 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -040010212 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -050010213 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -050010214};
10215
James Smart3ef6d242012-01-18 16:23:48 -050010216static const struct file_operations lpfc_mgmt_fop = {
10217 .open = lpfc_mgmt_open,
10218 .release = lpfc_mgmt_release,
10219};
10220
10221static struct miscdevice lpfc_mgmt_dev = {
10222 .minor = MISC_DYNAMIC_MINOR,
10223 .name = "lpfcmgmt",
10224 .fops = &lpfc_mgmt_fop,
10225};
10226
James Smarte59058c2008-08-24 21:49:00 -040010227/**
James Smart3621a712009-04-06 18:47:14 -040010228 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -040010229 *
10230 * This routine is to be invoked when the lpfc module is loaded into the
10231 * kernel. The special kernel macro module_init() is used to indicate the
10232 * role of this routine to the kernel as lpfc module entry point.
10233 *
10234 * Return codes
10235 * 0 - successful
10236 * -ENOMEM - FC attach transport failed
10237 * all others - failed
10238 */
dea31012005-04-17 16:05:31 -050010239static int __init
10240lpfc_init(void)
10241{
10242 int error = 0;
10243
10244 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010245 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -050010246
James Smart3ef6d242012-01-18 16:23:48 -050010247 error = misc_register(&lpfc_mgmt_dev);
10248 if (error)
10249 printk(KERN_ERR "Could not register lpfcmgmt device, "
10250 "misc_register returned with status %d", error);
10251
James Smart7ee5d432007-10-27 13:37:17 -040010252 if (lpfc_enable_npiv) {
10253 lpfc_transport_functions.vport_create = lpfc_vport_create;
10254 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
10255 }
dea31012005-04-17 16:05:31 -050010256 lpfc_transport_template =
10257 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -040010258 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -050010259 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -040010260 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -040010261 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -040010262 fc_attach_transport(&lpfc_vport_transport_functions);
10263 if (lpfc_vport_transport_template == NULL) {
10264 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040010265 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -040010266 }
James Smart7ee5d432007-10-27 13:37:17 -040010267 }
dea31012005-04-17 16:05:31 -050010268 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -050010269 if (error) {
dea31012005-04-17 16:05:31 -050010270 fc_release_transport(lpfc_transport_template);
James Smartd7c255b2008-08-24 21:50:00 -040010271 if (lpfc_enable_npiv)
10272 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -050010273 }
dea31012005-04-17 16:05:31 -050010274
10275 return error;
10276}
10277
James Smarte59058c2008-08-24 21:49:00 -040010278/**
James Smart3621a712009-04-06 18:47:14 -040010279 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -040010280 *
10281 * This routine is invoked when the lpfc module is removed from the kernel.
10282 * The special kernel macro module_exit() is used to indicate the role of
10283 * this routine to the kernel as lpfc module exit point.
10284 */
dea31012005-04-17 16:05:31 -050010285static void __exit
10286lpfc_exit(void)
10287{
James Smart3ef6d242012-01-18 16:23:48 -050010288 misc_deregister(&lpfc_mgmt_dev);
dea31012005-04-17 16:05:31 -050010289 pci_unregister_driver(&lpfc_driver);
10290 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040010291 if (lpfc_enable_npiv)
10292 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -050010293 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040010294 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
10295 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050010296 (1L << _dump_buf_data_order), _dump_buf_data);
10297 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
10298 }
10299
10300 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -040010301 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
10302 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050010303 (1L << _dump_buf_dif_order), _dump_buf_dif);
10304 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
10305 }
dea31012005-04-17 16:05:31 -050010306}
10307
10308module_init(lpfc_init);
10309module_exit(lpfc_exit);
10310MODULE_LICENSE("GPL");
10311MODULE_DESCRIPTION(LPFC_MODULE_DESC);
10312MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
10313MODULE_VERSION("0:" LPFC_DRIVER_VERSION);