blob: 33a24fc0afece0d1542c1c45359aa8c6eff261f2 [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 **/
Rashika Kheriae399b222014-09-03 12:55:28 -0400652static int
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 **/
Rashika Kheriae399b222014-09-03 12:55:28 -0400753static int
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 Smart68e814f2014-05-21 08:04:59 -0400823 * lpfc_sli4_free_sp_events - Cleanup sp_queue_events to free
824 * rspiocb which got deferred
825 *
826 * @phba: pointer to lpfc HBA data structure.
827 *
828 * This routine will cleanup completed slow path events after HBA is reset
829 * when bringing down the SLI Layer.
830 *
831 *
832 * Return codes
833 * void.
834 **/
835static void
836lpfc_sli4_free_sp_events(struct lpfc_hba *phba)
837{
838 struct lpfc_iocbq *rspiocbq;
839 struct hbq_dmabuf *dmabuf;
840 struct lpfc_cq_event *cq_event;
841
842 spin_lock_irq(&phba->hbalock);
843 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
844 spin_unlock_irq(&phba->hbalock);
845
846 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
847 /* Get the response iocb from the head of work queue */
848 spin_lock_irq(&phba->hbalock);
849 list_remove_head(&phba->sli4_hba.sp_queue_event,
850 cq_event, struct lpfc_cq_event, list);
851 spin_unlock_irq(&phba->hbalock);
852
853 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
854 case CQE_CODE_COMPL_WQE:
855 rspiocbq = container_of(cq_event, struct lpfc_iocbq,
856 cq_event);
857 lpfc_sli_release_iocbq(phba, rspiocbq);
858 break;
859 case CQE_CODE_RECEIVE:
860 case CQE_CODE_RECEIVE_V1:
861 dmabuf = container_of(cq_event, struct hbq_dmabuf,
862 cq_event);
863 lpfc_in_buf_free(phba, &dmabuf->dbuf);
864 }
865 }
866}
867
868/**
James Smartbcece5f2014-04-04 13:51:34 -0400869 * lpfc_hba_free_post_buf - Perform lpfc uninitialization after HBA reset
870 * @phba: pointer to lpfc HBA data structure.
871 *
872 * This routine will cleanup posted ELS buffers after the HBA is reset
873 * when bringing down the SLI Layer.
874 *
875 *
876 * Return codes
877 * void.
878 **/
879static void
880lpfc_hba_free_post_buf(struct lpfc_hba *phba)
881{
882 struct lpfc_sli *psli = &phba->sli;
883 struct lpfc_sli_ring *pring;
884 struct lpfc_dmabuf *mp, *next_mp;
James Smart07eab622014-04-04 13:51:44 -0400885 LIST_HEAD(buflist);
886 int count;
James Smartbcece5f2014-04-04 13:51:34 -0400887
888 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
889 lpfc_sli_hbqbuf_free_all(phba);
890 else {
891 /* Cleanup preposted buffers on the ELS ring */
James Smartbcece5f2014-04-04 13:51:34 -0400892 pring = &psli->ring[LPFC_ELS_RING];
James Smart07eab622014-04-04 13:51:44 -0400893 spin_lock_irq(&phba->hbalock);
894 list_splice_init(&pring->postbufq, &buflist);
895 spin_unlock_irq(&phba->hbalock);
896
897 count = 0;
898 list_for_each_entry_safe(mp, next_mp, &buflist, list) {
James Smartbcece5f2014-04-04 13:51:34 -0400899 list_del(&mp->list);
James Smart07eab622014-04-04 13:51:44 -0400900 count++;
James Smartbcece5f2014-04-04 13:51:34 -0400901 lpfc_mbuf_free(phba, mp->virt, mp->phys);
902 kfree(mp);
903 }
James Smart07eab622014-04-04 13:51:44 -0400904
905 spin_lock_irq(&phba->hbalock);
906 pring->postbufq_cnt -= count;
James Smartbcece5f2014-04-04 13:51:34 -0400907 spin_unlock_irq(&phba->hbalock);
908 }
909}
910
911/**
912 * lpfc_hba_clean_txcmplq - Perform lpfc uninitialization after HBA reset
913 * @phba: pointer to lpfc HBA data structure.
914 *
915 * This routine will cleanup the txcmplq after the HBA is reset when bringing
916 * down the SLI Layer.
917 *
918 * Return codes
919 * void
920 **/
921static void
922lpfc_hba_clean_txcmplq(struct lpfc_hba *phba)
923{
924 struct lpfc_sli *psli = &phba->sli;
925 struct lpfc_sli_ring *pring;
926 LIST_HEAD(completions);
927 int i;
928
James Smartbcece5f2014-04-04 13:51:34 -0400929 for (i = 0; i < psli->num_rings; i++) {
930 pring = &psli->ring[i];
931 if (phba->sli_rev >= LPFC_SLI_REV4)
932 spin_lock_irq(&pring->ring_lock);
933 else
934 spin_lock_irq(&phba->hbalock);
935 /* At this point in time the HBA is either reset or DOA. Either
936 * way, nothing should be on txcmplq as it will NEVER complete.
937 */
938 list_splice_init(&pring->txcmplq, &completions);
James Smartdb55fba2014-04-04 13:52:02 -0400939 pring->txcmplq_cnt = 0;
James Smartbcece5f2014-04-04 13:51:34 -0400940
941 if (phba->sli_rev >= LPFC_SLI_REV4)
942 spin_unlock_irq(&pring->ring_lock);
943 else
944 spin_unlock_irq(&phba->hbalock);
945
946 /* Cancel all the IOCBs from the completions list */
947 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
948 IOERR_SLI_ABORTED);
949 lpfc_sli_abort_iocb_ring(phba, pring);
950 }
951}
952
953/**
James Smart3772a992009-05-22 14:50:54 -0400954 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smartbcece5f2014-04-04 13:51:34 -0400955 int i;
James Smarte59058c2008-08-24 21:49:00 -0400956 * @phba: pointer to lpfc HBA data structure.
957 *
958 * This routine will do uninitialization after the HBA is reset when bring
959 * down the SLI Layer.
960 *
961 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200962 * 0 - success.
James Smarte59058c2008-08-24 21:49:00 -0400963 * Any other value - error.
964 **/
James Smart3772a992009-05-22 14:50:54 -0400965static int
966lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -0500967{
James Smartbcece5f2014-04-04 13:51:34 -0400968 lpfc_hba_free_post_buf(phba);
969 lpfc_hba_clean_txcmplq(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500970 return 0;
971}
James Smart5af5eee2010-10-22 11:06:38 -0400972
James Smartda0436e2009-05-22 14:51:39 -0400973/**
974 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
975 * @phba: pointer to lpfc HBA data structure.
976 *
977 * This routine will do uninitialization after the HBA is reset when bring
978 * down the SLI Layer.
979 *
980 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200981 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400982 * Any other value - error.
983 **/
984static int
985lpfc_hba_down_post_s4(struct lpfc_hba *phba)
986{
987 struct lpfc_scsi_buf *psb, *psb_next;
988 LIST_HEAD(aborts);
James Smartda0436e2009-05-22 14:51:39 -0400989 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500990 struct lpfc_sglq *sglq_entry = NULL;
James Smartdafe8ce2014-09-03 12:56:40 -0400991 struct lpfc_sli *psli = &phba->sli;
992 struct lpfc_sli_ring *pring;
James Smart0f65ff62010-02-26 14:14:23 -0500993
James Smartbcece5f2014-04-04 13:51:34 -0400994 lpfc_hba_free_post_buf(phba);
995 lpfc_hba_clean_txcmplq(phba);
James Smartdafe8ce2014-09-03 12:56:40 -0400996 pring = &psli->ring[LPFC_ELS_RING];
James Smartbcece5f2014-04-04 13:51:34 -0400997
James Smartda0436e2009-05-22 14:51:39 -0400998 /* At this point in time the HBA is either reset or DOA. Either
999 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
1000 * on the lpfc_sgl_list so that it can either be freed if the
1001 * driver is unloading or reposted if the driver is restarting
1002 * the port.
1003 */
1004 spin_lock_irq(&phba->hbalock); /* required for lpfc_sgl_list and */
1005 /* scsl_buf_list */
1006 /* abts_sgl_list_lock required because worker thread uses this
1007 * list.
1008 */
1009 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -05001010 list_for_each_entry(sglq_entry,
1011 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
1012 sglq_entry->state = SGL_FREED;
1013
James Smartdafe8ce2014-09-03 12:56:40 -04001014 spin_lock(&pring->ring_lock);
James Smartda0436e2009-05-22 14:51:39 -04001015 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
1016 &phba->sli4_hba.lpfc_sgl_list);
James Smartdafe8ce2014-09-03 12:56:40 -04001017 spin_unlock(&pring->ring_lock);
James Smartda0436e2009-05-22 14:51:39 -04001018 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
1019 /* abts_scsi_buf_list_lock required because worker thread uses this
1020 * list.
1021 */
1022 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
1023 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
1024 &aborts);
1025 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
1026 spin_unlock_irq(&phba->hbalock);
1027
1028 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
1029 psb->pCmd = NULL;
1030 psb->status = IOSTAT_SUCCESS;
1031 }
James Smarta40fc5f2013-04-17 20:17:40 -04001032 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
1033 list_splice(&aborts, &phba->lpfc_scsi_buf_list_put);
1034 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
James Smart68e814f2014-05-21 08:04:59 -04001035
1036 lpfc_sli4_free_sp_events(phba);
James Smartda0436e2009-05-22 14:51:39 -04001037 return 0;
1038}
1039
1040/**
1041 * lpfc_hba_down_post - Wrapper func for hba down post routine
1042 * @phba: pointer to lpfc HBA data structure.
1043 *
1044 * This routine wraps the actual SLI3 or SLI4 routine for performing
1045 * uninitialization after the HBA is reset when bring down the SLI Layer.
1046 *
1047 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001048 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001049 * Any other value - error.
1050 **/
1051int
1052lpfc_hba_down_post(struct lpfc_hba *phba)
1053{
1054 return (*phba->lpfc_hba_down_post)(phba);
1055}
Jamie Wellnitz41415862006-02-28 19:25:27 -05001056
James Smarte59058c2008-08-24 21:49:00 -04001057/**
James Smart3621a712009-04-06 18:47:14 -04001058 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001059 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1060 *
1061 * This is the HBA-timer timeout handler registered to the lpfc driver. When
1062 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
1063 * work-port-events bitmap and the worker thread is notified. This timeout
1064 * event will be used by the worker thread to invoke the actual timeout
1065 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
1066 * be performed in the timeout handler and the HBA timeout event bit shall
1067 * be cleared by the worker thread after it has taken the event bitmap out.
1068 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001069static void
James Smart858c9f62007-06-17 19:56:39 -05001070lpfc_hb_timeout(unsigned long ptr)
1071{
1072 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -04001073 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -05001074 unsigned long iflag;
1075
1076 phba = (struct lpfc_hba *)ptr;
James Smart93996272008-08-24 21:50:30 -04001077
1078 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -05001079 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04001080 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
1081 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -05001082 phba->pport->work_port_events |= WORKER_HB_TMO;
1083 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
1084
James Smart93996272008-08-24 21:50:30 -04001085 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -04001086 if (!tmo_posted)
1087 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -05001088 return;
1089}
1090
James Smarte59058c2008-08-24 21:49:00 -04001091/**
James Smart19ca7602010-11-20 23:11:55 -05001092 * lpfc_rrq_timeout - The RRQ-timer timeout handler
1093 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1094 *
1095 * This is the RRQ-timer timeout handler registered to the lpfc driver. When
1096 * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
1097 * work-port-events bitmap and the worker thread is notified. This timeout
1098 * event will be used by the worker thread to invoke the actual timeout
1099 * handler routine, lpfc_rrq_handler. Any periodical operations will
1100 * be performed in the timeout handler and the RRQ timeout event bit shall
1101 * be cleared by the worker thread after it has taken the event bitmap out.
1102 **/
1103static void
1104lpfc_rrq_timeout(unsigned long ptr)
1105{
1106 struct lpfc_hba *phba;
James Smart19ca7602010-11-20 23:11:55 -05001107 unsigned long iflag;
1108
1109 phba = (struct lpfc_hba *)ptr;
1110 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001111 if (!(phba->pport->load_flag & FC_UNLOADING))
1112 phba->hba_flag |= HBA_RRQ_ACTIVE;
1113 else
1114 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart19ca7602010-11-20 23:11:55 -05001115 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001116
1117 if (!(phba->pport->load_flag & FC_UNLOADING))
1118 lpfc_worker_wake_up(phba);
James Smart19ca7602010-11-20 23:11:55 -05001119}
1120
1121/**
James Smart3621a712009-04-06 18:47:14 -04001122 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -04001123 * @phba: pointer to lpfc hba data structure.
1124 * @pmboxq: pointer to the driver internal queue element for mailbox command.
1125 *
1126 * This is the callback function to the lpfc heart-beat mailbox command.
1127 * If configured, the lpfc driver issues the heart-beat mailbox command to
1128 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
1129 * heart-beat mailbox command is issued, the driver shall set up heart-beat
1130 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
1131 * heart-beat outstanding state. Once the mailbox command comes back and
1132 * no error conditions detected, the heart-beat mailbox command timer is
1133 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1134 * state is cleared for the next heart-beat. If the timer expired with the
1135 * heart-beat outstanding state set, the driver will put the HBA offline.
1136 **/
James Smart858c9f62007-06-17 19:56:39 -05001137static void
1138lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1139{
1140 unsigned long drvr_flag;
1141
1142 spin_lock_irqsave(&phba->hbalock, drvr_flag);
1143 phba->hb_outstanding = 0;
1144 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1145
James Smart93996272008-08-24 21:50:30 -04001146 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -05001147 mempool_free(pmboxq, phba->mbox_mem_pool);
1148 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1149 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -04001150 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -05001151 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001152 jiffies +
1153 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001154 return;
1155}
1156
James Smarte59058c2008-08-24 21:49:00 -04001157/**
James Smart3621a712009-04-06 18:47:14 -04001158 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001159 * @phba: pointer to lpfc hba data structure.
1160 *
1161 * This is the actual HBA-timer timeout handler to be invoked by the worker
1162 * thread whenever the HBA timer fired and HBA-timeout event posted. This
1163 * handler performs any periodic operations needed for the device. If such
1164 * periodic event has already been attended to either in the interrupt handler
1165 * or by processing slow-ring or fast-ring events within the HBA-timer
1166 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1167 * the timer for the next timeout period. If lpfc heart-beat mailbox command
1168 * is configured and there is no heart-beat mailbox command outstanding, a
1169 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1170 * has been a heart-beat mailbox command outstanding, the HBA shall be put
1171 * to offline.
1172 **/
James Smart858c9f62007-06-17 19:56:39 -05001173void
1174lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1175{
James Smart45ed1192009-10-02 15:17:02 -04001176 struct lpfc_vport **vports;
James Smart858c9f62007-06-17 19:56:39 -05001177 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -05001178 struct lpfc_dmabuf *buf_ptr;
James Smart45ed1192009-10-02 15:17:02 -04001179 int retval, i;
James Smart858c9f62007-06-17 19:56:39 -05001180 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -05001181 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -05001182
James Smart45ed1192009-10-02 15:17:02 -04001183 vports = lpfc_create_vport_work_array(phba);
1184 if (vports != NULL)
1185 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
1186 lpfc_rcv_seq_check_edtov(vports[i]);
1187 lpfc_destroy_vport_work_array(phba, vports);
1188
James Smart858c9f62007-06-17 19:56:39 -05001189 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -04001190 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -05001191 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1192 return;
1193
1194 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -05001195
James Smart256ec0d2013-04-17 20:14:58 -04001196 if (time_after(phba->last_completion_time +
1197 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL),
1198 jiffies)) {
James Smart858c9f62007-06-17 19:56:39 -05001199 spin_unlock_irq(&phba->pport->work_port_lock);
1200 if (!phba->hb_outstanding)
1201 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001202 jiffies +
1203 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001204 else
1205 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001206 jiffies +
1207 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001208 return;
1209 }
1210 spin_unlock_irq(&phba->pport->work_port_lock);
1211
James Smart0ff10d42008-01-11 01:52:36 -05001212 if (phba->elsbuf_cnt &&
1213 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1214 spin_lock_irq(&phba->hbalock);
1215 list_splice_init(&phba->elsbuf, &completions);
1216 phba->elsbuf_cnt = 0;
1217 phba->elsbuf_prev_cnt = 0;
1218 spin_unlock_irq(&phba->hbalock);
1219
1220 while (!list_empty(&completions)) {
1221 list_remove_head(&completions, buf_ptr,
1222 struct lpfc_dmabuf, list);
1223 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1224 kfree(buf_ptr);
1225 }
1226 }
1227 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1228
James Smart858c9f62007-06-17 19:56:39 -05001229 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -05001230 if (phba->cfg_enable_hba_heartbeat) {
1231 if (!phba->hb_outstanding) {
James Smartbc739052010-08-04 16:11:18 -04001232 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1233 (list_empty(&psli->mboxq))) {
1234 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1235 GFP_KERNEL);
1236 if (!pmboxq) {
1237 mod_timer(&phba->hb_tmofunc,
1238 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001239 msecs_to_jiffies(1000 *
1240 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001241 return;
1242 }
James Smart13815c82008-01-11 01:52:48 -05001243
James Smartbc739052010-08-04 16:11:18 -04001244 lpfc_heart_beat(phba, pmboxq);
1245 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1246 pmboxq->vport = phba->pport;
1247 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1248 MBX_NOWAIT);
James Smart13815c82008-01-11 01:52:48 -05001249
James Smartbc739052010-08-04 16:11:18 -04001250 if (retval != MBX_BUSY &&
1251 retval != MBX_SUCCESS) {
1252 mempool_free(pmboxq,
1253 phba->mbox_mem_pool);
1254 mod_timer(&phba->hb_tmofunc,
1255 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001256 msecs_to_jiffies(1000 *
1257 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001258 return;
1259 }
1260 phba->skipped_hb = 0;
1261 phba->hb_outstanding = 1;
1262 } else if (time_before_eq(phba->last_completion_time,
1263 phba->skipped_hb)) {
1264 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1265 "2857 Last completion time not "
1266 " updated in %d ms\n",
1267 jiffies_to_msecs(jiffies
1268 - phba->last_completion_time));
1269 } else
1270 phba->skipped_hb = jiffies;
1271
James Smart858c9f62007-06-17 19:56:39 -05001272 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001273 jiffies +
1274 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001275 return;
James Smart13815c82008-01-11 01:52:48 -05001276 } else {
1277 /*
1278 * If heart beat timeout called with hb_outstanding set
James Smartdcf2a4e2010-09-29 11:18:53 -04001279 * we need to give the hb mailbox cmd a chance to
1280 * complete or TMO.
James Smart13815c82008-01-11 01:52:48 -05001281 */
James Smartdcf2a4e2010-09-29 11:18:53 -04001282 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1283 "0459 Adapter heartbeat still out"
1284 "standing:last compl time was %d ms.\n",
1285 jiffies_to_msecs(jiffies
1286 - phba->last_completion_time));
1287 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001288 jiffies +
1289 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001290 }
James Smart858c9f62007-06-17 19:56:39 -05001291 }
1292}
1293
James Smarte59058c2008-08-24 21:49:00 -04001294/**
James Smart3621a712009-04-06 18:47:14 -04001295 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -04001296 * @phba: pointer to lpfc hba data structure.
1297 *
1298 * This routine is called to bring the HBA offline when HBA hardware error
1299 * other than Port Error 6 has been detected.
1300 **/
James Smart09372822008-01-11 01:52:54 -05001301static void
1302lpfc_offline_eratt(struct lpfc_hba *phba)
1303{
1304 struct lpfc_sli *psli = &phba->sli;
1305
1306 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001307 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -05001308 spin_unlock_irq(&phba->hbalock);
James Smart618a5232012-06-12 13:54:36 -04001309 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart09372822008-01-11 01:52:54 -05001310
1311 lpfc_offline(phba);
1312 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001313 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001314 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001315 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001316 lpfc_hba_down_post(phba);
1317 lpfc_sli_brdready(phba, HS_MBRDY);
1318 lpfc_unblock_mgmt_io(phba);
1319 phba->link_state = LPFC_HBA_ERROR;
1320 return;
1321}
1322
James Smarte59058c2008-08-24 21:49:00 -04001323/**
James Smartda0436e2009-05-22 14:51:39 -04001324 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1325 * @phba: pointer to lpfc hba data structure.
1326 *
1327 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1328 * other than Port Error 6 has been detected.
1329 **/
James Smarta88dbb62013-04-17 20:18:39 -04001330void
James Smartda0436e2009-05-22 14:51:39 -04001331lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1332{
James Smart618a5232012-06-12 13:54:36 -04001333 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smartda0436e2009-05-22 14:51:39 -04001334 lpfc_offline(phba);
1335 lpfc_sli4_brdreset(phba);
1336 lpfc_hba_down_post(phba);
1337 lpfc_sli4_post_status_check(phba);
1338 lpfc_unblock_mgmt_io(phba);
1339 phba->link_state = LPFC_HBA_ERROR;
1340}
1341
1342/**
James Smarta257bf92009-04-06 18:48:10 -04001343 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1344 * @phba: pointer to lpfc hba data structure.
1345 *
1346 * This routine is invoked to handle the deferred HBA hardware error
1347 * conditions. This type of error is indicated by HBA by setting ER1
1348 * and another ER bit in the host status register. The driver will
1349 * wait until the ER1 bit clears before handling the error condition.
1350 **/
1351static void
1352lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1353{
1354 uint32_t old_host_status = phba->work_hs;
James Smarta257bf92009-04-06 18:48:10 -04001355 struct lpfc_sli *psli = &phba->sli;
1356
James Smartf4b4c682009-05-22 14:53:12 -04001357 /* If the pci channel is offline, ignore possible errors,
1358 * since we cannot communicate with the pci card anyway.
1359 */
1360 if (pci_channel_offline(phba->pcidev)) {
1361 spin_lock_irq(&phba->hbalock);
1362 phba->hba_flag &= ~DEFER_ERATT;
1363 spin_unlock_irq(&phba->hbalock);
1364 return;
1365 }
1366
James Smarta257bf92009-04-06 18:48:10 -04001367 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1368 "0479 Deferred Adapter Hardware Error "
1369 "Data: x%x x%x x%x\n",
1370 phba->work_hs,
1371 phba->work_status[0], phba->work_status[1]);
1372
1373 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001374 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001375 spin_unlock_irq(&phba->hbalock);
1376
1377
1378 /*
1379 * Firmware stops when it triggred erratt. That could cause the I/Os
1380 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1381 * SCSI layer retry it after re-establishing link.
1382 */
James Smartdb55fba2014-04-04 13:52:02 -04001383 lpfc_sli_abort_fcp_rings(phba);
James Smarta257bf92009-04-06 18:48:10 -04001384
1385 /*
1386 * There was a firmware error. Take the hba offline and then
1387 * attempt to restart it.
1388 */
James Smart618a5232012-06-12 13:54:36 -04001389 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smarta257bf92009-04-06 18:48:10 -04001390 lpfc_offline(phba);
1391
1392 /* Wait for the ER1 bit to clear.*/
1393 while (phba->work_hs & HS_FFER1) {
1394 msleep(100);
James Smart9940b972011-03-11 16:06:12 -05001395 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1396 phba->work_hs = UNPLUG_ERR ;
1397 break;
1398 }
James Smarta257bf92009-04-06 18:48:10 -04001399 /* If driver is unloading let the worker thread continue */
1400 if (phba->pport->load_flag & FC_UNLOADING) {
1401 phba->work_hs = 0;
1402 break;
1403 }
1404 }
1405
1406 /*
1407 * This is to ptrotect against a race condition in which
1408 * first write to the host attention register clear the
1409 * host status register.
1410 */
1411 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1412 phba->work_hs = old_host_status & ~HS_FFER1;
1413
James Smart3772a992009-05-22 14:50:54 -04001414 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001415 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001416 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001417 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1418 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1419}
1420
James Smart3772a992009-05-22 14:50:54 -04001421static void
1422lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1423{
1424 struct lpfc_board_event_header board_event;
1425 struct Scsi_Host *shost;
1426
1427 board_event.event_type = FC_REG_BOARD_EVENT;
1428 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1429 shost = lpfc_shost_from_vport(phba->pport);
1430 fc_host_post_vendor_event(shost, fc_get_event_number(),
1431 sizeof(board_event),
1432 (char *) &board_event,
1433 LPFC_NL_VENDOR_ID);
1434}
1435
James Smarta257bf92009-04-06 18:48:10 -04001436/**
James Smart3772a992009-05-22 14:50:54 -04001437 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001438 * @phba: pointer to lpfc hba data structure.
1439 *
1440 * This routine is invoked to handle the following HBA hardware error
1441 * conditions:
1442 * 1 - HBA error attention interrupt
1443 * 2 - DMA ring index out of range
1444 * 3 - Mailbox command came back as unknown
1445 **/
James Smart3772a992009-05-22 14:50:54 -04001446static void
1447lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001448{
James Smart2e0fef82007-06-17 19:56:36 -05001449 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001450 struct lpfc_sli *psli = &phba->sli;
James Smartd2873e42006-08-18 17:46:43 -04001451 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001452 unsigned long temperature;
1453 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001454 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001455
Linas Vepstas8d63f372007-02-14 14:28:36 -06001456 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001457 * since we cannot communicate with the pci card anyway.
1458 */
1459 if (pci_channel_offline(phba->pcidev)) {
1460 spin_lock_irq(&phba->hbalock);
1461 phba->hba_flag &= ~DEFER_ERATT;
1462 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001463 return;
James Smart3772a992009-05-22 14:50:54 -04001464 }
1465
James Smart13815c82008-01-11 01:52:48 -05001466 /* If resets are disabled then leave the HBA alone and return */
1467 if (!phba->cfg_enable_hba_reset)
1468 return;
dea31012005-04-17 16:05:31 -05001469
James Smartea2151b2008-09-07 11:52:10 -04001470 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001471 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001472
James Smarta257bf92009-04-06 18:48:10 -04001473 if (phba->hba_flag & DEFER_ERATT)
1474 lpfc_handle_deferred_eratt(phba);
1475
James Smartdcf2a4e2010-09-29 11:18:53 -04001476 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1477 if (phba->work_hs & HS_FFER6)
1478 /* Re-establishing Link */
1479 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1480 "1301 Re-establishing Link "
1481 "Data: x%x x%x x%x\n",
1482 phba->work_hs, phba->work_status[0],
1483 phba->work_status[1]);
1484 if (phba->work_hs & HS_FFER8)
1485 /* Device Zeroization */
1486 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1487 "2861 Host Authentication device "
1488 "zeroization Data:x%x x%x x%x\n",
1489 phba->work_hs, phba->work_status[0],
1490 phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001491
James Smart92d7f7b2007-06-17 19:56:38 -05001492 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001493 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001494 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001495
1496 /*
1497 * Firmware stops when it triggled erratt with HS_FFER6.
1498 * That could cause the I/Os dropped by the firmware.
1499 * Error iocb (I/O) on txcmplq and let the SCSI layer
1500 * retry it after re-establishing link.
1501 */
James Smartdb55fba2014-04-04 13:52:02 -04001502 lpfc_sli_abort_fcp_rings(phba);
dea31012005-04-17 16:05:31 -05001503
dea31012005-04-17 16:05:31 -05001504 /*
1505 * There was a firmware error. Take the hba offline and then
1506 * attempt to restart it.
1507 */
James Smart618a5232012-06-12 13:54:36 -04001508 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
dea31012005-04-17 16:05:31 -05001509 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001510 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001511 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001512 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001513 return;
1514 }
James Smart46fa3112007-04-25 09:51:45 -04001515 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001516 } else if (phba->work_hs & HS_CRIT_TEMP) {
1517 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1518 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1519 temp_event_data.event_code = LPFC_CRIT_TEMP;
1520 temp_event_data.data = (uint32_t)temperature;
1521
1522 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001523 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001524 "(%ld), taking this port offline "
1525 "Data: x%x x%x x%x\n",
1526 temperature, phba->work_hs,
1527 phba->work_status[0], phba->work_status[1]);
1528
1529 shost = lpfc_shost_from_vport(phba->pport);
1530 fc_host_post_vendor_event(shost, fc_get_event_number(),
1531 sizeof(temp_event_data),
1532 (char *) &temp_event_data,
1533 SCSI_NL_VID_TYPE_PCI
1534 | PCI_VENDOR_ID_EMULEX);
1535
James Smart7af67052007-10-27 13:38:11 -04001536 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001537 phba->over_temp_state = HBA_OVER_TEMP;
1538 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001539 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001540
dea31012005-04-17 16:05:31 -05001541 } else {
1542 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001543 * failure is a value other than FFER6. Do not call the offline
1544 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001545 */
1546 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001547 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001548 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001549 phba->work_hs,
dea31012005-04-17 16:05:31 -05001550 phba->work_status[0], phba->work_status[1]);
1551
James Smartd2873e42006-08-18 17:46:43 -04001552 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001553 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001554 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001555 sizeof(event_data), (char *) &event_data,
1556 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1557
James Smart09372822008-01-11 01:52:54 -05001558 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001559 }
James Smart93996272008-08-24 21:50:30 -04001560 return;
dea31012005-04-17 16:05:31 -05001561}
1562
James Smarte59058c2008-08-24 21:49:00 -04001563/**
James Smart618a5232012-06-12 13:54:36 -04001564 * lpfc_sli4_port_sta_fn_reset - The SLI4 function reset due to port status reg
1565 * @phba: pointer to lpfc hba data structure.
1566 * @mbx_action: flag for mailbox shutdown action.
1567 *
1568 * This routine is invoked to perform an SLI4 port PCI function reset in
1569 * response to port status register polling attention. It waits for port
1570 * status register (ERR, RDY, RN) bits before proceeding with function reset.
1571 * During this process, interrupt vectors are freed and later requested
1572 * for handling possible port resource change.
1573 **/
1574static int
James Smarte10b2022014-02-20 09:57:43 -05001575lpfc_sli4_port_sta_fn_reset(struct lpfc_hba *phba, int mbx_action,
1576 bool en_rn_msg)
James Smart618a5232012-06-12 13:54:36 -04001577{
1578 int rc;
1579 uint32_t intr_mode;
1580
1581 /*
1582 * On error status condition, driver need to wait for port
1583 * ready before performing reset.
1584 */
1585 rc = lpfc_sli4_pdev_status_reg_wait(phba);
1586 if (!rc) {
1587 /* need reset: attempt for port recovery */
James Smarte10b2022014-02-20 09:57:43 -05001588 if (en_rn_msg)
1589 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1590 "2887 Reset Needed: Attempting Port "
1591 "Recovery...\n");
James Smart618a5232012-06-12 13:54:36 -04001592 lpfc_offline_prep(phba, mbx_action);
1593 lpfc_offline(phba);
1594 /* release interrupt for possible resource change */
1595 lpfc_sli4_disable_intr(phba);
1596 lpfc_sli_brdrestart(phba);
1597 /* request and enable interrupt */
1598 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
1599 if (intr_mode == LPFC_INTR_ERROR) {
1600 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1601 "3175 Failed to enable interrupt\n");
1602 return -EIO;
1603 } else {
1604 phba->intr_mode = intr_mode;
1605 }
1606 rc = lpfc_online(phba);
1607 if (rc == 0)
1608 lpfc_unblock_mgmt_io(phba);
1609 }
1610 return rc;
1611}
1612
1613/**
James Smartda0436e2009-05-22 14:51:39 -04001614 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1615 * @phba: pointer to lpfc hba data structure.
1616 *
1617 * This routine is invoked to handle the SLI4 HBA hardware error attention
1618 * conditions.
1619 **/
1620static void
1621lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1622{
1623 struct lpfc_vport *vport = phba->pport;
1624 uint32_t event_data;
1625 struct Scsi_Host *shost;
James Smart2fcee4b2010-12-15 17:57:46 -05001626 uint32_t if_type;
James Smart2e90f4b2011-12-13 13:22:37 -05001627 struct lpfc_register portstat_reg = {0};
1628 uint32_t reg_err1, reg_err2;
1629 uint32_t uerrlo_reg, uemasklo_reg;
1630 uint32_t pci_rd_rc1, pci_rd_rc2;
James Smarte10b2022014-02-20 09:57:43 -05001631 bool en_rn_msg = true;
James Smart73d91e52011-10-10 21:32:10 -04001632 int rc;
James Smartda0436e2009-05-22 14:51:39 -04001633
1634 /* If the pci channel is offline, ignore possible errors, since
1635 * we cannot communicate with the pci card anyway.
1636 */
1637 if (pci_channel_offline(phba->pcidev))
1638 return;
1639 /* If resets are disabled then leave the HBA alone and return */
1640 if (!phba->cfg_enable_hba_reset)
1641 return;
1642
James Smart2fcee4b2010-12-15 17:57:46 -05001643 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1644 switch (if_type) {
1645 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart2e90f4b2011-12-13 13:22:37 -05001646 pci_rd_rc1 = lpfc_readl(
1647 phba->sli4_hba.u.if_type0.UERRLOregaddr,
1648 &uerrlo_reg);
1649 pci_rd_rc2 = lpfc_readl(
1650 phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
1651 &uemasklo_reg);
1652 /* consider PCI bus read error as pci_channel_offline */
1653 if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
1654 return;
James Smart2fcee4b2010-12-15 17:57:46 -05001655 lpfc_sli4_offline_eratt(phba);
1656 break;
1657 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart2e90f4b2011-12-13 13:22:37 -05001658 pci_rd_rc1 = lpfc_readl(
1659 phba->sli4_hba.u.if_type2.STATUSregaddr,
1660 &portstat_reg.word0);
1661 /* consider PCI bus read error as pci_channel_offline */
James Smart6b5151f2012-01-18 16:24:06 -05001662 if (pci_rd_rc1 == -EIO) {
1663 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1664 "3151 PCI bus read access failure: x%x\n",
1665 readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
James Smart2e90f4b2011-12-13 13:22:37 -05001666 return;
James Smart6b5151f2012-01-18 16:24:06 -05001667 }
James Smart2e90f4b2011-12-13 13:22:37 -05001668 reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
1669 reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
James Smart2fcee4b2010-12-15 17:57:46 -05001670 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
1671 /* TODO: Register for Overtemp async events. */
1672 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1673 "2889 Port Overtemperature event, "
James Smart026abb82011-12-13 13:20:45 -05001674 "taking port offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001675 spin_lock_irq(&phba->hbalock);
1676 phba->over_temp_state = HBA_OVER_TEMP;
1677 spin_unlock_irq(&phba->hbalock);
1678 lpfc_sli4_offline_eratt(phba);
James Smart2e90f4b2011-12-13 13:22:37 -05001679 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001680 }
James Smart2e90f4b2011-12-13 13:22:37 -05001681 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smarte10b2022014-02-20 09:57:43 -05001682 reg_err2 == SLIPORT_ERR2_REG_FW_RESTART) {
James Smart2e90f4b2011-12-13 13:22:37 -05001683 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte10b2022014-02-20 09:57:43 -05001684 "3143 Port Down: Firmware Update "
1685 "Detected\n");
1686 en_rn_msg = false;
1687 } else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smart2e90f4b2011-12-13 13:22:37 -05001688 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1689 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1690 "3144 Port Down: Debug Dump\n");
1691 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1692 reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
1693 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1694 "3145 Port Down: Provisioning\n");
James Smart618a5232012-06-12 13:54:36 -04001695
1696 /* Check port status register for function reset */
James Smarte10b2022014-02-20 09:57:43 -05001697 rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT,
1698 en_rn_msg);
James Smart618a5232012-06-12 13:54:36 -04001699 if (rc == 0) {
1700 /* don't report event on forced debug dump */
1701 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1702 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1703 return;
1704 else
1705 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001706 }
James Smart618a5232012-06-12 13:54:36 -04001707 /* fall through for not able to recover */
James Smart6b5151f2012-01-18 16:24:06 -05001708 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1709 "3152 Unrecoverable error, bring the port "
1710 "offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001711 lpfc_sli4_offline_eratt(phba);
1712 break;
1713 case LPFC_SLI_INTF_IF_TYPE_1:
1714 default:
1715 break;
1716 }
James Smart2e90f4b2011-12-13 13:22:37 -05001717 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1718 "3123 Report dump event to upper layer\n");
1719 /* Send an internal error event to mgmt application */
1720 lpfc_board_errevt_to_mgmt(phba);
1721
1722 event_data = FC_REG_DUMP_EVENT;
1723 shost = lpfc_shost_from_vport(vport);
1724 fc_host_post_vendor_event(shost, fc_get_event_number(),
1725 sizeof(event_data), (char *) &event_data,
1726 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
James Smartda0436e2009-05-22 14:51:39 -04001727}
1728
1729/**
1730 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1731 * @phba: pointer to lpfc HBA data structure.
1732 *
1733 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1734 * routine from the API jump table function pointer from the lpfc_hba struct.
1735 *
1736 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001737 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001738 * Any other value - error.
1739 **/
1740void
1741lpfc_handle_eratt(struct lpfc_hba *phba)
1742{
1743 (*phba->lpfc_handle_eratt)(phba);
1744}
1745
1746/**
James Smart3621a712009-04-06 18:47:14 -04001747 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04001748 * @phba: pointer to lpfc hba data structure.
1749 *
1750 * This routine is invoked from the worker thread to handle a HBA host
1751 * attention link event.
1752 **/
dea31012005-04-17 16:05:31 -05001753void
James Smart2e0fef82007-06-17 19:56:36 -05001754lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001755{
James Smart2e0fef82007-06-17 19:56:36 -05001756 struct lpfc_vport *vport = phba->pport;
1757 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001758 LPFC_MBOXQ_t *pmb;
1759 volatile uint32_t control;
1760 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05001761 int rc = 0;
dea31012005-04-17 16:05:31 -05001762
1763 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001764 if (!pmb) {
1765 rc = 1;
dea31012005-04-17 16:05:31 -05001766 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05001767 }
dea31012005-04-17 16:05:31 -05001768
1769 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001770 if (!mp) {
1771 rc = 2;
dea31012005-04-17 16:05:31 -05001772 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05001773 }
dea31012005-04-17 16:05:31 -05001774
1775 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05001776 if (!mp->virt) {
1777 rc = 3;
dea31012005-04-17 16:05:31 -05001778 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05001779 }
dea31012005-04-17 16:05:31 -05001780
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05001781 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04001782 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05001783
1784 psli->slistat.link_event++;
James Smart76a95d72010-11-20 23:11:48 -05001785 lpfc_read_topology(phba, pmb, mp);
1786 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smart2e0fef82007-06-17 19:56:36 -05001787 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04001788 /* Block ELS IOCBs until we have processed this mbox command */
1789 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04001790 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05001791 if (rc == MBX_NOT_FINISHED) {
1792 rc = 4;
James Smart14691152006-12-02 13:34:28 -05001793 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05001794 }
dea31012005-04-17 16:05:31 -05001795
1796 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05001797 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001798 writel(HA_LATT, phba->HAregaddr);
1799 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001800 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001801
1802 return;
1803
James Smart14691152006-12-02 13:34:28 -05001804lpfc_handle_latt_free_mbuf:
James Smart0d2b6b82008-06-14 22:52:47 -04001805 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05001806 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001807lpfc_handle_latt_free_mp:
1808 kfree(mp);
1809lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04001810 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001811lpfc_handle_latt_err_exit:
1812 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001813 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001814 psli->sli_flag |= LPFC_PROCESS_LA;
1815 control = readl(phba->HCregaddr);
1816 control |= HC_LAINT_ENA;
1817 writel(control, phba->HCregaddr);
1818 readl(phba->HCregaddr); /* flush */
1819
1820 /* Clear Link Attention in HA REG */
1821 writel(HA_LATT, phba->HAregaddr);
1822 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001823 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001824 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001825 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001826
James Smart09372822008-01-11 01:52:54 -05001827 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1828 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05001829
1830 return;
1831}
1832
James Smarte59058c2008-08-24 21:49:00 -04001833/**
James Smart3621a712009-04-06 18:47:14 -04001834 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04001835 * @phba: pointer to lpfc hba data structure.
1836 * @vpd: pointer to the vital product data.
1837 * @len: length of the vital product data in bytes.
1838 *
1839 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1840 * an array of characters. In this routine, the ModelName, ProgramType, and
1841 * ModelDesc, etc. fields of the phba data structure will be populated.
1842 *
1843 * Return codes
1844 * 0 - pointer to the VPD passed in is NULL
1845 * 1 - success
1846 **/
James Smart3772a992009-05-22 14:50:54 -04001847int
James Smart2e0fef82007-06-17 19:56:36 -05001848lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05001849{
1850 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05001851 int Length;
dea31012005-04-17 16:05:31 -05001852 int i, j;
1853 int finished = 0;
1854 int index = 0;
1855
1856 if (!vpd)
1857 return 0;
1858
1859 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05001860 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001861 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05001862 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1863 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001864 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05001865 switch (vpd[index]) {
1866 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001867 case 0x91:
dea31012005-04-17 16:05:31 -05001868 index += 1;
1869 lenlo = vpd[index];
1870 index += 1;
1871 lenhi = vpd[index];
1872 index += 1;
1873 i = ((((unsigned short)lenhi) << 8) + lenlo);
1874 index += i;
1875 break;
1876 case 0x90:
1877 index += 1;
1878 lenlo = vpd[index];
1879 index += 1;
1880 lenhi = vpd[index];
1881 index += 1;
1882 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001883 if (Length > len - index)
1884 Length = len - index;
dea31012005-04-17 16:05:31 -05001885 while (Length > 0) {
1886 /* Look for Serial Number */
1887 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1888 index += 2;
1889 i = vpd[index];
1890 index += 1;
1891 j = 0;
1892 Length -= (3+i);
1893 while(i--) {
1894 phba->SerialNumber[j++] = vpd[index++];
1895 if (j == 31)
1896 break;
1897 }
1898 phba->SerialNumber[j] = 0;
1899 continue;
1900 }
1901 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1902 phba->vpd_flag |= VPD_MODEL_DESC;
1903 index += 2;
1904 i = vpd[index];
1905 index += 1;
1906 j = 0;
1907 Length -= (3+i);
1908 while(i--) {
1909 phba->ModelDesc[j++] = vpd[index++];
1910 if (j == 255)
1911 break;
1912 }
1913 phba->ModelDesc[j] = 0;
1914 continue;
1915 }
1916 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1917 phba->vpd_flag |= VPD_MODEL_NAME;
1918 index += 2;
1919 i = vpd[index];
1920 index += 1;
1921 j = 0;
1922 Length -= (3+i);
1923 while(i--) {
1924 phba->ModelName[j++] = vpd[index++];
1925 if (j == 79)
1926 break;
1927 }
1928 phba->ModelName[j] = 0;
1929 continue;
1930 }
1931 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1932 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1933 index += 2;
1934 i = vpd[index];
1935 index += 1;
1936 j = 0;
1937 Length -= (3+i);
1938 while(i--) {
1939 phba->ProgramType[j++] = vpd[index++];
1940 if (j == 255)
1941 break;
1942 }
1943 phba->ProgramType[j] = 0;
1944 continue;
1945 }
1946 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1947 phba->vpd_flag |= VPD_PORT;
1948 index += 2;
1949 i = vpd[index];
1950 index += 1;
1951 j = 0;
1952 Length -= (3+i);
1953 while(i--) {
James Smartcd1c8302011-10-10 21:33:25 -04001954 if ((phba->sli_rev == LPFC_SLI_REV4) &&
1955 (phba->sli4_hba.pport_name_sta ==
1956 LPFC_SLI4_PPNAME_GET)) {
1957 j++;
1958 index++;
1959 } else
1960 phba->Port[j++] = vpd[index++];
1961 if (j == 19)
1962 break;
dea31012005-04-17 16:05:31 -05001963 }
James Smartcd1c8302011-10-10 21:33:25 -04001964 if ((phba->sli_rev != LPFC_SLI_REV4) ||
1965 (phba->sli4_hba.pport_name_sta ==
1966 LPFC_SLI4_PPNAME_NON))
1967 phba->Port[j] = 0;
dea31012005-04-17 16:05:31 -05001968 continue;
1969 }
1970 else {
1971 index += 2;
1972 i = vpd[index];
1973 index += 1;
1974 index += i;
1975 Length -= (3 + i);
1976 }
1977 }
1978 finished = 0;
1979 break;
1980 case 0x78:
1981 finished = 1;
1982 break;
1983 default:
1984 index ++;
1985 break;
1986 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001987 }
dea31012005-04-17 16:05:31 -05001988
1989 return(1);
1990}
1991
James Smarte59058c2008-08-24 21:49:00 -04001992/**
James Smart3621a712009-04-06 18:47:14 -04001993 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04001994 * @phba: pointer to lpfc hba data structure.
1995 * @mdp: pointer to the data structure to hold the derived model name.
1996 * @descp: pointer to the data structure to hold the derived description.
1997 *
1998 * This routine retrieves HBA's description based on its registered PCI device
1999 * ID. The @descp passed into this function points to an array of 256 chars. It
2000 * shall be returned with the model name, maximum speed, and the host bus type.
2001 * The @mdp passed into this function points to an array of 80 chars. When the
2002 * function returns, the @mdp will be filled with the model name.
2003 **/
dea31012005-04-17 16:05:31 -05002004static void
James Smart2e0fef82007-06-17 19:56:36 -05002005lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05002006{
2007 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05002008 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002009 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04002010 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04002011 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002012 struct {
James Smarta747c9c2009-11-18 15:41:10 -05002013 char *name;
2014 char *bus;
2015 char *function;
2016 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002017
2018 if (mdp && mdp[0] != '\0'
2019 && descp && descp[0] != '\0')
2020 return;
2021
James Smartc0c11512011-05-24 11:41:34 -04002022 if (phba->lmt & LMT_16Gb)
2023 max_speed = 16;
2024 else if (phba->lmt & LMT_10Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002025 max_speed = 10;
2026 else if (phba->lmt & LMT_8Gb)
2027 max_speed = 8;
2028 else if (phba->lmt & LMT_4Gb)
2029 max_speed = 4;
2030 else if (phba->lmt & LMT_2Gb)
2031 max_speed = 2;
James Smart4169d862012-09-29 11:32:09 -04002032 else if (phba->lmt & LMT_1Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002033 max_speed = 1;
James Smart4169d862012-09-29 11:32:09 -04002034 else
2035 max_speed = 0;
dea31012005-04-17 16:05:31 -05002036
2037 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05002038
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002039 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002040 case PCI_DEVICE_ID_FIREFLY:
James Smart12222f42014-05-21 08:05:19 -04002041 m = (typeof(m)){"LP6000", "PCI",
2042 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002043 break;
dea31012005-04-17 16:05:31 -05002044 case PCI_DEVICE_ID_SUPERFLY:
2045 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smart12222f42014-05-21 08:05:19 -04002046 m = (typeof(m)){"LP7000", "PCI", ""};
dea31012005-04-17 16:05:31 -05002047 else
James Smart12222f42014-05-21 08:05:19 -04002048 m = (typeof(m)){"LP7000E", "PCI", ""};
2049 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
dea31012005-04-17 16:05:31 -05002050 break;
2051 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002052 m = (typeof(m)){"LP8000", "PCI",
James Smart12222f42014-05-21 08:05:19 -04002053 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002054 break;
2055 case PCI_DEVICE_ID_CENTAUR:
2056 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smart12222f42014-05-21 08:05:19 -04002057 m = (typeof(m)){"LP9002", "PCI", ""};
dea31012005-04-17 16:05:31 -05002058 else
James Smart12222f42014-05-21 08:05:19 -04002059 m = (typeof(m)){"LP9000", "PCI", ""};
2060 m.function = "Obsolete, Unsupported Fibre Channel Adapter";
dea31012005-04-17 16:05:31 -05002061 break;
2062 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002063 m = (typeof(m)){"LP952", "PCI",
James Smart12222f42014-05-21 08:05:19 -04002064 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002065 break;
2066 case PCI_DEVICE_ID_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05002067 m = (typeof(m)){"LP9802", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002068 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002069 break;
2070 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05002071 m = (typeof(m)){"LP10000", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002072 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002073 break;
2074 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05002075 m = (typeof(m)){"LPX1000", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002076 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002077 break;
2078 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002079 m = (typeof(m)){"LP982", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002080 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002081 break;
2082 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002083 m = (typeof(m)){"LP1050", "PCI-X",
James Smart12222f42014-05-21 08:05:19 -04002084 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002085 break;
2086 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05002087 m = (typeof(m)){"LP11000", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002088 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002089 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002090 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002091 m = (typeof(m)){"LP11000-SP", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002092 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002093 break;
2094 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002095 m = (typeof(m)){"LP11002-SP", "PCI-X2",
James Smart12222f42014-05-21 08:05:19 -04002096 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002097 break;
2098 case PCI_DEVICE_ID_NEPTUNE:
James Smart12222f42014-05-21 08:05:19 -04002099 m = (typeof(m)){"LPe1000", "PCIe",
2100 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002101 break;
2102 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smart12222f42014-05-21 08:05:19 -04002103 m = (typeof(m)){"LPe1000-SP", "PCIe",
2104 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002105 break;
2106 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smart12222f42014-05-21 08:05:19 -04002107 m = (typeof(m)){"LPe1002-SP", "PCIe",
2108 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002109 break;
dea31012005-04-17 16:05:31 -05002110 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05002111 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002112 break;
2113 case PCI_DEVICE_ID_BSMB:
James Smart12222f42014-05-21 08:05:19 -04002114 m = (typeof(m)){"LP111", "PCI-X2",
2115 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002116 break;
2117 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05002118 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002119 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002120 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002121 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002122 break;
2123 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002124 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04002125 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002126 break;
dea31012005-04-17 16:05:31 -05002127 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05002128 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002129 break;
2130 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05002131 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002132 break;
2133 case PCI_DEVICE_ID_LP101:
James Smart12222f42014-05-21 08:05:19 -04002134 m = (typeof(m)){"LP101", "PCI-X",
2135 "Obsolete, Unsupported Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002136 break;
2137 case PCI_DEVICE_ID_LP10000S:
James Smart12222f42014-05-21 08:05:19 -04002138 m = (typeof(m)){"LP10000-S", "PCI",
2139 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002140 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002141 case PCI_DEVICE_ID_LP11000S:
James Smart12222f42014-05-21 08:05:19 -04002142 m = (typeof(m)){"LP11000-S", "PCI-X2",
2143 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05002144 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002145 case PCI_DEVICE_ID_LPE11000S:
James Smart12222f42014-05-21 08:05:19 -04002146 m = (typeof(m)){"LPe11000-S", "PCIe",
2147 "Obsolete, Unsupported Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002148 break;
James Smartb87eab32007-04-25 09:53:28 -04002149 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05002150 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002151 break;
2152 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05002153 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002154 break;
2155 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05002156 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002157 break;
2158 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002159 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002160 break;
2161 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002162 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002163 break;
2164 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05002165 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002166 break;
James Smart84774a42008-08-24 21:50:06 -04002167 case PCI_DEVICE_ID_HORNET:
James Smart12222f42014-05-21 08:05:19 -04002168 m = (typeof(m)){"LP21000", "PCIe",
2169 "Obsolete, Unsupported FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002170 GE = 1;
2171 break;
2172 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05002173 m = (typeof(m)){"LPev12000", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002174 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002175 break;
2176 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05002177 m = (typeof(m)){"LPev12000", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002178 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002179 break;
2180 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05002181 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
James Smart12222f42014-05-21 08:05:19 -04002182 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002183 break;
James Smartda0436e2009-05-22 14:51:39 -04002184 case PCI_DEVICE_ID_TIGERSHARK:
2185 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002186 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04002187 break;
James Smarta747c9c2009-11-18 15:41:10 -05002188 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04002189 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002190 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2191 break;
2192 case PCI_DEVICE_ID_FALCON:
2193 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2194 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04002195 break;
James Smart98fc5dd2010-06-07 15:24:29 -04002196 case PCI_DEVICE_ID_BALIUS:
2197 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
James Smart12222f42014-05-21 08:05:19 -04002198 "Obsolete, Unsupported Fibre Channel Adapter"};
James Smart98fc5dd2010-06-07 15:24:29 -04002199 break;
James Smart085c6472010-11-20 23:11:37 -05002200 case PCI_DEVICE_ID_LANCER_FC:
James Smartc0c11512011-05-24 11:41:34 -04002201 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
James Smart085c6472010-11-20 23:11:37 -05002202 break;
James Smart12222f42014-05-21 08:05:19 -04002203 case PCI_DEVICE_ID_LANCER_FC_VF:
2204 m = (typeof(m)){"LPe16000", "PCIe",
2205 "Obsolete, Unsupported Fibre Channel Adapter"};
2206 break;
James Smart085c6472010-11-20 23:11:37 -05002207 case PCI_DEVICE_ID_LANCER_FCOE:
2208 oneConnect = 1;
James Smart079b5c92011-08-21 21:48:49 -04002209 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
James Smart085c6472010-11-20 23:11:37 -05002210 break;
James Smart12222f42014-05-21 08:05:19 -04002211 case PCI_DEVICE_ID_LANCER_FCOE_VF:
2212 oneConnect = 1;
2213 m = (typeof(m)){"OCe15100", "PCIe",
2214 "Obsolete, Unsupported FCoE"};
2215 break;
James Smartf8cafd32012-08-03 12:42:51 -04002216 case PCI_DEVICE_ID_SKYHAWK:
2217 case PCI_DEVICE_ID_SKYHAWK_VF:
2218 oneConnect = 1;
2219 m = (typeof(m)){"OCe14000", "PCIe", "FCoE"};
2220 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002221 default:
James Smarta747c9c2009-11-18 15:41:10 -05002222 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002223 break;
dea31012005-04-17 16:05:31 -05002224 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002225
2226 if (mdp && mdp[0] == '\0')
2227 snprintf(mdp, 79,"%s", m.name);
James Smartc0c11512011-05-24 11:41:34 -04002228 /*
2229 * oneConnect hba requires special processing, they are all initiators
James Smartda0436e2009-05-22 14:51:39 -04002230 * and we put the port number on the end
2231 */
2232 if (descp && descp[0] == '\0') {
2233 if (oneConnect)
2234 snprintf(descp, 255,
James Smart4169d862012-09-29 11:32:09 -04002235 "Emulex OneConnect %s, %s Initiator %s",
James Smarta747c9c2009-11-18 15:41:10 -05002236 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04002237 phba->Port);
James Smart4169d862012-09-29 11:32:09 -04002238 else if (max_speed == 0)
2239 snprintf(descp, 255,
2240 "Emulex %s %s %s ",
2241 m.name, m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002242 else
2243 snprintf(descp, 255,
2244 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05002245 m.name, max_speed, (GE) ? "GE" : "Gb",
2246 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002247 }
dea31012005-04-17 16:05:31 -05002248}
2249
James Smarte59058c2008-08-24 21:49:00 -04002250/**
James Smart3621a712009-04-06 18:47:14 -04002251 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04002252 * @phba: pointer to lpfc hba data structure.
2253 * @pring: pointer to a IOCB ring.
2254 * @cnt: the number of IOCBs to be posted to the IOCB ring.
2255 *
2256 * This routine posts a given number of IOCBs with the associated DMA buffer
2257 * descriptors specified by the cnt argument to the given IOCB ring.
2258 *
2259 * Return codes
2260 * The number of IOCBs NOT able to be posted to the IOCB ring.
2261 **/
dea31012005-04-17 16:05:31 -05002262int
James Smart495a7142008-06-14 22:52:59 -04002263lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05002264{
2265 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002266 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05002267 struct lpfc_dmabuf *mp1, *mp2;
2268
2269 cnt += pring->missbufcnt;
2270
2271 /* While there are buffers to post */
2272 while (cnt > 0) {
2273 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002274 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002275 if (iocb == NULL) {
2276 pring->missbufcnt = cnt;
2277 return cnt;
2278 }
dea31012005-04-17 16:05:31 -05002279 icmd = &iocb->iocb;
2280
2281 /* 2 buffers can be posted per command */
2282 /* Allocate buffer to post */
2283 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2284 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04002285 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2286 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002287 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002288 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002289 pring->missbufcnt = cnt;
2290 return cnt;
2291 }
2292
2293 INIT_LIST_HEAD(&mp1->list);
2294 /* Allocate buffer to post */
2295 if (cnt > 1) {
2296 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2297 if (mp2)
2298 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2299 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04002300 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002301 kfree(mp2);
dea31012005-04-17 16:05:31 -05002302 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2303 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002304 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002305 pring->missbufcnt = cnt;
2306 return cnt;
2307 }
2308
2309 INIT_LIST_HEAD(&mp2->list);
2310 } else {
2311 mp2 = NULL;
2312 }
2313
2314 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2315 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2316 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2317 icmd->ulpBdeCount = 1;
2318 cnt--;
2319 if (mp2) {
2320 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2321 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2322 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2323 cnt--;
2324 icmd->ulpBdeCount = 2;
2325 }
2326
2327 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2328 icmd->ulpLe = 1;
2329
James Smart3772a992009-05-22 14:50:54 -04002330 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2331 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05002332 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2333 kfree(mp1);
2334 cnt++;
2335 if (mp2) {
2336 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2337 kfree(mp2);
2338 cnt++;
2339 }
James Bottomley604a3e32005-10-29 10:28:33 -05002340 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002341 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05002342 return cnt;
2343 }
dea31012005-04-17 16:05:31 -05002344 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05002345 if (mp2)
dea31012005-04-17 16:05:31 -05002346 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05002347 }
2348 pring->missbufcnt = 0;
2349 return 0;
2350}
2351
James Smarte59058c2008-08-24 21:49:00 -04002352/**
James Smart3621a712009-04-06 18:47:14 -04002353 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04002354 * @phba: pointer to lpfc hba data structure.
2355 *
2356 * This routine posts initial receive IOCB buffers to the ELS ring. The
2357 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
2358 * set to 64 IOCBs.
2359 *
2360 * Return codes
2361 * 0 - success (currently always success)
2362 **/
dea31012005-04-17 16:05:31 -05002363static int
James Smart2e0fef82007-06-17 19:56:36 -05002364lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002365{
2366 struct lpfc_sli *psli = &phba->sli;
2367
2368 /* Ring 0, ELS / CT buffers */
James Smart495a7142008-06-14 22:52:59 -04002369 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05002370 /* Ring 2 - FCP no buffers needed */
2371
2372 return 0;
2373}
2374
2375#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2376
James Smarte59058c2008-08-24 21:49:00 -04002377/**
James Smart3621a712009-04-06 18:47:14 -04002378 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04002379 * @HashResultPointer: pointer to an array as hash table.
2380 *
2381 * This routine sets up the initial values to the array of hash table entries
2382 * for the LC HBAs.
2383 **/
dea31012005-04-17 16:05:31 -05002384static void
2385lpfc_sha_init(uint32_t * HashResultPointer)
2386{
2387 HashResultPointer[0] = 0x67452301;
2388 HashResultPointer[1] = 0xEFCDAB89;
2389 HashResultPointer[2] = 0x98BADCFE;
2390 HashResultPointer[3] = 0x10325476;
2391 HashResultPointer[4] = 0xC3D2E1F0;
2392}
2393
James Smarte59058c2008-08-24 21:49:00 -04002394/**
James Smart3621a712009-04-06 18:47:14 -04002395 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04002396 * @HashResultPointer: pointer to an initial/result hash table.
2397 * @HashWorkingPointer: pointer to an working hash table.
2398 *
2399 * This routine iterates an initial hash table pointed by @HashResultPointer
2400 * with the values from the working hash table pointeed by @HashWorkingPointer.
2401 * The results are putting back to the initial hash table, returned through
2402 * the @HashResultPointer as the result hash table.
2403 **/
dea31012005-04-17 16:05:31 -05002404static void
2405lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2406{
2407 int t;
2408 uint32_t TEMP;
2409 uint32_t A, B, C, D, E;
2410 t = 16;
2411 do {
2412 HashWorkingPointer[t] =
2413 S(1,
2414 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2415 8] ^
2416 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2417 } while (++t <= 79);
2418 t = 0;
2419 A = HashResultPointer[0];
2420 B = HashResultPointer[1];
2421 C = HashResultPointer[2];
2422 D = HashResultPointer[3];
2423 E = HashResultPointer[4];
2424
2425 do {
2426 if (t < 20) {
2427 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2428 } else if (t < 40) {
2429 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2430 } else if (t < 60) {
2431 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2432 } else {
2433 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2434 }
2435 TEMP += S(5, A) + E + HashWorkingPointer[t];
2436 E = D;
2437 D = C;
2438 C = S(30, B);
2439 B = A;
2440 A = TEMP;
2441 } while (++t <= 79);
2442
2443 HashResultPointer[0] += A;
2444 HashResultPointer[1] += B;
2445 HashResultPointer[2] += C;
2446 HashResultPointer[3] += D;
2447 HashResultPointer[4] += E;
2448
2449}
2450
James Smarte59058c2008-08-24 21:49:00 -04002451/**
James Smart3621a712009-04-06 18:47:14 -04002452 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002453 * @RandomChallenge: pointer to the entry of host challenge random number array.
2454 * @HashWorking: pointer to the entry of the working hash array.
2455 *
2456 * This routine calculates the working hash array referred by @HashWorking
2457 * from the challenge random numbers associated with the host, referred by
2458 * @RandomChallenge. The result is put into the entry of the working hash
2459 * array and returned by reference through @HashWorking.
2460 **/
dea31012005-04-17 16:05:31 -05002461static void
2462lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2463{
2464 *HashWorking = (*RandomChallenge ^ *HashWorking);
2465}
2466
James Smarte59058c2008-08-24 21:49:00 -04002467/**
James Smart3621a712009-04-06 18:47:14 -04002468 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002469 * @phba: pointer to lpfc hba data structure.
2470 * @hbainit: pointer to an array of unsigned 32-bit integers.
2471 *
2472 * This routine performs the special handling for LC HBA initialization.
2473 **/
dea31012005-04-17 16:05:31 -05002474void
2475lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2476{
2477 int t;
2478 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002479 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002480
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002481 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002482 if (!HashWorking)
2483 return;
2484
dea31012005-04-17 16:05:31 -05002485 HashWorking[0] = HashWorking[78] = *pwwnn++;
2486 HashWorking[1] = HashWorking[79] = *pwwnn;
2487
2488 for (t = 0; t < 7; t++)
2489 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2490
2491 lpfc_sha_init(hbainit);
2492 lpfc_sha_iterate(hbainit, HashWorking);
2493 kfree(HashWorking);
2494}
2495
James Smarte59058c2008-08-24 21:49:00 -04002496/**
James Smart3621a712009-04-06 18:47:14 -04002497 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002498 * @vport: pointer to a virtual N_Port data structure.
2499 *
2500 * This routine performs the necessary cleanups before deleting the @vport.
2501 * It invokes the discovery state machine to perform necessary state
2502 * transitions and to release the ndlps associated with the @vport. Note,
2503 * the physical port is treated as @vport 0.
2504 **/
James Smart87af33f2007-10-27 13:37:43 -04002505void
James Smart2e0fef82007-06-17 19:56:36 -05002506lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002507{
James Smart87af33f2007-10-27 13:37:43 -04002508 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002509 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002510 int i = 0;
dea31012005-04-17 16:05:31 -05002511
James Smart87af33f2007-10-27 13:37:43 -04002512 if (phba->link_state > LPFC_LINK_DOWN)
2513 lpfc_port_link_failure(vport);
2514
2515 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002516 if (!NLP_CHK_NODE_ACT(ndlp)) {
2517 ndlp = lpfc_enable_node(vport, ndlp,
2518 NLP_STE_UNUSED_NODE);
2519 if (!ndlp)
2520 continue;
2521 spin_lock_irq(&phba->ndlp_lock);
2522 NLP_SET_FREE_REQ(ndlp);
2523 spin_unlock_irq(&phba->ndlp_lock);
2524 /* Trigger the release of the ndlp memory */
2525 lpfc_nlp_put(ndlp);
2526 continue;
2527 }
2528 spin_lock_irq(&phba->ndlp_lock);
2529 if (NLP_CHK_FREE_REQ(ndlp)) {
2530 /* The ndlp should not be in memory free mode already */
2531 spin_unlock_irq(&phba->ndlp_lock);
2532 continue;
2533 } else
2534 /* Indicate request for freeing ndlp memory */
2535 NLP_SET_FREE_REQ(ndlp);
2536 spin_unlock_irq(&phba->ndlp_lock);
2537
James Smart58da1ff2008-04-07 10:15:56 -04002538 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2539 ndlp->nlp_DID == Fabric_DID) {
2540 /* Just free up ndlp with Fabric_DID for vports */
2541 lpfc_nlp_put(ndlp);
2542 continue;
2543 }
2544
James Smarteff4a012012-01-18 16:25:25 -05002545 /* take care of nodes in unused state before the state
2546 * machine taking action.
2547 */
2548 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
2549 lpfc_nlp_put(ndlp);
2550 continue;
2551 }
2552
James Smart87af33f2007-10-27 13:37:43 -04002553 if (ndlp->nlp_type & NLP_FABRIC)
2554 lpfc_disc_state_machine(vport, ndlp, NULL,
2555 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002556
James Smart87af33f2007-10-27 13:37:43 -04002557 lpfc_disc_state_machine(vport, ndlp, NULL,
2558 NLP_EVT_DEVICE_RM);
2559 }
2560
James Smarta8adb832007-10-27 13:37:53 -04002561 /* At this point, ALL ndlp's should be gone
2562 * because of the previous NLP_EVT_DEVICE_RM.
2563 * Lets wait for this to happen, if needed.
2564 */
James Smart87af33f2007-10-27 13:37:43 -04002565 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002566 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002567 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002568 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002569 list_for_each_entry_safe(ndlp, next_ndlp,
2570 &vport->fc_nodes, nlp_listp) {
2571 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2572 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002573 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002574 "usgmap:x%x refcnt:%d\n",
2575 ndlp->nlp_DID, (void *)ndlp,
2576 ndlp->nlp_usg_map,
2577 atomic_read(
2578 &ndlp->kref.refcount));
2579 }
James Smarta8adb832007-10-27 13:37:53 -04002580 break;
James Smart87af33f2007-10-27 13:37:43 -04002581 }
James Smarta8adb832007-10-27 13:37:53 -04002582
2583 /* Wait for any activity on ndlps to settle */
2584 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002585 }
James Smart1151e3e2011-02-16 12:39:35 -05002586 lpfc_cleanup_vports_rrqs(vport, NULL);
dea31012005-04-17 16:05:31 -05002587}
2588
James Smarte59058c2008-08-24 21:49:00 -04002589/**
James Smart3621a712009-04-06 18:47:14 -04002590 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002591 * @vport: pointer to a virtual N_Port data structure.
2592 *
2593 * This routine stops all the timers associated with a @vport. This function
2594 * is invoked before disabling or deleting a @vport. Note that the physical
2595 * port is treated as @vport 0.
2596 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002597void
2598lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002599{
James Smart92d7f7b2007-06-17 19:56:38 -05002600 del_timer_sync(&vport->els_tmofunc);
2601 del_timer_sync(&vport->fc_fdmitmo);
James Smart92494142011-02-16 12:39:44 -05002602 del_timer_sync(&vport->delayed_disc_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002603 lpfc_can_disctmo(vport);
2604 return;
dea31012005-04-17 16:05:31 -05002605}
2606
James Smarte59058c2008-08-24 21:49:00 -04002607/**
James Smartecfd03c2010-02-12 14:41:27 -05002608 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2609 * @phba: pointer to lpfc hba data structure.
2610 *
2611 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2612 * caller of this routine should already hold the host lock.
2613 **/
2614void
2615__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2616{
James Smart5ac6b302010-10-22 11:05:36 -04002617 /* Clear pending FCF rediscovery wait flag */
2618 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2619
James Smartecfd03c2010-02-12 14:41:27 -05002620 /* Now, try to stop the timer */
2621 del_timer(&phba->fcf.redisc_wait);
2622}
2623
2624/**
2625 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2626 * @phba: pointer to lpfc hba data structure.
2627 *
2628 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2629 * checks whether the FCF rediscovery wait timer is pending with the host
2630 * lock held before proceeding with disabling the timer and clearing the
2631 * wait timer pendig flag.
2632 **/
2633void
2634lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2635{
2636 spin_lock_irq(&phba->hbalock);
2637 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2638 /* FCF rediscovery timer already fired or stopped */
2639 spin_unlock_irq(&phba->hbalock);
2640 return;
2641 }
2642 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart5ac6b302010-10-22 11:05:36 -04002643 /* Clear failover in progress flags */
2644 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05002645 spin_unlock_irq(&phba->hbalock);
2646}
2647
2648/**
James Smart3772a992009-05-22 14:50:54 -04002649 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002650 * @phba: pointer to lpfc hba data structure.
2651 *
2652 * This routine stops all the timers associated with a HBA. This function is
2653 * invoked before either putting a HBA offline or unloading the driver.
2654 **/
James Smart3772a992009-05-22 14:50:54 -04002655void
2656lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002657{
James Smart51ef4c22007-08-02 11:10:31 -04002658 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05002659 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002660 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002661 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002662 del_timer_sync(&phba->hb_tmofunc);
James Smart1151e3e2011-02-16 12:39:35 -05002663 if (phba->sli_rev == LPFC_SLI_REV4) {
2664 del_timer_sync(&phba->rrq_tmr);
2665 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2666 }
James Smart3772a992009-05-22 14:50:54 -04002667 phba->hb_outstanding = 0;
2668
2669 switch (phba->pci_dev_grp) {
2670 case LPFC_PCI_DEV_LP:
2671 /* Stop any LightPulse device specific driver timers */
2672 del_timer_sync(&phba->fcp_poll_timer);
2673 break;
2674 case LPFC_PCI_DEV_OC:
2675 /* Stop any OneConnect device sepcific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05002676 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04002677 break;
2678 default:
2679 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2680 "0297 Invalid device group (x%x)\n",
2681 phba->pci_dev_grp);
2682 break;
2683 }
James Smart2e0fef82007-06-17 19:56:36 -05002684 return;
dea31012005-04-17 16:05:31 -05002685}
2686
James Smarte59058c2008-08-24 21:49:00 -04002687/**
James Smart3621a712009-04-06 18:47:14 -04002688 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002689 * @phba: pointer to lpfc hba data structure.
2690 *
2691 * This routine marks a HBA's management interface as blocked. Once the HBA's
2692 * management interface is marked as blocked, all the user space access to
2693 * the HBA, whether they are from sysfs interface or libdfc interface will
2694 * all be blocked. The HBA is set to block the management interface when the
2695 * driver prepares the HBA interface for online or offline.
2696 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002697static void
James Smart618a5232012-06-12 13:54:36 -04002698lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
Adrian Bunka6ababd2007-11-05 18:07:33 +01002699{
2700 unsigned long iflag;
James Smart6e7288d2010-06-07 15:23:35 -04002701 uint8_t actcmd = MBX_HEARTBEAT;
2702 unsigned long timeout;
2703
Adrian Bunka6ababd2007-11-05 18:07:33 +01002704 spin_lock_irqsave(&phba->hbalock, iflag);
2705 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
James Smart618a5232012-06-12 13:54:36 -04002706 spin_unlock_irqrestore(&phba->hbalock, iflag);
2707 if (mbx_action == LPFC_MBX_NO_WAIT)
2708 return;
2709 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
2710 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002711 if (phba->sli.mbox_active) {
James Smart6e7288d2010-06-07 15:23:35 -04002712 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
James Smarta183a152011-10-10 21:32:43 -04002713 /* Determine how long we might wait for the active mailbox
2714 * command to be gracefully completed by firmware.
2715 */
2716 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
2717 phba->sli.mbox_active) * 1000) + jiffies;
2718 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002719 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002720
James Smart6e7288d2010-06-07 15:23:35 -04002721 /* Wait for the outstnading mailbox command to complete */
2722 while (phba->sli.mbox_active) {
2723 /* Check active mailbox complete status every 2ms */
2724 msleep(2);
2725 if (time_after(jiffies, timeout)) {
2726 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2727 "2813 Mgmt IO is Blocked %x "
2728 "- mbox cmd %x still active\n",
2729 phba->sli.sli_flag, actcmd);
2730 break;
2731 }
2732 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002733}
2734
James Smarte59058c2008-08-24 21:49:00 -04002735/**
James Smart6b5151f2012-01-18 16:24:06 -05002736 * lpfc_sli4_node_prep - Assign RPIs for active nodes.
2737 * @phba: pointer to lpfc hba data structure.
2738 *
2739 * Allocate RPIs for all active remote nodes. This is needed whenever
2740 * an SLI4 adapter is reset and the driver is not unloading. Its purpose
2741 * is to fixup the temporary rpi assignments.
2742 **/
2743void
2744lpfc_sli4_node_prep(struct lpfc_hba *phba)
2745{
2746 struct lpfc_nodelist *ndlp, *next_ndlp;
2747 struct lpfc_vport **vports;
2748 int i;
2749
2750 if (phba->sli_rev != LPFC_SLI_REV4)
2751 return;
2752
2753 vports = lpfc_create_vport_work_array(phba);
2754 if (vports != NULL) {
2755 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2756 if (vports[i]->load_flag & FC_UNLOADING)
2757 continue;
2758
2759 list_for_each_entry_safe(ndlp, next_ndlp,
2760 &vports[i]->fc_nodes,
2761 nlp_listp) {
2762 if (NLP_CHK_NODE_ACT(ndlp))
2763 ndlp->nlp_rpi =
2764 lpfc_sli4_alloc_rpi(phba);
2765 }
2766 }
2767 }
2768 lpfc_destroy_vport_work_array(phba, vports);
2769}
2770
2771/**
James Smart3621a712009-04-06 18:47:14 -04002772 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04002773 * @phba: pointer to lpfc hba data structure.
2774 *
2775 * This routine initializes the HBA and brings a HBA online. During this
2776 * process, the management interface is blocked to prevent user space access
2777 * to the HBA interfering with the driver initialization.
2778 *
2779 * Return codes
2780 * 0 - successful
2781 * 1 - failed
2782 **/
dea31012005-04-17 16:05:31 -05002783int
James Smart2e0fef82007-06-17 19:56:36 -05002784lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002785{
Julia Lawall372bd282008-12-16 16:15:08 +01002786 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04002787 struct lpfc_vport **vports;
2788 int i;
James Smart16a3a202013-04-17 20:14:38 -04002789 bool vpis_cleared = false;
James Smart2e0fef82007-06-17 19:56:36 -05002790
dea31012005-04-17 16:05:31 -05002791 if (!phba)
2792 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01002793 vport = phba->pport;
dea31012005-04-17 16:05:31 -05002794
James Smart2e0fef82007-06-17 19:56:36 -05002795 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05002796 return 0;
2797
James Smarted957682007-06-17 19:56:37 -05002798 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002799 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05002800
James Smart618a5232012-06-12 13:54:36 -04002801 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05002802
James Smart46fa3112007-04-25 09:51:45 -04002803 if (!lpfc_sli_queue_setup(phba)) {
2804 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002805 return 1;
James Smart46fa3112007-04-25 09:51:45 -04002806 }
2807
James Smartda0436e2009-05-22 14:51:39 -04002808 if (phba->sli_rev == LPFC_SLI_REV4) {
2809 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2810 lpfc_unblock_mgmt_io(phba);
2811 return 1;
2812 }
James Smart16a3a202013-04-17 20:14:38 -04002813 spin_lock_irq(&phba->hbalock);
2814 if (!phba->sli4_hba.max_cfg_param.vpi_used)
2815 vpis_cleared = true;
2816 spin_unlock_irq(&phba->hbalock);
James Smartda0436e2009-05-22 14:51:39 -04002817 } else {
2818 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2819 lpfc_unblock_mgmt_io(phba);
2820 return 1;
2821 }
James Smart46fa3112007-04-25 09:51:45 -04002822 }
dea31012005-04-17 16:05:31 -05002823
James Smart549e55c2007-08-02 11:09:51 -04002824 vports = lpfc_create_vport_work_array(phba);
2825 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002826 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002827 struct Scsi_Host *shost;
2828 shost = lpfc_shost_from_vport(vports[i]);
2829 spin_lock_irq(shost->host_lock);
2830 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2831 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2832 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart16a3a202013-04-17 20:14:38 -04002833 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart1c6834a2009-07-19 10:01:26 -04002834 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart16a3a202013-04-17 20:14:38 -04002835 if ((vpis_cleared) &&
2836 (vports[i]->port_type !=
2837 LPFC_PHYSICAL_PORT))
2838 vports[i]->vpi = 0;
2839 }
James Smart549e55c2007-08-02 11:09:51 -04002840 spin_unlock_irq(shost->host_lock);
2841 }
James Smart09372822008-01-11 01:52:54 -05002842 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002843
James Smart46fa3112007-04-25 09:51:45 -04002844 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002845 return 0;
2846}
2847
James Smarte59058c2008-08-24 21:49:00 -04002848/**
James Smart3621a712009-04-06 18:47:14 -04002849 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04002850 * @phba: pointer to lpfc hba data structure.
2851 *
2852 * This routine marks a HBA's management interface as not blocked. Once the
2853 * HBA's management interface is marked as not blocked, all the user space
2854 * access to the HBA, whether they are from sysfs interface or libdfc
2855 * interface will be allowed. The HBA is set to block the management interface
2856 * when the driver prepares the HBA interface for online or offline and then
2857 * set to unblock the management interface afterwards.
2858 **/
James Smart46fa3112007-04-25 09:51:45 -04002859void
James Smart46fa3112007-04-25 09:51:45 -04002860lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2861{
2862 unsigned long iflag;
2863
James Smart2e0fef82007-06-17 19:56:36 -05002864 spin_lock_irqsave(&phba->hbalock, iflag);
2865 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2866 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04002867}
2868
James Smarte59058c2008-08-24 21:49:00 -04002869/**
James Smart3621a712009-04-06 18:47:14 -04002870 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04002871 * @phba: pointer to lpfc hba data structure.
2872 *
2873 * This routine is invoked to prepare a HBA to be brought offline. It performs
2874 * unregistration login to all the nodes on all vports and flushes the mailbox
2875 * queue to make it ready to be brought offline.
2876 **/
James Smart46fa3112007-04-25 09:51:45 -04002877void
James Smart618a5232012-06-12 13:54:36 -04002878lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
James Smart46fa3112007-04-25 09:51:45 -04002879{
James Smart2e0fef82007-06-17 19:56:36 -05002880 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04002881 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04002882 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05002883 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04002884 int i;
dea31012005-04-17 16:05:31 -05002885
James Smart2e0fef82007-06-17 19:56:36 -05002886 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002887 return;
dea31012005-04-17 16:05:31 -05002888
James Smart618a5232012-06-12 13:54:36 -04002889 lpfc_block_mgmt_io(phba, mbx_action);
dea31012005-04-17 16:05:31 -05002890
2891 lpfc_linkdown(phba);
2892
James Smart87af33f2007-10-27 13:37:43 -04002893 /* Issue an unreg_login to all nodes on all vports */
2894 vports = lpfc_create_vport_work_array(phba);
2895 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04002896 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04002897 if (vports[i]->load_flag & FC_UNLOADING)
2898 continue;
James Smart72100cc2010-02-12 14:43:01 -05002899 shost = lpfc_shost_from_vport(vports[i]);
2900 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002901 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002902 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2903 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05002904 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002905
James Smart87af33f2007-10-27 13:37:43 -04002906 shost = lpfc_shost_from_vport(vports[i]);
2907 list_for_each_entry_safe(ndlp, next_ndlp,
2908 &vports[i]->fc_nodes,
2909 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002910 if (!NLP_CHK_NODE_ACT(ndlp))
2911 continue;
James Smart87af33f2007-10-27 13:37:43 -04002912 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2913 continue;
2914 if (ndlp->nlp_type & NLP_FABRIC) {
2915 lpfc_disc_state_machine(vports[i], ndlp,
2916 NULL, NLP_EVT_DEVICE_RECOVERY);
2917 lpfc_disc_state_machine(vports[i], ndlp,
2918 NULL, NLP_EVT_DEVICE_RM);
2919 }
2920 spin_lock_irq(shost->host_lock);
2921 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart401ee0c2012-03-01 22:35:34 -05002922 spin_unlock_irq(shost->host_lock);
James Smart6b5151f2012-01-18 16:24:06 -05002923 /*
2924 * Whenever an SLI4 port goes offline, free the
James Smart401ee0c2012-03-01 22:35:34 -05002925 * RPI. Get a new RPI when the adapter port
2926 * comes back online.
James Smart6b5151f2012-01-18 16:24:06 -05002927 */
2928 if (phba->sli_rev == LPFC_SLI_REV4)
2929 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smart87af33f2007-10-27 13:37:43 -04002930 lpfc_unreg_rpi(vports[i], ndlp);
2931 }
2932 }
2933 }
James Smart09372822008-01-11 01:52:54 -05002934 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002935
James Smart618a5232012-06-12 13:54:36 -04002936 lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
James Smart46fa3112007-04-25 09:51:45 -04002937}
2938
James Smarte59058c2008-08-24 21:49:00 -04002939/**
James Smart3621a712009-04-06 18:47:14 -04002940 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04002941 * @phba: pointer to lpfc hba data structure.
2942 *
2943 * This routine actually brings a HBA offline. It stops all the timers
2944 * associated with the HBA, brings down the SLI layer, and eventually
2945 * marks the HBA as in offline state for the upper layer protocol.
2946 **/
James Smart46fa3112007-04-25 09:51:45 -04002947void
James Smart2e0fef82007-06-17 19:56:36 -05002948lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04002949{
James Smart549e55c2007-08-02 11:09:51 -04002950 struct Scsi_Host *shost;
2951 struct lpfc_vport **vports;
2952 int i;
James Smart46fa3112007-04-25 09:51:45 -04002953
James Smart549e55c2007-08-02 11:09:51 -04002954 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002955 return;
James Smart688a8862006-07-06 15:49:56 -04002956
James Smartda0436e2009-05-22 14:51:39 -04002957 /* stop port and all timers associated with this hba */
2958 lpfc_stop_port(phba);
James Smart51ef4c22007-08-02 11:10:31 -04002959 vports = lpfc_create_vport_work_array(phba);
2960 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002961 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04002962 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05002963 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05002964 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002965 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05002966 /* Bring down the SLI Layer and cleanup. The HBA is offline
2967 now. */
2968 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002969 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04002970 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002971 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04002972 vports = lpfc_create_vport_work_array(phba);
2973 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002974 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002975 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04002976 spin_lock_irq(shost->host_lock);
2977 vports[i]->work_port_events = 0;
2978 vports[i]->fc_flag |= FC_OFFLINE_MODE;
2979 spin_unlock_irq(shost->host_lock);
2980 }
James Smart09372822008-01-11 01:52:54 -05002981 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002982}
2983
James Smarte59058c2008-08-24 21:49:00 -04002984/**
James Smart3621a712009-04-06 18:47:14 -04002985 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04002986 * @phba: pointer to lpfc hba data structure.
2987 *
2988 * This routine is to free all the SCSI buffers and IOCBs from the driver
2989 * list back to kernel. It is called from lpfc_pci_remove_one to free
2990 * the internal resources before the device is removed from the system.
James Smarte59058c2008-08-24 21:49:00 -04002991 **/
James Smart8a9d2e82012-05-09 21:16:12 -04002992static void
James Smart2e0fef82007-06-17 19:56:36 -05002993lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002994{
2995 struct lpfc_scsi_buf *sb, *sb_next;
2996 struct lpfc_iocbq *io, *io_next;
2997
James Smart2e0fef82007-06-17 19:56:36 -05002998 spin_lock_irq(&phba->hbalock);
James Smarta40fc5f2013-04-17 20:17:40 -04002999
dea31012005-04-17 16:05:31 -05003000 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smarta40fc5f2013-04-17 20:17:40 -04003001
3002 spin_lock(&phba->scsi_buf_list_put_lock);
3003 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_put,
3004 list) {
dea31012005-04-17 16:05:31 -05003005 list_del(&sb->list);
3006 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05003007 sb->dma_handle);
dea31012005-04-17 16:05:31 -05003008 kfree(sb);
3009 phba->total_scsi_bufs--;
3010 }
James Smarta40fc5f2013-04-17 20:17:40 -04003011 spin_unlock(&phba->scsi_buf_list_put_lock);
3012
3013 spin_lock(&phba->scsi_buf_list_get_lock);
3014 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_get,
3015 list) {
3016 list_del(&sb->list);
3017 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
3018 sb->dma_handle);
3019 kfree(sb);
3020 phba->total_scsi_bufs--;
3021 }
3022 spin_unlock(&phba->scsi_buf_list_get_lock);
dea31012005-04-17 16:05:31 -05003023
3024 /* Release all the lpfc_iocbq entries maintained by this host. */
3025 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
3026 list_del(&io->list);
3027 kfree(io);
3028 phba->total_iocbq_bufs--;
3029 }
James Smart6d368e52011-05-24 11:44:12 -04003030
James Smart2e0fef82007-06-17 19:56:36 -05003031 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003032}
3033
3034/**
3035 * lpfc_sli4_xri_sgl_update - update xri-sgl sizing and mapping
3036 * @phba: pointer to lpfc hba data structure.
3037 *
3038 * This routine first calculates the sizes of the current els and allocated
3039 * scsi sgl lists, and then goes through all sgls to updates the physical
3040 * XRIs assigned due to port function reset. During port initialization, the
3041 * current els and allocated scsi sgl lists are 0s.
3042 *
3043 * Return codes
3044 * 0 - successful (for now, it always returns 0)
3045 **/
3046int
3047lpfc_sli4_xri_sgl_update(struct lpfc_hba *phba)
3048{
3049 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
3050 struct lpfc_scsi_buf *psb = NULL, *psb_next = NULL;
3051 uint16_t i, lxri, xri_cnt, els_xri_cnt, scsi_xri_cnt;
3052 LIST_HEAD(els_sgl_list);
3053 LIST_HEAD(scsi_sgl_list);
3054 int rc;
James Smartdafe8ce2014-09-03 12:56:40 -04003055 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart8a9d2e82012-05-09 21:16:12 -04003056
3057 /*
3058 * update on pci function's els xri-sgl list
3059 */
3060 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3061 if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
3062 /* els xri-sgl expanded */
3063 xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
3064 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3065 "3157 ELS xri-sgl count increased from "
3066 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3067 els_xri_cnt);
3068 /* allocate the additional els sgls */
3069 for (i = 0; i < xri_cnt; i++) {
3070 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3071 GFP_KERNEL);
3072 if (sglq_entry == NULL) {
3073 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3074 "2562 Failure to allocate an "
3075 "ELS sgl entry:%d\n", i);
3076 rc = -ENOMEM;
3077 goto out_free_mem;
3078 }
3079 sglq_entry->buff_type = GEN_BUFF_TYPE;
3080 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
3081 &sglq_entry->phys);
3082 if (sglq_entry->virt == NULL) {
3083 kfree(sglq_entry);
3084 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3085 "2563 Failure to allocate an "
3086 "ELS mbuf:%d\n", i);
3087 rc = -ENOMEM;
3088 goto out_free_mem;
3089 }
3090 sglq_entry->sgl = sglq_entry->virt;
3091 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
3092 sglq_entry->state = SGL_FREED;
3093 list_add_tail(&sglq_entry->list, &els_sgl_list);
3094 }
James Smart38c20672013-03-01 16:37:44 -05003095 spin_lock_irq(&phba->hbalock);
James Smartdafe8ce2014-09-03 12:56:40 -04003096 spin_lock(&pring->ring_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003097 list_splice_init(&els_sgl_list, &phba->sli4_hba.lpfc_sgl_list);
James Smartdafe8ce2014-09-03 12:56:40 -04003098 spin_unlock(&pring->ring_lock);
James Smart38c20672013-03-01 16:37:44 -05003099 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003100 } else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
3101 /* els xri-sgl shrinked */
3102 xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
3103 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3104 "3158 ELS xri-sgl count decreased from "
3105 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3106 els_xri_cnt);
3107 spin_lock_irq(&phba->hbalock);
James Smartdafe8ce2014-09-03 12:56:40 -04003108 spin_lock(&pring->ring_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003109 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &els_sgl_list);
James Smartdafe8ce2014-09-03 12:56:40 -04003110 spin_unlock(&pring->ring_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003111 spin_unlock_irq(&phba->hbalock);
3112 /* release extra els sgls from list */
3113 for (i = 0; i < xri_cnt; i++) {
3114 list_remove_head(&els_sgl_list,
3115 sglq_entry, struct lpfc_sglq, list);
3116 if (sglq_entry) {
3117 lpfc_mbuf_free(phba, sglq_entry->virt,
3118 sglq_entry->phys);
3119 kfree(sglq_entry);
3120 }
3121 }
3122 spin_lock_irq(&phba->hbalock);
James Smartdafe8ce2014-09-03 12:56:40 -04003123 spin_lock(&pring->ring_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003124 list_splice_init(&els_sgl_list, &phba->sli4_hba.lpfc_sgl_list);
James Smartdafe8ce2014-09-03 12:56:40 -04003125 spin_unlock(&pring->ring_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003126 spin_unlock_irq(&phba->hbalock);
3127 } else
3128 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3129 "3163 ELS xri-sgl count unchanged: %d\n",
3130 els_xri_cnt);
3131 phba->sli4_hba.els_xri_cnt = els_xri_cnt;
3132
3133 /* update xris to els sgls on the list */
3134 sglq_entry = NULL;
3135 sglq_entry_next = NULL;
3136 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
3137 &phba->sli4_hba.lpfc_sgl_list, list) {
3138 lxri = lpfc_sli4_next_xritag(phba);
3139 if (lxri == NO_XRI) {
3140 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3141 "2400 Failed to allocate xri for "
3142 "ELS sgl\n");
3143 rc = -ENOMEM;
3144 goto out_free_mem;
3145 }
3146 sglq_entry->sli4_lxritag = lxri;
3147 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3148 }
3149
3150 /*
3151 * update on pci function's allocated scsi xri-sgl list
3152 */
3153 phba->total_scsi_bufs = 0;
3154
3155 /* maximum number of xris available for scsi buffers */
3156 phba->sli4_hba.scsi_xri_max = phba->sli4_hba.max_cfg_param.max_xri -
3157 els_xri_cnt;
3158
3159 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3160 "2401 Current allocated SCSI xri-sgl count:%d, "
3161 "maximum SCSI xri count:%d\n",
3162 phba->sli4_hba.scsi_xri_cnt,
3163 phba->sli4_hba.scsi_xri_max);
3164
James Smarta40fc5f2013-04-17 20:17:40 -04003165 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart164cecd2013-09-06 12:22:38 -04003166 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003167 list_splice_init(&phba->lpfc_scsi_buf_list_get, &scsi_sgl_list);
3168 list_splice(&phba->lpfc_scsi_buf_list_put, &scsi_sgl_list);
James Smart164cecd2013-09-06 12:22:38 -04003169 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003170 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003171
3172 if (phba->sli4_hba.scsi_xri_cnt > phba->sli4_hba.scsi_xri_max) {
3173 /* max scsi xri shrinked below the allocated scsi buffers */
3174 scsi_xri_cnt = phba->sli4_hba.scsi_xri_cnt -
3175 phba->sli4_hba.scsi_xri_max;
3176 /* release the extra allocated scsi buffers */
3177 for (i = 0; i < scsi_xri_cnt; i++) {
3178 list_remove_head(&scsi_sgl_list, psb,
3179 struct lpfc_scsi_buf, list);
3180 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, psb->data,
3181 psb->dma_handle);
3182 kfree(psb);
3183 }
James Smarta40fc5f2013-04-17 20:17:40 -04003184 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003185 phba->sli4_hba.scsi_xri_cnt -= scsi_xri_cnt;
James Smarta40fc5f2013-04-17 20:17:40 -04003186 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003187 }
3188
3189 /* update xris associated to remaining allocated scsi buffers */
3190 psb = NULL;
3191 psb_next = NULL;
3192 list_for_each_entry_safe(psb, psb_next, &scsi_sgl_list, list) {
3193 lxri = lpfc_sli4_next_xritag(phba);
3194 if (lxri == NO_XRI) {
3195 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3196 "2560 Failed to allocate xri for "
3197 "scsi buffer\n");
3198 rc = -ENOMEM;
3199 goto out_free_mem;
3200 }
3201 psb->cur_iocbq.sli4_lxritag = lxri;
3202 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3203 }
James Smarta40fc5f2013-04-17 20:17:40 -04003204 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart164cecd2013-09-06 12:22:38 -04003205 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003206 list_splice_init(&scsi_sgl_list, &phba->lpfc_scsi_buf_list_get);
3207 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
James Smart164cecd2013-09-06 12:22:38 -04003208 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003209 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003210
dea31012005-04-17 16:05:31 -05003211 return 0;
James Smart8a9d2e82012-05-09 21:16:12 -04003212
3213out_free_mem:
3214 lpfc_free_els_sgl_list(phba);
3215 lpfc_scsi_free(phba);
3216 return rc;
dea31012005-04-17 16:05:31 -05003217}
3218
James Smarte59058c2008-08-24 21:49:00 -04003219/**
James Smart3621a712009-04-06 18:47:14 -04003220 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04003221 * @phba: pointer to lpfc hba data structure.
3222 * @instance: a unique integer ID to this FC port.
3223 * @dev: pointer to the device data structure.
3224 *
3225 * This routine creates a FC port for the upper layer protocol. The FC port
3226 * can be created on top of either a physical port or a virtual port provided
3227 * by the HBA. This routine also allocates a SCSI host data structure (shost)
3228 * and associates the FC port created before adding the shost into the SCSI
3229 * layer.
3230 *
3231 * Return codes
3232 * @vport - pointer to the virtual N_Port data structure.
3233 * NULL - port create failed.
3234 **/
James Smart2e0fef82007-06-17 19:56:36 -05003235struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04003236lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04003237{
James Smart2e0fef82007-06-17 19:56:36 -05003238 struct lpfc_vport *vport;
3239 struct Scsi_Host *shost;
3240 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04003241
James Smart3de2a652007-08-02 11:09:59 -04003242 if (dev != &phba->pcidev->dev)
3243 shost = scsi_host_alloc(&lpfc_vport_template,
3244 sizeof(struct lpfc_vport));
3245 else
3246 shost = scsi_host_alloc(&lpfc_template,
3247 sizeof(struct lpfc_vport));
James Smart2e0fef82007-06-17 19:56:36 -05003248 if (!shost)
3249 goto out;
James Smart47a86172007-04-25 09:53:22 -04003250
James Smart2e0fef82007-06-17 19:56:36 -05003251 vport = (struct lpfc_vport *) shost->hostdata;
3252 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05003253 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05003254 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05003255 vport->fc_rscn_flush = 0;
James Smart47a86172007-04-25 09:53:22 -04003256
James Smart3de2a652007-08-02 11:09:59 -04003257 lpfc_get_vport_cfgparam(vport);
James Smart2e0fef82007-06-17 19:56:36 -05003258 shost->unique_id = instance;
3259 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04003260 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05003261 shost->this_id = -1;
3262 shost->max_cmd_len = 16;
James Smartda0436e2009-05-22 14:51:39 -04003263 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart28baac72010-02-12 14:42:03 -05003264 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04003265 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smartda0436e2009-05-22 14:51:39 -04003266 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
3267 }
James Smart81301a92008-12-04 22:39:46 -05003268
James Smart47a86172007-04-25 09:53:22 -04003269 /*
James Smart2e0fef82007-06-17 19:56:36 -05003270 * Set initial can_queue value since 0 is no longer supported and
3271 * scsi_add_host will fail. This will be adjusted later based on the
3272 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04003273 */
James Smart2e0fef82007-06-17 19:56:36 -05003274 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04003275 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05003276 shost->transportt = lpfc_vport_transport_template;
3277 vport->port_type = LPFC_NPIV_PORT;
3278 } else {
3279 shost->transportt = lpfc_transport_template;
3280 vport->port_type = LPFC_PHYSICAL_PORT;
3281 }
James Smart47a86172007-04-25 09:53:22 -04003282
James Smart2e0fef82007-06-17 19:56:36 -05003283 /* Initialize all internally managed lists. */
3284 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04003285 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05003286 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04003287
James Smart2e0fef82007-06-17 19:56:36 -05003288 init_timer(&vport->fc_disctmo);
3289 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05003290 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04003291
James Smart2e0fef82007-06-17 19:56:36 -05003292 init_timer(&vport->fc_fdmitmo);
3293 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
James Smart92d7f7b2007-06-17 19:56:38 -05003294 vport->fc_fdmitmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04003295
James Smart2e0fef82007-06-17 19:56:36 -05003296 init_timer(&vport->els_tmofunc);
3297 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05003298 vport->els_tmofunc.data = (unsigned long)vport;
James Smart92494142011-02-16 12:39:44 -05003299
3300 init_timer(&vport->delayed_disc_tmo);
3301 vport->delayed_disc_tmo.function = lpfc_delayed_disc_tmo;
3302 vport->delayed_disc_tmo.data = (unsigned long)vport;
3303
James Bottomleyd139b9b2009-11-05 13:33:12 -06003304 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05003305 if (error)
3306 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04003307
James Smart549e55c2007-08-02 11:09:51 -04003308 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003309 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04003310 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003311 return vport;
James Smart47a86172007-04-25 09:53:22 -04003312
James Smart2e0fef82007-06-17 19:56:36 -05003313out_put_shost:
3314 scsi_host_put(shost);
3315out:
3316 return NULL;
James Smart47a86172007-04-25 09:53:22 -04003317}
3318
James Smarte59058c2008-08-24 21:49:00 -04003319/**
James Smart3621a712009-04-06 18:47:14 -04003320 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04003321 * @vport: pointer to an lpfc virtual N_Port data structure.
3322 *
3323 * This routine destroys a FC port from the upper layer protocol. All the
3324 * resources associated with the port are released.
3325 **/
James Smart2e0fef82007-06-17 19:56:36 -05003326void
3327destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04003328{
James Smart92d7f7b2007-06-17 19:56:38 -05003329 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3330 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003331
James Smart858c9f62007-06-17 19:56:39 -05003332 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05003333 fc_remove_host(shost);
3334 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04003335
James Smart92d7f7b2007-06-17 19:56:38 -05003336 spin_lock_irq(&phba->hbalock);
3337 list_del_init(&vport->listentry);
3338 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04003339
James Smart92d7f7b2007-06-17 19:56:38 -05003340 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04003341 return;
James Smart47a86172007-04-25 09:53:22 -04003342}
3343
James Smarte59058c2008-08-24 21:49:00 -04003344/**
James Smart3621a712009-04-06 18:47:14 -04003345 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04003346 *
3347 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
3348 * uses the kernel idr facility to perform the task.
3349 *
3350 * Return codes:
3351 * instance - a unique integer ID allocated as the new instance.
3352 * -1 - lpfc get instance failed.
3353 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003354int
3355lpfc_get_instance(void)
3356{
Tejun Heoab516032013-02-27 17:04:44 -08003357 int ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003358
Tejun Heoab516032013-02-27 17:04:44 -08003359 ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
3360 return ret < 0 ? -1 : ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003361}
3362
James Smarte59058c2008-08-24 21:49:00 -04003363/**
James Smart3621a712009-04-06 18:47:14 -04003364 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04003365 * @shost: pointer to SCSI host data structure.
3366 * @time: elapsed time of the scan in jiffies.
3367 *
3368 * This routine is called by the SCSI layer with a SCSI host to determine
3369 * whether the scan host is finished.
3370 *
3371 * Note: there is no scan_start function as adapter initialization will have
3372 * asynchronously kicked off the link initialization.
3373 *
3374 * Return codes
3375 * 0 - SCSI host scan is not over yet.
3376 * 1 - SCSI host scan is over.
3377 **/
James Smart47a86172007-04-25 09:53:22 -04003378int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
3379{
James Smart2e0fef82007-06-17 19:56:36 -05003380 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3381 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05003382 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04003383
James Smart858c9f62007-06-17 19:56:39 -05003384 spin_lock_irq(shost->host_lock);
3385
James Smart51ef4c22007-08-02 11:10:31 -04003386 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05003387 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003388 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05003389 }
James Smart256ec0d2013-04-17 20:14:58 -04003390 if (time >= msecs_to_jiffies(30 * 1000)) {
James Smart2e0fef82007-06-17 19:56:36 -05003391 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003392 "0461 Scanning longer than 30 "
3393 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003394 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003395 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003396 }
James Smart256ec0d2013-04-17 20:14:58 -04003397 if (time >= msecs_to_jiffies(15 * 1000) &&
3398 phba->link_state <= LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -05003399 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003400 "0465 Link down longer than 15 "
3401 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003402 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05003403 goto finished;
James Smart47a86172007-04-25 09:53:22 -04003404 }
3405
James Smart2e0fef82007-06-17 19:56:36 -05003406 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05003407 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003408 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05003409 goto finished;
James Smart256ec0d2013-04-17 20:14:58 -04003410 if (vport->fc_map_cnt == 0 && time < msecs_to_jiffies(2 * 1000))
James Smart858c9f62007-06-17 19:56:39 -05003411 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003412 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05003413 goto finished;
3414
3415 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003416
3417finished:
James Smart858c9f62007-06-17 19:56:39 -05003418 spin_unlock_irq(shost->host_lock);
3419 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05003420}
3421
James Smarte59058c2008-08-24 21:49:00 -04003422/**
James Smart3621a712009-04-06 18:47:14 -04003423 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04003424 * @shost: pointer to SCSI host data structure.
3425 *
3426 * This routine initializes a given SCSI host attributes on a FC port. The
3427 * SCSI host can be either on top of a physical port or a virtual port.
3428 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003429void lpfc_host_attrib_init(struct Scsi_Host *shost)
3430{
3431 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3432 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003433 /*
James Smart2e0fef82007-06-17 19:56:36 -05003434 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04003435 */
3436
James Smart2e0fef82007-06-17 19:56:36 -05003437 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
3438 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04003439 fc_host_supported_classes(shost) = FC_COS_CLASS3;
3440
3441 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003442 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003443 fc_host_supported_fc4s(shost)[2] = 1;
3444 fc_host_supported_fc4s(shost)[7] = 1;
3445
James Smart92d7f7b2007-06-17 19:56:38 -05003446 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
3447 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04003448
3449 fc_host_supported_speeds(shost) = 0;
James Smart88a2cfb2011-07-22 18:36:33 -04003450 if (phba->lmt & LMT_16Gb)
3451 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
James Smart47a86172007-04-25 09:53:22 -04003452 if (phba->lmt & LMT_10Gb)
3453 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04003454 if (phba->lmt & LMT_8Gb)
3455 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04003456 if (phba->lmt & LMT_4Gb)
3457 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
3458 if (phba->lmt & LMT_2Gb)
3459 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
3460 if (phba->lmt & LMT_1Gb)
3461 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
3462
3463 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05003464 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
3465 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04003466
Mike Christie0af5d702010-09-15 16:52:31 -05003467 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
3468
James Smart47a86172007-04-25 09:53:22 -04003469 /* This value is also unchanging */
3470 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003471 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003472 fc_host_active_fc4s(shost)[2] = 1;
3473 fc_host_active_fc4s(shost)[7] = 1;
3474
James Smart92d7f7b2007-06-17 19:56:38 -05003475 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04003476 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04003477 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04003478 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04003479}
dea31012005-04-17 16:05:31 -05003480
James Smarte59058c2008-08-24 21:49:00 -04003481/**
James Smartda0436e2009-05-22 14:51:39 -04003482 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04003483 * @phba: pointer to lpfc hba data structure.
3484 *
James Smartda0436e2009-05-22 14:51:39 -04003485 * This routine is invoked to stop an SLI3 device port, it stops the device
3486 * from generating interrupts and stops the device driver's timers for the
3487 * device.
James Smarte59058c2008-08-24 21:49:00 -04003488 **/
James Smartdb2378e2008-02-08 18:49:51 -05003489static void
James Smartda0436e2009-05-22 14:51:39 -04003490lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05003491{
James Smartda0436e2009-05-22 14:51:39 -04003492 /* Clear all interrupt enable conditions */
3493 writel(0, phba->HCregaddr);
3494 readl(phba->HCregaddr); /* flush */
3495 /* Clear all pending interrupts */
3496 writel(0xffffffff, phba->HAregaddr);
3497 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04003498
James Smartda0436e2009-05-22 14:51:39 -04003499 /* Reset some HBA SLI setup states */
3500 lpfc_stop_hba_timers(phba);
3501 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05003502}
3503
James Smarte59058c2008-08-24 21:49:00 -04003504/**
James Smartda0436e2009-05-22 14:51:39 -04003505 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05003506 * @phba: pointer to lpfc hba data structure.
3507 *
James Smartda0436e2009-05-22 14:51:39 -04003508 * This routine is invoked to stop an SLI4 device port, it stops the device
3509 * from generating interrupts and stops the device driver's timers for the
3510 * device.
3511 **/
3512static void
3513lpfc_stop_port_s4(struct lpfc_hba *phba)
3514{
3515 /* Reset some HBA SLI4 setup states */
3516 lpfc_stop_hba_timers(phba);
3517 phba->pport->work_port_events = 0;
3518 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04003519}
3520
3521/**
3522 * lpfc_stop_port - Wrapper function for stopping hba port
3523 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05003524 *
James Smartda0436e2009-05-22 14:51:39 -04003525 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
3526 * the API jump table function pointer from the lpfc_hba struct.
3527 **/
3528void
3529lpfc_stop_port(struct lpfc_hba *phba)
3530{
3531 phba->lpfc_stop_port(phba);
3532}
3533
3534/**
James Smartecfd03c2010-02-12 14:41:27 -05003535 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
3536 * @phba: Pointer to hba for which this call is being executed.
3537 *
3538 * This routine starts the timer waiting for the FCF rediscovery to complete.
3539 **/
3540void
3541lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
3542{
3543 unsigned long fcf_redisc_wait_tmo =
3544 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
3545 /* Start fcf rediscovery wait period timer */
3546 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
3547 spin_lock_irq(&phba->hbalock);
3548 /* Allow action to new fcf asynchronous event */
3549 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
3550 /* Mark the FCF rediscovery pending state */
3551 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
3552 spin_unlock_irq(&phba->hbalock);
3553}
3554
3555/**
3556 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
3557 * @ptr: Map to lpfc_hba data structure pointer.
3558 *
3559 * This routine is invoked when waiting for FCF table rediscover has been
3560 * timed out. If new FCF record(s) has (have) been discovered during the
3561 * wait period, a new FCF event shall be added to the FCOE async event
3562 * list, and then worker thread shall be waked up for processing from the
3563 * worker thread context.
3564 **/
Rashika Kheriae399b222014-09-03 12:55:28 -04003565static void
James Smartecfd03c2010-02-12 14:41:27 -05003566lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
3567{
3568 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
3569
3570 /* Don't send FCF rediscovery event if timer cancelled */
3571 spin_lock_irq(&phba->hbalock);
3572 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
3573 spin_unlock_irq(&phba->hbalock);
3574 return;
3575 }
3576 /* Clear FCF rediscovery timer pending flag */
3577 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
3578 /* FCF rediscovery event to worker thread */
3579 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
3580 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003581 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04003582 "2776 FCF rediscover quiescent timer expired\n");
James Smartecfd03c2010-02-12 14:41:27 -05003583 /* wake up worker thread */
3584 lpfc_worker_wake_up(phba);
3585}
3586
3587/**
James Smartda0436e2009-05-22 14:51:39 -04003588 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
3589 * @phba: pointer to lpfc hba data structure.
3590 * @acqe_link: pointer to the async link completion queue entry.
3591 *
3592 * This routine is to parse the SLI4 link-attention link fault code and
3593 * translate it into the base driver's read link attention mailbox command
3594 * status.
3595 *
3596 * Return: Link-attention status in terms of base driver's coding.
3597 **/
3598static uint16_t
3599lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
3600 struct lpfc_acqe_link *acqe_link)
3601{
3602 uint16_t latt_fault;
3603
3604 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
3605 case LPFC_ASYNC_LINK_FAULT_NONE:
3606 case LPFC_ASYNC_LINK_FAULT_LOCAL:
3607 case LPFC_ASYNC_LINK_FAULT_REMOTE:
3608 latt_fault = 0;
3609 break;
3610 default:
3611 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3612 "0398 Invalid link fault code: x%x\n",
3613 bf_get(lpfc_acqe_link_fault, acqe_link));
3614 latt_fault = MBXERR_ERROR;
3615 break;
3616 }
3617 return latt_fault;
3618}
3619
3620/**
3621 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
3622 * @phba: pointer to lpfc hba data structure.
3623 * @acqe_link: pointer to the async link completion queue entry.
3624 *
3625 * This routine is to parse the SLI4 link attention type and translate it
3626 * into the base driver's link attention type coding.
3627 *
3628 * Return: Link attention type in terms of base driver's coding.
3629 **/
3630static uint8_t
3631lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
3632 struct lpfc_acqe_link *acqe_link)
3633{
3634 uint8_t att_type;
3635
3636 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
3637 case LPFC_ASYNC_LINK_STATUS_DOWN:
3638 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
James Smart76a95d72010-11-20 23:11:48 -05003639 att_type = LPFC_ATT_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04003640 break;
3641 case LPFC_ASYNC_LINK_STATUS_UP:
3642 /* Ignore physical link up events - wait for logical link up */
James Smart76a95d72010-11-20 23:11:48 -05003643 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003644 break;
3645 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
James Smart76a95d72010-11-20 23:11:48 -05003646 att_type = LPFC_ATT_LINK_UP;
James Smartda0436e2009-05-22 14:51:39 -04003647 break;
3648 default:
3649 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3650 "0399 Invalid link attention type: x%x\n",
3651 bf_get(lpfc_acqe_link_status, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003652 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003653 break;
3654 }
3655 return att_type;
3656}
3657
3658/**
3659 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
3660 * @phba: pointer to lpfc hba data structure.
3661 * @acqe_link: pointer to the async link completion queue entry.
3662 *
3663 * This routine is to parse the SLI4 link-attention link speed and translate
3664 * it into the base driver's link-attention link speed coding.
3665 *
3666 * Return: Link-attention link speed in terms of base driver's coding.
3667 **/
3668static uint8_t
3669lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
3670 struct lpfc_acqe_link *acqe_link)
3671{
3672 uint8_t link_speed;
3673
3674 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
3675 case LPFC_ASYNC_LINK_SPEED_ZERO:
James Smartda0436e2009-05-22 14:51:39 -04003676 case LPFC_ASYNC_LINK_SPEED_10MBPS:
James Smartda0436e2009-05-22 14:51:39 -04003677 case LPFC_ASYNC_LINK_SPEED_100MBPS:
James Smart76a95d72010-11-20 23:11:48 -05003678 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003679 break;
3680 case LPFC_ASYNC_LINK_SPEED_1GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003681 link_speed = LPFC_LINK_SPEED_1GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003682 break;
3683 case LPFC_ASYNC_LINK_SPEED_10GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003684 link_speed = LPFC_LINK_SPEED_10GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003685 break;
3686 default:
3687 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3688 "0483 Invalid link-attention link speed: x%x\n",
3689 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003690 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003691 break;
3692 }
3693 return link_speed;
3694}
3695
3696/**
James Smart8b68cd52012-09-29 11:32:37 -04003697 * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
3698 * @phba: pointer to lpfc hba data structure.
3699 *
3700 * This routine is to get an SLI3 FC port's link speed in Mbps.
3701 *
3702 * Return: link speed in terms of Mbps.
3703 **/
3704uint32_t
3705lpfc_sli_port_speed_get(struct lpfc_hba *phba)
3706{
3707 uint32_t link_speed;
3708
3709 if (!lpfc_is_link_up(phba))
3710 return 0;
3711
3712 switch (phba->fc_linkspeed) {
3713 case LPFC_LINK_SPEED_1GHZ:
3714 link_speed = 1000;
3715 break;
3716 case LPFC_LINK_SPEED_2GHZ:
3717 link_speed = 2000;
3718 break;
3719 case LPFC_LINK_SPEED_4GHZ:
3720 link_speed = 4000;
3721 break;
3722 case LPFC_LINK_SPEED_8GHZ:
3723 link_speed = 8000;
3724 break;
3725 case LPFC_LINK_SPEED_10GHZ:
3726 link_speed = 10000;
3727 break;
3728 case LPFC_LINK_SPEED_16GHZ:
3729 link_speed = 16000;
3730 break;
3731 default:
3732 link_speed = 0;
3733 }
3734 return link_speed;
3735}
3736
3737/**
3738 * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
3739 * @phba: pointer to lpfc hba data structure.
3740 * @evt_code: asynchronous event code.
3741 * @speed_code: asynchronous event link speed code.
3742 *
3743 * This routine is to parse the giving SLI4 async event link speed code into
3744 * value of Mbps for the link speed.
3745 *
3746 * Return: link speed in terms of Mbps.
3747 **/
3748static uint32_t
3749lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
3750 uint8_t speed_code)
3751{
3752 uint32_t port_speed;
3753
3754 switch (evt_code) {
3755 case LPFC_TRAILER_CODE_LINK:
3756 switch (speed_code) {
3757 case LPFC_EVT_CODE_LINK_NO_LINK:
3758 port_speed = 0;
3759 break;
3760 case LPFC_EVT_CODE_LINK_10_MBIT:
3761 port_speed = 10;
3762 break;
3763 case LPFC_EVT_CODE_LINK_100_MBIT:
3764 port_speed = 100;
3765 break;
3766 case LPFC_EVT_CODE_LINK_1_GBIT:
3767 port_speed = 1000;
3768 break;
3769 case LPFC_EVT_CODE_LINK_10_GBIT:
3770 port_speed = 10000;
3771 break;
3772 default:
3773 port_speed = 0;
3774 }
3775 break;
3776 case LPFC_TRAILER_CODE_FC:
3777 switch (speed_code) {
3778 case LPFC_EVT_CODE_FC_NO_LINK:
3779 port_speed = 0;
3780 break;
3781 case LPFC_EVT_CODE_FC_1_GBAUD:
3782 port_speed = 1000;
3783 break;
3784 case LPFC_EVT_CODE_FC_2_GBAUD:
3785 port_speed = 2000;
3786 break;
3787 case LPFC_EVT_CODE_FC_4_GBAUD:
3788 port_speed = 4000;
3789 break;
3790 case LPFC_EVT_CODE_FC_8_GBAUD:
3791 port_speed = 8000;
3792 break;
3793 case LPFC_EVT_CODE_FC_10_GBAUD:
3794 port_speed = 10000;
3795 break;
3796 case LPFC_EVT_CODE_FC_16_GBAUD:
3797 port_speed = 16000;
3798 break;
3799 default:
3800 port_speed = 0;
3801 }
3802 break;
3803 default:
3804 port_speed = 0;
3805 }
3806 return port_speed;
3807}
3808
3809/**
James Smart70f3c072010-12-15 17:57:33 -05003810 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
James Smartda0436e2009-05-22 14:51:39 -04003811 * @phba: pointer to lpfc hba data structure.
3812 * @acqe_link: pointer to the async link completion queue entry.
3813 *
James Smart70f3c072010-12-15 17:57:33 -05003814 * This routine is to handle the SLI4 asynchronous FCoE link event.
James Smartda0436e2009-05-22 14:51:39 -04003815 **/
3816static void
3817lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3818 struct lpfc_acqe_link *acqe_link)
3819{
3820 struct lpfc_dmabuf *mp;
3821 LPFC_MBOXQ_t *pmb;
3822 MAILBOX_t *mb;
James Smart76a95d72010-11-20 23:11:48 -05003823 struct lpfc_mbx_read_top *la;
James Smartda0436e2009-05-22 14:51:39 -04003824 uint8_t att_type;
James Smart76a95d72010-11-20 23:11:48 -05003825 int rc;
James Smartda0436e2009-05-22 14:51:39 -04003826
3827 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05003828 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
James Smartda0436e2009-05-22 14:51:39 -04003829 return;
James Smart32b97932009-07-19 10:01:21 -04003830 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003831 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3832 if (!pmb) {
3833 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3834 "0395 The mboxq allocation failed\n");
3835 return;
3836 }
3837 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3838 if (!mp) {
3839 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3840 "0396 The lpfc_dmabuf allocation failed\n");
3841 goto out_free_pmb;
3842 }
3843 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3844 if (!mp->virt) {
3845 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3846 "0397 The mbuf allocation failed\n");
3847 goto out_free_dmabuf;
3848 }
3849
3850 /* Cleanup any outstanding ELS commands */
3851 lpfc_els_flush_all_cmd(phba);
3852
3853 /* Block ELS IOCBs until we have done process link event */
3854 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3855
3856 /* Update link event statistics */
3857 phba->sli.slistat.link_event++;
3858
James Smart76a95d72010-11-20 23:11:48 -05003859 /* Create lpfc_handle_latt mailbox command from link ACQE */
3860 lpfc_read_topology(phba, pmb, mp);
3861 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smartda0436e2009-05-22 14:51:39 -04003862 pmb->vport = phba->pport;
3863
James Smartda0436e2009-05-22 14:51:39 -04003864 /* Keep the link status for extra SLI4 state machine reference */
3865 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04003866 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
3867 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smartda0436e2009-05-22 14:51:39 -04003868 phba->sli4_hba.link_state.duplex =
3869 bf_get(lpfc_acqe_link_duplex, acqe_link);
3870 phba->sli4_hba.link_state.status =
3871 bf_get(lpfc_acqe_link_status, acqe_link);
James Smart70f3c072010-12-15 17:57:33 -05003872 phba->sli4_hba.link_state.type =
3873 bf_get(lpfc_acqe_link_type, acqe_link);
3874 phba->sli4_hba.link_state.number =
3875 bf_get(lpfc_acqe_link_number, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04003876 phba->sli4_hba.link_state.fault =
3877 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05003878 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04003879 bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
3880
James Smart70f3c072010-12-15 17:57:33 -05003881 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartc31098c2011-04-16 11:03:33 -04003882 "2900 Async FC/FCoE Link event - Speed:%dGBit "
3883 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
3884 "Logical speed:%dMbps Fault:%d\n",
James Smart70f3c072010-12-15 17:57:33 -05003885 phba->sli4_hba.link_state.speed,
3886 phba->sli4_hba.link_state.topology,
3887 phba->sli4_hba.link_state.status,
3888 phba->sli4_hba.link_state.type,
3889 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04003890 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05003891 phba->sli4_hba.link_state.fault);
James Smart76a95d72010-11-20 23:11:48 -05003892 /*
3893 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
3894 * topology info. Note: Optional for non FC-AL ports.
3895 */
3896 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3897 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3898 if (rc == MBX_NOT_FINISHED)
3899 goto out_free_dmabuf;
3900 return;
3901 }
3902 /*
3903 * For FCoE Mode: fill in all the topology information we need and call
3904 * the READ_TOPOLOGY completion routine to continue without actually
3905 * sending the READ_TOPOLOGY mailbox command to the port.
3906 */
3907 /* Parse and translate status field */
3908 mb = &pmb->u.mb;
3909 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
3910
3911 /* Parse and translate link attention fields */
3912 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
3913 la->eventTag = acqe_link->event_tag;
3914 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
3915 bf_set(lpfc_mbx_read_top_link_spd, la,
3916 lpfc_sli4_parse_latt_link_speed(phba, acqe_link));
3917
3918 /* Fake the the following irrelvant fields */
3919 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
3920 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
3921 bf_set(lpfc_mbx_read_top_il, la, 0);
3922 bf_set(lpfc_mbx_read_top_pb, la, 0);
3923 bf_set(lpfc_mbx_read_top_fa, la, 0);
3924 bf_set(lpfc_mbx_read_top_mm, la, 0);
James Smartda0436e2009-05-22 14:51:39 -04003925
3926 /* Invoke the lpfc_handle_latt mailbox command callback function */
James Smart76a95d72010-11-20 23:11:48 -05003927 lpfc_mbx_cmpl_read_topology(phba, pmb);
James Smartda0436e2009-05-22 14:51:39 -04003928
3929 return;
3930
3931out_free_dmabuf:
3932 kfree(mp);
3933out_free_pmb:
3934 mempool_free(pmb, phba->mbox_mem_pool);
3935}
3936
3937/**
James Smart70f3c072010-12-15 17:57:33 -05003938 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
3939 * @phba: pointer to lpfc hba data structure.
3940 * @acqe_fc: pointer to the async fc completion queue entry.
3941 *
3942 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
3943 * that the event was received and then issue a read_topology mailbox command so
3944 * that the rest of the driver will treat it the same as SLI3.
3945 **/
3946static void
3947lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
3948{
3949 struct lpfc_dmabuf *mp;
3950 LPFC_MBOXQ_t *pmb;
3951 int rc;
3952
3953 if (bf_get(lpfc_trailer_type, acqe_fc) !=
3954 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
3955 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3956 "2895 Non FC link Event detected.(%d)\n",
3957 bf_get(lpfc_trailer_type, acqe_fc));
3958 return;
3959 }
3960 /* Keep the link status for extra SLI4 state machine reference */
3961 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04003962 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
3963 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
James Smart70f3c072010-12-15 17:57:33 -05003964 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
3965 phba->sli4_hba.link_state.topology =
3966 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
3967 phba->sli4_hba.link_state.status =
3968 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
3969 phba->sli4_hba.link_state.type =
3970 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
3971 phba->sli4_hba.link_state.number =
3972 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
3973 phba->sli4_hba.link_state.fault =
3974 bf_get(lpfc_acqe_link_fault, acqe_fc);
3975 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04003976 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
James Smart70f3c072010-12-15 17:57:33 -05003977 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3978 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
3979 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
3980 "%dMbps Fault:%d\n",
3981 phba->sli4_hba.link_state.speed,
3982 phba->sli4_hba.link_state.topology,
3983 phba->sli4_hba.link_state.status,
3984 phba->sli4_hba.link_state.type,
3985 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04003986 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05003987 phba->sli4_hba.link_state.fault);
3988 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3989 if (!pmb) {
3990 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3991 "2897 The mboxq allocation failed\n");
3992 return;
3993 }
3994 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3995 if (!mp) {
3996 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3997 "2898 The lpfc_dmabuf allocation failed\n");
3998 goto out_free_pmb;
3999 }
4000 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
4001 if (!mp->virt) {
4002 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4003 "2899 The mbuf allocation failed\n");
4004 goto out_free_dmabuf;
4005 }
4006
4007 /* Cleanup any outstanding ELS commands */
4008 lpfc_els_flush_all_cmd(phba);
4009
4010 /* Block ELS IOCBs until we have done process link event */
4011 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
4012
4013 /* Update link event statistics */
4014 phba->sli.slistat.link_event++;
4015
4016 /* Create lpfc_handle_latt mailbox command from link ACQE */
4017 lpfc_read_topology(phba, pmb, mp);
4018 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
4019 pmb->vport = phba->pport;
4020
4021 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4022 if (rc == MBX_NOT_FINISHED)
4023 goto out_free_dmabuf;
4024 return;
4025
4026out_free_dmabuf:
4027 kfree(mp);
4028out_free_pmb:
4029 mempool_free(pmb, phba->mbox_mem_pool);
4030}
4031
4032/**
4033 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
4034 * @phba: pointer to lpfc hba data structure.
4035 * @acqe_fc: pointer to the async SLI completion queue entry.
4036 *
4037 * This routine is to handle the SLI4 asynchronous SLI events.
4038 **/
4039static void
4040lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
4041{
James Smart4b8bae02012-06-12 13:55:07 -04004042 char port_name;
James Smart8c1312e2012-10-31 14:45:09 -04004043 char message[128];
James Smart4b8bae02012-06-12 13:55:07 -04004044 uint8_t status;
4045 struct lpfc_acqe_misconfigured_event *misconfigured;
4046
4047 /* special case misconfigured event as it contains data for all ports */
4048 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
4049 LPFC_SLI_INTF_IF_TYPE_2) ||
4050 (bf_get(lpfc_trailer_type, acqe_sli) !=
4051 LPFC_SLI_EVENT_TYPE_MISCONFIGURED)) {
4052 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4053 "2901 Async SLI event - Event Data1:x%08x Event Data2:"
4054 "x%08x SLI Event Type:%d\n",
4055 acqe_sli->event_data1, acqe_sli->event_data2,
4056 bf_get(lpfc_trailer_type, acqe_sli));
4057 return;
4058 }
4059
4060 port_name = phba->Port[0];
4061 if (port_name == 0x00)
4062 port_name = '?'; /* get port name is empty */
4063
4064 misconfigured = (struct lpfc_acqe_misconfigured_event *)
4065 &acqe_sli->event_data1;
4066
4067 /* fetch the status for this port */
4068 switch (phba->sli4_hba.lnk_info.lnk_no) {
4069 case LPFC_LINK_NUMBER_0:
4070 status = bf_get(lpfc_sli_misconfigured_port0,
4071 &misconfigured->theEvent);
4072 break;
4073 case LPFC_LINK_NUMBER_1:
4074 status = bf_get(lpfc_sli_misconfigured_port1,
4075 &misconfigured->theEvent);
4076 break;
4077 case LPFC_LINK_NUMBER_2:
4078 status = bf_get(lpfc_sli_misconfigured_port2,
4079 &misconfigured->theEvent);
4080 break;
4081 case LPFC_LINK_NUMBER_3:
4082 status = bf_get(lpfc_sli_misconfigured_port3,
4083 &misconfigured->theEvent);
4084 break;
4085 default:
4086 status = ~LPFC_SLI_EVENT_STATUS_VALID;
4087 break;
4088 }
4089
4090 switch (status) {
4091 case LPFC_SLI_EVENT_STATUS_VALID:
4092 return; /* no message if the sfp is okay */
4093 case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
James Smart292098b2012-09-29 11:31:41 -04004094 sprintf(message, "Optics faulted/incorrectly installed/not " \
4095 "installed - Reseat optics, if issue not "
4096 "resolved, replace.");
James Smart4b8bae02012-06-12 13:55:07 -04004097 break;
4098 case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
4099 sprintf(message,
James Smart292098b2012-09-29 11:31:41 -04004100 "Optics of two types installed - Remove one optic or " \
4101 "install matching pair of optics.");
James Smart4b8bae02012-06-12 13:55:07 -04004102 break;
4103 case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
James Smart292098b2012-09-29 11:31:41 -04004104 sprintf(message, "Incompatible optics - Replace with " \
4105 "compatible optics for card to function.");
James Smart4b8bae02012-06-12 13:55:07 -04004106 break;
4107 default:
4108 /* firmware is reporting a status we don't know about */
4109 sprintf(message, "Unknown event status x%02x", status);
4110 break;
4111 }
4112
4113 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4114 "3176 Misconfigured Physical Port - "
4115 "Port Name %c %s\n", port_name, message);
James Smart70f3c072010-12-15 17:57:33 -05004116}
4117
4118/**
James Smartfc2b9892010-02-26 14:15:29 -05004119 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
4120 * @vport: pointer to vport data structure.
4121 *
4122 * This routine is to perform Clear Virtual Link (CVL) on a vport in
4123 * response to a CVL event.
4124 *
4125 * Return the pointer to the ndlp with the vport if successful, otherwise
4126 * return NULL.
4127 **/
4128static struct lpfc_nodelist *
4129lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
4130{
4131 struct lpfc_nodelist *ndlp;
4132 struct Scsi_Host *shost;
4133 struct lpfc_hba *phba;
4134
4135 if (!vport)
4136 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05004137 phba = vport->phba;
4138 if (!phba)
4139 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04004140 ndlp = lpfc_findnode_did(vport, Fabric_DID);
4141 if (!ndlp) {
4142 /* Cannot find existing Fabric ndlp, so allocate a new one */
4143 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
4144 if (!ndlp)
4145 return 0;
4146 lpfc_nlp_init(vport, ndlp, Fabric_DID);
4147 /* Set the node type */
4148 ndlp->nlp_type |= NLP_FABRIC;
4149 /* Put ndlp onto node list */
4150 lpfc_enqueue_node(vport, ndlp);
4151 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4152 /* re-setup ndlp without removing from node list */
4153 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
4154 if (!ndlp)
4155 return 0;
4156 }
James Smart63e801c2010-11-20 23:14:19 -05004157 if ((phba->pport->port_state < LPFC_FLOGI) &&
4158 (phba->pport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05004159 return NULL;
4160 /* If virtual link is not yet instantiated ignore CVL */
James Smart63e801c2010-11-20 23:14:19 -05004161 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
4162 && (vport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05004163 return NULL;
4164 shost = lpfc_shost_from_vport(vport);
4165 if (!shost)
4166 return NULL;
4167 lpfc_linkdown_port(vport);
4168 lpfc_cleanup_pending_mbox(vport);
4169 spin_lock_irq(shost->host_lock);
4170 vport->fc_flag |= FC_VPORT_CVL_RCVD;
4171 spin_unlock_irq(shost->host_lock);
4172
4173 return ndlp;
4174}
4175
4176/**
4177 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
4178 * @vport: pointer to lpfc hba data structure.
4179 *
4180 * This routine is to perform Clear Virtual Link (CVL) on all vports in
4181 * response to a FCF dead event.
4182 **/
4183static void
4184lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
4185{
4186 struct lpfc_vport **vports;
4187 int i;
4188
4189 vports = lpfc_create_vport_work_array(phba);
4190 if (vports)
4191 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
4192 lpfc_sli4_perform_vport_cvl(vports[i]);
4193 lpfc_destroy_vport_work_array(phba, vports);
4194}
4195
4196/**
James Smart76a95d72010-11-20 23:11:48 -05004197 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
James Smartda0436e2009-05-22 14:51:39 -04004198 * @phba: pointer to lpfc hba data structure.
4199 * @acqe_link: pointer to the async fcoe completion queue entry.
4200 *
4201 * This routine is to handle the SLI4 asynchronous fcoe event.
4202 **/
4203static void
James Smart76a95d72010-11-20 23:11:48 -05004204lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
James Smart70f3c072010-12-15 17:57:33 -05004205 struct lpfc_acqe_fip *acqe_fip)
James Smartda0436e2009-05-22 14:51:39 -04004206{
James Smart70f3c072010-12-15 17:57:33 -05004207 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004208 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04004209 struct lpfc_vport *vport;
4210 struct lpfc_nodelist *ndlp;
4211 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05004212 int active_vlink_present;
4213 struct lpfc_vport **vports;
4214 int i;
James Smartda0436e2009-05-22 14:51:39 -04004215
James Smart70f3c072010-12-15 17:57:33 -05004216 phba->fc_eventTag = acqe_fip->event_tag;
4217 phba->fcoe_eventtag = acqe_fip->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004218 switch (event_type) {
James Smart70f3c072010-12-15 17:57:33 -05004219 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
4220 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
4221 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
James Smart999d8132010-03-15 11:24:56 -04004222 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4223 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004224 "2546 New FCF event, evt_tag:x%x, "
4225 "index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004226 acqe_fip->event_tag,
4227 acqe_fip->index);
James Smart999d8132010-03-15 11:24:56 -04004228 else
4229 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
4230 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004231 "2788 FCF param modified event, "
4232 "evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004233 acqe_fip->event_tag,
4234 acqe_fip->index);
James Smart38b92ef2010-08-04 16:11:39 -04004235 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004236 /*
4237 * During period of FCF discovery, read the FCF
4238 * table record indexed by the event to update
James Smarta93ff372010-10-22 11:06:08 -04004239 * FCF roundrobin failover eligible FCF bmask.
James Smart0c9ab6f2010-02-26 14:15:57 -05004240 */
4241 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4242 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004243 "2779 Read FCF (x%x) for updating "
4244 "roundrobin FCF failover bmask\n",
James Smart70f3c072010-12-15 17:57:33 -05004245 acqe_fip->index);
4246 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004247 }
James Smart38b92ef2010-08-04 16:11:39 -04004248
4249 /* If the FCF discovery is in progress, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04004250 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04004251 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart38b92ef2010-08-04 16:11:39 -04004252 spin_unlock_irq(&phba->hbalock);
4253 break;
4254 }
4255 /* If fast FCF failover rescan event is pending, do nothing */
4256 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
4257 spin_unlock_irq(&phba->hbalock);
4258 break;
4259 }
4260
James Smartc2b97122013-05-31 17:05:36 -04004261 /* If the FCF has been in discovered state, do nothing. */
4262 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
James Smart3804dc82010-07-14 15:31:37 -04004263 spin_unlock_irq(&phba->hbalock);
4264 break;
4265 }
4266 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04004267
James Smart0c9ab6f2010-02-26 14:15:57 -05004268 /* Otherwise, scan the entire FCF table and re-discover SAN */
4269 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004270 "2770 Start FCF table scan per async FCF "
4271 "event, evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004272 acqe_fip->event_tag, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004273 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
4274 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04004275 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05004276 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4277 "2547 Issue FCF scan read FCF mailbox "
James Smarta93ff372010-10-22 11:06:08 -04004278 "command failed (x%x)\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004279 break;
4280
James Smart70f3c072010-12-15 17:57:33 -05004281 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
James Smartda0436e2009-05-22 14:51:39 -04004282 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04004283 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004284 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
4285 acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04004286 break;
4287
James Smart70f3c072010-12-15 17:57:33 -05004288 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
James Smart80c17842012-03-01 22:35:45 -05004289 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05004290 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004291 "2549 FCF (x%x) disconnected from network, "
James Smart70f3c072010-12-15 17:57:33 -05004292 "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart38b92ef2010-08-04 16:11:39 -04004293 /*
4294 * If we are in the middle of FCF failover process, clear
4295 * the corresponding FCF bit in the roundrobin bitmap.
James Smartda0436e2009-05-22 14:51:39 -04004296 */
James Smartfc2b9892010-02-26 14:15:29 -05004297 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004298 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05004299 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004300 /* Update FLOGI FCF failover eligible FCF bmask */
James Smart70f3c072010-12-15 17:57:33 -05004301 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004302 break;
4303 }
James Smart38b92ef2010-08-04 16:11:39 -04004304 spin_unlock_irq(&phba->hbalock);
4305
4306 /* If the event is not for currently used fcf do nothing */
James Smart70f3c072010-12-15 17:57:33 -05004307 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
James Smart38b92ef2010-08-04 16:11:39 -04004308 break;
4309
4310 /*
4311 * Otherwise, request the port to rediscover the entire FCF
4312 * table for a fast recovery from case that the current FCF
4313 * is no longer valid as we are not in the middle of FCF
4314 * failover process already.
4315 */
James Smartc2b97122013-05-31 17:05:36 -04004316 spin_lock_irq(&phba->hbalock);
4317 /* Mark the fast failover process in progress */
4318 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
4319 spin_unlock_irq(&phba->hbalock);
4320
4321 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
4322 "2771 Start FCF fast failover process due to "
4323 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
4324 "\n", acqe_fip->event_tag, acqe_fip->index);
4325 rc = lpfc_sli4_redisc_fcf_table(phba);
4326 if (rc) {
4327 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4328 LOG_DISCOVERY,
4329 "2772 Issue FCF rediscover mabilbox "
4330 "command failed, fail through to FCF "
4331 "dead event\n");
4332 spin_lock_irq(&phba->hbalock);
4333 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
4334 spin_unlock_irq(&phba->hbalock);
4335 /*
4336 * Last resort will fail over by treating this
4337 * as a link down to FCF registration.
4338 */
4339 lpfc_sli4_fcf_dead_failthrough(phba);
4340 } else {
4341 /* Reset FCF roundrobin bmask for new discovery */
4342 lpfc_sli4_clear_fcf_rr_bmask(phba);
4343 /*
4344 * Handling fast FCF failover to a DEAD FCF event is
4345 * considered equalivant to receiving CVL to all vports.
4346 */
4347 lpfc_sli4_perform_all_vport_cvl(phba);
4348 }
James Smartda0436e2009-05-22 14:51:39 -04004349 break;
James Smart70f3c072010-12-15 17:57:33 -05004350 case LPFC_FIP_EVENT_TYPE_CVL:
James Smart80c17842012-03-01 22:35:45 -05004351 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05004352 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6669f9b2009-10-02 15:16:45 -04004353 "2718 Clear Virtual Link Received for VPI 0x%x"
James Smart70f3c072010-12-15 17:57:33 -05004354 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart6d368e52011-05-24 11:44:12 -04004355
James Smart6669f9b2009-10-02 15:16:45 -04004356 vport = lpfc_find_vport_by_vpid(phba,
James Smart5248a742011-07-22 18:37:06 -04004357 acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004358 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004359 if (!ndlp)
4360 break;
James Smart695a8142010-01-26 23:08:03 -05004361 active_vlink_present = 0;
4362
4363 vports = lpfc_create_vport_work_array(phba);
4364 if (vports) {
4365 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
4366 i++) {
4367 if ((!(vports[i]->fc_flag &
4368 FC_VPORT_CVL_RCVD)) &&
4369 (vports[i]->port_state > LPFC_FDISC)) {
4370 active_vlink_present = 1;
4371 break;
4372 }
4373 }
4374 lpfc_destroy_vport_work_array(phba, vports);
4375 }
4376
4377 if (active_vlink_present) {
4378 /*
4379 * If there are other active VLinks present,
4380 * re-instantiate the Vlink using FDISC.
4381 */
James Smart256ec0d2013-04-17 20:14:58 -04004382 mod_timer(&ndlp->nlp_delayfunc,
4383 jiffies + msecs_to_jiffies(1000));
James Smartfc2b9892010-02-26 14:15:29 -05004384 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004385 spin_lock_irq(shost->host_lock);
4386 ndlp->nlp_flag |= NLP_DELAY_TMO;
4387 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05004388 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
4389 vport->port_state = LPFC_FDISC;
4390 } else {
James Smartecfd03c2010-02-12 14:41:27 -05004391 /*
4392 * Otherwise, we request port to rediscover
4393 * the entire FCF table for a fast recovery
4394 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05004395 * is no longer valid if we are not already
4396 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05004397 */
James Smartfc2b9892010-02-26 14:15:29 -05004398 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004399 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05004400 spin_unlock_irq(&phba->hbalock);
4401 break;
4402 }
4403 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05004404 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004405 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004406 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4407 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004408 "2773 Start FCF failover per CVL, "
James Smart70f3c072010-12-15 17:57:33 -05004409 "evt_tag:x%x\n", acqe_fip->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05004410 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05004411 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004412 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4413 LOG_DISCOVERY,
4414 "2774 Issue FCF rediscover "
4415 "mabilbox command failed, "
4416 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05004417 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004418 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004419 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05004420 /*
4421 * Last resort will be re-try on the
4422 * the current registered FCF entry.
4423 */
4424 lpfc_retry_pport_discovery(phba);
James Smart38b92ef2010-08-04 16:11:39 -04004425 } else
4426 /*
4427 * Reset FCF roundrobin bmask for new
4428 * discovery.
4429 */
James Smart7d791df2011-07-22 18:37:52 -04004430 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart6669f9b2009-10-02 15:16:45 -04004431 }
4432 break;
James Smartda0436e2009-05-22 14:51:39 -04004433 default:
4434 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4435 "0288 Unknown FCoE event type 0x%x event tag "
James Smart70f3c072010-12-15 17:57:33 -05004436 "0x%x\n", event_type, acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04004437 break;
4438 }
4439}
4440
4441/**
4442 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
4443 * @phba: pointer to lpfc hba data structure.
4444 * @acqe_link: pointer to the async dcbx completion queue entry.
4445 *
4446 * This routine is to handle the SLI4 asynchronous dcbx event.
4447 **/
4448static void
4449lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
4450 struct lpfc_acqe_dcbx *acqe_dcbx)
4451{
James Smart4d9ab992009-10-02 15:16:39 -04004452 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004453 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4454 "0290 The SLI4 DCBX asynchronous event is not "
4455 "handled yet\n");
4456}
4457
4458/**
James Smartb19a0612010-04-06 14:48:51 -04004459 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
4460 * @phba: pointer to lpfc hba data structure.
4461 * @acqe_link: pointer to the async grp5 completion queue entry.
4462 *
4463 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
4464 * is an asynchronous notified of a logical link speed change. The Port
4465 * reports the logical link speed in units of 10Mbps.
4466 **/
4467static void
4468lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
4469 struct lpfc_acqe_grp5 *acqe_grp5)
4470{
4471 uint16_t prev_ll_spd;
4472
4473 phba->fc_eventTag = acqe_grp5->event_tag;
4474 phba->fcoe_eventtag = acqe_grp5->event_tag;
4475 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
4476 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04004477 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
James Smartb19a0612010-04-06 14:48:51 -04004478 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4479 "2789 GRP5 Async Event: Updating logical link speed "
James Smart8b68cd52012-09-29 11:32:37 -04004480 "from %dMbps to %dMbps\n", prev_ll_spd,
4481 phba->sli4_hba.link_state.logical_speed);
James Smartb19a0612010-04-06 14:48:51 -04004482}
4483
4484/**
James Smartda0436e2009-05-22 14:51:39 -04004485 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
4486 * @phba: pointer to lpfc hba data structure.
4487 *
4488 * This routine is invoked by the worker thread to process all the pending
4489 * SLI4 asynchronous events.
4490 **/
4491void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
4492{
4493 struct lpfc_cq_event *cq_event;
4494
4495 /* First, declare the async event has been handled */
4496 spin_lock_irq(&phba->hbalock);
4497 phba->hba_flag &= ~ASYNC_EVENT;
4498 spin_unlock_irq(&phba->hbalock);
4499 /* Now, handle all the async events */
4500 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
4501 /* Get the first event from the head of the event queue */
4502 spin_lock_irq(&phba->hbalock);
4503 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
4504 cq_event, struct lpfc_cq_event, list);
4505 spin_unlock_irq(&phba->hbalock);
4506 /* Process the asynchronous event */
4507 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
4508 case LPFC_TRAILER_CODE_LINK:
4509 lpfc_sli4_async_link_evt(phba,
4510 &cq_event->cqe.acqe_link);
4511 break;
4512 case LPFC_TRAILER_CODE_FCOE:
James Smart70f3c072010-12-15 17:57:33 -05004513 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004514 break;
4515 case LPFC_TRAILER_CODE_DCBX:
4516 lpfc_sli4_async_dcbx_evt(phba,
4517 &cq_event->cqe.acqe_dcbx);
4518 break;
James Smartb19a0612010-04-06 14:48:51 -04004519 case LPFC_TRAILER_CODE_GRP5:
4520 lpfc_sli4_async_grp5_evt(phba,
4521 &cq_event->cqe.acqe_grp5);
4522 break;
James Smart70f3c072010-12-15 17:57:33 -05004523 case LPFC_TRAILER_CODE_FC:
4524 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
4525 break;
4526 case LPFC_TRAILER_CODE_SLI:
4527 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
4528 break;
James Smartda0436e2009-05-22 14:51:39 -04004529 default:
4530 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4531 "1804 Invalid asynchrous event code: "
4532 "x%x\n", bf_get(lpfc_trailer_code,
4533 &cq_event->cqe.mcqe_cmpl));
4534 break;
4535 }
4536 /* Free the completion event processed to the free pool */
4537 lpfc_sli4_cq_event_release(phba, cq_event);
4538 }
4539}
4540
4541/**
James Smartecfd03c2010-02-12 14:41:27 -05004542 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
4543 * @phba: pointer to lpfc hba data structure.
4544 *
4545 * This routine is invoked by the worker thread to process FCF table
4546 * rediscovery pending completion event.
4547 **/
4548void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
4549{
4550 int rc;
4551
4552 spin_lock_irq(&phba->hbalock);
4553 /* Clear FCF rediscovery timeout event */
4554 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
4555 /* Clear driver fast failover FCF record flag */
4556 phba->fcf.failover_rec.flag = 0;
4557 /* Set state for FCF fast failover */
4558 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
4559 spin_unlock_irq(&phba->hbalock);
4560
4561 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05004562 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004563 "2777 Start post-quiescent FCF table scan\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05004564 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05004565 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05004566 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4567 "2747 Issue FCF scan read FCF mailbox "
4568 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05004569}
4570
4571/**
James Smartda0436e2009-05-22 14:51:39 -04004572 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
4573 * @phba: pointer to lpfc hba data structure.
4574 * @dev_grp: The HBA PCI-Device group number.
4575 *
4576 * This routine is invoked to set up the per HBA PCI-Device group function
4577 * API jump table entries.
4578 *
4579 * Return: 0 if success, otherwise -ENODEV
4580 **/
4581int
4582lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05004583{
4584 int rc;
4585
James Smartda0436e2009-05-22 14:51:39 -04004586 /* Set up lpfc PCI-device group */
4587 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05004588
James Smartda0436e2009-05-22 14:51:39 -04004589 /* The LPFC_PCI_DEV_OC uses SLI4 */
4590 if (dev_grp == LPFC_PCI_DEV_OC)
4591 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05004592
James Smartda0436e2009-05-22 14:51:39 -04004593 /* Set up device INIT API function jump table */
4594 rc = lpfc_init_api_table_setup(phba, dev_grp);
4595 if (rc)
4596 return -ENODEV;
4597 /* Set up SCSI API function jump table */
4598 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
4599 if (rc)
4600 return -ENODEV;
4601 /* Set up SLI API function jump table */
4602 rc = lpfc_sli_api_table_setup(phba, dev_grp);
4603 if (rc)
4604 return -ENODEV;
4605 /* Set up MBOX API function jump table */
4606 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
4607 if (rc)
4608 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004609
James Smartda0436e2009-05-22 14:51:39 -04004610 return 0;
James Smart5b75da22008-12-04 22:39:35 -05004611}
4612
4613/**
James Smart3621a712009-04-06 18:47:14 -04004614 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05004615 * @phba: pointer to lpfc hba data structure.
4616 * @intr_mode: active interrupt mode adopted.
4617 *
4618 * This routine it invoked to log the currently used active interrupt mode
4619 * to the device.
James Smart3772a992009-05-22 14:50:54 -04004620 **/
4621static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05004622{
4623 switch (intr_mode) {
4624 case 0:
4625 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4626 "0470 Enable INTx interrupt mode.\n");
4627 break;
4628 case 1:
4629 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4630 "0481 Enabled MSI interrupt mode.\n");
4631 break;
4632 case 2:
4633 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4634 "0480 Enabled MSI-X interrupt mode.\n");
4635 break;
4636 default:
4637 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4638 "0482 Illegal interrupt mode.\n");
4639 break;
4640 }
4641 return;
4642}
4643
James Smart5b75da22008-12-04 22:39:35 -05004644/**
James Smart3772a992009-05-22 14:50:54 -04004645 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05004646 * @phba: pointer to lpfc hba data structure.
4647 *
James Smart3772a992009-05-22 14:50:54 -04004648 * This routine is invoked to enable the PCI device that is common to all
4649 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004650 *
4651 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004652 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004653 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05004654 **/
James Smart3772a992009-05-22 14:50:54 -04004655static int
4656lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004657{
James Smart3772a992009-05-22 14:50:54 -04004658 struct pci_dev *pdev;
James Smart079b5c92011-08-21 21:48:49 -04004659 int bars = 0;
James Smart5b75da22008-12-04 22:39:35 -05004660
James Smart3772a992009-05-22 14:50:54 -04004661 /* Obtain PCI device reference */
4662 if (!phba->pcidev)
4663 goto out_error;
4664 else
4665 pdev = phba->pcidev;
4666 /* Select PCI BARs */
4667 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4668 /* Enable PCI device */
4669 if (pci_enable_device_mem(pdev))
4670 goto out_error;
4671 /* Request PCI resource for the device */
4672 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
4673 goto out_disable_device;
4674 /* Set up device as PCI master and save state for EEH */
4675 pci_set_master(pdev);
4676 pci_try_set_mwi(pdev);
4677 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05004678
James Smart05580562011-05-24 11:40:48 -04004679 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
Jon Mason453193e2013-09-11 15:38:13 -07004680 if (pci_is_pcie(pdev))
James Smart05580562011-05-24 11:40:48 -04004681 pdev->needs_freset = 1;
4682
James Smart3772a992009-05-22 14:50:54 -04004683 return 0;
James Smart5b75da22008-12-04 22:39:35 -05004684
James Smart3772a992009-05-22 14:50:54 -04004685out_disable_device:
4686 pci_disable_device(pdev);
4687out_error:
James Smart079b5c92011-08-21 21:48:49 -04004688 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4689 "1401 Failed to enable pci device, bars:x%x\n", bars);
James Smart3772a992009-05-22 14:50:54 -04004690 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004691}
4692
4693/**
James Smart3772a992009-05-22 14:50:54 -04004694 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05004695 * @phba: pointer to lpfc hba data structure.
4696 *
James Smart3772a992009-05-22 14:50:54 -04004697 * This routine is invoked to disable the PCI device that is common to all
4698 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004699 **/
4700static void
James Smart3772a992009-05-22 14:50:54 -04004701lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004702{
James Smart3772a992009-05-22 14:50:54 -04004703 struct pci_dev *pdev;
4704 int bars;
James Smart5b75da22008-12-04 22:39:35 -05004705
James Smart3772a992009-05-22 14:50:54 -04004706 /* Obtain PCI device reference */
4707 if (!phba->pcidev)
4708 return;
4709 else
4710 pdev = phba->pcidev;
4711 /* Select PCI BARs */
4712 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4713 /* Release PCI resource and disable PCI device */
4714 pci_release_selected_regions(pdev, bars);
4715 pci_disable_device(pdev);
James Smart5b75da22008-12-04 22:39:35 -05004716
4717 return;
4718}
4719
4720/**
James Smart3772a992009-05-22 14:50:54 -04004721 * lpfc_reset_hba - Reset a hba
4722 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04004723 *
James Smart3772a992009-05-22 14:50:54 -04004724 * This routine is invoked to reset a hba device. It brings the HBA
4725 * offline, performs a board restart, and then brings the board back
4726 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
4727 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04004728 **/
James Smart3772a992009-05-22 14:50:54 -04004729void
4730lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004731{
James Smart3772a992009-05-22 14:50:54 -04004732 /* If resets are disabled then set error state and return. */
4733 if (!phba->cfg_enable_hba_reset) {
4734 phba->link_state = LPFC_HBA_ERROR;
4735 return;
4736 }
James Smartee620212014-04-04 13:51:53 -04004737 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
4738 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
4739 else
4740 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart3772a992009-05-22 14:50:54 -04004741 lpfc_offline(phba);
4742 lpfc_sli_brdrestart(phba);
4743 lpfc_online(phba);
4744 lpfc_unblock_mgmt_io(phba);
4745}
dea31012005-04-17 16:05:31 -05004746
James Smart3772a992009-05-22 14:50:54 -04004747/**
James Smart0a96e972011-07-22 18:37:28 -04004748 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
4749 * @phba: pointer to lpfc hba data structure.
4750 *
4751 * This function enables the PCI SR-IOV virtual functions to a physical
4752 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4753 * enable the number of virtual functions to the physical function. As
4754 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4755 * API call does not considered as an error condition for most of the device.
4756 **/
4757uint16_t
4758lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
4759{
4760 struct pci_dev *pdev = phba->pcidev;
4761 uint16_t nr_virtfn;
4762 int pos;
4763
James Smart0a96e972011-07-22 18:37:28 -04004764 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
4765 if (pos == 0)
4766 return 0;
4767
4768 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
4769 return nr_virtfn;
4770}
4771
4772/**
James Smart912e3ac2011-05-24 11:42:11 -04004773 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
4774 * @phba: pointer to lpfc hba data structure.
4775 * @nr_vfn: number of virtual functions to be enabled.
4776 *
4777 * This function enables the PCI SR-IOV virtual functions to a physical
4778 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4779 * enable the number of virtual functions to the physical function. As
4780 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4781 * API call does not considered as an error condition for most of the device.
4782 **/
4783int
4784lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
4785{
4786 struct pci_dev *pdev = phba->pcidev;
James Smart0a96e972011-07-22 18:37:28 -04004787 uint16_t max_nr_vfn;
James Smart912e3ac2011-05-24 11:42:11 -04004788 int rc;
4789
James Smart0a96e972011-07-22 18:37:28 -04004790 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
4791 if (nr_vfn > max_nr_vfn) {
4792 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4793 "3057 Requested vfs (%d) greater than "
4794 "supported vfs (%d)", nr_vfn, max_nr_vfn);
4795 return -EINVAL;
4796 }
4797
James Smart912e3ac2011-05-24 11:42:11 -04004798 rc = pci_enable_sriov(pdev, nr_vfn);
4799 if (rc) {
4800 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4801 "2806 Failed to enable sriov on this device "
4802 "with vfn number nr_vf:%d, rc:%d\n",
4803 nr_vfn, rc);
4804 } else
4805 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4806 "2807 Successful enable sriov on this device "
4807 "with vfn number nr_vf:%d\n", nr_vfn);
4808 return rc;
4809}
4810
4811/**
James Smart3772a992009-05-22 14:50:54 -04004812 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
4813 * @phba: pointer to lpfc hba data structure.
4814 *
4815 * This routine is invoked to set up the driver internal resources specific to
4816 * support the SLI-3 HBA device it attached to.
4817 *
4818 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004819 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004820 * other values - error
4821 **/
4822static int
4823lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
4824{
4825 struct lpfc_sli *psli;
James Smart912e3ac2011-05-24 11:42:11 -04004826 int rc;
dea31012005-04-17 16:05:31 -05004827
James Smart3772a992009-05-22 14:50:54 -04004828 /*
4829 * Initialize timers used by driver
4830 */
dea31012005-04-17 16:05:31 -05004831
James Smart3772a992009-05-22 14:50:54 -04004832 /* Heartbeat timer */
4833 init_timer(&phba->hb_tmofunc);
4834 phba->hb_tmofunc.function = lpfc_hb_timeout;
4835 phba->hb_tmofunc.data = (unsigned long)phba;
4836
4837 psli = &phba->sli;
4838 /* MBOX heartbeat timer */
4839 init_timer(&psli->mbox_tmo);
4840 psli->mbox_tmo.function = lpfc_mbox_timeout;
4841 psli->mbox_tmo.data = (unsigned long) phba;
4842 /* FCP polling mode timer */
4843 init_timer(&phba->fcp_poll_timer);
4844 phba->fcp_poll_timer.function = lpfc_poll_timeout;
4845 phba->fcp_poll_timer.data = (unsigned long) phba;
4846 /* Fabric block timer */
4847 init_timer(&phba->fabric_block_timer);
4848 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4849 phba->fabric_block_timer.data = (unsigned long) phba;
4850 /* EA polling mode timer */
4851 init_timer(&phba->eratt_poll);
4852 phba->eratt_poll.function = lpfc_poll_eratt;
4853 phba->eratt_poll.data = (unsigned long) phba;
4854
4855 /* Host attention work mask setup */
4856 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
4857 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
4858
4859 /* Get all the module params for configuring this host */
4860 lpfc_get_cfgparam(phba);
James Smart49198b32010-04-06 15:04:33 -04004861 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
4862 phba->menlo_flag |= HBA_MENLO_SUPPORT;
4863 /* check for menlo minimum sg count */
4864 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
4865 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
4866 }
4867
James Smart2a76a282012-08-03 12:35:54 -04004868 if (!phba->sli.ring)
4869 phba->sli.ring = (struct lpfc_sli_ring *)
4870 kzalloc(LPFC_SLI3_MAX_RING *
4871 sizeof(struct lpfc_sli_ring), GFP_KERNEL);
4872 if (!phba->sli.ring)
4873 return -ENOMEM;
4874
James Smart3772a992009-05-22 14:50:54 -04004875 /*
James Smart96f70772013-04-17 20:16:15 -04004876 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smart3772a992009-05-22 14:50:54 -04004877 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart3772a992009-05-22 14:50:54 -04004878 */
James Smart3772a992009-05-22 14:50:54 -04004879
James Smart96f70772013-04-17 20:16:15 -04004880 /* Initialize the host templates the configured values. */
James Smart3772a992009-05-22 14:50:54 -04004881 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4882 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4883
James Smart96f70772013-04-17 20:16:15 -04004884 /* There are going to be 2 reserved BDEs: 1 FCP cmnd + 1 FCP rsp */
4885 if (phba->cfg_enable_bg) {
4886 /*
4887 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
4888 * the FCP rsp, and a BDE for each. Sice we have no control
4889 * over how many protection data segments the SCSI Layer
4890 * will hand us (ie: there could be one for every block
4891 * in the IO), we just allocate enough BDEs to accomidate
4892 * our max amount and we need to limit lpfc_sg_seg_cnt to
4893 * minimize the risk of running out.
4894 */
4895 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4896 sizeof(struct fcp_rsp) +
4897 (LPFC_MAX_SG_SEG_CNT * sizeof(struct ulp_bde64));
4898
4899 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SEG_CNT_DIF)
4900 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT_DIF;
4901
4902 /* Total BDEs in BPL for scsi_sg_list and scsi_sg_prot_list */
4903 phba->cfg_total_seg_cnt = LPFC_MAX_SG_SEG_CNT;
4904 } else {
4905 /*
4906 * The scsi_buf for a regular I/O will hold the FCP cmnd,
4907 * the FCP rsp, a BDE for each, and a BDE for up to
4908 * cfg_sg_seg_cnt data segments.
4909 */
4910 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4911 sizeof(struct fcp_rsp) +
4912 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
4913
4914 /* Total BDEs in BPL for scsi_sg_list */
4915 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
4916 }
4917
4918 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
4919 "9088 sg_tablesize:%d dmabuf_size:%d total_bde:%d\n",
4920 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
4921 phba->cfg_total_seg_cnt);
4922
James Smart3772a992009-05-22 14:50:54 -04004923 phba->max_vpi = LPFC_MAX_VPI;
4924 /* This will be set to correct value after config_port mbox */
4925 phba->max_vports = 0;
4926
4927 /*
4928 * Initialize the SLI Layer to run with lpfc HBAs.
4929 */
4930 lpfc_sli_setup(phba);
4931 lpfc_sli_queue_setup(phba);
4932
4933 /* Allocate device driver memory */
4934 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
4935 return -ENOMEM;
4936
James Smart912e3ac2011-05-24 11:42:11 -04004937 /*
4938 * Enable sr-iov virtual functions if supported and configured
4939 * through the module parameter.
4940 */
4941 if (phba->cfg_sriov_nr_virtfn > 0) {
4942 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
4943 phba->cfg_sriov_nr_virtfn);
4944 if (rc) {
4945 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4946 "2808 Requested number of SR-IOV "
4947 "virtual functions (%d) is not "
4948 "supported\n",
4949 phba->cfg_sriov_nr_virtfn);
4950 phba->cfg_sriov_nr_virtfn = 0;
4951 }
4952 }
4953
James Smart3772a992009-05-22 14:50:54 -04004954 return 0;
4955}
4956
4957/**
4958 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
4959 * @phba: pointer to lpfc hba data structure.
4960 *
4961 * This routine is invoked to unset the driver internal resources set up
4962 * specific for supporting the SLI-3 HBA device it attached to.
4963 **/
4964static void
4965lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
4966{
4967 /* Free device driver memory allocated */
4968 lpfc_mem_free_all(phba);
4969
4970 return;
4971}
4972
4973/**
James Smartda0436e2009-05-22 14:51:39 -04004974 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
4975 * @phba: pointer to lpfc hba data structure.
4976 *
4977 * This routine is invoked to set up the driver internal resources specific to
4978 * support the SLI-4 HBA device it attached to.
4979 *
4980 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004981 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004982 * other values - error
4983 **/
4984static int
4985lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
4986{
James Smart7bb03bb2013-04-17 20:19:16 -04004987 struct lpfc_vector_map_info *cpup;
James Smartda0436e2009-05-22 14:51:39 -04004988 struct lpfc_sli *psli;
James Smart28baac72010-02-12 14:42:03 -05004989 LPFC_MBOXQ_t *mboxq;
James Smart96f70772013-04-17 20:16:15 -04004990 int rc, i, hbq_count, max_buf_size;
James Smart28baac72010-02-12 14:42:03 -05004991 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
4992 struct lpfc_mqe *mqe;
James Smart09294d42013-04-17 20:16:05 -04004993 int longs;
James Smart1ba981f2014-02-20 09:56:45 -05004994 int fof_vectors = 0;
James Smartda0436e2009-05-22 14:51:39 -04004995
James Smart716d3bc2013-09-06 12:18:28 -04004996 /* Get all the module params for configuring this host */
4997 lpfc_get_cfgparam(phba);
4998
James Smartda0436e2009-05-22 14:51:39 -04004999 /* Before proceed, wait for POST done and device ready */
5000 rc = lpfc_sli4_post_status_check(phba);
5001 if (rc)
5002 return -ENODEV;
5003
5004 /*
5005 * Initialize timers used by driver
5006 */
5007
5008 /* Heartbeat timer */
5009 init_timer(&phba->hb_tmofunc);
5010 phba->hb_tmofunc.function = lpfc_hb_timeout;
5011 phba->hb_tmofunc.data = (unsigned long)phba;
James Smart19ca7602010-11-20 23:11:55 -05005012 init_timer(&phba->rrq_tmr);
5013 phba->rrq_tmr.function = lpfc_rrq_timeout;
5014 phba->rrq_tmr.data = (unsigned long)phba;
James Smartda0436e2009-05-22 14:51:39 -04005015
5016 psli = &phba->sli;
5017 /* MBOX heartbeat timer */
5018 init_timer(&psli->mbox_tmo);
5019 psli->mbox_tmo.function = lpfc_mbox_timeout;
5020 psli->mbox_tmo.data = (unsigned long) phba;
5021 /* Fabric block timer */
5022 init_timer(&phba->fabric_block_timer);
5023 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
5024 phba->fabric_block_timer.data = (unsigned long) phba;
5025 /* EA polling mode timer */
5026 init_timer(&phba->eratt_poll);
5027 phba->eratt_poll.function = lpfc_poll_eratt;
5028 phba->eratt_poll.data = (unsigned long) phba;
James Smartecfd03c2010-02-12 14:41:27 -05005029 /* FCF rediscover timer */
5030 init_timer(&phba->fcf.redisc_wait);
5031 phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
5032 phba->fcf.redisc_wait.data = (unsigned long)phba;
5033
James Smartda0436e2009-05-22 14:51:39 -04005034 /*
James Smart7ad20aa2011-05-24 11:44:28 -04005035 * Control structure for handling external multi-buffer mailbox
5036 * command pass-through.
5037 */
5038 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
5039 sizeof(struct lpfc_mbox_ext_buf_ctx));
5040 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
5041
James Smartda0436e2009-05-22 14:51:39 -04005042 phba->max_vpi = LPFC_MAX_VPI;
James Smart67d12732012-08-03 12:36:13 -04005043
James Smartda0436e2009-05-22 14:51:39 -04005044 /* This will be set to correct value after the read_config mbox */
5045 phba->max_vports = 0;
5046
5047 /* Program the default value of vlan_id and fc_map */
5048 phba->valid_vlan = 0;
5049 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5050 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5051 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5052
5053 /*
James Smart2a76a282012-08-03 12:35:54 -04005054 * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
5055 * we will associate a new ring, for each FCP fastpath EQ/CQ/WQ tuple.
5056 */
5057 if (!phba->sli.ring)
5058 phba->sli.ring = kzalloc(
James Smart67d12732012-08-03 12:36:13 -04005059 (LPFC_SLI3_MAX_RING + phba->cfg_fcp_io_channel) *
James Smart2a76a282012-08-03 12:35:54 -04005060 sizeof(struct lpfc_sli_ring), GFP_KERNEL);
5061 if (!phba->sli.ring)
5062 return -ENOMEM;
James Smart085c6472010-11-20 23:11:37 -05005063
James Smart09294d42013-04-17 20:16:05 -04005064 /*
5065 * It doesn't matter what family our adapter is in, we are
5066 * limited to 2 Pages, 512 SGEs, for our SGL.
5067 * There are going to be 2 reserved SGEs: 1 FCP cmnd + 1 FCP rsp
5068 */
5069 max_buf_size = (2 * SLI4_PAGE_SIZE);
5070 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SGL_SEG_CNT - 2)
5071 phba->cfg_sg_seg_cnt = LPFC_MAX_SGL_SEG_CNT - 2;
James Smart09294d42013-04-17 20:16:05 -04005072
James Smartda0436e2009-05-22 14:51:39 -04005073 /*
James Smart96f70772013-04-17 20:16:15 -04005074 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smartda0436e2009-05-22 14:51:39 -04005075 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart28baac72010-02-12 14:42:03 -05005076 */
James Smart085c6472010-11-20 23:11:37 -05005077
James Smart96f70772013-04-17 20:16:15 -04005078 if (phba->cfg_enable_bg) {
5079 /*
5080 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
5081 * the FCP rsp, and a SGE for each. Sice we have no control
5082 * over how many protection data segments the SCSI Layer
5083 * will hand us (ie: there could be one for every block
5084 * in the IO), we just allocate enough SGEs to accomidate
5085 * our max amount and we need to limit lpfc_sg_seg_cnt to
5086 * minimize the risk of running out.
5087 */
5088 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5089 sizeof(struct fcp_rsp) + max_buf_size;
5090
5091 /* Total SGEs for scsi_sg_list and scsi_sg_prot_list */
5092 phba->cfg_total_seg_cnt = LPFC_MAX_SGL_SEG_CNT;
5093
5094 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SLI4_SEG_CNT_DIF)
5095 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SLI4_SEG_CNT_DIF;
5096 } else {
5097 /*
5098 * The scsi_buf for a regular I/O will hold the FCP cmnd,
5099 * the FCP rsp, a SGE for each, and a SGE for up to
5100 * cfg_sg_seg_cnt data segments.
5101 */
5102 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5103 sizeof(struct fcp_rsp) +
5104 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge));
5105
5106 /* Total SGEs for scsi_sg_list */
5107 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
5108 /*
5109 * NOTE: if (phba->cfg_sg_seg_cnt + 2) <= 256 we only need
5110 * to post 1 page for the SGL.
5111 */
James Smart085c6472010-11-20 23:11:37 -05005112 }
James Smartacd68592012-01-18 16:25:09 -05005113
James Smart96f70772013-04-17 20:16:15 -04005114 /* Initialize the host templates with the updated values. */
5115 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5116 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5117
5118 if (phba->cfg_sg_dma_buf_size <= LPFC_MIN_SG_SLI4_BUF_SZ)
5119 phba->cfg_sg_dma_buf_size = LPFC_MIN_SG_SLI4_BUF_SZ;
5120 else
5121 phba->cfg_sg_dma_buf_size =
5122 SLI4_PAGE_ALIGN(phba->cfg_sg_dma_buf_size);
5123
5124 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
5125 "9087 sg_tablesize:%d dmabuf_size:%d total_sge:%d\n",
5126 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
5127 phba->cfg_total_seg_cnt);
James Smartda0436e2009-05-22 14:51:39 -04005128
5129 /* Initialize buffer queue management fields */
5130 hbq_count = lpfc_sli_hbq_count();
5131 for (i = 0; i < hbq_count; ++i)
5132 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
5133 INIT_LIST_HEAD(&phba->rb_pend_list);
5134 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
5135 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
5136
5137 /*
5138 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
5139 */
5140 /* Initialize the Abort scsi buffer list used by driver */
5141 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
5142 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
5143 /* This abort list used by worker thread */
5144 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
5145
5146 /*
James Smart6d368e52011-05-24 11:44:12 -04005147 * Initialize driver internal slow-path work queues
James Smartda0436e2009-05-22 14:51:39 -04005148 */
5149
5150 /* Driver internel slow-path CQ Event pool */
5151 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
5152 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04005153 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04005154 /* Asynchronous event CQ Event work queue list */
5155 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
5156 /* Fast-path XRI aborted CQ Event work queue list */
5157 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
5158 /* Slow-path XRI aborted CQ Event work queue list */
5159 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
5160 /* Receive queue CQ Event work queue list */
5161 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
5162
James Smart6d368e52011-05-24 11:44:12 -04005163 /* Initialize extent block lists. */
5164 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
5165 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
5166 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
5167 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
5168
James Smartda0436e2009-05-22 14:51:39 -04005169 /* Initialize the driver internal SLI layer lists. */
5170 lpfc_sli_setup(phba);
5171 lpfc_sli_queue_setup(phba);
5172
5173 /* Allocate device driver memory */
5174 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
5175 if (rc)
5176 return -ENOMEM;
5177
James Smart2fcee4b2010-12-15 17:57:46 -05005178 /* IF Type 2 ports get initialized now. */
5179 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
5180 LPFC_SLI_INTF_IF_TYPE_2) {
5181 rc = lpfc_pci_function_reset(phba);
5182 if (unlikely(rc))
5183 return -ENODEV;
5184 }
5185
James Smartda0436e2009-05-22 14:51:39 -04005186 /* Create the bootstrap mailbox command */
5187 rc = lpfc_create_bootstrap_mbox(phba);
5188 if (unlikely(rc))
5189 goto out_free_mem;
5190
5191 /* Set up the host's endian order with the device. */
5192 rc = lpfc_setup_endian_order(phba);
5193 if (unlikely(rc))
5194 goto out_free_bsmbx;
5195
5196 /* Set up the hba's configuration parameters. */
5197 rc = lpfc_sli4_read_config(phba);
5198 if (unlikely(rc))
5199 goto out_free_bsmbx;
James Smartcff261f2013-12-17 20:29:47 -05005200 rc = lpfc_mem_alloc_active_rrq_pool_s4(phba);
5201 if (unlikely(rc))
5202 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04005203
James Smart2fcee4b2010-12-15 17:57:46 -05005204 /* IF Type 0 ports get initialized now. */
5205 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
5206 LPFC_SLI_INTF_IF_TYPE_0) {
5207 rc = lpfc_pci_function_reset(phba);
5208 if (unlikely(rc))
5209 goto out_free_bsmbx;
5210 }
James Smartda0436e2009-05-22 14:51:39 -04005211
James Smartcb5172e2010-03-15 11:25:07 -04005212 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
5213 GFP_KERNEL);
5214 if (!mboxq) {
5215 rc = -ENOMEM;
5216 goto out_free_bsmbx;
5217 }
5218
James Smartfedd3b72011-02-16 12:39:24 -05005219 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
James Smartcb5172e2010-03-15 11:25:07 -04005220 lpfc_supported_pages(mboxq);
5221 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smartfedd3b72011-02-16 12:39:24 -05005222 if (!rc) {
5223 mqe = &mboxq->u.mqe;
5224 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
5225 LPFC_MAX_SUPPORTED_PAGES);
5226 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
5227 switch (pn_page[i]) {
5228 case LPFC_SLI4_PARAMETERS:
5229 phba->sli4_hba.pc_sli4_params.supported = 1;
5230 break;
5231 default:
5232 break;
5233 }
5234 }
5235 /* Read the port's SLI4 Parameters capabilities if supported. */
5236 if (phba->sli4_hba.pc_sli4_params.supported)
5237 rc = lpfc_pc_sli4_params_get(phba, mboxq);
5238 if (rc) {
5239 mempool_free(mboxq, phba->mbox_mem_pool);
5240 rc = -EIO;
5241 goto out_free_bsmbx;
James Smartcb5172e2010-03-15 11:25:07 -04005242 }
5243 }
James Smartfedd3b72011-02-16 12:39:24 -05005244 /*
5245 * Get sli4 parameters that override parameters from Port capabilities.
James Smart6d368e52011-05-24 11:44:12 -04005246 * If this call fails, it isn't critical unless the SLI4 parameters come
5247 * back in conflict.
James Smartfedd3b72011-02-16 12:39:24 -05005248 */
James Smart6d368e52011-05-24 11:44:12 -04005249 rc = lpfc_get_sli4_parameters(phba, mboxq);
5250 if (rc) {
5251 if (phba->sli4_hba.extents_in_use &&
5252 phba->sli4_hba.rpi_hdrs_in_use) {
5253 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5254 "2999 Unsupported SLI4 Parameters "
5255 "Extents and RPI headers enabled.\n");
5256 goto out_free_bsmbx;
5257 }
5258 }
James Smartcb5172e2010-03-15 11:25:07 -04005259 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1ba981f2014-02-20 09:56:45 -05005260
5261 /* Verify OAS is supported */
5262 lpfc_sli4_oas_verify(phba);
5263 if (phba->cfg_fof)
5264 fof_vectors = 1;
5265
James Smart5350d872011-10-10 21:33:49 -04005266 /* Verify all the SLI4 queues */
5267 rc = lpfc_sli4_queue_verify(phba);
James Smartda0436e2009-05-22 14:51:39 -04005268 if (rc)
5269 goto out_free_bsmbx;
5270
5271 /* Create driver internal CQE event pool */
5272 rc = lpfc_sli4_cq_event_pool_create(phba);
5273 if (rc)
James Smart5350d872011-10-10 21:33:49 -04005274 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04005275
James Smart8a9d2e82012-05-09 21:16:12 -04005276 /* Initialize sgl lists per host */
5277 lpfc_init_sgl_list(phba);
5278
5279 /* Allocate and initialize active sgl array */
James Smartda0436e2009-05-22 14:51:39 -04005280 rc = lpfc_init_active_sgl_array(phba);
5281 if (rc) {
5282 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5283 "1430 Failed to initialize sgl list.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04005284 goto out_destroy_cq_event_pool;
James Smartda0436e2009-05-22 14:51:39 -04005285 }
James Smartda0436e2009-05-22 14:51:39 -04005286 rc = lpfc_sli4_init_rpi_hdrs(phba);
5287 if (rc) {
5288 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5289 "1432 Failed to initialize rpi headers.\n");
5290 goto out_free_active_sgl;
5291 }
5292
James Smarta93ff372010-10-22 11:06:08 -04005293 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
James Smart0c9ab6f2010-02-26 14:15:57 -05005294 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
5295 phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
5296 GFP_KERNEL);
5297 if (!phba->fcf.fcf_rr_bmask) {
5298 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5299 "2759 Failed allocate memory for FCF round "
5300 "robin failover bmask\n");
James Smart05580562011-05-24 11:40:48 -04005301 rc = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -05005302 goto out_remove_rpi_hdrs;
5303 }
5304
James Smart67d12732012-08-03 12:36:13 -04005305 phba->sli4_hba.fcp_eq_hdl =
5306 kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
James Smart1ba981f2014-02-20 09:56:45 -05005307 (fof_vectors + phba->cfg_fcp_io_channel)),
5308 GFP_KERNEL);
James Smart67d12732012-08-03 12:36:13 -04005309 if (!phba->sli4_hba.fcp_eq_hdl) {
5310 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5311 "2572 Failed allocate memory for "
5312 "fast-path per-EQ handle array\n");
5313 rc = -ENOMEM;
5314 goto out_free_fcf_rr_bmask;
James Smartda0436e2009-05-22 14:51:39 -04005315 }
5316
5317 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
James Smart1ba981f2014-02-20 09:56:45 -05005318 (fof_vectors +
5319 phba->cfg_fcp_io_channel)), GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005320 if (!phba->sli4_hba.msix_entries) {
5321 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5322 "2573 Failed allocate memory for msi-x "
5323 "interrupt vector entries\n");
James Smart05580562011-05-24 11:40:48 -04005324 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04005325 goto out_free_fcp_eq_hdl;
5326 }
5327
James Smart7bb03bb2013-04-17 20:19:16 -04005328 phba->sli4_hba.cpu_map = kzalloc((sizeof(struct lpfc_vector_map_info) *
5329 phba->sli4_hba.num_present_cpu),
5330 GFP_KERNEL);
5331 if (!phba->sli4_hba.cpu_map) {
5332 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5333 "3327 Failed allocate memory for msi-x "
5334 "interrupt vector mapping\n");
5335 rc = -ENOMEM;
5336 goto out_free_msix;
5337 }
James Smartb246de12013-05-31 17:03:07 -04005338 if (lpfc_used_cpu == NULL) {
5339 lpfc_used_cpu = kzalloc((sizeof(uint16_t) * lpfc_present_cpu),
5340 GFP_KERNEL);
5341 if (!lpfc_used_cpu) {
5342 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5343 "3335 Failed allocate memory for msi-x "
5344 "interrupt vector mapping\n");
5345 kfree(phba->sli4_hba.cpu_map);
5346 rc = -ENOMEM;
5347 goto out_free_msix;
5348 }
5349 for (i = 0; i < lpfc_present_cpu; i++)
5350 lpfc_used_cpu[i] = LPFC_VECTOR_MAP_EMPTY;
5351 }
5352
James Smart7bb03bb2013-04-17 20:19:16 -04005353 /* Initialize io channels for round robin */
5354 cpup = phba->sli4_hba.cpu_map;
5355 rc = 0;
5356 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
5357 cpup->channel_id = rc;
5358 rc++;
5359 if (rc >= phba->cfg_fcp_io_channel)
5360 rc = 0;
5361 }
5362
James Smart912e3ac2011-05-24 11:42:11 -04005363 /*
5364 * Enable sr-iov virtual functions if supported and configured
5365 * through the module parameter.
5366 */
5367 if (phba->cfg_sriov_nr_virtfn > 0) {
5368 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
5369 phba->cfg_sriov_nr_virtfn);
5370 if (rc) {
5371 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5372 "3020 Requested number of SR-IOV "
5373 "virtual functions (%d) is not "
5374 "supported\n",
5375 phba->cfg_sriov_nr_virtfn);
5376 phba->cfg_sriov_nr_virtfn = 0;
5377 }
5378 }
5379
James Smart5248a742011-07-22 18:37:06 -04005380 return 0;
James Smartda0436e2009-05-22 14:51:39 -04005381
James Smart7bb03bb2013-04-17 20:19:16 -04005382out_free_msix:
5383 kfree(phba->sli4_hba.msix_entries);
James Smartda0436e2009-05-22 14:51:39 -04005384out_free_fcp_eq_hdl:
5385 kfree(phba->sli4_hba.fcp_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05005386out_free_fcf_rr_bmask:
5387 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04005388out_remove_rpi_hdrs:
5389 lpfc_sli4_remove_rpi_hdrs(phba);
5390out_free_active_sgl:
5391 lpfc_free_active_sgl(phba);
James Smartda0436e2009-05-22 14:51:39 -04005392out_destroy_cq_event_pool:
5393 lpfc_sli4_cq_event_pool_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04005394out_free_bsmbx:
5395 lpfc_destroy_bootstrap_mbox(phba);
5396out_free_mem:
5397 lpfc_mem_free(phba);
5398 return rc;
5399}
5400
5401/**
5402 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
5403 * @phba: pointer to lpfc hba data structure.
5404 *
5405 * This routine is invoked to unset the driver internal resources set up
5406 * specific for supporting the SLI-4 HBA device it attached to.
5407 **/
5408static void
5409lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
5410{
5411 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
5412
James Smart7bb03bb2013-04-17 20:19:16 -04005413 /* Free memory allocated for msi-x interrupt vector to CPU mapping */
5414 kfree(phba->sli4_hba.cpu_map);
5415 phba->sli4_hba.num_present_cpu = 0;
5416 phba->sli4_hba.num_online_cpu = 0;
James Smart76fd07a2014-02-20 09:57:18 -05005417 phba->sli4_hba.curr_disp_cpu = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04005418
James Smartda0436e2009-05-22 14:51:39 -04005419 /* Free memory allocated for msi-x interrupt vector entries */
5420 kfree(phba->sli4_hba.msix_entries);
5421
5422 /* Free memory allocated for fast-path work queue handles */
5423 kfree(phba->sli4_hba.fcp_eq_hdl);
5424
5425 /* Free the allocated rpi headers. */
5426 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04005427 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04005428
James Smart0c9ab6f2010-02-26 14:15:57 -05005429 /* Free eligible FCF index bmask */
5430 kfree(phba->fcf.fcf_rr_bmask);
5431
James Smartda0436e2009-05-22 14:51:39 -04005432 /* Free the ELS sgl list */
5433 lpfc_free_active_sgl(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04005434 lpfc_free_els_sgl_list(phba);
James Smartda0436e2009-05-22 14:51:39 -04005435
James Smartda0436e2009-05-22 14:51:39 -04005436 /* Free the completion queue EQ event pool */
5437 lpfc_sli4_cq_event_release_all(phba);
5438 lpfc_sli4_cq_event_pool_destroy(phba);
5439
James Smart6d368e52011-05-24 11:44:12 -04005440 /* Release resource identifiers. */
5441 lpfc_sli4_dealloc_resource_identifiers(phba);
5442
James Smartda0436e2009-05-22 14:51:39 -04005443 /* Free the bsmbx region. */
5444 lpfc_destroy_bootstrap_mbox(phba);
5445
5446 /* Free the SLI Layer memory with SLI4 HBAs */
5447 lpfc_mem_free_all(phba);
5448
5449 /* Free the current connect table */
5450 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04005451 &phba->fcf_conn_rec_list, list) {
5452 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04005453 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04005454 }
James Smartda0436e2009-05-22 14:51:39 -04005455
5456 return;
5457}
5458
5459/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005460 * lpfc_init_api_table_setup - Set up init api function jump table
James Smart3772a992009-05-22 14:50:54 -04005461 * @phba: The hba struct for which this call is being executed.
5462 * @dev_grp: The HBA PCI-Device group number.
5463 *
5464 * This routine sets up the device INIT interface API function jump table
5465 * in @phba struct.
5466 *
5467 * Returns: 0 - success, -ENODEV - failure.
5468 **/
5469int
5470lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5471{
James Smart84d1b002010-02-12 14:42:33 -05005472 phba->lpfc_hba_init_link = lpfc_hba_init_link;
5473 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart7f860592011-03-11 16:05:52 -05005474 phba->lpfc_selective_reset = lpfc_selective_reset;
James Smart3772a992009-05-22 14:50:54 -04005475 switch (dev_grp) {
5476 case LPFC_PCI_DEV_LP:
5477 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
5478 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
5479 phba->lpfc_stop_port = lpfc_stop_port_s3;
5480 break;
James Smartda0436e2009-05-22 14:51:39 -04005481 case LPFC_PCI_DEV_OC:
5482 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
5483 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
5484 phba->lpfc_stop_port = lpfc_stop_port_s4;
5485 break;
James Smart3772a992009-05-22 14:50:54 -04005486 default:
5487 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5488 "1431 Invalid HBA PCI-device group: 0x%x\n",
5489 dev_grp);
5490 return -ENODEV;
5491 break;
5492 }
5493 return 0;
5494}
5495
5496/**
5497 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
5498 * @phba: pointer to lpfc hba data structure.
5499 *
5500 * This routine is invoked to set up the driver internal resources before the
5501 * device specific resource setup to support the HBA device it attached to.
5502 *
5503 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005504 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005505 * other values - error
5506 **/
5507static int
5508lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
5509{
5510 /*
5511 * Driver resources common to all SLI revisions
5512 */
James Smartea2151b2008-09-07 11:52:10 -04005513 atomic_set(&phba->fast_event_count, 0);
James Smart2e0fef82007-06-17 19:56:36 -05005514 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005515
James Smarte47c9092008-02-08 18:49:26 -05005516 /* Initialize ndlp management spinlock */
5517 spin_lock_init(&phba->ndlp_lock);
5518
James Smart2e0fef82007-06-17 19:56:36 -05005519 INIT_LIST_HEAD(&phba->port_list);
James Smart3772a992009-05-22 14:50:54 -04005520 INIT_LIST_HEAD(&phba->work_list);
James Smart84774a42008-08-24 21:50:06 -04005521 init_waitqueue_head(&phba->wait_4_mlo_m_q);
dea31012005-04-17 16:05:31 -05005522
James Smart3772a992009-05-22 14:50:54 -04005523 /* Initialize the wait queue head for the kernel thread */
5524 init_waitqueue_head(&phba->work_waitq);
James Smart858c9f62007-06-17 19:56:39 -05005525
James Smart3772a992009-05-22 14:50:54 -04005526 /* Initialize the scsi buffer list used by driver for scsi IO */
James Smarta40fc5f2013-04-17 20:17:40 -04005527 spin_lock_init(&phba->scsi_buf_list_get_lock);
5528 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_get);
5529 spin_lock_init(&phba->scsi_buf_list_put_lock);
5530 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
dea31012005-04-17 16:05:31 -05005531
James Smart3772a992009-05-22 14:50:54 -04005532 /* Initialize the fabric iocb list */
5533 INIT_LIST_HEAD(&phba->fabric_iocb_list);
dea31012005-04-17 16:05:31 -05005534
James Smart3772a992009-05-22 14:50:54 -04005535 /* Initialize list to save ELS buffers */
5536 INIT_LIST_HEAD(&phba->elsbuf);
dea31012005-04-17 16:05:31 -05005537
James Smart3772a992009-05-22 14:50:54 -04005538 /* Initialize FCF connection rec list */
5539 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
dea31012005-04-17 16:05:31 -05005540
James Smart1ba981f2014-02-20 09:56:45 -05005541 /* Initialize OAS configuration list */
5542 spin_lock_init(&phba->devicelock);
5543 INIT_LIST_HEAD(&phba->luns);
5544
James Smart3772a992009-05-22 14:50:54 -04005545 return 0;
5546}
dea31012005-04-17 16:05:31 -05005547
James Smart3772a992009-05-22 14:50:54 -04005548/**
5549 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
5550 * @phba: pointer to lpfc hba data structure.
5551 *
5552 * This routine is invoked to set up the driver internal resources after the
5553 * device specific resource setup to support the HBA device it attached to.
5554 *
5555 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005556 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005557 * other values - error
5558 **/
5559static int
5560lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
5561{
5562 int error;
5563
5564 /* Startup the kernel thread for this host adapter. */
5565 phba->worker_thread = kthread_run(lpfc_do_work, phba,
5566 "lpfc_worker_%d", phba->brd_no);
5567 if (IS_ERR(phba->worker_thread)) {
5568 error = PTR_ERR(phba->worker_thread);
5569 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005570 }
5571
James Smart3772a992009-05-22 14:50:54 -04005572 return 0;
5573}
5574
5575/**
5576 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
5577 * @phba: pointer to lpfc hba data structure.
5578 *
5579 * This routine is invoked to unset the driver internal resources set up after
5580 * the device specific resource setup for supporting the HBA device it
5581 * attached to.
5582 **/
5583static void
5584lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
5585{
5586 /* Stop kernel worker thread */
5587 kthread_stop(phba->worker_thread);
5588}
5589
5590/**
5591 * lpfc_free_iocb_list - Free iocb list.
5592 * @phba: pointer to lpfc hba data structure.
5593 *
5594 * This routine is invoked to free the driver's IOCB list and memory.
5595 **/
5596static void
5597lpfc_free_iocb_list(struct lpfc_hba *phba)
5598{
5599 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
5600
5601 spin_lock_irq(&phba->hbalock);
5602 list_for_each_entry_safe(iocbq_entry, iocbq_next,
5603 &phba->lpfc_iocb_list, list) {
5604 list_del(&iocbq_entry->list);
5605 kfree(iocbq_entry);
5606 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005607 }
James Smart3772a992009-05-22 14:50:54 -04005608 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005609
James Smart3772a992009-05-22 14:50:54 -04005610 return;
5611}
dea31012005-04-17 16:05:31 -05005612
James Smart3772a992009-05-22 14:50:54 -04005613/**
5614 * lpfc_init_iocb_list - Allocate and initialize iocb list.
5615 * @phba: pointer to lpfc hba data structure.
5616 *
5617 * This routine is invoked to allocate and initizlize the driver's IOCB
5618 * list and set up the IOCB tag array accordingly.
5619 *
5620 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005621 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005622 * other values - error
5623 **/
5624static int
5625lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
5626{
5627 struct lpfc_iocbq *iocbq_entry = NULL;
5628 uint16_t iotag;
5629 int i;
dea31012005-04-17 16:05:31 -05005630
5631 /* Initialize and populate the iocb list per host. */
5632 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04005633 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07005634 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05005635 if (iocbq_entry == NULL) {
5636 printk(KERN_ERR "%s: only allocated %d iocbs of "
5637 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07005638 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05005639 goto out_free_iocbq;
5640 }
5641
James Bottomley604a3e32005-10-29 10:28:33 -05005642 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
5643 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04005644 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05005645 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04005646 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05005647 goto out_free_iocbq;
5648 }
James Smart6d368e52011-05-24 11:44:12 -04005649 iocbq_entry->sli4_lxritag = NO_XRI;
James Smart3772a992009-05-22 14:50:54 -04005650 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05005651
5652 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005653 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
5654 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05005655 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005656 }
5657
James Smart3772a992009-05-22 14:50:54 -04005658 return 0;
5659
5660out_free_iocbq:
5661 lpfc_free_iocb_list(phba);
5662
5663 return -ENOMEM;
5664}
5665
5666/**
James Smart8a9d2e82012-05-09 21:16:12 -04005667 * lpfc_free_sgl_list - Free a given sgl list.
James Smartda0436e2009-05-22 14:51:39 -04005668 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -04005669 * @sglq_list: pointer to the head of sgl list.
James Smartda0436e2009-05-22 14:51:39 -04005670 *
James Smart8a9d2e82012-05-09 21:16:12 -04005671 * This routine is invoked to free a give sgl list and memory.
James Smartda0436e2009-05-22 14:51:39 -04005672 **/
James Smart8a9d2e82012-05-09 21:16:12 -04005673void
5674lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
James Smartda0436e2009-05-22 14:51:39 -04005675{
5676 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart8a9d2e82012-05-09 21:16:12 -04005677
5678 list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
5679 list_del(&sglq_entry->list);
5680 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
5681 kfree(sglq_entry);
5682 }
5683}
5684
5685/**
5686 * lpfc_free_els_sgl_list - Free els sgl list.
5687 * @phba: pointer to lpfc hba data structure.
5688 *
5689 * This routine is invoked to free the driver's els sgl list and memory.
5690 **/
5691static void
5692lpfc_free_els_sgl_list(struct lpfc_hba *phba)
5693{
James Smartda0436e2009-05-22 14:51:39 -04005694 LIST_HEAD(sglq_list);
James Smartdafe8ce2014-09-03 12:56:40 -04005695 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smartda0436e2009-05-22 14:51:39 -04005696
James Smart8a9d2e82012-05-09 21:16:12 -04005697 /* Retrieve all els sgls from driver list */
James Smartda0436e2009-05-22 14:51:39 -04005698 spin_lock_irq(&phba->hbalock);
James Smartdafe8ce2014-09-03 12:56:40 -04005699 spin_lock(&pring->ring_lock);
James Smartda0436e2009-05-22 14:51:39 -04005700 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
James Smartdafe8ce2014-09-03 12:56:40 -04005701 spin_unlock(&pring->ring_lock);
James Smartda0436e2009-05-22 14:51:39 -04005702 spin_unlock_irq(&phba->hbalock);
5703
James Smart8a9d2e82012-05-09 21:16:12 -04005704 /* Now free the sgl list */
5705 lpfc_free_sgl_list(phba, &sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04005706}
5707
5708/**
5709 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
5710 * @phba: pointer to lpfc hba data structure.
5711 *
5712 * This routine is invoked to allocate the driver's active sgl memory.
5713 * This array will hold the sglq_entry's for active IOs.
5714 **/
5715static int
5716lpfc_init_active_sgl_array(struct lpfc_hba *phba)
5717{
5718 int size;
5719 size = sizeof(struct lpfc_sglq *);
5720 size *= phba->sli4_hba.max_cfg_param.max_xri;
5721
5722 phba->sli4_hba.lpfc_sglq_active_list =
5723 kzalloc(size, GFP_KERNEL);
5724 if (!phba->sli4_hba.lpfc_sglq_active_list)
5725 return -ENOMEM;
5726 return 0;
5727}
5728
5729/**
5730 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
5731 * @phba: pointer to lpfc hba data structure.
5732 *
5733 * This routine is invoked to walk through the array of active sglq entries
5734 * and free all of the resources.
5735 * This is just a place holder for now.
5736 **/
5737static void
5738lpfc_free_active_sgl(struct lpfc_hba *phba)
5739{
5740 kfree(phba->sli4_hba.lpfc_sglq_active_list);
5741}
5742
5743/**
5744 * lpfc_init_sgl_list - Allocate and initialize sgl list.
5745 * @phba: pointer to lpfc hba data structure.
5746 *
5747 * This routine is invoked to allocate and initizlize the driver's sgl
5748 * list and set up the sgl xritag tag array accordingly.
5749 *
James Smartda0436e2009-05-22 14:51:39 -04005750 **/
James Smart8a9d2e82012-05-09 21:16:12 -04005751static void
James Smartda0436e2009-05-22 14:51:39 -04005752lpfc_init_sgl_list(struct lpfc_hba *phba)
5753{
James Smartda0436e2009-05-22 14:51:39 -04005754 /* Initialize and populate the sglq list per host/VF. */
5755 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
5756 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
5757
James Smart8a9d2e82012-05-09 21:16:12 -04005758 /* els xri-sgl book keeping */
5759 phba->sli4_hba.els_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04005760
James Smart8a9d2e82012-05-09 21:16:12 -04005761 /* scsi xri-buffer book keeping */
James Smartda0436e2009-05-22 14:51:39 -04005762 phba->sli4_hba.scsi_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04005763}
5764
5765/**
5766 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
5767 * @phba: pointer to lpfc hba data structure.
5768 *
5769 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -04005770 * port for those SLI4 ports that do not support extents. This routine
James Smartda0436e2009-05-22 14:51:39 -04005771 * posts a PAGE_SIZE memory region to the port to hold up to
James Smart88a2cfb2011-07-22 18:36:33 -04005772 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
5773 * and should be called only when interrupts are disabled.
James Smartda0436e2009-05-22 14:51:39 -04005774 *
5775 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005776 * 0 - successful
James Smart88a2cfb2011-07-22 18:36:33 -04005777 * -ERROR - otherwise.
James Smartda0436e2009-05-22 14:51:39 -04005778 **/
5779int
5780lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
5781{
5782 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04005783 struct lpfc_rpi_hdr *rpi_hdr;
5784
5785 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
James Smartff78d8f2011-12-13 13:21:35 -05005786 if (!phba->sli4_hba.rpi_hdrs_in_use)
James Smart6d368e52011-05-24 11:44:12 -04005787 return rc;
James Smart6d368e52011-05-24 11:44:12 -04005788 if (phba->sli4_hba.extents_in_use)
5789 return -EIO;
James Smartda0436e2009-05-22 14:51:39 -04005790
5791 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
5792 if (!rpi_hdr) {
5793 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5794 "0391 Error during rpi post operation\n");
5795 lpfc_sli4_remove_rpis(phba);
5796 rc = -ENODEV;
5797 }
5798
5799 return rc;
5800}
5801
5802/**
5803 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
5804 * @phba: pointer to lpfc hba data structure.
5805 *
5806 * This routine is invoked to allocate a single 4KB memory region to
5807 * support rpis and stores them in the phba. This single region
5808 * provides support for up to 64 rpis. The region is used globally
5809 * by the device.
5810 *
5811 * Returns:
5812 * A valid rpi hdr on success.
5813 * A NULL pointer on any failure.
5814 **/
5815struct lpfc_rpi_hdr *
5816lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
5817{
5818 uint16_t rpi_limit, curr_rpi_range;
5819 struct lpfc_dmabuf *dmabuf;
5820 struct lpfc_rpi_hdr *rpi_hdr;
James Smart9589b0622011-04-16 11:03:17 -04005821 uint32_t rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04005822
James Smart6d368e52011-05-24 11:44:12 -04005823 /*
5824 * If the SLI4 port supports extents, posting the rpi header isn't
5825 * required. Set the expected maximum count and let the actual value
5826 * get set when extents are fully allocated.
5827 */
5828 if (!phba->sli4_hba.rpi_hdrs_in_use)
5829 return NULL;
5830 if (phba->sli4_hba.extents_in_use)
5831 return NULL;
5832
5833 /* The limit on the logical index is just the max_rpi count. */
James Smartda0436e2009-05-22 14:51:39 -04005834 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
James Smart6d368e52011-05-24 11:44:12 -04005835 phba->sli4_hba.max_cfg_param.max_rpi - 1;
James Smartda0436e2009-05-22 14:51:39 -04005836
5837 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04005838 /*
5839 * Establish the starting RPI in this header block. The starting
5840 * rpi is normalized to a zero base because the physical rpi is
5841 * port based.
5842 */
James Smart97f2ecf2012-03-01 22:35:23 -05005843 curr_rpi_range = phba->sli4_hba.next_rpi;
James Smartda0436e2009-05-22 14:51:39 -04005844 spin_unlock_irq(&phba->hbalock);
5845
5846 /*
5847 * The port has a limited number of rpis. The increment here
5848 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
5849 * and to allow the full max_rpi range per port.
5850 */
5851 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
James Smart9589b0622011-04-16 11:03:17 -04005852 rpi_count = rpi_limit - curr_rpi_range;
5853 else
5854 rpi_count = LPFC_RPI_HDR_COUNT;
James Smartda0436e2009-05-22 14:51:39 -04005855
James Smart6d368e52011-05-24 11:44:12 -04005856 if (!rpi_count)
5857 return NULL;
James Smartda0436e2009-05-22 14:51:39 -04005858 /*
5859 * First allocate the protocol header region for the port. The
5860 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
5861 */
5862 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5863 if (!dmabuf)
5864 return NULL;
5865
Joe Perches1aee3832014-09-03 12:56:12 -04005866 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
5867 LPFC_HDR_TEMPLATE_SIZE,
5868 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005869 if (!dmabuf->virt) {
5870 rpi_hdr = NULL;
5871 goto err_free_dmabuf;
5872 }
5873
James Smartda0436e2009-05-22 14:51:39 -04005874 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
5875 rpi_hdr = NULL;
5876 goto err_free_coherent;
5877 }
5878
5879 /* Save the rpi header data for cleanup later. */
5880 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
5881 if (!rpi_hdr)
5882 goto err_free_coherent;
5883
5884 rpi_hdr->dmabuf = dmabuf;
5885 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
5886 rpi_hdr->page_count = 1;
5887 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04005888
5889 /* The rpi_hdr stores the logical index only. */
5890 rpi_hdr->start_rpi = curr_rpi_range;
James Smartda0436e2009-05-22 14:51:39 -04005891 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
5892
5893 /*
James Smart6d368e52011-05-24 11:44:12 -04005894 * The next_rpi stores the next logical module-64 rpi value used
5895 * to post physical rpis in subsequent rpi postings.
James Smartda0436e2009-05-22 14:51:39 -04005896 */
James Smart9589b0622011-04-16 11:03:17 -04005897 phba->sli4_hba.next_rpi += rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04005898 spin_unlock_irq(&phba->hbalock);
5899 return rpi_hdr;
5900
5901 err_free_coherent:
5902 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
5903 dmabuf->virt, dmabuf->phys);
5904 err_free_dmabuf:
5905 kfree(dmabuf);
5906 return NULL;
5907}
5908
5909/**
5910 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
5911 * @phba: pointer to lpfc hba data structure.
5912 *
5913 * This routine is invoked to remove all memory resources allocated
James Smart6d368e52011-05-24 11:44:12 -04005914 * to support rpis for SLI4 ports not supporting extents. This routine
5915 * presumes the caller has released all rpis consumed by fabric or port
5916 * logins and is prepared to have the header pages removed.
James Smartda0436e2009-05-22 14:51:39 -04005917 **/
5918void
5919lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
5920{
5921 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
5922
James Smart6d368e52011-05-24 11:44:12 -04005923 if (!phba->sli4_hba.rpi_hdrs_in_use)
5924 goto exit;
5925
James Smartda0436e2009-05-22 14:51:39 -04005926 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
5927 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
5928 list_del(&rpi_hdr->list);
5929 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
5930 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
5931 kfree(rpi_hdr->dmabuf);
5932 kfree(rpi_hdr);
5933 }
James Smart6d368e52011-05-24 11:44:12 -04005934 exit:
5935 /* There are no rpis available to the port now. */
5936 phba->sli4_hba.next_rpi = 0;
James Smartda0436e2009-05-22 14:51:39 -04005937}
5938
5939/**
James Smart3772a992009-05-22 14:50:54 -04005940 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
5941 * @pdev: pointer to pci device data structure.
5942 *
5943 * This routine is invoked to allocate the driver hba data structure for an
5944 * HBA device. If the allocation is successful, the phba reference to the
5945 * PCI device data structure is set.
5946 *
5947 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005948 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04005949 * NULL - error
5950 **/
5951static struct lpfc_hba *
5952lpfc_hba_alloc(struct pci_dev *pdev)
5953{
5954 struct lpfc_hba *phba;
5955
5956 /* Allocate memory for HBA structure */
5957 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
5958 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02005959 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04005960 return NULL;
5961 }
5962
5963 /* Set reference to PCI device in HBA structure */
5964 phba->pcidev = pdev;
5965
5966 /* Assign an unused board number */
5967 phba->brd_no = lpfc_get_instance();
5968 if (phba->brd_no < 0) {
5969 kfree(phba);
5970 return NULL;
5971 }
5972
James Smart4fede782010-01-26 23:08:55 -05005973 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04005974 INIT_LIST_HEAD(&phba->ct_ev_waiters);
5975
James Smart3772a992009-05-22 14:50:54 -04005976 return phba;
5977}
5978
5979/**
5980 * lpfc_hba_free - Free driver hba data structure with a device.
5981 * @phba: pointer to lpfc hba data structure.
5982 *
5983 * This routine is invoked to free the driver hba data structure with an
5984 * HBA device.
5985 **/
5986static void
5987lpfc_hba_free(struct lpfc_hba *phba)
5988{
5989 /* Release the driver assigned board number */
5990 idr_remove(&lpfc_hba_index, phba->brd_no);
5991
James Smart2a76a282012-08-03 12:35:54 -04005992 /* Free memory allocated with sli rings */
5993 kfree(phba->sli.ring);
5994 phba->sli.ring = NULL;
5995
James Smart3772a992009-05-22 14:50:54 -04005996 kfree(phba);
5997 return;
5998}
5999
6000/**
6001 * lpfc_create_shost - Create hba physical port with associated scsi host.
6002 * @phba: pointer to lpfc hba data structure.
6003 *
6004 * This routine is invoked to create HBA physical port and associate a SCSI
6005 * host with it.
6006 *
6007 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006008 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006009 * other values - error
6010 **/
6011static int
6012lpfc_create_shost(struct lpfc_hba *phba)
6013{
6014 struct lpfc_vport *vport;
6015 struct Scsi_Host *shost;
6016
6017 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05006018 phba->fc_edtov = FF_DEF_EDTOV;
6019 phba->fc_ratov = FF_DEF_RATOV;
6020 phba->fc_altov = FF_DEF_ALTOV;
6021 phba->fc_arbtov = FF_DEF_ARBTOV;
6022
James Smartd7c47992010-06-08 18:31:54 -04006023 atomic_set(&phba->sdev_cnt, 0);
James Smart3de2a652007-08-02 11:09:59 -04006024 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05006025 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04006026 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05006027
6028 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05006029 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05006030 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04006031 /* Put reference to SCSI host to driver's device private data */
6032 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05006033
James Smart3772a992009-05-22 14:50:54 -04006034 return 0;
6035}
dea31012005-04-17 16:05:31 -05006036
James Smart3772a992009-05-22 14:50:54 -04006037/**
6038 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
6039 * @phba: pointer to lpfc hba data structure.
6040 *
6041 * This routine is invoked to destroy HBA physical port and the associated
6042 * SCSI host.
6043 **/
6044static void
6045lpfc_destroy_shost(struct lpfc_hba *phba)
6046{
6047 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04006048
James Smart3772a992009-05-22 14:50:54 -04006049 /* Destroy physical port that associated with the SCSI host */
6050 destroy_port(vport);
6051
6052 return;
6053}
6054
6055/**
6056 * lpfc_setup_bg - Setup Block guard structures and debug areas.
6057 * @phba: pointer to lpfc hba data structure.
6058 * @shost: the shost to be used to detect Block guard settings.
6059 *
6060 * This routine sets up the local Block guard protocol settings for @shost.
6061 * This routine also allocates memory for debugging bg buffers.
6062 **/
6063static void
6064lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
6065{
James Smartbbeb79b2012-06-12 13:54:27 -04006066 uint32_t old_mask;
6067 uint32_t old_guard;
6068
James Smart3772a992009-05-22 14:50:54 -04006069 int pagecnt = 10;
6070 if (lpfc_prot_mask && lpfc_prot_guard) {
6071 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6072 "1478 Registering BlockGuard with the "
6073 "SCSI layer\n");
James Smartbbeb79b2012-06-12 13:54:27 -04006074
6075 old_mask = lpfc_prot_mask;
6076 old_guard = lpfc_prot_guard;
6077
6078 /* Only allow supported values */
6079 lpfc_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
6080 SHOST_DIX_TYPE0_PROTECTION |
6081 SHOST_DIX_TYPE1_PROTECTION);
6082 lpfc_prot_guard &= (SHOST_DIX_GUARD_IP | SHOST_DIX_GUARD_CRC);
6083
6084 /* DIF Type 1 protection for profiles AST1/C1 is end to end */
6085 if (lpfc_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
6086 lpfc_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
6087
6088 if (lpfc_prot_mask && lpfc_prot_guard) {
6089 if ((old_mask != lpfc_prot_mask) ||
6090 (old_guard != lpfc_prot_guard))
6091 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6092 "1475 Registering BlockGuard with the "
6093 "SCSI layer: mask %d guard %d\n",
6094 lpfc_prot_mask, lpfc_prot_guard);
6095
6096 scsi_host_set_prot(shost, lpfc_prot_mask);
6097 scsi_host_set_guard(shost, lpfc_prot_guard);
6098 } else
6099 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6100 "1479 Not Registering BlockGuard with the SCSI "
6101 "layer, Bad protection parameters: %d %d\n",
6102 old_mask, old_guard);
James Smart98c9ea52007-10-27 13:37:33 -04006103 }
James Smartbbeb79b2012-06-12 13:54:27 -04006104
James Smart81301a92008-12-04 22:39:46 -05006105 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05006106 while (pagecnt) {
6107 spin_lock_init(&_dump_buf_lock);
6108 _dump_buf_data =
6109 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
6110 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04006111 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6112 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04006113 "_dump_buf_data at 0x%p\n",
6114 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05006115 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04006116 memset(_dump_buf_data, 0,
6117 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05006118 break;
James Smart3772a992009-05-22 14:50:54 -04006119 } else
James Smart81301a92008-12-04 22:39:46 -05006120 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05006121 }
James Smart81301a92008-12-04 22:39:46 -05006122 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04006123 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6124 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04006125 "memory for hexdump\n");
6126 } else
James Smart6a9c52c2009-10-02 15:16:51 -04006127 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6128 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05006129 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05006130 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05006131 while (pagecnt) {
6132 _dump_buf_dif =
6133 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
6134 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04006135 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6136 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04006137 "_dump_buf_dif at 0x%p\n",
6138 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05006139 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04006140 memset(_dump_buf_dif, 0,
6141 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05006142 break;
James Smart3772a992009-05-22 14:50:54 -04006143 } else
James Smart81301a92008-12-04 22:39:46 -05006144 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05006145 }
James Smart81301a92008-12-04 22:39:46 -05006146 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04006147 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6148 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04006149 "memory for hexdump\n");
6150 } else
James Smart6a9c52c2009-10-02 15:16:51 -04006151 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6152 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04006153 _dump_buf_dif);
6154}
6155
6156/**
6157 * lpfc_post_init_setup - Perform necessary device post initialization setup.
6158 * @phba: pointer to lpfc hba data structure.
6159 *
6160 * This routine is invoked to perform all the necessary post initialization
6161 * setup for the device.
6162 **/
6163static void
6164lpfc_post_init_setup(struct lpfc_hba *phba)
6165{
6166 struct Scsi_Host *shost;
6167 struct lpfc_adapter_event_header adapter_event;
6168
6169 /* Get the default values for Model Name and Description */
6170 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
6171
6172 /*
6173 * hba setup may have changed the hba_queue_depth so we need to
6174 * adjust the value of can_queue.
6175 */
6176 shost = pci_get_drvdata(phba->pcidev);
6177 shost->can_queue = phba->cfg_hba_queue_depth - 10;
6178 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
6179 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05006180
6181 lpfc_host_attrib_init(shost);
6182
James Smart2e0fef82007-06-17 19:56:36 -05006183 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
6184 spin_lock_irq(shost->host_lock);
6185 lpfc_poll_start_timer(phba);
6186 spin_unlock_irq(shost->host_lock);
6187 }
James Smart8f6d98d2006-08-01 07:34:00 -04006188
James Smart93996272008-08-24 21:50:30 -04006189 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6190 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04006191 /* Send board arrival event to upper layer */
6192 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
6193 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
6194 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04006195 sizeof(adapter_event),
6196 (char *) &adapter_event,
6197 LPFC_NL_VENDOR_ID);
6198 return;
6199}
6200
6201/**
6202 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
6203 * @phba: pointer to lpfc hba data structure.
6204 *
6205 * This routine is invoked to set up the PCI device memory space for device
6206 * with SLI-3 interface spec.
6207 *
6208 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006209 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006210 * other values - error
6211 **/
6212static int
6213lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
6214{
6215 struct pci_dev *pdev;
6216 unsigned long bar0map_len, bar2map_len;
6217 int i, hbq_count;
6218 void *ptr;
6219 int error = -ENODEV;
6220
6221 /* Obtain PCI device reference */
6222 if (!phba->pcidev)
6223 return error;
6224 else
6225 pdev = phba->pcidev;
6226
6227 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05006228 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
6229 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
6230 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
6231 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smart3772a992009-05-22 14:50:54 -04006232 return error;
Michael Reed8e685972009-09-18 12:02:05 -05006233 }
6234 }
James Smart3772a992009-05-22 14:50:54 -04006235
6236 /* Get the bus address of Bar0 and Bar2 and the number of bytes
6237 * required by each mapping.
6238 */
6239 phba->pci_bar0_map = pci_resource_start(pdev, 0);
6240 bar0map_len = pci_resource_len(pdev, 0);
6241
6242 phba->pci_bar2_map = pci_resource_start(pdev, 2);
6243 bar2map_len = pci_resource_len(pdev, 2);
6244
6245 /* Map HBA SLIM to a kernel virtual address. */
6246 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
6247 if (!phba->slim_memmap_p) {
6248 dev_printk(KERN_ERR, &pdev->dev,
6249 "ioremap failed for SLIM memory.\n");
6250 goto out;
6251 }
6252
6253 /* Map HBA Control Registers to a kernel virtual address. */
6254 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
6255 if (!phba->ctrl_regs_memmap_p) {
6256 dev_printk(KERN_ERR, &pdev->dev,
6257 "ioremap failed for HBA control registers.\n");
6258 goto out_iounmap_slim;
6259 }
6260
6261 /* Allocate memory for SLI-2 structures */
Joe Perches1aee3832014-09-03 12:56:12 -04006262 phba->slim2p.virt = dma_zalloc_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6263 &phba->slim2p.phys, GFP_KERNEL);
James Smart3772a992009-05-22 14:50:54 -04006264 if (!phba->slim2p.virt)
6265 goto out_iounmap;
6266
James Smart3772a992009-05-22 14:50:54 -04006267 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04006268 phba->mbox_ext = (phba->slim2p.virt +
6269 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04006270 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
6271 phba->IOCBs = (phba->slim2p.virt +
6272 offsetof(struct lpfc_sli2_slim, IOCBs));
6273
6274 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
6275 lpfc_sli_hbq_size(),
6276 &phba->hbqslimp.phys,
6277 GFP_KERNEL);
6278 if (!phba->hbqslimp.virt)
6279 goto out_free_slim;
6280
6281 hbq_count = lpfc_sli_hbq_count();
6282 ptr = phba->hbqslimp.virt;
6283 for (i = 0; i < hbq_count; ++i) {
6284 phba->hbqs[i].hbq_virt = ptr;
6285 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
6286 ptr += (lpfc_hbq_defs[i]->entry_count *
6287 sizeof(struct lpfc_hbq_entry));
6288 }
6289 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
6290 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
6291
6292 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
6293
6294 INIT_LIST_HEAD(&phba->rb_pend_list);
6295
6296 phba->MBslimaddr = phba->slim_memmap_p;
6297 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
6298 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
6299 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
6300 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04006301
dea31012005-04-17 16:05:31 -05006302 return 0;
6303
dea31012005-04-17 16:05:31 -05006304out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04006305 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6306 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05006307out_iounmap:
6308 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05006309out_iounmap_slim:
dea31012005-04-17 16:05:31 -05006310 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05006311out:
6312 return error;
6313}
6314
James Smarte59058c2008-08-24 21:49:00 -04006315/**
James Smart3772a992009-05-22 14:50:54 -04006316 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
6317 * @phba: pointer to lpfc hba data structure.
6318 *
6319 * This routine is invoked to unset the PCI device memory space for device
6320 * with SLI-3 interface spec.
6321 **/
6322static void
6323lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
6324{
6325 struct pci_dev *pdev;
6326
6327 /* Obtain PCI device reference */
6328 if (!phba->pcidev)
6329 return;
6330 else
6331 pdev = phba->pcidev;
6332
6333 /* Free coherent DMA memory allocated */
6334 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
6335 phba->hbqslimp.virt, phba->hbqslimp.phys);
6336 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6337 phba->slim2p.virt, phba->slim2p.phys);
6338
6339 /* I/O memory unmap */
6340 iounmap(phba->ctrl_regs_memmap_p);
6341 iounmap(phba->slim_memmap_p);
6342
6343 return;
6344}
6345
6346/**
James Smartda0436e2009-05-22 14:51:39 -04006347 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
6348 * @phba: pointer to lpfc hba data structure.
6349 *
6350 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
6351 * done and check status.
6352 *
6353 * Return 0 if successful, otherwise -ENODEV.
6354 **/
6355int
6356lpfc_sli4_post_status_check(struct lpfc_hba *phba)
6357{
James Smart2fcee4b2010-12-15 17:57:46 -05006358 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
6359 struct lpfc_register reg_data;
6360 int i, port_error = 0;
6361 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04006362
James Smart9940b972011-03-11 16:06:12 -05006363 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
6364 memset(&reg_data, 0, sizeof(reg_data));
James Smart2fcee4b2010-12-15 17:57:46 -05006365 if (!phba->sli4_hba.PSMPHRregaddr)
James Smartda0436e2009-05-22 14:51:39 -04006366 return -ENODEV;
6367
James Smartda0436e2009-05-22 14:51:39 -04006368 /* Wait up to 30 seconds for the SLI Port POST done and ready */
6369 for (i = 0; i < 3000; i++) {
James Smart9940b972011-03-11 16:06:12 -05006370 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
6371 &portsmphr_reg.word0) ||
6372 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
James Smart2fcee4b2010-12-15 17:57:46 -05006373 /* Port has a fatal POST error, break out */
James Smartda0436e2009-05-22 14:51:39 -04006374 port_error = -ENODEV;
6375 break;
6376 }
James Smart2fcee4b2010-12-15 17:57:46 -05006377 if (LPFC_POST_STAGE_PORT_READY ==
6378 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
James Smartda0436e2009-05-22 14:51:39 -04006379 break;
James Smartda0436e2009-05-22 14:51:39 -04006380 msleep(10);
6381 }
6382
James Smart2fcee4b2010-12-15 17:57:46 -05006383 /*
6384 * If there was a port error during POST, then don't proceed with
6385 * other register reads as the data may not be valid. Just exit.
6386 */
6387 if (port_error) {
James Smartda0436e2009-05-22 14:51:39 -04006388 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05006389 "1408 Port Failed POST - portsmphr=0x%x, "
6390 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
6391 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
6392 portsmphr_reg.word0,
6393 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
6394 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
6395 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
6396 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
6397 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
6398 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
6399 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
6400 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
6401 } else {
James Smart28baac72010-02-12 14:42:03 -05006402 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05006403 "2534 Device Info: SLIFamily=0x%x, "
6404 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
6405 "SLIHint_2=0x%x, FT=0x%x\n",
James Smart28baac72010-02-12 14:42:03 -05006406 bf_get(lpfc_sli_intf_sli_family,
6407 &phba->sli4_hba.sli_intf),
6408 bf_get(lpfc_sli_intf_slirev,
6409 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05006410 bf_get(lpfc_sli_intf_if_type,
James Smart28baac72010-02-12 14:42:03 -05006411 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05006412 bf_get(lpfc_sli_intf_sli_hint1,
6413 &phba->sli4_hba.sli_intf),
6414 bf_get(lpfc_sli_intf_sli_hint2,
6415 &phba->sli4_hba.sli_intf),
6416 bf_get(lpfc_sli_intf_func_type,
James Smart28baac72010-02-12 14:42:03 -05006417 &phba->sli4_hba.sli_intf));
James Smart2fcee4b2010-12-15 17:57:46 -05006418 /*
6419 * Check for other Port errors during the initialization
6420 * process. Fail the load if the port did not come up
6421 * correctly.
6422 */
6423 if_type = bf_get(lpfc_sli_intf_if_type,
6424 &phba->sli4_hba.sli_intf);
6425 switch (if_type) {
6426 case LPFC_SLI_INTF_IF_TYPE_0:
6427 phba->sli4_hba.ue_mask_lo =
6428 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
6429 phba->sli4_hba.ue_mask_hi =
6430 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
6431 uerrlo_reg.word0 =
6432 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
6433 uerrhi_reg.word0 =
6434 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
6435 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
6436 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
6437 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6438 "1422 Unrecoverable Error "
6439 "Detected during POST "
6440 "uerr_lo_reg=0x%x, "
6441 "uerr_hi_reg=0x%x, "
6442 "ue_mask_lo_reg=0x%x, "
6443 "ue_mask_hi_reg=0x%x\n",
6444 uerrlo_reg.word0,
6445 uerrhi_reg.word0,
6446 phba->sli4_hba.ue_mask_lo,
6447 phba->sli4_hba.ue_mask_hi);
6448 port_error = -ENODEV;
6449 }
6450 break;
6451 case LPFC_SLI_INTF_IF_TYPE_2:
6452 /* Final checks. The port status should be clean. */
James Smart9940b972011-03-11 16:06:12 -05006453 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
6454 &reg_data.word0) ||
James Smart05580562011-05-24 11:40:48 -04006455 (bf_get(lpfc_sliport_status_err, &reg_data) &&
6456 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
James Smart2fcee4b2010-12-15 17:57:46 -05006457 phba->work_status[0] =
6458 readl(phba->sli4_hba.u.if_type2.
6459 ERR1regaddr);
6460 phba->work_status[1] =
6461 readl(phba->sli4_hba.u.if_type2.
6462 ERR2regaddr);
6463 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05006464 "2888 Unrecoverable port error "
6465 "following POST: port status reg "
6466 "0x%x, port_smphr reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05006467 "error 1=0x%x, error 2=0x%x\n",
6468 reg_data.word0,
6469 portsmphr_reg.word0,
6470 phba->work_status[0],
6471 phba->work_status[1]);
6472 port_error = -ENODEV;
6473 }
6474 break;
6475 case LPFC_SLI_INTF_IF_TYPE_1:
6476 default:
6477 break;
6478 }
James Smart28baac72010-02-12 14:42:03 -05006479 }
James Smartda0436e2009-05-22 14:51:39 -04006480 return port_error;
6481}
6482
6483/**
6484 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
6485 * @phba: pointer to lpfc hba data structure.
James Smart2fcee4b2010-12-15 17:57:46 -05006486 * @if_type: The SLI4 interface type getting configured.
James Smartda0436e2009-05-22 14:51:39 -04006487 *
6488 * This routine is invoked to set up SLI4 BAR0 PCI config space register
6489 * memory map.
6490 **/
6491static void
James Smart2fcee4b2010-12-15 17:57:46 -05006492lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04006493{
James Smart2fcee4b2010-12-15 17:57:46 -05006494 switch (if_type) {
6495 case LPFC_SLI_INTF_IF_TYPE_0:
6496 phba->sli4_hba.u.if_type0.UERRLOregaddr =
6497 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
6498 phba->sli4_hba.u.if_type0.UERRHIregaddr =
6499 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
6500 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
6501 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
6502 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
6503 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
6504 phba->sli4_hba.SLIINTFregaddr =
6505 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
6506 break;
6507 case LPFC_SLI_INTF_IF_TYPE_2:
6508 phba->sli4_hba.u.if_type2.ERR1regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006509 phba->sli4_hba.conf_regs_memmap_p +
6510 LPFC_CTL_PORT_ER1_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006511 phba->sli4_hba.u.if_type2.ERR2regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006512 phba->sli4_hba.conf_regs_memmap_p +
6513 LPFC_CTL_PORT_ER2_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006514 phba->sli4_hba.u.if_type2.CTRLregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006515 phba->sli4_hba.conf_regs_memmap_p +
6516 LPFC_CTL_PORT_CTL_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006517 phba->sli4_hba.u.if_type2.STATUSregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006518 phba->sli4_hba.conf_regs_memmap_p +
6519 LPFC_CTL_PORT_STA_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006520 phba->sli4_hba.SLIINTFregaddr =
6521 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
6522 phba->sli4_hba.PSMPHRregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006523 phba->sli4_hba.conf_regs_memmap_p +
6524 LPFC_CTL_PORT_SEM_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006525 phba->sli4_hba.RQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05006526 phba->sli4_hba.conf_regs_memmap_p +
6527 LPFC_ULP0_RQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05006528 phba->sli4_hba.WQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05006529 phba->sli4_hba.conf_regs_memmap_p +
6530 LPFC_ULP0_WQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05006531 phba->sli4_hba.EQCQDBregaddr =
6532 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
6533 phba->sli4_hba.MQDBregaddr =
6534 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
6535 phba->sli4_hba.BMBXregaddr =
6536 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
6537 break;
6538 case LPFC_SLI_INTF_IF_TYPE_1:
6539 default:
6540 dev_printk(KERN_ERR, &phba->pcidev->dev,
6541 "FATAL - unsupported SLI4 interface type - %d\n",
6542 if_type);
6543 break;
6544 }
James Smartda0436e2009-05-22 14:51:39 -04006545}
6546
6547/**
6548 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
6549 * @phba: pointer to lpfc hba data structure.
6550 *
6551 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
6552 * memory map.
6553 **/
6554static void
6555lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
6556{
James Smart2fcee4b2010-12-15 17:57:46 -05006557 phba->sli4_hba.PSMPHRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
6558 LPFC_SLIPORT_IF0_SMPHR;
James Smartda0436e2009-05-22 14:51:39 -04006559 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006560 LPFC_HST_ISR0;
James Smartda0436e2009-05-22 14:51:39 -04006561 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006562 LPFC_HST_IMR0;
James Smartda0436e2009-05-22 14:51:39 -04006563 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006564 LPFC_HST_ISCR0;
James Smartda0436e2009-05-22 14:51:39 -04006565}
6566
6567/**
6568 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
6569 * @phba: pointer to lpfc hba data structure.
6570 * @vf: virtual function number
6571 *
6572 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
6573 * based on the given viftual function number, @vf.
6574 *
6575 * Return 0 if successful, otherwise -ENODEV.
6576 **/
6577static int
6578lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
6579{
6580 if (vf > LPFC_VIR_FUNC_MAX)
6581 return -ENODEV;
6582
6583 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05006584 vf * LPFC_VFR_PAGE_SIZE +
6585 LPFC_ULP0_RQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04006586 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05006587 vf * LPFC_VFR_PAGE_SIZE +
6588 LPFC_ULP0_WQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04006589 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6590 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
6591 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6592 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
6593 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6594 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
6595 return 0;
6596}
6597
6598/**
6599 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
6600 * @phba: pointer to lpfc hba data structure.
6601 *
6602 * This routine is invoked to create the bootstrap mailbox
6603 * region consistent with the SLI-4 interface spec. This
6604 * routine allocates all memory necessary to communicate
6605 * mailbox commands to the port and sets up all alignment
6606 * needs. No locks are expected to be held when calling
6607 * this routine.
6608 *
6609 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006610 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04006611 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04006612 **/
6613static int
6614lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
6615{
6616 uint32_t bmbx_size;
6617 struct lpfc_dmabuf *dmabuf;
6618 struct dma_address *dma_address;
6619 uint32_t pa_addr;
6620 uint64_t phys_addr;
6621
6622 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
6623 if (!dmabuf)
6624 return -ENOMEM;
6625
6626 /*
6627 * The bootstrap mailbox region is comprised of 2 parts
6628 * plus an alignment restriction of 16 bytes.
6629 */
6630 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
Joe Perches1aee3832014-09-03 12:56:12 -04006631 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, bmbx_size,
6632 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04006633 if (!dmabuf->virt) {
6634 kfree(dmabuf);
6635 return -ENOMEM;
6636 }
James Smartda0436e2009-05-22 14:51:39 -04006637
6638 /*
6639 * Initialize the bootstrap mailbox pointers now so that the register
6640 * operations are simple later. The mailbox dma address is required
6641 * to be 16-byte aligned. Also align the virtual memory as each
6642 * maibox is copied into the bmbx mailbox region before issuing the
6643 * command to the port.
6644 */
6645 phba->sli4_hba.bmbx.dmabuf = dmabuf;
6646 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
6647
6648 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
6649 LPFC_ALIGN_16_BYTE);
6650 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
6651 LPFC_ALIGN_16_BYTE);
6652
6653 /*
6654 * Set the high and low physical addresses now. The SLI4 alignment
6655 * requirement is 16 bytes and the mailbox is posted to the port
6656 * as two 30-bit addresses. The other data is a bit marking whether
6657 * the 30-bit address is the high or low address.
6658 * Upcast bmbx aphys to 64bits so shift instruction compiles
6659 * clean on 32 bit machines.
6660 */
6661 dma_address = &phba->sli4_hba.bmbx.dma_address;
6662 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
6663 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
6664 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
6665 LPFC_BMBX_BIT1_ADDR_HI);
6666
6667 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
6668 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
6669 LPFC_BMBX_BIT1_ADDR_LO);
6670 return 0;
6671}
6672
6673/**
6674 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
6675 * @phba: pointer to lpfc hba data structure.
6676 *
6677 * This routine is invoked to teardown the bootstrap mailbox
6678 * region and release all host resources. This routine requires
6679 * the caller to ensure all mailbox commands recovered, no
6680 * additional mailbox comands are sent, and interrupts are disabled
6681 * before calling this routine.
6682 *
6683 **/
6684static void
6685lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
6686{
6687 dma_free_coherent(&phba->pcidev->dev,
6688 phba->sli4_hba.bmbx.bmbx_size,
6689 phba->sli4_hba.bmbx.dmabuf->virt,
6690 phba->sli4_hba.bmbx.dmabuf->phys);
6691
6692 kfree(phba->sli4_hba.bmbx.dmabuf);
6693 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
6694}
6695
6696/**
6697 * lpfc_sli4_read_config - Get the config parameters.
6698 * @phba: pointer to lpfc hba data structure.
6699 *
6700 * This routine is invoked to read the configuration parameters from the HBA.
6701 * The configuration parameters are used to set the base and maximum values
6702 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
6703 * allocation for the port.
6704 *
6705 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006706 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006707 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006708 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006709 **/
James Smartff78d8f2011-12-13 13:21:35 -05006710int
James Smartda0436e2009-05-22 14:51:39 -04006711lpfc_sli4_read_config(struct lpfc_hba *phba)
6712{
6713 LPFC_MBOXQ_t *pmb;
6714 struct lpfc_mbx_read_config *rd_config;
James Smart912e3ac2011-05-24 11:42:11 -04006715 union lpfc_sli4_cfg_shdr *shdr;
6716 uint32_t shdr_status, shdr_add_status;
6717 struct lpfc_mbx_get_func_cfg *get_func_cfg;
6718 struct lpfc_rsrc_desc_fcfcoe *desc;
James Smart8aa134a2012-08-14 14:25:29 -04006719 char *pdesc_0;
James Smart912e3ac2011-05-24 11:42:11 -04006720 uint32_t desc_count;
James Smart8aa134a2012-08-14 14:25:29 -04006721 int length, i, rc = 0, rc2;
James Smartda0436e2009-05-22 14:51:39 -04006722
6723 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6724 if (!pmb) {
6725 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6726 "2011 Unable to allocate memory for issuing "
6727 "SLI_CONFIG_SPECIAL mailbox command\n");
6728 return -ENOMEM;
6729 }
6730
6731 lpfc_read_config(phba, pmb);
6732
6733 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6734 if (rc != MBX_SUCCESS) {
6735 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6736 "2012 Mailbox failed , mbxCmd x%x "
6737 "READ_CONFIG, mbxStatus x%x\n",
6738 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6739 bf_get(lpfc_mqe_status, &pmb->u.mqe));
6740 rc = -EIO;
6741 } else {
6742 rd_config = &pmb->u.mqe.un.rd_config;
James Smartff78d8f2011-12-13 13:21:35 -05006743 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
6744 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
6745 phba->sli4_hba.lnk_info.lnk_tp =
6746 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
6747 phba->sli4_hba.lnk_info.lnk_no =
6748 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
6749 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6750 "3081 lnk_type:%d, lnk_numb:%d\n",
6751 phba->sli4_hba.lnk_info.lnk_tp,
6752 phba->sli4_hba.lnk_info.lnk_no);
6753 } else
6754 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6755 "3082 Mailbox (x%x) returned ldv:x0\n",
6756 bf_get(lpfc_mqe_command, &pmb->u.mqe));
James Smart6d368e52011-05-24 11:44:12 -04006757 phba->sli4_hba.extents_in_use =
6758 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04006759 phba->sli4_hba.max_cfg_param.max_xri =
6760 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
6761 phba->sli4_hba.max_cfg_param.xri_base =
6762 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
6763 phba->sli4_hba.max_cfg_param.max_vpi =
6764 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
6765 phba->sli4_hba.max_cfg_param.vpi_base =
6766 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
6767 phba->sli4_hba.max_cfg_param.max_rpi =
6768 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
6769 phba->sli4_hba.max_cfg_param.rpi_base =
6770 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
6771 phba->sli4_hba.max_cfg_param.max_vfi =
6772 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
6773 phba->sli4_hba.max_cfg_param.vfi_base =
6774 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
6775 phba->sli4_hba.max_cfg_param.max_fcfi =
6776 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04006777 phba->sli4_hba.max_cfg_param.max_eq =
6778 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
6779 phba->sli4_hba.max_cfg_param.max_rq =
6780 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
6781 phba->sli4_hba.max_cfg_param.max_wq =
6782 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
6783 phba->sli4_hba.max_cfg_param.max_cq =
6784 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
6785 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
6786 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
6787 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
6788 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
James Smart5ffc2662009-11-18 15:39:44 -05006789 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
6790 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04006791 phba->max_vports = phba->max_vpi;
6792 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart6d368e52011-05-24 11:44:12 -04006793 "2003 cfg params Extents? %d "
6794 "XRI(B:%d M:%d), "
James Smartda0436e2009-05-22 14:51:39 -04006795 "VPI(B:%d M:%d) "
6796 "VFI(B:%d M:%d) "
6797 "RPI(B:%d M:%d) "
James Smart6d368e52011-05-24 11:44:12 -04006798 "FCFI(Count:%d)\n",
6799 phba->sli4_hba.extents_in_use,
James Smartda0436e2009-05-22 14:51:39 -04006800 phba->sli4_hba.max_cfg_param.xri_base,
6801 phba->sli4_hba.max_cfg_param.max_xri,
6802 phba->sli4_hba.max_cfg_param.vpi_base,
6803 phba->sli4_hba.max_cfg_param.max_vpi,
6804 phba->sli4_hba.max_cfg_param.vfi_base,
6805 phba->sli4_hba.max_cfg_param.max_vfi,
6806 phba->sli4_hba.max_cfg_param.rpi_base,
6807 phba->sli4_hba.max_cfg_param.max_rpi,
James Smartda0436e2009-05-22 14:51:39 -04006808 phba->sli4_hba.max_cfg_param.max_fcfi);
6809 }
James Smart912e3ac2011-05-24 11:42:11 -04006810
6811 if (rc)
6812 goto read_cfg_out;
James Smartda0436e2009-05-22 14:51:39 -04006813
6814 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -04006815 length = phba->sli4_hba.max_cfg_param.max_xri -
6816 lpfc_sli4_get_els_iocb_cnt(phba);
6817 if (phba->cfg_hba_queue_depth > length) {
6818 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6819 "3361 HBA queue depth changed from %d to %d\n",
6820 phba->cfg_hba_queue_depth, length);
6821 phba->cfg_hba_queue_depth = length;
6822 }
James Smart912e3ac2011-05-24 11:42:11 -04006823
6824 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
6825 LPFC_SLI_INTF_IF_TYPE_2)
6826 goto read_cfg_out;
6827
6828 /* get the pf# and vf# for SLI4 if_type 2 port */
6829 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
6830 sizeof(struct lpfc_sli4_cfg_mhdr));
6831 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
6832 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
6833 length, LPFC_SLI4_MBX_EMBED);
6834
James Smart8aa134a2012-08-14 14:25:29 -04006835 rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart912e3ac2011-05-24 11:42:11 -04006836 shdr = (union lpfc_sli4_cfg_shdr *)
6837 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
6838 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6839 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart8aa134a2012-08-14 14:25:29 -04006840 if (rc2 || shdr_status || shdr_add_status) {
James Smart912e3ac2011-05-24 11:42:11 -04006841 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6842 "3026 Mailbox failed , mbxCmd x%x "
6843 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
6844 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6845 bf_get(lpfc_mqe_status, &pmb->u.mqe));
James Smart912e3ac2011-05-24 11:42:11 -04006846 goto read_cfg_out;
6847 }
6848
6849 /* search for fc_fcoe resrouce descriptor */
6850 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
6851 desc_count = get_func_cfg->func_cfg.rsrc_desc_count;
6852
James Smart8aa134a2012-08-14 14:25:29 -04006853 pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
6854 desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
6855 length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
6856 if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
6857 length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
6858 else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
6859 goto read_cfg_out;
6860
James Smart912e3ac2011-05-24 11:42:11 -04006861 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
James Smart8aa134a2012-08-14 14:25:29 -04006862 desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
James Smart912e3ac2011-05-24 11:42:11 -04006863 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
James Smart8aa134a2012-08-14 14:25:29 -04006864 bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
James Smart912e3ac2011-05-24 11:42:11 -04006865 phba->sli4_hba.iov.pf_number =
6866 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
6867 phba->sli4_hba.iov.vf_number =
6868 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
6869 break;
6870 }
6871 }
6872
6873 if (i < LPFC_RSRC_DESC_MAX_NUM)
6874 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6875 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
6876 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
6877 phba->sli4_hba.iov.vf_number);
James Smart8aa134a2012-08-14 14:25:29 -04006878 else
James Smart912e3ac2011-05-24 11:42:11 -04006879 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6880 "3028 GET_FUNCTION_CONFIG: failed to find "
6881 "Resrouce Descriptor:x%x\n",
6882 LPFC_RSRC_DESC_TYPE_FCFCOE);
James Smart912e3ac2011-05-24 11:42:11 -04006883
6884read_cfg_out:
6885 mempool_free(pmb, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04006886 return rc;
6887}
6888
6889/**
James Smart2fcee4b2010-12-15 17:57:46 -05006890 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
James Smartda0436e2009-05-22 14:51:39 -04006891 * @phba: pointer to lpfc hba data structure.
6892 *
James Smart2fcee4b2010-12-15 17:57:46 -05006893 * This routine is invoked to setup the port-side endian order when
6894 * the port if_type is 0. This routine has no function for other
6895 * if_types.
James Smartda0436e2009-05-22 14:51:39 -04006896 *
6897 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006898 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006899 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006900 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006901 **/
6902static int
6903lpfc_setup_endian_order(struct lpfc_hba *phba)
6904{
6905 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05006906 uint32_t if_type, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04006907 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
6908 HOST_ENDIAN_HIGH_WORD1};
6909
James Smart2fcee4b2010-12-15 17:57:46 -05006910 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
6911 switch (if_type) {
6912 case LPFC_SLI_INTF_IF_TYPE_0:
6913 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6914 GFP_KERNEL);
6915 if (!mboxq) {
6916 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6917 "0492 Unable to allocate memory for "
6918 "issuing SLI_CONFIG_SPECIAL mailbox "
6919 "command\n");
6920 return -ENOMEM;
6921 }
James Smartda0436e2009-05-22 14:51:39 -04006922
James Smart2fcee4b2010-12-15 17:57:46 -05006923 /*
6924 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
6925 * two words to contain special data values and no other data.
6926 */
6927 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
6928 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
6929 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6930 if (rc != MBX_SUCCESS) {
6931 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6932 "0493 SLI_CONFIG_SPECIAL mailbox "
6933 "failed with status x%x\n",
6934 rc);
6935 rc = -EIO;
6936 }
6937 mempool_free(mboxq, phba->mbox_mem_pool);
6938 break;
6939 case LPFC_SLI_INTF_IF_TYPE_2:
6940 case LPFC_SLI_INTF_IF_TYPE_1:
6941 default:
6942 break;
James Smartda0436e2009-05-22 14:51:39 -04006943 }
James Smartda0436e2009-05-22 14:51:39 -04006944 return rc;
6945}
6946
6947/**
James Smart5350d872011-10-10 21:33:49 -04006948 * lpfc_sli4_queue_verify - Verify and update EQ and CQ counts
James Smartda0436e2009-05-22 14:51:39 -04006949 * @phba: pointer to lpfc hba data structure.
6950 *
James Smart5350d872011-10-10 21:33:49 -04006951 * This routine is invoked to check the user settable queue counts for EQs and
6952 * CQs. after this routine is called the counts will be set to valid values that
6953 * adhere to the constraints of the system's interrupt vectors and the port's
6954 * queue resources.
James Smartda0436e2009-05-22 14:51:39 -04006955 *
6956 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006957 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006958 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04006959 **/
6960static int
James Smart5350d872011-10-10 21:33:49 -04006961lpfc_sli4_queue_verify(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04006962{
James Smart67d12732012-08-03 12:36:13 -04006963 int cfg_fcp_io_channel;
James Smart90695ee2012-08-14 14:25:36 -04006964 uint32_t cpu;
6965 uint32_t i = 0;
James Smart1ba981f2014-02-20 09:56:45 -05006966 int fof_vectors = phba->cfg_fof ? 1 : 0;
James Smartda0436e2009-05-22 14:51:39 -04006967
6968 /*
James Smart67d12732012-08-03 12:36:13 -04006969 * Sanity check for configured queue parameters against the run-time
James Smartda0436e2009-05-22 14:51:39 -04006970 * device parameters
6971 */
6972
James Smart67d12732012-08-03 12:36:13 -04006973 /* Sanity check on HBA EQ parameters */
6974 cfg_fcp_io_channel = phba->cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04006975
James Smart7bb03bb2013-04-17 20:19:16 -04006976 /* It doesn't make sense to have more io channels then online CPUs */
6977 for_each_present_cpu(cpu) {
6978 if (cpu_online(cpu))
6979 i++;
James Smart90695ee2012-08-14 14:25:36 -04006980 }
James Smart7bb03bb2013-04-17 20:19:16 -04006981 phba->sli4_hba.num_online_cpu = i;
James Smartb246de12013-05-31 17:03:07 -04006982 phba->sli4_hba.num_present_cpu = lpfc_present_cpu;
James Smart76fd07a2014-02-20 09:57:18 -05006983 phba->sli4_hba.curr_disp_cpu = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04006984
James Smart90695ee2012-08-14 14:25:36 -04006985 if (i < cfg_fcp_io_channel) {
James Smart82c3e9b2012-09-29 11:29:50 -04006986 lpfc_printf_log(phba,
6987 KERN_ERR, LOG_INIT,
James Smart90695ee2012-08-14 14:25:36 -04006988 "3188 Reducing IO channels to match number of "
James Smart7bb03bb2013-04-17 20:19:16 -04006989 "online CPUs: from %d to %d\n",
6990 cfg_fcp_io_channel, i);
James Smart90695ee2012-08-14 14:25:36 -04006991 cfg_fcp_io_channel = i;
6992 }
6993
James Smart1ba981f2014-02-20 09:56:45 -05006994 if (cfg_fcp_io_channel + fof_vectors >
James Smart67d12732012-08-03 12:36:13 -04006995 phba->sli4_hba.max_cfg_param.max_eq) {
James Smart82c3e9b2012-09-29 11:29:50 -04006996 if (phba->sli4_hba.max_cfg_param.max_eq <
6997 LPFC_FCP_IO_CHAN_MIN) {
James Smartda0436e2009-05-22 14:51:39 -04006998 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6999 "2574 Not enough EQs (%d) from the "
7000 "pci function for supporting FCP "
7001 "EQs (%d)\n",
7002 phba->sli4_hba.max_cfg_param.max_eq,
James Smart67d12732012-08-03 12:36:13 -04007003 phba->cfg_fcp_io_channel);
James Smartda0436e2009-05-22 14:51:39 -04007004 goto out_error;
7005 }
James Smart82c3e9b2012-09-29 11:29:50 -04007006 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7007 "2575 Reducing IO channels to match number of "
7008 "available EQs: from %d to %d\n",
7009 cfg_fcp_io_channel,
7010 phba->sli4_hba.max_cfg_param.max_eq);
James Smart1ba981f2014-02-20 09:56:45 -05007011 cfg_fcp_io_channel = phba->sli4_hba.max_cfg_param.max_eq -
7012 fof_vectors;
James Smartda0436e2009-05-22 14:51:39 -04007013 }
James Smart67d12732012-08-03 12:36:13 -04007014
James Smartda0436e2009-05-22 14:51:39 -04007015 /* The actual number of FCP event queues adopted */
James Smart67d12732012-08-03 12:36:13 -04007016 phba->cfg_fcp_io_channel = cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04007017
James Smartda0436e2009-05-22 14:51:39 -04007018 /* Get EQ depth from module parameter, fake the default for now */
7019 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
7020 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
7021
James Smart5350d872011-10-10 21:33:49 -04007022 /* Get CQ depth from module parameter, fake the default for now */
7023 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
7024 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
7025
7026 return 0;
7027out_error:
7028 return -ENOMEM;
7029}
7030
7031/**
7032 * lpfc_sli4_queue_create - Create all the SLI4 queues
7033 * @phba: pointer to lpfc hba data structure.
7034 *
7035 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
7036 * operation. For each SLI4 queue type, the parameters such as queue entry
7037 * count (queue depth) shall be taken from the module parameter. For now,
7038 * we just use some constant number as place holder.
7039 *
7040 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07007041 * 0 - successful
James Smart5350d872011-10-10 21:33:49 -04007042 * -ENOMEM - No availble memory
7043 * -EIO - The mailbox failed to complete successfully.
7044 **/
7045int
7046lpfc_sli4_queue_create(struct lpfc_hba *phba)
7047{
7048 struct lpfc_queue *qdesc;
James Smart67d12732012-08-03 12:36:13 -04007049 int idx;
James Smart5350d872011-10-10 21:33:49 -04007050
7051 /*
James Smart67d12732012-08-03 12:36:13 -04007052 * Create HBA Record arrays.
James Smart5350d872011-10-10 21:33:49 -04007053 */
James Smart67d12732012-08-03 12:36:13 -04007054 if (!phba->cfg_fcp_io_channel)
7055 return -ERANGE;
James Smart5350d872011-10-10 21:33:49 -04007056
James Smart67d12732012-08-03 12:36:13 -04007057 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
7058 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
7059 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
7060 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
7061 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
7062 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
7063
7064 phba->sli4_hba.hba_eq = kzalloc((sizeof(struct lpfc_queue *) *
7065 phba->cfg_fcp_io_channel), GFP_KERNEL);
7066 if (!phba->sli4_hba.hba_eq) {
James Smartda0436e2009-05-22 14:51:39 -04007067 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04007068 "2576 Failed allocate memory for "
7069 "fast-path EQ record array\n");
James Smartda0436e2009-05-22 14:51:39 -04007070 goto out_error;
7071 }
James Smart67d12732012-08-03 12:36:13 -04007072
7073 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
7074 phba->cfg_fcp_io_channel), GFP_KERNEL);
7075 if (!phba->sli4_hba.fcp_cq) {
7076 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7077 "2577 Failed allocate memory for fast-path "
7078 "CQ record array\n");
7079 goto out_error;
7080 }
7081
7082 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
7083 phba->cfg_fcp_io_channel), GFP_KERNEL);
7084 if (!phba->sli4_hba.fcp_wq) {
7085 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7086 "2578 Failed allocate memory for fast-path "
7087 "WQ record array\n");
7088 goto out_error;
7089 }
James Smartda0436e2009-05-22 14:51:39 -04007090
James Smart5350d872011-10-10 21:33:49 -04007091 /*
James Smart67d12732012-08-03 12:36:13 -04007092 * Since the first EQ can have multiple CQs associated with it,
7093 * this array is used to quickly see if we have a FCP fast-path
7094 * CQ match.
James Smart5350d872011-10-10 21:33:49 -04007095 */
James Smart67d12732012-08-03 12:36:13 -04007096 phba->sli4_hba.fcp_cq_map = kzalloc((sizeof(uint16_t) *
7097 phba->cfg_fcp_io_channel), GFP_KERNEL);
7098 if (!phba->sli4_hba.fcp_cq_map) {
7099 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7100 "2545 Failed allocate memory for fast-path "
7101 "CQ map\n");
7102 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007103 }
James Smart67d12732012-08-03 12:36:13 -04007104
7105 /*
7106 * Create HBA Event Queues (EQs). The cfg_fcp_io_channel specifies
7107 * how many EQs to create.
7108 */
7109 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7110
7111 /* Create EQs */
James Smartda0436e2009-05-22 14:51:39 -04007112 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
7113 phba->sli4_hba.eq_ecount);
7114 if (!qdesc) {
7115 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04007116 "0497 Failed allocate EQ (%d)\n", idx);
7117 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007118 }
James Smart67d12732012-08-03 12:36:13 -04007119 phba->sli4_hba.hba_eq[idx] = qdesc;
7120
7121 /* Create Fast Path FCP CQs */
7122 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7123 phba->sli4_hba.cq_ecount);
7124 if (!qdesc) {
7125 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7126 "0499 Failed allocate fast-path FCP "
7127 "CQ (%d)\n", idx);
7128 goto out_error;
7129 }
7130 phba->sli4_hba.fcp_cq[idx] = qdesc;
7131
7132 /* Create Fast Path FCP WQs */
7133 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
7134 phba->sli4_hba.wq_ecount);
7135 if (!qdesc) {
7136 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7137 "0503 Failed allocate fast-path FCP "
7138 "WQ (%d)\n", idx);
7139 goto out_error;
7140 }
7141 phba->sli4_hba.fcp_wq[idx] = qdesc;
James Smartda0436e2009-05-22 14:51:39 -04007142 }
7143
James Smart67d12732012-08-03 12:36:13 -04007144
James Smartda0436e2009-05-22 14:51:39 -04007145 /*
James Smart67d12732012-08-03 12:36:13 -04007146 * Create Slow Path Completion Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04007147 */
7148
James Smartda0436e2009-05-22 14:51:39 -04007149 /* Create slow-path Mailbox Command Complete Queue */
7150 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7151 phba->sli4_hba.cq_ecount);
7152 if (!qdesc) {
7153 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7154 "0500 Failed allocate slow-path mailbox CQ\n");
James Smart67d12732012-08-03 12:36:13 -04007155 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007156 }
7157 phba->sli4_hba.mbx_cq = qdesc;
7158
7159 /* Create slow-path ELS Complete Queue */
7160 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7161 phba->sli4_hba.cq_ecount);
7162 if (!qdesc) {
7163 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7164 "0501 Failed allocate slow-path ELS CQ\n");
James Smart67d12732012-08-03 12:36:13 -04007165 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007166 }
7167 phba->sli4_hba.els_cq = qdesc;
7168
James Smartda0436e2009-05-22 14:51:39 -04007169
James Smart5350d872011-10-10 21:33:49 -04007170 /*
James Smart67d12732012-08-03 12:36:13 -04007171 * Create Slow Path Work Queues (WQs)
James Smart5350d872011-10-10 21:33:49 -04007172 */
James Smartda0436e2009-05-22 14:51:39 -04007173
7174 /* Create Mailbox Command Queue */
James Smartda0436e2009-05-22 14:51:39 -04007175
7176 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
7177 phba->sli4_hba.mq_ecount);
7178 if (!qdesc) {
7179 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7180 "0505 Failed allocate slow-path MQ\n");
James Smart67d12732012-08-03 12:36:13 -04007181 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007182 }
7183 phba->sli4_hba.mbx_wq = qdesc;
7184
7185 /*
James Smart67d12732012-08-03 12:36:13 -04007186 * Create ELS Work Queues
James Smartda0436e2009-05-22 14:51:39 -04007187 */
James Smartda0436e2009-05-22 14:51:39 -04007188
7189 /* Create slow-path ELS Work Queue */
7190 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
7191 phba->sli4_hba.wq_ecount);
7192 if (!qdesc) {
7193 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7194 "0504 Failed allocate slow-path ELS WQ\n");
James Smart67d12732012-08-03 12:36:13 -04007195 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007196 }
7197 phba->sli4_hba.els_wq = qdesc;
7198
James Smartda0436e2009-05-22 14:51:39 -04007199 /*
7200 * Create Receive Queue (RQ)
7201 */
James Smartda0436e2009-05-22 14:51:39 -04007202
7203 /* Create Receive Queue for header */
7204 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
7205 phba->sli4_hba.rq_ecount);
7206 if (!qdesc) {
7207 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7208 "0506 Failed allocate receive HRQ\n");
James Smart67d12732012-08-03 12:36:13 -04007209 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007210 }
7211 phba->sli4_hba.hdr_rq = qdesc;
7212
7213 /* Create Receive Queue for data */
7214 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
7215 phba->sli4_hba.rq_ecount);
7216 if (!qdesc) {
7217 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7218 "0507 Failed allocate receive DRQ\n");
James Smart67d12732012-08-03 12:36:13 -04007219 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007220 }
7221 phba->sli4_hba.dat_rq = qdesc;
7222
James Smart1ba981f2014-02-20 09:56:45 -05007223 /* Create the Queues needed for Flash Optimized Fabric operations */
7224 if (phba->cfg_fof)
7225 lpfc_fof_queue_create(phba);
James Smartda0436e2009-05-22 14:51:39 -04007226 return 0;
7227
James Smartda0436e2009-05-22 14:51:39 -04007228out_error:
James Smart67d12732012-08-03 12:36:13 -04007229 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007230 return -ENOMEM;
7231}
7232
7233/**
7234 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
7235 * @phba: pointer to lpfc hba data structure.
7236 *
7237 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
7238 * operation.
7239 *
7240 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007241 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007242 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007243 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007244 **/
James Smart5350d872011-10-10 21:33:49 -04007245void
James Smartda0436e2009-05-22 14:51:39 -04007246lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
7247{
James Smart67d12732012-08-03 12:36:13 -04007248 int idx;
7249
James Smart1ba981f2014-02-20 09:56:45 -05007250 if (phba->cfg_fof)
7251 lpfc_fof_queue_destroy(phba);
7252
James Smart67d12732012-08-03 12:36:13 -04007253 if (phba->sli4_hba.hba_eq != NULL) {
7254 /* Release HBA event queue */
7255 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7256 if (phba->sli4_hba.hba_eq[idx] != NULL) {
7257 lpfc_sli4_queue_free(
7258 phba->sli4_hba.hba_eq[idx]);
7259 phba->sli4_hba.hba_eq[idx] = NULL;
7260 }
7261 }
7262 kfree(phba->sli4_hba.hba_eq);
7263 phba->sli4_hba.hba_eq = NULL;
7264 }
7265
7266 if (phba->sli4_hba.fcp_cq != NULL) {
7267 /* Release FCP completion queue */
7268 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7269 if (phba->sli4_hba.fcp_cq[idx] != NULL) {
7270 lpfc_sli4_queue_free(
7271 phba->sli4_hba.fcp_cq[idx]);
7272 phba->sli4_hba.fcp_cq[idx] = NULL;
7273 }
7274 }
7275 kfree(phba->sli4_hba.fcp_cq);
7276 phba->sli4_hba.fcp_cq = NULL;
7277 }
7278
7279 if (phba->sli4_hba.fcp_wq != NULL) {
7280 /* Release FCP work queue */
7281 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7282 if (phba->sli4_hba.fcp_wq[idx] != NULL) {
7283 lpfc_sli4_queue_free(
7284 phba->sli4_hba.fcp_wq[idx]);
7285 phba->sli4_hba.fcp_wq[idx] = NULL;
7286 }
7287 }
7288 kfree(phba->sli4_hba.fcp_wq);
7289 phba->sli4_hba.fcp_wq = NULL;
7290 }
7291
7292 /* Release FCP CQ mapping array */
7293 if (phba->sli4_hba.fcp_cq_map != NULL) {
7294 kfree(phba->sli4_hba.fcp_cq_map);
7295 phba->sli4_hba.fcp_cq_map = NULL;
7296 }
James Smartda0436e2009-05-22 14:51:39 -04007297
7298 /* Release mailbox command work queue */
James Smart67d12732012-08-03 12:36:13 -04007299 if (phba->sli4_hba.mbx_wq != NULL) {
7300 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
7301 phba->sli4_hba.mbx_wq = NULL;
7302 }
James Smartda0436e2009-05-22 14:51:39 -04007303
7304 /* Release ELS work queue */
James Smart67d12732012-08-03 12:36:13 -04007305 if (phba->sli4_hba.els_wq != NULL) {
7306 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
7307 phba->sli4_hba.els_wq = NULL;
7308 }
James Smartda0436e2009-05-22 14:51:39 -04007309
7310 /* Release unsolicited receive queue */
James Smart67d12732012-08-03 12:36:13 -04007311 if (phba->sli4_hba.hdr_rq != NULL) {
7312 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
7313 phba->sli4_hba.hdr_rq = NULL;
7314 }
7315 if (phba->sli4_hba.dat_rq != NULL) {
7316 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
7317 phba->sli4_hba.dat_rq = NULL;
7318 }
James Smartda0436e2009-05-22 14:51:39 -04007319
James Smartda0436e2009-05-22 14:51:39 -04007320 /* Release ELS complete queue */
James Smart67d12732012-08-03 12:36:13 -04007321 if (phba->sli4_hba.els_cq != NULL) {
7322 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
7323 phba->sli4_hba.els_cq = NULL;
7324 }
James Smartda0436e2009-05-22 14:51:39 -04007325
7326 /* Release mailbox command complete queue */
James Smart67d12732012-08-03 12:36:13 -04007327 if (phba->sli4_hba.mbx_cq != NULL) {
7328 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
7329 phba->sli4_hba.mbx_cq = NULL;
7330 }
James Smartda0436e2009-05-22 14:51:39 -04007331
7332 return;
7333}
7334
7335/**
7336 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
7337 * @phba: pointer to lpfc hba data structure.
7338 *
7339 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
7340 * operation.
7341 *
7342 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007343 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007344 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007345 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007346 **/
7347int
7348lpfc_sli4_queue_setup(struct lpfc_hba *phba)
7349{
James Smart2a76a282012-08-03 12:35:54 -04007350 struct lpfc_sli *psli = &phba->sli;
7351 struct lpfc_sli_ring *pring;
James Smartda0436e2009-05-22 14:51:39 -04007352 int rc = -ENOMEM;
7353 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
7354 int fcp_cq_index = 0;
James Smart962bc512013-01-03 15:44:00 -05007355 uint32_t shdr_status, shdr_add_status;
7356 union lpfc_sli4_cfg_shdr *shdr;
7357 LPFC_MBOXQ_t *mboxq;
7358 uint32_t length;
7359
7360 /* Check for dual-ULP support */
7361 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7362 if (!mboxq) {
7363 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7364 "3249 Unable to allocate memory for "
7365 "QUERY_FW_CFG mailbox command\n");
7366 return -ENOMEM;
7367 }
7368 length = (sizeof(struct lpfc_mbx_query_fw_config) -
7369 sizeof(struct lpfc_sli4_cfg_mhdr));
7370 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7371 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
7372 length, LPFC_SLI4_MBX_EMBED);
7373
7374 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7375
7376 shdr = (union lpfc_sli4_cfg_shdr *)
7377 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7378 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7379 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7380 if (shdr_status || shdr_add_status || rc) {
7381 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7382 "3250 QUERY_FW_CFG mailbox failed with status "
7383 "x%x add_status x%x, mbx status x%x\n",
7384 shdr_status, shdr_add_status, rc);
7385 if (rc != MBX_TIMEOUT)
7386 mempool_free(mboxq, phba->mbox_mem_pool);
7387 rc = -ENXIO;
7388 goto out_error;
7389 }
7390
7391 phba->sli4_hba.fw_func_mode =
7392 mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
7393 phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
7394 phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
7395 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7396 "3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
7397 "ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
7398 phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
7399
7400 if (rc != MBX_TIMEOUT)
7401 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04007402
7403 /*
James Smart67d12732012-08-03 12:36:13 -04007404 * Set up HBA Event Queues (EQs)
James Smartda0436e2009-05-22 14:51:39 -04007405 */
7406
James Smart67d12732012-08-03 12:36:13 -04007407 /* Set up HBA event queue */
7408 if (phba->cfg_fcp_io_channel && !phba->sli4_hba.hba_eq) {
James Smart2e90f4b2011-12-13 13:22:37 -05007409 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7410 "3147 Fast-path EQs not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007411 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007412 goto out_error;
James Smart2e90f4b2011-12-13 13:22:37 -05007413 }
James Smart67d12732012-08-03 12:36:13 -04007414 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
7415 if (!phba->sli4_hba.hba_eq[fcp_eqidx]) {
James Smartda0436e2009-05-22 14:51:39 -04007416 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7417 "0522 Fast-path EQ (%d) not "
7418 "allocated\n", fcp_eqidx);
James Smart1b511972011-12-13 13:23:09 -05007419 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007420 goto out_destroy_hba_eq;
James Smartda0436e2009-05-22 14:51:39 -04007421 }
James Smart67d12732012-08-03 12:36:13 -04007422 rc = lpfc_eq_create(phba, phba->sli4_hba.hba_eq[fcp_eqidx],
James Smartbf8dae82012-08-03 12:36:24 -04007423 (phba->cfg_fcp_imax / phba->cfg_fcp_io_channel));
James Smartda0436e2009-05-22 14:51:39 -04007424 if (rc) {
7425 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7426 "0523 Failed setup of fast-path EQ "
7427 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
James Smart67d12732012-08-03 12:36:13 -04007428 goto out_destroy_hba_eq;
James Smartda0436e2009-05-22 14:51:39 -04007429 }
7430 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04007431 "2584 HBA EQ setup: "
James Smartda0436e2009-05-22 14:51:39 -04007432 "queue[%d]-id=%d\n", fcp_eqidx,
James Smart67d12732012-08-03 12:36:13 -04007433 phba->sli4_hba.hba_eq[fcp_eqidx]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007434 }
7435
James Smart67d12732012-08-03 12:36:13 -04007436 /* Set up fast-path FCP Response Complete Queue */
7437 if (!phba->sli4_hba.fcp_cq) {
7438 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7439 "3148 Fast-path FCP CQ array not "
7440 "allocated\n");
7441 rc = -ENOMEM;
7442 goto out_destroy_hba_eq;
7443 }
7444
7445 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_io_channel; fcp_cqidx++) {
7446 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
7447 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7448 "0526 Fast-path FCP CQ (%d) not "
7449 "allocated\n", fcp_cqidx);
7450 rc = -ENOMEM;
7451 goto out_destroy_fcp_cq;
7452 }
7453 rc = lpfc_cq_create(phba, phba->sli4_hba.fcp_cq[fcp_cqidx],
7454 phba->sli4_hba.hba_eq[fcp_cqidx], LPFC_WCQ, LPFC_FCP);
7455 if (rc) {
7456 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7457 "0527 Failed setup of fast-path FCP "
7458 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
7459 goto out_destroy_fcp_cq;
7460 }
7461
7462 /* Setup fcp_cq_map for fast lookup */
7463 phba->sli4_hba.fcp_cq_map[fcp_cqidx] =
7464 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id;
7465
7466 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7467 "2588 FCP CQ setup: cq[%d]-id=%d, "
7468 "parent seq[%d]-id=%d\n",
7469 fcp_cqidx,
7470 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
7471 fcp_cqidx,
7472 phba->sli4_hba.hba_eq[fcp_cqidx]->queue_id);
7473 }
7474
7475 /* Set up fast-path FCP Work Queue */
7476 if (!phba->sli4_hba.fcp_wq) {
7477 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7478 "3149 Fast-path FCP WQ array not "
7479 "allocated\n");
7480 rc = -ENOMEM;
7481 goto out_destroy_fcp_cq;
7482 }
7483
7484 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_io_channel; fcp_wqidx++) {
7485 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
7486 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7487 "0534 Fast-path FCP WQ (%d) not "
7488 "allocated\n", fcp_wqidx);
7489 rc = -ENOMEM;
7490 goto out_destroy_fcp_wq;
7491 }
7492 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
7493 phba->sli4_hba.fcp_cq[fcp_wqidx],
7494 LPFC_FCP);
7495 if (rc) {
7496 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7497 "0535 Failed setup of fast-path FCP "
7498 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
7499 goto out_destroy_fcp_wq;
7500 }
7501
7502 /* Bind this WQ to the next FCP ring */
7503 pring = &psli->ring[MAX_SLI3_CONFIGURED_RINGS + fcp_wqidx];
7504 pring->sli.sli4.wqp = (void *)phba->sli4_hba.fcp_wq[fcp_wqidx];
7505 phba->sli4_hba.fcp_cq[fcp_wqidx]->pring = pring;
7506
7507 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7508 "2591 FCP WQ setup: wq[%d]-id=%d, "
7509 "parent cq[%d]-id=%d\n",
7510 fcp_wqidx,
7511 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
7512 fcp_cq_index,
7513 phba->sli4_hba.fcp_cq[fcp_wqidx]->queue_id);
7514 }
James Smartda0436e2009-05-22 14:51:39 -04007515 /*
7516 * Set up Complete Queues (CQs)
7517 */
7518
7519 /* Set up slow-path MBOX Complete Queue as the first CQ */
7520 if (!phba->sli4_hba.mbx_cq) {
7521 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7522 "0528 Mailbox CQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007523 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007524 goto out_destroy_fcp_wq;
James Smartda0436e2009-05-22 14:51:39 -04007525 }
James Smart67d12732012-08-03 12:36:13 -04007526 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq,
7527 phba->sli4_hba.hba_eq[0], LPFC_MCQ, LPFC_MBOX);
James Smartda0436e2009-05-22 14:51:39 -04007528 if (rc) {
7529 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7530 "0529 Failed setup of slow-path mailbox CQ: "
7531 "rc = 0x%x\n", rc);
James Smart67d12732012-08-03 12:36:13 -04007532 goto out_destroy_fcp_wq;
James Smartda0436e2009-05-22 14:51:39 -04007533 }
7534 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7535 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
7536 phba->sli4_hba.mbx_cq->queue_id,
James Smart67d12732012-08-03 12:36:13 -04007537 phba->sli4_hba.hba_eq[0]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007538
7539 /* Set up slow-path ELS Complete Queue */
7540 if (!phba->sli4_hba.els_cq) {
7541 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7542 "0530 ELS CQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007543 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04007544 goto out_destroy_mbx_cq;
7545 }
James Smart67d12732012-08-03 12:36:13 -04007546 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq,
7547 phba->sli4_hba.hba_eq[0], LPFC_WCQ, LPFC_ELS);
James Smartda0436e2009-05-22 14:51:39 -04007548 if (rc) {
7549 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7550 "0531 Failed setup of slow-path ELS CQ: "
7551 "rc = 0x%x\n", rc);
7552 goto out_destroy_mbx_cq;
7553 }
7554 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7555 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
7556 phba->sli4_hba.els_cq->queue_id,
James Smart67d12732012-08-03 12:36:13 -04007557 phba->sli4_hba.hba_eq[0]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007558
7559 /*
7560 * Set up all the Work Queues (WQs)
7561 */
7562
7563 /* Set up Mailbox Command Queue */
7564 if (!phba->sli4_hba.mbx_wq) {
7565 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7566 "0538 Slow-path MQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007567 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007568 goto out_destroy_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04007569 }
7570 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
7571 phba->sli4_hba.mbx_cq, LPFC_MBOX);
7572 if (rc) {
7573 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7574 "0539 Failed setup of slow-path MQ: "
7575 "rc = 0x%x\n", rc);
James Smart67d12732012-08-03 12:36:13 -04007576 goto out_destroy_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04007577 }
7578 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7579 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
7580 phba->sli4_hba.mbx_wq->queue_id,
7581 phba->sli4_hba.mbx_cq->queue_id);
7582
7583 /* Set up slow-path ELS Work Queue */
7584 if (!phba->sli4_hba.els_wq) {
7585 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7586 "0536 Slow-path ELS WQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007587 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04007588 goto out_destroy_mbx_wq;
7589 }
7590 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
7591 phba->sli4_hba.els_cq, LPFC_ELS);
7592 if (rc) {
7593 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7594 "0537 Failed setup of slow-path ELS WQ: "
7595 "rc = 0x%x\n", rc);
7596 goto out_destroy_mbx_wq;
7597 }
James Smart2a76a282012-08-03 12:35:54 -04007598
7599 /* Bind this WQ to the ELS ring */
7600 pring = &psli->ring[LPFC_ELS_RING];
7601 pring->sli.sli4.wqp = (void *)phba->sli4_hba.els_wq;
7602 phba->sli4_hba.els_cq->pring = pring;
7603
James Smartda0436e2009-05-22 14:51:39 -04007604 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7605 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
7606 phba->sli4_hba.els_wq->queue_id,
7607 phba->sli4_hba.els_cq->queue_id);
7608
James Smartda0436e2009-05-22 14:51:39 -04007609 /*
7610 * Create Receive Queue (RQ)
7611 */
7612 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
7613 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7614 "0540 Receive Queue not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007615 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007616 goto out_destroy_els_wq;
James Smartda0436e2009-05-22 14:51:39 -04007617 }
James Smart73d91e52011-10-10 21:32:10 -04007618
7619 lpfc_rq_adjust_repost(phba, phba->sli4_hba.hdr_rq, LPFC_ELS_HBQ);
7620 lpfc_rq_adjust_repost(phba, phba->sli4_hba.dat_rq, LPFC_ELS_HBQ);
7621
James Smartda0436e2009-05-22 14:51:39 -04007622 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04007623 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04007624 if (rc) {
7625 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7626 "0541 Failed setup of Receive Queue: "
7627 "rc = 0x%x\n", rc);
7628 goto out_destroy_fcp_wq;
7629 }
James Smart73d91e52011-10-10 21:32:10 -04007630
James Smartda0436e2009-05-22 14:51:39 -04007631 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7632 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
7633 "parent cq-id=%d\n",
7634 phba->sli4_hba.hdr_rq->queue_id,
7635 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04007636 phba->sli4_hba.els_cq->queue_id);
James Smart1ba981f2014-02-20 09:56:45 -05007637
7638 if (phba->cfg_fof) {
7639 rc = lpfc_fof_queue_setup(phba);
7640 if (rc) {
7641 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7642 "0549 Failed setup of FOF Queues: "
7643 "rc = 0x%x\n", rc);
7644 goto out_destroy_els_rq;
7645 }
7646 }
James Smartda0436e2009-05-22 14:51:39 -04007647 return 0;
7648
James Smart1ba981f2014-02-20 09:56:45 -05007649out_destroy_els_rq:
7650 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
James Smart2e90f4b2011-12-13 13:22:37 -05007651out_destroy_els_wq:
James Smartda0436e2009-05-22 14:51:39 -04007652 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7653out_destroy_mbx_wq:
7654 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
James Smart2e90f4b2011-12-13 13:22:37 -05007655out_destroy_els_cq:
James Smartda0436e2009-05-22 14:51:39 -04007656 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
7657out_destroy_mbx_cq:
7658 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
James Smart67d12732012-08-03 12:36:13 -04007659out_destroy_fcp_wq:
7660 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
7661 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
7662out_destroy_fcp_cq:
7663 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
7664 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
7665out_destroy_hba_eq:
James Smartda0436e2009-05-22 14:51:39 -04007666 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
James Smart67d12732012-08-03 12:36:13 -04007667 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[fcp_eqidx]);
James Smartda0436e2009-05-22 14:51:39 -04007668out_error:
7669 return rc;
7670}
7671
7672/**
7673 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
7674 * @phba: pointer to lpfc hba data structure.
7675 *
7676 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
7677 * operation.
7678 *
7679 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007680 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007681 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007682 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007683 **/
7684void
7685lpfc_sli4_queue_unset(struct lpfc_hba *phba)
7686{
7687 int fcp_qidx;
7688
James Smart1ba981f2014-02-20 09:56:45 -05007689 /* Unset the queues created for Flash Optimized Fabric operations */
7690 if (phba->cfg_fof)
7691 lpfc_fof_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007692 /* Unset mailbox command work queue */
7693 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
7694 /* Unset ELS work queue */
7695 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7696 /* Unset unsolicited receive queue */
7697 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
7698 /* Unset FCP work queue */
James Smart67d12732012-08-03 12:36:13 -04007699 if (phba->sli4_hba.fcp_wq) {
7700 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
7701 fcp_qidx++)
7702 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
7703 }
James Smartda0436e2009-05-22 14:51:39 -04007704 /* Unset mailbox command complete queue */
7705 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
7706 /* Unset ELS complete queue */
7707 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
James Smartda0436e2009-05-22 14:51:39 -04007708 /* Unset FCP response complete queue */
James Smart2e90f4b2011-12-13 13:22:37 -05007709 if (phba->sli4_hba.fcp_cq) {
James Smart67d12732012-08-03 12:36:13 -04007710 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
7711 fcp_qidx++)
James Smart2e90f4b2011-12-13 13:22:37 -05007712 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05007713 }
James Smartda0436e2009-05-22 14:51:39 -04007714 /* Unset fast-path event queue */
James Smart67d12732012-08-03 12:36:13 -04007715 if (phba->sli4_hba.hba_eq) {
7716 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
James Smart2e90f4b2011-12-13 13:22:37 -05007717 fcp_qidx++)
James Smart67d12732012-08-03 12:36:13 -04007718 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[fcp_qidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05007719 }
James Smartda0436e2009-05-22 14:51:39 -04007720}
7721
7722/**
7723 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
7724 * @phba: pointer to lpfc hba data structure.
7725 *
7726 * This routine is invoked to allocate and set up a pool of completion queue
7727 * events. The body of the completion queue event is a completion queue entry
7728 * CQE. For now, this pool is used for the interrupt service routine to queue
7729 * the following HBA completion queue events for the worker thread to process:
7730 * - Mailbox asynchronous events
7731 * - Receive queue completion unsolicited events
7732 * Later, this can be used for all the slow-path events.
7733 *
7734 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007735 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007736 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04007737 **/
7738static int
7739lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
7740{
7741 struct lpfc_cq_event *cq_event;
7742 int i;
7743
7744 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
7745 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
7746 if (!cq_event)
7747 goto out_pool_create_fail;
7748 list_add_tail(&cq_event->list,
7749 &phba->sli4_hba.sp_cqe_event_pool);
7750 }
7751 return 0;
7752
7753out_pool_create_fail:
7754 lpfc_sli4_cq_event_pool_destroy(phba);
7755 return -ENOMEM;
7756}
7757
7758/**
7759 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
7760 * @phba: pointer to lpfc hba data structure.
7761 *
7762 * This routine is invoked to free the pool of completion queue events at
7763 * driver unload time. Note that, it is the responsibility of the driver
7764 * cleanup routine to free all the outstanding completion-queue events
7765 * allocated from this pool back into the pool before invoking this routine
7766 * to destroy the pool.
7767 **/
7768static void
7769lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
7770{
7771 struct lpfc_cq_event *cq_event, *next_cq_event;
7772
7773 list_for_each_entry_safe(cq_event, next_cq_event,
7774 &phba->sli4_hba.sp_cqe_event_pool, list) {
7775 list_del(&cq_event->list);
7776 kfree(cq_event);
7777 }
7778}
7779
7780/**
7781 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
7782 * @phba: pointer to lpfc hba data structure.
7783 *
7784 * This routine is the lock free version of the API invoked to allocate a
7785 * completion-queue event from the free pool.
7786 *
7787 * Return: Pointer to the newly allocated completion-queue event if successful
7788 * NULL otherwise.
7789 **/
7790struct lpfc_cq_event *
7791__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
7792{
7793 struct lpfc_cq_event *cq_event = NULL;
7794
7795 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
7796 struct lpfc_cq_event, list);
7797 return cq_event;
7798}
7799
7800/**
7801 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
7802 * @phba: pointer to lpfc hba data structure.
7803 *
7804 * This routine is the lock version of the API invoked to allocate a
7805 * completion-queue event from the free pool.
7806 *
7807 * Return: Pointer to the newly allocated completion-queue event if successful
7808 * NULL otherwise.
7809 **/
7810struct lpfc_cq_event *
7811lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
7812{
7813 struct lpfc_cq_event *cq_event;
7814 unsigned long iflags;
7815
7816 spin_lock_irqsave(&phba->hbalock, iflags);
7817 cq_event = __lpfc_sli4_cq_event_alloc(phba);
7818 spin_unlock_irqrestore(&phba->hbalock, iflags);
7819 return cq_event;
7820}
7821
7822/**
7823 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
7824 * @phba: pointer to lpfc hba data structure.
7825 * @cq_event: pointer to the completion queue event to be freed.
7826 *
7827 * This routine is the lock free version of the API invoked to release a
7828 * completion-queue event back into the free pool.
7829 **/
7830void
7831__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
7832 struct lpfc_cq_event *cq_event)
7833{
7834 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
7835}
7836
7837/**
7838 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
7839 * @phba: pointer to lpfc hba data structure.
7840 * @cq_event: pointer to the completion queue event to be freed.
7841 *
7842 * This routine is the lock version of the API invoked to release a
7843 * completion-queue event back into the free pool.
7844 **/
7845void
7846lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
7847 struct lpfc_cq_event *cq_event)
7848{
7849 unsigned long iflags;
7850 spin_lock_irqsave(&phba->hbalock, iflags);
7851 __lpfc_sli4_cq_event_release(phba, cq_event);
7852 spin_unlock_irqrestore(&phba->hbalock, iflags);
7853}
7854
7855/**
7856 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
7857 * @phba: pointer to lpfc hba data structure.
7858 *
7859 * This routine is to free all the pending completion-queue events to the
7860 * back into the free pool for device reset.
7861 **/
7862static void
7863lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
7864{
7865 LIST_HEAD(cqelist);
7866 struct lpfc_cq_event *cqe;
7867 unsigned long iflags;
7868
7869 /* Retrieve all the pending WCQEs from pending WCQE lists */
7870 spin_lock_irqsave(&phba->hbalock, iflags);
7871 /* Pending FCP XRI abort events */
7872 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
7873 &cqelist);
7874 /* Pending ELS XRI abort events */
7875 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
7876 &cqelist);
7877 /* Pending asynnc events */
7878 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
7879 &cqelist);
7880 spin_unlock_irqrestore(&phba->hbalock, iflags);
7881
7882 while (!list_empty(&cqelist)) {
7883 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
7884 lpfc_sli4_cq_event_release(phba, cqe);
7885 }
7886}
7887
7888/**
7889 * lpfc_pci_function_reset - Reset pci function.
7890 * @phba: pointer to lpfc hba data structure.
7891 *
7892 * This routine is invoked to request a PCI function reset. It will destroys
7893 * all resources assigned to the PCI function which originates this request.
7894 *
7895 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007896 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007897 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007898 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007899 **/
7900int
7901lpfc_pci_function_reset(struct lpfc_hba *phba)
7902{
7903 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05007904 uint32_t rc = 0, if_type;
James Smartda0436e2009-05-22 14:51:39 -04007905 uint32_t shdr_status, shdr_add_status;
James Smart2f6fa2c2014-09-03 12:57:08 -04007906 uint32_t rdy_chk;
7907 uint32_t port_reset = 0;
James Smartda0436e2009-05-22 14:51:39 -04007908 union lpfc_sli4_cfg_shdr *shdr;
James Smart2fcee4b2010-12-15 17:57:46 -05007909 struct lpfc_register reg_data;
James Smart2b81f942012-03-01 22:37:18 -05007910 uint16_t devid;
James Smartda0436e2009-05-22 14:51:39 -04007911
James Smart2fcee4b2010-12-15 17:57:46 -05007912 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7913 switch (if_type) {
7914 case LPFC_SLI_INTF_IF_TYPE_0:
7915 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
7916 GFP_KERNEL);
7917 if (!mboxq) {
7918 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7919 "0494 Unable to allocate memory for "
7920 "issuing SLI_FUNCTION_RESET mailbox "
7921 "command\n");
7922 return -ENOMEM;
7923 }
7924
7925 /* Setup PCI function reset mailbox-ioctl command */
7926 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7927 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
7928 LPFC_SLI4_MBX_EMBED);
7929 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7930 shdr = (union lpfc_sli4_cfg_shdr *)
7931 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7932 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7933 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
7934 &shdr->response);
7935 if (rc != MBX_TIMEOUT)
7936 mempool_free(mboxq, phba->mbox_mem_pool);
7937 if (shdr_status || shdr_add_status || rc) {
7938 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7939 "0495 SLI_FUNCTION_RESET mailbox "
7940 "failed with status x%x add_status x%x,"
7941 " mbx status x%x\n",
7942 shdr_status, shdr_add_status, rc);
7943 rc = -ENXIO;
7944 }
7945 break;
7946 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart2f6fa2c2014-09-03 12:57:08 -04007947wait:
7948 /*
7949 * Poll the Port Status Register and wait for RDY for
7950 * up to 30 seconds. If the port doesn't respond, treat
7951 * it as an error.
7952 */
7953 for (rdy_chk = 0; rdy_chk < 3000; rdy_chk++) {
7954 if (lpfc_readl(phba->sli4_hba.u.if_type2.
7955 STATUSregaddr, &reg_data.word0)) {
7956 rc = -ENODEV;
7957 goto out;
7958 }
7959 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
7960 break;
7961 msleep(20);
7962 }
7963
7964 if (!bf_get(lpfc_sliport_status_rdy, &reg_data)) {
7965 phba->work_status[0] = readl(
7966 phba->sli4_hba.u.if_type2.ERR1regaddr);
7967 phba->work_status[1] = readl(
7968 phba->sli4_hba.u.if_type2.ERR2regaddr);
7969 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7970 "2890 Port not ready, port status reg "
7971 "0x%x error 1=0x%x, error 2=0x%x\n",
7972 reg_data.word0,
7973 phba->work_status[0],
7974 phba->work_status[1]);
7975 rc = -ENODEV;
7976 goto out;
7977 }
7978
7979 if (!port_reset) {
7980 /*
7981 * Reset the port now
7982 */
James Smart2fcee4b2010-12-15 17:57:46 -05007983 reg_data.word0 = 0;
7984 bf_set(lpfc_sliport_ctrl_end, &reg_data,
7985 LPFC_SLIPORT_LITTLE_ENDIAN);
7986 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
7987 LPFC_SLIPORT_INIT_PORT);
7988 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
7989 CTRLregaddr);
James Smart8fcb8ac2012-03-01 22:35:58 -05007990 /* flush */
James Smart2b81f942012-03-01 22:37:18 -05007991 pci_read_config_word(phba->pcidev,
7992 PCI_DEVICE_ID, &devid);
James Smart2fcee4b2010-12-15 17:57:46 -05007993
James Smart2f6fa2c2014-09-03 12:57:08 -04007994 port_reset = 1;
7995 msleep(20);
7996 goto wait;
7997 } else if (bf_get(lpfc_sliport_status_rn, &reg_data)) {
7998 rc = -ENODEV;
7999 goto out;
James Smart2fcee4b2010-12-15 17:57:46 -05008000 }
8001 break;
James Smart2f6fa2c2014-09-03 12:57:08 -04008002
James Smart2fcee4b2010-12-15 17:57:46 -05008003 case LPFC_SLI_INTF_IF_TYPE_1:
8004 default:
8005 break;
James Smartda0436e2009-05-22 14:51:39 -04008006 }
8007
James Smart73d91e52011-10-10 21:32:10 -04008008out:
James Smart2fcee4b2010-12-15 17:57:46 -05008009 /* Catch the not-ready port failure after a port reset. */
James Smart2f6fa2c2014-09-03 12:57:08 -04008010 if (rc) {
James Smart229adb02013-04-17 20:16:51 -04008011 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8012 "3317 HBA not functional: IP Reset Failed "
James Smart2f6fa2c2014-09-03 12:57:08 -04008013 "try: echo fw_reset > board_mode\n");
James Smart2fcee4b2010-12-15 17:57:46 -05008014 rc = -ENODEV;
James Smart229adb02013-04-17 20:16:51 -04008015 }
James Smart2fcee4b2010-12-15 17:57:46 -05008016
James Smartda0436e2009-05-22 14:51:39 -04008017 return rc;
8018}
8019
8020/**
James Smartda0436e2009-05-22 14:51:39 -04008021 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
8022 * @phba: pointer to lpfc hba data structure.
8023 *
8024 * This routine is invoked to set up the PCI device memory space for device
8025 * with SLI-4 interface spec.
8026 *
8027 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008028 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008029 * other values - error
8030 **/
8031static int
8032lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
8033{
8034 struct pci_dev *pdev;
8035 unsigned long bar0map_len, bar1map_len, bar2map_len;
8036 int error = -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -05008037 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04008038
8039 /* Obtain PCI device reference */
8040 if (!phba->pcidev)
8041 return error;
8042 else
8043 pdev = phba->pcidev;
8044
8045 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05008046 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
8047 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
8048 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
8049 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smartda0436e2009-05-22 14:51:39 -04008050 return error;
Michael Reed8e685972009-09-18 12:02:05 -05008051 }
8052 }
James Smartda0436e2009-05-22 14:51:39 -04008053
James Smart2fcee4b2010-12-15 17:57:46 -05008054 /*
8055 * The BARs and register set definitions and offset locations are
8056 * dependent on the if_type.
8057 */
8058 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
8059 &phba->sli4_hba.sli_intf.word0)) {
8060 return error;
8061 }
8062
8063 /* There is no SLI3 failback for SLI4 devices. */
8064 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
8065 LPFC_SLI_INTF_VALID) {
8066 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8067 "2894 SLI_INTF reg contents invalid "
8068 "sli_intf reg 0x%x\n",
8069 phba->sli4_hba.sli_intf.word0);
8070 return error;
8071 }
8072
8073 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
8074 /*
8075 * Get the bus address of SLI4 device Bar regions and the
8076 * number of bytes required by each mapping. The mapping of the
8077 * particular PCI BARs regions is dependent on the type of
8078 * SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -04008079 */
James Smartf5ca6f22013-09-06 12:21:09 -04008080 if (pci_resource_start(pdev, PCI_64BIT_BAR0)) {
8081 phba->pci_bar0_map = pci_resource_start(pdev, PCI_64BIT_BAR0);
8082 bar0map_len = pci_resource_len(pdev, PCI_64BIT_BAR0);
James Smart2fcee4b2010-12-15 17:57:46 -05008083
8084 /*
8085 * Map SLI4 PCI Config Space Register base to a kernel virtual
8086 * addr
8087 */
8088 phba->sli4_hba.conf_regs_memmap_p =
8089 ioremap(phba->pci_bar0_map, bar0map_len);
8090 if (!phba->sli4_hba.conf_regs_memmap_p) {
8091 dev_printk(KERN_ERR, &pdev->dev,
8092 "ioremap failed for SLI4 PCI config "
8093 "registers.\n");
8094 goto out;
8095 }
James Smartf5ca6f22013-09-06 12:21:09 -04008096 phba->pci_bar0_memmap_p = phba->sli4_hba.conf_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05008097 /* Set up BAR0 PCI config space register memory map */
8098 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smart1dfb5a42010-02-12 14:40:50 -05008099 } else {
8100 phba->pci_bar0_map = pci_resource_start(pdev, 1);
8101 bar0map_len = pci_resource_len(pdev, 1);
James Smart2fcee4b2010-12-15 17:57:46 -05008102 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8103 dev_printk(KERN_ERR, &pdev->dev,
8104 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
8105 goto out;
8106 }
8107 phba->sli4_hba.conf_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04008108 ioremap(phba->pci_bar0_map, bar0map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05008109 if (!phba->sli4_hba.conf_regs_memmap_p) {
8110 dev_printk(KERN_ERR, &pdev->dev,
8111 "ioremap failed for SLI4 PCI config "
8112 "registers.\n");
8113 goto out;
8114 }
8115 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smartda0436e2009-05-22 14:51:39 -04008116 }
8117
James Smartc31098c2011-04-16 11:03:33 -04008118 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
James Smartf5ca6f22013-09-06 12:21:09 -04008119 (pci_resource_start(pdev, PCI_64BIT_BAR2))) {
James Smart2fcee4b2010-12-15 17:57:46 -05008120 /*
8121 * Map SLI4 if type 0 HBA Control Register base to a kernel
8122 * virtual address and setup the registers.
8123 */
James Smartf5ca6f22013-09-06 12:21:09 -04008124 phba->pci_bar1_map = pci_resource_start(pdev, PCI_64BIT_BAR2);
8125 bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
James Smart2fcee4b2010-12-15 17:57:46 -05008126 phba->sli4_hba.ctrl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04008127 ioremap(phba->pci_bar1_map, bar1map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05008128 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
8129 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04008130 "ioremap failed for SLI4 HBA control registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05008131 goto out_iounmap_conf;
8132 }
James Smartf5ca6f22013-09-06 12:21:09 -04008133 phba->pci_bar2_memmap_p = phba->sli4_hba.ctrl_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05008134 lpfc_sli4_bar1_register_memmap(phba);
James Smartda0436e2009-05-22 14:51:39 -04008135 }
8136
James Smartc31098c2011-04-16 11:03:33 -04008137 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
James Smartf5ca6f22013-09-06 12:21:09 -04008138 (pci_resource_start(pdev, PCI_64BIT_BAR4))) {
James Smart2fcee4b2010-12-15 17:57:46 -05008139 /*
8140 * Map SLI4 if type 0 HBA Doorbell Register base to a kernel
8141 * virtual address and setup the registers.
8142 */
James Smartf5ca6f22013-09-06 12:21:09 -04008143 phba->pci_bar2_map = pci_resource_start(pdev, PCI_64BIT_BAR4);
8144 bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
James Smart2fcee4b2010-12-15 17:57:46 -05008145 phba->sli4_hba.drbl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04008146 ioremap(phba->pci_bar2_map, bar2map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05008147 if (!phba->sli4_hba.drbl_regs_memmap_p) {
8148 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04008149 "ioremap failed for SLI4 HBA doorbell registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05008150 goto out_iounmap_ctrl;
8151 }
James Smartf5ca6f22013-09-06 12:21:09 -04008152 phba->pci_bar4_memmap_p = phba->sli4_hba.drbl_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05008153 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
8154 if (error)
8155 goto out_iounmap_all;
James Smartda0436e2009-05-22 14:51:39 -04008156 }
8157
James Smartda0436e2009-05-22 14:51:39 -04008158 return 0;
8159
8160out_iounmap_all:
8161 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
8162out_iounmap_ctrl:
8163 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
8164out_iounmap_conf:
8165 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8166out:
8167 return error;
8168}
8169
8170/**
8171 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
8172 * @phba: pointer to lpfc hba data structure.
8173 *
8174 * This routine is invoked to unset the PCI device memory space for device
8175 * with SLI-4 interface spec.
8176 **/
8177static void
8178lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
8179{
James Smart2e90f4b2011-12-13 13:22:37 -05008180 uint32_t if_type;
8181 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smartda0436e2009-05-22 14:51:39 -04008182
James Smart2e90f4b2011-12-13 13:22:37 -05008183 switch (if_type) {
8184 case LPFC_SLI_INTF_IF_TYPE_0:
8185 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
8186 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
8187 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8188 break;
8189 case LPFC_SLI_INTF_IF_TYPE_2:
8190 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8191 break;
8192 case LPFC_SLI_INTF_IF_TYPE_1:
8193 default:
8194 dev_printk(KERN_ERR, &phba->pcidev->dev,
8195 "FATAL - unsupported SLI4 interface type - %d\n",
8196 if_type);
8197 break;
8198 }
James Smartda0436e2009-05-22 14:51:39 -04008199}
8200
8201/**
James Smart3772a992009-05-22 14:50:54 -04008202 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
8203 * @phba: pointer to lpfc hba data structure.
8204 *
8205 * This routine is invoked to enable the MSI-X interrupt vectors to device
Alexander Gordeev4f871e12014-09-03 12:56:29 -04008206 * with SLI-3 interface specs. The kernel function pci_enable_msix_exact()
8207 * is called to enable the MSI-X vectors. Note that pci_enable_msix_exact(),
8208 * once invoked, enables either all or nothing, depending on the current
James Smart3772a992009-05-22 14:50:54 -04008209 * availability of PCI vector resources. The device driver is responsible
8210 * for calling the individual request_irq() to register each MSI-X vector
8211 * with a interrupt handler, which is done in this function. Note that
8212 * later when device is unloading, the driver should always call free_irq()
8213 * on all MSI-X vectors it has done request_irq() on before calling
8214 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
8215 * will be left with MSI-X enabled and leaks its vectors.
8216 *
8217 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008218 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008219 * other values - error
8220 **/
8221static int
8222lpfc_sli_enable_msix(struct lpfc_hba *phba)
8223{
8224 int rc, i;
8225 LPFC_MBOXQ_t *pmb;
8226
8227 /* Set up MSI-X multi-message vectors */
8228 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8229 phba->msix_entries[i].entry = i;
8230
8231 /* Configure MSI-X capability structure */
Alexander Gordeev4f871e12014-09-03 12:56:29 -04008232 rc = pci_enable_msix_exact(phba->pcidev, phba->msix_entries,
8233 LPFC_MSIX_VECTORS);
James Smart3772a992009-05-22 14:50:54 -04008234 if (rc) {
8235 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8236 "0420 PCI enable MSI-X failed (%d)\n", rc);
Alexander Gordeev029165a2014-07-16 20:05:15 +02008237 goto vec_fail_out;
James Smart3772a992009-05-22 14:50:54 -04008238 }
8239 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8240 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8241 "0477 MSI-X entry[%d]: vector=x%x "
8242 "message=%d\n", i,
8243 phba->msix_entries[i].vector,
8244 phba->msix_entries[i].entry);
8245 /*
8246 * Assign MSI-X vectors to interrupt handlers
8247 */
8248
8249 /* vector-0 is associated to slow-path handler */
8250 rc = request_irq(phba->msix_entries[0].vector,
8251 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
8252 LPFC_SP_DRIVER_HANDLER_NAME, phba);
8253 if (rc) {
8254 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8255 "0421 MSI-X slow-path request_irq failed "
8256 "(%d)\n", rc);
8257 goto msi_fail_out;
8258 }
8259
8260 /* vector-1 is associated to fast-path handler */
8261 rc = request_irq(phba->msix_entries[1].vector,
8262 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
8263 LPFC_FP_DRIVER_HANDLER_NAME, phba);
8264
8265 if (rc) {
8266 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8267 "0429 MSI-X fast-path request_irq failed "
8268 "(%d)\n", rc);
8269 goto irq_fail_out;
8270 }
8271
8272 /*
8273 * Configure HBA MSI-X attention conditions to messages
8274 */
8275 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8276
8277 if (!pmb) {
8278 rc = -ENOMEM;
8279 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8280 "0474 Unable to allocate memory for issuing "
8281 "MBOX_CONFIG_MSI command\n");
8282 goto mem_fail_out;
8283 }
8284 rc = lpfc_config_msi(phba, pmb);
8285 if (rc)
8286 goto mbx_fail_out;
8287 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
8288 if (rc != MBX_SUCCESS) {
8289 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
8290 "0351 Config MSI mailbox command failed, "
8291 "mbxCmd x%x, mbxStatus x%x\n",
8292 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
8293 goto mbx_fail_out;
8294 }
8295
8296 /* Free memory allocated for mailbox command */
8297 mempool_free(pmb, phba->mbox_mem_pool);
8298 return rc;
8299
8300mbx_fail_out:
8301 /* Free memory allocated for mailbox command */
8302 mempool_free(pmb, phba->mbox_mem_pool);
8303
8304mem_fail_out:
8305 /* free the irq already requested */
8306 free_irq(phba->msix_entries[1].vector, phba);
8307
8308irq_fail_out:
8309 /* free the irq already requested */
8310 free_irq(phba->msix_entries[0].vector, phba);
8311
8312msi_fail_out:
8313 /* Unconfigure MSI-X capability structure */
8314 pci_disable_msix(phba->pcidev);
Alexander Gordeev029165a2014-07-16 20:05:15 +02008315
8316vec_fail_out:
James Smart3772a992009-05-22 14:50:54 -04008317 return rc;
8318}
8319
8320/**
8321 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
8322 * @phba: pointer to lpfc hba data structure.
8323 *
8324 * This routine is invoked to release the MSI-X vectors and then disable the
8325 * MSI-X interrupt mode to device with SLI-3 interface spec.
8326 **/
8327static void
8328lpfc_sli_disable_msix(struct lpfc_hba *phba)
8329{
8330 int i;
8331
8332 /* Free up MSI-X multi-message vectors */
8333 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8334 free_irq(phba->msix_entries[i].vector, phba);
8335 /* Disable MSI-X */
8336 pci_disable_msix(phba->pcidev);
8337
8338 return;
8339}
8340
8341/**
8342 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
8343 * @phba: pointer to lpfc hba data structure.
8344 *
8345 * This routine is invoked to enable the MSI interrupt mode to device with
8346 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
8347 * enable the MSI vector. The device driver is responsible for calling the
8348 * request_irq() to register MSI vector with a interrupt the handler, which
8349 * is done in this function.
8350 *
8351 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008352 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008353 * other values - error
8354 */
8355static int
8356lpfc_sli_enable_msi(struct lpfc_hba *phba)
8357{
8358 int rc;
8359
8360 rc = pci_enable_msi(phba->pcidev);
8361 if (!rc)
8362 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8363 "0462 PCI enable MSI mode success.\n");
8364 else {
8365 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8366 "0471 PCI enable MSI mode failed (%d)\n", rc);
8367 return rc;
8368 }
8369
8370 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
8371 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8372 if (rc) {
8373 pci_disable_msi(phba->pcidev);
8374 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8375 "0478 MSI request_irq failed (%d)\n", rc);
8376 }
8377 return rc;
8378}
8379
8380/**
8381 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
8382 * @phba: pointer to lpfc hba data structure.
8383 *
8384 * This routine is invoked to disable the MSI interrupt mode to device with
8385 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
8386 * done request_irq() on before calling pci_disable_msi(). Failure to do so
8387 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
8388 * its vector.
8389 */
8390static void
8391lpfc_sli_disable_msi(struct lpfc_hba *phba)
8392{
8393 free_irq(phba->pcidev->irq, phba);
8394 pci_disable_msi(phba->pcidev);
8395 return;
8396}
8397
8398/**
8399 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
8400 * @phba: pointer to lpfc hba data structure.
8401 *
8402 * This routine is invoked to enable device interrupt and associate driver's
8403 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
8404 * spec. Depends on the interrupt mode configured to the driver, the driver
8405 * will try to fallback from the configured interrupt mode to an interrupt
8406 * mode which is supported by the platform, kernel, and device in the order
8407 * of:
8408 * MSI-X -> MSI -> IRQ.
8409 *
8410 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008411 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008412 * other values - error
8413 **/
8414static uint32_t
8415lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
8416{
8417 uint32_t intr_mode = LPFC_INTR_ERROR;
8418 int retval;
8419
8420 if (cfg_mode == 2) {
8421 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
8422 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
8423 if (!retval) {
8424 /* Now, try to enable MSI-X interrupt mode */
8425 retval = lpfc_sli_enable_msix(phba);
8426 if (!retval) {
8427 /* Indicate initialization to MSI-X mode */
8428 phba->intr_type = MSIX;
8429 intr_mode = 2;
8430 }
8431 }
8432 }
8433
8434 /* Fallback to MSI if MSI-X initialization failed */
8435 if (cfg_mode >= 1 && phba->intr_type == NONE) {
8436 retval = lpfc_sli_enable_msi(phba);
8437 if (!retval) {
8438 /* Indicate initialization to MSI mode */
8439 phba->intr_type = MSI;
8440 intr_mode = 1;
8441 }
8442 }
8443
8444 /* Fallback to INTx if both MSI-X/MSI initalization failed */
8445 if (phba->intr_type == NONE) {
8446 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
8447 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8448 if (!retval) {
8449 /* Indicate initialization to INTx mode */
8450 phba->intr_type = INTx;
8451 intr_mode = 0;
8452 }
8453 }
8454 return intr_mode;
8455}
8456
8457/**
8458 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
8459 * @phba: pointer to lpfc hba data structure.
8460 *
8461 * This routine is invoked to disable device interrupt and disassociate the
8462 * driver's interrupt handler(s) from interrupt vector(s) to device with
8463 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
8464 * release the interrupt vector(s) for the message signaled interrupt.
8465 **/
8466static void
8467lpfc_sli_disable_intr(struct lpfc_hba *phba)
8468{
8469 /* Disable the currently initialized interrupt mode */
8470 if (phba->intr_type == MSIX)
8471 lpfc_sli_disable_msix(phba);
8472 else if (phba->intr_type == MSI)
8473 lpfc_sli_disable_msi(phba);
8474 else if (phba->intr_type == INTx)
8475 free_irq(phba->pcidev->irq, phba);
8476
8477 /* Reset interrupt management states */
8478 phba->intr_type = NONE;
8479 phba->sli.slistat.sli_intr = 0;
8480
8481 return;
8482}
8483
8484/**
James Smart7bb03bb2013-04-17 20:19:16 -04008485 * lpfc_find_next_cpu - Find next available CPU that matches the phys_id
8486 * @phba: pointer to lpfc hba data structure.
8487 *
8488 * Find next available CPU to use for IRQ to CPU affinity.
8489 */
8490static int
8491lpfc_find_next_cpu(struct lpfc_hba *phba, uint32_t phys_id)
8492{
8493 struct lpfc_vector_map_info *cpup;
8494 int cpu;
8495
8496 cpup = phba->sli4_hba.cpu_map;
8497 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8498 /* CPU must be online */
8499 if (cpu_online(cpu)) {
8500 if ((cpup->irq == LPFC_VECTOR_MAP_EMPTY) &&
8501 (lpfc_used_cpu[cpu] == LPFC_VECTOR_MAP_EMPTY) &&
8502 (cpup->phys_id == phys_id)) {
8503 return cpu;
8504 }
8505 }
8506 cpup++;
8507 }
8508
8509 /*
8510 * If we get here, we have used ALL CPUs for the specific
8511 * phys_id. Now we need to clear out lpfc_used_cpu and start
8512 * reusing CPUs.
8513 */
8514
8515 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8516 if (lpfc_used_cpu[cpu] == phys_id)
8517 lpfc_used_cpu[cpu] = LPFC_VECTOR_MAP_EMPTY;
8518 }
8519
8520 cpup = phba->sli4_hba.cpu_map;
8521 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8522 /* CPU must be online */
8523 if (cpu_online(cpu)) {
8524 if ((cpup->irq == LPFC_VECTOR_MAP_EMPTY) &&
8525 (cpup->phys_id == phys_id)) {
8526 return cpu;
8527 }
8528 }
8529 cpup++;
8530 }
8531 return LPFC_VECTOR_MAP_EMPTY;
8532}
8533
8534/**
8535 * lpfc_sli4_set_affinity - Set affinity for HBA IRQ vectors
8536 * @phba: pointer to lpfc hba data structure.
8537 * @vectors: number of HBA vectors
8538 *
8539 * Affinitize MSIX IRQ vectors to CPUs. Try to equally spread vector
8540 * affinization across multple physical CPUs (numa nodes).
8541 * In addition, this routine will assign an IO channel for each CPU
8542 * to use when issuing I/Os.
8543 */
8544static int
8545lpfc_sli4_set_affinity(struct lpfc_hba *phba, int vectors)
8546{
8547 int i, idx, saved_chann, used_chann, cpu, phys_id;
James Smartec2087a2013-09-06 12:20:36 -04008548 int max_phys_id, min_phys_id;
8549 int num_io_channel, first_cpu, chan;
James Smart7bb03bb2013-04-17 20:19:16 -04008550 struct lpfc_vector_map_info *cpup;
8551#ifdef CONFIG_X86
8552 struct cpuinfo_x86 *cpuinfo;
8553#endif
8554 struct cpumask *mask;
8555 uint8_t chann[LPFC_FCP_IO_CHAN_MAX+1];
8556
8557 /* If there is no mapping, just return */
8558 if (!phba->cfg_fcp_cpu_map)
8559 return 1;
8560
8561 /* Init cpu_map array */
8562 memset(phba->sli4_hba.cpu_map, 0xff,
8563 (sizeof(struct lpfc_vector_map_info) *
8564 phba->sli4_hba.num_present_cpu));
8565
8566 max_phys_id = 0;
James Smartec2087a2013-09-06 12:20:36 -04008567 min_phys_id = 0xff;
James Smart7bb03bb2013-04-17 20:19:16 -04008568 phys_id = 0;
8569 num_io_channel = 0;
8570 first_cpu = LPFC_VECTOR_MAP_EMPTY;
8571
8572 /* Update CPU map with physical id and core id of each CPU */
8573 cpup = phba->sli4_hba.cpu_map;
8574 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8575#ifdef CONFIG_X86
8576 cpuinfo = &cpu_data(cpu);
8577 cpup->phys_id = cpuinfo->phys_proc_id;
8578 cpup->core_id = cpuinfo->cpu_core_id;
8579#else
8580 /* No distinction between CPUs for other platforms */
8581 cpup->phys_id = 0;
8582 cpup->core_id = 0;
8583#endif
8584
8585 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8586 "3328 CPU physid %d coreid %d\n",
8587 cpup->phys_id, cpup->core_id);
8588
8589 if (cpup->phys_id > max_phys_id)
8590 max_phys_id = cpup->phys_id;
James Smartec2087a2013-09-06 12:20:36 -04008591 if (cpup->phys_id < min_phys_id)
8592 min_phys_id = cpup->phys_id;
James Smart7bb03bb2013-04-17 20:19:16 -04008593 cpup++;
8594 }
8595
James Smartec2087a2013-09-06 12:20:36 -04008596 phys_id = min_phys_id;
James Smart7bb03bb2013-04-17 20:19:16 -04008597 /* Now associate the HBA vectors with specific CPUs */
8598 for (idx = 0; idx < vectors; idx++) {
8599 cpup = phba->sli4_hba.cpu_map;
8600 cpu = lpfc_find_next_cpu(phba, phys_id);
8601 if (cpu == LPFC_VECTOR_MAP_EMPTY) {
8602
8603 /* Try for all phys_id's */
8604 for (i = 1; i < max_phys_id; i++) {
8605 phys_id++;
8606 if (phys_id > max_phys_id)
James Smartec2087a2013-09-06 12:20:36 -04008607 phys_id = min_phys_id;
James Smart7bb03bb2013-04-17 20:19:16 -04008608 cpu = lpfc_find_next_cpu(phba, phys_id);
8609 if (cpu == LPFC_VECTOR_MAP_EMPTY)
8610 continue;
8611 goto found;
8612 }
8613
James Smartec2087a2013-09-06 12:20:36 -04008614 /* Use round robin for scheduling */
8615 phba->cfg_fcp_io_sched = LPFC_FCP_SCHED_ROUND_ROBIN;
8616 chan = 0;
8617 cpup = phba->sli4_hba.cpu_map;
8618 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
8619 cpup->channel_id = chan;
8620 cpup++;
8621 chan++;
8622 if (chan >= phba->cfg_fcp_io_channel)
8623 chan = 0;
8624 }
8625
James Smart7bb03bb2013-04-17 20:19:16 -04008626 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8627 "3329 Cannot set affinity:"
8628 "Error mapping vector %d (%d)\n",
8629 idx, vectors);
8630 return 0;
8631 }
8632found:
8633 cpup += cpu;
8634 if (phba->cfg_fcp_cpu_map == LPFC_DRIVER_CPU_MAP)
8635 lpfc_used_cpu[cpu] = phys_id;
8636
8637 /* Associate vector with selected CPU */
8638 cpup->irq = phba->sli4_hba.msix_entries[idx].vector;
8639
8640 /* Associate IO channel with selected CPU */
8641 cpup->channel_id = idx;
8642 num_io_channel++;
8643
8644 if (first_cpu == LPFC_VECTOR_MAP_EMPTY)
8645 first_cpu = cpu;
8646
8647 /* Now affinitize to the selected CPU */
8648 mask = &cpup->maskbits;
8649 cpumask_clear(mask);
8650 cpumask_set_cpu(cpu, mask);
8651 i = irq_set_affinity_hint(phba->sli4_hba.msix_entries[idx].
8652 vector, mask);
8653
8654 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8655 "3330 Set Affinity: CPU %d channel %d "
8656 "irq %d (%x)\n",
8657 cpu, cpup->channel_id,
8658 phba->sli4_hba.msix_entries[idx].vector, i);
8659
8660 /* Spread vector mapping across multple physical CPU nodes */
8661 phys_id++;
8662 if (phys_id > max_phys_id)
James Smartec2087a2013-09-06 12:20:36 -04008663 phys_id = min_phys_id;
James Smart7bb03bb2013-04-17 20:19:16 -04008664 }
8665
8666 /*
8667 * Finally fill in the IO channel for any remaining CPUs.
8668 * At this point, all IO channels have been assigned to a specific
8669 * MSIx vector, mapped to a specific CPU.
8670 * Base the remaining IO channel assigned, to IO channels already
8671 * assigned to other CPUs on the same phys_id.
8672 */
James Smartec2087a2013-09-06 12:20:36 -04008673 for (i = min_phys_id; i <= max_phys_id; i++) {
James Smart7bb03bb2013-04-17 20:19:16 -04008674 /*
8675 * If there are no io channels already mapped to
8676 * this phys_id, just round robin thru the io_channels.
8677 * Setup chann[] for round robin.
8678 */
8679 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++)
8680 chann[idx] = idx;
8681
8682 saved_chann = 0;
8683 used_chann = 0;
8684
8685 /*
8686 * First build a list of IO channels already assigned
8687 * to this phys_id before reassigning the same IO
8688 * channels to the remaining CPUs.
8689 */
8690 cpup = phba->sli4_hba.cpu_map;
8691 cpu = first_cpu;
8692 cpup += cpu;
8693 for (idx = 0; idx < phba->sli4_hba.num_present_cpu;
8694 idx++) {
8695 if (cpup->phys_id == i) {
8696 /*
8697 * Save any IO channels that are
8698 * already mapped to this phys_id.
8699 */
8700 if (cpup->irq != LPFC_VECTOR_MAP_EMPTY) {
8701 chann[saved_chann] =
8702 cpup->channel_id;
8703 saved_chann++;
8704 goto out;
8705 }
8706
8707 /* See if we are using round-robin */
8708 if (saved_chann == 0)
8709 saved_chann =
8710 phba->cfg_fcp_io_channel;
8711
8712 /* Associate next IO channel with CPU */
8713 cpup->channel_id = chann[used_chann];
8714 num_io_channel++;
8715 used_chann++;
8716 if (used_chann == saved_chann)
8717 used_chann = 0;
8718
8719 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8720 "3331 Set IO_CHANN "
8721 "CPU %d channel %d\n",
8722 idx, cpup->channel_id);
8723 }
8724out:
8725 cpu++;
8726 if (cpu >= phba->sli4_hba.num_present_cpu) {
8727 cpup = phba->sli4_hba.cpu_map;
8728 cpu = 0;
8729 } else {
8730 cpup++;
8731 }
8732 }
8733 }
8734
8735 if (phba->sli4_hba.num_online_cpu != phba->sli4_hba.num_present_cpu) {
8736 cpup = phba->sli4_hba.cpu_map;
8737 for (idx = 0; idx < phba->sli4_hba.num_present_cpu; idx++) {
8738 if (cpup->channel_id == LPFC_VECTOR_MAP_EMPTY) {
8739 cpup->channel_id = 0;
8740 num_io_channel++;
8741
8742 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8743 "3332 Assign IO_CHANN "
8744 "CPU %d channel %d\n",
8745 idx, cpup->channel_id);
8746 }
8747 cpup++;
8748 }
8749 }
8750
8751 /* Sanity check */
8752 if (num_io_channel != phba->sli4_hba.num_present_cpu)
8753 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8754 "3333 Set affinity mismatch:"
James Smartec2087a2013-09-06 12:20:36 -04008755 "%d chann != %d cpus: %d vectors\n",
James Smart7bb03bb2013-04-17 20:19:16 -04008756 num_io_channel, phba->sli4_hba.num_present_cpu,
8757 vectors);
8758
James Smartec2087a2013-09-06 12:20:36 -04008759 /* Enable using cpu affinity for scheduling */
James Smart7bb03bb2013-04-17 20:19:16 -04008760 phba->cfg_fcp_io_sched = LPFC_FCP_SCHED_BY_CPU;
8761 return 1;
8762}
8763
8764
8765/**
James Smartda0436e2009-05-22 14:51:39 -04008766 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
8767 * @phba: pointer to lpfc hba data structure.
8768 *
8769 * This routine is invoked to enable the MSI-X interrupt vectors to device
Alexander Gordeev4f871e12014-09-03 12:56:29 -04008770 * with SLI-4 interface spec. The kernel function pci_enable_msix_range()
8771 * is called to enable the MSI-X vectors. The device driver is responsible
8772 * for calling the individual request_irq() to register each MSI-X vector
8773 * with a interrupt handler, which is done in this function. Note that
8774 * later when device is unloading, the driver should always call free_irq()
8775 * on all MSI-X vectors it has done request_irq() on before calling
8776 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
8777 * will be left with MSI-X enabled and leaks its vectors.
James Smartda0436e2009-05-22 14:51:39 -04008778 *
8779 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008780 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008781 * other values - error
8782 **/
8783static int
8784lpfc_sli4_enable_msix(struct lpfc_hba *phba)
8785{
James Smart75baf692010-06-08 18:31:21 -04008786 int vectors, rc, index;
James Smartda0436e2009-05-22 14:51:39 -04008787
8788 /* Set up MSI-X multi-message vectors */
James Smart82c3e9b2012-09-29 11:29:50 -04008789 for (index = 0; index < phba->cfg_fcp_io_channel; index++)
James Smartda0436e2009-05-22 14:51:39 -04008790 phba->sli4_hba.msix_entries[index].entry = index;
8791
8792 /* Configure MSI-X capability structure */
James Smart82c3e9b2012-09-29 11:29:50 -04008793 vectors = phba->cfg_fcp_io_channel;
James Smart1ba981f2014-02-20 09:56:45 -05008794 if (phba->cfg_fof) {
8795 phba->sli4_hba.msix_entries[index].entry = index;
8796 vectors++;
8797 }
Alexander Gordeev4f871e12014-09-03 12:56:29 -04008798 rc = pci_enable_msix_range(phba->pcidev, phba->sli4_hba.msix_entries,
8799 2, vectors);
8800 if (rc < 0) {
James Smartda0436e2009-05-22 14:51:39 -04008801 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8802 "0484 PCI enable MSI-X failed (%d)\n", rc);
Alexander Gordeev029165a2014-07-16 20:05:15 +02008803 goto vec_fail_out;
James Smartda0436e2009-05-22 14:51:39 -04008804 }
Alexander Gordeev4f871e12014-09-03 12:56:29 -04008805 vectors = rc;
James Smart75baf692010-06-08 18:31:21 -04008806
James Smartda0436e2009-05-22 14:51:39 -04008807 /* Log MSI-X vector assignment */
James Smart75baf692010-06-08 18:31:21 -04008808 for (index = 0; index < vectors; index++)
James Smartda0436e2009-05-22 14:51:39 -04008809 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8810 "0489 MSI-X entry[%d]: vector=x%x "
8811 "message=%d\n", index,
8812 phba->sli4_hba.msix_entries[index].vector,
8813 phba->sli4_hba.msix_entries[index].entry);
James Smart67d12732012-08-03 12:36:13 -04008814
James Smart7bb03bb2013-04-17 20:19:16 -04008815 /* Assign MSI-X vectors to interrupt handlers */
James Smart67d12732012-08-03 12:36:13 -04008816 for (index = 0; index < vectors; index++) {
James Smart4305f182012-08-03 12:36:33 -04008817 memset(&phba->sli4_hba.handler_name[index], 0, 16);
8818 sprintf((char *)&phba->sli4_hba.handler_name[index],
8819 LPFC_DRIVER_HANDLER_NAME"%d", index);
James Smartda0436e2009-05-22 14:51:39 -04008820
James Smart67d12732012-08-03 12:36:13 -04008821 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8822 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
James Smartba20c852012-08-03 12:36:52 -04008823 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].fcp_eq_in_use, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008824 if (phba->cfg_fof && (index == (vectors - 1)))
8825 rc = request_irq(
8826 phba->sli4_hba.msix_entries[index].vector,
8827 &lpfc_sli4_fof_intr_handler, IRQF_SHARED,
8828 (char *)&phba->sli4_hba.handler_name[index],
8829 &phba->sli4_hba.fcp_eq_hdl[index]);
8830 else
8831 rc = request_irq(
8832 phba->sli4_hba.msix_entries[index].vector,
James Smart67d12732012-08-03 12:36:13 -04008833 &lpfc_sli4_hba_intr_handler, IRQF_SHARED,
James Smart4305f182012-08-03 12:36:33 -04008834 (char *)&phba->sli4_hba.handler_name[index],
James Smart67d12732012-08-03 12:36:13 -04008835 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -04008836 if (rc) {
8837 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8838 "0486 MSI-X fast-path (%d) "
8839 "request_irq failed (%d)\n", index, rc);
8840 goto cfg_fail_out;
8841 }
8842 }
8843
James Smart1ba981f2014-02-20 09:56:45 -05008844 if (phba->cfg_fof)
8845 vectors--;
8846
James Smart82c3e9b2012-09-29 11:29:50 -04008847 if (vectors != phba->cfg_fcp_io_channel) {
8848 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8849 "3238 Reducing IO channels to match number of "
8850 "MSI-X vectors, requested %d got %d\n",
8851 phba->cfg_fcp_io_channel, vectors);
8852 phba->cfg_fcp_io_channel = vectors;
8853 }
James Smart7bb03bb2013-04-17 20:19:16 -04008854
8855 lpfc_sli4_set_affinity(phba, vectors);
James Smartda0436e2009-05-22 14:51:39 -04008856 return rc;
8857
8858cfg_fail_out:
8859 /* free the irq already requested */
James Smartacbd8612013-09-06 12:21:19 -04008860 for (--index; index >= 0; index--) {
8861 irq_set_affinity_hint(phba->sli4_hba.msix_entries[index].
8862 vector, NULL);
James Smart67d12732012-08-03 12:36:13 -04008863 free_irq(phba->sli4_hba.msix_entries[index].vector,
8864 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartacbd8612013-09-06 12:21:19 -04008865 }
James Smartda0436e2009-05-22 14:51:39 -04008866
James Smartda0436e2009-05-22 14:51:39 -04008867 /* Unconfigure MSI-X capability structure */
8868 pci_disable_msix(phba->pcidev);
Alexander Gordeev029165a2014-07-16 20:05:15 +02008869
8870vec_fail_out:
James Smartda0436e2009-05-22 14:51:39 -04008871 return rc;
8872}
8873
8874/**
8875 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
8876 * @phba: pointer to lpfc hba data structure.
8877 *
8878 * This routine is invoked to release the MSI-X vectors and then disable the
8879 * MSI-X interrupt mode to device with SLI-4 interface spec.
8880 **/
8881static void
8882lpfc_sli4_disable_msix(struct lpfc_hba *phba)
8883{
8884 int index;
8885
8886 /* Free up MSI-X multi-message vectors */
James Smartacbd8612013-09-06 12:21:19 -04008887 for (index = 0; index < phba->cfg_fcp_io_channel; index++) {
8888 irq_set_affinity_hint(phba->sli4_hba.msix_entries[index].
8889 vector, NULL);
James Smartda0436e2009-05-22 14:51:39 -04008890 free_irq(phba->sli4_hba.msix_entries[index].vector,
James Smart67d12732012-08-03 12:36:13 -04008891 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartacbd8612013-09-06 12:21:19 -04008892 }
James Smart1ba981f2014-02-20 09:56:45 -05008893 if (phba->cfg_fof) {
8894 free_irq(phba->sli4_hba.msix_entries[index].vector,
8895 &phba->sli4_hba.fcp_eq_hdl[index]);
8896 }
James Smartda0436e2009-05-22 14:51:39 -04008897 /* Disable MSI-X */
8898 pci_disable_msix(phba->pcidev);
8899
8900 return;
8901}
8902
8903/**
8904 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
8905 * @phba: pointer to lpfc hba data structure.
8906 *
8907 * This routine is invoked to enable the MSI interrupt mode to device with
8908 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
8909 * to enable the MSI vector. The device driver is responsible for calling
8910 * the request_irq() to register MSI vector with a interrupt the handler,
8911 * which is done in this function.
8912 *
8913 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008914 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008915 * other values - error
8916 **/
8917static int
8918lpfc_sli4_enable_msi(struct lpfc_hba *phba)
8919{
8920 int rc, index;
8921
8922 rc = pci_enable_msi(phba->pcidev);
8923 if (!rc)
8924 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8925 "0487 PCI enable MSI mode success.\n");
8926 else {
8927 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8928 "0488 PCI enable MSI mode failed (%d)\n", rc);
8929 return rc;
8930 }
8931
8932 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
8933 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8934 if (rc) {
8935 pci_disable_msi(phba->pcidev);
8936 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8937 "0490 MSI request_irq failed (%d)\n", rc);
James Smart75baf692010-06-08 18:31:21 -04008938 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008939 }
8940
James Smart67d12732012-08-03 12:36:13 -04008941 for (index = 0; index < phba->cfg_fcp_io_channel; index++) {
James Smartda0436e2009-05-22 14:51:39 -04008942 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8943 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
8944 }
8945
James Smart1ba981f2014-02-20 09:56:45 -05008946 if (phba->cfg_fof) {
8947 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8948 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
8949 }
James Smart75baf692010-06-08 18:31:21 -04008950 return 0;
James Smartda0436e2009-05-22 14:51:39 -04008951}
8952
8953/**
8954 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
8955 * @phba: pointer to lpfc hba data structure.
8956 *
8957 * This routine is invoked to disable the MSI interrupt mode to device with
8958 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
8959 * done request_irq() on before calling pci_disable_msi(). Failure to do so
8960 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
8961 * its vector.
8962 **/
8963static void
8964lpfc_sli4_disable_msi(struct lpfc_hba *phba)
8965{
8966 free_irq(phba->pcidev->irq, phba);
8967 pci_disable_msi(phba->pcidev);
8968 return;
8969}
8970
8971/**
8972 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
8973 * @phba: pointer to lpfc hba data structure.
8974 *
8975 * This routine is invoked to enable device interrupt and associate driver's
8976 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
8977 * interface spec. Depends on the interrupt mode configured to the driver,
8978 * the driver will try to fallback from the configured interrupt mode to an
8979 * interrupt mode which is supported by the platform, kernel, and device in
8980 * the order of:
8981 * MSI-X -> MSI -> IRQ.
8982 *
8983 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008984 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008985 * other values - error
8986 **/
8987static uint32_t
8988lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
8989{
8990 uint32_t intr_mode = LPFC_INTR_ERROR;
8991 int retval, index;
8992
8993 if (cfg_mode == 2) {
8994 /* Preparation before conf_msi mbox cmd */
8995 retval = 0;
8996 if (!retval) {
8997 /* Now, try to enable MSI-X interrupt mode */
8998 retval = lpfc_sli4_enable_msix(phba);
8999 if (!retval) {
9000 /* Indicate initialization to MSI-X mode */
9001 phba->intr_type = MSIX;
9002 intr_mode = 2;
9003 }
9004 }
9005 }
9006
9007 /* Fallback to MSI if MSI-X initialization failed */
9008 if (cfg_mode >= 1 && phba->intr_type == NONE) {
9009 retval = lpfc_sli4_enable_msi(phba);
9010 if (!retval) {
9011 /* Indicate initialization to MSI mode */
9012 phba->intr_type = MSI;
9013 intr_mode = 1;
9014 }
9015 }
9016
9017 /* Fallback to INTx if both MSI-X/MSI initalization failed */
9018 if (phba->intr_type == NONE) {
9019 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
9020 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
9021 if (!retval) {
9022 /* Indicate initialization to INTx mode */
9023 phba->intr_type = INTx;
9024 intr_mode = 0;
James Smart67d12732012-08-03 12:36:13 -04009025 for (index = 0; index < phba->cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04009026 index++) {
9027 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
9028 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
James Smartba20c852012-08-03 12:36:52 -04009029 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].
9030 fcp_eq_in_use, 1);
James Smartda0436e2009-05-22 14:51:39 -04009031 }
James Smart1ba981f2014-02-20 09:56:45 -05009032 if (phba->cfg_fof) {
9033 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
9034 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
9035 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].
9036 fcp_eq_in_use, 1);
9037 }
James Smartda0436e2009-05-22 14:51:39 -04009038 }
9039 }
9040 return intr_mode;
9041}
9042
9043/**
9044 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
9045 * @phba: pointer to lpfc hba data structure.
9046 *
9047 * This routine is invoked to disable device interrupt and disassociate
9048 * the driver's interrupt handler(s) from interrupt vector(s) to device
9049 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
9050 * will release the interrupt vector(s) for the message signaled interrupt.
9051 **/
9052static void
9053lpfc_sli4_disable_intr(struct lpfc_hba *phba)
9054{
9055 /* Disable the currently initialized interrupt mode */
9056 if (phba->intr_type == MSIX)
9057 lpfc_sli4_disable_msix(phba);
9058 else if (phba->intr_type == MSI)
9059 lpfc_sli4_disable_msi(phba);
9060 else if (phba->intr_type == INTx)
9061 free_irq(phba->pcidev->irq, phba);
9062
9063 /* Reset interrupt management states */
9064 phba->intr_type = NONE;
9065 phba->sli.slistat.sli_intr = 0;
9066
9067 return;
9068}
9069
9070/**
James Smart3772a992009-05-22 14:50:54 -04009071 * lpfc_unset_hba - Unset SLI3 hba device initialization
9072 * @phba: pointer to lpfc hba data structure.
9073 *
9074 * This routine is invoked to unset the HBA device initialization steps to
9075 * a device with SLI-3 interface spec.
9076 **/
9077static void
9078lpfc_unset_hba(struct lpfc_hba *phba)
9079{
9080 struct lpfc_vport *vport = phba->pport;
9081 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
9082
9083 spin_lock_irq(shost->host_lock);
9084 vport->load_flag |= FC_UNLOADING;
9085 spin_unlock_irq(shost->host_lock);
9086
James Smart72859902012-01-18 16:25:38 -05009087 kfree(phba->vpi_bmask);
9088 kfree(phba->vpi_ids);
9089
James Smart3772a992009-05-22 14:50:54 -04009090 lpfc_stop_hba_timers(phba);
9091
9092 phba->pport->work_port_events = 0;
9093
9094 lpfc_sli_hba_down(phba);
9095
9096 lpfc_sli_brdrestart(phba);
9097
9098 lpfc_sli_disable_intr(phba);
9099
9100 return;
9101}
9102
9103/**
James Smart5af5eee2010-10-22 11:06:38 -04009104 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
9105 * @phba: Pointer to HBA context object.
9106 *
9107 * This function is called in the SLI4 code path to wait for completion
9108 * of device's XRIs exchange busy. It will check the XRI exchange busy
9109 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
9110 * that, it will check the XRI exchange busy on outstanding FCP and ELS
9111 * I/Os every 30 seconds, log error message, and wait forever. Only when
9112 * all XRI exchange busy complete, the driver unload shall proceed with
9113 * invoking the function reset ioctl mailbox command to the CNA and the
9114 * the rest of the driver unload resource release.
9115 **/
9116static void
9117lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
9118{
9119 int wait_time = 0;
9120 int fcp_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
9121 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
9122
9123 while (!fcp_xri_cmpl || !els_xri_cmpl) {
9124 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
9125 if (!fcp_xri_cmpl)
9126 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9127 "2877 FCP XRI exchange busy "
9128 "wait time: %d seconds.\n",
9129 wait_time/1000);
9130 if (!els_xri_cmpl)
9131 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9132 "2878 ELS XRI exchange busy "
9133 "wait time: %d seconds.\n",
9134 wait_time/1000);
9135 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
9136 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
9137 } else {
9138 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
9139 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
9140 }
9141 fcp_xri_cmpl =
9142 list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
9143 els_xri_cmpl =
9144 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
9145 }
9146}
9147
9148/**
James Smartda0436e2009-05-22 14:51:39 -04009149 * lpfc_sli4_hba_unset - Unset the fcoe hba
9150 * @phba: Pointer to HBA context object.
9151 *
9152 * This function is called in the SLI4 code path to reset the HBA's FCoE
9153 * function. The caller is not required to hold any lock. This routine
9154 * issues PCI function reset mailbox command to reset the FCoE function.
9155 * At the end of the function, it calls lpfc_hba_down_post function to
9156 * free any pending commands.
9157 **/
9158static void
9159lpfc_sli4_hba_unset(struct lpfc_hba *phba)
9160{
9161 int wait_cnt = 0;
9162 LPFC_MBOXQ_t *mboxq;
James Smart912e3ac2011-05-24 11:42:11 -04009163 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -04009164
9165 lpfc_stop_hba_timers(phba);
9166 phba->sli4_hba.intr_enable = 0;
9167
9168 /*
9169 * Gracefully wait out the potential current outstanding asynchronous
9170 * mailbox command.
9171 */
9172
9173 /* First, block any pending async mailbox command from posted */
9174 spin_lock_irq(&phba->hbalock);
9175 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
9176 spin_unlock_irq(&phba->hbalock);
9177 /* Now, trying to wait it out if we can */
9178 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9179 msleep(10);
9180 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
9181 break;
9182 }
9183 /* Forcefully release the outstanding mailbox command if timed out */
9184 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9185 spin_lock_irq(&phba->hbalock);
9186 mboxq = phba->sli.mbox_active;
9187 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
9188 __lpfc_mbox_cmpl_put(phba, mboxq);
9189 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9190 phba->sli.mbox_active = NULL;
9191 spin_unlock_irq(&phba->hbalock);
9192 }
9193
James Smart5af5eee2010-10-22 11:06:38 -04009194 /* Abort all iocbs associated with the hba */
9195 lpfc_sli_hba_iocb_abort(phba);
9196
9197 /* Wait for completion of device XRI exchange busy */
9198 lpfc_sli4_xri_exchange_busy_wait(phba);
9199
James Smartda0436e2009-05-22 14:51:39 -04009200 /* Disable PCI subsystem interrupt */
9201 lpfc_sli4_disable_intr(phba);
9202
James Smart912e3ac2011-05-24 11:42:11 -04009203 /* Disable SR-IOV if enabled */
9204 if (phba->cfg_sriov_nr_virtfn)
9205 pci_disable_sriov(pdev);
9206
James Smartda0436e2009-05-22 14:51:39 -04009207 /* Stop kthread signal shall trigger work_done one more time */
9208 kthread_stop(phba->worker_thread);
9209
James Smart3677a3a2010-09-29 11:19:14 -04009210 /* Reset SLI4 HBA FCoE function */
9211 lpfc_pci_function_reset(phba);
James Smart5350d872011-10-10 21:33:49 -04009212 lpfc_sli4_queue_destroy(phba);
James Smart3677a3a2010-09-29 11:19:14 -04009213
James Smartda0436e2009-05-22 14:51:39 -04009214 /* Stop the SLI4 device port */
9215 phba->pport->work_port_events = 0;
9216}
9217
James Smart28baac72010-02-12 14:42:03 -05009218 /**
9219 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
9220 * @phba: Pointer to HBA context object.
9221 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
9222 *
9223 * This function is called in the SLI4 code path to read the port's
9224 * sli4 capabilities.
9225 *
9226 * This function may be be called from any context that can block-wait
9227 * for the completion. The expectation is that this routine is called
9228 * typically from probe_one or from the online routine.
9229 **/
9230int
9231lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9232{
9233 int rc;
9234 struct lpfc_mqe *mqe;
9235 struct lpfc_pc_sli4_params *sli4_params;
9236 uint32_t mbox_tmo;
9237
9238 rc = 0;
9239 mqe = &mboxq->u.mqe;
9240
9241 /* Read the port's SLI4 Parameters port capabilities */
James Smartfedd3b72011-02-16 12:39:24 -05009242 lpfc_pc_sli4_params(mboxq);
James Smart28baac72010-02-12 14:42:03 -05009243 if (!phba->sli4_hba.intr_enable)
9244 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9245 else {
James Smarta183a152011-10-10 21:32:43 -04009246 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart28baac72010-02-12 14:42:03 -05009247 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
9248 }
9249
9250 if (unlikely(rc))
9251 return 1;
9252
9253 sli4_params = &phba->sli4_hba.pc_sli4_params;
9254 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
9255 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
9256 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
9257 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
9258 &mqe->un.sli4_params);
9259 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
9260 &mqe->un.sli4_params);
9261 sli4_params->proto_types = mqe->un.sli4_params.word3;
9262 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
9263 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
9264 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
9265 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
9266 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
9267 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
9268 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
9269 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
9270 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
9271 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
9272 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
9273 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
9274 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
9275 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
9276 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
9277 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
9278 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
9279 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
9280 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
9281 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
James Smart05580562011-05-24 11:40:48 -04009282
9283 /* Make sure that sge_supp_len can be handled by the driver */
9284 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
9285 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
9286
James Smart28baac72010-02-12 14:42:03 -05009287 return rc;
9288}
9289
James Smartda0436e2009-05-22 14:51:39 -04009290/**
James Smartfedd3b72011-02-16 12:39:24 -05009291 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
9292 * @phba: Pointer to HBA context object.
9293 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
9294 *
9295 * This function is called in the SLI4 code path to read the port's
9296 * sli4 capabilities.
9297 *
9298 * This function may be be called from any context that can block-wait
9299 * for the completion. The expectation is that this routine is called
9300 * typically from probe_one or from the online routine.
9301 **/
9302int
9303lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9304{
9305 int rc;
9306 struct lpfc_mqe *mqe = &mboxq->u.mqe;
9307 struct lpfc_pc_sli4_params *sli4_params;
James Smarta183a152011-10-10 21:32:43 -04009308 uint32_t mbox_tmo;
James Smartfedd3b72011-02-16 12:39:24 -05009309 int length;
9310 struct lpfc_sli4_parameters *mbx_sli4_parameters;
9311
James Smart6d368e52011-05-24 11:44:12 -04009312 /*
9313 * By default, the driver assumes the SLI4 port requires RPI
9314 * header postings. The SLI4_PARAM response will correct this
9315 * assumption.
9316 */
9317 phba->sli4_hba.rpi_hdrs_in_use = 1;
9318
James Smartfedd3b72011-02-16 12:39:24 -05009319 /* Read the port's SLI4 Config Parameters */
9320 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
9321 sizeof(struct lpfc_sli4_cfg_mhdr));
9322 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
9323 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
9324 length, LPFC_SLI4_MBX_EMBED);
9325 if (!phba->sli4_hba.intr_enable)
9326 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -04009327 else {
9328 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
9329 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
9330 }
James Smartfedd3b72011-02-16 12:39:24 -05009331 if (unlikely(rc))
9332 return rc;
9333 sli4_params = &phba->sli4_hba.pc_sli4_params;
9334 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
9335 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
9336 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
9337 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
9338 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
9339 mbx_sli4_parameters);
9340 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
9341 mbx_sli4_parameters);
9342 if (bf_get(cfg_phwq, mbx_sli4_parameters))
9343 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
9344 else
9345 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
9346 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
9347 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
James Smart1ba981f2014-02-20 09:56:45 -05009348 sli4_params->oas_supported = bf_get(cfg_oas, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -05009349 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
9350 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
9351 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
9352 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
James Smart0c651872013-07-15 18:33:23 -04009353 sli4_params->wqsize = bf_get(cfg_wqsize, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -05009354 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
9355 mbx_sli4_parameters);
9356 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
9357 mbx_sli4_parameters);
James Smart6d368e52011-05-24 11:44:12 -04009358 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
9359 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
James Smart05580562011-05-24 11:40:48 -04009360
9361 /* Make sure that sge_supp_len can be handled by the driver */
9362 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
9363 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
9364
James Smartfedd3b72011-02-16 12:39:24 -05009365 return 0;
9366}
9367
9368/**
James Smart3772a992009-05-22 14:50:54 -04009369 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
9370 * @pdev: pointer to PCI device
9371 * @pid: pointer to PCI device identifier
9372 *
9373 * This routine is to be called to attach a device with SLI-3 interface spec
9374 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
9375 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
9376 * information of the device and driver to see if the driver state that it can
9377 * support this kind of device. If the match is successful, the driver core
9378 * invokes this routine. If this routine determines it can claim the HBA, it
9379 * does all the initialization that it needs to do to handle the HBA properly.
9380 *
9381 * Return code
9382 * 0 - driver can claim the device
9383 * negative value - driver can not claim the device
9384 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009385static int
James Smart3772a992009-05-22 14:50:54 -04009386lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
9387{
9388 struct lpfc_hba *phba;
9389 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04009390 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -04009391 int error;
9392 uint32_t cfg_mode, intr_mode;
9393
9394 /* Allocate memory for HBA structure */
9395 phba = lpfc_hba_alloc(pdev);
9396 if (!phba)
9397 return -ENOMEM;
9398
9399 /* Perform generic PCI device enabling operation */
9400 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -04009401 if (error)
James Smart3772a992009-05-22 14:50:54 -04009402 goto out_free_phba;
James Smart3772a992009-05-22 14:50:54 -04009403
9404 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
9405 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
9406 if (error)
9407 goto out_disable_pci_dev;
9408
9409 /* Set up SLI-3 specific device PCI memory space */
9410 error = lpfc_sli_pci_mem_setup(phba);
9411 if (error) {
9412 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9413 "1402 Failed to set up pci memory space.\n");
9414 goto out_disable_pci_dev;
9415 }
9416
9417 /* Set up phase-1 common device driver resources */
9418 error = lpfc_setup_driver_resource_phase1(phba);
9419 if (error) {
9420 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9421 "1403 Failed to set up driver resource.\n");
9422 goto out_unset_pci_mem_s3;
9423 }
9424
9425 /* Set up SLI-3 specific device driver resources */
9426 error = lpfc_sli_driver_resource_setup(phba);
9427 if (error) {
9428 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9429 "1404 Failed to set up driver resource.\n");
9430 goto out_unset_pci_mem_s3;
9431 }
9432
9433 /* Initialize and populate the iocb list per host */
9434 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
9435 if (error) {
9436 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9437 "1405 Failed to initialize iocb list.\n");
9438 goto out_unset_driver_resource_s3;
9439 }
9440
9441 /* Set up common device driver resources */
9442 error = lpfc_setup_driver_resource_phase2(phba);
9443 if (error) {
9444 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9445 "1406 Failed to set up driver resource.\n");
9446 goto out_free_iocb_list;
9447 }
9448
James Smart079b5c92011-08-21 21:48:49 -04009449 /* Get the default values for Model Name and Description */
9450 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
9451
James Smart3772a992009-05-22 14:50:54 -04009452 /* Create SCSI host to the physical port */
9453 error = lpfc_create_shost(phba);
9454 if (error) {
9455 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9456 "1407 Failed to create scsi host.\n");
9457 goto out_unset_driver_resource;
9458 }
9459
9460 /* Configure sysfs attributes */
9461 vport = phba->pport;
9462 error = lpfc_alloc_sysfs_attr(vport);
9463 if (error) {
9464 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9465 "1476 Failed to allocate sysfs attr\n");
9466 goto out_destroy_shost;
9467 }
9468
James Smart6669f9b2009-10-02 15:16:45 -04009469 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -04009470 /* Now, trying to enable interrupt and bring up the device */
9471 cfg_mode = phba->cfg_use_msi;
9472 while (true) {
9473 /* Put device to a known state before enabling interrupt */
9474 lpfc_stop_port(phba);
9475 /* Configure and enable interrupt */
9476 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
9477 if (intr_mode == LPFC_INTR_ERROR) {
9478 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9479 "0431 Failed to enable interrupt.\n");
9480 error = -ENODEV;
9481 goto out_free_sysfs_attr;
9482 }
9483 /* SLI-3 HBA setup */
9484 if (lpfc_sli_hba_setup(phba)) {
9485 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9486 "1477 Failed to set up hba\n");
9487 error = -ENODEV;
9488 goto out_remove_device;
9489 }
9490
9491 /* Wait 50ms for the interrupts of previous mailbox commands */
9492 msleep(50);
9493 /* Check active interrupts on message signaled interrupts */
9494 if (intr_mode == 0 ||
9495 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
9496 /* Log the current active interrupt mode */
9497 phba->intr_mode = intr_mode;
9498 lpfc_log_intr_mode(phba, intr_mode);
9499 break;
9500 } else {
9501 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9502 "0447 Configure interrupt mode (%d) "
9503 "failed active interrupt test.\n",
9504 intr_mode);
9505 /* Disable the current interrupt mode */
9506 lpfc_sli_disable_intr(phba);
9507 /* Try next level of interrupt mode */
9508 cfg_mode = --intr_mode;
9509 }
9510 }
9511
9512 /* Perform post initialization setup */
9513 lpfc_post_init_setup(phba);
9514
9515 /* Check if there are static vports to be created. */
9516 lpfc_create_static_vport(phba);
9517
9518 return 0;
9519
9520out_remove_device:
9521 lpfc_unset_hba(phba);
9522out_free_sysfs_attr:
9523 lpfc_free_sysfs_attr(vport);
9524out_destroy_shost:
9525 lpfc_destroy_shost(phba);
9526out_unset_driver_resource:
9527 lpfc_unset_driver_resource_phase2(phba);
9528out_free_iocb_list:
9529 lpfc_free_iocb_list(phba);
9530out_unset_driver_resource_s3:
9531 lpfc_sli_driver_resource_unset(phba);
9532out_unset_pci_mem_s3:
9533 lpfc_sli_pci_mem_unset(phba);
9534out_disable_pci_dev:
9535 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04009536 if (shost)
9537 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -04009538out_free_phba:
9539 lpfc_hba_free(phba);
9540 return error;
9541}
9542
9543/**
9544 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -04009545 * @pdev: pointer to PCI device
9546 *
James Smart3772a992009-05-22 14:50:54 -04009547 * This routine is to be called to disattach a device with SLI-3 interface
9548 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
9549 * removed from PCI bus, it performs all the necessary cleanup for the HBA
9550 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -04009551 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009552static void
James Smart3772a992009-05-22 14:50:54 -04009553lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -05009554{
James Smart2e0fef82007-06-17 19:56:36 -05009555 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9556 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -05009557 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -05009558 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -05009559 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05009560 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
9561
James Smart549e55c2007-08-02 11:09:51 -04009562 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04009563 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04009564 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009565
James Smart858c9f62007-06-17 19:56:39 -05009566 lpfc_free_sysfs_attr(vport);
9567
James Smarteada2722008-12-04 22:39:13 -05009568 /* Release all the vports against this physical port */
9569 vports = lpfc_create_vport_work_array(phba);
9570 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -04009571 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
9572 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
9573 continue;
James Smarteada2722008-12-04 22:39:13 -05009574 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -04009575 }
James Smarteada2722008-12-04 22:39:13 -05009576 lpfc_destroy_vport_work_array(phba, vports);
9577
9578 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -05009579 fc_remove_host(shost);
9580 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04009581 lpfc_cleanup(vport);
9582
James Smart2e0fef82007-06-17 19:56:36 -05009583 /*
9584 * Bring down the SLI Layer. This step disable all interrupts,
9585 * clears the rings, discards all mailbox commands, and resets
9586 * the HBA.
9587 */
James Smarta257bf92009-04-06 18:48:10 -04009588
Justin P. Mattock48e34d02010-12-30 15:07:58 -08009589 /* HBA interrupt will be disabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -05009590 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -04009591 /* Stop kthread signal shall trigger work_done one more time */
9592 kthread_stop(phba->worker_thread);
9593 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -05009594 lpfc_sli_brdrestart(phba);
9595
James Smart72859902012-01-18 16:25:38 -05009596 kfree(phba->vpi_bmask);
9597 kfree(phba->vpi_ids);
9598
James Smart3772a992009-05-22 14:50:54 -04009599 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05009600 spin_lock_irq(&phba->hbalock);
9601 list_del_init(&vport->listentry);
9602 spin_unlock_irq(&phba->hbalock);
9603
James Smart858c9f62007-06-17 19:56:39 -05009604 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05009605
James Smart912e3ac2011-05-24 11:42:11 -04009606 /* Disable SR-IOV if enabled */
9607 if (phba->cfg_sriov_nr_virtfn)
9608 pci_disable_sriov(pdev);
9609
James Smart5b75da22008-12-04 22:39:35 -05009610 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009611 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -05009612
James Smart858c9f62007-06-17 19:56:39 -05009613 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05009614
9615 /*
9616 * Call scsi_free before mem_free since scsi bufs are released to their
9617 * corresponding pools here.
9618 */
9619 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -04009620 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -05009621
James Smart34b02dc2008-08-24 21:49:55 -04009622 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
9623 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -05009624
James Smart2e0fef82007-06-17 19:56:36 -05009625 /* Free resources associated with SLI2 interface */
9626 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -04009627 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -05009628
9629 /* unmap adapter SLIM and Control Registers */
9630 iounmap(phba->ctrl_regs_memmap_p);
9631 iounmap(phba->slim_memmap_p);
9632
James Smart3772a992009-05-22 14:50:54 -04009633 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -05009634
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05009635 pci_release_selected_regions(pdev, bars);
James Smart2e0fef82007-06-17 19:56:36 -05009636 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05009637}
9638
Linas Vepstas8d63f372007-02-14 14:28:36 -06009639/**
James Smart3772a992009-05-22 14:50:54 -04009640 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05009641 * @pdev: pointer to PCI device
9642 * @msg: power management message
9643 *
James Smart3772a992009-05-22 14:50:54 -04009644 * This routine is to be called from the kernel's PCI subsystem to support
9645 * system Power Management (PM) to device with SLI-3 interface spec. When
9646 * PM invokes this method, it quiesces the device by stopping the driver's
9647 * worker thread for the device, turning off device's interrupt and DMA,
9648 * and bring the device offline. Note that as the driver implements the
9649 * minimum PM requirements to a power-aware driver's PM support for the
9650 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
9651 * to the suspend() method call will be treated as SUSPEND and the driver will
9652 * fully reinitialize its device during resume() method call, the driver will
9653 * set device to PCI_D3hot state in PCI config space instead of setting it
9654 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -05009655 *
9656 * Return code
James Smart3772a992009-05-22 14:50:54 -04009657 * 0 - driver suspended the device
9658 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05009659 **/
9660static int
James Smart3772a992009-05-22 14:50:54 -04009661lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -05009662{
9663 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9664 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9665
9666 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9667 "0473 PCI device Power Management suspend.\n");
9668
9669 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -04009670 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3a55b532008-12-04 22:38:54 -05009671 lpfc_offline(phba);
9672 kthread_stop(phba->worker_thread);
9673
9674 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -04009675 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -05009676
9677 /* Save device state to PCI config space */
9678 pci_save_state(pdev);
9679 pci_set_power_state(pdev, PCI_D3hot);
9680
9681 return 0;
9682}
9683
9684/**
James Smart3772a992009-05-22 14:50:54 -04009685 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05009686 * @pdev: pointer to PCI device
9687 *
James Smart3772a992009-05-22 14:50:54 -04009688 * This routine is to be called from the kernel's PCI subsystem to support
9689 * system Power Management (PM) to device with SLI-3 interface spec. When PM
9690 * invokes this method, it restores the device's PCI config space state and
9691 * fully reinitializes the device and brings it online. Note that as the
9692 * driver implements the minimum PM requirements to a power-aware driver's
9693 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
9694 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
9695 * driver will fully reinitialize its device during resume() method call,
9696 * the device will be set to PCI_D0 directly in PCI config space before
9697 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -05009698 *
9699 * Return code
James Smart3772a992009-05-22 14:50:54 -04009700 * 0 - driver suspended the device
9701 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05009702 **/
9703static int
James Smart3772a992009-05-22 14:50:54 -04009704lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -05009705{
9706 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9707 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -05009708 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05009709 int error;
9710
9711 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9712 "0452 PCI device Power Management resume.\n");
9713
9714 /* Restore device state from PCI config space */
9715 pci_set_power_state(pdev, PCI_D0);
9716 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -04009717
James Smart1dfb5a42010-02-12 14:40:50 -05009718 /*
9719 * As the new kernel behavior of pci_restore_state() API call clears
9720 * device saved_state flag, need to save the restored state again.
9721 */
9722 pci_save_state(pdev);
9723
James Smart3a55b532008-12-04 22:38:54 -05009724 if (pdev->is_busmaster)
9725 pci_set_master(pdev);
9726
9727 /* Startup the kernel thread for this host adapter. */
9728 phba->worker_thread = kthread_run(lpfc_do_work, phba,
9729 "lpfc_worker_%d", phba->brd_no);
9730 if (IS_ERR(phba->worker_thread)) {
9731 error = PTR_ERR(phba->worker_thread);
9732 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9733 "0434 PM resume failed to start worker "
9734 "thread: error=x%x.\n", error);
9735 return error;
9736 }
9737
James Smart5b75da22008-12-04 22:39:35 -05009738 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009739 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05009740 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -05009741 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -05009742 "0430 PM resume Failed to enable interrupt\n");
9743 return -EIO;
9744 } else
9745 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05009746
9747 /* Restart HBA and bring it online */
9748 lpfc_sli_brdrestart(phba);
9749 lpfc_online(phba);
9750
James Smart5b75da22008-12-04 22:39:35 -05009751 /* Log the current active interrupt mode */
9752 lpfc_log_intr_mode(phba, phba->intr_mode);
9753
James Smart3a55b532008-12-04 22:38:54 -05009754 return 0;
9755}
9756
9757/**
James Smart891478a2009-11-18 15:40:23 -05009758 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
9759 * @phba: pointer to lpfc hba data structure.
9760 *
9761 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -04009762 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -05009763 **/
9764static void
9765lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
9766{
9767 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9768 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -04009769
9770 /*
9771 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
9772 * and let the SCSI mid-layer to retry them to recover.
9773 */
James Smartdb55fba2014-04-04 13:52:02 -04009774 lpfc_sli_abort_fcp_rings(phba);
James Smart891478a2009-11-18 15:40:23 -05009775}
9776
9777/**
James Smart0d878412009-10-02 15:16:56 -04009778 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
9779 * @phba: pointer to lpfc hba data structure.
9780 *
9781 * This routine is called to prepare the SLI3 device for PCI slot reset. It
9782 * disables the device interrupt and pci device, and aborts the internal FCP
9783 * pending I/Os.
9784 **/
9785static void
9786lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
9787{
James Smart0d878412009-10-02 15:16:56 -04009788 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05009789 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -04009790
James Smart75baf692010-06-08 18:31:21 -04009791 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -04009792 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -04009793
James Smarte2af0d22010-03-15 11:25:32 -04009794 /* Block all SCSI devices' I/Os on the host */
9795 lpfc_scsi_dev_block(phba);
9796
James Smartea714f32013-04-17 20:18:47 -04009797 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
9798 lpfc_sli_flush_fcp_rings(phba);
9799
James Smarte2af0d22010-03-15 11:25:32 -04009800 /* stop all timers */
9801 lpfc_stop_hba_timers(phba);
9802
James Smart0d878412009-10-02 15:16:56 -04009803 /* Disable interrupt and pci device */
9804 lpfc_sli_disable_intr(phba);
9805 pci_disable_device(phba->pcidev);
James Smart0d878412009-10-02 15:16:56 -04009806}
9807
9808/**
9809 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
9810 * @phba: pointer to lpfc hba data structure.
9811 *
9812 * This routine is called to prepare the SLI3 device for PCI slot permanently
9813 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
9814 * pending I/Os.
9815 **/
9816static void
James Smart75baf692010-06-08 18:31:21 -04009817lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
James Smart0d878412009-10-02 15:16:56 -04009818{
9819 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05009820 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -04009821 /* Block all SCSI devices' I/Os on the host */
9822 lpfc_scsi_dev_block(phba);
9823
9824 /* stop all timers */
9825 lpfc_stop_hba_timers(phba);
9826
James Smart0d878412009-10-02 15:16:56 -04009827 /* Clean up all driver's outstanding SCSI I/Os */
9828 lpfc_sli_flush_fcp_rings(phba);
9829}
9830
9831/**
James Smart3772a992009-05-22 14:50:54 -04009832 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -04009833 * @pdev: pointer to PCI device.
9834 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009835 *
James Smart3772a992009-05-22 14:50:54 -04009836 * This routine is called from the PCI subsystem for I/O error handling to
9837 * device with SLI-3 interface spec. This function is called by the PCI
9838 * subsystem after a PCI bus error affecting this device has been detected.
9839 * When this function is invoked, it will need to stop all the I/Os and
9840 * interrupt(s) to the device. Once that is done, it will return
9841 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
9842 * as desired.
James Smarte59058c2008-08-24 21:49:00 -04009843 *
9844 * Return codes
James Smart0d878412009-10-02 15:16:56 -04009845 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -04009846 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
9847 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -04009848 **/
James Smart3772a992009-05-22 14:50:54 -04009849static pci_ers_result_t
9850lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009851{
James Smart51ef4c22007-08-02 11:10:31 -04009852 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9853 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009854
James Smart0d878412009-10-02 15:16:56 -04009855 switch (state) {
9856 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -05009857 /* Non-fatal error, prepare for recovery */
9858 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -04009859 return PCI_ERS_RESULT_CAN_RECOVER;
9860 case pci_channel_io_frozen:
9861 /* Fatal error, prepare for slot reset */
9862 lpfc_sli_prep_dev_for_reset(phba);
9863 return PCI_ERS_RESULT_NEED_RESET;
9864 case pci_channel_io_perm_failure:
9865 /* Permanent failure, prepare for device down */
James Smart75baf692010-06-08 18:31:21 -04009866 lpfc_sli_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009867 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -04009868 default:
9869 /* Unknown state, prepare and request slot reset */
9870 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9871 "0472 Unknown PCI error state: x%x\n", state);
9872 lpfc_sli_prep_dev_for_reset(phba);
9873 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -04009874 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06009875}
9876
9877/**
James Smart3772a992009-05-22 14:50:54 -04009878 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -04009879 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009880 *
James Smart3772a992009-05-22 14:50:54 -04009881 * This routine is called from the PCI subsystem for error handling to
9882 * device with SLI-3 interface spec. This is called after PCI bus has been
9883 * reset to restart the PCI card from scratch, as if from a cold-boot.
9884 * During the PCI subsystem error recovery, after driver returns
9885 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
9886 * recovery and then call this routine before calling the .resume method
9887 * to recover the device. This function will initialize the HBA device,
9888 * enable the interrupt, but it will just put the HBA to offline state
9889 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -04009890 *
9891 * Return codes
James Smart3772a992009-05-22 14:50:54 -04009892 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
9893 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -06009894 */
James Smart3772a992009-05-22 14:50:54 -04009895static pci_ers_result_t
9896lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009897{
James Smart51ef4c22007-08-02 11:10:31 -04009898 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9899 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009900 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -05009901 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009902
9903 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +11009904 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -06009905 printk(KERN_ERR "lpfc: Cannot re-enable "
9906 "PCI device after reset.\n");
9907 return PCI_ERS_RESULT_DISCONNECT;
9908 }
9909
James Smart97207482008-12-04 22:39:19 -05009910 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05009911
9912 /*
9913 * As the new kernel behavior of pci_restore_state() API call clears
9914 * device saved_state flag, need to save the restored state again.
9915 */
9916 pci_save_state(pdev);
9917
James Smart97207482008-12-04 22:39:19 -05009918 if (pdev->is_busmaster)
9919 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009920
James Smart92d7f7b2007-06-17 19:56:38 -05009921 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04009922 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05009923 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009924
James Smart5b75da22008-12-04 22:39:35 -05009925 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009926 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05009927 if (intr_mode == LPFC_INTR_ERROR) {
9928 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9929 "0427 Cannot re-enable interrupt after "
9930 "slot reset.\n");
9931 return PCI_ERS_RESULT_DISCONNECT;
9932 } else
9933 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009934
James Smart75baf692010-06-08 18:31:21 -04009935 /* Take device offline, it will perform cleanup */
James Smart618a5232012-06-12 13:54:36 -04009936 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009937 lpfc_offline(phba);
9938 lpfc_sli_brdrestart(phba);
9939
James Smart5b75da22008-12-04 22:39:35 -05009940 /* Log the current active interrupt mode */
9941 lpfc_log_intr_mode(phba, phba->intr_mode);
9942
Linas Vepstas8d63f372007-02-14 14:28:36 -06009943 return PCI_ERS_RESULT_RECOVERED;
9944}
9945
9946/**
James Smart3772a992009-05-22 14:50:54 -04009947 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -04009948 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -06009949 *
James Smart3772a992009-05-22 14:50:54 -04009950 * This routine is called from the PCI subsystem for error handling to device
9951 * with SLI-3 interface spec. It is called when kernel error recovery tells
9952 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
9953 * error recovery. After this call, traffic can start to flow from this device
9954 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009955 */
James Smart3772a992009-05-22 14:50:54 -04009956static void
9957lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009958{
James Smart51ef4c22007-08-02 11:10:31 -04009959 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9960 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009961
James Smarte2af0d22010-03-15 11:25:32 -04009962 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -04009963 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -04009964
9965 /* Clean up Advanced Error Reporting (AER) if needed */
9966 if (phba->hba_flag & HBA_AER_ENABLED)
9967 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009968}
9969
James Smart3772a992009-05-22 14:50:54 -04009970/**
James Smartda0436e2009-05-22 14:51:39 -04009971 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
9972 * @phba: pointer to lpfc hba data structure.
9973 *
9974 * returns the number of ELS/CT IOCBs to reserve
9975 **/
9976int
9977lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
9978{
9979 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
9980
James Smartf1126682009-06-10 17:22:44 -04009981 if (phba->sli_rev == LPFC_SLI_REV4) {
9982 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -04009983 return 10;
James Smartf1126682009-06-10 17:22:44 -04009984 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -04009985 return 25;
James Smartf1126682009-06-10 17:22:44 -04009986 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -04009987 return 50;
James Smartf1126682009-06-10 17:22:44 -04009988 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -04009989 return 100;
James Smart8a9d2e82012-05-09 21:16:12 -04009990 else if (max_xri <= 1536)
James Smart6a9c52c2009-10-02 15:16:51 -04009991 return 150;
James Smart8a9d2e82012-05-09 21:16:12 -04009992 else if (max_xri <= 2048)
9993 return 200;
9994 else
9995 return 250;
James Smartf1126682009-06-10 17:22:44 -04009996 } else
9997 return 0;
James Smartda0436e2009-05-22 14:51:39 -04009998}
9999
10000/**
James Smart52d52442011-05-24 11:42:45 -040010001 * lpfc_write_firmware - attempt to write a firmware image to the port
James Smart52d52442011-05-24 11:42:45 -040010002 * @fw: pointer to firmware image returned from request_firmware.
James Smartce396282012-09-29 11:30:56 -040010003 * @phba: pointer to lpfc hba data structure.
James Smart52d52442011-05-24 11:42:45 -040010004 *
James Smart52d52442011-05-24 11:42:45 -040010005 **/
James Smartce396282012-09-29 11:30:56 -040010006static void
10007lpfc_write_firmware(const struct firmware *fw, void *context)
James Smart52d52442011-05-24 11:42:45 -040010008{
James Smartce396282012-09-29 11:30:56 -040010009 struct lpfc_hba *phba = (struct lpfc_hba *)context;
James Smart6b5151f2012-01-18 16:24:06 -050010010 char fwrev[FW_REV_STR_SIZE];
James Smartce396282012-09-29 11:30:56 -040010011 struct lpfc_grp_hdr *image;
James Smart52d52442011-05-24 11:42:45 -040010012 struct list_head dma_buffer_list;
10013 int i, rc = 0;
10014 struct lpfc_dmabuf *dmabuf, *next;
10015 uint32_t offset = 0, temp_offset = 0;
10016
James Smartc71ab862012-10-31 14:44:33 -040010017 /* It can be null in no-wait mode, sanity check */
James Smartce396282012-09-29 11:30:56 -040010018 if (!fw) {
10019 rc = -ENXIO;
10020 goto out;
10021 }
10022 image = (struct lpfc_grp_hdr *)fw->data;
10023
James Smart52d52442011-05-24 11:42:45 -040010024 INIT_LIST_HEAD(&dma_buffer_list);
James Smart079b5c92011-08-21 21:48:49 -040010025 if ((be32_to_cpu(image->magic_number) != LPFC_GROUP_OJECT_MAGIC_NUM) ||
10026 (bf_get_be32(lpfc_grp_hdr_file_type, image) !=
10027 LPFC_FILE_TYPE_GROUP) ||
10028 (bf_get_be32(lpfc_grp_hdr_id, image) != LPFC_FILE_ID_GROUP) ||
10029 (be32_to_cpu(image->size) != fw->size)) {
James Smart52d52442011-05-24 11:42:45 -040010030 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10031 "3022 Invalid FW image found. "
James Smart079b5c92011-08-21 21:48:49 -040010032 "Magic:%x Type:%x ID:%x\n",
10033 be32_to_cpu(image->magic_number),
10034 bf_get_be32(lpfc_grp_hdr_file_type, image),
10035 bf_get_be32(lpfc_grp_hdr_id, image));
James Smartce396282012-09-29 11:30:56 -040010036 rc = -EINVAL;
10037 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010038 }
10039 lpfc_decode_firmware_rev(phba, fwrev, 1);
James Smart88a2cfb2011-07-22 18:36:33 -040010040 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
James Smart52d52442011-05-24 11:42:45 -040010041 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartce396282012-09-29 11:30:56 -040010042 "3023 Updating Firmware, Current Version:%s "
James Smart52d52442011-05-24 11:42:45 -040010043 "New Version:%s\n",
James Smart88a2cfb2011-07-22 18:36:33 -040010044 fwrev, image->revision);
James Smart52d52442011-05-24 11:42:45 -040010045 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
10046 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
10047 GFP_KERNEL);
10048 if (!dmabuf) {
10049 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040010050 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010051 }
10052 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
10053 SLI4_PAGE_SIZE,
10054 &dmabuf->phys,
10055 GFP_KERNEL);
10056 if (!dmabuf->virt) {
10057 kfree(dmabuf);
10058 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040010059 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010060 }
10061 list_add_tail(&dmabuf->list, &dma_buffer_list);
10062 }
10063 while (offset < fw->size) {
10064 temp_offset = offset;
10065 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
James Smart079b5c92011-08-21 21:48:49 -040010066 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
James Smart52d52442011-05-24 11:42:45 -040010067 memcpy(dmabuf->virt,
10068 fw->data + temp_offset,
James Smart079b5c92011-08-21 21:48:49 -040010069 fw->size - temp_offset);
10070 temp_offset = fw->size;
James Smart52d52442011-05-24 11:42:45 -040010071 break;
10072 }
James Smart52d52442011-05-24 11:42:45 -040010073 memcpy(dmabuf->virt, fw->data + temp_offset,
10074 SLI4_PAGE_SIZE);
James Smart88a2cfb2011-07-22 18:36:33 -040010075 temp_offset += SLI4_PAGE_SIZE;
James Smart52d52442011-05-24 11:42:45 -040010076 }
10077 rc = lpfc_wr_object(phba, &dma_buffer_list,
10078 (fw->size - offset), &offset);
James Smartce396282012-09-29 11:30:56 -040010079 if (rc)
10080 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010081 }
10082 rc = offset;
10083 }
James Smartce396282012-09-29 11:30:56 -040010084
10085release_out:
James Smart52d52442011-05-24 11:42:45 -040010086 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
10087 list_del(&dmabuf->list);
10088 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
10089 dmabuf->virt, dmabuf->phys);
10090 kfree(dmabuf);
10091 }
James Smartce396282012-09-29 11:30:56 -040010092 release_firmware(fw);
10093out:
10094 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartc71ab862012-10-31 14:44:33 -040010095 "3024 Firmware update done: %d.\n", rc);
James Smartce396282012-09-29 11:30:56 -040010096 return;
James Smart52d52442011-05-24 11:42:45 -040010097}
10098
10099/**
James Smartc71ab862012-10-31 14:44:33 -040010100 * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
10101 * @phba: pointer to lpfc hba data structure.
10102 *
10103 * This routine is called to perform Linux generic firmware upgrade on device
10104 * that supports such feature.
10105 **/
10106int
10107lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
10108{
10109 uint8_t file_name[ELX_MODEL_NAME_SIZE];
10110 int ret;
10111 const struct firmware *fw;
10112
10113 /* Only supported on SLI4 interface type 2 for now */
10114 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
10115 LPFC_SLI_INTF_IF_TYPE_2)
10116 return -EPERM;
10117
10118 snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
10119
10120 if (fw_upgrade == INT_FW_UPGRADE) {
10121 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
10122 file_name, &phba->pcidev->dev,
10123 GFP_KERNEL, (void *)phba,
10124 lpfc_write_firmware);
10125 } else if (fw_upgrade == RUN_FW_UPGRADE) {
10126 ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
10127 if (!ret)
10128 lpfc_write_firmware(fw, (void *)phba);
10129 } else {
10130 ret = -EINVAL;
10131 }
10132
10133 return ret;
10134}
10135
10136/**
James Smartda0436e2009-05-22 14:51:39 -040010137 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
10138 * @pdev: pointer to PCI device
10139 * @pid: pointer to PCI device identifier
10140 *
10141 * This routine is called from the kernel's PCI subsystem to device with
10142 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
10143 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
10144 * information of the device and driver to see if the driver state that it
10145 * can support this kind of device. If the match is successful, the driver
10146 * core invokes this routine. If this routine determines it can claim the HBA,
10147 * it does all the initialization that it needs to do to handle the HBA
10148 * properly.
10149 *
10150 * Return code
10151 * 0 - driver can claim the device
10152 * negative value - driver can not claim the device
10153 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010154static int
James Smartda0436e2009-05-22 14:51:39 -040010155lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
10156{
10157 struct lpfc_hba *phba;
10158 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -040010159 struct Scsi_Host *shost = NULL;
James Smartce396282012-09-29 11:30:56 -040010160 int error, ret;
James Smartda0436e2009-05-22 14:51:39 -040010161 uint32_t cfg_mode, intr_mode;
James Smart67d12732012-08-03 12:36:13 -040010162 int adjusted_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -040010163
10164 /* Allocate memory for HBA structure */
10165 phba = lpfc_hba_alloc(pdev);
10166 if (!phba)
10167 return -ENOMEM;
10168
10169 /* Perform generic PCI device enabling operation */
10170 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -040010171 if (error)
James Smartda0436e2009-05-22 14:51:39 -040010172 goto out_free_phba;
James Smartda0436e2009-05-22 14:51:39 -040010173
10174 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
10175 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
10176 if (error)
10177 goto out_disable_pci_dev;
10178
10179 /* Set up SLI-4 specific device PCI memory space */
10180 error = lpfc_sli4_pci_mem_setup(phba);
10181 if (error) {
10182 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10183 "1410 Failed to set up pci memory space.\n");
10184 goto out_disable_pci_dev;
10185 }
10186
10187 /* Set up phase-1 common device driver resources */
10188 error = lpfc_setup_driver_resource_phase1(phba);
10189 if (error) {
10190 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10191 "1411 Failed to set up driver resource.\n");
10192 goto out_unset_pci_mem_s4;
10193 }
10194
10195 /* Set up SLI-4 Specific device driver resources */
10196 error = lpfc_sli4_driver_resource_setup(phba);
10197 if (error) {
10198 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10199 "1412 Failed to set up driver resource.\n");
10200 goto out_unset_pci_mem_s4;
10201 }
10202
10203 /* Initialize and populate the iocb list per host */
James Smart2a9bf3d2010-06-07 15:24:45 -040010204
10205 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10206 "2821 initialize iocb list %d.\n",
10207 phba->cfg_iocb_cnt*1024);
10208 error = lpfc_init_iocb_list(phba, phba->cfg_iocb_cnt*1024);
10209
James Smartda0436e2009-05-22 14:51:39 -040010210 if (error) {
10211 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10212 "1413 Failed to initialize iocb list.\n");
10213 goto out_unset_driver_resource_s4;
10214 }
10215
James Smart19ca7602010-11-20 23:11:55 -050010216 INIT_LIST_HEAD(&phba->active_rrq_list);
James Smart7d791df2011-07-22 18:37:52 -040010217 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
James Smart19ca7602010-11-20 23:11:55 -050010218
James Smartda0436e2009-05-22 14:51:39 -040010219 /* Set up common device driver resources */
10220 error = lpfc_setup_driver_resource_phase2(phba);
10221 if (error) {
10222 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10223 "1414 Failed to set up driver resource.\n");
10224 goto out_free_iocb_list;
10225 }
10226
James Smart079b5c92011-08-21 21:48:49 -040010227 /* Get the default values for Model Name and Description */
10228 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
10229
James Smartda0436e2009-05-22 14:51:39 -040010230 /* Create SCSI host to the physical port */
10231 error = lpfc_create_shost(phba);
10232 if (error) {
10233 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10234 "1415 Failed to create scsi host.\n");
10235 goto out_unset_driver_resource;
10236 }
10237
10238 /* Configure sysfs attributes */
10239 vport = phba->pport;
10240 error = lpfc_alloc_sysfs_attr(vport);
10241 if (error) {
10242 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10243 "1416 Failed to allocate sysfs attr\n");
10244 goto out_destroy_shost;
10245 }
10246
James Smart6669f9b2009-10-02 15:16:45 -040010247 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -040010248 /* Now, trying to enable interrupt and bring up the device */
10249 cfg_mode = phba->cfg_use_msi;
James Smartda0436e2009-05-22 14:51:39 -040010250
James Smart7b15db32013-01-03 15:43:29 -050010251 /* Put device to a known state before enabling interrupt */
10252 lpfc_stop_port(phba);
10253 /* Configure and enable interrupt */
10254 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
10255 if (intr_mode == LPFC_INTR_ERROR) {
10256 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10257 "0426 Failed to enable interrupt.\n");
10258 error = -ENODEV;
10259 goto out_free_sysfs_attr;
James Smartda0436e2009-05-22 14:51:39 -040010260 }
James Smart7b15db32013-01-03 15:43:29 -050010261 /* Default to single EQ for non-MSI-X */
10262 if (phba->intr_type != MSIX)
10263 adjusted_fcp_io_channel = 1;
10264 else
10265 adjusted_fcp_io_channel = phba->cfg_fcp_io_channel;
10266 phba->cfg_fcp_io_channel = adjusted_fcp_io_channel;
10267 /* Set up SLI-4 HBA */
10268 if (lpfc_sli4_hba_setup(phba)) {
10269 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10270 "1421 Failed to set up hba\n");
10271 error = -ENODEV;
10272 goto out_disable_intr;
10273 }
10274
10275 /* Log the current active interrupt mode */
10276 phba->intr_mode = intr_mode;
10277 lpfc_log_intr_mode(phba, intr_mode);
James Smartda0436e2009-05-22 14:51:39 -040010278
10279 /* Perform post initialization setup */
10280 lpfc_post_init_setup(phba);
10281
James Smartc71ab862012-10-31 14:44:33 -040010282 /* check for firmware upgrade or downgrade */
10283 if (phba->cfg_request_firmware_upgrade)
10284 ret = lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
James Smart52d52442011-05-24 11:42:45 -040010285
James Smart1c6834a2009-07-19 10:01:26 -040010286 /* Check if there are static vports to be created. */
10287 lpfc_create_static_vport(phba);
James Smartda0436e2009-05-22 14:51:39 -040010288 return 0;
10289
10290out_disable_intr:
10291 lpfc_sli4_disable_intr(phba);
10292out_free_sysfs_attr:
10293 lpfc_free_sysfs_attr(vport);
10294out_destroy_shost:
10295 lpfc_destroy_shost(phba);
10296out_unset_driver_resource:
10297 lpfc_unset_driver_resource_phase2(phba);
10298out_free_iocb_list:
10299 lpfc_free_iocb_list(phba);
10300out_unset_driver_resource_s4:
10301 lpfc_sli4_driver_resource_unset(phba);
10302out_unset_pci_mem_s4:
10303 lpfc_sli4_pci_mem_unset(phba);
10304out_disable_pci_dev:
10305 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040010306 if (shost)
10307 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -040010308out_free_phba:
10309 lpfc_hba_free(phba);
10310 return error;
10311}
10312
10313/**
10314 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
10315 * @pdev: pointer to PCI device
10316 *
10317 * This routine is called from the kernel's PCI subsystem to device with
10318 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
10319 * removed from PCI bus, it performs all the necessary cleanup for the HBA
10320 * device to be removed from the PCI subsystem properly.
10321 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010322static void
James Smartda0436e2009-05-22 14:51:39 -040010323lpfc_pci_remove_one_s4(struct pci_dev *pdev)
10324{
10325 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10326 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
10327 struct lpfc_vport **vports;
10328 struct lpfc_hba *phba = vport->phba;
10329 int i;
10330
10331 /* Mark the device unloading flag */
10332 spin_lock_irq(&phba->hbalock);
10333 vport->load_flag |= FC_UNLOADING;
10334 spin_unlock_irq(&phba->hbalock);
10335
10336 /* Free the HBA sysfs attributes */
10337 lpfc_free_sysfs_attr(vport);
10338
10339 /* Release all the vports against this physical port */
10340 vports = lpfc_create_vport_work_array(phba);
10341 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040010342 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
10343 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
10344 continue;
James Smartda0436e2009-05-22 14:51:39 -040010345 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040010346 }
James Smartda0436e2009-05-22 14:51:39 -040010347 lpfc_destroy_vport_work_array(phba, vports);
10348
10349 /* Remove FC host and then SCSI host with the physical port */
10350 fc_remove_host(shost);
10351 scsi_remove_host(shost);
10352
10353 /* Perform cleanup on the physical port */
10354 lpfc_cleanup(vport);
10355
10356 /*
10357 * Bring down the SLI Layer. This step disables all interrupts,
10358 * clears the rings, discards all mailbox commands, and resets
10359 * the HBA FCoE function.
10360 */
10361 lpfc_debugfs_terminate(vport);
10362 lpfc_sli4_hba_unset(phba);
10363
10364 spin_lock_irq(&phba->hbalock);
10365 list_del_init(&vport->listentry);
10366 spin_unlock_irq(&phba->hbalock);
10367
James Smart3677a3a2010-09-29 11:19:14 -040010368 /* Perform scsi free before driver resource_unset since scsi
James Smartda0436e2009-05-22 14:51:39 -040010369 * buffers are released to their corresponding pools here.
10370 */
10371 lpfc_scsi_free(phba);
James Smart67d12732012-08-03 12:36:13 -040010372
James Smartda0436e2009-05-22 14:51:39 -040010373 lpfc_sli4_driver_resource_unset(phba);
10374
10375 /* Unmap adapter Control and Doorbell registers */
10376 lpfc_sli4_pci_mem_unset(phba);
10377
10378 /* Release PCI resources and disable device's PCI function */
10379 scsi_host_put(shost);
10380 lpfc_disable_pci_dev(phba);
10381
10382 /* Finally, free the driver's device data structure */
10383 lpfc_hba_free(phba);
10384
10385 return;
10386}
10387
10388/**
10389 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
10390 * @pdev: pointer to PCI device
10391 * @msg: power management message
10392 *
10393 * This routine is called from the kernel's PCI subsystem to support system
10394 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
10395 * this method, it quiesces the device by stopping the driver's worker
10396 * thread for the device, turning off device's interrupt and DMA, and bring
10397 * the device offline. Note that as the driver implements the minimum PM
10398 * requirements to a power-aware driver's PM support for suspend/resume -- all
10399 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
10400 * method call will be treated as SUSPEND and the driver will fully
10401 * reinitialize its device during resume() method call, the driver will set
10402 * device to PCI_D3hot state in PCI config space instead of setting it
10403 * according to the @msg provided by the PM.
10404 *
10405 * Return code
10406 * 0 - driver suspended the device
10407 * Error otherwise
10408 **/
10409static int
10410lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
10411{
10412 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10413 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10414
10415 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart75baf692010-06-08 18:31:21 -040010416 "2843 PCI device Power Management suspend.\n");
James Smartda0436e2009-05-22 14:51:39 -040010417
10418 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040010419 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smartda0436e2009-05-22 14:51:39 -040010420 lpfc_offline(phba);
10421 kthread_stop(phba->worker_thread);
10422
10423 /* Disable interrupt from device */
10424 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040010425 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -040010426
10427 /* Save device state to PCI config space */
10428 pci_save_state(pdev);
10429 pci_set_power_state(pdev, PCI_D3hot);
10430
10431 return 0;
10432}
10433
10434/**
10435 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
10436 * @pdev: pointer to PCI device
10437 *
10438 * This routine is called from the kernel's PCI subsystem to support system
10439 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
10440 * this method, it restores the device's PCI config space state and fully
10441 * reinitializes the device and brings it online. Note that as the driver
10442 * implements the minimum PM requirements to a power-aware driver's PM for
10443 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
10444 * to the suspend() method call will be treated as SUSPEND and the driver
10445 * will fully reinitialize its device during resume() method call, the device
10446 * will be set to PCI_D0 directly in PCI config space before restoring the
10447 * state.
10448 *
10449 * Return code
10450 * 0 - driver suspended the device
10451 * Error otherwise
10452 **/
10453static int
10454lpfc_pci_resume_one_s4(struct pci_dev *pdev)
10455{
10456 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10457 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10458 uint32_t intr_mode;
10459 int error;
10460
10461 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10462 "0292 PCI device Power Management resume.\n");
10463
10464 /* Restore device state from PCI config space */
10465 pci_set_power_state(pdev, PCI_D0);
10466 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050010467
10468 /*
10469 * As the new kernel behavior of pci_restore_state() API call clears
10470 * device saved_state flag, need to save the restored state again.
10471 */
10472 pci_save_state(pdev);
10473
James Smartda0436e2009-05-22 14:51:39 -040010474 if (pdev->is_busmaster)
10475 pci_set_master(pdev);
10476
10477 /* Startup the kernel thread for this host adapter. */
10478 phba->worker_thread = kthread_run(lpfc_do_work, phba,
10479 "lpfc_worker_%d", phba->brd_no);
10480 if (IS_ERR(phba->worker_thread)) {
10481 error = PTR_ERR(phba->worker_thread);
10482 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10483 "0293 PM resume failed to start worker "
10484 "thread: error=x%x.\n", error);
10485 return error;
10486 }
10487
10488 /* Configure and enable interrupt */
10489 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
10490 if (intr_mode == LPFC_INTR_ERROR) {
10491 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10492 "0294 PM resume Failed to enable interrupt\n");
10493 return -EIO;
10494 } else
10495 phba->intr_mode = intr_mode;
10496
10497 /* Restart HBA and bring it online */
10498 lpfc_sli_brdrestart(phba);
10499 lpfc_online(phba);
10500
10501 /* Log the current active interrupt mode */
10502 lpfc_log_intr_mode(phba, phba->intr_mode);
10503
10504 return 0;
10505}
10506
10507/**
James Smart75baf692010-06-08 18:31:21 -040010508 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
10509 * @phba: pointer to lpfc hba data structure.
10510 *
10511 * This routine is called to prepare the SLI4 device for PCI slot recover. It
10512 * aborts all the outstanding SCSI I/Os to the pci device.
10513 **/
10514static void
10515lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
10516{
James Smart75baf692010-06-08 18:31:21 -040010517 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10518 "2828 PCI channel I/O abort preparing for recovery\n");
10519 /*
10520 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
10521 * and let the SCSI mid-layer to retry them to recover.
10522 */
James Smartdb55fba2014-04-04 13:52:02 -040010523 lpfc_sli_abort_fcp_rings(phba);
James Smart75baf692010-06-08 18:31:21 -040010524}
10525
10526/**
10527 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
10528 * @phba: pointer to lpfc hba data structure.
10529 *
10530 * This routine is called to prepare the SLI4 device for PCI slot reset. It
10531 * disables the device interrupt and pci device, and aborts the internal FCP
10532 * pending I/Os.
10533 **/
10534static void
10535lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
10536{
10537 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10538 "2826 PCI channel disable preparing for reset\n");
10539
10540 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040010541 lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
James Smart75baf692010-06-08 18:31:21 -040010542
10543 /* Block all SCSI devices' I/Os on the host */
10544 lpfc_scsi_dev_block(phba);
10545
James Smartea714f32013-04-17 20:18:47 -040010546 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
10547 lpfc_sli_flush_fcp_rings(phba);
10548
James Smart75baf692010-06-08 18:31:21 -040010549 /* stop all timers */
10550 lpfc_stop_hba_timers(phba);
10551
10552 /* Disable interrupt and pci device */
10553 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040010554 lpfc_sli4_queue_destroy(phba);
James Smart75baf692010-06-08 18:31:21 -040010555 pci_disable_device(phba->pcidev);
James Smart75baf692010-06-08 18:31:21 -040010556}
10557
10558/**
10559 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
10560 * @phba: pointer to lpfc hba data structure.
10561 *
10562 * This routine is called to prepare the SLI4 device for PCI slot permanently
10563 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
10564 * pending I/Os.
10565 **/
10566static void
10567lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
10568{
10569 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10570 "2827 PCI channel permanent disable for failure\n");
10571
10572 /* Block all SCSI devices' I/Os on the host */
10573 lpfc_scsi_dev_block(phba);
10574
10575 /* stop all timers */
10576 lpfc_stop_hba_timers(phba);
10577
10578 /* Clean up all driver's outstanding SCSI I/Os */
10579 lpfc_sli_flush_fcp_rings(phba);
10580}
10581
10582/**
James Smartda0436e2009-05-22 14:51:39 -040010583 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
10584 * @pdev: pointer to PCI device.
10585 * @state: the current PCI connection state.
10586 *
10587 * This routine is called from the PCI subsystem for error handling to device
10588 * with SLI-4 interface spec. This function is called by the PCI subsystem
10589 * after a PCI bus error affecting this device has been detected. When this
10590 * function is invoked, it will need to stop all the I/Os and interrupt(s)
10591 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
10592 * for the PCI subsystem to perform proper recovery as desired.
10593 *
10594 * Return codes
10595 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10596 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10597 **/
10598static pci_ers_result_t
10599lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
10600{
James Smart75baf692010-06-08 18:31:21 -040010601 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10602 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10603
10604 switch (state) {
10605 case pci_channel_io_normal:
10606 /* Non-fatal error, prepare for recovery */
10607 lpfc_sli4_prep_dev_for_recover(phba);
10608 return PCI_ERS_RESULT_CAN_RECOVER;
10609 case pci_channel_io_frozen:
10610 /* Fatal error, prepare for slot reset */
10611 lpfc_sli4_prep_dev_for_reset(phba);
10612 return PCI_ERS_RESULT_NEED_RESET;
10613 case pci_channel_io_perm_failure:
10614 /* Permanent failure, prepare for device down */
10615 lpfc_sli4_prep_dev_for_perm_failure(phba);
10616 return PCI_ERS_RESULT_DISCONNECT;
10617 default:
10618 /* Unknown state, prepare and request slot reset */
10619 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10620 "2825 Unknown PCI error state: x%x\n", state);
10621 lpfc_sli4_prep_dev_for_reset(phba);
10622 return PCI_ERS_RESULT_NEED_RESET;
10623 }
James Smartda0436e2009-05-22 14:51:39 -040010624}
10625
10626/**
10627 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
10628 * @pdev: pointer to PCI device.
10629 *
10630 * This routine is called from the PCI subsystem for error handling to device
10631 * with SLI-4 interface spec. It is called after PCI bus has been reset to
10632 * restart the PCI card from scratch, as if from a cold-boot. During the
10633 * PCI subsystem error recovery, after the driver returns
10634 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
10635 * recovery and then call this routine before calling the .resume method to
10636 * recover the device. This function will initialize the HBA device, enable
10637 * the interrupt, but it will just put the HBA to offline state without
10638 * passing any I/O traffic.
10639 *
10640 * Return codes
10641 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10642 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10643 */
10644static pci_ers_result_t
10645lpfc_io_slot_reset_s4(struct pci_dev *pdev)
10646{
James Smart75baf692010-06-08 18:31:21 -040010647 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10648 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10649 struct lpfc_sli *psli = &phba->sli;
10650 uint32_t intr_mode;
10651
10652 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
10653 if (pci_enable_device_mem(pdev)) {
10654 printk(KERN_ERR "lpfc: Cannot re-enable "
10655 "PCI device after reset.\n");
10656 return PCI_ERS_RESULT_DISCONNECT;
10657 }
10658
10659 pci_restore_state(pdev);
James Smart0a96e972011-07-22 18:37:28 -040010660
10661 /*
10662 * As the new kernel behavior of pci_restore_state() API call clears
10663 * device saved_state flag, need to save the restored state again.
10664 */
10665 pci_save_state(pdev);
10666
James Smart75baf692010-06-08 18:31:21 -040010667 if (pdev->is_busmaster)
10668 pci_set_master(pdev);
10669
10670 spin_lock_irq(&phba->hbalock);
10671 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
10672 spin_unlock_irq(&phba->hbalock);
10673
10674 /* Configure and enable interrupt */
10675 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
10676 if (intr_mode == LPFC_INTR_ERROR) {
10677 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10678 "2824 Cannot re-enable interrupt after "
10679 "slot reset.\n");
10680 return PCI_ERS_RESULT_DISCONNECT;
10681 } else
10682 phba->intr_mode = intr_mode;
10683
10684 /* Log the current active interrupt mode */
10685 lpfc_log_intr_mode(phba, phba->intr_mode);
10686
James Smartda0436e2009-05-22 14:51:39 -040010687 return PCI_ERS_RESULT_RECOVERED;
10688}
10689
10690/**
10691 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
10692 * @pdev: pointer to PCI device
10693 *
10694 * This routine is called from the PCI subsystem for error handling to device
10695 * with SLI-4 interface spec. It is called when kernel error recovery tells
10696 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
10697 * error recovery. After this call, traffic can start to flow from this device
10698 * again.
10699 **/
10700static void
10701lpfc_io_resume_s4(struct pci_dev *pdev)
10702{
James Smart75baf692010-06-08 18:31:21 -040010703 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10704 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10705
10706 /*
10707 * In case of slot reset, as function reset is performed through
10708 * mailbox command which needs DMA to be enabled, this operation
10709 * has to be moved to the io resume phase. Taking device offline
10710 * will perform the necessary cleanup.
10711 */
10712 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
10713 /* Perform device reset */
James Smart618a5232012-06-12 13:54:36 -040010714 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040010715 lpfc_offline(phba);
10716 lpfc_sli_brdrestart(phba);
10717 /* Bring the device back online */
10718 lpfc_online(phba);
10719 }
10720
10721 /* Clean up Advanced Error Reporting (AER) if needed */
10722 if (phba->hba_flag & HBA_AER_ENABLED)
10723 pci_cleanup_aer_uncorrect_error_status(pdev);
James Smartda0436e2009-05-22 14:51:39 -040010724}
10725
10726/**
James Smart3772a992009-05-22 14:50:54 -040010727 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
10728 * @pdev: pointer to PCI device
10729 * @pid: pointer to PCI device identifier
10730 *
10731 * This routine is to be registered to the kernel's PCI subsystem. When an
10732 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
10733 * at PCI device-specific information of the device and driver to see if the
10734 * driver state that it can support this kind of device. If the match is
10735 * successful, the driver core invokes this routine. This routine dispatches
10736 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
10737 * do all the initialization that it needs to do to handle the HBA device
10738 * properly.
10739 *
10740 * Return code
10741 * 0 - driver can claim the device
10742 * negative value - driver can not claim the device
10743 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010744static int
James Smart3772a992009-05-22 14:50:54 -040010745lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
10746{
10747 int rc;
James Smart8fa38512009-07-19 10:01:03 -040010748 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -040010749
James Smart28baac72010-02-12 14:42:03 -050010750 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -040010751 return -ENODEV;
10752
James Smart8fa38512009-07-19 10:01:03 -040010753 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -050010754 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -040010755 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040010756 else
James Smart3772a992009-05-22 14:50:54 -040010757 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040010758
James Smart3772a992009-05-22 14:50:54 -040010759 return rc;
10760}
10761
10762/**
10763 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
10764 * @pdev: pointer to PCI device
10765 *
10766 * This routine is to be registered to the kernel's PCI subsystem. When an
10767 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
10768 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
10769 * remove routine, which will perform all the necessary cleanup for the
10770 * device to be removed from the PCI subsystem properly.
10771 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010772static void
James Smart3772a992009-05-22 14:50:54 -040010773lpfc_pci_remove_one(struct pci_dev *pdev)
10774{
10775 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10776 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10777
10778 switch (phba->pci_dev_grp) {
10779 case LPFC_PCI_DEV_LP:
10780 lpfc_pci_remove_one_s3(pdev);
10781 break;
James Smartda0436e2009-05-22 14:51:39 -040010782 case LPFC_PCI_DEV_OC:
10783 lpfc_pci_remove_one_s4(pdev);
10784 break;
James Smart3772a992009-05-22 14:50:54 -040010785 default:
10786 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10787 "1424 Invalid PCI device group: 0x%x\n",
10788 phba->pci_dev_grp);
10789 break;
10790 }
10791 return;
10792}
10793
10794/**
10795 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
10796 * @pdev: pointer to PCI device
10797 * @msg: power management message
10798 *
10799 * This routine is to be registered to the kernel's PCI subsystem to support
10800 * system Power Management (PM). When PM invokes this method, it dispatches
10801 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
10802 * suspend the device.
10803 *
10804 * Return code
10805 * 0 - driver suspended the device
10806 * Error otherwise
10807 **/
10808static int
10809lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
10810{
10811 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10812 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10813 int rc = -ENODEV;
10814
10815 switch (phba->pci_dev_grp) {
10816 case LPFC_PCI_DEV_LP:
10817 rc = lpfc_pci_suspend_one_s3(pdev, msg);
10818 break;
James Smartda0436e2009-05-22 14:51:39 -040010819 case LPFC_PCI_DEV_OC:
10820 rc = lpfc_pci_suspend_one_s4(pdev, msg);
10821 break;
James Smart3772a992009-05-22 14:50:54 -040010822 default:
10823 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10824 "1425 Invalid PCI device group: 0x%x\n",
10825 phba->pci_dev_grp);
10826 break;
10827 }
10828 return rc;
10829}
10830
10831/**
10832 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
10833 * @pdev: pointer to PCI device
10834 *
10835 * This routine is to be registered to the kernel's PCI subsystem to support
10836 * system Power Management (PM). When PM invokes this method, it dispatches
10837 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
10838 * resume the device.
10839 *
10840 * Return code
10841 * 0 - driver suspended the device
10842 * Error otherwise
10843 **/
10844static int
10845lpfc_pci_resume_one(struct pci_dev *pdev)
10846{
10847 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10848 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10849 int rc = -ENODEV;
10850
10851 switch (phba->pci_dev_grp) {
10852 case LPFC_PCI_DEV_LP:
10853 rc = lpfc_pci_resume_one_s3(pdev);
10854 break;
James Smartda0436e2009-05-22 14:51:39 -040010855 case LPFC_PCI_DEV_OC:
10856 rc = lpfc_pci_resume_one_s4(pdev);
10857 break;
James Smart3772a992009-05-22 14:50:54 -040010858 default:
10859 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10860 "1426 Invalid PCI device group: 0x%x\n",
10861 phba->pci_dev_grp);
10862 break;
10863 }
10864 return rc;
10865}
10866
10867/**
10868 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
10869 * @pdev: pointer to PCI device.
10870 * @state: the current PCI connection state.
10871 *
10872 * This routine is registered to the PCI subsystem for error handling. This
10873 * function is called by the PCI subsystem after a PCI bus error affecting
10874 * this device has been detected. When this routine is invoked, it dispatches
10875 * the action to the proper SLI-3 or SLI-4 device error detected handling
10876 * routine, which will perform the proper error detected operation.
10877 *
10878 * Return codes
10879 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10880 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10881 **/
10882static pci_ers_result_t
10883lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
10884{
10885 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10886 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10887 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
10888
10889 switch (phba->pci_dev_grp) {
10890 case LPFC_PCI_DEV_LP:
10891 rc = lpfc_io_error_detected_s3(pdev, state);
10892 break;
James Smartda0436e2009-05-22 14:51:39 -040010893 case LPFC_PCI_DEV_OC:
10894 rc = lpfc_io_error_detected_s4(pdev, state);
10895 break;
James Smart3772a992009-05-22 14:50:54 -040010896 default:
10897 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10898 "1427 Invalid PCI device group: 0x%x\n",
10899 phba->pci_dev_grp);
10900 break;
10901 }
10902 return rc;
10903}
10904
10905/**
10906 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
10907 * @pdev: pointer to PCI device.
10908 *
10909 * This routine is registered to the PCI subsystem for error handling. This
10910 * function is called after PCI bus has been reset to restart the PCI card
10911 * from scratch, as if from a cold-boot. When this routine is invoked, it
10912 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
10913 * routine, which will perform the proper device reset.
10914 *
10915 * Return codes
10916 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10917 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10918 **/
10919static pci_ers_result_t
10920lpfc_io_slot_reset(struct pci_dev *pdev)
10921{
10922 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10923 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10924 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
10925
10926 switch (phba->pci_dev_grp) {
10927 case LPFC_PCI_DEV_LP:
10928 rc = lpfc_io_slot_reset_s3(pdev);
10929 break;
James Smartda0436e2009-05-22 14:51:39 -040010930 case LPFC_PCI_DEV_OC:
10931 rc = lpfc_io_slot_reset_s4(pdev);
10932 break;
James Smart3772a992009-05-22 14:50:54 -040010933 default:
10934 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10935 "1428 Invalid PCI device group: 0x%x\n",
10936 phba->pci_dev_grp);
10937 break;
10938 }
10939 return rc;
10940}
10941
10942/**
10943 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
10944 * @pdev: pointer to PCI device
10945 *
10946 * This routine is registered to the PCI subsystem for error handling. It
10947 * is called when kernel error recovery tells the lpfc driver that it is
10948 * OK to resume normal PCI operation after PCI bus error recovery. When
10949 * this routine is invoked, it dispatches the action to the proper SLI-3
10950 * or SLI-4 device io_resume routine, which will resume the device operation.
10951 **/
10952static void
10953lpfc_io_resume(struct pci_dev *pdev)
10954{
10955 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10956 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10957
10958 switch (phba->pci_dev_grp) {
10959 case LPFC_PCI_DEV_LP:
10960 lpfc_io_resume_s3(pdev);
10961 break;
James Smartda0436e2009-05-22 14:51:39 -040010962 case LPFC_PCI_DEV_OC:
10963 lpfc_io_resume_s4(pdev);
10964 break;
James Smart3772a992009-05-22 14:50:54 -040010965 default:
10966 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10967 "1429 Invalid PCI device group: 0x%x\n",
10968 phba->pci_dev_grp);
10969 break;
10970 }
10971 return;
10972}
10973
James Smart1ba981f2014-02-20 09:56:45 -050010974/**
10975 * lpfc_sli4_oas_verify - Verify OAS is supported by this adapter
10976 * @phba: pointer to lpfc hba data structure.
10977 *
10978 * This routine checks to see if OAS is supported for this adapter. If
10979 * supported, the configure Flash Optimized Fabric flag is set. Otherwise,
10980 * the enable oas flag is cleared and the pool created for OAS device data
10981 * is destroyed.
10982 *
10983 **/
10984void
10985lpfc_sli4_oas_verify(struct lpfc_hba *phba)
10986{
10987
10988 if (!phba->cfg_EnableXLane)
10989 return;
10990
10991 if (phba->sli4_hba.pc_sli4_params.oas_supported) {
10992 phba->cfg_fof = 1;
10993 } else {
James Smartf38fa0b2014-04-04 13:52:21 -040010994 phba->cfg_fof = 0;
James Smart1ba981f2014-02-20 09:56:45 -050010995 if (phba->device_data_mem_pool)
10996 mempool_destroy(phba->device_data_mem_pool);
10997 phba->device_data_mem_pool = NULL;
10998 }
10999
11000 return;
11001}
11002
11003/**
11004 * lpfc_fof_queue_setup - Set up all the fof queues
11005 * @phba: pointer to lpfc hba data structure.
11006 *
11007 * This routine is invoked to set up all the fof queues for the FC HBA
11008 * operation.
11009 *
11010 * Return codes
11011 * 0 - successful
11012 * -ENOMEM - No available memory
11013 **/
11014int
11015lpfc_fof_queue_setup(struct lpfc_hba *phba)
11016{
11017 struct lpfc_sli *psli = &phba->sli;
11018 int rc;
11019
11020 rc = lpfc_eq_create(phba, phba->sli4_hba.fof_eq, LPFC_MAX_IMAX);
11021 if (rc)
11022 return -ENOMEM;
11023
James Smartf38fa0b2014-04-04 13:52:21 -040011024 if (phba->cfg_fof) {
James Smart1ba981f2014-02-20 09:56:45 -050011025
11026 rc = lpfc_cq_create(phba, phba->sli4_hba.oas_cq,
11027 phba->sli4_hba.fof_eq, LPFC_WCQ, LPFC_FCP);
11028 if (rc)
11029 goto out_oas_cq;
11030
11031 rc = lpfc_wq_create(phba, phba->sli4_hba.oas_wq,
11032 phba->sli4_hba.oas_cq, LPFC_FCP);
11033 if (rc)
11034 goto out_oas_wq;
11035
11036 phba->sli4_hba.oas_cq->pring = &psli->ring[LPFC_FCP_OAS_RING];
11037 phba->sli4_hba.oas_ring = &psli->ring[LPFC_FCP_OAS_RING];
11038 }
11039
11040 return 0;
11041
11042out_oas_wq:
James Smartf38fa0b2014-04-04 13:52:21 -040011043 lpfc_cq_destroy(phba, phba->sli4_hba.oas_cq);
James Smart1ba981f2014-02-20 09:56:45 -050011044out_oas_cq:
11045 lpfc_eq_destroy(phba, phba->sli4_hba.fof_eq);
11046 return rc;
11047
11048}
11049
11050/**
11051 * lpfc_fof_queue_create - Create all the fof queues
11052 * @phba: pointer to lpfc hba data structure.
11053 *
11054 * This routine is invoked to allocate all the fof queues for the FC HBA
11055 * operation. For each SLI4 queue type, the parameters such as queue entry
11056 * count (queue depth) shall be taken from the module parameter. For now,
11057 * we just use some constant number as place holder.
11058 *
11059 * Return codes
11060 * 0 - successful
11061 * -ENOMEM - No availble memory
11062 * -EIO - The mailbox failed to complete successfully.
11063 **/
11064int
11065lpfc_fof_queue_create(struct lpfc_hba *phba)
11066{
11067 struct lpfc_queue *qdesc;
11068
11069 /* Create FOF EQ */
11070 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
11071 phba->sli4_hba.eq_ecount);
11072 if (!qdesc)
11073 goto out_error;
11074
11075 phba->sli4_hba.fof_eq = qdesc;
11076
James Smartf38fa0b2014-04-04 13:52:21 -040011077 if (phba->cfg_fof) {
James Smart1ba981f2014-02-20 09:56:45 -050011078
11079 /* Create OAS CQ */
11080 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
11081 phba->sli4_hba.cq_ecount);
11082 if (!qdesc)
11083 goto out_error;
11084
11085 phba->sli4_hba.oas_cq = qdesc;
11086
11087 /* Create OAS WQ */
11088 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
11089 phba->sli4_hba.wq_ecount);
11090 if (!qdesc)
11091 goto out_error;
11092
11093 phba->sli4_hba.oas_wq = qdesc;
11094
11095 }
11096 return 0;
11097
11098out_error:
11099 lpfc_fof_queue_destroy(phba);
11100 return -ENOMEM;
11101}
11102
11103/**
11104 * lpfc_fof_queue_destroy - Destroy all the fof queues
11105 * @phba: pointer to lpfc hba data structure.
11106 *
11107 * This routine is invoked to release all the SLI4 queues with the FC HBA
11108 * operation.
11109 *
11110 * Return codes
11111 * 0 - successful
11112 **/
11113int
11114lpfc_fof_queue_destroy(struct lpfc_hba *phba)
11115{
11116 /* Release FOF Event queue */
11117 if (phba->sli4_hba.fof_eq != NULL) {
11118 lpfc_sli4_queue_free(phba->sli4_hba.fof_eq);
11119 phba->sli4_hba.fof_eq = NULL;
11120 }
11121
11122 /* Release OAS Completion queue */
11123 if (phba->sli4_hba.oas_cq != NULL) {
11124 lpfc_sli4_queue_free(phba->sli4_hba.oas_cq);
11125 phba->sli4_hba.oas_cq = NULL;
11126 }
11127
11128 /* Release OAS Work queue */
11129 if (phba->sli4_hba.oas_wq != NULL) {
11130 lpfc_sli4_queue_free(phba->sli4_hba.oas_wq);
11131 phba->sli4_hba.oas_wq = NULL;
11132 }
11133 return 0;
11134}
11135
dea31012005-04-17 16:05:31 -050011136static struct pci_device_id lpfc_id_table[] = {
11137 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
11138 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -040011139 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
11140 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011141 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
11142 PCI_ANY_ID, PCI_ANY_ID, },
11143 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
11144 PCI_ANY_ID, PCI_ANY_ID, },
11145 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
11146 PCI_ANY_ID, PCI_ANY_ID, },
11147 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
11148 PCI_ANY_ID, PCI_ANY_ID, },
11149 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
11150 PCI_ANY_ID, PCI_ANY_ID, },
11151 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
11152 PCI_ANY_ID, PCI_ANY_ID, },
11153 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
11154 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050011155 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
11156 PCI_ANY_ID, PCI_ANY_ID, },
11157 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
11158 PCI_ANY_ID, PCI_ANY_ID, },
11159 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
11160 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011161 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
11162 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050011163 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
11164 PCI_ANY_ID, PCI_ANY_ID, },
11165 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
11166 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011167 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
11168 PCI_ANY_ID, PCI_ANY_ID, },
11169 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
11170 PCI_ANY_ID, PCI_ANY_ID, },
11171 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
11172 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -040011173 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
11174 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050011175 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
11176 PCI_ANY_ID, PCI_ANY_ID, },
11177 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
11178 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011179 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
11180 PCI_ANY_ID, PCI_ANY_ID, },
11181 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
11182 PCI_ANY_ID, PCI_ANY_ID, },
11183 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
11184 PCI_ANY_ID, PCI_ANY_ID, },
11185 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
11186 PCI_ANY_ID, PCI_ANY_ID, },
11187 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
11188 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050011189 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
11190 PCI_ANY_ID, PCI_ANY_ID, },
11191 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
11192 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -040011193 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
11194 PCI_ANY_ID, PCI_ANY_ID, },
11195 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
11196 PCI_ANY_ID, PCI_ANY_ID, },
11197 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
11198 PCI_ANY_ID, PCI_ANY_ID, },
11199 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
11200 PCI_ANY_ID, PCI_ANY_ID, },
11201 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
11202 PCI_ANY_ID, PCI_ANY_ID, },
11203 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
11204 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -040011205 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
11206 PCI_ANY_ID, PCI_ANY_ID, },
11207 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
11208 PCI_ANY_ID, PCI_ANY_ID, },
11209 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
11210 PCI_ANY_ID, PCI_ANY_ID, },
James Smart3772a992009-05-22 14:50:54 -040011211 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
11212 PCI_ANY_ID, PCI_ANY_ID, },
James Smarta747c9c2009-11-18 15:41:10 -050011213 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
11214 PCI_ANY_ID, PCI_ANY_ID, },
11215 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
James Smart6669f9b2009-10-02 15:16:45 -040011216 PCI_ANY_ID, PCI_ANY_ID, },
James Smart98fc5dd2010-06-07 15:24:29 -040011217 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BALIUS,
11218 PCI_ANY_ID, PCI_ANY_ID, },
James Smart085c6472010-11-20 23:11:37 -050011219 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC,
11220 PCI_ANY_ID, PCI_ANY_ID, },
11221 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE,
11222 PCI_ANY_ID, PCI_ANY_ID, },
James Smartc0c11512011-05-24 11:41:34 -040011223 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC_VF,
11224 PCI_ANY_ID, PCI_ANY_ID, },
11225 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE_VF,
11226 PCI_ANY_ID, PCI_ANY_ID, },
James Smartf8cafd32012-08-03 12:42:51 -040011227 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SKYHAWK,
11228 PCI_ANY_ID, PCI_ANY_ID, },
11229 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SKYHAWK_VF,
11230 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011231 { 0 }
11232};
11233
11234MODULE_DEVICE_TABLE(pci, lpfc_id_table);
11235
Stephen Hemmingera55b2d22012-09-07 09:33:16 -070011236static const struct pci_error_handlers lpfc_err_handler = {
Linas Vepstas8d63f372007-02-14 14:28:36 -060011237 .error_detected = lpfc_io_error_detected,
11238 .slot_reset = lpfc_io_slot_reset,
11239 .resume = lpfc_io_resume,
11240};
11241
dea31012005-04-17 16:05:31 -050011242static struct pci_driver lpfc_driver = {
11243 .name = LPFC_DRIVER_NAME,
11244 .id_table = lpfc_id_table,
11245 .probe = lpfc_pci_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011246 .remove = lpfc_pci_remove_one,
James Smart3a55b532008-12-04 22:38:54 -050011247 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -040011248 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -050011249 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -050011250};
11251
James Smart3ef6d242012-01-18 16:23:48 -050011252static const struct file_operations lpfc_mgmt_fop = {
Al Viro858feac2013-04-14 22:39:37 -040011253 .owner = THIS_MODULE,
James Smart3ef6d242012-01-18 16:23:48 -050011254};
11255
11256static struct miscdevice lpfc_mgmt_dev = {
11257 .minor = MISC_DYNAMIC_MINOR,
11258 .name = "lpfcmgmt",
11259 .fops = &lpfc_mgmt_fop,
11260};
11261
James Smarte59058c2008-08-24 21:49:00 -040011262/**
James Smart3621a712009-04-06 18:47:14 -040011263 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -040011264 *
11265 * This routine is to be invoked when the lpfc module is loaded into the
11266 * kernel. The special kernel macro module_init() is used to indicate the
11267 * role of this routine to the kernel as lpfc module entry point.
11268 *
11269 * Return codes
11270 * 0 - successful
11271 * -ENOMEM - FC attach transport failed
11272 * all others - failed
11273 */
dea31012005-04-17 16:05:31 -050011274static int __init
11275lpfc_init(void)
11276{
James Smart7bb03bb2013-04-17 20:19:16 -040011277 int cpu;
dea31012005-04-17 16:05:31 -050011278 int error = 0;
11279
11280 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040011281 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -050011282
James Smart3ef6d242012-01-18 16:23:48 -050011283 error = misc_register(&lpfc_mgmt_dev);
11284 if (error)
11285 printk(KERN_ERR "Could not register lpfcmgmt device, "
11286 "misc_register returned with status %d", error);
11287
James Smart7ee5d432007-10-27 13:37:17 -040011288 if (lpfc_enable_npiv) {
11289 lpfc_transport_functions.vport_create = lpfc_vport_create;
11290 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
11291 }
dea31012005-04-17 16:05:31 -050011292 lpfc_transport_template =
11293 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -040011294 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -050011295 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -040011296 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -040011297 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -040011298 fc_attach_transport(&lpfc_vport_transport_functions);
11299 if (lpfc_vport_transport_template == NULL) {
11300 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040011301 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -040011302 }
James Smart7ee5d432007-10-27 13:37:17 -040011303 }
James Smart7bb03bb2013-04-17 20:19:16 -040011304
11305 /* Initialize in case vector mapping is needed */
James Smartb246de12013-05-31 17:03:07 -040011306 lpfc_used_cpu = NULL;
11307 lpfc_present_cpu = 0;
11308 for_each_present_cpu(cpu)
11309 lpfc_present_cpu++;
James Smart7bb03bb2013-04-17 20:19:16 -040011310
dea31012005-04-17 16:05:31 -050011311 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -050011312 if (error) {
dea31012005-04-17 16:05:31 -050011313 fc_release_transport(lpfc_transport_template);
James Smartd7c255b2008-08-24 21:50:00 -040011314 if (lpfc_enable_npiv)
11315 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -050011316 }
dea31012005-04-17 16:05:31 -050011317
11318 return error;
11319}
11320
James Smarte59058c2008-08-24 21:49:00 -040011321/**
James Smart3621a712009-04-06 18:47:14 -040011322 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -040011323 *
11324 * This routine is invoked when the lpfc module is removed from the kernel.
11325 * The special kernel macro module_exit() is used to indicate the role of
11326 * this routine to the kernel as lpfc module exit point.
11327 */
dea31012005-04-17 16:05:31 -050011328static void __exit
11329lpfc_exit(void)
11330{
James Smart3ef6d242012-01-18 16:23:48 -050011331 misc_deregister(&lpfc_mgmt_dev);
dea31012005-04-17 16:05:31 -050011332 pci_unregister_driver(&lpfc_driver);
11333 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040011334 if (lpfc_enable_npiv)
11335 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -050011336 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040011337 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
11338 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050011339 (1L << _dump_buf_data_order), _dump_buf_data);
11340 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
11341 }
11342
11343 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -040011344 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
11345 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050011346 (1L << _dump_buf_dif_order), _dump_buf_dif);
11347 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
11348 }
James Smartb246de12013-05-31 17:03:07 -040011349 kfree(lpfc_used_cpu);
dea31012005-04-17 16:05:31 -050011350}
11351
11352module_init(lpfc_init);
11353module_exit(lpfc_exit);
11354MODULE_LICENSE("GPL");
11355MODULE_DESCRIPTION(LPFC_MODULE_DESC);
11356MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
11357MODULE_VERSION("0:" LPFC_DRIVER_VERSION);