blob: a79e2c21c5e4fc35a95b383c1645eba7b72b267a [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smartd4379ac2012-03-01 22:37:07 -05004 * Copyright (C) 2004-2012 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/delay.h>
24#include <linux/dma-mapping.h>
25#include <linux/idr.h>
26#include <linux/interrupt.h>
Paul Gortmakeracf3368f2011-05-27 09:47:43 -040027#include <linux/module.h>
dea31012005-04-17 16:05:31 -050028#include <linux/kthread.h>
29#include <linux/pci.h>
30#include <linux/spinlock.h>
James Smart92d7f7b2007-06-17 19:56:38 -050031#include <linux/ctype.h>
James Smart0d878412009-10-02 15:16:56 -040032#include <linux/aer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
James Smart52d52442011-05-24 11:42:45 -040034#include <linux/firmware.h>
James Smart3ef6d242012-01-18 16:23:48 -050035#include <linux/miscdevice.h>
dea31012005-04-17 16:05:31 -050036
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040037#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050038#include <scsi/scsi_device.h>
39#include <scsi/scsi_host.h>
40#include <scsi/scsi_transport_fc.h>
41
James Smartda0436e2009-05-22 14:51:39 -040042#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050043#include "lpfc_hw.h"
44#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040045#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040046#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050047#include "lpfc_disc.h"
48#include "lpfc_scsi.h"
49#include "lpfc.h"
50#include "lpfc_logmsg.h"
51#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050052#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050053#include "lpfc_version.h"
54
James Smart81301a92008-12-04 22:39:46 -050055char *_dump_buf_data;
56unsigned long _dump_buf_data_order;
57char *_dump_buf_dif;
58unsigned long _dump_buf_dif_order;
59spinlock_t _dump_buf_lock;
60
dea31012005-04-17 16:05:31 -050061static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
62static int lpfc_post_rcv_buf(struct lpfc_hba *);
James Smart5350d872011-10-10 21:33:49 -040063static int lpfc_sli4_queue_verify(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040064static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
65static int lpfc_setup_endian_order(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040066static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
James Smart8a9d2e82012-05-09 21:16:12 -040067static void lpfc_free_els_sgl_list(struct lpfc_hba *);
68static void lpfc_init_sgl_list(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040069static int lpfc_init_active_sgl_array(struct lpfc_hba *);
70static void lpfc_free_active_sgl(struct lpfc_hba *);
71static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
72static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
73static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
74static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
75static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
dea31012005-04-17 16:05:31 -050076
77static struct scsi_transport_template *lpfc_transport_template = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050078static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea31012005-04-17 16:05:31 -050079static DEFINE_IDR(lpfc_hba_index);
80
James Smarte59058c2008-08-24 21:49:00 -040081/**
James Smart3621a712009-04-06 18:47:14 -040082 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
James Smarte59058c2008-08-24 21:49:00 -040083 * @phba: pointer to lpfc hba data structure.
84 *
85 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
86 * mailbox command. It retrieves the revision information from the HBA and
87 * collects the Vital Product Data (VPD) about the HBA for preparing the
88 * configuration of the HBA.
89 *
90 * Return codes:
91 * 0 - success.
92 * -ERESTART - requests the SLI layer to reset the HBA and try again.
93 * Any other value - indicates an error.
94 **/
dea31012005-04-17 16:05:31 -050095int
James Smart2e0fef82007-06-17 19:56:36 -050096lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050097{
98 lpfc_vpd_t *vp = &phba->vpd;
99 int i = 0, rc;
100 LPFC_MBOXQ_t *pmb;
101 MAILBOX_t *mb;
102 char *lpfc_vpd_data = NULL;
103 uint16_t offset = 0;
104 static char licensed[56] =
105 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -0400106 static int init_key = 1;
dea31012005-04-17 16:05:31 -0500107
108 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
109 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500110 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500111 return -ENOMEM;
112 }
113
James Smart04c68492009-05-22 14:52:52 -0400114 mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -0500115 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500116
117 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -0400118 if (init_key) {
119 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -0500120
James Smart65a29c12006-07-06 15:50:50 -0400121 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
122 *ptext = cpu_to_be32(*ptext);
123 init_key = 0;
124 }
dea31012005-04-17 16:05:31 -0500125
126 lpfc_read_nv(phba, pmb);
127 memset((char*)mb->un.varRDnvp.rsvd3, 0,
128 sizeof (mb->un.varRDnvp.rsvd3));
129 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
130 sizeof (licensed));
131
132 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
133
134 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500135 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smarte8b62012007-08-02 11:10:09 -0400136 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500137 "error, mbxCmd x%x READ_NVPARM, "
138 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500139 mb->mbxCommand, mb->mbxStatus);
140 mempool_free(pmb, phba->mbox_mem_pool);
141 return -ERESTART;
142 }
143 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500144 sizeof(phba->wwnn));
145 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
146 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500147 }
148
James Smart92d7f7b2007-06-17 19:56:38 -0500149 phba->sli3_options = 0x0;
150
dea31012005-04-17 16:05:31 -0500151 /* Setup and issue mailbox READ REV command */
152 lpfc_read_rev(phba, pmb);
153 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
154 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500155 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400156 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500157 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500158 mb->mbxCommand, mb->mbxStatus);
159 mempool_free( pmb, phba->mbox_mem_pool);
160 return -ERESTART;
161 }
162
James Smart92d7f7b2007-06-17 19:56:38 -0500163
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500164 /*
165 * The value of rr must be 1 since the driver set the cv field to 1.
166 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500167 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500168 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500169 vp->rev.rBit = 0;
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500170 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400171 "0440 Adapter failed to init, READ_REV has "
172 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500173 mempool_free(pmb, phba->mbox_mem_pool);
174 return -ERESTART;
dea31012005-04-17 16:05:31 -0500175 }
176
James Smart495a7142008-06-14 22:52:59 -0400177 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
178 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -0500179 return -EINVAL;
James Smart495a7142008-06-14 22:52:59 -0400180 }
James Smarted957682007-06-17 19:56:37 -0500181
dea31012005-04-17 16:05:31 -0500182 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500183 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500184 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500185 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
186 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
187 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
188 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500189 vp->rev.biuRev = mb->un.varRdRev.biuRev;
190 vp->rev.smRev = mb->un.varRdRev.smRev;
191 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
192 vp->rev.endecRev = mb->un.varRdRev.endecRev;
193 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
194 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
195 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
196 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
197 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
198 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
199
James Smart92d7f7b2007-06-17 19:56:38 -0500200 /* If the sli feature level is less then 9, we must
201 * tear down all RPIs and VPIs on link down if NPIV
202 * is enabled.
203 */
204 if (vp->rev.feaLevelHigh < 9)
205 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
206
dea31012005-04-17 16:05:31 -0500207 if (lpfc_is_LC_HBA(phba->pcidev->device))
208 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
209 sizeof (phba->RandomData));
210
dea31012005-04-17 16:05:31 -0500211 /* Get adapter VPD information */
dea31012005-04-17 16:05:31 -0500212 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
213 if (!lpfc_vpd_data)
James Smartd7c255b2008-08-24 21:50:00 -0400214 goto out_free_mbox;
dea31012005-04-17 16:05:31 -0500215 do {
James Smarta0c87cb2009-07-19 10:01:10 -0400216 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
dea31012005-04-17 16:05:31 -0500217 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
218
219 if (rc != MBX_SUCCESS) {
220 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400221 "0441 VPD not present on adapter, "
dea31012005-04-17 16:05:31 -0500222 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500223 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500224 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500225 }
James Smart04c68492009-05-22 14:52:52 -0400226 /* dump mem may return a zero when finished or we got a
227 * mailbox error, either way we are done.
228 */
229 if (mb->un.varDmp.word_cnt == 0)
230 break;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500231 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
232 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
James Smartd7c255b2008-08-24 21:50:00 -0400233 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
234 lpfc_vpd_data + offset,
James Smart92d7f7b2007-06-17 19:56:38 -0500235 mb->un.varDmp.word_cnt);
dea31012005-04-17 16:05:31 -0500236 offset += mb->un.varDmp.word_cnt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500237 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
238 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500239
240 kfree(lpfc_vpd_data);
dea31012005-04-17 16:05:31 -0500241out_free_mbox:
242 mempool_free(pmb, phba->mbox_mem_pool);
243 return 0;
244}
245
James Smarte59058c2008-08-24 21:49:00 -0400246/**
James Smart3621a712009-04-06 18:47:14 -0400247 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
James Smarte59058c2008-08-24 21:49:00 -0400248 * @phba: pointer to lpfc hba data structure.
249 * @pmboxq: pointer to the driver internal queue element for mailbox command.
250 *
251 * This is the completion handler for driver's configuring asynchronous event
252 * mailbox command to the device. If the mailbox command returns successfully,
253 * it will set internal async event support flag to 1; otherwise, it will
254 * set internal async event support flag to 0.
255 **/
James Smart57127f12007-10-27 13:37:05 -0400256static void
257lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
258{
James Smart04c68492009-05-22 14:52:52 -0400259 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
James Smart57127f12007-10-27 13:37:05 -0400260 phba->temp_sensor_support = 1;
261 else
262 phba->temp_sensor_support = 0;
263 mempool_free(pmboxq, phba->mbox_mem_pool);
264 return;
265}
266
James Smarte59058c2008-08-24 21:49:00 -0400267/**
James Smart3621a712009-04-06 18:47:14 -0400268 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
James Smart97207482008-12-04 22:39:19 -0500269 * @phba: pointer to lpfc hba data structure.
270 * @pmboxq: pointer to the driver internal queue element for mailbox command.
271 *
272 * This is the completion handler for dump mailbox command for getting
273 * wake up parameters. When this command complete, the response contain
274 * Option rom version of the HBA. This function translate the version number
275 * into a human readable string and store it in OptionROMVersion.
276 **/
277static void
278lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
279{
280 struct prog_id *prg;
281 uint32_t prog_id_word;
282 char dist = ' ';
283 /* character array used for decoding dist type. */
284 char dist_char[] = "nabx";
285
James Smart04c68492009-05-22 14:52:52 -0400286 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
James Smart9f1e1b52008-12-04 22:39:40 -0500287 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500288 return;
James Smart9f1e1b52008-12-04 22:39:40 -0500289 }
James Smart97207482008-12-04 22:39:19 -0500290
291 prg = (struct prog_id *) &prog_id_word;
292
293 /* word 7 contain option rom version */
James Smart04c68492009-05-22 14:52:52 -0400294 prog_id_word = pmboxq->u.mb.un.varWords[7];
James Smart97207482008-12-04 22:39:19 -0500295
296 /* Decode the Option rom version word to a readable string */
297 if (prg->dist < 4)
298 dist = dist_char[prg->dist];
299
300 if ((prg->dist == 3) && (prg->num == 0))
301 sprintf(phba->OptionROMVersion, "%d.%d%d",
302 prg->ver, prg->rev, prg->lev);
303 else
304 sprintf(phba->OptionROMVersion, "%d.%d%d%c%d",
305 prg->ver, prg->rev, prg->lev,
306 dist, prg->num);
James Smart9f1e1b52008-12-04 22:39:40 -0500307 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500308 return;
309}
310
311/**
James Smart05580562011-05-24 11:40:48 -0400312 * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
313 * cfg_soft_wwnn, cfg_soft_wwpn
314 * @vport: pointer to lpfc vport data structure.
315 *
316 *
317 * Return codes
318 * None.
319 **/
320void
321lpfc_update_vport_wwn(struct lpfc_vport *vport)
322{
323 /* If the soft name exists then update it using the service params */
324 if (vport->phba->cfg_soft_wwnn)
325 u64_to_wwn(vport->phba->cfg_soft_wwnn,
326 vport->fc_sparam.nodeName.u.wwn);
327 if (vport->phba->cfg_soft_wwpn)
328 u64_to_wwn(vport->phba->cfg_soft_wwpn,
329 vport->fc_sparam.portName.u.wwn);
330
331 /*
332 * If the name is empty or there exists a soft name
333 * then copy the service params name, otherwise use the fc name
334 */
335 if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
336 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
337 sizeof(struct lpfc_name));
338 else
339 memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
340 sizeof(struct lpfc_name));
341
342 if (vport->fc_portname.u.wwn[0] == 0 || vport->phba->cfg_soft_wwpn)
343 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
344 sizeof(struct lpfc_name));
345 else
346 memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
347 sizeof(struct lpfc_name));
348}
349
350/**
James Smart3621a712009-04-06 18:47:14 -0400351 * lpfc_config_port_post - Perform lpfc initialization after config port
James Smarte59058c2008-08-24 21:49:00 -0400352 * @phba: pointer to lpfc hba data structure.
353 *
354 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
355 * command call. It performs all internal resource and state setups on the
356 * port: post IOCB buffers, enable appropriate host interrupt attentions,
357 * ELS ring timers, etc.
358 *
359 * Return codes
360 * 0 - success.
361 * Any other value - error.
362 **/
dea31012005-04-17 16:05:31 -0500363int
James Smart2e0fef82007-06-17 19:56:36 -0500364lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500365{
James Smart2e0fef82007-06-17 19:56:36 -0500366 struct lpfc_vport *vport = phba->pport;
James Smarta257bf92009-04-06 18:48:10 -0400367 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -0500368 LPFC_MBOXQ_t *pmb;
369 MAILBOX_t *mb;
370 struct lpfc_dmabuf *mp;
371 struct lpfc_sli *psli = &phba->sli;
372 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500373 int i, j;
374 int rc;
dea31012005-04-17 16:05:31 -0500375
James Smart7af67052007-10-27 13:38:11 -0400376 spin_lock_irq(&phba->hbalock);
377 /*
378 * If the Config port completed correctly the HBA is not
379 * over heated any more.
380 */
381 if (phba->over_temp_state == HBA_OVER_TEMP)
382 phba->over_temp_state = HBA_NORMAL_TEMP;
383 spin_unlock_irq(&phba->hbalock);
384
dea31012005-04-17 16:05:31 -0500385 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
386 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500387 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500388 return -ENOMEM;
389 }
James Smart04c68492009-05-22 14:52:52 -0400390 mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500391
dea31012005-04-17 16:05:31 -0500392 /* Get login parameters for NID. */
James Smart9f1177a2010-02-26 14:12:57 -0500393 rc = lpfc_read_sparam(phba, pmb, 0);
394 if (rc) {
395 mempool_free(pmb, phba->mbox_mem_pool);
396 return -ENOMEM;
397 }
398
James Smarted957682007-06-17 19:56:37 -0500399 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500400 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500401 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400402 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500403 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500404 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500405 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500406 mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart9f1177a2010-02-26 14:12:57 -0500407 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500408 lpfc_mbuf_free(phba, mp->virt, mp->phys);
409 kfree(mp);
410 return -EIO;
411 }
412
413 mp = (struct lpfc_dmabuf *) pmb->context1;
414
James Smart2e0fef82007-06-17 19:56:36 -0500415 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500416 lpfc_mbuf_free(phba, mp->virt, mp->phys);
417 kfree(mp);
418 pmb->context1 = NULL;
James Smart05580562011-05-24 11:40:48 -0400419 lpfc_update_vport_wwn(vport);
James Smarta257bf92009-04-06 18:48:10 -0400420
421 /* Update the fc_host data structures with new wwn. */
422 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
423 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart21e9a0a2009-05-22 14:53:21 -0400424 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smarta257bf92009-04-06 18:48:10 -0400425
dea31012005-04-17 16:05:31 -0500426 /* If no serial number in VPD data, use low 6 bytes of WWNN */
427 /* This should be consolidated into parse_vpd ? - mr */
428 if (phba->SerialNumber[0] == 0) {
429 uint8_t *outptr;
430
James Smart2e0fef82007-06-17 19:56:36 -0500431 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500432 for (i = 0; i < 12; i++) {
433 status = *outptr++;
434 j = ((status & 0xf0) >> 4);
435 if (j <= 9)
436 phba->SerialNumber[i] =
437 (char)((uint8_t) 0x30 + (uint8_t) j);
438 else
439 phba->SerialNumber[i] =
440 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
441 i++;
442 j = (status & 0xf);
443 if (j <= 9)
444 phba->SerialNumber[i] =
445 (char)((uint8_t) 0x30 + (uint8_t) j);
446 else
447 phba->SerialNumber[i] =
448 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
449 }
450 }
451
dea31012005-04-17 16:05:31 -0500452 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500453 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500454 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500455 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400456 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500457 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500458 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500459 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500460 mempool_free( pmb, phba->mbox_mem_pool);
461 return -EIO;
462 }
463
James Smarta0c87cb2009-07-19 10:01:10 -0400464 /* Check if the port is disabled */
465 lpfc_sli_read_link_ste(phba);
466
dea31012005-04-17 16:05:31 -0500467 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
468 if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
469 phba->cfg_hba_queue_depth =
James Smartf1126682009-06-10 17:22:44 -0400470 (mb->un.varRdConfig.max_xri + 1) -
471 lpfc_sli4_get_els_iocb_cnt(phba);
dea31012005-04-17 16:05:31 -0500472
473 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500474
475 /* Get the default values for Model Name and Description */
476 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
477
James Smart2e0fef82007-06-17 19:56:36 -0500478 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500479
James Smart0b727fe2007-10-27 13:37:25 -0400480 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smarta4bc3372006-12-02 13:34:16 -0500481 if (psli->ring[psli->extra_ring].cmdringaddr)
482 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500483 if (psli->ring[psli->fcp_ring].cmdringaddr)
484 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
485 if (psli->ring[psli->next_ring].cmdringaddr)
486 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
487
488 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500489 if (phba->sli_rev != 3)
490 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500491
James Smart93996272008-08-24 21:50:30 -0400492 /*
493 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
494 */
495 if (phba->intr_type == MSIX) {
496 rc = lpfc_config_msi(phba, pmb);
497 if (rc) {
498 mempool_free(pmb, phba->mbox_mem_pool);
499 return -EIO;
500 }
501 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
502 if (rc != MBX_SUCCESS) {
503 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
504 "0352 Config MSI mailbox command "
505 "failed, mbxCmd x%x, mbxStatus x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400506 pmb->u.mb.mbxCommand,
507 pmb->u.mb.mbxStatus);
James Smart93996272008-08-24 21:50:30 -0400508 mempool_free(pmb, phba->mbox_mem_pool);
509 return -EIO;
510 }
511 }
512
James Smart04c68492009-05-22 14:52:52 -0400513 spin_lock_irq(&phba->hbalock);
James Smart93996272008-08-24 21:50:30 -0400514 /* Initialize ERATT handling flag */
515 phba->hba_flag &= ~HBA_ERATT_HANDLED;
516
dea31012005-04-17 16:05:31 -0500517 /* Enable appropriate host interrupts */
James Smart9940b972011-03-11 16:06:12 -0500518 if (lpfc_readl(phba->HCregaddr, &status)) {
519 spin_unlock_irq(&phba->hbalock);
520 return -EIO;
521 }
dea31012005-04-17 16:05:31 -0500522 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
523 if (psli->num_rings > 0)
524 status |= HC_R0INT_ENA;
525 if (psli->num_rings > 1)
526 status |= HC_R1INT_ENA;
527 if (psli->num_rings > 2)
528 status |= HC_R2INT_ENA;
529 if (psli->num_rings > 3)
530 status |= HC_R3INT_ENA;
531
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500532 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
533 (phba->cfg_poll & DISABLE_FCP_RING_INT))
James Smart93996272008-08-24 21:50:30 -0400534 status &= ~(HC_R0INT_ENA);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500535
dea31012005-04-17 16:05:31 -0500536 writel(status, phba->HCregaddr);
537 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500538 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500539
James Smart93996272008-08-24 21:50:30 -0400540 /* Set up ring-0 (ELS) timer */
541 timeout = phba->fc_ratov * 2;
James Smart2e0fef82007-06-17 19:56:36 -0500542 mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
James Smart93996272008-08-24 21:50:30 -0400543 /* Set up heart beat (HB) timer */
James Smart858c9f62007-06-17 19:56:39 -0500544 mod_timer(&phba->hb_tmofunc, jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
545 phba->hb_outstanding = 0;
546 phba->last_completion_time = jiffies;
James Smart93996272008-08-24 21:50:30 -0400547 /* Set up error attention (ERATT) polling timer */
548 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
dea31012005-04-17 16:05:31 -0500549
James Smarta0c87cb2009-07-19 10:01:10 -0400550 if (phba->hba_flag & LINK_DISABLED) {
551 lpfc_printf_log(phba,
552 KERN_ERR, LOG_INIT,
553 "2598 Adapter Link is disabled.\n");
554 lpfc_down_link(phba, pmb);
555 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
556 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
557 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
558 lpfc_printf_log(phba,
559 KERN_ERR, LOG_INIT,
560 "2599 Adapter failed to issue DOWN_LINK"
561 " mbox command rc 0x%x\n", rc);
562
563 mempool_free(pmb, phba->mbox_mem_pool);
564 return -EIO;
565 }
James Smarte40a02c2010-02-26 14:13:54 -0500566 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart026abb82011-12-13 13:20:45 -0500567 mempool_free(pmb, phba->mbox_mem_pool);
568 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
569 if (rc)
570 return rc;
dea31012005-04-17 16:05:31 -0500571 }
572 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400573 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500574 if (!pmb) {
575 phba->link_state = LPFC_HBA_ERROR;
576 return -ENOMEM;
577 }
578
James Smart57127f12007-10-27 13:37:05 -0400579 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
580 pmb->mbox_cmpl = lpfc_config_async_cmpl;
581 pmb->vport = phba->pport;
582 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500583
James Smart57127f12007-10-27 13:37:05 -0400584 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
585 lpfc_printf_log(phba,
586 KERN_ERR,
587 LOG_INIT,
588 "0456 Adapter failed to issue "
James Smarte4e74272009-07-19 10:01:38 -0400589 "ASYNCEVT_ENABLE mbox status x%x\n",
James Smart57127f12007-10-27 13:37:05 -0400590 rc);
591 mempool_free(pmb, phba->mbox_mem_pool);
592 }
James Smart97207482008-12-04 22:39:19 -0500593
594 /* Get Option rom version */
595 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500596 if (!pmb) {
597 phba->link_state = LPFC_HBA_ERROR;
598 return -ENOMEM;
599 }
600
James Smart97207482008-12-04 22:39:19 -0500601 lpfc_dump_wakeup_param(phba, pmb);
602 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
603 pmb->vport = phba->pport;
604 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
605
606 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
607 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
James Smarte4e74272009-07-19 10:01:38 -0400608 "to get Option ROM version status x%x\n", rc);
James Smart97207482008-12-04 22:39:19 -0500609 mempool_free(pmb, phba->mbox_mem_pool);
610 }
611
James Smartd7c255b2008-08-24 21:50:00 -0400612 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400613}
614
James Smarte59058c2008-08-24 21:49:00 -0400615/**
James Smart84d1b002010-02-12 14:42:33 -0500616 * lpfc_hba_init_link - Initialize the FC link
617 * @phba: pointer to lpfc hba data structure.
James Smart6e7288d2010-06-07 15:23:35 -0400618 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500619 *
620 * This routine will issue the INIT_LINK mailbox command call.
621 * It is available to other drivers through the lpfc_hba data
622 * structure for use as a delayed link up mechanism with the
623 * module parameter lpfc_suppress_link_up.
624 *
625 * Return code
626 * 0 - success
627 * Any other value - error
628 **/
629int
James Smart6e7288d2010-06-07 15:23:35 -0400630lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500631{
James Smart1b511972011-12-13 13:23:09 -0500632 return lpfc_hba_init_link_fc_topology(phba, phba->cfg_topology, flag);
633}
634
635/**
636 * lpfc_hba_init_link_fc_topology - Initialize FC link with desired topology
637 * @phba: pointer to lpfc hba data structure.
638 * @fc_topology: desired fc topology.
639 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
640 *
641 * This routine will issue the INIT_LINK mailbox command call.
642 * It is available to other drivers through the lpfc_hba data
643 * structure for use as a delayed link up mechanism with the
644 * module parameter lpfc_suppress_link_up.
645 *
646 * Return code
647 * 0 - success
648 * Any other value - error
649 **/
650int
651lpfc_hba_init_link_fc_topology(struct lpfc_hba *phba, uint32_t fc_topology,
652 uint32_t flag)
653{
James Smart84d1b002010-02-12 14:42:33 -0500654 struct lpfc_vport *vport = phba->pport;
655 LPFC_MBOXQ_t *pmb;
656 MAILBOX_t *mb;
657 int rc;
658
659 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
660 if (!pmb) {
661 phba->link_state = LPFC_HBA_ERROR;
662 return -ENOMEM;
663 }
664 mb = &pmb->u.mb;
665 pmb->vport = vport;
666
James Smart026abb82011-12-13 13:20:45 -0500667 if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_MAX) ||
668 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G) &&
669 !(phba->lmt & LMT_1Gb)) ||
670 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G) &&
671 !(phba->lmt & LMT_2Gb)) ||
672 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G) &&
673 !(phba->lmt & LMT_4Gb)) ||
674 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G) &&
675 !(phba->lmt & LMT_8Gb)) ||
676 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G) &&
677 !(phba->lmt & LMT_10Gb)) ||
678 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G) &&
679 !(phba->lmt & LMT_16Gb))) {
680 /* Reset link speed to auto */
681 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
682 "1302 Invalid speed for this board:%d "
683 "Reset link speed to auto.\n",
684 phba->cfg_link_speed);
685 phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
686 }
James Smart1b511972011-12-13 13:23:09 -0500687 lpfc_init_link(phba, pmb, fc_topology, phba->cfg_link_speed);
James Smart84d1b002010-02-12 14:42:33 -0500688 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart1b511972011-12-13 13:23:09 -0500689 if (phba->sli_rev < LPFC_SLI_REV4)
690 lpfc_set_loopback_flag(phba);
James Smart6e7288d2010-06-07 15:23:35 -0400691 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart76a95d72010-11-20 23:11:48 -0500692 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
James Smart84d1b002010-02-12 14:42:33 -0500693 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
694 "0498 Adapter failed to init, mbxCmd x%x "
695 "INIT_LINK, mbxStatus x%x\n",
696 mb->mbxCommand, mb->mbxStatus);
James Smart76a95d72010-11-20 23:11:48 -0500697 if (phba->sli_rev <= LPFC_SLI_REV3) {
698 /* Clear all interrupt enable conditions */
699 writel(0, phba->HCregaddr);
700 readl(phba->HCregaddr); /* flush */
701 /* Clear all pending interrupts */
702 writel(0xffffffff, phba->HAregaddr);
703 readl(phba->HAregaddr); /* flush */
704 }
James Smart84d1b002010-02-12 14:42:33 -0500705 phba->link_state = LPFC_HBA_ERROR;
James Smart6e7288d2010-06-07 15:23:35 -0400706 if (rc != MBX_BUSY || flag == MBX_POLL)
James Smart84d1b002010-02-12 14:42:33 -0500707 mempool_free(pmb, phba->mbox_mem_pool);
708 return -EIO;
709 }
James Smarte40a02c2010-02-26 14:13:54 -0500710 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
James Smart6e7288d2010-06-07 15:23:35 -0400711 if (flag == MBX_POLL)
712 mempool_free(pmb, phba->mbox_mem_pool);
James Smart84d1b002010-02-12 14:42:33 -0500713
714 return 0;
715}
716
717/**
718 * lpfc_hba_down_link - this routine downs the FC link
James Smart6e7288d2010-06-07 15:23:35 -0400719 * @phba: pointer to lpfc hba data structure.
720 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500721 *
722 * This routine will issue the DOWN_LINK mailbox command call.
723 * It is available to other drivers through the lpfc_hba data
724 * structure for use to stop the link.
725 *
726 * Return code
727 * 0 - success
728 * Any other value - error
729 **/
730int
James Smart6e7288d2010-06-07 15:23:35 -0400731lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500732{
733 LPFC_MBOXQ_t *pmb;
734 int rc;
735
736 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
737 if (!pmb) {
738 phba->link_state = LPFC_HBA_ERROR;
739 return -ENOMEM;
740 }
741
742 lpfc_printf_log(phba,
743 KERN_ERR, LOG_INIT,
744 "0491 Adapter Link is disabled.\n");
745 lpfc_down_link(phba, pmb);
746 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6e7288d2010-06-07 15:23:35 -0400747 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart84d1b002010-02-12 14:42:33 -0500748 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
749 lpfc_printf_log(phba,
750 KERN_ERR, LOG_INIT,
751 "2522 Adapter failed to issue DOWN_LINK"
752 " mbox command rc 0x%x\n", rc);
753
754 mempool_free(pmb, phba->mbox_mem_pool);
755 return -EIO;
756 }
James Smart6e7288d2010-06-07 15:23:35 -0400757 if (flag == MBX_POLL)
758 mempool_free(pmb, phba->mbox_mem_pool);
759
James Smart84d1b002010-02-12 14:42:33 -0500760 return 0;
761}
762
763/**
James Smart3621a712009-04-06 18:47:14 -0400764 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400765 * @phba: pointer to lpfc HBA data structure.
766 *
767 * This routine will do LPFC uninitialization before the HBA is reset when
768 * bringing down the SLI Layer.
769 *
770 * Return codes
771 * 0 - success.
772 * Any other value - error.
773 **/
dea31012005-04-17 16:05:31 -0500774int
James Smart2e0fef82007-06-17 19:56:36 -0500775lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500776{
James Smart1b32f6a2008-02-08 18:49:39 -0500777 struct lpfc_vport **vports;
778 int i;
James Smart3772a992009-05-22 14:50:54 -0400779
780 if (phba->sli_rev <= LPFC_SLI_REV3) {
781 /* Disable interrupts */
782 writel(0, phba->HCregaddr);
783 readl(phba->HCregaddr); /* flush */
784 }
dea31012005-04-17 16:05:31 -0500785
James Smart1b32f6a2008-02-08 18:49:39 -0500786 if (phba->pport->load_flag & FC_UNLOADING)
787 lpfc_cleanup_discovery_resources(phba->pport);
788 else {
789 vports = lpfc_create_vport_work_array(phba);
790 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400791 for (i = 0; i <= phba->max_vports &&
792 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500793 lpfc_cleanup_discovery_resources(vports[i]);
794 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500795 }
796 return 0;
dea31012005-04-17 16:05:31 -0500797}
798
James Smarte59058c2008-08-24 21:49:00 -0400799/**
James Smart3772a992009-05-22 14:50:54 -0400800 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400801 * @phba: pointer to lpfc HBA data structure.
802 *
803 * This routine will do uninitialization after the HBA is reset when bring
804 * down the SLI Layer.
805 *
806 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200807 * 0 - success.
James Smarte59058c2008-08-24 21:49:00 -0400808 * Any other value - error.
809 **/
James Smart3772a992009-05-22 14:50:54 -0400810static int
811lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -0500812{
813 struct lpfc_sli *psli = &phba->sli;
814 struct lpfc_sli_ring *pring;
815 struct lpfc_dmabuf *mp, *next_mp;
James Smart09372822008-01-11 01:52:54 -0500816 LIST_HEAD(completions);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500817 int i;
818
James Smart92d7f7b2007-06-17 19:56:38 -0500819 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
820 lpfc_sli_hbqbuf_free_all(phba);
821 else {
822 /* Cleanup preposted buffers on the ELS ring */
823 pring = &psli->ring[LPFC_ELS_RING];
824 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
825 list_del(&mp->list);
826 pring->postbufq_cnt--;
827 lpfc_mbuf_free(phba, mp->virt, mp->phys);
828 kfree(mp);
829 }
Jamie Wellnitz41415862006-02-28 19:25:27 -0500830 }
831
James Smart09372822008-01-11 01:52:54 -0500832 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500833 for (i = 0; i < psli->num_rings; i++) {
834 pring = &psli->ring[i];
James Smart09372822008-01-11 01:52:54 -0500835
836 /* At this point in time the HBA is either reset or DOA. Either
837 * way, nothing should be on txcmplq as it will NEVER complete.
838 */
839 list_splice_init(&pring->txcmplq, &completions);
840 pring->txcmplq_cnt = 0;
841 spin_unlock_irq(&phba->hbalock);
842
James Smarta257bf92009-04-06 18:48:10 -0400843 /* Cancel all the IOCBs from the completions list */
844 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
845 IOERR_SLI_ABORTED);
James Smart09372822008-01-11 01:52:54 -0500846
Jamie Wellnitz41415862006-02-28 19:25:27 -0500847 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart09372822008-01-11 01:52:54 -0500848 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500849 }
James Smart09372822008-01-11 01:52:54 -0500850 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500851
852 return 0;
853}
James Smart5af5eee2010-10-22 11:06:38 -0400854
James Smartda0436e2009-05-22 14:51:39 -0400855/**
856 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
857 * @phba: pointer to lpfc HBA data structure.
858 *
859 * This routine will do uninitialization after the HBA is reset when bring
860 * down the SLI Layer.
861 *
862 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200863 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400864 * Any other value - error.
865 **/
866static int
867lpfc_hba_down_post_s4(struct lpfc_hba *phba)
868{
869 struct lpfc_scsi_buf *psb, *psb_next;
870 LIST_HEAD(aborts);
871 int ret;
872 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500873 struct lpfc_sglq *sglq_entry = NULL;
874
James Smartda0436e2009-05-22 14:51:39 -0400875 ret = lpfc_hba_down_post_s3(phba);
876 if (ret)
877 return ret;
878 /* At this point in time the HBA is either reset or DOA. Either
879 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
880 * on the lpfc_sgl_list so that it can either be freed if the
881 * driver is unloading or reposted if the driver is restarting
882 * the port.
883 */
884 spin_lock_irq(&phba->hbalock); /* required for lpfc_sgl_list and */
885 /* scsl_buf_list */
886 /* abts_sgl_list_lock required because worker thread uses this
887 * list.
888 */
889 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -0500890 list_for_each_entry(sglq_entry,
891 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
892 sglq_entry->state = SGL_FREED;
893
James Smartda0436e2009-05-22 14:51:39 -0400894 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
895 &phba->sli4_hba.lpfc_sgl_list);
896 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
897 /* abts_scsi_buf_list_lock required because worker thread uses this
898 * list.
899 */
900 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
901 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
902 &aborts);
903 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
904 spin_unlock_irq(&phba->hbalock);
905
906 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
907 psb->pCmd = NULL;
908 psb->status = IOSTAT_SUCCESS;
909 }
910 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
911 list_splice(&aborts, &phba->lpfc_scsi_buf_list);
912 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
913 return 0;
914}
915
916/**
917 * lpfc_hba_down_post - Wrapper func for hba down post routine
918 * @phba: pointer to lpfc HBA data structure.
919 *
920 * This routine wraps the actual SLI3 or SLI4 routine for performing
921 * uninitialization after the HBA is reset when bring down the SLI Layer.
922 *
923 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200924 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400925 * Any other value - error.
926 **/
927int
928lpfc_hba_down_post(struct lpfc_hba *phba)
929{
930 return (*phba->lpfc_hba_down_post)(phba);
931}
Jamie Wellnitz41415862006-02-28 19:25:27 -0500932
James Smarte59058c2008-08-24 21:49:00 -0400933/**
James Smart3621a712009-04-06 18:47:14 -0400934 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -0400935 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
936 *
937 * This is the HBA-timer timeout handler registered to the lpfc driver. When
938 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
939 * work-port-events bitmap and the worker thread is notified. This timeout
940 * event will be used by the worker thread to invoke the actual timeout
941 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
942 * be performed in the timeout handler and the HBA timeout event bit shall
943 * be cleared by the worker thread after it has taken the event bitmap out.
944 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100945static void
James Smart858c9f62007-06-17 19:56:39 -0500946lpfc_hb_timeout(unsigned long ptr)
947{
948 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400949 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -0500950 unsigned long iflag;
951
952 phba = (struct lpfc_hba *)ptr;
James Smart93996272008-08-24 21:50:30 -0400953
954 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -0500955 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -0400956 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
957 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -0500958 phba->pport->work_port_events |= WORKER_HB_TMO;
959 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
960
James Smart93996272008-08-24 21:50:30 -0400961 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -0400962 if (!tmo_posted)
963 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -0500964 return;
965}
966
James Smarte59058c2008-08-24 21:49:00 -0400967/**
James Smart19ca7602010-11-20 23:11:55 -0500968 * lpfc_rrq_timeout - The RRQ-timer timeout handler
969 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
970 *
971 * This is the RRQ-timer timeout handler registered to the lpfc driver. When
972 * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
973 * work-port-events bitmap and the worker thread is notified. This timeout
974 * event will be used by the worker thread to invoke the actual timeout
975 * handler routine, lpfc_rrq_handler. Any periodical operations will
976 * be performed in the timeout handler and the RRQ timeout event bit shall
977 * be cleared by the worker thread after it has taken the event bitmap out.
978 **/
979static void
980lpfc_rrq_timeout(unsigned long ptr)
981{
982 struct lpfc_hba *phba;
James Smart19ca7602010-11-20 23:11:55 -0500983 unsigned long iflag;
984
985 phba = (struct lpfc_hba *)ptr;
986 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -0500987 phba->hba_flag |= HBA_RRQ_ACTIVE;
James Smart19ca7602010-11-20 23:11:55 -0500988 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -0500989 lpfc_worker_wake_up(phba);
James Smart19ca7602010-11-20 23:11:55 -0500990}
991
992/**
James Smart3621a712009-04-06 18:47:14 -0400993 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -0400994 * @phba: pointer to lpfc hba data structure.
995 * @pmboxq: pointer to the driver internal queue element for mailbox command.
996 *
997 * This is the callback function to the lpfc heart-beat mailbox command.
998 * If configured, the lpfc driver issues the heart-beat mailbox command to
999 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
1000 * heart-beat mailbox command is issued, the driver shall set up heart-beat
1001 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
1002 * heart-beat outstanding state. Once the mailbox command comes back and
1003 * no error conditions detected, the heart-beat mailbox command timer is
1004 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1005 * state is cleared for the next heart-beat. If the timer expired with the
1006 * heart-beat outstanding state set, the driver will put the HBA offline.
1007 **/
James Smart858c9f62007-06-17 19:56:39 -05001008static void
1009lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1010{
1011 unsigned long drvr_flag;
1012
1013 spin_lock_irqsave(&phba->hbalock, drvr_flag);
1014 phba->hb_outstanding = 0;
1015 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1016
James Smart93996272008-08-24 21:50:30 -04001017 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -05001018 mempool_free(pmboxq, phba->mbox_mem_pool);
1019 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1020 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -04001021 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -05001022 mod_timer(&phba->hb_tmofunc,
1023 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1024 return;
1025}
1026
James Smarte59058c2008-08-24 21:49:00 -04001027/**
James Smart3621a712009-04-06 18:47:14 -04001028 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001029 * @phba: pointer to lpfc hba data structure.
1030 *
1031 * This is the actual HBA-timer timeout handler to be invoked by the worker
1032 * thread whenever the HBA timer fired and HBA-timeout event posted. This
1033 * handler performs any periodic operations needed for the device. If such
1034 * periodic event has already been attended to either in the interrupt handler
1035 * or by processing slow-ring or fast-ring events within the HBA-timer
1036 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1037 * the timer for the next timeout period. If lpfc heart-beat mailbox command
1038 * is configured and there is no heart-beat mailbox command outstanding, a
1039 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1040 * has been a heart-beat mailbox command outstanding, the HBA shall be put
1041 * to offline.
1042 **/
James Smart858c9f62007-06-17 19:56:39 -05001043void
1044lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1045{
James Smart45ed1192009-10-02 15:17:02 -04001046 struct lpfc_vport **vports;
James Smart858c9f62007-06-17 19:56:39 -05001047 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -05001048 struct lpfc_dmabuf *buf_ptr;
James Smart45ed1192009-10-02 15:17:02 -04001049 int retval, i;
James Smart858c9f62007-06-17 19:56:39 -05001050 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -05001051 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -05001052
James Smart45ed1192009-10-02 15:17:02 -04001053 vports = lpfc_create_vport_work_array(phba);
1054 if (vports != NULL)
1055 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
1056 lpfc_rcv_seq_check_edtov(vports[i]);
1057 lpfc_destroy_vport_work_array(phba, vports);
1058
James Smart858c9f62007-06-17 19:56:39 -05001059 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -04001060 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -05001061 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1062 return;
1063
1064 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -05001065
1066 if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
1067 jiffies)) {
1068 spin_unlock_irq(&phba->pport->work_port_lock);
1069 if (!phba->hb_outstanding)
1070 mod_timer(&phba->hb_tmofunc,
1071 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1072 else
1073 mod_timer(&phba->hb_tmofunc,
1074 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
1075 return;
1076 }
1077 spin_unlock_irq(&phba->pport->work_port_lock);
1078
James Smart0ff10d42008-01-11 01:52:36 -05001079 if (phba->elsbuf_cnt &&
1080 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1081 spin_lock_irq(&phba->hbalock);
1082 list_splice_init(&phba->elsbuf, &completions);
1083 phba->elsbuf_cnt = 0;
1084 phba->elsbuf_prev_cnt = 0;
1085 spin_unlock_irq(&phba->hbalock);
1086
1087 while (!list_empty(&completions)) {
1088 list_remove_head(&completions, buf_ptr,
1089 struct lpfc_dmabuf, list);
1090 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1091 kfree(buf_ptr);
1092 }
1093 }
1094 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1095
James Smart858c9f62007-06-17 19:56:39 -05001096 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -05001097 if (phba->cfg_enable_hba_heartbeat) {
1098 if (!phba->hb_outstanding) {
James Smartbc739052010-08-04 16:11:18 -04001099 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1100 (list_empty(&psli->mboxq))) {
1101 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1102 GFP_KERNEL);
1103 if (!pmboxq) {
1104 mod_timer(&phba->hb_tmofunc,
1105 jiffies +
1106 HZ * LPFC_HB_MBOX_INTERVAL);
1107 return;
1108 }
James Smart13815c82008-01-11 01:52:48 -05001109
James Smartbc739052010-08-04 16:11:18 -04001110 lpfc_heart_beat(phba, pmboxq);
1111 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1112 pmboxq->vport = phba->pport;
1113 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1114 MBX_NOWAIT);
James Smart13815c82008-01-11 01:52:48 -05001115
James Smartbc739052010-08-04 16:11:18 -04001116 if (retval != MBX_BUSY &&
1117 retval != MBX_SUCCESS) {
1118 mempool_free(pmboxq,
1119 phba->mbox_mem_pool);
1120 mod_timer(&phba->hb_tmofunc,
1121 jiffies +
1122 HZ * LPFC_HB_MBOX_INTERVAL);
1123 return;
1124 }
1125 phba->skipped_hb = 0;
1126 phba->hb_outstanding = 1;
1127 } else if (time_before_eq(phba->last_completion_time,
1128 phba->skipped_hb)) {
1129 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1130 "2857 Last completion time not "
1131 " updated in %d ms\n",
1132 jiffies_to_msecs(jiffies
1133 - phba->last_completion_time));
1134 } else
1135 phba->skipped_hb = jiffies;
1136
James Smart858c9f62007-06-17 19:56:39 -05001137 mod_timer(&phba->hb_tmofunc,
James Smart13815c82008-01-11 01:52:48 -05001138 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
James Smart858c9f62007-06-17 19:56:39 -05001139 return;
James Smart13815c82008-01-11 01:52:48 -05001140 } else {
1141 /*
1142 * If heart beat timeout called with hb_outstanding set
James Smartdcf2a4e2010-09-29 11:18:53 -04001143 * we need to give the hb mailbox cmd a chance to
1144 * complete or TMO.
James Smart13815c82008-01-11 01:52:48 -05001145 */
James Smartdcf2a4e2010-09-29 11:18:53 -04001146 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1147 "0459 Adapter heartbeat still out"
1148 "standing:last compl time was %d ms.\n",
1149 jiffies_to_msecs(jiffies
1150 - phba->last_completion_time));
1151 mod_timer(&phba->hb_tmofunc,
1152 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
James Smart858c9f62007-06-17 19:56:39 -05001153 }
James Smart858c9f62007-06-17 19:56:39 -05001154 }
1155}
1156
James Smarte59058c2008-08-24 21:49:00 -04001157/**
James Smart3621a712009-04-06 18:47:14 -04001158 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -04001159 * @phba: pointer to lpfc hba data structure.
1160 *
1161 * This routine is called to bring the HBA offline when HBA hardware error
1162 * other than Port Error 6 has been detected.
1163 **/
James Smart09372822008-01-11 01:52:54 -05001164static void
1165lpfc_offline_eratt(struct lpfc_hba *phba)
1166{
1167 struct lpfc_sli *psli = &phba->sli;
1168
1169 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001170 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -05001171 spin_unlock_irq(&phba->hbalock);
1172 lpfc_offline_prep(phba);
1173
1174 lpfc_offline(phba);
1175 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001176 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001177 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001178 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001179 lpfc_hba_down_post(phba);
1180 lpfc_sli_brdready(phba, HS_MBRDY);
1181 lpfc_unblock_mgmt_io(phba);
1182 phba->link_state = LPFC_HBA_ERROR;
1183 return;
1184}
1185
James Smarte59058c2008-08-24 21:49:00 -04001186/**
James Smartda0436e2009-05-22 14:51:39 -04001187 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1188 * @phba: pointer to lpfc hba data structure.
1189 *
1190 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1191 * other than Port Error 6 has been detected.
1192 **/
1193static void
1194lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1195{
1196 lpfc_offline_prep(phba);
1197 lpfc_offline(phba);
1198 lpfc_sli4_brdreset(phba);
1199 lpfc_hba_down_post(phba);
1200 lpfc_sli4_post_status_check(phba);
1201 lpfc_unblock_mgmt_io(phba);
1202 phba->link_state = LPFC_HBA_ERROR;
1203}
1204
1205/**
James Smarta257bf92009-04-06 18:48:10 -04001206 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1207 * @phba: pointer to lpfc hba data structure.
1208 *
1209 * This routine is invoked to handle the deferred HBA hardware error
1210 * conditions. This type of error is indicated by HBA by setting ER1
1211 * and another ER bit in the host status register. The driver will
1212 * wait until the ER1 bit clears before handling the error condition.
1213 **/
1214static void
1215lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1216{
1217 uint32_t old_host_status = phba->work_hs;
1218 struct lpfc_sli_ring *pring;
1219 struct lpfc_sli *psli = &phba->sli;
1220
James Smartf4b4c682009-05-22 14:53:12 -04001221 /* If the pci channel is offline, ignore possible errors,
1222 * since we cannot communicate with the pci card anyway.
1223 */
1224 if (pci_channel_offline(phba->pcidev)) {
1225 spin_lock_irq(&phba->hbalock);
1226 phba->hba_flag &= ~DEFER_ERATT;
1227 spin_unlock_irq(&phba->hbalock);
1228 return;
1229 }
1230
James Smarta257bf92009-04-06 18:48:10 -04001231 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1232 "0479 Deferred Adapter Hardware Error "
1233 "Data: x%x x%x x%x\n",
1234 phba->work_hs,
1235 phba->work_status[0], phba->work_status[1]);
1236
1237 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001238 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001239 spin_unlock_irq(&phba->hbalock);
1240
1241
1242 /*
1243 * Firmware stops when it triggred erratt. That could cause the I/Os
1244 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1245 * SCSI layer retry it after re-establishing link.
1246 */
1247 pring = &psli->ring[psli->fcp_ring];
1248 lpfc_sli_abort_iocb_ring(phba, pring);
1249
1250 /*
1251 * There was a firmware error. Take the hba offline and then
1252 * attempt to restart it.
1253 */
1254 lpfc_offline_prep(phba);
1255 lpfc_offline(phba);
1256
1257 /* Wait for the ER1 bit to clear.*/
1258 while (phba->work_hs & HS_FFER1) {
1259 msleep(100);
James Smart9940b972011-03-11 16:06:12 -05001260 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1261 phba->work_hs = UNPLUG_ERR ;
1262 break;
1263 }
James Smarta257bf92009-04-06 18:48:10 -04001264 /* If driver is unloading let the worker thread continue */
1265 if (phba->pport->load_flag & FC_UNLOADING) {
1266 phba->work_hs = 0;
1267 break;
1268 }
1269 }
1270
1271 /*
1272 * This is to ptrotect against a race condition in which
1273 * first write to the host attention register clear the
1274 * host status register.
1275 */
1276 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1277 phba->work_hs = old_host_status & ~HS_FFER1;
1278
James Smart3772a992009-05-22 14:50:54 -04001279 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001280 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001281 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001282 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1283 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1284}
1285
James Smart3772a992009-05-22 14:50:54 -04001286static void
1287lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1288{
1289 struct lpfc_board_event_header board_event;
1290 struct Scsi_Host *shost;
1291
1292 board_event.event_type = FC_REG_BOARD_EVENT;
1293 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1294 shost = lpfc_shost_from_vport(phba->pport);
1295 fc_host_post_vendor_event(shost, fc_get_event_number(),
1296 sizeof(board_event),
1297 (char *) &board_event,
1298 LPFC_NL_VENDOR_ID);
1299}
1300
James Smarta257bf92009-04-06 18:48:10 -04001301/**
James Smart3772a992009-05-22 14:50:54 -04001302 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001303 * @phba: pointer to lpfc hba data structure.
1304 *
1305 * This routine is invoked to handle the following HBA hardware error
1306 * conditions:
1307 * 1 - HBA error attention interrupt
1308 * 2 - DMA ring index out of range
1309 * 3 - Mailbox command came back as unknown
1310 **/
James Smart3772a992009-05-22 14:50:54 -04001311static void
1312lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001313{
James Smart2e0fef82007-06-17 19:56:36 -05001314 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001315 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001316 struct lpfc_sli_ring *pring;
James Smartd2873e42006-08-18 17:46:43 -04001317 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001318 unsigned long temperature;
1319 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001320 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001321
Linas Vepstas8d63f372007-02-14 14:28:36 -06001322 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001323 * since we cannot communicate with the pci card anyway.
1324 */
1325 if (pci_channel_offline(phba->pcidev)) {
1326 spin_lock_irq(&phba->hbalock);
1327 phba->hba_flag &= ~DEFER_ERATT;
1328 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001329 return;
James Smart3772a992009-05-22 14:50:54 -04001330 }
1331
James Smart13815c82008-01-11 01:52:48 -05001332 /* If resets are disabled then leave the HBA alone and return */
1333 if (!phba->cfg_enable_hba_reset)
1334 return;
dea31012005-04-17 16:05:31 -05001335
James Smartea2151b2008-09-07 11:52:10 -04001336 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001337 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001338
James Smarta257bf92009-04-06 18:48:10 -04001339 if (phba->hba_flag & DEFER_ERATT)
1340 lpfc_handle_deferred_eratt(phba);
1341
James Smartdcf2a4e2010-09-29 11:18:53 -04001342 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1343 if (phba->work_hs & HS_FFER6)
1344 /* Re-establishing Link */
1345 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1346 "1301 Re-establishing Link "
1347 "Data: x%x x%x x%x\n",
1348 phba->work_hs, phba->work_status[0],
1349 phba->work_status[1]);
1350 if (phba->work_hs & HS_FFER8)
1351 /* Device Zeroization */
1352 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1353 "2861 Host Authentication device "
1354 "zeroization Data:x%x x%x x%x\n",
1355 phba->work_hs, phba->work_status[0],
1356 phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001357
James Smart92d7f7b2007-06-17 19:56:38 -05001358 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001359 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001360 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001361
1362 /*
1363 * Firmware stops when it triggled erratt with HS_FFER6.
1364 * That could cause the I/Os dropped by the firmware.
1365 * Error iocb (I/O) on txcmplq and let the SCSI layer
1366 * retry it after re-establishing link.
1367 */
1368 pring = &psli->ring[psli->fcp_ring];
1369 lpfc_sli_abort_iocb_ring(phba, pring);
1370
dea31012005-04-17 16:05:31 -05001371 /*
1372 * There was a firmware error. Take the hba offline and then
1373 * attempt to restart it.
1374 */
James Smart46fa3112007-04-25 09:51:45 -04001375 lpfc_offline_prep(phba);
dea31012005-04-17 16:05:31 -05001376 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001377 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001378 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001379 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001380 return;
1381 }
James Smart46fa3112007-04-25 09:51:45 -04001382 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001383 } else if (phba->work_hs & HS_CRIT_TEMP) {
1384 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1385 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1386 temp_event_data.event_code = LPFC_CRIT_TEMP;
1387 temp_event_data.data = (uint32_t)temperature;
1388
1389 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001390 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001391 "(%ld), taking this port offline "
1392 "Data: x%x x%x x%x\n",
1393 temperature, phba->work_hs,
1394 phba->work_status[0], phba->work_status[1]);
1395
1396 shost = lpfc_shost_from_vport(phba->pport);
1397 fc_host_post_vendor_event(shost, fc_get_event_number(),
1398 sizeof(temp_event_data),
1399 (char *) &temp_event_data,
1400 SCSI_NL_VID_TYPE_PCI
1401 | PCI_VENDOR_ID_EMULEX);
1402
James Smart7af67052007-10-27 13:38:11 -04001403 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001404 phba->over_temp_state = HBA_OVER_TEMP;
1405 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001406 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001407
dea31012005-04-17 16:05:31 -05001408 } else {
1409 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001410 * failure is a value other than FFER6. Do not call the offline
1411 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001412 */
1413 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001414 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001415 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001416 phba->work_hs,
dea31012005-04-17 16:05:31 -05001417 phba->work_status[0], phba->work_status[1]);
1418
James Smartd2873e42006-08-18 17:46:43 -04001419 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001420 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001421 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001422 sizeof(event_data), (char *) &event_data,
1423 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1424
James Smart09372822008-01-11 01:52:54 -05001425 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001426 }
James Smart93996272008-08-24 21:50:30 -04001427 return;
dea31012005-04-17 16:05:31 -05001428}
1429
James Smarte59058c2008-08-24 21:49:00 -04001430/**
James Smartda0436e2009-05-22 14:51:39 -04001431 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1432 * @phba: pointer to lpfc hba data structure.
1433 *
1434 * This routine is invoked to handle the SLI4 HBA hardware error attention
1435 * conditions.
1436 **/
1437static void
1438lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1439{
1440 struct lpfc_vport *vport = phba->pport;
1441 uint32_t event_data;
1442 struct Scsi_Host *shost;
James Smart2fcee4b2010-12-15 17:57:46 -05001443 uint32_t if_type;
James Smart2e90f4b2011-12-13 13:22:37 -05001444 struct lpfc_register portstat_reg = {0};
1445 uint32_t reg_err1, reg_err2;
1446 uint32_t uerrlo_reg, uemasklo_reg;
1447 uint32_t pci_rd_rc1, pci_rd_rc2;
James Smart73d91e52011-10-10 21:32:10 -04001448 int rc;
James Smartda0436e2009-05-22 14:51:39 -04001449
1450 /* If the pci channel is offline, ignore possible errors, since
1451 * we cannot communicate with the pci card anyway.
1452 */
1453 if (pci_channel_offline(phba->pcidev))
1454 return;
1455 /* If resets are disabled then leave the HBA alone and return */
1456 if (!phba->cfg_enable_hba_reset)
1457 return;
1458
James Smart2fcee4b2010-12-15 17:57:46 -05001459 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1460 switch (if_type) {
1461 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart2e90f4b2011-12-13 13:22:37 -05001462 pci_rd_rc1 = lpfc_readl(
1463 phba->sli4_hba.u.if_type0.UERRLOregaddr,
1464 &uerrlo_reg);
1465 pci_rd_rc2 = lpfc_readl(
1466 phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
1467 &uemasklo_reg);
1468 /* consider PCI bus read error as pci_channel_offline */
1469 if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
1470 return;
James Smart2fcee4b2010-12-15 17:57:46 -05001471 lpfc_sli4_offline_eratt(phba);
1472 break;
1473 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart2e90f4b2011-12-13 13:22:37 -05001474 pci_rd_rc1 = lpfc_readl(
1475 phba->sli4_hba.u.if_type2.STATUSregaddr,
1476 &portstat_reg.word0);
1477 /* consider PCI bus read error as pci_channel_offline */
James Smart6b5151f2012-01-18 16:24:06 -05001478 if (pci_rd_rc1 == -EIO) {
1479 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1480 "3151 PCI bus read access failure: x%x\n",
1481 readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
James Smart2e90f4b2011-12-13 13:22:37 -05001482 return;
James Smart6b5151f2012-01-18 16:24:06 -05001483 }
James Smart2e90f4b2011-12-13 13:22:37 -05001484 reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
1485 reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
James Smart2fcee4b2010-12-15 17:57:46 -05001486 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
1487 /* TODO: Register for Overtemp async events. */
1488 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1489 "2889 Port Overtemperature event, "
James Smart026abb82011-12-13 13:20:45 -05001490 "taking port offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001491 spin_lock_irq(&phba->hbalock);
1492 phba->over_temp_state = HBA_OVER_TEMP;
1493 spin_unlock_irq(&phba->hbalock);
1494 lpfc_sli4_offline_eratt(phba);
James Smart2e90f4b2011-12-13 13:22:37 -05001495 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001496 }
James Smart2e90f4b2011-12-13 13:22:37 -05001497 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1498 reg_err2 == SLIPORT_ERR2_REG_FW_RESTART)
1499 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1500 "3143 Port Down: Firmware Restarted\n");
1501 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1502 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1503 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1504 "3144 Port Down: Debug Dump\n");
1505 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1506 reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
1507 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1508 "3145 Port Down: Provisioning\n");
James Smart73d91e52011-10-10 21:32:10 -04001509 /*
1510 * On error status condition, driver need to wait for port
1511 * ready before performing reset.
1512 */
1513 rc = lpfc_sli4_pdev_status_reg_wait(phba);
1514 if (!rc) {
James Smart079b5c92011-08-21 21:48:49 -04001515 /* need reset: attempt for port recovery */
James Smart2fcee4b2010-12-15 17:57:46 -05001516 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -05001517 "2887 Reset Needed: Attempting Port "
1518 "Recovery...\n");
James Smart079b5c92011-08-21 21:48:49 -04001519 lpfc_offline_prep(phba);
1520 lpfc_offline(phba);
1521 lpfc_sli_brdrestart(phba);
1522 if (lpfc_online(phba) == 0) {
1523 lpfc_unblock_mgmt_io(phba);
James Smart2e90f4b2011-12-13 13:22:37 -05001524 /* don't report event on forced debug dump */
1525 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1526 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1527 return;
1528 else
1529 break;
James Smart079b5c92011-08-21 21:48:49 -04001530 }
1531 /* fall through for not able to recover */
James Smart2fcee4b2010-12-15 17:57:46 -05001532 }
James Smart6b5151f2012-01-18 16:24:06 -05001533 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1534 "3152 Unrecoverable error, bring the port "
1535 "offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001536 lpfc_sli4_offline_eratt(phba);
1537 break;
1538 case LPFC_SLI_INTF_IF_TYPE_1:
1539 default:
1540 break;
1541 }
James Smart2e90f4b2011-12-13 13:22:37 -05001542 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1543 "3123 Report dump event to upper layer\n");
1544 /* Send an internal error event to mgmt application */
1545 lpfc_board_errevt_to_mgmt(phba);
1546
1547 event_data = FC_REG_DUMP_EVENT;
1548 shost = lpfc_shost_from_vport(vport);
1549 fc_host_post_vendor_event(shost, fc_get_event_number(),
1550 sizeof(event_data), (char *) &event_data,
1551 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
James Smartda0436e2009-05-22 14:51:39 -04001552}
1553
1554/**
1555 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1556 * @phba: pointer to lpfc HBA data structure.
1557 *
1558 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1559 * routine from the API jump table function pointer from the lpfc_hba struct.
1560 *
1561 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001562 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001563 * Any other value - error.
1564 **/
1565void
1566lpfc_handle_eratt(struct lpfc_hba *phba)
1567{
1568 (*phba->lpfc_handle_eratt)(phba);
1569}
1570
1571/**
James Smart3621a712009-04-06 18:47:14 -04001572 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04001573 * @phba: pointer to lpfc hba data structure.
1574 *
1575 * This routine is invoked from the worker thread to handle a HBA host
1576 * attention link event.
1577 **/
dea31012005-04-17 16:05:31 -05001578void
James Smart2e0fef82007-06-17 19:56:36 -05001579lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001580{
James Smart2e0fef82007-06-17 19:56:36 -05001581 struct lpfc_vport *vport = phba->pport;
1582 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001583 LPFC_MBOXQ_t *pmb;
1584 volatile uint32_t control;
1585 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05001586 int rc = 0;
dea31012005-04-17 16:05:31 -05001587
1588 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001589 if (!pmb) {
1590 rc = 1;
dea31012005-04-17 16:05:31 -05001591 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05001592 }
dea31012005-04-17 16:05:31 -05001593
1594 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001595 if (!mp) {
1596 rc = 2;
dea31012005-04-17 16:05:31 -05001597 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05001598 }
dea31012005-04-17 16:05:31 -05001599
1600 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05001601 if (!mp->virt) {
1602 rc = 3;
dea31012005-04-17 16:05:31 -05001603 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05001604 }
dea31012005-04-17 16:05:31 -05001605
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05001606 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04001607 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05001608
1609 psli->slistat.link_event++;
James Smart76a95d72010-11-20 23:11:48 -05001610 lpfc_read_topology(phba, pmb, mp);
1611 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smart2e0fef82007-06-17 19:56:36 -05001612 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04001613 /* Block ELS IOCBs until we have processed this mbox command */
1614 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04001615 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05001616 if (rc == MBX_NOT_FINISHED) {
1617 rc = 4;
James Smart14691152006-12-02 13:34:28 -05001618 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05001619 }
dea31012005-04-17 16:05:31 -05001620
1621 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05001622 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001623 writel(HA_LATT, phba->HAregaddr);
1624 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001625 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001626
1627 return;
1628
James Smart14691152006-12-02 13:34:28 -05001629lpfc_handle_latt_free_mbuf:
James Smart0d2b6b82008-06-14 22:52:47 -04001630 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05001631 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001632lpfc_handle_latt_free_mp:
1633 kfree(mp);
1634lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04001635 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001636lpfc_handle_latt_err_exit:
1637 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001638 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001639 psli->sli_flag |= LPFC_PROCESS_LA;
1640 control = readl(phba->HCregaddr);
1641 control |= HC_LAINT_ENA;
1642 writel(control, phba->HCregaddr);
1643 readl(phba->HCregaddr); /* flush */
1644
1645 /* Clear Link Attention in HA REG */
1646 writel(HA_LATT, phba->HAregaddr);
1647 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001648 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001649 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001650 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001651
James Smart09372822008-01-11 01:52:54 -05001652 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1653 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05001654
1655 return;
1656}
1657
James Smarte59058c2008-08-24 21:49:00 -04001658/**
James Smart3621a712009-04-06 18:47:14 -04001659 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04001660 * @phba: pointer to lpfc hba data structure.
1661 * @vpd: pointer to the vital product data.
1662 * @len: length of the vital product data in bytes.
1663 *
1664 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1665 * an array of characters. In this routine, the ModelName, ProgramType, and
1666 * ModelDesc, etc. fields of the phba data structure will be populated.
1667 *
1668 * Return codes
1669 * 0 - pointer to the VPD passed in is NULL
1670 * 1 - success
1671 **/
James Smart3772a992009-05-22 14:50:54 -04001672int
James Smart2e0fef82007-06-17 19:56:36 -05001673lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05001674{
1675 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05001676 int Length;
dea31012005-04-17 16:05:31 -05001677 int i, j;
1678 int finished = 0;
1679 int index = 0;
1680
1681 if (!vpd)
1682 return 0;
1683
1684 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05001685 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001686 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05001687 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1688 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001689 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05001690 switch (vpd[index]) {
1691 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001692 case 0x91:
dea31012005-04-17 16:05:31 -05001693 index += 1;
1694 lenlo = vpd[index];
1695 index += 1;
1696 lenhi = vpd[index];
1697 index += 1;
1698 i = ((((unsigned short)lenhi) << 8) + lenlo);
1699 index += i;
1700 break;
1701 case 0x90:
1702 index += 1;
1703 lenlo = vpd[index];
1704 index += 1;
1705 lenhi = vpd[index];
1706 index += 1;
1707 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001708 if (Length > len - index)
1709 Length = len - index;
dea31012005-04-17 16:05:31 -05001710 while (Length > 0) {
1711 /* Look for Serial Number */
1712 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1713 index += 2;
1714 i = vpd[index];
1715 index += 1;
1716 j = 0;
1717 Length -= (3+i);
1718 while(i--) {
1719 phba->SerialNumber[j++] = vpd[index++];
1720 if (j == 31)
1721 break;
1722 }
1723 phba->SerialNumber[j] = 0;
1724 continue;
1725 }
1726 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1727 phba->vpd_flag |= VPD_MODEL_DESC;
1728 index += 2;
1729 i = vpd[index];
1730 index += 1;
1731 j = 0;
1732 Length -= (3+i);
1733 while(i--) {
1734 phba->ModelDesc[j++] = vpd[index++];
1735 if (j == 255)
1736 break;
1737 }
1738 phba->ModelDesc[j] = 0;
1739 continue;
1740 }
1741 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1742 phba->vpd_flag |= VPD_MODEL_NAME;
1743 index += 2;
1744 i = vpd[index];
1745 index += 1;
1746 j = 0;
1747 Length -= (3+i);
1748 while(i--) {
1749 phba->ModelName[j++] = vpd[index++];
1750 if (j == 79)
1751 break;
1752 }
1753 phba->ModelName[j] = 0;
1754 continue;
1755 }
1756 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1757 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1758 index += 2;
1759 i = vpd[index];
1760 index += 1;
1761 j = 0;
1762 Length -= (3+i);
1763 while(i--) {
1764 phba->ProgramType[j++] = vpd[index++];
1765 if (j == 255)
1766 break;
1767 }
1768 phba->ProgramType[j] = 0;
1769 continue;
1770 }
1771 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1772 phba->vpd_flag |= VPD_PORT;
1773 index += 2;
1774 i = vpd[index];
1775 index += 1;
1776 j = 0;
1777 Length -= (3+i);
1778 while(i--) {
James Smartcd1c8302011-10-10 21:33:25 -04001779 if ((phba->sli_rev == LPFC_SLI_REV4) &&
1780 (phba->sli4_hba.pport_name_sta ==
1781 LPFC_SLI4_PPNAME_GET)) {
1782 j++;
1783 index++;
1784 } else
1785 phba->Port[j++] = vpd[index++];
1786 if (j == 19)
1787 break;
dea31012005-04-17 16:05:31 -05001788 }
James Smartcd1c8302011-10-10 21:33:25 -04001789 if ((phba->sli_rev != LPFC_SLI_REV4) ||
1790 (phba->sli4_hba.pport_name_sta ==
1791 LPFC_SLI4_PPNAME_NON))
1792 phba->Port[j] = 0;
dea31012005-04-17 16:05:31 -05001793 continue;
1794 }
1795 else {
1796 index += 2;
1797 i = vpd[index];
1798 index += 1;
1799 index += i;
1800 Length -= (3 + i);
1801 }
1802 }
1803 finished = 0;
1804 break;
1805 case 0x78:
1806 finished = 1;
1807 break;
1808 default:
1809 index ++;
1810 break;
1811 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001812 }
dea31012005-04-17 16:05:31 -05001813
1814 return(1);
1815}
1816
James Smarte59058c2008-08-24 21:49:00 -04001817/**
James Smart3621a712009-04-06 18:47:14 -04001818 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04001819 * @phba: pointer to lpfc hba data structure.
1820 * @mdp: pointer to the data structure to hold the derived model name.
1821 * @descp: pointer to the data structure to hold the derived description.
1822 *
1823 * This routine retrieves HBA's description based on its registered PCI device
1824 * ID. The @descp passed into this function points to an array of 256 chars. It
1825 * shall be returned with the model name, maximum speed, and the host bus type.
1826 * The @mdp passed into this function points to an array of 80 chars. When the
1827 * function returns, the @mdp will be filled with the model name.
1828 **/
dea31012005-04-17 16:05:31 -05001829static void
James Smart2e0fef82007-06-17 19:56:36 -05001830lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05001831{
1832 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05001833 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001834 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04001835 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04001836 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001837 struct {
James Smarta747c9c2009-11-18 15:41:10 -05001838 char *name;
1839 char *bus;
1840 char *function;
1841 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001842
1843 if (mdp && mdp[0] != '\0'
1844 && descp && descp[0] != '\0')
1845 return;
1846
James Smartc0c11512011-05-24 11:41:34 -04001847 if (phba->lmt & LMT_16Gb)
1848 max_speed = 16;
1849 else if (phba->lmt & LMT_10Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001850 max_speed = 10;
1851 else if (phba->lmt & LMT_8Gb)
1852 max_speed = 8;
1853 else if (phba->lmt & LMT_4Gb)
1854 max_speed = 4;
1855 else if (phba->lmt & LMT_2Gb)
1856 max_speed = 2;
1857 else
1858 max_speed = 1;
dea31012005-04-17 16:05:31 -05001859
1860 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05001861
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001862 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001863 case PCI_DEVICE_ID_FIREFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001864 m = (typeof(m)){"LP6000", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001865 break;
dea31012005-04-17 16:05:31 -05001866 case PCI_DEVICE_ID_SUPERFLY:
1867 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smarta747c9c2009-11-18 15:41:10 -05001868 m = (typeof(m)){"LP7000", "PCI",
1869 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001870 else
James Smarta747c9c2009-11-18 15:41:10 -05001871 m = (typeof(m)){"LP7000E", "PCI",
1872 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001873 break;
1874 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001875 m = (typeof(m)){"LP8000", "PCI",
1876 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001877 break;
1878 case PCI_DEVICE_ID_CENTAUR:
1879 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smarta747c9c2009-11-18 15:41:10 -05001880 m = (typeof(m)){"LP9002", "PCI",
1881 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001882 else
James Smarta747c9c2009-11-18 15:41:10 -05001883 m = (typeof(m)){"LP9000", "PCI",
1884 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001885 break;
1886 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001887 m = (typeof(m)){"LP952", "PCI",
1888 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001889 break;
1890 case PCI_DEVICE_ID_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05001891 m = (typeof(m)){"LP9802", "PCI-X",
1892 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001893 break;
1894 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05001895 m = (typeof(m)){"LP10000", "PCI-X",
1896 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001897 break;
1898 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05001899 m = (typeof(m)){"LPX1000", "PCI-X",
1900 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001901 break;
1902 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001903 m = (typeof(m)){"LP982", "PCI-X",
1904 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001905 break;
1906 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001907 m = (typeof(m)){"LP1050", "PCI-X",
1908 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001909 break;
1910 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05001911 m = (typeof(m)){"LP11000", "PCI-X2",
1912 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001913 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001914 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001915 m = (typeof(m)){"LP11000-SP", "PCI-X2",
1916 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001917 break;
1918 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001919 m = (typeof(m)){"LP11002-SP", "PCI-X2",
1920 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001921 break;
1922 case PCI_DEVICE_ID_NEPTUNE:
James Smarta747c9c2009-11-18 15:41:10 -05001923 m = (typeof(m)){"LPe1000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001924 break;
1925 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001926 m = (typeof(m)){"LPe1000-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001927 break;
1928 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001929 m = (typeof(m)){"LPe1002-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001930 break;
dea31012005-04-17 16:05:31 -05001931 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05001932 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001933 break;
1934 case PCI_DEVICE_ID_BSMB:
James Smarta747c9c2009-11-18 15:41:10 -05001935 m = (typeof(m)){"LP111", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001936 break;
1937 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05001938 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001939 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001940 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001941 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001942 break;
1943 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001944 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04001945 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001946 break;
dea31012005-04-17 16:05:31 -05001947 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05001948 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001949 break;
1950 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05001951 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001952 break;
1953 case PCI_DEVICE_ID_LP101:
James Smarta747c9c2009-11-18 15:41:10 -05001954 m = (typeof(m)){"LP101", "PCI-X", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001955 break;
1956 case PCI_DEVICE_ID_LP10000S:
James Smarta747c9c2009-11-18 15:41:10 -05001957 m = (typeof(m)){"LP10000-S", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001958 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001959 case PCI_DEVICE_ID_LP11000S:
James Smarta747c9c2009-11-18 15:41:10 -05001960 m = (typeof(m)){"LP11000-S", "PCI-X2", "Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05001961 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001962 case PCI_DEVICE_ID_LPE11000S:
James Smarta747c9c2009-11-18 15:41:10 -05001963 m = (typeof(m)){"LPe11000-S", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05001964 break;
James Smartb87eab32007-04-25 09:53:28 -04001965 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05001966 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001967 break;
1968 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05001969 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001970 break;
1971 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05001972 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001973 break;
1974 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001975 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001976 break;
1977 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001978 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001979 break;
1980 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05001981 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001982 break;
James Smart84774a42008-08-24 21:50:06 -04001983 case PCI_DEVICE_ID_HORNET:
James Smarta747c9c2009-11-18 15:41:10 -05001984 m = (typeof(m)){"LP21000", "PCIe", "FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001985 GE = 1;
1986 break;
1987 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05001988 m = (typeof(m)){"LPev12000", "PCIe IOV",
1989 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001990 break;
1991 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05001992 m = (typeof(m)){"LPev12000", "PCIe IOV",
1993 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001994 break;
1995 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05001996 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
1997 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001998 break;
James Smartda0436e2009-05-22 14:51:39 -04001999 case PCI_DEVICE_ID_TIGERSHARK:
2000 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002001 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04002002 break;
James Smarta747c9c2009-11-18 15:41:10 -05002003 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04002004 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002005 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2006 break;
2007 case PCI_DEVICE_ID_FALCON:
2008 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2009 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04002010 break;
James Smart98fc5dd2010-06-07 15:24:29 -04002011 case PCI_DEVICE_ID_BALIUS:
2012 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
2013 "Fibre Channel Adapter"};
2014 break;
James Smart085c6472010-11-20 23:11:37 -05002015 case PCI_DEVICE_ID_LANCER_FC:
James Smartc0c11512011-05-24 11:41:34 -04002016 case PCI_DEVICE_ID_LANCER_FC_VF:
2017 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
James Smart085c6472010-11-20 23:11:37 -05002018 break;
2019 case PCI_DEVICE_ID_LANCER_FCOE:
James Smartc0c11512011-05-24 11:41:34 -04002020 case PCI_DEVICE_ID_LANCER_FCOE_VF:
James Smart085c6472010-11-20 23:11:37 -05002021 oneConnect = 1;
James Smart079b5c92011-08-21 21:48:49 -04002022 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
James Smart085c6472010-11-20 23:11:37 -05002023 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002024 default:
James Smarta747c9c2009-11-18 15:41:10 -05002025 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002026 break;
dea31012005-04-17 16:05:31 -05002027 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002028
2029 if (mdp && mdp[0] == '\0')
2030 snprintf(mdp, 79,"%s", m.name);
James Smartc0c11512011-05-24 11:41:34 -04002031 /*
2032 * oneConnect hba requires special processing, they are all initiators
James Smartda0436e2009-05-22 14:51:39 -04002033 * and we put the port number on the end
2034 */
2035 if (descp && descp[0] == '\0') {
2036 if (oneConnect)
2037 snprintf(descp, 255,
James Smarta747c9c2009-11-18 15:41:10 -05002038 "Emulex OneConnect %s, %s Initiator, Port %s",
2039 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04002040 phba->Port);
2041 else
2042 snprintf(descp, 255,
2043 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05002044 m.name, max_speed, (GE) ? "GE" : "Gb",
2045 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002046 }
dea31012005-04-17 16:05:31 -05002047}
2048
James Smarte59058c2008-08-24 21:49:00 -04002049/**
James Smart3621a712009-04-06 18:47:14 -04002050 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04002051 * @phba: pointer to lpfc hba data structure.
2052 * @pring: pointer to a IOCB ring.
2053 * @cnt: the number of IOCBs to be posted to the IOCB ring.
2054 *
2055 * This routine posts a given number of IOCBs with the associated DMA buffer
2056 * descriptors specified by the cnt argument to the given IOCB ring.
2057 *
2058 * Return codes
2059 * The number of IOCBs NOT able to be posted to the IOCB ring.
2060 **/
dea31012005-04-17 16:05:31 -05002061int
James Smart495a7142008-06-14 22:52:59 -04002062lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05002063{
2064 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002065 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05002066 struct lpfc_dmabuf *mp1, *mp2;
2067
2068 cnt += pring->missbufcnt;
2069
2070 /* While there are buffers to post */
2071 while (cnt > 0) {
2072 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002073 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002074 if (iocb == NULL) {
2075 pring->missbufcnt = cnt;
2076 return cnt;
2077 }
dea31012005-04-17 16:05:31 -05002078 icmd = &iocb->iocb;
2079
2080 /* 2 buffers can be posted per command */
2081 /* Allocate buffer to post */
2082 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2083 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04002084 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2085 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002086 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002087 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002088 pring->missbufcnt = cnt;
2089 return cnt;
2090 }
2091
2092 INIT_LIST_HEAD(&mp1->list);
2093 /* Allocate buffer to post */
2094 if (cnt > 1) {
2095 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2096 if (mp2)
2097 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2098 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04002099 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002100 kfree(mp2);
dea31012005-04-17 16:05:31 -05002101 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2102 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002103 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002104 pring->missbufcnt = cnt;
2105 return cnt;
2106 }
2107
2108 INIT_LIST_HEAD(&mp2->list);
2109 } else {
2110 mp2 = NULL;
2111 }
2112
2113 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2114 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2115 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2116 icmd->ulpBdeCount = 1;
2117 cnt--;
2118 if (mp2) {
2119 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2120 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2121 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2122 cnt--;
2123 icmd->ulpBdeCount = 2;
2124 }
2125
2126 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2127 icmd->ulpLe = 1;
2128
James Smart3772a992009-05-22 14:50:54 -04002129 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2130 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05002131 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2132 kfree(mp1);
2133 cnt++;
2134 if (mp2) {
2135 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2136 kfree(mp2);
2137 cnt++;
2138 }
James Bottomley604a3e32005-10-29 10:28:33 -05002139 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002140 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05002141 return cnt;
2142 }
dea31012005-04-17 16:05:31 -05002143 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05002144 if (mp2)
dea31012005-04-17 16:05:31 -05002145 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05002146 }
2147 pring->missbufcnt = 0;
2148 return 0;
2149}
2150
James Smarte59058c2008-08-24 21:49:00 -04002151/**
James Smart3621a712009-04-06 18:47:14 -04002152 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04002153 * @phba: pointer to lpfc hba data structure.
2154 *
2155 * This routine posts initial receive IOCB buffers to the ELS ring. The
2156 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
2157 * set to 64 IOCBs.
2158 *
2159 * Return codes
2160 * 0 - success (currently always success)
2161 **/
dea31012005-04-17 16:05:31 -05002162static int
James Smart2e0fef82007-06-17 19:56:36 -05002163lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002164{
2165 struct lpfc_sli *psli = &phba->sli;
2166
2167 /* Ring 0, ELS / CT buffers */
James Smart495a7142008-06-14 22:52:59 -04002168 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05002169 /* Ring 2 - FCP no buffers needed */
2170
2171 return 0;
2172}
2173
2174#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2175
James Smarte59058c2008-08-24 21:49:00 -04002176/**
James Smart3621a712009-04-06 18:47:14 -04002177 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04002178 * @HashResultPointer: pointer to an array as hash table.
2179 *
2180 * This routine sets up the initial values to the array of hash table entries
2181 * for the LC HBAs.
2182 **/
dea31012005-04-17 16:05:31 -05002183static void
2184lpfc_sha_init(uint32_t * HashResultPointer)
2185{
2186 HashResultPointer[0] = 0x67452301;
2187 HashResultPointer[1] = 0xEFCDAB89;
2188 HashResultPointer[2] = 0x98BADCFE;
2189 HashResultPointer[3] = 0x10325476;
2190 HashResultPointer[4] = 0xC3D2E1F0;
2191}
2192
James Smarte59058c2008-08-24 21:49:00 -04002193/**
James Smart3621a712009-04-06 18:47:14 -04002194 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04002195 * @HashResultPointer: pointer to an initial/result hash table.
2196 * @HashWorkingPointer: pointer to an working hash table.
2197 *
2198 * This routine iterates an initial hash table pointed by @HashResultPointer
2199 * with the values from the working hash table pointeed by @HashWorkingPointer.
2200 * The results are putting back to the initial hash table, returned through
2201 * the @HashResultPointer as the result hash table.
2202 **/
dea31012005-04-17 16:05:31 -05002203static void
2204lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2205{
2206 int t;
2207 uint32_t TEMP;
2208 uint32_t A, B, C, D, E;
2209 t = 16;
2210 do {
2211 HashWorkingPointer[t] =
2212 S(1,
2213 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2214 8] ^
2215 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2216 } while (++t <= 79);
2217 t = 0;
2218 A = HashResultPointer[0];
2219 B = HashResultPointer[1];
2220 C = HashResultPointer[2];
2221 D = HashResultPointer[3];
2222 E = HashResultPointer[4];
2223
2224 do {
2225 if (t < 20) {
2226 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2227 } else if (t < 40) {
2228 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2229 } else if (t < 60) {
2230 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2231 } else {
2232 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2233 }
2234 TEMP += S(5, A) + E + HashWorkingPointer[t];
2235 E = D;
2236 D = C;
2237 C = S(30, B);
2238 B = A;
2239 A = TEMP;
2240 } while (++t <= 79);
2241
2242 HashResultPointer[0] += A;
2243 HashResultPointer[1] += B;
2244 HashResultPointer[2] += C;
2245 HashResultPointer[3] += D;
2246 HashResultPointer[4] += E;
2247
2248}
2249
James Smarte59058c2008-08-24 21:49:00 -04002250/**
James Smart3621a712009-04-06 18:47:14 -04002251 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002252 * @RandomChallenge: pointer to the entry of host challenge random number array.
2253 * @HashWorking: pointer to the entry of the working hash array.
2254 *
2255 * This routine calculates the working hash array referred by @HashWorking
2256 * from the challenge random numbers associated with the host, referred by
2257 * @RandomChallenge. The result is put into the entry of the working hash
2258 * array and returned by reference through @HashWorking.
2259 **/
dea31012005-04-17 16:05:31 -05002260static void
2261lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2262{
2263 *HashWorking = (*RandomChallenge ^ *HashWorking);
2264}
2265
James Smarte59058c2008-08-24 21:49:00 -04002266/**
James Smart3621a712009-04-06 18:47:14 -04002267 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002268 * @phba: pointer to lpfc hba data structure.
2269 * @hbainit: pointer to an array of unsigned 32-bit integers.
2270 *
2271 * This routine performs the special handling for LC HBA initialization.
2272 **/
dea31012005-04-17 16:05:31 -05002273void
2274lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2275{
2276 int t;
2277 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002278 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002279
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002280 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002281 if (!HashWorking)
2282 return;
2283
dea31012005-04-17 16:05:31 -05002284 HashWorking[0] = HashWorking[78] = *pwwnn++;
2285 HashWorking[1] = HashWorking[79] = *pwwnn;
2286
2287 for (t = 0; t < 7; t++)
2288 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2289
2290 lpfc_sha_init(hbainit);
2291 lpfc_sha_iterate(hbainit, HashWorking);
2292 kfree(HashWorking);
2293}
2294
James Smarte59058c2008-08-24 21:49:00 -04002295/**
James Smart3621a712009-04-06 18:47:14 -04002296 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002297 * @vport: pointer to a virtual N_Port data structure.
2298 *
2299 * This routine performs the necessary cleanups before deleting the @vport.
2300 * It invokes the discovery state machine to perform necessary state
2301 * transitions and to release the ndlps associated with the @vport. Note,
2302 * the physical port is treated as @vport 0.
2303 **/
James Smart87af33f2007-10-27 13:37:43 -04002304void
James Smart2e0fef82007-06-17 19:56:36 -05002305lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002306{
James Smart87af33f2007-10-27 13:37:43 -04002307 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002308 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002309 int i = 0;
dea31012005-04-17 16:05:31 -05002310
James Smart87af33f2007-10-27 13:37:43 -04002311 if (phba->link_state > LPFC_LINK_DOWN)
2312 lpfc_port_link_failure(vport);
2313
2314 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002315 if (!NLP_CHK_NODE_ACT(ndlp)) {
2316 ndlp = lpfc_enable_node(vport, ndlp,
2317 NLP_STE_UNUSED_NODE);
2318 if (!ndlp)
2319 continue;
2320 spin_lock_irq(&phba->ndlp_lock);
2321 NLP_SET_FREE_REQ(ndlp);
2322 spin_unlock_irq(&phba->ndlp_lock);
2323 /* Trigger the release of the ndlp memory */
2324 lpfc_nlp_put(ndlp);
2325 continue;
2326 }
2327 spin_lock_irq(&phba->ndlp_lock);
2328 if (NLP_CHK_FREE_REQ(ndlp)) {
2329 /* The ndlp should not be in memory free mode already */
2330 spin_unlock_irq(&phba->ndlp_lock);
2331 continue;
2332 } else
2333 /* Indicate request for freeing ndlp memory */
2334 NLP_SET_FREE_REQ(ndlp);
2335 spin_unlock_irq(&phba->ndlp_lock);
2336
James Smart58da1ff2008-04-07 10:15:56 -04002337 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2338 ndlp->nlp_DID == Fabric_DID) {
2339 /* Just free up ndlp with Fabric_DID for vports */
2340 lpfc_nlp_put(ndlp);
2341 continue;
2342 }
2343
James Smarteff4a012012-01-18 16:25:25 -05002344 /* take care of nodes in unused state before the state
2345 * machine taking action.
2346 */
2347 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
2348 lpfc_nlp_put(ndlp);
2349 continue;
2350 }
2351
James Smart87af33f2007-10-27 13:37:43 -04002352 if (ndlp->nlp_type & NLP_FABRIC)
2353 lpfc_disc_state_machine(vport, ndlp, NULL,
2354 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002355
James Smart87af33f2007-10-27 13:37:43 -04002356 lpfc_disc_state_machine(vport, ndlp, NULL,
2357 NLP_EVT_DEVICE_RM);
2358 }
2359
James Smarta8adb832007-10-27 13:37:53 -04002360 /* At this point, ALL ndlp's should be gone
2361 * because of the previous NLP_EVT_DEVICE_RM.
2362 * Lets wait for this to happen, if needed.
2363 */
James Smart87af33f2007-10-27 13:37:43 -04002364 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002365 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002366 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002367 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002368 list_for_each_entry_safe(ndlp, next_ndlp,
2369 &vport->fc_nodes, nlp_listp) {
2370 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2371 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002372 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002373 "usgmap:x%x refcnt:%d\n",
2374 ndlp->nlp_DID, (void *)ndlp,
2375 ndlp->nlp_usg_map,
2376 atomic_read(
2377 &ndlp->kref.refcount));
2378 }
James Smarta8adb832007-10-27 13:37:53 -04002379 break;
James Smart87af33f2007-10-27 13:37:43 -04002380 }
James Smarta8adb832007-10-27 13:37:53 -04002381
2382 /* Wait for any activity on ndlps to settle */
2383 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002384 }
James Smart1151e3e2011-02-16 12:39:35 -05002385 lpfc_cleanup_vports_rrqs(vport, NULL);
dea31012005-04-17 16:05:31 -05002386}
2387
James Smarte59058c2008-08-24 21:49:00 -04002388/**
James Smart3621a712009-04-06 18:47:14 -04002389 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002390 * @vport: pointer to a virtual N_Port data structure.
2391 *
2392 * This routine stops all the timers associated with a @vport. This function
2393 * is invoked before disabling or deleting a @vport. Note that the physical
2394 * port is treated as @vport 0.
2395 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002396void
2397lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002398{
James Smart92d7f7b2007-06-17 19:56:38 -05002399 del_timer_sync(&vport->els_tmofunc);
2400 del_timer_sync(&vport->fc_fdmitmo);
James Smart92494142011-02-16 12:39:44 -05002401 del_timer_sync(&vport->delayed_disc_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002402 lpfc_can_disctmo(vport);
2403 return;
dea31012005-04-17 16:05:31 -05002404}
2405
James Smarte59058c2008-08-24 21:49:00 -04002406/**
James Smartecfd03c2010-02-12 14:41:27 -05002407 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2408 * @phba: pointer to lpfc hba data structure.
2409 *
2410 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2411 * caller of this routine should already hold the host lock.
2412 **/
2413void
2414__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2415{
James Smart5ac6b302010-10-22 11:05:36 -04002416 /* Clear pending FCF rediscovery wait flag */
2417 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2418
James Smartecfd03c2010-02-12 14:41:27 -05002419 /* Now, try to stop the timer */
2420 del_timer(&phba->fcf.redisc_wait);
2421}
2422
2423/**
2424 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2425 * @phba: pointer to lpfc hba data structure.
2426 *
2427 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2428 * checks whether the FCF rediscovery wait timer is pending with the host
2429 * lock held before proceeding with disabling the timer and clearing the
2430 * wait timer pendig flag.
2431 **/
2432void
2433lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2434{
2435 spin_lock_irq(&phba->hbalock);
2436 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2437 /* FCF rediscovery timer already fired or stopped */
2438 spin_unlock_irq(&phba->hbalock);
2439 return;
2440 }
2441 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart5ac6b302010-10-22 11:05:36 -04002442 /* Clear failover in progress flags */
2443 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05002444 spin_unlock_irq(&phba->hbalock);
2445}
2446
2447/**
James Smart3772a992009-05-22 14:50:54 -04002448 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002449 * @phba: pointer to lpfc hba data structure.
2450 *
2451 * This routine stops all the timers associated with a HBA. This function is
2452 * invoked before either putting a HBA offline or unloading the driver.
2453 **/
James Smart3772a992009-05-22 14:50:54 -04002454void
2455lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002456{
James Smart51ef4c22007-08-02 11:10:31 -04002457 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05002458 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002459 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002460 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002461 del_timer_sync(&phba->hb_tmofunc);
James Smart1151e3e2011-02-16 12:39:35 -05002462 if (phba->sli_rev == LPFC_SLI_REV4) {
2463 del_timer_sync(&phba->rrq_tmr);
2464 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2465 }
James Smart3772a992009-05-22 14:50:54 -04002466 phba->hb_outstanding = 0;
2467
2468 switch (phba->pci_dev_grp) {
2469 case LPFC_PCI_DEV_LP:
2470 /* Stop any LightPulse device specific driver timers */
2471 del_timer_sync(&phba->fcp_poll_timer);
2472 break;
2473 case LPFC_PCI_DEV_OC:
2474 /* Stop any OneConnect device sepcific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05002475 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04002476 break;
2477 default:
2478 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2479 "0297 Invalid device group (x%x)\n",
2480 phba->pci_dev_grp);
2481 break;
2482 }
James Smart2e0fef82007-06-17 19:56:36 -05002483 return;
dea31012005-04-17 16:05:31 -05002484}
2485
James Smarte59058c2008-08-24 21:49:00 -04002486/**
James Smart3621a712009-04-06 18:47:14 -04002487 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002488 * @phba: pointer to lpfc hba data structure.
2489 *
2490 * This routine marks a HBA's management interface as blocked. Once the HBA's
2491 * management interface is marked as blocked, all the user space access to
2492 * the HBA, whether they are from sysfs interface or libdfc interface will
2493 * all be blocked. The HBA is set to block the management interface when the
2494 * driver prepares the HBA interface for online or offline.
2495 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002496static void
2497lpfc_block_mgmt_io(struct lpfc_hba * phba)
2498{
2499 unsigned long iflag;
James Smart6e7288d2010-06-07 15:23:35 -04002500 uint8_t actcmd = MBX_HEARTBEAT;
2501 unsigned long timeout;
2502
James Smarta183a152011-10-10 21:32:43 -04002503 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
Adrian Bunka6ababd2007-11-05 18:07:33 +01002504 spin_lock_irqsave(&phba->hbalock, iflag);
2505 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
James Smarta183a152011-10-10 21:32:43 -04002506 if (phba->sli.mbox_active) {
James Smart6e7288d2010-06-07 15:23:35 -04002507 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
James Smarta183a152011-10-10 21:32:43 -04002508 /* Determine how long we might wait for the active mailbox
2509 * command to be gracefully completed by firmware.
2510 */
2511 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
2512 phba->sli.mbox_active) * 1000) + jiffies;
2513 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002514 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002515
James Smart6e7288d2010-06-07 15:23:35 -04002516 /* Wait for the outstnading mailbox command to complete */
2517 while (phba->sli.mbox_active) {
2518 /* Check active mailbox complete status every 2ms */
2519 msleep(2);
2520 if (time_after(jiffies, timeout)) {
2521 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2522 "2813 Mgmt IO is Blocked %x "
2523 "- mbox cmd %x still active\n",
2524 phba->sli.sli_flag, actcmd);
2525 break;
2526 }
2527 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002528}
2529
James Smarte59058c2008-08-24 21:49:00 -04002530/**
James Smart6b5151f2012-01-18 16:24:06 -05002531 * lpfc_sli4_node_prep - Assign RPIs for active nodes.
2532 * @phba: pointer to lpfc hba data structure.
2533 *
2534 * Allocate RPIs for all active remote nodes. This is needed whenever
2535 * an SLI4 adapter is reset and the driver is not unloading. Its purpose
2536 * is to fixup the temporary rpi assignments.
2537 **/
2538void
2539lpfc_sli4_node_prep(struct lpfc_hba *phba)
2540{
2541 struct lpfc_nodelist *ndlp, *next_ndlp;
2542 struct lpfc_vport **vports;
2543 int i;
2544
2545 if (phba->sli_rev != LPFC_SLI_REV4)
2546 return;
2547
2548 vports = lpfc_create_vport_work_array(phba);
2549 if (vports != NULL) {
2550 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2551 if (vports[i]->load_flag & FC_UNLOADING)
2552 continue;
2553
2554 list_for_each_entry_safe(ndlp, next_ndlp,
2555 &vports[i]->fc_nodes,
2556 nlp_listp) {
2557 if (NLP_CHK_NODE_ACT(ndlp))
2558 ndlp->nlp_rpi =
2559 lpfc_sli4_alloc_rpi(phba);
2560 }
2561 }
2562 }
2563 lpfc_destroy_vport_work_array(phba, vports);
2564}
2565
2566/**
James Smart3621a712009-04-06 18:47:14 -04002567 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04002568 * @phba: pointer to lpfc hba data structure.
2569 *
2570 * This routine initializes the HBA and brings a HBA online. During this
2571 * process, the management interface is blocked to prevent user space access
2572 * to the HBA interfering with the driver initialization.
2573 *
2574 * Return codes
2575 * 0 - successful
2576 * 1 - failed
2577 **/
dea31012005-04-17 16:05:31 -05002578int
James Smart2e0fef82007-06-17 19:56:36 -05002579lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002580{
Julia Lawall372bd282008-12-16 16:15:08 +01002581 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04002582 struct lpfc_vport **vports;
2583 int i;
James Smart2e0fef82007-06-17 19:56:36 -05002584
dea31012005-04-17 16:05:31 -05002585 if (!phba)
2586 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01002587 vport = phba->pport;
dea31012005-04-17 16:05:31 -05002588
James Smart2e0fef82007-06-17 19:56:36 -05002589 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05002590 return 0;
2591
James Smarted957682007-06-17 19:56:37 -05002592 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002593 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05002594
James Smart46fa3112007-04-25 09:51:45 -04002595 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002596
James Smart46fa3112007-04-25 09:51:45 -04002597 if (!lpfc_sli_queue_setup(phba)) {
2598 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002599 return 1;
James Smart46fa3112007-04-25 09:51:45 -04002600 }
2601
James Smartda0436e2009-05-22 14:51:39 -04002602 if (phba->sli_rev == LPFC_SLI_REV4) {
2603 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2604 lpfc_unblock_mgmt_io(phba);
2605 return 1;
2606 }
2607 } else {
2608 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2609 lpfc_unblock_mgmt_io(phba);
2610 return 1;
2611 }
James Smart46fa3112007-04-25 09:51:45 -04002612 }
dea31012005-04-17 16:05:31 -05002613
James Smart549e55c2007-08-02 11:09:51 -04002614 vports = lpfc_create_vport_work_array(phba);
2615 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002616 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002617 struct Scsi_Host *shost;
2618 shost = lpfc_shost_from_vport(vports[i]);
2619 spin_lock_irq(shost->host_lock);
2620 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2621 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2622 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart1c6834a2009-07-19 10:01:26 -04002623 if (phba->sli_rev == LPFC_SLI_REV4)
2624 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart549e55c2007-08-02 11:09:51 -04002625 spin_unlock_irq(shost->host_lock);
2626 }
James Smart09372822008-01-11 01:52:54 -05002627 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002628
James Smart46fa3112007-04-25 09:51:45 -04002629 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002630 return 0;
2631}
2632
James Smarte59058c2008-08-24 21:49:00 -04002633/**
James Smart3621a712009-04-06 18:47:14 -04002634 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04002635 * @phba: pointer to lpfc hba data structure.
2636 *
2637 * This routine marks a HBA's management interface as not blocked. Once the
2638 * HBA's management interface is marked as not blocked, all the user space
2639 * access to the HBA, whether they are from sysfs interface or libdfc
2640 * interface will be allowed. The HBA is set to block the management interface
2641 * when the driver prepares the HBA interface for online or offline and then
2642 * set to unblock the management interface afterwards.
2643 **/
James Smart46fa3112007-04-25 09:51:45 -04002644void
James Smart46fa3112007-04-25 09:51:45 -04002645lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2646{
2647 unsigned long iflag;
2648
James Smart2e0fef82007-06-17 19:56:36 -05002649 spin_lock_irqsave(&phba->hbalock, iflag);
2650 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2651 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04002652}
2653
James Smarte59058c2008-08-24 21:49:00 -04002654/**
James Smart3621a712009-04-06 18:47:14 -04002655 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04002656 * @phba: pointer to lpfc hba data structure.
2657 *
2658 * This routine is invoked to prepare a HBA to be brought offline. It performs
2659 * unregistration login to all the nodes on all vports and flushes the mailbox
2660 * queue to make it ready to be brought offline.
2661 **/
James Smart46fa3112007-04-25 09:51:45 -04002662void
2663lpfc_offline_prep(struct lpfc_hba * phba)
2664{
James Smart2e0fef82007-06-17 19:56:36 -05002665 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04002666 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04002667 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05002668 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04002669 int i;
dea31012005-04-17 16:05:31 -05002670
James Smart2e0fef82007-06-17 19:56:36 -05002671 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002672 return;
dea31012005-04-17 16:05:31 -05002673
James Smart46fa3112007-04-25 09:51:45 -04002674 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002675
2676 lpfc_linkdown(phba);
2677
James Smart87af33f2007-10-27 13:37:43 -04002678 /* Issue an unreg_login to all nodes on all vports */
2679 vports = lpfc_create_vport_work_array(phba);
2680 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04002681 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04002682 if (vports[i]->load_flag & FC_UNLOADING)
2683 continue;
James Smart72100cc2010-02-12 14:43:01 -05002684 shost = lpfc_shost_from_vport(vports[i]);
2685 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002686 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002687 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2688 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05002689 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002690
James Smart87af33f2007-10-27 13:37:43 -04002691 shost = lpfc_shost_from_vport(vports[i]);
2692 list_for_each_entry_safe(ndlp, next_ndlp,
2693 &vports[i]->fc_nodes,
2694 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002695 if (!NLP_CHK_NODE_ACT(ndlp))
2696 continue;
James Smart87af33f2007-10-27 13:37:43 -04002697 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2698 continue;
2699 if (ndlp->nlp_type & NLP_FABRIC) {
2700 lpfc_disc_state_machine(vports[i], ndlp,
2701 NULL, NLP_EVT_DEVICE_RECOVERY);
2702 lpfc_disc_state_machine(vports[i], ndlp,
2703 NULL, NLP_EVT_DEVICE_RM);
2704 }
2705 spin_lock_irq(shost->host_lock);
2706 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart401ee0c2012-03-01 22:35:34 -05002707 spin_unlock_irq(shost->host_lock);
James Smart6b5151f2012-01-18 16:24:06 -05002708 /*
2709 * Whenever an SLI4 port goes offline, free the
James Smart401ee0c2012-03-01 22:35:34 -05002710 * RPI. Get a new RPI when the adapter port
2711 * comes back online.
James Smart6b5151f2012-01-18 16:24:06 -05002712 */
2713 if (phba->sli_rev == LPFC_SLI_REV4)
2714 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smart87af33f2007-10-27 13:37:43 -04002715 lpfc_unreg_rpi(vports[i], ndlp);
2716 }
2717 }
2718 }
James Smart09372822008-01-11 01:52:54 -05002719 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002720
James Smart04c68492009-05-22 14:52:52 -04002721 lpfc_sli_mbox_sys_shutdown(phba);
James Smart46fa3112007-04-25 09:51:45 -04002722}
2723
James Smarte59058c2008-08-24 21:49:00 -04002724/**
James Smart3621a712009-04-06 18:47:14 -04002725 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04002726 * @phba: pointer to lpfc hba data structure.
2727 *
2728 * This routine actually brings a HBA offline. It stops all the timers
2729 * associated with the HBA, brings down the SLI layer, and eventually
2730 * marks the HBA as in offline state for the upper layer protocol.
2731 **/
James Smart46fa3112007-04-25 09:51:45 -04002732void
James Smart2e0fef82007-06-17 19:56:36 -05002733lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04002734{
James Smart549e55c2007-08-02 11:09:51 -04002735 struct Scsi_Host *shost;
2736 struct lpfc_vport **vports;
2737 int i;
James Smart46fa3112007-04-25 09:51:45 -04002738
James Smart549e55c2007-08-02 11:09:51 -04002739 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002740 return;
James Smart688a8862006-07-06 15:49:56 -04002741
James Smartda0436e2009-05-22 14:51:39 -04002742 /* stop port and all timers associated with this hba */
2743 lpfc_stop_port(phba);
James Smart51ef4c22007-08-02 11:10:31 -04002744 vports = lpfc_create_vport_work_array(phba);
2745 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002746 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04002747 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05002748 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05002749 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002750 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05002751 /* Bring down the SLI Layer and cleanup. The HBA is offline
2752 now. */
2753 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002754 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04002755 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002756 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04002757 vports = lpfc_create_vport_work_array(phba);
2758 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002759 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002760 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04002761 spin_lock_irq(shost->host_lock);
2762 vports[i]->work_port_events = 0;
2763 vports[i]->fc_flag |= FC_OFFLINE_MODE;
2764 spin_unlock_irq(shost->host_lock);
2765 }
James Smart09372822008-01-11 01:52:54 -05002766 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002767}
2768
James Smarte59058c2008-08-24 21:49:00 -04002769/**
James Smart3621a712009-04-06 18:47:14 -04002770 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04002771 * @phba: pointer to lpfc hba data structure.
2772 *
2773 * This routine is to free all the SCSI buffers and IOCBs from the driver
2774 * list back to kernel. It is called from lpfc_pci_remove_one to free
2775 * the internal resources before the device is removed from the system.
James Smarte59058c2008-08-24 21:49:00 -04002776 **/
James Smart8a9d2e82012-05-09 21:16:12 -04002777static void
James Smart2e0fef82007-06-17 19:56:36 -05002778lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002779{
2780 struct lpfc_scsi_buf *sb, *sb_next;
2781 struct lpfc_iocbq *io, *io_next;
2782
James Smart2e0fef82007-06-17 19:56:36 -05002783 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002784 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smart1c6f4ef52009-11-18 15:40:49 -05002785 spin_lock(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05002786 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
2787 list_del(&sb->list);
2788 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05002789 sb->dma_handle);
dea31012005-04-17 16:05:31 -05002790 kfree(sb);
2791 phba->total_scsi_bufs--;
2792 }
James Smart1c6f4ef52009-11-18 15:40:49 -05002793 spin_unlock(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05002794
2795 /* Release all the lpfc_iocbq entries maintained by this host. */
2796 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2797 list_del(&io->list);
2798 kfree(io);
2799 phba->total_iocbq_bufs--;
2800 }
James Smart6d368e52011-05-24 11:44:12 -04002801
James Smart2e0fef82007-06-17 19:56:36 -05002802 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04002803}
2804
2805/**
2806 * lpfc_sli4_xri_sgl_update - update xri-sgl sizing and mapping
2807 * @phba: pointer to lpfc hba data structure.
2808 *
2809 * This routine first calculates the sizes of the current els and allocated
2810 * scsi sgl lists, and then goes through all sgls to updates the physical
2811 * XRIs assigned due to port function reset. During port initialization, the
2812 * current els and allocated scsi sgl lists are 0s.
2813 *
2814 * Return codes
2815 * 0 - successful (for now, it always returns 0)
2816 **/
2817int
2818lpfc_sli4_xri_sgl_update(struct lpfc_hba *phba)
2819{
2820 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
2821 struct lpfc_scsi_buf *psb = NULL, *psb_next = NULL;
2822 uint16_t i, lxri, xri_cnt, els_xri_cnt, scsi_xri_cnt;
2823 LIST_HEAD(els_sgl_list);
2824 LIST_HEAD(scsi_sgl_list);
2825 int rc;
2826
2827 /*
2828 * update on pci function's els xri-sgl list
2829 */
2830 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
2831 if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
2832 /* els xri-sgl expanded */
2833 xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
2834 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2835 "3157 ELS xri-sgl count increased from "
2836 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
2837 els_xri_cnt);
2838 /* allocate the additional els sgls */
2839 for (i = 0; i < xri_cnt; i++) {
2840 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
2841 GFP_KERNEL);
2842 if (sglq_entry == NULL) {
2843 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2844 "2562 Failure to allocate an "
2845 "ELS sgl entry:%d\n", i);
2846 rc = -ENOMEM;
2847 goto out_free_mem;
2848 }
2849 sglq_entry->buff_type = GEN_BUFF_TYPE;
2850 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
2851 &sglq_entry->phys);
2852 if (sglq_entry->virt == NULL) {
2853 kfree(sglq_entry);
2854 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2855 "2563 Failure to allocate an "
2856 "ELS mbuf:%d\n", i);
2857 rc = -ENOMEM;
2858 goto out_free_mem;
2859 }
2860 sglq_entry->sgl = sglq_entry->virt;
2861 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
2862 sglq_entry->state = SGL_FREED;
2863 list_add_tail(&sglq_entry->list, &els_sgl_list);
2864 }
2865 spin_lock(&phba->hbalock);
2866 list_splice_init(&els_sgl_list, &phba->sli4_hba.lpfc_sgl_list);
2867 spin_unlock(&phba->hbalock);
2868 } else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
2869 /* els xri-sgl shrinked */
2870 xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
2871 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2872 "3158 ELS xri-sgl count decreased from "
2873 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
2874 els_xri_cnt);
2875 spin_lock_irq(&phba->hbalock);
2876 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &els_sgl_list);
2877 spin_unlock_irq(&phba->hbalock);
2878 /* release extra els sgls from list */
2879 for (i = 0; i < xri_cnt; i++) {
2880 list_remove_head(&els_sgl_list,
2881 sglq_entry, struct lpfc_sglq, list);
2882 if (sglq_entry) {
2883 lpfc_mbuf_free(phba, sglq_entry->virt,
2884 sglq_entry->phys);
2885 kfree(sglq_entry);
2886 }
2887 }
2888 spin_lock_irq(&phba->hbalock);
2889 list_splice_init(&els_sgl_list, &phba->sli4_hba.lpfc_sgl_list);
2890 spin_unlock_irq(&phba->hbalock);
2891 } else
2892 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2893 "3163 ELS xri-sgl count unchanged: %d\n",
2894 els_xri_cnt);
2895 phba->sli4_hba.els_xri_cnt = els_xri_cnt;
2896
2897 /* update xris to els sgls on the list */
2898 sglq_entry = NULL;
2899 sglq_entry_next = NULL;
2900 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
2901 &phba->sli4_hba.lpfc_sgl_list, list) {
2902 lxri = lpfc_sli4_next_xritag(phba);
2903 if (lxri == NO_XRI) {
2904 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2905 "2400 Failed to allocate xri for "
2906 "ELS sgl\n");
2907 rc = -ENOMEM;
2908 goto out_free_mem;
2909 }
2910 sglq_entry->sli4_lxritag = lxri;
2911 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
2912 }
2913
2914 /*
2915 * update on pci function's allocated scsi xri-sgl list
2916 */
2917 phba->total_scsi_bufs = 0;
2918
2919 /* maximum number of xris available for scsi buffers */
2920 phba->sli4_hba.scsi_xri_max = phba->sli4_hba.max_cfg_param.max_xri -
2921 els_xri_cnt;
2922
2923 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2924 "2401 Current allocated SCSI xri-sgl count:%d, "
2925 "maximum SCSI xri count:%d\n",
2926 phba->sli4_hba.scsi_xri_cnt,
2927 phba->sli4_hba.scsi_xri_max);
2928
2929 spin_lock_irq(&phba->scsi_buf_list_lock);
2930 list_splice_init(&phba->lpfc_scsi_buf_list, &scsi_sgl_list);
2931 spin_unlock_irq(&phba->scsi_buf_list_lock);
2932
2933 if (phba->sli4_hba.scsi_xri_cnt > phba->sli4_hba.scsi_xri_max) {
2934 /* max scsi xri shrinked below the allocated scsi buffers */
2935 scsi_xri_cnt = phba->sli4_hba.scsi_xri_cnt -
2936 phba->sli4_hba.scsi_xri_max;
2937 /* release the extra allocated scsi buffers */
2938 for (i = 0; i < scsi_xri_cnt; i++) {
2939 list_remove_head(&scsi_sgl_list, psb,
2940 struct lpfc_scsi_buf, list);
2941 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, psb->data,
2942 psb->dma_handle);
2943 kfree(psb);
2944 }
2945 spin_lock_irq(&phba->scsi_buf_list_lock);
2946 phba->sli4_hba.scsi_xri_cnt -= scsi_xri_cnt;
2947 spin_unlock_irq(&phba->scsi_buf_list_lock);
2948 }
2949
2950 /* update xris associated to remaining allocated scsi buffers */
2951 psb = NULL;
2952 psb_next = NULL;
2953 list_for_each_entry_safe(psb, psb_next, &scsi_sgl_list, list) {
2954 lxri = lpfc_sli4_next_xritag(phba);
2955 if (lxri == NO_XRI) {
2956 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2957 "2560 Failed to allocate xri for "
2958 "scsi buffer\n");
2959 rc = -ENOMEM;
2960 goto out_free_mem;
2961 }
2962 psb->cur_iocbq.sli4_lxritag = lxri;
2963 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
2964 }
2965 spin_lock(&phba->scsi_buf_list_lock);
2966 list_splice_init(&scsi_sgl_list, &phba->lpfc_scsi_buf_list);
2967 spin_unlock(&phba->scsi_buf_list_lock);
2968
dea31012005-04-17 16:05:31 -05002969 return 0;
James Smart8a9d2e82012-05-09 21:16:12 -04002970
2971out_free_mem:
2972 lpfc_free_els_sgl_list(phba);
2973 lpfc_scsi_free(phba);
2974 return rc;
dea31012005-04-17 16:05:31 -05002975}
2976
James Smarte59058c2008-08-24 21:49:00 -04002977/**
James Smart3621a712009-04-06 18:47:14 -04002978 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04002979 * @phba: pointer to lpfc hba data structure.
2980 * @instance: a unique integer ID to this FC port.
2981 * @dev: pointer to the device data structure.
2982 *
2983 * This routine creates a FC port for the upper layer protocol. The FC port
2984 * can be created on top of either a physical port or a virtual port provided
2985 * by the HBA. This routine also allocates a SCSI host data structure (shost)
2986 * and associates the FC port created before adding the shost into the SCSI
2987 * layer.
2988 *
2989 * Return codes
2990 * @vport - pointer to the virtual N_Port data structure.
2991 * NULL - port create failed.
2992 **/
James Smart2e0fef82007-06-17 19:56:36 -05002993struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04002994lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04002995{
James Smart2e0fef82007-06-17 19:56:36 -05002996 struct lpfc_vport *vport;
2997 struct Scsi_Host *shost;
2998 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04002999
James Smart3de2a652007-08-02 11:09:59 -04003000 if (dev != &phba->pcidev->dev)
3001 shost = scsi_host_alloc(&lpfc_vport_template,
3002 sizeof(struct lpfc_vport));
3003 else
3004 shost = scsi_host_alloc(&lpfc_template,
3005 sizeof(struct lpfc_vport));
James Smart2e0fef82007-06-17 19:56:36 -05003006 if (!shost)
3007 goto out;
James Smart47a86172007-04-25 09:53:22 -04003008
James Smart2e0fef82007-06-17 19:56:36 -05003009 vport = (struct lpfc_vport *) shost->hostdata;
3010 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05003011 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05003012 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05003013 vport->fc_rscn_flush = 0;
James Smart47a86172007-04-25 09:53:22 -04003014
James Smart3de2a652007-08-02 11:09:59 -04003015 lpfc_get_vport_cfgparam(vport);
James Smart2e0fef82007-06-17 19:56:36 -05003016 shost->unique_id = instance;
3017 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04003018 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05003019 shost->this_id = -1;
3020 shost->max_cmd_len = 16;
James Smartda0436e2009-05-22 14:51:39 -04003021 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart28baac72010-02-12 14:42:03 -05003022 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04003023 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smartda0436e2009-05-22 14:51:39 -04003024 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
3025 }
James Smart81301a92008-12-04 22:39:46 -05003026
James Smart47a86172007-04-25 09:53:22 -04003027 /*
James Smart2e0fef82007-06-17 19:56:36 -05003028 * Set initial can_queue value since 0 is no longer supported and
3029 * scsi_add_host will fail. This will be adjusted later based on the
3030 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04003031 */
James Smart2e0fef82007-06-17 19:56:36 -05003032 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04003033 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05003034 shost->transportt = lpfc_vport_transport_template;
3035 vport->port_type = LPFC_NPIV_PORT;
3036 } else {
3037 shost->transportt = lpfc_transport_template;
3038 vport->port_type = LPFC_PHYSICAL_PORT;
3039 }
James Smart47a86172007-04-25 09:53:22 -04003040
James Smart2e0fef82007-06-17 19:56:36 -05003041 /* Initialize all internally managed lists. */
3042 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04003043 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05003044 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04003045
James Smart2e0fef82007-06-17 19:56:36 -05003046 init_timer(&vport->fc_disctmo);
3047 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05003048 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04003049
James Smart2e0fef82007-06-17 19:56:36 -05003050 init_timer(&vport->fc_fdmitmo);
3051 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
James Smart92d7f7b2007-06-17 19:56:38 -05003052 vport->fc_fdmitmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04003053
James Smart2e0fef82007-06-17 19:56:36 -05003054 init_timer(&vport->els_tmofunc);
3055 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05003056 vport->els_tmofunc.data = (unsigned long)vport;
James Smart92494142011-02-16 12:39:44 -05003057
3058 init_timer(&vport->delayed_disc_tmo);
3059 vport->delayed_disc_tmo.function = lpfc_delayed_disc_tmo;
3060 vport->delayed_disc_tmo.data = (unsigned long)vport;
3061
James Bottomleyd139b9b2009-11-05 13:33:12 -06003062 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05003063 if (error)
3064 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04003065
James Smart549e55c2007-08-02 11:09:51 -04003066 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003067 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04003068 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003069 return vport;
James Smart47a86172007-04-25 09:53:22 -04003070
James Smart2e0fef82007-06-17 19:56:36 -05003071out_put_shost:
3072 scsi_host_put(shost);
3073out:
3074 return NULL;
James Smart47a86172007-04-25 09:53:22 -04003075}
3076
James Smarte59058c2008-08-24 21:49:00 -04003077/**
James Smart3621a712009-04-06 18:47:14 -04003078 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04003079 * @vport: pointer to an lpfc virtual N_Port data structure.
3080 *
3081 * This routine destroys a FC port from the upper layer protocol. All the
3082 * resources associated with the port are released.
3083 **/
James Smart2e0fef82007-06-17 19:56:36 -05003084void
3085destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04003086{
James Smart92d7f7b2007-06-17 19:56:38 -05003087 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3088 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003089
James Smart858c9f62007-06-17 19:56:39 -05003090 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05003091 fc_remove_host(shost);
3092 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04003093
James Smart92d7f7b2007-06-17 19:56:38 -05003094 spin_lock_irq(&phba->hbalock);
3095 list_del_init(&vport->listentry);
3096 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04003097
James Smart92d7f7b2007-06-17 19:56:38 -05003098 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04003099 return;
James Smart47a86172007-04-25 09:53:22 -04003100}
3101
James Smarte59058c2008-08-24 21:49:00 -04003102/**
James Smart3621a712009-04-06 18:47:14 -04003103 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04003104 *
3105 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
3106 * uses the kernel idr facility to perform the task.
3107 *
3108 * Return codes:
3109 * instance - a unique integer ID allocated as the new instance.
3110 * -1 - lpfc get instance failed.
3111 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003112int
3113lpfc_get_instance(void)
3114{
3115 int instance = 0;
3116
3117 /* Assign an unused number */
3118 if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
3119 return -1;
3120 if (idr_get_new(&lpfc_hba_index, NULL, &instance))
3121 return -1;
3122 return instance;
3123}
3124
James Smarte59058c2008-08-24 21:49:00 -04003125/**
James Smart3621a712009-04-06 18:47:14 -04003126 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04003127 * @shost: pointer to SCSI host data structure.
3128 * @time: elapsed time of the scan in jiffies.
3129 *
3130 * This routine is called by the SCSI layer with a SCSI host to determine
3131 * whether the scan host is finished.
3132 *
3133 * Note: there is no scan_start function as adapter initialization will have
3134 * asynchronously kicked off the link initialization.
3135 *
3136 * Return codes
3137 * 0 - SCSI host scan is not over yet.
3138 * 1 - SCSI host scan is over.
3139 **/
James Smart47a86172007-04-25 09:53:22 -04003140int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
3141{
James Smart2e0fef82007-06-17 19:56:36 -05003142 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3143 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05003144 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04003145
James Smart858c9f62007-06-17 19:56:39 -05003146 spin_lock_irq(shost->host_lock);
3147
James Smart51ef4c22007-08-02 11:10:31 -04003148 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05003149 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003150 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05003151 }
James Smart2e0fef82007-06-17 19:56:36 -05003152 if (time >= 30 * HZ) {
3153 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003154 "0461 Scanning longer than 30 "
3155 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003156 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003157 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003158 }
3159 if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
3160 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003161 "0465 Link down longer than 15 "
3162 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003163 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05003164 goto finished;
James Smart47a86172007-04-25 09:53:22 -04003165 }
3166
James Smart2e0fef82007-06-17 19:56:36 -05003167 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05003168 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003169 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05003170 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003171 if (vport->fc_map_cnt == 0 && time < 2 * HZ)
James Smart858c9f62007-06-17 19:56:39 -05003172 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003173 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05003174 goto finished;
3175
3176 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003177
3178finished:
James Smart858c9f62007-06-17 19:56:39 -05003179 spin_unlock_irq(shost->host_lock);
3180 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05003181}
3182
James Smarte59058c2008-08-24 21:49:00 -04003183/**
James Smart3621a712009-04-06 18:47:14 -04003184 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04003185 * @shost: pointer to SCSI host data structure.
3186 *
3187 * This routine initializes a given SCSI host attributes on a FC port. The
3188 * SCSI host can be either on top of a physical port or a virtual port.
3189 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003190void lpfc_host_attrib_init(struct Scsi_Host *shost)
3191{
3192 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3193 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003194 /*
James Smart2e0fef82007-06-17 19:56:36 -05003195 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04003196 */
3197
James Smart2e0fef82007-06-17 19:56:36 -05003198 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
3199 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04003200 fc_host_supported_classes(shost) = FC_COS_CLASS3;
3201
3202 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003203 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003204 fc_host_supported_fc4s(shost)[2] = 1;
3205 fc_host_supported_fc4s(shost)[7] = 1;
3206
James Smart92d7f7b2007-06-17 19:56:38 -05003207 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
3208 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04003209
3210 fc_host_supported_speeds(shost) = 0;
James Smart88a2cfb2011-07-22 18:36:33 -04003211 if (phba->lmt & LMT_16Gb)
3212 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
James Smart47a86172007-04-25 09:53:22 -04003213 if (phba->lmt & LMT_10Gb)
3214 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04003215 if (phba->lmt & LMT_8Gb)
3216 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04003217 if (phba->lmt & LMT_4Gb)
3218 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
3219 if (phba->lmt & LMT_2Gb)
3220 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
3221 if (phba->lmt & LMT_1Gb)
3222 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
3223
3224 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05003225 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
3226 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04003227
Mike Christie0af5d702010-09-15 16:52:31 -05003228 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
3229
James Smart47a86172007-04-25 09:53:22 -04003230 /* This value is also unchanging */
3231 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003232 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003233 fc_host_active_fc4s(shost)[2] = 1;
3234 fc_host_active_fc4s(shost)[7] = 1;
3235
James Smart92d7f7b2007-06-17 19:56:38 -05003236 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04003237 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04003238 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04003239 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04003240}
dea31012005-04-17 16:05:31 -05003241
James Smarte59058c2008-08-24 21:49:00 -04003242/**
James Smartda0436e2009-05-22 14:51:39 -04003243 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04003244 * @phba: pointer to lpfc hba data structure.
3245 *
James Smartda0436e2009-05-22 14:51:39 -04003246 * This routine is invoked to stop an SLI3 device port, it stops the device
3247 * from generating interrupts and stops the device driver's timers for the
3248 * device.
James Smarte59058c2008-08-24 21:49:00 -04003249 **/
James Smartdb2378e2008-02-08 18:49:51 -05003250static void
James Smartda0436e2009-05-22 14:51:39 -04003251lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05003252{
James Smartda0436e2009-05-22 14:51:39 -04003253 /* Clear all interrupt enable conditions */
3254 writel(0, phba->HCregaddr);
3255 readl(phba->HCregaddr); /* flush */
3256 /* Clear all pending interrupts */
3257 writel(0xffffffff, phba->HAregaddr);
3258 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04003259
James Smartda0436e2009-05-22 14:51:39 -04003260 /* Reset some HBA SLI setup states */
3261 lpfc_stop_hba_timers(phba);
3262 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05003263}
3264
James Smarte59058c2008-08-24 21:49:00 -04003265/**
James Smartda0436e2009-05-22 14:51:39 -04003266 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05003267 * @phba: pointer to lpfc hba data structure.
3268 *
James Smartda0436e2009-05-22 14:51:39 -04003269 * This routine is invoked to stop an SLI4 device port, it stops the device
3270 * from generating interrupts and stops the device driver's timers for the
3271 * device.
3272 **/
3273static void
3274lpfc_stop_port_s4(struct lpfc_hba *phba)
3275{
3276 /* Reset some HBA SLI4 setup states */
3277 lpfc_stop_hba_timers(phba);
3278 phba->pport->work_port_events = 0;
3279 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04003280}
3281
3282/**
3283 * lpfc_stop_port - Wrapper function for stopping hba port
3284 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05003285 *
James Smartda0436e2009-05-22 14:51:39 -04003286 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
3287 * the API jump table function pointer from the lpfc_hba struct.
3288 **/
3289void
3290lpfc_stop_port(struct lpfc_hba *phba)
3291{
3292 phba->lpfc_stop_port(phba);
3293}
3294
3295/**
James Smartecfd03c2010-02-12 14:41:27 -05003296 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
3297 * @phba: Pointer to hba for which this call is being executed.
3298 *
3299 * This routine starts the timer waiting for the FCF rediscovery to complete.
3300 **/
3301void
3302lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
3303{
3304 unsigned long fcf_redisc_wait_tmo =
3305 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
3306 /* Start fcf rediscovery wait period timer */
3307 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
3308 spin_lock_irq(&phba->hbalock);
3309 /* Allow action to new fcf asynchronous event */
3310 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
3311 /* Mark the FCF rediscovery pending state */
3312 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
3313 spin_unlock_irq(&phba->hbalock);
3314}
3315
3316/**
3317 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
3318 * @ptr: Map to lpfc_hba data structure pointer.
3319 *
3320 * This routine is invoked when waiting for FCF table rediscover has been
3321 * timed out. If new FCF record(s) has (have) been discovered during the
3322 * wait period, a new FCF event shall be added to the FCOE async event
3323 * list, and then worker thread shall be waked up for processing from the
3324 * worker thread context.
3325 **/
3326void
3327lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
3328{
3329 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
3330
3331 /* Don't send FCF rediscovery event if timer cancelled */
3332 spin_lock_irq(&phba->hbalock);
3333 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
3334 spin_unlock_irq(&phba->hbalock);
3335 return;
3336 }
3337 /* Clear FCF rediscovery timer pending flag */
3338 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
3339 /* FCF rediscovery event to worker thread */
3340 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
3341 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003342 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04003343 "2776 FCF rediscover quiescent timer expired\n");
James Smartecfd03c2010-02-12 14:41:27 -05003344 /* wake up worker thread */
3345 lpfc_worker_wake_up(phba);
3346}
3347
3348/**
James Smartda0436e2009-05-22 14:51:39 -04003349 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
3350 * @phba: pointer to lpfc hba data structure.
3351 * @acqe_link: pointer to the async link completion queue entry.
3352 *
3353 * This routine is to parse the SLI4 link-attention link fault code and
3354 * translate it into the base driver's read link attention mailbox command
3355 * status.
3356 *
3357 * Return: Link-attention status in terms of base driver's coding.
3358 **/
3359static uint16_t
3360lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
3361 struct lpfc_acqe_link *acqe_link)
3362{
3363 uint16_t latt_fault;
3364
3365 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
3366 case LPFC_ASYNC_LINK_FAULT_NONE:
3367 case LPFC_ASYNC_LINK_FAULT_LOCAL:
3368 case LPFC_ASYNC_LINK_FAULT_REMOTE:
3369 latt_fault = 0;
3370 break;
3371 default:
3372 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3373 "0398 Invalid link fault code: x%x\n",
3374 bf_get(lpfc_acqe_link_fault, acqe_link));
3375 latt_fault = MBXERR_ERROR;
3376 break;
3377 }
3378 return latt_fault;
3379}
3380
3381/**
3382 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
3383 * @phba: pointer to lpfc hba data structure.
3384 * @acqe_link: pointer to the async link completion queue entry.
3385 *
3386 * This routine is to parse the SLI4 link attention type and translate it
3387 * into the base driver's link attention type coding.
3388 *
3389 * Return: Link attention type in terms of base driver's coding.
3390 **/
3391static uint8_t
3392lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
3393 struct lpfc_acqe_link *acqe_link)
3394{
3395 uint8_t att_type;
3396
3397 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
3398 case LPFC_ASYNC_LINK_STATUS_DOWN:
3399 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
James Smart76a95d72010-11-20 23:11:48 -05003400 att_type = LPFC_ATT_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04003401 break;
3402 case LPFC_ASYNC_LINK_STATUS_UP:
3403 /* Ignore physical link up events - wait for logical link up */
James Smart76a95d72010-11-20 23:11:48 -05003404 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003405 break;
3406 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
James Smart76a95d72010-11-20 23:11:48 -05003407 att_type = LPFC_ATT_LINK_UP;
James Smartda0436e2009-05-22 14:51:39 -04003408 break;
3409 default:
3410 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3411 "0399 Invalid link attention type: x%x\n",
3412 bf_get(lpfc_acqe_link_status, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003413 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003414 break;
3415 }
3416 return att_type;
3417}
3418
3419/**
3420 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
3421 * @phba: pointer to lpfc hba data structure.
3422 * @acqe_link: pointer to the async link completion queue entry.
3423 *
3424 * This routine is to parse the SLI4 link-attention link speed and translate
3425 * it into the base driver's link-attention link speed coding.
3426 *
3427 * Return: Link-attention link speed in terms of base driver's coding.
3428 **/
3429static uint8_t
3430lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
3431 struct lpfc_acqe_link *acqe_link)
3432{
3433 uint8_t link_speed;
3434
3435 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
3436 case LPFC_ASYNC_LINK_SPEED_ZERO:
James Smartda0436e2009-05-22 14:51:39 -04003437 case LPFC_ASYNC_LINK_SPEED_10MBPS:
James Smartda0436e2009-05-22 14:51:39 -04003438 case LPFC_ASYNC_LINK_SPEED_100MBPS:
James Smart76a95d72010-11-20 23:11:48 -05003439 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003440 break;
3441 case LPFC_ASYNC_LINK_SPEED_1GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003442 link_speed = LPFC_LINK_SPEED_1GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003443 break;
3444 case LPFC_ASYNC_LINK_SPEED_10GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003445 link_speed = LPFC_LINK_SPEED_10GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003446 break;
3447 default:
3448 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3449 "0483 Invalid link-attention link speed: x%x\n",
3450 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003451 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003452 break;
3453 }
3454 return link_speed;
3455}
3456
3457/**
James Smart70f3c072010-12-15 17:57:33 -05003458 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
James Smartda0436e2009-05-22 14:51:39 -04003459 * @phba: pointer to lpfc hba data structure.
3460 * @acqe_link: pointer to the async link completion queue entry.
3461 *
James Smart70f3c072010-12-15 17:57:33 -05003462 * This routine is to handle the SLI4 asynchronous FCoE link event.
James Smartda0436e2009-05-22 14:51:39 -04003463 **/
3464static void
3465lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3466 struct lpfc_acqe_link *acqe_link)
3467{
3468 struct lpfc_dmabuf *mp;
3469 LPFC_MBOXQ_t *pmb;
3470 MAILBOX_t *mb;
James Smart76a95d72010-11-20 23:11:48 -05003471 struct lpfc_mbx_read_top *la;
James Smartda0436e2009-05-22 14:51:39 -04003472 uint8_t att_type;
James Smart76a95d72010-11-20 23:11:48 -05003473 int rc;
James Smartda0436e2009-05-22 14:51:39 -04003474
3475 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05003476 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
James Smartda0436e2009-05-22 14:51:39 -04003477 return;
James Smart32b97932009-07-19 10:01:21 -04003478 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003479 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3480 if (!pmb) {
3481 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3482 "0395 The mboxq allocation failed\n");
3483 return;
3484 }
3485 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3486 if (!mp) {
3487 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3488 "0396 The lpfc_dmabuf allocation failed\n");
3489 goto out_free_pmb;
3490 }
3491 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3492 if (!mp->virt) {
3493 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3494 "0397 The mbuf allocation failed\n");
3495 goto out_free_dmabuf;
3496 }
3497
3498 /* Cleanup any outstanding ELS commands */
3499 lpfc_els_flush_all_cmd(phba);
3500
3501 /* Block ELS IOCBs until we have done process link event */
3502 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3503
3504 /* Update link event statistics */
3505 phba->sli.slistat.link_event++;
3506
James Smart76a95d72010-11-20 23:11:48 -05003507 /* Create lpfc_handle_latt mailbox command from link ACQE */
3508 lpfc_read_topology(phba, pmb, mp);
3509 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smartda0436e2009-05-22 14:51:39 -04003510 pmb->vport = phba->pport;
3511
James Smartda0436e2009-05-22 14:51:39 -04003512 /* Keep the link status for extra SLI4 state machine reference */
3513 phba->sli4_hba.link_state.speed =
3514 bf_get(lpfc_acqe_link_speed, acqe_link);
3515 phba->sli4_hba.link_state.duplex =
3516 bf_get(lpfc_acqe_link_duplex, acqe_link);
3517 phba->sli4_hba.link_state.status =
3518 bf_get(lpfc_acqe_link_status, acqe_link);
James Smart70f3c072010-12-15 17:57:33 -05003519 phba->sli4_hba.link_state.type =
3520 bf_get(lpfc_acqe_link_type, acqe_link);
3521 phba->sli4_hba.link_state.number =
3522 bf_get(lpfc_acqe_link_number, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04003523 phba->sli4_hba.link_state.fault =
3524 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05003525 phba->sli4_hba.link_state.logical_speed =
James Smart70f3c072010-12-15 17:57:33 -05003526 bf_get(lpfc_acqe_logical_link_speed, acqe_link);
3527 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartc31098c2011-04-16 11:03:33 -04003528 "2900 Async FC/FCoE Link event - Speed:%dGBit "
3529 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
3530 "Logical speed:%dMbps Fault:%d\n",
James Smart70f3c072010-12-15 17:57:33 -05003531 phba->sli4_hba.link_state.speed,
3532 phba->sli4_hba.link_state.topology,
3533 phba->sli4_hba.link_state.status,
3534 phba->sli4_hba.link_state.type,
3535 phba->sli4_hba.link_state.number,
3536 phba->sli4_hba.link_state.logical_speed * 10,
3537 phba->sli4_hba.link_state.fault);
James Smart76a95d72010-11-20 23:11:48 -05003538 /*
3539 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
3540 * topology info. Note: Optional for non FC-AL ports.
3541 */
3542 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3543 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3544 if (rc == MBX_NOT_FINISHED)
3545 goto out_free_dmabuf;
3546 return;
3547 }
3548 /*
3549 * For FCoE Mode: fill in all the topology information we need and call
3550 * the READ_TOPOLOGY completion routine to continue without actually
3551 * sending the READ_TOPOLOGY mailbox command to the port.
3552 */
3553 /* Parse and translate status field */
3554 mb = &pmb->u.mb;
3555 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
3556
3557 /* Parse and translate link attention fields */
3558 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
3559 la->eventTag = acqe_link->event_tag;
3560 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
3561 bf_set(lpfc_mbx_read_top_link_spd, la,
3562 lpfc_sli4_parse_latt_link_speed(phba, acqe_link));
3563
3564 /* Fake the the following irrelvant fields */
3565 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
3566 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
3567 bf_set(lpfc_mbx_read_top_il, la, 0);
3568 bf_set(lpfc_mbx_read_top_pb, la, 0);
3569 bf_set(lpfc_mbx_read_top_fa, la, 0);
3570 bf_set(lpfc_mbx_read_top_mm, la, 0);
James Smartda0436e2009-05-22 14:51:39 -04003571
3572 /* Invoke the lpfc_handle_latt mailbox command callback function */
James Smart76a95d72010-11-20 23:11:48 -05003573 lpfc_mbx_cmpl_read_topology(phba, pmb);
James Smartda0436e2009-05-22 14:51:39 -04003574
3575 return;
3576
3577out_free_dmabuf:
3578 kfree(mp);
3579out_free_pmb:
3580 mempool_free(pmb, phba->mbox_mem_pool);
3581}
3582
3583/**
James Smart70f3c072010-12-15 17:57:33 -05003584 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
3585 * @phba: pointer to lpfc hba data structure.
3586 * @acqe_fc: pointer to the async fc completion queue entry.
3587 *
3588 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
3589 * that the event was received and then issue a read_topology mailbox command so
3590 * that the rest of the driver will treat it the same as SLI3.
3591 **/
3592static void
3593lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
3594{
3595 struct lpfc_dmabuf *mp;
3596 LPFC_MBOXQ_t *pmb;
3597 int rc;
3598
3599 if (bf_get(lpfc_trailer_type, acqe_fc) !=
3600 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
3601 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3602 "2895 Non FC link Event detected.(%d)\n",
3603 bf_get(lpfc_trailer_type, acqe_fc));
3604 return;
3605 }
3606 /* Keep the link status for extra SLI4 state machine reference */
3607 phba->sli4_hba.link_state.speed =
3608 bf_get(lpfc_acqe_fc_la_speed, acqe_fc);
3609 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
3610 phba->sli4_hba.link_state.topology =
3611 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
3612 phba->sli4_hba.link_state.status =
3613 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
3614 phba->sli4_hba.link_state.type =
3615 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
3616 phba->sli4_hba.link_state.number =
3617 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
3618 phba->sli4_hba.link_state.fault =
3619 bf_get(lpfc_acqe_link_fault, acqe_fc);
3620 phba->sli4_hba.link_state.logical_speed =
3621 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc);
3622 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3623 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
3624 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
3625 "%dMbps Fault:%d\n",
3626 phba->sli4_hba.link_state.speed,
3627 phba->sli4_hba.link_state.topology,
3628 phba->sli4_hba.link_state.status,
3629 phba->sli4_hba.link_state.type,
3630 phba->sli4_hba.link_state.number,
3631 phba->sli4_hba.link_state.logical_speed * 10,
3632 phba->sli4_hba.link_state.fault);
3633 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3634 if (!pmb) {
3635 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3636 "2897 The mboxq allocation failed\n");
3637 return;
3638 }
3639 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3640 if (!mp) {
3641 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3642 "2898 The lpfc_dmabuf allocation failed\n");
3643 goto out_free_pmb;
3644 }
3645 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3646 if (!mp->virt) {
3647 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3648 "2899 The mbuf allocation failed\n");
3649 goto out_free_dmabuf;
3650 }
3651
3652 /* Cleanup any outstanding ELS commands */
3653 lpfc_els_flush_all_cmd(phba);
3654
3655 /* Block ELS IOCBs until we have done process link event */
3656 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3657
3658 /* Update link event statistics */
3659 phba->sli.slistat.link_event++;
3660
3661 /* Create lpfc_handle_latt mailbox command from link ACQE */
3662 lpfc_read_topology(phba, pmb, mp);
3663 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
3664 pmb->vport = phba->pport;
3665
3666 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3667 if (rc == MBX_NOT_FINISHED)
3668 goto out_free_dmabuf;
3669 return;
3670
3671out_free_dmabuf:
3672 kfree(mp);
3673out_free_pmb:
3674 mempool_free(pmb, phba->mbox_mem_pool);
3675}
3676
3677/**
3678 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
3679 * @phba: pointer to lpfc hba data structure.
3680 * @acqe_fc: pointer to the async SLI completion queue entry.
3681 *
3682 * This routine is to handle the SLI4 asynchronous SLI events.
3683 **/
3684static void
3685lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
3686{
3687 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3688 "2901 Async SLI event - Event Data1:x%08x Event Data2:"
3689 "x%08x SLI Event Type:%d",
3690 acqe_sli->event_data1, acqe_sli->event_data2,
3691 bf_get(lpfc_trailer_type, acqe_sli));
3692 return;
3693}
3694
3695/**
James Smartfc2b9892010-02-26 14:15:29 -05003696 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
3697 * @vport: pointer to vport data structure.
3698 *
3699 * This routine is to perform Clear Virtual Link (CVL) on a vport in
3700 * response to a CVL event.
3701 *
3702 * Return the pointer to the ndlp with the vport if successful, otherwise
3703 * return NULL.
3704 **/
3705static struct lpfc_nodelist *
3706lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
3707{
3708 struct lpfc_nodelist *ndlp;
3709 struct Scsi_Host *shost;
3710 struct lpfc_hba *phba;
3711
3712 if (!vport)
3713 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05003714 phba = vport->phba;
3715 if (!phba)
3716 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04003717 ndlp = lpfc_findnode_did(vport, Fabric_DID);
3718 if (!ndlp) {
3719 /* Cannot find existing Fabric ndlp, so allocate a new one */
3720 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
3721 if (!ndlp)
3722 return 0;
3723 lpfc_nlp_init(vport, ndlp, Fabric_DID);
3724 /* Set the node type */
3725 ndlp->nlp_type |= NLP_FABRIC;
3726 /* Put ndlp onto node list */
3727 lpfc_enqueue_node(vport, ndlp);
3728 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
3729 /* re-setup ndlp without removing from node list */
3730 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
3731 if (!ndlp)
3732 return 0;
3733 }
James Smart63e801c2010-11-20 23:14:19 -05003734 if ((phba->pport->port_state < LPFC_FLOGI) &&
3735 (phba->pport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05003736 return NULL;
3737 /* If virtual link is not yet instantiated ignore CVL */
James Smart63e801c2010-11-20 23:14:19 -05003738 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
3739 && (vport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05003740 return NULL;
3741 shost = lpfc_shost_from_vport(vport);
3742 if (!shost)
3743 return NULL;
3744 lpfc_linkdown_port(vport);
3745 lpfc_cleanup_pending_mbox(vport);
3746 spin_lock_irq(shost->host_lock);
3747 vport->fc_flag |= FC_VPORT_CVL_RCVD;
3748 spin_unlock_irq(shost->host_lock);
3749
3750 return ndlp;
3751}
3752
3753/**
3754 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
3755 * @vport: pointer to lpfc hba data structure.
3756 *
3757 * This routine is to perform Clear Virtual Link (CVL) on all vports in
3758 * response to a FCF dead event.
3759 **/
3760static void
3761lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
3762{
3763 struct lpfc_vport **vports;
3764 int i;
3765
3766 vports = lpfc_create_vport_work_array(phba);
3767 if (vports)
3768 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
3769 lpfc_sli4_perform_vport_cvl(vports[i]);
3770 lpfc_destroy_vport_work_array(phba, vports);
3771}
3772
3773/**
James Smart76a95d72010-11-20 23:11:48 -05003774 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
James Smartda0436e2009-05-22 14:51:39 -04003775 * @phba: pointer to lpfc hba data structure.
3776 * @acqe_link: pointer to the async fcoe completion queue entry.
3777 *
3778 * This routine is to handle the SLI4 asynchronous fcoe event.
3779 **/
3780static void
James Smart76a95d72010-11-20 23:11:48 -05003781lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
James Smart70f3c072010-12-15 17:57:33 -05003782 struct lpfc_acqe_fip *acqe_fip)
James Smartda0436e2009-05-22 14:51:39 -04003783{
James Smart70f3c072010-12-15 17:57:33 -05003784 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04003785 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04003786 struct lpfc_vport *vport;
3787 struct lpfc_nodelist *ndlp;
3788 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05003789 int active_vlink_present;
3790 struct lpfc_vport **vports;
3791 int i;
James Smartda0436e2009-05-22 14:51:39 -04003792
James Smart70f3c072010-12-15 17:57:33 -05003793 phba->fc_eventTag = acqe_fip->event_tag;
3794 phba->fcoe_eventtag = acqe_fip->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003795 switch (event_type) {
James Smart70f3c072010-12-15 17:57:33 -05003796 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
3797 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
3798 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
James Smart999d8132010-03-15 11:24:56 -04003799 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3800 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003801 "2546 New FCF event, evt_tag:x%x, "
3802 "index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05003803 acqe_fip->event_tag,
3804 acqe_fip->index);
James Smart999d8132010-03-15 11:24:56 -04003805 else
3806 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
3807 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003808 "2788 FCF param modified event, "
3809 "evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05003810 acqe_fip->event_tag,
3811 acqe_fip->index);
James Smart38b92ef2010-08-04 16:11:39 -04003812 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smart0c9ab6f2010-02-26 14:15:57 -05003813 /*
3814 * During period of FCF discovery, read the FCF
3815 * table record indexed by the event to update
James Smarta93ff372010-10-22 11:06:08 -04003816 * FCF roundrobin failover eligible FCF bmask.
James Smart0c9ab6f2010-02-26 14:15:57 -05003817 */
3818 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
3819 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003820 "2779 Read FCF (x%x) for updating "
3821 "roundrobin FCF failover bmask\n",
James Smart70f3c072010-12-15 17:57:33 -05003822 acqe_fip->index);
3823 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05003824 }
James Smart38b92ef2010-08-04 16:11:39 -04003825
3826 /* If the FCF discovery is in progress, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04003827 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04003828 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart38b92ef2010-08-04 16:11:39 -04003829 spin_unlock_irq(&phba->hbalock);
3830 break;
3831 }
3832 /* If fast FCF failover rescan event is pending, do nothing */
3833 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
3834 spin_unlock_irq(&phba->hbalock);
3835 break;
3836 }
3837
3838 /* If the FCF has been in discovered state, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04003839 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
3840 spin_unlock_irq(&phba->hbalock);
3841 break;
3842 }
3843 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04003844
James Smart0c9ab6f2010-02-26 14:15:57 -05003845 /* Otherwise, scan the entire FCF table and re-discover SAN */
3846 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003847 "2770 Start FCF table scan per async FCF "
3848 "event, evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05003849 acqe_fip->event_tag, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05003850 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3851 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04003852 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05003853 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3854 "2547 Issue FCF scan read FCF mailbox "
James Smarta93ff372010-10-22 11:06:08 -04003855 "command failed (x%x)\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04003856 break;
3857
James Smart70f3c072010-12-15 17:57:33 -05003858 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
James Smartda0436e2009-05-22 14:51:39 -04003859 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04003860 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05003861 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
3862 acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04003863 break;
3864
James Smart70f3c072010-12-15 17:57:33 -05003865 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
James Smart80c17842012-03-01 22:35:45 -05003866 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05003867 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003868 "2549 FCF (x%x) disconnected from network, "
James Smart70f3c072010-12-15 17:57:33 -05003869 "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart38b92ef2010-08-04 16:11:39 -04003870 /*
3871 * If we are in the middle of FCF failover process, clear
3872 * the corresponding FCF bit in the roundrobin bitmap.
James Smartda0436e2009-05-22 14:51:39 -04003873 */
James Smartfc2b9892010-02-26 14:15:29 -05003874 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003875 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05003876 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003877 /* Update FLOGI FCF failover eligible FCF bmask */
James Smart70f3c072010-12-15 17:57:33 -05003878 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05003879 break;
3880 }
James Smart38b92ef2010-08-04 16:11:39 -04003881 spin_unlock_irq(&phba->hbalock);
3882
3883 /* If the event is not for currently used fcf do nothing */
James Smart70f3c072010-12-15 17:57:33 -05003884 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
James Smart38b92ef2010-08-04 16:11:39 -04003885 break;
3886
3887 /*
3888 * Otherwise, request the port to rediscover the entire FCF
3889 * table for a fast recovery from case that the current FCF
3890 * is no longer valid as we are not in the middle of FCF
3891 * failover process already.
3892 */
3893 spin_lock_irq(&phba->hbalock);
James Smartfc2b9892010-02-26 14:15:29 -05003894 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05003895 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003896 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04003897
James Smart0c9ab6f2010-02-26 14:15:57 -05003898 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3899 "2771 Start FCF fast failover process due to "
3900 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
James Smart70f3c072010-12-15 17:57:33 -05003901 "\n", acqe_fip->event_tag, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05003902 rc = lpfc_sli4_redisc_fcf_table(phba);
3903 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05003904 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3905 LOG_DISCOVERY,
3906 "2772 Issue FCF rediscover mabilbox "
3907 "command failed, fail through to FCF "
3908 "dead event\n");
James Smartfc2b9892010-02-26 14:15:29 -05003909 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003910 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003911 spin_unlock_irq(&phba->hbalock);
3912 /*
3913 * Last resort will fail over by treating this
3914 * as a link down to FCF registration.
3915 */
3916 lpfc_sli4_fcf_dead_failthrough(phba);
James Smart38b92ef2010-08-04 16:11:39 -04003917 } else {
3918 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04003919 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart38b92ef2010-08-04 16:11:39 -04003920 /*
3921 * Handling fast FCF failover to a DEAD FCF event is
3922 * considered equalivant to receiving CVL to all vports.
James Smartfc2b9892010-02-26 14:15:29 -05003923 */
3924 lpfc_sli4_perform_all_vport_cvl(phba);
James Smart38b92ef2010-08-04 16:11:39 -04003925 }
James Smartda0436e2009-05-22 14:51:39 -04003926 break;
James Smart70f3c072010-12-15 17:57:33 -05003927 case LPFC_FIP_EVENT_TYPE_CVL:
James Smart80c17842012-03-01 22:35:45 -05003928 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05003929 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6669f9b2009-10-02 15:16:45 -04003930 "2718 Clear Virtual Link Received for VPI 0x%x"
James Smart70f3c072010-12-15 17:57:33 -05003931 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart6d368e52011-05-24 11:44:12 -04003932
James Smart6669f9b2009-10-02 15:16:45 -04003933 vport = lpfc_find_vport_by_vpid(phba,
James Smart5248a742011-07-22 18:37:06 -04003934 acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05003935 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04003936 if (!ndlp)
3937 break;
James Smart695a8142010-01-26 23:08:03 -05003938 active_vlink_present = 0;
3939
3940 vports = lpfc_create_vport_work_array(phba);
3941 if (vports) {
3942 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
3943 i++) {
3944 if ((!(vports[i]->fc_flag &
3945 FC_VPORT_CVL_RCVD)) &&
3946 (vports[i]->port_state > LPFC_FDISC)) {
3947 active_vlink_present = 1;
3948 break;
3949 }
3950 }
3951 lpfc_destroy_vport_work_array(phba, vports);
3952 }
3953
3954 if (active_vlink_present) {
3955 /*
3956 * If there are other active VLinks present,
3957 * re-instantiate the Vlink using FDISC.
3958 */
James Smart6669f9b2009-10-02 15:16:45 -04003959 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
James Smartfc2b9892010-02-26 14:15:29 -05003960 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04003961 spin_lock_irq(shost->host_lock);
3962 ndlp->nlp_flag |= NLP_DELAY_TMO;
3963 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05003964 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
3965 vport->port_state = LPFC_FDISC;
3966 } else {
James Smartecfd03c2010-02-12 14:41:27 -05003967 /*
3968 * Otherwise, we request port to rediscover
3969 * the entire FCF table for a fast recovery
3970 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05003971 * is no longer valid if we are not already
3972 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05003973 */
James Smartfc2b9892010-02-26 14:15:29 -05003974 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003975 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05003976 spin_unlock_irq(&phba->hbalock);
3977 break;
3978 }
3979 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05003980 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003981 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003982 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
3983 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003984 "2773 Start FCF failover per CVL, "
James Smart70f3c072010-12-15 17:57:33 -05003985 "evt_tag:x%x\n", acqe_fip->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05003986 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05003987 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05003988 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3989 LOG_DISCOVERY,
3990 "2774 Issue FCF rediscover "
3991 "mabilbox command failed, "
3992 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05003993 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003994 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003995 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05003996 /*
3997 * Last resort will be re-try on the
3998 * the current registered FCF entry.
3999 */
4000 lpfc_retry_pport_discovery(phba);
James Smart38b92ef2010-08-04 16:11:39 -04004001 } else
4002 /*
4003 * Reset FCF roundrobin bmask for new
4004 * discovery.
4005 */
James Smart7d791df2011-07-22 18:37:52 -04004006 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart6669f9b2009-10-02 15:16:45 -04004007 }
4008 break;
James Smartda0436e2009-05-22 14:51:39 -04004009 default:
4010 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4011 "0288 Unknown FCoE event type 0x%x event tag "
James Smart70f3c072010-12-15 17:57:33 -05004012 "0x%x\n", event_type, acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04004013 break;
4014 }
4015}
4016
4017/**
4018 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
4019 * @phba: pointer to lpfc hba data structure.
4020 * @acqe_link: pointer to the async dcbx completion queue entry.
4021 *
4022 * This routine is to handle the SLI4 asynchronous dcbx event.
4023 **/
4024static void
4025lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
4026 struct lpfc_acqe_dcbx *acqe_dcbx)
4027{
James Smart4d9ab992009-10-02 15:16:39 -04004028 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004029 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4030 "0290 The SLI4 DCBX asynchronous event is not "
4031 "handled yet\n");
4032}
4033
4034/**
James Smartb19a0612010-04-06 14:48:51 -04004035 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
4036 * @phba: pointer to lpfc hba data structure.
4037 * @acqe_link: pointer to the async grp5 completion queue entry.
4038 *
4039 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
4040 * is an asynchronous notified of a logical link speed change. The Port
4041 * reports the logical link speed in units of 10Mbps.
4042 **/
4043static void
4044lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
4045 struct lpfc_acqe_grp5 *acqe_grp5)
4046{
4047 uint16_t prev_ll_spd;
4048
4049 phba->fc_eventTag = acqe_grp5->event_tag;
4050 phba->fcoe_eventtag = acqe_grp5->event_tag;
4051 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
4052 phba->sli4_hba.link_state.logical_speed =
4053 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5));
4054 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4055 "2789 GRP5 Async Event: Updating logical link speed "
4056 "from %dMbps to %dMbps\n", (prev_ll_spd * 10),
4057 (phba->sli4_hba.link_state.logical_speed*10));
4058}
4059
4060/**
James Smartda0436e2009-05-22 14:51:39 -04004061 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
4062 * @phba: pointer to lpfc hba data structure.
4063 *
4064 * This routine is invoked by the worker thread to process all the pending
4065 * SLI4 asynchronous events.
4066 **/
4067void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
4068{
4069 struct lpfc_cq_event *cq_event;
4070
4071 /* First, declare the async event has been handled */
4072 spin_lock_irq(&phba->hbalock);
4073 phba->hba_flag &= ~ASYNC_EVENT;
4074 spin_unlock_irq(&phba->hbalock);
4075 /* Now, handle all the async events */
4076 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
4077 /* Get the first event from the head of the event queue */
4078 spin_lock_irq(&phba->hbalock);
4079 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
4080 cq_event, struct lpfc_cq_event, list);
4081 spin_unlock_irq(&phba->hbalock);
4082 /* Process the asynchronous event */
4083 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
4084 case LPFC_TRAILER_CODE_LINK:
4085 lpfc_sli4_async_link_evt(phba,
4086 &cq_event->cqe.acqe_link);
4087 break;
4088 case LPFC_TRAILER_CODE_FCOE:
James Smart70f3c072010-12-15 17:57:33 -05004089 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004090 break;
4091 case LPFC_TRAILER_CODE_DCBX:
4092 lpfc_sli4_async_dcbx_evt(phba,
4093 &cq_event->cqe.acqe_dcbx);
4094 break;
James Smartb19a0612010-04-06 14:48:51 -04004095 case LPFC_TRAILER_CODE_GRP5:
4096 lpfc_sli4_async_grp5_evt(phba,
4097 &cq_event->cqe.acqe_grp5);
4098 break;
James Smart70f3c072010-12-15 17:57:33 -05004099 case LPFC_TRAILER_CODE_FC:
4100 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
4101 break;
4102 case LPFC_TRAILER_CODE_SLI:
4103 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
4104 break;
James Smartda0436e2009-05-22 14:51:39 -04004105 default:
4106 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4107 "1804 Invalid asynchrous event code: "
4108 "x%x\n", bf_get(lpfc_trailer_code,
4109 &cq_event->cqe.mcqe_cmpl));
4110 break;
4111 }
4112 /* Free the completion event processed to the free pool */
4113 lpfc_sli4_cq_event_release(phba, cq_event);
4114 }
4115}
4116
4117/**
James Smartecfd03c2010-02-12 14:41:27 -05004118 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
4119 * @phba: pointer to lpfc hba data structure.
4120 *
4121 * This routine is invoked by the worker thread to process FCF table
4122 * rediscovery pending completion event.
4123 **/
4124void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
4125{
4126 int rc;
4127
4128 spin_lock_irq(&phba->hbalock);
4129 /* Clear FCF rediscovery timeout event */
4130 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
4131 /* Clear driver fast failover FCF record flag */
4132 phba->fcf.failover_rec.flag = 0;
4133 /* Set state for FCF fast failover */
4134 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
4135 spin_unlock_irq(&phba->hbalock);
4136
4137 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05004138 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004139 "2777 Start post-quiescent FCF table scan\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05004140 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05004141 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05004142 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4143 "2747 Issue FCF scan read FCF mailbox "
4144 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05004145}
4146
4147/**
James Smartda0436e2009-05-22 14:51:39 -04004148 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
4149 * @phba: pointer to lpfc hba data structure.
4150 * @dev_grp: The HBA PCI-Device group number.
4151 *
4152 * This routine is invoked to set up the per HBA PCI-Device group function
4153 * API jump table entries.
4154 *
4155 * Return: 0 if success, otherwise -ENODEV
4156 **/
4157int
4158lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05004159{
4160 int rc;
4161
James Smartda0436e2009-05-22 14:51:39 -04004162 /* Set up lpfc PCI-device group */
4163 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05004164
James Smartda0436e2009-05-22 14:51:39 -04004165 /* The LPFC_PCI_DEV_OC uses SLI4 */
4166 if (dev_grp == LPFC_PCI_DEV_OC)
4167 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05004168
James Smartda0436e2009-05-22 14:51:39 -04004169 /* Set up device INIT API function jump table */
4170 rc = lpfc_init_api_table_setup(phba, dev_grp);
4171 if (rc)
4172 return -ENODEV;
4173 /* Set up SCSI API function jump table */
4174 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
4175 if (rc)
4176 return -ENODEV;
4177 /* Set up SLI API function jump table */
4178 rc = lpfc_sli_api_table_setup(phba, dev_grp);
4179 if (rc)
4180 return -ENODEV;
4181 /* Set up MBOX API function jump table */
4182 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
4183 if (rc)
4184 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004185
James Smartda0436e2009-05-22 14:51:39 -04004186 return 0;
James Smart5b75da22008-12-04 22:39:35 -05004187}
4188
4189/**
James Smart3621a712009-04-06 18:47:14 -04004190 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05004191 * @phba: pointer to lpfc hba data structure.
4192 * @intr_mode: active interrupt mode adopted.
4193 *
4194 * This routine it invoked to log the currently used active interrupt mode
4195 * to the device.
James Smart3772a992009-05-22 14:50:54 -04004196 **/
4197static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05004198{
4199 switch (intr_mode) {
4200 case 0:
4201 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4202 "0470 Enable INTx interrupt mode.\n");
4203 break;
4204 case 1:
4205 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4206 "0481 Enabled MSI interrupt mode.\n");
4207 break;
4208 case 2:
4209 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4210 "0480 Enabled MSI-X interrupt mode.\n");
4211 break;
4212 default:
4213 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4214 "0482 Illegal interrupt mode.\n");
4215 break;
4216 }
4217 return;
4218}
4219
James Smart5b75da22008-12-04 22:39:35 -05004220/**
James Smart3772a992009-05-22 14:50:54 -04004221 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05004222 * @phba: pointer to lpfc hba data structure.
4223 *
James Smart3772a992009-05-22 14:50:54 -04004224 * This routine is invoked to enable the PCI device that is common to all
4225 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004226 *
4227 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004228 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004229 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05004230 **/
James Smart3772a992009-05-22 14:50:54 -04004231static int
4232lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004233{
James Smart3772a992009-05-22 14:50:54 -04004234 struct pci_dev *pdev;
James Smart079b5c92011-08-21 21:48:49 -04004235 int bars = 0;
James Smart5b75da22008-12-04 22:39:35 -05004236
James Smart3772a992009-05-22 14:50:54 -04004237 /* Obtain PCI device reference */
4238 if (!phba->pcidev)
4239 goto out_error;
4240 else
4241 pdev = phba->pcidev;
4242 /* Select PCI BARs */
4243 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4244 /* Enable PCI device */
4245 if (pci_enable_device_mem(pdev))
4246 goto out_error;
4247 /* Request PCI resource for the device */
4248 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
4249 goto out_disable_device;
4250 /* Set up device as PCI master and save state for EEH */
4251 pci_set_master(pdev);
4252 pci_try_set_mwi(pdev);
4253 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05004254
James Smart05580562011-05-24 11:40:48 -04004255 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
4256 if (pci_find_capability(pdev, PCI_CAP_ID_EXP))
4257 pdev->needs_freset = 1;
4258
James Smart3772a992009-05-22 14:50:54 -04004259 return 0;
James Smart5b75da22008-12-04 22:39:35 -05004260
James Smart3772a992009-05-22 14:50:54 -04004261out_disable_device:
4262 pci_disable_device(pdev);
4263out_error:
James Smart079b5c92011-08-21 21:48:49 -04004264 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4265 "1401 Failed to enable pci device, bars:x%x\n", bars);
James Smart3772a992009-05-22 14:50:54 -04004266 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004267}
4268
4269/**
James Smart3772a992009-05-22 14:50:54 -04004270 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05004271 * @phba: pointer to lpfc hba data structure.
4272 *
James Smart3772a992009-05-22 14:50:54 -04004273 * This routine is invoked to disable the PCI device that is common to all
4274 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004275 **/
4276static void
James Smart3772a992009-05-22 14:50:54 -04004277lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004278{
James Smart3772a992009-05-22 14:50:54 -04004279 struct pci_dev *pdev;
4280 int bars;
James Smart5b75da22008-12-04 22:39:35 -05004281
James Smart3772a992009-05-22 14:50:54 -04004282 /* Obtain PCI device reference */
4283 if (!phba->pcidev)
4284 return;
4285 else
4286 pdev = phba->pcidev;
4287 /* Select PCI BARs */
4288 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4289 /* Release PCI resource and disable PCI device */
4290 pci_release_selected_regions(pdev, bars);
4291 pci_disable_device(pdev);
4292 /* Null out PCI private reference to driver */
4293 pci_set_drvdata(pdev, NULL);
James Smart5b75da22008-12-04 22:39:35 -05004294
4295 return;
4296}
4297
4298/**
James Smart3772a992009-05-22 14:50:54 -04004299 * lpfc_reset_hba - Reset a hba
4300 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04004301 *
James Smart3772a992009-05-22 14:50:54 -04004302 * This routine is invoked to reset a hba device. It brings the HBA
4303 * offline, performs a board restart, and then brings the board back
4304 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
4305 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04004306 **/
James Smart3772a992009-05-22 14:50:54 -04004307void
4308lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004309{
James Smart3772a992009-05-22 14:50:54 -04004310 /* If resets are disabled then set error state and return. */
4311 if (!phba->cfg_enable_hba_reset) {
4312 phba->link_state = LPFC_HBA_ERROR;
4313 return;
4314 }
4315 lpfc_offline_prep(phba);
4316 lpfc_offline(phba);
4317 lpfc_sli_brdrestart(phba);
4318 lpfc_online(phba);
4319 lpfc_unblock_mgmt_io(phba);
4320}
dea31012005-04-17 16:05:31 -05004321
James Smart3772a992009-05-22 14:50:54 -04004322/**
James Smart0a96e972011-07-22 18:37:28 -04004323 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
4324 * @phba: pointer to lpfc hba data structure.
4325 *
4326 * This function enables the PCI SR-IOV virtual functions to a physical
4327 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4328 * enable the number of virtual functions to the physical function. As
4329 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4330 * API call does not considered as an error condition for most of the device.
4331 **/
4332uint16_t
4333lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
4334{
4335 struct pci_dev *pdev = phba->pcidev;
4336 uint16_t nr_virtfn;
4337 int pos;
4338
James Smart0a96e972011-07-22 18:37:28 -04004339 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
4340 if (pos == 0)
4341 return 0;
4342
4343 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
4344 return nr_virtfn;
4345}
4346
4347/**
James Smart912e3ac2011-05-24 11:42:11 -04004348 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
4349 * @phba: pointer to lpfc hba data structure.
4350 * @nr_vfn: number of virtual functions to be enabled.
4351 *
4352 * This function enables the PCI SR-IOV virtual functions to a physical
4353 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4354 * enable the number of virtual functions to the physical function. As
4355 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4356 * API call does not considered as an error condition for most of the device.
4357 **/
4358int
4359lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
4360{
4361 struct pci_dev *pdev = phba->pcidev;
James Smart0a96e972011-07-22 18:37:28 -04004362 uint16_t max_nr_vfn;
James Smart912e3ac2011-05-24 11:42:11 -04004363 int rc;
4364
James Smart0a96e972011-07-22 18:37:28 -04004365 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
4366 if (nr_vfn > max_nr_vfn) {
4367 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4368 "3057 Requested vfs (%d) greater than "
4369 "supported vfs (%d)", nr_vfn, max_nr_vfn);
4370 return -EINVAL;
4371 }
4372
James Smart912e3ac2011-05-24 11:42:11 -04004373 rc = pci_enable_sriov(pdev, nr_vfn);
4374 if (rc) {
4375 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4376 "2806 Failed to enable sriov on this device "
4377 "with vfn number nr_vf:%d, rc:%d\n",
4378 nr_vfn, rc);
4379 } else
4380 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4381 "2807 Successful enable sriov on this device "
4382 "with vfn number nr_vf:%d\n", nr_vfn);
4383 return rc;
4384}
4385
4386/**
James Smart3772a992009-05-22 14:50:54 -04004387 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
4388 * @phba: pointer to lpfc hba data structure.
4389 *
4390 * This routine is invoked to set up the driver internal resources specific to
4391 * support the SLI-3 HBA device it attached to.
4392 *
4393 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004394 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004395 * other values - error
4396 **/
4397static int
4398lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
4399{
4400 struct lpfc_sli *psli;
James Smart912e3ac2011-05-24 11:42:11 -04004401 int rc;
dea31012005-04-17 16:05:31 -05004402
James Smart3772a992009-05-22 14:50:54 -04004403 /*
4404 * Initialize timers used by driver
4405 */
dea31012005-04-17 16:05:31 -05004406
James Smart3772a992009-05-22 14:50:54 -04004407 /* Heartbeat timer */
4408 init_timer(&phba->hb_tmofunc);
4409 phba->hb_tmofunc.function = lpfc_hb_timeout;
4410 phba->hb_tmofunc.data = (unsigned long)phba;
4411
4412 psli = &phba->sli;
4413 /* MBOX heartbeat timer */
4414 init_timer(&psli->mbox_tmo);
4415 psli->mbox_tmo.function = lpfc_mbox_timeout;
4416 psli->mbox_tmo.data = (unsigned long) phba;
4417 /* FCP polling mode timer */
4418 init_timer(&phba->fcp_poll_timer);
4419 phba->fcp_poll_timer.function = lpfc_poll_timeout;
4420 phba->fcp_poll_timer.data = (unsigned long) phba;
4421 /* Fabric block timer */
4422 init_timer(&phba->fabric_block_timer);
4423 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4424 phba->fabric_block_timer.data = (unsigned long) phba;
4425 /* EA polling mode timer */
4426 init_timer(&phba->eratt_poll);
4427 phba->eratt_poll.function = lpfc_poll_eratt;
4428 phba->eratt_poll.data = (unsigned long) phba;
4429
4430 /* Host attention work mask setup */
4431 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
4432 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
4433
4434 /* Get all the module params for configuring this host */
4435 lpfc_get_cfgparam(phba);
James Smart49198b32010-04-06 15:04:33 -04004436 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
4437 phba->menlo_flag |= HBA_MENLO_SUPPORT;
4438 /* check for menlo minimum sg count */
4439 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
4440 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
4441 }
4442
James Smart3772a992009-05-22 14:50:54 -04004443 /*
4444 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
4445 * used to create the sg_dma_buf_pool must be dynamically calculated.
4446 * 2 segments are added since the IOCB needs a command and response bde.
4447 */
4448 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4449 sizeof(struct fcp_rsp) +
4450 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
4451
4452 if (phba->cfg_enable_bg) {
4453 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT;
4454 phba->cfg_sg_dma_buf_size +=
4455 phba->cfg_prot_sg_seg_cnt * sizeof(struct ulp_bde64);
4456 }
4457
4458 /* Also reinitialize the host templates with new values. */
4459 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4460 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4461
4462 phba->max_vpi = LPFC_MAX_VPI;
4463 /* This will be set to correct value after config_port mbox */
4464 phba->max_vports = 0;
4465
4466 /*
4467 * Initialize the SLI Layer to run with lpfc HBAs.
4468 */
4469 lpfc_sli_setup(phba);
4470 lpfc_sli_queue_setup(phba);
4471
4472 /* Allocate device driver memory */
4473 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
4474 return -ENOMEM;
4475
James Smart912e3ac2011-05-24 11:42:11 -04004476 /*
4477 * Enable sr-iov virtual functions if supported and configured
4478 * through the module parameter.
4479 */
4480 if (phba->cfg_sriov_nr_virtfn > 0) {
4481 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
4482 phba->cfg_sriov_nr_virtfn);
4483 if (rc) {
4484 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4485 "2808 Requested number of SR-IOV "
4486 "virtual functions (%d) is not "
4487 "supported\n",
4488 phba->cfg_sriov_nr_virtfn);
4489 phba->cfg_sriov_nr_virtfn = 0;
4490 }
4491 }
4492
James Smart3772a992009-05-22 14:50:54 -04004493 return 0;
4494}
4495
4496/**
4497 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
4498 * @phba: pointer to lpfc hba data structure.
4499 *
4500 * This routine is invoked to unset the driver internal resources set up
4501 * specific for supporting the SLI-3 HBA device it attached to.
4502 **/
4503static void
4504lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
4505{
4506 /* Free device driver memory allocated */
4507 lpfc_mem_free_all(phba);
4508
4509 return;
4510}
4511
4512/**
James Smartda0436e2009-05-22 14:51:39 -04004513 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
4514 * @phba: pointer to lpfc hba data structure.
4515 *
4516 * This routine is invoked to set up the driver internal resources specific to
4517 * support the SLI-4 HBA device it attached to.
4518 *
4519 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004520 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004521 * other values - error
4522 **/
4523static int
4524lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
4525{
4526 struct lpfc_sli *psli;
James Smart28baac72010-02-12 14:42:03 -05004527 LPFC_MBOXQ_t *mboxq;
4528 int rc, i, hbq_count, buf_size, dma_buf_size, max_buf_size;
4529 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
4530 struct lpfc_mqe *mqe;
James Smart085c6472010-11-20 23:11:37 -05004531 int longs, sli_family;
James Smartacd68592012-01-18 16:25:09 -05004532 int sges_per_segment;
James Smartda0436e2009-05-22 14:51:39 -04004533
4534 /* Before proceed, wait for POST done and device ready */
4535 rc = lpfc_sli4_post_status_check(phba);
4536 if (rc)
4537 return -ENODEV;
4538
4539 /*
4540 * Initialize timers used by driver
4541 */
4542
4543 /* Heartbeat timer */
4544 init_timer(&phba->hb_tmofunc);
4545 phba->hb_tmofunc.function = lpfc_hb_timeout;
4546 phba->hb_tmofunc.data = (unsigned long)phba;
James Smart19ca7602010-11-20 23:11:55 -05004547 init_timer(&phba->rrq_tmr);
4548 phba->rrq_tmr.function = lpfc_rrq_timeout;
4549 phba->rrq_tmr.data = (unsigned long)phba;
James Smartda0436e2009-05-22 14:51:39 -04004550
4551 psli = &phba->sli;
4552 /* MBOX heartbeat timer */
4553 init_timer(&psli->mbox_tmo);
4554 psli->mbox_tmo.function = lpfc_mbox_timeout;
4555 psli->mbox_tmo.data = (unsigned long) phba;
4556 /* Fabric block timer */
4557 init_timer(&phba->fabric_block_timer);
4558 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4559 phba->fabric_block_timer.data = (unsigned long) phba;
4560 /* EA polling mode timer */
4561 init_timer(&phba->eratt_poll);
4562 phba->eratt_poll.function = lpfc_poll_eratt;
4563 phba->eratt_poll.data = (unsigned long) phba;
James Smartecfd03c2010-02-12 14:41:27 -05004564 /* FCF rediscover timer */
4565 init_timer(&phba->fcf.redisc_wait);
4566 phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
4567 phba->fcf.redisc_wait.data = (unsigned long)phba;
4568
James Smartda0436e2009-05-22 14:51:39 -04004569 /*
James Smart7ad20aa2011-05-24 11:44:28 -04004570 * Control structure for handling external multi-buffer mailbox
4571 * command pass-through.
4572 */
4573 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
4574 sizeof(struct lpfc_mbox_ext_buf_ctx));
4575 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
4576
4577 /*
James Smartda0436e2009-05-22 14:51:39 -04004578 * We need to do a READ_CONFIG mailbox command here before
4579 * calling lpfc_get_cfgparam. For VFs this will report the
4580 * MAX_XRI, MAX_VPI, MAX_RPI, MAX_IOCB, and MAX_VFI settings.
4581 * All of the resources allocated
4582 * for this Port are tied to these values.
4583 */
4584 /* Get all the module params for configuring this host */
4585 lpfc_get_cfgparam(phba);
4586 phba->max_vpi = LPFC_MAX_VPI;
4587 /* This will be set to correct value after the read_config mbox */
4588 phba->max_vports = 0;
4589
4590 /* Program the default value of vlan_id and fc_map */
4591 phba->valid_vlan = 0;
4592 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4593 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4594 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4595
James Smartacd68592012-01-18 16:25:09 -05004596 /* With BlockGuard we can have multiple SGEs per Data Segemnt */
4597 sges_per_segment = 1;
4598 if (phba->cfg_enable_bg)
4599 sges_per_segment = 2;
4600
James Smartda0436e2009-05-22 14:51:39 -04004601 /*
4602 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
4603 * used to create the sg_dma_buf_pool must be dynamically calculated.
4604 * 2 segments are added since the IOCB needs a command and response bde.
4605 * To insure that the scsi sgl does not cross a 4k page boundary only
James Smart28baac72010-02-12 14:42:03 -05004606 * sgl sizes of must be a power of 2.
James Smartda0436e2009-05-22 14:51:39 -04004607 */
James Smart28baac72010-02-12 14:42:03 -05004608 buf_size = (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp) +
James Smartacd68592012-01-18 16:25:09 -05004609 (((phba->cfg_sg_seg_cnt * sges_per_segment) + 2) *
4610 sizeof(struct sli4_sge)));
James Smart085c6472010-11-20 23:11:37 -05004611
4612 sli_family = bf_get(lpfc_sli_intf_sli_family, &phba->sli4_hba.sli_intf);
4613 max_buf_size = LPFC_SLI4_MAX_BUF_SIZE;
4614 switch (sli_family) {
4615 case LPFC_SLI_INTF_FAMILY_BE2:
4616 case LPFC_SLI_INTF_FAMILY_BE3:
4617 /* There is a single hint for BE - 2 pages per BPL. */
4618 if (bf_get(lpfc_sli_intf_sli_hint1, &phba->sli4_hba.sli_intf) ==
4619 LPFC_SLI_INTF_SLI_HINT1_1)
4620 max_buf_size = LPFC_SLI4_FL1_MAX_BUF_SIZE;
4621 break;
4622 case LPFC_SLI_INTF_FAMILY_LNCR_A0:
4623 case LPFC_SLI_INTF_FAMILY_LNCR_B0:
4624 default:
4625 break;
4626 }
James Smartacd68592012-01-18 16:25:09 -05004627
James Smart28baac72010-02-12 14:42:03 -05004628 for (dma_buf_size = LPFC_SLI4_MIN_BUF_SIZE;
4629 dma_buf_size < max_buf_size && buf_size > dma_buf_size;
4630 dma_buf_size = dma_buf_size << 1)
4631 ;
4632 if (dma_buf_size == max_buf_size)
4633 phba->cfg_sg_seg_cnt = (dma_buf_size -
4634 sizeof(struct fcp_cmnd) - sizeof(struct fcp_rsp) -
4635 (2 * sizeof(struct sli4_sge))) /
4636 sizeof(struct sli4_sge);
4637 phba->cfg_sg_dma_buf_size = dma_buf_size;
James Smartda0436e2009-05-22 14:51:39 -04004638
4639 /* Initialize buffer queue management fields */
4640 hbq_count = lpfc_sli_hbq_count();
4641 for (i = 0; i < hbq_count; ++i)
4642 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
4643 INIT_LIST_HEAD(&phba->rb_pend_list);
4644 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
4645 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
4646
4647 /*
4648 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
4649 */
4650 /* Initialize the Abort scsi buffer list used by driver */
4651 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
4652 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
4653 /* This abort list used by worker thread */
4654 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
4655
4656 /*
James Smart6d368e52011-05-24 11:44:12 -04004657 * Initialize driver internal slow-path work queues
James Smartda0436e2009-05-22 14:51:39 -04004658 */
4659
4660 /* Driver internel slow-path CQ Event pool */
4661 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
4662 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04004663 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04004664 /* Asynchronous event CQ Event work queue list */
4665 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
4666 /* Fast-path XRI aborted CQ Event work queue list */
4667 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
4668 /* Slow-path XRI aborted CQ Event work queue list */
4669 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
4670 /* Receive queue CQ Event work queue list */
4671 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
4672
James Smart6d368e52011-05-24 11:44:12 -04004673 /* Initialize extent block lists. */
4674 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
4675 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
4676 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
4677 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
4678
James Smartda0436e2009-05-22 14:51:39 -04004679 /* Initialize the driver internal SLI layer lists. */
4680 lpfc_sli_setup(phba);
4681 lpfc_sli_queue_setup(phba);
4682
4683 /* Allocate device driver memory */
4684 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
4685 if (rc)
4686 return -ENOMEM;
4687
James Smart2fcee4b2010-12-15 17:57:46 -05004688 /* IF Type 2 ports get initialized now. */
4689 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
4690 LPFC_SLI_INTF_IF_TYPE_2) {
4691 rc = lpfc_pci_function_reset(phba);
4692 if (unlikely(rc))
4693 return -ENODEV;
4694 }
4695
James Smartda0436e2009-05-22 14:51:39 -04004696 /* Create the bootstrap mailbox command */
4697 rc = lpfc_create_bootstrap_mbox(phba);
4698 if (unlikely(rc))
4699 goto out_free_mem;
4700
4701 /* Set up the host's endian order with the device. */
4702 rc = lpfc_setup_endian_order(phba);
4703 if (unlikely(rc))
4704 goto out_free_bsmbx;
4705
4706 /* Set up the hba's configuration parameters. */
4707 rc = lpfc_sli4_read_config(phba);
4708 if (unlikely(rc))
4709 goto out_free_bsmbx;
4710
James Smart2fcee4b2010-12-15 17:57:46 -05004711 /* IF Type 0 ports get initialized now. */
4712 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
4713 LPFC_SLI_INTF_IF_TYPE_0) {
4714 rc = lpfc_pci_function_reset(phba);
4715 if (unlikely(rc))
4716 goto out_free_bsmbx;
4717 }
James Smartda0436e2009-05-22 14:51:39 -04004718
James Smartcb5172e2010-03-15 11:25:07 -04004719 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
4720 GFP_KERNEL);
4721 if (!mboxq) {
4722 rc = -ENOMEM;
4723 goto out_free_bsmbx;
4724 }
4725
James Smartfedd3b72011-02-16 12:39:24 -05004726 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
James Smartcb5172e2010-03-15 11:25:07 -04004727 lpfc_supported_pages(mboxq);
4728 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smartfedd3b72011-02-16 12:39:24 -05004729 if (!rc) {
4730 mqe = &mboxq->u.mqe;
4731 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
4732 LPFC_MAX_SUPPORTED_PAGES);
4733 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
4734 switch (pn_page[i]) {
4735 case LPFC_SLI4_PARAMETERS:
4736 phba->sli4_hba.pc_sli4_params.supported = 1;
4737 break;
4738 default:
4739 break;
4740 }
4741 }
4742 /* Read the port's SLI4 Parameters capabilities if supported. */
4743 if (phba->sli4_hba.pc_sli4_params.supported)
4744 rc = lpfc_pc_sli4_params_get(phba, mboxq);
4745 if (rc) {
4746 mempool_free(mboxq, phba->mbox_mem_pool);
4747 rc = -EIO;
4748 goto out_free_bsmbx;
James Smartcb5172e2010-03-15 11:25:07 -04004749 }
4750 }
James Smartfedd3b72011-02-16 12:39:24 -05004751 /*
4752 * Get sli4 parameters that override parameters from Port capabilities.
James Smart6d368e52011-05-24 11:44:12 -04004753 * If this call fails, it isn't critical unless the SLI4 parameters come
4754 * back in conflict.
James Smartfedd3b72011-02-16 12:39:24 -05004755 */
James Smart6d368e52011-05-24 11:44:12 -04004756 rc = lpfc_get_sli4_parameters(phba, mboxq);
4757 if (rc) {
4758 if (phba->sli4_hba.extents_in_use &&
4759 phba->sli4_hba.rpi_hdrs_in_use) {
4760 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4761 "2999 Unsupported SLI4 Parameters "
4762 "Extents and RPI headers enabled.\n");
4763 goto out_free_bsmbx;
4764 }
4765 }
James Smartcb5172e2010-03-15 11:25:07 -04004766 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart5350d872011-10-10 21:33:49 -04004767 /* Verify all the SLI4 queues */
4768 rc = lpfc_sli4_queue_verify(phba);
James Smartda0436e2009-05-22 14:51:39 -04004769 if (rc)
4770 goto out_free_bsmbx;
4771
4772 /* Create driver internal CQE event pool */
4773 rc = lpfc_sli4_cq_event_pool_create(phba);
4774 if (rc)
James Smart5350d872011-10-10 21:33:49 -04004775 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04004776
James Smart8a9d2e82012-05-09 21:16:12 -04004777 /* Initialize sgl lists per host */
4778 lpfc_init_sgl_list(phba);
4779
4780 /* Allocate and initialize active sgl array */
James Smartda0436e2009-05-22 14:51:39 -04004781 rc = lpfc_init_active_sgl_array(phba);
4782 if (rc) {
4783 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4784 "1430 Failed to initialize sgl list.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04004785 goto out_destroy_cq_event_pool;
James Smartda0436e2009-05-22 14:51:39 -04004786 }
James Smartda0436e2009-05-22 14:51:39 -04004787 rc = lpfc_sli4_init_rpi_hdrs(phba);
4788 if (rc) {
4789 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4790 "1432 Failed to initialize rpi headers.\n");
4791 goto out_free_active_sgl;
4792 }
4793
James Smarta93ff372010-10-22 11:06:08 -04004794 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
James Smart0c9ab6f2010-02-26 14:15:57 -05004795 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
4796 phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
4797 GFP_KERNEL);
4798 if (!phba->fcf.fcf_rr_bmask) {
4799 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4800 "2759 Failed allocate memory for FCF round "
4801 "robin failover bmask\n");
James Smart05580562011-05-24 11:40:48 -04004802 rc = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -05004803 goto out_remove_rpi_hdrs;
4804 }
4805
James Smart5350d872011-10-10 21:33:49 -04004806 /*
4807 * The cfg_fcp_eq_count can be zero whenever there is exactly one
4808 * interrupt vector. This is not an error
4809 */
4810 if (phba->cfg_fcp_eq_count) {
4811 phba->sli4_hba.fcp_eq_hdl =
4812 kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
James Smartda0436e2009-05-22 14:51:39 -04004813 phba->cfg_fcp_eq_count), GFP_KERNEL);
James Smart5350d872011-10-10 21:33:49 -04004814 if (!phba->sli4_hba.fcp_eq_hdl) {
4815 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4816 "2572 Failed allocate memory for "
4817 "fast-path per-EQ handle array\n");
4818 rc = -ENOMEM;
4819 goto out_free_fcf_rr_bmask;
4820 }
James Smartda0436e2009-05-22 14:51:39 -04004821 }
4822
4823 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
4824 phba->sli4_hba.cfg_eqn), GFP_KERNEL);
4825 if (!phba->sli4_hba.msix_entries) {
4826 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4827 "2573 Failed allocate memory for msi-x "
4828 "interrupt vector entries\n");
James Smart05580562011-05-24 11:40:48 -04004829 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04004830 goto out_free_fcp_eq_hdl;
4831 }
4832
James Smart912e3ac2011-05-24 11:42:11 -04004833 /*
4834 * Enable sr-iov virtual functions if supported and configured
4835 * through the module parameter.
4836 */
4837 if (phba->cfg_sriov_nr_virtfn > 0) {
4838 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
4839 phba->cfg_sriov_nr_virtfn);
4840 if (rc) {
4841 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4842 "3020 Requested number of SR-IOV "
4843 "virtual functions (%d) is not "
4844 "supported\n",
4845 phba->cfg_sriov_nr_virtfn);
4846 phba->cfg_sriov_nr_virtfn = 0;
4847 }
4848 }
4849
James Smart5248a742011-07-22 18:37:06 -04004850 return 0;
James Smartda0436e2009-05-22 14:51:39 -04004851
4852out_free_fcp_eq_hdl:
4853 kfree(phba->sli4_hba.fcp_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05004854out_free_fcf_rr_bmask:
4855 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04004856out_remove_rpi_hdrs:
4857 lpfc_sli4_remove_rpi_hdrs(phba);
4858out_free_active_sgl:
4859 lpfc_free_active_sgl(phba);
James Smartda0436e2009-05-22 14:51:39 -04004860out_destroy_cq_event_pool:
4861 lpfc_sli4_cq_event_pool_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04004862out_free_bsmbx:
4863 lpfc_destroy_bootstrap_mbox(phba);
4864out_free_mem:
4865 lpfc_mem_free(phba);
4866 return rc;
4867}
4868
4869/**
4870 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
4871 * @phba: pointer to lpfc hba data structure.
4872 *
4873 * This routine is invoked to unset the driver internal resources set up
4874 * specific for supporting the SLI-4 HBA device it attached to.
4875 **/
4876static void
4877lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
4878{
4879 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
4880
James Smartda0436e2009-05-22 14:51:39 -04004881 /* Free memory allocated for msi-x interrupt vector entries */
4882 kfree(phba->sli4_hba.msix_entries);
4883
4884 /* Free memory allocated for fast-path work queue handles */
4885 kfree(phba->sli4_hba.fcp_eq_hdl);
4886
4887 /* Free the allocated rpi headers. */
4888 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04004889 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04004890
James Smart0c9ab6f2010-02-26 14:15:57 -05004891 /* Free eligible FCF index bmask */
4892 kfree(phba->fcf.fcf_rr_bmask);
4893
James Smartda0436e2009-05-22 14:51:39 -04004894 /* Free the ELS sgl list */
4895 lpfc_free_active_sgl(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04004896 lpfc_free_els_sgl_list(phba);
James Smartda0436e2009-05-22 14:51:39 -04004897
James Smartda0436e2009-05-22 14:51:39 -04004898 /* Free the completion queue EQ event pool */
4899 lpfc_sli4_cq_event_release_all(phba);
4900 lpfc_sli4_cq_event_pool_destroy(phba);
4901
James Smart6d368e52011-05-24 11:44:12 -04004902 /* Release resource identifiers. */
4903 lpfc_sli4_dealloc_resource_identifiers(phba);
4904
James Smartda0436e2009-05-22 14:51:39 -04004905 /* Free the bsmbx region. */
4906 lpfc_destroy_bootstrap_mbox(phba);
4907
4908 /* Free the SLI Layer memory with SLI4 HBAs */
4909 lpfc_mem_free_all(phba);
4910
4911 /* Free the current connect table */
4912 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04004913 &phba->fcf_conn_rec_list, list) {
4914 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04004915 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04004916 }
James Smartda0436e2009-05-22 14:51:39 -04004917
4918 return;
4919}
4920
4921/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004922 * lpfc_init_api_table_setup - Set up init api function jump table
James Smart3772a992009-05-22 14:50:54 -04004923 * @phba: The hba struct for which this call is being executed.
4924 * @dev_grp: The HBA PCI-Device group number.
4925 *
4926 * This routine sets up the device INIT interface API function jump table
4927 * in @phba struct.
4928 *
4929 * Returns: 0 - success, -ENODEV - failure.
4930 **/
4931int
4932lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4933{
James Smart84d1b002010-02-12 14:42:33 -05004934 phba->lpfc_hba_init_link = lpfc_hba_init_link;
4935 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart7f860592011-03-11 16:05:52 -05004936 phba->lpfc_selective_reset = lpfc_selective_reset;
James Smart3772a992009-05-22 14:50:54 -04004937 switch (dev_grp) {
4938 case LPFC_PCI_DEV_LP:
4939 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
4940 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
4941 phba->lpfc_stop_port = lpfc_stop_port_s3;
4942 break;
James Smartda0436e2009-05-22 14:51:39 -04004943 case LPFC_PCI_DEV_OC:
4944 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
4945 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
4946 phba->lpfc_stop_port = lpfc_stop_port_s4;
4947 break;
James Smart3772a992009-05-22 14:50:54 -04004948 default:
4949 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4950 "1431 Invalid HBA PCI-device group: 0x%x\n",
4951 dev_grp);
4952 return -ENODEV;
4953 break;
4954 }
4955 return 0;
4956}
4957
4958/**
4959 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
4960 * @phba: pointer to lpfc hba data structure.
4961 *
4962 * This routine is invoked to set up the driver internal resources before the
4963 * device specific resource setup to support the HBA device it attached to.
4964 *
4965 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004966 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004967 * other values - error
4968 **/
4969static int
4970lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
4971{
4972 /*
4973 * Driver resources common to all SLI revisions
4974 */
James Smartea2151b2008-09-07 11:52:10 -04004975 atomic_set(&phba->fast_event_count, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004976 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004977
James Smarte47c9092008-02-08 18:49:26 -05004978 /* Initialize ndlp management spinlock */
4979 spin_lock_init(&phba->ndlp_lock);
4980
James Smart2e0fef82007-06-17 19:56:36 -05004981 INIT_LIST_HEAD(&phba->port_list);
James Smart3772a992009-05-22 14:50:54 -04004982 INIT_LIST_HEAD(&phba->work_list);
James Smart84774a42008-08-24 21:50:06 -04004983 init_waitqueue_head(&phba->wait_4_mlo_m_q);
dea31012005-04-17 16:05:31 -05004984
James Smart3772a992009-05-22 14:50:54 -04004985 /* Initialize the wait queue head for the kernel thread */
4986 init_waitqueue_head(&phba->work_waitq);
James Smart858c9f62007-06-17 19:56:39 -05004987
James Smart3772a992009-05-22 14:50:54 -04004988 /* Initialize the scsi buffer list used by driver for scsi IO */
4989 spin_lock_init(&phba->scsi_buf_list_lock);
4990 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
dea31012005-04-17 16:05:31 -05004991
James Smart3772a992009-05-22 14:50:54 -04004992 /* Initialize the fabric iocb list */
4993 INIT_LIST_HEAD(&phba->fabric_iocb_list);
dea31012005-04-17 16:05:31 -05004994
James Smart3772a992009-05-22 14:50:54 -04004995 /* Initialize list to save ELS buffers */
4996 INIT_LIST_HEAD(&phba->elsbuf);
dea31012005-04-17 16:05:31 -05004997
James Smart3772a992009-05-22 14:50:54 -04004998 /* Initialize FCF connection rec list */
4999 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
dea31012005-04-17 16:05:31 -05005000
James Smart3772a992009-05-22 14:50:54 -04005001 return 0;
5002}
dea31012005-04-17 16:05:31 -05005003
James Smart3772a992009-05-22 14:50:54 -04005004/**
5005 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
5006 * @phba: pointer to lpfc hba data structure.
5007 *
5008 * This routine is invoked to set up the driver internal resources after the
5009 * device specific resource setup to support the HBA device it attached to.
5010 *
5011 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005012 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005013 * other values - error
5014 **/
5015static int
5016lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
5017{
5018 int error;
5019
5020 /* Startup the kernel thread for this host adapter. */
5021 phba->worker_thread = kthread_run(lpfc_do_work, phba,
5022 "lpfc_worker_%d", phba->brd_no);
5023 if (IS_ERR(phba->worker_thread)) {
5024 error = PTR_ERR(phba->worker_thread);
5025 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005026 }
5027
James Smart3772a992009-05-22 14:50:54 -04005028 return 0;
5029}
5030
5031/**
5032 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
5033 * @phba: pointer to lpfc hba data structure.
5034 *
5035 * This routine is invoked to unset the driver internal resources set up after
5036 * the device specific resource setup for supporting the HBA device it
5037 * attached to.
5038 **/
5039static void
5040lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
5041{
5042 /* Stop kernel worker thread */
5043 kthread_stop(phba->worker_thread);
5044}
5045
5046/**
5047 * lpfc_free_iocb_list - Free iocb list.
5048 * @phba: pointer to lpfc hba data structure.
5049 *
5050 * This routine is invoked to free the driver's IOCB list and memory.
5051 **/
5052static void
5053lpfc_free_iocb_list(struct lpfc_hba *phba)
5054{
5055 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
5056
5057 spin_lock_irq(&phba->hbalock);
5058 list_for_each_entry_safe(iocbq_entry, iocbq_next,
5059 &phba->lpfc_iocb_list, list) {
5060 list_del(&iocbq_entry->list);
5061 kfree(iocbq_entry);
5062 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005063 }
James Smart3772a992009-05-22 14:50:54 -04005064 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005065
James Smart3772a992009-05-22 14:50:54 -04005066 return;
5067}
dea31012005-04-17 16:05:31 -05005068
James Smart3772a992009-05-22 14:50:54 -04005069/**
5070 * lpfc_init_iocb_list - Allocate and initialize iocb list.
5071 * @phba: pointer to lpfc hba data structure.
5072 *
5073 * This routine is invoked to allocate and initizlize the driver's IOCB
5074 * list and set up the IOCB tag array accordingly.
5075 *
5076 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005077 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005078 * other values - error
5079 **/
5080static int
5081lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
5082{
5083 struct lpfc_iocbq *iocbq_entry = NULL;
5084 uint16_t iotag;
5085 int i;
dea31012005-04-17 16:05:31 -05005086
5087 /* Initialize and populate the iocb list per host. */
5088 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04005089 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07005090 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05005091 if (iocbq_entry == NULL) {
5092 printk(KERN_ERR "%s: only allocated %d iocbs of "
5093 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07005094 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05005095 goto out_free_iocbq;
5096 }
5097
James Bottomley604a3e32005-10-29 10:28:33 -05005098 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
5099 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04005100 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05005101 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04005102 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05005103 goto out_free_iocbq;
5104 }
James Smart6d368e52011-05-24 11:44:12 -04005105 iocbq_entry->sli4_lxritag = NO_XRI;
James Smart3772a992009-05-22 14:50:54 -04005106 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05005107
5108 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005109 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
5110 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05005111 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005112 }
5113
James Smart3772a992009-05-22 14:50:54 -04005114 return 0;
5115
5116out_free_iocbq:
5117 lpfc_free_iocb_list(phba);
5118
5119 return -ENOMEM;
5120}
5121
5122/**
James Smart8a9d2e82012-05-09 21:16:12 -04005123 * lpfc_free_sgl_list - Free a given sgl list.
James Smartda0436e2009-05-22 14:51:39 -04005124 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -04005125 * @sglq_list: pointer to the head of sgl list.
James Smartda0436e2009-05-22 14:51:39 -04005126 *
James Smart8a9d2e82012-05-09 21:16:12 -04005127 * This routine is invoked to free a give sgl list and memory.
James Smartda0436e2009-05-22 14:51:39 -04005128 **/
James Smart8a9d2e82012-05-09 21:16:12 -04005129void
5130lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
James Smartda0436e2009-05-22 14:51:39 -04005131{
5132 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart8a9d2e82012-05-09 21:16:12 -04005133
5134 list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
5135 list_del(&sglq_entry->list);
5136 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
5137 kfree(sglq_entry);
5138 }
5139}
5140
5141/**
5142 * lpfc_free_els_sgl_list - Free els sgl list.
5143 * @phba: pointer to lpfc hba data structure.
5144 *
5145 * This routine is invoked to free the driver's els sgl list and memory.
5146 **/
5147static void
5148lpfc_free_els_sgl_list(struct lpfc_hba *phba)
5149{
James Smartda0436e2009-05-22 14:51:39 -04005150 LIST_HEAD(sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04005151
James Smart8a9d2e82012-05-09 21:16:12 -04005152 /* Retrieve all els sgls from driver list */
James Smartda0436e2009-05-22 14:51:39 -04005153 spin_lock_irq(&phba->hbalock);
5154 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
5155 spin_unlock_irq(&phba->hbalock);
5156
James Smart8a9d2e82012-05-09 21:16:12 -04005157 /* Now free the sgl list */
5158 lpfc_free_sgl_list(phba, &sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04005159}
5160
5161/**
5162 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
5163 * @phba: pointer to lpfc hba data structure.
5164 *
5165 * This routine is invoked to allocate the driver's active sgl memory.
5166 * This array will hold the sglq_entry's for active IOs.
5167 **/
5168static int
5169lpfc_init_active_sgl_array(struct lpfc_hba *phba)
5170{
5171 int size;
5172 size = sizeof(struct lpfc_sglq *);
5173 size *= phba->sli4_hba.max_cfg_param.max_xri;
5174
5175 phba->sli4_hba.lpfc_sglq_active_list =
5176 kzalloc(size, GFP_KERNEL);
5177 if (!phba->sli4_hba.lpfc_sglq_active_list)
5178 return -ENOMEM;
5179 return 0;
5180}
5181
5182/**
5183 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
5184 * @phba: pointer to lpfc hba data structure.
5185 *
5186 * This routine is invoked to walk through the array of active sglq entries
5187 * and free all of the resources.
5188 * This is just a place holder for now.
5189 **/
5190static void
5191lpfc_free_active_sgl(struct lpfc_hba *phba)
5192{
5193 kfree(phba->sli4_hba.lpfc_sglq_active_list);
5194}
5195
5196/**
5197 * lpfc_init_sgl_list - Allocate and initialize sgl list.
5198 * @phba: pointer to lpfc hba data structure.
5199 *
5200 * This routine is invoked to allocate and initizlize the driver's sgl
5201 * list and set up the sgl xritag tag array accordingly.
5202 *
James Smartda0436e2009-05-22 14:51:39 -04005203 **/
James Smart8a9d2e82012-05-09 21:16:12 -04005204static void
James Smartda0436e2009-05-22 14:51:39 -04005205lpfc_init_sgl_list(struct lpfc_hba *phba)
5206{
James Smartda0436e2009-05-22 14:51:39 -04005207 /* Initialize and populate the sglq list per host/VF. */
5208 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
5209 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
5210
James Smart8a9d2e82012-05-09 21:16:12 -04005211 /* els xri-sgl book keeping */
5212 phba->sli4_hba.els_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04005213
James Smart8a9d2e82012-05-09 21:16:12 -04005214 /* scsi xri-buffer book keeping */
James Smartda0436e2009-05-22 14:51:39 -04005215 phba->sli4_hba.scsi_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04005216}
5217
5218/**
5219 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
5220 * @phba: pointer to lpfc hba data structure.
5221 *
5222 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -04005223 * port for those SLI4 ports that do not support extents. This routine
James Smartda0436e2009-05-22 14:51:39 -04005224 * posts a PAGE_SIZE memory region to the port to hold up to
James Smart88a2cfb2011-07-22 18:36:33 -04005225 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
5226 * and should be called only when interrupts are disabled.
James Smartda0436e2009-05-22 14:51:39 -04005227 *
5228 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005229 * 0 - successful
James Smart88a2cfb2011-07-22 18:36:33 -04005230 * -ERROR - otherwise.
James Smartda0436e2009-05-22 14:51:39 -04005231 **/
5232int
5233lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
5234{
5235 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04005236 struct lpfc_rpi_hdr *rpi_hdr;
5237
5238 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
James Smartff78d8f2011-12-13 13:21:35 -05005239 if (!phba->sli4_hba.rpi_hdrs_in_use)
James Smart6d368e52011-05-24 11:44:12 -04005240 return rc;
James Smart6d368e52011-05-24 11:44:12 -04005241 if (phba->sli4_hba.extents_in_use)
5242 return -EIO;
James Smartda0436e2009-05-22 14:51:39 -04005243
5244 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
5245 if (!rpi_hdr) {
5246 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5247 "0391 Error during rpi post operation\n");
5248 lpfc_sli4_remove_rpis(phba);
5249 rc = -ENODEV;
5250 }
5251
5252 return rc;
5253}
5254
5255/**
5256 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
5257 * @phba: pointer to lpfc hba data structure.
5258 *
5259 * This routine is invoked to allocate a single 4KB memory region to
5260 * support rpis and stores them in the phba. This single region
5261 * provides support for up to 64 rpis. The region is used globally
5262 * by the device.
5263 *
5264 * Returns:
5265 * A valid rpi hdr on success.
5266 * A NULL pointer on any failure.
5267 **/
5268struct lpfc_rpi_hdr *
5269lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
5270{
5271 uint16_t rpi_limit, curr_rpi_range;
5272 struct lpfc_dmabuf *dmabuf;
5273 struct lpfc_rpi_hdr *rpi_hdr;
James Smart9589b0622011-04-16 11:03:17 -04005274 uint32_t rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04005275
James Smart6d368e52011-05-24 11:44:12 -04005276 /*
5277 * If the SLI4 port supports extents, posting the rpi header isn't
5278 * required. Set the expected maximum count and let the actual value
5279 * get set when extents are fully allocated.
5280 */
5281 if (!phba->sli4_hba.rpi_hdrs_in_use)
5282 return NULL;
5283 if (phba->sli4_hba.extents_in_use)
5284 return NULL;
5285
5286 /* The limit on the logical index is just the max_rpi count. */
James Smartda0436e2009-05-22 14:51:39 -04005287 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
James Smart6d368e52011-05-24 11:44:12 -04005288 phba->sli4_hba.max_cfg_param.max_rpi - 1;
James Smartda0436e2009-05-22 14:51:39 -04005289
5290 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04005291 /*
5292 * Establish the starting RPI in this header block. The starting
5293 * rpi is normalized to a zero base because the physical rpi is
5294 * port based.
5295 */
James Smart97f2ecf2012-03-01 22:35:23 -05005296 curr_rpi_range = phba->sli4_hba.next_rpi;
James Smartda0436e2009-05-22 14:51:39 -04005297 spin_unlock_irq(&phba->hbalock);
5298
5299 /*
5300 * The port has a limited number of rpis. The increment here
5301 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
5302 * and to allow the full max_rpi range per port.
5303 */
5304 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
James Smart9589b0622011-04-16 11:03:17 -04005305 rpi_count = rpi_limit - curr_rpi_range;
5306 else
5307 rpi_count = LPFC_RPI_HDR_COUNT;
James Smartda0436e2009-05-22 14:51:39 -04005308
James Smart6d368e52011-05-24 11:44:12 -04005309 if (!rpi_count)
5310 return NULL;
James Smartda0436e2009-05-22 14:51:39 -04005311 /*
5312 * First allocate the protocol header region for the port. The
5313 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
5314 */
5315 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5316 if (!dmabuf)
5317 return NULL;
5318
5319 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5320 LPFC_HDR_TEMPLATE_SIZE,
5321 &dmabuf->phys,
5322 GFP_KERNEL);
5323 if (!dmabuf->virt) {
5324 rpi_hdr = NULL;
5325 goto err_free_dmabuf;
5326 }
5327
5328 memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
5329 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
5330 rpi_hdr = NULL;
5331 goto err_free_coherent;
5332 }
5333
5334 /* Save the rpi header data for cleanup later. */
5335 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
5336 if (!rpi_hdr)
5337 goto err_free_coherent;
5338
5339 rpi_hdr->dmabuf = dmabuf;
5340 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
5341 rpi_hdr->page_count = 1;
5342 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04005343
5344 /* The rpi_hdr stores the logical index only. */
5345 rpi_hdr->start_rpi = curr_rpi_range;
James Smartda0436e2009-05-22 14:51:39 -04005346 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
5347
5348 /*
James Smart6d368e52011-05-24 11:44:12 -04005349 * The next_rpi stores the next logical module-64 rpi value used
5350 * to post physical rpis in subsequent rpi postings.
James Smartda0436e2009-05-22 14:51:39 -04005351 */
James Smart9589b0622011-04-16 11:03:17 -04005352 phba->sli4_hba.next_rpi += rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04005353 spin_unlock_irq(&phba->hbalock);
5354 return rpi_hdr;
5355
5356 err_free_coherent:
5357 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
5358 dmabuf->virt, dmabuf->phys);
5359 err_free_dmabuf:
5360 kfree(dmabuf);
5361 return NULL;
5362}
5363
5364/**
5365 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
5366 * @phba: pointer to lpfc hba data structure.
5367 *
5368 * This routine is invoked to remove all memory resources allocated
James Smart6d368e52011-05-24 11:44:12 -04005369 * to support rpis for SLI4 ports not supporting extents. This routine
5370 * presumes the caller has released all rpis consumed by fabric or port
5371 * logins and is prepared to have the header pages removed.
James Smartda0436e2009-05-22 14:51:39 -04005372 **/
5373void
5374lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
5375{
5376 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
5377
James Smart6d368e52011-05-24 11:44:12 -04005378 if (!phba->sli4_hba.rpi_hdrs_in_use)
5379 goto exit;
5380
James Smartda0436e2009-05-22 14:51:39 -04005381 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
5382 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
5383 list_del(&rpi_hdr->list);
5384 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
5385 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
5386 kfree(rpi_hdr->dmabuf);
5387 kfree(rpi_hdr);
5388 }
James Smart6d368e52011-05-24 11:44:12 -04005389 exit:
5390 /* There are no rpis available to the port now. */
5391 phba->sli4_hba.next_rpi = 0;
James Smartda0436e2009-05-22 14:51:39 -04005392}
5393
5394/**
James Smart3772a992009-05-22 14:50:54 -04005395 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
5396 * @pdev: pointer to pci device data structure.
5397 *
5398 * This routine is invoked to allocate the driver hba data structure for an
5399 * HBA device. If the allocation is successful, the phba reference to the
5400 * PCI device data structure is set.
5401 *
5402 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005403 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04005404 * NULL - error
5405 **/
5406static struct lpfc_hba *
5407lpfc_hba_alloc(struct pci_dev *pdev)
5408{
5409 struct lpfc_hba *phba;
5410
5411 /* Allocate memory for HBA structure */
5412 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
5413 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02005414 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04005415 return NULL;
5416 }
5417
5418 /* Set reference to PCI device in HBA structure */
5419 phba->pcidev = pdev;
5420
5421 /* Assign an unused board number */
5422 phba->brd_no = lpfc_get_instance();
5423 if (phba->brd_no < 0) {
5424 kfree(phba);
5425 return NULL;
5426 }
5427
James Smart4fede782010-01-26 23:08:55 -05005428 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04005429 INIT_LIST_HEAD(&phba->ct_ev_waiters);
5430
James Smart3772a992009-05-22 14:50:54 -04005431 return phba;
5432}
5433
5434/**
5435 * lpfc_hba_free - Free driver hba data structure with a device.
5436 * @phba: pointer to lpfc hba data structure.
5437 *
5438 * This routine is invoked to free the driver hba data structure with an
5439 * HBA device.
5440 **/
5441static void
5442lpfc_hba_free(struct lpfc_hba *phba)
5443{
5444 /* Release the driver assigned board number */
5445 idr_remove(&lpfc_hba_index, phba->brd_no);
5446
5447 kfree(phba);
5448 return;
5449}
5450
5451/**
5452 * lpfc_create_shost - Create hba physical port with associated scsi host.
5453 * @phba: pointer to lpfc hba data structure.
5454 *
5455 * This routine is invoked to create HBA physical port and associate a SCSI
5456 * host with it.
5457 *
5458 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005459 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005460 * other values - error
5461 **/
5462static int
5463lpfc_create_shost(struct lpfc_hba *phba)
5464{
5465 struct lpfc_vport *vport;
5466 struct Scsi_Host *shost;
5467
5468 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05005469 phba->fc_edtov = FF_DEF_EDTOV;
5470 phba->fc_ratov = FF_DEF_RATOV;
5471 phba->fc_altov = FF_DEF_ALTOV;
5472 phba->fc_arbtov = FF_DEF_ARBTOV;
5473
James Smartd7c47992010-06-08 18:31:54 -04005474 atomic_set(&phba->sdev_cnt, 0);
James Smart3de2a652007-08-02 11:09:59 -04005475 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05005476 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04005477 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05005478
5479 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05005480 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05005481 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04005482 /* Put reference to SCSI host to driver's device private data */
5483 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05005484
James Smart3772a992009-05-22 14:50:54 -04005485 return 0;
5486}
dea31012005-04-17 16:05:31 -05005487
James Smart3772a992009-05-22 14:50:54 -04005488/**
5489 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
5490 * @phba: pointer to lpfc hba data structure.
5491 *
5492 * This routine is invoked to destroy HBA physical port and the associated
5493 * SCSI host.
5494 **/
5495static void
5496lpfc_destroy_shost(struct lpfc_hba *phba)
5497{
5498 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04005499
James Smart3772a992009-05-22 14:50:54 -04005500 /* Destroy physical port that associated with the SCSI host */
5501 destroy_port(vport);
5502
5503 return;
5504}
5505
5506/**
5507 * lpfc_setup_bg - Setup Block guard structures and debug areas.
5508 * @phba: pointer to lpfc hba data structure.
5509 * @shost: the shost to be used to detect Block guard settings.
5510 *
5511 * This routine sets up the local Block guard protocol settings for @shost.
5512 * This routine also allocates memory for debugging bg buffers.
5513 **/
5514static void
5515lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
5516{
James Smartbbeb79b2012-06-12 13:54:27 -04005517 uint32_t old_mask;
5518 uint32_t old_guard;
5519
James Smart3772a992009-05-22 14:50:54 -04005520 int pagecnt = 10;
5521 if (lpfc_prot_mask && lpfc_prot_guard) {
5522 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5523 "1478 Registering BlockGuard with the "
5524 "SCSI layer\n");
James Smartbbeb79b2012-06-12 13:54:27 -04005525
5526 old_mask = lpfc_prot_mask;
5527 old_guard = lpfc_prot_guard;
5528
5529 /* Only allow supported values */
5530 lpfc_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
5531 SHOST_DIX_TYPE0_PROTECTION |
5532 SHOST_DIX_TYPE1_PROTECTION);
5533 lpfc_prot_guard &= (SHOST_DIX_GUARD_IP | SHOST_DIX_GUARD_CRC);
5534
5535 /* DIF Type 1 protection for profiles AST1/C1 is end to end */
5536 if (lpfc_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
5537 lpfc_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
5538
5539 if (lpfc_prot_mask && lpfc_prot_guard) {
5540 if ((old_mask != lpfc_prot_mask) ||
5541 (old_guard != lpfc_prot_guard))
5542 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5543 "1475 Registering BlockGuard with the "
5544 "SCSI layer: mask %d guard %d\n",
5545 lpfc_prot_mask, lpfc_prot_guard);
5546
5547 scsi_host_set_prot(shost, lpfc_prot_mask);
5548 scsi_host_set_guard(shost, lpfc_prot_guard);
5549 } else
5550 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5551 "1479 Not Registering BlockGuard with the SCSI "
5552 "layer, Bad protection parameters: %d %d\n",
5553 old_mask, old_guard);
James Smart98c9ea52007-10-27 13:37:33 -04005554 }
James Smartbbeb79b2012-06-12 13:54:27 -04005555
James Smart81301a92008-12-04 22:39:46 -05005556 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05005557 while (pagecnt) {
5558 spin_lock_init(&_dump_buf_lock);
5559 _dump_buf_data =
5560 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
5561 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04005562 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5563 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04005564 "_dump_buf_data at 0x%p\n",
5565 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05005566 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04005567 memset(_dump_buf_data, 0,
5568 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05005569 break;
James Smart3772a992009-05-22 14:50:54 -04005570 } else
James Smart81301a92008-12-04 22:39:46 -05005571 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05005572 }
James Smart81301a92008-12-04 22:39:46 -05005573 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04005574 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5575 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04005576 "memory for hexdump\n");
5577 } else
James Smart6a9c52c2009-10-02 15:16:51 -04005578 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5579 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05005580 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05005581 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05005582 while (pagecnt) {
5583 _dump_buf_dif =
5584 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
5585 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04005586 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5587 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04005588 "_dump_buf_dif at 0x%p\n",
5589 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05005590 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04005591 memset(_dump_buf_dif, 0,
5592 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05005593 break;
James Smart3772a992009-05-22 14:50:54 -04005594 } else
James Smart81301a92008-12-04 22:39:46 -05005595 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05005596 }
James Smart81301a92008-12-04 22:39:46 -05005597 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04005598 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5599 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04005600 "memory for hexdump\n");
5601 } else
James Smart6a9c52c2009-10-02 15:16:51 -04005602 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5603 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04005604 _dump_buf_dif);
5605}
5606
5607/**
5608 * lpfc_post_init_setup - Perform necessary device post initialization setup.
5609 * @phba: pointer to lpfc hba data structure.
5610 *
5611 * This routine is invoked to perform all the necessary post initialization
5612 * setup for the device.
5613 **/
5614static void
5615lpfc_post_init_setup(struct lpfc_hba *phba)
5616{
5617 struct Scsi_Host *shost;
5618 struct lpfc_adapter_event_header adapter_event;
5619
5620 /* Get the default values for Model Name and Description */
5621 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
5622
5623 /*
5624 * hba setup may have changed the hba_queue_depth so we need to
5625 * adjust the value of can_queue.
5626 */
5627 shost = pci_get_drvdata(phba->pcidev);
5628 shost->can_queue = phba->cfg_hba_queue_depth - 10;
5629 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
5630 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05005631
5632 lpfc_host_attrib_init(shost);
5633
James Smart2e0fef82007-06-17 19:56:36 -05005634 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
5635 spin_lock_irq(shost->host_lock);
5636 lpfc_poll_start_timer(phba);
5637 spin_unlock_irq(shost->host_lock);
5638 }
James Smart8f6d98d2006-08-01 07:34:00 -04005639
James Smart93996272008-08-24 21:50:30 -04005640 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5641 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04005642 /* Send board arrival event to upper layer */
5643 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
5644 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
5645 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04005646 sizeof(adapter_event),
5647 (char *) &adapter_event,
5648 LPFC_NL_VENDOR_ID);
5649 return;
5650}
5651
5652/**
5653 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
5654 * @phba: pointer to lpfc hba data structure.
5655 *
5656 * This routine is invoked to set up the PCI device memory space for device
5657 * with SLI-3 interface spec.
5658 *
5659 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005660 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005661 * other values - error
5662 **/
5663static int
5664lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
5665{
5666 struct pci_dev *pdev;
5667 unsigned long bar0map_len, bar2map_len;
5668 int i, hbq_count;
5669 void *ptr;
5670 int error = -ENODEV;
5671
5672 /* Obtain PCI device reference */
5673 if (!phba->pcidev)
5674 return error;
5675 else
5676 pdev = phba->pcidev;
5677
5678 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05005679 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
5680 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
5681 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
5682 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smart3772a992009-05-22 14:50:54 -04005683 return error;
Michael Reed8e685972009-09-18 12:02:05 -05005684 }
5685 }
James Smart3772a992009-05-22 14:50:54 -04005686
5687 /* Get the bus address of Bar0 and Bar2 and the number of bytes
5688 * required by each mapping.
5689 */
5690 phba->pci_bar0_map = pci_resource_start(pdev, 0);
5691 bar0map_len = pci_resource_len(pdev, 0);
5692
5693 phba->pci_bar2_map = pci_resource_start(pdev, 2);
5694 bar2map_len = pci_resource_len(pdev, 2);
5695
5696 /* Map HBA SLIM to a kernel virtual address. */
5697 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
5698 if (!phba->slim_memmap_p) {
5699 dev_printk(KERN_ERR, &pdev->dev,
5700 "ioremap failed for SLIM memory.\n");
5701 goto out;
5702 }
5703
5704 /* Map HBA Control Registers to a kernel virtual address. */
5705 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
5706 if (!phba->ctrl_regs_memmap_p) {
5707 dev_printk(KERN_ERR, &pdev->dev,
5708 "ioremap failed for HBA control registers.\n");
5709 goto out_iounmap_slim;
5710 }
5711
5712 /* Allocate memory for SLI-2 structures */
5713 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
5714 SLI2_SLIM_SIZE,
5715 &phba->slim2p.phys,
5716 GFP_KERNEL);
5717 if (!phba->slim2p.virt)
5718 goto out_iounmap;
5719
5720 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
5721 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04005722 phba->mbox_ext = (phba->slim2p.virt +
5723 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04005724 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
5725 phba->IOCBs = (phba->slim2p.virt +
5726 offsetof(struct lpfc_sli2_slim, IOCBs));
5727
5728 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
5729 lpfc_sli_hbq_size(),
5730 &phba->hbqslimp.phys,
5731 GFP_KERNEL);
5732 if (!phba->hbqslimp.virt)
5733 goto out_free_slim;
5734
5735 hbq_count = lpfc_sli_hbq_count();
5736 ptr = phba->hbqslimp.virt;
5737 for (i = 0; i < hbq_count; ++i) {
5738 phba->hbqs[i].hbq_virt = ptr;
5739 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
5740 ptr += (lpfc_hbq_defs[i]->entry_count *
5741 sizeof(struct lpfc_hbq_entry));
5742 }
5743 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
5744 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
5745
5746 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
5747
5748 INIT_LIST_HEAD(&phba->rb_pend_list);
5749
5750 phba->MBslimaddr = phba->slim_memmap_p;
5751 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
5752 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
5753 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
5754 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04005755
dea31012005-04-17 16:05:31 -05005756 return 0;
5757
dea31012005-04-17 16:05:31 -05005758out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04005759 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
5760 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05005761out_iounmap:
5762 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005763out_iounmap_slim:
dea31012005-04-17 16:05:31 -05005764 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05005765out:
5766 return error;
5767}
5768
James Smarte59058c2008-08-24 21:49:00 -04005769/**
James Smart3772a992009-05-22 14:50:54 -04005770 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
5771 * @phba: pointer to lpfc hba data structure.
5772 *
5773 * This routine is invoked to unset the PCI device memory space for device
5774 * with SLI-3 interface spec.
5775 **/
5776static void
5777lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
5778{
5779 struct pci_dev *pdev;
5780
5781 /* Obtain PCI device reference */
5782 if (!phba->pcidev)
5783 return;
5784 else
5785 pdev = phba->pcidev;
5786
5787 /* Free coherent DMA memory allocated */
5788 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
5789 phba->hbqslimp.virt, phba->hbqslimp.phys);
5790 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
5791 phba->slim2p.virt, phba->slim2p.phys);
5792
5793 /* I/O memory unmap */
5794 iounmap(phba->ctrl_regs_memmap_p);
5795 iounmap(phba->slim_memmap_p);
5796
5797 return;
5798}
5799
5800/**
James Smartda0436e2009-05-22 14:51:39 -04005801 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
5802 * @phba: pointer to lpfc hba data structure.
5803 *
5804 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
5805 * done and check status.
5806 *
5807 * Return 0 if successful, otherwise -ENODEV.
5808 **/
5809int
5810lpfc_sli4_post_status_check(struct lpfc_hba *phba)
5811{
James Smart2fcee4b2010-12-15 17:57:46 -05005812 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
5813 struct lpfc_register reg_data;
5814 int i, port_error = 0;
5815 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04005816
James Smart9940b972011-03-11 16:06:12 -05005817 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
5818 memset(&reg_data, 0, sizeof(reg_data));
James Smart2fcee4b2010-12-15 17:57:46 -05005819 if (!phba->sli4_hba.PSMPHRregaddr)
James Smartda0436e2009-05-22 14:51:39 -04005820 return -ENODEV;
5821
James Smartda0436e2009-05-22 14:51:39 -04005822 /* Wait up to 30 seconds for the SLI Port POST done and ready */
5823 for (i = 0; i < 3000; i++) {
James Smart9940b972011-03-11 16:06:12 -05005824 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
5825 &portsmphr_reg.word0) ||
5826 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
James Smart2fcee4b2010-12-15 17:57:46 -05005827 /* Port has a fatal POST error, break out */
James Smartda0436e2009-05-22 14:51:39 -04005828 port_error = -ENODEV;
5829 break;
5830 }
James Smart2fcee4b2010-12-15 17:57:46 -05005831 if (LPFC_POST_STAGE_PORT_READY ==
5832 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
James Smartda0436e2009-05-22 14:51:39 -04005833 break;
James Smartda0436e2009-05-22 14:51:39 -04005834 msleep(10);
5835 }
5836
James Smart2fcee4b2010-12-15 17:57:46 -05005837 /*
5838 * If there was a port error during POST, then don't proceed with
5839 * other register reads as the data may not be valid. Just exit.
5840 */
5841 if (port_error) {
James Smartda0436e2009-05-22 14:51:39 -04005842 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05005843 "1408 Port Failed POST - portsmphr=0x%x, "
5844 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
5845 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
5846 portsmphr_reg.word0,
5847 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
5848 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
5849 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
5850 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
5851 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
5852 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
5853 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
5854 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
5855 } else {
James Smart28baac72010-02-12 14:42:03 -05005856 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05005857 "2534 Device Info: SLIFamily=0x%x, "
5858 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
5859 "SLIHint_2=0x%x, FT=0x%x\n",
James Smart28baac72010-02-12 14:42:03 -05005860 bf_get(lpfc_sli_intf_sli_family,
5861 &phba->sli4_hba.sli_intf),
5862 bf_get(lpfc_sli_intf_slirev,
5863 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05005864 bf_get(lpfc_sli_intf_if_type,
James Smart28baac72010-02-12 14:42:03 -05005865 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05005866 bf_get(lpfc_sli_intf_sli_hint1,
5867 &phba->sli4_hba.sli_intf),
5868 bf_get(lpfc_sli_intf_sli_hint2,
5869 &phba->sli4_hba.sli_intf),
5870 bf_get(lpfc_sli_intf_func_type,
James Smart28baac72010-02-12 14:42:03 -05005871 &phba->sli4_hba.sli_intf));
James Smart2fcee4b2010-12-15 17:57:46 -05005872 /*
5873 * Check for other Port errors during the initialization
5874 * process. Fail the load if the port did not come up
5875 * correctly.
5876 */
5877 if_type = bf_get(lpfc_sli_intf_if_type,
5878 &phba->sli4_hba.sli_intf);
5879 switch (if_type) {
5880 case LPFC_SLI_INTF_IF_TYPE_0:
5881 phba->sli4_hba.ue_mask_lo =
5882 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
5883 phba->sli4_hba.ue_mask_hi =
5884 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
5885 uerrlo_reg.word0 =
5886 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
5887 uerrhi_reg.word0 =
5888 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
5889 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
5890 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
5891 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5892 "1422 Unrecoverable Error "
5893 "Detected during POST "
5894 "uerr_lo_reg=0x%x, "
5895 "uerr_hi_reg=0x%x, "
5896 "ue_mask_lo_reg=0x%x, "
5897 "ue_mask_hi_reg=0x%x\n",
5898 uerrlo_reg.word0,
5899 uerrhi_reg.word0,
5900 phba->sli4_hba.ue_mask_lo,
5901 phba->sli4_hba.ue_mask_hi);
5902 port_error = -ENODEV;
5903 }
5904 break;
5905 case LPFC_SLI_INTF_IF_TYPE_2:
5906 /* Final checks. The port status should be clean. */
James Smart9940b972011-03-11 16:06:12 -05005907 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
5908 &reg_data.word0) ||
James Smart05580562011-05-24 11:40:48 -04005909 (bf_get(lpfc_sliport_status_err, &reg_data) &&
5910 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
James Smart2fcee4b2010-12-15 17:57:46 -05005911 phba->work_status[0] =
5912 readl(phba->sli4_hba.u.if_type2.
5913 ERR1regaddr);
5914 phba->work_status[1] =
5915 readl(phba->sli4_hba.u.if_type2.
5916 ERR2regaddr);
5917 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05005918 "2888 Unrecoverable port error "
5919 "following POST: port status reg "
5920 "0x%x, port_smphr reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05005921 "error 1=0x%x, error 2=0x%x\n",
5922 reg_data.word0,
5923 portsmphr_reg.word0,
5924 phba->work_status[0],
5925 phba->work_status[1]);
5926 port_error = -ENODEV;
5927 }
5928 break;
5929 case LPFC_SLI_INTF_IF_TYPE_1:
5930 default:
5931 break;
5932 }
James Smart28baac72010-02-12 14:42:03 -05005933 }
James Smartda0436e2009-05-22 14:51:39 -04005934 return port_error;
5935}
5936
5937/**
5938 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
5939 * @phba: pointer to lpfc hba data structure.
James Smart2fcee4b2010-12-15 17:57:46 -05005940 * @if_type: The SLI4 interface type getting configured.
James Smartda0436e2009-05-22 14:51:39 -04005941 *
5942 * This routine is invoked to set up SLI4 BAR0 PCI config space register
5943 * memory map.
5944 **/
5945static void
James Smart2fcee4b2010-12-15 17:57:46 -05005946lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04005947{
James Smart2fcee4b2010-12-15 17:57:46 -05005948 switch (if_type) {
5949 case LPFC_SLI_INTF_IF_TYPE_0:
5950 phba->sli4_hba.u.if_type0.UERRLOregaddr =
5951 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
5952 phba->sli4_hba.u.if_type0.UERRHIregaddr =
5953 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
5954 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
5955 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
5956 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
5957 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
5958 phba->sli4_hba.SLIINTFregaddr =
5959 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
5960 break;
5961 case LPFC_SLI_INTF_IF_TYPE_2:
5962 phba->sli4_hba.u.if_type2.ERR1regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04005963 phba->sli4_hba.conf_regs_memmap_p +
5964 LPFC_CTL_PORT_ER1_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05005965 phba->sli4_hba.u.if_type2.ERR2regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04005966 phba->sli4_hba.conf_regs_memmap_p +
5967 LPFC_CTL_PORT_ER2_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05005968 phba->sli4_hba.u.if_type2.CTRLregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04005969 phba->sli4_hba.conf_regs_memmap_p +
5970 LPFC_CTL_PORT_CTL_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05005971 phba->sli4_hba.u.if_type2.STATUSregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04005972 phba->sli4_hba.conf_regs_memmap_p +
5973 LPFC_CTL_PORT_STA_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05005974 phba->sli4_hba.SLIINTFregaddr =
5975 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
5976 phba->sli4_hba.PSMPHRregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04005977 phba->sli4_hba.conf_regs_memmap_p +
5978 LPFC_CTL_PORT_SEM_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05005979 phba->sli4_hba.RQDBregaddr =
5980 phba->sli4_hba.conf_regs_memmap_p + LPFC_RQ_DOORBELL;
5981 phba->sli4_hba.WQDBregaddr =
5982 phba->sli4_hba.conf_regs_memmap_p + LPFC_WQ_DOORBELL;
5983 phba->sli4_hba.EQCQDBregaddr =
5984 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
5985 phba->sli4_hba.MQDBregaddr =
5986 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
5987 phba->sli4_hba.BMBXregaddr =
5988 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
5989 break;
5990 case LPFC_SLI_INTF_IF_TYPE_1:
5991 default:
5992 dev_printk(KERN_ERR, &phba->pcidev->dev,
5993 "FATAL - unsupported SLI4 interface type - %d\n",
5994 if_type);
5995 break;
5996 }
James Smartda0436e2009-05-22 14:51:39 -04005997}
5998
5999/**
6000 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
6001 * @phba: pointer to lpfc hba data structure.
6002 *
6003 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
6004 * memory map.
6005 **/
6006static void
6007lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
6008{
James Smart2fcee4b2010-12-15 17:57:46 -05006009 phba->sli4_hba.PSMPHRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
6010 LPFC_SLIPORT_IF0_SMPHR;
James Smartda0436e2009-05-22 14:51:39 -04006011 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006012 LPFC_HST_ISR0;
James Smartda0436e2009-05-22 14:51:39 -04006013 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006014 LPFC_HST_IMR0;
James Smartda0436e2009-05-22 14:51:39 -04006015 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006016 LPFC_HST_ISCR0;
James Smartda0436e2009-05-22 14:51:39 -04006017}
6018
6019/**
6020 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
6021 * @phba: pointer to lpfc hba data structure.
6022 * @vf: virtual function number
6023 *
6024 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
6025 * based on the given viftual function number, @vf.
6026 *
6027 * Return 0 if successful, otherwise -ENODEV.
6028 **/
6029static int
6030lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
6031{
6032 if (vf > LPFC_VIR_FUNC_MAX)
6033 return -ENODEV;
6034
6035 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6036 vf * LPFC_VFR_PAGE_SIZE + LPFC_RQ_DOORBELL);
6037 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6038 vf * LPFC_VFR_PAGE_SIZE + LPFC_WQ_DOORBELL);
6039 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6040 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
6041 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6042 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
6043 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6044 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
6045 return 0;
6046}
6047
6048/**
6049 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
6050 * @phba: pointer to lpfc hba data structure.
6051 *
6052 * This routine is invoked to create the bootstrap mailbox
6053 * region consistent with the SLI-4 interface spec. This
6054 * routine allocates all memory necessary to communicate
6055 * mailbox commands to the port and sets up all alignment
6056 * needs. No locks are expected to be held when calling
6057 * this routine.
6058 *
6059 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006060 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04006061 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04006062 **/
6063static int
6064lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
6065{
6066 uint32_t bmbx_size;
6067 struct lpfc_dmabuf *dmabuf;
6068 struct dma_address *dma_address;
6069 uint32_t pa_addr;
6070 uint64_t phys_addr;
6071
6072 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
6073 if (!dmabuf)
6074 return -ENOMEM;
6075
6076 /*
6077 * The bootstrap mailbox region is comprised of 2 parts
6078 * plus an alignment restriction of 16 bytes.
6079 */
6080 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
6081 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
6082 bmbx_size,
6083 &dmabuf->phys,
6084 GFP_KERNEL);
6085 if (!dmabuf->virt) {
6086 kfree(dmabuf);
6087 return -ENOMEM;
6088 }
6089 memset(dmabuf->virt, 0, bmbx_size);
6090
6091 /*
6092 * Initialize the bootstrap mailbox pointers now so that the register
6093 * operations are simple later. The mailbox dma address is required
6094 * to be 16-byte aligned. Also align the virtual memory as each
6095 * maibox is copied into the bmbx mailbox region before issuing the
6096 * command to the port.
6097 */
6098 phba->sli4_hba.bmbx.dmabuf = dmabuf;
6099 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
6100
6101 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
6102 LPFC_ALIGN_16_BYTE);
6103 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
6104 LPFC_ALIGN_16_BYTE);
6105
6106 /*
6107 * Set the high and low physical addresses now. The SLI4 alignment
6108 * requirement is 16 bytes and the mailbox is posted to the port
6109 * as two 30-bit addresses. The other data is a bit marking whether
6110 * the 30-bit address is the high or low address.
6111 * Upcast bmbx aphys to 64bits so shift instruction compiles
6112 * clean on 32 bit machines.
6113 */
6114 dma_address = &phba->sli4_hba.bmbx.dma_address;
6115 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
6116 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
6117 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
6118 LPFC_BMBX_BIT1_ADDR_HI);
6119
6120 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
6121 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
6122 LPFC_BMBX_BIT1_ADDR_LO);
6123 return 0;
6124}
6125
6126/**
6127 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
6128 * @phba: pointer to lpfc hba data structure.
6129 *
6130 * This routine is invoked to teardown the bootstrap mailbox
6131 * region and release all host resources. This routine requires
6132 * the caller to ensure all mailbox commands recovered, no
6133 * additional mailbox comands are sent, and interrupts are disabled
6134 * before calling this routine.
6135 *
6136 **/
6137static void
6138lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
6139{
6140 dma_free_coherent(&phba->pcidev->dev,
6141 phba->sli4_hba.bmbx.bmbx_size,
6142 phba->sli4_hba.bmbx.dmabuf->virt,
6143 phba->sli4_hba.bmbx.dmabuf->phys);
6144
6145 kfree(phba->sli4_hba.bmbx.dmabuf);
6146 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
6147}
6148
6149/**
6150 * lpfc_sli4_read_config - Get the config parameters.
6151 * @phba: pointer to lpfc hba data structure.
6152 *
6153 * This routine is invoked to read the configuration parameters from the HBA.
6154 * The configuration parameters are used to set the base and maximum values
6155 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
6156 * allocation for the port.
6157 *
6158 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006159 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006160 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006161 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006162 **/
James Smartff78d8f2011-12-13 13:21:35 -05006163int
James Smartda0436e2009-05-22 14:51:39 -04006164lpfc_sli4_read_config(struct lpfc_hba *phba)
6165{
6166 LPFC_MBOXQ_t *pmb;
6167 struct lpfc_mbx_read_config *rd_config;
James Smart912e3ac2011-05-24 11:42:11 -04006168 union lpfc_sli4_cfg_shdr *shdr;
6169 uint32_t shdr_status, shdr_add_status;
6170 struct lpfc_mbx_get_func_cfg *get_func_cfg;
6171 struct lpfc_rsrc_desc_fcfcoe *desc;
6172 uint32_t desc_count;
6173 int length, i, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04006174
6175 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6176 if (!pmb) {
6177 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6178 "2011 Unable to allocate memory for issuing "
6179 "SLI_CONFIG_SPECIAL mailbox command\n");
6180 return -ENOMEM;
6181 }
6182
6183 lpfc_read_config(phba, pmb);
6184
6185 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6186 if (rc != MBX_SUCCESS) {
6187 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6188 "2012 Mailbox failed , mbxCmd x%x "
6189 "READ_CONFIG, mbxStatus x%x\n",
6190 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6191 bf_get(lpfc_mqe_status, &pmb->u.mqe));
6192 rc = -EIO;
6193 } else {
6194 rd_config = &pmb->u.mqe.un.rd_config;
James Smartff78d8f2011-12-13 13:21:35 -05006195 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
6196 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
6197 phba->sli4_hba.lnk_info.lnk_tp =
6198 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
6199 phba->sli4_hba.lnk_info.lnk_no =
6200 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
6201 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6202 "3081 lnk_type:%d, lnk_numb:%d\n",
6203 phba->sli4_hba.lnk_info.lnk_tp,
6204 phba->sli4_hba.lnk_info.lnk_no);
6205 } else
6206 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6207 "3082 Mailbox (x%x) returned ldv:x0\n",
6208 bf_get(lpfc_mqe_command, &pmb->u.mqe));
James Smart6d368e52011-05-24 11:44:12 -04006209 phba->sli4_hba.extents_in_use =
6210 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04006211 phba->sli4_hba.max_cfg_param.max_xri =
6212 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
6213 phba->sli4_hba.max_cfg_param.xri_base =
6214 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
6215 phba->sli4_hba.max_cfg_param.max_vpi =
6216 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
6217 phba->sli4_hba.max_cfg_param.vpi_base =
6218 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
6219 phba->sli4_hba.max_cfg_param.max_rpi =
6220 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
6221 phba->sli4_hba.max_cfg_param.rpi_base =
6222 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
6223 phba->sli4_hba.max_cfg_param.max_vfi =
6224 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
6225 phba->sli4_hba.max_cfg_param.vfi_base =
6226 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
6227 phba->sli4_hba.max_cfg_param.max_fcfi =
6228 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04006229 phba->sli4_hba.max_cfg_param.max_eq =
6230 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
6231 phba->sli4_hba.max_cfg_param.max_rq =
6232 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
6233 phba->sli4_hba.max_cfg_param.max_wq =
6234 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
6235 phba->sli4_hba.max_cfg_param.max_cq =
6236 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
6237 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
6238 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
6239 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
6240 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
James Smart5ffc2662009-11-18 15:39:44 -05006241 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
6242 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04006243 phba->max_vports = phba->max_vpi;
6244 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart6d368e52011-05-24 11:44:12 -04006245 "2003 cfg params Extents? %d "
6246 "XRI(B:%d M:%d), "
James Smartda0436e2009-05-22 14:51:39 -04006247 "VPI(B:%d M:%d) "
6248 "VFI(B:%d M:%d) "
6249 "RPI(B:%d M:%d) "
James Smart6d368e52011-05-24 11:44:12 -04006250 "FCFI(Count:%d)\n",
6251 phba->sli4_hba.extents_in_use,
James Smartda0436e2009-05-22 14:51:39 -04006252 phba->sli4_hba.max_cfg_param.xri_base,
6253 phba->sli4_hba.max_cfg_param.max_xri,
6254 phba->sli4_hba.max_cfg_param.vpi_base,
6255 phba->sli4_hba.max_cfg_param.max_vpi,
6256 phba->sli4_hba.max_cfg_param.vfi_base,
6257 phba->sli4_hba.max_cfg_param.max_vfi,
6258 phba->sli4_hba.max_cfg_param.rpi_base,
6259 phba->sli4_hba.max_cfg_param.max_rpi,
James Smartda0436e2009-05-22 14:51:39 -04006260 phba->sli4_hba.max_cfg_param.max_fcfi);
6261 }
James Smart912e3ac2011-05-24 11:42:11 -04006262
6263 if (rc)
6264 goto read_cfg_out;
James Smartda0436e2009-05-22 14:51:39 -04006265
6266 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smartb92938b2010-06-07 15:24:12 -04006267 if (phba->cfg_hba_queue_depth >
6268 (phba->sli4_hba.max_cfg_param.max_xri -
6269 lpfc_sli4_get_els_iocb_cnt(phba)))
James Smartda0436e2009-05-22 14:51:39 -04006270 phba->cfg_hba_queue_depth =
James Smartb92938b2010-06-07 15:24:12 -04006271 phba->sli4_hba.max_cfg_param.max_xri -
6272 lpfc_sli4_get_els_iocb_cnt(phba);
James Smart912e3ac2011-05-24 11:42:11 -04006273
6274 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
6275 LPFC_SLI_INTF_IF_TYPE_2)
6276 goto read_cfg_out;
6277
6278 /* get the pf# and vf# for SLI4 if_type 2 port */
6279 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
6280 sizeof(struct lpfc_sli4_cfg_mhdr));
6281 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
6282 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
6283 length, LPFC_SLI4_MBX_EMBED);
6284
6285 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6286 shdr = (union lpfc_sli4_cfg_shdr *)
6287 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
6288 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6289 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6290 if (rc || shdr_status || shdr_add_status) {
6291 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6292 "3026 Mailbox failed , mbxCmd x%x "
6293 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
6294 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6295 bf_get(lpfc_mqe_status, &pmb->u.mqe));
6296 rc = -EIO;
6297 goto read_cfg_out;
6298 }
6299
6300 /* search for fc_fcoe resrouce descriptor */
6301 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
6302 desc_count = get_func_cfg->func_cfg.rsrc_desc_count;
6303
6304 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
6305 desc = (struct lpfc_rsrc_desc_fcfcoe *)
6306 &get_func_cfg->func_cfg.desc[i];
6307 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
6308 bf_get(lpfc_rsrc_desc_pcie_type, desc)) {
6309 phba->sli4_hba.iov.pf_number =
6310 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
6311 phba->sli4_hba.iov.vf_number =
6312 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
6313 break;
6314 }
6315 }
6316
6317 if (i < LPFC_RSRC_DESC_MAX_NUM)
6318 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6319 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
6320 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
6321 phba->sli4_hba.iov.vf_number);
6322 else {
6323 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6324 "3028 GET_FUNCTION_CONFIG: failed to find "
6325 "Resrouce Descriptor:x%x\n",
6326 LPFC_RSRC_DESC_TYPE_FCFCOE);
6327 rc = -EIO;
6328 }
6329
6330read_cfg_out:
6331 mempool_free(pmb, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04006332 return rc;
6333}
6334
6335/**
James Smart2fcee4b2010-12-15 17:57:46 -05006336 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
James Smartda0436e2009-05-22 14:51:39 -04006337 * @phba: pointer to lpfc hba data structure.
6338 *
James Smart2fcee4b2010-12-15 17:57:46 -05006339 * This routine is invoked to setup the port-side endian order when
6340 * the port if_type is 0. This routine has no function for other
6341 * if_types.
James Smartda0436e2009-05-22 14:51:39 -04006342 *
6343 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006344 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006345 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006346 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006347 **/
6348static int
6349lpfc_setup_endian_order(struct lpfc_hba *phba)
6350{
6351 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05006352 uint32_t if_type, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04006353 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
6354 HOST_ENDIAN_HIGH_WORD1};
6355
James Smart2fcee4b2010-12-15 17:57:46 -05006356 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
6357 switch (if_type) {
6358 case LPFC_SLI_INTF_IF_TYPE_0:
6359 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6360 GFP_KERNEL);
6361 if (!mboxq) {
6362 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6363 "0492 Unable to allocate memory for "
6364 "issuing SLI_CONFIG_SPECIAL mailbox "
6365 "command\n");
6366 return -ENOMEM;
6367 }
James Smartda0436e2009-05-22 14:51:39 -04006368
James Smart2fcee4b2010-12-15 17:57:46 -05006369 /*
6370 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
6371 * two words to contain special data values and no other data.
6372 */
6373 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
6374 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
6375 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6376 if (rc != MBX_SUCCESS) {
6377 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6378 "0493 SLI_CONFIG_SPECIAL mailbox "
6379 "failed with status x%x\n",
6380 rc);
6381 rc = -EIO;
6382 }
6383 mempool_free(mboxq, phba->mbox_mem_pool);
6384 break;
6385 case LPFC_SLI_INTF_IF_TYPE_2:
6386 case LPFC_SLI_INTF_IF_TYPE_1:
6387 default:
6388 break;
James Smartda0436e2009-05-22 14:51:39 -04006389 }
James Smartda0436e2009-05-22 14:51:39 -04006390 return rc;
6391}
6392
6393/**
James Smart5350d872011-10-10 21:33:49 -04006394 * lpfc_sli4_queue_verify - Verify and update EQ and CQ counts
James Smartda0436e2009-05-22 14:51:39 -04006395 * @phba: pointer to lpfc hba data structure.
6396 *
James Smart5350d872011-10-10 21:33:49 -04006397 * This routine is invoked to check the user settable queue counts for EQs and
6398 * CQs. after this routine is called the counts will be set to valid values that
6399 * adhere to the constraints of the system's interrupt vectors and the port's
6400 * queue resources.
James Smartda0436e2009-05-22 14:51:39 -04006401 *
6402 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006403 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006404 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04006405 **/
6406static int
James Smart5350d872011-10-10 21:33:49 -04006407lpfc_sli4_queue_verify(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04006408{
James Smartda0436e2009-05-22 14:51:39 -04006409 int cfg_fcp_wq_count;
6410 int cfg_fcp_eq_count;
6411
6412 /*
6413 * Sanity check for confiugred queue parameters against the run-time
6414 * device parameters
6415 */
6416
6417 /* Sanity check on FCP fast-path WQ parameters */
6418 cfg_fcp_wq_count = phba->cfg_fcp_wq_count;
6419 if (cfg_fcp_wq_count >
6420 (phba->sli4_hba.max_cfg_param.max_wq - LPFC_SP_WQN_DEF)) {
6421 cfg_fcp_wq_count = phba->sli4_hba.max_cfg_param.max_wq -
6422 LPFC_SP_WQN_DEF;
6423 if (cfg_fcp_wq_count < LPFC_FP_WQN_MIN) {
6424 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6425 "2581 Not enough WQs (%d) from "
6426 "the pci function for supporting "
6427 "FCP WQs (%d)\n",
6428 phba->sli4_hba.max_cfg_param.max_wq,
6429 phba->cfg_fcp_wq_count);
6430 goto out_error;
6431 }
6432 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6433 "2582 Not enough WQs (%d) from the pci "
6434 "function for supporting the requested "
6435 "FCP WQs (%d), the actual FCP WQs can "
6436 "be supported: %d\n",
6437 phba->sli4_hba.max_cfg_param.max_wq,
6438 phba->cfg_fcp_wq_count, cfg_fcp_wq_count);
6439 }
6440 /* The actual number of FCP work queues adopted */
6441 phba->cfg_fcp_wq_count = cfg_fcp_wq_count;
6442
6443 /* Sanity check on FCP fast-path EQ parameters */
6444 cfg_fcp_eq_count = phba->cfg_fcp_eq_count;
6445 if (cfg_fcp_eq_count >
6446 (phba->sli4_hba.max_cfg_param.max_eq - LPFC_SP_EQN_DEF)) {
6447 cfg_fcp_eq_count = phba->sli4_hba.max_cfg_param.max_eq -
6448 LPFC_SP_EQN_DEF;
6449 if (cfg_fcp_eq_count < LPFC_FP_EQN_MIN) {
6450 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6451 "2574 Not enough EQs (%d) from the "
6452 "pci function for supporting FCP "
6453 "EQs (%d)\n",
6454 phba->sli4_hba.max_cfg_param.max_eq,
6455 phba->cfg_fcp_eq_count);
6456 goto out_error;
6457 }
6458 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6459 "2575 Not enough EQs (%d) from the pci "
6460 "function for supporting the requested "
6461 "FCP EQs (%d), the actual FCP EQs can "
6462 "be supported: %d\n",
6463 phba->sli4_hba.max_cfg_param.max_eq,
6464 phba->cfg_fcp_eq_count, cfg_fcp_eq_count);
6465 }
6466 /* It does not make sense to have more EQs than WQs */
6467 if (cfg_fcp_eq_count > phba->cfg_fcp_wq_count) {
6468 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smart6a9c52c2009-10-02 15:16:51 -04006469 "2593 The FCP EQ count(%d) cannot be greater "
6470 "than the FCP WQ count(%d), limiting the "
6471 "FCP EQ count to %d\n", cfg_fcp_eq_count,
James Smartda0436e2009-05-22 14:51:39 -04006472 phba->cfg_fcp_wq_count,
6473 phba->cfg_fcp_wq_count);
6474 cfg_fcp_eq_count = phba->cfg_fcp_wq_count;
6475 }
6476 /* The actual number of FCP event queues adopted */
6477 phba->cfg_fcp_eq_count = cfg_fcp_eq_count;
6478 /* The overall number of event queues used */
6479 phba->sli4_hba.cfg_eqn = phba->cfg_fcp_eq_count + LPFC_SP_EQN_DEF;
6480
James Smartda0436e2009-05-22 14:51:39 -04006481 /* Get EQ depth from module parameter, fake the default for now */
6482 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
6483 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
6484
James Smart5350d872011-10-10 21:33:49 -04006485 /* Get CQ depth from module parameter, fake the default for now */
6486 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
6487 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
6488
6489 return 0;
6490out_error:
6491 return -ENOMEM;
6492}
6493
6494/**
6495 * lpfc_sli4_queue_create - Create all the SLI4 queues
6496 * @phba: pointer to lpfc hba data structure.
6497 *
6498 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
6499 * operation. For each SLI4 queue type, the parameters such as queue entry
6500 * count (queue depth) shall be taken from the module parameter. For now,
6501 * we just use some constant number as place holder.
6502 *
6503 * Return codes
6504 * 0 - sucessful
6505 * -ENOMEM - No availble memory
6506 * -EIO - The mailbox failed to complete successfully.
6507 **/
6508int
6509lpfc_sli4_queue_create(struct lpfc_hba *phba)
6510{
6511 struct lpfc_queue *qdesc;
6512 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
6513
6514 /*
6515 * Create Event Queues (EQs)
6516 */
6517
James Smartda0436e2009-05-22 14:51:39 -04006518 /* Create slow path event queue */
6519 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
6520 phba->sli4_hba.eq_ecount);
6521 if (!qdesc) {
6522 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6523 "0496 Failed allocate slow-path EQ\n");
6524 goto out_error;
6525 }
6526 phba->sli4_hba.sp_eq = qdesc;
6527
James Smart5350d872011-10-10 21:33:49 -04006528 /*
6529 * Create fast-path FCP Event Queue(s). The cfg_fcp_eq_count can be
6530 * zero whenever there is exactly one interrupt vector. This is not
6531 * an error.
6532 */
6533 if (phba->cfg_fcp_eq_count) {
6534 phba->sli4_hba.fp_eq = kzalloc((sizeof(struct lpfc_queue *) *
6535 phba->cfg_fcp_eq_count), GFP_KERNEL);
6536 if (!phba->sli4_hba.fp_eq) {
6537 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6538 "2576 Failed allocate memory for "
6539 "fast-path EQ record array\n");
6540 goto out_free_sp_eq;
6541 }
James Smartda0436e2009-05-22 14:51:39 -04006542 }
6543 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
6544 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
6545 phba->sli4_hba.eq_ecount);
6546 if (!qdesc) {
6547 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6548 "0497 Failed allocate fast-path EQ\n");
6549 goto out_free_fp_eq;
6550 }
6551 phba->sli4_hba.fp_eq[fcp_eqidx] = qdesc;
6552 }
6553
6554 /*
6555 * Create Complete Queues (CQs)
6556 */
6557
James Smartda0436e2009-05-22 14:51:39 -04006558 /* Create slow-path Mailbox Command Complete Queue */
6559 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6560 phba->sli4_hba.cq_ecount);
6561 if (!qdesc) {
6562 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6563 "0500 Failed allocate slow-path mailbox CQ\n");
6564 goto out_free_fp_eq;
6565 }
6566 phba->sli4_hba.mbx_cq = qdesc;
6567
6568 /* Create slow-path ELS Complete Queue */
6569 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6570 phba->sli4_hba.cq_ecount);
6571 if (!qdesc) {
6572 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6573 "0501 Failed allocate slow-path ELS CQ\n");
6574 goto out_free_mbx_cq;
6575 }
6576 phba->sli4_hba.els_cq = qdesc;
6577
James Smartda0436e2009-05-22 14:51:39 -04006578
James Smart5350d872011-10-10 21:33:49 -04006579 /*
6580 * Create fast-path FCP Completion Queue(s), one-to-one with FCP EQs.
6581 * If there are no FCP EQs then create exactly one FCP CQ.
6582 */
6583 if (phba->cfg_fcp_eq_count)
6584 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
6585 phba->cfg_fcp_eq_count),
6586 GFP_KERNEL);
6587 else
6588 phba->sli4_hba.fcp_cq = kzalloc(sizeof(struct lpfc_queue *),
6589 GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04006590 if (!phba->sli4_hba.fcp_cq) {
6591 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6592 "2577 Failed allocate memory for fast-path "
6593 "CQ record array\n");
James Smart4d9ab992009-10-02 15:16:39 -04006594 goto out_free_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04006595 }
James Smart5350d872011-10-10 21:33:49 -04006596 fcp_cqidx = 0;
6597 do {
James Smartda0436e2009-05-22 14:51:39 -04006598 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6599 phba->sli4_hba.cq_ecount);
6600 if (!qdesc) {
6601 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6602 "0499 Failed allocate fast-path FCP "
6603 "CQ (%d)\n", fcp_cqidx);
6604 goto out_free_fcp_cq;
6605 }
6606 phba->sli4_hba.fcp_cq[fcp_cqidx] = qdesc;
James Smart5350d872011-10-10 21:33:49 -04006607 } while (++fcp_cqidx < phba->cfg_fcp_eq_count);
James Smartda0436e2009-05-22 14:51:39 -04006608
6609 /* Create Mailbox Command Queue */
6610 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
6611 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
6612
6613 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
6614 phba->sli4_hba.mq_ecount);
6615 if (!qdesc) {
6616 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6617 "0505 Failed allocate slow-path MQ\n");
6618 goto out_free_fcp_cq;
6619 }
6620 phba->sli4_hba.mbx_wq = qdesc;
6621
6622 /*
6623 * Create all the Work Queues (WQs)
6624 */
6625 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
6626 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
6627
6628 /* Create slow-path ELS Work Queue */
6629 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
6630 phba->sli4_hba.wq_ecount);
6631 if (!qdesc) {
6632 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6633 "0504 Failed allocate slow-path ELS WQ\n");
6634 goto out_free_mbx_wq;
6635 }
6636 phba->sli4_hba.els_wq = qdesc;
6637
6638 /* Create fast-path FCP Work Queue(s) */
6639 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
6640 phba->cfg_fcp_wq_count), GFP_KERNEL);
6641 if (!phba->sli4_hba.fcp_wq) {
6642 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6643 "2578 Failed allocate memory for fast-path "
6644 "WQ record array\n");
6645 goto out_free_els_wq;
6646 }
6647 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
6648 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
6649 phba->sli4_hba.wq_ecount);
6650 if (!qdesc) {
6651 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6652 "0503 Failed allocate fast-path FCP "
6653 "WQ (%d)\n", fcp_wqidx);
6654 goto out_free_fcp_wq;
6655 }
6656 phba->sli4_hba.fcp_wq[fcp_wqidx] = qdesc;
6657 }
6658
6659 /*
6660 * Create Receive Queue (RQ)
6661 */
6662 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
6663 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
6664
6665 /* Create Receive Queue for header */
6666 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
6667 phba->sli4_hba.rq_ecount);
6668 if (!qdesc) {
6669 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6670 "0506 Failed allocate receive HRQ\n");
6671 goto out_free_fcp_wq;
6672 }
6673 phba->sli4_hba.hdr_rq = qdesc;
6674
6675 /* Create Receive Queue for data */
6676 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
6677 phba->sli4_hba.rq_ecount);
6678 if (!qdesc) {
6679 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6680 "0507 Failed allocate receive DRQ\n");
6681 goto out_free_hdr_rq;
6682 }
6683 phba->sli4_hba.dat_rq = qdesc;
6684
6685 return 0;
6686
6687out_free_hdr_rq:
6688 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
6689 phba->sli4_hba.hdr_rq = NULL;
6690out_free_fcp_wq:
6691 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--) {
6692 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_wqidx]);
6693 phba->sli4_hba.fcp_wq[fcp_wqidx] = NULL;
6694 }
6695 kfree(phba->sli4_hba.fcp_wq);
James Smart2e90f4b2011-12-13 13:22:37 -05006696 phba->sli4_hba.fcp_wq = NULL;
James Smartda0436e2009-05-22 14:51:39 -04006697out_free_els_wq:
6698 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
6699 phba->sli4_hba.els_wq = NULL;
6700out_free_mbx_wq:
6701 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
6702 phba->sli4_hba.mbx_wq = NULL;
6703out_free_fcp_cq:
6704 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--) {
6705 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_cqidx]);
6706 phba->sli4_hba.fcp_cq[fcp_cqidx] = NULL;
6707 }
6708 kfree(phba->sli4_hba.fcp_cq);
James Smart2e90f4b2011-12-13 13:22:37 -05006709 phba->sli4_hba.fcp_cq = NULL;
James Smartda0436e2009-05-22 14:51:39 -04006710out_free_els_cq:
6711 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
6712 phba->sli4_hba.els_cq = NULL;
6713out_free_mbx_cq:
6714 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
6715 phba->sli4_hba.mbx_cq = NULL;
6716out_free_fp_eq:
6717 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--) {
6718 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_eqidx]);
6719 phba->sli4_hba.fp_eq[fcp_eqidx] = NULL;
6720 }
6721 kfree(phba->sli4_hba.fp_eq);
James Smart2e90f4b2011-12-13 13:22:37 -05006722 phba->sli4_hba.fp_eq = NULL;
James Smartda0436e2009-05-22 14:51:39 -04006723out_free_sp_eq:
6724 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
6725 phba->sli4_hba.sp_eq = NULL;
6726out_error:
6727 return -ENOMEM;
6728}
6729
6730/**
6731 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
6732 * @phba: pointer to lpfc hba data structure.
6733 *
6734 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
6735 * operation.
6736 *
6737 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006738 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006739 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006740 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006741 **/
James Smart5350d872011-10-10 21:33:49 -04006742void
James Smartda0436e2009-05-22 14:51:39 -04006743lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
6744{
6745 int fcp_qidx;
6746
6747 /* Release mailbox command work queue */
6748 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
6749 phba->sli4_hba.mbx_wq = NULL;
6750
6751 /* Release ELS work queue */
6752 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
6753 phba->sli4_hba.els_wq = NULL;
6754
6755 /* Release FCP work queue */
James Smart2e90f4b2011-12-13 13:22:37 -05006756 if (phba->sli4_hba.fcp_wq != NULL)
6757 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count;
6758 fcp_qidx++)
6759 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_qidx]);
James Smartda0436e2009-05-22 14:51:39 -04006760 kfree(phba->sli4_hba.fcp_wq);
6761 phba->sli4_hba.fcp_wq = NULL;
6762
6763 /* Release unsolicited receive queue */
6764 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
6765 phba->sli4_hba.hdr_rq = NULL;
6766 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
6767 phba->sli4_hba.dat_rq = NULL;
6768
James Smartda0436e2009-05-22 14:51:39 -04006769 /* Release ELS complete queue */
6770 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
6771 phba->sli4_hba.els_cq = NULL;
6772
6773 /* Release mailbox command complete queue */
6774 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
6775 phba->sli4_hba.mbx_cq = NULL;
6776
6777 /* Release FCP response complete queue */
James Smart05580562011-05-24 11:40:48 -04006778 fcp_qidx = 0;
James Smart2e90f4b2011-12-13 13:22:37 -05006779 if (phba->sli4_hba.fcp_cq != NULL)
6780 do
6781 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_qidx]);
6782 while (++fcp_qidx < phba->cfg_fcp_eq_count);
James Smartda0436e2009-05-22 14:51:39 -04006783 kfree(phba->sli4_hba.fcp_cq);
6784 phba->sli4_hba.fcp_cq = NULL;
6785
6786 /* Release fast-path event queue */
James Smart2e90f4b2011-12-13 13:22:37 -05006787 if (phba->sli4_hba.fp_eq != NULL)
6788 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count;
6789 fcp_qidx++)
6790 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_qidx]);
James Smartda0436e2009-05-22 14:51:39 -04006791 kfree(phba->sli4_hba.fp_eq);
6792 phba->sli4_hba.fp_eq = NULL;
6793
6794 /* Release slow-path event queue */
6795 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
6796 phba->sli4_hba.sp_eq = NULL;
6797
6798 return;
6799}
6800
6801/**
6802 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
6803 * @phba: pointer to lpfc hba data structure.
6804 *
6805 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
6806 * operation.
6807 *
6808 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006809 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006810 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006811 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006812 **/
6813int
6814lpfc_sli4_queue_setup(struct lpfc_hba *phba)
6815{
6816 int rc = -ENOMEM;
6817 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
6818 int fcp_cq_index = 0;
6819
6820 /*
6821 * Set up Event Queues (EQs)
6822 */
6823
6824 /* Set up slow-path event queue */
6825 if (!phba->sli4_hba.sp_eq) {
6826 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6827 "0520 Slow-path EQ not allocated\n");
6828 goto out_error;
6829 }
6830 rc = lpfc_eq_create(phba, phba->sli4_hba.sp_eq,
6831 LPFC_SP_DEF_IMAX);
6832 if (rc) {
6833 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6834 "0521 Failed setup of slow-path EQ: "
6835 "rc = 0x%x\n", rc);
6836 goto out_error;
6837 }
6838 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6839 "2583 Slow-path EQ setup: queue-id=%d\n",
6840 phba->sli4_hba.sp_eq->queue_id);
6841
6842 /* Set up fast-path event queue */
James Smart1b511972011-12-13 13:23:09 -05006843 if (phba->cfg_fcp_eq_count && !phba->sli4_hba.fp_eq) {
James Smart2e90f4b2011-12-13 13:22:37 -05006844 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6845 "3147 Fast-path EQs not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05006846 rc = -ENOMEM;
James Smart2e90f4b2011-12-13 13:22:37 -05006847 goto out_destroy_sp_eq;
6848 }
James Smartda0436e2009-05-22 14:51:39 -04006849 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
6850 if (!phba->sli4_hba.fp_eq[fcp_eqidx]) {
6851 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6852 "0522 Fast-path EQ (%d) not "
6853 "allocated\n", fcp_eqidx);
James Smart1b511972011-12-13 13:23:09 -05006854 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04006855 goto out_destroy_fp_eq;
6856 }
6857 rc = lpfc_eq_create(phba, phba->sli4_hba.fp_eq[fcp_eqidx],
6858 phba->cfg_fcp_imax);
6859 if (rc) {
6860 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6861 "0523 Failed setup of fast-path EQ "
6862 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
6863 goto out_destroy_fp_eq;
6864 }
6865 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6866 "2584 Fast-path EQ setup: "
6867 "queue[%d]-id=%d\n", fcp_eqidx,
6868 phba->sli4_hba.fp_eq[fcp_eqidx]->queue_id);
6869 }
6870
6871 /*
6872 * Set up Complete Queues (CQs)
6873 */
6874
6875 /* Set up slow-path MBOX Complete Queue as the first CQ */
6876 if (!phba->sli4_hba.mbx_cq) {
6877 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6878 "0528 Mailbox CQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05006879 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04006880 goto out_destroy_fp_eq;
6881 }
6882 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq, phba->sli4_hba.sp_eq,
6883 LPFC_MCQ, LPFC_MBOX);
6884 if (rc) {
6885 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6886 "0529 Failed setup of slow-path mailbox CQ: "
6887 "rc = 0x%x\n", rc);
6888 goto out_destroy_fp_eq;
6889 }
6890 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6891 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
6892 phba->sli4_hba.mbx_cq->queue_id,
6893 phba->sli4_hba.sp_eq->queue_id);
6894
6895 /* Set up slow-path ELS Complete Queue */
6896 if (!phba->sli4_hba.els_cq) {
6897 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6898 "0530 ELS CQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05006899 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04006900 goto out_destroy_mbx_cq;
6901 }
6902 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq, phba->sli4_hba.sp_eq,
6903 LPFC_WCQ, LPFC_ELS);
6904 if (rc) {
6905 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6906 "0531 Failed setup of slow-path ELS CQ: "
6907 "rc = 0x%x\n", rc);
6908 goto out_destroy_mbx_cq;
6909 }
6910 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6911 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
6912 phba->sli4_hba.els_cq->queue_id,
6913 phba->sli4_hba.sp_eq->queue_id);
6914
James Smartda0436e2009-05-22 14:51:39 -04006915 /* Set up fast-path FCP Response Complete Queue */
James Smart2e90f4b2011-12-13 13:22:37 -05006916 if (!phba->sli4_hba.fcp_cq) {
6917 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6918 "3148 Fast-path FCP CQ array not "
6919 "allocated\n");
James Smart1b511972011-12-13 13:23:09 -05006920 rc = -ENOMEM;
James Smart2e90f4b2011-12-13 13:22:37 -05006921 goto out_destroy_els_cq;
6922 }
James Smart05580562011-05-24 11:40:48 -04006923 fcp_cqidx = 0;
6924 do {
James Smartda0436e2009-05-22 14:51:39 -04006925 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
6926 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6927 "0526 Fast-path FCP CQ (%d) not "
6928 "allocated\n", fcp_cqidx);
James Smart1b511972011-12-13 13:23:09 -05006929 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04006930 goto out_destroy_fcp_cq;
6931 }
James Smart05580562011-05-24 11:40:48 -04006932 if (phba->cfg_fcp_eq_count)
6933 rc = lpfc_cq_create(phba,
6934 phba->sli4_hba.fcp_cq[fcp_cqidx],
6935 phba->sli4_hba.fp_eq[fcp_cqidx],
6936 LPFC_WCQ, LPFC_FCP);
6937 else
6938 rc = lpfc_cq_create(phba,
6939 phba->sli4_hba.fcp_cq[fcp_cqidx],
6940 phba->sli4_hba.sp_eq,
6941 LPFC_WCQ, LPFC_FCP);
James Smartda0436e2009-05-22 14:51:39 -04006942 if (rc) {
6943 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6944 "0527 Failed setup of fast-path FCP "
6945 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
6946 goto out_destroy_fcp_cq;
6947 }
6948 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6949 "2588 FCP CQ setup: cq[%d]-id=%d, "
James Smart05580562011-05-24 11:40:48 -04006950 "parent %seq[%d]-id=%d\n",
James Smartda0436e2009-05-22 14:51:39 -04006951 fcp_cqidx,
6952 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
James Smart05580562011-05-24 11:40:48 -04006953 (phba->cfg_fcp_eq_count) ? "" : "sp_",
James Smartda0436e2009-05-22 14:51:39 -04006954 fcp_cqidx,
James Smart05580562011-05-24 11:40:48 -04006955 (phba->cfg_fcp_eq_count) ?
6956 phba->sli4_hba.fp_eq[fcp_cqidx]->queue_id :
6957 phba->sli4_hba.sp_eq->queue_id);
6958 } while (++fcp_cqidx < phba->cfg_fcp_eq_count);
James Smartda0436e2009-05-22 14:51:39 -04006959
6960 /*
6961 * Set up all the Work Queues (WQs)
6962 */
6963
6964 /* Set up Mailbox Command Queue */
6965 if (!phba->sli4_hba.mbx_wq) {
6966 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6967 "0538 Slow-path MQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05006968 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04006969 goto out_destroy_fcp_cq;
6970 }
6971 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
6972 phba->sli4_hba.mbx_cq, LPFC_MBOX);
6973 if (rc) {
6974 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6975 "0539 Failed setup of slow-path MQ: "
6976 "rc = 0x%x\n", rc);
6977 goto out_destroy_fcp_cq;
6978 }
6979 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6980 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
6981 phba->sli4_hba.mbx_wq->queue_id,
6982 phba->sli4_hba.mbx_cq->queue_id);
6983
6984 /* Set up slow-path ELS Work Queue */
6985 if (!phba->sli4_hba.els_wq) {
6986 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6987 "0536 Slow-path ELS WQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05006988 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04006989 goto out_destroy_mbx_wq;
6990 }
6991 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
6992 phba->sli4_hba.els_cq, LPFC_ELS);
6993 if (rc) {
6994 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6995 "0537 Failed setup of slow-path ELS WQ: "
6996 "rc = 0x%x\n", rc);
6997 goto out_destroy_mbx_wq;
6998 }
6999 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7000 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
7001 phba->sli4_hba.els_wq->queue_id,
7002 phba->sli4_hba.els_cq->queue_id);
7003
7004 /* Set up fast-path FCP Work Queue */
James Smart2e90f4b2011-12-13 13:22:37 -05007005 if (!phba->sli4_hba.fcp_wq) {
7006 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7007 "3149 Fast-path FCP WQ array not "
7008 "allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007009 rc = -ENOMEM;
James Smart2e90f4b2011-12-13 13:22:37 -05007010 goto out_destroy_els_wq;
7011 }
James Smartda0436e2009-05-22 14:51:39 -04007012 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
7013 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
7014 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7015 "0534 Fast-path FCP WQ (%d) not "
7016 "allocated\n", fcp_wqidx);
James Smart1b511972011-12-13 13:23:09 -05007017 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04007018 goto out_destroy_fcp_wq;
7019 }
7020 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
7021 phba->sli4_hba.fcp_cq[fcp_cq_index],
7022 LPFC_FCP);
7023 if (rc) {
7024 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7025 "0535 Failed setup of fast-path FCP "
7026 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
7027 goto out_destroy_fcp_wq;
7028 }
7029 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7030 "2591 FCP WQ setup: wq[%d]-id=%d, "
7031 "parent cq[%d]-id=%d\n",
7032 fcp_wqidx,
7033 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
7034 fcp_cq_index,
7035 phba->sli4_hba.fcp_cq[fcp_cq_index]->queue_id);
7036 /* Round robin FCP Work Queue's Completion Queue assignment */
James Smart05580562011-05-24 11:40:48 -04007037 if (phba->cfg_fcp_eq_count)
7038 fcp_cq_index = ((fcp_cq_index + 1) %
7039 phba->cfg_fcp_eq_count);
James Smartda0436e2009-05-22 14:51:39 -04007040 }
7041
7042 /*
7043 * Create Receive Queue (RQ)
7044 */
7045 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
7046 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7047 "0540 Receive Queue not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007048 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04007049 goto out_destroy_fcp_wq;
7050 }
James Smart73d91e52011-10-10 21:32:10 -04007051
7052 lpfc_rq_adjust_repost(phba, phba->sli4_hba.hdr_rq, LPFC_ELS_HBQ);
7053 lpfc_rq_adjust_repost(phba, phba->sli4_hba.dat_rq, LPFC_ELS_HBQ);
7054
James Smartda0436e2009-05-22 14:51:39 -04007055 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04007056 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04007057 if (rc) {
7058 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7059 "0541 Failed setup of Receive Queue: "
7060 "rc = 0x%x\n", rc);
7061 goto out_destroy_fcp_wq;
7062 }
James Smart73d91e52011-10-10 21:32:10 -04007063
James Smartda0436e2009-05-22 14:51:39 -04007064 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7065 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
7066 "parent cq-id=%d\n",
7067 phba->sli4_hba.hdr_rq->queue_id,
7068 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04007069 phba->sli4_hba.els_cq->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007070 return 0;
7071
7072out_destroy_fcp_wq:
7073 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
7074 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05007075out_destroy_els_wq:
James Smartda0436e2009-05-22 14:51:39 -04007076 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7077out_destroy_mbx_wq:
7078 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
7079out_destroy_fcp_cq:
7080 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
7081 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05007082out_destroy_els_cq:
James Smartda0436e2009-05-22 14:51:39 -04007083 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
7084out_destroy_mbx_cq:
7085 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
7086out_destroy_fp_eq:
7087 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
7088 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_eqidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05007089out_destroy_sp_eq:
James Smartda0436e2009-05-22 14:51:39 -04007090 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
7091out_error:
7092 return rc;
7093}
7094
7095/**
7096 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
7097 * @phba: pointer to lpfc hba data structure.
7098 *
7099 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
7100 * operation.
7101 *
7102 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007103 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007104 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007105 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007106 **/
7107void
7108lpfc_sli4_queue_unset(struct lpfc_hba *phba)
7109{
7110 int fcp_qidx;
7111
7112 /* Unset mailbox command work queue */
7113 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
7114 /* Unset ELS work queue */
7115 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7116 /* Unset unsolicited receive queue */
7117 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
7118 /* Unset FCP work queue */
7119 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
7120 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
7121 /* Unset mailbox command complete queue */
7122 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
7123 /* Unset ELS complete queue */
7124 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
James Smartda0436e2009-05-22 14:51:39 -04007125 /* Unset FCP response complete queue */
James Smart2e90f4b2011-12-13 13:22:37 -05007126 if (phba->sli4_hba.fcp_cq) {
7127 fcp_qidx = 0;
7128 do {
7129 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
7130 } while (++fcp_qidx < phba->cfg_fcp_eq_count);
7131 }
James Smartda0436e2009-05-22 14:51:39 -04007132 /* Unset fast-path event queue */
James Smart2e90f4b2011-12-13 13:22:37 -05007133 if (phba->sli4_hba.fp_eq) {
7134 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count;
7135 fcp_qidx++)
7136 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_qidx]);
7137 }
James Smartda0436e2009-05-22 14:51:39 -04007138 /* Unset slow-path event queue */
7139 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
7140}
7141
7142/**
7143 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
7144 * @phba: pointer to lpfc hba data structure.
7145 *
7146 * This routine is invoked to allocate and set up a pool of completion queue
7147 * events. The body of the completion queue event is a completion queue entry
7148 * CQE. For now, this pool is used for the interrupt service routine to queue
7149 * the following HBA completion queue events for the worker thread to process:
7150 * - Mailbox asynchronous events
7151 * - Receive queue completion unsolicited events
7152 * Later, this can be used for all the slow-path events.
7153 *
7154 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007155 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007156 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04007157 **/
7158static int
7159lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
7160{
7161 struct lpfc_cq_event *cq_event;
7162 int i;
7163
7164 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
7165 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
7166 if (!cq_event)
7167 goto out_pool_create_fail;
7168 list_add_tail(&cq_event->list,
7169 &phba->sli4_hba.sp_cqe_event_pool);
7170 }
7171 return 0;
7172
7173out_pool_create_fail:
7174 lpfc_sli4_cq_event_pool_destroy(phba);
7175 return -ENOMEM;
7176}
7177
7178/**
7179 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
7180 * @phba: pointer to lpfc hba data structure.
7181 *
7182 * This routine is invoked to free the pool of completion queue events at
7183 * driver unload time. Note that, it is the responsibility of the driver
7184 * cleanup routine to free all the outstanding completion-queue events
7185 * allocated from this pool back into the pool before invoking this routine
7186 * to destroy the pool.
7187 **/
7188static void
7189lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
7190{
7191 struct lpfc_cq_event *cq_event, *next_cq_event;
7192
7193 list_for_each_entry_safe(cq_event, next_cq_event,
7194 &phba->sli4_hba.sp_cqe_event_pool, list) {
7195 list_del(&cq_event->list);
7196 kfree(cq_event);
7197 }
7198}
7199
7200/**
7201 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
7202 * @phba: pointer to lpfc hba data structure.
7203 *
7204 * This routine is the lock free version of the API invoked to allocate a
7205 * completion-queue event from the free pool.
7206 *
7207 * Return: Pointer to the newly allocated completion-queue event if successful
7208 * NULL otherwise.
7209 **/
7210struct lpfc_cq_event *
7211__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
7212{
7213 struct lpfc_cq_event *cq_event = NULL;
7214
7215 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
7216 struct lpfc_cq_event, list);
7217 return cq_event;
7218}
7219
7220/**
7221 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
7222 * @phba: pointer to lpfc hba data structure.
7223 *
7224 * This routine is the lock version of the API invoked to allocate a
7225 * completion-queue event from the free pool.
7226 *
7227 * Return: Pointer to the newly allocated completion-queue event if successful
7228 * NULL otherwise.
7229 **/
7230struct lpfc_cq_event *
7231lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
7232{
7233 struct lpfc_cq_event *cq_event;
7234 unsigned long iflags;
7235
7236 spin_lock_irqsave(&phba->hbalock, iflags);
7237 cq_event = __lpfc_sli4_cq_event_alloc(phba);
7238 spin_unlock_irqrestore(&phba->hbalock, iflags);
7239 return cq_event;
7240}
7241
7242/**
7243 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
7244 * @phba: pointer to lpfc hba data structure.
7245 * @cq_event: pointer to the completion queue event to be freed.
7246 *
7247 * This routine is the lock free version of the API invoked to release a
7248 * completion-queue event back into the free pool.
7249 **/
7250void
7251__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
7252 struct lpfc_cq_event *cq_event)
7253{
7254 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
7255}
7256
7257/**
7258 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
7259 * @phba: pointer to lpfc hba data structure.
7260 * @cq_event: pointer to the completion queue event to be freed.
7261 *
7262 * This routine is the lock version of the API invoked to release a
7263 * completion-queue event back into the free pool.
7264 **/
7265void
7266lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
7267 struct lpfc_cq_event *cq_event)
7268{
7269 unsigned long iflags;
7270 spin_lock_irqsave(&phba->hbalock, iflags);
7271 __lpfc_sli4_cq_event_release(phba, cq_event);
7272 spin_unlock_irqrestore(&phba->hbalock, iflags);
7273}
7274
7275/**
7276 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
7277 * @phba: pointer to lpfc hba data structure.
7278 *
7279 * This routine is to free all the pending completion-queue events to the
7280 * back into the free pool for device reset.
7281 **/
7282static void
7283lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
7284{
7285 LIST_HEAD(cqelist);
7286 struct lpfc_cq_event *cqe;
7287 unsigned long iflags;
7288
7289 /* Retrieve all the pending WCQEs from pending WCQE lists */
7290 spin_lock_irqsave(&phba->hbalock, iflags);
7291 /* Pending FCP XRI abort events */
7292 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
7293 &cqelist);
7294 /* Pending ELS XRI abort events */
7295 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
7296 &cqelist);
7297 /* Pending asynnc events */
7298 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
7299 &cqelist);
7300 spin_unlock_irqrestore(&phba->hbalock, iflags);
7301
7302 while (!list_empty(&cqelist)) {
7303 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
7304 lpfc_sli4_cq_event_release(phba, cqe);
7305 }
7306}
7307
7308/**
7309 * lpfc_pci_function_reset - Reset pci function.
7310 * @phba: pointer to lpfc hba data structure.
7311 *
7312 * This routine is invoked to request a PCI function reset. It will destroys
7313 * all resources assigned to the PCI function which originates this request.
7314 *
7315 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007316 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007317 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007318 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007319 **/
7320int
7321lpfc_pci_function_reset(struct lpfc_hba *phba)
7322{
7323 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05007324 uint32_t rc = 0, if_type;
James Smartda0436e2009-05-22 14:51:39 -04007325 uint32_t shdr_status, shdr_add_status;
James Smart2fcee4b2010-12-15 17:57:46 -05007326 uint32_t rdy_chk, num_resets = 0, reset_again = 0;
James Smartda0436e2009-05-22 14:51:39 -04007327 union lpfc_sli4_cfg_shdr *shdr;
James Smart2fcee4b2010-12-15 17:57:46 -05007328 struct lpfc_register reg_data;
James Smart2b81f942012-03-01 22:37:18 -05007329 uint16_t devid;
James Smartda0436e2009-05-22 14:51:39 -04007330
James Smart2fcee4b2010-12-15 17:57:46 -05007331 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7332 switch (if_type) {
7333 case LPFC_SLI_INTF_IF_TYPE_0:
7334 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
7335 GFP_KERNEL);
7336 if (!mboxq) {
7337 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7338 "0494 Unable to allocate memory for "
7339 "issuing SLI_FUNCTION_RESET mailbox "
7340 "command\n");
7341 return -ENOMEM;
7342 }
7343
7344 /* Setup PCI function reset mailbox-ioctl command */
7345 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7346 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
7347 LPFC_SLI4_MBX_EMBED);
7348 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7349 shdr = (union lpfc_sli4_cfg_shdr *)
7350 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7351 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7352 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
7353 &shdr->response);
7354 if (rc != MBX_TIMEOUT)
7355 mempool_free(mboxq, phba->mbox_mem_pool);
7356 if (shdr_status || shdr_add_status || rc) {
7357 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7358 "0495 SLI_FUNCTION_RESET mailbox "
7359 "failed with status x%x add_status x%x,"
7360 " mbx status x%x\n",
7361 shdr_status, shdr_add_status, rc);
7362 rc = -ENXIO;
7363 }
7364 break;
7365 case LPFC_SLI_INTF_IF_TYPE_2:
7366 for (num_resets = 0;
7367 num_resets < MAX_IF_TYPE_2_RESETS;
7368 num_resets++) {
7369 reg_data.word0 = 0;
7370 bf_set(lpfc_sliport_ctrl_end, &reg_data,
7371 LPFC_SLIPORT_LITTLE_ENDIAN);
7372 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
7373 LPFC_SLIPORT_INIT_PORT);
7374 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
7375 CTRLregaddr);
James Smart8fcb8ac2012-03-01 22:35:58 -05007376 /* flush */
James Smart2b81f942012-03-01 22:37:18 -05007377 pci_read_config_word(phba->pcidev,
7378 PCI_DEVICE_ID, &devid);
James Smart2fcee4b2010-12-15 17:57:46 -05007379 /*
7380 * Poll the Port Status Register and wait for RDY for
7381 * up to 10 seconds. If the port doesn't respond, treat
7382 * it as an error. If the port responds with RN, start
7383 * the loop again.
7384 */
7385 for (rdy_chk = 0; rdy_chk < 1000; rdy_chk++) {
James Smart73d91e52011-10-10 21:32:10 -04007386 msleep(10);
James Smart9940b972011-03-11 16:06:12 -05007387 if (lpfc_readl(phba->sli4_hba.u.if_type2.
7388 STATUSregaddr, &reg_data.word0)) {
7389 rc = -ENODEV;
James Smart73d91e52011-10-10 21:32:10 -04007390 goto out;
James Smart9940b972011-03-11 16:06:12 -05007391 }
James Smart6b5151f2012-01-18 16:24:06 -05007392 if (bf_get(lpfc_sliport_status_rn, &reg_data))
7393 reset_again++;
James Smart2fcee4b2010-12-15 17:57:46 -05007394 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
7395 break;
James Smart2fcee4b2010-12-15 17:57:46 -05007396 }
7397
7398 /*
7399 * If the port responds to the init request with
7400 * reset needed, delay for a bit and restart the loop.
7401 */
James Smart6b5151f2012-01-18 16:24:06 -05007402 if (reset_again && (rdy_chk < 1000)) {
James Smart2fcee4b2010-12-15 17:57:46 -05007403 msleep(10);
7404 reset_again = 0;
7405 continue;
7406 }
7407
7408 /* Detect any port errors. */
James Smart2fcee4b2010-12-15 17:57:46 -05007409 if ((bf_get(lpfc_sliport_status_err, &reg_data)) ||
7410 (rdy_chk >= 1000)) {
7411 phba->work_status[0] = readl(
7412 phba->sli4_hba.u.if_type2.ERR1regaddr);
7413 phba->work_status[1] = readl(
7414 phba->sli4_hba.u.if_type2.ERR2regaddr);
7415 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05007416 "2890 Port error detected during port "
James Smart8a9d2e82012-05-09 21:16:12 -04007417 "reset(%d): wait_tmo:%d ms, "
7418 "port status reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05007419 "error 1=0x%x, error 2=0x%x\n",
James Smart8a9d2e82012-05-09 21:16:12 -04007420 num_resets, rdy_chk*10,
7421 reg_data.word0,
James Smart2fcee4b2010-12-15 17:57:46 -05007422 phba->work_status[0],
7423 phba->work_status[1]);
7424 rc = -ENODEV;
7425 }
7426
7427 /*
7428 * Terminate the outer loop provided the Port indicated
7429 * ready within 10 seconds.
7430 */
7431 if (rdy_chk < 1000)
7432 break;
7433 }
James Smart05580562011-05-24 11:40:48 -04007434 /* delay driver action following IF_TYPE_2 function reset */
7435 msleep(100);
James Smart2fcee4b2010-12-15 17:57:46 -05007436 break;
7437 case LPFC_SLI_INTF_IF_TYPE_1:
7438 default:
7439 break;
James Smartda0436e2009-05-22 14:51:39 -04007440 }
7441
James Smart73d91e52011-10-10 21:32:10 -04007442out:
James Smart2fcee4b2010-12-15 17:57:46 -05007443 /* Catch the not-ready port failure after a port reset. */
7444 if (num_resets >= MAX_IF_TYPE_2_RESETS)
7445 rc = -ENODEV;
7446
James Smartda0436e2009-05-22 14:51:39 -04007447 return rc;
7448}
7449
7450/**
7451 * lpfc_sli4_send_nop_mbox_cmds - Send sli-4 nop mailbox commands
7452 * @phba: pointer to lpfc hba data structure.
7453 * @cnt: number of nop mailbox commands to send.
7454 *
7455 * This routine is invoked to send a number @cnt of NOP mailbox command and
7456 * wait for each command to complete.
7457 *
7458 * Return: the number of NOP mailbox command completed.
7459 **/
7460static int
7461lpfc_sli4_send_nop_mbox_cmds(struct lpfc_hba *phba, uint32_t cnt)
7462{
7463 LPFC_MBOXQ_t *mboxq;
7464 int length, cmdsent;
7465 uint32_t mbox_tmo;
7466 uint32_t rc = 0;
7467 uint32_t shdr_status, shdr_add_status;
7468 union lpfc_sli4_cfg_shdr *shdr;
7469
7470 if (cnt == 0) {
7471 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7472 "2518 Requested to send 0 NOP mailbox cmd\n");
7473 return cnt;
7474 }
7475
7476 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7477 if (!mboxq) {
7478 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7479 "2519 Unable to allocate memory for issuing "
7480 "NOP mailbox command\n");
7481 return 0;
7482 }
7483
7484 /* Set up NOP SLI4_CONFIG mailbox-ioctl command */
7485 length = (sizeof(struct lpfc_mbx_nop) -
7486 sizeof(struct lpfc_sli4_cfg_mhdr));
7487 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7488 LPFC_MBOX_OPCODE_NOP, length, LPFC_SLI4_MBX_EMBED);
7489
James Smartda0436e2009-05-22 14:51:39 -04007490 for (cmdsent = 0; cmdsent < cnt; cmdsent++) {
7491 if (!phba->sli4_hba.intr_enable)
7492 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -04007493 else {
7494 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04007495 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smarta183a152011-10-10 21:32:43 -04007496 }
James Smartda0436e2009-05-22 14:51:39 -04007497 if (rc == MBX_TIMEOUT)
7498 break;
7499 /* Check return status */
7500 shdr = (union lpfc_sli4_cfg_shdr *)
7501 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7502 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7503 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
7504 &shdr->response);
7505 if (shdr_status || shdr_add_status || rc) {
7506 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7507 "2520 NOP mailbox command failed "
7508 "status x%x add_status x%x mbx "
7509 "status x%x\n", shdr_status,
7510 shdr_add_status, rc);
7511 break;
7512 }
7513 }
7514
7515 if (rc != MBX_TIMEOUT)
7516 mempool_free(mboxq, phba->mbox_mem_pool);
7517
7518 return cmdsent;
7519}
7520
7521/**
James Smartda0436e2009-05-22 14:51:39 -04007522 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
7523 * @phba: pointer to lpfc hba data structure.
7524 *
7525 * This routine is invoked to set up the PCI device memory space for device
7526 * with SLI-4 interface spec.
7527 *
7528 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007529 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007530 * other values - error
7531 **/
7532static int
7533lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
7534{
7535 struct pci_dev *pdev;
7536 unsigned long bar0map_len, bar1map_len, bar2map_len;
7537 int error = -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -05007538 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04007539
7540 /* Obtain PCI device reference */
7541 if (!phba->pcidev)
7542 return error;
7543 else
7544 pdev = phba->pcidev;
7545
7546 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05007547 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
7548 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
7549 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
7550 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smartda0436e2009-05-22 14:51:39 -04007551 return error;
Michael Reed8e685972009-09-18 12:02:05 -05007552 }
7553 }
James Smartda0436e2009-05-22 14:51:39 -04007554
James Smart2fcee4b2010-12-15 17:57:46 -05007555 /*
7556 * The BARs and register set definitions and offset locations are
7557 * dependent on the if_type.
7558 */
7559 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
7560 &phba->sli4_hba.sli_intf.word0)) {
7561 return error;
7562 }
7563
7564 /* There is no SLI3 failback for SLI4 devices. */
7565 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
7566 LPFC_SLI_INTF_VALID) {
7567 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7568 "2894 SLI_INTF reg contents invalid "
7569 "sli_intf reg 0x%x\n",
7570 phba->sli4_hba.sli_intf.word0);
7571 return error;
7572 }
7573
7574 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7575 /*
7576 * Get the bus address of SLI4 device Bar regions and the
7577 * number of bytes required by each mapping. The mapping of the
7578 * particular PCI BARs regions is dependent on the type of
7579 * SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -04007580 */
James Smart1dfb5a42010-02-12 14:40:50 -05007581 if (pci_resource_start(pdev, 0)) {
7582 phba->pci_bar0_map = pci_resource_start(pdev, 0);
7583 bar0map_len = pci_resource_len(pdev, 0);
James Smart2fcee4b2010-12-15 17:57:46 -05007584
7585 /*
7586 * Map SLI4 PCI Config Space Register base to a kernel virtual
7587 * addr
7588 */
7589 phba->sli4_hba.conf_regs_memmap_p =
7590 ioremap(phba->pci_bar0_map, bar0map_len);
7591 if (!phba->sli4_hba.conf_regs_memmap_p) {
7592 dev_printk(KERN_ERR, &pdev->dev,
7593 "ioremap failed for SLI4 PCI config "
7594 "registers.\n");
7595 goto out;
7596 }
7597 /* Set up BAR0 PCI config space register memory map */
7598 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smart1dfb5a42010-02-12 14:40:50 -05007599 } else {
7600 phba->pci_bar0_map = pci_resource_start(pdev, 1);
7601 bar0map_len = pci_resource_len(pdev, 1);
James Smart2fcee4b2010-12-15 17:57:46 -05007602 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
7603 dev_printk(KERN_ERR, &pdev->dev,
7604 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
7605 goto out;
7606 }
7607 phba->sli4_hba.conf_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04007608 ioremap(phba->pci_bar0_map, bar0map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05007609 if (!phba->sli4_hba.conf_regs_memmap_p) {
7610 dev_printk(KERN_ERR, &pdev->dev,
7611 "ioremap failed for SLI4 PCI config "
7612 "registers.\n");
7613 goto out;
7614 }
7615 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smartda0436e2009-05-22 14:51:39 -04007616 }
7617
James Smartc31098c2011-04-16 11:03:33 -04007618 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
7619 (pci_resource_start(pdev, 2))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007620 /*
7621 * Map SLI4 if type 0 HBA Control Register base to a kernel
7622 * virtual address and setup the registers.
7623 */
7624 phba->pci_bar1_map = pci_resource_start(pdev, 2);
7625 bar1map_len = pci_resource_len(pdev, 2);
7626 phba->sli4_hba.ctrl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04007627 ioremap(phba->pci_bar1_map, bar1map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05007628 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
7629 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04007630 "ioremap failed for SLI4 HBA control registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05007631 goto out_iounmap_conf;
7632 }
7633 lpfc_sli4_bar1_register_memmap(phba);
James Smartda0436e2009-05-22 14:51:39 -04007634 }
7635
James Smartc31098c2011-04-16 11:03:33 -04007636 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
7637 (pci_resource_start(pdev, 4))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007638 /*
7639 * Map SLI4 if type 0 HBA Doorbell Register base to a kernel
7640 * virtual address and setup the registers.
7641 */
7642 phba->pci_bar2_map = pci_resource_start(pdev, 4);
7643 bar2map_len = pci_resource_len(pdev, 4);
7644 phba->sli4_hba.drbl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04007645 ioremap(phba->pci_bar2_map, bar2map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05007646 if (!phba->sli4_hba.drbl_regs_memmap_p) {
7647 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04007648 "ioremap failed for SLI4 HBA doorbell registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05007649 goto out_iounmap_ctrl;
7650 }
7651 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
7652 if (error)
7653 goto out_iounmap_all;
James Smartda0436e2009-05-22 14:51:39 -04007654 }
7655
James Smartda0436e2009-05-22 14:51:39 -04007656 return 0;
7657
7658out_iounmap_all:
7659 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
7660out_iounmap_ctrl:
7661 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
7662out_iounmap_conf:
7663 iounmap(phba->sli4_hba.conf_regs_memmap_p);
7664out:
7665 return error;
7666}
7667
7668/**
7669 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
7670 * @phba: pointer to lpfc hba data structure.
7671 *
7672 * This routine is invoked to unset the PCI device memory space for device
7673 * with SLI-4 interface spec.
7674 **/
7675static void
7676lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
7677{
James Smart2e90f4b2011-12-13 13:22:37 -05007678 uint32_t if_type;
7679 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smartda0436e2009-05-22 14:51:39 -04007680
James Smart2e90f4b2011-12-13 13:22:37 -05007681 switch (if_type) {
7682 case LPFC_SLI_INTF_IF_TYPE_0:
7683 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
7684 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
7685 iounmap(phba->sli4_hba.conf_regs_memmap_p);
7686 break;
7687 case LPFC_SLI_INTF_IF_TYPE_2:
7688 iounmap(phba->sli4_hba.conf_regs_memmap_p);
7689 break;
7690 case LPFC_SLI_INTF_IF_TYPE_1:
7691 default:
7692 dev_printk(KERN_ERR, &phba->pcidev->dev,
7693 "FATAL - unsupported SLI4 interface type - %d\n",
7694 if_type);
7695 break;
7696 }
James Smartda0436e2009-05-22 14:51:39 -04007697}
7698
7699/**
James Smart3772a992009-05-22 14:50:54 -04007700 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
7701 * @phba: pointer to lpfc hba data structure.
7702 *
7703 * This routine is invoked to enable the MSI-X interrupt vectors to device
7704 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
7705 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
7706 * invoked, enables either all or nothing, depending on the current
7707 * availability of PCI vector resources. The device driver is responsible
7708 * for calling the individual request_irq() to register each MSI-X vector
7709 * with a interrupt handler, which is done in this function. Note that
7710 * later when device is unloading, the driver should always call free_irq()
7711 * on all MSI-X vectors it has done request_irq() on before calling
7712 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
7713 * will be left with MSI-X enabled and leaks its vectors.
7714 *
7715 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007716 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007717 * other values - error
7718 **/
7719static int
7720lpfc_sli_enable_msix(struct lpfc_hba *phba)
7721{
7722 int rc, i;
7723 LPFC_MBOXQ_t *pmb;
7724
7725 /* Set up MSI-X multi-message vectors */
7726 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
7727 phba->msix_entries[i].entry = i;
7728
7729 /* Configure MSI-X capability structure */
7730 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
7731 ARRAY_SIZE(phba->msix_entries));
7732 if (rc) {
7733 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7734 "0420 PCI enable MSI-X failed (%d)\n", rc);
7735 goto msi_fail_out;
7736 }
7737 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
7738 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7739 "0477 MSI-X entry[%d]: vector=x%x "
7740 "message=%d\n", i,
7741 phba->msix_entries[i].vector,
7742 phba->msix_entries[i].entry);
7743 /*
7744 * Assign MSI-X vectors to interrupt handlers
7745 */
7746
7747 /* vector-0 is associated to slow-path handler */
7748 rc = request_irq(phba->msix_entries[0].vector,
7749 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
7750 LPFC_SP_DRIVER_HANDLER_NAME, phba);
7751 if (rc) {
7752 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7753 "0421 MSI-X slow-path request_irq failed "
7754 "(%d)\n", rc);
7755 goto msi_fail_out;
7756 }
7757
7758 /* vector-1 is associated to fast-path handler */
7759 rc = request_irq(phba->msix_entries[1].vector,
7760 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
7761 LPFC_FP_DRIVER_HANDLER_NAME, phba);
7762
7763 if (rc) {
7764 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7765 "0429 MSI-X fast-path request_irq failed "
7766 "(%d)\n", rc);
7767 goto irq_fail_out;
7768 }
7769
7770 /*
7771 * Configure HBA MSI-X attention conditions to messages
7772 */
7773 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7774
7775 if (!pmb) {
7776 rc = -ENOMEM;
7777 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7778 "0474 Unable to allocate memory for issuing "
7779 "MBOX_CONFIG_MSI command\n");
7780 goto mem_fail_out;
7781 }
7782 rc = lpfc_config_msi(phba, pmb);
7783 if (rc)
7784 goto mbx_fail_out;
7785 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
7786 if (rc != MBX_SUCCESS) {
7787 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
7788 "0351 Config MSI mailbox command failed, "
7789 "mbxCmd x%x, mbxStatus x%x\n",
7790 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
7791 goto mbx_fail_out;
7792 }
7793
7794 /* Free memory allocated for mailbox command */
7795 mempool_free(pmb, phba->mbox_mem_pool);
7796 return rc;
7797
7798mbx_fail_out:
7799 /* Free memory allocated for mailbox command */
7800 mempool_free(pmb, phba->mbox_mem_pool);
7801
7802mem_fail_out:
7803 /* free the irq already requested */
7804 free_irq(phba->msix_entries[1].vector, phba);
7805
7806irq_fail_out:
7807 /* free the irq already requested */
7808 free_irq(phba->msix_entries[0].vector, phba);
7809
7810msi_fail_out:
7811 /* Unconfigure MSI-X capability structure */
7812 pci_disable_msix(phba->pcidev);
7813 return rc;
7814}
7815
7816/**
7817 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
7818 * @phba: pointer to lpfc hba data structure.
7819 *
7820 * This routine is invoked to release the MSI-X vectors and then disable the
7821 * MSI-X interrupt mode to device with SLI-3 interface spec.
7822 **/
7823static void
7824lpfc_sli_disable_msix(struct lpfc_hba *phba)
7825{
7826 int i;
7827
7828 /* Free up MSI-X multi-message vectors */
7829 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
7830 free_irq(phba->msix_entries[i].vector, phba);
7831 /* Disable MSI-X */
7832 pci_disable_msix(phba->pcidev);
7833
7834 return;
7835}
7836
7837/**
7838 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
7839 * @phba: pointer to lpfc hba data structure.
7840 *
7841 * This routine is invoked to enable the MSI interrupt mode to device with
7842 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
7843 * enable the MSI vector. The device driver is responsible for calling the
7844 * request_irq() to register MSI vector with a interrupt the handler, which
7845 * is done in this function.
7846 *
7847 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007848 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007849 * other values - error
7850 */
7851static int
7852lpfc_sli_enable_msi(struct lpfc_hba *phba)
7853{
7854 int rc;
7855
7856 rc = pci_enable_msi(phba->pcidev);
7857 if (!rc)
7858 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7859 "0462 PCI enable MSI mode success.\n");
7860 else {
7861 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7862 "0471 PCI enable MSI mode failed (%d)\n", rc);
7863 return rc;
7864 }
7865
7866 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
7867 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7868 if (rc) {
7869 pci_disable_msi(phba->pcidev);
7870 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7871 "0478 MSI request_irq failed (%d)\n", rc);
7872 }
7873 return rc;
7874}
7875
7876/**
7877 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
7878 * @phba: pointer to lpfc hba data structure.
7879 *
7880 * This routine is invoked to disable the MSI interrupt mode to device with
7881 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
7882 * done request_irq() on before calling pci_disable_msi(). Failure to do so
7883 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
7884 * its vector.
7885 */
7886static void
7887lpfc_sli_disable_msi(struct lpfc_hba *phba)
7888{
7889 free_irq(phba->pcidev->irq, phba);
7890 pci_disable_msi(phba->pcidev);
7891 return;
7892}
7893
7894/**
7895 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
7896 * @phba: pointer to lpfc hba data structure.
7897 *
7898 * This routine is invoked to enable device interrupt and associate driver's
7899 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
7900 * spec. Depends on the interrupt mode configured to the driver, the driver
7901 * will try to fallback from the configured interrupt mode to an interrupt
7902 * mode which is supported by the platform, kernel, and device in the order
7903 * of:
7904 * MSI-X -> MSI -> IRQ.
7905 *
7906 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007907 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007908 * other values - error
7909 **/
7910static uint32_t
7911lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
7912{
7913 uint32_t intr_mode = LPFC_INTR_ERROR;
7914 int retval;
7915
7916 if (cfg_mode == 2) {
7917 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
7918 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
7919 if (!retval) {
7920 /* Now, try to enable MSI-X interrupt mode */
7921 retval = lpfc_sli_enable_msix(phba);
7922 if (!retval) {
7923 /* Indicate initialization to MSI-X mode */
7924 phba->intr_type = MSIX;
7925 intr_mode = 2;
7926 }
7927 }
7928 }
7929
7930 /* Fallback to MSI if MSI-X initialization failed */
7931 if (cfg_mode >= 1 && phba->intr_type == NONE) {
7932 retval = lpfc_sli_enable_msi(phba);
7933 if (!retval) {
7934 /* Indicate initialization to MSI mode */
7935 phba->intr_type = MSI;
7936 intr_mode = 1;
7937 }
7938 }
7939
7940 /* Fallback to INTx if both MSI-X/MSI initalization failed */
7941 if (phba->intr_type == NONE) {
7942 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
7943 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7944 if (!retval) {
7945 /* Indicate initialization to INTx mode */
7946 phba->intr_type = INTx;
7947 intr_mode = 0;
7948 }
7949 }
7950 return intr_mode;
7951}
7952
7953/**
7954 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
7955 * @phba: pointer to lpfc hba data structure.
7956 *
7957 * This routine is invoked to disable device interrupt and disassociate the
7958 * driver's interrupt handler(s) from interrupt vector(s) to device with
7959 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
7960 * release the interrupt vector(s) for the message signaled interrupt.
7961 **/
7962static void
7963lpfc_sli_disable_intr(struct lpfc_hba *phba)
7964{
7965 /* Disable the currently initialized interrupt mode */
7966 if (phba->intr_type == MSIX)
7967 lpfc_sli_disable_msix(phba);
7968 else if (phba->intr_type == MSI)
7969 lpfc_sli_disable_msi(phba);
7970 else if (phba->intr_type == INTx)
7971 free_irq(phba->pcidev->irq, phba);
7972
7973 /* Reset interrupt management states */
7974 phba->intr_type = NONE;
7975 phba->sli.slistat.sli_intr = 0;
7976
7977 return;
7978}
7979
7980/**
James Smartda0436e2009-05-22 14:51:39 -04007981 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
7982 * @phba: pointer to lpfc hba data structure.
7983 *
7984 * This routine is invoked to enable the MSI-X interrupt vectors to device
7985 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
7986 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
7987 * enables either all or nothing, depending on the current availability of
7988 * PCI vector resources. The device driver is responsible for calling the
7989 * individual request_irq() to register each MSI-X vector with a interrupt
7990 * handler, which is done in this function. Note that later when device is
7991 * unloading, the driver should always call free_irq() on all MSI-X vectors
7992 * it has done request_irq() on before calling pci_disable_msix(). Failure
7993 * to do so results in a BUG_ON() and a device will be left with MSI-X
7994 * enabled and leaks its vectors.
7995 *
7996 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007997 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007998 * other values - error
7999 **/
8000static int
8001lpfc_sli4_enable_msix(struct lpfc_hba *phba)
8002{
James Smart75baf692010-06-08 18:31:21 -04008003 int vectors, rc, index;
James Smartda0436e2009-05-22 14:51:39 -04008004
8005 /* Set up MSI-X multi-message vectors */
8006 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
8007 phba->sli4_hba.msix_entries[index].entry = index;
8008
8009 /* Configure MSI-X capability structure */
James Smart75baf692010-06-08 18:31:21 -04008010 vectors = phba->sli4_hba.cfg_eqn;
8011enable_msix_vectors:
James Smartda0436e2009-05-22 14:51:39 -04008012 rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
James Smart75baf692010-06-08 18:31:21 -04008013 vectors);
8014 if (rc > 1) {
8015 vectors = rc;
8016 goto enable_msix_vectors;
8017 } else if (rc) {
James Smartda0436e2009-05-22 14:51:39 -04008018 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8019 "0484 PCI enable MSI-X failed (%d)\n", rc);
8020 goto msi_fail_out;
8021 }
James Smart75baf692010-06-08 18:31:21 -04008022
James Smartda0436e2009-05-22 14:51:39 -04008023 /* Log MSI-X vector assignment */
James Smart75baf692010-06-08 18:31:21 -04008024 for (index = 0; index < vectors; index++)
James Smartda0436e2009-05-22 14:51:39 -04008025 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8026 "0489 MSI-X entry[%d]: vector=x%x "
8027 "message=%d\n", index,
8028 phba->sli4_hba.msix_entries[index].vector,
8029 phba->sli4_hba.msix_entries[index].entry);
8030 /*
8031 * Assign MSI-X vectors to interrupt handlers
8032 */
James Smart05580562011-05-24 11:40:48 -04008033 if (vectors > 1)
8034 rc = request_irq(phba->sli4_hba.msix_entries[0].vector,
8035 &lpfc_sli4_sp_intr_handler, IRQF_SHARED,
8036 LPFC_SP_DRIVER_HANDLER_NAME, phba);
8037 else
8038 /* All Interrupts need to be handled by one EQ */
8039 rc = request_irq(phba->sli4_hba.msix_entries[0].vector,
8040 &lpfc_sli4_intr_handler, IRQF_SHARED,
8041 LPFC_DRIVER_NAME, phba);
James Smartda0436e2009-05-22 14:51:39 -04008042 if (rc) {
8043 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8044 "0485 MSI-X slow-path request_irq failed "
8045 "(%d)\n", rc);
8046 goto msi_fail_out;
8047 }
8048
8049 /* The rest of the vector(s) are associated to fast-path handler(s) */
James Smart75baf692010-06-08 18:31:21 -04008050 for (index = 1; index < vectors; index++) {
James Smartda0436e2009-05-22 14:51:39 -04008051 phba->sli4_hba.fcp_eq_hdl[index - 1].idx = index - 1;
8052 phba->sli4_hba.fcp_eq_hdl[index - 1].phba = phba;
8053 rc = request_irq(phba->sli4_hba.msix_entries[index].vector,
8054 &lpfc_sli4_fp_intr_handler, IRQF_SHARED,
8055 LPFC_FP_DRIVER_HANDLER_NAME,
8056 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
8057 if (rc) {
8058 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8059 "0486 MSI-X fast-path (%d) "
8060 "request_irq failed (%d)\n", index, rc);
8061 goto cfg_fail_out;
8062 }
8063 }
James Smart75baf692010-06-08 18:31:21 -04008064 phba->sli4_hba.msix_vec_nr = vectors;
James Smartda0436e2009-05-22 14:51:39 -04008065
8066 return rc;
8067
8068cfg_fail_out:
8069 /* free the irq already requested */
8070 for (--index; index >= 1; index--)
8071 free_irq(phba->sli4_hba.msix_entries[index - 1].vector,
8072 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
8073
8074 /* free the irq already requested */
8075 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
8076
8077msi_fail_out:
8078 /* Unconfigure MSI-X capability structure */
8079 pci_disable_msix(phba->pcidev);
8080 return rc;
8081}
8082
8083/**
8084 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
8085 * @phba: pointer to lpfc hba data structure.
8086 *
8087 * This routine is invoked to release the MSI-X vectors and then disable the
8088 * MSI-X interrupt mode to device with SLI-4 interface spec.
8089 **/
8090static void
8091lpfc_sli4_disable_msix(struct lpfc_hba *phba)
8092{
8093 int index;
8094
8095 /* Free up MSI-X multi-message vectors */
8096 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
8097
James Smart75baf692010-06-08 18:31:21 -04008098 for (index = 1; index < phba->sli4_hba.msix_vec_nr; index++)
James Smartda0436e2009-05-22 14:51:39 -04008099 free_irq(phba->sli4_hba.msix_entries[index].vector,
8100 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
James Smart75baf692010-06-08 18:31:21 -04008101
James Smartda0436e2009-05-22 14:51:39 -04008102 /* Disable MSI-X */
8103 pci_disable_msix(phba->pcidev);
8104
8105 return;
8106}
8107
8108/**
8109 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
8110 * @phba: pointer to lpfc hba data structure.
8111 *
8112 * This routine is invoked to enable the MSI interrupt mode to device with
8113 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
8114 * to enable the MSI vector. The device driver is responsible for calling
8115 * the request_irq() to register MSI vector with a interrupt the handler,
8116 * which is done in this function.
8117 *
8118 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008119 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008120 * other values - error
8121 **/
8122static int
8123lpfc_sli4_enable_msi(struct lpfc_hba *phba)
8124{
8125 int rc, index;
8126
8127 rc = pci_enable_msi(phba->pcidev);
8128 if (!rc)
8129 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8130 "0487 PCI enable MSI mode success.\n");
8131 else {
8132 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8133 "0488 PCI enable MSI mode failed (%d)\n", rc);
8134 return rc;
8135 }
8136
8137 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
8138 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8139 if (rc) {
8140 pci_disable_msi(phba->pcidev);
8141 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8142 "0490 MSI request_irq failed (%d)\n", rc);
James Smart75baf692010-06-08 18:31:21 -04008143 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008144 }
8145
8146 for (index = 0; index < phba->cfg_fcp_eq_count; index++) {
8147 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8148 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
8149 }
8150
James Smart75baf692010-06-08 18:31:21 -04008151 return 0;
James Smartda0436e2009-05-22 14:51:39 -04008152}
8153
8154/**
8155 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
8156 * @phba: pointer to lpfc hba data structure.
8157 *
8158 * This routine is invoked to disable the MSI interrupt mode to device with
8159 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
8160 * done request_irq() on before calling pci_disable_msi(). Failure to do so
8161 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
8162 * its vector.
8163 **/
8164static void
8165lpfc_sli4_disable_msi(struct lpfc_hba *phba)
8166{
8167 free_irq(phba->pcidev->irq, phba);
8168 pci_disable_msi(phba->pcidev);
8169 return;
8170}
8171
8172/**
8173 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
8174 * @phba: pointer to lpfc hba data structure.
8175 *
8176 * This routine is invoked to enable device interrupt and associate driver's
8177 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
8178 * interface spec. Depends on the interrupt mode configured to the driver,
8179 * the driver will try to fallback from the configured interrupt mode to an
8180 * interrupt mode which is supported by the platform, kernel, and device in
8181 * the order of:
8182 * MSI-X -> MSI -> IRQ.
8183 *
8184 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008185 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008186 * other values - error
8187 **/
8188static uint32_t
8189lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
8190{
8191 uint32_t intr_mode = LPFC_INTR_ERROR;
8192 int retval, index;
8193
8194 if (cfg_mode == 2) {
8195 /* Preparation before conf_msi mbox cmd */
8196 retval = 0;
8197 if (!retval) {
8198 /* Now, try to enable MSI-X interrupt mode */
8199 retval = lpfc_sli4_enable_msix(phba);
8200 if (!retval) {
8201 /* Indicate initialization to MSI-X mode */
8202 phba->intr_type = MSIX;
8203 intr_mode = 2;
8204 }
8205 }
8206 }
8207
8208 /* Fallback to MSI if MSI-X initialization failed */
8209 if (cfg_mode >= 1 && phba->intr_type == NONE) {
8210 retval = lpfc_sli4_enable_msi(phba);
8211 if (!retval) {
8212 /* Indicate initialization to MSI mode */
8213 phba->intr_type = MSI;
8214 intr_mode = 1;
8215 }
8216 }
8217
8218 /* Fallback to INTx if both MSI-X/MSI initalization failed */
8219 if (phba->intr_type == NONE) {
8220 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
8221 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8222 if (!retval) {
8223 /* Indicate initialization to INTx mode */
8224 phba->intr_type = INTx;
8225 intr_mode = 0;
8226 for (index = 0; index < phba->cfg_fcp_eq_count;
8227 index++) {
8228 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8229 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
8230 }
8231 }
8232 }
8233 return intr_mode;
8234}
8235
8236/**
8237 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
8238 * @phba: pointer to lpfc hba data structure.
8239 *
8240 * This routine is invoked to disable device interrupt and disassociate
8241 * the driver's interrupt handler(s) from interrupt vector(s) to device
8242 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
8243 * will release the interrupt vector(s) for the message signaled interrupt.
8244 **/
8245static void
8246lpfc_sli4_disable_intr(struct lpfc_hba *phba)
8247{
8248 /* Disable the currently initialized interrupt mode */
8249 if (phba->intr_type == MSIX)
8250 lpfc_sli4_disable_msix(phba);
8251 else if (phba->intr_type == MSI)
8252 lpfc_sli4_disable_msi(phba);
8253 else if (phba->intr_type == INTx)
8254 free_irq(phba->pcidev->irq, phba);
8255
8256 /* Reset interrupt management states */
8257 phba->intr_type = NONE;
8258 phba->sli.slistat.sli_intr = 0;
8259
8260 return;
8261}
8262
8263/**
James Smart3772a992009-05-22 14:50:54 -04008264 * lpfc_unset_hba - Unset SLI3 hba device initialization
8265 * @phba: pointer to lpfc hba data structure.
8266 *
8267 * This routine is invoked to unset the HBA device initialization steps to
8268 * a device with SLI-3 interface spec.
8269 **/
8270static void
8271lpfc_unset_hba(struct lpfc_hba *phba)
8272{
8273 struct lpfc_vport *vport = phba->pport;
8274 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
8275
8276 spin_lock_irq(shost->host_lock);
8277 vport->load_flag |= FC_UNLOADING;
8278 spin_unlock_irq(shost->host_lock);
8279
James Smart72859902012-01-18 16:25:38 -05008280 kfree(phba->vpi_bmask);
8281 kfree(phba->vpi_ids);
8282
James Smart3772a992009-05-22 14:50:54 -04008283 lpfc_stop_hba_timers(phba);
8284
8285 phba->pport->work_port_events = 0;
8286
8287 lpfc_sli_hba_down(phba);
8288
8289 lpfc_sli_brdrestart(phba);
8290
8291 lpfc_sli_disable_intr(phba);
8292
8293 return;
8294}
8295
8296/**
James Smartda0436e2009-05-22 14:51:39 -04008297 * lpfc_sli4_unset_hba - Unset SLI4 hba device initialization.
8298 * @phba: pointer to lpfc hba data structure.
8299 *
8300 * This routine is invoked to unset the HBA device initialization steps to
8301 * a device with SLI-4 interface spec.
8302 **/
8303static void
8304lpfc_sli4_unset_hba(struct lpfc_hba *phba)
8305{
8306 struct lpfc_vport *vport = phba->pport;
8307 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
8308
8309 spin_lock_irq(shost->host_lock);
8310 vport->load_flag |= FC_UNLOADING;
8311 spin_unlock_irq(shost->host_lock);
8312
8313 phba->pport->work_port_events = 0;
8314
James Smart3677a3a2010-09-29 11:19:14 -04008315 /* Stop the SLI4 device port */
8316 lpfc_stop_port(phba);
James Smartda0436e2009-05-22 14:51:39 -04008317
8318 lpfc_sli4_disable_intr(phba);
8319
James Smart3677a3a2010-09-29 11:19:14 -04008320 /* Reset SLI4 HBA FCoE function */
8321 lpfc_pci_function_reset(phba);
James Smart5350d872011-10-10 21:33:49 -04008322 lpfc_sli4_queue_destroy(phba);
James Smart3677a3a2010-09-29 11:19:14 -04008323
James Smartda0436e2009-05-22 14:51:39 -04008324 return;
8325}
8326
8327/**
James Smart5af5eee2010-10-22 11:06:38 -04008328 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
8329 * @phba: Pointer to HBA context object.
8330 *
8331 * This function is called in the SLI4 code path to wait for completion
8332 * of device's XRIs exchange busy. It will check the XRI exchange busy
8333 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
8334 * that, it will check the XRI exchange busy on outstanding FCP and ELS
8335 * I/Os every 30 seconds, log error message, and wait forever. Only when
8336 * all XRI exchange busy complete, the driver unload shall proceed with
8337 * invoking the function reset ioctl mailbox command to the CNA and the
8338 * the rest of the driver unload resource release.
8339 **/
8340static void
8341lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
8342{
8343 int wait_time = 0;
8344 int fcp_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
8345 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
8346
8347 while (!fcp_xri_cmpl || !els_xri_cmpl) {
8348 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
8349 if (!fcp_xri_cmpl)
8350 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8351 "2877 FCP XRI exchange busy "
8352 "wait time: %d seconds.\n",
8353 wait_time/1000);
8354 if (!els_xri_cmpl)
8355 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8356 "2878 ELS XRI exchange busy "
8357 "wait time: %d seconds.\n",
8358 wait_time/1000);
8359 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
8360 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
8361 } else {
8362 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
8363 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
8364 }
8365 fcp_xri_cmpl =
8366 list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
8367 els_xri_cmpl =
8368 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
8369 }
8370}
8371
8372/**
James Smartda0436e2009-05-22 14:51:39 -04008373 * lpfc_sli4_hba_unset - Unset the fcoe hba
8374 * @phba: Pointer to HBA context object.
8375 *
8376 * This function is called in the SLI4 code path to reset the HBA's FCoE
8377 * function. The caller is not required to hold any lock. This routine
8378 * issues PCI function reset mailbox command to reset the FCoE function.
8379 * At the end of the function, it calls lpfc_hba_down_post function to
8380 * free any pending commands.
8381 **/
8382static void
8383lpfc_sli4_hba_unset(struct lpfc_hba *phba)
8384{
8385 int wait_cnt = 0;
8386 LPFC_MBOXQ_t *mboxq;
James Smart912e3ac2011-05-24 11:42:11 -04008387 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -04008388
8389 lpfc_stop_hba_timers(phba);
8390 phba->sli4_hba.intr_enable = 0;
8391
8392 /*
8393 * Gracefully wait out the potential current outstanding asynchronous
8394 * mailbox command.
8395 */
8396
8397 /* First, block any pending async mailbox command from posted */
8398 spin_lock_irq(&phba->hbalock);
8399 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
8400 spin_unlock_irq(&phba->hbalock);
8401 /* Now, trying to wait it out if we can */
8402 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8403 msleep(10);
8404 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
8405 break;
8406 }
8407 /* Forcefully release the outstanding mailbox command if timed out */
8408 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8409 spin_lock_irq(&phba->hbalock);
8410 mboxq = phba->sli.mbox_active;
8411 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8412 __lpfc_mbox_cmpl_put(phba, mboxq);
8413 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8414 phba->sli.mbox_active = NULL;
8415 spin_unlock_irq(&phba->hbalock);
8416 }
8417
James Smart5af5eee2010-10-22 11:06:38 -04008418 /* Abort all iocbs associated with the hba */
8419 lpfc_sli_hba_iocb_abort(phba);
8420
8421 /* Wait for completion of device XRI exchange busy */
8422 lpfc_sli4_xri_exchange_busy_wait(phba);
8423
James Smartda0436e2009-05-22 14:51:39 -04008424 /* Disable PCI subsystem interrupt */
8425 lpfc_sli4_disable_intr(phba);
8426
James Smart912e3ac2011-05-24 11:42:11 -04008427 /* Disable SR-IOV if enabled */
8428 if (phba->cfg_sriov_nr_virtfn)
8429 pci_disable_sriov(pdev);
8430
James Smartda0436e2009-05-22 14:51:39 -04008431 /* Stop kthread signal shall trigger work_done one more time */
8432 kthread_stop(phba->worker_thread);
8433
James Smart3677a3a2010-09-29 11:19:14 -04008434 /* Reset SLI4 HBA FCoE function */
8435 lpfc_pci_function_reset(phba);
James Smart5350d872011-10-10 21:33:49 -04008436 lpfc_sli4_queue_destroy(phba);
James Smart3677a3a2010-09-29 11:19:14 -04008437
James Smartda0436e2009-05-22 14:51:39 -04008438 /* Stop the SLI4 device port */
8439 phba->pport->work_port_events = 0;
8440}
8441
James Smart28baac72010-02-12 14:42:03 -05008442 /**
8443 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
8444 * @phba: Pointer to HBA context object.
8445 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
8446 *
8447 * This function is called in the SLI4 code path to read the port's
8448 * sli4 capabilities.
8449 *
8450 * This function may be be called from any context that can block-wait
8451 * for the completion. The expectation is that this routine is called
8452 * typically from probe_one or from the online routine.
8453 **/
8454int
8455lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8456{
8457 int rc;
8458 struct lpfc_mqe *mqe;
8459 struct lpfc_pc_sli4_params *sli4_params;
8460 uint32_t mbox_tmo;
8461
8462 rc = 0;
8463 mqe = &mboxq->u.mqe;
8464
8465 /* Read the port's SLI4 Parameters port capabilities */
James Smartfedd3b72011-02-16 12:39:24 -05008466 lpfc_pc_sli4_params(mboxq);
James Smart28baac72010-02-12 14:42:03 -05008467 if (!phba->sli4_hba.intr_enable)
8468 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8469 else {
James Smarta183a152011-10-10 21:32:43 -04008470 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart28baac72010-02-12 14:42:03 -05008471 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
8472 }
8473
8474 if (unlikely(rc))
8475 return 1;
8476
8477 sli4_params = &phba->sli4_hba.pc_sli4_params;
8478 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
8479 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
8480 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
8481 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
8482 &mqe->un.sli4_params);
8483 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
8484 &mqe->un.sli4_params);
8485 sli4_params->proto_types = mqe->un.sli4_params.word3;
8486 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
8487 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
8488 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
8489 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
8490 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
8491 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
8492 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
8493 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
8494 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
8495 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
8496 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
8497 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
8498 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
8499 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
8500 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
8501 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
8502 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
8503 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
8504 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
8505 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
James Smart05580562011-05-24 11:40:48 -04008506
8507 /* Make sure that sge_supp_len can be handled by the driver */
8508 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
8509 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
8510
James Smart28baac72010-02-12 14:42:03 -05008511 return rc;
8512}
8513
James Smartda0436e2009-05-22 14:51:39 -04008514/**
James Smartfedd3b72011-02-16 12:39:24 -05008515 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
8516 * @phba: Pointer to HBA context object.
8517 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
8518 *
8519 * This function is called in the SLI4 code path to read the port's
8520 * sli4 capabilities.
8521 *
8522 * This function may be be called from any context that can block-wait
8523 * for the completion. The expectation is that this routine is called
8524 * typically from probe_one or from the online routine.
8525 **/
8526int
8527lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8528{
8529 int rc;
8530 struct lpfc_mqe *mqe = &mboxq->u.mqe;
8531 struct lpfc_pc_sli4_params *sli4_params;
James Smarta183a152011-10-10 21:32:43 -04008532 uint32_t mbox_tmo;
James Smartfedd3b72011-02-16 12:39:24 -05008533 int length;
8534 struct lpfc_sli4_parameters *mbx_sli4_parameters;
8535
James Smart6d368e52011-05-24 11:44:12 -04008536 /*
8537 * By default, the driver assumes the SLI4 port requires RPI
8538 * header postings. The SLI4_PARAM response will correct this
8539 * assumption.
8540 */
8541 phba->sli4_hba.rpi_hdrs_in_use = 1;
8542
James Smartfedd3b72011-02-16 12:39:24 -05008543 /* Read the port's SLI4 Config Parameters */
8544 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
8545 sizeof(struct lpfc_sli4_cfg_mhdr));
8546 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
8547 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
8548 length, LPFC_SLI4_MBX_EMBED);
8549 if (!phba->sli4_hba.intr_enable)
8550 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -04008551 else {
8552 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
8553 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
8554 }
James Smartfedd3b72011-02-16 12:39:24 -05008555 if (unlikely(rc))
8556 return rc;
8557 sli4_params = &phba->sli4_hba.pc_sli4_params;
8558 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
8559 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
8560 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
8561 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
8562 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
8563 mbx_sli4_parameters);
8564 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
8565 mbx_sli4_parameters);
8566 if (bf_get(cfg_phwq, mbx_sli4_parameters))
8567 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
8568 else
8569 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
8570 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
8571 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
8572 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
8573 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
8574 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
8575 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
8576 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
8577 mbx_sli4_parameters);
8578 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
8579 mbx_sli4_parameters);
James Smart6d368e52011-05-24 11:44:12 -04008580 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
8581 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
James Smart05580562011-05-24 11:40:48 -04008582
8583 /* Make sure that sge_supp_len can be handled by the driver */
8584 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
8585 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
8586
James Smartfedd3b72011-02-16 12:39:24 -05008587 return 0;
8588}
8589
8590/**
James Smart3772a992009-05-22 14:50:54 -04008591 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
8592 * @pdev: pointer to PCI device
8593 * @pid: pointer to PCI device identifier
8594 *
8595 * This routine is to be called to attach a device with SLI-3 interface spec
8596 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
8597 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
8598 * information of the device and driver to see if the driver state that it can
8599 * support this kind of device. If the match is successful, the driver core
8600 * invokes this routine. If this routine determines it can claim the HBA, it
8601 * does all the initialization that it needs to do to handle the HBA properly.
8602 *
8603 * Return code
8604 * 0 - driver can claim the device
8605 * negative value - driver can not claim the device
8606 **/
8607static int __devinit
8608lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
8609{
8610 struct lpfc_hba *phba;
8611 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04008612 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -04008613 int error;
8614 uint32_t cfg_mode, intr_mode;
8615
8616 /* Allocate memory for HBA structure */
8617 phba = lpfc_hba_alloc(pdev);
8618 if (!phba)
8619 return -ENOMEM;
8620
8621 /* Perform generic PCI device enabling operation */
8622 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -04008623 if (error)
James Smart3772a992009-05-22 14:50:54 -04008624 goto out_free_phba;
James Smart3772a992009-05-22 14:50:54 -04008625
8626 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
8627 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
8628 if (error)
8629 goto out_disable_pci_dev;
8630
8631 /* Set up SLI-3 specific device PCI memory space */
8632 error = lpfc_sli_pci_mem_setup(phba);
8633 if (error) {
8634 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8635 "1402 Failed to set up pci memory space.\n");
8636 goto out_disable_pci_dev;
8637 }
8638
8639 /* Set up phase-1 common device driver resources */
8640 error = lpfc_setup_driver_resource_phase1(phba);
8641 if (error) {
8642 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8643 "1403 Failed to set up driver resource.\n");
8644 goto out_unset_pci_mem_s3;
8645 }
8646
8647 /* Set up SLI-3 specific device driver resources */
8648 error = lpfc_sli_driver_resource_setup(phba);
8649 if (error) {
8650 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8651 "1404 Failed to set up driver resource.\n");
8652 goto out_unset_pci_mem_s3;
8653 }
8654
8655 /* Initialize and populate the iocb list per host */
8656 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
8657 if (error) {
8658 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8659 "1405 Failed to initialize iocb list.\n");
8660 goto out_unset_driver_resource_s3;
8661 }
8662
8663 /* Set up common device driver resources */
8664 error = lpfc_setup_driver_resource_phase2(phba);
8665 if (error) {
8666 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8667 "1406 Failed to set up driver resource.\n");
8668 goto out_free_iocb_list;
8669 }
8670
James Smart079b5c92011-08-21 21:48:49 -04008671 /* Get the default values for Model Name and Description */
8672 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
8673
James Smart3772a992009-05-22 14:50:54 -04008674 /* Create SCSI host to the physical port */
8675 error = lpfc_create_shost(phba);
8676 if (error) {
8677 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8678 "1407 Failed to create scsi host.\n");
8679 goto out_unset_driver_resource;
8680 }
8681
8682 /* Configure sysfs attributes */
8683 vport = phba->pport;
8684 error = lpfc_alloc_sysfs_attr(vport);
8685 if (error) {
8686 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8687 "1476 Failed to allocate sysfs attr\n");
8688 goto out_destroy_shost;
8689 }
8690
James Smart6669f9b2009-10-02 15:16:45 -04008691 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -04008692 /* Now, trying to enable interrupt and bring up the device */
8693 cfg_mode = phba->cfg_use_msi;
8694 while (true) {
8695 /* Put device to a known state before enabling interrupt */
8696 lpfc_stop_port(phba);
8697 /* Configure and enable interrupt */
8698 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
8699 if (intr_mode == LPFC_INTR_ERROR) {
8700 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8701 "0431 Failed to enable interrupt.\n");
8702 error = -ENODEV;
8703 goto out_free_sysfs_attr;
8704 }
8705 /* SLI-3 HBA setup */
8706 if (lpfc_sli_hba_setup(phba)) {
8707 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8708 "1477 Failed to set up hba\n");
8709 error = -ENODEV;
8710 goto out_remove_device;
8711 }
8712
8713 /* Wait 50ms for the interrupts of previous mailbox commands */
8714 msleep(50);
8715 /* Check active interrupts on message signaled interrupts */
8716 if (intr_mode == 0 ||
8717 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
8718 /* Log the current active interrupt mode */
8719 phba->intr_mode = intr_mode;
8720 lpfc_log_intr_mode(phba, intr_mode);
8721 break;
8722 } else {
8723 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8724 "0447 Configure interrupt mode (%d) "
8725 "failed active interrupt test.\n",
8726 intr_mode);
8727 /* Disable the current interrupt mode */
8728 lpfc_sli_disable_intr(phba);
8729 /* Try next level of interrupt mode */
8730 cfg_mode = --intr_mode;
8731 }
8732 }
8733
8734 /* Perform post initialization setup */
8735 lpfc_post_init_setup(phba);
8736
8737 /* Check if there are static vports to be created. */
8738 lpfc_create_static_vport(phba);
8739
8740 return 0;
8741
8742out_remove_device:
8743 lpfc_unset_hba(phba);
8744out_free_sysfs_attr:
8745 lpfc_free_sysfs_attr(vport);
8746out_destroy_shost:
8747 lpfc_destroy_shost(phba);
8748out_unset_driver_resource:
8749 lpfc_unset_driver_resource_phase2(phba);
8750out_free_iocb_list:
8751 lpfc_free_iocb_list(phba);
8752out_unset_driver_resource_s3:
8753 lpfc_sli_driver_resource_unset(phba);
8754out_unset_pci_mem_s3:
8755 lpfc_sli_pci_mem_unset(phba);
8756out_disable_pci_dev:
8757 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04008758 if (shost)
8759 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -04008760out_free_phba:
8761 lpfc_hba_free(phba);
8762 return error;
8763}
8764
8765/**
8766 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -04008767 * @pdev: pointer to PCI device
8768 *
James Smart3772a992009-05-22 14:50:54 -04008769 * This routine is to be called to disattach a device with SLI-3 interface
8770 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
8771 * removed from PCI bus, it performs all the necessary cleanup for the HBA
8772 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -04008773 **/
dea31012005-04-17 16:05:31 -05008774static void __devexit
James Smart3772a992009-05-22 14:50:54 -04008775lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -05008776{
James Smart2e0fef82007-06-17 19:56:36 -05008777 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8778 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -05008779 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -05008780 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -05008781 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05008782 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
8783
James Smart549e55c2007-08-02 11:09:51 -04008784 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04008785 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04008786 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008787
James Smart858c9f62007-06-17 19:56:39 -05008788 lpfc_free_sysfs_attr(vport);
8789
James Smarteada2722008-12-04 22:39:13 -05008790 /* Release all the vports against this physical port */
8791 vports = lpfc_create_vport_work_array(phba);
8792 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -04008793 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
8794 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
8795 continue;
James Smarteada2722008-12-04 22:39:13 -05008796 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -04008797 }
James Smarteada2722008-12-04 22:39:13 -05008798 lpfc_destroy_vport_work_array(phba, vports);
8799
8800 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -05008801 fc_remove_host(shost);
8802 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04008803 lpfc_cleanup(vport);
8804
James Smart2e0fef82007-06-17 19:56:36 -05008805 /*
8806 * Bring down the SLI Layer. This step disable all interrupts,
8807 * clears the rings, discards all mailbox commands, and resets
8808 * the HBA.
8809 */
James Smarta257bf92009-04-06 18:48:10 -04008810
Justin P. Mattock48e34d02010-12-30 15:07:58 -08008811 /* HBA interrupt will be disabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -05008812 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -04008813 /* Stop kthread signal shall trigger work_done one more time */
8814 kthread_stop(phba->worker_thread);
8815 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -05008816 lpfc_sli_brdrestart(phba);
8817
James Smart72859902012-01-18 16:25:38 -05008818 kfree(phba->vpi_bmask);
8819 kfree(phba->vpi_ids);
8820
James Smart3772a992009-05-22 14:50:54 -04008821 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05008822 spin_lock_irq(&phba->hbalock);
8823 list_del_init(&vport->listentry);
8824 spin_unlock_irq(&phba->hbalock);
8825
James Smart858c9f62007-06-17 19:56:39 -05008826 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05008827
James Smart912e3ac2011-05-24 11:42:11 -04008828 /* Disable SR-IOV if enabled */
8829 if (phba->cfg_sriov_nr_virtfn)
8830 pci_disable_sriov(pdev);
8831
James Smart5b75da22008-12-04 22:39:35 -05008832 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04008833 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -05008834
8835 pci_set_drvdata(pdev, NULL);
James Smart858c9f62007-06-17 19:56:39 -05008836 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05008837
8838 /*
8839 * Call scsi_free before mem_free since scsi bufs are released to their
8840 * corresponding pools here.
8841 */
8842 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -04008843 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -05008844
James Smart34b02dc2008-08-24 21:49:55 -04008845 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
8846 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -05008847
James Smart2e0fef82007-06-17 19:56:36 -05008848 /* Free resources associated with SLI2 interface */
8849 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -04008850 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -05008851
8852 /* unmap adapter SLIM and Control Registers */
8853 iounmap(phba->ctrl_regs_memmap_p);
8854 iounmap(phba->slim_memmap_p);
8855
James Smart3772a992009-05-22 14:50:54 -04008856 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -05008857
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05008858 pci_release_selected_regions(pdev, bars);
James Smart2e0fef82007-06-17 19:56:36 -05008859 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05008860}
8861
Linas Vepstas8d63f372007-02-14 14:28:36 -06008862/**
James Smart3772a992009-05-22 14:50:54 -04008863 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05008864 * @pdev: pointer to PCI device
8865 * @msg: power management message
8866 *
James Smart3772a992009-05-22 14:50:54 -04008867 * This routine is to be called from the kernel's PCI subsystem to support
8868 * system Power Management (PM) to device with SLI-3 interface spec. When
8869 * PM invokes this method, it quiesces the device by stopping the driver's
8870 * worker thread for the device, turning off device's interrupt and DMA,
8871 * and bring the device offline. Note that as the driver implements the
8872 * minimum PM requirements to a power-aware driver's PM support for the
8873 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
8874 * to the suspend() method call will be treated as SUSPEND and the driver will
8875 * fully reinitialize its device during resume() method call, the driver will
8876 * set device to PCI_D3hot state in PCI config space instead of setting it
8877 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -05008878 *
8879 * Return code
James Smart3772a992009-05-22 14:50:54 -04008880 * 0 - driver suspended the device
8881 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05008882 **/
8883static int
James Smart3772a992009-05-22 14:50:54 -04008884lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -05008885{
8886 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8887 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8888
8889 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8890 "0473 PCI device Power Management suspend.\n");
8891
8892 /* Bring down the device */
8893 lpfc_offline_prep(phba);
8894 lpfc_offline(phba);
8895 kthread_stop(phba->worker_thread);
8896
8897 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -04008898 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -05008899
8900 /* Save device state to PCI config space */
8901 pci_save_state(pdev);
8902 pci_set_power_state(pdev, PCI_D3hot);
8903
8904 return 0;
8905}
8906
8907/**
James Smart3772a992009-05-22 14:50:54 -04008908 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05008909 * @pdev: pointer to PCI device
8910 *
James Smart3772a992009-05-22 14:50:54 -04008911 * This routine is to be called from the kernel's PCI subsystem to support
8912 * system Power Management (PM) to device with SLI-3 interface spec. When PM
8913 * invokes this method, it restores the device's PCI config space state and
8914 * fully reinitializes the device and brings it online. Note that as the
8915 * driver implements the minimum PM requirements to a power-aware driver's
8916 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
8917 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
8918 * driver will fully reinitialize its device during resume() method call,
8919 * the device will be set to PCI_D0 directly in PCI config space before
8920 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -05008921 *
8922 * Return code
James Smart3772a992009-05-22 14:50:54 -04008923 * 0 - driver suspended the device
8924 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05008925 **/
8926static int
James Smart3772a992009-05-22 14:50:54 -04008927lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -05008928{
8929 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8930 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -05008931 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05008932 int error;
8933
8934 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8935 "0452 PCI device Power Management resume.\n");
8936
8937 /* Restore device state from PCI config space */
8938 pci_set_power_state(pdev, PCI_D0);
8939 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -04008940
James Smart1dfb5a42010-02-12 14:40:50 -05008941 /*
8942 * As the new kernel behavior of pci_restore_state() API call clears
8943 * device saved_state flag, need to save the restored state again.
8944 */
8945 pci_save_state(pdev);
8946
James Smart3a55b532008-12-04 22:38:54 -05008947 if (pdev->is_busmaster)
8948 pci_set_master(pdev);
8949
8950 /* Startup the kernel thread for this host adapter. */
8951 phba->worker_thread = kthread_run(lpfc_do_work, phba,
8952 "lpfc_worker_%d", phba->brd_no);
8953 if (IS_ERR(phba->worker_thread)) {
8954 error = PTR_ERR(phba->worker_thread);
8955 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8956 "0434 PM resume failed to start worker "
8957 "thread: error=x%x.\n", error);
8958 return error;
8959 }
8960
James Smart5b75da22008-12-04 22:39:35 -05008961 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04008962 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05008963 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -05008964 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -05008965 "0430 PM resume Failed to enable interrupt\n");
8966 return -EIO;
8967 } else
8968 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05008969
8970 /* Restart HBA and bring it online */
8971 lpfc_sli_brdrestart(phba);
8972 lpfc_online(phba);
8973
James Smart5b75da22008-12-04 22:39:35 -05008974 /* Log the current active interrupt mode */
8975 lpfc_log_intr_mode(phba, phba->intr_mode);
8976
James Smart3a55b532008-12-04 22:38:54 -05008977 return 0;
8978}
8979
8980/**
James Smart891478a2009-11-18 15:40:23 -05008981 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
8982 * @phba: pointer to lpfc hba data structure.
8983 *
8984 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -04008985 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -05008986 **/
8987static void
8988lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
8989{
James Smarte2af0d22010-03-15 11:25:32 -04008990 struct lpfc_sli *psli = &phba->sli;
8991 struct lpfc_sli_ring *pring;
8992
James Smart891478a2009-11-18 15:40:23 -05008993 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8994 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -04008995
8996 /*
8997 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
8998 * and let the SCSI mid-layer to retry them to recover.
8999 */
9000 pring = &psli->ring[psli->fcp_ring];
9001 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart891478a2009-11-18 15:40:23 -05009002}
9003
9004/**
James Smart0d878412009-10-02 15:16:56 -04009005 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
9006 * @phba: pointer to lpfc hba data structure.
9007 *
9008 * This routine is called to prepare the SLI3 device for PCI slot reset. It
9009 * disables the device interrupt and pci device, and aborts the internal FCP
9010 * pending I/Os.
9011 **/
9012static void
9013lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
9014{
James Smart0d878412009-10-02 15:16:56 -04009015 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05009016 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -04009017
James Smart75baf692010-06-08 18:31:21 -04009018 /* Block any management I/Os to the device */
9019 lpfc_block_mgmt_io(phba);
9020
James Smarte2af0d22010-03-15 11:25:32 -04009021 /* Block all SCSI devices' I/Os on the host */
9022 lpfc_scsi_dev_block(phba);
9023
9024 /* stop all timers */
9025 lpfc_stop_hba_timers(phba);
9026
James Smart0d878412009-10-02 15:16:56 -04009027 /* Disable interrupt and pci device */
9028 lpfc_sli_disable_intr(phba);
9029 pci_disable_device(phba->pcidev);
James Smart75baf692010-06-08 18:31:21 -04009030
James Smarte2af0d22010-03-15 11:25:32 -04009031 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
9032 lpfc_sli_flush_fcp_rings(phba);
James Smart0d878412009-10-02 15:16:56 -04009033}
9034
9035/**
9036 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
9037 * @phba: pointer to lpfc hba data structure.
9038 *
9039 * This routine is called to prepare the SLI3 device for PCI slot permanently
9040 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
9041 * pending I/Os.
9042 **/
9043static void
James Smart75baf692010-06-08 18:31:21 -04009044lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
James Smart0d878412009-10-02 15:16:56 -04009045{
9046 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05009047 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -04009048 /* Block all SCSI devices' I/Os on the host */
9049 lpfc_scsi_dev_block(phba);
9050
9051 /* stop all timers */
9052 lpfc_stop_hba_timers(phba);
9053
James Smart0d878412009-10-02 15:16:56 -04009054 /* Clean up all driver's outstanding SCSI I/Os */
9055 lpfc_sli_flush_fcp_rings(phba);
9056}
9057
9058/**
James Smart3772a992009-05-22 14:50:54 -04009059 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -04009060 * @pdev: pointer to PCI device.
9061 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009062 *
James Smart3772a992009-05-22 14:50:54 -04009063 * This routine is called from the PCI subsystem for I/O error handling to
9064 * device with SLI-3 interface spec. This function is called by the PCI
9065 * subsystem after a PCI bus error affecting this device has been detected.
9066 * When this function is invoked, it will need to stop all the I/Os and
9067 * interrupt(s) to the device. Once that is done, it will return
9068 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
9069 * as desired.
James Smarte59058c2008-08-24 21:49:00 -04009070 *
9071 * Return codes
James Smart0d878412009-10-02 15:16:56 -04009072 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -04009073 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
9074 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -04009075 **/
James Smart3772a992009-05-22 14:50:54 -04009076static pci_ers_result_t
9077lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009078{
James Smart51ef4c22007-08-02 11:10:31 -04009079 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9080 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009081
James Smart0d878412009-10-02 15:16:56 -04009082 switch (state) {
9083 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -05009084 /* Non-fatal error, prepare for recovery */
9085 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -04009086 return PCI_ERS_RESULT_CAN_RECOVER;
9087 case pci_channel_io_frozen:
9088 /* Fatal error, prepare for slot reset */
9089 lpfc_sli_prep_dev_for_reset(phba);
9090 return PCI_ERS_RESULT_NEED_RESET;
9091 case pci_channel_io_perm_failure:
9092 /* Permanent failure, prepare for device down */
James Smart75baf692010-06-08 18:31:21 -04009093 lpfc_sli_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009094 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -04009095 default:
9096 /* Unknown state, prepare and request slot reset */
9097 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9098 "0472 Unknown PCI error state: x%x\n", state);
9099 lpfc_sli_prep_dev_for_reset(phba);
9100 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -04009101 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06009102}
9103
9104/**
James Smart3772a992009-05-22 14:50:54 -04009105 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -04009106 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009107 *
James Smart3772a992009-05-22 14:50:54 -04009108 * This routine is called from the PCI subsystem for error handling to
9109 * device with SLI-3 interface spec. This is called after PCI bus has been
9110 * reset to restart the PCI card from scratch, as if from a cold-boot.
9111 * During the PCI subsystem error recovery, after driver returns
9112 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
9113 * recovery and then call this routine before calling the .resume method
9114 * to recover the device. This function will initialize the HBA device,
9115 * enable the interrupt, but it will just put the HBA to offline state
9116 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -04009117 *
9118 * Return codes
James Smart3772a992009-05-22 14:50:54 -04009119 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
9120 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -06009121 */
James Smart3772a992009-05-22 14:50:54 -04009122static pci_ers_result_t
9123lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009124{
James Smart51ef4c22007-08-02 11:10:31 -04009125 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9126 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009127 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -05009128 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009129
9130 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +11009131 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -06009132 printk(KERN_ERR "lpfc: Cannot re-enable "
9133 "PCI device after reset.\n");
9134 return PCI_ERS_RESULT_DISCONNECT;
9135 }
9136
James Smart97207482008-12-04 22:39:19 -05009137 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05009138
9139 /*
9140 * As the new kernel behavior of pci_restore_state() API call clears
9141 * device saved_state flag, need to save the restored state again.
9142 */
9143 pci_save_state(pdev);
9144
James Smart97207482008-12-04 22:39:19 -05009145 if (pdev->is_busmaster)
9146 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009147
James Smart92d7f7b2007-06-17 19:56:38 -05009148 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04009149 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05009150 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009151
James Smart5b75da22008-12-04 22:39:35 -05009152 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009153 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05009154 if (intr_mode == LPFC_INTR_ERROR) {
9155 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9156 "0427 Cannot re-enable interrupt after "
9157 "slot reset.\n");
9158 return PCI_ERS_RESULT_DISCONNECT;
9159 } else
9160 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009161
James Smart75baf692010-06-08 18:31:21 -04009162 /* Take device offline, it will perform cleanup */
9163 lpfc_offline_prep(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009164 lpfc_offline(phba);
9165 lpfc_sli_brdrestart(phba);
9166
James Smart5b75da22008-12-04 22:39:35 -05009167 /* Log the current active interrupt mode */
9168 lpfc_log_intr_mode(phba, phba->intr_mode);
9169
Linas Vepstas8d63f372007-02-14 14:28:36 -06009170 return PCI_ERS_RESULT_RECOVERED;
9171}
9172
9173/**
James Smart3772a992009-05-22 14:50:54 -04009174 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -04009175 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -06009176 *
James Smart3772a992009-05-22 14:50:54 -04009177 * This routine is called from the PCI subsystem for error handling to device
9178 * with SLI-3 interface spec. It is called when kernel error recovery tells
9179 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
9180 * error recovery. After this call, traffic can start to flow from this device
9181 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009182 */
James Smart3772a992009-05-22 14:50:54 -04009183static void
9184lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009185{
James Smart51ef4c22007-08-02 11:10:31 -04009186 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9187 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009188
James Smarte2af0d22010-03-15 11:25:32 -04009189 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -04009190 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -04009191
9192 /* Clean up Advanced Error Reporting (AER) if needed */
9193 if (phba->hba_flag & HBA_AER_ENABLED)
9194 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009195}
9196
James Smart3772a992009-05-22 14:50:54 -04009197/**
James Smartda0436e2009-05-22 14:51:39 -04009198 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
9199 * @phba: pointer to lpfc hba data structure.
9200 *
9201 * returns the number of ELS/CT IOCBs to reserve
9202 **/
9203int
9204lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
9205{
9206 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
9207
James Smartf1126682009-06-10 17:22:44 -04009208 if (phba->sli_rev == LPFC_SLI_REV4) {
9209 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -04009210 return 10;
James Smartf1126682009-06-10 17:22:44 -04009211 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -04009212 return 25;
James Smartf1126682009-06-10 17:22:44 -04009213 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -04009214 return 50;
James Smartf1126682009-06-10 17:22:44 -04009215 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -04009216 return 100;
James Smart8a9d2e82012-05-09 21:16:12 -04009217 else if (max_xri <= 1536)
James Smart6a9c52c2009-10-02 15:16:51 -04009218 return 150;
James Smart8a9d2e82012-05-09 21:16:12 -04009219 else if (max_xri <= 2048)
9220 return 200;
9221 else
9222 return 250;
James Smartf1126682009-06-10 17:22:44 -04009223 } else
9224 return 0;
James Smartda0436e2009-05-22 14:51:39 -04009225}
9226
9227/**
James Smart52d52442011-05-24 11:42:45 -04009228 * lpfc_write_firmware - attempt to write a firmware image to the port
9229 * @phba: pointer to lpfc hba data structure.
9230 * @fw: pointer to firmware image returned from request_firmware.
9231 *
9232 * returns the number of bytes written if write is successful.
9233 * returns a negative error value if there were errors.
9234 * returns 0 if firmware matches currently active firmware on port.
9235 **/
9236int
9237lpfc_write_firmware(struct lpfc_hba *phba, const struct firmware *fw)
9238{
James Smart6b5151f2012-01-18 16:24:06 -05009239 char fwrev[FW_REV_STR_SIZE];
James Smart52d52442011-05-24 11:42:45 -04009240 struct lpfc_grp_hdr *image = (struct lpfc_grp_hdr *)fw->data;
9241 struct list_head dma_buffer_list;
9242 int i, rc = 0;
9243 struct lpfc_dmabuf *dmabuf, *next;
9244 uint32_t offset = 0, temp_offset = 0;
9245
9246 INIT_LIST_HEAD(&dma_buffer_list);
James Smart079b5c92011-08-21 21:48:49 -04009247 if ((be32_to_cpu(image->magic_number) != LPFC_GROUP_OJECT_MAGIC_NUM) ||
9248 (bf_get_be32(lpfc_grp_hdr_file_type, image) !=
9249 LPFC_FILE_TYPE_GROUP) ||
9250 (bf_get_be32(lpfc_grp_hdr_id, image) != LPFC_FILE_ID_GROUP) ||
9251 (be32_to_cpu(image->size) != fw->size)) {
James Smart52d52442011-05-24 11:42:45 -04009252 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9253 "3022 Invalid FW image found. "
James Smart079b5c92011-08-21 21:48:49 -04009254 "Magic:%x Type:%x ID:%x\n",
9255 be32_to_cpu(image->magic_number),
9256 bf_get_be32(lpfc_grp_hdr_file_type, image),
9257 bf_get_be32(lpfc_grp_hdr_id, image));
James Smart52d52442011-05-24 11:42:45 -04009258 return -EINVAL;
9259 }
9260 lpfc_decode_firmware_rev(phba, fwrev, 1);
James Smart88a2cfb2011-07-22 18:36:33 -04009261 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
James Smart52d52442011-05-24 11:42:45 -04009262 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9263 "3023 Updating Firmware. Current Version:%s "
9264 "New Version:%s\n",
James Smart88a2cfb2011-07-22 18:36:33 -04009265 fwrev, image->revision);
James Smart52d52442011-05-24 11:42:45 -04009266 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
9267 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
9268 GFP_KERNEL);
9269 if (!dmabuf) {
9270 rc = -ENOMEM;
9271 goto out;
9272 }
9273 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
9274 SLI4_PAGE_SIZE,
9275 &dmabuf->phys,
9276 GFP_KERNEL);
9277 if (!dmabuf->virt) {
9278 kfree(dmabuf);
9279 rc = -ENOMEM;
9280 goto out;
9281 }
9282 list_add_tail(&dmabuf->list, &dma_buffer_list);
9283 }
9284 while (offset < fw->size) {
9285 temp_offset = offset;
9286 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
James Smart079b5c92011-08-21 21:48:49 -04009287 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
James Smart52d52442011-05-24 11:42:45 -04009288 memcpy(dmabuf->virt,
9289 fw->data + temp_offset,
James Smart079b5c92011-08-21 21:48:49 -04009290 fw->size - temp_offset);
9291 temp_offset = fw->size;
James Smart52d52442011-05-24 11:42:45 -04009292 break;
9293 }
James Smart52d52442011-05-24 11:42:45 -04009294 memcpy(dmabuf->virt, fw->data + temp_offset,
9295 SLI4_PAGE_SIZE);
James Smart88a2cfb2011-07-22 18:36:33 -04009296 temp_offset += SLI4_PAGE_SIZE;
James Smart52d52442011-05-24 11:42:45 -04009297 }
9298 rc = lpfc_wr_object(phba, &dma_buffer_list,
9299 (fw->size - offset), &offset);
9300 if (rc) {
9301 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9302 "3024 Firmware update failed. "
9303 "%d\n", rc);
9304 goto out;
9305 }
9306 }
9307 rc = offset;
9308 }
9309out:
9310 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
9311 list_del(&dmabuf->list);
9312 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
9313 dmabuf->virt, dmabuf->phys);
9314 kfree(dmabuf);
9315 }
9316 return rc;
9317}
9318
9319/**
James Smartda0436e2009-05-22 14:51:39 -04009320 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
9321 * @pdev: pointer to PCI device
9322 * @pid: pointer to PCI device identifier
9323 *
9324 * This routine is called from the kernel's PCI subsystem to device with
9325 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
9326 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
9327 * information of the device and driver to see if the driver state that it
9328 * can support this kind of device. If the match is successful, the driver
9329 * core invokes this routine. If this routine determines it can claim the HBA,
9330 * it does all the initialization that it needs to do to handle the HBA
9331 * properly.
9332 *
9333 * Return code
9334 * 0 - driver can claim the device
9335 * negative value - driver can not claim the device
9336 **/
9337static int __devinit
9338lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
9339{
9340 struct lpfc_hba *phba;
9341 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04009342 struct Scsi_Host *shost = NULL;
James Smartda0436e2009-05-22 14:51:39 -04009343 int error;
9344 uint32_t cfg_mode, intr_mode;
9345 int mcnt;
James Smart05580562011-05-24 11:40:48 -04009346 int adjusted_fcp_eq_count;
James Smart52d52442011-05-24 11:42:45 -04009347 const struct firmware *fw;
9348 uint8_t file_name[16];
James Smartda0436e2009-05-22 14:51:39 -04009349
9350 /* Allocate memory for HBA structure */
9351 phba = lpfc_hba_alloc(pdev);
9352 if (!phba)
9353 return -ENOMEM;
9354
9355 /* Perform generic PCI device enabling operation */
9356 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -04009357 if (error)
James Smartda0436e2009-05-22 14:51:39 -04009358 goto out_free_phba;
James Smartda0436e2009-05-22 14:51:39 -04009359
9360 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
9361 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
9362 if (error)
9363 goto out_disable_pci_dev;
9364
9365 /* Set up SLI-4 specific device PCI memory space */
9366 error = lpfc_sli4_pci_mem_setup(phba);
9367 if (error) {
9368 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9369 "1410 Failed to set up pci memory space.\n");
9370 goto out_disable_pci_dev;
9371 }
9372
9373 /* Set up phase-1 common device driver resources */
9374 error = lpfc_setup_driver_resource_phase1(phba);
9375 if (error) {
9376 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9377 "1411 Failed to set up driver resource.\n");
9378 goto out_unset_pci_mem_s4;
9379 }
9380
9381 /* Set up SLI-4 Specific device driver resources */
9382 error = lpfc_sli4_driver_resource_setup(phba);
9383 if (error) {
9384 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9385 "1412 Failed to set up driver resource.\n");
9386 goto out_unset_pci_mem_s4;
9387 }
9388
9389 /* Initialize and populate the iocb list per host */
James Smart2a9bf3d2010-06-07 15:24:45 -04009390
9391 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9392 "2821 initialize iocb list %d.\n",
9393 phba->cfg_iocb_cnt*1024);
9394 error = lpfc_init_iocb_list(phba, phba->cfg_iocb_cnt*1024);
9395
James Smartda0436e2009-05-22 14:51:39 -04009396 if (error) {
9397 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9398 "1413 Failed to initialize iocb list.\n");
9399 goto out_unset_driver_resource_s4;
9400 }
9401
James Smart19ca7602010-11-20 23:11:55 -05009402 INIT_LIST_HEAD(&phba->active_rrq_list);
James Smart7d791df2011-07-22 18:37:52 -04009403 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
James Smart19ca7602010-11-20 23:11:55 -05009404
James Smartda0436e2009-05-22 14:51:39 -04009405 /* Set up common device driver resources */
9406 error = lpfc_setup_driver_resource_phase2(phba);
9407 if (error) {
9408 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9409 "1414 Failed to set up driver resource.\n");
9410 goto out_free_iocb_list;
9411 }
9412
James Smart079b5c92011-08-21 21:48:49 -04009413 /* Get the default values for Model Name and Description */
9414 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
9415
James Smartda0436e2009-05-22 14:51:39 -04009416 /* Create SCSI host to the physical port */
9417 error = lpfc_create_shost(phba);
9418 if (error) {
9419 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9420 "1415 Failed to create scsi host.\n");
9421 goto out_unset_driver_resource;
9422 }
9423
9424 /* Configure sysfs attributes */
9425 vport = phba->pport;
9426 error = lpfc_alloc_sysfs_attr(vport);
9427 if (error) {
9428 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9429 "1416 Failed to allocate sysfs attr\n");
9430 goto out_destroy_shost;
9431 }
9432
James Smart6669f9b2009-10-02 15:16:45 -04009433 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -04009434 /* Now, trying to enable interrupt and bring up the device */
9435 cfg_mode = phba->cfg_use_msi;
9436 while (true) {
9437 /* Put device to a known state before enabling interrupt */
9438 lpfc_stop_port(phba);
9439 /* Configure and enable interrupt */
9440 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
9441 if (intr_mode == LPFC_INTR_ERROR) {
9442 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9443 "0426 Failed to enable interrupt.\n");
9444 error = -ENODEV;
9445 goto out_free_sysfs_attr;
9446 }
James Smart05580562011-05-24 11:40:48 -04009447 /* Default to single EQ for non-MSI-X */
James Smartdef9c7a2009-12-21 17:02:28 -05009448 if (phba->intr_type != MSIX)
James Smart05580562011-05-24 11:40:48 -04009449 adjusted_fcp_eq_count = 0;
9450 else if (phba->sli4_hba.msix_vec_nr <
9451 phba->cfg_fcp_eq_count + 1)
9452 adjusted_fcp_eq_count = phba->sli4_hba.msix_vec_nr - 1;
9453 else
9454 adjusted_fcp_eq_count = phba->cfg_fcp_eq_count;
James Smart05580562011-05-24 11:40:48 -04009455 phba->cfg_fcp_eq_count = adjusted_fcp_eq_count;
James Smartda0436e2009-05-22 14:51:39 -04009456 /* Set up SLI-4 HBA */
9457 if (lpfc_sli4_hba_setup(phba)) {
9458 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9459 "1421 Failed to set up hba\n");
9460 error = -ENODEV;
9461 goto out_disable_intr;
9462 }
9463
9464 /* Send NOP mbx cmds for non-INTx mode active interrupt test */
9465 if (intr_mode != 0)
9466 mcnt = lpfc_sli4_send_nop_mbox_cmds(phba,
9467 LPFC_ACT_INTR_CNT);
9468
9469 /* Check active interrupts received only for MSI/MSI-X */
9470 if (intr_mode == 0 ||
9471 phba->sli.slistat.sli_intr >= LPFC_ACT_INTR_CNT) {
9472 /* Log the current active interrupt mode */
9473 phba->intr_mode = intr_mode;
9474 lpfc_log_intr_mode(phba, intr_mode);
9475 break;
9476 }
9477 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9478 "0451 Configure interrupt mode (%d) "
9479 "failed active interrupt test.\n",
9480 intr_mode);
James Smart2fcee4b2010-12-15 17:57:46 -05009481 /* Unset the previous SLI-4 HBA setup. */
9482 /*
9483 * TODO: Is this operation compatible with IF TYPE 2
9484 * devices? All port state is deleted and cleared.
9485 */
James Smartda0436e2009-05-22 14:51:39 -04009486 lpfc_sli4_unset_hba(phba);
9487 /* Try next level of interrupt mode */
9488 cfg_mode = --intr_mode;
9489 }
9490
9491 /* Perform post initialization setup */
9492 lpfc_post_init_setup(phba);
9493
James Smart026abb82011-12-13 13:20:45 -05009494 /* check for firmware upgrade or downgrade (if_type 2 only) */
9495 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
9496 LPFC_SLI_INTF_IF_TYPE_2) {
9497 snprintf(file_name, 16, "%s.grp", phba->ModelName);
9498 error = request_firmware(&fw, file_name, &phba->pcidev->dev);
9499 if (!error) {
9500 lpfc_write_firmware(phba, fw);
9501 release_firmware(fw);
9502 }
James Smart52d52442011-05-24 11:42:45 -04009503 }
9504
James Smart1c6834a2009-07-19 10:01:26 -04009505 /* Check if there are static vports to be created. */
9506 lpfc_create_static_vport(phba);
James Smartda0436e2009-05-22 14:51:39 -04009507 return 0;
9508
9509out_disable_intr:
9510 lpfc_sli4_disable_intr(phba);
9511out_free_sysfs_attr:
9512 lpfc_free_sysfs_attr(vport);
9513out_destroy_shost:
9514 lpfc_destroy_shost(phba);
9515out_unset_driver_resource:
9516 lpfc_unset_driver_resource_phase2(phba);
9517out_free_iocb_list:
9518 lpfc_free_iocb_list(phba);
9519out_unset_driver_resource_s4:
9520 lpfc_sli4_driver_resource_unset(phba);
9521out_unset_pci_mem_s4:
9522 lpfc_sli4_pci_mem_unset(phba);
9523out_disable_pci_dev:
9524 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04009525 if (shost)
9526 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -04009527out_free_phba:
9528 lpfc_hba_free(phba);
9529 return error;
9530}
9531
9532/**
9533 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
9534 * @pdev: pointer to PCI device
9535 *
9536 * This routine is called from the kernel's PCI subsystem to device with
9537 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
9538 * removed from PCI bus, it performs all the necessary cleanup for the HBA
9539 * device to be removed from the PCI subsystem properly.
9540 **/
9541static void __devexit
9542lpfc_pci_remove_one_s4(struct pci_dev *pdev)
9543{
9544 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9545 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
9546 struct lpfc_vport **vports;
9547 struct lpfc_hba *phba = vport->phba;
9548 int i;
9549
9550 /* Mark the device unloading flag */
9551 spin_lock_irq(&phba->hbalock);
9552 vport->load_flag |= FC_UNLOADING;
9553 spin_unlock_irq(&phba->hbalock);
9554
9555 /* Free the HBA sysfs attributes */
9556 lpfc_free_sysfs_attr(vport);
9557
9558 /* Release all the vports against this physical port */
9559 vports = lpfc_create_vport_work_array(phba);
9560 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -04009561 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
9562 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
9563 continue;
James Smartda0436e2009-05-22 14:51:39 -04009564 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -04009565 }
James Smartda0436e2009-05-22 14:51:39 -04009566 lpfc_destroy_vport_work_array(phba, vports);
9567
9568 /* Remove FC host and then SCSI host with the physical port */
9569 fc_remove_host(shost);
9570 scsi_remove_host(shost);
9571
9572 /* Perform cleanup on the physical port */
9573 lpfc_cleanup(vport);
9574
9575 /*
9576 * Bring down the SLI Layer. This step disables all interrupts,
9577 * clears the rings, discards all mailbox commands, and resets
9578 * the HBA FCoE function.
9579 */
9580 lpfc_debugfs_terminate(vport);
9581 lpfc_sli4_hba_unset(phba);
9582
9583 spin_lock_irq(&phba->hbalock);
9584 list_del_init(&vport->listentry);
9585 spin_unlock_irq(&phba->hbalock);
9586
James Smart3677a3a2010-09-29 11:19:14 -04009587 /* Perform scsi free before driver resource_unset since scsi
James Smartda0436e2009-05-22 14:51:39 -04009588 * buffers are released to their corresponding pools here.
9589 */
9590 lpfc_scsi_free(phba);
9591 lpfc_sli4_driver_resource_unset(phba);
9592
9593 /* Unmap adapter Control and Doorbell registers */
9594 lpfc_sli4_pci_mem_unset(phba);
9595
9596 /* Release PCI resources and disable device's PCI function */
9597 scsi_host_put(shost);
9598 lpfc_disable_pci_dev(phba);
9599
9600 /* Finally, free the driver's device data structure */
9601 lpfc_hba_free(phba);
9602
9603 return;
9604}
9605
9606/**
9607 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
9608 * @pdev: pointer to PCI device
9609 * @msg: power management message
9610 *
9611 * This routine is called from the kernel's PCI subsystem to support system
9612 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
9613 * this method, it quiesces the device by stopping the driver's worker
9614 * thread for the device, turning off device's interrupt and DMA, and bring
9615 * the device offline. Note that as the driver implements the minimum PM
9616 * requirements to a power-aware driver's PM support for suspend/resume -- all
9617 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
9618 * method call will be treated as SUSPEND and the driver will fully
9619 * reinitialize its device during resume() method call, the driver will set
9620 * device to PCI_D3hot state in PCI config space instead of setting it
9621 * according to the @msg provided by the PM.
9622 *
9623 * Return code
9624 * 0 - driver suspended the device
9625 * Error otherwise
9626 **/
9627static int
9628lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
9629{
9630 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9631 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9632
9633 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart75baf692010-06-08 18:31:21 -04009634 "2843 PCI device Power Management suspend.\n");
James Smartda0436e2009-05-22 14:51:39 -04009635
9636 /* Bring down the device */
9637 lpfc_offline_prep(phba);
9638 lpfc_offline(phba);
9639 kthread_stop(phba->worker_thread);
9640
9641 /* Disable interrupt from device */
9642 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -04009643 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04009644
9645 /* Save device state to PCI config space */
9646 pci_save_state(pdev);
9647 pci_set_power_state(pdev, PCI_D3hot);
9648
9649 return 0;
9650}
9651
9652/**
9653 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
9654 * @pdev: pointer to PCI device
9655 *
9656 * This routine is called from the kernel's PCI subsystem to support system
9657 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
9658 * this method, it restores the device's PCI config space state and fully
9659 * reinitializes the device and brings it online. Note that as the driver
9660 * implements the minimum PM requirements to a power-aware driver's PM for
9661 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
9662 * to the suspend() method call will be treated as SUSPEND and the driver
9663 * will fully reinitialize its device during resume() method call, the device
9664 * will be set to PCI_D0 directly in PCI config space before restoring the
9665 * state.
9666 *
9667 * Return code
9668 * 0 - driver suspended the device
9669 * Error otherwise
9670 **/
9671static int
9672lpfc_pci_resume_one_s4(struct pci_dev *pdev)
9673{
9674 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9675 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9676 uint32_t intr_mode;
9677 int error;
9678
9679 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9680 "0292 PCI device Power Management resume.\n");
9681
9682 /* Restore device state from PCI config space */
9683 pci_set_power_state(pdev, PCI_D0);
9684 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05009685
9686 /*
9687 * As the new kernel behavior of pci_restore_state() API call clears
9688 * device saved_state flag, need to save the restored state again.
9689 */
9690 pci_save_state(pdev);
9691
James Smartda0436e2009-05-22 14:51:39 -04009692 if (pdev->is_busmaster)
9693 pci_set_master(pdev);
9694
9695 /* Startup the kernel thread for this host adapter. */
9696 phba->worker_thread = kthread_run(lpfc_do_work, phba,
9697 "lpfc_worker_%d", phba->brd_no);
9698 if (IS_ERR(phba->worker_thread)) {
9699 error = PTR_ERR(phba->worker_thread);
9700 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9701 "0293 PM resume failed to start worker "
9702 "thread: error=x%x.\n", error);
9703 return error;
9704 }
9705
9706 /* Configure and enable interrupt */
9707 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
9708 if (intr_mode == LPFC_INTR_ERROR) {
9709 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9710 "0294 PM resume Failed to enable interrupt\n");
9711 return -EIO;
9712 } else
9713 phba->intr_mode = intr_mode;
9714
9715 /* Restart HBA and bring it online */
9716 lpfc_sli_brdrestart(phba);
9717 lpfc_online(phba);
9718
9719 /* Log the current active interrupt mode */
9720 lpfc_log_intr_mode(phba, phba->intr_mode);
9721
9722 return 0;
9723}
9724
9725/**
James Smart75baf692010-06-08 18:31:21 -04009726 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
9727 * @phba: pointer to lpfc hba data structure.
9728 *
9729 * This routine is called to prepare the SLI4 device for PCI slot recover. It
9730 * aborts all the outstanding SCSI I/Os to the pci device.
9731 **/
9732static void
9733lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
9734{
9735 struct lpfc_sli *psli = &phba->sli;
9736 struct lpfc_sli_ring *pring;
9737
9738 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9739 "2828 PCI channel I/O abort preparing for recovery\n");
9740 /*
9741 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
9742 * and let the SCSI mid-layer to retry them to recover.
9743 */
9744 pring = &psli->ring[psli->fcp_ring];
9745 lpfc_sli_abort_iocb_ring(phba, pring);
9746}
9747
9748/**
9749 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
9750 * @phba: pointer to lpfc hba data structure.
9751 *
9752 * This routine is called to prepare the SLI4 device for PCI slot reset. It
9753 * disables the device interrupt and pci device, and aborts the internal FCP
9754 * pending I/Os.
9755 **/
9756static void
9757lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
9758{
9759 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9760 "2826 PCI channel disable preparing for reset\n");
9761
9762 /* Block any management I/Os to the device */
9763 lpfc_block_mgmt_io(phba);
9764
9765 /* Block all SCSI devices' I/Os on the host */
9766 lpfc_scsi_dev_block(phba);
9767
9768 /* stop all timers */
9769 lpfc_stop_hba_timers(phba);
9770
9771 /* Disable interrupt and pci device */
9772 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -04009773 lpfc_sli4_queue_destroy(phba);
James Smart75baf692010-06-08 18:31:21 -04009774 pci_disable_device(phba->pcidev);
9775
9776 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
9777 lpfc_sli_flush_fcp_rings(phba);
9778}
9779
9780/**
9781 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
9782 * @phba: pointer to lpfc hba data structure.
9783 *
9784 * This routine is called to prepare the SLI4 device for PCI slot permanently
9785 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
9786 * pending I/Os.
9787 **/
9788static void
9789lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
9790{
9791 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9792 "2827 PCI channel permanent disable for failure\n");
9793
9794 /* Block all SCSI devices' I/Os on the host */
9795 lpfc_scsi_dev_block(phba);
9796
9797 /* stop all timers */
9798 lpfc_stop_hba_timers(phba);
9799
9800 /* Clean up all driver's outstanding SCSI I/Os */
9801 lpfc_sli_flush_fcp_rings(phba);
9802}
9803
9804/**
James Smartda0436e2009-05-22 14:51:39 -04009805 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
9806 * @pdev: pointer to PCI device.
9807 * @state: the current PCI connection state.
9808 *
9809 * This routine is called from the PCI subsystem for error handling to device
9810 * with SLI-4 interface spec. This function is called by the PCI subsystem
9811 * after a PCI bus error affecting this device has been detected. When this
9812 * function is invoked, it will need to stop all the I/Os and interrupt(s)
9813 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
9814 * for the PCI subsystem to perform proper recovery as desired.
9815 *
9816 * Return codes
9817 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
9818 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
9819 **/
9820static pci_ers_result_t
9821lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
9822{
James Smart75baf692010-06-08 18:31:21 -04009823 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9824 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9825
9826 switch (state) {
9827 case pci_channel_io_normal:
9828 /* Non-fatal error, prepare for recovery */
9829 lpfc_sli4_prep_dev_for_recover(phba);
9830 return PCI_ERS_RESULT_CAN_RECOVER;
9831 case pci_channel_io_frozen:
9832 /* Fatal error, prepare for slot reset */
9833 lpfc_sli4_prep_dev_for_reset(phba);
9834 return PCI_ERS_RESULT_NEED_RESET;
9835 case pci_channel_io_perm_failure:
9836 /* Permanent failure, prepare for device down */
9837 lpfc_sli4_prep_dev_for_perm_failure(phba);
9838 return PCI_ERS_RESULT_DISCONNECT;
9839 default:
9840 /* Unknown state, prepare and request slot reset */
9841 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9842 "2825 Unknown PCI error state: x%x\n", state);
9843 lpfc_sli4_prep_dev_for_reset(phba);
9844 return PCI_ERS_RESULT_NEED_RESET;
9845 }
James Smartda0436e2009-05-22 14:51:39 -04009846}
9847
9848/**
9849 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
9850 * @pdev: pointer to PCI device.
9851 *
9852 * This routine is called from the PCI subsystem for error handling to device
9853 * with SLI-4 interface spec. It is called after PCI bus has been reset to
9854 * restart the PCI card from scratch, as if from a cold-boot. During the
9855 * PCI subsystem error recovery, after the driver returns
9856 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
9857 * recovery and then call this routine before calling the .resume method to
9858 * recover the device. This function will initialize the HBA device, enable
9859 * the interrupt, but it will just put the HBA to offline state without
9860 * passing any I/O traffic.
9861 *
9862 * Return codes
9863 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
9864 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
9865 */
9866static pci_ers_result_t
9867lpfc_io_slot_reset_s4(struct pci_dev *pdev)
9868{
James Smart75baf692010-06-08 18:31:21 -04009869 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9870 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9871 struct lpfc_sli *psli = &phba->sli;
9872 uint32_t intr_mode;
9873
9874 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
9875 if (pci_enable_device_mem(pdev)) {
9876 printk(KERN_ERR "lpfc: Cannot re-enable "
9877 "PCI device after reset.\n");
9878 return PCI_ERS_RESULT_DISCONNECT;
9879 }
9880
9881 pci_restore_state(pdev);
James Smart0a96e972011-07-22 18:37:28 -04009882
9883 /*
9884 * As the new kernel behavior of pci_restore_state() API call clears
9885 * device saved_state flag, need to save the restored state again.
9886 */
9887 pci_save_state(pdev);
9888
James Smart75baf692010-06-08 18:31:21 -04009889 if (pdev->is_busmaster)
9890 pci_set_master(pdev);
9891
9892 spin_lock_irq(&phba->hbalock);
9893 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
9894 spin_unlock_irq(&phba->hbalock);
9895
9896 /* Configure and enable interrupt */
9897 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
9898 if (intr_mode == LPFC_INTR_ERROR) {
9899 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9900 "2824 Cannot re-enable interrupt after "
9901 "slot reset.\n");
9902 return PCI_ERS_RESULT_DISCONNECT;
9903 } else
9904 phba->intr_mode = intr_mode;
9905
9906 /* Log the current active interrupt mode */
9907 lpfc_log_intr_mode(phba, phba->intr_mode);
9908
James Smartda0436e2009-05-22 14:51:39 -04009909 return PCI_ERS_RESULT_RECOVERED;
9910}
9911
9912/**
9913 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
9914 * @pdev: pointer to PCI device
9915 *
9916 * This routine is called from the PCI subsystem for error handling to device
9917 * with SLI-4 interface spec. It is called when kernel error recovery tells
9918 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
9919 * error recovery. After this call, traffic can start to flow from this device
9920 * again.
9921 **/
9922static void
9923lpfc_io_resume_s4(struct pci_dev *pdev)
9924{
James Smart75baf692010-06-08 18:31:21 -04009925 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9926 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9927
9928 /*
9929 * In case of slot reset, as function reset is performed through
9930 * mailbox command which needs DMA to be enabled, this operation
9931 * has to be moved to the io resume phase. Taking device offline
9932 * will perform the necessary cleanup.
9933 */
9934 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
9935 /* Perform device reset */
9936 lpfc_offline_prep(phba);
9937 lpfc_offline(phba);
9938 lpfc_sli_brdrestart(phba);
9939 /* Bring the device back online */
9940 lpfc_online(phba);
9941 }
9942
9943 /* Clean up Advanced Error Reporting (AER) if needed */
9944 if (phba->hba_flag & HBA_AER_ENABLED)
9945 pci_cleanup_aer_uncorrect_error_status(pdev);
James Smartda0436e2009-05-22 14:51:39 -04009946}
9947
9948/**
James Smart3772a992009-05-22 14:50:54 -04009949 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
9950 * @pdev: pointer to PCI device
9951 * @pid: pointer to PCI device identifier
9952 *
9953 * This routine is to be registered to the kernel's PCI subsystem. When an
9954 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
9955 * at PCI device-specific information of the device and driver to see if the
9956 * driver state that it can support this kind of device. If the match is
9957 * successful, the driver core invokes this routine. This routine dispatches
9958 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
9959 * do all the initialization that it needs to do to handle the HBA device
9960 * properly.
9961 *
9962 * Return code
9963 * 0 - driver can claim the device
9964 * negative value - driver can not claim the device
9965 **/
9966static int __devinit
9967lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
9968{
9969 int rc;
James Smart8fa38512009-07-19 10:01:03 -04009970 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -04009971
James Smart28baac72010-02-12 14:42:03 -05009972 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -04009973 return -ENODEV;
9974
James Smart8fa38512009-07-19 10:01:03 -04009975 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -05009976 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -04009977 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04009978 else
James Smart3772a992009-05-22 14:50:54 -04009979 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04009980
James Smart3772a992009-05-22 14:50:54 -04009981 return rc;
9982}
9983
9984/**
9985 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
9986 * @pdev: pointer to PCI device
9987 *
9988 * This routine is to be registered to the kernel's PCI subsystem. When an
9989 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
9990 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
9991 * remove routine, which will perform all the necessary cleanup for the
9992 * device to be removed from the PCI subsystem properly.
9993 **/
9994static void __devexit
9995lpfc_pci_remove_one(struct pci_dev *pdev)
9996{
9997 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9998 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9999
10000 switch (phba->pci_dev_grp) {
10001 case LPFC_PCI_DEV_LP:
10002 lpfc_pci_remove_one_s3(pdev);
10003 break;
James Smartda0436e2009-05-22 14:51:39 -040010004 case LPFC_PCI_DEV_OC:
10005 lpfc_pci_remove_one_s4(pdev);
10006 break;
James Smart3772a992009-05-22 14:50:54 -040010007 default:
10008 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10009 "1424 Invalid PCI device group: 0x%x\n",
10010 phba->pci_dev_grp);
10011 break;
10012 }
10013 return;
10014}
10015
10016/**
10017 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
10018 * @pdev: pointer to PCI device
10019 * @msg: power management message
10020 *
10021 * This routine is to be registered to the kernel's PCI subsystem to support
10022 * system Power Management (PM). When PM invokes this method, it dispatches
10023 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
10024 * suspend the device.
10025 *
10026 * Return code
10027 * 0 - driver suspended the device
10028 * Error otherwise
10029 **/
10030static int
10031lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
10032{
10033 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10034 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10035 int rc = -ENODEV;
10036
10037 switch (phba->pci_dev_grp) {
10038 case LPFC_PCI_DEV_LP:
10039 rc = lpfc_pci_suspend_one_s3(pdev, msg);
10040 break;
James Smartda0436e2009-05-22 14:51:39 -040010041 case LPFC_PCI_DEV_OC:
10042 rc = lpfc_pci_suspend_one_s4(pdev, msg);
10043 break;
James Smart3772a992009-05-22 14:50:54 -040010044 default:
10045 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10046 "1425 Invalid PCI device group: 0x%x\n",
10047 phba->pci_dev_grp);
10048 break;
10049 }
10050 return rc;
10051}
10052
10053/**
10054 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
10055 * @pdev: pointer to PCI device
10056 *
10057 * This routine is to be registered to the kernel's PCI subsystem to support
10058 * system Power Management (PM). When PM invokes this method, it dispatches
10059 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
10060 * resume the device.
10061 *
10062 * Return code
10063 * 0 - driver suspended the device
10064 * Error otherwise
10065 **/
10066static int
10067lpfc_pci_resume_one(struct pci_dev *pdev)
10068{
10069 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10070 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10071 int rc = -ENODEV;
10072
10073 switch (phba->pci_dev_grp) {
10074 case LPFC_PCI_DEV_LP:
10075 rc = lpfc_pci_resume_one_s3(pdev);
10076 break;
James Smartda0436e2009-05-22 14:51:39 -040010077 case LPFC_PCI_DEV_OC:
10078 rc = lpfc_pci_resume_one_s4(pdev);
10079 break;
James Smart3772a992009-05-22 14:50:54 -040010080 default:
10081 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10082 "1426 Invalid PCI device group: 0x%x\n",
10083 phba->pci_dev_grp);
10084 break;
10085 }
10086 return rc;
10087}
10088
10089/**
10090 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
10091 * @pdev: pointer to PCI device.
10092 * @state: the current PCI connection state.
10093 *
10094 * This routine is registered to the PCI subsystem for error handling. This
10095 * function is called by the PCI subsystem after a PCI bus error affecting
10096 * this device has been detected. When this routine is invoked, it dispatches
10097 * the action to the proper SLI-3 or SLI-4 device error detected handling
10098 * routine, which will perform the proper error detected operation.
10099 *
10100 * Return codes
10101 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10102 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10103 **/
10104static pci_ers_result_t
10105lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
10106{
10107 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10108 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10109 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
10110
10111 switch (phba->pci_dev_grp) {
10112 case LPFC_PCI_DEV_LP:
10113 rc = lpfc_io_error_detected_s3(pdev, state);
10114 break;
James Smartda0436e2009-05-22 14:51:39 -040010115 case LPFC_PCI_DEV_OC:
10116 rc = lpfc_io_error_detected_s4(pdev, state);
10117 break;
James Smart3772a992009-05-22 14:50:54 -040010118 default:
10119 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10120 "1427 Invalid PCI device group: 0x%x\n",
10121 phba->pci_dev_grp);
10122 break;
10123 }
10124 return rc;
10125}
10126
10127/**
10128 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
10129 * @pdev: pointer to PCI device.
10130 *
10131 * This routine is registered to the PCI subsystem for error handling. This
10132 * function is called after PCI bus has been reset to restart the PCI card
10133 * from scratch, as if from a cold-boot. When this routine is invoked, it
10134 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
10135 * routine, which will perform the proper device reset.
10136 *
10137 * Return codes
10138 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10139 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10140 **/
10141static pci_ers_result_t
10142lpfc_io_slot_reset(struct pci_dev *pdev)
10143{
10144 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10145 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10146 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
10147
10148 switch (phba->pci_dev_grp) {
10149 case LPFC_PCI_DEV_LP:
10150 rc = lpfc_io_slot_reset_s3(pdev);
10151 break;
James Smartda0436e2009-05-22 14:51:39 -040010152 case LPFC_PCI_DEV_OC:
10153 rc = lpfc_io_slot_reset_s4(pdev);
10154 break;
James Smart3772a992009-05-22 14:50:54 -040010155 default:
10156 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10157 "1428 Invalid PCI device group: 0x%x\n",
10158 phba->pci_dev_grp);
10159 break;
10160 }
10161 return rc;
10162}
10163
10164/**
10165 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
10166 * @pdev: pointer to PCI device
10167 *
10168 * This routine is registered to the PCI subsystem for error handling. It
10169 * is called when kernel error recovery tells the lpfc driver that it is
10170 * OK to resume normal PCI operation after PCI bus error recovery. When
10171 * this routine is invoked, it dispatches the action to the proper SLI-3
10172 * or SLI-4 device io_resume routine, which will resume the device operation.
10173 **/
10174static void
10175lpfc_io_resume(struct pci_dev *pdev)
10176{
10177 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10178 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10179
10180 switch (phba->pci_dev_grp) {
10181 case LPFC_PCI_DEV_LP:
10182 lpfc_io_resume_s3(pdev);
10183 break;
James Smartda0436e2009-05-22 14:51:39 -040010184 case LPFC_PCI_DEV_OC:
10185 lpfc_io_resume_s4(pdev);
10186 break;
James Smart3772a992009-05-22 14:50:54 -040010187 default:
10188 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10189 "1429 Invalid PCI device group: 0x%x\n",
10190 phba->pci_dev_grp);
10191 break;
10192 }
10193 return;
10194}
10195
James Smart3ef6d242012-01-18 16:23:48 -050010196/**
10197 * lpfc_mgmt_open - method called when 'lpfcmgmt' is opened from userspace
10198 * @inode: pointer to the inode representing the lpfcmgmt device
10199 * @filep: pointer to the file representing the open lpfcmgmt device
10200 *
10201 * This routine puts a reference count on the lpfc module whenever the
10202 * character device is opened
10203 **/
10204static int
10205lpfc_mgmt_open(struct inode *inode, struct file *filep)
10206{
10207 try_module_get(THIS_MODULE);
10208 return 0;
10209}
10210
10211/**
10212 * lpfc_mgmt_release - method called when 'lpfcmgmt' is closed in userspace
10213 * @inode: pointer to the inode representing the lpfcmgmt device
10214 * @filep: pointer to the file representing the open lpfcmgmt device
10215 *
10216 * This routine removes a reference count from the lpfc module when the
10217 * character device is closed
10218 **/
10219static int
10220lpfc_mgmt_release(struct inode *inode, struct file *filep)
10221{
10222 module_put(THIS_MODULE);
10223 return 0;
10224}
10225
dea31012005-04-17 16:05:31 -050010226static struct pci_device_id lpfc_id_table[] = {
10227 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
10228 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -040010229 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
10230 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010231 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
10232 PCI_ANY_ID, PCI_ANY_ID, },
10233 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
10234 PCI_ANY_ID, PCI_ANY_ID, },
10235 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
10236 PCI_ANY_ID, PCI_ANY_ID, },
10237 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
10238 PCI_ANY_ID, PCI_ANY_ID, },
10239 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
10240 PCI_ANY_ID, PCI_ANY_ID, },
10241 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
10242 PCI_ANY_ID, PCI_ANY_ID, },
10243 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
10244 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050010245 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
10246 PCI_ANY_ID, PCI_ANY_ID, },
10247 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
10248 PCI_ANY_ID, PCI_ANY_ID, },
10249 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
10250 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010251 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
10252 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050010253 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
10254 PCI_ANY_ID, PCI_ANY_ID, },
10255 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
10256 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010257 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
10258 PCI_ANY_ID, PCI_ANY_ID, },
10259 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
10260 PCI_ANY_ID, PCI_ANY_ID, },
10261 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
10262 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -040010263 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
10264 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050010265 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
10266 PCI_ANY_ID, PCI_ANY_ID, },
10267 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
10268 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010269 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
10270 PCI_ANY_ID, PCI_ANY_ID, },
10271 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
10272 PCI_ANY_ID, PCI_ANY_ID, },
10273 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
10274 PCI_ANY_ID, PCI_ANY_ID, },
10275 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
10276 PCI_ANY_ID, PCI_ANY_ID, },
10277 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
10278 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050010279 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
10280 PCI_ANY_ID, PCI_ANY_ID, },
10281 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
10282 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -040010283 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
10284 PCI_ANY_ID, PCI_ANY_ID, },
10285 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
10286 PCI_ANY_ID, PCI_ANY_ID, },
10287 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
10288 PCI_ANY_ID, PCI_ANY_ID, },
10289 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
10290 PCI_ANY_ID, PCI_ANY_ID, },
10291 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
10292 PCI_ANY_ID, PCI_ANY_ID, },
10293 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
10294 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -040010295 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
10296 PCI_ANY_ID, PCI_ANY_ID, },
10297 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
10298 PCI_ANY_ID, PCI_ANY_ID, },
10299 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
10300 PCI_ANY_ID, PCI_ANY_ID, },
James Smart3772a992009-05-22 14:50:54 -040010301 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
10302 PCI_ANY_ID, PCI_ANY_ID, },
James Smarta747c9c2009-11-18 15:41:10 -050010303 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
10304 PCI_ANY_ID, PCI_ANY_ID, },
10305 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
James Smart6669f9b2009-10-02 15:16:45 -040010306 PCI_ANY_ID, PCI_ANY_ID, },
James Smart98fc5dd2010-06-07 15:24:29 -040010307 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BALIUS,
10308 PCI_ANY_ID, PCI_ANY_ID, },
James Smart085c6472010-11-20 23:11:37 -050010309 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC,
10310 PCI_ANY_ID, PCI_ANY_ID, },
10311 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE,
10312 PCI_ANY_ID, PCI_ANY_ID, },
James Smartc0c11512011-05-24 11:41:34 -040010313 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC_VF,
10314 PCI_ANY_ID, PCI_ANY_ID, },
10315 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE_VF,
10316 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050010317 { 0 }
10318};
10319
10320MODULE_DEVICE_TABLE(pci, lpfc_id_table);
10321
Linas Vepstas8d63f372007-02-14 14:28:36 -060010322static struct pci_error_handlers lpfc_err_handler = {
10323 .error_detected = lpfc_io_error_detected,
10324 .slot_reset = lpfc_io_slot_reset,
10325 .resume = lpfc_io_resume,
10326};
10327
dea31012005-04-17 16:05:31 -050010328static struct pci_driver lpfc_driver = {
10329 .name = LPFC_DRIVER_NAME,
10330 .id_table = lpfc_id_table,
10331 .probe = lpfc_pci_probe_one,
10332 .remove = __devexit_p(lpfc_pci_remove_one),
James Smart3a55b532008-12-04 22:38:54 -050010333 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -040010334 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -050010335 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -050010336};
10337
James Smart3ef6d242012-01-18 16:23:48 -050010338static const struct file_operations lpfc_mgmt_fop = {
10339 .open = lpfc_mgmt_open,
10340 .release = lpfc_mgmt_release,
10341};
10342
10343static struct miscdevice lpfc_mgmt_dev = {
10344 .minor = MISC_DYNAMIC_MINOR,
10345 .name = "lpfcmgmt",
10346 .fops = &lpfc_mgmt_fop,
10347};
10348
James Smarte59058c2008-08-24 21:49:00 -040010349/**
James Smart3621a712009-04-06 18:47:14 -040010350 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -040010351 *
10352 * This routine is to be invoked when the lpfc module is loaded into the
10353 * kernel. The special kernel macro module_init() is used to indicate the
10354 * role of this routine to the kernel as lpfc module entry point.
10355 *
10356 * Return codes
10357 * 0 - successful
10358 * -ENOMEM - FC attach transport failed
10359 * all others - failed
10360 */
dea31012005-04-17 16:05:31 -050010361static int __init
10362lpfc_init(void)
10363{
10364 int error = 0;
10365
10366 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010367 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -050010368
James Smart3ef6d242012-01-18 16:23:48 -050010369 error = misc_register(&lpfc_mgmt_dev);
10370 if (error)
10371 printk(KERN_ERR "Could not register lpfcmgmt device, "
10372 "misc_register returned with status %d", error);
10373
James Smart7ee5d432007-10-27 13:37:17 -040010374 if (lpfc_enable_npiv) {
10375 lpfc_transport_functions.vport_create = lpfc_vport_create;
10376 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
10377 }
dea31012005-04-17 16:05:31 -050010378 lpfc_transport_template =
10379 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -040010380 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -050010381 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -040010382 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -040010383 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -040010384 fc_attach_transport(&lpfc_vport_transport_functions);
10385 if (lpfc_vport_transport_template == NULL) {
10386 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040010387 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -040010388 }
James Smart7ee5d432007-10-27 13:37:17 -040010389 }
dea31012005-04-17 16:05:31 -050010390 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -050010391 if (error) {
dea31012005-04-17 16:05:31 -050010392 fc_release_transport(lpfc_transport_template);
James Smartd7c255b2008-08-24 21:50:00 -040010393 if (lpfc_enable_npiv)
10394 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -050010395 }
dea31012005-04-17 16:05:31 -050010396
10397 return error;
10398}
10399
James Smarte59058c2008-08-24 21:49:00 -040010400/**
James Smart3621a712009-04-06 18:47:14 -040010401 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -040010402 *
10403 * This routine is invoked when the lpfc module is removed from the kernel.
10404 * The special kernel macro module_exit() is used to indicate the role of
10405 * this routine to the kernel as lpfc module exit point.
10406 */
dea31012005-04-17 16:05:31 -050010407static void __exit
10408lpfc_exit(void)
10409{
James Smart3ef6d242012-01-18 16:23:48 -050010410 misc_deregister(&lpfc_mgmt_dev);
dea31012005-04-17 16:05:31 -050010411 pci_unregister_driver(&lpfc_driver);
10412 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040010413 if (lpfc_enable_npiv)
10414 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -050010415 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040010416 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
10417 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050010418 (1L << _dump_buf_data_order), _dump_buf_data);
10419 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
10420 }
10421
10422 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -040010423 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
10424 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050010425 (1L << _dump_buf_dif_order), _dump_buf_dif);
10426 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
10427 }
dea31012005-04-17 16:05:31 -050010428}
10429
10430module_init(lpfc_init);
10431module_exit(lpfc_exit);
10432MODULE_LICENSE("GPL");
10433MODULE_DESCRIPTION(LPFC_MODULE_DESC);
10434MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
10435MODULE_VERSION("0:" LPFC_DRIVER_VERSION);