blob: 2fb0396c3806bf7cc1314f2db4e5c68e5529538e [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 Smart16a59fb2014-04-04 13:52:41 -04004 * Copyright (C) 2004-2014 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>
James Smart7bb03bb2013-04-17 20:19:16 -040036#include <linux/percpu.h>
dea31012005-04-17 16:05:31 -050037
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040038#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050039#include <scsi/scsi_device.h>
40#include <scsi/scsi_host.h>
41#include <scsi/scsi_transport_fc.h>
42
James Smartda0436e2009-05-22 14:51:39 -040043#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050044#include "lpfc_hw.h"
45#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040046#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040047#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050048#include "lpfc_disc.h"
49#include "lpfc_scsi.h"
50#include "lpfc.h"
51#include "lpfc_logmsg.h"
52#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050053#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050054#include "lpfc_version.h"
55
James Smart81301a92008-12-04 22:39:46 -050056char *_dump_buf_data;
57unsigned long _dump_buf_data_order;
58char *_dump_buf_dif;
59unsigned long _dump_buf_dif_order;
60spinlock_t _dump_buf_lock;
61
James Smart7bb03bb2013-04-17 20:19:16 -040062/* Used when mapping IRQ vectors in a driver centric manner */
James Smartb246de12013-05-31 17:03:07 -040063uint16_t *lpfc_used_cpu;
64uint32_t lpfc_present_cpu;
James Smart7bb03bb2013-04-17 20:19:16 -040065
dea31012005-04-17 16:05:31 -050066static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
67static int lpfc_post_rcv_buf(struct lpfc_hba *);
James Smart5350d872011-10-10 21:33:49 -040068static int lpfc_sli4_queue_verify(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040069static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
70static int lpfc_setup_endian_order(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040071static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
James Smart8a9d2e82012-05-09 21:16:12 -040072static void lpfc_free_els_sgl_list(struct lpfc_hba *);
73static void lpfc_init_sgl_list(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040074static int lpfc_init_active_sgl_array(struct lpfc_hba *);
75static void lpfc_free_active_sgl(struct lpfc_hba *);
76static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
77static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
78static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
79static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
80static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
James Smart618a5232012-06-12 13:54:36 -040081static void lpfc_sli4_disable_intr(struct lpfc_hba *);
82static uint32_t lpfc_sli4_enable_intr(struct lpfc_hba *, uint32_t);
James Smart1ba981f2014-02-20 09:56:45 -050083static void lpfc_sli4_oas_verify(struct lpfc_hba *phba);
dea31012005-04-17 16:05:31 -050084
85static struct scsi_transport_template *lpfc_transport_template = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050086static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea31012005-04-17 16:05:31 -050087static DEFINE_IDR(lpfc_hba_index);
88
James Smarte59058c2008-08-24 21:49:00 -040089/**
James Smart3621a712009-04-06 18:47:14 -040090 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
James Smarte59058c2008-08-24 21:49:00 -040091 * @phba: pointer to lpfc hba data structure.
92 *
93 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
94 * mailbox command. It retrieves the revision information from the HBA and
95 * collects the Vital Product Data (VPD) about the HBA for preparing the
96 * configuration of the HBA.
97 *
98 * Return codes:
99 * 0 - success.
100 * -ERESTART - requests the SLI layer to reset the HBA and try again.
101 * Any other value - indicates an error.
102 **/
dea31012005-04-17 16:05:31 -0500103int
James Smart2e0fef82007-06-17 19:56:36 -0500104lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500105{
106 lpfc_vpd_t *vp = &phba->vpd;
107 int i = 0, rc;
108 LPFC_MBOXQ_t *pmb;
109 MAILBOX_t *mb;
110 char *lpfc_vpd_data = NULL;
111 uint16_t offset = 0;
112 static char licensed[56] =
113 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -0400114 static int init_key = 1;
dea31012005-04-17 16:05:31 -0500115
116 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
117 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500118 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500119 return -ENOMEM;
120 }
121
James Smart04c68492009-05-22 14:52:52 -0400122 mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -0500123 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500124
125 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -0400126 if (init_key) {
127 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -0500128
James Smart65a29c12006-07-06 15:50:50 -0400129 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
130 *ptext = cpu_to_be32(*ptext);
131 init_key = 0;
132 }
dea31012005-04-17 16:05:31 -0500133
134 lpfc_read_nv(phba, pmb);
135 memset((char*)mb->un.varRDnvp.rsvd3, 0,
136 sizeof (mb->un.varRDnvp.rsvd3));
137 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
138 sizeof (licensed));
139
140 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
141
142 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500143 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smarte8b62012007-08-02 11:10:09 -0400144 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500145 "error, mbxCmd x%x READ_NVPARM, "
146 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500147 mb->mbxCommand, mb->mbxStatus);
148 mempool_free(pmb, phba->mbox_mem_pool);
149 return -ERESTART;
150 }
151 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500152 sizeof(phba->wwnn));
153 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
154 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500155 }
156
James Smart92d7f7b2007-06-17 19:56:38 -0500157 phba->sli3_options = 0x0;
158
dea31012005-04-17 16:05:31 -0500159 /* Setup and issue mailbox READ REV command */
160 lpfc_read_rev(phba, pmb);
161 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
162 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500163 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400164 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500165 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500166 mb->mbxCommand, mb->mbxStatus);
167 mempool_free( pmb, phba->mbox_mem_pool);
168 return -ERESTART;
169 }
170
James Smart92d7f7b2007-06-17 19:56:38 -0500171
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500172 /*
173 * The value of rr must be 1 since the driver set the cv field to 1.
174 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500175 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500176 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500177 vp->rev.rBit = 0;
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500178 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400179 "0440 Adapter failed to init, READ_REV has "
180 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500181 mempool_free(pmb, phba->mbox_mem_pool);
182 return -ERESTART;
dea31012005-04-17 16:05:31 -0500183 }
184
James Smart495a7142008-06-14 22:52:59 -0400185 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
186 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -0500187 return -EINVAL;
James Smart495a7142008-06-14 22:52:59 -0400188 }
James Smarted957682007-06-17 19:56:37 -0500189
dea31012005-04-17 16:05:31 -0500190 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500191 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500192 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500193 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
194 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
195 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
196 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500197 vp->rev.biuRev = mb->un.varRdRev.biuRev;
198 vp->rev.smRev = mb->un.varRdRev.smRev;
199 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
200 vp->rev.endecRev = mb->un.varRdRev.endecRev;
201 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
202 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
203 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
204 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
205 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
206 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
207
James Smart92d7f7b2007-06-17 19:56:38 -0500208 /* If the sli feature level is less then 9, we must
209 * tear down all RPIs and VPIs on link down if NPIV
210 * is enabled.
211 */
212 if (vp->rev.feaLevelHigh < 9)
213 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
214
dea31012005-04-17 16:05:31 -0500215 if (lpfc_is_LC_HBA(phba->pcidev->device))
216 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
217 sizeof (phba->RandomData));
218
dea31012005-04-17 16:05:31 -0500219 /* Get adapter VPD information */
dea31012005-04-17 16:05:31 -0500220 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
221 if (!lpfc_vpd_data)
James Smartd7c255b2008-08-24 21:50:00 -0400222 goto out_free_mbox;
dea31012005-04-17 16:05:31 -0500223 do {
James Smarta0c87cb2009-07-19 10:01:10 -0400224 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
dea31012005-04-17 16:05:31 -0500225 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
226
227 if (rc != MBX_SUCCESS) {
228 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400229 "0441 VPD not present on adapter, "
dea31012005-04-17 16:05:31 -0500230 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500231 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500232 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500233 }
James Smart04c68492009-05-22 14:52:52 -0400234 /* dump mem may return a zero when finished or we got a
235 * mailbox error, either way we are done.
236 */
237 if (mb->un.varDmp.word_cnt == 0)
238 break;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500239 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
240 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
James Smartd7c255b2008-08-24 21:50:00 -0400241 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
242 lpfc_vpd_data + offset,
James Smart92d7f7b2007-06-17 19:56:38 -0500243 mb->un.varDmp.word_cnt);
dea31012005-04-17 16:05:31 -0500244 offset += mb->un.varDmp.word_cnt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500245 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
246 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500247
248 kfree(lpfc_vpd_data);
dea31012005-04-17 16:05:31 -0500249out_free_mbox:
250 mempool_free(pmb, phba->mbox_mem_pool);
251 return 0;
252}
253
James Smarte59058c2008-08-24 21:49:00 -0400254/**
James Smart3621a712009-04-06 18:47:14 -0400255 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
James Smarte59058c2008-08-24 21:49:00 -0400256 * @phba: pointer to lpfc hba data structure.
257 * @pmboxq: pointer to the driver internal queue element for mailbox command.
258 *
259 * This is the completion handler for driver's configuring asynchronous event
260 * mailbox command to the device. If the mailbox command returns successfully,
261 * it will set internal async event support flag to 1; otherwise, it will
262 * set internal async event support flag to 0.
263 **/
James Smart57127f12007-10-27 13:37:05 -0400264static void
265lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
266{
James Smart04c68492009-05-22 14:52:52 -0400267 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
James Smart57127f12007-10-27 13:37:05 -0400268 phba->temp_sensor_support = 1;
269 else
270 phba->temp_sensor_support = 0;
271 mempool_free(pmboxq, phba->mbox_mem_pool);
272 return;
273}
274
James Smarte59058c2008-08-24 21:49:00 -0400275/**
James Smart3621a712009-04-06 18:47:14 -0400276 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
James Smart97207482008-12-04 22:39:19 -0500277 * @phba: pointer to lpfc hba data structure.
278 * @pmboxq: pointer to the driver internal queue element for mailbox command.
279 *
280 * This is the completion handler for dump mailbox command for getting
281 * wake up parameters. When this command complete, the response contain
282 * Option rom version of the HBA. This function translate the version number
283 * into a human readable string and store it in OptionROMVersion.
284 **/
285static void
286lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
287{
288 struct prog_id *prg;
289 uint32_t prog_id_word;
290 char dist = ' ';
291 /* character array used for decoding dist type. */
292 char dist_char[] = "nabx";
293
James Smart04c68492009-05-22 14:52:52 -0400294 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
James Smart9f1e1b52008-12-04 22:39:40 -0500295 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500296 return;
James Smart9f1e1b52008-12-04 22:39:40 -0500297 }
James Smart97207482008-12-04 22:39:19 -0500298
299 prg = (struct prog_id *) &prog_id_word;
300
301 /* word 7 contain option rom version */
James Smart04c68492009-05-22 14:52:52 -0400302 prog_id_word = pmboxq->u.mb.un.varWords[7];
James Smart97207482008-12-04 22:39:19 -0500303
304 /* Decode the Option rom version word to a readable string */
305 if (prg->dist < 4)
306 dist = dist_char[prg->dist];
307
308 if ((prg->dist == 3) && (prg->num == 0))
309 sprintf(phba->OptionROMVersion, "%d.%d%d",
310 prg->ver, prg->rev, prg->lev);
311 else
312 sprintf(phba->OptionROMVersion, "%d.%d%d%c%d",
313 prg->ver, prg->rev, prg->lev,
314 dist, prg->num);
James Smart9f1e1b52008-12-04 22:39:40 -0500315 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500316 return;
317}
318
319/**
James Smart05580562011-05-24 11:40:48 -0400320 * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
321 * cfg_soft_wwnn, cfg_soft_wwpn
322 * @vport: pointer to lpfc vport data structure.
323 *
324 *
325 * Return codes
326 * None.
327 **/
328void
329lpfc_update_vport_wwn(struct lpfc_vport *vport)
330{
331 /* If the soft name exists then update it using the service params */
332 if (vport->phba->cfg_soft_wwnn)
333 u64_to_wwn(vport->phba->cfg_soft_wwnn,
334 vport->fc_sparam.nodeName.u.wwn);
335 if (vport->phba->cfg_soft_wwpn)
336 u64_to_wwn(vport->phba->cfg_soft_wwpn,
337 vport->fc_sparam.portName.u.wwn);
338
339 /*
340 * If the name is empty or there exists a soft name
341 * then copy the service params name, otherwise use the fc name
342 */
343 if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
344 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
345 sizeof(struct lpfc_name));
346 else
347 memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
348 sizeof(struct lpfc_name));
349
350 if (vport->fc_portname.u.wwn[0] == 0 || vport->phba->cfg_soft_wwpn)
351 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
352 sizeof(struct lpfc_name));
353 else
354 memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
355 sizeof(struct lpfc_name));
356}
357
358/**
James Smart3621a712009-04-06 18:47:14 -0400359 * lpfc_config_port_post - Perform lpfc initialization after config port
James Smarte59058c2008-08-24 21:49:00 -0400360 * @phba: pointer to lpfc hba data structure.
361 *
362 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
363 * command call. It performs all internal resource and state setups on the
364 * port: post IOCB buffers, enable appropriate host interrupt attentions,
365 * ELS ring timers, etc.
366 *
367 * Return codes
368 * 0 - success.
369 * Any other value - error.
370 **/
dea31012005-04-17 16:05:31 -0500371int
James Smart2e0fef82007-06-17 19:56:36 -0500372lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500373{
James Smart2e0fef82007-06-17 19:56:36 -0500374 struct lpfc_vport *vport = phba->pport;
James Smarta257bf92009-04-06 18:48:10 -0400375 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -0500376 LPFC_MBOXQ_t *pmb;
377 MAILBOX_t *mb;
378 struct lpfc_dmabuf *mp;
379 struct lpfc_sli *psli = &phba->sli;
380 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500381 int i, j;
382 int rc;
dea31012005-04-17 16:05:31 -0500383
James Smart7af67052007-10-27 13:38:11 -0400384 spin_lock_irq(&phba->hbalock);
385 /*
386 * If the Config port completed correctly the HBA is not
387 * over heated any more.
388 */
389 if (phba->over_temp_state == HBA_OVER_TEMP)
390 phba->over_temp_state = HBA_NORMAL_TEMP;
391 spin_unlock_irq(&phba->hbalock);
392
dea31012005-04-17 16:05:31 -0500393 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
394 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500395 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500396 return -ENOMEM;
397 }
James Smart04c68492009-05-22 14:52:52 -0400398 mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500399
dea31012005-04-17 16:05:31 -0500400 /* Get login parameters for NID. */
James Smart9f1177a2010-02-26 14:12:57 -0500401 rc = lpfc_read_sparam(phba, pmb, 0);
402 if (rc) {
403 mempool_free(pmb, phba->mbox_mem_pool);
404 return -ENOMEM;
405 }
406
James Smarted957682007-06-17 19:56:37 -0500407 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500408 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500409 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400410 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500411 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500412 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500413 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500414 mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart9f1177a2010-02-26 14:12:57 -0500415 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500416 lpfc_mbuf_free(phba, mp->virt, mp->phys);
417 kfree(mp);
418 return -EIO;
419 }
420
421 mp = (struct lpfc_dmabuf *) pmb->context1;
422
James Smart2e0fef82007-06-17 19:56:36 -0500423 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500424 lpfc_mbuf_free(phba, mp->virt, mp->phys);
425 kfree(mp);
426 pmb->context1 = NULL;
James Smart05580562011-05-24 11:40:48 -0400427 lpfc_update_vport_wwn(vport);
James Smarta257bf92009-04-06 18:48:10 -0400428
429 /* Update the fc_host data structures with new wwn. */
430 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
431 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart21e9a0a2009-05-22 14:53:21 -0400432 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smarta257bf92009-04-06 18:48:10 -0400433
dea31012005-04-17 16:05:31 -0500434 /* If no serial number in VPD data, use low 6 bytes of WWNN */
435 /* This should be consolidated into parse_vpd ? - mr */
436 if (phba->SerialNumber[0] == 0) {
437 uint8_t *outptr;
438
James Smart2e0fef82007-06-17 19:56:36 -0500439 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500440 for (i = 0; i < 12; i++) {
441 status = *outptr++;
442 j = ((status & 0xf0) >> 4);
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 i++;
450 j = (status & 0xf);
451 if (j <= 9)
452 phba->SerialNumber[i] =
453 (char)((uint8_t) 0x30 + (uint8_t) j);
454 else
455 phba->SerialNumber[i] =
456 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
457 }
458 }
459
dea31012005-04-17 16:05:31 -0500460 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500461 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500462 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500463 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400464 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500465 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500466 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500467 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500468 mempool_free( pmb, phba->mbox_mem_pool);
469 return -EIO;
470 }
471
James Smarta0c87cb2009-07-19 10:01:10 -0400472 /* Check if the port is disabled */
473 lpfc_sli_read_link_ste(phba);
474
dea31012005-04-17 16:05:31 -0500475 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -0400476 i = (mb->un.varRdConfig.max_xri + 1);
477 if (phba->cfg_hba_queue_depth > i) {
478 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
479 "3359 HBA queue depth changed from %d to %d\n",
480 phba->cfg_hba_queue_depth, i);
481 phba->cfg_hba_queue_depth = i;
482 }
483
484 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
485 i = (mb->un.varRdConfig.max_xri >> 3);
486 if (phba->pport->cfg_lun_queue_depth > i) {
487 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
488 "3360 LUN queue depth changed from %d to %d\n",
489 phba->pport->cfg_lun_queue_depth, i);
490 phba->pport->cfg_lun_queue_depth = i;
491 }
dea31012005-04-17 16:05:31 -0500492
493 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500494
495 /* Get the default values for Model Name and Description */
496 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
497
James Smart2e0fef82007-06-17 19:56:36 -0500498 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500499
James Smart0b727fe2007-10-27 13:37:25 -0400500 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smart7e56aa22012-08-03 12:35:34 -0400501 if (psli->ring[psli->extra_ring].sli.sli3.cmdringaddr)
James Smarta4bc3372006-12-02 13:34:16 -0500502 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
James Smart7e56aa22012-08-03 12:35:34 -0400503 if (psli->ring[psli->fcp_ring].sli.sli3.cmdringaddr)
dea31012005-04-17 16:05:31 -0500504 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
James Smart7e56aa22012-08-03 12:35:34 -0400505 if (psli->ring[psli->next_ring].sli.sli3.cmdringaddr)
dea31012005-04-17 16:05:31 -0500506 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
507
508 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500509 if (phba->sli_rev != 3)
510 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500511
James Smart93996272008-08-24 21:50:30 -0400512 /*
513 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
514 */
515 if (phba->intr_type == MSIX) {
516 rc = lpfc_config_msi(phba, pmb);
517 if (rc) {
518 mempool_free(pmb, phba->mbox_mem_pool);
519 return -EIO;
520 }
521 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
522 if (rc != MBX_SUCCESS) {
523 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
524 "0352 Config MSI mailbox command "
525 "failed, mbxCmd x%x, mbxStatus x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400526 pmb->u.mb.mbxCommand,
527 pmb->u.mb.mbxStatus);
James Smart93996272008-08-24 21:50:30 -0400528 mempool_free(pmb, phba->mbox_mem_pool);
529 return -EIO;
530 }
531 }
532
James Smart04c68492009-05-22 14:52:52 -0400533 spin_lock_irq(&phba->hbalock);
James Smart93996272008-08-24 21:50:30 -0400534 /* Initialize ERATT handling flag */
535 phba->hba_flag &= ~HBA_ERATT_HANDLED;
536
dea31012005-04-17 16:05:31 -0500537 /* Enable appropriate host interrupts */
James Smart9940b972011-03-11 16:06:12 -0500538 if (lpfc_readl(phba->HCregaddr, &status)) {
539 spin_unlock_irq(&phba->hbalock);
540 return -EIO;
541 }
dea31012005-04-17 16:05:31 -0500542 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
543 if (psli->num_rings > 0)
544 status |= HC_R0INT_ENA;
545 if (psli->num_rings > 1)
546 status |= HC_R1INT_ENA;
547 if (psli->num_rings > 2)
548 status |= HC_R2INT_ENA;
549 if (psli->num_rings > 3)
550 status |= HC_R3INT_ENA;
551
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500552 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
553 (phba->cfg_poll & DISABLE_FCP_RING_INT))
James Smart93996272008-08-24 21:50:30 -0400554 status &= ~(HC_R0INT_ENA);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500555
dea31012005-04-17 16:05:31 -0500556 writel(status, phba->HCregaddr);
557 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500558 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500559
James Smart93996272008-08-24 21:50:30 -0400560 /* Set up ring-0 (ELS) timer */
561 timeout = phba->fc_ratov * 2;
James Smart256ec0d2013-04-17 20:14:58 -0400562 mod_timer(&vport->els_tmofunc,
563 jiffies + msecs_to_jiffies(1000 * timeout));
James Smart93996272008-08-24 21:50:30 -0400564 /* Set up heart beat (HB) timer */
James Smart256ec0d2013-04-17 20:14:58 -0400565 mod_timer(&phba->hb_tmofunc,
566 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -0500567 phba->hb_outstanding = 0;
568 phba->last_completion_time = jiffies;
James Smart93996272008-08-24 21:50:30 -0400569 /* Set up error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -0400570 mod_timer(&phba->eratt_poll,
571 jiffies + msecs_to_jiffies(1000 * LPFC_ERATT_POLL_INTERVAL));
dea31012005-04-17 16:05:31 -0500572
James Smarta0c87cb2009-07-19 10:01:10 -0400573 if (phba->hba_flag & LINK_DISABLED) {
574 lpfc_printf_log(phba,
575 KERN_ERR, LOG_INIT,
576 "2598 Adapter Link is disabled.\n");
577 lpfc_down_link(phba, pmb);
578 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
579 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
580 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
581 lpfc_printf_log(phba,
582 KERN_ERR, LOG_INIT,
583 "2599 Adapter failed to issue DOWN_LINK"
584 " mbox command rc 0x%x\n", rc);
585
586 mempool_free(pmb, phba->mbox_mem_pool);
587 return -EIO;
588 }
James Smarte40a02c2010-02-26 14:13:54 -0500589 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart026abb82011-12-13 13:20:45 -0500590 mempool_free(pmb, phba->mbox_mem_pool);
591 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
592 if (rc)
593 return rc;
dea31012005-04-17 16:05:31 -0500594 }
595 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400596 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500597 if (!pmb) {
598 phba->link_state = LPFC_HBA_ERROR;
599 return -ENOMEM;
600 }
601
James Smart57127f12007-10-27 13:37:05 -0400602 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
603 pmb->mbox_cmpl = lpfc_config_async_cmpl;
604 pmb->vport = phba->pport;
605 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500606
James Smart57127f12007-10-27 13:37:05 -0400607 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
608 lpfc_printf_log(phba,
609 KERN_ERR,
610 LOG_INIT,
611 "0456 Adapter failed to issue "
James Smarte4e74272009-07-19 10:01:38 -0400612 "ASYNCEVT_ENABLE mbox status x%x\n",
James Smart57127f12007-10-27 13:37:05 -0400613 rc);
614 mempool_free(pmb, phba->mbox_mem_pool);
615 }
James Smart97207482008-12-04 22:39:19 -0500616
617 /* Get Option rom version */
618 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500619 if (!pmb) {
620 phba->link_state = LPFC_HBA_ERROR;
621 return -ENOMEM;
622 }
623
James Smart97207482008-12-04 22:39:19 -0500624 lpfc_dump_wakeup_param(phba, pmb);
625 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
626 pmb->vport = phba->pport;
627 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
628
629 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
630 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
James Smarte4e74272009-07-19 10:01:38 -0400631 "to get Option ROM version status x%x\n", rc);
James Smart97207482008-12-04 22:39:19 -0500632 mempool_free(pmb, phba->mbox_mem_pool);
633 }
634
James Smartd7c255b2008-08-24 21:50:00 -0400635 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400636}
637
James Smarte59058c2008-08-24 21:49:00 -0400638/**
James Smart84d1b002010-02-12 14:42:33 -0500639 * lpfc_hba_init_link - Initialize the FC link
640 * @phba: pointer to lpfc hba data structure.
James Smart6e7288d2010-06-07 15:23:35 -0400641 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500642 *
643 * This routine will issue the INIT_LINK mailbox command call.
644 * It is available to other drivers through the lpfc_hba data
645 * structure for use as a delayed link up mechanism with the
646 * module parameter lpfc_suppress_link_up.
647 *
648 * Return code
649 * 0 - success
650 * Any other value - error
651 **/
652int
James Smart6e7288d2010-06-07 15:23:35 -0400653lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500654{
James Smart1b511972011-12-13 13:23:09 -0500655 return lpfc_hba_init_link_fc_topology(phba, phba->cfg_topology, flag);
656}
657
658/**
659 * lpfc_hba_init_link_fc_topology - Initialize FC link with desired topology
660 * @phba: pointer to lpfc hba data structure.
661 * @fc_topology: desired fc topology.
662 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
663 *
664 * This routine will issue the INIT_LINK mailbox command call.
665 * It is available to other drivers through the lpfc_hba data
666 * structure for use as a delayed link up mechanism with the
667 * module parameter lpfc_suppress_link_up.
668 *
669 * Return code
670 * 0 - success
671 * Any other value - error
672 **/
673int
674lpfc_hba_init_link_fc_topology(struct lpfc_hba *phba, uint32_t fc_topology,
675 uint32_t flag)
676{
James Smart84d1b002010-02-12 14:42:33 -0500677 struct lpfc_vport *vport = phba->pport;
678 LPFC_MBOXQ_t *pmb;
679 MAILBOX_t *mb;
680 int rc;
681
682 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
683 if (!pmb) {
684 phba->link_state = LPFC_HBA_ERROR;
685 return -ENOMEM;
686 }
687 mb = &pmb->u.mb;
688 pmb->vport = vport;
689
James Smart026abb82011-12-13 13:20:45 -0500690 if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_MAX) ||
691 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G) &&
692 !(phba->lmt & LMT_1Gb)) ||
693 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G) &&
694 !(phba->lmt & LMT_2Gb)) ||
695 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G) &&
696 !(phba->lmt & LMT_4Gb)) ||
697 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G) &&
698 !(phba->lmt & LMT_8Gb)) ||
699 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G) &&
700 !(phba->lmt & LMT_10Gb)) ||
701 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G) &&
702 !(phba->lmt & LMT_16Gb))) {
703 /* Reset link speed to auto */
704 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
705 "1302 Invalid speed for this board:%d "
706 "Reset link speed to auto.\n",
707 phba->cfg_link_speed);
708 phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
709 }
James Smart1b511972011-12-13 13:23:09 -0500710 lpfc_init_link(phba, pmb, fc_topology, phba->cfg_link_speed);
James Smart84d1b002010-02-12 14:42:33 -0500711 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart1b511972011-12-13 13:23:09 -0500712 if (phba->sli_rev < LPFC_SLI_REV4)
713 lpfc_set_loopback_flag(phba);
James Smart6e7288d2010-06-07 15:23:35 -0400714 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart76a95d72010-11-20 23:11:48 -0500715 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
James Smart84d1b002010-02-12 14:42:33 -0500716 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
717 "0498 Adapter failed to init, mbxCmd x%x "
718 "INIT_LINK, mbxStatus x%x\n",
719 mb->mbxCommand, mb->mbxStatus);
James Smart76a95d72010-11-20 23:11:48 -0500720 if (phba->sli_rev <= LPFC_SLI_REV3) {
721 /* Clear all interrupt enable conditions */
722 writel(0, phba->HCregaddr);
723 readl(phba->HCregaddr); /* flush */
724 /* Clear all pending interrupts */
725 writel(0xffffffff, phba->HAregaddr);
726 readl(phba->HAregaddr); /* flush */
727 }
James Smart84d1b002010-02-12 14:42:33 -0500728 phba->link_state = LPFC_HBA_ERROR;
James Smart6e7288d2010-06-07 15:23:35 -0400729 if (rc != MBX_BUSY || flag == MBX_POLL)
James Smart84d1b002010-02-12 14:42:33 -0500730 mempool_free(pmb, phba->mbox_mem_pool);
731 return -EIO;
732 }
James Smarte40a02c2010-02-26 14:13:54 -0500733 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
James Smart6e7288d2010-06-07 15:23:35 -0400734 if (flag == MBX_POLL)
735 mempool_free(pmb, phba->mbox_mem_pool);
James Smart84d1b002010-02-12 14:42:33 -0500736
737 return 0;
738}
739
740/**
741 * lpfc_hba_down_link - this routine downs the FC link
James Smart6e7288d2010-06-07 15:23:35 -0400742 * @phba: pointer to lpfc hba data structure.
743 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500744 *
745 * This routine will issue the DOWN_LINK mailbox command call.
746 * It is available to other drivers through the lpfc_hba data
747 * structure for use to stop the link.
748 *
749 * Return code
750 * 0 - success
751 * Any other value - error
752 **/
753int
James Smart6e7288d2010-06-07 15:23:35 -0400754lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500755{
756 LPFC_MBOXQ_t *pmb;
757 int rc;
758
759 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
760 if (!pmb) {
761 phba->link_state = LPFC_HBA_ERROR;
762 return -ENOMEM;
763 }
764
765 lpfc_printf_log(phba,
766 KERN_ERR, LOG_INIT,
767 "0491 Adapter Link is disabled.\n");
768 lpfc_down_link(phba, pmb);
769 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6e7288d2010-06-07 15:23:35 -0400770 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart84d1b002010-02-12 14:42:33 -0500771 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
772 lpfc_printf_log(phba,
773 KERN_ERR, LOG_INIT,
774 "2522 Adapter failed to issue DOWN_LINK"
775 " mbox command rc 0x%x\n", rc);
776
777 mempool_free(pmb, phba->mbox_mem_pool);
778 return -EIO;
779 }
James Smart6e7288d2010-06-07 15:23:35 -0400780 if (flag == MBX_POLL)
781 mempool_free(pmb, phba->mbox_mem_pool);
782
James Smart84d1b002010-02-12 14:42:33 -0500783 return 0;
784}
785
786/**
James Smart3621a712009-04-06 18:47:14 -0400787 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400788 * @phba: pointer to lpfc HBA data structure.
789 *
790 * This routine will do LPFC uninitialization before the HBA is reset when
791 * bringing down the SLI Layer.
792 *
793 * Return codes
794 * 0 - success.
795 * Any other value - error.
796 **/
dea31012005-04-17 16:05:31 -0500797int
James Smart2e0fef82007-06-17 19:56:36 -0500798lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500799{
James Smart1b32f6a2008-02-08 18:49:39 -0500800 struct lpfc_vport **vports;
801 int i;
James Smart3772a992009-05-22 14:50:54 -0400802
803 if (phba->sli_rev <= LPFC_SLI_REV3) {
804 /* Disable interrupts */
805 writel(0, phba->HCregaddr);
806 readl(phba->HCregaddr); /* flush */
807 }
dea31012005-04-17 16:05:31 -0500808
James Smart1b32f6a2008-02-08 18:49:39 -0500809 if (phba->pport->load_flag & FC_UNLOADING)
810 lpfc_cleanup_discovery_resources(phba->pport);
811 else {
812 vports = lpfc_create_vport_work_array(phba);
813 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400814 for (i = 0; i <= phba->max_vports &&
815 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500816 lpfc_cleanup_discovery_resources(vports[i]);
817 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500818 }
819 return 0;
dea31012005-04-17 16:05:31 -0500820}
821
James Smarte59058c2008-08-24 21:49:00 -0400822/**
James Smartbcece5f2014-04-04 13:51:34 -0400823 * lpfc_hba_free_post_buf - Perform lpfc uninitialization after HBA reset
824 * @phba: pointer to lpfc HBA data structure.
825 *
826 * This routine will cleanup posted ELS buffers after the HBA is reset
827 * when bringing down the SLI Layer.
828 *
829 *
830 * Return codes
831 * void.
832 **/
833static void
834lpfc_hba_free_post_buf(struct lpfc_hba *phba)
835{
836 struct lpfc_sli *psli = &phba->sli;
837 struct lpfc_sli_ring *pring;
838 struct lpfc_dmabuf *mp, *next_mp;
James Smart07eab622014-04-04 13:51:44 -0400839 LIST_HEAD(buflist);
840 int count;
James Smartbcece5f2014-04-04 13:51:34 -0400841
842 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
843 lpfc_sli_hbqbuf_free_all(phba);
844 else {
845 /* Cleanup preposted buffers on the ELS ring */
James Smartbcece5f2014-04-04 13:51:34 -0400846 pring = &psli->ring[LPFC_ELS_RING];
James Smart07eab622014-04-04 13:51:44 -0400847 spin_lock_irq(&phba->hbalock);
848 list_splice_init(&pring->postbufq, &buflist);
849 spin_unlock_irq(&phba->hbalock);
850
851 count = 0;
852 list_for_each_entry_safe(mp, next_mp, &buflist, list) {
James Smartbcece5f2014-04-04 13:51:34 -0400853 list_del(&mp->list);
James Smart07eab622014-04-04 13:51:44 -0400854 count++;
James Smartbcece5f2014-04-04 13:51:34 -0400855 lpfc_mbuf_free(phba, mp->virt, mp->phys);
856 kfree(mp);
857 }
James Smart07eab622014-04-04 13:51:44 -0400858
859 spin_lock_irq(&phba->hbalock);
860 pring->postbufq_cnt -= count;
James Smartbcece5f2014-04-04 13:51:34 -0400861 spin_unlock_irq(&phba->hbalock);
862 }
863}
864
865/**
866 * lpfc_hba_clean_txcmplq - Perform lpfc uninitialization after HBA reset
867 * @phba: pointer to lpfc HBA data structure.
868 *
869 * This routine will cleanup the txcmplq after the HBA is reset when bringing
870 * down the SLI Layer.
871 *
872 * Return codes
873 * void
874 **/
875static void
876lpfc_hba_clean_txcmplq(struct lpfc_hba *phba)
877{
878 struct lpfc_sli *psli = &phba->sli;
879 struct lpfc_sli_ring *pring;
880 LIST_HEAD(completions);
881 int i;
882
James Smartbcece5f2014-04-04 13:51:34 -0400883 for (i = 0; i < psli->num_rings; i++) {
884 pring = &psli->ring[i];
885 if (phba->sli_rev >= LPFC_SLI_REV4)
886 spin_lock_irq(&pring->ring_lock);
887 else
888 spin_lock_irq(&phba->hbalock);
889 /* At this point in time the HBA is either reset or DOA. Either
890 * way, nothing should be on txcmplq as it will NEVER complete.
891 */
892 list_splice_init(&pring->txcmplq, &completions);
James Smartdb55fba2014-04-04 13:52:02 -0400893 pring->txcmplq_cnt = 0;
James Smartbcece5f2014-04-04 13:51:34 -0400894
895 if (phba->sli_rev >= LPFC_SLI_REV4)
896 spin_unlock_irq(&pring->ring_lock);
897 else
898 spin_unlock_irq(&phba->hbalock);
899
900 /* Cancel all the IOCBs from the completions list */
901 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
902 IOERR_SLI_ABORTED);
903 lpfc_sli_abort_iocb_ring(phba, pring);
904 }
905}
906
907/**
James Smart3772a992009-05-22 14:50:54 -0400908 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smartbcece5f2014-04-04 13:51:34 -0400909 int i;
James Smarte59058c2008-08-24 21:49:00 -0400910 * @phba: pointer to lpfc HBA data structure.
911 *
912 * This routine will do uninitialization after the HBA is reset when bring
913 * down the SLI Layer.
914 *
915 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200916 * 0 - success.
James Smarte59058c2008-08-24 21:49:00 -0400917 * Any other value - error.
918 **/
James Smart3772a992009-05-22 14:50:54 -0400919static int
920lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -0500921{
James Smartbcece5f2014-04-04 13:51:34 -0400922 lpfc_hba_free_post_buf(phba);
923 lpfc_hba_clean_txcmplq(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500924 return 0;
925}
James Smart5af5eee2010-10-22 11:06:38 -0400926
James Smartda0436e2009-05-22 14:51:39 -0400927/**
928 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
929 * @phba: pointer to lpfc HBA data structure.
930 *
931 * This routine will do uninitialization after the HBA is reset when bring
932 * down the SLI Layer.
933 *
934 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200935 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400936 * Any other value - error.
937 **/
938static int
939lpfc_hba_down_post_s4(struct lpfc_hba *phba)
940{
941 struct lpfc_scsi_buf *psb, *psb_next;
942 LIST_HEAD(aborts);
James Smartda0436e2009-05-22 14:51:39 -0400943 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500944 struct lpfc_sglq *sglq_entry = NULL;
945
James Smartbcece5f2014-04-04 13:51:34 -0400946 lpfc_hba_free_post_buf(phba);
947 lpfc_hba_clean_txcmplq(phba);
948
James Smartda0436e2009-05-22 14:51:39 -0400949 /* At this point in time the HBA is either reset or DOA. Either
950 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
951 * on the lpfc_sgl_list so that it can either be freed if the
952 * driver is unloading or reposted if the driver is restarting
953 * the port.
954 */
955 spin_lock_irq(&phba->hbalock); /* required for lpfc_sgl_list and */
956 /* scsl_buf_list */
957 /* abts_sgl_list_lock required because worker thread uses this
958 * list.
959 */
960 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -0500961 list_for_each_entry(sglq_entry,
962 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
963 sglq_entry->state = SGL_FREED;
964
James Smartda0436e2009-05-22 14:51:39 -0400965 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
966 &phba->sli4_hba.lpfc_sgl_list);
967 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
968 /* abts_scsi_buf_list_lock required because worker thread uses this
969 * list.
970 */
971 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
972 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
973 &aborts);
974 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
975 spin_unlock_irq(&phba->hbalock);
976
977 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
978 psb->pCmd = NULL;
979 psb->status = IOSTAT_SUCCESS;
980 }
James Smarta40fc5f2013-04-17 20:17:40 -0400981 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
982 list_splice(&aborts, &phba->lpfc_scsi_buf_list_put);
983 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400984 return 0;
985}
986
987/**
988 * lpfc_hba_down_post - Wrapper func for hba down post routine
989 * @phba: pointer to lpfc HBA data structure.
990 *
991 * This routine wraps the actual SLI3 or SLI4 routine for performing
992 * uninitialization after the HBA is reset when bring down the SLI Layer.
993 *
994 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200995 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400996 * Any other value - error.
997 **/
998int
999lpfc_hba_down_post(struct lpfc_hba *phba)
1000{
1001 return (*phba->lpfc_hba_down_post)(phba);
1002}
Jamie Wellnitz41415862006-02-28 19:25:27 -05001003
James Smarte59058c2008-08-24 21:49:00 -04001004/**
James Smart3621a712009-04-06 18:47:14 -04001005 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001006 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1007 *
1008 * This is the HBA-timer timeout handler registered to the lpfc driver. When
1009 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
1010 * work-port-events bitmap and the worker thread is notified. This timeout
1011 * event will be used by the worker thread to invoke the actual timeout
1012 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
1013 * be performed in the timeout handler and the HBA timeout event bit shall
1014 * be cleared by the worker thread after it has taken the event bitmap out.
1015 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001016static void
James Smart858c9f62007-06-17 19:56:39 -05001017lpfc_hb_timeout(unsigned long ptr)
1018{
1019 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -04001020 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -05001021 unsigned long iflag;
1022
1023 phba = (struct lpfc_hba *)ptr;
James Smart93996272008-08-24 21:50:30 -04001024
1025 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -05001026 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04001027 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
1028 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -05001029 phba->pport->work_port_events |= WORKER_HB_TMO;
1030 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
1031
James Smart93996272008-08-24 21:50:30 -04001032 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -04001033 if (!tmo_posted)
1034 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -05001035 return;
1036}
1037
James Smarte59058c2008-08-24 21:49:00 -04001038/**
James Smart19ca7602010-11-20 23:11:55 -05001039 * lpfc_rrq_timeout - The RRQ-timer timeout handler
1040 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1041 *
1042 * This is the RRQ-timer timeout handler registered to the lpfc driver. When
1043 * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
1044 * work-port-events bitmap and the worker thread is notified. This timeout
1045 * event will be used by the worker thread to invoke the actual timeout
1046 * handler routine, lpfc_rrq_handler. Any periodical operations will
1047 * be performed in the timeout handler and the RRQ timeout event bit shall
1048 * be cleared by the worker thread after it has taken the event bitmap out.
1049 **/
1050static void
1051lpfc_rrq_timeout(unsigned long ptr)
1052{
1053 struct lpfc_hba *phba;
James Smart19ca7602010-11-20 23:11:55 -05001054 unsigned long iflag;
1055
1056 phba = (struct lpfc_hba *)ptr;
1057 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001058 if (!(phba->pport->load_flag & FC_UNLOADING))
1059 phba->hba_flag |= HBA_RRQ_ACTIVE;
1060 else
1061 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart19ca7602010-11-20 23:11:55 -05001062 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001063
1064 if (!(phba->pport->load_flag & FC_UNLOADING))
1065 lpfc_worker_wake_up(phba);
James Smart19ca7602010-11-20 23:11:55 -05001066}
1067
1068/**
James Smart3621a712009-04-06 18:47:14 -04001069 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -04001070 * @phba: pointer to lpfc hba data structure.
1071 * @pmboxq: pointer to the driver internal queue element for mailbox command.
1072 *
1073 * This is the callback function to the lpfc heart-beat mailbox command.
1074 * If configured, the lpfc driver issues the heart-beat mailbox command to
1075 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
1076 * heart-beat mailbox command is issued, the driver shall set up heart-beat
1077 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
1078 * heart-beat outstanding state. Once the mailbox command comes back and
1079 * no error conditions detected, the heart-beat mailbox command timer is
1080 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1081 * state is cleared for the next heart-beat. If the timer expired with the
1082 * heart-beat outstanding state set, the driver will put the HBA offline.
1083 **/
James Smart858c9f62007-06-17 19:56:39 -05001084static void
1085lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1086{
1087 unsigned long drvr_flag;
1088
1089 spin_lock_irqsave(&phba->hbalock, drvr_flag);
1090 phba->hb_outstanding = 0;
1091 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1092
James Smart93996272008-08-24 21:50:30 -04001093 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -05001094 mempool_free(pmboxq, phba->mbox_mem_pool);
1095 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1096 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -04001097 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -05001098 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001099 jiffies +
1100 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001101 return;
1102}
1103
James Smarte59058c2008-08-24 21:49:00 -04001104/**
James Smart3621a712009-04-06 18:47:14 -04001105 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001106 * @phba: pointer to lpfc hba data structure.
1107 *
1108 * This is the actual HBA-timer timeout handler to be invoked by the worker
1109 * thread whenever the HBA timer fired and HBA-timeout event posted. This
1110 * handler performs any periodic operations needed for the device. If such
1111 * periodic event has already been attended to either in the interrupt handler
1112 * or by processing slow-ring or fast-ring events within the HBA-timer
1113 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1114 * the timer for the next timeout period. If lpfc heart-beat mailbox command
1115 * is configured and there is no heart-beat mailbox command outstanding, a
1116 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1117 * has been a heart-beat mailbox command outstanding, the HBA shall be put
1118 * to offline.
1119 **/
James Smart858c9f62007-06-17 19:56:39 -05001120void
1121lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1122{
James Smart45ed1192009-10-02 15:17:02 -04001123 struct lpfc_vport **vports;
James Smart858c9f62007-06-17 19:56:39 -05001124 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -05001125 struct lpfc_dmabuf *buf_ptr;
James Smart45ed1192009-10-02 15:17:02 -04001126 int retval, i;
James Smart858c9f62007-06-17 19:56:39 -05001127 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -05001128 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -05001129
James Smart45ed1192009-10-02 15:17:02 -04001130 vports = lpfc_create_vport_work_array(phba);
1131 if (vports != NULL)
1132 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
1133 lpfc_rcv_seq_check_edtov(vports[i]);
1134 lpfc_destroy_vport_work_array(phba, vports);
1135
James Smart858c9f62007-06-17 19:56:39 -05001136 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -04001137 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -05001138 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1139 return;
1140
1141 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -05001142
James Smart256ec0d2013-04-17 20:14:58 -04001143 if (time_after(phba->last_completion_time +
1144 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL),
1145 jiffies)) {
James Smart858c9f62007-06-17 19:56:39 -05001146 spin_unlock_irq(&phba->pport->work_port_lock);
1147 if (!phba->hb_outstanding)
1148 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001149 jiffies +
1150 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001151 else
1152 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001153 jiffies +
1154 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001155 return;
1156 }
1157 spin_unlock_irq(&phba->pport->work_port_lock);
1158
James Smart0ff10d42008-01-11 01:52:36 -05001159 if (phba->elsbuf_cnt &&
1160 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1161 spin_lock_irq(&phba->hbalock);
1162 list_splice_init(&phba->elsbuf, &completions);
1163 phba->elsbuf_cnt = 0;
1164 phba->elsbuf_prev_cnt = 0;
1165 spin_unlock_irq(&phba->hbalock);
1166
1167 while (!list_empty(&completions)) {
1168 list_remove_head(&completions, buf_ptr,
1169 struct lpfc_dmabuf, list);
1170 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1171 kfree(buf_ptr);
1172 }
1173 }
1174 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1175
James Smart858c9f62007-06-17 19:56:39 -05001176 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -05001177 if (phba->cfg_enable_hba_heartbeat) {
1178 if (!phba->hb_outstanding) {
James Smartbc739052010-08-04 16:11:18 -04001179 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1180 (list_empty(&psli->mboxq))) {
1181 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1182 GFP_KERNEL);
1183 if (!pmboxq) {
1184 mod_timer(&phba->hb_tmofunc,
1185 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001186 msecs_to_jiffies(1000 *
1187 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001188 return;
1189 }
James Smart13815c82008-01-11 01:52:48 -05001190
James Smartbc739052010-08-04 16:11:18 -04001191 lpfc_heart_beat(phba, pmboxq);
1192 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1193 pmboxq->vport = phba->pport;
1194 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1195 MBX_NOWAIT);
James Smart13815c82008-01-11 01:52:48 -05001196
James Smartbc739052010-08-04 16:11:18 -04001197 if (retval != MBX_BUSY &&
1198 retval != MBX_SUCCESS) {
1199 mempool_free(pmboxq,
1200 phba->mbox_mem_pool);
1201 mod_timer(&phba->hb_tmofunc,
1202 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001203 msecs_to_jiffies(1000 *
1204 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001205 return;
1206 }
1207 phba->skipped_hb = 0;
1208 phba->hb_outstanding = 1;
1209 } else if (time_before_eq(phba->last_completion_time,
1210 phba->skipped_hb)) {
1211 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1212 "2857 Last completion time not "
1213 " updated in %d ms\n",
1214 jiffies_to_msecs(jiffies
1215 - phba->last_completion_time));
1216 } else
1217 phba->skipped_hb = jiffies;
1218
James Smart858c9f62007-06-17 19:56:39 -05001219 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001220 jiffies +
1221 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001222 return;
James Smart13815c82008-01-11 01:52:48 -05001223 } else {
1224 /*
1225 * If heart beat timeout called with hb_outstanding set
James Smartdcf2a4e2010-09-29 11:18:53 -04001226 * we need to give the hb mailbox cmd a chance to
1227 * complete or TMO.
James Smart13815c82008-01-11 01:52:48 -05001228 */
James Smartdcf2a4e2010-09-29 11:18:53 -04001229 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1230 "0459 Adapter heartbeat still out"
1231 "standing:last compl time was %d ms.\n",
1232 jiffies_to_msecs(jiffies
1233 - phba->last_completion_time));
1234 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001235 jiffies +
1236 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001237 }
James Smart858c9f62007-06-17 19:56:39 -05001238 }
1239}
1240
James Smarte59058c2008-08-24 21:49:00 -04001241/**
James Smart3621a712009-04-06 18:47:14 -04001242 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -04001243 * @phba: pointer to lpfc hba data structure.
1244 *
1245 * This routine is called to bring the HBA offline when HBA hardware error
1246 * other than Port Error 6 has been detected.
1247 **/
James Smart09372822008-01-11 01:52:54 -05001248static void
1249lpfc_offline_eratt(struct lpfc_hba *phba)
1250{
1251 struct lpfc_sli *psli = &phba->sli;
1252
1253 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001254 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -05001255 spin_unlock_irq(&phba->hbalock);
James Smart618a5232012-06-12 13:54:36 -04001256 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart09372822008-01-11 01:52:54 -05001257
1258 lpfc_offline(phba);
1259 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001260 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001261 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001262 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001263 lpfc_hba_down_post(phba);
1264 lpfc_sli_brdready(phba, HS_MBRDY);
1265 lpfc_unblock_mgmt_io(phba);
1266 phba->link_state = LPFC_HBA_ERROR;
1267 return;
1268}
1269
James Smarte59058c2008-08-24 21:49:00 -04001270/**
James Smartda0436e2009-05-22 14:51:39 -04001271 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1272 * @phba: pointer to lpfc hba data structure.
1273 *
1274 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1275 * other than Port Error 6 has been detected.
1276 **/
James Smarta88dbb62013-04-17 20:18:39 -04001277void
James Smartda0436e2009-05-22 14:51:39 -04001278lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1279{
James Smart618a5232012-06-12 13:54:36 -04001280 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smartda0436e2009-05-22 14:51:39 -04001281 lpfc_offline(phba);
1282 lpfc_sli4_brdreset(phba);
1283 lpfc_hba_down_post(phba);
1284 lpfc_sli4_post_status_check(phba);
1285 lpfc_unblock_mgmt_io(phba);
1286 phba->link_state = LPFC_HBA_ERROR;
1287}
1288
1289/**
James Smarta257bf92009-04-06 18:48:10 -04001290 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1291 * @phba: pointer to lpfc hba data structure.
1292 *
1293 * This routine is invoked to handle the deferred HBA hardware error
1294 * conditions. This type of error is indicated by HBA by setting ER1
1295 * and another ER bit in the host status register. The driver will
1296 * wait until the ER1 bit clears before handling the error condition.
1297 **/
1298static void
1299lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1300{
1301 uint32_t old_host_status = phba->work_hs;
James Smarta257bf92009-04-06 18:48:10 -04001302 struct lpfc_sli *psli = &phba->sli;
1303
James Smartf4b4c682009-05-22 14:53:12 -04001304 /* If the pci channel is offline, ignore possible errors,
1305 * since we cannot communicate with the pci card anyway.
1306 */
1307 if (pci_channel_offline(phba->pcidev)) {
1308 spin_lock_irq(&phba->hbalock);
1309 phba->hba_flag &= ~DEFER_ERATT;
1310 spin_unlock_irq(&phba->hbalock);
1311 return;
1312 }
1313
James Smarta257bf92009-04-06 18:48:10 -04001314 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1315 "0479 Deferred Adapter Hardware Error "
1316 "Data: x%x x%x x%x\n",
1317 phba->work_hs,
1318 phba->work_status[0], phba->work_status[1]);
1319
1320 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001321 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001322 spin_unlock_irq(&phba->hbalock);
1323
1324
1325 /*
1326 * Firmware stops when it triggred erratt. That could cause the I/Os
1327 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1328 * SCSI layer retry it after re-establishing link.
1329 */
James Smartdb55fba2014-04-04 13:52:02 -04001330 lpfc_sli_abort_fcp_rings(phba);
James Smarta257bf92009-04-06 18:48:10 -04001331
1332 /*
1333 * There was a firmware error. Take the hba offline and then
1334 * attempt to restart it.
1335 */
James Smart618a5232012-06-12 13:54:36 -04001336 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smarta257bf92009-04-06 18:48:10 -04001337 lpfc_offline(phba);
1338
1339 /* Wait for the ER1 bit to clear.*/
1340 while (phba->work_hs & HS_FFER1) {
1341 msleep(100);
James Smart9940b972011-03-11 16:06:12 -05001342 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1343 phba->work_hs = UNPLUG_ERR ;
1344 break;
1345 }
James Smarta257bf92009-04-06 18:48:10 -04001346 /* If driver is unloading let the worker thread continue */
1347 if (phba->pport->load_flag & FC_UNLOADING) {
1348 phba->work_hs = 0;
1349 break;
1350 }
1351 }
1352
1353 /*
1354 * This is to ptrotect against a race condition in which
1355 * first write to the host attention register clear the
1356 * host status register.
1357 */
1358 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1359 phba->work_hs = old_host_status & ~HS_FFER1;
1360
James Smart3772a992009-05-22 14:50:54 -04001361 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001362 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001363 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001364 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1365 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1366}
1367
James Smart3772a992009-05-22 14:50:54 -04001368static void
1369lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1370{
1371 struct lpfc_board_event_header board_event;
1372 struct Scsi_Host *shost;
1373
1374 board_event.event_type = FC_REG_BOARD_EVENT;
1375 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1376 shost = lpfc_shost_from_vport(phba->pport);
1377 fc_host_post_vendor_event(shost, fc_get_event_number(),
1378 sizeof(board_event),
1379 (char *) &board_event,
1380 LPFC_NL_VENDOR_ID);
1381}
1382
James Smarta257bf92009-04-06 18:48:10 -04001383/**
James Smart3772a992009-05-22 14:50:54 -04001384 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001385 * @phba: pointer to lpfc hba data structure.
1386 *
1387 * This routine is invoked to handle the following HBA hardware error
1388 * conditions:
1389 * 1 - HBA error attention interrupt
1390 * 2 - DMA ring index out of range
1391 * 3 - Mailbox command came back as unknown
1392 **/
James Smart3772a992009-05-22 14:50:54 -04001393static void
1394lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001395{
James Smart2e0fef82007-06-17 19:56:36 -05001396 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001397 struct lpfc_sli *psli = &phba->sli;
James Smartd2873e42006-08-18 17:46:43 -04001398 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001399 unsigned long temperature;
1400 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001401 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001402
Linas Vepstas8d63f372007-02-14 14:28:36 -06001403 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001404 * since we cannot communicate with the pci card anyway.
1405 */
1406 if (pci_channel_offline(phba->pcidev)) {
1407 spin_lock_irq(&phba->hbalock);
1408 phba->hba_flag &= ~DEFER_ERATT;
1409 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001410 return;
James Smart3772a992009-05-22 14:50:54 -04001411 }
1412
James Smart13815c82008-01-11 01:52:48 -05001413 /* If resets are disabled then leave the HBA alone and return */
1414 if (!phba->cfg_enable_hba_reset)
1415 return;
dea31012005-04-17 16:05:31 -05001416
James Smartea2151b2008-09-07 11:52:10 -04001417 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001418 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001419
James Smarta257bf92009-04-06 18:48:10 -04001420 if (phba->hba_flag & DEFER_ERATT)
1421 lpfc_handle_deferred_eratt(phba);
1422
James Smartdcf2a4e2010-09-29 11:18:53 -04001423 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1424 if (phba->work_hs & HS_FFER6)
1425 /* Re-establishing Link */
1426 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1427 "1301 Re-establishing Link "
1428 "Data: x%x x%x x%x\n",
1429 phba->work_hs, phba->work_status[0],
1430 phba->work_status[1]);
1431 if (phba->work_hs & HS_FFER8)
1432 /* Device Zeroization */
1433 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1434 "2861 Host Authentication device "
1435 "zeroization Data:x%x x%x x%x\n",
1436 phba->work_hs, phba->work_status[0],
1437 phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001438
James Smart92d7f7b2007-06-17 19:56:38 -05001439 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001440 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001441 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001442
1443 /*
1444 * Firmware stops when it triggled erratt with HS_FFER6.
1445 * That could cause the I/Os dropped by the firmware.
1446 * Error iocb (I/O) on txcmplq and let the SCSI layer
1447 * retry it after re-establishing link.
1448 */
James Smartdb55fba2014-04-04 13:52:02 -04001449 lpfc_sli_abort_fcp_rings(phba);
dea31012005-04-17 16:05:31 -05001450
dea31012005-04-17 16:05:31 -05001451 /*
1452 * There was a firmware error. Take the hba offline and then
1453 * attempt to restart it.
1454 */
James Smart618a5232012-06-12 13:54:36 -04001455 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
dea31012005-04-17 16:05:31 -05001456 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001457 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001458 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001459 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001460 return;
1461 }
James Smart46fa3112007-04-25 09:51:45 -04001462 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001463 } else if (phba->work_hs & HS_CRIT_TEMP) {
1464 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1465 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1466 temp_event_data.event_code = LPFC_CRIT_TEMP;
1467 temp_event_data.data = (uint32_t)temperature;
1468
1469 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001470 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001471 "(%ld), taking this port offline "
1472 "Data: x%x x%x x%x\n",
1473 temperature, phba->work_hs,
1474 phba->work_status[0], phba->work_status[1]);
1475
1476 shost = lpfc_shost_from_vport(phba->pport);
1477 fc_host_post_vendor_event(shost, fc_get_event_number(),
1478 sizeof(temp_event_data),
1479 (char *) &temp_event_data,
1480 SCSI_NL_VID_TYPE_PCI
1481 | PCI_VENDOR_ID_EMULEX);
1482
James Smart7af67052007-10-27 13:38:11 -04001483 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001484 phba->over_temp_state = HBA_OVER_TEMP;
1485 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001486 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001487
dea31012005-04-17 16:05:31 -05001488 } else {
1489 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001490 * failure is a value other than FFER6. Do not call the offline
1491 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001492 */
1493 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001494 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001495 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001496 phba->work_hs,
dea31012005-04-17 16:05:31 -05001497 phba->work_status[0], phba->work_status[1]);
1498
James Smartd2873e42006-08-18 17:46:43 -04001499 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001500 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001501 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001502 sizeof(event_data), (char *) &event_data,
1503 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1504
James Smart09372822008-01-11 01:52:54 -05001505 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001506 }
James Smart93996272008-08-24 21:50:30 -04001507 return;
dea31012005-04-17 16:05:31 -05001508}
1509
James Smarte59058c2008-08-24 21:49:00 -04001510/**
James Smart618a5232012-06-12 13:54:36 -04001511 * lpfc_sli4_port_sta_fn_reset - The SLI4 function reset due to port status reg
1512 * @phba: pointer to lpfc hba data structure.
1513 * @mbx_action: flag for mailbox shutdown action.
1514 *
1515 * This routine is invoked to perform an SLI4 port PCI function reset in
1516 * response to port status register polling attention. It waits for port
1517 * status register (ERR, RDY, RN) bits before proceeding with function reset.
1518 * During this process, interrupt vectors are freed and later requested
1519 * for handling possible port resource change.
1520 **/
1521static int
James Smarte10b2022014-02-20 09:57:43 -05001522lpfc_sli4_port_sta_fn_reset(struct lpfc_hba *phba, int mbx_action,
1523 bool en_rn_msg)
James Smart618a5232012-06-12 13:54:36 -04001524{
1525 int rc;
1526 uint32_t intr_mode;
1527
1528 /*
1529 * On error status condition, driver need to wait for port
1530 * ready before performing reset.
1531 */
1532 rc = lpfc_sli4_pdev_status_reg_wait(phba);
1533 if (!rc) {
1534 /* need reset: attempt for port recovery */
James Smarte10b2022014-02-20 09:57:43 -05001535 if (en_rn_msg)
1536 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1537 "2887 Reset Needed: Attempting Port "
1538 "Recovery...\n");
James Smart618a5232012-06-12 13:54:36 -04001539 lpfc_offline_prep(phba, mbx_action);
1540 lpfc_offline(phba);
1541 /* release interrupt for possible resource change */
1542 lpfc_sli4_disable_intr(phba);
1543 lpfc_sli_brdrestart(phba);
1544 /* request and enable interrupt */
1545 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
1546 if (intr_mode == LPFC_INTR_ERROR) {
1547 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1548 "3175 Failed to enable interrupt\n");
1549 return -EIO;
1550 } else {
1551 phba->intr_mode = intr_mode;
1552 }
1553 rc = lpfc_online(phba);
1554 if (rc == 0)
1555 lpfc_unblock_mgmt_io(phba);
1556 }
1557 return rc;
1558}
1559
1560/**
James Smartda0436e2009-05-22 14:51:39 -04001561 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1562 * @phba: pointer to lpfc hba data structure.
1563 *
1564 * This routine is invoked to handle the SLI4 HBA hardware error attention
1565 * conditions.
1566 **/
1567static void
1568lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1569{
1570 struct lpfc_vport *vport = phba->pport;
1571 uint32_t event_data;
1572 struct Scsi_Host *shost;
James Smart2fcee4b2010-12-15 17:57:46 -05001573 uint32_t if_type;
James Smart2e90f4b2011-12-13 13:22:37 -05001574 struct lpfc_register portstat_reg = {0};
1575 uint32_t reg_err1, reg_err2;
1576 uint32_t uerrlo_reg, uemasklo_reg;
1577 uint32_t pci_rd_rc1, pci_rd_rc2;
James Smarte10b2022014-02-20 09:57:43 -05001578 bool en_rn_msg = true;
James Smart73d91e52011-10-10 21:32:10 -04001579 int rc;
James Smartda0436e2009-05-22 14:51:39 -04001580
1581 /* If the pci channel is offline, ignore possible errors, since
1582 * we cannot communicate with the pci card anyway.
1583 */
1584 if (pci_channel_offline(phba->pcidev))
1585 return;
1586 /* If resets are disabled then leave the HBA alone and return */
1587 if (!phba->cfg_enable_hba_reset)
1588 return;
1589
James Smart2fcee4b2010-12-15 17:57:46 -05001590 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1591 switch (if_type) {
1592 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart2e90f4b2011-12-13 13:22:37 -05001593 pci_rd_rc1 = lpfc_readl(
1594 phba->sli4_hba.u.if_type0.UERRLOregaddr,
1595 &uerrlo_reg);
1596 pci_rd_rc2 = lpfc_readl(
1597 phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
1598 &uemasklo_reg);
1599 /* consider PCI bus read error as pci_channel_offline */
1600 if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
1601 return;
James Smart2fcee4b2010-12-15 17:57:46 -05001602 lpfc_sli4_offline_eratt(phba);
1603 break;
1604 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart2e90f4b2011-12-13 13:22:37 -05001605 pci_rd_rc1 = lpfc_readl(
1606 phba->sli4_hba.u.if_type2.STATUSregaddr,
1607 &portstat_reg.word0);
1608 /* consider PCI bus read error as pci_channel_offline */
James Smart6b5151f2012-01-18 16:24:06 -05001609 if (pci_rd_rc1 == -EIO) {
1610 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1611 "3151 PCI bus read access failure: x%x\n",
1612 readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
James Smart2e90f4b2011-12-13 13:22:37 -05001613 return;
James Smart6b5151f2012-01-18 16:24:06 -05001614 }
James Smart2e90f4b2011-12-13 13:22:37 -05001615 reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
1616 reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
James Smart2fcee4b2010-12-15 17:57:46 -05001617 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
1618 /* TODO: Register for Overtemp async events. */
1619 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1620 "2889 Port Overtemperature event, "
James Smart026abb82011-12-13 13:20:45 -05001621 "taking port offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001622 spin_lock_irq(&phba->hbalock);
1623 phba->over_temp_state = HBA_OVER_TEMP;
1624 spin_unlock_irq(&phba->hbalock);
1625 lpfc_sli4_offline_eratt(phba);
James Smart2e90f4b2011-12-13 13:22:37 -05001626 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001627 }
James Smart2e90f4b2011-12-13 13:22:37 -05001628 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smarte10b2022014-02-20 09:57:43 -05001629 reg_err2 == SLIPORT_ERR2_REG_FW_RESTART) {
James Smart2e90f4b2011-12-13 13:22:37 -05001630 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte10b2022014-02-20 09:57:43 -05001631 "3143 Port Down: Firmware Update "
1632 "Detected\n");
1633 en_rn_msg = false;
1634 } else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smart2e90f4b2011-12-13 13:22:37 -05001635 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1636 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1637 "3144 Port Down: Debug Dump\n");
1638 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1639 reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
1640 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1641 "3145 Port Down: Provisioning\n");
James Smart618a5232012-06-12 13:54:36 -04001642
1643 /* Check port status register for function reset */
James Smarte10b2022014-02-20 09:57:43 -05001644 rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT,
1645 en_rn_msg);
James Smart618a5232012-06-12 13:54:36 -04001646 if (rc == 0) {
1647 /* don't report event on forced debug dump */
1648 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1649 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1650 return;
1651 else
1652 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001653 }
James Smart618a5232012-06-12 13:54:36 -04001654 /* fall through for not able to recover */
James Smart6b5151f2012-01-18 16:24:06 -05001655 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1656 "3152 Unrecoverable error, bring the port "
1657 "offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001658 lpfc_sli4_offline_eratt(phba);
1659 break;
1660 case LPFC_SLI_INTF_IF_TYPE_1:
1661 default:
1662 break;
1663 }
James Smart2e90f4b2011-12-13 13:22:37 -05001664 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1665 "3123 Report dump event to upper layer\n");
1666 /* Send an internal error event to mgmt application */
1667 lpfc_board_errevt_to_mgmt(phba);
1668
1669 event_data = FC_REG_DUMP_EVENT;
1670 shost = lpfc_shost_from_vport(vport);
1671 fc_host_post_vendor_event(shost, fc_get_event_number(),
1672 sizeof(event_data), (char *) &event_data,
1673 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
James Smartda0436e2009-05-22 14:51:39 -04001674}
1675
1676/**
1677 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1678 * @phba: pointer to lpfc HBA data structure.
1679 *
1680 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1681 * routine from the API jump table function pointer from the lpfc_hba struct.
1682 *
1683 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001684 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001685 * Any other value - error.
1686 **/
1687void
1688lpfc_handle_eratt(struct lpfc_hba *phba)
1689{
1690 (*phba->lpfc_handle_eratt)(phba);
1691}
1692
1693/**
James Smart3621a712009-04-06 18:47:14 -04001694 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04001695 * @phba: pointer to lpfc hba data structure.
1696 *
1697 * This routine is invoked from the worker thread to handle a HBA host
1698 * attention link event.
1699 **/
dea31012005-04-17 16:05:31 -05001700void
James Smart2e0fef82007-06-17 19:56:36 -05001701lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001702{
James Smart2e0fef82007-06-17 19:56:36 -05001703 struct lpfc_vport *vport = phba->pport;
1704 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001705 LPFC_MBOXQ_t *pmb;
1706 volatile uint32_t control;
1707 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05001708 int rc = 0;
dea31012005-04-17 16:05:31 -05001709
1710 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001711 if (!pmb) {
1712 rc = 1;
dea31012005-04-17 16:05:31 -05001713 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05001714 }
dea31012005-04-17 16:05:31 -05001715
1716 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001717 if (!mp) {
1718 rc = 2;
dea31012005-04-17 16:05:31 -05001719 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05001720 }
dea31012005-04-17 16:05:31 -05001721
1722 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05001723 if (!mp->virt) {
1724 rc = 3;
dea31012005-04-17 16:05:31 -05001725 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05001726 }
dea31012005-04-17 16:05:31 -05001727
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05001728 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04001729 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05001730
1731 psli->slistat.link_event++;
James Smart76a95d72010-11-20 23:11:48 -05001732 lpfc_read_topology(phba, pmb, mp);
1733 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smart2e0fef82007-06-17 19:56:36 -05001734 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04001735 /* Block ELS IOCBs until we have processed this mbox command */
1736 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04001737 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05001738 if (rc == MBX_NOT_FINISHED) {
1739 rc = 4;
James Smart14691152006-12-02 13:34:28 -05001740 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05001741 }
dea31012005-04-17 16:05:31 -05001742
1743 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05001744 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001745 writel(HA_LATT, phba->HAregaddr);
1746 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001747 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001748
1749 return;
1750
James Smart14691152006-12-02 13:34:28 -05001751lpfc_handle_latt_free_mbuf:
James Smart0d2b6b82008-06-14 22:52:47 -04001752 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05001753 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001754lpfc_handle_latt_free_mp:
1755 kfree(mp);
1756lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04001757 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001758lpfc_handle_latt_err_exit:
1759 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001760 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001761 psli->sli_flag |= LPFC_PROCESS_LA;
1762 control = readl(phba->HCregaddr);
1763 control |= HC_LAINT_ENA;
1764 writel(control, phba->HCregaddr);
1765 readl(phba->HCregaddr); /* flush */
1766
1767 /* Clear Link Attention in HA REG */
1768 writel(HA_LATT, phba->HAregaddr);
1769 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001770 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001771 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001772 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001773
James Smart09372822008-01-11 01:52:54 -05001774 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1775 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05001776
1777 return;
1778}
1779
James Smarte59058c2008-08-24 21:49:00 -04001780/**
James Smart3621a712009-04-06 18:47:14 -04001781 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04001782 * @phba: pointer to lpfc hba data structure.
1783 * @vpd: pointer to the vital product data.
1784 * @len: length of the vital product data in bytes.
1785 *
1786 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1787 * an array of characters. In this routine, the ModelName, ProgramType, and
1788 * ModelDesc, etc. fields of the phba data structure will be populated.
1789 *
1790 * Return codes
1791 * 0 - pointer to the VPD passed in is NULL
1792 * 1 - success
1793 **/
James Smart3772a992009-05-22 14:50:54 -04001794int
James Smart2e0fef82007-06-17 19:56:36 -05001795lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05001796{
1797 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05001798 int Length;
dea31012005-04-17 16:05:31 -05001799 int i, j;
1800 int finished = 0;
1801 int index = 0;
1802
1803 if (!vpd)
1804 return 0;
1805
1806 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05001807 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001808 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05001809 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1810 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001811 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05001812 switch (vpd[index]) {
1813 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001814 case 0x91:
dea31012005-04-17 16:05:31 -05001815 index += 1;
1816 lenlo = vpd[index];
1817 index += 1;
1818 lenhi = vpd[index];
1819 index += 1;
1820 i = ((((unsigned short)lenhi) << 8) + lenlo);
1821 index += i;
1822 break;
1823 case 0x90:
1824 index += 1;
1825 lenlo = vpd[index];
1826 index += 1;
1827 lenhi = vpd[index];
1828 index += 1;
1829 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001830 if (Length > len - index)
1831 Length = len - index;
dea31012005-04-17 16:05:31 -05001832 while (Length > 0) {
1833 /* Look for Serial Number */
1834 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1835 index += 2;
1836 i = vpd[index];
1837 index += 1;
1838 j = 0;
1839 Length -= (3+i);
1840 while(i--) {
1841 phba->SerialNumber[j++] = vpd[index++];
1842 if (j == 31)
1843 break;
1844 }
1845 phba->SerialNumber[j] = 0;
1846 continue;
1847 }
1848 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1849 phba->vpd_flag |= VPD_MODEL_DESC;
1850 index += 2;
1851 i = vpd[index];
1852 index += 1;
1853 j = 0;
1854 Length -= (3+i);
1855 while(i--) {
1856 phba->ModelDesc[j++] = vpd[index++];
1857 if (j == 255)
1858 break;
1859 }
1860 phba->ModelDesc[j] = 0;
1861 continue;
1862 }
1863 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1864 phba->vpd_flag |= VPD_MODEL_NAME;
1865 index += 2;
1866 i = vpd[index];
1867 index += 1;
1868 j = 0;
1869 Length -= (3+i);
1870 while(i--) {
1871 phba->ModelName[j++] = vpd[index++];
1872 if (j == 79)
1873 break;
1874 }
1875 phba->ModelName[j] = 0;
1876 continue;
1877 }
1878 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1879 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1880 index += 2;
1881 i = vpd[index];
1882 index += 1;
1883 j = 0;
1884 Length -= (3+i);
1885 while(i--) {
1886 phba->ProgramType[j++] = vpd[index++];
1887 if (j == 255)
1888 break;
1889 }
1890 phba->ProgramType[j] = 0;
1891 continue;
1892 }
1893 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1894 phba->vpd_flag |= VPD_PORT;
1895 index += 2;
1896 i = vpd[index];
1897 index += 1;
1898 j = 0;
1899 Length -= (3+i);
1900 while(i--) {
James Smartcd1c8302011-10-10 21:33:25 -04001901 if ((phba->sli_rev == LPFC_SLI_REV4) &&
1902 (phba->sli4_hba.pport_name_sta ==
1903 LPFC_SLI4_PPNAME_GET)) {
1904 j++;
1905 index++;
1906 } else
1907 phba->Port[j++] = vpd[index++];
1908 if (j == 19)
1909 break;
dea31012005-04-17 16:05:31 -05001910 }
James Smartcd1c8302011-10-10 21:33:25 -04001911 if ((phba->sli_rev != LPFC_SLI_REV4) ||
1912 (phba->sli4_hba.pport_name_sta ==
1913 LPFC_SLI4_PPNAME_NON))
1914 phba->Port[j] = 0;
dea31012005-04-17 16:05:31 -05001915 continue;
1916 }
1917 else {
1918 index += 2;
1919 i = vpd[index];
1920 index += 1;
1921 index += i;
1922 Length -= (3 + i);
1923 }
1924 }
1925 finished = 0;
1926 break;
1927 case 0x78:
1928 finished = 1;
1929 break;
1930 default:
1931 index ++;
1932 break;
1933 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001934 }
dea31012005-04-17 16:05:31 -05001935
1936 return(1);
1937}
1938
James Smarte59058c2008-08-24 21:49:00 -04001939/**
James Smart3621a712009-04-06 18:47:14 -04001940 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04001941 * @phba: pointer to lpfc hba data structure.
1942 * @mdp: pointer to the data structure to hold the derived model name.
1943 * @descp: pointer to the data structure to hold the derived description.
1944 *
1945 * This routine retrieves HBA's description based on its registered PCI device
1946 * ID. The @descp passed into this function points to an array of 256 chars. It
1947 * shall be returned with the model name, maximum speed, and the host bus type.
1948 * The @mdp passed into this function points to an array of 80 chars. When the
1949 * function returns, the @mdp will be filled with the model name.
1950 **/
dea31012005-04-17 16:05:31 -05001951static void
James Smart2e0fef82007-06-17 19:56:36 -05001952lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05001953{
1954 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05001955 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001956 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04001957 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04001958 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001959 struct {
James Smarta747c9c2009-11-18 15:41:10 -05001960 char *name;
1961 char *bus;
1962 char *function;
1963 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001964
1965 if (mdp && mdp[0] != '\0'
1966 && descp && descp[0] != '\0')
1967 return;
1968
James Smartc0c11512011-05-24 11:41:34 -04001969 if (phba->lmt & LMT_16Gb)
1970 max_speed = 16;
1971 else if (phba->lmt & LMT_10Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001972 max_speed = 10;
1973 else if (phba->lmt & LMT_8Gb)
1974 max_speed = 8;
1975 else if (phba->lmt & LMT_4Gb)
1976 max_speed = 4;
1977 else if (phba->lmt & LMT_2Gb)
1978 max_speed = 2;
James Smart4169d862012-09-29 11:32:09 -04001979 else if (phba->lmt & LMT_1Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001980 max_speed = 1;
James Smart4169d862012-09-29 11:32:09 -04001981 else
1982 max_speed = 0;
dea31012005-04-17 16:05:31 -05001983
1984 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05001985
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001986 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001987 case PCI_DEVICE_ID_FIREFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001988 m = (typeof(m)){"LP6000", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001989 break;
dea31012005-04-17 16:05:31 -05001990 case PCI_DEVICE_ID_SUPERFLY:
1991 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smarta747c9c2009-11-18 15:41:10 -05001992 m = (typeof(m)){"LP7000", "PCI",
1993 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001994 else
James Smarta747c9c2009-11-18 15:41:10 -05001995 m = (typeof(m)){"LP7000E", "PCI",
1996 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001997 break;
1998 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001999 m = (typeof(m)){"LP8000", "PCI",
2000 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002001 break;
2002 case PCI_DEVICE_ID_CENTAUR:
2003 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smarta747c9c2009-11-18 15:41:10 -05002004 m = (typeof(m)){"LP9002", "PCI",
2005 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002006 else
James Smarta747c9c2009-11-18 15:41:10 -05002007 m = (typeof(m)){"LP9000", "PCI",
2008 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002009 break;
2010 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002011 m = (typeof(m)){"LP952", "PCI",
2012 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002013 break;
2014 case PCI_DEVICE_ID_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05002015 m = (typeof(m)){"LP9802", "PCI-X",
2016 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002017 break;
2018 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05002019 m = (typeof(m)){"LP10000", "PCI-X",
2020 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002021 break;
2022 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05002023 m = (typeof(m)){"LPX1000", "PCI-X",
2024 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002025 break;
2026 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002027 m = (typeof(m)){"LP982", "PCI-X",
2028 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002029 break;
2030 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002031 m = (typeof(m)){"LP1050", "PCI-X",
2032 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002033 break;
2034 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05002035 m = (typeof(m)){"LP11000", "PCI-X2",
2036 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002037 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002038 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002039 m = (typeof(m)){"LP11000-SP", "PCI-X2",
2040 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002041 break;
2042 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002043 m = (typeof(m)){"LP11002-SP", "PCI-X2",
2044 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002045 break;
2046 case PCI_DEVICE_ID_NEPTUNE:
James Smarta747c9c2009-11-18 15:41:10 -05002047 m = (typeof(m)){"LPe1000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002048 break;
2049 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002050 m = (typeof(m)){"LPe1000-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002051 break;
2052 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002053 m = (typeof(m)){"LPe1002-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002054 break;
dea31012005-04-17 16:05:31 -05002055 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05002056 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002057 break;
2058 case PCI_DEVICE_ID_BSMB:
James Smarta747c9c2009-11-18 15:41:10 -05002059 m = (typeof(m)){"LP111", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002060 break;
2061 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05002062 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002063 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002064 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002065 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002066 break;
2067 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002068 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04002069 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002070 break;
dea31012005-04-17 16:05:31 -05002071 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05002072 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002073 break;
2074 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05002075 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002076 break;
2077 case PCI_DEVICE_ID_LP101:
James Smarta747c9c2009-11-18 15:41:10 -05002078 m = (typeof(m)){"LP101", "PCI-X", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002079 break;
2080 case PCI_DEVICE_ID_LP10000S:
James Smarta747c9c2009-11-18 15:41:10 -05002081 m = (typeof(m)){"LP10000-S", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002082 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002083 case PCI_DEVICE_ID_LP11000S:
James Smarta747c9c2009-11-18 15:41:10 -05002084 m = (typeof(m)){"LP11000-S", "PCI-X2", "Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05002085 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002086 case PCI_DEVICE_ID_LPE11000S:
James Smarta747c9c2009-11-18 15:41:10 -05002087 m = (typeof(m)){"LPe11000-S", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002088 break;
James Smartb87eab32007-04-25 09:53:28 -04002089 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05002090 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002091 break;
2092 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05002093 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002094 break;
2095 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05002096 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002097 break;
2098 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002099 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002100 break;
2101 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002102 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002103 break;
2104 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05002105 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002106 break;
James Smart84774a42008-08-24 21:50:06 -04002107 case PCI_DEVICE_ID_HORNET:
James Smarta747c9c2009-11-18 15:41:10 -05002108 m = (typeof(m)){"LP21000", "PCIe", "FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002109 GE = 1;
2110 break;
2111 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05002112 m = (typeof(m)){"LPev12000", "PCIe IOV",
2113 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002114 break;
2115 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05002116 m = (typeof(m)){"LPev12000", "PCIe IOV",
2117 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002118 break;
2119 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05002120 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
2121 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002122 break;
James Smartda0436e2009-05-22 14:51:39 -04002123 case PCI_DEVICE_ID_TIGERSHARK:
2124 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002125 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04002126 break;
James Smarta747c9c2009-11-18 15:41:10 -05002127 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04002128 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002129 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2130 break;
2131 case PCI_DEVICE_ID_FALCON:
2132 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2133 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04002134 break;
James Smart98fc5dd2010-06-07 15:24:29 -04002135 case PCI_DEVICE_ID_BALIUS:
2136 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
2137 "Fibre Channel Adapter"};
2138 break;
James Smart085c6472010-11-20 23:11:37 -05002139 case PCI_DEVICE_ID_LANCER_FC:
James Smartc0c11512011-05-24 11:41:34 -04002140 case PCI_DEVICE_ID_LANCER_FC_VF:
2141 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
James Smart085c6472010-11-20 23:11:37 -05002142 break;
2143 case PCI_DEVICE_ID_LANCER_FCOE:
James Smartc0c11512011-05-24 11:41:34 -04002144 case PCI_DEVICE_ID_LANCER_FCOE_VF:
James Smart085c6472010-11-20 23:11:37 -05002145 oneConnect = 1;
James Smart079b5c92011-08-21 21:48:49 -04002146 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
James Smart085c6472010-11-20 23:11:37 -05002147 break;
James Smartf8cafd32012-08-03 12:42:51 -04002148 case PCI_DEVICE_ID_SKYHAWK:
2149 case PCI_DEVICE_ID_SKYHAWK_VF:
2150 oneConnect = 1;
2151 m = (typeof(m)){"OCe14000", "PCIe", "FCoE"};
2152 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002153 default:
James Smarta747c9c2009-11-18 15:41:10 -05002154 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002155 break;
dea31012005-04-17 16:05:31 -05002156 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002157
2158 if (mdp && mdp[0] == '\0')
2159 snprintf(mdp, 79,"%s", m.name);
James Smartc0c11512011-05-24 11:41:34 -04002160 /*
2161 * oneConnect hba requires special processing, they are all initiators
James Smartda0436e2009-05-22 14:51:39 -04002162 * and we put the port number on the end
2163 */
2164 if (descp && descp[0] == '\0') {
2165 if (oneConnect)
2166 snprintf(descp, 255,
James Smart4169d862012-09-29 11:32:09 -04002167 "Emulex OneConnect %s, %s Initiator %s",
James Smarta747c9c2009-11-18 15:41:10 -05002168 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04002169 phba->Port);
James Smart4169d862012-09-29 11:32:09 -04002170 else if (max_speed == 0)
2171 snprintf(descp, 255,
2172 "Emulex %s %s %s ",
2173 m.name, m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002174 else
2175 snprintf(descp, 255,
2176 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05002177 m.name, max_speed, (GE) ? "GE" : "Gb",
2178 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002179 }
dea31012005-04-17 16:05:31 -05002180}
2181
James Smarte59058c2008-08-24 21:49:00 -04002182/**
James Smart3621a712009-04-06 18:47:14 -04002183 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04002184 * @phba: pointer to lpfc hba data structure.
2185 * @pring: pointer to a IOCB ring.
2186 * @cnt: the number of IOCBs to be posted to the IOCB ring.
2187 *
2188 * This routine posts a given number of IOCBs with the associated DMA buffer
2189 * descriptors specified by the cnt argument to the given IOCB ring.
2190 *
2191 * Return codes
2192 * The number of IOCBs NOT able to be posted to the IOCB ring.
2193 **/
dea31012005-04-17 16:05:31 -05002194int
James Smart495a7142008-06-14 22:52:59 -04002195lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05002196{
2197 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002198 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05002199 struct lpfc_dmabuf *mp1, *mp2;
2200
2201 cnt += pring->missbufcnt;
2202
2203 /* While there are buffers to post */
2204 while (cnt > 0) {
2205 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002206 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002207 if (iocb == NULL) {
2208 pring->missbufcnt = cnt;
2209 return cnt;
2210 }
dea31012005-04-17 16:05:31 -05002211 icmd = &iocb->iocb;
2212
2213 /* 2 buffers can be posted per command */
2214 /* Allocate buffer to post */
2215 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2216 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04002217 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2218 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002219 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002220 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002221 pring->missbufcnt = cnt;
2222 return cnt;
2223 }
2224
2225 INIT_LIST_HEAD(&mp1->list);
2226 /* Allocate buffer to post */
2227 if (cnt > 1) {
2228 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2229 if (mp2)
2230 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2231 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04002232 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002233 kfree(mp2);
dea31012005-04-17 16:05:31 -05002234 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2235 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002236 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002237 pring->missbufcnt = cnt;
2238 return cnt;
2239 }
2240
2241 INIT_LIST_HEAD(&mp2->list);
2242 } else {
2243 mp2 = NULL;
2244 }
2245
2246 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2247 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2248 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2249 icmd->ulpBdeCount = 1;
2250 cnt--;
2251 if (mp2) {
2252 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2253 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2254 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2255 cnt--;
2256 icmd->ulpBdeCount = 2;
2257 }
2258
2259 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2260 icmd->ulpLe = 1;
2261
James Smart3772a992009-05-22 14:50:54 -04002262 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2263 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05002264 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2265 kfree(mp1);
2266 cnt++;
2267 if (mp2) {
2268 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2269 kfree(mp2);
2270 cnt++;
2271 }
James Bottomley604a3e32005-10-29 10:28:33 -05002272 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002273 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05002274 return cnt;
2275 }
dea31012005-04-17 16:05:31 -05002276 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05002277 if (mp2)
dea31012005-04-17 16:05:31 -05002278 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05002279 }
2280 pring->missbufcnt = 0;
2281 return 0;
2282}
2283
James Smarte59058c2008-08-24 21:49:00 -04002284/**
James Smart3621a712009-04-06 18:47:14 -04002285 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04002286 * @phba: pointer to lpfc hba data structure.
2287 *
2288 * This routine posts initial receive IOCB buffers to the ELS ring. The
2289 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
2290 * set to 64 IOCBs.
2291 *
2292 * Return codes
2293 * 0 - success (currently always success)
2294 **/
dea31012005-04-17 16:05:31 -05002295static int
James Smart2e0fef82007-06-17 19:56:36 -05002296lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002297{
2298 struct lpfc_sli *psli = &phba->sli;
2299
2300 /* Ring 0, ELS / CT buffers */
James Smart495a7142008-06-14 22:52:59 -04002301 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05002302 /* Ring 2 - FCP no buffers needed */
2303
2304 return 0;
2305}
2306
2307#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2308
James Smarte59058c2008-08-24 21:49:00 -04002309/**
James Smart3621a712009-04-06 18:47:14 -04002310 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04002311 * @HashResultPointer: pointer to an array as hash table.
2312 *
2313 * This routine sets up the initial values to the array of hash table entries
2314 * for the LC HBAs.
2315 **/
dea31012005-04-17 16:05:31 -05002316static void
2317lpfc_sha_init(uint32_t * HashResultPointer)
2318{
2319 HashResultPointer[0] = 0x67452301;
2320 HashResultPointer[1] = 0xEFCDAB89;
2321 HashResultPointer[2] = 0x98BADCFE;
2322 HashResultPointer[3] = 0x10325476;
2323 HashResultPointer[4] = 0xC3D2E1F0;
2324}
2325
James Smarte59058c2008-08-24 21:49:00 -04002326/**
James Smart3621a712009-04-06 18:47:14 -04002327 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04002328 * @HashResultPointer: pointer to an initial/result hash table.
2329 * @HashWorkingPointer: pointer to an working hash table.
2330 *
2331 * This routine iterates an initial hash table pointed by @HashResultPointer
2332 * with the values from the working hash table pointeed by @HashWorkingPointer.
2333 * The results are putting back to the initial hash table, returned through
2334 * the @HashResultPointer as the result hash table.
2335 **/
dea31012005-04-17 16:05:31 -05002336static void
2337lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2338{
2339 int t;
2340 uint32_t TEMP;
2341 uint32_t A, B, C, D, E;
2342 t = 16;
2343 do {
2344 HashWorkingPointer[t] =
2345 S(1,
2346 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2347 8] ^
2348 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2349 } while (++t <= 79);
2350 t = 0;
2351 A = HashResultPointer[0];
2352 B = HashResultPointer[1];
2353 C = HashResultPointer[2];
2354 D = HashResultPointer[3];
2355 E = HashResultPointer[4];
2356
2357 do {
2358 if (t < 20) {
2359 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2360 } else if (t < 40) {
2361 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2362 } else if (t < 60) {
2363 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2364 } else {
2365 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2366 }
2367 TEMP += S(5, A) + E + HashWorkingPointer[t];
2368 E = D;
2369 D = C;
2370 C = S(30, B);
2371 B = A;
2372 A = TEMP;
2373 } while (++t <= 79);
2374
2375 HashResultPointer[0] += A;
2376 HashResultPointer[1] += B;
2377 HashResultPointer[2] += C;
2378 HashResultPointer[3] += D;
2379 HashResultPointer[4] += E;
2380
2381}
2382
James Smarte59058c2008-08-24 21:49:00 -04002383/**
James Smart3621a712009-04-06 18:47:14 -04002384 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002385 * @RandomChallenge: pointer to the entry of host challenge random number array.
2386 * @HashWorking: pointer to the entry of the working hash array.
2387 *
2388 * This routine calculates the working hash array referred by @HashWorking
2389 * from the challenge random numbers associated with the host, referred by
2390 * @RandomChallenge. The result is put into the entry of the working hash
2391 * array and returned by reference through @HashWorking.
2392 **/
dea31012005-04-17 16:05:31 -05002393static void
2394lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2395{
2396 *HashWorking = (*RandomChallenge ^ *HashWorking);
2397}
2398
James Smarte59058c2008-08-24 21:49:00 -04002399/**
James Smart3621a712009-04-06 18:47:14 -04002400 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002401 * @phba: pointer to lpfc hba data structure.
2402 * @hbainit: pointer to an array of unsigned 32-bit integers.
2403 *
2404 * This routine performs the special handling for LC HBA initialization.
2405 **/
dea31012005-04-17 16:05:31 -05002406void
2407lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2408{
2409 int t;
2410 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002411 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002412
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002413 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002414 if (!HashWorking)
2415 return;
2416
dea31012005-04-17 16:05:31 -05002417 HashWorking[0] = HashWorking[78] = *pwwnn++;
2418 HashWorking[1] = HashWorking[79] = *pwwnn;
2419
2420 for (t = 0; t < 7; t++)
2421 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2422
2423 lpfc_sha_init(hbainit);
2424 lpfc_sha_iterate(hbainit, HashWorking);
2425 kfree(HashWorking);
2426}
2427
James Smarte59058c2008-08-24 21:49:00 -04002428/**
James Smart3621a712009-04-06 18:47:14 -04002429 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002430 * @vport: pointer to a virtual N_Port data structure.
2431 *
2432 * This routine performs the necessary cleanups before deleting the @vport.
2433 * It invokes the discovery state machine to perform necessary state
2434 * transitions and to release the ndlps associated with the @vport. Note,
2435 * the physical port is treated as @vport 0.
2436 **/
James Smart87af33f2007-10-27 13:37:43 -04002437void
James Smart2e0fef82007-06-17 19:56:36 -05002438lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002439{
James Smart87af33f2007-10-27 13:37:43 -04002440 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002441 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002442 int i = 0;
dea31012005-04-17 16:05:31 -05002443
James Smart87af33f2007-10-27 13:37:43 -04002444 if (phba->link_state > LPFC_LINK_DOWN)
2445 lpfc_port_link_failure(vport);
2446
2447 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002448 if (!NLP_CHK_NODE_ACT(ndlp)) {
2449 ndlp = lpfc_enable_node(vport, ndlp,
2450 NLP_STE_UNUSED_NODE);
2451 if (!ndlp)
2452 continue;
2453 spin_lock_irq(&phba->ndlp_lock);
2454 NLP_SET_FREE_REQ(ndlp);
2455 spin_unlock_irq(&phba->ndlp_lock);
2456 /* Trigger the release of the ndlp memory */
2457 lpfc_nlp_put(ndlp);
2458 continue;
2459 }
2460 spin_lock_irq(&phba->ndlp_lock);
2461 if (NLP_CHK_FREE_REQ(ndlp)) {
2462 /* The ndlp should not be in memory free mode already */
2463 spin_unlock_irq(&phba->ndlp_lock);
2464 continue;
2465 } else
2466 /* Indicate request for freeing ndlp memory */
2467 NLP_SET_FREE_REQ(ndlp);
2468 spin_unlock_irq(&phba->ndlp_lock);
2469
James Smart58da1ff2008-04-07 10:15:56 -04002470 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2471 ndlp->nlp_DID == Fabric_DID) {
2472 /* Just free up ndlp with Fabric_DID for vports */
2473 lpfc_nlp_put(ndlp);
2474 continue;
2475 }
2476
James Smarteff4a012012-01-18 16:25:25 -05002477 /* take care of nodes in unused state before the state
2478 * machine taking action.
2479 */
2480 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
2481 lpfc_nlp_put(ndlp);
2482 continue;
2483 }
2484
James Smart87af33f2007-10-27 13:37:43 -04002485 if (ndlp->nlp_type & NLP_FABRIC)
2486 lpfc_disc_state_machine(vport, ndlp, NULL,
2487 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002488
James Smart87af33f2007-10-27 13:37:43 -04002489 lpfc_disc_state_machine(vport, ndlp, NULL,
2490 NLP_EVT_DEVICE_RM);
2491 }
2492
James Smarta8adb832007-10-27 13:37:53 -04002493 /* At this point, ALL ndlp's should be gone
2494 * because of the previous NLP_EVT_DEVICE_RM.
2495 * Lets wait for this to happen, if needed.
2496 */
James Smart87af33f2007-10-27 13:37:43 -04002497 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002498 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002499 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002500 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002501 list_for_each_entry_safe(ndlp, next_ndlp,
2502 &vport->fc_nodes, nlp_listp) {
2503 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2504 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002505 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002506 "usgmap:x%x refcnt:%d\n",
2507 ndlp->nlp_DID, (void *)ndlp,
2508 ndlp->nlp_usg_map,
2509 atomic_read(
2510 &ndlp->kref.refcount));
2511 }
James Smarta8adb832007-10-27 13:37:53 -04002512 break;
James Smart87af33f2007-10-27 13:37:43 -04002513 }
James Smarta8adb832007-10-27 13:37:53 -04002514
2515 /* Wait for any activity on ndlps to settle */
2516 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002517 }
James Smart1151e3e2011-02-16 12:39:35 -05002518 lpfc_cleanup_vports_rrqs(vport, NULL);
dea31012005-04-17 16:05:31 -05002519}
2520
James Smarte59058c2008-08-24 21:49:00 -04002521/**
James Smart3621a712009-04-06 18:47:14 -04002522 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002523 * @vport: pointer to a virtual N_Port data structure.
2524 *
2525 * This routine stops all the timers associated with a @vport. This function
2526 * is invoked before disabling or deleting a @vport. Note that the physical
2527 * port is treated as @vport 0.
2528 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002529void
2530lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002531{
James Smart92d7f7b2007-06-17 19:56:38 -05002532 del_timer_sync(&vport->els_tmofunc);
2533 del_timer_sync(&vport->fc_fdmitmo);
James Smart92494142011-02-16 12:39:44 -05002534 del_timer_sync(&vport->delayed_disc_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002535 lpfc_can_disctmo(vport);
2536 return;
dea31012005-04-17 16:05:31 -05002537}
2538
James Smarte59058c2008-08-24 21:49:00 -04002539/**
James Smartecfd03c2010-02-12 14:41:27 -05002540 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2541 * @phba: pointer to lpfc hba data structure.
2542 *
2543 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2544 * caller of this routine should already hold the host lock.
2545 **/
2546void
2547__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2548{
James Smart5ac6b302010-10-22 11:05:36 -04002549 /* Clear pending FCF rediscovery wait flag */
2550 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2551
James Smartecfd03c2010-02-12 14:41:27 -05002552 /* Now, try to stop the timer */
2553 del_timer(&phba->fcf.redisc_wait);
2554}
2555
2556/**
2557 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2558 * @phba: pointer to lpfc hba data structure.
2559 *
2560 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2561 * checks whether the FCF rediscovery wait timer is pending with the host
2562 * lock held before proceeding with disabling the timer and clearing the
2563 * wait timer pendig flag.
2564 **/
2565void
2566lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2567{
2568 spin_lock_irq(&phba->hbalock);
2569 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2570 /* FCF rediscovery timer already fired or stopped */
2571 spin_unlock_irq(&phba->hbalock);
2572 return;
2573 }
2574 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart5ac6b302010-10-22 11:05:36 -04002575 /* Clear failover in progress flags */
2576 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05002577 spin_unlock_irq(&phba->hbalock);
2578}
2579
2580/**
James Smart3772a992009-05-22 14:50:54 -04002581 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002582 * @phba: pointer to lpfc hba data structure.
2583 *
2584 * This routine stops all the timers associated with a HBA. This function is
2585 * invoked before either putting a HBA offline or unloading the driver.
2586 **/
James Smart3772a992009-05-22 14:50:54 -04002587void
2588lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002589{
James Smart51ef4c22007-08-02 11:10:31 -04002590 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05002591 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002592 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002593 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002594 del_timer_sync(&phba->hb_tmofunc);
James Smart1151e3e2011-02-16 12:39:35 -05002595 if (phba->sli_rev == LPFC_SLI_REV4) {
2596 del_timer_sync(&phba->rrq_tmr);
2597 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2598 }
James Smart3772a992009-05-22 14:50:54 -04002599 phba->hb_outstanding = 0;
2600
2601 switch (phba->pci_dev_grp) {
2602 case LPFC_PCI_DEV_LP:
2603 /* Stop any LightPulse device specific driver timers */
2604 del_timer_sync(&phba->fcp_poll_timer);
2605 break;
2606 case LPFC_PCI_DEV_OC:
2607 /* Stop any OneConnect device sepcific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05002608 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04002609 break;
2610 default:
2611 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2612 "0297 Invalid device group (x%x)\n",
2613 phba->pci_dev_grp);
2614 break;
2615 }
James Smart2e0fef82007-06-17 19:56:36 -05002616 return;
dea31012005-04-17 16:05:31 -05002617}
2618
James Smarte59058c2008-08-24 21:49:00 -04002619/**
James Smart3621a712009-04-06 18:47:14 -04002620 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002621 * @phba: pointer to lpfc hba data structure.
2622 *
2623 * This routine marks a HBA's management interface as blocked. Once the HBA's
2624 * management interface is marked as blocked, all the user space access to
2625 * the HBA, whether they are from sysfs interface or libdfc interface will
2626 * all be blocked. The HBA is set to block the management interface when the
2627 * driver prepares the HBA interface for online or offline.
2628 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002629static void
James Smart618a5232012-06-12 13:54:36 -04002630lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
Adrian Bunka6ababd2007-11-05 18:07:33 +01002631{
2632 unsigned long iflag;
James Smart6e7288d2010-06-07 15:23:35 -04002633 uint8_t actcmd = MBX_HEARTBEAT;
2634 unsigned long timeout;
2635
Adrian Bunka6ababd2007-11-05 18:07:33 +01002636 spin_lock_irqsave(&phba->hbalock, iflag);
2637 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
James Smart618a5232012-06-12 13:54:36 -04002638 spin_unlock_irqrestore(&phba->hbalock, iflag);
2639 if (mbx_action == LPFC_MBX_NO_WAIT)
2640 return;
2641 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
2642 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002643 if (phba->sli.mbox_active) {
James Smart6e7288d2010-06-07 15:23:35 -04002644 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
James Smarta183a152011-10-10 21:32:43 -04002645 /* Determine how long we might wait for the active mailbox
2646 * command to be gracefully completed by firmware.
2647 */
2648 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
2649 phba->sli.mbox_active) * 1000) + jiffies;
2650 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002651 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002652
James Smart6e7288d2010-06-07 15:23:35 -04002653 /* Wait for the outstnading mailbox command to complete */
2654 while (phba->sli.mbox_active) {
2655 /* Check active mailbox complete status every 2ms */
2656 msleep(2);
2657 if (time_after(jiffies, timeout)) {
2658 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2659 "2813 Mgmt IO is Blocked %x "
2660 "- mbox cmd %x still active\n",
2661 phba->sli.sli_flag, actcmd);
2662 break;
2663 }
2664 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002665}
2666
James Smarte59058c2008-08-24 21:49:00 -04002667/**
James Smart6b5151f2012-01-18 16:24:06 -05002668 * lpfc_sli4_node_prep - Assign RPIs for active nodes.
2669 * @phba: pointer to lpfc hba data structure.
2670 *
2671 * Allocate RPIs for all active remote nodes. This is needed whenever
2672 * an SLI4 adapter is reset and the driver is not unloading. Its purpose
2673 * is to fixup the temporary rpi assignments.
2674 **/
2675void
2676lpfc_sli4_node_prep(struct lpfc_hba *phba)
2677{
2678 struct lpfc_nodelist *ndlp, *next_ndlp;
2679 struct lpfc_vport **vports;
2680 int i;
2681
2682 if (phba->sli_rev != LPFC_SLI_REV4)
2683 return;
2684
2685 vports = lpfc_create_vport_work_array(phba);
2686 if (vports != NULL) {
2687 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2688 if (vports[i]->load_flag & FC_UNLOADING)
2689 continue;
2690
2691 list_for_each_entry_safe(ndlp, next_ndlp,
2692 &vports[i]->fc_nodes,
2693 nlp_listp) {
2694 if (NLP_CHK_NODE_ACT(ndlp))
2695 ndlp->nlp_rpi =
2696 lpfc_sli4_alloc_rpi(phba);
2697 }
2698 }
2699 }
2700 lpfc_destroy_vport_work_array(phba, vports);
2701}
2702
2703/**
James Smart3621a712009-04-06 18:47:14 -04002704 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04002705 * @phba: pointer to lpfc hba data structure.
2706 *
2707 * This routine initializes the HBA and brings a HBA online. During this
2708 * process, the management interface is blocked to prevent user space access
2709 * to the HBA interfering with the driver initialization.
2710 *
2711 * Return codes
2712 * 0 - successful
2713 * 1 - failed
2714 **/
dea31012005-04-17 16:05:31 -05002715int
James Smart2e0fef82007-06-17 19:56:36 -05002716lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002717{
Julia Lawall372bd282008-12-16 16:15:08 +01002718 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04002719 struct lpfc_vport **vports;
2720 int i;
James Smart16a3a202013-04-17 20:14:38 -04002721 bool vpis_cleared = false;
James Smart2e0fef82007-06-17 19:56:36 -05002722
dea31012005-04-17 16:05:31 -05002723 if (!phba)
2724 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01002725 vport = phba->pport;
dea31012005-04-17 16:05:31 -05002726
James Smart2e0fef82007-06-17 19:56:36 -05002727 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05002728 return 0;
2729
James Smarted957682007-06-17 19:56:37 -05002730 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002731 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05002732
James Smart618a5232012-06-12 13:54:36 -04002733 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05002734
James Smart46fa3112007-04-25 09:51:45 -04002735 if (!lpfc_sli_queue_setup(phba)) {
2736 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002737 return 1;
James Smart46fa3112007-04-25 09:51:45 -04002738 }
2739
James Smartda0436e2009-05-22 14:51:39 -04002740 if (phba->sli_rev == LPFC_SLI_REV4) {
2741 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2742 lpfc_unblock_mgmt_io(phba);
2743 return 1;
2744 }
James Smart16a3a202013-04-17 20:14:38 -04002745 spin_lock_irq(&phba->hbalock);
2746 if (!phba->sli4_hba.max_cfg_param.vpi_used)
2747 vpis_cleared = true;
2748 spin_unlock_irq(&phba->hbalock);
James Smartda0436e2009-05-22 14:51:39 -04002749 } else {
2750 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2751 lpfc_unblock_mgmt_io(phba);
2752 return 1;
2753 }
James Smart46fa3112007-04-25 09:51:45 -04002754 }
dea31012005-04-17 16:05:31 -05002755
James Smart549e55c2007-08-02 11:09:51 -04002756 vports = lpfc_create_vport_work_array(phba);
2757 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002758 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002759 struct Scsi_Host *shost;
2760 shost = lpfc_shost_from_vport(vports[i]);
2761 spin_lock_irq(shost->host_lock);
2762 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2763 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2764 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart16a3a202013-04-17 20:14:38 -04002765 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart1c6834a2009-07-19 10:01:26 -04002766 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart16a3a202013-04-17 20:14:38 -04002767 if ((vpis_cleared) &&
2768 (vports[i]->port_type !=
2769 LPFC_PHYSICAL_PORT))
2770 vports[i]->vpi = 0;
2771 }
James Smart549e55c2007-08-02 11:09:51 -04002772 spin_unlock_irq(shost->host_lock);
2773 }
James Smart09372822008-01-11 01:52:54 -05002774 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002775
James Smart46fa3112007-04-25 09:51:45 -04002776 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002777 return 0;
2778}
2779
James Smarte59058c2008-08-24 21:49:00 -04002780/**
James Smart3621a712009-04-06 18:47:14 -04002781 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04002782 * @phba: pointer to lpfc hba data structure.
2783 *
2784 * This routine marks a HBA's management interface as not blocked. Once the
2785 * HBA's management interface is marked as not blocked, all the user space
2786 * access to the HBA, whether they are from sysfs interface or libdfc
2787 * interface will be allowed. The HBA is set to block the management interface
2788 * when the driver prepares the HBA interface for online or offline and then
2789 * set to unblock the management interface afterwards.
2790 **/
James Smart46fa3112007-04-25 09:51:45 -04002791void
James Smart46fa3112007-04-25 09:51:45 -04002792lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2793{
2794 unsigned long iflag;
2795
James Smart2e0fef82007-06-17 19:56:36 -05002796 spin_lock_irqsave(&phba->hbalock, iflag);
2797 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2798 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04002799}
2800
James Smarte59058c2008-08-24 21:49:00 -04002801/**
James Smart3621a712009-04-06 18:47:14 -04002802 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04002803 * @phba: pointer to lpfc hba data structure.
2804 *
2805 * This routine is invoked to prepare a HBA to be brought offline. It performs
2806 * unregistration login to all the nodes on all vports and flushes the mailbox
2807 * queue to make it ready to be brought offline.
2808 **/
James Smart46fa3112007-04-25 09:51:45 -04002809void
James Smart618a5232012-06-12 13:54:36 -04002810lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
James Smart46fa3112007-04-25 09:51:45 -04002811{
James Smart2e0fef82007-06-17 19:56:36 -05002812 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04002813 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04002814 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05002815 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04002816 int i;
dea31012005-04-17 16:05:31 -05002817
James Smart2e0fef82007-06-17 19:56:36 -05002818 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002819 return;
dea31012005-04-17 16:05:31 -05002820
James Smart618a5232012-06-12 13:54:36 -04002821 lpfc_block_mgmt_io(phba, mbx_action);
dea31012005-04-17 16:05:31 -05002822
2823 lpfc_linkdown(phba);
2824
James Smart87af33f2007-10-27 13:37:43 -04002825 /* Issue an unreg_login to all nodes on all vports */
2826 vports = lpfc_create_vport_work_array(phba);
2827 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04002828 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04002829 if (vports[i]->load_flag & FC_UNLOADING)
2830 continue;
James Smart72100cc2010-02-12 14:43:01 -05002831 shost = lpfc_shost_from_vport(vports[i]);
2832 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002833 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002834 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2835 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05002836 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002837
James Smart87af33f2007-10-27 13:37:43 -04002838 shost = lpfc_shost_from_vport(vports[i]);
2839 list_for_each_entry_safe(ndlp, next_ndlp,
2840 &vports[i]->fc_nodes,
2841 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002842 if (!NLP_CHK_NODE_ACT(ndlp))
2843 continue;
James Smart87af33f2007-10-27 13:37:43 -04002844 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2845 continue;
2846 if (ndlp->nlp_type & NLP_FABRIC) {
2847 lpfc_disc_state_machine(vports[i], ndlp,
2848 NULL, NLP_EVT_DEVICE_RECOVERY);
2849 lpfc_disc_state_machine(vports[i], ndlp,
2850 NULL, NLP_EVT_DEVICE_RM);
2851 }
2852 spin_lock_irq(shost->host_lock);
2853 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart401ee0c2012-03-01 22:35:34 -05002854 spin_unlock_irq(shost->host_lock);
James Smart6b5151f2012-01-18 16:24:06 -05002855 /*
2856 * Whenever an SLI4 port goes offline, free the
James Smart401ee0c2012-03-01 22:35:34 -05002857 * RPI. Get a new RPI when the adapter port
2858 * comes back online.
James Smart6b5151f2012-01-18 16:24:06 -05002859 */
2860 if (phba->sli_rev == LPFC_SLI_REV4)
2861 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smart87af33f2007-10-27 13:37:43 -04002862 lpfc_unreg_rpi(vports[i], ndlp);
2863 }
2864 }
2865 }
James Smart09372822008-01-11 01:52:54 -05002866 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002867
James Smart618a5232012-06-12 13:54:36 -04002868 lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
James Smart46fa3112007-04-25 09:51:45 -04002869}
2870
James Smarte59058c2008-08-24 21:49:00 -04002871/**
James Smart3621a712009-04-06 18:47:14 -04002872 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04002873 * @phba: pointer to lpfc hba data structure.
2874 *
2875 * This routine actually brings a HBA offline. It stops all the timers
2876 * associated with the HBA, brings down the SLI layer, and eventually
2877 * marks the HBA as in offline state for the upper layer protocol.
2878 **/
James Smart46fa3112007-04-25 09:51:45 -04002879void
James Smart2e0fef82007-06-17 19:56:36 -05002880lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04002881{
James Smart549e55c2007-08-02 11:09:51 -04002882 struct Scsi_Host *shost;
2883 struct lpfc_vport **vports;
2884 int i;
James Smart46fa3112007-04-25 09:51:45 -04002885
James Smart549e55c2007-08-02 11:09:51 -04002886 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002887 return;
James Smart688a8862006-07-06 15:49:56 -04002888
James Smartda0436e2009-05-22 14:51:39 -04002889 /* stop port and all timers associated with this hba */
2890 lpfc_stop_port(phba);
James Smart51ef4c22007-08-02 11:10:31 -04002891 vports = lpfc_create_vport_work_array(phba);
2892 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002893 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04002894 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05002895 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05002896 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002897 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05002898 /* Bring down the SLI Layer and cleanup. The HBA is offline
2899 now. */
2900 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002901 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04002902 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002903 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04002904 vports = lpfc_create_vport_work_array(phba);
2905 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002906 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002907 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04002908 spin_lock_irq(shost->host_lock);
2909 vports[i]->work_port_events = 0;
2910 vports[i]->fc_flag |= FC_OFFLINE_MODE;
2911 spin_unlock_irq(shost->host_lock);
2912 }
James Smart09372822008-01-11 01:52:54 -05002913 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002914}
2915
James Smarte59058c2008-08-24 21:49:00 -04002916/**
James Smart3621a712009-04-06 18:47:14 -04002917 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04002918 * @phba: pointer to lpfc hba data structure.
2919 *
2920 * This routine is to free all the SCSI buffers and IOCBs from the driver
2921 * list back to kernel. It is called from lpfc_pci_remove_one to free
2922 * the internal resources before the device is removed from the system.
James Smarte59058c2008-08-24 21:49:00 -04002923 **/
James Smart8a9d2e82012-05-09 21:16:12 -04002924static void
James Smart2e0fef82007-06-17 19:56:36 -05002925lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002926{
2927 struct lpfc_scsi_buf *sb, *sb_next;
2928 struct lpfc_iocbq *io, *io_next;
2929
James Smart2e0fef82007-06-17 19:56:36 -05002930 spin_lock_irq(&phba->hbalock);
James Smarta40fc5f2013-04-17 20:17:40 -04002931
dea31012005-04-17 16:05:31 -05002932 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smarta40fc5f2013-04-17 20:17:40 -04002933
2934 spin_lock(&phba->scsi_buf_list_put_lock);
2935 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_put,
2936 list) {
dea31012005-04-17 16:05:31 -05002937 list_del(&sb->list);
2938 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05002939 sb->dma_handle);
dea31012005-04-17 16:05:31 -05002940 kfree(sb);
2941 phba->total_scsi_bufs--;
2942 }
James Smarta40fc5f2013-04-17 20:17:40 -04002943 spin_unlock(&phba->scsi_buf_list_put_lock);
2944
2945 spin_lock(&phba->scsi_buf_list_get_lock);
2946 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_get,
2947 list) {
2948 list_del(&sb->list);
2949 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
2950 sb->dma_handle);
2951 kfree(sb);
2952 phba->total_scsi_bufs--;
2953 }
2954 spin_unlock(&phba->scsi_buf_list_get_lock);
dea31012005-04-17 16:05:31 -05002955
2956 /* Release all the lpfc_iocbq entries maintained by this host. */
2957 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2958 list_del(&io->list);
2959 kfree(io);
2960 phba->total_iocbq_bufs--;
2961 }
James Smart6d368e52011-05-24 11:44:12 -04002962
James Smart2e0fef82007-06-17 19:56:36 -05002963 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04002964}
2965
2966/**
2967 * lpfc_sli4_xri_sgl_update - update xri-sgl sizing and mapping
2968 * @phba: pointer to lpfc hba data structure.
2969 *
2970 * This routine first calculates the sizes of the current els and allocated
2971 * scsi sgl lists, and then goes through all sgls to updates the physical
2972 * XRIs assigned due to port function reset. During port initialization, the
2973 * current els and allocated scsi sgl lists are 0s.
2974 *
2975 * Return codes
2976 * 0 - successful (for now, it always returns 0)
2977 **/
2978int
2979lpfc_sli4_xri_sgl_update(struct lpfc_hba *phba)
2980{
2981 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
2982 struct lpfc_scsi_buf *psb = NULL, *psb_next = NULL;
2983 uint16_t i, lxri, xri_cnt, els_xri_cnt, scsi_xri_cnt;
2984 LIST_HEAD(els_sgl_list);
2985 LIST_HEAD(scsi_sgl_list);
2986 int rc;
2987
2988 /*
2989 * update on pci function's els xri-sgl list
2990 */
2991 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
2992 if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
2993 /* els xri-sgl expanded */
2994 xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
2995 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2996 "3157 ELS xri-sgl count increased from "
2997 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
2998 els_xri_cnt);
2999 /* allocate the additional els sgls */
3000 for (i = 0; i < xri_cnt; i++) {
3001 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3002 GFP_KERNEL);
3003 if (sglq_entry == NULL) {
3004 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3005 "2562 Failure to allocate an "
3006 "ELS sgl entry:%d\n", i);
3007 rc = -ENOMEM;
3008 goto out_free_mem;
3009 }
3010 sglq_entry->buff_type = GEN_BUFF_TYPE;
3011 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
3012 &sglq_entry->phys);
3013 if (sglq_entry->virt == NULL) {
3014 kfree(sglq_entry);
3015 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3016 "2563 Failure to allocate an "
3017 "ELS mbuf:%d\n", i);
3018 rc = -ENOMEM;
3019 goto out_free_mem;
3020 }
3021 sglq_entry->sgl = sglq_entry->virt;
3022 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
3023 sglq_entry->state = SGL_FREED;
3024 list_add_tail(&sglq_entry->list, &els_sgl_list);
3025 }
James Smart38c20672013-03-01 16:37:44 -05003026 spin_lock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003027 list_splice_init(&els_sgl_list, &phba->sli4_hba.lpfc_sgl_list);
James Smart38c20672013-03-01 16:37:44 -05003028 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003029 } else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
3030 /* els xri-sgl shrinked */
3031 xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
3032 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3033 "3158 ELS xri-sgl count decreased from "
3034 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3035 els_xri_cnt);
3036 spin_lock_irq(&phba->hbalock);
3037 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &els_sgl_list);
3038 spin_unlock_irq(&phba->hbalock);
3039 /* release extra els sgls from list */
3040 for (i = 0; i < xri_cnt; i++) {
3041 list_remove_head(&els_sgl_list,
3042 sglq_entry, struct lpfc_sglq, list);
3043 if (sglq_entry) {
3044 lpfc_mbuf_free(phba, sglq_entry->virt,
3045 sglq_entry->phys);
3046 kfree(sglq_entry);
3047 }
3048 }
3049 spin_lock_irq(&phba->hbalock);
3050 list_splice_init(&els_sgl_list, &phba->sli4_hba.lpfc_sgl_list);
3051 spin_unlock_irq(&phba->hbalock);
3052 } else
3053 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3054 "3163 ELS xri-sgl count unchanged: %d\n",
3055 els_xri_cnt);
3056 phba->sli4_hba.els_xri_cnt = els_xri_cnt;
3057
3058 /* update xris to els sgls on the list */
3059 sglq_entry = NULL;
3060 sglq_entry_next = NULL;
3061 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
3062 &phba->sli4_hba.lpfc_sgl_list, list) {
3063 lxri = lpfc_sli4_next_xritag(phba);
3064 if (lxri == NO_XRI) {
3065 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3066 "2400 Failed to allocate xri for "
3067 "ELS sgl\n");
3068 rc = -ENOMEM;
3069 goto out_free_mem;
3070 }
3071 sglq_entry->sli4_lxritag = lxri;
3072 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3073 }
3074
3075 /*
3076 * update on pci function's allocated scsi xri-sgl list
3077 */
3078 phba->total_scsi_bufs = 0;
3079
3080 /* maximum number of xris available for scsi buffers */
3081 phba->sli4_hba.scsi_xri_max = phba->sli4_hba.max_cfg_param.max_xri -
3082 els_xri_cnt;
3083
3084 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3085 "2401 Current allocated SCSI xri-sgl count:%d, "
3086 "maximum SCSI xri count:%d\n",
3087 phba->sli4_hba.scsi_xri_cnt,
3088 phba->sli4_hba.scsi_xri_max);
3089
James Smarta40fc5f2013-04-17 20:17:40 -04003090 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart164cecd2013-09-06 12:22:38 -04003091 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003092 list_splice_init(&phba->lpfc_scsi_buf_list_get, &scsi_sgl_list);
3093 list_splice(&phba->lpfc_scsi_buf_list_put, &scsi_sgl_list);
James Smart164cecd2013-09-06 12:22:38 -04003094 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003095 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003096
3097 if (phba->sli4_hba.scsi_xri_cnt > phba->sli4_hba.scsi_xri_max) {
3098 /* max scsi xri shrinked below the allocated scsi buffers */
3099 scsi_xri_cnt = phba->sli4_hba.scsi_xri_cnt -
3100 phba->sli4_hba.scsi_xri_max;
3101 /* release the extra allocated scsi buffers */
3102 for (i = 0; i < scsi_xri_cnt; i++) {
3103 list_remove_head(&scsi_sgl_list, psb,
3104 struct lpfc_scsi_buf, list);
3105 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, psb->data,
3106 psb->dma_handle);
3107 kfree(psb);
3108 }
James Smarta40fc5f2013-04-17 20:17:40 -04003109 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003110 phba->sli4_hba.scsi_xri_cnt -= scsi_xri_cnt;
James Smarta40fc5f2013-04-17 20:17:40 -04003111 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003112 }
3113
3114 /* update xris associated to remaining allocated scsi buffers */
3115 psb = NULL;
3116 psb_next = NULL;
3117 list_for_each_entry_safe(psb, psb_next, &scsi_sgl_list, list) {
3118 lxri = lpfc_sli4_next_xritag(phba);
3119 if (lxri == NO_XRI) {
3120 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3121 "2560 Failed to allocate xri for "
3122 "scsi buffer\n");
3123 rc = -ENOMEM;
3124 goto out_free_mem;
3125 }
3126 psb->cur_iocbq.sli4_lxritag = lxri;
3127 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3128 }
James Smarta40fc5f2013-04-17 20:17:40 -04003129 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart164cecd2013-09-06 12:22:38 -04003130 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003131 list_splice_init(&scsi_sgl_list, &phba->lpfc_scsi_buf_list_get);
3132 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
James Smart164cecd2013-09-06 12:22:38 -04003133 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003134 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003135
dea31012005-04-17 16:05:31 -05003136 return 0;
James Smart8a9d2e82012-05-09 21:16:12 -04003137
3138out_free_mem:
3139 lpfc_free_els_sgl_list(phba);
3140 lpfc_scsi_free(phba);
3141 return rc;
dea31012005-04-17 16:05:31 -05003142}
3143
James Smarte59058c2008-08-24 21:49:00 -04003144/**
James Smart3621a712009-04-06 18:47:14 -04003145 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04003146 * @phba: pointer to lpfc hba data structure.
3147 * @instance: a unique integer ID to this FC port.
3148 * @dev: pointer to the device data structure.
3149 *
3150 * This routine creates a FC port for the upper layer protocol. The FC port
3151 * can be created on top of either a physical port or a virtual port provided
3152 * by the HBA. This routine also allocates a SCSI host data structure (shost)
3153 * and associates the FC port created before adding the shost into the SCSI
3154 * layer.
3155 *
3156 * Return codes
3157 * @vport - pointer to the virtual N_Port data structure.
3158 * NULL - port create failed.
3159 **/
James Smart2e0fef82007-06-17 19:56:36 -05003160struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04003161lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04003162{
James Smart2e0fef82007-06-17 19:56:36 -05003163 struct lpfc_vport *vport;
3164 struct Scsi_Host *shost;
3165 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04003166
James Smart3de2a652007-08-02 11:09:59 -04003167 if (dev != &phba->pcidev->dev)
3168 shost = scsi_host_alloc(&lpfc_vport_template,
3169 sizeof(struct lpfc_vport));
3170 else
3171 shost = scsi_host_alloc(&lpfc_template,
3172 sizeof(struct lpfc_vport));
James Smart2e0fef82007-06-17 19:56:36 -05003173 if (!shost)
3174 goto out;
James Smart47a86172007-04-25 09:53:22 -04003175
James Smart2e0fef82007-06-17 19:56:36 -05003176 vport = (struct lpfc_vport *) shost->hostdata;
3177 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05003178 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05003179 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05003180 vport->fc_rscn_flush = 0;
James Smart47a86172007-04-25 09:53:22 -04003181
James Smart3de2a652007-08-02 11:09:59 -04003182 lpfc_get_vport_cfgparam(vport);
James Smart2e0fef82007-06-17 19:56:36 -05003183 shost->unique_id = instance;
3184 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04003185 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05003186 shost->this_id = -1;
3187 shost->max_cmd_len = 16;
James Smartda0436e2009-05-22 14:51:39 -04003188 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart28baac72010-02-12 14:42:03 -05003189 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04003190 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smartda0436e2009-05-22 14:51:39 -04003191 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
3192 }
James Smart81301a92008-12-04 22:39:46 -05003193
James Smart47a86172007-04-25 09:53:22 -04003194 /*
James Smart2e0fef82007-06-17 19:56:36 -05003195 * Set initial can_queue value since 0 is no longer supported and
3196 * scsi_add_host will fail. This will be adjusted later based on the
3197 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04003198 */
James Smart2e0fef82007-06-17 19:56:36 -05003199 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04003200 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05003201 shost->transportt = lpfc_vport_transport_template;
3202 vport->port_type = LPFC_NPIV_PORT;
3203 } else {
3204 shost->transportt = lpfc_transport_template;
3205 vport->port_type = LPFC_PHYSICAL_PORT;
3206 }
James Smart47a86172007-04-25 09:53:22 -04003207
James Smart2e0fef82007-06-17 19:56:36 -05003208 /* Initialize all internally managed lists. */
3209 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04003210 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05003211 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04003212
James Smart2e0fef82007-06-17 19:56:36 -05003213 init_timer(&vport->fc_disctmo);
3214 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05003215 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04003216
James Smart2e0fef82007-06-17 19:56:36 -05003217 init_timer(&vport->fc_fdmitmo);
3218 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
James Smart92d7f7b2007-06-17 19:56:38 -05003219 vport->fc_fdmitmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04003220
James Smart2e0fef82007-06-17 19:56:36 -05003221 init_timer(&vport->els_tmofunc);
3222 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05003223 vport->els_tmofunc.data = (unsigned long)vport;
James Smart92494142011-02-16 12:39:44 -05003224
3225 init_timer(&vport->delayed_disc_tmo);
3226 vport->delayed_disc_tmo.function = lpfc_delayed_disc_tmo;
3227 vport->delayed_disc_tmo.data = (unsigned long)vport;
3228
James Bottomleyd139b9b2009-11-05 13:33:12 -06003229 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05003230 if (error)
3231 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04003232
James Smart549e55c2007-08-02 11:09:51 -04003233 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003234 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04003235 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003236 return vport;
James Smart47a86172007-04-25 09:53:22 -04003237
James Smart2e0fef82007-06-17 19:56:36 -05003238out_put_shost:
3239 scsi_host_put(shost);
3240out:
3241 return NULL;
James Smart47a86172007-04-25 09:53:22 -04003242}
3243
James Smarte59058c2008-08-24 21:49:00 -04003244/**
James Smart3621a712009-04-06 18:47:14 -04003245 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04003246 * @vport: pointer to an lpfc virtual N_Port data structure.
3247 *
3248 * This routine destroys a FC port from the upper layer protocol. All the
3249 * resources associated with the port are released.
3250 **/
James Smart2e0fef82007-06-17 19:56:36 -05003251void
3252destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04003253{
James Smart92d7f7b2007-06-17 19:56:38 -05003254 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3255 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003256
James Smart858c9f62007-06-17 19:56:39 -05003257 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05003258 fc_remove_host(shost);
3259 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04003260
James Smart92d7f7b2007-06-17 19:56:38 -05003261 spin_lock_irq(&phba->hbalock);
3262 list_del_init(&vport->listentry);
3263 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04003264
James Smart92d7f7b2007-06-17 19:56:38 -05003265 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04003266 return;
James Smart47a86172007-04-25 09:53:22 -04003267}
3268
James Smarte59058c2008-08-24 21:49:00 -04003269/**
James Smart3621a712009-04-06 18:47:14 -04003270 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04003271 *
3272 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
3273 * uses the kernel idr facility to perform the task.
3274 *
3275 * Return codes:
3276 * instance - a unique integer ID allocated as the new instance.
3277 * -1 - lpfc get instance failed.
3278 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003279int
3280lpfc_get_instance(void)
3281{
Tejun Heoab516032013-02-27 17:04:44 -08003282 int ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003283
Tejun Heoab516032013-02-27 17:04:44 -08003284 ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
3285 return ret < 0 ? -1 : ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003286}
3287
James Smarte59058c2008-08-24 21:49:00 -04003288/**
James Smart3621a712009-04-06 18:47:14 -04003289 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04003290 * @shost: pointer to SCSI host data structure.
3291 * @time: elapsed time of the scan in jiffies.
3292 *
3293 * This routine is called by the SCSI layer with a SCSI host to determine
3294 * whether the scan host is finished.
3295 *
3296 * Note: there is no scan_start function as adapter initialization will have
3297 * asynchronously kicked off the link initialization.
3298 *
3299 * Return codes
3300 * 0 - SCSI host scan is not over yet.
3301 * 1 - SCSI host scan is over.
3302 **/
James Smart47a86172007-04-25 09:53:22 -04003303int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
3304{
James Smart2e0fef82007-06-17 19:56:36 -05003305 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3306 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05003307 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04003308
James Smart858c9f62007-06-17 19:56:39 -05003309 spin_lock_irq(shost->host_lock);
3310
James Smart51ef4c22007-08-02 11:10:31 -04003311 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05003312 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003313 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05003314 }
James Smart256ec0d2013-04-17 20:14:58 -04003315 if (time >= msecs_to_jiffies(30 * 1000)) {
James Smart2e0fef82007-06-17 19:56:36 -05003316 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003317 "0461 Scanning longer than 30 "
3318 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003319 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003320 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003321 }
James Smart256ec0d2013-04-17 20:14:58 -04003322 if (time >= msecs_to_jiffies(15 * 1000) &&
3323 phba->link_state <= LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -05003324 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003325 "0465 Link down longer than 15 "
3326 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003327 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05003328 goto finished;
James Smart47a86172007-04-25 09:53:22 -04003329 }
3330
James Smart2e0fef82007-06-17 19:56:36 -05003331 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05003332 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003333 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05003334 goto finished;
James Smart256ec0d2013-04-17 20:14:58 -04003335 if (vport->fc_map_cnt == 0 && time < msecs_to_jiffies(2 * 1000))
James Smart858c9f62007-06-17 19:56:39 -05003336 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003337 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05003338 goto finished;
3339
3340 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003341
3342finished:
James Smart858c9f62007-06-17 19:56:39 -05003343 spin_unlock_irq(shost->host_lock);
3344 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05003345}
3346
James Smarte59058c2008-08-24 21:49:00 -04003347/**
James Smart3621a712009-04-06 18:47:14 -04003348 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04003349 * @shost: pointer to SCSI host data structure.
3350 *
3351 * This routine initializes a given SCSI host attributes on a FC port. The
3352 * SCSI host can be either on top of a physical port or a virtual port.
3353 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003354void lpfc_host_attrib_init(struct Scsi_Host *shost)
3355{
3356 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3357 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003358 /*
James Smart2e0fef82007-06-17 19:56:36 -05003359 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04003360 */
3361
James Smart2e0fef82007-06-17 19:56:36 -05003362 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
3363 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04003364 fc_host_supported_classes(shost) = FC_COS_CLASS3;
3365
3366 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003367 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003368 fc_host_supported_fc4s(shost)[2] = 1;
3369 fc_host_supported_fc4s(shost)[7] = 1;
3370
James Smart92d7f7b2007-06-17 19:56:38 -05003371 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
3372 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04003373
3374 fc_host_supported_speeds(shost) = 0;
James Smart88a2cfb2011-07-22 18:36:33 -04003375 if (phba->lmt & LMT_16Gb)
3376 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
James Smart47a86172007-04-25 09:53:22 -04003377 if (phba->lmt & LMT_10Gb)
3378 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04003379 if (phba->lmt & LMT_8Gb)
3380 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04003381 if (phba->lmt & LMT_4Gb)
3382 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
3383 if (phba->lmt & LMT_2Gb)
3384 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
3385 if (phba->lmt & LMT_1Gb)
3386 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
3387
3388 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05003389 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
3390 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04003391
Mike Christie0af5d702010-09-15 16:52:31 -05003392 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
3393
James Smart47a86172007-04-25 09:53:22 -04003394 /* This value is also unchanging */
3395 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003396 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003397 fc_host_active_fc4s(shost)[2] = 1;
3398 fc_host_active_fc4s(shost)[7] = 1;
3399
James Smart92d7f7b2007-06-17 19:56:38 -05003400 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04003401 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04003402 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04003403 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04003404}
dea31012005-04-17 16:05:31 -05003405
James Smarte59058c2008-08-24 21:49:00 -04003406/**
James Smartda0436e2009-05-22 14:51:39 -04003407 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04003408 * @phba: pointer to lpfc hba data structure.
3409 *
James Smartda0436e2009-05-22 14:51:39 -04003410 * This routine is invoked to stop an SLI3 device port, it stops the device
3411 * from generating interrupts and stops the device driver's timers for the
3412 * device.
James Smarte59058c2008-08-24 21:49:00 -04003413 **/
James Smartdb2378e2008-02-08 18:49:51 -05003414static void
James Smartda0436e2009-05-22 14:51:39 -04003415lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05003416{
James Smartda0436e2009-05-22 14:51:39 -04003417 /* Clear all interrupt enable conditions */
3418 writel(0, phba->HCregaddr);
3419 readl(phba->HCregaddr); /* flush */
3420 /* Clear all pending interrupts */
3421 writel(0xffffffff, phba->HAregaddr);
3422 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04003423
James Smartda0436e2009-05-22 14:51:39 -04003424 /* Reset some HBA SLI setup states */
3425 lpfc_stop_hba_timers(phba);
3426 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05003427}
3428
James Smarte59058c2008-08-24 21:49:00 -04003429/**
James Smartda0436e2009-05-22 14:51:39 -04003430 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05003431 * @phba: pointer to lpfc hba data structure.
3432 *
James Smartda0436e2009-05-22 14:51:39 -04003433 * This routine is invoked to stop an SLI4 device port, it stops the device
3434 * from generating interrupts and stops the device driver's timers for the
3435 * device.
3436 **/
3437static void
3438lpfc_stop_port_s4(struct lpfc_hba *phba)
3439{
3440 /* Reset some HBA SLI4 setup states */
3441 lpfc_stop_hba_timers(phba);
3442 phba->pport->work_port_events = 0;
3443 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04003444}
3445
3446/**
3447 * lpfc_stop_port - Wrapper function for stopping hba port
3448 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05003449 *
James Smartda0436e2009-05-22 14:51:39 -04003450 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
3451 * the API jump table function pointer from the lpfc_hba struct.
3452 **/
3453void
3454lpfc_stop_port(struct lpfc_hba *phba)
3455{
3456 phba->lpfc_stop_port(phba);
3457}
3458
3459/**
James Smartecfd03c2010-02-12 14:41:27 -05003460 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
3461 * @phba: Pointer to hba for which this call is being executed.
3462 *
3463 * This routine starts the timer waiting for the FCF rediscovery to complete.
3464 **/
3465void
3466lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
3467{
3468 unsigned long fcf_redisc_wait_tmo =
3469 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
3470 /* Start fcf rediscovery wait period timer */
3471 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
3472 spin_lock_irq(&phba->hbalock);
3473 /* Allow action to new fcf asynchronous event */
3474 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
3475 /* Mark the FCF rediscovery pending state */
3476 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
3477 spin_unlock_irq(&phba->hbalock);
3478}
3479
3480/**
3481 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
3482 * @ptr: Map to lpfc_hba data structure pointer.
3483 *
3484 * This routine is invoked when waiting for FCF table rediscover has been
3485 * timed out. If new FCF record(s) has (have) been discovered during the
3486 * wait period, a new FCF event shall be added to the FCOE async event
3487 * list, and then worker thread shall be waked up for processing from the
3488 * worker thread context.
3489 **/
3490void
3491lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
3492{
3493 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
3494
3495 /* Don't send FCF rediscovery event if timer cancelled */
3496 spin_lock_irq(&phba->hbalock);
3497 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
3498 spin_unlock_irq(&phba->hbalock);
3499 return;
3500 }
3501 /* Clear FCF rediscovery timer pending flag */
3502 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
3503 /* FCF rediscovery event to worker thread */
3504 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
3505 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003506 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04003507 "2776 FCF rediscover quiescent timer expired\n");
James Smartecfd03c2010-02-12 14:41:27 -05003508 /* wake up worker thread */
3509 lpfc_worker_wake_up(phba);
3510}
3511
3512/**
James Smartda0436e2009-05-22 14:51:39 -04003513 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
3514 * @phba: pointer to lpfc hba data structure.
3515 * @acqe_link: pointer to the async link completion queue entry.
3516 *
3517 * This routine is to parse the SLI4 link-attention link fault code and
3518 * translate it into the base driver's read link attention mailbox command
3519 * status.
3520 *
3521 * Return: Link-attention status in terms of base driver's coding.
3522 **/
3523static uint16_t
3524lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
3525 struct lpfc_acqe_link *acqe_link)
3526{
3527 uint16_t latt_fault;
3528
3529 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
3530 case LPFC_ASYNC_LINK_FAULT_NONE:
3531 case LPFC_ASYNC_LINK_FAULT_LOCAL:
3532 case LPFC_ASYNC_LINK_FAULT_REMOTE:
3533 latt_fault = 0;
3534 break;
3535 default:
3536 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3537 "0398 Invalid link fault code: x%x\n",
3538 bf_get(lpfc_acqe_link_fault, acqe_link));
3539 latt_fault = MBXERR_ERROR;
3540 break;
3541 }
3542 return latt_fault;
3543}
3544
3545/**
3546 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
3547 * @phba: pointer to lpfc hba data structure.
3548 * @acqe_link: pointer to the async link completion queue entry.
3549 *
3550 * This routine is to parse the SLI4 link attention type and translate it
3551 * into the base driver's link attention type coding.
3552 *
3553 * Return: Link attention type in terms of base driver's coding.
3554 **/
3555static uint8_t
3556lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
3557 struct lpfc_acqe_link *acqe_link)
3558{
3559 uint8_t att_type;
3560
3561 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
3562 case LPFC_ASYNC_LINK_STATUS_DOWN:
3563 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
James Smart76a95d72010-11-20 23:11:48 -05003564 att_type = LPFC_ATT_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04003565 break;
3566 case LPFC_ASYNC_LINK_STATUS_UP:
3567 /* Ignore physical link up events - wait for logical link up */
James Smart76a95d72010-11-20 23:11:48 -05003568 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003569 break;
3570 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
James Smart76a95d72010-11-20 23:11:48 -05003571 att_type = LPFC_ATT_LINK_UP;
James Smartda0436e2009-05-22 14:51:39 -04003572 break;
3573 default:
3574 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3575 "0399 Invalid link attention type: x%x\n",
3576 bf_get(lpfc_acqe_link_status, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003577 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003578 break;
3579 }
3580 return att_type;
3581}
3582
3583/**
3584 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
3585 * @phba: pointer to lpfc hba data structure.
3586 * @acqe_link: pointer to the async link completion queue entry.
3587 *
3588 * This routine is to parse the SLI4 link-attention link speed and translate
3589 * it into the base driver's link-attention link speed coding.
3590 *
3591 * Return: Link-attention link speed in terms of base driver's coding.
3592 **/
3593static uint8_t
3594lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
3595 struct lpfc_acqe_link *acqe_link)
3596{
3597 uint8_t link_speed;
3598
3599 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
3600 case LPFC_ASYNC_LINK_SPEED_ZERO:
James Smartda0436e2009-05-22 14:51:39 -04003601 case LPFC_ASYNC_LINK_SPEED_10MBPS:
James Smartda0436e2009-05-22 14:51:39 -04003602 case LPFC_ASYNC_LINK_SPEED_100MBPS:
James Smart76a95d72010-11-20 23:11:48 -05003603 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003604 break;
3605 case LPFC_ASYNC_LINK_SPEED_1GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003606 link_speed = LPFC_LINK_SPEED_1GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003607 break;
3608 case LPFC_ASYNC_LINK_SPEED_10GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003609 link_speed = LPFC_LINK_SPEED_10GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003610 break;
3611 default:
3612 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3613 "0483 Invalid link-attention link speed: x%x\n",
3614 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003615 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003616 break;
3617 }
3618 return link_speed;
3619}
3620
3621/**
James Smart8b68cd52012-09-29 11:32:37 -04003622 * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
3623 * @phba: pointer to lpfc hba data structure.
3624 *
3625 * This routine is to get an SLI3 FC port's link speed in Mbps.
3626 *
3627 * Return: link speed in terms of Mbps.
3628 **/
3629uint32_t
3630lpfc_sli_port_speed_get(struct lpfc_hba *phba)
3631{
3632 uint32_t link_speed;
3633
3634 if (!lpfc_is_link_up(phba))
3635 return 0;
3636
3637 switch (phba->fc_linkspeed) {
3638 case LPFC_LINK_SPEED_1GHZ:
3639 link_speed = 1000;
3640 break;
3641 case LPFC_LINK_SPEED_2GHZ:
3642 link_speed = 2000;
3643 break;
3644 case LPFC_LINK_SPEED_4GHZ:
3645 link_speed = 4000;
3646 break;
3647 case LPFC_LINK_SPEED_8GHZ:
3648 link_speed = 8000;
3649 break;
3650 case LPFC_LINK_SPEED_10GHZ:
3651 link_speed = 10000;
3652 break;
3653 case LPFC_LINK_SPEED_16GHZ:
3654 link_speed = 16000;
3655 break;
3656 default:
3657 link_speed = 0;
3658 }
3659 return link_speed;
3660}
3661
3662/**
3663 * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
3664 * @phba: pointer to lpfc hba data structure.
3665 * @evt_code: asynchronous event code.
3666 * @speed_code: asynchronous event link speed code.
3667 *
3668 * This routine is to parse the giving SLI4 async event link speed code into
3669 * value of Mbps for the link speed.
3670 *
3671 * Return: link speed in terms of Mbps.
3672 **/
3673static uint32_t
3674lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
3675 uint8_t speed_code)
3676{
3677 uint32_t port_speed;
3678
3679 switch (evt_code) {
3680 case LPFC_TRAILER_CODE_LINK:
3681 switch (speed_code) {
3682 case LPFC_EVT_CODE_LINK_NO_LINK:
3683 port_speed = 0;
3684 break;
3685 case LPFC_EVT_CODE_LINK_10_MBIT:
3686 port_speed = 10;
3687 break;
3688 case LPFC_EVT_CODE_LINK_100_MBIT:
3689 port_speed = 100;
3690 break;
3691 case LPFC_EVT_CODE_LINK_1_GBIT:
3692 port_speed = 1000;
3693 break;
3694 case LPFC_EVT_CODE_LINK_10_GBIT:
3695 port_speed = 10000;
3696 break;
3697 default:
3698 port_speed = 0;
3699 }
3700 break;
3701 case LPFC_TRAILER_CODE_FC:
3702 switch (speed_code) {
3703 case LPFC_EVT_CODE_FC_NO_LINK:
3704 port_speed = 0;
3705 break;
3706 case LPFC_EVT_CODE_FC_1_GBAUD:
3707 port_speed = 1000;
3708 break;
3709 case LPFC_EVT_CODE_FC_2_GBAUD:
3710 port_speed = 2000;
3711 break;
3712 case LPFC_EVT_CODE_FC_4_GBAUD:
3713 port_speed = 4000;
3714 break;
3715 case LPFC_EVT_CODE_FC_8_GBAUD:
3716 port_speed = 8000;
3717 break;
3718 case LPFC_EVT_CODE_FC_10_GBAUD:
3719 port_speed = 10000;
3720 break;
3721 case LPFC_EVT_CODE_FC_16_GBAUD:
3722 port_speed = 16000;
3723 break;
3724 default:
3725 port_speed = 0;
3726 }
3727 break;
3728 default:
3729 port_speed = 0;
3730 }
3731 return port_speed;
3732}
3733
3734/**
James Smart70f3c072010-12-15 17:57:33 -05003735 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
James Smartda0436e2009-05-22 14:51:39 -04003736 * @phba: pointer to lpfc hba data structure.
3737 * @acqe_link: pointer to the async link completion queue entry.
3738 *
James Smart70f3c072010-12-15 17:57:33 -05003739 * This routine is to handle the SLI4 asynchronous FCoE link event.
James Smartda0436e2009-05-22 14:51:39 -04003740 **/
3741static void
3742lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3743 struct lpfc_acqe_link *acqe_link)
3744{
3745 struct lpfc_dmabuf *mp;
3746 LPFC_MBOXQ_t *pmb;
3747 MAILBOX_t *mb;
James Smart76a95d72010-11-20 23:11:48 -05003748 struct lpfc_mbx_read_top *la;
James Smartda0436e2009-05-22 14:51:39 -04003749 uint8_t att_type;
James Smart76a95d72010-11-20 23:11:48 -05003750 int rc;
James Smartda0436e2009-05-22 14:51:39 -04003751
3752 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05003753 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
James Smartda0436e2009-05-22 14:51:39 -04003754 return;
James Smart32b97932009-07-19 10:01:21 -04003755 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003756 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3757 if (!pmb) {
3758 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3759 "0395 The mboxq allocation failed\n");
3760 return;
3761 }
3762 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3763 if (!mp) {
3764 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3765 "0396 The lpfc_dmabuf allocation failed\n");
3766 goto out_free_pmb;
3767 }
3768 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3769 if (!mp->virt) {
3770 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3771 "0397 The mbuf allocation failed\n");
3772 goto out_free_dmabuf;
3773 }
3774
3775 /* Cleanup any outstanding ELS commands */
3776 lpfc_els_flush_all_cmd(phba);
3777
3778 /* Block ELS IOCBs until we have done process link event */
3779 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3780
3781 /* Update link event statistics */
3782 phba->sli.slistat.link_event++;
3783
James Smart76a95d72010-11-20 23:11:48 -05003784 /* Create lpfc_handle_latt mailbox command from link ACQE */
3785 lpfc_read_topology(phba, pmb, mp);
3786 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smartda0436e2009-05-22 14:51:39 -04003787 pmb->vport = phba->pport;
3788
James Smartda0436e2009-05-22 14:51:39 -04003789 /* Keep the link status for extra SLI4 state machine reference */
3790 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04003791 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
3792 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smartda0436e2009-05-22 14:51:39 -04003793 phba->sli4_hba.link_state.duplex =
3794 bf_get(lpfc_acqe_link_duplex, acqe_link);
3795 phba->sli4_hba.link_state.status =
3796 bf_get(lpfc_acqe_link_status, acqe_link);
James Smart70f3c072010-12-15 17:57:33 -05003797 phba->sli4_hba.link_state.type =
3798 bf_get(lpfc_acqe_link_type, acqe_link);
3799 phba->sli4_hba.link_state.number =
3800 bf_get(lpfc_acqe_link_number, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04003801 phba->sli4_hba.link_state.fault =
3802 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05003803 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04003804 bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
3805
James Smart70f3c072010-12-15 17:57:33 -05003806 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartc31098c2011-04-16 11:03:33 -04003807 "2900 Async FC/FCoE Link event - Speed:%dGBit "
3808 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
3809 "Logical speed:%dMbps Fault:%d\n",
James Smart70f3c072010-12-15 17:57:33 -05003810 phba->sli4_hba.link_state.speed,
3811 phba->sli4_hba.link_state.topology,
3812 phba->sli4_hba.link_state.status,
3813 phba->sli4_hba.link_state.type,
3814 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04003815 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05003816 phba->sli4_hba.link_state.fault);
James Smart76a95d72010-11-20 23:11:48 -05003817 /*
3818 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
3819 * topology info. Note: Optional for non FC-AL ports.
3820 */
3821 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3822 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3823 if (rc == MBX_NOT_FINISHED)
3824 goto out_free_dmabuf;
3825 return;
3826 }
3827 /*
3828 * For FCoE Mode: fill in all the topology information we need and call
3829 * the READ_TOPOLOGY completion routine to continue without actually
3830 * sending the READ_TOPOLOGY mailbox command to the port.
3831 */
3832 /* Parse and translate status field */
3833 mb = &pmb->u.mb;
3834 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
3835
3836 /* Parse and translate link attention fields */
3837 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
3838 la->eventTag = acqe_link->event_tag;
3839 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
3840 bf_set(lpfc_mbx_read_top_link_spd, la,
3841 lpfc_sli4_parse_latt_link_speed(phba, acqe_link));
3842
3843 /* Fake the the following irrelvant fields */
3844 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
3845 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
3846 bf_set(lpfc_mbx_read_top_il, la, 0);
3847 bf_set(lpfc_mbx_read_top_pb, la, 0);
3848 bf_set(lpfc_mbx_read_top_fa, la, 0);
3849 bf_set(lpfc_mbx_read_top_mm, la, 0);
James Smartda0436e2009-05-22 14:51:39 -04003850
3851 /* Invoke the lpfc_handle_latt mailbox command callback function */
James Smart76a95d72010-11-20 23:11:48 -05003852 lpfc_mbx_cmpl_read_topology(phba, pmb);
James Smartda0436e2009-05-22 14:51:39 -04003853
3854 return;
3855
3856out_free_dmabuf:
3857 kfree(mp);
3858out_free_pmb:
3859 mempool_free(pmb, phba->mbox_mem_pool);
3860}
3861
3862/**
James Smart70f3c072010-12-15 17:57:33 -05003863 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
3864 * @phba: pointer to lpfc hba data structure.
3865 * @acqe_fc: pointer to the async fc completion queue entry.
3866 *
3867 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
3868 * that the event was received and then issue a read_topology mailbox command so
3869 * that the rest of the driver will treat it the same as SLI3.
3870 **/
3871static void
3872lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
3873{
3874 struct lpfc_dmabuf *mp;
3875 LPFC_MBOXQ_t *pmb;
3876 int rc;
3877
3878 if (bf_get(lpfc_trailer_type, acqe_fc) !=
3879 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
3880 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3881 "2895 Non FC link Event detected.(%d)\n",
3882 bf_get(lpfc_trailer_type, acqe_fc));
3883 return;
3884 }
3885 /* Keep the link status for extra SLI4 state machine reference */
3886 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04003887 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
3888 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
James Smart70f3c072010-12-15 17:57:33 -05003889 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
3890 phba->sli4_hba.link_state.topology =
3891 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
3892 phba->sli4_hba.link_state.status =
3893 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
3894 phba->sli4_hba.link_state.type =
3895 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
3896 phba->sli4_hba.link_state.number =
3897 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
3898 phba->sli4_hba.link_state.fault =
3899 bf_get(lpfc_acqe_link_fault, acqe_fc);
3900 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04003901 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
James Smart70f3c072010-12-15 17:57:33 -05003902 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3903 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
3904 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
3905 "%dMbps Fault:%d\n",
3906 phba->sli4_hba.link_state.speed,
3907 phba->sli4_hba.link_state.topology,
3908 phba->sli4_hba.link_state.status,
3909 phba->sli4_hba.link_state.type,
3910 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04003911 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05003912 phba->sli4_hba.link_state.fault);
3913 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3914 if (!pmb) {
3915 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3916 "2897 The mboxq allocation failed\n");
3917 return;
3918 }
3919 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3920 if (!mp) {
3921 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3922 "2898 The lpfc_dmabuf allocation failed\n");
3923 goto out_free_pmb;
3924 }
3925 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3926 if (!mp->virt) {
3927 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3928 "2899 The mbuf allocation failed\n");
3929 goto out_free_dmabuf;
3930 }
3931
3932 /* Cleanup any outstanding ELS commands */
3933 lpfc_els_flush_all_cmd(phba);
3934
3935 /* Block ELS IOCBs until we have done process link event */
3936 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3937
3938 /* Update link event statistics */
3939 phba->sli.slistat.link_event++;
3940
3941 /* Create lpfc_handle_latt mailbox command from link ACQE */
3942 lpfc_read_topology(phba, pmb, mp);
3943 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
3944 pmb->vport = phba->pport;
3945
3946 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3947 if (rc == MBX_NOT_FINISHED)
3948 goto out_free_dmabuf;
3949 return;
3950
3951out_free_dmabuf:
3952 kfree(mp);
3953out_free_pmb:
3954 mempool_free(pmb, phba->mbox_mem_pool);
3955}
3956
3957/**
3958 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
3959 * @phba: pointer to lpfc hba data structure.
3960 * @acqe_fc: pointer to the async SLI completion queue entry.
3961 *
3962 * This routine is to handle the SLI4 asynchronous SLI events.
3963 **/
3964static void
3965lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
3966{
James Smart4b8bae02012-06-12 13:55:07 -04003967 char port_name;
James Smart8c1312e2012-10-31 14:45:09 -04003968 char message[128];
James Smart4b8bae02012-06-12 13:55:07 -04003969 uint8_t status;
3970 struct lpfc_acqe_misconfigured_event *misconfigured;
3971
3972 /* special case misconfigured event as it contains data for all ports */
3973 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3974 LPFC_SLI_INTF_IF_TYPE_2) ||
3975 (bf_get(lpfc_trailer_type, acqe_sli) !=
3976 LPFC_SLI_EVENT_TYPE_MISCONFIGURED)) {
3977 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3978 "2901 Async SLI event - Event Data1:x%08x Event Data2:"
3979 "x%08x SLI Event Type:%d\n",
3980 acqe_sli->event_data1, acqe_sli->event_data2,
3981 bf_get(lpfc_trailer_type, acqe_sli));
3982 return;
3983 }
3984
3985 port_name = phba->Port[0];
3986 if (port_name == 0x00)
3987 port_name = '?'; /* get port name is empty */
3988
3989 misconfigured = (struct lpfc_acqe_misconfigured_event *)
3990 &acqe_sli->event_data1;
3991
3992 /* fetch the status for this port */
3993 switch (phba->sli4_hba.lnk_info.lnk_no) {
3994 case LPFC_LINK_NUMBER_0:
3995 status = bf_get(lpfc_sli_misconfigured_port0,
3996 &misconfigured->theEvent);
3997 break;
3998 case LPFC_LINK_NUMBER_1:
3999 status = bf_get(lpfc_sli_misconfigured_port1,
4000 &misconfigured->theEvent);
4001 break;
4002 case LPFC_LINK_NUMBER_2:
4003 status = bf_get(lpfc_sli_misconfigured_port2,
4004 &misconfigured->theEvent);
4005 break;
4006 case LPFC_LINK_NUMBER_3:
4007 status = bf_get(lpfc_sli_misconfigured_port3,
4008 &misconfigured->theEvent);
4009 break;
4010 default:
4011 status = ~LPFC_SLI_EVENT_STATUS_VALID;
4012 break;
4013 }
4014
4015 switch (status) {
4016 case LPFC_SLI_EVENT_STATUS_VALID:
4017 return; /* no message if the sfp is okay */
4018 case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
James Smart292098b2012-09-29 11:31:41 -04004019 sprintf(message, "Optics faulted/incorrectly installed/not " \
4020 "installed - Reseat optics, if issue not "
4021 "resolved, replace.");
James Smart4b8bae02012-06-12 13:55:07 -04004022 break;
4023 case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
4024 sprintf(message,
James Smart292098b2012-09-29 11:31:41 -04004025 "Optics of two types installed - Remove one optic or " \
4026 "install matching pair of optics.");
James Smart4b8bae02012-06-12 13:55:07 -04004027 break;
4028 case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
James Smart292098b2012-09-29 11:31:41 -04004029 sprintf(message, "Incompatible optics - Replace with " \
4030 "compatible optics for card to function.");
James Smart4b8bae02012-06-12 13:55:07 -04004031 break;
4032 default:
4033 /* firmware is reporting a status we don't know about */
4034 sprintf(message, "Unknown event status x%02x", status);
4035 break;
4036 }
4037
4038 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4039 "3176 Misconfigured Physical Port - "
4040 "Port Name %c %s\n", port_name, message);
James Smart70f3c072010-12-15 17:57:33 -05004041}
4042
4043/**
James Smartfc2b9892010-02-26 14:15:29 -05004044 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
4045 * @vport: pointer to vport data structure.
4046 *
4047 * This routine is to perform Clear Virtual Link (CVL) on a vport in
4048 * response to a CVL event.
4049 *
4050 * Return the pointer to the ndlp with the vport if successful, otherwise
4051 * return NULL.
4052 **/
4053static struct lpfc_nodelist *
4054lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
4055{
4056 struct lpfc_nodelist *ndlp;
4057 struct Scsi_Host *shost;
4058 struct lpfc_hba *phba;
4059
4060 if (!vport)
4061 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05004062 phba = vport->phba;
4063 if (!phba)
4064 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04004065 ndlp = lpfc_findnode_did(vport, Fabric_DID);
4066 if (!ndlp) {
4067 /* Cannot find existing Fabric ndlp, so allocate a new one */
4068 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
4069 if (!ndlp)
4070 return 0;
4071 lpfc_nlp_init(vport, ndlp, Fabric_DID);
4072 /* Set the node type */
4073 ndlp->nlp_type |= NLP_FABRIC;
4074 /* Put ndlp onto node list */
4075 lpfc_enqueue_node(vport, ndlp);
4076 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4077 /* re-setup ndlp without removing from node list */
4078 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
4079 if (!ndlp)
4080 return 0;
4081 }
James Smart63e801c2010-11-20 23:14:19 -05004082 if ((phba->pport->port_state < LPFC_FLOGI) &&
4083 (phba->pport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05004084 return NULL;
4085 /* If virtual link is not yet instantiated ignore CVL */
James Smart63e801c2010-11-20 23:14:19 -05004086 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
4087 && (vport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05004088 return NULL;
4089 shost = lpfc_shost_from_vport(vport);
4090 if (!shost)
4091 return NULL;
4092 lpfc_linkdown_port(vport);
4093 lpfc_cleanup_pending_mbox(vport);
4094 spin_lock_irq(shost->host_lock);
4095 vport->fc_flag |= FC_VPORT_CVL_RCVD;
4096 spin_unlock_irq(shost->host_lock);
4097
4098 return ndlp;
4099}
4100
4101/**
4102 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
4103 * @vport: pointer to lpfc hba data structure.
4104 *
4105 * This routine is to perform Clear Virtual Link (CVL) on all vports in
4106 * response to a FCF dead event.
4107 **/
4108static void
4109lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
4110{
4111 struct lpfc_vport **vports;
4112 int i;
4113
4114 vports = lpfc_create_vport_work_array(phba);
4115 if (vports)
4116 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
4117 lpfc_sli4_perform_vport_cvl(vports[i]);
4118 lpfc_destroy_vport_work_array(phba, vports);
4119}
4120
4121/**
James Smart76a95d72010-11-20 23:11:48 -05004122 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
James Smartda0436e2009-05-22 14:51:39 -04004123 * @phba: pointer to lpfc hba data structure.
4124 * @acqe_link: pointer to the async fcoe completion queue entry.
4125 *
4126 * This routine is to handle the SLI4 asynchronous fcoe event.
4127 **/
4128static void
James Smart76a95d72010-11-20 23:11:48 -05004129lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
James Smart70f3c072010-12-15 17:57:33 -05004130 struct lpfc_acqe_fip *acqe_fip)
James Smartda0436e2009-05-22 14:51:39 -04004131{
James Smart70f3c072010-12-15 17:57:33 -05004132 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004133 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04004134 struct lpfc_vport *vport;
4135 struct lpfc_nodelist *ndlp;
4136 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05004137 int active_vlink_present;
4138 struct lpfc_vport **vports;
4139 int i;
James Smartda0436e2009-05-22 14:51:39 -04004140
James Smart70f3c072010-12-15 17:57:33 -05004141 phba->fc_eventTag = acqe_fip->event_tag;
4142 phba->fcoe_eventtag = acqe_fip->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004143 switch (event_type) {
James Smart70f3c072010-12-15 17:57:33 -05004144 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
4145 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
4146 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
James Smart999d8132010-03-15 11:24:56 -04004147 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4148 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004149 "2546 New FCF event, evt_tag:x%x, "
4150 "index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004151 acqe_fip->event_tag,
4152 acqe_fip->index);
James Smart999d8132010-03-15 11:24:56 -04004153 else
4154 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
4155 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004156 "2788 FCF param modified event, "
4157 "evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004158 acqe_fip->event_tag,
4159 acqe_fip->index);
James Smart38b92ef2010-08-04 16:11:39 -04004160 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004161 /*
4162 * During period of FCF discovery, read the FCF
4163 * table record indexed by the event to update
James Smarta93ff372010-10-22 11:06:08 -04004164 * FCF roundrobin failover eligible FCF bmask.
James Smart0c9ab6f2010-02-26 14:15:57 -05004165 */
4166 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4167 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004168 "2779 Read FCF (x%x) for updating "
4169 "roundrobin FCF failover bmask\n",
James Smart70f3c072010-12-15 17:57:33 -05004170 acqe_fip->index);
4171 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004172 }
James Smart38b92ef2010-08-04 16:11:39 -04004173
4174 /* If the FCF discovery is in progress, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04004175 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04004176 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart38b92ef2010-08-04 16:11:39 -04004177 spin_unlock_irq(&phba->hbalock);
4178 break;
4179 }
4180 /* If fast FCF failover rescan event is pending, do nothing */
4181 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
4182 spin_unlock_irq(&phba->hbalock);
4183 break;
4184 }
4185
James Smartc2b97122013-05-31 17:05:36 -04004186 /* If the FCF has been in discovered state, do nothing. */
4187 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
James Smart3804dc82010-07-14 15:31:37 -04004188 spin_unlock_irq(&phba->hbalock);
4189 break;
4190 }
4191 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04004192
James Smart0c9ab6f2010-02-26 14:15:57 -05004193 /* Otherwise, scan the entire FCF table and re-discover SAN */
4194 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004195 "2770 Start FCF table scan per async FCF "
4196 "event, evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004197 acqe_fip->event_tag, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004198 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
4199 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04004200 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05004201 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4202 "2547 Issue FCF scan read FCF mailbox "
James Smarta93ff372010-10-22 11:06:08 -04004203 "command failed (x%x)\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004204 break;
4205
James Smart70f3c072010-12-15 17:57:33 -05004206 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
James Smartda0436e2009-05-22 14:51:39 -04004207 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04004208 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004209 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
4210 acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04004211 break;
4212
James Smart70f3c072010-12-15 17:57:33 -05004213 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
James Smart80c17842012-03-01 22:35:45 -05004214 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05004215 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004216 "2549 FCF (x%x) disconnected from network, "
James Smart70f3c072010-12-15 17:57:33 -05004217 "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart38b92ef2010-08-04 16:11:39 -04004218 /*
4219 * If we are in the middle of FCF failover process, clear
4220 * the corresponding FCF bit in the roundrobin bitmap.
James Smartda0436e2009-05-22 14:51:39 -04004221 */
James Smartfc2b9892010-02-26 14:15:29 -05004222 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004223 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05004224 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004225 /* Update FLOGI FCF failover eligible FCF bmask */
James Smart70f3c072010-12-15 17:57:33 -05004226 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004227 break;
4228 }
James Smart38b92ef2010-08-04 16:11:39 -04004229 spin_unlock_irq(&phba->hbalock);
4230
4231 /* If the event is not for currently used fcf do nothing */
James Smart70f3c072010-12-15 17:57:33 -05004232 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
James Smart38b92ef2010-08-04 16:11:39 -04004233 break;
4234
4235 /*
4236 * Otherwise, request the port to rediscover the entire FCF
4237 * table for a fast recovery from case that the current FCF
4238 * is no longer valid as we are not in the middle of FCF
4239 * failover process already.
4240 */
James Smartc2b97122013-05-31 17:05:36 -04004241 spin_lock_irq(&phba->hbalock);
4242 /* Mark the fast failover process in progress */
4243 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
4244 spin_unlock_irq(&phba->hbalock);
4245
4246 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
4247 "2771 Start FCF fast failover process due to "
4248 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
4249 "\n", acqe_fip->event_tag, acqe_fip->index);
4250 rc = lpfc_sli4_redisc_fcf_table(phba);
4251 if (rc) {
4252 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4253 LOG_DISCOVERY,
4254 "2772 Issue FCF rediscover mabilbox "
4255 "command failed, fail through to FCF "
4256 "dead event\n");
4257 spin_lock_irq(&phba->hbalock);
4258 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
4259 spin_unlock_irq(&phba->hbalock);
4260 /*
4261 * Last resort will fail over by treating this
4262 * as a link down to FCF registration.
4263 */
4264 lpfc_sli4_fcf_dead_failthrough(phba);
4265 } else {
4266 /* Reset FCF roundrobin bmask for new discovery */
4267 lpfc_sli4_clear_fcf_rr_bmask(phba);
4268 /*
4269 * Handling fast FCF failover to a DEAD FCF event is
4270 * considered equalivant to receiving CVL to all vports.
4271 */
4272 lpfc_sli4_perform_all_vport_cvl(phba);
4273 }
James Smartda0436e2009-05-22 14:51:39 -04004274 break;
James Smart70f3c072010-12-15 17:57:33 -05004275 case LPFC_FIP_EVENT_TYPE_CVL:
James Smart80c17842012-03-01 22:35:45 -05004276 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05004277 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6669f9b2009-10-02 15:16:45 -04004278 "2718 Clear Virtual Link Received for VPI 0x%x"
James Smart70f3c072010-12-15 17:57:33 -05004279 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart6d368e52011-05-24 11:44:12 -04004280
James Smart6669f9b2009-10-02 15:16:45 -04004281 vport = lpfc_find_vport_by_vpid(phba,
James Smart5248a742011-07-22 18:37:06 -04004282 acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004283 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004284 if (!ndlp)
4285 break;
James Smart695a8142010-01-26 23:08:03 -05004286 active_vlink_present = 0;
4287
4288 vports = lpfc_create_vport_work_array(phba);
4289 if (vports) {
4290 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
4291 i++) {
4292 if ((!(vports[i]->fc_flag &
4293 FC_VPORT_CVL_RCVD)) &&
4294 (vports[i]->port_state > LPFC_FDISC)) {
4295 active_vlink_present = 1;
4296 break;
4297 }
4298 }
4299 lpfc_destroy_vport_work_array(phba, vports);
4300 }
4301
4302 if (active_vlink_present) {
4303 /*
4304 * If there are other active VLinks present,
4305 * re-instantiate the Vlink using FDISC.
4306 */
James Smart256ec0d2013-04-17 20:14:58 -04004307 mod_timer(&ndlp->nlp_delayfunc,
4308 jiffies + msecs_to_jiffies(1000));
James Smartfc2b9892010-02-26 14:15:29 -05004309 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004310 spin_lock_irq(shost->host_lock);
4311 ndlp->nlp_flag |= NLP_DELAY_TMO;
4312 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05004313 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
4314 vport->port_state = LPFC_FDISC;
4315 } else {
James Smartecfd03c2010-02-12 14:41:27 -05004316 /*
4317 * Otherwise, we request port to rediscover
4318 * the entire FCF table for a fast recovery
4319 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05004320 * is no longer valid if we are not already
4321 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05004322 */
James Smartfc2b9892010-02-26 14:15:29 -05004323 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004324 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05004325 spin_unlock_irq(&phba->hbalock);
4326 break;
4327 }
4328 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05004329 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004330 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004331 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4332 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004333 "2773 Start FCF failover per CVL, "
James Smart70f3c072010-12-15 17:57:33 -05004334 "evt_tag:x%x\n", acqe_fip->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05004335 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05004336 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004337 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4338 LOG_DISCOVERY,
4339 "2774 Issue FCF rediscover "
4340 "mabilbox command failed, "
4341 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05004342 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004343 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004344 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05004345 /*
4346 * Last resort will be re-try on the
4347 * the current registered FCF entry.
4348 */
4349 lpfc_retry_pport_discovery(phba);
James Smart38b92ef2010-08-04 16:11:39 -04004350 } else
4351 /*
4352 * Reset FCF roundrobin bmask for new
4353 * discovery.
4354 */
James Smart7d791df2011-07-22 18:37:52 -04004355 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart6669f9b2009-10-02 15:16:45 -04004356 }
4357 break;
James Smartda0436e2009-05-22 14:51:39 -04004358 default:
4359 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4360 "0288 Unknown FCoE event type 0x%x event tag "
James Smart70f3c072010-12-15 17:57:33 -05004361 "0x%x\n", event_type, acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04004362 break;
4363 }
4364}
4365
4366/**
4367 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
4368 * @phba: pointer to lpfc hba data structure.
4369 * @acqe_link: pointer to the async dcbx completion queue entry.
4370 *
4371 * This routine is to handle the SLI4 asynchronous dcbx event.
4372 **/
4373static void
4374lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
4375 struct lpfc_acqe_dcbx *acqe_dcbx)
4376{
James Smart4d9ab992009-10-02 15:16:39 -04004377 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004378 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4379 "0290 The SLI4 DCBX asynchronous event is not "
4380 "handled yet\n");
4381}
4382
4383/**
James Smartb19a0612010-04-06 14:48:51 -04004384 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
4385 * @phba: pointer to lpfc hba data structure.
4386 * @acqe_link: pointer to the async grp5 completion queue entry.
4387 *
4388 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
4389 * is an asynchronous notified of a logical link speed change. The Port
4390 * reports the logical link speed in units of 10Mbps.
4391 **/
4392static void
4393lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
4394 struct lpfc_acqe_grp5 *acqe_grp5)
4395{
4396 uint16_t prev_ll_spd;
4397
4398 phba->fc_eventTag = acqe_grp5->event_tag;
4399 phba->fcoe_eventtag = acqe_grp5->event_tag;
4400 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
4401 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04004402 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
James Smartb19a0612010-04-06 14:48:51 -04004403 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4404 "2789 GRP5 Async Event: Updating logical link speed "
James Smart8b68cd52012-09-29 11:32:37 -04004405 "from %dMbps to %dMbps\n", prev_ll_spd,
4406 phba->sli4_hba.link_state.logical_speed);
James Smartb19a0612010-04-06 14:48:51 -04004407}
4408
4409/**
James Smartda0436e2009-05-22 14:51:39 -04004410 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
4411 * @phba: pointer to lpfc hba data structure.
4412 *
4413 * This routine is invoked by the worker thread to process all the pending
4414 * SLI4 asynchronous events.
4415 **/
4416void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
4417{
4418 struct lpfc_cq_event *cq_event;
4419
4420 /* First, declare the async event has been handled */
4421 spin_lock_irq(&phba->hbalock);
4422 phba->hba_flag &= ~ASYNC_EVENT;
4423 spin_unlock_irq(&phba->hbalock);
4424 /* Now, handle all the async events */
4425 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
4426 /* Get the first event from the head of the event queue */
4427 spin_lock_irq(&phba->hbalock);
4428 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
4429 cq_event, struct lpfc_cq_event, list);
4430 spin_unlock_irq(&phba->hbalock);
4431 /* Process the asynchronous event */
4432 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
4433 case LPFC_TRAILER_CODE_LINK:
4434 lpfc_sli4_async_link_evt(phba,
4435 &cq_event->cqe.acqe_link);
4436 break;
4437 case LPFC_TRAILER_CODE_FCOE:
James Smart70f3c072010-12-15 17:57:33 -05004438 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004439 break;
4440 case LPFC_TRAILER_CODE_DCBX:
4441 lpfc_sli4_async_dcbx_evt(phba,
4442 &cq_event->cqe.acqe_dcbx);
4443 break;
James Smartb19a0612010-04-06 14:48:51 -04004444 case LPFC_TRAILER_CODE_GRP5:
4445 lpfc_sli4_async_grp5_evt(phba,
4446 &cq_event->cqe.acqe_grp5);
4447 break;
James Smart70f3c072010-12-15 17:57:33 -05004448 case LPFC_TRAILER_CODE_FC:
4449 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
4450 break;
4451 case LPFC_TRAILER_CODE_SLI:
4452 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
4453 break;
James Smartda0436e2009-05-22 14:51:39 -04004454 default:
4455 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4456 "1804 Invalid asynchrous event code: "
4457 "x%x\n", bf_get(lpfc_trailer_code,
4458 &cq_event->cqe.mcqe_cmpl));
4459 break;
4460 }
4461 /* Free the completion event processed to the free pool */
4462 lpfc_sli4_cq_event_release(phba, cq_event);
4463 }
4464}
4465
4466/**
James Smartecfd03c2010-02-12 14:41:27 -05004467 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
4468 * @phba: pointer to lpfc hba data structure.
4469 *
4470 * This routine is invoked by the worker thread to process FCF table
4471 * rediscovery pending completion event.
4472 **/
4473void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
4474{
4475 int rc;
4476
4477 spin_lock_irq(&phba->hbalock);
4478 /* Clear FCF rediscovery timeout event */
4479 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
4480 /* Clear driver fast failover FCF record flag */
4481 phba->fcf.failover_rec.flag = 0;
4482 /* Set state for FCF fast failover */
4483 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
4484 spin_unlock_irq(&phba->hbalock);
4485
4486 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05004487 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004488 "2777 Start post-quiescent FCF table scan\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05004489 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05004490 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05004491 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4492 "2747 Issue FCF scan read FCF mailbox "
4493 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05004494}
4495
4496/**
James Smartda0436e2009-05-22 14:51:39 -04004497 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
4498 * @phba: pointer to lpfc hba data structure.
4499 * @dev_grp: The HBA PCI-Device group number.
4500 *
4501 * This routine is invoked to set up the per HBA PCI-Device group function
4502 * API jump table entries.
4503 *
4504 * Return: 0 if success, otherwise -ENODEV
4505 **/
4506int
4507lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05004508{
4509 int rc;
4510
James Smartda0436e2009-05-22 14:51:39 -04004511 /* Set up lpfc PCI-device group */
4512 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05004513
James Smartda0436e2009-05-22 14:51:39 -04004514 /* The LPFC_PCI_DEV_OC uses SLI4 */
4515 if (dev_grp == LPFC_PCI_DEV_OC)
4516 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05004517
James Smartda0436e2009-05-22 14:51:39 -04004518 /* Set up device INIT API function jump table */
4519 rc = lpfc_init_api_table_setup(phba, dev_grp);
4520 if (rc)
4521 return -ENODEV;
4522 /* Set up SCSI API function jump table */
4523 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
4524 if (rc)
4525 return -ENODEV;
4526 /* Set up SLI API function jump table */
4527 rc = lpfc_sli_api_table_setup(phba, dev_grp);
4528 if (rc)
4529 return -ENODEV;
4530 /* Set up MBOX API function jump table */
4531 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
4532 if (rc)
4533 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004534
James Smartda0436e2009-05-22 14:51:39 -04004535 return 0;
James Smart5b75da22008-12-04 22:39:35 -05004536}
4537
4538/**
James Smart3621a712009-04-06 18:47:14 -04004539 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05004540 * @phba: pointer to lpfc hba data structure.
4541 * @intr_mode: active interrupt mode adopted.
4542 *
4543 * This routine it invoked to log the currently used active interrupt mode
4544 * to the device.
James Smart3772a992009-05-22 14:50:54 -04004545 **/
4546static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05004547{
4548 switch (intr_mode) {
4549 case 0:
4550 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4551 "0470 Enable INTx interrupt mode.\n");
4552 break;
4553 case 1:
4554 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4555 "0481 Enabled MSI interrupt mode.\n");
4556 break;
4557 case 2:
4558 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4559 "0480 Enabled MSI-X interrupt mode.\n");
4560 break;
4561 default:
4562 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4563 "0482 Illegal interrupt mode.\n");
4564 break;
4565 }
4566 return;
4567}
4568
James Smart5b75da22008-12-04 22:39:35 -05004569/**
James Smart3772a992009-05-22 14:50:54 -04004570 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05004571 * @phba: pointer to lpfc hba data structure.
4572 *
James Smart3772a992009-05-22 14:50:54 -04004573 * This routine is invoked to enable the PCI device that is common to all
4574 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004575 *
4576 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004577 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004578 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05004579 **/
James Smart3772a992009-05-22 14:50:54 -04004580static int
4581lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004582{
James Smart3772a992009-05-22 14:50:54 -04004583 struct pci_dev *pdev;
James Smart079b5c92011-08-21 21:48:49 -04004584 int bars = 0;
James Smart5b75da22008-12-04 22:39:35 -05004585
James Smart3772a992009-05-22 14:50:54 -04004586 /* Obtain PCI device reference */
4587 if (!phba->pcidev)
4588 goto out_error;
4589 else
4590 pdev = phba->pcidev;
4591 /* Select PCI BARs */
4592 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4593 /* Enable PCI device */
4594 if (pci_enable_device_mem(pdev))
4595 goto out_error;
4596 /* Request PCI resource for the device */
4597 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
4598 goto out_disable_device;
4599 /* Set up device as PCI master and save state for EEH */
4600 pci_set_master(pdev);
4601 pci_try_set_mwi(pdev);
4602 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05004603
James Smart05580562011-05-24 11:40:48 -04004604 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
Jon Mason453193e2013-09-11 15:38:13 -07004605 if (pci_is_pcie(pdev))
James Smart05580562011-05-24 11:40:48 -04004606 pdev->needs_freset = 1;
4607
James Smart3772a992009-05-22 14:50:54 -04004608 return 0;
James Smart5b75da22008-12-04 22:39:35 -05004609
James Smart3772a992009-05-22 14:50:54 -04004610out_disable_device:
4611 pci_disable_device(pdev);
4612out_error:
James Smart079b5c92011-08-21 21:48:49 -04004613 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4614 "1401 Failed to enable pci device, bars:x%x\n", bars);
James Smart3772a992009-05-22 14:50:54 -04004615 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004616}
4617
4618/**
James Smart3772a992009-05-22 14:50:54 -04004619 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05004620 * @phba: pointer to lpfc hba data structure.
4621 *
James Smart3772a992009-05-22 14:50:54 -04004622 * This routine is invoked to disable the PCI device that is common to all
4623 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004624 **/
4625static void
James Smart3772a992009-05-22 14:50:54 -04004626lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004627{
James Smart3772a992009-05-22 14:50:54 -04004628 struct pci_dev *pdev;
4629 int bars;
James Smart5b75da22008-12-04 22:39:35 -05004630
James Smart3772a992009-05-22 14:50:54 -04004631 /* Obtain PCI device reference */
4632 if (!phba->pcidev)
4633 return;
4634 else
4635 pdev = phba->pcidev;
4636 /* Select PCI BARs */
4637 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4638 /* Release PCI resource and disable PCI device */
4639 pci_release_selected_regions(pdev, bars);
4640 pci_disable_device(pdev);
James Smart5b75da22008-12-04 22:39:35 -05004641
4642 return;
4643}
4644
4645/**
James Smart3772a992009-05-22 14:50:54 -04004646 * lpfc_reset_hba - Reset a hba
4647 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04004648 *
James Smart3772a992009-05-22 14:50:54 -04004649 * This routine is invoked to reset a hba device. It brings the HBA
4650 * offline, performs a board restart, and then brings the board back
4651 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
4652 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04004653 **/
James Smart3772a992009-05-22 14:50:54 -04004654void
4655lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004656{
James Smart3772a992009-05-22 14:50:54 -04004657 /* If resets are disabled then set error state and return. */
4658 if (!phba->cfg_enable_hba_reset) {
4659 phba->link_state = LPFC_HBA_ERROR;
4660 return;
4661 }
James Smartee620212014-04-04 13:51:53 -04004662 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
4663 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
4664 else
4665 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart3772a992009-05-22 14:50:54 -04004666 lpfc_offline(phba);
4667 lpfc_sli_brdrestart(phba);
4668 lpfc_online(phba);
4669 lpfc_unblock_mgmt_io(phba);
4670}
dea31012005-04-17 16:05:31 -05004671
James Smart3772a992009-05-22 14:50:54 -04004672/**
James Smart0a96e972011-07-22 18:37:28 -04004673 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
4674 * @phba: pointer to lpfc hba data structure.
4675 *
4676 * This function enables the PCI SR-IOV virtual functions to a physical
4677 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4678 * enable the number of virtual functions to the physical function. As
4679 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4680 * API call does not considered as an error condition for most of the device.
4681 **/
4682uint16_t
4683lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
4684{
4685 struct pci_dev *pdev = phba->pcidev;
4686 uint16_t nr_virtfn;
4687 int pos;
4688
James Smart0a96e972011-07-22 18:37:28 -04004689 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
4690 if (pos == 0)
4691 return 0;
4692
4693 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
4694 return nr_virtfn;
4695}
4696
4697/**
James Smart912e3ac2011-05-24 11:42:11 -04004698 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
4699 * @phba: pointer to lpfc hba data structure.
4700 * @nr_vfn: number of virtual functions to be enabled.
4701 *
4702 * This function enables the PCI SR-IOV virtual functions to a physical
4703 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4704 * enable the number of virtual functions to the physical function. As
4705 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4706 * API call does not considered as an error condition for most of the device.
4707 **/
4708int
4709lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
4710{
4711 struct pci_dev *pdev = phba->pcidev;
James Smart0a96e972011-07-22 18:37:28 -04004712 uint16_t max_nr_vfn;
James Smart912e3ac2011-05-24 11:42:11 -04004713 int rc;
4714
James Smart0a96e972011-07-22 18:37:28 -04004715 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
4716 if (nr_vfn > max_nr_vfn) {
4717 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4718 "3057 Requested vfs (%d) greater than "
4719 "supported vfs (%d)", nr_vfn, max_nr_vfn);
4720 return -EINVAL;
4721 }
4722
James Smart912e3ac2011-05-24 11:42:11 -04004723 rc = pci_enable_sriov(pdev, nr_vfn);
4724 if (rc) {
4725 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4726 "2806 Failed to enable sriov on this device "
4727 "with vfn number nr_vf:%d, rc:%d\n",
4728 nr_vfn, rc);
4729 } else
4730 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4731 "2807 Successful enable sriov on this device "
4732 "with vfn number nr_vf:%d\n", nr_vfn);
4733 return rc;
4734}
4735
4736/**
James Smart3772a992009-05-22 14:50:54 -04004737 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
4738 * @phba: pointer to lpfc hba data structure.
4739 *
4740 * This routine is invoked to set up the driver internal resources specific to
4741 * support the SLI-3 HBA device it attached to.
4742 *
4743 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004744 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004745 * other values - error
4746 **/
4747static int
4748lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
4749{
4750 struct lpfc_sli *psli;
James Smart912e3ac2011-05-24 11:42:11 -04004751 int rc;
dea31012005-04-17 16:05:31 -05004752
James Smart3772a992009-05-22 14:50:54 -04004753 /*
4754 * Initialize timers used by driver
4755 */
dea31012005-04-17 16:05:31 -05004756
James Smart3772a992009-05-22 14:50:54 -04004757 /* Heartbeat timer */
4758 init_timer(&phba->hb_tmofunc);
4759 phba->hb_tmofunc.function = lpfc_hb_timeout;
4760 phba->hb_tmofunc.data = (unsigned long)phba;
4761
4762 psli = &phba->sli;
4763 /* MBOX heartbeat timer */
4764 init_timer(&psli->mbox_tmo);
4765 psli->mbox_tmo.function = lpfc_mbox_timeout;
4766 psli->mbox_tmo.data = (unsigned long) phba;
4767 /* FCP polling mode timer */
4768 init_timer(&phba->fcp_poll_timer);
4769 phba->fcp_poll_timer.function = lpfc_poll_timeout;
4770 phba->fcp_poll_timer.data = (unsigned long) phba;
4771 /* Fabric block timer */
4772 init_timer(&phba->fabric_block_timer);
4773 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4774 phba->fabric_block_timer.data = (unsigned long) phba;
4775 /* EA polling mode timer */
4776 init_timer(&phba->eratt_poll);
4777 phba->eratt_poll.function = lpfc_poll_eratt;
4778 phba->eratt_poll.data = (unsigned long) phba;
4779
4780 /* Host attention work mask setup */
4781 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
4782 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
4783
4784 /* Get all the module params for configuring this host */
4785 lpfc_get_cfgparam(phba);
James Smart49198b32010-04-06 15:04:33 -04004786 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
4787 phba->menlo_flag |= HBA_MENLO_SUPPORT;
4788 /* check for menlo minimum sg count */
4789 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
4790 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
4791 }
4792
James Smart2a76a282012-08-03 12:35:54 -04004793 if (!phba->sli.ring)
4794 phba->sli.ring = (struct lpfc_sli_ring *)
4795 kzalloc(LPFC_SLI3_MAX_RING *
4796 sizeof(struct lpfc_sli_ring), GFP_KERNEL);
4797 if (!phba->sli.ring)
4798 return -ENOMEM;
4799
James Smart3772a992009-05-22 14:50:54 -04004800 /*
James Smart96f70772013-04-17 20:16:15 -04004801 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smart3772a992009-05-22 14:50:54 -04004802 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart3772a992009-05-22 14:50:54 -04004803 */
James Smart3772a992009-05-22 14:50:54 -04004804
James Smart96f70772013-04-17 20:16:15 -04004805 /* Initialize the host templates the configured values. */
James Smart3772a992009-05-22 14:50:54 -04004806 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4807 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4808
James Smart96f70772013-04-17 20:16:15 -04004809 /* There are going to be 2 reserved BDEs: 1 FCP cmnd + 1 FCP rsp */
4810 if (phba->cfg_enable_bg) {
4811 /*
4812 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
4813 * the FCP rsp, and a BDE for each. Sice we have no control
4814 * over how many protection data segments the SCSI Layer
4815 * will hand us (ie: there could be one for every block
4816 * in the IO), we just allocate enough BDEs to accomidate
4817 * our max amount and we need to limit lpfc_sg_seg_cnt to
4818 * minimize the risk of running out.
4819 */
4820 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4821 sizeof(struct fcp_rsp) +
4822 (LPFC_MAX_SG_SEG_CNT * sizeof(struct ulp_bde64));
4823
4824 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SEG_CNT_DIF)
4825 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT_DIF;
4826
4827 /* Total BDEs in BPL for scsi_sg_list and scsi_sg_prot_list */
4828 phba->cfg_total_seg_cnt = LPFC_MAX_SG_SEG_CNT;
4829 } else {
4830 /*
4831 * The scsi_buf for a regular I/O will hold the FCP cmnd,
4832 * the FCP rsp, a BDE for each, and a BDE for up to
4833 * cfg_sg_seg_cnt data segments.
4834 */
4835 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4836 sizeof(struct fcp_rsp) +
4837 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
4838
4839 /* Total BDEs in BPL for scsi_sg_list */
4840 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
4841 }
4842
4843 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
4844 "9088 sg_tablesize:%d dmabuf_size:%d total_bde:%d\n",
4845 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
4846 phba->cfg_total_seg_cnt);
4847
James Smart3772a992009-05-22 14:50:54 -04004848 phba->max_vpi = LPFC_MAX_VPI;
4849 /* This will be set to correct value after config_port mbox */
4850 phba->max_vports = 0;
4851
4852 /*
4853 * Initialize the SLI Layer to run with lpfc HBAs.
4854 */
4855 lpfc_sli_setup(phba);
4856 lpfc_sli_queue_setup(phba);
4857
4858 /* Allocate device driver memory */
4859 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
4860 return -ENOMEM;
4861
James Smart912e3ac2011-05-24 11:42:11 -04004862 /*
4863 * Enable sr-iov virtual functions if supported and configured
4864 * through the module parameter.
4865 */
4866 if (phba->cfg_sriov_nr_virtfn > 0) {
4867 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
4868 phba->cfg_sriov_nr_virtfn);
4869 if (rc) {
4870 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4871 "2808 Requested number of SR-IOV "
4872 "virtual functions (%d) is not "
4873 "supported\n",
4874 phba->cfg_sriov_nr_virtfn);
4875 phba->cfg_sriov_nr_virtfn = 0;
4876 }
4877 }
4878
James Smart3772a992009-05-22 14:50:54 -04004879 return 0;
4880}
4881
4882/**
4883 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
4884 * @phba: pointer to lpfc hba data structure.
4885 *
4886 * This routine is invoked to unset the driver internal resources set up
4887 * specific for supporting the SLI-3 HBA device it attached to.
4888 **/
4889static void
4890lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
4891{
4892 /* Free device driver memory allocated */
4893 lpfc_mem_free_all(phba);
4894
4895 return;
4896}
4897
4898/**
James Smartda0436e2009-05-22 14:51:39 -04004899 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
4900 * @phba: pointer to lpfc hba data structure.
4901 *
4902 * This routine is invoked to set up the driver internal resources specific to
4903 * support the SLI-4 HBA device it attached to.
4904 *
4905 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004906 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004907 * other values - error
4908 **/
4909static int
4910lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
4911{
James Smart7bb03bb2013-04-17 20:19:16 -04004912 struct lpfc_vector_map_info *cpup;
James Smartda0436e2009-05-22 14:51:39 -04004913 struct lpfc_sli *psli;
James Smart28baac72010-02-12 14:42:03 -05004914 LPFC_MBOXQ_t *mboxq;
James Smart96f70772013-04-17 20:16:15 -04004915 int rc, i, hbq_count, max_buf_size;
James Smart28baac72010-02-12 14:42:03 -05004916 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
4917 struct lpfc_mqe *mqe;
James Smart09294d42013-04-17 20:16:05 -04004918 int longs;
James Smart1ba981f2014-02-20 09:56:45 -05004919 int fof_vectors = 0;
James Smartda0436e2009-05-22 14:51:39 -04004920
James Smart716d3bc2013-09-06 12:18:28 -04004921 /* Get all the module params for configuring this host */
4922 lpfc_get_cfgparam(phba);
4923
James Smartda0436e2009-05-22 14:51:39 -04004924 /* Before proceed, wait for POST done and device ready */
4925 rc = lpfc_sli4_post_status_check(phba);
4926 if (rc)
4927 return -ENODEV;
4928
4929 /*
4930 * Initialize timers used by driver
4931 */
4932
4933 /* Heartbeat timer */
4934 init_timer(&phba->hb_tmofunc);
4935 phba->hb_tmofunc.function = lpfc_hb_timeout;
4936 phba->hb_tmofunc.data = (unsigned long)phba;
James Smart19ca7602010-11-20 23:11:55 -05004937 init_timer(&phba->rrq_tmr);
4938 phba->rrq_tmr.function = lpfc_rrq_timeout;
4939 phba->rrq_tmr.data = (unsigned long)phba;
James Smartda0436e2009-05-22 14:51:39 -04004940
4941 psli = &phba->sli;
4942 /* MBOX heartbeat timer */
4943 init_timer(&psli->mbox_tmo);
4944 psli->mbox_tmo.function = lpfc_mbox_timeout;
4945 psli->mbox_tmo.data = (unsigned long) phba;
4946 /* Fabric block timer */
4947 init_timer(&phba->fabric_block_timer);
4948 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4949 phba->fabric_block_timer.data = (unsigned long) phba;
4950 /* EA polling mode timer */
4951 init_timer(&phba->eratt_poll);
4952 phba->eratt_poll.function = lpfc_poll_eratt;
4953 phba->eratt_poll.data = (unsigned long) phba;
James Smartecfd03c2010-02-12 14:41:27 -05004954 /* FCF rediscover timer */
4955 init_timer(&phba->fcf.redisc_wait);
4956 phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
4957 phba->fcf.redisc_wait.data = (unsigned long)phba;
4958
James Smartda0436e2009-05-22 14:51:39 -04004959 /*
James Smart7ad20aa2011-05-24 11:44:28 -04004960 * Control structure for handling external multi-buffer mailbox
4961 * command pass-through.
4962 */
4963 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
4964 sizeof(struct lpfc_mbox_ext_buf_ctx));
4965 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
4966
James Smartda0436e2009-05-22 14:51:39 -04004967 phba->max_vpi = LPFC_MAX_VPI;
James Smart67d12732012-08-03 12:36:13 -04004968
James Smartda0436e2009-05-22 14:51:39 -04004969 /* This will be set to correct value after the read_config mbox */
4970 phba->max_vports = 0;
4971
4972 /* Program the default value of vlan_id and fc_map */
4973 phba->valid_vlan = 0;
4974 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4975 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4976 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4977
4978 /*
James Smart2a76a282012-08-03 12:35:54 -04004979 * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
4980 * we will associate a new ring, for each FCP fastpath EQ/CQ/WQ tuple.
4981 */
4982 if (!phba->sli.ring)
4983 phba->sli.ring = kzalloc(
James Smart67d12732012-08-03 12:36:13 -04004984 (LPFC_SLI3_MAX_RING + phba->cfg_fcp_io_channel) *
James Smart2a76a282012-08-03 12:35:54 -04004985 sizeof(struct lpfc_sli_ring), GFP_KERNEL);
4986 if (!phba->sli.ring)
4987 return -ENOMEM;
James Smart085c6472010-11-20 23:11:37 -05004988
James Smart09294d42013-04-17 20:16:05 -04004989 /*
4990 * It doesn't matter what family our adapter is in, we are
4991 * limited to 2 Pages, 512 SGEs, for our SGL.
4992 * There are going to be 2 reserved SGEs: 1 FCP cmnd + 1 FCP rsp
4993 */
4994 max_buf_size = (2 * SLI4_PAGE_SIZE);
4995 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SGL_SEG_CNT - 2)
4996 phba->cfg_sg_seg_cnt = LPFC_MAX_SGL_SEG_CNT - 2;
James Smart09294d42013-04-17 20:16:05 -04004997
James Smartda0436e2009-05-22 14:51:39 -04004998 /*
James Smart96f70772013-04-17 20:16:15 -04004999 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smartda0436e2009-05-22 14:51:39 -04005000 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart28baac72010-02-12 14:42:03 -05005001 */
James Smart085c6472010-11-20 23:11:37 -05005002
James Smart96f70772013-04-17 20:16:15 -04005003 if (phba->cfg_enable_bg) {
5004 /*
5005 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
5006 * the FCP rsp, and a SGE for each. Sice we have no control
5007 * over how many protection data segments the SCSI Layer
5008 * will hand us (ie: there could be one for every block
5009 * in the IO), we just allocate enough SGEs to accomidate
5010 * our max amount and we need to limit lpfc_sg_seg_cnt to
5011 * minimize the risk of running out.
5012 */
5013 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5014 sizeof(struct fcp_rsp) + max_buf_size;
5015
5016 /* Total SGEs for scsi_sg_list and scsi_sg_prot_list */
5017 phba->cfg_total_seg_cnt = LPFC_MAX_SGL_SEG_CNT;
5018
5019 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SLI4_SEG_CNT_DIF)
5020 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SLI4_SEG_CNT_DIF;
5021 } else {
5022 /*
5023 * The scsi_buf for a regular I/O will hold the FCP cmnd,
5024 * the FCP rsp, a SGE for each, and a SGE for up to
5025 * cfg_sg_seg_cnt data segments.
5026 */
5027 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5028 sizeof(struct fcp_rsp) +
5029 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge));
5030
5031 /* Total SGEs for scsi_sg_list */
5032 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
5033 /*
5034 * NOTE: if (phba->cfg_sg_seg_cnt + 2) <= 256 we only need
5035 * to post 1 page for the SGL.
5036 */
James Smart085c6472010-11-20 23:11:37 -05005037 }
James Smartacd68592012-01-18 16:25:09 -05005038
James Smart96f70772013-04-17 20:16:15 -04005039 /* Initialize the host templates with the updated values. */
5040 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5041 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5042
5043 if (phba->cfg_sg_dma_buf_size <= LPFC_MIN_SG_SLI4_BUF_SZ)
5044 phba->cfg_sg_dma_buf_size = LPFC_MIN_SG_SLI4_BUF_SZ;
5045 else
5046 phba->cfg_sg_dma_buf_size =
5047 SLI4_PAGE_ALIGN(phba->cfg_sg_dma_buf_size);
5048
5049 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
5050 "9087 sg_tablesize:%d dmabuf_size:%d total_sge:%d\n",
5051 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
5052 phba->cfg_total_seg_cnt);
James Smartda0436e2009-05-22 14:51:39 -04005053
5054 /* Initialize buffer queue management fields */
5055 hbq_count = lpfc_sli_hbq_count();
5056 for (i = 0; i < hbq_count; ++i)
5057 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
5058 INIT_LIST_HEAD(&phba->rb_pend_list);
5059 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
5060 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
5061
5062 /*
5063 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
5064 */
5065 /* Initialize the Abort scsi buffer list used by driver */
5066 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
5067 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
5068 /* This abort list used by worker thread */
5069 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
5070
5071 /*
James Smart6d368e52011-05-24 11:44:12 -04005072 * Initialize driver internal slow-path work queues
James Smartda0436e2009-05-22 14:51:39 -04005073 */
5074
5075 /* Driver internel slow-path CQ Event pool */
5076 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
5077 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04005078 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04005079 /* Asynchronous event CQ Event work queue list */
5080 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
5081 /* Fast-path XRI aborted CQ Event work queue list */
5082 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
5083 /* Slow-path XRI aborted CQ Event work queue list */
5084 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
5085 /* Receive queue CQ Event work queue list */
5086 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
5087
James Smart6d368e52011-05-24 11:44:12 -04005088 /* Initialize extent block lists. */
5089 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
5090 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
5091 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
5092 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
5093
James Smartda0436e2009-05-22 14:51:39 -04005094 /* Initialize the driver internal SLI layer lists. */
5095 lpfc_sli_setup(phba);
5096 lpfc_sli_queue_setup(phba);
5097
5098 /* Allocate device driver memory */
5099 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
5100 if (rc)
5101 return -ENOMEM;
5102
James Smart2fcee4b2010-12-15 17:57:46 -05005103 /* IF Type 2 ports get initialized now. */
5104 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
5105 LPFC_SLI_INTF_IF_TYPE_2) {
5106 rc = lpfc_pci_function_reset(phba);
5107 if (unlikely(rc))
5108 return -ENODEV;
5109 }
5110
James Smartda0436e2009-05-22 14:51:39 -04005111 /* Create the bootstrap mailbox command */
5112 rc = lpfc_create_bootstrap_mbox(phba);
5113 if (unlikely(rc))
5114 goto out_free_mem;
5115
5116 /* Set up the host's endian order with the device. */
5117 rc = lpfc_setup_endian_order(phba);
5118 if (unlikely(rc))
5119 goto out_free_bsmbx;
5120
5121 /* Set up the hba's configuration parameters. */
5122 rc = lpfc_sli4_read_config(phba);
5123 if (unlikely(rc))
5124 goto out_free_bsmbx;
James Smartcff261f2013-12-17 20:29:47 -05005125 rc = lpfc_mem_alloc_active_rrq_pool_s4(phba);
5126 if (unlikely(rc))
5127 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04005128
James Smart2fcee4b2010-12-15 17:57:46 -05005129 /* IF Type 0 ports get initialized now. */
5130 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
5131 LPFC_SLI_INTF_IF_TYPE_0) {
5132 rc = lpfc_pci_function_reset(phba);
5133 if (unlikely(rc))
5134 goto out_free_bsmbx;
5135 }
James Smartda0436e2009-05-22 14:51:39 -04005136
James Smartcb5172e2010-03-15 11:25:07 -04005137 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
5138 GFP_KERNEL);
5139 if (!mboxq) {
5140 rc = -ENOMEM;
5141 goto out_free_bsmbx;
5142 }
5143
James Smartfedd3b72011-02-16 12:39:24 -05005144 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
James Smartcb5172e2010-03-15 11:25:07 -04005145 lpfc_supported_pages(mboxq);
5146 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smartfedd3b72011-02-16 12:39:24 -05005147 if (!rc) {
5148 mqe = &mboxq->u.mqe;
5149 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
5150 LPFC_MAX_SUPPORTED_PAGES);
5151 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
5152 switch (pn_page[i]) {
5153 case LPFC_SLI4_PARAMETERS:
5154 phba->sli4_hba.pc_sli4_params.supported = 1;
5155 break;
5156 default:
5157 break;
5158 }
5159 }
5160 /* Read the port's SLI4 Parameters capabilities if supported. */
5161 if (phba->sli4_hba.pc_sli4_params.supported)
5162 rc = lpfc_pc_sli4_params_get(phba, mboxq);
5163 if (rc) {
5164 mempool_free(mboxq, phba->mbox_mem_pool);
5165 rc = -EIO;
5166 goto out_free_bsmbx;
James Smartcb5172e2010-03-15 11:25:07 -04005167 }
5168 }
James Smartfedd3b72011-02-16 12:39:24 -05005169 /*
5170 * Get sli4 parameters that override parameters from Port capabilities.
James Smart6d368e52011-05-24 11:44:12 -04005171 * If this call fails, it isn't critical unless the SLI4 parameters come
5172 * back in conflict.
James Smartfedd3b72011-02-16 12:39:24 -05005173 */
James Smart6d368e52011-05-24 11:44:12 -04005174 rc = lpfc_get_sli4_parameters(phba, mboxq);
5175 if (rc) {
5176 if (phba->sli4_hba.extents_in_use &&
5177 phba->sli4_hba.rpi_hdrs_in_use) {
5178 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5179 "2999 Unsupported SLI4 Parameters "
5180 "Extents and RPI headers enabled.\n");
5181 goto out_free_bsmbx;
5182 }
5183 }
James Smartcb5172e2010-03-15 11:25:07 -04005184 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1ba981f2014-02-20 09:56:45 -05005185
5186 /* Verify OAS is supported */
5187 lpfc_sli4_oas_verify(phba);
5188 if (phba->cfg_fof)
5189 fof_vectors = 1;
5190
James Smart5350d872011-10-10 21:33:49 -04005191 /* Verify all the SLI4 queues */
5192 rc = lpfc_sli4_queue_verify(phba);
James Smartda0436e2009-05-22 14:51:39 -04005193 if (rc)
5194 goto out_free_bsmbx;
5195
5196 /* Create driver internal CQE event pool */
5197 rc = lpfc_sli4_cq_event_pool_create(phba);
5198 if (rc)
James Smart5350d872011-10-10 21:33:49 -04005199 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04005200
James Smart8a9d2e82012-05-09 21:16:12 -04005201 /* Initialize sgl lists per host */
5202 lpfc_init_sgl_list(phba);
5203
5204 /* Allocate and initialize active sgl array */
James Smartda0436e2009-05-22 14:51:39 -04005205 rc = lpfc_init_active_sgl_array(phba);
5206 if (rc) {
5207 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5208 "1430 Failed to initialize sgl list.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04005209 goto out_destroy_cq_event_pool;
James Smartda0436e2009-05-22 14:51:39 -04005210 }
James Smartda0436e2009-05-22 14:51:39 -04005211 rc = lpfc_sli4_init_rpi_hdrs(phba);
5212 if (rc) {
5213 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5214 "1432 Failed to initialize rpi headers.\n");
5215 goto out_free_active_sgl;
5216 }
5217
James Smarta93ff372010-10-22 11:06:08 -04005218 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
James Smart0c9ab6f2010-02-26 14:15:57 -05005219 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
5220 phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
5221 GFP_KERNEL);
5222 if (!phba->fcf.fcf_rr_bmask) {
5223 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5224 "2759 Failed allocate memory for FCF round "
5225 "robin failover bmask\n");
James Smart05580562011-05-24 11:40:48 -04005226 rc = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -05005227 goto out_remove_rpi_hdrs;
5228 }
5229
James Smart67d12732012-08-03 12:36:13 -04005230 phba->sli4_hba.fcp_eq_hdl =
5231 kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
James Smart1ba981f2014-02-20 09:56:45 -05005232 (fof_vectors + phba->cfg_fcp_io_channel)),
5233 GFP_KERNEL);
James Smart67d12732012-08-03 12:36:13 -04005234 if (!phba->sli4_hba.fcp_eq_hdl) {
5235 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5236 "2572 Failed allocate memory for "
5237 "fast-path per-EQ handle array\n");
5238 rc = -ENOMEM;
5239 goto out_free_fcf_rr_bmask;
James Smartda0436e2009-05-22 14:51:39 -04005240 }
5241
5242 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
James Smart1ba981f2014-02-20 09:56:45 -05005243 (fof_vectors +
5244 phba->cfg_fcp_io_channel)), GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005245 if (!phba->sli4_hba.msix_entries) {
5246 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5247 "2573 Failed allocate memory for msi-x "
5248 "interrupt vector entries\n");
James Smart05580562011-05-24 11:40:48 -04005249 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04005250 goto out_free_fcp_eq_hdl;
5251 }
5252
James Smart7bb03bb2013-04-17 20:19:16 -04005253 phba->sli4_hba.cpu_map = kzalloc((sizeof(struct lpfc_vector_map_info) *
5254 phba->sli4_hba.num_present_cpu),
5255 GFP_KERNEL);
5256 if (!phba->sli4_hba.cpu_map) {
5257 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5258 "3327 Failed allocate memory for msi-x "
5259 "interrupt vector mapping\n");
5260 rc = -ENOMEM;
5261 goto out_free_msix;
5262 }
James Smartb246de12013-05-31 17:03:07 -04005263 if (lpfc_used_cpu == NULL) {
5264 lpfc_used_cpu = kzalloc((sizeof(uint16_t) * lpfc_present_cpu),
5265 GFP_KERNEL);
5266 if (!lpfc_used_cpu) {
5267 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5268 "3335 Failed allocate memory for msi-x "
5269 "interrupt vector mapping\n");
5270 kfree(phba->sli4_hba.cpu_map);
5271 rc = -ENOMEM;
5272 goto out_free_msix;
5273 }
5274 for (i = 0; i < lpfc_present_cpu; i++)
5275 lpfc_used_cpu[i] = LPFC_VECTOR_MAP_EMPTY;
5276 }
5277
James Smart7bb03bb2013-04-17 20:19:16 -04005278 /* Initialize io channels for round robin */
5279 cpup = phba->sli4_hba.cpu_map;
5280 rc = 0;
5281 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
5282 cpup->channel_id = rc;
5283 rc++;
5284 if (rc >= phba->cfg_fcp_io_channel)
5285 rc = 0;
5286 }
5287
James Smart912e3ac2011-05-24 11:42:11 -04005288 /*
5289 * Enable sr-iov virtual functions if supported and configured
5290 * through the module parameter.
5291 */
5292 if (phba->cfg_sriov_nr_virtfn > 0) {
5293 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
5294 phba->cfg_sriov_nr_virtfn);
5295 if (rc) {
5296 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5297 "3020 Requested number of SR-IOV "
5298 "virtual functions (%d) is not "
5299 "supported\n",
5300 phba->cfg_sriov_nr_virtfn);
5301 phba->cfg_sriov_nr_virtfn = 0;
5302 }
5303 }
5304
James Smart5248a742011-07-22 18:37:06 -04005305 return 0;
James Smartda0436e2009-05-22 14:51:39 -04005306
James Smart7bb03bb2013-04-17 20:19:16 -04005307out_free_msix:
5308 kfree(phba->sli4_hba.msix_entries);
James Smartda0436e2009-05-22 14:51:39 -04005309out_free_fcp_eq_hdl:
5310 kfree(phba->sli4_hba.fcp_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05005311out_free_fcf_rr_bmask:
5312 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04005313out_remove_rpi_hdrs:
5314 lpfc_sli4_remove_rpi_hdrs(phba);
5315out_free_active_sgl:
5316 lpfc_free_active_sgl(phba);
James Smartda0436e2009-05-22 14:51:39 -04005317out_destroy_cq_event_pool:
5318 lpfc_sli4_cq_event_pool_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04005319out_free_bsmbx:
5320 lpfc_destroy_bootstrap_mbox(phba);
5321out_free_mem:
5322 lpfc_mem_free(phba);
5323 return rc;
5324}
5325
5326/**
5327 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
5328 * @phba: pointer to lpfc hba data structure.
5329 *
5330 * This routine is invoked to unset the driver internal resources set up
5331 * specific for supporting the SLI-4 HBA device it attached to.
5332 **/
5333static void
5334lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
5335{
5336 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
5337
James Smart7bb03bb2013-04-17 20:19:16 -04005338 /* Free memory allocated for msi-x interrupt vector to CPU mapping */
5339 kfree(phba->sli4_hba.cpu_map);
5340 phba->sli4_hba.num_present_cpu = 0;
5341 phba->sli4_hba.num_online_cpu = 0;
James Smart76fd07a2014-02-20 09:57:18 -05005342 phba->sli4_hba.curr_disp_cpu = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04005343
James Smartda0436e2009-05-22 14:51:39 -04005344 /* Free memory allocated for msi-x interrupt vector entries */
5345 kfree(phba->sli4_hba.msix_entries);
5346
5347 /* Free memory allocated for fast-path work queue handles */
5348 kfree(phba->sli4_hba.fcp_eq_hdl);
5349
5350 /* Free the allocated rpi headers. */
5351 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04005352 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04005353
James Smart0c9ab6f2010-02-26 14:15:57 -05005354 /* Free eligible FCF index bmask */
5355 kfree(phba->fcf.fcf_rr_bmask);
5356
James Smartda0436e2009-05-22 14:51:39 -04005357 /* Free the ELS sgl list */
5358 lpfc_free_active_sgl(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04005359 lpfc_free_els_sgl_list(phba);
James Smartda0436e2009-05-22 14:51:39 -04005360
James Smartda0436e2009-05-22 14:51:39 -04005361 /* Free the completion queue EQ event pool */
5362 lpfc_sli4_cq_event_release_all(phba);
5363 lpfc_sli4_cq_event_pool_destroy(phba);
5364
James Smart6d368e52011-05-24 11:44:12 -04005365 /* Release resource identifiers. */
5366 lpfc_sli4_dealloc_resource_identifiers(phba);
5367
James Smartda0436e2009-05-22 14:51:39 -04005368 /* Free the bsmbx region. */
5369 lpfc_destroy_bootstrap_mbox(phba);
5370
5371 /* Free the SLI Layer memory with SLI4 HBAs */
5372 lpfc_mem_free_all(phba);
5373
5374 /* Free the current connect table */
5375 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04005376 &phba->fcf_conn_rec_list, list) {
5377 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04005378 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04005379 }
James Smartda0436e2009-05-22 14:51:39 -04005380
5381 return;
5382}
5383
5384/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005385 * lpfc_init_api_table_setup - Set up init api function jump table
James Smart3772a992009-05-22 14:50:54 -04005386 * @phba: The hba struct for which this call is being executed.
5387 * @dev_grp: The HBA PCI-Device group number.
5388 *
5389 * This routine sets up the device INIT interface API function jump table
5390 * in @phba struct.
5391 *
5392 * Returns: 0 - success, -ENODEV - failure.
5393 **/
5394int
5395lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5396{
James Smart84d1b002010-02-12 14:42:33 -05005397 phba->lpfc_hba_init_link = lpfc_hba_init_link;
5398 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart7f860592011-03-11 16:05:52 -05005399 phba->lpfc_selective_reset = lpfc_selective_reset;
James Smart3772a992009-05-22 14:50:54 -04005400 switch (dev_grp) {
5401 case LPFC_PCI_DEV_LP:
5402 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
5403 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
5404 phba->lpfc_stop_port = lpfc_stop_port_s3;
5405 break;
James Smartda0436e2009-05-22 14:51:39 -04005406 case LPFC_PCI_DEV_OC:
5407 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
5408 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
5409 phba->lpfc_stop_port = lpfc_stop_port_s4;
5410 break;
James Smart3772a992009-05-22 14:50:54 -04005411 default:
5412 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5413 "1431 Invalid HBA PCI-device group: 0x%x\n",
5414 dev_grp);
5415 return -ENODEV;
5416 break;
5417 }
5418 return 0;
5419}
5420
5421/**
5422 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
5423 * @phba: pointer to lpfc hba data structure.
5424 *
5425 * This routine is invoked to set up the driver internal resources before the
5426 * device specific resource setup to support the HBA device it attached to.
5427 *
5428 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005429 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005430 * other values - error
5431 **/
5432static int
5433lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
5434{
5435 /*
5436 * Driver resources common to all SLI revisions
5437 */
James Smartea2151b2008-09-07 11:52:10 -04005438 atomic_set(&phba->fast_event_count, 0);
James Smart2e0fef82007-06-17 19:56:36 -05005439 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005440
James Smarte47c9092008-02-08 18:49:26 -05005441 /* Initialize ndlp management spinlock */
5442 spin_lock_init(&phba->ndlp_lock);
5443
James Smart2e0fef82007-06-17 19:56:36 -05005444 INIT_LIST_HEAD(&phba->port_list);
James Smart3772a992009-05-22 14:50:54 -04005445 INIT_LIST_HEAD(&phba->work_list);
James Smart84774a42008-08-24 21:50:06 -04005446 init_waitqueue_head(&phba->wait_4_mlo_m_q);
dea31012005-04-17 16:05:31 -05005447
James Smart3772a992009-05-22 14:50:54 -04005448 /* Initialize the wait queue head for the kernel thread */
5449 init_waitqueue_head(&phba->work_waitq);
James Smart858c9f62007-06-17 19:56:39 -05005450
James Smart3772a992009-05-22 14:50:54 -04005451 /* Initialize the scsi buffer list used by driver for scsi IO */
James Smarta40fc5f2013-04-17 20:17:40 -04005452 spin_lock_init(&phba->scsi_buf_list_get_lock);
5453 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_get);
5454 spin_lock_init(&phba->scsi_buf_list_put_lock);
5455 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
dea31012005-04-17 16:05:31 -05005456
James Smart3772a992009-05-22 14:50:54 -04005457 /* Initialize the fabric iocb list */
5458 INIT_LIST_HEAD(&phba->fabric_iocb_list);
dea31012005-04-17 16:05:31 -05005459
James Smart3772a992009-05-22 14:50:54 -04005460 /* Initialize list to save ELS buffers */
5461 INIT_LIST_HEAD(&phba->elsbuf);
dea31012005-04-17 16:05:31 -05005462
James Smart3772a992009-05-22 14:50:54 -04005463 /* Initialize FCF connection rec list */
5464 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
dea31012005-04-17 16:05:31 -05005465
James Smart1ba981f2014-02-20 09:56:45 -05005466 /* Initialize OAS configuration list */
5467 spin_lock_init(&phba->devicelock);
5468 INIT_LIST_HEAD(&phba->luns);
5469
James Smart3772a992009-05-22 14:50:54 -04005470 return 0;
5471}
dea31012005-04-17 16:05:31 -05005472
James Smart3772a992009-05-22 14:50:54 -04005473/**
5474 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
5475 * @phba: pointer to lpfc hba data structure.
5476 *
5477 * This routine is invoked to set up the driver internal resources after the
5478 * device specific resource setup to support the HBA device it attached to.
5479 *
5480 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005481 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005482 * other values - error
5483 **/
5484static int
5485lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
5486{
5487 int error;
5488
5489 /* Startup the kernel thread for this host adapter. */
5490 phba->worker_thread = kthread_run(lpfc_do_work, phba,
5491 "lpfc_worker_%d", phba->brd_no);
5492 if (IS_ERR(phba->worker_thread)) {
5493 error = PTR_ERR(phba->worker_thread);
5494 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005495 }
5496
James Smart3772a992009-05-22 14:50:54 -04005497 return 0;
5498}
5499
5500/**
5501 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
5502 * @phba: pointer to lpfc hba data structure.
5503 *
5504 * This routine is invoked to unset the driver internal resources set up after
5505 * the device specific resource setup for supporting the HBA device it
5506 * attached to.
5507 **/
5508static void
5509lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
5510{
5511 /* Stop kernel worker thread */
5512 kthread_stop(phba->worker_thread);
5513}
5514
5515/**
5516 * lpfc_free_iocb_list - Free iocb list.
5517 * @phba: pointer to lpfc hba data structure.
5518 *
5519 * This routine is invoked to free the driver's IOCB list and memory.
5520 **/
5521static void
5522lpfc_free_iocb_list(struct lpfc_hba *phba)
5523{
5524 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
5525
5526 spin_lock_irq(&phba->hbalock);
5527 list_for_each_entry_safe(iocbq_entry, iocbq_next,
5528 &phba->lpfc_iocb_list, list) {
5529 list_del(&iocbq_entry->list);
5530 kfree(iocbq_entry);
5531 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005532 }
James Smart3772a992009-05-22 14:50:54 -04005533 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005534
James Smart3772a992009-05-22 14:50:54 -04005535 return;
5536}
dea31012005-04-17 16:05:31 -05005537
James Smart3772a992009-05-22 14:50:54 -04005538/**
5539 * lpfc_init_iocb_list - Allocate and initialize iocb list.
5540 * @phba: pointer to lpfc hba data structure.
5541 *
5542 * This routine is invoked to allocate and initizlize the driver's IOCB
5543 * list and set up the IOCB tag array accordingly.
5544 *
5545 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005546 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005547 * other values - error
5548 **/
5549static int
5550lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
5551{
5552 struct lpfc_iocbq *iocbq_entry = NULL;
5553 uint16_t iotag;
5554 int i;
dea31012005-04-17 16:05:31 -05005555
5556 /* Initialize and populate the iocb list per host. */
5557 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04005558 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07005559 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05005560 if (iocbq_entry == NULL) {
5561 printk(KERN_ERR "%s: only allocated %d iocbs of "
5562 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07005563 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05005564 goto out_free_iocbq;
5565 }
5566
James Bottomley604a3e32005-10-29 10:28:33 -05005567 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
5568 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04005569 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05005570 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04005571 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05005572 goto out_free_iocbq;
5573 }
James Smart6d368e52011-05-24 11:44:12 -04005574 iocbq_entry->sli4_lxritag = NO_XRI;
James Smart3772a992009-05-22 14:50:54 -04005575 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05005576
5577 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005578 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
5579 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05005580 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005581 }
5582
James Smart3772a992009-05-22 14:50:54 -04005583 return 0;
5584
5585out_free_iocbq:
5586 lpfc_free_iocb_list(phba);
5587
5588 return -ENOMEM;
5589}
5590
5591/**
James Smart8a9d2e82012-05-09 21:16:12 -04005592 * lpfc_free_sgl_list - Free a given sgl list.
James Smartda0436e2009-05-22 14:51:39 -04005593 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -04005594 * @sglq_list: pointer to the head of sgl list.
James Smartda0436e2009-05-22 14:51:39 -04005595 *
James Smart8a9d2e82012-05-09 21:16:12 -04005596 * This routine is invoked to free a give sgl list and memory.
James Smartda0436e2009-05-22 14:51:39 -04005597 **/
James Smart8a9d2e82012-05-09 21:16:12 -04005598void
5599lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
James Smartda0436e2009-05-22 14:51:39 -04005600{
5601 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart8a9d2e82012-05-09 21:16:12 -04005602
5603 list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
5604 list_del(&sglq_entry->list);
5605 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
5606 kfree(sglq_entry);
5607 }
5608}
5609
5610/**
5611 * lpfc_free_els_sgl_list - Free els sgl list.
5612 * @phba: pointer to lpfc hba data structure.
5613 *
5614 * This routine is invoked to free the driver's els sgl list and memory.
5615 **/
5616static void
5617lpfc_free_els_sgl_list(struct lpfc_hba *phba)
5618{
James Smartda0436e2009-05-22 14:51:39 -04005619 LIST_HEAD(sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04005620
James Smart8a9d2e82012-05-09 21:16:12 -04005621 /* Retrieve all els sgls from driver list */
James Smartda0436e2009-05-22 14:51:39 -04005622 spin_lock_irq(&phba->hbalock);
5623 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
5624 spin_unlock_irq(&phba->hbalock);
5625
James Smart8a9d2e82012-05-09 21:16:12 -04005626 /* Now free the sgl list */
5627 lpfc_free_sgl_list(phba, &sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04005628}
5629
5630/**
5631 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
5632 * @phba: pointer to lpfc hba data structure.
5633 *
5634 * This routine is invoked to allocate the driver's active sgl memory.
5635 * This array will hold the sglq_entry's for active IOs.
5636 **/
5637static int
5638lpfc_init_active_sgl_array(struct lpfc_hba *phba)
5639{
5640 int size;
5641 size = sizeof(struct lpfc_sglq *);
5642 size *= phba->sli4_hba.max_cfg_param.max_xri;
5643
5644 phba->sli4_hba.lpfc_sglq_active_list =
5645 kzalloc(size, GFP_KERNEL);
5646 if (!phba->sli4_hba.lpfc_sglq_active_list)
5647 return -ENOMEM;
5648 return 0;
5649}
5650
5651/**
5652 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
5653 * @phba: pointer to lpfc hba data structure.
5654 *
5655 * This routine is invoked to walk through the array of active sglq entries
5656 * and free all of the resources.
5657 * This is just a place holder for now.
5658 **/
5659static void
5660lpfc_free_active_sgl(struct lpfc_hba *phba)
5661{
5662 kfree(phba->sli4_hba.lpfc_sglq_active_list);
5663}
5664
5665/**
5666 * lpfc_init_sgl_list - Allocate and initialize sgl list.
5667 * @phba: pointer to lpfc hba data structure.
5668 *
5669 * This routine is invoked to allocate and initizlize the driver's sgl
5670 * list and set up the sgl xritag tag array accordingly.
5671 *
James Smartda0436e2009-05-22 14:51:39 -04005672 **/
James Smart8a9d2e82012-05-09 21:16:12 -04005673static void
James Smartda0436e2009-05-22 14:51:39 -04005674lpfc_init_sgl_list(struct lpfc_hba *phba)
5675{
James Smartda0436e2009-05-22 14:51:39 -04005676 /* Initialize and populate the sglq list per host/VF. */
5677 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
5678 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
5679
James Smart8a9d2e82012-05-09 21:16:12 -04005680 /* els xri-sgl book keeping */
5681 phba->sli4_hba.els_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04005682
James Smart8a9d2e82012-05-09 21:16:12 -04005683 /* scsi xri-buffer book keeping */
James Smartda0436e2009-05-22 14:51:39 -04005684 phba->sli4_hba.scsi_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04005685}
5686
5687/**
5688 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
5689 * @phba: pointer to lpfc hba data structure.
5690 *
5691 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -04005692 * port for those SLI4 ports that do not support extents. This routine
James Smartda0436e2009-05-22 14:51:39 -04005693 * posts a PAGE_SIZE memory region to the port to hold up to
James Smart88a2cfb2011-07-22 18:36:33 -04005694 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
5695 * and should be called only when interrupts are disabled.
James Smartda0436e2009-05-22 14:51:39 -04005696 *
5697 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005698 * 0 - successful
James Smart88a2cfb2011-07-22 18:36:33 -04005699 * -ERROR - otherwise.
James Smartda0436e2009-05-22 14:51:39 -04005700 **/
5701int
5702lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
5703{
5704 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04005705 struct lpfc_rpi_hdr *rpi_hdr;
5706
5707 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
James Smartff78d8f2011-12-13 13:21:35 -05005708 if (!phba->sli4_hba.rpi_hdrs_in_use)
James Smart6d368e52011-05-24 11:44:12 -04005709 return rc;
James Smart6d368e52011-05-24 11:44:12 -04005710 if (phba->sli4_hba.extents_in_use)
5711 return -EIO;
James Smartda0436e2009-05-22 14:51:39 -04005712
5713 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
5714 if (!rpi_hdr) {
5715 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5716 "0391 Error during rpi post operation\n");
5717 lpfc_sli4_remove_rpis(phba);
5718 rc = -ENODEV;
5719 }
5720
5721 return rc;
5722}
5723
5724/**
5725 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
5726 * @phba: pointer to lpfc hba data structure.
5727 *
5728 * This routine is invoked to allocate a single 4KB memory region to
5729 * support rpis and stores them in the phba. This single region
5730 * provides support for up to 64 rpis. The region is used globally
5731 * by the device.
5732 *
5733 * Returns:
5734 * A valid rpi hdr on success.
5735 * A NULL pointer on any failure.
5736 **/
5737struct lpfc_rpi_hdr *
5738lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
5739{
5740 uint16_t rpi_limit, curr_rpi_range;
5741 struct lpfc_dmabuf *dmabuf;
5742 struct lpfc_rpi_hdr *rpi_hdr;
James Smart9589b0622011-04-16 11:03:17 -04005743 uint32_t rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04005744
James Smart6d368e52011-05-24 11:44:12 -04005745 /*
5746 * If the SLI4 port supports extents, posting the rpi header isn't
5747 * required. Set the expected maximum count and let the actual value
5748 * get set when extents are fully allocated.
5749 */
5750 if (!phba->sli4_hba.rpi_hdrs_in_use)
5751 return NULL;
5752 if (phba->sli4_hba.extents_in_use)
5753 return NULL;
5754
5755 /* The limit on the logical index is just the max_rpi count. */
James Smartda0436e2009-05-22 14:51:39 -04005756 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
James Smart6d368e52011-05-24 11:44:12 -04005757 phba->sli4_hba.max_cfg_param.max_rpi - 1;
James Smartda0436e2009-05-22 14:51:39 -04005758
5759 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04005760 /*
5761 * Establish the starting RPI in this header block. The starting
5762 * rpi is normalized to a zero base because the physical rpi is
5763 * port based.
5764 */
James Smart97f2ecf2012-03-01 22:35:23 -05005765 curr_rpi_range = phba->sli4_hba.next_rpi;
James Smartda0436e2009-05-22 14:51:39 -04005766 spin_unlock_irq(&phba->hbalock);
5767
5768 /*
5769 * The port has a limited number of rpis. The increment here
5770 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
5771 * and to allow the full max_rpi range per port.
5772 */
5773 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
James Smart9589b0622011-04-16 11:03:17 -04005774 rpi_count = rpi_limit - curr_rpi_range;
5775 else
5776 rpi_count = LPFC_RPI_HDR_COUNT;
James Smartda0436e2009-05-22 14:51:39 -04005777
James Smart6d368e52011-05-24 11:44:12 -04005778 if (!rpi_count)
5779 return NULL;
James Smartda0436e2009-05-22 14:51:39 -04005780 /*
5781 * First allocate the protocol header region for the port. The
5782 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
5783 */
5784 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5785 if (!dmabuf)
5786 return NULL;
5787
5788 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5789 LPFC_HDR_TEMPLATE_SIZE,
5790 &dmabuf->phys,
5791 GFP_KERNEL);
5792 if (!dmabuf->virt) {
5793 rpi_hdr = NULL;
5794 goto err_free_dmabuf;
5795 }
5796
5797 memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
5798 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
5799 rpi_hdr = NULL;
5800 goto err_free_coherent;
5801 }
5802
5803 /* Save the rpi header data for cleanup later. */
5804 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
5805 if (!rpi_hdr)
5806 goto err_free_coherent;
5807
5808 rpi_hdr->dmabuf = dmabuf;
5809 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
5810 rpi_hdr->page_count = 1;
5811 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04005812
5813 /* The rpi_hdr stores the logical index only. */
5814 rpi_hdr->start_rpi = curr_rpi_range;
James Smartda0436e2009-05-22 14:51:39 -04005815 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
5816
5817 /*
James Smart6d368e52011-05-24 11:44:12 -04005818 * The next_rpi stores the next logical module-64 rpi value used
5819 * to post physical rpis in subsequent rpi postings.
James Smartda0436e2009-05-22 14:51:39 -04005820 */
James Smart9589b0622011-04-16 11:03:17 -04005821 phba->sli4_hba.next_rpi += rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04005822 spin_unlock_irq(&phba->hbalock);
5823 return rpi_hdr;
5824
5825 err_free_coherent:
5826 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
5827 dmabuf->virt, dmabuf->phys);
5828 err_free_dmabuf:
5829 kfree(dmabuf);
5830 return NULL;
5831}
5832
5833/**
5834 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
5835 * @phba: pointer to lpfc hba data structure.
5836 *
5837 * This routine is invoked to remove all memory resources allocated
James Smart6d368e52011-05-24 11:44:12 -04005838 * to support rpis for SLI4 ports not supporting extents. This routine
5839 * presumes the caller has released all rpis consumed by fabric or port
5840 * logins and is prepared to have the header pages removed.
James Smartda0436e2009-05-22 14:51:39 -04005841 **/
5842void
5843lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
5844{
5845 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
5846
James Smart6d368e52011-05-24 11:44:12 -04005847 if (!phba->sli4_hba.rpi_hdrs_in_use)
5848 goto exit;
5849
James Smartda0436e2009-05-22 14:51:39 -04005850 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
5851 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
5852 list_del(&rpi_hdr->list);
5853 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
5854 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
5855 kfree(rpi_hdr->dmabuf);
5856 kfree(rpi_hdr);
5857 }
James Smart6d368e52011-05-24 11:44:12 -04005858 exit:
5859 /* There are no rpis available to the port now. */
5860 phba->sli4_hba.next_rpi = 0;
James Smartda0436e2009-05-22 14:51:39 -04005861}
5862
5863/**
James Smart3772a992009-05-22 14:50:54 -04005864 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
5865 * @pdev: pointer to pci device data structure.
5866 *
5867 * This routine is invoked to allocate the driver hba data structure for an
5868 * HBA device. If the allocation is successful, the phba reference to the
5869 * PCI device data structure is set.
5870 *
5871 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005872 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04005873 * NULL - error
5874 **/
5875static struct lpfc_hba *
5876lpfc_hba_alloc(struct pci_dev *pdev)
5877{
5878 struct lpfc_hba *phba;
5879
5880 /* Allocate memory for HBA structure */
5881 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
5882 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02005883 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04005884 return NULL;
5885 }
5886
5887 /* Set reference to PCI device in HBA structure */
5888 phba->pcidev = pdev;
5889
5890 /* Assign an unused board number */
5891 phba->brd_no = lpfc_get_instance();
5892 if (phba->brd_no < 0) {
5893 kfree(phba);
5894 return NULL;
5895 }
5896
James Smart4fede782010-01-26 23:08:55 -05005897 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04005898 INIT_LIST_HEAD(&phba->ct_ev_waiters);
5899
James Smart3772a992009-05-22 14:50:54 -04005900 return phba;
5901}
5902
5903/**
5904 * lpfc_hba_free - Free driver hba data structure with a device.
5905 * @phba: pointer to lpfc hba data structure.
5906 *
5907 * This routine is invoked to free the driver hba data structure with an
5908 * HBA device.
5909 **/
5910static void
5911lpfc_hba_free(struct lpfc_hba *phba)
5912{
5913 /* Release the driver assigned board number */
5914 idr_remove(&lpfc_hba_index, phba->brd_no);
5915
James Smart2a76a282012-08-03 12:35:54 -04005916 /* Free memory allocated with sli rings */
5917 kfree(phba->sli.ring);
5918 phba->sli.ring = NULL;
5919
James Smart3772a992009-05-22 14:50:54 -04005920 kfree(phba);
5921 return;
5922}
5923
5924/**
5925 * lpfc_create_shost - Create hba physical port with associated scsi host.
5926 * @phba: pointer to lpfc hba data structure.
5927 *
5928 * This routine is invoked to create HBA physical port and associate a SCSI
5929 * host with it.
5930 *
5931 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005932 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005933 * other values - error
5934 **/
5935static int
5936lpfc_create_shost(struct lpfc_hba *phba)
5937{
5938 struct lpfc_vport *vport;
5939 struct Scsi_Host *shost;
5940
5941 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05005942 phba->fc_edtov = FF_DEF_EDTOV;
5943 phba->fc_ratov = FF_DEF_RATOV;
5944 phba->fc_altov = FF_DEF_ALTOV;
5945 phba->fc_arbtov = FF_DEF_ARBTOV;
5946
James Smartd7c47992010-06-08 18:31:54 -04005947 atomic_set(&phba->sdev_cnt, 0);
James Smart3de2a652007-08-02 11:09:59 -04005948 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05005949 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04005950 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05005951
5952 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05005953 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05005954 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04005955 /* Put reference to SCSI host to driver's device private data */
5956 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05005957
James Smart3772a992009-05-22 14:50:54 -04005958 return 0;
5959}
dea31012005-04-17 16:05:31 -05005960
James Smart3772a992009-05-22 14:50:54 -04005961/**
5962 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
5963 * @phba: pointer to lpfc hba data structure.
5964 *
5965 * This routine is invoked to destroy HBA physical port and the associated
5966 * SCSI host.
5967 **/
5968static void
5969lpfc_destroy_shost(struct lpfc_hba *phba)
5970{
5971 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04005972
James Smart3772a992009-05-22 14:50:54 -04005973 /* Destroy physical port that associated with the SCSI host */
5974 destroy_port(vport);
5975
5976 return;
5977}
5978
5979/**
5980 * lpfc_setup_bg - Setup Block guard structures and debug areas.
5981 * @phba: pointer to lpfc hba data structure.
5982 * @shost: the shost to be used to detect Block guard settings.
5983 *
5984 * This routine sets up the local Block guard protocol settings for @shost.
5985 * This routine also allocates memory for debugging bg buffers.
5986 **/
5987static void
5988lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
5989{
James Smartbbeb79b2012-06-12 13:54:27 -04005990 uint32_t old_mask;
5991 uint32_t old_guard;
5992
James Smart3772a992009-05-22 14:50:54 -04005993 int pagecnt = 10;
5994 if (lpfc_prot_mask && lpfc_prot_guard) {
5995 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5996 "1478 Registering BlockGuard with the "
5997 "SCSI layer\n");
James Smartbbeb79b2012-06-12 13:54:27 -04005998
5999 old_mask = lpfc_prot_mask;
6000 old_guard = lpfc_prot_guard;
6001
6002 /* Only allow supported values */
6003 lpfc_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
6004 SHOST_DIX_TYPE0_PROTECTION |
6005 SHOST_DIX_TYPE1_PROTECTION);
6006 lpfc_prot_guard &= (SHOST_DIX_GUARD_IP | SHOST_DIX_GUARD_CRC);
6007
6008 /* DIF Type 1 protection for profiles AST1/C1 is end to end */
6009 if (lpfc_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
6010 lpfc_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
6011
6012 if (lpfc_prot_mask && lpfc_prot_guard) {
6013 if ((old_mask != lpfc_prot_mask) ||
6014 (old_guard != lpfc_prot_guard))
6015 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6016 "1475 Registering BlockGuard with the "
6017 "SCSI layer: mask %d guard %d\n",
6018 lpfc_prot_mask, lpfc_prot_guard);
6019
6020 scsi_host_set_prot(shost, lpfc_prot_mask);
6021 scsi_host_set_guard(shost, lpfc_prot_guard);
6022 } else
6023 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6024 "1479 Not Registering BlockGuard with the SCSI "
6025 "layer, Bad protection parameters: %d %d\n",
6026 old_mask, old_guard);
James Smart98c9ea52007-10-27 13:37:33 -04006027 }
James Smartbbeb79b2012-06-12 13:54:27 -04006028
James Smart81301a92008-12-04 22:39:46 -05006029 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05006030 while (pagecnt) {
6031 spin_lock_init(&_dump_buf_lock);
6032 _dump_buf_data =
6033 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
6034 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04006035 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6036 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04006037 "_dump_buf_data at 0x%p\n",
6038 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05006039 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04006040 memset(_dump_buf_data, 0,
6041 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05006042 break;
James Smart3772a992009-05-22 14:50:54 -04006043 } else
James Smart81301a92008-12-04 22:39:46 -05006044 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05006045 }
James Smart81301a92008-12-04 22:39:46 -05006046 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04006047 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6048 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04006049 "memory for hexdump\n");
6050 } else
James Smart6a9c52c2009-10-02 15:16:51 -04006051 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6052 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05006053 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05006054 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05006055 while (pagecnt) {
6056 _dump_buf_dif =
6057 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
6058 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04006059 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6060 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04006061 "_dump_buf_dif at 0x%p\n",
6062 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05006063 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04006064 memset(_dump_buf_dif, 0,
6065 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05006066 break;
James Smart3772a992009-05-22 14:50:54 -04006067 } else
James Smart81301a92008-12-04 22:39:46 -05006068 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05006069 }
James Smart81301a92008-12-04 22:39:46 -05006070 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04006071 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6072 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04006073 "memory for hexdump\n");
6074 } else
James Smart6a9c52c2009-10-02 15:16:51 -04006075 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6076 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04006077 _dump_buf_dif);
6078}
6079
6080/**
6081 * lpfc_post_init_setup - Perform necessary device post initialization setup.
6082 * @phba: pointer to lpfc hba data structure.
6083 *
6084 * This routine is invoked to perform all the necessary post initialization
6085 * setup for the device.
6086 **/
6087static void
6088lpfc_post_init_setup(struct lpfc_hba *phba)
6089{
6090 struct Scsi_Host *shost;
6091 struct lpfc_adapter_event_header adapter_event;
6092
6093 /* Get the default values for Model Name and Description */
6094 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
6095
6096 /*
6097 * hba setup may have changed the hba_queue_depth so we need to
6098 * adjust the value of can_queue.
6099 */
6100 shost = pci_get_drvdata(phba->pcidev);
6101 shost->can_queue = phba->cfg_hba_queue_depth - 10;
6102 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
6103 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05006104
6105 lpfc_host_attrib_init(shost);
6106
James Smart2e0fef82007-06-17 19:56:36 -05006107 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
6108 spin_lock_irq(shost->host_lock);
6109 lpfc_poll_start_timer(phba);
6110 spin_unlock_irq(shost->host_lock);
6111 }
James Smart8f6d98d2006-08-01 07:34:00 -04006112
James Smart93996272008-08-24 21:50:30 -04006113 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6114 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04006115 /* Send board arrival event to upper layer */
6116 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
6117 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
6118 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04006119 sizeof(adapter_event),
6120 (char *) &adapter_event,
6121 LPFC_NL_VENDOR_ID);
6122 return;
6123}
6124
6125/**
6126 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
6127 * @phba: pointer to lpfc hba data structure.
6128 *
6129 * This routine is invoked to set up the PCI device memory space for device
6130 * with SLI-3 interface spec.
6131 *
6132 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006133 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006134 * other values - error
6135 **/
6136static int
6137lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
6138{
6139 struct pci_dev *pdev;
6140 unsigned long bar0map_len, bar2map_len;
6141 int i, hbq_count;
6142 void *ptr;
6143 int error = -ENODEV;
6144
6145 /* Obtain PCI device reference */
6146 if (!phba->pcidev)
6147 return error;
6148 else
6149 pdev = phba->pcidev;
6150
6151 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05006152 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
6153 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
6154 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
6155 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smart3772a992009-05-22 14:50:54 -04006156 return error;
Michael Reed8e685972009-09-18 12:02:05 -05006157 }
6158 }
James Smart3772a992009-05-22 14:50:54 -04006159
6160 /* Get the bus address of Bar0 and Bar2 and the number of bytes
6161 * required by each mapping.
6162 */
6163 phba->pci_bar0_map = pci_resource_start(pdev, 0);
6164 bar0map_len = pci_resource_len(pdev, 0);
6165
6166 phba->pci_bar2_map = pci_resource_start(pdev, 2);
6167 bar2map_len = pci_resource_len(pdev, 2);
6168
6169 /* Map HBA SLIM to a kernel virtual address. */
6170 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
6171 if (!phba->slim_memmap_p) {
6172 dev_printk(KERN_ERR, &pdev->dev,
6173 "ioremap failed for SLIM memory.\n");
6174 goto out;
6175 }
6176
6177 /* Map HBA Control Registers to a kernel virtual address. */
6178 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
6179 if (!phba->ctrl_regs_memmap_p) {
6180 dev_printk(KERN_ERR, &pdev->dev,
6181 "ioremap failed for HBA control registers.\n");
6182 goto out_iounmap_slim;
6183 }
6184
6185 /* Allocate memory for SLI-2 structures */
6186 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
6187 SLI2_SLIM_SIZE,
6188 &phba->slim2p.phys,
6189 GFP_KERNEL);
6190 if (!phba->slim2p.virt)
6191 goto out_iounmap;
6192
6193 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
6194 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04006195 phba->mbox_ext = (phba->slim2p.virt +
6196 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04006197 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
6198 phba->IOCBs = (phba->slim2p.virt +
6199 offsetof(struct lpfc_sli2_slim, IOCBs));
6200
6201 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
6202 lpfc_sli_hbq_size(),
6203 &phba->hbqslimp.phys,
6204 GFP_KERNEL);
6205 if (!phba->hbqslimp.virt)
6206 goto out_free_slim;
6207
6208 hbq_count = lpfc_sli_hbq_count();
6209 ptr = phba->hbqslimp.virt;
6210 for (i = 0; i < hbq_count; ++i) {
6211 phba->hbqs[i].hbq_virt = ptr;
6212 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
6213 ptr += (lpfc_hbq_defs[i]->entry_count *
6214 sizeof(struct lpfc_hbq_entry));
6215 }
6216 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
6217 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
6218
6219 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
6220
6221 INIT_LIST_HEAD(&phba->rb_pend_list);
6222
6223 phba->MBslimaddr = phba->slim_memmap_p;
6224 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
6225 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
6226 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
6227 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04006228
dea31012005-04-17 16:05:31 -05006229 return 0;
6230
dea31012005-04-17 16:05:31 -05006231out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04006232 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6233 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05006234out_iounmap:
6235 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05006236out_iounmap_slim:
dea31012005-04-17 16:05:31 -05006237 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05006238out:
6239 return error;
6240}
6241
James Smarte59058c2008-08-24 21:49:00 -04006242/**
James Smart3772a992009-05-22 14:50:54 -04006243 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
6244 * @phba: pointer to lpfc hba data structure.
6245 *
6246 * This routine is invoked to unset the PCI device memory space for device
6247 * with SLI-3 interface spec.
6248 **/
6249static void
6250lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
6251{
6252 struct pci_dev *pdev;
6253
6254 /* Obtain PCI device reference */
6255 if (!phba->pcidev)
6256 return;
6257 else
6258 pdev = phba->pcidev;
6259
6260 /* Free coherent DMA memory allocated */
6261 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
6262 phba->hbqslimp.virt, phba->hbqslimp.phys);
6263 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6264 phba->slim2p.virt, phba->slim2p.phys);
6265
6266 /* I/O memory unmap */
6267 iounmap(phba->ctrl_regs_memmap_p);
6268 iounmap(phba->slim_memmap_p);
6269
6270 return;
6271}
6272
6273/**
James Smartda0436e2009-05-22 14:51:39 -04006274 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
6275 * @phba: pointer to lpfc hba data structure.
6276 *
6277 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
6278 * done and check status.
6279 *
6280 * Return 0 if successful, otherwise -ENODEV.
6281 **/
6282int
6283lpfc_sli4_post_status_check(struct lpfc_hba *phba)
6284{
James Smart2fcee4b2010-12-15 17:57:46 -05006285 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
6286 struct lpfc_register reg_data;
6287 int i, port_error = 0;
6288 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04006289
James Smart9940b972011-03-11 16:06:12 -05006290 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
6291 memset(&reg_data, 0, sizeof(reg_data));
James Smart2fcee4b2010-12-15 17:57:46 -05006292 if (!phba->sli4_hba.PSMPHRregaddr)
James Smartda0436e2009-05-22 14:51:39 -04006293 return -ENODEV;
6294
James Smartda0436e2009-05-22 14:51:39 -04006295 /* Wait up to 30 seconds for the SLI Port POST done and ready */
6296 for (i = 0; i < 3000; i++) {
James Smart9940b972011-03-11 16:06:12 -05006297 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
6298 &portsmphr_reg.word0) ||
6299 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
James Smart2fcee4b2010-12-15 17:57:46 -05006300 /* Port has a fatal POST error, break out */
James Smartda0436e2009-05-22 14:51:39 -04006301 port_error = -ENODEV;
6302 break;
6303 }
James Smart2fcee4b2010-12-15 17:57:46 -05006304 if (LPFC_POST_STAGE_PORT_READY ==
6305 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
James Smartda0436e2009-05-22 14:51:39 -04006306 break;
James Smartda0436e2009-05-22 14:51:39 -04006307 msleep(10);
6308 }
6309
James Smart2fcee4b2010-12-15 17:57:46 -05006310 /*
6311 * If there was a port error during POST, then don't proceed with
6312 * other register reads as the data may not be valid. Just exit.
6313 */
6314 if (port_error) {
James Smartda0436e2009-05-22 14:51:39 -04006315 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05006316 "1408 Port Failed POST - portsmphr=0x%x, "
6317 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
6318 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
6319 portsmphr_reg.word0,
6320 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
6321 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
6322 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
6323 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
6324 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
6325 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
6326 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
6327 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
6328 } else {
James Smart28baac72010-02-12 14:42:03 -05006329 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05006330 "2534 Device Info: SLIFamily=0x%x, "
6331 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
6332 "SLIHint_2=0x%x, FT=0x%x\n",
James Smart28baac72010-02-12 14:42:03 -05006333 bf_get(lpfc_sli_intf_sli_family,
6334 &phba->sli4_hba.sli_intf),
6335 bf_get(lpfc_sli_intf_slirev,
6336 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05006337 bf_get(lpfc_sli_intf_if_type,
James Smart28baac72010-02-12 14:42:03 -05006338 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05006339 bf_get(lpfc_sli_intf_sli_hint1,
6340 &phba->sli4_hba.sli_intf),
6341 bf_get(lpfc_sli_intf_sli_hint2,
6342 &phba->sli4_hba.sli_intf),
6343 bf_get(lpfc_sli_intf_func_type,
James Smart28baac72010-02-12 14:42:03 -05006344 &phba->sli4_hba.sli_intf));
James Smart2fcee4b2010-12-15 17:57:46 -05006345 /*
6346 * Check for other Port errors during the initialization
6347 * process. Fail the load if the port did not come up
6348 * correctly.
6349 */
6350 if_type = bf_get(lpfc_sli_intf_if_type,
6351 &phba->sli4_hba.sli_intf);
6352 switch (if_type) {
6353 case LPFC_SLI_INTF_IF_TYPE_0:
6354 phba->sli4_hba.ue_mask_lo =
6355 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
6356 phba->sli4_hba.ue_mask_hi =
6357 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
6358 uerrlo_reg.word0 =
6359 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
6360 uerrhi_reg.word0 =
6361 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
6362 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
6363 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
6364 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6365 "1422 Unrecoverable Error "
6366 "Detected during POST "
6367 "uerr_lo_reg=0x%x, "
6368 "uerr_hi_reg=0x%x, "
6369 "ue_mask_lo_reg=0x%x, "
6370 "ue_mask_hi_reg=0x%x\n",
6371 uerrlo_reg.word0,
6372 uerrhi_reg.word0,
6373 phba->sli4_hba.ue_mask_lo,
6374 phba->sli4_hba.ue_mask_hi);
6375 port_error = -ENODEV;
6376 }
6377 break;
6378 case LPFC_SLI_INTF_IF_TYPE_2:
6379 /* Final checks. The port status should be clean. */
James Smart9940b972011-03-11 16:06:12 -05006380 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
6381 &reg_data.word0) ||
James Smart05580562011-05-24 11:40:48 -04006382 (bf_get(lpfc_sliport_status_err, &reg_data) &&
6383 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
James Smart2fcee4b2010-12-15 17:57:46 -05006384 phba->work_status[0] =
6385 readl(phba->sli4_hba.u.if_type2.
6386 ERR1regaddr);
6387 phba->work_status[1] =
6388 readl(phba->sli4_hba.u.if_type2.
6389 ERR2regaddr);
6390 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05006391 "2888 Unrecoverable port error "
6392 "following POST: port status reg "
6393 "0x%x, port_smphr reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05006394 "error 1=0x%x, error 2=0x%x\n",
6395 reg_data.word0,
6396 portsmphr_reg.word0,
6397 phba->work_status[0],
6398 phba->work_status[1]);
6399 port_error = -ENODEV;
6400 }
6401 break;
6402 case LPFC_SLI_INTF_IF_TYPE_1:
6403 default:
6404 break;
6405 }
James Smart28baac72010-02-12 14:42:03 -05006406 }
James Smartda0436e2009-05-22 14:51:39 -04006407 return port_error;
6408}
6409
6410/**
6411 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
6412 * @phba: pointer to lpfc hba data structure.
James Smart2fcee4b2010-12-15 17:57:46 -05006413 * @if_type: The SLI4 interface type getting configured.
James Smartda0436e2009-05-22 14:51:39 -04006414 *
6415 * This routine is invoked to set up SLI4 BAR0 PCI config space register
6416 * memory map.
6417 **/
6418static void
James Smart2fcee4b2010-12-15 17:57:46 -05006419lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04006420{
James Smart2fcee4b2010-12-15 17:57:46 -05006421 switch (if_type) {
6422 case LPFC_SLI_INTF_IF_TYPE_0:
6423 phba->sli4_hba.u.if_type0.UERRLOregaddr =
6424 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
6425 phba->sli4_hba.u.if_type0.UERRHIregaddr =
6426 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
6427 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
6428 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
6429 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
6430 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
6431 phba->sli4_hba.SLIINTFregaddr =
6432 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
6433 break;
6434 case LPFC_SLI_INTF_IF_TYPE_2:
6435 phba->sli4_hba.u.if_type2.ERR1regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006436 phba->sli4_hba.conf_regs_memmap_p +
6437 LPFC_CTL_PORT_ER1_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006438 phba->sli4_hba.u.if_type2.ERR2regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006439 phba->sli4_hba.conf_regs_memmap_p +
6440 LPFC_CTL_PORT_ER2_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006441 phba->sli4_hba.u.if_type2.CTRLregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006442 phba->sli4_hba.conf_regs_memmap_p +
6443 LPFC_CTL_PORT_CTL_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006444 phba->sli4_hba.u.if_type2.STATUSregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006445 phba->sli4_hba.conf_regs_memmap_p +
6446 LPFC_CTL_PORT_STA_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006447 phba->sli4_hba.SLIINTFregaddr =
6448 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
6449 phba->sli4_hba.PSMPHRregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006450 phba->sli4_hba.conf_regs_memmap_p +
6451 LPFC_CTL_PORT_SEM_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006452 phba->sli4_hba.RQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05006453 phba->sli4_hba.conf_regs_memmap_p +
6454 LPFC_ULP0_RQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05006455 phba->sli4_hba.WQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05006456 phba->sli4_hba.conf_regs_memmap_p +
6457 LPFC_ULP0_WQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05006458 phba->sli4_hba.EQCQDBregaddr =
6459 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
6460 phba->sli4_hba.MQDBregaddr =
6461 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
6462 phba->sli4_hba.BMBXregaddr =
6463 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
6464 break;
6465 case LPFC_SLI_INTF_IF_TYPE_1:
6466 default:
6467 dev_printk(KERN_ERR, &phba->pcidev->dev,
6468 "FATAL - unsupported SLI4 interface type - %d\n",
6469 if_type);
6470 break;
6471 }
James Smartda0436e2009-05-22 14:51:39 -04006472}
6473
6474/**
6475 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
6476 * @phba: pointer to lpfc hba data structure.
6477 *
6478 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
6479 * memory map.
6480 **/
6481static void
6482lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
6483{
James Smart2fcee4b2010-12-15 17:57:46 -05006484 phba->sli4_hba.PSMPHRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
6485 LPFC_SLIPORT_IF0_SMPHR;
James Smartda0436e2009-05-22 14:51:39 -04006486 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006487 LPFC_HST_ISR0;
James Smartda0436e2009-05-22 14:51:39 -04006488 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006489 LPFC_HST_IMR0;
James Smartda0436e2009-05-22 14:51:39 -04006490 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006491 LPFC_HST_ISCR0;
James Smartda0436e2009-05-22 14:51:39 -04006492}
6493
6494/**
6495 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
6496 * @phba: pointer to lpfc hba data structure.
6497 * @vf: virtual function number
6498 *
6499 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
6500 * based on the given viftual function number, @vf.
6501 *
6502 * Return 0 if successful, otherwise -ENODEV.
6503 **/
6504static int
6505lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
6506{
6507 if (vf > LPFC_VIR_FUNC_MAX)
6508 return -ENODEV;
6509
6510 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05006511 vf * LPFC_VFR_PAGE_SIZE +
6512 LPFC_ULP0_RQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04006513 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05006514 vf * LPFC_VFR_PAGE_SIZE +
6515 LPFC_ULP0_WQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04006516 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6517 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
6518 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6519 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
6520 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6521 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
6522 return 0;
6523}
6524
6525/**
6526 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
6527 * @phba: pointer to lpfc hba data structure.
6528 *
6529 * This routine is invoked to create the bootstrap mailbox
6530 * region consistent with the SLI-4 interface spec. This
6531 * routine allocates all memory necessary to communicate
6532 * mailbox commands to the port and sets up all alignment
6533 * needs. No locks are expected to be held when calling
6534 * this routine.
6535 *
6536 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006537 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04006538 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04006539 **/
6540static int
6541lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
6542{
6543 uint32_t bmbx_size;
6544 struct lpfc_dmabuf *dmabuf;
6545 struct dma_address *dma_address;
6546 uint32_t pa_addr;
6547 uint64_t phys_addr;
6548
6549 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
6550 if (!dmabuf)
6551 return -ENOMEM;
6552
6553 /*
6554 * The bootstrap mailbox region is comprised of 2 parts
6555 * plus an alignment restriction of 16 bytes.
6556 */
6557 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
6558 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
6559 bmbx_size,
6560 &dmabuf->phys,
6561 GFP_KERNEL);
6562 if (!dmabuf->virt) {
6563 kfree(dmabuf);
6564 return -ENOMEM;
6565 }
6566 memset(dmabuf->virt, 0, bmbx_size);
6567
6568 /*
6569 * Initialize the bootstrap mailbox pointers now so that the register
6570 * operations are simple later. The mailbox dma address is required
6571 * to be 16-byte aligned. Also align the virtual memory as each
6572 * maibox is copied into the bmbx mailbox region before issuing the
6573 * command to the port.
6574 */
6575 phba->sli4_hba.bmbx.dmabuf = dmabuf;
6576 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
6577
6578 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
6579 LPFC_ALIGN_16_BYTE);
6580 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
6581 LPFC_ALIGN_16_BYTE);
6582
6583 /*
6584 * Set the high and low physical addresses now. The SLI4 alignment
6585 * requirement is 16 bytes and the mailbox is posted to the port
6586 * as two 30-bit addresses. The other data is a bit marking whether
6587 * the 30-bit address is the high or low address.
6588 * Upcast bmbx aphys to 64bits so shift instruction compiles
6589 * clean on 32 bit machines.
6590 */
6591 dma_address = &phba->sli4_hba.bmbx.dma_address;
6592 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
6593 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
6594 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
6595 LPFC_BMBX_BIT1_ADDR_HI);
6596
6597 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
6598 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
6599 LPFC_BMBX_BIT1_ADDR_LO);
6600 return 0;
6601}
6602
6603/**
6604 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
6605 * @phba: pointer to lpfc hba data structure.
6606 *
6607 * This routine is invoked to teardown the bootstrap mailbox
6608 * region and release all host resources. This routine requires
6609 * the caller to ensure all mailbox commands recovered, no
6610 * additional mailbox comands are sent, and interrupts are disabled
6611 * before calling this routine.
6612 *
6613 **/
6614static void
6615lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
6616{
6617 dma_free_coherent(&phba->pcidev->dev,
6618 phba->sli4_hba.bmbx.bmbx_size,
6619 phba->sli4_hba.bmbx.dmabuf->virt,
6620 phba->sli4_hba.bmbx.dmabuf->phys);
6621
6622 kfree(phba->sli4_hba.bmbx.dmabuf);
6623 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
6624}
6625
6626/**
6627 * lpfc_sli4_read_config - Get the config parameters.
6628 * @phba: pointer to lpfc hba data structure.
6629 *
6630 * This routine is invoked to read the configuration parameters from the HBA.
6631 * The configuration parameters are used to set the base and maximum values
6632 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
6633 * allocation for the port.
6634 *
6635 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006636 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006637 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006638 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006639 **/
James Smartff78d8f2011-12-13 13:21:35 -05006640int
James Smartda0436e2009-05-22 14:51:39 -04006641lpfc_sli4_read_config(struct lpfc_hba *phba)
6642{
6643 LPFC_MBOXQ_t *pmb;
6644 struct lpfc_mbx_read_config *rd_config;
James Smart912e3ac2011-05-24 11:42:11 -04006645 union lpfc_sli4_cfg_shdr *shdr;
6646 uint32_t shdr_status, shdr_add_status;
6647 struct lpfc_mbx_get_func_cfg *get_func_cfg;
6648 struct lpfc_rsrc_desc_fcfcoe *desc;
James Smart8aa134a2012-08-14 14:25:29 -04006649 char *pdesc_0;
James Smart912e3ac2011-05-24 11:42:11 -04006650 uint32_t desc_count;
James Smart8aa134a2012-08-14 14:25:29 -04006651 int length, i, rc = 0, rc2;
James Smartda0436e2009-05-22 14:51:39 -04006652
6653 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6654 if (!pmb) {
6655 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6656 "2011 Unable to allocate memory for issuing "
6657 "SLI_CONFIG_SPECIAL mailbox command\n");
6658 return -ENOMEM;
6659 }
6660
6661 lpfc_read_config(phba, pmb);
6662
6663 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6664 if (rc != MBX_SUCCESS) {
6665 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6666 "2012 Mailbox failed , mbxCmd x%x "
6667 "READ_CONFIG, mbxStatus x%x\n",
6668 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6669 bf_get(lpfc_mqe_status, &pmb->u.mqe));
6670 rc = -EIO;
6671 } else {
6672 rd_config = &pmb->u.mqe.un.rd_config;
James Smartff78d8f2011-12-13 13:21:35 -05006673 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
6674 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
6675 phba->sli4_hba.lnk_info.lnk_tp =
6676 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
6677 phba->sli4_hba.lnk_info.lnk_no =
6678 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
6679 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6680 "3081 lnk_type:%d, lnk_numb:%d\n",
6681 phba->sli4_hba.lnk_info.lnk_tp,
6682 phba->sli4_hba.lnk_info.lnk_no);
6683 } else
6684 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6685 "3082 Mailbox (x%x) returned ldv:x0\n",
6686 bf_get(lpfc_mqe_command, &pmb->u.mqe));
James Smart6d368e52011-05-24 11:44:12 -04006687 phba->sli4_hba.extents_in_use =
6688 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04006689 phba->sli4_hba.max_cfg_param.max_xri =
6690 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
6691 phba->sli4_hba.max_cfg_param.xri_base =
6692 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
6693 phba->sli4_hba.max_cfg_param.max_vpi =
6694 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
6695 phba->sli4_hba.max_cfg_param.vpi_base =
6696 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
6697 phba->sli4_hba.max_cfg_param.max_rpi =
6698 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
6699 phba->sli4_hba.max_cfg_param.rpi_base =
6700 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
6701 phba->sli4_hba.max_cfg_param.max_vfi =
6702 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
6703 phba->sli4_hba.max_cfg_param.vfi_base =
6704 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
6705 phba->sli4_hba.max_cfg_param.max_fcfi =
6706 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04006707 phba->sli4_hba.max_cfg_param.max_eq =
6708 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
6709 phba->sli4_hba.max_cfg_param.max_rq =
6710 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
6711 phba->sli4_hba.max_cfg_param.max_wq =
6712 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
6713 phba->sli4_hba.max_cfg_param.max_cq =
6714 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
6715 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
6716 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
6717 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
6718 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
James Smart5ffc2662009-11-18 15:39:44 -05006719 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
6720 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04006721 phba->max_vports = phba->max_vpi;
6722 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart6d368e52011-05-24 11:44:12 -04006723 "2003 cfg params Extents? %d "
6724 "XRI(B:%d M:%d), "
James Smartda0436e2009-05-22 14:51:39 -04006725 "VPI(B:%d M:%d) "
6726 "VFI(B:%d M:%d) "
6727 "RPI(B:%d M:%d) "
James Smart6d368e52011-05-24 11:44:12 -04006728 "FCFI(Count:%d)\n",
6729 phba->sli4_hba.extents_in_use,
James Smartda0436e2009-05-22 14:51:39 -04006730 phba->sli4_hba.max_cfg_param.xri_base,
6731 phba->sli4_hba.max_cfg_param.max_xri,
6732 phba->sli4_hba.max_cfg_param.vpi_base,
6733 phba->sli4_hba.max_cfg_param.max_vpi,
6734 phba->sli4_hba.max_cfg_param.vfi_base,
6735 phba->sli4_hba.max_cfg_param.max_vfi,
6736 phba->sli4_hba.max_cfg_param.rpi_base,
6737 phba->sli4_hba.max_cfg_param.max_rpi,
James Smartda0436e2009-05-22 14:51:39 -04006738 phba->sli4_hba.max_cfg_param.max_fcfi);
6739 }
James Smart912e3ac2011-05-24 11:42:11 -04006740
6741 if (rc)
6742 goto read_cfg_out;
James Smartda0436e2009-05-22 14:51:39 -04006743
6744 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -04006745 length = phba->sli4_hba.max_cfg_param.max_xri -
6746 lpfc_sli4_get_els_iocb_cnt(phba);
6747 if (phba->cfg_hba_queue_depth > length) {
6748 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6749 "3361 HBA queue depth changed from %d to %d\n",
6750 phba->cfg_hba_queue_depth, length);
6751 phba->cfg_hba_queue_depth = length;
6752 }
James Smart912e3ac2011-05-24 11:42:11 -04006753
6754 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
6755 LPFC_SLI_INTF_IF_TYPE_2)
6756 goto read_cfg_out;
6757
6758 /* get the pf# and vf# for SLI4 if_type 2 port */
6759 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
6760 sizeof(struct lpfc_sli4_cfg_mhdr));
6761 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
6762 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
6763 length, LPFC_SLI4_MBX_EMBED);
6764
James Smart8aa134a2012-08-14 14:25:29 -04006765 rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart912e3ac2011-05-24 11:42:11 -04006766 shdr = (union lpfc_sli4_cfg_shdr *)
6767 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
6768 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6769 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart8aa134a2012-08-14 14:25:29 -04006770 if (rc2 || shdr_status || shdr_add_status) {
James Smart912e3ac2011-05-24 11:42:11 -04006771 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6772 "3026 Mailbox failed , mbxCmd x%x "
6773 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
6774 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6775 bf_get(lpfc_mqe_status, &pmb->u.mqe));
James Smart912e3ac2011-05-24 11:42:11 -04006776 goto read_cfg_out;
6777 }
6778
6779 /* search for fc_fcoe resrouce descriptor */
6780 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
6781 desc_count = get_func_cfg->func_cfg.rsrc_desc_count;
6782
James Smart8aa134a2012-08-14 14:25:29 -04006783 pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
6784 desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
6785 length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
6786 if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
6787 length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
6788 else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
6789 goto read_cfg_out;
6790
James Smart912e3ac2011-05-24 11:42:11 -04006791 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
James Smart8aa134a2012-08-14 14:25:29 -04006792 desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
James Smart912e3ac2011-05-24 11:42:11 -04006793 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
James Smart8aa134a2012-08-14 14:25:29 -04006794 bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
James Smart912e3ac2011-05-24 11:42:11 -04006795 phba->sli4_hba.iov.pf_number =
6796 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
6797 phba->sli4_hba.iov.vf_number =
6798 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
6799 break;
6800 }
6801 }
6802
6803 if (i < LPFC_RSRC_DESC_MAX_NUM)
6804 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6805 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
6806 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
6807 phba->sli4_hba.iov.vf_number);
James Smart8aa134a2012-08-14 14:25:29 -04006808 else
James Smart912e3ac2011-05-24 11:42:11 -04006809 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6810 "3028 GET_FUNCTION_CONFIG: failed to find "
6811 "Resrouce Descriptor:x%x\n",
6812 LPFC_RSRC_DESC_TYPE_FCFCOE);
James Smart912e3ac2011-05-24 11:42:11 -04006813
6814read_cfg_out:
6815 mempool_free(pmb, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04006816 return rc;
6817}
6818
6819/**
James Smart2fcee4b2010-12-15 17:57:46 -05006820 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
James Smartda0436e2009-05-22 14:51:39 -04006821 * @phba: pointer to lpfc hba data structure.
6822 *
James Smart2fcee4b2010-12-15 17:57:46 -05006823 * This routine is invoked to setup the port-side endian order when
6824 * the port if_type is 0. This routine has no function for other
6825 * if_types.
James Smartda0436e2009-05-22 14:51:39 -04006826 *
6827 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006828 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006829 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006830 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006831 **/
6832static int
6833lpfc_setup_endian_order(struct lpfc_hba *phba)
6834{
6835 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05006836 uint32_t if_type, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04006837 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
6838 HOST_ENDIAN_HIGH_WORD1};
6839
James Smart2fcee4b2010-12-15 17:57:46 -05006840 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
6841 switch (if_type) {
6842 case LPFC_SLI_INTF_IF_TYPE_0:
6843 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6844 GFP_KERNEL);
6845 if (!mboxq) {
6846 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6847 "0492 Unable to allocate memory for "
6848 "issuing SLI_CONFIG_SPECIAL mailbox "
6849 "command\n");
6850 return -ENOMEM;
6851 }
James Smartda0436e2009-05-22 14:51:39 -04006852
James Smart2fcee4b2010-12-15 17:57:46 -05006853 /*
6854 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
6855 * two words to contain special data values and no other data.
6856 */
6857 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
6858 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
6859 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6860 if (rc != MBX_SUCCESS) {
6861 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6862 "0493 SLI_CONFIG_SPECIAL mailbox "
6863 "failed with status x%x\n",
6864 rc);
6865 rc = -EIO;
6866 }
6867 mempool_free(mboxq, phba->mbox_mem_pool);
6868 break;
6869 case LPFC_SLI_INTF_IF_TYPE_2:
6870 case LPFC_SLI_INTF_IF_TYPE_1:
6871 default:
6872 break;
James Smartda0436e2009-05-22 14:51:39 -04006873 }
James Smartda0436e2009-05-22 14:51:39 -04006874 return rc;
6875}
6876
6877/**
James Smart5350d872011-10-10 21:33:49 -04006878 * lpfc_sli4_queue_verify - Verify and update EQ and CQ counts
James Smartda0436e2009-05-22 14:51:39 -04006879 * @phba: pointer to lpfc hba data structure.
6880 *
James Smart5350d872011-10-10 21:33:49 -04006881 * This routine is invoked to check the user settable queue counts for EQs and
6882 * CQs. after this routine is called the counts will be set to valid values that
6883 * adhere to the constraints of the system's interrupt vectors and the port's
6884 * queue resources.
James Smartda0436e2009-05-22 14:51:39 -04006885 *
6886 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006887 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006888 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04006889 **/
6890static int
James Smart5350d872011-10-10 21:33:49 -04006891lpfc_sli4_queue_verify(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04006892{
James Smart67d12732012-08-03 12:36:13 -04006893 int cfg_fcp_io_channel;
James Smart90695ee2012-08-14 14:25:36 -04006894 uint32_t cpu;
6895 uint32_t i = 0;
James Smart1ba981f2014-02-20 09:56:45 -05006896 int fof_vectors = phba->cfg_fof ? 1 : 0;
James Smartda0436e2009-05-22 14:51:39 -04006897
6898 /*
James Smart67d12732012-08-03 12:36:13 -04006899 * Sanity check for configured queue parameters against the run-time
James Smartda0436e2009-05-22 14:51:39 -04006900 * device parameters
6901 */
6902
James Smart67d12732012-08-03 12:36:13 -04006903 /* Sanity check on HBA EQ parameters */
6904 cfg_fcp_io_channel = phba->cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04006905
James Smart7bb03bb2013-04-17 20:19:16 -04006906 /* It doesn't make sense to have more io channels then online CPUs */
6907 for_each_present_cpu(cpu) {
6908 if (cpu_online(cpu))
6909 i++;
James Smart90695ee2012-08-14 14:25:36 -04006910 }
James Smart7bb03bb2013-04-17 20:19:16 -04006911 phba->sli4_hba.num_online_cpu = i;
James Smartb246de12013-05-31 17:03:07 -04006912 phba->sli4_hba.num_present_cpu = lpfc_present_cpu;
James Smart76fd07a2014-02-20 09:57:18 -05006913 phba->sli4_hba.curr_disp_cpu = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04006914
James Smart90695ee2012-08-14 14:25:36 -04006915 if (i < cfg_fcp_io_channel) {
James Smart82c3e9b2012-09-29 11:29:50 -04006916 lpfc_printf_log(phba,
6917 KERN_ERR, LOG_INIT,
James Smart90695ee2012-08-14 14:25:36 -04006918 "3188 Reducing IO channels to match number of "
James Smart7bb03bb2013-04-17 20:19:16 -04006919 "online CPUs: from %d to %d\n",
6920 cfg_fcp_io_channel, i);
James Smart90695ee2012-08-14 14:25:36 -04006921 cfg_fcp_io_channel = i;
6922 }
6923
James Smart1ba981f2014-02-20 09:56:45 -05006924 if (cfg_fcp_io_channel + fof_vectors >
James Smart67d12732012-08-03 12:36:13 -04006925 phba->sli4_hba.max_cfg_param.max_eq) {
James Smart82c3e9b2012-09-29 11:29:50 -04006926 if (phba->sli4_hba.max_cfg_param.max_eq <
6927 LPFC_FCP_IO_CHAN_MIN) {
James Smartda0436e2009-05-22 14:51:39 -04006928 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6929 "2574 Not enough EQs (%d) from the "
6930 "pci function for supporting FCP "
6931 "EQs (%d)\n",
6932 phba->sli4_hba.max_cfg_param.max_eq,
James Smart67d12732012-08-03 12:36:13 -04006933 phba->cfg_fcp_io_channel);
James Smartda0436e2009-05-22 14:51:39 -04006934 goto out_error;
6935 }
James Smart82c3e9b2012-09-29 11:29:50 -04006936 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6937 "2575 Reducing IO channels to match number of "
6938 "available EQs: from %d to %d\n",
6939 cfg_fcp_io_channel,
6940 phba->sli4_hba.max_cfg_param.max_eq);
James Smart1ba981f2014-02-20 09:56:45 -05006941 cfg_fcp_io_channel = phba->sli4_hba.max_cfg_param.max_eq -
6942 fof_vectors;
James Smartda0436e2009-05-22 14:51:39 -04006943 }
James Smart67d12732012-08-03 12:36:13 -04006944
James Smartda0436e2009-05-22 14:51:39 -04006945 /* The actual number of FCP event queues adopted */
James Smart67d12732012-08-03 12:36:13 -04006946 phba->cfg_fcp_io_channel = cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04006947
James Smartda0436e2009-05-22 14:51:39 -04006948 /* Get EQ depth from module parameter, fake the default for now */
6949 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
6950 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
6951
James Smart5350d872011-10-10 21:33:49 -04006952 /* Get CQ depth from module parameter, fake the default for now */
6953 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
6954 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
6955
6956 return 0;
6957out_error:
6958 return -ENOMEM;
6959}
6960
6961/**
6962 * lpfc_sli4_queue_create - Create all the SLI4 queues
6963 * @phba: pointer to lpfc hba data structure.
6964 *
6965 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
6966 * operation. For each SLI4 queue type, the parameters such as queue entry
6967 * count (queue depth) shall be taken from the module parameter. For now,
6968 * we just use some constant number as place holder.
6969 *
6970 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07006971 * 0 - successful
James Smart5350d872011-10-10 21:33:49 -04006972 * -ENOMEM - No availble memory
6973 * -EIO - The mailbox failed to complete successfully.
6974 **/
6975int
6976lpfc_sli4_queue_create(struct lpfc_hba *phba)
6977{
6978 struct lpfc_queue *qdesc;
James Smart67d12732012-08-03 12:36:13 -04006979 int idx;
James Smart5350d872011-10-10 21:33:49 -04006980
6981 /*
James Smart67d12732012-08-03 12:36:13 -04006982 * Create HBA Record arrays.
James Smart5350d872011-10-10 21:33:49 -04006983 */
James Smart67d12732012-08-03 12:36:13 -04006984 if (!phba->cfg_fcp_io_channel)
6985 return -ERANGE;
James Smart5350d872011-10-10 21:33:49 -04006986
James Smart67d12732012-08-03 12:36:13 -04006987 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
6988 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
6989 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
6990 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
6991 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
6992 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
6993
6994 phba->sli4_hba.hba_eq = kzalloc((sizeof(struct lpfc_queue *) *
6995 phba->cfg_fcp_io_channel), GFP_KERNEL);
6996 if (!phba->sli4_hba.hba_eq) {
James Smartda0436e2009-05-22 14:51:39 -04006997 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04006998 "2576 Failed allocate memory for "
6999 "fast-path EQ record array\n");
James Smartda0436e2009-05-22 14:51:39 -04007000 goto out_error;
7001 }
James Smart67d12732012-08-03 12:36:13 -04007002
7003 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
7004 phba->cfg_fcp_io_channel), GFP_KERNEL);
7005 if (!phba->sli4_hba.fcp_cq) {
7006 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7007 "2577 Failed allocate memory for fast-path "
7008 "CQ record array\n");
7009 goto out_error;
7010 }
7011
7012 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
7013 phba->cfg_fcp_io_channel), GFP_KERNEL);
7014 if (!phba->sli4_hba.fcp_wq) {
7015 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7016 "2578 Failed allocate memory for fast-path "
7017 "WQ record array\n");
7018 goto out_error;
7019 }
James Smartda0436e2009-05-22 14:51:39 -04007020
James Smart5350d872011-10-10 21:33:49 -04007021 /*
James Smart67d12732012-08-03 12:36:13 -04007022 * Since the first EQ can have multiple CQs associated with it,
7023 * this array is used to quickly see if we have a FCP fast-path
7024 * CQ match.
James Smart5350d872011-10-10 21:33:49 -04007025 */
James Smart67d12732012-08-03 12:36:13 -04007026 phba->sli4_hba.fcp_cq_map = kzalloc((sizeof(uint16_t) *
7027 phba->cfg_fcp_io_channel), GFP_KERNEL);
7028 if (!phba->sli4_hba.fcp_cq_map) {
7029 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7030 "2545 Failed allocate memory for fast-path "
7031 "CQ map\n");
7032 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007033 }
James Smart67d12732012-08-03 12:36:13 -04007034
7035 /*
7036 * Create HBA Event Queues (EQs). The cfg_fcp_io_channel specifies
7037 * how many EQs to create.
7038 */
7039 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7040
7041 /* Create EQs */
James Smartda0436e2009-05-22 14:51:39 -04007042 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
7043 phba->sli4_hba.eq_ecount);
7044 if (!qdesc) {
7045 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04007046 "0497 Failed allocate EQ (%d)\n", idx);
7047 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007048 }
James Smart67d12732012-08-03 12:36:13 -04007049 phba->sli4_hba.hba_eq[idx] = qdesc;
7050
7051 /* Create Fast Path FCP CQs */
7052 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7053 phba->sli4_hba.cq_ecount);
7054 if (!qdesc) {
7055 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7056 "0499 Failed allocate fast-path FCP "
7057 "CQ (%d)\n", idx);
7058 goto out_error;
7059 }
7060 phba->sli4_hba.fcp_cq[idx] = qdesc;
7061
7062 /* Create Fast Path FCP WQs */
7063 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
7064 phba->sli4_hba.wq_ecount);
7065 if (!qdesc) {
7066 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7067 "0503 Failed allocate fast-path FCP "
7068 "WQ (%d)\n", idx);
7069 goto out_error;
7070 }
7071 phba->sli4_hba.fcp_wq[idx] = qdesc;
James Smartda0436e2009-05-22 14:51:39 -04007072 }
7073
James Smart67d12732012-08-03 12:36:13 -04007074
James Smartda0436e2009-05-22 14:51:39 -04007075 /*
James Smart67d12732012-08-03 12:36:13 -04007076 * Create Slow Path Completion Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04007077 */
7078
James Smartda0436e2009-05-22 14:51:39 -04007079 /* Create slow-path Mailbox Command Complete Queue */
7080 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7081 phba->sli4_hba.cq_ecount);
7082 if (!qdesc) {
7083 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7084 "0500 Failed allocate slow-path mailbox CQ\n");
James Smart67d12732012-08-03 12:36:13 -04007085 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007086 }
7087 phba->sli4_hba.mbx_cq = qdesc;
7088
7089 /* Create slow-path ELS Complete Queue */
7090 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7091 phba->sli4_hba.cq_ecount);
7092 if (!qdesc) {
7093 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7094 "0501 Failed allocate slow-path ELS CQ\n");
James Smart67d12732012-08-03 12:36:13 -04007095 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007096 }
7097 phba->sli4_hba.els_cq = qdesc;
7098
James Smartda0436e2009-05-22 14:51:39 -04007099
James Smart5350d872011-10-10 21:33:49 -04007100 /*
James Smart67d12732012-08-03 12:36:13 -04007101 * Create Slow Path Work Queues (WQs)
James Smart5350d872011-10-10 21:33:49 -04007102 */
James Smartda0436e2009-05-22 14:51:39 -04007103
7104 /* Create Mailbox Command Queue */
James Smartda0436e2009-05-22 14:51:39 -04007105
7106 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
7107 phba->sli4_hba.mq_ecount);
7108 if (!qdesc) {
7109 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7110 "0505 Failed allocate slow-path MQ\n");
James Smart67d12732012-08-03 12:36:13 -04007111 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007112 }
7113 phba->sli4_hba.mbx_wq = qdesc;
7114
7115 /*
James Smart67d12732012-08-03 12:36:13 -04007116 * Create ELS Work Queues
James Smartda0436e2009-05-22 14:51:39 -04007117 */
James Smartda0436e2009-05-22 14:51:39 -04007118
7119 /* Create slow-path ELS Work Queue */
7120 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
7121 phba->sli4_hba.wq_ecount);
7122 if (!qdesc) {
7123 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7124 "0504 Failed allocate slow-path ELS WQ\n");
James Smart67d12732012-08-03 12:36:13 -04007125 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007126 }
7127 phba->sli4_hba.els_wq = qdesc;
7128
James Smartda0436e2009-05-22 14:51:39 -04007129 /*
7130 * Create Receive Queue (RQ)
7131 */
James Smartda0436e2009-05-22 14:51:39 -04007132
7133 /* Create Receive Queue for header */
7134 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
7135 phba->sli4_hba.rq_ecount);
7136 if (!qdesc) {
7137 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7138 "0506 Failed allocate receive HRQ\n");
James Smart67d12732012-08-03 12:36:13 -04007139 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007140 }
7141 phba->sli4_hba.hdr_rq = qdesc;
7142
7143 /* Create Receive Queue for data */
7144 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
7145 phba->sli4_hba.rq_ecount);
7146 if (!qdesc) {
7147 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7148 "0507 Failed allocate receive DRQ\n");
James Smart67d12732012-08-03 12:36:13 -04007149 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007150 }
7151 phba->sli4_hba.dat_rq = qdesc;
7152
James Smart1ba981f2014-02-20 09:56:45 -05007153 /* Create the Queues needed for Flash Optimized Fabric operations */
7154 if (phba->cfg_fof)
7155 lpfc_fof_queue_create(phba);
James Smartda0436e2009-05-22 14:51:39 -04007156 return 0;
7157
James Smartda0436e2009-05-22 14:51:39 -04007158out_error:
James Smart67d12732012-08-03 12:36:13 -04007159 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007160 return -ENOMEM;
7161}
7162
7163/**
7164 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
7165 * @phba: pointer to lpfc hba data structure.
7166 *
7167 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
7168 * operation.
7169 *
7170 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007171 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007172 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007173 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007174 **/
James Smart5350d872011-10-10 21:33:49 -04007175void
James Smartda0436e2009-05-22 14:51:39 -04007176lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
7177{
James Smart67d12732012-08-03 12:36:13 -04007178 int idx;
7179
James Smart1ba981f2014-02-20 09:56:45 -05007180 if (phba->cfg_fof)
7181 lpfc_fof_queue_destroy(phba);
7182
James Smart67d12732012-08-03 12:36:13 -04007183 if (phba->sli4_hba.hba_eq != NULL) {
7184 /* Release HBA event queue */
7185 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7186 if (phba->sli4_hba.hba_eq[idx] != NULL) {
7187 lpfc_sli4_queue_free(
7188 phba->sli4_hba.hba_eq[idx]);
7189 phba->sli4_hba.hba_eq[idx] = NULL;
7190 }
7191 }
7192 kfree(phba->sli4_hba.hba_eq);
7193 phba->sli4_hba.hba_eq = NULL;
7194 }
7195
7196 if (phba->sli4_hba.fcp_cq != NULL) {
7197 /* Release FCP completion queue */
7198 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7199 if (phba->sli4_hba.fcp_cq[idx] != NULL) {
7200 lpfc_sli4_queue_free(
7201 phba->sli4_hba.fcp_cq[idx]);
7202 phba->sli4_hba.fcp_cq[idx] = NULL;
7203 }
7204 }
7205 kfree(phba->sli4_hba.fcp_cq);
7206 phba->sli4_hba.fcp_cq = NULL;
7207 }
7208
7209 if (phba->sli4_hba.fcp_wq != NULL) {
7210 /* Release FCP work queue */
7211 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7212 if (phba->sli4_hba.fcp_wq[idx] != NULL) {
7213 lpfc_sli4_queue_free(
7214 phba->sli4_hba.fcp_wq[idx]);
7215 phba->sli4_hba.fcp_wq[idx] = NULL;
7216 }
7217 }
7218 kfree(phba->sli4_hba.fcp_wq);
7219 phba->sli4_hba.fcp_wq = NULL;
7220 }
7221
7222 /* Release FCP CQ mapping array */
7223 if (phba->sli4_hba.fcp_cq_map != NULL) {
7224 kfree(phba->sli4_hba.fcp_cq_map);
7225 phba->sli4_hba.fcp_cq_map = NULL;
7226 }
James Smartda0436e2009-05-22 14:51:39 -04007227
7228 /* Release mailbox command work queue */
James Smart67d12732012-08-03 12:36:13 -04007229 if (phba->sli4_hba.mbx_wq != NULL) {
7230 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
7231 phba->sli4_hba.mbx_wq = NULL;
7232 }
James Smartda0436e2009-05-22 14:51:39 -04007233
7234 /* Release ELS work queue */
James Smart67d12732012-08-03 12:36:13 -04007235 if (phba->sli4_hba.els_wq != NULL) {
7236 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
7237 phba->sli4_hba.els_wq = NULL;
7238 }
James Smartda0436e2009-05-22 14:51:39 -04007239
7240 /* Release unsolicited receive queue */
James Smart67d12732012-08-03 12:36:13 -04007241 if (phba->sli4_hba.hdr_rq != NULL) {
7242 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
7243 phba->sli4_hba.hdr_rq = NULL;
7244 }
7245 if (phba->sli4_hba.dat_rq != NULL) {
7246 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
7247 phba->sli4_hba.dat_rq = NULL;
7248 }
James Smartda0436e2009-05-22 14:51:39 -04007249
James Smartda0436e2009-05-22 14:51:39 -04007250 /* Release ELS complete queue */
James Smart67d12732012-08-03 12:36:13 -04007251 if (phba->sli4_hba.els_cq != NULL) {
7252 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
7253 phba->sli4_hba.els_cq = NULL;
7254 }
James Smartda0436e2009-05-22 14:51:39 -04007255
7256 /* Release mailbox command complete queue */
James Smart67d12732012-08-03 12:36:13 -04007257 if (phba->sli4_hba.mbx_cq != NULL) {
7258 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
7259 phba->sli4_hba.mbx_cq = NULL;
7260 }
James Smartda0436e2009-05-22 14:51:39 -04007261
7262 return;
7263}
7264
7265/**
7266 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
7267 * @phba: pointer to lpfc hba data structure.
7268 *
7269 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
7270 * operation.
7271 *
7272 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007273 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007274 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007275 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007276 **/
7277int
7278lpfc_sli4_queue_setup(struct lpfc_hba *phba)
7279{
James Smart2a76a282012-08-03 12:35:54 -04007280 struct lpfc_sli *psli = &phba->sli;
7281 struct lpfc_sli_ring *pring;
James Smartda0436e2009-05-22 14:51:39 -04007282 int rc = -ENOMEM;
7283 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
7284 int fcp_cq_index = 0;
James Smart962bc512013-01-03 15:44:00 -05007285 uint32_t shdr_status, shdr_add_status;
7286 union lpfc_sli4_cfg_shdr *shdr;
7287 LPFC_MBOXQ_t *mboxq;
7288 uint32_t length;
7289
7290 /* Check for dual-ULP support */
7291 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7292 if (!mboxq) {
7293 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7294 "3249 Unable to allocate memory for "
7295 "QUERY_FW_CFG mailbox command\n");
7296 return -ENOMEM;
7297 }
7298 length = (sizeof(struct lpfc_mbx_query_fw_config) -
7299 sizeof(struct lpfc_sli4_cfg_mhdr));
7300 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7301 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
7302 length, LPFC_SLI4_MBX_EMBED);
7303
7304 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7305
7306 shdr = (union lpfc_sli4_cfg_shdr *)
7307 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7308 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7309 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7310 if (shdr_status || shdr_add_status || rc) {
7311 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7312 "3250 QUERY_FW_CFG mailbox failed with status "
7313 "x%x add_status x%x, mbx status x%x\n",
7314 shdr_status, shdr_add_status, rc);
7315 if (rc != MBX_TIMEOUT)
7316 mempool_free(mboxq, phba->mbox_mem_pool);
7317 rc = -ENXIO;
7318 goto out_error;
7319 }
7320
7321 phba->sli4_hba.fw_func_mode =
7322 mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
7323 phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
7324 phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
7325 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7326 "3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
7327 "ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
7328 phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
7329
7330 if (rc != MBX_TIMEOUT)
7331 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04007332
7333 /*
James Smart67d12732012-08-03 12:36:13 -04007334 * Set up HBA Event Queues (EQs)
James Smartda0436e2009-05-22 14:51:39 -04007335 */
7336
James Smart67d12732012-08-03 12:36:13 -04007337 /* Set up HBA event queue */
7338 if (phba->cfg_fcp_io_channel && !phba->sli4_hba.hba_eq) {
James Smart2e90f4b2011-12-13 13:22:37 -05007339 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7340 "3147 Fast-path EQs not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007341 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007342 goto out_error;
James Smart2e90f4b2011-12-13 13:22:37 -05007343 }
James Smart67d12732012-08-03 12:36:13 -04007344 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
7345 if (!phba->sli4_hba.hba_eq[fcp_eqidx]) {
James Smartda0436e2009-05-22 14:51:39 -04007346 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7347 "0522 Fast-path EQ (%d) not "
7348 "allocated\n", fcp_eqidx);
James Smart1b511972011-12-13 13:23:09 -05007349 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007350 goto out_destroy_hba_eq;
James Smartda0436e2009-05-22 14:51:39 -04007351 }
James Smart67d12732012-08-03 12:36:13 -04007352 rc = lpfc_eq_create(phba, phba->sli4_hba.hba_eq[fcp_eqidx],
James Smartbf8dae82012-08-03 12:36:24 -04007353 (phba->cfg_fcp_imax / phba->cfg_fcp_io_channel));
James Smartda0436e2009-05-22 14:51:39 -04007354 if (rc) {
7355 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7356 "0523 Failed setup of fast-path EQ "
7357 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
James Smart67d12732012-08-03 12:36:13 -04007358 goto out_destroy_hba_eq;
James Smartda0436e2009-05-22 14:51:39 -04007359 }
7360 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04007361 "2584 HBA EQ setup: "
James Smartda0436e2009-05-22 14:51:39 -04007362 "queue[%d]-id=%d\n", fcp_eqidx,
James Smart67d12732012-08-03 12:36:13 -04007363 phba->sli4_hba.hba_eq[fcp_eqidx]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007364 }
7365
James Smart67d12732012-08-03 12:36:13 -04007366 /* Set up fast-path FCP Response Complete Queue */
7367 if (!phba->sli4_hba.fcp_cq) {
7368 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7369 "3148 Fast-path FCP CQ array not "
7370 "allocated\n");
7371 rc = -ENOMEM;
7372 goto out_destroy_hba_eq;
7373 }
7374
7375 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_io_channel; fcp_cqidx++) {
7376 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
7377 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7378 "0526 Fast-path FCP CQ (%d) not "
7379 "allocated\n", fcp_cqidx);
7380 rc = -ENOMEM;
7381 goto out_destroy_fcp_cq;
7382 }
7383 rc = lpfc_cq_create(phba, phba->sli4_hba.fcp_cq[fcp_cqidx],
7384 phba->sli4_hba.hba_eq[fcp_cqidx], LPFC_WCQ, LPFC_FCP);
7385 if (rc) {
7386 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7387 "0527 Failed setup of fast-path FCP "
7388 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
7389 goto out_destroy_fcp_cq;
7390 }
7391
7392 /* Setup fcp_cq_map for fast lookup */
7393 phba->sli4_hba.fcp_cq_map[fcp_cqidx] =
7394 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id;
7395
7396 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7397 "2588 FCP CQ setup: cq[%d]-id=%d, "
7398 "parent seq[%d]-id=%d\n",
7399 fcp_cqidx,
7400 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
7401 fcp_cqidx,
7402 phba->sli4_hba.hba_eq[fcp_cqidx]->queue_id);
7403 }
7404
7405 /* Set up fast-path FCP Work Queue */
7406 if (!phba->sli4_hba.fcp_wq) {
7407 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7408 "3149 Fast-path FCP WQ array not "
7409 "allocated\n");
7410 rc = -ENOMEM;
7411 goto out_destroy_fcp_cq;
7412 }
7413
7414 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_io_channel; fcp_wqidx++) {
7415 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
7416 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7417 "0534 Fast-path FCP WQ (%d) not "
7418 "allocated\n", fcp_wqidx);
7419 rc = -ENOMEM;
7420 goto out_destroy_fcp_wq;
7421 }
7422 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
7423 phba->sli4_hba.fcp_cq[fcp_wqidx],
7424 LPFC_FCP);
7425 if (rc) {
7426 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7427 "0535 Failed setup of fast-path FCP "
7428 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
7429 goto out_destroy_fcp_wq;
7430 }
7431
7432 /* Bind this WQ to the next FCP ring */
7433 pring = &psli->ring[MAX_SLI3_CONFIGURED_RINGS + fcp_wqidx];
7434 pring->sli.sli4.wqp = (void *)phba->sli4_hba.fcp_wq[fcp_wqidx];
7435 phba->sli4_hba.fcp_cq[fcp_wqidx]->pring = pring;
7436
7437 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7438 "2591 FCP WQ setup: wq[%d]-id=%d, "
7439 "parent cq[%d]-id=%d\n",
7440 fcp_wqidx,
7441 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
7442 fcp_cq_index,
7443 phba->sli4_hba.fcp_cq[fcp_wqidx]->queue_id);
7444 }
James Smartda0436e2009-05-22 14:51:39 -04007445 /*
7446 * Set up Complete Queues (CQs)
7447 */
7448
7449 /* Set up slow-path MBOX Complete Queue as the first CQ */
7450 if (!phba->sli4_hba.mbx_cq) {
7451 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7452 "0528 Mailbox CQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007453 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007454 goto out_destroy_fcp_wq;
James Smartda0436e2009-05-22 14:51:39 -04007455 }
James Smart67d12732012-08-03 12:36:13 -04007456 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq,
7457 phba->sli4_hba.hba_eq[0], LPFC_MCQ, LPFC_MBOX);
James Smartda0436e2009-05-22 14:51:39 -04007458 if (rc) {
7459 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7460 "0529 Failed setup of slow-path mailbox CQ: "
7461 "rc = 0x%x\n", rc);
James Smart67d12732012-08-03 12:36:13 -04007462 goto out_destroy_fcp_wq;
James Smartda0436e2009-05-22 14:51:39 -04007463 }
7464 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7465 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
7466 phba->sli4_hba.mbx_cq->queue_id,
James Smart67d12732012-08-03 12:36:13 -04007467 phba->sli4_hba.hba_eq[0]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007468
7469 /* Set up slow-path ELS Complete Queue */
7470 if (!phba->sli4_hba.els_cq) {
7471 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7472 "0530 ELS CQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007473 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04007474 goto out_destroy_mbx_cq;
7475 }
James Smart67d12732012-08-03 12:36:13 -04007476 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq,
7477 phba->sli4_hba.hba_eq[0], LPFC_WCQ, LPFC_ELS);
James Smartda0436e2009-05-22 14:51:39 -04007478 if (rc) {
7479 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7480 "0531 Failed setup of slow-path ELS CQ: "
7481 "rc = 0x%x\n", rc);
7482 goto out_destroy_mbx_cq;
7483 }
7484 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7485 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
7486 phba->sli4_hba.els_cq->queue_id,
James Smart67d12732012-08-03 12:36:13 -04007487 phba->sli4_hba.hba_eq[0]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007488
7489 /*
7490 * Set up all the Work Queues (WQs)
7491 */
7492
7493 /* Set up Mailbox Command Queue */
7494 if (!phba->sli4_hba.mbx_wq) {
7495 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7496 "0538 Slow-path MQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007497 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007498 goto out_destroy_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04007499 }
7500 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
7501 phba->sli4_hba.mbx_cq, LPFC_MBOX);
7502 if (rc) {
7503 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7504 "0539 Failed setup of slow-path MQ: "
7505 "rc = 0x%x\n", rc);
James Smart67d12732012-08-03 12:36:13 -04007506 goto out_destroy_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04007507 }
7508 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7509 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
7510 phba->sli4_hba.mbx_wq->queue_id,
7511 phba->sli4_hba.mbx_cq->queue_id);
7512
7513 /* Set up slow-path ELS Work Queue */
7514 if (!phba->sli4_hba.els_wq) {
7515 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7516 "0536 Slow-path ELS WQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007517 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04007518 goto out_destroy_mbx_wq;
7519 }
7520 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
7521 phba->sli4_hba.els_cq, LPFC_ELS);
7522 if (rc) {
7523 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7524 "0537 Failed setup of slow-path ELS WQ: "
7525 "rc = 0x%x\n", rc);
7526 goto out_destroy_mbx_wq;
7527 }
James Smart2a76a282012-08-03 12:35:54 -04007528
7529 /* Bind this WQ to the ELS ring */
7530 pring = &psli->ring[LPFC_ELS_RING];
7531 pring->sli.sli4.wqp = (void *)phba->sli4_hba.els_wq;
7532 phba->sli4_hba.els_cq->pring = pring;
7533
James Smartda0436e2009-05-22 14:51:39 -04007534 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7535 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
7536 phba->sli4_hba.els_wq->queue_id,
7537 phba->sli4_hba.els_cq->queue_id);
7538
James Smartda0436e2009-05-22 14:51:39 -04007539 /*
7540 * Create Receive Queue (RQ)
7541 */
7542 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
7543 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7544 "0540 Receive Queue not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007545 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007546 goto out_destroy_els_wq;
James Smartda0436e2009-05-22 14:51:39 -04007547 }
James Smart73d91e52011-10-10 21:32:10 -04007548
7549 lpfc_rq_adjust_repost(phba, phba->sli4_hba.hdr_rq, LPFC_ELS_HBQ);
7550 lpfc_rq_adjust_repost(phba, phba->sli4_hba.dat_rq, LPFC_ELS_HBQ);
7551
James Smartda0436e2009-05-22 14:51:39 -04007552 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04007553 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04007554 if (rc) {
7555 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7556 "0541 Failed setup of Receive Queue: "
7557 "rc = 0x%x\n", rc);
7558 goto out_destroy_fcp_wq;
7559 }
James Smart73d91e52011-10-10 21:32:10 -04007560
James Smartda0436e2009-05-22 14:51:39 -04007561 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7562 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
7563 "parent cq-id=%d\n",
7564 phba->sli4_hba.hdr_rq->queue_id,
7565 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04007566 phba->sli4_hba.els_cq->queue_id);
James Smart1ba981f2014-02-20 09:56:45 -05007567
7568 if (phba->cfg_fof) {
7569 rc = lpfc_fof_queue_setup(phba);
7570 if (rc) {
7571 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7572 "0549 Failed setup of FOF Queues: "
7573 "rc = 0x%x\n", rc);
7574 goto out_destroy_els_rq;
7575 }
7576 }
James Smartda0436e2009-05-22 14:51:39 -04007577 return 0;
7578
James Smart1ba981f2014-02-20 09:56:45 -05007579out_destroy_els_rq:
7580 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
James Smart2e90f4b2011-12-13 13:22:37 -05007581out_destroy_els_wq:
James Smartda0436e2009-05-22 14:51:39 -04007582 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7583out_destroy_mbx_wq:
7584 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
James Smart2e90f4b2011-12-13 13:22:37 -05007585out_destroy_els_cq:
James Smartda0436e2009-05-22 14:51:39 -04007586 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
7587out_destroy_mbx_cq:
7588 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
James Smart67d12732012-08-03 12:36:13 -04007589out_destroy_fcp_wq:
7590 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
7591 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
7592out_destroy_fcp_cq:
7593 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
7594 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
7595out_destroy_hba_eq:
James Smartda0436e2009-05-22 14:51:39 -04007596 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
James Smart67d12732012-08-03 12:36:13 -04007597 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[fcp_eqidx]);
James Smartda0436e2009-05-22 14:51:39 -04007598out_error:
7599 return rc;
7600}
7601
7602/**
7603 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
7604 * @phba: pointer to lpfc hba data structure.
7605 *
7606 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
7607 * operation.
7608 *
7609 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007610 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007611 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007612 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007613 **/
7614void
7615lpfc_sli4_queue_unset(struct lpfc_hba *phba)
7616{
7617 int fcp_qidx;
7618
James Smart1ba981f2014-02-20 09:56:45 -05007619 /* Unset the queues created for Flash Optimized Fabric operations */
7620 if (phba->cfg_fof)
7621 lpfc_fof_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007622 /* Unset mailbox command work queue */
7623 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
7624 /* Unset ELS work queue */
7625 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7626 /* Unset unsolicited receive queue */
7627 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
7628 /* Unset FCP work queue */
James Smart67d12732012-08-03 12:36:13 -04007629 if (phba->sli4_hba.fcp_wq) {
7630 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
7631 fcp_qidx++)
7632 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
7633 }
James Smartda0436e2009-05-22 14:51:39 -04007634 /* Unset mailbox command complete queue */
7635 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
7636 /* Unset ELS complete queue */
7637 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
James Smartda0436e2009-05-22 14:51:39 -04007638 /* Unset FCP response complete queue */
James Smart2e90f4b2011-12-13 13:22:37 -05007639 if (phba->sli4_hba.fcp_cq) {
James Smart67d12732012-08-03 12:36:13 -04007640 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
7641 fcp_qidx++)
James Smart2e90f4b2011-12-13 13:22:37 -05007642 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05007643 }
James Smartda0436e2009-05-22 14:51:39 -04007644 /* Unset fast-path event queue */
James Smart67d12732012-08-03 12:36:13 -04007645 if (phba->sli4_hba.hba_eq) {
7646 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
James Smart2e90f4b2011-12-13 13:22:37 -05007647 fcp_qidx++)
James Smart67d12732012-08-03 12:36:13 -04007648 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[fcp_qidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05007649 }
James Smartda0436e2009-05-22 14:51:39 -04007650}
7651
7652/**
7653 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
7654 * @phba: pointer to lpfc hba data structure.
7655 *
7656 * This routine is invoked to allocate and set up a pool of completion queue
7657 * events. The body of the completion queue event is a completion queue entry
7658 * CQE. For now, this pool is used for the interrupt service routine to queue
7659 * the following HBA completion queue events for the worker thread to process:
7660 * - Mailbox asynchronous events
7661 * - Receive queue completion unsolicited events
7662 * Later, this can be used for all the slow-path events.
7663 *
7664 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007665 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007666 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04007667 **/
7668static int
7669lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
7670{
7671 struct lpfc_cq_event *cq_event;
7672 int i;
7673
7674 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
7675 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
7676 if (!cq_event)
7677 goto out_pool_create_fail;
7678 list_add_tail(&cq_event->list,
7679 &phba->sli4_hba.sp_cqe_event_pool);
7680 }
7681 return 0;
7682
7683out_pool_create_fail:
7684 lpfc_sli4_cq_event_pool_destroy(phba);
7685 return -ENOMEM;
7686}
7687
7688/**
7689 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
7690 * @phba: pointer to lpfc hba data structure.
7691 *
7692 * This routine is invoked to free the pool of completion queue events at
7693 * driver unload time. Note that, it is the responsibility of the driver
7694 * cleanup routine to free all the outstanding completion-queue events
7695 * allocated from this pool back into the pool before invoking this routine
7696 * to destroy the pool.
7697 **/
7698static void
7699lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
7700{
7701 struct lpfc_cq_event *cq_event, *next_cq_event;
7702
7703 list_for_each_entry_safe(cq_event, next_cq_event,
7704 &phba->sli4_hba.sp_cqe_event_pool, list) {
7705 list_del(&cq_event->list);
7706 kfree(cq_event);
7707 }
7708}
7709
7710/**
7711 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
7712 * @phba: pointer to lpfc hba data structure.
7713 *
7714 * This routine is the lock free version of the API invoked to allocate a
7715 * completion-queue event from the free pool.
7716 *
7717 * Return: Pointer to the newly allocated completion-queue event if successful
7718 * NULL otherwise.
7719 **/
7720struct lpfc_cq_event *
7721__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
7722{
7723 struct lpfc_cq_event *cq_event = NULL;
7724
7725 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
7726 struct lpfc_cq_event, list);
7727 return cq_event;
7728}
7729
7730/**
7731 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
7732 * @phba: pointer to lpfc hba data structure.
7733 *
7734 * This routine is the lock version of the API invoked to allocate a
7735 * completion-queue event from the free pool.
7736 *
7737 * Return: Pointer to the newly allocated completion-queue event if successful
7738 * NULL otherwise.
7739 **/
7740struct lpfc_cq_event *
7741lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
7742{
7743 struct lpfc_cq_event *cq_event;
7744 unsigned long iflags;
7745
7746 spin_lock_irqsave(&phba->hbalock, iflags);
7747 cq_event = __lpfc_sli4_cq_event_alloc(phba);
7748 spin_unlock_irqrestore(&phba->hbalock, iflags);
7749 return cq_event;
7750}
7751
7752/**
7753 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
7754 * @phba: pointer to lpfc hba data structure.
7755 * @cq_event: pointer to the completion queue event to be freed.
7756 *
7757 * This routine is the lock free version of the API invoked to release a
7758 * completion-queue event back into the free pool.
7759 **/
7760void
7761__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
7762 struct lpfc_cq_event *cq_event)
7763{
7764 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
7765}
7766
7767/**
7768 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
7769 * @phba: pointer to lpfc hba data structure.
7770 * @cq_event: pointer to the completion queue event to be freed.
7771 *
7772 * This routine is the lock version of the API invoked to release a
7773 * completion-queue event back into the free pool.
7774 **/
7775void
7776lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
7777 struct lpfc_cq_event *cq_event)
7778{
7779 unsigned long iflags;
7780 spin_lock_irqsave(&phba->hbalock, iflags);
7781 __lpfc_sli4_cq_event_release(phba, cq_event);
7782 spin_unlock_irqrestore(&phba->hbalock, iflags);
7783}
7784
7785/**
7786 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
7787 * @phba: pointer to lpfc hba data structure.
7788 *
7789 * This routine is to free all the pending completion-queue events to the
7790 * back into the free pool for device reset.
7791 **/
7792static void
7793lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
7794{
7795 LIST_HEAD(cqelist);
7796 struct lpfc_cq_event *cqe;
7797 unsigned long iflags;
7798
7799 /* Retrieve all the pending WCQEs from pending WCQE lists */
7800 spin_lock_irqsave(&phba->hbalock, iflags);
7801 /* Pending FCP XRI abort events */
7802 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
7803 &cqelist);
7804 /* Pending ELS XRI abort events */
7805 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
7806 &cqelist);
7807 /* Pending asynnc events */
7808 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
7809 &cqelist);
7810 spin_unlock_irqrestore(&phba->hbalock, iflags);
7811
7812 while (!list_empty(&cqelist)) {
7813 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
7814 lpfc_sli4_cq_event_release(phba, cqe);
7815 }
7816}
7817
7818/**
7819 * lpfc_pci_function_reset - Reset pci function.
7820 * @phba: pointer to lpfc hba data structure.
7821 *
7822 * This routine is invoked to request a PCI function reset. It will destroys
7823 * all resources assigned to the PCI function which originates this request.
7824 *
7825 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007826 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007827 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007828 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007829 **/
7830int
7831lpfc_pci_function_reset(struct lpfc_hba *phba)
7832{
7833 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05007834 uint32_t rc = 0, if_type;
James Smartda0436e2009-05-22 14:51:39 -04007835 uint32_t shdr_status, shdr_add_status;
James Smart2fcee4b2010-12-15 17:57:46 -05007836 uint32_t rdy_chk, num_resets = 0, reset_again = 0;
James Smartda0436e2009-05-22 14:51:39 -04007837 union lpfc_sli4_cfg_shdr *shdr;
James Smart2fcee4b2010-12-15 17:57:46 -05007838 struct lpfc_register reg_data;
James Smart2b81f942012-03-01 22:37:18 -05007839 uint16_t devid;
James Smartda0436e2009-05-22 14:51:39 -04007840
James Smart2fcee4b2010-12-15 17:57:46 -05007841 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7842 switch (if_type) {
7843 case LPFC_SLI_INTF_IF_TYPE_0:
7844 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
7845 GFP_KERNEL);
7846 if (!mboxq) {
7847 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7848 "0494 Unable to allocate memory for "
7849 "issuing SLI_FUNCTION_RESET mailbox "
7850 "command\n");
7851 return -ENOMEM;
7852 }
7853
7854 /* Setup PCI function reset mailbox-ioctl command */
7855 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7856 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
7857 LPFC_SLI4_MBX_EMBED);
7858 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7859 shdr = (union lpfc_sli4_cfg_shdr *)
7860 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7861 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7862 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
7863 &shdr->response);
7864 if (rc != MBX_TIMEOUT)
7865 mempool_free(mboxq, phba->mbox_mem_pool);
7866 if (shdr_status || shdr_add_status || rc) {
7867 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7868 "0495 SLI_FUNCTION_RESET mailbox "
7869 "failed with status x%x add_status x%x,"
7870 " mbx status x%x\n",
7871 shdr_status, shdr_add_status, rc);
7872 rc = -ENXIO;
7873 }
7874 break;
7875 case LPFC_SLI_INTF_IF_TYPE_2:
7876 for (num_resets = 0;
7877 num_resets < MAX_IF_TYPE_2_RESETS;
7878 num_resets++) {
7879 reg_data.word0 = 0;
7880 bf_set(lpfc_sliport_ctrl_end, &reg_data,
7881 LPFC_SLIPORT_LITTLE_ENDIAN);
7882 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
7883 LPFC_SLIPORT_INIT_PORT);
7884 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
7885 CTRLregaddr);
James Smart8fcb8ac2012-03-01 22:35:58 -05007886 /* flush */
James Smart2b81f942012-03-01 22:37:18 -05007887 pci_read_config_word(phba->pcidev,
7888 PCI_DEVICE_ID, &devid);
James Smart2fcee4b2010-12-15 17:57:46 -05007889 /*
7890 * Poll the Port Status Register and wait for RDY for
7891 * up to 10 seconds. If the port doesn't respond, treat
7892 * it as an error. If the port responds with RN, start
7893 * the loop again.
7894 */
7895 for (rdy_chk = 0; rdy_chk < 1000; rdy_chk++) {
James Smart73d91e52011-10-10 21:32:10 -04007896 msleep(10);
James Smart9940b972011-03-11 16:06:12 -05007897 if (lpfc_readl(phba->sli4_hba.u.if_type2.
7898 STATUSregaddr, &reg_data.word0)) {
7899 rc = -ENODEV;
James Smart73d91e52011-10-10 21:32:10 -04007900 goto out;
James Smart9940b972011-03-11 16:06:12 -05007901 }
James Smart6b5151f2012-01-18 16:24:06 -05007902 if (bf_get(lpfc_sliport_status_rn, &reg_data))
7903 reset_again++;
James Smart2fcee4b2010-12-15 17:57:46 -05007904 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
7905 break;
James Smart2fcee4b2010-12-15 17:57:46 -05007906 }
7907
7908 /*
7909 * If the port responds to the init request with
7910 * reset needed, delay for a bit and restart the loop.
7911 */
James Smart6b5151f2012-01-18 16:24:06 -05007912 if (reset_again && (rdy_chk < 1000)) {
James Smart2fcee4b2010-12-15 17:57:46 -05007913 msleep(10);
7914 reset_again = 0;
7915 continue;
7916 }
7917
7918 /* Detect any port errors. */
James Smart2fcee4b2010-12-15 17:57:46 -05007919 if ((bf_get(lpfc_sliport_status_err, &reg_data)) ||
7920 (rdy_chk >= 1000)) {
7921 phba->work_status[0] = readl(
7922 phba->sli4_hba.u.if_type2.ERR1regaddr);
7923 phba->work_status[1] = readl(
7924 phba->sli4_hba.u.if_type2.ERR2regaddr);
7925 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05007926 "2890 Port error detected during port "
James Smart8a9d2e82012-05-09 21:16:12 -04007927 "reset(%d): wait_tmo:%d ms, "
7928 "port status reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05007929 "error 1=0x%x, error 2=0x%x\n",
James Smart8a9d2e82012-05-09 21:16:12 -04007930 num_resets, rdy_chk*10,
7931 reg_data.word0,
James Smart2fcee4b2010-12-15 17:57:46 -05007932 phba->work_status[0],
7933 phba->work_status[1]);
7934 rc = -ENODEV;
7935 }
7936
7937 /*
7938 * Terminate the outer loop provided the Port indicated
7939 * ready within 10 seconds.
7940 */
7941 if (rdy_chk < 1000)
7942 break;
7943 }
James Smart05580562011-05-24 11:40:48 -04007944 /* delay driver action following IF_TYPE_2 function reset */
7945 msleep(100);
James Smart2fcee4b2010-12-15 17:57:46 -05007946 break;
7947 case LPFC_SLI_INTF_IF_TYPE_1:
7948 default:
7949 break;
James Smartda0436e2009-05-22 14:51:39 -04007950 }
7951
James Smart73d91e52011-10-10 21:32:10 -04007952out:
James Smart2fcee4b2010-12-15 17:57:46 -05007953 /* Catch the not-ready port failure after a port reset. */
James Smart229adb02013-04-17 20:16:51 -04007954 if (num_resets >= MAX_IF_TYPE_2_RESETS) {
7955 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7956 "3317 HBA not functional: IP Reset Failed "
7957 "after (%d) retries, try: "
7958 "echo fw_reset > board_mode\n", num_resets);
James Smart2fcee4b2010-12-15 17:57:46 -05007959 rc = -ENODEV;
James Smart229adb02013-04-17 20:16:51 -04007960 }
James Smart2fcee4b2010-12-15 17:57:46 -05007961
James Smartda0436e2009-05-22 14:51:39 -04007962 return rc;
7963}
7964
7965/**
James Smartda0436e2009-05-22 14:51:39 -04007966 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
7967 * @phba: pointer to lpfc hba data structure.
7968 *
7969 * This routine is invoked to set up the PCI device memory space for device
7970 * with SLI-4 interface spec.
7971 *
7972 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007973 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007974 * other values - error
7975 **/
7976static int
7977lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
7978{
7979 struct pci_dev *pdev;
7980 unsigned long bar0map_len, bar1map_len, bar2map_len;
7981 int error = -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -05007982 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04007983
7984 /* Obtain PCI device reference */
7985 if (!phba->pcidev)
7986 return error;
7987 else
7988 pdev = phba->pcidev;
7989
7990 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05007991 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
7992 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
7993 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
7994 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smartda0436e2009-05-22 14:51:39 -04007995 return error;
Michael Reed8e685972009-09-18 12:02:05 -05007996 }
7997 }
James Smartda0436e2009-05-22 14:51:39 -04007998
James Smart2fcee4b2010-12-15 17:57:46 -05007999 /*
8000 * The BARs and register set definitions and offset locations are
8001 * dependent on the if_type.
8002 */
8003 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
8004 &phba->sli4_hba.sli_intf.word0)) {
8005 return error;
8006 }
8007
8008 /* There is no SLI3 failback for SLI4 devices. */
8009 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
8010 LPFC_SLI_INTF_VALID) {
8011 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8012 "2894 SLI_INTF reg contents invalid "
8013 "sli_intf reg 0x%x\n",
8014 phba->sli4_hba.sli_intf.word0);
8015 return error;
8016 }
8017
8018 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
8019 /*
8020 * Get the bus address of SLI4 device Bar regions and the
8021 * number of bytes required by each mapping. The mapping of the
8022 * particular PCI BARs regions is dependent on the type of
8023 * SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -04008024 */
James Smartf5ca6f22013-09-06 12:21:09 -04008025 if (pci_resource_start(pdev, PCI_64BIT_BAR0)) {
8026 phba->pci_bar0_map = pci_resource_start(pdev, PCI_64BIT_BAR0);
8027 bar0map_len = pci_resource_len(pdev, PCI_64BIT_BAR0);
James Smart2fcee4b2010-12-15 17:57:46 -05008028
8029 /*
8030 * Map SLI4 PCI Config Space Register base to a kernel virtual
8031 * addr
8032 */
8033 phba->sli4_hba.conf_regs_memmap_p =
8034 ioremap(phba->pci_bar0_map, bar0map_len);
8035 if (!phba->sli4_hba.conf_regs_memmap_p) {
8036 dev_printk(KERN_ERR, &pdev->dev,
8037 "ioremap failed for SLI4 PCI config "
8038 "registers.\n");
8039 goto out;
8040 }
James Smartf5ca6f22013-09-06 12:21:09 -04008041 phba->pci_bar0_memmap_p = phba->sli4_hba.conf_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05008042 /* Set up BAR0 PCI config space register memory map */
8043 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smart1dfb5a42010-02-12 14:40:50 -05008044 } else {
8045 phba->pci_bar0_map = pci_resource_start(pdev, 1);
8046 bar0map_len = pci_resource_len(pdev, 1);
James Smart2fcee4b2010-12-15 17:57:46 -05008047 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8048 dev_printk(KERN_ERR, &pdev->dev,
8049 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
8050 goto out;
8051 }
8052 phba->sli4_hba.conf_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04008053 ioremap(phba->pci_bar0_map, bar0map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05008054 if (!phba->sli4_hba.conf_regs_memmap_p) {
8055 dev_printk(KERN_ERR, &pdev->dev,
8056 "ioremap failed for SLI4 PCI config "
8057 "registers.\n");
8058 goto out;
8059 }
8060 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smartda0436e2009-05-22 14:51:39 -04008061 }
8062
James Smartc31098c2011-04-16 11:03:33 -04008063 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
James Smartf5ca6f22013-09-06 12:21:09 -04008064 (pci_resource_start(pdev, PCI_64BIT_BAR2))) {
James Smart2fcee4b2010-12-15 17:57:46 -05008065 /*
8066 * Map SLI4 if type 0 HBA Control Register base to a kernel
8067 * virtual address and setup the registers.
8068 */
James Smartf5ca6f22013-09-06 12:21:09 -04008069 phba->pci_bar1_map = pci_resource_start(pdev, PCI_64BIT_BAR2);
8070 bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
James Smart2fcee4b2010-12-15 17:57:46 -05008071 phba->sli4_hba.ctrl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04008072 ioremap(phba->pci_bar1_map, bar1map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05008073 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
8074 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04008075 "ioremap failed for SLI4 HBA control registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05008076 goto out_iounmap_conf;
8077 }
James Smartf5ca6f22013-09-06 12:21:09 -04008078 phba->pci_bar2_memmap_p = phba->sli4_hba.ctrl_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05008079 lpfc_sli4_bar1_register_memmap(phba);
James Smartda0436e2009-05-22 14:51:39 -04008080 }
8081
James Smartc31098c2011-04-16 11:03:33 -04008082 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
James Smartf5ca6f22013-09-06 12:21:09 -04008083 (pci_resource_start(pdev, PCI_64BIT_BAR4))) {
James Smart2fcee4b2010-12-15 17:57:46 -05008084 /*
8085 * Map SLI4 if type 0 HBA Doorbell Register base to a kernel
8086 * virtual address and setup the registers.
8087 */
James Smartf5ca6f22013-09-06 12:21:09 -04008088 phba->pci_bar2_map = pci_resource_start(pdev, PCI_64BIT_BAR4);
8089 bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
James Smart2fcee4b2010-12-15 17:57:46 -05008090 phba->sli4_hba.drbl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04008091 ioremap(phba->pci_bar2_map, bar2map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05008092 if (!phba->sli4_hba.drbl_regs_memmap_p) {
8093 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04008094 "ioremap failed for SLI4 HBA doorbell registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05008095 goto out_iounmap_ctrl;
8096 }
James Smartf5ca6f22013-09-06 12:21:09 -04008097 phba->pci_bar4_memmap_p = phba->sli4_hba.drbl_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05008098 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
8099 if (error)
8100 goto out_iounmap_all;
James Smartda0436e2009-05-22 14:51:39 -04008101 }
8102
James Smartda0436e2009-05-22 14:51:39 -04008103 return 0;
8104
8105out_iounmap_all:
8106 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
8107out_iounmap_ctrl:
8108 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
8109out_iounmap_conf:
8110 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8111out:
8112 return error;
8113}
8114
8115/**
8116 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
8117 * @phba: pointer to lpfc hba data structure.
8118 *
8119 * This routine is invoked to unset the PCI device memory space for device
8120 * with SLI-4 interface spec.
8121 **/
8122static void
8123lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
8124{
James Smart2e90f4b2011-12-13 13:22:37 -05008125 uint32_t if_type;
8126 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smartda0436e2009-05-22 14:51:39 -04008127
James Smart2e90f4b2011-12-13 13:22:37 -05008128 switch (if_type) {
8129 case LPFC_SLI_INTF_IF_TYPE_0:
8130 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
8131 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
8132 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8133 break;
8134 case LPFC_SLI_INTF_IF_TYPE_2:
8135 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8136 break;
8137 case LPFC_SLI_INTF_IF_TYPE_1:
8138 default:
8139 dev_printk(KERN_ERR, &phba->pcidev->dev,
8140 "FATAL - unsupported SLI4 interface type - %d\n",
8141 if_type);
8142 break;
8143 }
James Smartda0436e2009-05-22 14:51:39 -04008144}
8145
8146/**
James Smart3772a992009-05-22 14:50:54 -04008147 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
8148 * @phba: pointer to lpfc hba data structure.
8149 *
8150 * This routine is invoked to enable the MSI-X interrupt vectors to device
8151 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
8152 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
8153 * invoked, enables either all or nothing, depending on the current
8154 * availability of PCI vector resources. The device driver is responsible
8155 * for calling the individual request_irq() to register each MSI-X vector
8156 * with a interrupt handler, which is done in this function. Note that
8157 * later when device is unloading, the driver should always call free_irq()
8158 * on all MSI-X vectors it has done request_irq() on before calling
8159 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
8160 * will be left with MSI-X enabled and leaks its vectors.
8161 *
8162 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008163 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008164 * other values - error
8165 **/
8166static int
8167lpfc_sli_enable_msix(struct lpfc_hba *phba)
8168{
8169 int rc, i;
8170 LPFC_MBOXQ_t *pmb;
8171
8172 /* Set up MSI-X multi-message vectors */
8173 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8174 phba->msix_entries[i].entry = i;
8175
8176 /* Configure MSI-X capability structure */
8177 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
8178 ARRAY_SIZE(phba->msix_entries));
8179 if (rc) {
8180 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8181 "0420 PCI enable MSI-X failed (%d)\n", rc);
8182 goto msi_fail_out;
8183 }
8184 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8185 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8186 "0477 MSI-X entry[%d]: vector=x%x "
8187 "message=%d\n", i,
8188 phba->msix_entries[i].vector,
8189 phba->msix_entries[i].entry);
8190 /*
8191 * Assign MSI-X vectors to interrupt handlers
8192 */
8193
8194 /* vector-0 is associated to slow-path handler */
8195 rc = request_irq(phba->msix_entries[0].vector,
8196 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
8197 LPFC_SP_DRIVER_HANDLER_NAME, phba);
8198 if (rc) {
8199 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8200 "0421 MSI-X slow-path request_irq failed "
8201 "(%d)\n", rc);
8202 goto msi_fail_out;
8203 }
8204
8205 /* vector-1 is associated to fast-path handler */
8206 rc = request_irq(phba->msix_entries[1].vector,
8207 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
8208 LPFC_FP_DRIVER_HANDLER_NAME, phba);
8209
8210 if (rc) {
8211 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8212 "0429 MSI-X fast-path request_irq failed "
8213 "(%d)\n", rc);
8214 goto irq_fail_out;
8215 }
8216
8217 /*
8218 * Configure HBA MSI-X attention conditions to messages
8219 */
8220 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8221
8222 if (!pmb) {
8223 rc = -ENOMEM;
8224 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8225 "0474 Unable to allocate memory for issuing "
8226 "MBOX_CONFIG_MSI command\n");
8227 goto mem_fail_out;
8228 }
8229 rc = lpfc_config_msi(phba, pmb);
8230 if (rc)
8231 goto mbx_fail_out;
8232 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
8233 if (rc != MBX_SUCCESS) {
8234 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
8235 "0351 Config MSI mailbox command failed, "
8236 "mbxCmd x%x, mbxStatus x%x\n",
8237 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
8238 goto mbx_fail_out;
8239 }
8240
8241 /* Free memory allocated for mailbox command */
8242 mempool_free(pmb, phba->mbox_mem_pool);
8243 return rc;
8244
8245mbx_fail_out:
8246 /* Free memory allocated for mailbox command */
8247 mempool_free(pmb, phba->mbox_mem_pool);
8248
8249mem_fail_out:
8250 /* free the irq already requested */
8251 free_irq(phba->msix_entries[1].vector, phba);
8252
8253irq_fail_out:
8254 /* free the irq already requested */
8255 free_irq(phba->msix_entries[0].vector, phba);
8256
8257msi_fail_out:
8258 /* Unconfigure MSI-X capability structure */
8259 pci_disable_msix(phba->pcidev);
8260 return rc;
8261}
8262
8263/**
8264 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
8265 * @phba: pointer to lpfc hba data structure.
8266 *
8267 * This routine is invoked to release the MSI-X vectors and then disable the
8268 * MSI-X interrupt mode to device with SLI-3 interface spec.
8269 **/
8270static void
8271lpfc_sli_disable_msix(struct lpfc_hba *phba)
8272{
8273 int i;
8274
8275 /* Free up MSI-X multi-message vectors */
8276 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8277 free_irq(phba->msix_entries[i].vector, phba);
8278 /* Disable MSI-X */
8279 pci_disable_msix(phba->pcidev);
8280
8281 return;
8282}
8283
8284/**
8285 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
8286 * @phba: pointer to lpfc hba data structure.
8287 *
8288 * This routine is invoked to enable the MSI interrupt mode to device with
8289 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
8290 * enable the MSI vector. The device driver is responsible for calling the
8291 * request_irq() to register MSI vector with a interrupt the handler, which
8292 * is done in this function.
8293 *
8294 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008295 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008296 * other values - error
8297 */
8298static int
8299lpfc_sli_enable_msi(struct lpfc_hba *phba)
8300{
8301 int rc;
8302
8303 rc = pci_enable_msi(phba->pcidev);
8304 if (!rc)
8305 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8306 "0462 PCI enable MSI mode success.\n");
8307 else {
8308 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8309 "0471 PCI enable MSI mode failed (%d)\n", rc);
8310 return rc;
8311 }
8312
8313 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
8314 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8315 if (rc) {
8316 pci_disable_msi(phba->pcidev);
8317 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8318 "0478 MSI request_irq failed (%d)\n", rc);
8319 }
8320 return rc;
8321}
8322
8323/**
8324 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
8325 * @phba: pointer to lpfc hba data structure.
8326 *
8327 * This routine is invoked to disable the MSI interrupt mode to device with
8328 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
8329 * done request_irq() on before calling pci_disable_msi(). Failure to do so
8330 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
8331 * its vector.
8332 */
8333static void
8334lpfc_sli_disable_msi(struct lpfc_hba *phba)
8335{
8336 free_irq(phba->pcidev->irq, phba);
8337 pci_disable_msi(phba->pcidev);
8338 return;
8339}
8340
8341/**
8342 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
8343 * @phba: pointer to lpfc hba data structure.
8344 *
8345 * This routine is invoked to enable device interrupt and associate driver's
8346 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
8347 * spec. Depends on the interrupt mode configured to the driver, the driver
8348 * will try to fallback from the configured interrupt mode to an interrupt
8349 * mode which is supported by the platform, kernel, and device in the order
8350 * of:
8351 * MSI-X -> MSI -> IRQ.
8352 *
8353 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008354 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008355 * other values - error
8356 **/
8357static uint32_t
8358lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
8359{
8360 uint32_t intr_mode = LPFC_INTR_ERROR;
8361 int retval;
8362
8363 if (cfg_mode == 2) {
8364 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
8365 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
8366 if (!retval) {
8367 /* Now, try to enable MSI-X interrupt mode */
8368 retval = lpfc_sli_enable_msix(phba);
8369 if (!retval) {
8370 /* Indicate initialization to MSI-X mode */
8371 phba->intr_type = MSIX;
8372 intr_mode = 2;
8373 }
8374 }
8375 }
8376
8377 /* Fallback to MSI if MSI-X initialization failed */
8378 if (cfg_mode >= 1 && phba->intr_type == NONE) {
8379 retval = lpfc_sli_enable_msi(phba);
8380 if (!retval) {
8381 /* Indicate initialization to MSI mode */
8382 phba->intr_type = MSI;
8383 intr_mode = 1;
8384 }
8385 }
8386
8387 /* Fallback to INTx if both MSI-X/MSI initalization failed */
8388 if (phba->intr_type == NONE) {
8389 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
8390 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8391 if (!retval) {
8392 /* Indicate initialization to INTx mode */
8393 phba->intr_type = INTx;
8394 intr_mode = 0;
8395 }
8396 }
8397 return intr_mode;
8398}
8399
8400/**
8401 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
8402 * @phba: pointer to lpfc hba data structure.
8403 *
8404 * This routine is invoked to disable device interrupt and disassociate the
8405 * driver's interrupt handler(s) from interrupt vector(s) to device with
8406 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
8407 * release the interrupt vector(s) for the message signaled interrupt.
8408 **/
8409static void
8410lpfc_sli_disable_intr(struct lpfc_hba *phba)
8411{
8412 /* Disable the currently initialized interrupt mode */
8413 if (phba->intr_type == MSIX)
8414 lpfc_sli_disable_msix(phba);
8415 else if (phba->intr_type == MSI)
8416 lpfc_sli_disable_msi(phba);
8417 else if (phba->intr_type == INTx)
8418 free_irq(phba->pcidev->irq, phba);
8419
8420 /* Reset interrupt management states */
8421 phba->intr_type = NONE;
8422 phba->sli.slistat.sli_intr = 0;
8423
8424 return;
8425}
8426
8427/**
James Smart7bb03bb2013-04-17 20:19:16 -04008428 * lpfc_find_next_cpu - Find next available CPU that matches the phys_id
8429 * @phba: pointer to lpfc hba data structure.
8430 *
8431 * Find next available CPU to use for IRQ to CPU affinity.
8432 */
8433static int
8434lpfc_find_next_cpu(struct lpfc_hba *phba, uint32_t phys_id)
8435{
8436 struct lpfc_vector_map_info *cpup;
8437 int cpu;
8438
8439 cpup = phba->sli4_hba.cpu_map;
8440 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8441 /* CPU must be online */
8442 if (cpu_online(cpu)) {
8443 if ((cpup->irq == LPFC_VECTOR_MAP_EMPTY) &&
8444 (lpfc_used_cpu[cpu] == LPFC_VECTOR_MAP_EMPTY) &&
8445 (cpup->phys_id == phys_id)) {
8446 return cpu;
8447 }
8448 }
8449 cpup++;
8450 }
8451
8452 /*
8453 * If we get here, we have used ALL CPUs for the specific
8454 * phys_id. Now we need to clear out lpfc_used_cpu and start
8455 * reusing CPUs.
8456 */
8457
8458 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8459 if (lpfc_used_cpu[cpu] == phys_id)
8460 lpfc_used_cpu[cpu] = LPFC_VECTOR_MAP_EMPTY;
8461 }
8462
8463 cpup = phba->sli4_hba.cpu_map;
8464 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8465 /* CPU must be online */
8466 if (cpu_online(cpu)) {
8467 if ((cpup->irq == LPFC_VECTOR_MAP_EMPTY) &&
8468 (cpup->phys_id == phys_id)) {
8469 return cpu;
8470 }
8471 }
8472 cpup++;
8473 }
8474 return LPFC_VECTOR_MAP_EMPTY;
8475}
8476
8477/**
8478 * lpfc_sli4_set_affinity - Set affinity for HBA IRQ vectors
8479 * @phba: pointer to lpfc hba data structure.
8480 * @vectors: number of HBA vectors
8481 *
8482 * Affinitize MSIX IRQ vectors to CPUs. Try to equally spread vector
8483 * affinization across multple physical CPUs (numa nodes).
8484 * In addition, this routine will assign an IO channel for each CPU
8485 * to use when issuing I/Os.
8486 */
8487static int
8488lpfc_sli4_set_affinity(struct lpfc_hba *phba, int vectors)
8489{
8490 int i, idx, saved_chann, used_chann, cpu, phys_id;
James Smartec2087a2013-09-06 12:20:36 -04008491 int max_phys_id, min_phys_id;
8492 int num_io_channel, first_cpu, chan;
James Smart7bb03bb2013-04-17 20:19:16 -04008493 struct lpfc_vector_map_info *cpup;
8494#ifdef CONFIG_X86
8495 struct cpuinfo_x86 *cpuinfo;
8496#endif
8497 struct cpumask *mask;
8498 uint8_t chann[LPFC_FCP_IO_CHAN_MAX+1];
8499
8500 /* If there is no mapping, just return */
8501 if (!phba->cfg_fcp_cpu_map)
8502 return 1;
8503
8504 /* Init cpu_map array */
8505 memset(phba->sli4_hba.cpu_map, 0xff,
8506 (sizeof(struct lpfc_vector_map_info) *
8507 phba->sli4_hba.num_present_cpu));
8508
8509 max_phys_id = 0;
James Smartec2087a2013-09-06 12:20:36 -04008510 min_phys_id = 0xff;
James Smart7bb03bb2013-04-17 20:19:16 -04008511 phys_id = 0;
8512 num_io_channel = 0;
8513 first_cpu = LPFC_VECTOR_MAP_EMPTY;
8514
8515 /* Update CPU map with physical id and core id of each CPU */
8516 cpup = phba->sli4_hba.cpu_map;
8517 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8518#ifdef CONFIG_X86
8519 cpuinfo = &cpu_data(cpu);
8520 cpup->phys_id = cpuinfo->phys_proc_id;
8521 cpup->core_id = cpuinfo->cpu_core_id;
8522#else
8523 /* No distinction between CPUs for other platforms */
8524 cpup->phys_id = 0;
8525 cpup->core_id = 0;
8526#endif
8527
8528 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8529 "3328 CPU physid %d coreid %d\n",
8530 cpup->phys_id, cpup->core_id);
8531
8532 if (cpup->phys_id > max_phys_id)
8533 max_phys_id = cpup->phys_id;
James Smartec2087a2013-09-06 12:20:36 -04008534 if (cpup->phys_id < min_phys_id)
8535 min_phys_id = cpup->phys_id;
James Smart7bb03bb2013-04-17 20:19:16 -04008536 cpup++;
8537 }
8538
James Smartec2087a2013-09-06 12:20:36 -04008539 phys_id = min_phys_id;
James Smart7bb03bb2013-04-17 20:19:16 -04008540 /* Now associate the HBA vectors with specific CPUs */
8541 for (idx = 0; idx < vectors; idx++) {
8542 cpup = phba->sli4_hba.cpu_map;
8543 cpu = lpfc_find_next_cpu(phba, phys_id);
8544 if (cpu == LPFC_VECTOR_MAP_EMPTY) {
8545
8546 /* Try for all phys_id's */
8547 for (i = 1; i < max_phys_id; i++) {
8548 phys_id++;
8549 if (phys_id > max_phys_id)
James Smartec2087a2013-09-06 12:20:36 -04008550 phys_id = min_phys_id;
James Smart7bb03bb2013-04-17 20:19:16 -04008551 cpu = lpfc_find_next_cpu(phba, phys_id);
8552 if (cpu == LPFC_VECTOR_MAP_EMPTY)
8553 continue;
8554 goto found;
8555 }
8556
James Smartec2087a2013-09-06 12:20:36 -04008557 /* Use round robin for scheduling */
8558 phba->cfg_fcp_io_sched = LPFC_FCP_SCHED_ROUND_ROBIN;
8559 chan = 0;
8560 cpup = phba->sli4_hba.cpu_map;
8561 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
8562 cpup->channel_id = chan;
8563 cpup++;
8564 chan++;
8565 if (chan >= phba->cfg_fcp_io_channel)
8566 chan = 0;
8567 }
8568
James Smart7bb03bb2013-04-17 20:19:16 -04008569 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8570 "3329 Cannot set affinity:"
8571 "Error mapping vector %d (%d)\n",
8572 idx, vectors);
8573 return 0;
8574 }
8575found:
8576 cpup += cpu;
8577 if (phba->cfg_fcp_cpu_map == LPFC_DRIVER_CPU_MAP)
8578 lpfc_used_cpu[cpu] = phys_id;
8579
8580 /* Associate vector with selected CPU */
8581 cpup->irq = phba->sli4_hba.msix_entries[idx].vector;
8582
8583 /* Associate IO channel with selected CPU */
8584 cpup->channel_id = idx;
8585 num_io_channel++;
8586
8587 if (first_cpu == LPFC_VECTOR_MAP_EMPTY)
8588 first_cpu = cpu;
8589
8590 /* Now affinitize to the selected CPU */
8591 mask = &cpup->maskbits;
8592 cpumask_clear(mask);
8593 cpumask_set_cpu(cpu, mask);
8594 i = irq_set_affinity_hint(phba->sli4_hba.msix_entries[idx].
8595 vector, mask);
8596
8597 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8598 "3330 Set Affinity: CPU %d channel %d "
8599 "irq %d (%x)\n",
8600 cpu, cpup->channel_id,
8601 phba->sli4_hba.msix_entries[idx].vector, i);
8602
8603 /* Spread vector mapping across multple physical CPU nodes */
8604 phys_id++;
8605 if (phys_id > max_phys_id)
James Smartec2087a2013-09-06 12:20:36 -04008606 phys_id = min_phys_id;
James Smart7bb03bb2013-04-17 20:19:16 -04008607 }
8608
8609 /*
8610 * Finally fill in the IO channel for any remaining CPUs.
8611 * At this point, all IO channels have been assigned to a specific
8612 * MSIx vector, mapped to a specific CPU.
8613 * Base the remaining IO channel assigned, to IO channels already
8614 * assigned to other CPUs on the same phys_id.
8615 */
James Smartec2087a2013-09-06 12:20:36 -04008616 for (i = min_phys_id; i <= max_phys_id; i++) {
James Smart7bb03bb2013-04-17 20:19:16 -04008617 /*
8618 * If there are no io channels already mapped to
8619 * this phys_id, just round robin thru the io_channels.
8620 * Setup chann[] for round robin.
8621 */
8622 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++)
8623 chann[idx] = idx;
8624
8625 saved_chann = 0;
8626 used_chann = 0;
8627
8628 /*
8629 * First build a list of IO channels already assigned
8630 * to this phys_id before reassigning the same IO
8631 * channels to the remaining CPUs.
8632 */
8633 cpup = phba->sli4_hba.cpu_map;
8634 cpu = first_cpu;
8635 cpup += cpu;
8636 for (idx = 0; idx < phba->sli4_hba.num_present_cpu;
8637 idx++) {
8638 if (cpup->phys_id == i) {
8639 /*
8640 * Save any IO channels that are
8641 * already mapped to this phys_id.
8642 */
8643 if (cpup->irq != LPFC_VECTOR_MAP_EMPTY) {
8644 chann[saved_chann] =
8645 cpup->channel_id;
8646 saved_chann++;
8647 goto out;
8648 }
8649
8650 /* See if we are using round-robin */
8651 if (saved_chann == 0)
8652 saved_chann =
8653 phba->cfg_fcp_io_channel;
8654
8655 /* Associate next IO channel with CPU */
8656 cpup->channel_id = chann[used_chann];
8657 num_io_channel++;
8658 used_chann++;
8659 if (used_chann == saved_chann)
8660 used_chann = 0;
8661
8662 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8663 "3331 Set IO_CHANN "
8664 "CPU %d channel %d\n",
8665 idx, cpup->channel_id);
8666 }
8667out:
8668 cpu++;
8669 if (cpu >= phba->sli4_hba.num_present_cpu) {
8670 cpup = phba->sli4_hba.cpu_map;
8671 cpu = 0;
8672 } else {
8673 cpup++;
8674 }
8675 }
8676 }
8677
8678 if (phba->sli4_hba.num_online_cpu != phba->sli4_hba.num_present_cpu) {
8679 cpup = phba->sli4_hba.cpu_map;
8680 for (idx = 0; idx < phba->sli4_hba.num_present_cpu; idx++) {
8681 if (cpup->channel_id == LPFC_VECTOR_MAP_EMPTY) {
8682 cpup->channel_id = 0;
8683 num_io_channel++;
8684
8685 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8686 "3332 Assign IO_CHANN "
8687 "CPU %d channel %d\n",
8688 idx, cpup->channel_id);
8689 }
8690 cpup++;
8691 }
8692 }
8693
8694 /* Sanity check */
8695 if (num_io_channel != phba->sli4_hba.num_present_cpu)
8696 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8697 "3333 Set affinity mismatch:"
James Smartec2087a2013-09-06 12:20:36 -04008698 "%d chann != %d cpus: %d vectors\n",
James Smart7bb03bb2013-04-17 20:19:16 -04008699 num_io_channel, phba->sli4_hba.num_present_cpu,
8700 vectors);
8701
James Smartec2087a2013-09-06 12:20:36 -04008702 /* Enable using cpu affinity for scheduling */
James Smart7bb03bb2013-04-17 20:19:16 -04008703 phba->cfg_fcp_io_sched = LPFC_FCP_SCHED_BY_CPU;
8704 return 1;
8705}
8706
8707
8708/**
James Smartda0436e2009-05-22 14:51:39 -04008709 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
8710 * @phba: pointer to lpfc hba data structure.
8711 *
8712 * This routine is invoked to enable the MSI-X interrupt vectors to device
8713 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
8714 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
8715 * enables either all or nothing, depending on the current availability of
8716 * PCI vector resources. The device driver is responsible for calling the
8717 * individual request_irq() to register each MSI-X vector with a interrupt
8718 * handler, which is done in this function. Note that later when device is
8719 * unloading, the driver should always call free_irq() on all MSI-X vectors
8720 * it has done request_irq() on before calling pci_disable_msix(). Failure
8721 * to do so results in a BUG_ON() and a device will be left with MSI-X
8722 * enabled and leaks its vectors.
8723 *
8724 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008725 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008726 * other values - error
8727 **/
8728static int
8729lpfc_sli4_enable_msix(struct lpfc_hba *phba)
8730{
James Smart75baf692010-06-08 18:31:21 -04008731 int vectors, rc, index;
James Smartda0436e2009-05-22 14:51:39 -04008732
8733 /* Set up MSI-X multi-message vectors */
James Smart82c3e9b2012-09-29 11:29:50 -04008734 for (index = 0; index < phba->cfg_fcp_io_channel; index++)
James Smartda0436e2009-05-22 14:51:39 -04008735 phba->sli4_hba.msix_entries[index].entry = index;
8736
8737 /* Configure MSI-X capability structure */
James Smart82c3e9b2012-09-29 11:29:50 -04008738 vectors = phba->cfg_fcp_io_channel;
James Smart1ba981f2014-02-20 09:56:45 -05008739 if (phba->cfg_fof) {
8740 phba->sli4_hba.msix_entries[index].entry = index;
8741 vectors++;
8742 }
James Smart75baf692010-06-08 18:31:21 -04008743enable_msix_vectors:
James Smartda0436e2009-05-22 14:51:39 -04008744 rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
James Smart75baf692010-06-08 18:31:21 -04008745 vectors);
8746 if (rc > 1) {
8747 vectors = rc;
8748 goto enable_msix_vectors;
8749 } else if (rc) {
James Smartda0436e2009-05-22 14:51:39 -04008750 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8751 "0484 PCI enable MSI-X failed (%d)\n", rc);
8752 goto msi_fail_out;
8753 }
James Smart75baf692010-06-08 18:31:21 -04008754
James Smartda0436e2009-05-22 14:51:39 -04008755 /* Log MSI-X vector assignment */
James Smart75baf692010-06-08 18:31:21 -04008756 for (index = 0; index < vectors; index++)
James Smartda0436e2009-05-22 14:51:39 -04008757 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8758 "0489 MSI-X entry[%d]: vector=x%x "
8759 "message=%d\n", index,
8760 phba->sli4_hba.msix_entries[index].vector,
8761 phba->sli4_hba.msix_entries[index].entry);
James Smart67d12732012-08-03 12:36:13 -04008762
James Smart7bb03bb2013-04-17 20:19:16 -04008763 /* Assign MSI-X vectors to interrupt handlers */
James Smart67d12732012-08-03 12:36:13 -04008764 for (index = 0; index < vectors; index++) {
James Smart4305f182012-08-03 12:36:33 -04008765 memset(&phba->sli4_hba.handler_name[index], 0, 16);
8766 sprintf((char *)&phba->sli4_hba.handler_name[index],
8767 LPFC_DRIVER_HANDLER_NAME"%d", index);
James Smartda0436e2009-05-22 14:51:39 -04008768
James Smart67d12732012-08-03 12:36:13 -04008769 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8770 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
James Smartba20c852012-08-03 12:36:52 -04008771 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].fcp_eq_in_use, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008772 if (phba->cfg_fof && (index == (vectors - 1)))
8773 rc = request_irq(
8774 phba->sli4_hba.msix_entries[index].vector,
8775 &lpfc_sli4_fof_intr_handler, IRQF_SHARED,
8776 (char *)&phba->sli4_hba.handler_name[index],
8777 &phba->sli4_hba.fcp_eq_hdl[index]);
8778 else
8779 rc = request_irq(
8780 phba->sli4_hba.msix_entries[index].vector,
James Smart67d12732012-08-03 12:36:13 -04008781 &lpfc_sli4_hba_intr_handler, IRQF_SHARED,
James Smart4305f182012-08-03 12:36:33 -04008782 (char *)&phba->sli4_hba.handler_name[index],
James Smart67d12732012-08-03 12:36:13 -04008783 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -04008784 if (rc) {
8785 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8786 "0486 MSI-X fast-path (%d) "
8787 "request_irq failed (%d)\n", index, rc);
8788 goto cfg_fail_out;
8789 }
8790 }
8791
James Smart1ba981f2014-02-20 09:56:45 -05008792 if (phba->cfg_fof)
8793 vectors--;
8794
James Smart82c3e9b2012-09-29 11:29:50 -04008795 if (vectors != phba->cfg_fcp_io_channel) {
8796 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8797 "3238 Reducing IO channels to match number of "
8798 "MSI-X vectors, requested %d got %d\n",
8799 phba->cfg_fcp_io_channel, vectors);
8800 phba->cfg_fcp_io_channel = vectors;
8801 }
James Smart7bb03bb2013-04-17 20:19:16 -04008802
8803 lpfc_sli4_set_affinity(phba, vectors);
James Smartda0436e2009-05-22 14:51:39 -04008804 return rc;
8805
8806cfg_fail_out:
8807 /* free the irq already requested */
James Smartacbd8612013-09-06 12:21:19 -04008808 for (--index; index >= 0; index--) {
8809 irq_set_affinity_hint(phba->sli4_hba.msix_entries[index].
8810 vector, NULL);
James Smart67d12732012-08-03 12:36:13 -04008811 free_irq(phba->sli4_hba.msix_entries[index].vector,
8812 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartacbd8612013-09-06 12:21:19 -04008813 }
James Smartda0436e2009-05-22 14:51:39 -04008814
8815msi_fail_out:
8816 /* Unconfigure MSI-X capability structure */
8817 pci_disable_msix(phba->pcidev);
8818 return rc;
8819}
8820
8821/**
8822 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
8823 * @phba: pointer to lpfc hba data structure.
8824 *
8825 * This routine is invoked to release the MSI-X vectors and then disable the
8826 * MSI-X interrupt mode to device with SLI-4 interface spec.
8827 **/
8828static void
8829lpfc_sli4_disable_msix(struct lpfc_hba *phba)
8830{
8831 int index;
8832
8833 /* Free up MSI-X multi-message vectors */
James Smartacbd8612013-09-06 12:21:19 -04008834 for (index = 0; index < phba->cfg_fcp_io_channel; index++) {
8835 irq_set_affinity_hint(phba->sli4_hba.msix_entries[index].
8836 vector, NULL);
James Smartda0436e2009-05-22 14:51:39 -04008837 free_irq(phba->sli4_hba.msix_entries[index].vector,
James Smart67d12732012-08-03 12:36:13 -04008838 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartacbd8612013-09-06 12:21:19 -04008839 }
James Smart1ba981f2014-02-20 09:56:45 -05008840 if (phba->cfg_fof) {
8841 free_irq(phba->sli4_hba.msix_entries[index].vector,
8842 &phba->sli4_hba.fcp_eq_hdl[index]);
8843 }
James Smartda0436e2009-05-22 14:51:39 -04008844 /* Disable MSI-X */
8845 pci_disable_msix(phba->pcidev);
8846
8847 return;
8848}
8849
8850/**
8851 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
8852 * @phba: pointer to lpfc hba data structure.
8853 *
8854 * This routine is invoked to enable the MSI interrupt mode to device with
8855 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
8856 * to enable the MSI vector. The device driver is responsible for calling
8857 * the request_irq() to register MSI vector with a interrupt the handler,
8858 * which is done in this function.
8859 *
8860 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008861 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008862 * other values - error
8863 **/
8864static int
8865lpfc_sli4_enable_msi(struct lpfc_hba *phba)
8866{
8867 int rc, index;
8868
8869 rc = pci_enable_msi(phba->pcidev);
8870 if (!rc)
8871 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8872 "0487 PCI enable MSI mode success.\n");
8873 else {
8874 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8875 "0488 PCI enable MSI mode failed (%d)\n", rc);
8876 return rc;
8877 }
8878
8879 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
8880 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8881 if (rc) {
8882 pci_disable_msi(phba->pcidev);
8883 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8884 "0490 MSI request_irq failed (%d)\n", rc);
James Smart75baf692010-06-08 18:31:21 -04008885 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008886 }
8887
James Smart67d12732012-08-03 12:36:13 -04008888 for (index = 0; index < phba->cfg_fcp_io_channel; index++) {
James Smartda0436e2009-05-22 14:51:39 -04008889 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8890 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
8891 }
8892
James Smart1ba981f2014-02-20 09:56:45 -05008893 if (phba->cfg_fof) {
8894 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8895 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
8896 }
James Smart75baf692010-06-08 18:31:21 -04008897 return 0;
James Smartda0436e2009-05-22 14:51:39 -04008898}
8899
8900/**
8901 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
8902 * @phba: pointer to lpfc hba data structure.
8903 *
8904 * This routine is invoked to disable the MSI interrupt mode to device with
8905 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
8906 * done request_irq() on before calling pci_disable_msi(). Failure to do so
8907 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
8908 * its vector.
8909 **/
8910static void
8911lpfc_sli4_disable_msi(struct lpfc_hba *phba)
8912{
8913 free_irq(phba->pcidev->irq, phba);
8914 pci_disable_msi(phba->pcidev);
8915 return;
8916}
8917
8918/**
8919 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
8920 * @phba: pointer to lpfc hba data structure.
8921 *
8922 * This routine is invoked to enable device interrupt and associate driver's
8923 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
8924 * interface spec. Depends on the interrupt mode configured to the driver,
8925 * the driver will try to fallback from the configured interrupt mode to an
8926 * interrupt mode which is supported by the platform, kernel, and device in
8927 * the order of:
8928 * MSI-X -> MSI -> IRQ.
8929 *
8930 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008931 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008932 * other values - error
8933 **/
8934static uint32_t
8935lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
8936{
8937 uint32_t intr_mode = LPFC_INTR_ERROR;
8938 int retval, index;
8939
8940 if (cfg_mode == 2) {
8941 /* Preparation before conf_msi mbox cmd */
8942 retval = 0;
8943 if (!retval) {
8944 /* Now, try to enable MSI-X interrupt mode */
8945 retval = lpfc_sli4_enable_msix(phba);
8946 if (!retval) {
8947 /* Indicate initialization to MSI-X mode */
8948 phba->intr_type = MSIX;
8949 intr_mode = 2;
8950 }
8951 }
8952 }
8953
8954 /* Fallback to MSI if MSI-X initialization failed */
8955 if (cfg_mode >= 1 && phba->intr_type == NONE) {
8956 retval = lpfc_sli4_enable_msi(phba);
8957 if (!retval) {
8958 /* Indicate initialization to MSI mode */
8959 phba->intr_type = MSI;
8960 intr_mode = 1;
8961 }
8962 }
8963
8964 /* Fallback to INTx if both MSI-X/MSI initalization failed */
8965 if (phba->intr_type == NONE) {
8966 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
8967 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8968 if (!retval) {
8969 /* Indicate initialization to INTx mode */
8970 phba->intr_type = INTx;
8971 intr_mode = 0;
James Smart67d12732012-08-03 12:36:13 -04008972 for (index = 0; index < phba->cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04008973 index++) {
8974 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8975 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
James Smartba20c852012-08-03 12:36:52 -04008976 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].
8977 fcp_eq_in_use, 1);
James Smartda0436e2009-05-22 14:51:39 -04008978 }
James Smart1ba981f2014-02-20 09:56:45 -05008979 if (phba->cfg_fof) {
8980 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8981 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
8982 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].
8983 fcp_eq_in_use, 1);
8984 }
James Smartda0436e2009-05-22 14:51:39 -04008985 }
8986 }
8987 return intr_mode;
8988}
8989
8990/**
8991 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
8992 * @phba: pointer to lpfc hba data structure.
8993 *
8994 * This routine is invoked to disable device interrupt and disassociate
8995 * the driver's interrupt handler(s) from interrupt vector(s) to device
8996 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
8997 * will release the interrupt vector(s) for the message signaled interrupt.
8998 **/
8999static void
9000lpfc_sli4_disable_intr(struct lpfc_hba *phba)
9001{
9002 /* Disable the currently initialized interrupt mode */
9003 if (phba->intr_type == MSIX)
9004 lpfc_sli4_disable_msix(phba);
9005 else if (phba->intr_type == MSI)
9006 lpfc_sli4_disable_msi(phba);
9007 else if (phba->intr_type == INTx)
9008 free_irq(phba->pcidev->irq, phba);
9009
9010 /* Reset interrupt management states */
9011 phba->intr_type = NONE;
9012 phba->sli.slistat.sli_intr = 0;
9013
9014 return;
9015}
9016
9017/**
James Smart3772a992009-05-22 14:50:54 -04009018 * lpfc_unset_hba - Unset SLI3 hba device initialization
9019 * @phba: pointer to lpfc hba data structure.
9020 *
9021 * This routine is invoked to unset the HBA device initialization steps to
9022 * a device with SLI-3 interface spec.
9023 **/
9024static void
9025lpfc_unset_hba(struct lpfc_hba *phba)
9026{
9027 struct lpfc_vport *vport = phba->pport;
9028 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
9029
9030 spin_lock_irq(shost->host_lock);
9031 vport->load_flag |= FC_UNLOADING;
9032 spin_unlock_irq(shost->host_lock);
9033
James Smart72859902012-01-18 16:25:38 -05009034 kfree(phba->vpi_bmask);
9035 kfree(phba->vpi_ids);
9036
James Smart3772a992009-05-22 14:50:54 -04009037 lpfc_stop_hba_timers(phba);
9038
9039 phba->pport->work_port_events = 0;
9040
9041 lpfc_sli_hba_down(phba);
9042
9043 lpfc_sli_brdrestart(phba);
9044
9045 lpfc_sli_disable_intr(phba);
9046
9047 return;
9048}
9049
9050/**
James Smart5af5eee2010-10-22 11:06:38 -04009051 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
9052 * @phba: Pointer to HBA context object.
9053 *
9054 * This function is called in the SLI4 code path to wait for completion
9055 * of device's XRIs exchange busy. It will check the XRI exchange busy
9056 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
9057 * that, it will check the XRI exchange busy on outstanding FCP and ELS
9058 * I/Os every 30 seconds, log error message, and wait forever. Only when
9059 * all XRI exchange busy complete, the driver unload shall proceed with
9060 * invoking the function reset ioctl mailbox command to the CNA and the
9061 * the rest of the driver unload resource release.
9062 **/
9063static void
9064lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
9065{
9066 int wait_time = 0;
9067 int fcp_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
9068 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
9069
9070 while (!fcp_xri_cmpl || !els_xri_cmpl) {
9071 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
9072 if (!fcp_xri_cmpl)
9073 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9074 "2877 FCP XRI exchange busy "
9075 "wait time: %d seconds.\n",
9076 wait_time/1000);
9077 if (!els_xri_cmpl)
9078 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9079 "2878 ELS XRI exchange busy "
9080 "wait time: %d seconds.\n",
9081 wait_time/1000);
9082 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
9083 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
9084 } else {
9085 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
9086 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
9087 }
9088 fcp_xri_cmpl =
9089 list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
9090 els_xri_cmpl =
9091 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
9092 }
9093}
9094
9095/**
James Smartda0436e2009-05-22 14:51:39 -04009096 * lpfc_sli4_hba_unset - Unset the fcoe hba
9097 * @phba: Pointer to HBA context object.
9098 *
9099 * This function is called in the SLI4 code path to reset the HBA's FCoE
9100 * function. The caller is not required to hold any lock. This routine
9101 * issues PCI function reset mailbox command to reset the FCoE function.
9102 * At the end of the function, it calls lpfc_hba_down_post function to
9103 * free any pending commands.
9104 **/
9105static void
9106lpfc_sli4_hba_unset(struct lpfc_hba *phba)
9107{
9108 int wait_cnt = 0;
9109 LPFC_MBOXQ_t *mboxq;
James Smart912e3ac2011-05-24 11:42:11 -04009110 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -04009111
9112 lpfc_stop_hba_timers(phba);
9113 phba->sli4_hba.intr_enable = 0;
9114
9115 /*
9116 * Gracefully wait out the potential current outstanding asynchronous
9117 * mailbox command.
9118 */
9119
9120 /* First, block any pending async mailbox command from posted */
9121 spin_lock_irq(&phba->hbalock);
9122 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
9123 spin_unlock_irq(&phba->hbalock);
9124 /* Now, trying to wait it out if we can */
9125 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9126 msleep(10);
9127 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
9128 break;
9129 }
9130 /* Forcefully release the outstanding mailbox command if timed out */
9131 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9132 spin_lock_irq(&phba->hbalock);
9133 mboxq = phba->sli.mbox_active;
9134 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
9135 __lpfc_mbox_cmpl_put(phba, mboxq);
9136 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9137 phba->sli.mbox_active = NULL;
9138 spin_unlock_irq(&phba->hbalock);
9139 }
9140
James Smart5af5eee2010-10-22 11:06:38 -04009141 /* Abort all iocbs associated with the hba */
9142 lpfc_sli_hba_iocb_abort(phba);
9143
9144 /* Wait for completion of device XRI exchange busy */
9145 lpfc_sli4_xri_exchange_busy_wait(phba);
9146
James Smartda0436e2009-05-22 14:51:39 -04009147 /* Disable PCI subsystem interrupt */
9148 lpfc_sli4_disable_intr(phba);
9149
James Smart912e3ac2011-05-24 11:42:11 -04009150 /* Disable SR-IOV if enabled */
9151 if (phba->cfg_sriov_nr_virtfn)
9152 pci_disable_sriov(pdev);
9153
James Smartda0436e2009-05-22 14:51:39 -04009154 /* Stop kthread signal shall trigger work_done one more time */
9155 kthread_stop(phba->worker_thread);
9156
James Smart3677a3a2010-09-29 11:19:14 -04009157 /* Reset SLI4 HBA FCoE function */
9158 lpfc_pci_function_reset(phba);
James Smart5350d872011-10-10 21:33:49 -04009159 lpfc_sli4_queue_destroy(phba);
James Smart3677a3a2010-09-29 11:19:14 -04009160
James Smartda0436e2009-05-22 14:51:39 -04009161 /* Stop the SLI4 device port */
9162 phba->pport->work_port_events = 0;
9163}
9164
James Smart28baac72010-02-12 14:42:03 -05009165 /**
9166 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
9167 * @phba: Pointer to HBA context object.
9168 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
9169 *
9170 * This function is called in the SLI4 code path to read the port's
9171 * sli4 capabilities.
9172 *
9173 * This function may be be called from any context that can block-wait
9174 * for the completion. The expectation is that this routine is called
9175 * typically from probe_one or from the online routine.
9176 **/
9177int
9178lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9179{
9180 int rc;
9181 struct lpfc_mqe *mqe;
9182 struct lpfc_pc_sli4_params *sli4_params;
9183 uint32_t mbox_tmo;
9184
9185 rc = 0;
9186 mqe = &mboxq->u.mqe;
9187
9188 /* Read the port's SLI4 Parameters port capabilities */
James Smartfedd3b72011-02-16 12:39:24 -05009189 lpfc_pc_sli4_params(mboxq);
James Smart28baac72010-02-12 14:42:03 -05009190 if (!phba->sli4_hba.intr_enable)
9191 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9192 else {
James Smarta183a152011-10-10 21:32:43 -04009193 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart28baac72010-02-12 14:42:03 -05009194 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
9195 }
9196
9197 if (unlikely(rc))
9198 return 1;
9199
9200 sli4_params = &phba->sli4_hba.pc_sli4_params;
9201 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
9202 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
9203 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
9204 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
9205 &mqe->un.sli4_params);
9206 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
9207 &mqe->un.sli4_params);
9208 sli4_params->proto_types = mqe->un.sli4_params.word3;
9209 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
9210 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
9211 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
9212 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
9213 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
9214 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
9215 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
9216 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
9217 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
9218 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
9219 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
9220 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
9221 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
9222 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
9223 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
9224 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
9225 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
9226 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
9227 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
9228 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
James Smart05580562011-05-24 11:40:48 -04009229
9230 /* Make sure that sge_supp_len can be handled by the driver */
9231 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
9232 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
9233
James Smart28baac72010-02-12 14:42:03 -05009234 return rc;
9235}
9236
James Smartda0436e2009-05-22 14:51:39 -04009237/**
James Smartfedd3b72011-02-16 12:39:24 -05009238 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
9239 * @phba: Pointer to HBA context object.
9240 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
9241 *
9242 * This function is called in the SLI4 code path to read the port's
9243 * sli4 capabilities.
9244 *
9245 * This function may be be called from any context that can block-wait
9246 * for the completion. The expectation is that this routine is called
9247 * typically from probe_one or from the online routine.
9248 **/
9249int
9250lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9251{
9252 int rc;
9253 struct lpfc_mqe *mqe = &mboxq->u.mqe;
9254 struct lpfc_pc_sli4_params *sli4_params;
James Smarta183a152011-10-10 21:32:43 -04009255 uint32_t mbox_tmo;
James Smartfedd3b72011-02-16 12:39:24 -05009256 int length;
9257 struct lpfc_sli4_parameters *mbx_sli4_parameters;
9258
James Smart6d368e52011-05-24 11:44:12 -04009259 /*
9260 * By default, the driver assumes the SLI4 port requires RPI
9261 * header postings. The SLI4_PARAM response will correct this
9262 * assumption.
9263 */
9264 phba->sli4_hba.rpi_hdrs_in_use = 1;
9265
James Smartfedd3b72011-02-16 12:39:24 -05009266 /* Read the port's SLI4 Config Parameters */
9267 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
9268 sizeof(struct lpfc_sli4_cfg_mhdr));
9269 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
9270 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
9271 length, LPFC_SLI4_MBX_EMBED);
9272 if (!phba->sli4_hba.intr_enable)
9273 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -04009274 else {
9275 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
9276 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
9277 }
James Smartfedd3b72011-02-16 12:39:24 -05009278 if (unlikely(rc))
9279 return rc;
9280 sli4_params = &phba->sli4_hba.pc_sli4_params;
9281 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
9282 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
9283 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
9284 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
9285 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
9286 mbx_sli4_parameters);
9287 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
9288 mbx_sli4_parameters);
9289 if (bf_get(cfg_phwq, mbx_sli4_parameters))
9290 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
9291 else
9292 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
9293 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
9294 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
James Smart1ba981f2014-02-20 09:56:45 -05009295 sli4_params->oas_supported = bf_get(cfg_oas, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -05009296 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
9297 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
9298 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
9299 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
James Smart0c651872013-07-15 18:33:23 -04009300 sli4_params->wqsize = bf_get(cfg_wqsize, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -05009301 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
9302 mbx_sli4_parameters);
9303 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
9304 mbx_sli4_parameters);
James Smart6d368e52011-05-24 11:44:12 -04009305 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
9306 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
James Smart05580562011-05-24 11:40:48 -04009307
9308 /* Make sure that sge_supp_len can be handled by the driver */
9309 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
9310 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
9311
James Smartfedd3b72011-02-16 12:39:24 -05009312 return 0;
9313}
9314
9315/**
James Smart3772a992009-05-22 14:50:54 -04009316 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
9317 * @pdev: pointer to PCI device
9318 * @pid: pointer to PCI device identifier
9319 *
9320 * This routine is to be called to attach a device with SLI-3 interface spec
9321 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
9322 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
9323 * information of the device and driver to see if the driver state that it can
9324 * support this kind of device. If the match is successful, the driver core
9325 * invokes this routine. If this routine determines it can claim the HBA, it
9326 * does all the initialization that it needs to do to handle the HBA properly.
9327 *
9328 * Return code
9329 * 0 - driver can claim the device
9330 * negative value - driver can not claim the device
9331 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009332static int
James Smart3772a992009-05-22 14:50:54 -04009333lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
9334{
9335 struct lpfc_hba *phba;
9336 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04009337 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -04009338 int error;
9339 uint32_t cfg_mode, intr_mode;
9340
9341 /* Allocate memory for HBA structure */
9342 phba = lpfc_hba_alloc(pdev);
9343 if (!phba)
9344 return -ENOMEM;
9345
9346 /* Perform generic PCI device enabling operation */
9347 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -04009348 if (error)
James Smart3772a992009-05-22 14:50:54 -04009349 goto out_free_phba;
James Smart3772a992009-05-22 14:50:54 -04009350
9351 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
9352 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
9353 if (error)
9354 goto out_disable_pci_dev;
9355
9356 /* Set up SLI-3 specific device PCI memory space */
9357 error = lpfc_sli_pci_mem_setup(phba);
9358 if (error) {
9359 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9360 "1402 Failed to set up pci memory space.\n");
9361 goto out_disable_pci_dev;
9362 }
9363
9364 /* Set up phase-1 common device driver resources */
9365 error = lpfc_setup_driver_resource_phase1(phba);
9366 if (error) {
9367 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9368 "1403 Failed to set up driver resource.\n");
9369 goto out_unset_pci_mem_s3;
9370 }
9371
9372 /* Set up SLI-3 specific device driver resources */
9373 error = lpfc_sli_driver_resource_setup(phba);
9374 if (error) {
9375 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9376 "1404 Failed to set up driver resource.\n");
9377 goto out_unset_pci_mem_s3;
9378 }
9379
9380 /* Initialize and populate the iocb list per host */
9381 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
9382 if (error) {
9383 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9384 "1405 Failed to initialize iocb list.\n");
9385 goto out_unset_driver_resource_s3;
9386 }
9387
9388 /* Set up common device driver resources */
9389 error = lpfc_setup_driver_resource_phase2(phba);
9390 if (error) {
9391 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9392 "1406 Failed to set up driver resource.\n");
9393 goto out_free_iocb_list;
9394 }
9395
James Smart079b5c92011-08-21 21:48:49 -04009396 /* Get the default values for Model Name and Description */
9397 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
9398
James Smart3772a992009-05-22 14:50:54 -04009399 /* Create SCSI host to the physical port */
9400 error = lpfc_create_shost(phba);
9401 if (error) {
9402 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9403 "1407 Failed to create scsi host.\n");
9404 goto out_unset_driver_resource;
9405 }
9406
9407 /* Configure sysfs attributes */
9408 vport = phba->pport;
9409 error = lpfc_alloc_sysfs_attr(vport);
9410 if (error) {
9411 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9412 "1476 Failed to allocate sysfs attr\n");
9413 goto out_destroy_shost;
9414 }
9415
James Smart6669f9b2009-10-02 15:16:45 -04009416 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -04009417 /* Now, trying to enable interrupt and bring up the device */
9418 cfg_mode = phba->cfg_use_msi;
9419 while (true) {
9420 /* Put device to a known state before enabling interrupt */
9421 lpfc_stop_port(phba);
9422 /* Configure and enable interrupt */
9423 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
9424 if (intr_mode == LPFC_INTR_ERROR) {
9425 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9426 "0431 Failed to enable interrupt.\n");
9427 error = -ENODEV;
9428 goto out_free_sysfs_attr;
9429 }
9430 /* SLI-3 HBA setup */
9431 if (lpfc_sli_hba_setup(phba)) {
9432 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9433 "1477 Failed to set up hba\n");
9434 error = -ENODEV;
9435 goto out_remove_device;
9436 }
9437
9438 /* Wait 50ms for the interrupts of previous mailbox commands */
9439 msleep(50);
9440 /* Check active interrupts on message signaled interrupts */
9441 if (intr_mode == 0 ||
9442 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
9443 /* Log the current active interrupt mode */
9444 phba->intr_mode = intr_mode;
9445 lpfc_log_intr_mode(phba, intr_mode);
9446 break;
9447 } else {
9448 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9449 "0447 Configure interrupt mode (%d) "
9450 "failed active interrupt test.\n",
9451 intr_mode);
9452 /* Disable the current interrupt mode */
9453 lpfc_sli_disable_intr(phba);
9454 /* Try next level of interrupt mode */
9455 cfg_mode = --intr_mode;
9456 }
9457 }
9458
9459 /* Perform post initialization setup */
9460 lpfc_post_init_setup(phba);
9461
9462 /* Check if there are static vports to be created. */
9463 lpfc_create_static_vport(phba);
9464
9465 return 0;
9466
9467out_remove_device:
9468 lpfc_unset_hba(phba);
9469out_free_sysfs_attr:
9470 lpfc_free_sysfs_attr(vport);
9471out_destroy_shost:
9472 lpfc_destroy_shost(phba);
9473out_unset_driver_resource:
9474 lpfc_unset_driver_resource_phase2(phba);
9475out_free_iocb_list:
9476 lpfc_free_iocb_list(phba);
9477out_unset_driver_resource_s3:
9478 lpfc_sli_driver_resource_unset(phba);
9479out_unset_pci_mem_s3:
9480 lpfc_sli_pci_mem_unset(phba);
9481out_disable_pci_dev:
9482 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04009483 if (shost)
9484 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -04009485out_free_phba:
9486 lpfc_hba_free(phba);
9487 return error;
9488}
9489
9490/**
9491 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -04009492 * @pdev: pointer to PCI device
9493 *
James Smart3772a992009-05-22 14:50:54 -04009494 * This routine is to be called to disattach a device with SLI-3 interface
9495 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
9496 * removed from PCI bus, it performs all the necessary cleanup for the HBA
9497 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -04009498 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009499static void
James Smart3772a992009-05-22 14:50:54 -04009500lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -05009501{
James Smart2e0fef82007-06-17 19:56:36 -05009502 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9503 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -05009504 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -05009505 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -05009506 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05009507 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
9508
James Smart549e55c2007-08-02 11:09:51 -04009509 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04009510 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04009511 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009512
James Smart858c9f62007-06-17 19:56:39 -05009513 lpfc_free_sysfs_attr(vport);
9514
James Smarteada2722008-12-04 22:39:13 -05009515 /* Release all the vports against this physical port */
9516 vports = lpfc_create_vport_work_array(phba);
9517 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -04009518 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
9519 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
9520 continue;
James Smarteada2722008-12-04 22:39:13 -05009521 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -04009522 }
James Smarteada2722008-12-04 22:39:13 -05009523 lpfc_destroy_vport_work_array(phba, vports);
9524
9525 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -05009526 fc_remove_host(shost);
9527 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04009528 lpfc_cleanup(vport);
9529
James Smart2e0fef82007-06-17 19:56:36 -05009530 /*
9531 * Bring down the SLI Layer. This step disable all interrupts,
9532 * clears the rings, discards all mailbox commands, and resets
9533 * the HBA.
9534 */
James Smarta257bf92009-04-06 18:48:10 -04009535
Justin P. Mattock48e34d02010-12-30 15:07:58 -08009536 /* HBA interrupt will be disabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -05009537 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -04009538 /* Stop kthread signal shall trigger work_done one more time */
9539 kthread_stop(phba->worker_thread);
9540 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -05009541 lpfc_sli_brdrestart(phba);
9542
James Smart72859902012-01-18 16:25:38 -05009543 kfree(phba->vpi_bmask);
9544 kfree(phba->vpi_ids);
9545
James Smart3772a992009-05-22 14:50:54 -04009546 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05009547 spin_lock_irq(&phba->hbalock);
9548 list_del_init(&vport->listentry);
9549 spin_unlock_irq(&phba->hbalock);
9550
James Smart858c9f62007-06-17 19:56:39 -05009551 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05009552
James Smart912e3ac2011-05-24 11:42:11 -04009553 /* Disable SR-IOV if enabled */
9554 if (phba->cfg_sriov_nr_virtfn)
9555 pci_disable_sriov(pdev);
9556
James Smart5b75da22008-12-04 22:39:35 -05009557 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009558 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -05009559
James Smart858c9f62007-06-17 19:56:39 -05009560 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05009561
9562 /*
9563 * Call scsi_free before mem_free since scsi bufs are released to their
9564 * corresponding pools here.
9565 */
9566 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -04009567 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -05009568
James Smart34b02dc2008-08-24 21:49:55 -04009569 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
9570 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -05009571
James Smart2e0fef82007-06-17 19:56:36 -05009572 /* Free resources associated with SLI2 interface */
9573 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -04009574 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -05009575
9576 /* unmap adapter SLIM and Control Registers */
9577 iounmap(phba->ctrl_regs_memmap_p);
9578 iounmap(phba->slim_memmap_p);
9579
James Smart3772a992009-05-22 14:50:54 -04009580 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -05009581
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05009582 pci_release_selected_regions(pdev, bars);
James Smart2e0fef82007-06-17 19:56:36 -05009583 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05009584}
9585
Linas Vepstas8d63f372007-02-14 14:28:36 -06009586/**
James Smart3772a992009-05-22 14:50:54 -04009587 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05009588 * @pdev: pointer to PCI device
9589 * @msg: power management message
9590 *
James Smart3772a992009-05-22 14:50:54 -04009591 * This routine is to be called from the kernel's PCI subsystem to support
9592 * system Power Management (PM) to device with SLI-3 interface spec. When
9593 * PM invokes this method, it quiesces the device by stopping the driver's
9594 * worker thread for the device, turning off device's interrupt and DMA,
9595 * and bring the device offline. Note that as the driver implements the
9596 * minimum PM requirements to a power-aware driver's PM support for the
9597 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
9598 * to the suspend() method call will be treated as SUSPEND and the driver will
9599 * fully reinitialize its device during resume() method call, the driver will
9600 * set device to PCI_D3hot state in PCI config space instead of setting it
9601 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -05009602 *
9603 * Return code
James Smart3772a992009-05-22 14:50:54 -04009604 * 0 - driver suspended the device
9605 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05009606 **/
9607static int
James Smart3772a992009-05-22 14:50:54 -04009608lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -05009609{
9610 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9611 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9612
9613 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9614 "0473 PCI device Power Management suspend.\n");
9615
9616 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -04009617 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3a55b532008-12-04 22:38:54 -05009618 lpfc_offline(phba);
9619 kthread_stop(phba->worker_thread);
9620
9621 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -04009622 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -05009623
9624 /* Save device state to PCI config space */
9625 pci_save_state(pdev);
9626 pci_set_power_state(pdev, PCI_D3hot);
9627
9628 return 0;
9629}
9630
9631/**
James Smart3772a992009-05-22 14:50:54 -04009632 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05009633 * @pdev: pointer to PCI device
9634 *
James Smart3772a992009-05-22 14:50:54 -04009635 * This routine is to be called from the kernel's PCI subsystem to support
9636 * system Power Management (PM) to device with SLI-3 interface spec. When PM
9637 * invokes this method, it restores the device's PCI config space state and
9638 * fully reinitializes the device and brings it online. Note that as the
9639 * driver implements the minimum PM requirements to a power-aware driver's
9640 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
9641 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
9642 * driver will fully reinitialize its device during resume() method call,
9643 * the device will be set to PCI_D0 directly in PCI config space before
9644 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -05009645 *
9646 * Return code
James Smart3772a992009-05-22 14:50:54 -04009647 * 0 - driver suspended the device
9648 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05009649 **/
9650static int
James Smart3772a992009-05-22 14:50:54 -04009651lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -05009652{
9653 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9654 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -05009655 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05009656 int error;
9657
9658 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9659 "0452 PCI device Power Management resume.\n");
9660
9661 /* Restore device state from PCI config space */
9662 pci_set_power_state(pdev, PCI_D0);
9663 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -04009664
James Smart1dfb5a42010-02-12 14:40:50 -05009665 /*
9666 * As the new kernel behavior of pci_restore_state() API call clears
9667 * device saved_state flag, need to save the restored state again.
9668 */
9669 pci_save_state(pdev);
9670
James Smart3a55b532008-12-04 22:38:54 -05009671 if (pdev->is_busmaster)
9672 pci_set_master(pdev);
9673
9674 /* Startup the kernel thread for this host adapter. */
9675 phba->worker_thread = kthread_run(lpfc_do_work, phba,
9676 "lpfc_worker_%d", phba->brd_no);
9677 if (IS_ERR(phba->worker_thread)) {
9678 error = PTR_ERR(phba->worker_thread);
9679 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9680 "0434 PM resume failed to start worker "
9681 "thread: error=x%x.\n", error);
9682 return error;
9683 }
9684
James Smart5b75da22008-12-04 22:39:35 -05009685 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009686 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05009687 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -05009688 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -05009689 "0430 PM resume Failed to enable interrupt\n");
9690 return -EIO;
9691 } else
9692 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05009693
9694 /* Restart HBA and bring it online */
9695 lpfc_sli_brdrestart(phba);
9696 lpfc_online(phba);
9697
James Smart5b75da22008-12-04 22:39:35 -05009698 /* Log the current active interrupt mode */
9699 lpfc_log_intr_mode(phba, phba->intr_mode);
9700
James Smart3a55b532008-12-04 22:38:54 -05009701 return 0;
9702}
9703
9704/**
James Smart891478a2009-11-18 15:40:23 -05009705 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
9706 * @phba: pointer to lpfc hba data structure.
9707 *
9708 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -04009709 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -05009710 **/
9711static void
9712lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
9713{
9714 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9715 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -04009716
9717 /*
9718 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
9719 * and let the SCSI mid-layer to retry them to recover.
9720 */
James Smartdb55fba2014-04-04 13:52:02 -04009721 lpfc_sli_abort_fcp_rings(phba);
James Smart891478a2009-11-18 15:40:23 -05009722}
9723
9724/**
James Smart0d878412009-10-02 15:16:56 -04009725 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
9726 * @phba: pointer to lpfc hba data structure.
9727 *
9728 * This routine is called to prepare the SLI3 device for PCI slot reset. It
9729 * disables the device interrupt and pci device, and aborts the internal FCP
9730 * pending I/Os.
9731 **/
9732static void
9733lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
9734{
James Smart0d878412009-10-02 15:16:56 -04009735 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05009736 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -04009737
James Smart75baf692010-06-08 18:31:21 -04009738 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -04009739 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -04009740
James Smarte2af0d22010-03-15 11:25:32 -04009741 /* Block all SCSI devices' I/Os on the host */
9742 lpfc_scsi_dev_block(phba);
9743
James Smartea714f32013-04-17 20:18:47 -04009744 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
9745 lpfc_sli_flush_fcp_rings(phba);
9746
James Smarte2af0d22010-03-15 11:25:32 -04009747 /* stop all timers */
9748 lpfc_stop_hba_timers(phba);
9749
James Smart0d878412009-10-02 15:16:56 -04009750 /* Disable interrupt and pci device */
9751 lpfc_sli_disable_intr(phba);
9752 pci_disable_device(phba->pcidev);
James Smart0d878412009-10-02 15:16:56 -04009753}
9754
9755/**
9756 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
9757 * @phba: pointer to lpfc hba data structure.
9758 *
9759 * This routine is called to prepare the SLI3 device for PCI slot permanently
9760 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
9761 * pending I/Os.
9762 **/
9763static void
James Smart75baf692010-06-08 18:31:21 -04009764lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
James Smart0d878412009-10-02 15:16:56 -04009765{
9766 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05009767 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -04009768 /* Block all SCSI devices' I/Os on the host */
9769 lpfc_scsi_dev_block(phba);
9770
9771 /* stop all timers */
9772 lpfc_stop_hba_timers(phba);
9773
James Smart0d878412009-10-02 15:16:56 -04009774 /* Clean up all driver's outstanding SCSI I/Os */
9775 lpfc_sli_flush_fcp_rings(phba);
9776}
9777
9778/**
James Smart3772a992009-05-22 14:50:54 -04009779 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -04009780 * @pdev: pointer to PCI device.
9781 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009782 *
James Smart3772a992009-05-22 14:50:54 -04009783 * This routine is called from the PCI subsystem for I/O error handling to
9784 * device with SLI-3 interface spec. This function is called by the PCI
9785 * subsystem after a PCI bus error affecting this device has been detected.
9786 * When this function is invoked, it will need to stop all the I/Os and
9787 * interrupt(s) to the device. Once that is done, it will return
9788 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
9789 * as desired.
James Smarte59058c2008-08-24 21:49:00 -04009790 *
9791 * Return codes
James Smart0d878412009-10-02 15:16:56 -04009792 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -04009793 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
9794 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -04009795 **/
James Smart3772a992009-05-22 14:50:54 -04009796static pci_ers_result_t
9797lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009798{
James Smart51ef4c22007-08-02 11:10:31 -04009799 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9800 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009801
James Smart0d878412009-10-02 15:16:56 -04009802 switch (state) {
9803 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -05009804 /* Non-fatal error, prepare for recovery */
9805 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -04009806 return PCI_ERS_RESULT_CAN_RECOVER;
9807 case pci_channel_io_frozen:
9808 /* Fatal error, prepare for slot reset */
9809 lpfc_sli_prep_dev_for_reset(phba);
9810 return PCI_ERS_RESULT_NEED_RESET;
9811 case pci_channel_io_perm_failure:
9812 /* Permanent failure, prepare for device down */
James Smart75baf692010-06-08 18:31:21 -04009813 lpfc_sli_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009814 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -04009815 default:
9816 /* Unknown state, prepare and request slot reset */
9817 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9818 "0472 Unknown PCI error state: x%x\n", state);
9819 lpfc_sli_prep_dev_for_reset(phba);
9820 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -04009821 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06009822}
9823
9824/**
James Smart3772a992009-05-22 14:50:54 -04009825 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -04009826 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009827 *
James Smart3772a992009-05-22 14:50:54 -04009828 * This routine is called from the PCI subsystem for error handling to
9829 * device with SLI-3 interface spec. This is called after PCI bus has been
9830 * reset to restart the PCI card from scratch, as if from a cold-boot.
9831 * During the PCI subsystem error recovery, after driver returns
9832 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
9833 * recovery and then call this routine before calling the .resume method
9834 * to recover the device. This function will initialize the HBA device,
9835 * enable the interrupt, but it will just put the HBA to offline state
9836 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -04009837 *
9838 * Return codes
James Smart3772a992009-05-22 14:50:54 -04009839 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
9840 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -06009841 */
James Smart3772a992009-05-22 14:50:54 -04009842static pci_ers_result_t
9843lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009844{
James Smart51ef4c22007-08-02 11:10:31 -04009845 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9846 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009847 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -05009848 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009849
9850 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +11009851 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -06009852 printk(KERN_ERR "lpfc: Cannot re-enable "
9853 "PCI device after reset.\n");
9854 return PCI_ERS_RESULT_DISCONNECT;
9855 }
9856
James Smart97207482008-12-04 22:39:19 -05009857 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05009858
9859 /*
9860 * As the new kernel behavior of pci_restore_state() API call clears
9861 * device saved_state flag, need to save the restored state again.
9862 */
9863 pci_save_state(pdev);
9864
James Smart97207482008-12-04 22:39:19 -05009865 if (pdev->is_busmaster)
9866 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009867
James Smart92d7f7b2007-06-17 19:56:38 -05009868 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04009869 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05009870 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009871
James Smart5b75da22008-12-04 22:39:35 -05009872 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009873 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05009874 if (intr_mode == LPFC_INTR_ERROR) {
9875 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9876 "0427 Cannot re-enable interrupt after "
9877 "slot reset.\n");
9878 return PCI_ERS_RESULT_DISCONNECT;
9879 } else
9880 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009881
James Smart75baf692010-06-08 18:31:21 -04009882 /* Take device offline, it will perform cleanup */
James Smart618a5232012-06-12 13:54:36 -04009883 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009884 lpfc_offline(phba);
9885 lpfc_sli_brdrestart(phba);
9886
James Smart5b75da22008-12-04 22:39:35 -05009887 /* Log the current active interrupt mode */
9888 lpfc_log_intr_mode(phba, phba->intr_mode);
9889
Linas Vepstas8d63f372007-02-14 14:28:36 -06009890 return PCI_ERS_RESULT_RECOVERED;
9891}
9892
9893/**
James Smart3772a992009-05-22 14:50:54 -04009894 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -04009895 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -06009896 *
James Smart3772a992009-05-22 14:50:54 -04009897 * This routine is called from the PCI subsystem for error handling to device
9898 * with SLI-3 interface spec. It is called when kernel error recovery tells
9899 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
9900 * error recovery. After this call, traffic can start to flow from this device
9901 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009902 */
James Smart3772a992009-05-22 14:50:54 -04009903static void
9904lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009905{
James Smart51ef4c22007-08-02 11:10:31 -04009906 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9907 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009908
James Smarte2af0d22010-03-15 11:25:32 -04009909 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -04009910 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -04009911
9912 /* Clean up Advanced Error Reporting (AER) if needed */
9913 if (phba->hba_flag & HBA_AER_ENABLED)
9914 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009915}
9916
James Smart3772a992009-05-22 14:50:54 -04009917/**
James Smartda0436e2009-05-22 14:51:39 -04009918 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
9919 * @phba: pointer to lpfc hba data structure.
9920 *
9921 * returns the number of ELS/CT IOCBs to reserve
9922 **/
9923int
9924lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
9925{
9926 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
9927
James Smartf1126682009-06-10 17:22:44 -04009928 if (phba->sli_rev == LPFC_SLI_REV4) {
9929 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -04009930 return 10;
James Smartf1126682009-06-10 17:22:44 -04009931 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -04009932 return 25;
James Smartf1126682009-06-10 17:22:44 -04009933 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -04009934 return 50;
James Smartf1126682009-06-10 17:22:44 -04009935 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -04009936 return 100;
James Smart8a9d2e82012-05-09 21:16:12 -04009937 else if (max_xri <= 1536)
James Smart6a9c52c2009-10-02 15:16:51 -04009938 return 150;
James Smart8a9d2e82012-05-09 21:16:12 -04009939 else if (max_xri <= 2048)
9940 return 200;
9941 else
9942 return 250;
James Smartf1126682009-06-10 17:22:44 -04009943 } else
9944 return 0;
James Smartda0436e2009-05-22 14:51:39 -04009945}
9946
9947/**
James Smart52d52442011-05-24 11:42:45 -04009948 * lpfc_write_firmware - attempt to write a firmware image to the port
James Smart52d52442011-05-24 11:42:45 -04009949 * @fw: pointer to firmware image returned from request_firmware.
James Smartce396282012-09-29 11:30:56 -04009950 * @phba: pointer to lpfc hba data structure.
James Smart52d52442011-05-24 11:42:45 -04009951 *
James Smart52d52442011-05-24 11:42:45 -04009952 **/
James Smartce396282012-09-29 11:30:56 -04009953static void
9954lpfc_write_firmware(const struct firmware *fw, void *context)
James Smart52d52442011-05-24 11:42:45 -04009955{
James Smartce396282012-09-29 11:30:56 -04009956 struct lpfc_hba *phba = (struct lpfc_hba *)context;
James Smart6b5151f2012-01-18 16:24:06 -05009957 char fwrev[FW_REV_STR_SIZE];
James Smartce396282012-09-29 11:30:56 -04009958 struct lpfc_grp_hdr *image;
James Smart52d52442011-05-24 11:42:45 -04009959 struct list_head dma_buffer_list;
9960 int i, rc = 0;
9961 struct lpfc_dmabuf *dmabuf, *next;
9962 uint32_t offset = 0, temp_offset = 0;
9963
James Smartc71ab862012-10-31 14:44:33 -04009964 /* It can be null in no-wait mode, sanity check */
James Smartce396282012-09-29 11:30:56 -04009965 if (!fw) {
9966 rc = -ENXIO;
9967 goto out;
9968 }
9969 image = (struct lpfc_grp_hdr *)fw->data;
9970
James Smart52d52442011-05-24 11:42:45 -04009971 INIT_LIST_HEAD(&dma_buffer_list);
James Smart079b5c92011-08-21 21:48:49 -04009972 if ((be32_to_cpu(image->magic_number) != LPFC_GROUP_OJECT_MAGIC_NUM) ||
9973 (bf_get_be32(lpfc_grp_hdr_file_type, image) !=
9974 LPFC_FILE_TYPE_GROUP) ||
9975 (bf_get_be32(lpfc_grp_hdr_id, image) != LPFC_FILE_ID_GROUP) ||
9976 (be32_to_cpu(image->size) != fw->size)) {
James Smart52d52442011-05-24 11:42:45 -04009977 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9978 "3022 Invalid FW image found. "
James Smart079b5c92011-08-21 21:48:49 -04009979 "Magic:%x Type:%x ID:%x\n",
9980 be32_to_cpu(image->magic_number),
9981 bf_get_be32(lpfc_grp_hdr_file_type, image),
9982 bf_get_be32(lpfc_grp_hdr_id, image));
James Smartce396282012-09-29 11:30:56 -04009983 rc = -EINVAL;
9984 goto release_out;
James Smart52d52442011-05-24 11:42:45 -04009985 }
9986 lpfc_decode_firmware_rev(phba, fwrev, 1);
James Smart88a2cfb2011-07-22 18:36:33 -04009987 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
James Smart52d52442011-05-24 11:42:45 -04009988 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartce396282012-09-29 11:30:56 -04009989 "3023 Updating Firmware, Current Version:%s "
James Smart52d52442011-05-24 11:42:45 -04009990 "New Version:%s\n",
James Smart88a2cfb2011-07-22 18:36:33 -04009991 fwrev, image->revision);
James Smart52d52442011-05-24 11:42:45 -04009992 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
9993 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
9994 GFP_KERNEL);
9995 if (!dmabuf) {
9996 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -04009997 goto release_out;
James Smart52d52442011-05-24 11:42:45 -04009998 }
9999 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
10000 SLI4_PAGE_SIZE,
10001 &dmabuf->phys,
10002 GFP_KERNEL);
10003 if (!dmabuf->virt) {
10004 kfree(dmabuf);
10005 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040010006 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010007 }
10008 list_add_tail(&dmabuf->list, &dma_buffer_list);
10009 }
10010 while (offset < fw->size) {
10011 temp_offset = offset;
10012 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
James Smart079b5c92011-08-21 21:48:49 -040010013 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
James Smart52d52442011-05-24 11:42:45 -040010014 memcpy(dmabuf->virt,
10015 fw->data + temp_offset,
James Smart079b5c92011-08-21 21:48:49 -040010016 fw->size - temp_offset);
10017 temp_offset = fw->size;
James Smart52d52442011-05-24 11:42:45 -040010018 break;
10019 }
James Smart52d52442011-05-24 11:42:45 -040010020 memcpy(dmabuf->virt, fw->data + temp_offset,
10021 SLI4_PAGE_SIZE);
James Smart88a2cfb2011-07-22 18:36:33 -040010022 temp_offset += SLI4_PAGE_SIZE;
James Smart52d52442011-05-24 11:42:45 -040010023 }
10024 rc = lpfc_wr_object(phba, &dma_buffer_list,
10025 (fw->size - offset), &offset);
James Smartce396282012-09-29 11:30:56 -040010026 if (rc)
10027 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010028 }
10029 rc = offset;
10030 }
James Smartce396282012-09-29 11:30:56 -040010031
10032release_out:
James Smart52d52442011-05-24 11:42:45 -040010033 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
10034 list_del(&dmabuf->list);
10035 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
10036 dmabuf->virt, dmabuf->phys);
10037 kfree(dmabuf);
10038 }
James Smartce396282012-09-29 11:30:56 -040010039 release_firmware(fw);
10040out:
10041 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartc71ab862012-10-31 14:44:33 -040010042 "3024 Firmware update done: %d.\n", rc);
James Smartce396282012-09-29 11:30:56 -040010043 return;
James Smart52d52442011-05-24 11:42:45 -040010044}
10045
10046/**
James Smartc71ab862012-10-31 14:44:33 -040010047 * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
10048 * @phba: pointer to lpfc hba data structure.
10049 *
10050 * This routine is called to perform Linux generic firmware upgrade on device
10051 * that supports such feature.
10052 **/
10053int
10054lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
10055{
10056 uint8_t file_name[ELX_MODEL_NAME_SIZE];
10057 int ret;
10058 const struct firmware *fw;
10059
10060 /* Only supported on SLI4 interface type 2 for now */
10061 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
10062 LPFC_SLI_INTF_IF_TYPE_2)
10063 return -EPERM;
10064
10065 snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
10066
10067 if (fw_upgrade == INT_FW_UPGRADE) {
10068 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
10069 file_name, &phba->pcidev->dev,
10070 GFP_KERNEL, (void *)phba,
10071 lpfc_write_firmware);
10072 } else if (fw_upgrade == RUN_FW_UPGRADE) {
10073 ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
10074 if (!ret)
10075 lpfc_write_firmware(fw, (void *)phba);
10076 } else {
10077 ret = -EINVAL;
10078 }
10079
10080 return ret;
10081}
10082
10083/**
James Smartda0436e2009-05-22 14:51:39 -040010084 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
10085 * @pdev: pointer to PCI device
10086 * @pid: pointer to PCI device identifier
10087 *
10088 * This routine is called from the kernel's PCI subsystem to device with
10089 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
10090 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
10091 * information of the device and driver to see if the driver state that it
10092 * can support this kind of device. If the match is successful, the driver
10093 * core invokes this routine. If this routine determines it can claim the HBA,
10094 * it does all the initialization that it needs to do to handle the HBA
10095 * properly.
10096 *
10097 * Return code
10098 * 0 - driver can claim the device
10099 * negative value - driver can not claim the device
10100 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010101static int
James Smartda0436e2009-05-22 14:51:39 -040010102lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
10103{
10104 struct lpfc_hba *phba;
10105 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -040010106 struct Scsi_Host *shost = NULL;
James Smartce396282012-09-29 11:30:56 -040010107 int error, ret;
James Smartda0436e2009-05-22 14:51:39 -040010108 uint32_t cfg_mode, intr_mode;
James Smart67d12732012-08-03 12:36:13 -040010109 int adjusted_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -040010110
10111 /* Allocate memory for HBA structure */
10112 phba = lpfc_hba_alloc(pdev);
10113 if (!phba)
10114 return -ENOMEM;
10115
10116 /* Perform generic PCI device enabling operation */
10117 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -040010118 if (error)
James Smartda0436e2009-05-22 14:51:39 -040010119 goto out_free_phba;
James Smartda0436e2009-05-22 14:51:39 -040010120
10121 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
10122 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
10123 if (error)
10124 goto out_disable_pci_dev;
10125
10126 /* Set up SLI-4 specific device PCI memory space */
10127 error = lpfc_sli4_pci_mem_setup(phba);
10128 if (error) {
10129 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10130 "1410 Failed to set up pci memory space.\n");
10131 goto out_disable_pci_dev;
10132 }
10133
10134 /* Set up phase-1 common device driver resources */
10135 error = lpfc_setup_driver_resource_phase1(phba);
10136 if (error) {
10137 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10138 "1411 Failed to set up driver resource.\n");
10139 goto out_unset_pci_mem_s4;
10140 }
10141
10142 /* Set up SLI-4 Specific device driver resources */
10143 error = lpfc_sli4_driver_resource_setup(phba);
10144 if (error) {
10145 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10146 "1412 Failed to set up driver resource.\n");
10147 goto out_unset_pci_mem_s4;
10148 }
10149
10150 /* Initialize and populate the iocb list per host */
James Smart2a9bf3d2010-06-07 15:24:45 -040010151
10152 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10153 "2821 initialize iocb list %d.\n",
10154 phba->cfg_iocb_cnt*1024);
10155 error = lpfc_init_iocb_list(phba, phba->cfg_iocb_cnt*1024);
10156
James Smartda0436e2009-05-22 14:51:39 -040010157 if (error) {
10158 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10159 "1413 Failed to initialize iocb list.\n");
10160 goto out_unset_driver_resource_s4;
10161 }
10162
James Smart19ca7602010-11-20 23:11:55 -050010163 INIT_LIST_HEAD(&phba->active_rrq_list);
James Smart7d791df2011-07-22 18:37:52 -040010164 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
James Smart19ca7602010-11-20 23:11:55 -050010165
James Smartda0436e2009-05-22 14:51:39 -040010166 /* Set up common device driver resources */
10167 error = lpfc_setup_driver_resource_phase2(phba);
10168 if (error) {
10169 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10170 "1414 Failed to set up driver resource.\n");
10171 goto out_free_iocb_list;
10172 }
10173
James Smart079b5c92011-08-21 21:48:49 -040010174 /* Get the default values for Model Name and Description */
10175 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
10176
James Smartda0436e2009-05-22 14:51:39 -040010177 /* Create SCSI host to the physical port */
10178 error = lpfc_create_shost(phba);
10179 if (error) {
10180 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10181 "1415 Failed to create scsi host.\n");
10182 goto out_unset_driver_resource;
10183 }
10184
10185 /* Configure sysfs attributes */
10186 vport = phba->pport;
10187 error = lpfc_alloc_sysfs_attr(vport);
10188 if (error) {
10189 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10190 "1416 Failed to allocate sysfs attr\n");
10191 goto out_destroy_shost;
10192 }
10193
James Smart6669f9b2009-10-02 15:16:45 -040010194 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -040010195 /* Now, trying to enable interrupt and bring up the device */
10196 cfg_mode = phba->cfg_use_msi;
James Smartda0436e2009-05-22 14:51:39 -040010197
James Smart7b15db32013-01-03 15:43:29 -050010198 /* Put device to a known state before enabling interrupt */
10199 lpfc_stop_port(phba);
10200 /* Configure and enable interrupt */
10201 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
10202 if (intr_mode == LPFC_INTR_ERROR) {
10203 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10204 "0426 Failed to enable interrupt.\n");
10205 error = -ENODEV;
10206 goto out_free_sysfs_attr;
James Smartda0436e2009-05-22 14:51:39 -040010207 }
James Smart7b15db32013-01-03 15:43:29 -050010208 /* Default to single EQ for non-MSI-X */
10209 if (phba->intr_type != MSIX)
10210 adjusted_fcp_io_channel = 1;
10211 else
10212 adjusted_fcp_io_channel = phba->cfg_fcp_io_channel;
10213 phba->cfg_fcp_io_channel = adjusted_fcp_io_channel;
10214 /* Set up SLI-4 HBA */
10215 if (lpfc_sli4_hba_setup(phba)) {
10216 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10217 "1421 Failed to set up hba\n");
10218 error = -ENODEV;
10219 goto out_disable_intr;
10220 }
10221
10222 /* Log the current active interrupt mode */
10223 phba->intr_mode = intr_mode;
10224 lpfc_log_intr_mode(phba, intr_mode);
James Smartda0436e2009-05-22 14:51:39 -040010225
10226 /* Perform post initialization setup */
10227 lpfc_post_init_setup(phba);
10228
James Smartc71ab862012-10-31 14:44:33 -040010229 /* check for firmware upgrade or downgrade */
10230 if (phba->cfg_request_firmware_upgrade)
10231 ret = lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
James Smart52d52442011-05-24 11:42:45 -040010232
James Smart1c6834a2009-07-19 10:01:26 -040010233 /* Check if there are static vports to be created. */
10234 lpfc_create_static_vport(phba);
James Smartda0436e2009-05-22 14:51:39 -040010235 return 0;
10236
10237out_disable_intr:
10238 lpfc_sli4_disable_intr(phba);
10239out_free_sysfs_attr:
10240 lpfc_free_sysfs_attr(vport);
10241out_destroy_shost:
10242 lpfc_destroy_shost(phba);
10243out_unset_driver_resource:
10244 lpfc_unset_driver_resource_phase2(phba);
10245out_free_iocb_list:
10246 lpfc_free_iocb_list(phba);
10247out_unset_driver_resource_s4:
10248 lpfc_sli4_driver_resource_unset(phba);
10249out_unset_pci_mem_s4:
10250 lpfc_sli4_pci_mem_unset(phba);
10251out_disable_pci_dev:
10252 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040010253 if (shost)
10254 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -040010255out_free_phba:
10256 lpfc_hba_free(phba);
10257 return error;
10258}
10259
10260/**
10261 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
10262 * @pdev: pointer to PCI device
10263 *
10264 * This routine is called from the kernel's PCI subsystem to device with
10265 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
10266 * removed from PCI bus, it performs all the necessary cleanup for the HBA
10267 * device to be removed from the PCI subsystem properly.
10268 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010269static void
James Smartda0436e2009-05-22 14:51:39 -040010270lpfc_pci_remove_one_s4(struct pci_dev *pdev)
10271{
10272 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10273 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
10274 struct lpfc_vport **vports;
10275 struct lpfc_hba *phba = vport->phba;
10276 int i;
10277
10278 /* Mark the device unloading flag */
10279 spin_lock_irq(&phba->hbalock);
10280 vport->load_flag |= FC_UNLOADING;
10281 spin_unlock_irq(&phba->hbalock);
10282
10283 /* Free the HBA sysfs attributes */
10284 lpfc_free_sysfs_attr(vport);
10285
10286 /* Release all the vports against this physical port */
10287 vports = lpfc_create_vport_work_array(phba);
10288 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040010289 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
10290 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
10291 continue;
James Smartda0436e2009-05-22 14:51:39 -040010292 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040010293 }
James Smartda0436e2009-05-22 14:51:39 -040010294 lpfc_destroy_vport_work_array(phba, vports);
10295
10296 /* Remove FC host and then SCSI host with the physical port */
10297 fc_remove_host(shost);
10298 scsi_remove_host(shost);
10299
10300 /* Perform cleanup on the physical port */
10301 lpfc_cleanup(vport);
10302
10303 /*
10304 * Bring down the SLI Layer. This step disables all interrupts,
10305 * clears the rings, discards all mailbox commands, and resets
10306 * the HBA FCoE function.
10307 */
10308 lpfc_debugfs_terminate(vport);
10309 lpfc_sli4_hba_unset(phba);
10310
10311 spin_lock_irq(&phba->hbalock);
10312 list_del_init(&vport->listentry);
10313 spin_unlock_irq(&phba->hbalock);
10314
James Smart3677a3a2010-09-29 11:19:14 -040010315 /* Perform scsi free before driver resource_unset since scsi
James Smartda0436e2009-05-22 14:51:39 -040010316 * buffers are released to their corresponding pools here.
10317 */
10318 lpfc_scsi_free(phba);
James Smart67d12732012-08-03 12:36:13 -040010319
James Smartda0436e2009-05-22 14:51:39 -040010320 lpfc_sli4_driver_resource_unset(phba);
10321
10322 /* Unmap adapter Control and Doorbell registers */
10323 lpfc_sli4_pci_mem_unset(phba);
10324
10325 /* Release PCI resources and disable device's PCI function */
10326 scsi_host_put(shost);
10327 lpfc_disable_pci_dev(phba);
10328
10329 /* Finally, free the driver's device data structure */
10330 lpfc_hba_free(phba);
10331
10332 return;
10333}
10334
10335/**
10336 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
10337 * @pdev: pointer to PCI device
10338 * @msg: power management message
10339 *
10340 * This routine is called from the kernel's PCI subsystem to support system
10341 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
10342 * this method, it quiesces the device by stopping the driver's worker
10343 * thread for the device, turning off device's interrupt and DMA, and bring
10344 * the device offline. Note that as the driver implements the minimum PM
10345 * requirements to a power-aware driver's PM support for suspend/resume -- all
10346 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
10347 * method call will be treated as SUSPEND and the driver will fully
10348 * reinitialize its device during resume() method call, the driver will set
10349 * device to PCI_D3hot state in PCI config space instead of setting it
10350 * according to the @msg provided by the PM.
10351 *
10352 * Return code
10353 * 0 - driver suspended the device
10354 * Error otherwise
10355 **/
10356static int
10357lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
10358{
10359 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10360 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10361
10362 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart75baf692010-06-08 18:31:21 -040010363 "2843 PCI device Power Management suspend.\n");
James Smartda0436e2009-05-22 14:51:39 -040010364
10365 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040010366 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smartda0436e2009-05-22 14:51:39 -040010367 lpfc_offline(phba);
10368 kthread_stop(phba->worker_thread);
10369
10370 /* Disable interrupt from device */
10371 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040010372 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -040010373
10374 /* Save device state to PCI config space */
10375 pci_save_state(pdev);
10376 pci_set_power_state(pdev, PCI_D3hot);
10377
10378 return 0;
10379}
10380
10381/**
10382 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
10383 * @pdev: pointer to PCI device
10384 *
10385 * This routine is called from the kernel's PCI subsystem to support system
10386 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
10387 * this method, it restores the device's PCI config space state and fully
10388 * reinitializes the device and brings it online. Note that as the driver
10389 * implements the minimum PM requirements to a power-aware driver's PM for
10390 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
10391 * to the suspend() method call will be treated as SUSPEND and the driver
10392 * will fully reinitialize its device during resume() method call, the device
10393 * will be set to PCI_D0 directly in PCI config space before restoring the
10394 * state.
10395 *
10396 * Return code
10397 * 0 - driver suspended the device
10398 * Error otherwise
10399 **/
10400static int
10401lpfc_pci_resume_one_s4(struct pci_dev *pdev)
10402{
10403 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10404 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10405 uint32_t intr_mode;
10406 int error;
10407
10408 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10409 "0292 PCI device Power Management resume.\n");
10410
10411 /* Restore device state from PCI config space */
10412 pci_set_power_state(pdev, PCI_D0);
10413 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050010414
10415 /*
10416 * As the new kernel behavior of pci_restore_state() API call clears
10417 * device saved_state flag, need to save the restored state again.
10418 */
10419 pci_save_state(pdev);
10420
James Smartda0436e2009-05-22 14:51:39 -040010421 if (pdev->is_busmaster)
10422 pci_set_master(pdev);
10423
10424 /* Startup the kernel thread for this host adapter. */
10425 phba->worker_thread = kthread_run(lpfc_do_work, phba,
10426 "lpfc_worker_%d", phba->brd_no);
10427 if (IS_ERR(phba->worker_thread)) {
10428 error = PTR_ERR(phba->worker_thread);
10429 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10430 "0293 PM resume failed to start worker "
10431 "thread: error=x%x.\n", error);
10432 return error;
10433 }
10434
10435 /* Configure and enable interrupt */
10436 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
10437 if (intr_mode == LPFC_INTR_ERROR) {
10438 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10439 "0294 PM resume Failed to enable interrupt\n");
10440 return -EIO;
10441 } else
10442 phba->intr_mode = intr_mode;
10443
10444 /* Restart HBA and bring it online */
10445 lpfc_sli_brdrestart(phba);
10446 lpfc_online(phba);
10447
10448 /* Log the current active interrupt mode */
10449 lpfc_log_intr_mode(phba, phba->intr_mode);
10450
10451 return 0;
10452}
10453
10454/**
James Smart75baf692010-06-08 18:31:21 -040010455 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
10456 * @phba: pointer to lpfc hba data structure.
10457 *
10458 * This routine is called to prepare the SLI4 device for PCI slot recover. It
10459 * aborts all the outstanding SCSI I/Os to the pci device.
10460 **/
10461static void
10462lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
10463{
James Smart75baf692010-06-08 18:31:21 -040010464 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10465 "2828 PCI channel I/O abort preparing for recovery\n");
10466 /*
10467 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
10468 * and let the SCSI mid-layer to retry them to recover.
10469 */
James Smartdb55fba2014-04-04 13:52:02 -040010470 lpfc_sli_abort_fcp_rings(phba);
James Smart75baf692010-06-08 18:31:21 -040010471}
10472
10473/**
10474 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
10475 * @phba: pointer to lpfc hba data structure.
10476 *
10477 * This routine is called to prepare the SLI4 device for PCI slot reset. It
10478 * disables the device interrupt and pci device, and aborts the internal FCP
10479 * pending I/Os.
10480 **/
10481static void
10482lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
10483{
10484 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10485 "2826 PCI channel disable preparing for reset\n");
10486
10487 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040010488 lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
James Smart75baf692010-06-08 18:31:21 -040010489
10490 /* Block all SCSI devices' I/Os on the host */
10491 lpfc_scsi_dev_block(phba);
10492
James Smartea714f32013-04-17 20:18:47 -040010493 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
10494 lpfc_sli_flush_fcp_rings(phba);
10495
James Smart75baf692010-06-08 18:31:21 -040010496 /* stop all timers */
10497 lpfc_stop_hba_timers(phba);
10498
10499 /* Disable interrupt and pci device */
10500 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040010501 lpfc_sli4_queue_destroy(phba);
James Smart75baf692010-06-08 18:31:21 -040010502 pci_disable_device(phba->pcidev);
James Smart75baf692010-06-08 18:31:21 -040010503}
10504
10505/**
10506 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
10507 * @phba: pointer to lpfc hba data structure.
10508 *
10509 * This routine is called to prepare the SLI4 device for PCI slot permanently
10510 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
10511 * pending I/Os.
10512 **/
10513static void
10514lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
10515{
10516 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10517 "2827 PCI channel permanent disable for failure\n");
10518
10519 /* Block all SCSI devices' I/Os on the host */
10520 lpfc_scsi_dev_block(phba);
10521
10522 /* stop all timers */
10523 lpfc_stop_hba_timers(phba);
10524
10525 /* Clean up all driver's outstanding SCSI I/Os */
10526 lpfc_sli_flush_fcp_rings(phba);
10527}
10528
10529/**
James Smartda0436e2009-05-22 14:51:39 -040010530 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
10531 * @pdev: pointer to PCI device.
10532 * @state: the current PCI connection state.
10533 *
10534 * This routine is called from the PCI subsystem for error handling to device
10535 * with SLI-4 interface spec. This function is called by the PCI subsystem
10536 * after a PCI bus error affecting this device has been detected. When this
10537 * function is invoked, it will need to stop all the I/Os and interrupt(s)
10538 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
10539 * for the PCI subsystem to perform proper recovery as desired.
10540 *
10541 * Return codes
10542 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10543 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10544 **/
10545static pci_ers_result_t
10546lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
10547{
James Smart75baf692010-06-08 18:31:21 -040010548 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10549 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10550
10551 switch (state) {
10552 case pci_channel_io_normal:
10553 /* Non-fatal error, prepare for recovery */
10554 lpfc_sli4_prep_dev_for_recover(phba);
10555 return PCI_ERS_RESULT_CAN_RECOVER;
10556 case pci_channel_io_frozen:
10557 /* Fatal error, prepare for slot reset */
10558 lpfc_sli4_prep_dev_for_reset(phba);
10559 return PCI_ERS_RESULT_NEED_RESET;
10560 case pci_channel_io_perm_failure:
10561 /* Permanent failure, prepare for device down */
10562 lpfc_sli4_prep_dev_for_perm_failure(phba);
10563 return PCI_ERS_RESULT_DISCONNECT;
10564 default:
10565 /* Unknown state, prepare and request slot reset */
10566 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10567 "2825 Unknown PCI error state: x%x\n", state);
10568 lpfc_sli4_prep_dev_for_reset(phba);
10569 return PCI_ERS_RESULT_NEED_RESET;
10570 }
James Smartda0436e2009-05-22 14:51:39 -040010571}
10572
10573/**
10574 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
10575 * @pdev: pointer to PCI device.
10576 *
10577 * This routine is called from the PCI subsystem for error handling to device
10578 * with SLI-4 interface spec. It is called after PCI bus has been reset to
10579 * restart the PCI card from scratch, as if from a cold-boot. During the
10580 * PCI subsystem error recovery, after the driver returns
10581 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
10582 * recovery and then call this routine before calling the .resume method to
10583 * recover the device. This function will initialize the HBA device, enable
10584 * the interrupt, but it will just put the HBA to offline state without
10585 * passing any I/O traffic.
10586 *
10587 * Return codes
10588 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10589 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10590 */
10591static pci_ers_result_t
10592lpfc_io_slot_reset_s4(struct pci_dev *pdev)
10593{
James Smart75baf692010-06-08 18:31:21 -040010594 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10595 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10596 struct lpfc_sli *psli = &phba->sli;
10597 uint32_t intr_mode;
10598
10599 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
10600 if (pci_enable_device_mem(pdev)) {
10601 printk(KERN_ERR "lpfc: Cannot re-enable "
10602 "PCI device after reset.\n");
10603 return PCI_ERS_RESULT_DISCONNECT;
10604 }
10605
10606 pci_restore_state(pdev);
James Smart0a96e972011-07-22 18:37:28 -040010607
10608 /*
10609 * As the new kernel behavior of pci_restore_state() API call clears
10610 * device saved_state flag, need to save the restored state again.
10611 */
10612 pci_save_state(pdev);
10613
James Smart75baf692010-06-08 18:31:21 -040010614 if (pdev->is_busmaster)
10615 pci_set_master(pdev);
10616
10617 spin_lock_irq(&phba->hbalock);
10618 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
10619 spin_unlock_irq(&phba->hbalock);
10620
10621 /* Configure and enable interrupt */
10622 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
10623 if (intr_mode == LPFC_INTR_ERROR) {
10624 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10625 "2824 Cannot re-enable interrupt after "
10626 "slot reset.\n");
10627 return PCI_ERS_RESULT_DISCONNECT;
10628 } else
10629 phba->intr_mode = intr_mode;
10630
10631 /* Log the current active interrupt mode */
10632 lpfc_log_intr_mode(phba, phba->intr_mode);
10633
James Smartda0436e2009-05-22 14:51:39 -040010634 return PCI_ERS_RESULT_RECOVERED;
10635}
10636
10637/**
10638 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
10639 * @pdev: pointer to PCI device
10640 *
10641 * This routine is called from the PCI subsystem for error handling to device
10642 * with SLI-4 interface spec. It is called when kernel error recovery tells
10643 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
10644 * error recovery. After this call, traffic can start to flow from this device
10645 * again.
10646 **/
10647static void
10648lpfc_io_resume_s4(struct pci_dev *pdev)
10649{
James Smart75baf692010-06-08 18:31:21 -040010650 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10651 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10652
10653 /*
10654 * In case of slot reset, as function reset is performed through
10655 * mailbox command which needs DMA to be enabled, this operation
10656 * has to be moved to the io resume phase. Taking device offline
10657 * will perform the necessary cleanup.
10658 */
10659 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
10660 /* Perform device reset */
James Smart618a5232012-06-12 13:54:36 -040010661 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040010662 lpfc_offline(phba);
10663 lpfc_sli_brdrestart(phba);
10664 /* Bring the device back online */
10665 lpfc_online(phba);
10666 }
10667
10668 /* Clean up Advanced Error Reporting (AER) if needed */
10669 if (phba->hba_flag & HBA_AER_ENABLED)
10670 pci_cleanup_aer_uncorrect_error_status(pdev);
James Smartda0436e2009-05-22 14:51:39 -040010671}
10672
10673/**
James Smart3772a992009-05-22 14:50:54 -040010674 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
10675 * @pdev: pointer to PCI device
10676 * @pid: pointer to PCI device identifier
10677 *
10678 * This routine is to be registered to the kernel's PCI subsystem. When an
10679 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
10680 * at PCI device-specific information of the device and driver to see if the
10681 * driver state that it can support this kind of device. If the match is
10682 * successful, the driver core invokes this routine. This routine dispatches
10683 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
10684 * do all the initialization that it needs to do to handle the HBA device
10685 * properly.
10686 *
10687 * Return code
10688 * 0 - driver can claim the device
10689 * negative value - driver can not claim the device
10690 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010691static int
James Smart3772a992009-05-22 14:50:54 -040010692lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
10693{
10694 int rc;
James Smart8fa38512009-07-19 10:01:03 -040010695 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -040010696
James Smart28baac72010-02-12 14:42:03 -050010697 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -040010698 return -ENODEV;
10699
James Smart8fa38512009-07-19 10:01:03 -040010700 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -050010701 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -040010702 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040010703 else
James Smart3772a992009-05-22 14:50:54 -040010704 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040010705
James Smart3772a992009-05-22 14:50:54 -040010706 return rc;
10707}
10708
10709/**
10710 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
10711 * @pdev: pointer to PCI device
10712 *
10713 * This routine is to be registered to the kernel's PCI subsystem. When an
10714 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
10715 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
10716 * remove routine, which will perform all the necessary cleanup for the
10717 * device to be removed from the PCI subsystem properly.
10718 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010719static void
James Smart3772a992009-05-22 14:50:54 -040010720lpfc_pci_remove_one(struct pci_dev *pdev)
10721{
10722 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10723 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10724
10725 switch (phba->pci_dev_grp) {
10726 case LPFC_PCI_DEV_LP:
10727 lpfc_pci_remove_one_s3(pdev);
10728 break;
James Smartda0436e2009-05-22 14:51:39 -040010729 case LPFC_PCI_DEV_OC:
10730 lpfc_pci_remove_one_s4(pdev);
10731 break;
James Smart3772a992009-05-22 14:50:54 -040010732 default:
10733 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10734 "1424 Invalid PCI device group: 0x%x\n",
10735 phba->pci_dev_grp);
10736 break;
10737 }
10738 return;
10739}
10740
10741/**
10742 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
10743 * @pdev: pointer to PCI device
10744 * @msg: power management message
10745 *
10746 * This routine is to be registered to the kernel's PCI subsystem to support
10747 * system Power Management (PM). When PM invokes this method, it dispatches
10748 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
10749 * suspend the device.
10750 *
10751 * Return code
10752 * 0 - driver suspended the device
10753 * Error otherwise
10754 **/
10755static int
10756lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
10757{
10758 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10759 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10760 int rc = -ENODEV;
10761
10762 switch (phba->pci_dev_grp) {
10763 case LPFC_PCI_DEV_LP:
10764 rc = lpfc_pci_suspend_one_s3(pdev, msg);
10765 break;
James Smartda0436e2009-05-22 14:51:39 -040010766 case LPFC_PCI_DEV_OC:
10767 rc = lpfc_pci_suspend_one_s4(pdev, msg);
10768 break;
James Smart3772a992009-05-22 14:50:54 -040010769 default:
10770 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10771 "1425 Invalid PCI device group: 0x%x\n",
10772 phba->pci_dev_grp);
10773 break;
10774 }
10775 return rc;
10776}
10777
10778/**
10779 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
10780 * @pdev: pointer to PCI device
10781 *
10782 * This routine is to be registered to the kernel's PCI subsystem to support
10783 * system Power Management (PM). When PM invokes this method, it dispatches
10784 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
10785 * resume the device.
10786 *
10787 * Return code
10788 * 0 - driver suspended the device
10789 * Error otherwise
10790 **/
10791static int
10792lpfc_pci_resume_one(struct pci_dev *pdev)
10793{
10794 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10795 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10796 int rc = -ENODEV;
10797
10798 switch (phba->pci_dev_grp) {
10799 case LPFC_PCI_DEV_LP:
10800 rc = lpfc_pci_resume_one_s3(pdev);
10801 break;
James Smartda0436e2009-05-22 14:51:39 -040010802 case LPFC_PCI_DEV_OC:
10803 rc = lpfc_pci_resume_one_s4(pdev);
10804 break;
James Smart3772a992009-05-22 14:50:54 -040010805 default:
10806 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10807 "1426 Invalid PCI device group: 0x%x\n",
10808 phba->pci_dev_grp);
10809 break;
10810 }
10811 return rc;
10812}
10813
10814/**
10815 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
10816 * @pdev: pointer to PCI device.
10817 * @state: the current PCI connection state.
10818 *
10819 * This routine is registered to the PCI subsystem for error handling. This
10820 * function is called by the PCI subsystem after a PCI bus error affecting
10821 * this device has been detected. When this routine is invoked, it dispatches
10822 * the action to the proper SLI-3 or SLI-4 device error detected handling
10823 * routine, which will perform the proper error detected operation.
10824 *
10825 * Return codes
10826 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10827 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10828 **/
10829static pci_ers_result_t
10830lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
10831{
10832 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10833 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10834 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
10835
10836 switch (phba->pci_dev_grp) {
10837 case LPFC_PCI_DEV_LP:
10838 rc = lpfc_io_error_detected_s3(pdev, state);
10839 break;
James Smartda0436e2009-05-22 14:51:39 -040010840 case LPFC_PCI_DEV_OC:
10841 rc = lpfc_io_error_detected_s4(pdev, state);
10842 break;
James Smart3772a992009-05-22 14:50:54 -040010843 default:
10844 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10845 "1427 Invalid PCI device group: 0x%x\n",
10846 phba->pci_dev_grp);
10847 break;
10848 }
10849 return rc;
10850}
10851
10852/**
10853 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
10854 * @pdev: pointer to PCI device.
10855 *
10856 * This routine is registered to the PCI subsystem for error handling. This
10857 * function is called after PCI bus has been reset to restart the PCI card
10858 * from scratch, as if from a cold-boot. When this routine is invoked, it
10859 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
10860 * routine, which will perform the proper device reset.
10861 *
10862 * Return codes
10863 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10864 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10865 **/
10866static pci_ers_result_t
10867lpfc_io_slot_reset(struct pci_dev *pdev)
10868{
10869 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10870 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10871 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
10872
10873 switch (phba->pci_dev_grp) {
10874 case LPFC_PCI_DEV_LP:
10875 rc = lpfc_io_slot_reset_s3(pdev);
10876 break;
James Smartda0436e2009-05-22 14:51:39 -040010877 case LPFC_PCI_DEV_OC:
10878 rc = lpfc_io_slot_reset_s4(pdev);
10879 break;
James Smart3772a992009-05-22 14:50:54 -040010880 default:
10881 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10882 "1428 Invalid PCI device group: 0x%x\n",
10883 phba->pci_dev_grp);
10884 break;
10885 }
10886 return rc;
10887}
10888
10889/**
10890 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
10891 * @pdev: pointer to PCI device
10892 *
10893 * This routine is registered to the PCI subsystem for error handling. It
10894 * is called when kernel error recovery tells the lpfc driver that it is
10895 * OK to resume normal PCI operation after PCI bus error recovery. When
10896 * this routine is invoked, it dispatches the action to the proper SLI-3
10897 * or SLI-4 device io_resume routine, which will resume the device operation.
10898 **/
10899static void
10900lpfc_io_resume(struct pci_dev *pdev)
10901{
10902 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10903 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10904
10905 switch (phba->pci_dev_grp) {
10906 case LPFC_PCI_DEV_LP:
10907 lpfc_io_resume_s3(pdev);
10908 break;
James Smartda0436e2009-05-22 14:51:39 -040010909 case LPFC_PCI_DEV_OC:
10910 lpfc_io_resume_s4(pdev);
10911 break;
James Smart3772a992009-05-22 14:50:54 -040010912 default:
10913 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10914 "1429 Invalid PCI device group: 0x%x\n",
10915 phba->pci_dev_grp);
10916 break;
10917 }
10918 return;
10919}
10920
James Smart1ba981f2014-02-20 09:56:45 -050010921/**
10922 * lpfc_sli4_oas_verify - Verify OAS is supported by this adapter
10923 * @phba: pointer to lpfc hba data structure.
10924 *
10925 * This routine checks to see if OAS is supported for this adapter. If
10926 * supported, the configure Flash Optimized Fabric flag is set. Otherwise,
10927 * the enable oas flag is cleared and the pool created for OAS device data
10928 * is destroyed.
10929 *
10930 **/
10931void
10932lpfc_sli4_oas_verify(struct lpfc_hba *phba)
10933{
10934
10935 if (!phba->cfg_EnableXLane)
10936 return;
10937
10938 if (phba->sli4_hba.pc_sli4_params.oas_supported) {
10939 phba->cfg_fof = 1;
10940 } else {
James Smartf38fa0b2014-04-04 13:52:21 -040010941 phba->cfg_fof = 0;
James Smart1ba981f2014-02-20 09:56:45 -050010942 if (phba->device_data_mem_pool)
10943 mempool_destroy(phba->device_data_mem_pool);
10944 phba->device_data_mem_pool = NULL;
10945 }
10946
10947 return;
10948}
10949
10950/**
10951 * lpfc_fof_queue_setup - Set up all the fof queues
10952 * @phba: pointer to lpfc hba data structure.
10953 *
10954 * This routine is invoked to set up all the fof queues for the FC HBA
10955 * operation.
10956 *
10957 * Return codes
10958 * 0 - successful
10959 * -ENOMEM - No available memory
10960 **/
10961int
10962lpfc_fof_queue_setup(struct lpfc_hba *phba)
10963{
10964 struct lpfc_sli *psli = &phba->sli;
10965 int rc;
10966
10967 rc = lpfc_eq_create(phba, phba->sli4_hba.fof_eq, LPFC_MAX_IMAX);
10968 if (rc)
10969 return -ENOMEM;
10970
James Smartf38fa0b2014-04-04 13:52:21 -040010971 if (phba->cfg_fof) {
James Smart1ba981f2014-02-20 09:56:45 -050010972
10973 rc = lpfc_cq_create(phba, phba->sli4_hba.oas_cq,
10974 phba->sli4_hba.fof_eq, LPFC_WCQ, LPFC_FCP);
10975 if (rc)
10976 goto out_oas_cq;
10977
10978 rc = lpfc_wq_create(phba, phba->sli4_hba.oas_wq,
10979 phba->sli4_hba.oas_cq, LPFC_FCP);
10980 if (rc)
10981 goto out_oas_wq;
10982
10983 phba->sli4_hba.oas_cq->pring = &psli->ring[LPFC_FCP_OAS_RING];
10984 phba->sli4_hba.oas_ring = &psli->ring[LPFC_FCP_OAS_RING];
10985 }
10986
10987 return 0;
10988
10989out_oas_wq:
James Smartf38fa0b2014-04-04 13:52:21 -040010990 lpfc_cq_destroy(phba, phba->sli4_hba.oas_cq);
James Smart1ba981f2014-02-20 09:56:45 -050010991out_oas_cq:
10992 lpfc_eq_destroy(phba, phba->sli4_hba.fof_eq);
10993 return rc;
10994
10995}
10996
10997/**
10998 * lpfc_fof_queue_create - Create all the fof queues
10999 * @phba: pointer to lpfc hba data structure.
11000 *
11001 * This routine is invoked to allocate all the fof queues for the FC HBA
11002 * operation. For each SLI4 queue type, the parameters such as queue entry
11003 * count (queue depth) shall be taken from the module parameter. For now,
11004 * we just use some constant number as place holder.
11005 *
11006 * Return codes
11007 * 0 - successful
11008 * -ENOMEM - No availble memory
11009 * -EIO - The mailbox failed to complete successfully.
11010 **/
11011int
11012lpfc_fof_queue_create(struct lpfc_hba *phba)
11013{
11014 struct lpfc_queue *qdesc;
11015
11016 /* Create FOF EQ */
11017 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
11018 phba->sli4_hba.eq_ecount);
11019 if (!qdesc)
11020 goto out_error;
11021
11022 phba->sli4_hba.fof_eq = qdesc;
11023
James Smartf38fa0b2014-04-04 13:52:21 -040011024 if (phba->cfg_fof) {
James Smart1ba981f2014-02-20 09:56:45 -050011025
11026 /* Create OAS CQ */
11027 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
11028 phba->sli4_hba.cq_ecount);
11029 if (!qdesc)
11030 goto out_error;
11031
11032 phba->sli4_hba.oas_cq = qdesc;
11033
11034 /* Create OAS WQ */
11035 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
11036 phba->sli4_hba.wq_ecount);
11037 if (!qdesc)
11038 goto out_error;
11039
11040 phba->sli4_hba.oas_wq = qdesc;
11041
11042 }
11043 return 0;
11044
11045out_error:
11046 lpfc_fof_queue_destroy(phba);
11047 return -ENOMEM;
11048}
11049
11050/**
11051 * lpfc_fof_queue_destroy - Destroy all the fof queues
11052 * @phba: pointer to lpfc hba data structure.
11053 *
11054 * This routine is invoked to release all the SLI4 queues with the FC HBA
11055 * operation.
11056 *
11057 * Return codes
11058 * 0 - successful
11059 **/
11060int
11061lpfc_fof_queue_destroy(struct lpfc_hba *phba)
11062{
11063 /* Release FOF Event queue */
11064 if (phba->sli4_hba.fof_eq != NULL) {
11065 lpfc_sli4_queue_free(phba->sli4_hba.fof_eq);
11066 phba->sli4_hba.fof_eq = NULL;
11067 }
11068
11069 /* Release OAS Completion queue */
11070 if (phba->sli4_hba.oas_cq != NULL) {
11071 lpfc_sli4_queue_free(phba->sli4_hba.oas_cq);
11072 phba->sli4_hba.oas_cq = NULL;
11073 }
11074
11075 /* Release OAS Work queue */
11076 if (phba->sli4_hba.oas_wq != NULL) {
11077 lpfc_sli4_queue_free(phba->sli4_hba.oas_wq);
11078 phba->sli4_hba.oas_wq = NULL;
11079 }
11080 return 0;
11081}
11082
dea31012005-04-17 16:05:31 -050011083static struct pci_device_id lpfc_id_table[] = {
11084 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
11085 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -040011086 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
11087 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011088 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
11089 PCI_ANY_ID, PCI_ANY_ID, },
11090 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
11091 PCI_ANY_ID, PCI_ANY_ID, },
11092 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
11093 PCI_ANY_ID, PCI_ANY_ID, },
11094 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
11095 PCI_ANY_ID, PCI_ANY_ID, },
11096 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
11097 PCI_ANY_ID, PCI_ANY_ID, },
11098 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
11099 PCI_ANY_ID, PCI_ANY_ID, },
11100 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
11101 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050011102 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
11103 PCI_ANY_ID, PCI_ANY_ID, },
11104 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
11105 PCI_ANY_ID, PCI_ANY_ID, },
11106 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
11107 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011108 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
11109 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050011110 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
11111 PCI_ANY_ID, PCI_ANY_ID, },
11112 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
11113 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011114 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
11115 PCI_ANY_ID, PCI_ANY_ID, },
11116 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
11117 PCI_ANY_ID, PCI_ANY_ID, },
11118 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
11119 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -040011120 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
11121 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050011122 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
11123 PCI_ANY_ID, PCI_ANY_ID, },
11124 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
11125 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011126 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
11127 PCI_ANY_ID, PCI_ANY_ID, },
11128 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
11129 PCI_ANY_ID, PCI_ANY_ID, },
11130 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
11131 PCI_ANY_ID, PCI_ANY_ID, },
11132 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
11133 PCI_ANY_ID, PCI_ANY_ID, },
11134 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
11135 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050011136 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
11137 PCI_ANY_ID, PCI_ANY_ID, },
11138 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
11139 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -040011140 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
11141 PCI_ANY_ID, PCI_ANY_ID, },
11142 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
11143 PCI_ANY_ID, PCI_ANY_ID, },
11144 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
11145 PCI_ANY_ID, PCI_ANY_ID, },
11146 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
11147 PCI_ANY_ID, PCI_ANY_ID, },
11148 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
11149 PCI_ANY_ID, PCI_ANY_ID, },
11150 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
11151 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -040011152 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
11153 PCI_ANY_ID, PCI_ANY_ID, },
11154 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
11155 PCI_ANY_ID, PCI_ANY_ID, },
11156 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
11157 PCI_ANY_ID, PCI_ANY_ID, },
James Smart3772a992009-05-22 14:50:54 -040011158 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
11159 PCI_ANY_ID, PCI_ANY_ID, },
James Smarta747c9c2009-11-18 15:41:10 -050011160 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
11161 PCI_ANY_ID, PCI_ANY_ID, },
11162 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
James Smart6669f9b2009-10-02 15:16:45 -040011163 PCI_ANY_ID, PCI_ANY_ID, },
James Smart98fc5dd2010-06-07 15:24:29 -040011164 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BALIUS,
11165 PCI_ANY_ID, PCI_ANY_ID, },
James Smart085c6472010-11-20 23:11:37 -050011166 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC,
11167 PCI_ANY_ID, PCI_ANY_ID, },
11168 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE,
11169 PCI_ANY_ID, PCI_ANY_ID, },
James Smartc0c11512011-05-24 11:41:34 -040011170 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC_VF,
11171 PCI_ANY_ID, PCI_ANY_ID, },
11172 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE_VF,
11173 PCI_ANY_ID, PCI_ANY_ID, },
James Smartf8cafd32012-08-03 12:42:51 -040011174 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SKYHAWK,
11175 PCI_ANY_ID, PCI_ANY_ID, },
11176 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SKYHAWK_VF,
11177 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011178 { 0 }
11179};
11180
11181MODULE_DEVICE_TABLE(pci, lpfc_id_table);
11182
Stephen Hemmingera55b2d22012-09-07 09:33:16 -070011183static const struct pci_error_handlers lpfc_err_handler = {
Linas Vepstas8d63f372007-02-14 14:28:36 -060011184 .error_detected = lpfc_io_error_detected,
11185 .slot_reset = lpfc_io_slot_reset,
11186 .resume = lpfc_io_resume,
11187};
11188
dea31012005-04-17 16:05:31 -050011189static struct pci_driver lpfc_driver = {
11190 .name = LPFC_DRIVER_NAME,
11191 .id_table = lpfc_id_table,
11192 .probe = lpfc_pci_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011193 .remove = lpfc_pci_remove_one,
James Smart3a55b532008-12-04 22:38:54 -050011194 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -040011195 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -050011196 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -050011197};
11198
James Smart3ef6d242012-01-18 16:23:48 -050011199static const struct file_operations lpfc_mgmt_fop = {
Al Viro858feac2013-04-14 22:39:37 -040011200 .owner = THIS_MODULE,
James Smart3ef6d242012-01-18 16:23:48 -050011201};
11202
11203static struct miscdevice lpfc_mgmt_dev = {
11204 .minor = MISC_DYNAMIC_MINOR,
11205 .name = "lpfcmgmt",
11206 .fops = &lpfc_mgmt_fop,
11207};
11208
James Smarte59058c2008-08-24 21:49:00 -040011209/**
James Smart3621a712009-04-06 18:47:14 -040011210 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -040011211 *
11212 * This routine is to be invoked when the lpfc module is loaded into the
11213 * kernel. The special kernel macro module_init() is used to indicate the
11214 * role of this routine to the kernel as lpfc module entry point.
11215 *
11216 * Return codes
11217 * 0 - successful
11218 * -ENOMEM - FC attach transport failed
11219 * all others - failed
11220 */
dea31012005-04-17 16:05:31 -050011221static int __init
11222lpfc_init(void)
11223{
James Smart7bb03bb2013-04-17 20:19:16 -040011224 int cpu;
dea31012005-04-17 16:05:31 -050011225 int error = 0;
11226
11227 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040011228 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -050011229
James Smart3ef6d242012-01-18 16:23:48 -050011230 error = misc_register(&lpfc_mgmt_dev);
11231 if (error)
11232 printk(KERN_ERR "Could not register lpfcmgmt device, "
11233 "misc_register returned with status %d", error);
11234
James Smart7ee5d432007-10-27 13:37:17 -040011235 if (lpfc_enable_npiv) {
11236 lpfc_transport_functions.vport_create = lpfc_vport_create;
11237 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
11238 }
dea31012005-04-17 16:05:31 -050011239 lpfc_transport_template =
11240 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -040011241 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -050011242 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -040011243 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -040011244 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -040011245 fc_attach_transport(&lpfc_vport_transport_functions);
11246 if (lpfc_vport_transport_template == NULL) {
11247 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040011248 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -040011249 }
James Smart7ee5d432007-10-27 13:37:17 -040011250 }
James Smart7bb03bb2013-04-17 20:19:16 -040011251
11252 /* Initialize in case vector mapping is needed */
James Smartb246de12013-05-31 17:03:07 -040011253 lpfc_used_cpu = NULL;
11254 lpfc_present_cpu = 0;
11255 for_each_present_cpu(cpu)
11256 lpfc_present_cpu++;
James Smart7bb03bb2013-04-17 20:19:16 -040011257
dea31012005-04-17 16:05:31 -050011258 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -050011259 if (error) {
dea31012005-04-17 16:05:31 -050011260 fc_release_transport(lpfc_transport_template);
James Smartd7c255b2008-08-24 21:50:00 -040011261 if (lpfc_enable_npiv)
11262 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -050011263 }
dea31012005-04-17 16:05:31 -050011264
11265 return error;
11266}
11267
James Smarte59058c2008-08-24 21:49:00 -040011268/**
James Smart3621a712009-04-06 18:47:14 -040011269 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -040011270 *
11271 * This routine is invoked when the lpfc module is removed from the kernel.
11272 * The special kernel macro module_exit() is used to indicate the role of
11273 * this routine to the kernel as lpfc module exit point.
11274 */
dea31012005-04-17 16:05:31 -050011275static void __exit
11276lpfc_exit(void)
11277{
James Smart3ef6d242012-01-18 16:23:48 -050011278 misc_deregister(&lpfc_mgmt_dev);
dea31012005-04-17 16:05:31 -050011279 pci_unregister_driver(&lpfc_driver);
11280 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040011281 if (lpfc_enable_npiv)
11282 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -050011283 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040011284 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
11285 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050011286 (1L << _dump_buf_data_order), _dump_buf_data);
11287 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
11288 }
11289
11290 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -040011291 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
11292 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050011293 (1L << _dump_buf_dif_order), _dump_buf_dif);
11294 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
11295 }
James Smartb246de12013-05-31 17:03:07 -040011296 kfree(lpfc_used_cpu);
dea31012005-04-17 16:05:31 -050011297}
11298
11299module_init(lpfc_init);
11300module_exit(lpfc_exit);
11301MODULE_LICENSE("GPL");
11302MODULE_DESCRIPTION(LPFC_MODULE_DESC);
11303MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
11304MODULE_VERSION("0:" LPFC_DRIVER_VERSION);