blob: 0d9230486d6e7e19ef8cec9f2bc860bb556df449 [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))
James Smarta2fc4aef2014-09-03 12:57:55 -0400309 snprintf(phba->OptionROMVersion, 32, "%d.%d%d",
James Smart97207482008-12-04 22:39:19 -0500310 prg->ver, prg->rev, prg->lev);
311 else
James Smarta2fc4aef2014-09-03 12:57:55 -0400312 snprintf(phba->OptionROMVersion, 32, "%d.%d%d%c%d",
James Smart97207482008-12-04 22:39:19 -0500313 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);
James Smarta2fc4aef2014-09-03 12:57:55 -04003180 if (psb) {
3181 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
3182 psb->data, psb->dma_handle);
3183 kfree(psb);
3184 }
James Smart8a9d2e82012-05-09 21:16:12 -04003185 }
James Smarta40fc5f2013-04-17 20:17:40 -04003186 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003187 phba->sli4_hba.scsi_xri_cnt -= scsi_xri_cnt;
James Smarta40fc5f2013-04-17 20:17:40 -04003188 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003189 }
3190
3191 /* update xris associated to remaining allocated scsi buffers */
3192 psb = NULL;
3193 psb_next = NULL;
3194 list_for_each_entry_safe(psb, psb_next, &scsi_sgl_list, list) {
3195 lxri = lpfc_sli4_next_xritag(phba);
3196 if (lxri == NO_XRI) {
3197 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3198 "2560 Failed to allocate xri for "
3199 "scsi buffer\n");
3200 rc = -ENOMEM;
3201 goto out_free_mem;
3202 }
3203 psb->cur_iocbq.sli4_lxritag = lxri;
3204 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3205 }
James Smarta40fc5f2013-04-17 20:17:40 -04003206 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart164cecd2013-09-06 12:22:38 -04003207 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003208 list_splice_init(&scsi_sgl_list, &phba->lpfc_scsi_buf_list_get);
3209 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
James Smart164cecd2013-09-06 12:22:38 -04003210 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003211 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003212
dea31012005-04-17 16:05:31 -05003213 return 0;
James Smart8a9d2e82012-05-09 21:16:12 -04003214
3215out_free_mem:
3216 lpfc_free_els_sgl_list(phba);
3217 lpfc_scsi_free(phba);
3218 return rc;
dea31012005-04-17 16:05:31 -05003219}
3220
James Smarte59058c2008-08-24 21:49:00 -04003221/**
James Smart3621a712009-04-06 18:47:14 -04003222 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04003223 * @phba: pointer to lpfc hba data structure.
3224 * @instance: a unique integer ID to this FC port.
3225 * @dev: pointer to the device data structure.
3226 *
3227 * This routine creates a FC port for the upper layer protocol. The FC port
3228 * can be created on top of either a physical port or a virtual port provided
3229 * by the HBA. This routine also allocates a SCSI host data structure (shost)
3230 * and associates the FC port created before adding the shost into the SCSI
3231 * layer.
3232 *
3233 * Return codes
3234 * @vport - pointer to the virtual N_Port data structure.
3235 * NULL - port create failed.
3236 **/
James Smart2e0fef82007-06-17 19:56:36 -05003237struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04003238lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04003239{
James Smart2e0fef82007-06-17 19:56:36 -05003240 struct lpfc_vport *vport;
3241 struct Scsi_Host *shost;
3242 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04003243
James Smart3de2a652007-08-02 11:09:59 -04003244 if (dev != &phba->pcidev->dev)
3245 shost = scsi_host_alloc(&lpfc_vport_template,
3246 sizeof(struct lpfc_vport));
3247 else
3248 shost = scsi_host_alloc(&lpfc_template,
3249 sizeof(struct lpfc_vport));
James Smart2e0fef82007-06-17 19:56:36 -05003250 if (!shost)
3251 goto out;
James Smart47a86172007-04-25 09:53:22 -04003252
James Smart2e0fef82007-06-17 19:56:36 -05003253 vport = (struct lpfc_vport *) shost->hostdata;
3254 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05003255 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05003256 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05003257 vport->fc_rscn_flush = 0;
James Smart47a86172007-04-25 09:53:22 -04003258
James Smart3de2a652007-08-02 11:09:59 -04003259 lpfc_get_vport_cfgparam(vport);
James Smart2e0fef82007-06-17 19:56:36 -05003260 shost->unique_id = instance;
3261 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04003262 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05003263 shost->this_id = -1;
3264 shost->max_cmd_len = 16;
James Smartda0436e2009-05-22 14:51:39 -04003265 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart28baac72010-02-12 14:42:03 -05003266 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04003267 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smartda0436e2009-05-22 14:51:39 -04003268 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
3269 }
James Smart81301a92008-12-04 22:39:46 -05003270
James Smart47a86172007-04-25 09:53:22 -04003271 /*
James Smart2e0fef82007-06-17 19:56:36 -05003272 * Set initial can_queue value since 0 is no longer supported and
3273 * scsi_add_host will fail. This will be adjusted later based on the
3274 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04003275 */
James Smart2e0fef82007-06-17 19:56:36 -05003276 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04003277 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05003278 shost->transportt = lpfc_vport_transport_template;
3279 vport->port_type = LPFC_NPIV_PORT;
3280 } else {
3281 shost->transportt = lpfc_transport_template;
3282 vport->port_type = LPFC_PHYSICAL_PORT;
3283 }
James Smart47a86172007-04-25 09:53:22 -04003284
James Smart2e0fef82007-06-17 19:56:36 -05003285 /* Initialize all internally managed lists. */
3286 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04003287 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05003288 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04003289
James Smart2e0fef82007-06-17 19:56:36 -05003290 init_timer(&vport->fc_disctmo);
3291 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05003292 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04003293
James Smart2e0fef82007-06-17 19:56:36 -05003294 init_timer(&vport->fc_fdmitmo);
3295 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
James Smart92d7f7b2007-06-17 19:56:38 -05003296 vport->fc_fdmitmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04003297
James Smart2e0fef82007-06-17 19:56:36 -05003298 init_timer(&vport->els_tmofunc);
3299 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05003300 vport->els_tmofunc.data = (unsigned long)vport;
James Smart92494142011-02-16 12:39:44 -05003301
3302 init_timer(&vport->delayed_disc_tmo);
3303 vport->delayed_disc_tmo.function = lpfc_delayed_disc_tmo;
3304 vport->delayed_disc_tmo.data = (unsigned long)vport;
3305
James Bottomleyd139b9b2009-11-05 13:33:12 -06003306 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05003307 if (error)
3308 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04003309
James Smart549e55c2007-08-02 11:09:51 -04003310 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003311 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04003312 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003313 return vport;
James Smart47a86172007-04-25 09:53:22 -04003314
James Smart2e0fef82007-06-17 19:56:36 -05003315out_put_shost:
3316 scsi_host_put(shost);
3317out:
3318 return NULL;
James Smart47a86172007-04-25 09:53:22 -04003319}
3320
James Smarte59058c2008-08-24 21:49:00 -04003321/**
James Smart3621a712009-04-06 18:47:14 -04003322 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04003323 * @vport: pointer to an lpfc virtual N_Port data structure.
3324 *
3325 * This routine destroys a FC port from the upper layer protocol. All the
3326 * resources associated with the port are released.
3327 **/
James Smart2e0fef82007-06-17 19:56:36 -05003328void
3329destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04003330{
James Smart92d7f7b2007-06-17 19:56:38 -05003331 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3332 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003333
James Smart858c9f62007-06-17 19:56:39 -05003334 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05003335 fc_remove_host(shost);
3336 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04003337
James Smart92d7f7b2007-06-17 19:56:38 -05003338 spin_lock_irq(&phba->hbalock);
3339 list_del_init(&vport->listentry);
3340 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04003341
James Smart92d7f7b2007-06-17 19:56:38 -05003342 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04003343 return;
James Smart47a86172007-04-25 09:53:22 -04003344}
3345
James Smarte59058c2008-08-24 21:49:00 -04003346/**
James Smart3621a712009-04-06 18:47:14 -04003347 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04003348 *
3349 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
3350 * uses the kernel idr facility to perform the task.
3351 *
3352 * Return codes:
3353 * instance - a unique integer ID allocated as the new instance.
3354 * -1 - lpfc get instance failed.
3355 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003356int
3357lpfc_get_instance(void)
3358{
Tejun Heoab516032013-02-27 17:04:44 -08003359 int ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003360
Tejun Heoab516032013-02-27 17:04:44 -08003361 ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
3362 return ret < 0 ? -1 : ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003363}
3364
James Smarte59058c2008-08-24 21:49:00 -04003365/**
James Smart3621a712009-04-06 18:47:14 -04003366 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04003367 * @shost: pointer to SCSI host data structure.
3368 * @time: elapsed time of the scan in jiffies.
3369 *
3370 * This routine is called by the SCSI layer with a SCSI host to determine
3371 * whether the scan host is finished.
3372 *
3373 * Note: there is no scan_start function as adapter initialization will have
3374 * asynchronously kicked off the link initialization.
3375 *
3376 * Return codes
3377 * 0 - SCSI host scan is not over yet.
3378 * 1 - SCSI host scan is over.
3379 **/
James Smart47a86172007-04-25 09:53:22 -04003380int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
3381{
James Smart2e0fef82007-06-17 19:56:36 -05003382 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3383 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05003384 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04003385
James Smart858c9f62007-06-17 19:56:39 -05003386 spin_lock_irq(shost->host_lock);
3387
James Smart51ef4c22007-08-02 11:10:31 -04003388 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05003389 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003390 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05003391 }
James Smart256ec0d2013-04-17 20:14:58 -04003392 if (time >= msecs_to_jiffies(30 * 1000)) {
James Smart2e0fef82007-06-17 19:56:36 -05003393 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003394 "0461 Scanning longer than 30 "
3395 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003396 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003397 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003398 }
James Smart256ec0d2013-04-17 20:14:58 -04003399 if (time >= msecs_to_jiffies(15 * 1000) &&
3400 phba->link_state <= LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -05003401 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003402 "0465 Link down longer than 15 "
3403 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003404 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05003405 goto finished;
James Smart47a86172007-04-25 09:53:22 -04003406 }
3407
James Smart2e0fef82007-06-17 19:56:36 -05003408 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05003409 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003410 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05003411 goto finished;
James Smart256ec0d2013-04-17 20:14:58 -04003412 if (vport->fc_map_cnt == 0 && time < msecs_to_jiffies(2 * 1000))
James Smart858c9f62007-06-17 19:56:39 -05003413 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003414 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05003415 goto finished;
3416
3417 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003418
3419finished:
James Smart858c9f62007-06-17 19:56:39 -05003420 spin_unlock_irq(shost->host_lock);
3421 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05003422}
3423
James Smarte59058c2008-08-24 21:49:00 -04003424/**
James Smart3621a712009-04-06 18:47:14 -04003425 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04003426 * @shost: pointer to SCSI host data structure.
3427 *
3428 * This routine initializes a given SCSI host attributes on a FC port. The
3429 * SCSI host can be either on top of a physical port or a virtual port.
3430 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003431void lpfc_host_attrib_init(struct Scsi_Host *shost)
3432{
3433 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3434 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003435 /*
James Smart2e0fef82007-06-17 19:56:36 -05003436 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04003437 */
3438
James Smart2e0fef82007-06-17 19:56:36 -05003439 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
3440 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04003441 fc_host_supported_classes(shost) = FC_COS_CLASS3;
3442
3443 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003444 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003445 fc_host_supported_fc4s(shost)[2] = 1;
3446 fc_host_supported_fc4s(shost)[7] = 1;
3447
James Smart92d7f7b2007-06-17 19:56:38 -05003448 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
3449 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04003450
3451 fc_host_supported_speeds(shost) = 0;
James Smart88a2cfb2011-07-22 18:36:33 -04003452 if (phba->lmt & LMT_16Gb)
3453 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
James Smart47a86172007-04-25 09:53:22 -04003454 if (phba->lmt & LMT_10Gb)
3455 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04003456 if (phba->lmt & LMT_8Gb)
3457 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04003458 if (phba->lmt & LMT_4Gb)
3459 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
3460 if (phba->lmt & LMT_2Gb)
3461 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
3462 if (phba->lmt & LMT_1Gb)
3463 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
3464
3465 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05003466 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
3467 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04003468
Mike Christie0af5d702010-09-15 16:52:31 -05003469 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
3470
James Smart47a86172007-04-25 09:53:22 -04003471 /* This value is also unchanging */
3472 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003473 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003474 fc_host_active_fc4s(shost)[2] = 1;
3475 fc_host_active_fc4s(shost)[7] = 1;
3476
James Smart92d7f7b2007-06-17 19:56:38 -05003477 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04003478 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04003479 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04003480 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04003481}
dea31012005-04-17 16:05:31 -05003482
James Smarte59058c2008-08-24 21:49:00 -04003483/**
James Smartda0436e2009-05-22 14:51:39 -04003484 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04003485 * @phba: pointer to lpfc hba data structure.
3486 *
James Smartda0436e2009-05-22 14:51:39 -04003487 * This routine is invoked to stop an SLI3 device port, it stops the device
3488 * from generating interrupts and stops the device driver's timers for the
3489 * device.
James Smarte59058c2008-08-24 21:49:00 -04003490 **/
James Smartdb2378e2008-02-08 18:49:51 -05003491static void
James Smartda0436e2009-05-22 14:51:39 -04003492lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05003493{
James Smartda0436e2009-05-22 14:51:39 -04003494 /* Clear all interrupt enable conditions */
3495 writel(0, phba->HCregaddr);
3496 readl(phba->HCregaddr); /* flush */
3497 /* Clear all pending interrupts */
3498 writel(0xffffffff, phba->HAregaddr);
3499 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04003500
James Smartda0436e2009-05-22 14:51:39 -04003501 /* Reset some HBA SLI setup states */
3502 lpfc_stop_hba_timers(phba);
3503 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05003504}
3505
James Smarte59058c2008-08-24 21:49:00 -04003506/**
James Smartda0436e2009-05-22 14:51:39 -04003507 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05003508 * @phba: pointer to lpfc hba data structure.
3509 *
James Smartda0436e2009-05-22 14:51:39 -04003510 * This routine is invoked to stop an SLI4 device port, it stops the device
3511 * from generating interrupts and stops the device driver's timers for the
3512 * device.
3513 **/
3514static void
3515lpfc_stop_port_s4(struct lpfc_hba *phba)
3516{
3517 /* Reset some HBA SLI4 setup states */
3518 lpfc_stop_hba_timers(phba);
3519 phba->pport->work_port_events = 0;
3520 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04003521}
3522
3523/**
3524 * lpfc_stop_port - Wrapper function for stopping hba port
3525 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05003526 *
James Smartda0436e2009-05-22 14:51:39 -04003527 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
3528 * the API jump table function pointer from the lpfc_hba struct.
3529 **/
3530void
3531lpfc_stop_port(struct lpfc_hba *phba)
3532{
3533 phba->lpfc_stop_port(phba);
3534}
3535
3536/**
James Smartecfd03c2010-02-12 14:41:27 -05003537 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
3538 * @phba: Pointer to hba for which this call is being executed.
3539 *
3540 * This routine starts the timer waiting for the FCF rediscovery to complete.
3541 **/
3542void
3543lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
3544{
3545 unsigned long fcf_redisc_wait_tmo =
3546 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
3547 /* Start fcf rediscovery wait period timer */
3548 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
3549 spin_lock_irq(&phba->hbalock);
3550 /* Allow action to new fcf asynchronous event */
3551 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
3552 /* Mark the FCF rediscovery pending state */
3553 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
3554 spin_unlock_irq(&phba->hbalock);
3555}
3556
3557/**
3558 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
3559 * @ptr: Map to lpfc_hba data structure pointer.
3560 *
3561 * This routine is invoked when waiting for FCF table rediscover has been
3562 * timed out. If new FCF record(s) has (have) been discovered during the
3563 * wait period, a new FCF event shall be added to the FCOE async event
3564 * list, and then worker thread shall be waked up for processing from the
3565 * worker thread context.
3566 **/
Rashika Kheriae399b222014-09-03 12:55:28 -04003567static void
James Smartecfd03c2010-02-12 14:41:27 -05003568lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
3569{
3570 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
3571
3572 /* Don't send FCF rediscovery event if timer cancelled */
3573 spin_lock_irq(&phba->hbalock);
3574 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
3575 spin_unlock_irq(&phba->hbalock);
3576 return;
3577 }
3578 /* Clear FCF rediscovery timer pending flag */
3579 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
3580 /* FCF rediscovery event to worker thread */
3581 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
3582 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003583 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04003584 "2776 FCF rediscover quiescent timer expired\n");
James Smartecfd03c2010-02-12 14:41:27 -05003585 /* wake up worker thread */
3586 lpfc_worker_wake_up(phba);
3587}
3588
3589/**
James Smartda0436e2009-05-22 14:51:39 -04003590 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
3591 * @phba: pointer to lpfc hba data structure.
3592 * @acqe_link: pointer to the async link completion queue entry.
3593 *
3594 * This routine is to parse the SLI4 link-attention link fault code and
3595 * translate it into the base driver's read link attention mailbox command
3596 * status.
3597 *
3598 * Return: Link-attention status in terms of base driver's coding.
3599 **/
3600static uint16_t
3601lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
3602 struct lpfc_acqe_link *acqe_link)
3603{
3604 uint16_t latt_fault;
3605
3606 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
3607 case LPFC_ASYNC_LINK_FAULT_NONE:
3608 case LPFC_ASYNC_LINK_FAULT_LOCAL:
3609 case LPFC_ASYNC_LINK_FAULT_REMOTE:
3610 latt_fault = 0;
3611 break;
3612 default:
3613 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3614 "0398 Invalid link fault code: x%x\n",
3615 bf_get(lpfc_acqe_link_fault, acqe_link));
3616 latt_fault = MBXERR_ERROR;
3617 break;
3618 }
3619 return latt_fault;
3620}
3621
3622/**
3623 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
3624 * @phba: pointer to lpfc hba data structure.
3625 * @acqe_link: pointer to the async link completion queue entry.
3626 *
3627 * This routine is to parse the SLI4 link attention type and translate it
3628 * into the base driver's link attention type coding.
3629 *
3630 * Return: Link attention type in terms of base driver's coding.
3631 **/
3632static uint8_t
3633lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
3634 struct lpfc_acqe_link *acqe_link)
3635{
3636 uint8_t att_type;
3637
3638 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
3639 case LPFC_ASYNC_LINK_STATUS_DOWN:
3640 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
James Smart76a95d72010-11-20 23:11:48 -05003641 att_type = LPFC_ATT_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04003642 break;
3643 case LPFC_ASYNC_LINK_STATUS_UP:
3644 /* Ignore physical link up events - wait for logical link up */
James Smart76a95d72010-11-20 23:11:48 -05003645 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003646 break;
3647 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
James Smart76a95d72010-11-20 23:11:48 -05003648 att_type = LPFC_ATT_LINK_UP;
James Smartda0436e2009-05-22 14:51:39 -04003649 break;
3650 default:
3651 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3652 "0399 Invalid link attention type: x%x\n",
3653 bf_get(lpfc_acqe_link_status, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003654 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003655 break;
3656 }
3657 return att_type;
3658}
3659
3660/**
3661 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
3662 * @phba: pointer to lpfc hba data structure.
3663 * @acqe_link: pointer to the async link completion queue entry.
3664 *
3665 * This routine is to parse the SLI4 link-attention link speed and translate
3666 * it into the base driver's link-attention link speed coding.
3667 *
3668 * Return: Link-attention link speed in terms of base driver's coding.
3669 **/
3670static uint8_t
3671lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
3672 struct lpfc_acqe_link *acqe_link)
3673{
3674 uint8_t link_speed;
3675
3676 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
3677 case LPFC_ASYNC_LINK_SPEED_ZERO:
James Smartda0436e2009-05-22 14:51:39 -04003678 case LPFC_ASYNC_LINK_SPEED_10MBPS:
James Smartda0436e2009-05-22 14:51:39 -04003679 case LPFC_ASYNC_LINK_SPEED_100MBPS:
James Smart76a95d72010-11-20 23:11:48 -05003680 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003681 break;
3682 case LPFC_ASYNC_LINK_SPEED_1GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003683 link_speed = LPFC_LINK_SPEED_1GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003684 break;
3685 case LPFC_ASYNC_LINK_SPEED_10GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003686 link_speed = LPFC_LINK_SPEED_10GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003687 break;
3688 default:
3689 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3690 "0483 Invalid link-attention link speed: x%x\n",
3691 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003692 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003693 break;
3694 }
3695 return link_speed;
3696}
3697
3698/**
James Smart8b68cd52012-09-29 11:32:37 -04003699 * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
3700 * @phba: pointer to lpfc hba data structure.
3701 *
3702 * This routine is to get an SLI3 FC port's link speed in Mbps.
3703 *
3704 * Return: link speed in terms of Mbps.
3705 **/
3706uint32_t
3707lpfc_sli_port_speed_get(struct lpfc_hba *phba)
3708{
3709 uint32_t link_speed;
3710
3711 if (!lpfc_is_link_up(phba))
3712 return 0;
3713
3714 switch (phba->fc_linkspeed) {
3715 case LPFC_LINK_SPEED_1GHZ:
3716 link_speed = 1000;
3717 break;
3718 case LPFC_LINK_SPEED_2GHZ:
3719 link_speed = 2000;
3720 break;
3721 case LPFC_LINK_SPEED_4GHZ:
3722 link_speed = 4000;
3723 break;
3724 case LPFC_LINK_SPEED_8GHZ:
3725 link_speed = 8000;
3726 break;
3727 case LPFC_LINK_SPEED_10GHZ:
3728 link_speed = 10000;
3729 break;
3730 case LPFC_LINK_SPEED_16GHZ:
3731 link_speed = 16000;
3732 break;
3733 default:
3734 link_speed = 0;
3735 }
3736 return link_speed;
3737}
3738
3739/**
3740 * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
3741 * @phba: pointer to lpfc hba data structure.
3742 * @evt_code: asynchronous event code.
3743 * @speed_code: asynchronous event link speed code.
3744 *
3745 * This routine is to parse the giving SLI4 async event link speed code into
3746 * value of Mbps for the link speed.
3747 *
3748 * Return: link speed in terms of Mbps.
3749 **/
3750static uint32_t
3751lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
3752 uint8_t speed_code)
3753{
3754 uint32_t port_speed;
3755
3756 switch (evt_code) {
3757 case LPFC_TRAILER_CODE_LINK:
3758 switch (speed_code) {
3759 case LPFC_EVT_CODE_LINK_NO_LINK:
3760 port_speed = 0;
3761 break;
3762 case LPFC_EVT_CODE_LINK_10_MBIT:
3763 port_speed = 10;
3764 break;
3765 case LPFC_EVT_CODE_LINK_100_MBIT:
3766 port_speed = 100;
3767 break;
3768 case LPFC_EVT_CODE_LINK_1_GBIT:
3769 port_speed = 1000;
3770 break;
3771 case LPFC_EVT_CODE_LINK_10_GBIT:
3772 port_speed = 10000;
3773 break;
3774 default:
3775 port_speed = 0;
3776 }
3777 break;
3778 case LPFC_TRAILER_CODE_FC:
3779 switch (speed_code) {
3780 case LPFC_EVT_CODE_FC_NO_LINK:
3781 port_speed = 0;
3782 break;
3783 case LPFC_EVT_CODE_FC_1_GBAUD:
3784 port_speed = 1000;
3785 break;
3786 case LPFC_EVT_CODE_FC_2_GBAUD:
3787 port_speed = 2000;
3788 break;
3789 case LPFC_EVT_CODE_FC_4_GBAUD:
3790 port_speed = 4000;
3791 break;
3792 case LPFC_EVT_CODE_FC_8_GBAUD:
3793 port_speed = 8000;
3794 break;
3795 case LPFC_EVT_CODE_FC_10_GBAUD:
3796 port_speed = 10000;
3797 break;
3798 case LPFC_EVT_CODE_FC_16_GBAUD:
3799 port_speed = 16000;
3800 break;
3801 default:
3802 port_speed = 0;
3803 }
3804 break;
3805 default:
3806 port_speed = 0;
3807 }
3808 return port_speed;
3809}
3810
3811/**
James Smart70f3c072010-12-15 17:57:33 -05003812 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
James Smartda0436e2009-05-22 14:51:39 -04003813 * @phba: pointer to lpfc hba data structure.
3814 * @acqe_link: pointer to the async link completion queue entry.
3815 *
James Smart70f3c072010-12-15 17:57:33 -05003816 * This routine is to handle the SLI4 asynchronous FCoE link event.
James Smartda0436e2009-05-22 14:51:39 -04003817 **/
3818static void
3819lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3820 struct lpfc_acqe_link *acqe_link)
3821{
3822 struct lpfc_dmabuf *mp;
3823 LPFC_MBOXQ_t *pmb;
3824 MAILBOX_t *mb;
James Smart76a95d72010-11-20 23:11:48 -05003825 struct lpfc_mbx_read_top *la;
James Smartda0436e2009-05-22 14:51:39 -04003826 uint8_t att_type;
James Smart76a95d72010-11-20 23:11:48 -05003827 int rc;
James Smartda0436e2009-05-22 14:51:39 -04003828
3829 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05003830 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
James Smartda0436e2009-05-22 14:51:39 -04003831 return;
James Smart32b97932009-07-19 10:01:21 -04003832 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003833 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3834 if (!pmb) {
3835 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3836 "0395 The mboxq allocation failed\n");
3837 return;
3838 }
3839 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3840 if (!mp) {
3841 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3842 "0396 The lpfc_dmabuf allocation failed\n");
3843 goto out_free_pmb;
3844 }
3845 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3846 if (!mp->virt) {
3847 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3848 "0397 The mbuf allocation failed\n");
3849 goto out_free_dmabuf;
3850 }
3851
3852 /* Cleanup any outstanding ELS commands */
3853 lpfc_els_flush_all_cmd(phba);
3854
3855 /* Block ELS IOCBs until we have done process link event */
3856 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3857
3858 /* Update link event statistics */
3859 phba->sli.slistat.link_event++;
3860
James Smart76a95d72010-11-20 23:11:48 -05003861 /* Create lpfc_handle_latt mailbox command from link ACQE */
3862 lpfc_read_topology(phba, pmb, mp);
3863 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smartda0436e2009-05-22 14:51:39 -04003864 pmb->vport = phba->pport;
3865
James Smartda0436e2009-05-22 14:51:39 -04003866 /* Keep the link status for extra SLI4 state machine reference */
3867 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04003868 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
3869 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smartda0436e2009-05-22 14:51:39 -04003870 phba->sli4_hba.link_state.duplex =
3871 bf_get(lpfc_acqe_link_duplex, acqe_link);
3872 phba->sli4_hba.link_state.status =
3873 bf_get(lpfc_acqe_link_status, acqe_link);
James Smart70f3c072010-12-15 17:57:33 -05003874 phba->sli4_hba.link_state.type =
3875 bf_get(lpfc_acqe_link_type, acqe_link);
3876 phba->sli4_hba.link_state.number =
3877 bf_get(lpfc_acqe_link_number, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04003878 phba->sli4_hba.link_state.fault =
3879 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05003880 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04003881 bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
3882
James Smart70f3c072010-12-15 17:57:33 -05003883 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartc31098c2011-04-16 11:03:33 -04003884 "2900 Async FC/FCoE Link event - Speed:%dGBit "
3885 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
3886 "Logical speed:%dMbps Fault:%d\n",
James Smart70f3c072010-12-15 17:57:33 -05003887 phba->sli4_hba.link_state.speed,
3888 phba->sli4_hba.link_state.topology,
3889 phba->sli4_hba.link_state.status,
3890 phba->sli4_hba.link_state.type,
3891 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04003892 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05003893 phba->sli4_hba.link_state.fault);
James Smart76a95d72010-11-20 23:11:48 -05003894 /*
3895 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
3896 * topology info. Note: Optional for non FC-AL ports.
3897 */
3898 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3899 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3900 if (rc == MBX_NOT_FINISHED)
3901 goto out_free_dmabuf;
3902 return;
3903 }
3904 /*
3905 * For FCoE Mode: fill in all the topology information we need and call
3906 * the READ_TOPOLOGY completion routine to continue without actually
3907 * sending the READ_TOPOLOGY mailbox command to the port.
3908 */
3909 /* Parse and translate status field */
3910 mb = &pmb->u.mb;
3911 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
3912
3913 /* Parse and translate link attention fields */
3914 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
3915 la->eventTag = acqe_link->event_tag;
3916 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
3917 bf_set(lpfc_mbx_read_top_link_spd, la,
3918 lpfc_sli4_parse_latt_link_speed(phba, acqe_link));
3919
3920 /* Fake the the following irrelvant fields */
3921 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
3922 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
3923 bf_set(lpfc_mbx_read_top_il, la, 0);
3924 bf_set(lpfc_mbx_read_top_pb, la, 0);
3925 bf_set(lpfc_mbx_read_top_fa, la, 0);
3926 bf_set(lpfc_mbx_read_top_mm, la, 0);
James Smartda0436e2009-05-22 14:51:39 -04003927
3928 /* Invoke the lpfc_handle_latt mailbox command callback function */
James Smart76a95d72010-11-20 23:11:48 -05003929 lpfc_mbx_cmpl_read_topology(phba, pmb);
James Smartda0436e2009-05-22 14:51:39 -04003930
3931 return;
3932
3933out_free_dmabuf:
3934 kfree(mp);
3935out_free_pmb:
3936 mempool_free(pmb, phba->mbox_mem_pool);
3937}
3938
3939/**
James Smart70f3c072010-12-15 17:57:33 -05003940 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
3941 * @phba: pointer to lpfc hba data structure.
3942 * @acqe_fc: pointer to the async fc completion queue entry.
3943 *
3944 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
3945 * that the event was received and then issue a read_topology mailbox command so
3946 * that the rest of the driver will treat it the same as SLI3.
3947 **/
3948static void
3949lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
3950{
3951 struct lpfc_dmabuf *mp;
3952 LPFC_MBOXQ_t *pmb;
3953 int rc;
3954
3955 if (bf_get(lpfc_trailer_type, acqe_fc) !=
3956 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
3957 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3958 "2895 Non FC link Event detected.(%d)\n",
3959 bf_get(lpfc_trailer_type, acqe_fc));
3960 return;
3961 }
3962 /* Keep the link status for extra SLI4 state machine reference */
3963 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04003964 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
3965 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
James Smart70f3c072010-12-15 17:57:33 -05003966 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
3967 phba->sli4_hba.link_state.topology =
3968 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
3969 phba->sli4_hba.link_state.status =
3970 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
3971 phba->sli4_hba.link_state.type =
3972 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
3973 phba->sli4_hba.link_state.number =
3974 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
3975 phba->sli4_hba.link_state.fault =
3976 bf_get(lpfc_acqe_link_fault, acqe_fc);
3977 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04003978 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
James Smart70f3c072010-12-15 17:57:33 -05003979 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3980 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
3981 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
3982 "%dMbps Fault:%d\n",
3983 phba->sli4_hba.link_state.speed,
3984 phba->sli4_hba.link_state.topology,
3985 phba->sli4_hba.link_state.status,
3986 phba->sli4_hba.link_state.type,
3987 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04003988 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05003989 phba->sli4_hba.link_state.fault);
3990 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3991 if (!pmb) {
3992 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3993 "2897 The mboxq allocation failed\n");
3994 return;
3995 }
3996 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3997 if (!mp) {
3998 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3999 "2898 The lpfc_dmabuf allocation failed\n");
4000 goto out_free_pmb;
4001 }
4002 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
4003 if (!mp->virt) {
4004 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4005 "2899 The mbuf allocation failed\n");
4006 goto out_free_dmabuf;
4007 }
4008
4009 /* Cleanup any outstanding ELS commands */
4010 lpfc_els_flush_all_cmd(phba);
4011
4012 /* Block ELS IOCBs until we have done process link event */
4013 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
4014
4015 /* Update link event statistics */
4016 phba->sli.slistat.link_event++;
4017
4018 /* Create lpfc_handle_latt mailbox command from link ACQE */
4019 lpfc_read_topology(phba, pmb, mp);
4020 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
4021 pmb->vport = phba->pport;
4022
4023 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4024 if (rc == MBX_NOT_FINISHED)
4025 goto out_free_dmabuf;
4026 return;
4027
4028out_free_dmabuf:
4029 kfree(mp);
4030out_free_pmb:
4031 mempool_free(pmb, phba->mbox_mem_pool);
4032}
4033
4034/**
4035 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
4036 * @phba: pointer to lpfc hba data structure.
4037 * @acqe_fc: pointer to the async SLI completion queue entry.
4038 *
4039 * This routine is to handle the SLI4 asynchronous SLI events.
4040 **/
4041static void
4042lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
4043{
James Smart4b8bae02012-06-12 13:55:07 -04004044 char port_name;
James Smart8c1312e2012-10-31 14:45:09 -04004045 char message[128];
James Smart4b8bae02012-06-12 13:55:07 -04004046 uint8_t status;
4047 struct lpfc_acqe_misconfigured_event *misconfigured;
4048
4049 /* special case misconfigured event as it contains data for all ports */
4050 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
4051 LPFC_SLI_INTF_IF_TYPE_2) ||
4052 (bf_get(lpfc_trailer_type, acqe_sli) !=
4053 LPFC_SLI_EVENT_TYPE_MISCONFIGURED)) {
4054 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4055 "2901 Async SLI event - Event Data1:x%08x Event Data2:"
4056 "x%08x SLI Event Type:%d\n",
4057 acqe_sli->event_data1, acqe_sli->event_data2,
4058 bf_get(lpfc_trailer_type, acqe_sli));
4059 return;
4060 }
4061
4062 port_name = phba->Port[0];
4063 if (port_name == 0x00)
4064 port_name = '?'; /* get port name is empty */
4065
4066 misconfigured = (struct lpfc_acqe_misconfigured_event *)
4067 &acqe_sli->event_data1;
4068
4069 /* fetch the status for this port */
4070 switch (phba->sli4_hba.lnk_info.lnk_no) {
4071 case LPFC_LINK_NUMBER_0:
4072 status = bf_get(lpfc_sli_misconfigured_port0,
4073 &misconfigured->theEvent);
4074 break;
4075 case LPFC_LINK_NUMBER_1:
4076 status = bf_get(lpfc_sli_misconfigured_port1,
4077 &misconfigured->theEvent);
4078 break;
4079 case LPFC_LINK_NUMBER_2:
4080 status = bf_get(lpfc_sli_misconfigured_port2,
4081 &misconfigured->theEvent);
4082 break;
4083 case LPFC_LINK_NUMBER_3:
4084 status = bf_get(lpfc_sli_misconfigured_port3,
4085 &misconfigured->theEvent);
4086 break;
4087 default:
4088 status = ~LPFC_SLI_EVENT_STATUS_VALID;
4089 break;
4090 }
4091
4092 switch (status) {
4093 case LPFC_SLI_EVENT_STATUS_VALID:
4094 return; /* no message if the sfp is okay */
4095 case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
James Smart292098b2012-09-29 11:31:41 -04004096 sprintf(message, "Optics faulted/incorrectly installed/not " \
4097 "installed - Reseat optics, if issue not "
4098 "resolved, replace.");
James Smart4b8bae02012-06-12 13:55:07 -04004099 break;
4100 case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
4101 sprintf(message,
James Smart292098b2012-09-29 11:31:41 -04004102 "Optics of two types installed - Remove one optic or " \
4103 "install matching pair of optics.");
James Smart4b8bae02012-06-12 13:55:07 -04004104 break;
4105 case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
James Smart292098b2012-09-29 11:31:41 -04004106 sprintf(message, "Incompatible optics - Replace with " \
4107 "compatible optics for card to function.");
James Smart4b8bae02012-06-12 13:55:07 -04004108 break;
4109 default:
4110 /* firmware is reporting a status we don't know about */
4111 sprintf(message, "Unknown event status x%02x", status);
4112 break;
4113 }
4114
4115 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4116 "3176 Misconfigured Physical Port - "
4117 "Port Name %c %s\n", port_name, message);
James Smart70f3c072010-12-15 17:57:33 -05004118}
4119
4120/**
James Smartfc2b9892010-02-26 14:15:29 -05004121 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
4122 * @vport: pointer to vport data structure.
4123 *
4124 * This routine is to perform Clear Virtual Link (CVL) on a vport in
4125 * response to a CVL event.
4126 *
4127 * Return the pointer to the ndlp with the vport if successful, otherwise
4128 * return NULL.
4129 **/
4130static struct lpfc_nodelist *
4131lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
4132{
4133 struct lpfc_nodelist *ndlp;
4134 struct Scsi_Host *shost;
4135 struct lpfc_hba *phba;
4136
4137 if (!vport)
4138 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05004139 phba = vport->phba;
4140 if (!phba)
4141 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04004142 ndlp = lpfc_findnode_did(vport, Fabric_DID);
4143 if (!ndlp) {
4144 /* Cannot find existing Fabric ndlp, so allocate a new one */
4145 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
4146 if (!ndlp)
4147 return 0;
4148 lpfc_nlp_init(vport, ndlp, Fabric_DID);
4149 /* Set the node type */
4150 ndlp->nlp_type |= NLP_FABRIC;
4151 /* Put ndlp onto node list */
4152 lpfc_enqueue_node(vport, ndlp);
4153 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4154 /* re-setup ndlp without removing from node list */
4155 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
4156 if (!ndlp)
4157 return 0;
4158 }
James Smart63e801c2010-11-20 23:14:19 -05004159 if ((phba->pport->port_state < LPFC_FLOGI) &&
4160 (phba->pport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05004161 return NULL;
4162 /* If virtual link is not yet instantiated ignore CVL */
James Smart63e801c2010-11-20 23:14:19 -05004163 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
4164 && (vport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05004165 return NULL;
4166 shost = lpfc_shost_from_vport(vport);
4167 if (!shost)
4168 return NULL;
4169 lpfc_linkdown_port(vport);
4170 lpfc_cleanup_pending_mbox(vport);
4171 spin_lock_irq(shost->host_lock);
4172 vport->fc_flag |= FC_VPORT_CVL_RCVD;
4173 spin_unlock_irq(shost->host_lock);
4174
4175 return ndlp;
4176}
4177
4178/**
4179 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
4180 * @vport: pointer to lpfc hba data structure.
4181 *
4182 * This routine is to perform Clear Virtual Link (CVL) on all vports in
4183 * response to a FCF dead event.
4184 **/
4185static void
4186lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
4187{
4188 struct lpfc_vport **vports;
4189 int i;
4190
4191 vports = lpfc_create_vport_work_array(phba);
4192 if (vports)
4193 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
4194 lpfc_sli4_perform_vport_cvl(vports[i]);
4195 lpfc_destroy_vport_work_array(phba, vports);
4196}
4197
4198/**
James Smart76a95d72010-11-20 23:11:48 -05004199 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
James Smartda0436e2009-05-22 14:51:39 -04004200 * @phba: pointer to lpfc hba data structure.
4201 * @acqe_link: pointer to the async fcoe completion queue entry.
4202 *
4203 * This routine is to handle the SLI4 asynchronous fcoe event.
4204 **/
4205static void
James Smart76a95d72010-11-20 23:11:48 -05004206lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
James Smart70f3c072010-12-15 17:57:33 -05004207 struct lpfc_acqe_fip *acqe_fip)
James Smartda0436e2009-05-22 14:51:39 -04004208{
James Smart70f3c072010-12-15 17:57:33 -05004209 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004210 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04004211 struct lpfc_vport *vport;
4212 struct lpfc_nodelist *ndlp;
4213 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05004214 int active_vlink_present;
4215 struct lpfc_vport **vports;
4216 int i;
James Smartda0436e2009-05-22 14:51:39 -04004217
James Smart70f3c072010-12-15 17:57:33 -05004218 phba->fc_eventTag = acqe_fip->event_tag;
4219 phba->fcoe_eventtag = acqe_fip->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004220 switch (event_type) {
James Smart70f3c072010-12-15 17:57:33 -05004221 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
4222 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
4223 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
James Smart999d8132010-03-15 11:24:56 -04004224 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4225 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004226 "2546 New FCF event, evt_tag:x%x, "
4227 "index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004228 acqe_fip->event_tag,
4229 acqe_fip->index);
James Smart999d8132010-03-15 11:24:56 -04004230 else
4231 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
4232 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004233 "2788 FCF param modified event, "
4234 "evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004235 acqe_fip->event_tag,
4236 acqe_fip->index);
James Smart38b92ef2010-08-04 16:11:39 -04004237 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004238 /*
4239 * During period of FCF discovery, read the FCF
4240 * table record indexed by the event to update
James Smarta93ff372010-10-22 11:06:08 -04004241 * FCF roundrobin failover eligible FCF bmask.
James Smart0c9ab6f2010-02-26 14:15:57 -05004242 */
4243 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4244 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004245 "2779 Read FCF (x%x) for updating "
4246 "roundrobin FCF failover bmask\n",
James Smart70f3c072010-12-15 17:57:33 -05004247 acqe_fip->index);
4248 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004249 }
James Smart38b92ef2010-08-04 16:11:39 -04004250
4251 /* If the FCF discovery is in progress, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04004252 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04004253 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart38b92ef2010-08-04 16:11:39 -04004254 spin_unlock_irq(&phba->hbalock);
4255 break;
4256 }
4257 /* If fast FCF failover rescan event is pending, do nothing */
4258 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
4259 spin_unlock_irq(&phba->hbalock);
4260 break;
4261 }
4262
James Smartc2b97122013-05-31 17:05:36 -04004263 /* If the FCF has been in discovered state, do nothing. */
4264 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
James Smart3804dc82010-07-14 15:31:37 -04004265 spin_unlock_irq(&phba->hbalock);
4266 break;
4267 }
4268 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04004269
James Smart0c9ab6f2010-02-26 14:15:57 -05004270 /* Otherwise, scan the entire FCF table and re-discover SAN */
4271 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004272 "2770 Start FCF table scan per async FCF "
4273 "event, evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004274 acqe_fip->event_tag, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004275 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
4276 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04004277 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05004278 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4279 "2547 Issue FCF scan read FCF mailbox "
James Smarta93ff372010-10-22 11:06:08 -04004280 "command failed (x%x)\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004281 break;
4282
James Smart70f3c072010-12-15 17:57:33 -05004283 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
James Smartda0436e2009-05-22 14:51:39 -04004284 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04004285 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004286 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
4287 acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04004288 break;
4289
James Smart70f3c072010-12-15 17:57:33 -05004290 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
James Smart80c17842012-03-01 22:35:45 -05004291 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05004292 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004293 "2549 FCF (x%x) disconnected from network, "
James Smart70f3c072010-12-15 17:57:33 -05004294 "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart38b92ef2010-08-04 16:11:39 -04004295 /*
4296 * If we are in the middle of FCF failover process, clear
4297 * the corresponding FCF bit in the roundrobin bitmap.
James Smartda0436e2009-05-22 14:51:39 -04004298 */
James Smartfc2b9892010-02-26 14:15:29 -05004299 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004300 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05004301 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004302 /* Update FLOGI FCF failover eligible FCF bmask */
James Smart70f3c072010-12-15 17:57:33 -05004303 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004304 break;
4305 }
James Smart38b92ef2010-08-04 16:11:39 -04004306 spin_unlock_irq(&phba->hbalock);
4307
4308 /* If the event is not for currently used fcf do nothing */
James Smart70f3c072010-12-15 17:57:33 -05004309 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
James Smart38b92ef2010-08-04 16:11:39 -04004310 break;
4311
4312 /*
4313 * Otherwise, request the port to rediscover the entire FCF
4314 * table for a fast recovery from case that the current FCF
4315 * is no longer valid as we are not in the middle of FCF
4316 * failover process already.
4317 */
James Smartc2b97122013-05-31 17:05:36 -04004318 spin_lock_irq(&phba->hbalock);
4319 /* Mark the fast failover process in progress */
4320 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
4321 spin_unlock_irq(&phba->hbalock);
4322
4323 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
4324 "2771 Start FCF fast failover process due to "
4325 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
4326 "\n", acqe_fip->event_tag, acqe_fip->index);
4327 rc = lpfc_sli4_redisc_fcf_table(phba);
4328 if (rc) {
4329 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4330 LOG_DISCOVERY,
4331 "2772 Issue FCF rediscover mabilbox "
4332 "command failed, fail through to FCF "
4333 "dead event\n");
4334 spin_lock_irq(&phba->hbalock);
4335 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
4336 spin_unlock_irq(&phba->hbalock);
4337 /*
4338 * Last resort will fail over by treating this
4339 * as a link down to FCF registration.
4340 */
4341 lpfc_sli4_fcf_dead_failthrough(phba);
4342 } else {
4343 /* Reset FCF roundrobin bmask for new discovery */
4344 lpfc_sli4_clear_fcf_rr_bmask(phba);
4345 /*
4346 * Handling fast FCF failover to a DEAD FCF event is
4347 * considered equalivant to receiving CVL to all vports.
4348 */
4349 lpfc_sli4_perform_all_vport_cvl(phba);
4350 }
James Smartda0436e2009-05-22 14:51:39 -04004351 break;
James Smart70f3c072010-12-15 17:57:33 -05004352 case LPFC_FIP_EVENT_TYPE_CVL:
James Smart80c17842012-03-01 22:35:45 -05004353 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05004354 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6669f9b2009-10-02 15:16:45 -04004355 "2718 Clear Virtual Link Received for VPI 0x%x"
James Smart70f3c072010-12-15 17:57:33 -05004356 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart6d368e52011-05-24 11:44:12 -04004357
James Smart6669f9b2009-10-02 15:16:45 -04004358 vport = lpfc_find_vport_by_vpid(phba,
James Smart5248a742011-07-22 18:37:06 -04004359 acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004360 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004361 if (!ndlp)
4362 break;
James Smart695a8142010-01-26 23:08:03 -05004363 active_vlink_present = 0;
4364
4365 vports = lpfc_create_vport_work_array(phba);
4366 if (vports) {
4367 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
4368 i++) {
4369 if ((!(vports[i]->fc_flag &
4370 FC_VPORT_CVL_RCVD)) &&
4371 (vports[i]->port_state > LPFC_FDISC)) {
4372 active_vlink_present = 1;
4373 break;
4374 }
4375 }
4376 lpfc_destroy_vport_work_array(phba, vports);
4377 }
4378
4379 if (active_vlink_present) {
4380 /*
4381 * If there are other active VLinks present,
4382 * re-instantiate the Vlink using FDISC.
4383 */
James Smart256ec0d2013-04-17 20:14:58 -04004384 mod_timer(&ndlp->nlp_delayfunc,
4385 jiffies + msecs_to_jiffies(1000));
James Smartfc2b9892010-02-26 14:15:29 -05004386 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004387 spin_lock_irq(shost->host_lock);
4388 ndlp->nlp_flag |= NLP_DELAY_TMO;
4389 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05004390 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
4391 vport->port_state = LPFC_FDISC;
4392 } else {
James Smartecfd03c2010-02-12 14:41:27 -05004393 /*
4394 * Otherwise, we request port to rediscover
4395 * the entire FCF table for a fast recovery
4396 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05004397 * is no longer valid if we are not already
4398 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05004399 */
James Smartfc2b9892010-02-26 14:15:29 -05004400 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004401 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05004402 spin_unlock_irq(&phba->hbalock);
4403 break;
4404 }
4405 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05004406 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004407 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004408 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4409 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004410 "2773 Start FCF failover per CVL, "
James Smart70f3c072010-12-15 17:57:33 -05004411 "evt_tag:x%x\n", acqe_fip->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05004412 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05004413 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004414 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4415 LOG_DISCOVERY,
4416 "2774 Issue FCF rediscover "
4417 "mabilbox command failed, "
4418 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05004419 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004420 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004421 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05004422 /*
4423 * Last resort will be re-try on the
4424 * the current registered FCF entry.
4425 */
4426 lpfc_retry_pport_discovery(phba);
James Smart38b92ef2010-08-04 16:11:39 -04004427 } else
4428 /*
4429 * Reset FCF roundrobin bmask for new
4430 * discovery.
4431 */
James Smart7d791df2011-07-22 18:37:52 -04004432 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart6669f9b2009-10-02 15:16:45 -04004433 }
4434 break;
James Smartda0436e2009-05-22 14:51:39 -04004435 default:
4436 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4437 "0288 Unknown FCoE event type 0x%x event tag "
James Smart70f3c072010-12-15 17:57:33 -05004438 "0x%x\n", event_type, acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04004439 break;
4440 }
4441}
4442
4443/**
4444 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
4445 * @phba: pointer to lpfc hba data structure.
4446 * @acqe_link: pointer to the async dcbx completion queue entry.
4447 *
4448 * This routine is to handle the SLI4 asynchronous dcbx event.
4449 **/
4450static void
4451lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
4452 struct lpfc_acqe_dcbx *acqe_dcbx)
4453{
James Smart4d9ab992009-10-02 15:16:39 -04004454 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004455 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4456 "0290 The SLI4 DCBX asynchronous event is not "
4457 "handled yet\n");
4458}
4459
4460/**
James Smartb19a0612010-04-06 14:48:51 -04004461 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
4462 * @phba: pointer to lpfc hba data structure.
4463 * @acqe_link: pointer to the async grp5 completion queue entry.
4464 *
4465 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
4466 * is an asynchronous notified of a logical link speed change. The Port
4467 * reports the logical link speed in units of 10Mbps.
4468 **/
4469static void
4470lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
4471 struct lpfc_acqe_grp5 *acqe_grp5)
4472{
4473 uint16_t prev_ll_spd;
4474
4475 phba->fc_eventTag = acqe_grp5->event_tag;
4476 phba->fcoe_eventtag = acqe_grp5->event_tag;
4477 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
4478 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04004479 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
James Smartb19a0612010-04-06 14:48:51 -04004480 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4481 "2789 GRP5 Async Event: Updating logical link speed "
James Smart8b68cd52012-09-29 11:32:37 -04004482 "from %dMbps to %dMbps\n", prev_ll_spd,
4483 phba->sli4_hba.link_state.logical_speed);
James Smartb19a0612010-04-06 14:48:51 -04004484}
4485
4486/**
James Smartda0436e2009-05-22 14:51:39 -04004487 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
4488 * @phba: pointer to lpfc hba data structure.
4489 *
4490 * This routine is invoked by the worker thread to process all the pending
4491 * SLI4 asynchronous events.
4492 **/
4493void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
4494{
4495 struct lpfc_cq_event *cq_event;
4496
4497 /* First, declare the async event has been handled */
4498 spin_lock_irq(&phba->hbalock);
4499 phba->hba_flag &= ~ASYNC_EVENT;
4500 spin_unlock_irq(&phba->hbalock);
4501 /* Now, handle all the async events */
4502 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
4503 /* Get the first event from the head of the event queue */
4504 spin_lock_irq(&phba->hbalock);
4505 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
4506 cq_event, struct lpfc_cq_event, list);
4507 spin_unlock_irq(&phba->hbalock);
4508 /* Process the asynchronous event */
4509 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
4510 case LPFC_TRAILER_CODE_LINK:
4511 lpfc_sli4_async_link_evt(phba,
4512 &cq_event->cqe.acqe_link);
4513 break;
4514 case LPFC_TRAILER_CODE_FCOE:
James Smart70f3c072010-12-15 17:57:33 -05004515 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004516 break;
4517 case LPFC_TRAILER_CODE_DCBX:
4518 lpfc_sli4_async_dcbx_evt(phba,
4519 &cq_event->cqe.acqe_dcbx);
4520 break;
James Smartb19a0612010-04-06 14:48:51 -04004521 case LPFC_TRAILER_CODE_GRP5:
4522 lpfc_sli4_async_grp5_evt(phba,
4523 &cq_event->cqe.acqe_grp5);
4524 break;
James Smart70f3c072010-12-15 17:57:33 -05004525 case LPFC_TRAILER_CODE_FC:
4526 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
4527 break;
4528 case LPFC_TRAILER_CODE_SLI:
4529 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
4530 break;
James Smartda0436e2009-05-22 14:51:39 -04004531 default:
4532 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4533 "1804 Invalid asynchrous event code: "
4534 "x%x\n", bf_get(lpfc_trailer_code,
4535 &cq_event->cqe.mcqe_cmpl));
4536 break;
4537 }
4538 /* Free the completion event processed to the free pool */
4539 lpfc_sli4_cq_event_release(phba, cq_event);
4540 }
4541}
4542
4543/**
James Smartecfd03c2010-02-12 14:41:27 -05004544 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
4545 * @phba: pointer to lpfc hba data structure.
4546 *
4547 * This routine is invoked by the worker thread to process FCF table
4548 * rediscovery pending completion event.
4549 **/
4550void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
4551{
4552 int rc;
4553
4554 spin_lock_irq(&phba->hbalock);
4555 /* Clear FCF rediscovery timeout event */
4556 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
4557 /* Clear driver fast failover FCF record flag */
4558 phba->fcf.failover_rec.flag = 0;
4559 /* Set state for FCF fast failover */
4560 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
4561 spin_unlock_irq(&phba->hbalock);
4562
4563 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05004564 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004565 "2777 Start post-quiescent FCF table scan\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05004566 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05004567 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05004568 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4569 "2747 Issue FCF scan read FCF mailbox "
4570 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05004571}
4572
4573/**
James Smartda0436e2009-05-22 14:51:39 -04004574 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
4575 * @phba: pointer to lpfc hba data structure.
4576 * @dev_grp: The HBA PCI-Device group number.
4577 *
4578 * This routine is invoked to set up the per HBA PCI-Device group function
4579 * API jump table entries.
4580 *
4581 * Return: 0 if success, otherwise -ENODEV
4582 **/
4583int
4584lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05004585{
4586 int rc;
4587
James Smartda0436e2009-05-22 14:51:39 -04004588 /* Set up lpfc PCI-device group */
4589 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05004590
James Smartda0436e2009-05-22 14:51:39 -04004591 /* The LPFC_PCI_DEV_OC uses SLI4 */
4592 if (dev_grp == LPFC_PCI_DEV_OC)
4593 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05004594
James Smartda0436e2009-05-22 14:51:39 -04004595 /* Set up device INIT API function jump table */
4596 rc = lpfc_init_api_table_setup(phba, dev_grp);
4597 if (rc)
4598 return -ENODEV;
4599 /* Set up SCSI API function jump table */
4600 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
4601 if (rc)
4602 return -ENODEV;
4603 /* Set up SLI API function jump table */
4604 rc = lpfc_sli_api_table_setup(phba, dev_grp);
4605 if (rc)
4606 return -ENODEV;
4607 /* Set up MBOX API function jump table */
4608 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
4609 if (rc)
4610 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004611
James Smartda0436e2009-05-22 14:51:39 -04004612 return 0;
James Smart5b75da22008-12-04 22:39:35 -05004613}
4614
4615/**
James Smart3621a712009-04-06 18:47:14 -04004616 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05004617 * @phba: pointer to lpfc hba data structure.
4618 * @intr_mode: active interrupt mode adopted.
4619 *
4620 * This routine it invoked to log the currently used active interrupt mode
4621 * to the device.
James Smart3772a992009-05-22 14:50:54 -04004622 **/
4623static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05004624{
4625 switch (intr_mode) {
4626 case 0:
4627 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4628 "0470 Enable INTx interrupt mode.\n");
4629 break;
4630 case 1:
4631 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4632 "0481 Enabled MSI interrupt mode.\n");
4633 break;
4634 case 2:
4635 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4636 "0480 Enabled MSI-X interrupt mode.\n");
4637 break;
4638 default:
4639 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4640 "0482 Illegal interrupt mode.\n");
4641 break;
4642 }
4643 return;
4644}
4645
James Smart5b75da22008-12-04 22:39:35 -05004646/**
James Smart3772a992009-05-22 14:50:54 -04004647 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05004648 * @phba: pointer to lpfc hba data structure.
4649 *
James Smart3772a992009-05-22 14:50:54 -04004650 * This routine is invoked to enable the PCI device that is common to all
4651 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004652 *
4653 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004654 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004655 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05004656 **/
James Smart3772a992009-05-22 14:50:54 -04004657static int
4658lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004659{
James Smart3772a992009-05-22 14:50:54 -04004660 struct pci_dev *pdev;
James Smart079b5c92011-08-21 21:48:49 -04004661 int bars = 0;
James Smart5b75da22008-12-04 22:39:35 -05004662
James Smart3772a992009-05-22 14:50:54 -04004663 /* Obtain PCI device reference */
4664 if (!phba->pcidev)
4665 goto out_error;
4666 else
4667 pdev = phba->pcidev;
4668 /* Select PCI BARs */
4669 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4670 /* Enable PCI device */
4671 if (pci_enable_device_mem(pdev))
4672 goto out_error;
4673 /* Request PCI resource for the device */
4674 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
4675 goto out_disable_device;
4676 /* Set up device as PCI master and save state for EEH */
4677 pci_set_master(pdev);
4678 pci_try_set_mwi(pdev);
4679 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05004680
James Smart05580562011-05-24 11:40:48 -04004681 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
Jon Mason453193e2013-09-11 15:38:13 -07004682 if (pci_is_pcie(pdev))
James Smart05580562011-05-24 11:40:48 -04004683 pdev->needs_freset = 1;
4684
James Smart3772a992009-05-22 14:50:54 -04004685 return 0;
James Smart5b75da22008-12-04 22:39:35 -05004686
James Smart3772a992009-05-22 14:50:54 -04004687out_disable_device:
4688 pci_disable_device(pdev);
4689out_error:
James Smart079b5c92011-08-21 21:48:49 -04004690 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4691 "1401 Failed to enable pci device, bars:x%x\n", bars);
James Smart3772a992009-05-22 14:50:54 -04004692 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004693}
4694
4695/**
James Smart3772a992009-05-22 14:50:54 -04004696 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05004697 * @phba: pointer to lpfc hba data structure.
4698 *
James Smart3772a992009-05-22 14:50:54 -04004699 * This routine is invoked to disable the PCI device that is common to all
4700 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004701 **/
4702static void
James Smart3772a992009-05-22 14:50:54 -04004703lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004704{
James Smart3772a992009-05-22 14:50:54 -04004705 struct pci_dev *pdev;
4706 int bars;
James Smart5b75da22008-12-04 22:39:35 -05004707
James Smart3772a992009-05-22 14:50:54 -04004708 /* Obtain PCI device reference */
4709 if (!phba->pcidev)
4710 return;
4711 else
4712 pdev = phba->pcidev;
4713 /* Select PCI BARs */
4714 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4715 /* Release PCI resource and disable PCI device */
4716 pci_release_selected_regions(pdev, bars);
4717 pci_disable_device(pdev);
James Smart5b75da22008-12-04 22:39:35 -05004718
4719 return;
4720}
4721
4722/**
James Smart3772a992009-05-22 14:50:54 -04004723 * lpfc_reset_hba - Reset a hba
4724 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04004725 *
James Smart3772a992009-05-22 14:50:54 -04004726 * This routine is invoked to reset a hba device. It brings the HBA
4727 * offline, performs a board restart, and then brings the board back
4728 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
4729 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04004730 **/
James Smart3772a992009-05-22 14:50:54 -04004731void
4732lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004733{
James Smart3772a992009-05-22 14:50:54 -04004734 /* If resets are disabled then set error state and return. */
4735 if (!phba->cfg_enable_hba_reset) {
4736 phba->link_state = LPFC_HBA_ERROR;
4737 return;
4738 }
James Smartee620212014-04-04 13:51:53 -04004739 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
4740 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
4741 else
4742 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart3772a992009-05-22 14:50:54 -04004743 lpfc_offline(phba);
4744 lpfc_sli_brdrestart(phba);
4745 lpfc_online(phba);
4746 lpfc_unblock_mgmt_io(phba);
4747}
dea31012005-04-17 16:05:31 -05004748
James Smart3772a992009-05-22 14:50:54 -04004749/**
James Smart0a96e972011-07-22 18:37:28 -04004750 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
4751 * @phba: pointer to lpfc hba data structure.
4752 *
4753 * This function enables the PCI SR-IOV virtual functions to a physical
4754 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4755 * enable the number of virtual functions to the physical function. As
4756 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4757 * API call does not considered as an error condition for most of the device.
4758 **/
4759uint16_t
4760lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
4761{
4762 struct pci_dev *pdev = phba->pcidev;
4763 uint16_t nr_virtfn;
4764 int pos;
4765
James Smart0a96e972011-07-22 18:37:28 -04004766 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
4767 if (pos == 0)
4768 return 0;
4769
4770 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
4771 return nr_virtfn;
4772}
4773
4774/**
James Smart912e3ac2011-05-24 11:42:11 -04004775 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
4776 * @phba: pointer to lpfc hba data structure.
4777 * @nr_vfn: number of virtual functions to be enabled.
4778 *
4779 * This function enables the PCI SR-IOV virtual functions to a physical
4780 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4781 * enable the number of virtual functions to the physical function. As
4782 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4783 * API call does not considered as an error condition for most of the device.
4784 **/
4785int
4786lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
4787{
4788 struct pci_dev *pdev = phba->pcidev;
James Smart0a96e972011-07-22 18:37:28 -04004789 uint16_t max_nr_vfn;
James Smart912e3ac2011-05-24 11:42:11 -04004790 int rc;
4791
James Smart0a96e972011-07-22 18:37:28 -04004792 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
4793 if (nr_vfn > max_nr_vfn) {
4794 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4795 "3057 Requested vfs (%d) greater than "
4796 "supported vfs (%d)", nr_vfn, max_nr_vfn);
4797 return -EINVAL;
4798 }
4799
James Smart912e3ac2011-05-24 11:42:11 -04004800 rc = pci_enable_sriov(pdev, nr_vfn);
4801 if (rc) {
4802 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4803 "2806 Failed to enable sriov on this device "
4804 "with vfn number nr_vf:%d, rc:%d\n",
4805 nr_vfn, rc);
4806 } else
4807 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4808 "2807 Successful enable sriov on this device "
4809 "with vfn number nr_vf:%d\n", nr_vfn);
4810 return rc;
4811}
4812
4813/**
James Smart3772a992009-05-22 14:50:54 -04004814 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
4815 * @phba: pointer to lpfc hba data structure.
4816 *
4817 * This routine is invoked to set up the driver internal resources specific to
4818 * support the SLI-3 HBA device it attached to.
4819 *
4820 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004821 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004822 * other values - error
4823 **/
4824static int
4825lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
4826{
4827 struct lpfc_sli *psli;
James Smart912e3ac2011-05-24 11:42:11 -04004828 int rc;
dea31012005-04-17 16:05:31 -05004829
James Smart3772a992009-05-22 14:50:54 -04004830 /*
4831 * Initialize timers used by driver
4832 */
dea31012005-04-17 16:05:31 -05004833
James Smart3772a992009-05-22 14:50:54 -04004834 /* Heartbeat timer */
4835 init_timer(&phba->hb_tmofunc);
4836 phba->hb_tmofunc.function = lpfc_hb_timeout;
4837 phba->hb_tmofunc.data = (unsigned long)phba;
4838
4839 psli = &phba->sli;
4840 /* MBOX heartbeat timer */
4841 init_timer(&psli->mbox_tmo);
4842 psli->mbox_tmo.function = lpfc_mbox_timeout;
4843 psli->mbox_tmo.data = (unsigned long) phba;
4844 /* FCP polling mode timer */
4845 init_timer(&phba->fcp_poll_timer);
4846 phba->fcp_poll_timer.function = lpfc_poll_timeout;
4847 phba->fcp_poll_timer.data = (unsigned long) phba;
4848 /* Fabric block timer */
4849 init_timer(&phba->fabric_block_timer);
4850 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4851 phba->fabric_block_timer.data = (unsigned long) phba;
4852 /* EA polling mode timer */
4853 init_timer(&phba->eratt_poll);
4854 phba->eratt_poll.function = lpfc_poll_eratt;
4855 phba->eratt_poll.data = (unsigned long) phba;
4856
4857 /* Host attention work mask setup */
4858 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
4859 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
4860
4861 /* Get all the module params for configuring this host */
4862 lpfc_get_cfgparam(phba);
James Smart49198b32010-04-06 15:04:33 -04004863 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
4864 phba->menlo_flag |= HBA_MENLO_SUPPORT;
4865 /* check for menlo minimum sg count */
4866 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
4867 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
4868 }
4869
James Smart2a76a282012-08-03 12:35:54 -04004870 if (!phba->sli.ring)
4871 phba->sli.ring = (struct lpfc_sli_ring *)
4872 kzalloc(LPFC_SLI3_MAX_RING *
4873 sizeof(struct lpfc_sli_ring), GFP_KERNEL);
4874 if (!phba->sli.ring)
4875 return -ENOMEM;
4876
James Smart3772a992009-05-22 14:50:54 -04004877 /*
James Smart96f70772013-04-17 20:16:15 -04004878 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smart3772a992009-05-22 14:50:54 -04004879 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart3772a992009-05-22 14:50:54 -04004880 */
James Smart3772a992009-05-22 14:50:54 -04004881
James Smart96f70772013-04-17 20:16:15 -04004882 /* Initialize the host templates the configured values. */
James Smart3772a992009-05-22 14:50:54 -04004883 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4884 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4885
James Smart96f70772013-04-17 20:16:15 -04004886 /* There are going to be 2 reserved BDEs: 1 FCP cmnd + 1 FCP rsp */
4887 if (phba->cfg_enable_bg) {
4888 /*
4889 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
4890 * the FCP rsp, and a BDE for each. Sice we have no control
4891 * over how many protection data segments the SCSI Layer
4892 * will hand us (ie: there could be one for every block
4893 * in the IO), we just allocate enough BDEs to accomidate
4894 * our max amount and we need to limit lpfc_sg_seg_cnt to
4895 * minimize the risk of running out.
4896 */
4897 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4898 sizeof(struct fcp_rsp) +
4899 (LPFC_MAX_SG_SEG_CNT * sizeof(struct ulp_bde64));
4900
4901 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SEG_CNT_DIF)
4902 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT_DIF;
4903
4904 /* Total BDEs in BPL for scsi_sg_list and scsi_sg_prot_list */
4905 phba->cfg_total_seg_cnt = LPFC_MAX_SG_SEG_CNT;
4906 } else {
4907 /*
4908 * The scsi_buf for a regular I/O will hold the FCP cmnd,
4909 * the FCP rsp, a BDE for each, and a BDE for up to
4910 * cfg_sg_seg_cnt data segments.
4911 */
4912 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4913 sizeof(struct fcp_rsp) +
4914 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
4915
4916 /* Total BDEs in BPL for scsi_sg_list */
4917 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
4918 }
4919
4920 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
4921 "9088 sg_tablesize:%d dmabuf_size:%d total_bde:%d\n",
4922 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
4923 phba->cfg_total_seg_cnt);
4924
James Smart3772a992009-05-22 14:50:54 -04004925 phba->max_vpi = LPFC_MAX_VPI;
4926 /* This will be set to correct value after config_port mbox */
4927 phba->max_vports = 0;
4928
4929 /*
4930 * Initialize the SLI Layer to run with lpfc HBAs.
4931 */
4932 lpfc_sli_setup(phba);
4933 lpfc_sli_queue_setup(phba);
4934
4935 /* Allocate device driver memory */
4936 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
4937 return -ENOMEM;
4938
James Smart912e3ac2011-05-24 11:42:11 -04004939 /*
4940 * Enable sr-iov virtual functions if supported and configured
4941 * through the module parameter.
4942 */
4943 if (phba->cfg_sriov_nr_virtfn > 0) {
4944 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
4945 phba->cfg_sriov_nr_virtfn);
4946 if (rc) {
4947 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4948 "2808 Requested number of SR-IOV "
4949 "virtual functions (%d) is not "
4950 "supported\n",
4951 phba->cfg_sriov_nr_virtfn);
4952 phba->cfg_sriov_nr_virtfn = 0;
4953 }
4954 }
4955
James Smart3772a992009-05-22 14:50:54 -04004956 return 0;
4957}
4958
4959/**
4960 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
4961 * @phba: pointer to lpfc hba data structure.
4962 *
4963 * This routine is invoked to unset the driver internal resources set up
4964 * specific for supporting the SLI-3 HBA device it attached to.
4965 **/
4966static void
4967lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
4968{
4969 /* Free device driver memory allocated */
4970 lpfc_mem_free_all(phba);
4971
4972 return;
4973}
4974
4975/**
James Smartda0436e2009-05-22 14:51:39 -04004976 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
4977 * @phba: pointer to lpfc hba data structure.
4978 *
4979 * This routine is invoked to set up the driver internal resources specific to
4980 * support the SLI-4 HBA device it attached to.
4981 *
4982 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004983 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004984 * other values - error
4985 **/
4986static int
4987lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
4988{
James Smart7bb03bb2013-04-17 20:19:16 -04004989 struct lpfc_vector_map_info *cpup;
James Smartda0436e2009-05-22 14:51:39 -04004990 struct lpfc_sli *psli;
James Smart28baac72010-02-12 14:42:03 -05004991 LPFC_MBOXQ_t *mboxq;
James Smart96f70772013-04-17 20:16:15 -04004992 int rc, i, hbq_count, max_buf_size;
James Smart28baac72010-02-12 14:42:03 -05004993 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
4994 struct lpfc_mqe *mqe;
James Smart09294d42013-04-17 20:16:05 -04004995 int longs;
James Smart1ba981f2014-02-20 09:56:45 -05004996 int fof_vectors = 0;
James Smartda0436e2009-05-22 14:51:39 -04004997
James Smart716d3bc2013-09-06 12:18:28 -04004998 /* Get all the module params for configuring this host */
4999 lpfc_get_cfgparam(phba);
5000
James Smartda0436e2009-05-22 14:51:39 -04005001 /* Before proceed, wait for POST done and device ready */
5002 rc = lpfc_sli4_post_status_check(phba);
5003 if (rc)
5004 return -ENODEV;
5005
5006 /*
5007 * Initialize timers used by driver
5008 */
5009
5010 /* Heartbeat timer */
5011 init_timer(&phba->hb_tmofunc);
5012 phba->hb_tmofunc.function = lpfc_hb_timeout;
5013 phba->hb_tmofunc.data = (unsigned long)phba;
James Smart19ca7602010-11-20 23:11:55 -05005014 init_timer(&phba->rrq_tmr);
5015 phba->rrq_tmr.function = lpfc_rrq_timeout;
5016 phba->rrq_tmr.data = (unsigned long)phba;
James Smartda0436e2009-05-22 14:51:39 -04005017
5018 psli = &phba->sli;
5019 /* MBOX heartbeat timer */
5020 init_timer(&psli->mbox_tmo);
5021 psli->mbox_tmo.function = lpfc_mbox_timeout;
5022 psli->mbox_tmo.data = (unsigned long) phba;
5023 /* Fabric block timer */
5024 init_timer(&phba->fabric_block_timer);
5025 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
5026 phba->fabric_block_timer.data = (unsigned long) phba;
5027 /* EA polling mode timer */
5028 init_timer(&phba->eratt_poll);
5029 phba->eratt_poll.function = lpfc_poll_eratt;
5030 phba->eratt_poll.data = (unsigned long) phba;
James Smartecfd03c2010-02-12 14:41:27 -05005031 /* FCF rediscover timer */
5032 init_timer(&phba->fcf.redisc_wait);
5033 phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
5034 phba->fcf.redisc_wait.data = (unsigned long)phba;
5035
James Smartda0436e2009-05-22 14:51:39 -04005036 /*
James Smart7ad20aa2011-05-24 11:44:28 -04005037 * Control structure for handling external multi-buffer mailbox
5038 * command pass-through.
5039 */
5040 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
5041 sizeof(struct lpfc_mbox_ext_buf_ctx));
5042 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
5043
James Smartda0436e2009-05-22 14:51:39 -04005044 phba->max_vpi = LPFC_MAX_VPI;
James Smart67d12732012-08-03 12:36:13 -04005045
James Smartda0436e2009-05-22 14:51:39 -04005046 /* This will be set to correct value after the read_config mbox */
5047 phba->max_vports = 0;
5048
5049 /* Program the default value of vlan_id and fc_map */
5050 phba->valid_vlan = 0;
5051 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5052 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5053 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5054
5055 /*
James Smart2a76a282012-08-03 12:35:54 -04005056 * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
5057 * we will associate a new ring, for each FCP fastpath EQ/CQ/WQ tuple.
5058 */
5059 if (!phba->sli.ring)
5060 phba->sli.ring = kzalloc(
James Smart67d12732012-08-03 12:36:13 -04005061 (LPFC_SLI3_MAX_RING + phba->cfg_fcp_io_channel) *
James Smart2a76a282012-08-03 12:35:54 -04005062 sizeof(struct lpfc_sli_ring), GFP_KERNEL);
5063 if (!phba->sli.ring)
5064 return -ENOMEM;
James Smart085c6472010-11-20 23:11:37 -05005065
James Smart09294d42013-04-17 20:16:05 -04005066 /*
5067 * It doesn't matter what family our adapter is in, we are
5068 * limited to 2 Pages, 512 SGEs, for our SGL.
5069 * There are going to be 2 reserved SGEs: 1 FCP cmnd + 1 FCP rsp
5070 */
5071 max_buf_size = (2 * SLI4_PAGE_SIZE);
5072 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SGL_SEG_CNT - 2)
5073 phba->cfg_sg_seg_cnt = LPFC_MAX_SGL_SEG_CNT - 2;
James Smart09294d42013-04-17 20:16:05 -04005074
James Smartda0436e2009-05-22 14:51:39 -04005075 /*
James Smart96f70772013-04-17 20:16:15 -04005076 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smartda0436e2009-05-22 14:51:39 -04005077 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart28baac72010-02-12 14:42:03 -05005078 */
James Smart085c6472010-11-20 23:11:37 -05005079
James Smart96f70772013-04-17 20:16:15 -04005080 if (phba->cfg_enable_bg) {
5081 /*
5082 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
5083 * the FCP rsp, and a SGE for each. Sice we have no control
5084 * over how many protection data segments the SCSI Layer
5085 * will hand us (ie: there could be one for every block
5086 * in the IO), we just allocate enough SGEs to accomidate
5087 * our max amount and we need to limit lpfc_sg_seg_cnt to
5088 * minimize the risk of running out.
5089 */
5090 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5091 sizeof(struct fcp_rsp) + max_buf_size;
5092
5093 /* Total SGEs for scsi_sg_list and scsi_sg_prot_list */
5094 phba->cfg_total_seg_cnt = LPFC_MAX_SGL_SEG_CNT;
5095
5096 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SLI4_SEG_CNT_DIF)
5097 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SLI4_SEG_CNT_DIF;
5098 } else {
5099 /*
5100 * The scsi_buf for a regular I/O will hold the FCP cmnd,
5101 * the FCP rsp, a SGE for each, and a SGE for up to
5102 * cfg_sg_seg_cnt data segments.
5103 */
5104 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5105 sizeof(struct fcp_rsp) +
5106 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge));
5107
5108 /* Total SGEs for scsi_sg_list */
5109 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
5110 /*
5111 * NOTE: if (phba->cfg_sg_seg_cnt + 2) <= 256 we only need
5112 * to post 1 page for the SGL.
5113 */
James Smart085c6472010-11-20 23:11:37 -05005114 }
James Smartacd68592012-01-18 16:25:09 -05005115
James Smart96f70772013-04-17 20:16:15 -04005116 /* Initialize the host templates with the updated values. */
5117 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5118 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5119
5120 if (phba->cfg_sg_dma_buf_size <= LPFC_MIN_SG_SLI4_BUF_SZ)
5121 phba->cfg_sg_dma_buf_size = LPFC_MIN_SG_SLI4_BUF_SZ;
5122 else
5123 phba->cfg_sg_dma_buf_size =
5124 SLI4_PAGE_ALIGN(phba->cfg_sg_dma_buf_size);
5125
5126 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
5127 "9087 sg_tablesize:%d dmabuf_size:%d total_sge:%d\n",
5128 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
5129 phba->cfg_total_seg_cnt);
James Smartda0436e2009-05-22 14:51:39 -04005130
5131 /* Initialize buffer queue management fields */
5132 hbq_count = lpfc_sli_hbq_count();
5133 for (i = 0; i < hbq_count; ++i)
5134 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
5135 INIT_LIST_HEAD(&phba->rb_pend_list);
5136 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
5137 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
5138
5139 /*
5140 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
5141 */
5142 /* Initialize the Abort scsi buffer list used by driver */
5143 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
5144 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
5145 /* This abort list used by worker thread */
5146 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
5147
5148 /*
James Smart6d368e52011-05-24 11:44:12 -04005149 * Initialize driver internal slow-path work queues
James Smartda0436e2009-05-22 14:51:39 -04005150 */
5151
5152 /* Driver internel slow-path CQ Event pool */
5153 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
5154 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04005155 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04005156 /* Asynchronous event CQ Event work queue list */
5157 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
5158 /* Fast-path XRI aborted CQ Event work queue list */
5159 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
5160 /* Slow-path XRI aborted CQ Event work queue list */
5161 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
5162 /* Receive queue CQ Event work queue list */
5163 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
5164
James Smart6d368e52011-05-24 11:44:12 -04005165 /* Initialize extent block lists. */
5166 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
5167 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
5168 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
5169 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
5170
James Smartda0436e2009-05-22 14:51:39 -04005171 /* Initialize the driver internal SLI layer lists. */
5172 lpfc_sli_setup(phba);
5173 lpfc_sli_queue_setup(phba);
5174
5175 /* Allocate device driver memory */
5176 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
5177 if (rc)
5178 return -ENOMEM;
5179
James Smart2fcee4b2010-12-15 17:57:46 -05005180 /* IF Type 2 ports get initialized now. */
5181 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
5182 LPFC_SLI_INTF_IF_TYPE_2) {
5183 rc = lpfc_pci_function_reset(phba);
5184 if (unlikely(rc))
5185 return -ENODEV;
5186 }
5187
James Smartda0436e2009-05-22 14:51:39 -04005188 /* Create the bootstrap mailbox command */
5189 rc = lpfc_create_bootstrap_mbox(phba);
5190 if (unlikely(rc))
5191 goto out_free_mem;
5192
5193 /* Set up the host's endian order with the device. */
5194 rc = lpfc_setup_endian_order(phba);
5195 if (unlikely(rc))
5196 goto out_free_bsmbx;
5197
5198 /* Set up the hba's configuration parameters. */
5199 rc = lpfc_sli4_read_config(phba);
5200 if (unlikely(rc))
5201 goto out_free_bsmbx;
James Smartcff261f2013-12-17 20:29:47 -05005202 rc = lpfc_mem_alloc_active_rrq_pool_s4(phba);
5203 if (unlikely(rc))
5204 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04005205
James Smart2fcee4b2010-12-15 17:57:46 -05005206 /* IF Type 0 ports get initialized now. */
5207 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
5208 LPFC_SLI_INTF_IF_TYPE_0) {
5209 rc = lpfc_pci_function_reset(phba);
5210 if (unlikely(rc))
5211 goto out_free_bsmbx;
5212 }
James Smartda0436e2009-05-22 14:51:39 -04005213
James Smartcb5172e2010-03-15 11:25:07 -04005214 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
5215 GFP_KERNEL);
5216 if (!mboxq) {
5217 rc = -ENOMEM;
5218 goto out_free_bsmbx;
5219 }
5220
James Smartfedd3b72011-02-16 12:39:24 -05005221 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
James Smartcb5172e2010-03-15 11:25:07 -04005222 lpfc_supported_pages(mboxq);
5223 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smartfedd3b72011-02-16 12:39:24 -05005224 if (!rc) {
5225 mqe = &mboxq->u.mqe;
5226 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
5227 LPFC_MAX_SUPPORTED_PAGES);
5228 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
5229 switch (pn_page[i]) {
5230 case LPFC_SLI4_PARAMETERS:
5231 phba->sli4_hba.pc_sli4_params.supported = 1;
5232 break;
5233 default:
5234 break;
5235 }
5236 }
5237 /* Read the port's SLI4 Parameters capabilities if supported. */
5238 if (phba->sli4_hba.pc_sli4_params.supported)
5239 rc = lpfc_pc_sli4_params_get(phba, mboxq);
5240 if (rc) {
5241 mempool_free(mboxq, phba->mbox_mem_pool);
5242 rc = -EIO;
5243 goto out_free_bsmbx;
James Smartcb5172e2010-03-15 11:25:07 -04005244 }
5245 }
James Smartfedd3b72011-02-16 12:39:24 -05005246 /*
5247 * Get sli4 parameters that override parameters from Port capabilities.
James Smart6d368e52011-05-24 11:44:12 -04005248 * If this call fails, it isn't critical unless the SLI4 parameters come
5249 * back in conflict.
James Smartfedd3b72011-02-16 12:39:24 -05005250 */
James Smart6d368e52011-05-24 11:44:12 -04005251 rc = lpfc_get_sli4_parameters(phba, mboxq);
5252 if (rc) {
5253 if (phba->sli4_hba.extents_in_use &&
5254 phba->sli4_hba.rpi_hdrs_in_use) {
5255 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5256 "2999 Unsupported SLI4 Parameters "
5257 "Extents and RPI headers enabled.\n");
5258 goto out_free_bsmbx;
5259 }
5260 }
James Smartcb5172e2010-03-15 11:25:07 -04005261 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1ba981f2014-02-20 09:56:45 -05005262
5263 /* Verify OAS is supported */
5264 lpfc_sli4_oas_verify(phba);
5265 if (phba->cfg_fof)
5266 fof_vectors = 1;
5267
James Smart5350d872011-10-10 21:33:49 -04005268 /* Verify all the SLI4 queues */
5269 rc = lpfc_sli4_queue_verify(phba);
James Smartda0436e2009-05-22 14:51:39 -04005270 if (rc)
5271 goto out_free_bsmbx;
5272
5273 /* Create driver internal CQE event pool */
5274 rc = lpfc_sli4_cq_event_pool_create(phba);
5275 if (rc)
James Smart5350d872011-10-10 21:33:49 -04005276 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04005277
James Smart8a9d2e82012-05-09 21:16:12 -04005278 /* Initialize sgl lists per host */
5279 lpfc_init_sgl_list(phba);
5280
5281 /* Allocate and initialize active sgl array */
James Smartda0436e2009-05-22 14:51:39 -04005282 rc = lpfc_init_active_sgl_array(phba);
5283 if (rc) {
5284 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5285 "1430 Failed to initialize sgl list.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04005286 goto out_destroy_cq_event_pool;
James Smartda0436e2009-05-22 14:51:39 -04005287 }
James Smartda0436e2009-05-22 14:51:39 -04005288 rc = lpfc_sli4_init_rpi_hdrs(phba);
5289 if (rc) {
5290 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5291 "1432 Failed to initialize rpi headers.\n");
5292 goto out_free_active_sgl;
5293 }
5294
James Smarta93ff372010-10-22 11:06:08 -04005295 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
James Smart0c9ab6f2010-02-26 14:15:57 -05005296 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
5297 phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
5298 GFP_KERNEL);
5299 if (!phba->fcf.fcf_rr_bmask) {
5300 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5301 "2759 Failed allocate memory for FCF round "
5302 "robin failover bmask\n");
James Smart05580562011-05-24 11:40:48 -04005303 rc = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -05005304 goto out_remove_rpi_hdrs;
5305 }
5306
James Smart67d12732012-08-03 12:36:13 -04005307 phba->sli4_hba.fcp_eq_hdl =
5308 kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
James Smart1ba981f2014-02-20 09:56:45 -05005309 (fof_vectors + phba->cfg_fcp_io_channel)),
5310 GFP_KERNEL);
James Smart67d12732012-08-03 12:36:13 -04005311 if (!phba->sli4_hba.fcp_eq_hdl) {
5312 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5313 "2572 Failed allocate memory for "
5314 "fast-path per-EQ handle array\n");
5315 rc = -ENOMEM;
5316 goto out_free_fcf_rr_bmask;
James Smartda0436e2009-05-22 14:51:39 -04005317 }
5318
5319 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
James Smart1ba981f2014-02-20 09:56:45 -05005320 (fof_vectors +
5321 phba->cfg_fcp_io_channel)), GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005322 if (!phba->sli4_hba.msix_entries) {
5323 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5324 "2573 Failed allocate memory for msi-x "
5325 "interrupt vector entries\n");
James Smart05580562011-05-24 11:40:48 -04005326 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04005327 goto out_free_fcp_eq_hdl;
5328 }
5329
James Smart7bb03bb2013-04-17 20:19:16 -04005330 phba->sli4_hba.cpu_map = kzalloc((sizeof(struct lpfc_vector_map_info) *
5331 phba->sli4_hba.num_present_cpu),
5332 GFP_KERNEL);
5333 if (!phba->sli4_hba.cpu_map) {
5334 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5335 "3327 Failed allocate memory for msi-x "
5336 "interrupt vector mapping\n");
5337 rc = -ENOMEM;
5338 goto out_free_msix;
5339 }
James Smartb246de12013-05-31 17:03:07 -04005340 if (lpfc_used_cpu == NULL) {
5341 lpfc_used_cpu = kzalloc((sizeof(uint16_t) * lpfc_present_cpu),
5342 GFP_KERNEL);
5343 if (!lpfc_used_cpu) {
5344 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5345 "3335 Failed allocate memory for msi-x "
5346 "interrupt vector mapping\n");
5347 kfree(phba->sli4_hba.cpu_map);
5348 rc = -ENOMEM;
5349 goto out_free_msix;
5350 }
5351 for (i = 0; i < lpfc_present_cpu; i++)
5352 lpfc_used_cpu[i] = LPFC_VECTOR_MAP_EMPTY;
5353 }
5354
James Smart7bb03bb2013-04-17 20:19:16 -04005355 /* Initialize io channels for round robin */
5356 cpup = phba->sli4_hba.cpu_map;
5357 rc = 0;
5358 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
5359 cpup->channel_id = rc;
5360 rc++;
5361 if (rc >= phba->cfg_fcp_io_channel)
5362 rc = 0;
5363 }
5364
James Smart912e3ac2011-05-24 11:42:11 -04005365 /*
5366 * Enable sr-iov virtual functions if supported and configured
5367 * through the module parameter.
5368 */
5369 if (phba->cfg_sriov_nr_virtfn > 0) {
5370 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
5371 phba->cfg_sriov_nr_virtfn);
5372 if (rc) {
5373 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5374 "3020 Requested number of SR-IOV "
5375 "virtual functions (%d) is not "
5376 "supported\n",
5377 phba->cfg_sriov_nr_virtfn);
5378 phba->cfg_sriov_nr_virtfn = 0;
5379 }
5380 }
5381
James Smart5248a742011-07-22 18:37:06 -04005382 return 0;
James Smartda0436e2009-05-22 14:51:39 -04005383
James Smart7bb03bb2013-04-17 20:19:16 -04005384out_free_msix:
5385 kfree(phba->sli4_hba.msix_entries);
James Smartda0436e2009-05-22 14:51:39 -04005386out_free_fcp_eq_hdl:
5387 kfree(phba->sli4_hba.fcp_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05005388out_free_fcf_rr_bmask:
5389 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04005390out_remove_rpi_hdrs:
5391 lpfc_sli4_remove_rpi_hdrs(phba);
5392out_free_active_sgl:
5393 lpfc_free_active_sgl(phba);
James Smartda0436e2009-05-22 14:51:39 -04005394out_destroy_cq_event_pool:
5395 lpfc_sli4_cq_event_pool_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04005396out_free_bsmbx:
5397 lpfc_destroy_bootstrap_mbox(phba);
5398out_free_mem:
5399 lpfc_mem_free(phba);
5400 return rc;
5401}
5402
5403/**
5404 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
5405 * @phba: pointer to lpfc hba data structure.
5406 *
5407 * This routine is invoked to unset the driver internal resources set up
5408 * specific for supporting the SLI-4 HBA device it attached to.
5409 **/
5410static void
5411lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
5412{
5413 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
5414
James Smart7bb03bb2013-04-17 20:19:16 -04005415 /* Free memory allocated for msi-x interrupt vector to CPU mapping */
5416 kfree(phba->sli4_hba.cpu_map);
5417 phba->sli4_hba.num_present_cpu = 0;
5418 phba->sli4_hba.num_online_cpu = 0;
James Smart76fd07a2014-02-20 09:57:18 -05005419 phba->sli4_hba.curr_disp_cpu = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04005420
James Smartda0436e2009-05-22 14:51:39 -04005421 /* Free memory allocated for msi-x interrupt vector entries */
5422 kfree(phba->sli4_hba.msix_entries);
5423
5424 /* Free memory allocated for fast-path work queue handles */
5425 kfree(phba->sli4_hba.fcp_eq_hdl);
5426
5427 /* Free the allocated rpi headers. */
5428 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04005429 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04005430
James Smart0c9ab6f2010-02-26 14:15:57 -05005431 /* Free eligible FCF index bmask */
5432 kfree(phba->fcf.fcf_rr_bmask);
5433
James Smartda0436e2009-05-22 14:51:39 -04005434 /* Free the ELS sgl list */
5435 lpfc_free_active_sgl(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04005436 lpfc_free_els_sgl_list(phba);
James Smartda0436e2009-05-22 14:51:39 -04005437
James Smartda0436e2009-05-22 14:51:39 -04005438 /* Free the completion queue EQ event pool */
5439 lpfc_sli4_cq_event_release_all(phba);
5440 lpfc_sli4_cq_event_pool_destroy(phba);
5441
James Smart6d368e52011-05-24 11:44:12 -04005442 /* Release resource identifiers. */
5443 lpfc_sli4_dealloc_resource_identifiers(phba);
5444
James Smartda0436e2009-05-22 14:51:39 -04005445 /* Free the bsmbx region. */
5446 lpfc_destroy_bootstrap_mbox(phba);
5447
5448 /* Free the SLI Layer memory with SLI4 HBAs */
5449 lpfc_mem_free_all(phba);
5450
5451 /* Free the current connect table */
5452 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04005453 &phba->fcf_conn_rec_list, list) {
5454 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04005455 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04005456 }
James Smartda0436e2009-05-22 14:51:39 -04005457
5458 return;
5459}
5460
5461/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005462 * lpfc_init_api_table_setup - Set up init api function jump table
James Smart3772a992009-05-22 14:50:54 -04005463 * @phba: The hba struct for which this call is being executed.
5464 * @dev_grp: The HBA PCI-Device group number.
5465 *
5466 * This routine sets up the device INIT interface API function jump table
5467 * in @phba struct.
5468 *
5469 * Returns: 0 - success, -ENODEV - failure.
5470 **/
5471int
5472lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5473{
James Smart84d1b002010-02-12 14:42:33 -05005474 phba->lpfc_hba_init_link = lpfc_hba_init_link;
5475 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart7f860592011-03-11 16:05:52 -05005476 phba->lpfc_selective_reset = lpfc_selective_reset;
James Smart3772a992009-05-22 14:50:54 -04005477 switch (dev_grp) {
5478 case LPFC_PCI_DEV_LP:
5479 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
5480 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
5481 phba->lpfc_stop_port = lpfc_stop_port_s3;
5482 break;
James Smartda0436e2009-05-22 14:51:39 -04005483 case LPFC_PCI_DEV_OC:
5484 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
5485 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
5486 phba->lpfc_stop_port = lpfc_stop_port_s4;
5487 break;
James Smart3772a992009-05-22 14:50:54 -04005488 default:
5489 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5490 "1431 Invalid HBA PCI-device group: 0x%x\n",
5491 dev_grp);
5492 return -ENODEV;
5493 break;
5494 }
5495 return 0;
5496}
5497
5498/**
5499 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
5500 * @phba: pointer to lpfc hba data structure.
5501 *
5502 * This routine is invoked to set up the driver internal resources before the
5503 * device specific resource setup to support the HBA device it attached to.
5504 *
5505 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005506 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005507 * other values - error
5508 **/
5509static int
5510lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
5511{
5512 /*
5513 * Driver resources common to all SLI revisions
5514 */
James Smartea2151b2008-09-07 11:52:10 -04005515 atomic_set(&phba->fast_event_count, 0);
James Smart2e0fef82007-06-17 19:56:36 -05005516 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005517
James Smarte47c9092008-02-08 18:49:26 -05005518 /* Initialize ndlp management spinlock */
5519 spin_lock_init(&phba->ndlp_lock);
5520
James Smart2e0fef82007-06-17 19:56:36 -05005521 INIT_LIST_HEAD(&phba->port_list);
James Smart3772a992009-05-22 14:50:54 -04005522 INIT_LIST_HEAD(&phba->work_list);
James Smart84774a42008-08-24 21:50:06 -04005523 init_waitqueue_head(&phba->wait_4_mlo_m_q);
dea31012005-04-17 16:05:31 -05005524
James Smart3772a992009-05-22 14:50:54 -04005525 /* Initialize the wait queue head for the kernel thread */
5526 init_waitqueue_head(&phba->work_waitq);
James Smart858c9f62007-06-17 19:56:39 -05005527
James Smart3772a992009-05-22 14:50:54 -04005528 /* Initialize the scsi buffer list used by driver for scsi IO */
James Smarta40fc5f2013-04-17 20:17:40 -04005529 spin_lock_init(&phba->scsi_buf_list_get_lock);
5530 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_get);
5531 spin_lock_init(&phba->scsi_buf_list_put_lock);
5532 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
dea31012005-04-17 16:05:31 -05005533
James Smart3772a992009-05-22 14:50:54 -04005534 /* Initialize the fabric iocb list */
5535 INIT_LIST_HEAD(&phba->fabric_iocb_list);
dea31012005-04-17 16:05:31 -05005536
James Smart3772a992009-05-22 14:50:54 -04005537 /* Initialize list to save ELS buffers */
5538 INIT_LIST_HEAD(&phba->elsbuf);
dea31012005-04-17 16:05:31 -05005539
James Smart3772a992009-05-22 14:50:54 -04005540 /* Initialize FCF connection rec list */
5541 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
dea31012005-04-17 16:05:31 -05005542
James Smart1ba981f2014-02-20 09:56:45 -05005543 /* Initialize OAS configuration list */
5544 spin_lock_init(&phba->devicelock);
5545 INIT_LIST_HEAD(&phba->luns);
5546
James Smart3772a992009-05-22 14:50:54 -04005547 return 0;
5548}
dea31012005-04-17 16:05:31 -05005549
James Smart3772a992009-05-22 14:50:54 -04005550/**
5551 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
5552 * @phba: pointer to lpfc hba data structure.
5553 *
5554 * This routine is invoked to set up the driver internal resources after the
5555 * device specific resource setup to support the HBA device it attached to.
5556 *
5557 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005558 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005559 * other values - error
5560 **/
5561static int
5562lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
5563{
5564 int error;
5565
5566 /* Startup the kernel thread for this host adapter. */
5567 phba->worker_thread = kthread_run(lpfc_do_work, phba,
5568 "lpfc_worker_%d", phba->brd_no);
5569 if (IS_ERR(phba->worker_thread)) {
5570 error = PTR_ERR(phba->worker_thread);
5571 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005572 }
5573
James Smart3772a992009-05-22 14:50:54 -04005574 return 0;
5575}
5576
5577/**
5578 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
5579 * @phba: pointer to lpfc hba data structure.
5580 *
5581 * This routine is invoked to unset the driver internal resources set up after
5582 * the device specific resource setup for supporting the HBA device it
5583 * attached to.
5584 **/
5585static void
5586lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
5587{
5588 /* Stop kernel worker thread */
5589 kthread_stop(phba->worker_thread);
5590}
5591
5592/**
5593 * lpfc_free_iocb_list - Free iocb list.
5594 * @phba: pointer to lpfc hba data structure.
5595 *
5596 * This routine is invoked to free the driver's IOCB list and memory.
5597 **/
5598static void
5599lpfc_free_iocb_list(struct lpfc_hba *phba)
5600{
5601 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
5602
5603 spin_lock_irq(&phba->hbalock);
5604 list_for_each_entry_safe(iocbq_entry, iocbq_next,
5605 &phba->lpfc_iocb_list, list) {
5606 list_del(&iocbq_entry->list);
5607 kfree(iocbq_entry);
5608 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005609 }
James Smart3772a992009-05-22 14:50:54 -04005610 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005611
James Smart3772a992009-05-22 14:50:54 -04005612 return;
5613}
dea31012005-04-17 16:05:31 -05005614
James Smart3772a992009-05-22 14:50:54 -04005615/**
5616 * lpfc_init_iocb_list - Allocate and initialize iocb list.
5617 * @phba: pointer to lpfc hba data structure.
5618 *
5619 * This routine is invoked to allocate and initizlize the driver's IOCB
5620 * list and set up the IOCB tag array accordingly.
5621 *
5622 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005623 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005624 * other values - error
5625 **/
5626static int
5627lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
5628{
5629 struct lpfc_iocbq *iocbq_entry = NULL;
5630 uint16_t iotag;
5631 int i;
dea31012005-04-17 16:05:31 -05005632
5633 /* Initialize and populate the iocb list per host. */
5634 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04005635 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07005636 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05005637 if (iocbq_entry == NULL) {
5638 printk(KERN_ERR "%s: only allocated %d iocbs of "
5639 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07005640 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05005641 goto out_free_iocbq;
5642 }
5643
James Bottomley604a3e32005-10-29 10:28:33 -05005644 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
5645 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04005646 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05005647 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04005648 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05005649 goto out_free_iocbq;
5650 }
James Smart6d368e52011-05-24 11:44:12 -04005651 iocbq_entry->sli4_lxritag = NO_XRI;
James Smart3772a992009-05-22 14:50:54 -04005652 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05005653
5654 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005655 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
5656 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05005657 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005658 }
5659
James Smart3772a992009-05-22 14:50:54 -04005660 return 0;
5661
5662out_free_iocbq:
5663 lpfc_free_iocb_list(phba);
5664
5665 return -ENOMEM;
5666}
5667
5668/**
James Smart8a9d2e82012-05-09 21:16:12 -04005669 * lpfc_free_sgl_list - Free a given sgl list.
James Smartda0436e2009-05-22 14:51:39 -04005670 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -04005671 * @sglq_list: pointer to the head of sgl list.
James Smartda0436e2009-05-22 14:51:39 -04005672 *
James Smart8a9d2e82012-05-09 21:16:12 -04005673 * This routine is invoked to free a give sgl list and memory.
James Smartda0436e2009-05-22 14:51:39 -04005674 **/
James Smart8a9d2e82012-05-09 21:16:12 -04005675void
5676lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
James Smartda0436e2009-05-22 14:51:39 -04005677{
5678 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart8a9d2e82012-05-09 21:16:12 -04005679
5680 list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
5681 list_del(&sglq_entry->list);
5682 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
5683 kfree(sglq_entry);
5684 }
5685}
5686
5687/**
5688 * lpfc_free_els_sgl_list - Free els sgl list.
5689 * @phba: pointer to lpfc hba data structure.
5690 *
5691 * This routine is invoked to free the driver's els sgl list and memory.
5692 **/
5693static void
5694lpfc_free_els_sgl_list(struct lpfc_hba *phba)
5695{
James Smartda0436e2009-05-22 14:51:39 -04005696 LIST_HEAD(sglq_list);
James Smartdafe8ce2014-09-03 12:56:40 -04005697 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smartda0436e2009-05-22 14:51:39 -04005698
James Smart8a9d2e82012-05-09 21:16:12 -04005699 /* Retrieve all els sgls from driver list */
James Smartda0436e2009-05-22 14:51:39 -04005700 spin_lock_irq(&phba->hbalock);
James Smartdafe8ce2014-09-03 12:56:40 -04005701 spin_lock(&pring->ring_lock);
James Smartda0436e2009-05-22 14:51:39 -04005702 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
James Smartdafe8ce2014-09-03 12:56:40 -04005703 spin_unlock(&pring->ring_lock);
James Smartda0436e2009-05-22 14:51:39 -04005704 spin_unlock_irq(&phba->hbalock);
5705
James Smart8a9d2e82012-05-09 21:16:12 -04005706 /* Now free the sgl list */
5707 lpfc_free_sgl_list(phba, &sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04005708}
5709
5710/**
5711 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
5712 * @phba: pointer to lpfc hba data structure.
5713 *
5714 * This routine is invoked to allocate the driver's active sgl memory.
5715 * This array will hold the sglq_entry's for active IOs.
5716 **/
5717static int
5718lpfc_init_active_sgl_array(struct lpfc_hba *phba)
5719{
5720 int size;
5721 size = sizeof(struct lpfc_sglq *);
5722 size *= phba->sli4_hba.max_cfg_param.max_xri;
5723
5724 phba->sli4_hba.lpfc_sglq_active_list =
5725 kzalloc(size, GFP_KERNEL);
5726 if (!phba->sli4_hba.lpfc_sglq_active_list)
5727 return -ENOMEM;
5728 return 0;
5729}
5730
5731/**
5732 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
5733 * @phba: pointer to lpfc hba data structure.
5734 *
5735 * This routine is invoked to walk through the array of active sglq entries
5736 * and free all of the resources.
5737 * This is just a place holder for now.
5738 **/
5739static void
5740lpfc_free_active_sgl(struct lpfc_hba *phba)
5741{
5742 kfree(phba->sli4_hba.lpfc_sglq_active_list);
5743}
5744
5745/**
5746 * lpfc_init_sgl_list - Allocate and initialize sgl list.
5747 * @phba: pointer to lpfc hba data structure.
5748 *
5749 * This routine is invoked to allocate and initizlize the driver's sgl
5750 * list and set up the sgl xritag tag array accordingly.
5751 *
James Smartda0436e2009-05-22 14:51:39 -04005752 **/
James Smart8a9d2e82012-05-09 21:16:12 -04005753static void
James Smartda0436e2009-05-22 14:51:39 -04005754lpfc_init_sgl_list(struct lpfc_hba *phba)
5755{
James Smartda0436e2009-05-22 14:51:39 -04005756 /* Initialize and populate the sglq list per host/VF. */
5757 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
5758 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
5759
James Smart8a9d2e82012-05-09 21:16:12 -04005760 /* els xri-sgl book keeping */
5761 phba->sli4_hba.els_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04005762
James Smart8a9d2e82012-05-09 21:16:12 -04005763 /* scsi xri-buffer book keeping */
James Smartda0436e2009-05-22 14:51:39 -04005764 phba->sli4_hba.scsi_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04005765}
5766
5767/**
5768 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
5769 * @phba: pointer to lpfc hba data structure.
5770 *
5771 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -04005772 * port for those SLI4 ports that do not support extents. This routine
James Smartda0436e2009-05-22 14:51:39 -04005773 * posts a PAGE_SIZE memory region to the port to hold up to
James Smart88a2cfb2011-07-22 18:36:33 -04005774 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
5775 * and should be called only when interrupts are disabled.
James Smartda0436e2009-05-22 14:51:39 -04005776 *
5777 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005778 * 0 - successful
James Smart88a2cfb2011-07-22 18:36:33 -04005779 * -ERROR - otherwise.
James Smartda0436e2009-05-22 14:51:39 -04005780 **/
5781int
5782lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
5783{
5784 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04005785 struct lpfc_rpi_hdr *rpi_hdr;
5786
5787 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
James Smartff78d8f2011-12-13 13:21:35 -05005788 if (!phba->sli4_hba.rpi_hdrs_in_use)
James Smart6d368e52011-05-24 11:44:12 -04005789 return rc;
James Smart6d368e52011-05-24 11:44:12 -04005790 if (phba->sli4_hba.extents_in_use)
5791 return -EIO;
James Smartda0436e2009-05-22 14:51:39 -04005792
5793 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
5794 if (!rpi_hdr) {
5795 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5796 "0391 Error during rpi post operation\n");
5797 lpfc_sli4_remove_rpis(phba);
5798 rc = -ENODEV;
5799 }
5800
5801 return rc;
5802}
5803
5804/**
5805 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
5806 * @phba: pointer to lpfc hba data structure.
5807 *
5808 * This routine is invoked to allocate a single 4KB memory region to
5809 * support rpis and stores them in the phba. This single region
5810 * provides support for up to 64 rpis. The region is used globally
5811 * by the device.
5812 *
5813 * Returns:
5814 * A valid rpi hdr on success.
5815 * A NULL pointer on any failure.
5816 **/
5817struct lpfc_rpi_hdr *
5818lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
5819{
5820 uint16_t rpi_limit, curr_rpi_range;
5821 struct lpfc_dmabuf *dmabuf;
5822 struct lpfc_rpi_hdr *rpi_hdr;
James Smart9589b0622011-04-16 11:03:17 -04005823 uint32_t rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04005824
James Smart6d368e52011-05-24 11:44:12 -04005825 /*
5826 * If the SLI4 port supports extents, posting the rpi header isn't
5827 * required. Set the expected maximum count and let the actual value
5828 * get set when extents are fully allocated.
5829 */
5830 if (!phba->sli4_hba.rpi_hdrs_in_use)
5831 return NULL;
5832 if (phba->sli4_hba.extents_in_use)
5833 return NULL;
5834
5835 /* The limit on the logical index is just the max_rpi count. */
James Smartda0436e2009-05-22 14:51:39 -04005836 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
James Smart6d368e52011-05-24 11:44:12 -04005837 phba->sli4_hba.max_cfg_param.max_rpi - 1;
James Smartda0436e2009-05-22 14:51:39 -04005838
5839 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04005840 /*
5841 * Establish the starting RPI in this header block. The starting
5842 * rpi is normalized to a zero base because the physical rpi is
5843 * port based.
5844 */
James Smart97f2ecf2012-03-01 22:35:23 -05005845 curr_rpi_range = phba->sli4_hba.next_rpi;
James Smartda0436e2009-05-22 14:51:39 -04005846 spin_unlock_irq(&phba->hbalock);
5847
5848 /*
5849 * The port has a limited number of rpis. The increment here
5850 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
5851 * and to allow the full max_rpi range per port.
5852 */
5853 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
James Smart9589b0622011-04-16 11:03:17 -04005854 rpi_count = rpi_limit - curr_rpi_range;
5855 else
5856 rpi_count = LPFC_RPI_HDR_COUNT;
James Smartda0436e2009-05-22 14:51:39 -04005857
James Smart6d368e52011-05-24 11:44:12 -04005858 if (!rpi_count)
5859 return NULL;
James Smartda0436e2009-05-22 14:51:39 -04005860 /*
5861 * First allocate the protocol header region for the port. The
5862 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
5863 */
5864 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5865 if (!dmabuf)
5866 return NULL;
5867
Joe Perches1aee3832014-09-03 12:56:12 -04005868 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
5869 LPFC_HDR_TEMPLATE_SIZE,
5870 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005871 if (!dmabuf->virt) {
5872 rpi_hdr = NULL;
5873 goto err_free_dmabuf;
5874 }
5875
James Smartda0436e2009-05-22 14:51:39 -04005876 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
5877 rpi_hdr = NULL;
5878 goto err_free_coherent;
5879 }
5880
5881 /* Save the rpi header data for cleanup later. */
5882 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
5883 if (!rpi_hdr)
5884 goto err_free_coherent;
5885
5886 rpi_hdr->dmabuf = dmabuf;
5887 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
5888 rpi_hdr->page_count = 1;
5889 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04005890
5891 /* The rpi_hdr stores the logical index only. */
5892 rpi_hdr->start_rpi = curr_rpi_range;
James Smartda0436e2009-05-22 14:51:39 -04005893 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
5894
5895 /*
James Smart6d368e52011-05-24 11:44:12 -04005896 * The next_rpi stores the next logical module-64 rpi value used
5897 * to post physical rpis in subsequent rpi postings.
James Smartda0436e2009-05-22 14:51:39 -04005898 */
James Smart9589b0622011-04-16 11:03:17 -04005899 phba->sli4_hba.next_rpi += rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04005900 spin_unlock_irq(&phba->hbalock);
5901 return rpi_hdr;
5902
5903 err_free_coherent:
5904 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
5905 dmabuf->virt, dmabuf->phys);
5906 err_free_dmabuf:
5907 kfree(dmabuf);
5908 return NULL;
5909}
5910
5911/**
5912 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
5913 * @phba: pointer to lpfc hba data structure.
5914 *
5915 * This routine is invoked to remove all memory resources allocated
James Smart6d368e52011-05-24 11:44:12 -04005916 * to support rpis for SLI4 ports not supporting extents. This routine
5917 * presumes the caller has released all rpis consumed by fabric or port
5918 * logins and is prepared to have the header pages removed.
James Smartda0436e2009-05-22 14:51:39 -04005919 **/
5920void
5921lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
5922{
5923 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
5924
James Smart6d368e52011-05-24 11:44:12 -04005925 if (!phba->sli4_hba.rpi_hdrs_in_use)
5926 goto exit;
5927
James Smartda0436e2009-05-22 14:51:39 -04005928 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
5929 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
5930 list_del(&rpi_hdr->list);
5931 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
5932 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
5933 kfree(rpi_hdr->dmabuf);
5934 kfree(rpi_hdr);
5935 }
James Smart6d368e52011-05-24 11:44:12 -04005936 exit:
5937 /* There are no rpis available to the port now. */
5938 phba->sli4_hba.next_rpi = 0;
James Smartda0436e2009-05-22 14:51:39 -04005939}
5940
5941/**
James Smart3772a992009-05-22 14:50:54 -04005942 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
5943 * @pdev: pointer to pci device data structure.
5944 *
5945 * This routine is invoked to allocate the driver hba data structure for an
5946 * HBA device. If the allocation is successful, the phba reference to the
5947 * PCI device data structure is set.
5948 *
5949 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005950 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04005951 * NULL - error
5952 **/
5953static struct lpfc_hba *
5954lpfc_hba_alloc(struct pci_dev *pdev)
5955{
5956 struct lpfc_hba *phba;
5957
5958 /* Allocate memory for HBA structure */
5959 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
5960 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02005961 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04005962 return NULL;
5963 }
5964
5965 /* Set reference to PCI device in HBA structure */
5966 phba->pcidev = pdev;
5967
5968 /* Assign an unused board number */
5969 phba->brd_no = lpfc_get_instance();
5970 if (phba->brd_no < 0) {
5971 kfree(phba);
5972 return NULL;
5973 }
5974
James Smart4fede782010-01-26 23:08:55 -05005975 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04005976 INIT_LIST_HEAD(&phba->ct_ev_waiters);
5977
James Smart3772a992009-05-22 14:50:54 -04005978 return phba;
5979}
5980
5981/**
5982 * lpfc_hba_free - Free driver hba data structure with a device.
5983 * @phba: pointer to lpfc hba data structure.
5984 *
5985 * This routine is invoked to free the driver hba data structure with an
5986 * HBA device.
5987 **/
5988static void
5989lpfc_hba_free(struct lpfc_hba *phba)
5990{
5991 /* Release the driver assigned board number */
5992 idr_remove(&lpfc_hba_index, phba->brd_no);
5993
James Smart2a76a282012-08-03 12:35:54 -04005994 /* Free memory allocated with sli rings */
5995 kfree(phba->sli.ring);
5996 phba->sli.ring = NULL;
5997
James Smart3772a992009-05-22 14:50:54 -04005998 kfree(phba);
5999 return;
6000}
6001
6002/**
6003 * lpfc_create_shost - Create hba physical port with associated scsi host.
6004 * @phba: pointer to lpfc hba data structure.
6005 *
6006 * This routine is invoked to create HBA physical port and associate a SCSI
6007 * host with it.
6008 *
6009 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006010 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006011 * other values - error
6012 **/
6013static int
6014lpfc_create_shost(struct lpfc_hba *phba)
6015{
6016 struct lpfc_vport *vport;
6017 struct Scsi_Host *shost;
6018
6019 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05006020 phba->fc_edtov = FF_DEF_EDTOV;
6021 phba->fc_ratov = FF_DEF_RATOV;
6022 phba->fc_altov = FF_DEF_ALTOV;
6023 phba->fc_arbtov = FF_DEF_ARBTOV;
6024
James Smartd7c47992010-06-08 18:31:54 -04006025 atomic_set(&phba->sdev_cnt, 0);
James Smart3de2a652007-08-02 11:09:59 -04006026 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05006027 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04006028 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05006029
6030 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05006031 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05006032 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04006033 /* Put reference to SCSI host to driver's device private data */
6034 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05006035
James Smart3772a992009-05-22 14:50:54 -04006036 return 0;
6037}
dea31012005-04-17 16:05:31 -05006038
James Smart3772a992009-05-22 14:50:54 -04006039/**
6040 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
6041 * @phba: pointer to lpfc hba data structure.
6042 *
6043 * This routine is invoked to destroy HBA physical port and the associated
6044 * SCSI host.
6045 **/
6046static void
6047lpfc_destroy_shost(struct lpfc_hba *phba)
6048{
6049 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04006050
James Smart3772a992009-05-22 14:50:54 -04006051 /* Destroy physical port that associated with the SCSI host */
6052 destroy_port(vport);
6053
6054 return;
6055}
6056
6057/**
6058 * lpfc_setup_bg - Setup Block guard structures and debug areas.
6059 * @phba: pointer to lpfc hba data structure.
6060 * @shost: the shost to be used to detect Block guard settings.
6061 *
6062 * This routine sets up the local Block guard protocol settings for @shost.
6063 * This routine also allocates memory for debugging bg buffers.
6064 **/
6065static void
6066lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
6067{
James Smartbbeb79b2012-06-12 13:54:27 -04006068 uint32_t old_mask;
6069 uint32_t old_guard;
6070
James Smart3772a992009-05-22 14:50:54 -04006071 int pagecnt = 10;
6072 if (lpfc_prot_mask && lpfc_prot_guard) {
6073 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6074 "1478 Registering BlockGuard with the "
6075 "SCSI layer\n");
James Smartbbeb79b2012-06-12 13:54:27 -04006076
6077 old_mask = lpfc_prot_mask;
6078 old_guard = lpfc_prot_guard;
6079
6080 /* Only allow supported values */
6081 lpfc_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
6082 SHOST_DIX_TYPE0_PROTECTION |
6083 SHOST_DIX_TYPE1_PROTECTION);
6084 lpfc_prot_guard &= (SHOST_DIX_GUARD_IP | SHOST_DIX_GUARD_CRC);
6085
6086 /* DIF Type 1 protection for profiles AST1/C1 is end to end */
6087 if (lpfc_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
6088 lpfc_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
6089
6090 if (lpfc_prot_mask && lpfc_prot_guard) {
6091 if ((old_mask != lpfc_prot_mask) ||
6092 (old_guard != lpfc_prot_guard))
6093 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6094 "1475 Registering BlockGuard with the "
6095 "SCSI layer: mask %d guard %d\n",
6096 lpfc_prot_mask, lpfc_prot_guard);
6097
6098 scsi_host_set_prot(shost, lpfc_prot_mask);
6099 scsi_host_set_guard(shost, lpfc_prot_guard);
6100 } else
6101 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6102 "1479 Not Registering BlockGuard with the SCSI "
6103 "layer, Bad protection parameters: %d %d\n",
6104 old_mask, old_guard);
James Smart98c9ea52007-10-27 13:37:33 -04006105 }
James Smartbbeb79b2012-06-12 13:54:27 -04006106
James Smart81301a92008-12-04 22:39:46 -05006107 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05006108 while (pagecnt) {
6109 spin_lock_init(&_dump_buf_lock);
6110 _dump_buf_data =
6111 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
6112 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04006113 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6114 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04006115 "_dump_buf_data at 0x%p\n",
6116 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05006117 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04006118 memset(_dump_buf_data, 0,
6119 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05006120 break;
James Smart3772a992009-05-22 14:50:54 -04006121 } else
James Smart81301a92008-12-04 22:39:46 -05006122 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05006123 }
James Smart81301a92008-12-04 22:39:46 -05006124 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04006125 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6126 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04006127 "memory for hexdump\n");
6128 } else
James Smart6a9c52c2009-10-02 15:16:51 -04006129 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6130 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05006131 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05006132 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05006133 while (pagecnt) {
6134 _dump_buf_dif =
6135 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
6136 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04006137 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6138 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04006139 "_dump_buf_dif at 0x%p\n",
6140 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05006141 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04006142 memset(_dump_buf_dif, 0,
6143 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05006144 break;
James Smart3772a992009-05-22 14:50:54 -04006145 } else
James Smart81301a92008-12-04 22:39:46 -05006146 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05006147 }
James Smart81301a92008-12-04 22:39:46 -05006148 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04006149 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6150 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04006151 "memory for hexdump\n");
6152 } else
James Smart6a9c52c2009-10-02 15:16:51 -04006153 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6154 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04006155 _dump_buf_dif);
6156}
6157
6158/**
6159 * lpfc_post_init_setup - Perform necessary device post initialization setup.
6160 * @phba: pointer to lpfc hba data structure.
6161 *
6162 * This routine is invoked to perform all the necessary post initialization
6163 * setup for the device.
6164 **/
6165static void
6166lpfc_post_init_setup(struct lpfc_hba *phba)
6167{
6168 struct Scsi_Host *shost;
6169 struct lpfc_adapter_event_header adapter_event;
6170
6171 /* Get the default values for Model Name and Description */
6172 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
6173
6174 /*
6175 * hba setup may have changed the hba_queue_depth so we need to
6176 * adjust the value of can_queue.
6177 */
6178 shost = pci_get_drvdata(phba->pcidev);
6179 shost->can_queue = phba->cfg_hba_queue_depth - 10;
6180 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
6181 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05006182
6183 lpfc_host_attrib_init(shost);
6184
James Smart2e0fef82007-06-17 19:56:36 -05006185 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
6186 spin_lock_irq(shost->host_lock);
6187 lpfc_poll_start_timer(phba);
6188 spin_unlock_irq(shost->host_lock);
6189 }
James Smart8f6d98d2006-08-01 07:34:00 -04006190
James Smart93996272008-08-24 21:50:30 -04006191 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6192 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04006193 /* Send board arrival event to upper layer */
6194 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
6195 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
6196 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04006197 sizeof(adapter_event),
6198 (char *) &adapter_event,
6199 LPFC_NL_VENDOR_ID);
6200 return;
6201}
6202
6203/**
6204 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
6205 * @phba: pointer to lpfc hba data structure.
6206 *
6207 * This routine is invoked to set up the PCI device memory space for device
6208 * with SLI-3 interface spec.
6209 *
6210 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006211 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006212 * other values - error
6213 **/
6214static int
6215lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
6216{
6217 struct pci_dev *pdev;
6218 unsigned long bar0map_len, bar2map_len;
6219 int i, hbq_count;
6220 void *ptr;
6221 int error = -ENODEV;
6222
6223 /* Obtain PCI device reference */
6224 if (!phba->pcidev)
6225 return error;
6226 else
6227 pdev = phba->pcidev;
6228
6229 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05006230 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
6231 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
6232 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
6233 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smart3772a992009-05-22 14:50:54 -04006234 return error;
Michael Reed8e685972009-09-18 12:02:05 -05006235 }
6236 }
James Smart3772a992009-05-22 14:50:54 -04006237
6238 /* Get the bus address of Bar0 and Bar2 and the number of bytes
6239 * required by each mapping.
6240 */
6241 phba->pci_bar0_map = pci_resource_start(pdev, 0);
6242 bar0map_len = pci_resource_len(pdev, 0);
6243
6244 phba->pci_bar2_map = pci_resource_start(pdev, 2);
6245 bar2map_len = pci_resource_len(pdev, 2);
6246
6247 /* Map HBA SLIM to a kernel virtual address. */
6248 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
6249 if (!phba->slim_memmap_p) {
6250 dev_printk(KERN_ERR, &pdev->dev,
6251 "ioremap failed for SLIM memory.\n");
6252 goto out;
6253 }
6254
6255 /* Map HBA Control Registers to a kernel virtual address. */
6256 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
6257 if (!phba->ctrl_regs_memmap_p) {
6258 dev_printk(KERN_ERR, &pdev->dev,
6259 "ioremap failed for HBA control registers.\n");
6260 goto out_iounmap_slim;
6261 }
6262
6263 /* Allocate memory for SLI-2 structures */
Joe Perches1aee3832014-09-03 12:56:12 -04006264 phba->slim2p.virt = dma_zalloc_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6265 &phba->slim2p.phys, GFP_KERNEL);
James Smart3772a992009-05-22 14:50:54 -04006266 if (!phba->slim2p.virt)
6267 goto out_iounmap;
6268
James Smart3772a992009-05-22 14:50:54 -04006269 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04006270 phba->mbox_ext = (phba->slim2p.virt +
6271 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04006272 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
6273 phba->IOCBs = (phba->slim2p.virt +
6274 offsetof(struct lpfc_sli2_slim, IOCBs));
6275
6276 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
6277 lpfc_sli_hbq_size(),
6278 &phba->hbqslimp.phys,
6279 GFP_KERNEL);
6280 if (!phba->hbqslimp.virt)
6281 goto out_free_slim;
6282
6283 hbq_count = lpfc_sli_hbq_count();
6284 ptr = phba->hbqslimp.virt;
6285 for (i = 0; i < hbq_count; ++i) {
6286 phba->hbqs[i].hbq_virt = ptr;
6287 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
6288 ptr += (lpfc_hbq_defs[i]->entry_count *
6289 sizeof(struct lpfc_hbq_entry));
6290 }
6291 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
6292 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
6293
6294 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
6295
6296 INIT_LIST_HEAD(&phba->rb_pend_list);
6297
6298 phba->MBslimaddr = phba->slim_memmap_p;
6299 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
6300 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
6301 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
6302 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04006303
dea31012005-04-17 16:05:31 -05006304 return 0;
6305
dea31012005-04-17 16:05:31 -05006306out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04006307 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6308 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05006309out_iounmap:
6310 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05006311out_iounmap_slim:
dea31012005-04-17 16:05:31 -05006312 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05006313out:
6314 return error;
6315}
6316
James Smarte59058c2008-08-24 21:49:00 -04006317/**
James Smart3772a992009-05-22 14:50:54 -04006318 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
6319 * @phba: pointer to lpfc hba data structure.
6320 *
6321 * This routine is invoked to unset the PCI device memory space for device
6322 * with SLI-3 interface spec.
6323 **/
6324static void
6325lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
6326{
6327 struct pci_dev *pdev;
6328
6329 /* Obtain PCI device reference */
6330 if (!phba->pcidev)
6331 return;
6332 else
6333 pdev = phba->pcidev;
6334
6335 /* Free coherent DMA memory allocated */
6336 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
6337 phba->hbqslimp.virt, phba->hbqslimp.phys);
6338 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6339 phba->slim2p.virt, phba->slim2p.phys);
6340
6341 /* I/O memory unmap */
6342 iounmap(phba->ctrl_regs_memmap_p);
6343 iounmap(phba->slim_memmap_p);
6344
6345 return;
6346}
6347
6348/**
James Smartda0436e2009-05-22 14:51:39 -04006349 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
6350 * @phba: pointer to lpfc hba data structure.
6351 *
6352 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
6353 * done and check status.
6354 *
6355 * Return 0 if successful, otherwise -ENODEV.
6356 **/
6357int
6358lpfc_sli4_post_status_check(struct lpfc_hba *phba)
6359{
James Smart2fcee4b2010-12-15 17:57:46 -05006360 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
6361 struct lpfc_register reg_data;
6362 int i, port_error = 0;
6363 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04006364
James Smart9940b972011-03-11 16:06:12 -05006365 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
6366 memset(&reg_data, 0, sizeof(reg_data));
James Smart2fcee4b2010-12-15 17:57:46 -05006367 if (!phba->sli4_hba.PSMPHRregaddr)
James Smartda0436e2009-05-22 14:51:39 -04006368 return -ENODEV;
6369
James Smartda0436e2009-05-22 14:51:39 -04006370 /* Wait up to 30 seconds for the SLI Port POST done and ready */
6371 for (i = 0; i < 3000; i++) {
James Smart9940b972011-03-11 16:06:12 -05006372 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
6373 &portsmphr_reg.word0) ||
6374 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
James Smart2fcee4b2010-12-15 17:57:46 -05006375 /* Port has a fatal POST error, break out */
James Smartda0436e2009-05-22 14:51:39 -04006376 port_error = -ENODEV;
6377 break;
6378 }
James Smart2fcee4b2010-12-15 17:57:46 -05006379 if (LPFC_POST_STAGE_PORT_READY ==
6380 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
James Smartda0436e2009-05-22 14:51:39 -04006381 break;
James Smartda0436e2009-05-22 14:51:39 -04006382 msleep(10);
6383 }
6384
James Smart2fcee4b2010-12-15 17:57:46 -05006385 /*
6386 * If there was a port error during POST, then don't proceed with
6387 * other register reads as the data may not be valid. Just exit.
6388 */
6389 if (port_error) {
James Smartda0436e2009-05-22 14:51:39 -04006390 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05006391 "1408 Port Failed POST - portsmphr=0x%x, "
6392 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
6393 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
6394 portsmphr_reg.word0,
6395 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
6396 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
6397 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
6398 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
6399 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
6400 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
6401 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
6402 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
6403 } else {
James Smart28baac72010-02-12 14:42:03 -05006404 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05006405 "2534 Device Info: SLIFamily=0x%x, "
6406 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
6407 "SLIHint_2=0x%x, FT=0x%x\n",
James Smart28baac72010-02-12 14:42:03 -05006408 bf_get(lpfc_sli_intf_sli_family,
6409 &phba->sli4_hba.sli_intf),
6410 bf_get(lpfc_sli_intf_slirev,
6411 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05006412 bf_get(lpfc_sli_intf_if_type,
James Smart28baac72010-02-12 14:42:03 -05006413 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05006414 bf_get(lpfc_sli_intf_sli_hint1,
6415 &phba->sli4_hba.sli_intf),
6416 bf_get(lpfc_sli_intf_sli_hint2,
6417 &phba->sli4_hba.sli_intf),
6418 bf_get(lpfc_sli_intf_func_type,
James Smart28baac72010-02-12 14:42:03 -05006419 &phba->sli4_hba.sli_intf));
James Smart2fcee4b2010-12-15 17:57:46 -05006420 /*
6421 * Check for other Port errors during the initialization
6422 * process. Fail the load if the port did not come up
6423 * correctly.
6424 */
6425 if_type = bf_get(lpfc_sli_intf_if_type,
6426 &phba->sli4_hba.sli_intf);
6427 switch (if_type) {
6428 case LPFC_SLI_INTF_IF_TYPE_0:
6429 phba->sli4_hba.ue_mask_lo =
6430 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
6431 phba->sli4_hba.ue_mask_hi =
6432 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
6433 uerrlo_reg.word0 =
6434 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
6435 uerrhi_reg.word0 =
6436 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
6437 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
6438 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
6439 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6440 "1422 Unrecoverable Error "
6441 "Detected during POST "
6442 "uerr_lo_reg=0x%x, "
6443 "uerr_hi_reg=0x%x, "
6444 "ue_mask_lo_reg=0x%x, "
6445 "ue_mask_hi_reg=0x%x\n",
6446 uerrlo_reg.word0,
6447 uerrhi_reg.word0,
6448 phba->sli4_hba.ue_mask_lo,
6449 phba->sli4_hba.ue_mask_hi);
6450 port_error = -ENODEV;
6451 }
6452 break;
6453 case LPFC_SLI_INTF_IF_TYPE_2:
6454 /* Final checks. The port status should be clean. */
James Smart9940b972011-03-11 16:06:12 -05006455 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
6456 &reg_data.word0) ||
James Smart05580562011-05-24 11:40:48 -04006457 (bf_get(lpfc_sliport_status_err, &reg_data) &&
6458 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
James Smart2fcee4b2010-12-15 17:57:46 -05006459 phba->work_status[0] =
6460 readl(phba->sli4_hba.u.if_type2.
6461 ERR1regaddr);
6462 phba->work_status[1] =
6463 readl(phba->sli4_hba.u.if_type2.
6464 ERR2regaddr);
6465 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05006466 "2888 Unrecoverable port error "
6467 "following POST: port status reg "
6468 "0x%x, port_smphr reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05006469 "error 1=0x%x, error 2=0x%x\n",
6470 reg_data.word0,
6471 portsmphr_reg.word0,
6472 phba->work_status[0],
6473 phba->work_status[1]);
6474 port_error = -ENODEV;
6475 }
6476 break;
6477 case LPFC_SLI_INTF_IF_TYPE_1:
6478 default:
6479 break;
6480 }
James Smart28baac72010-02-12 14:42:03 -05006481 }
James Smartda0436e2009-05-22 14:51:39 -04006482 return port_error;
6483}
6484
6485/**
6486 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
6487 * @phba: pointer to lpfc hba data structure.
James Smart2fcee4b2010-12-15 17:57:46 -05006488 * @if_type: The SLI4 interface type getting configured.
James Smartda0436e2009-05-22 14:51:39 -04006489 *
6490 * This routine is invoked to set up SLI4 BAR0 PCI config space register
6491 * memory map.
6492 **/
6493static void
James Smart2fcee4b2010-12-15 17:57:46 -05006494lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04006495{
James Smart2fcee4b2010-12-15 17:57:46 -05006496 switch (if_type) {
6497 case LPFC_SLI_INTF_IF_TYPE_0:
6498 phba->sli4_hba.u.if_type0.UERRLOregaddr =
6499 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
6500 phba->sli4_hba.u.if_type0.UERRHIregaddr =
6501 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
6502 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
6503 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
6504 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
6505 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
6506 phba->sli4_hba.SLIINTFregaddr =
6507 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
6508 break;
6509 case LPFC_SLI_INTF_IF_TYPE_2:
6510 phba->sli4_hba.u.if_type2.ERR1regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006511 phba->sli4_hba.conf_regs_memmap_p +
6512 LPFC_CTL_PORT_ER1_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006513 phba->sli4_hba.u.if_type2.ERR2regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006514 phba->sli4_hba.conf_regs_memmap_p +
6515 LPFC_CTL_PORT_ER2_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006516 phba->sli4_hba.u.if_type2.CTRLregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006517 phba->sli4_hba.conf_regs_memmap_p +
6518 LPFC_CTL_PORT_CTL_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006519 phba->sli4_hba.u.if_type2.STATUSregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006520 phba->sli4_hba.conf_regs_memmap_p +
6521 LPFC_CTL_PORT_STA_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006522 phba->sli4_hba.SLIINTFregaddr =
6523 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
6524 phba->sli4_hba.PSMPHRregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006525 phba->sli4_hba.conf_regs_memmap_p +
6526 LPFC_CTL_PORT_SEM_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006527 phba->sli4_hba.RQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05006528 phba->sli4_hba.conf_regs_memmap_p +
6529 LPFC_ULP0_RQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05006530 phba->sli4_hba.WQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05006531 phba->sli4_hba.conf_regs_memmap_p +
6532 LPFC_ULP0_WQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05006533 phba->sli4_hba.EQCQDBregaddr =
6534 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
6535 phba->sli4_hba.MQDBregaddr =
6536 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
6537 phba->sli4_hba.BMBXregaddr =
6538 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
6539 break;
6540 case LPFC_SLI_INTF_IF_TYPE_1:
6541 default:
6542 dev_printk(KERN_ERR, &phba->pcidev->dev,
6543 "FATAL - unsupported SLI4 interface type - %d\n",
6544 if_type);
6545 break;
6546 }
James Smartda0436e2009-05-22 14:51:39 -04006547}
6548
6549/**
6550 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
6551 * @phba: pointer to lpfc hba data structure.
6552 *
6553 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
6554 * memory map.
6555 **/
6556static void
6557lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
6558{
James Smart2fcee4b2010-12-15 17:57:46 -05006559 phba->sli4_hba.PSMPHRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
6560 LPFC_SLIPORT_IF0_SMPHR;
James Smartda0436e2009-05-22 14:51:39 -04006561 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006562 LPFC_HST_ISR0;
James Smartda0436e2009-05-22 14:51:39 -04006563 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006564 LPFC_HST_IMR0;
James Smartda0436e2009-05-22 14:51:39 -04006565 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006566 LPFC_HST_ISCR0;
James Smartda0436e2009-05-22 14:51:39 -04006567}
6568
6569/**
6570 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
6571 * @phba: pointer to lpfc hba data structure.
6572 * @vf: virtual function number
6573 *
6574 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
6575 * based on the given viftual function number, @vf.
6576 *
6577 * Return 0 if successful, otherwise -ENODEV.
6578 **/
6579static int
6580lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
6581{
6582 if (vf > LPFC_VIR_FUNC_MAX)
6583 return -ENODEV;
6584
6585 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05006586 vf * LPFC_VFR_PAGE_SIZE +
6587 LPFC_ULP0_RQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04006588 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05006589 vf * LPFC_VFR_PAGE_SIZE +
6590 LPFC_ULP0_WQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04006591 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6592 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
6593 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6594 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
6595 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6596 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
6597 return 0;
6598}
6599
6600/**
6601 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
6602 * @phba: pointer to lpfc hba data structure.
6603 *
6604 * This routine is invoked to create the bootstrap mailbox
6605 * region consistent with the SLI-4 interface spec. This
6606 * routine allocates all memory necessary to communicate
6607 * mailbox commands to the port and sets up all alignment
6608 * needs. No locks are expected to be held when calling
6609 * this routine.
6610 *
6611 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006612 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04006613 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04006614 **/
6615static int
6616lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
6617{
6618 uint32_t bmbx_size;
6619 struct lpfc_dmabuf *dmabuf;
6620 struct dma_address *dma_address;
6621 uint32_t pa_addr;
6622 uint64_t phys_addr;
6623
6624 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
6625 if (!dmabuf)
6626 return -ENOMEM;
6627
6628 /*
6629 * The bootstrap mailbox region is comprised of 2 parts
6630 * plus an alignment restriction of 16 bytes.
6631 */
6632 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
Joe Perches1aee3832014-09-03 12:56:12 -04006633 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, bmbx_size,
6634 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04006635 if (!dmabuf->virt) {
6636 kfree(dmabuf);
6637 return -ENOMEM;
6638 }
James Smartda0436e2009-05-22 14:51:39 -04006639
6640 /*
6641 * Initialize the bootstrap mailbox pointers now so that the register
6642 * operations are simple later. The mailbox dma address is required
6643 * to be 16-byte aligned. Also align the virtual memory as each
6644 * maibox is copied into the bmbx mailbox region before issuing the
6645 * command to the port.
6646 */
6647 phba->sli4_hba.bmbx.dmabuf = dmabuf;
6648 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
6649
6650 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
6651 LPFC_ALIGN_16_BYTE);
6652 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
6653 LPFC_ALIGN_16_BYTE);
6654
6655 /*
6656 * Set the high and low physical addresses now. The SLI4 alignment
6657 * requirement is 16 bytes and the mailbox is posted to the port
6658 * as two 30-bit addresses. The other data is a bit marking whether
6659 * the 30-bit address is the high or low address.
6660 * Upcast bmbx aphys to 64bits so shift instruction compiles
6661 * clean on 32 bit machines.
6662 */
6663 dma_address = &phba->sli4_hba.bmbx.dma_address;
6664 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
6665 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
6666 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
6667 LPFC_BMBX_BIT1_ADDR_HI);
6668
6669 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
6670 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
6671 LPFC_BMBX_BIT1_ADDR_LO);
6672 return 0;
6673}
6674
6675/**
6676 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
6677 * @phba: pointer to lpfc hba data structure.
6678 *
6679 * This routine is invoked to teardown the bootstrap mailbox
6680 * region and release all host resources. This routine requires
6681 * the caller to ensure all mailbox commands recovered, no
6682 * additional mailbox comands are sent, and interrupts are disabled
6683 * before calling this routine.
6684 *
6685 **/
6686static void
6687lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
6688{
6689 dma_free_coherent(&phba->pcidev->dev,
6690 phba->sli4_hba.bmbx.bmbx_size,
6691 phba->sli4_hba.bmbx.dmabuf->virt,
6692 phba->sli4_hba.bmbx.dmabuf->phys);
6693
6694 kfree(phba->sli4_hba.bmbx.dmabuf);
6695 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
6696}
6697
6698/**
6699 * lpfc_sli4_read_config - Get the config parameters.
6700 * @phba: pointer to lpfc hba data structure.
6701 *
6702 * This routine is invoked to read the configuration parameters from the HBA.
6703 * The configuration parameters are used to set the base and maximum values
6704 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
6705 * allocation for the port.
6706 *
6707 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006708 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006709 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006710 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006711 **/
James Smartff78d8f2011-12-13 13:21:35 -05006712int
James Smartda0436e2009-05-22 14:51:39 -04006713lpfc_sli4_read_config(struct lpfc_hba *phba)
6714{
6715 LPFC_MBOXQ_t *pmb;
6716 struct lpfc_mbx_read_config *rd_config;
James Smart912e3ac2011-05-24 11:42:11 -04006717 union lpfc_sli4_cfg_shdr *shdr;
6718 uint32_t shdr_status, shdr_add_status;
6719 struct lpfc_mbx_get_func_cfg *get_func_cfg;
6720 struct lpfc_rsrc_desc_fcfcoe *desc;
James Smart8aa134a2012-08-14 14:25:29 -04006721 char *pdesc_0;
James Smart912e3ac2011-05-24 11:42:11 -04006722 uint32_t desc_count;
James Smart8aa134a2012-08-14 14:25:29 -04006723 int length, i, rc = 0, rc2;
James Smartda0436e2009-05-22 14:51:39 -04006724
6725 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6726 if (!pmb) {
6727 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6728 "2011 Unable to allocate memory for issuing "
6729 "SLI_CONFIG_SPECIAL mailbox command\n");
6730 return -ENOMEM;
6731 }
6732
6733 lpfc_read_config(phba, pmb);
6734
6735 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6736 if (rc != MBX_SUCCESS) {
6737 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6738 "2012 Mailbox failed , mbxCmd x%x "
6739 "READ_CONFIG, mbxStatus x%x\n",
6740 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6741 bf_get(lpfc_mqe_status, &pmb->u.mqe));
6742 rc = -EIO;
6743 } else {
6744 rd_config = &pmb->u.mqe.un.rd_config;
James Smartff78d8f2011-12-13 13:21:35 -05006745 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
6746 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
6747 phba->sli4_hba.lnk_info.lnk_tp =
6748 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
6749 phba->sli4_hba.lnk_info.lnk_no =
6750 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
6751 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6752 "3081 lnk_type:%d, lnk_numb:%d\n",
6753 phba->sli4_hba.lnk_info.lnk_tp,
6754 phba->sli4_hba.lnk_info.lnk_no);
6755 } else
6756 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6757 "3082 Mailbox (x%x) returned ldv:x0\n",
6758 bf_get(lpfc_mqe_command, &pmb->u.mqe));
James Smart6d368e52011-05-24 11:44:12 -04006759 phba->sli4_hba.extents_in_use =
6760 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04006761 phba->sli4_hba.max_cfg_param.max_xri =
6762 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
6763 phba->sli4_hba.max_cfg_param.xri_base =
6764 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
6765 phba->sli4_hba.max_cfg_param.max_vpi =
6766 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
6767 phba->sli4_hba.max_cfg_param.vpi_base =
6768 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
6769 phba->sli4_hba.max_cfg_param.max_rpi =
6770 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
6771 phba->sli4_hba.max_cfg_param.rpi_base =
6772 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
6773 phba->sli4_hba.max_cfg_param.max_vfi =
6774 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
6775 phba->sli4_hba.max_cfg_param.vfi_base =
6776 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
6777 phba->sli4_hba.max_cfg_param.max_fcfi =
6778 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04006779 phba->sli4_hba.max_cfg_param.max_eq =
6780 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
6781 phba->sli4_hba.max_cfg_param.max_rq =
6782 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
6783 phba->sli4_hba.max_cfg_param.max_wq =
6784 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
6785 phba->sli4_hba.max_cfg_param.max_cq =
6786 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
6787 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
6788 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
6789 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
6790 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
James Smart5ffc2662009-11-18 15:39:44 -05006791 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
6792 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04006793 phba->max_vports = phba->max_vpi;
6794 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart6d368e52011-05-24 11:44:12 -04006795 "2003 cfg params Extents? %d "
6796 "XRI(B:%d M:%d), "
James Smartda0436e2009-05-22 14:51:39 -04006797 "VPI(B:%d M:%d) "
6798 "VFI(B:%d M:%d) "
6799 "RPI(B:%d M:%d) "
James Smart6d368e52011-05-24 11:44:12 -04006800 "FCFI(Count:%d)\n",
6801 phba->sli4_hba.extents_in_use,
James Smartda0436e2009-05-22 14:51:39 -04006802 phba->sli4_hba.max_cfg_param.xri_base,
6803 phba->sli4_hba.max_cfg_param.max_xri,
6804 phba->sli4_hba.max_cfg_param.vpi_base,
6805 phba->sli4_hba.max_cfg_param.max_vpi,
6806 phba->sli4_hba.max_cfg_param.vfi_base,
6807 phba->sli4_hba.max_cfg_param.max_vfi,
6808 phba->sli4_hba.max_cfg_param.rpi_base,
6809 phba->sli4_hba.max_cfg_param.max_rpi,
James Smartda0436e2009-05-22 14:51:39 -04006810 phba->sli4_hba.max_cfg_param.max_fcfi);
6811 }
James Smart912e3ac2011-05-24 11:42:11 -04006812
6813 if (rc)
6814 goto read_cfg_out;
James Smartda0436e2009-05-22 14:51:39 -04006815
6816 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -04006817 length = phba->sli4_hba.max_cfg_param.max_xri -
6818 lpfc_sli4_get_els_iocb_cnt(phba);
6819 if (phba->cfg_hba_queue_depth > length) {
6820 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6821 "3361 HBA queue depth changed from %d to %d\n",
6822 phba->cfg_hba_queue_depth, length);
6823 phba->cfg_hba_queue_depth = length;
6824 }
James Smart912e3ac2011-05-24 11:42:11 -04006825
6826 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
6827 LPFC_SLI_INTF_IF_TYPE_2)
6828 goto read_cfg_out;
6829
6830 /* get the pf# and vf# for SLI4 if_type 2 port */
6831 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
6832 sizeof(struct lpfc_sli4_cfg_mhdr));
6833 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
6834 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
6835 length, LPFC_SLI4_MBX_EMBED);
6836
James Smart8aa134a2012-08-14 14:25:29 -04006837 rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart912e3ac2011-05-24 11:42:11 -04006838 shdr = (union lpfc_sli4_cfg_shdr *)
6839 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
6840 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6841 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart8aa134a2012-08-14 14:25:29 -04006842 if (rc2 || shdr_status || shdr_add_status) {
James Smart912e3ac2011-05-24 11:42:11 -04006843 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6844 "3026 Mailbox failed , mbxCmd x%x "
6845 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
6846 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6847 bf_get(lpfc_mqe_status, &pmb->u.mqe));
James Smart912e3ac2011-05-24 11:42:11 -04006848 goto read_cfg_out;
6849 }
6850
6851 /* search for fc_fcoe resrouce descriptor */
6852 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
6853 desc_count = get_func_cfg->func_cfg.rsrc_desc_count;
6854
James Smart8aa134a2012-08-14 14:25:29 -04006855 pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
6856 desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
6857 length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
6858 if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
6859 length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
6860 else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
6861 goto read_cfg_out;
6862
James Smart912e3ac2011-05-24 11:42:11 -04006863 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
James Smart8aa134a2012-08-14 14:25:29 -04006864 desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
James Smart912e3ac2011-05-24 11:42:11 -04006865 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
James Smart8aa134a2012-08-14 14:25:29 -04006866 bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
James Smart912e3ac2011-05-24 11:42:11 -04006867 phba->sli4_hba.iov.pf_number =
6868 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
6869 phba->sli4_hba.iov.vf_number =
6870 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
6871 break;
6872 }
6873 }
6874
6875 if (i < LPFC_RSRC_DESC_MAX_NUM)
6876 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6877 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
6878 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
6879 phba->sli4_hba.iov.vf_number);
James Smart8aa134a2012-08-14 14:25:29 -04006880 else
James Smart912e3ac2011-05-24 11:42:11 -04006881 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6882 "3028 GET_FUNCTION_CONFIG: failed to find "
6883 "Resrouce Descriptor:x%x\n",
6884 LPFC_RSRC_DESC_TYPE_FCFCOE);
James Smart912e3ac2011-05-24 11:42:11 -04006885
6886read_cfg_out:
6887 mempool_free(pmb, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04006888 return rc;
6889}
6890
6891/**
James Smart2fcee4b2010-12-15 17:57:46 -05006892 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
James Smartda0436e2009-05-22 14:51:39 -04006893 * @phba: pointer to lpfc hba data structure.
6894 *
James Smart2fcee4b2010-12-15 17:57:46 -05006895 * This routine is invoked to setup the port-side endian order when
6896 * the port if_type is 0. This routine has no function for other
6897 * if_types.
James Smartda0436e2009-05-22 14:51:39 -04006898 *
6899 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006900 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006901 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006902 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006903 **/
6904static int
6905lpfc_setup_endian_order(struct lpfc_hba *phba)
6906{
6907 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05006908 uint32_t if_type, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04006909 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
6910 HOST_ENDIAN_HIGH_WORD1};
6911
James Smart2fcee4b2010-12-15 17:57:46 -05006912 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
6913 switch (if_type) {
6914 case LPFC_SLI_INTF_IF_TYPE_0:
6915 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6916 GFP_KERNEL);
6917 if (!mboxq) {
6918 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6919 "0492 Unable to allocate memory for "
6920 "issuing SLI_CONFIG_SPECIAL mailbox "
6921 "command\n");
6922 return -ENOMEM;
6923 }
James Smartda0436e2009-05-22 14:51:39 -04006924
James Smart2fcee4b2010-12-15 17:57:46 -05006925 /*
6926 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
6927 * two words to contain special data values and no other data.
6928 */
6929 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
6930 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
6931 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6932 if (rc != MBX_SUCCESS) {
6933 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6934 "0493 SLI_CONFIG_SPECIAL mailbox "
6935 "failed with status x%x\n",
6936 rc);
6937 rc = -EIO;
6938 }
6939 mempool_free(mboxq, phba->mbox_mem_pool);
6940 break;
6941 case LPFC_SLI_INTF_IF_TYPE_2:
6942 case LPFC_SLI_INTF_IF_TYPE_1:
6943 default:
6944 break;
James Smartda0436e2009-05-22 14:51:39 -04006945 }
James Smartda0436e2009-05-22 14:51:39 -04006946 return rc;
6947}
6948
6949/**
James Smart5350d872011-10-10 21:33:49 -04006950 * lpfc_sli4_queue_verify - Verify and update EQ and CQ counts
James Smartda0436e2009-05-22 14:51:39 -04006951 * @phba: pointer to lpfc hba data structure.
6952 *
James Smart5350d872011-10-10 21:33:49 -04006953 * This routine is invoked to check the user settable queue counts for EQs and
6954 * CQs. after this routine is called the counts will be set to valid values that
6955 * adhere to the constraints of the system's interrupt vectors and the port's
6956 * queue resources.
James Smartda0436e2009-05-22 14:51:39 -04006957 *
6958 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006959 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006960 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04006961 **/
6962static int
James Smart5350d872011-10-10 21:33:49 -04006963lpfc_sli4_queue_verify(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04006964{
James Smart67d12732012-08-03 12:36:13 -04006965 int cfg_fcp_io_channel;
James Smart90695ee2012-08-14 14:25:36 -04006966 uint32_t cpu;
6967 uint32_t i = 0;
James Smart1ba981f2014-02-20 09:56:45 -05006968 int fof_vectors = phba->cfg_fof ? 1 : 0;
James Smartda0436e2009-05-22 14:51:39 -04006969
6970 /*
James Smart67d12732012-08-03 12:36:13 -04006971 * Sanity check for configured queue parameters against the run-time
James Smartda0436e2009-05-22 14:51:39 -04006972 * device parameters
6973 */
6974
James Smart67d12732012-08-03 12:36:13 -04006975 /* Sanity check on HBA EQ parameters */
6976 cfg_fcp_io_channel = phba->cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04006977
James Smart7bb03bb2013-04-17 20:19:16 -04006978 /* It doesn't make sense to have more io channels then online CPUs */
6979 for_each_present_cpu(cpu) {
6980 if (cpu_online(cpu))
6981 i++;
James Smart90695ee2012-08-14 14:25:36 -04006982 }
James Smart7bb03bb2013-04-17 20:19:16 -04006983 phba->sli4_hba.num_online_cpu = i;
James Smartb246de12013-05-31 17:03:07 -04006984 phba->sli4_hba.num_present_cpu = lpfc_present_cpu;
James Smart76fd07a2014-02-20 09:57:18 -05006985 phba->sli4_hba.curr_disp_cpu = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04006986
James Smart90695ee2012-08-14 14:25:36 -04006987 if (i < cfg_fcp_io_channel) {
James Smart82c3e9b2012-09-29 11:29:50 -04006988 lpfc_printf_log(phba,
6989 KERN_ERR, LOG_INIT,
James Smart90695ee2012-08-14 14:25:36 -04006990 "3188 Reducing IO channels to match number of "
James Smart7bb03bb2013-04-17 20:19:16 -04006991 "online CPUs: from %d to %d\n",
6992 cfg_fcp_io_channel, i);
James Smart90695ee2012-08-14 14:25:36 -04006993 cfg_fcp_io_channel = i;
6994 }
6995
James Smart1ba981f2014-02-20 09:56:45 -05006996 if (cfg_fcp_io_channel + fof_vectors >
James Smart67d12732012-08-03 12:36:13 -04006997 phba->sli4_hba.max_cfg_param.max_eq) {
James Smart82c3e9b2012-09-29 11:29:50 -04006998 if (phba->sli4_hba.max_cfg_param.max_eq <
6999 LPFC_FCP_IO_CHAN_MIN) {
James Smartda0436e2009-05-22 14:51:39 -04007000 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7001 "2574 Not enough EQs (%d) from the "
7002 "pci function for supporting FCP "
7003 "EQs (%d)\n",
7004 phba->sli4_hba.max_cfg_param.max_eq,
James Smart67d12732012-08-03 12:36:13 -04007005 phba->cfg_fcp_io_channel);
James Smartda0436e2009-05-22 14:51:39 -04007006 goto out_error;
7007 }
James Smart82c3e9b2012-09-29 11:29:50 -04007008 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7009 "2575 Reducing IO channels to match number of "
7010 "available EQs: from %d to %d\n",
7011 cfg_fcp_io_channel,
7012 phba->sli4_hba.max_cfg_param.max_eq);
James Smart1ba981f2014-02-20 09:56:45 -05007013 cfg_fcp_io_channel = phba->sli4_hba.max_cfg_param.max_eq -
7014 fof_vectors;
James Smartda0436e2009-05-22 14:51:39 -04007015 }
James Smart67d12732012-08-03 12:36:13 -04007016
James Smartda0436e2009-05-22 14:51:39 -04007017 /* The actual number of FCP event queues adopted */
James Smart67d12732012-08-03 12:36:13 -04007018 phba->cfg_fcp_io_channel = cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04007019
James Smartda0436e2009-05-22 14:51:39 -04007020 /* Get EQ depth from module parameter, fake the default for now */
7021 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
7022 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
7023
James Smart5350d872011-10-10 21:33:49 -04007024 /* Get CQ depth from module parameter, fake the default for now */
7025 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
7026 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
7027
7028 return 0;
7029out_error:
7030 return -ENOMEM;
7031}
7032
7033/**
7034 * lpfc_sli4_queue_create - Create all the SLI4 queues
7035 * @phba: pointer to lpfc hba data structure.
7036 *
7037 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
7038 * operation. For each SLI4 queue type, the parameters such as queue entry
7039 * count (queue depth) shall be taken from the module parameter. For now,
7040 * we just use some constant number as place holder.
7041 *
7042 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07007043 * 0 - successful
James Smart5350d872011-10-10 21:33:49 -04007044 * -ENOMEM - No availble memory
7045 * -EIO - The mailbox failed to complete successfully.
7046 **/
7047int
7048lpfc_sli4_queue_create(struct lpfc_hba *phba)
7049{
7050 struct lpfc_queue *qdesc;
James Smart67d12732012-08-03 12:36:13 -04007051 int idx;
James Smart5350d872011-10-10 21:33:49 -04007052
7053 /*
James Smart67d12732012-08-03 12:36:13 -04007054 * Create HBA Record arrays.
James Smart5350d872011-10-10 21:33:49 -04007055 */
James Smart67d12732012-08-03 12:36:13 -04007056 if (!phba->cfg_fcp_io_channel)
7057 return -ERANGE;
James Smart5350d872011-10-10 21:33:49 -04007058
James Smart67d12732012-08-03 12:36:13 -04007059 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
7060 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
7061 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
7062 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
7063 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
7064 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
7065
7066 phba->sli4_hba.hba_eq = kzalloc((sizeof(struct lpfc_queue *) *
7067 phba->cfg_fcp_io_channel), GFP_KERNEL);
7068 if (!phba->sli4_hba.hba_eq) {
James Smartda0436e2009-05-22 14:51:39 -04007069 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04007070 "2576 Failed allocate memory for "
7071 "fast-path EQ record array\n");
James Smartda0436e2009-05-22 14:51:39 -04007072 goto out_error;
7073 }
James Smart67d12732012-08-03 12:36:13 -04007074
7075 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
7076 phba->cfg_fcp_io_channel), GFP_KERNEL);
7077 if (!phba->sli4_hba.fcp_cq) {
7078 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7079 "2577 Failed allocate memory for fast-path "
7080 "CQ record array\n");
7081 goto out_error;
7082 }
7083
7084 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
7085 phba->cfg_fcp_io_channel), GFP_KERNEL);
7086 if (!phba->sli4_hba.fcp_wq) {
7087 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7088 "2578 Failed allocate memory for fast-path "
7089 "WQ record array\n");
7090 goto out_error;
7091 }
James Smartda0436e2009-05-22 14:51:39 -04007092
James Smart5350d872011-10-10 21:33:49 -04007093 /*
James Smart67d12732012-08-03 12:36:13 -04007094 * Since the first EQ can have multiple CQs associated with it,
7095 * this array is used to quickly see if we have a FCP fast-path
7096 * CQ match.
James Smart5350d872011-10-10 21:33:49 -04007097 */
James Smart67d12732012-08-03 12:36:13 -04007098 phba->sli4_hba.fcp_cq_map = kzalloc((sizeof(uint16_t) *
7099 phba->cfg_fcp_io_channel), GFP_KERNEL);
7100 if (!phba->sli4_hba.fcp_cq_map) {
7101 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7102 "2545 Failed allocate memory for fast-path "
7103 "CQ map\n");
7104 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007105 }
James Smart67d12732012-08-03 12:36:13 -04007106
7107 /*
7108 * Create HBA Event Queues (EQs). The cfg_fcp_io_channel specifies
7109 * how many EQs to create.
7110 */
7111 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7112
7113 /* Create EQs */
James Smartda0436e2009-05-22 14:51:39 -04007114 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
7115 phba->sli4_hba.eq_ecount);
7116 if (!qdesc) {
7117 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04007118 "0497 Failed allocate EQ (%d)\n", idx);
7119 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007120 }
James Smart67d12732012-08-03 12:36:13 -04007121 phba->sli4_hba.hba_eq[idx] = qdesc;
7122
7123 /* Create Fast Path FCP CQs */
7124 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7125 phba->sli4_hba.cq_ecount);
7126 if (!qdesc) {
7127 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7128 "0499 Failed allocate fast-path FCP "
7129 "CQ (%d)\n", idx);
7130 goto out_error;
7131 }
7132 phba->sli4_hba.fcp_cq[idx] = qdesc;
7133
7134 /* Create Fast Path FCP WQs */
7135 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
7136 phba->sli4_hba.wq_ecount);
7137 if (!qdesc) {
7138 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7139 "0503 Failed allocate fast-path FCP "
7140 "WQ (%d)\n", idx);
7141 goto out_error;
7142 }
7143 phba->sli4_hba.fcp_wq[idx] = qdesc;
James Smartda0436e2009-05-22 14:51:39 -04007144 }
7145
James Smart67d12732012-08-03 12:36:13 -04007146
James Smartda0436e2009-05-22 14:51:39 -04007147 /*
James Smart67d12732012-08-03 12:36:13 -04007148 * Create Slow Path Completion Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04007149 */
7150
James Smartda0436e2009-05-22 14:51:39 -04007151 /* Create slow-path Mailbox Command Complete Queue */
7152 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7153 phba->sli4_hba.cq_ecount);
7154 if (!qdesc) {
7155 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7156 "0500 Failed allocate slow-path mailbox CQ\n");
James Smart67d12732012-08-03 12:36:13 -04007157 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007158 }
7159 phba->sli4_hba.mbx_cq = qdesc;
7160
7161 /* Create slow-path ELS Complete Queue */
7162 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7163 phba->sli4_hba.cq_ecount);
7164 if (!qdesc) {
7165 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7166 "0501 Failed allocate slow-path ELS CQ\n");
James Smart67d12732012-08-03 12:36:13 -04007167 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007168 }
7169 phba->sli4_hba.els_cq = qdesc;
7170
James Smartda0436e2009-05-22 14:51:39 -04007171
James Smart5350d872011-10-10 21:33:49 -04007172 /*
James Smart67d12732012-08-03 12:36:13 -04007173 * Create Slow Path Work Queues (WQs)
James Smart5350d872011-10-10 21:33:49 -04007174 */
James Smartda0436e2009-05-22 14:51:39 -04007175
7176 /* Create Mailbox Command Queue */
James Smartda0436e2009-05-22 14:51:39 -04007177
7178 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
7179 phba->sli4_hba.mq_ecount);
7180 if (!qdesc) {
7181 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7182 "0505 Failed allocate slow-path MQ\n");
James Smart67d12732012-08-03 12:36:13 -04007183 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007184 }
7185 phba->sli4_hba.mbx_wq = qdesc;
7186
7187 /*
James Smart67d12732012-08-03 12:36:13 -04007188 * Create ELS Work Queues
James Smartda0436e2009-05-22 14:51:39 -04007189 */
James Smartda0436e2009-05-22 14:51:39 -04007190
7191 /* Create slow-path ELS Work Queue */
7192 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
7193 phba->sli4_hba.wq_ecount);
7194 if (!qdesc) {
7195 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7196 "0504 Failed allocate slow-path ELS WQ\n");
James Smart67d12732012-08-03 12:36:13 -04007197 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007198 }
7199 phba->sli4_hba.els_wq = qdesc;
7200
James Smartda0436e2009-05-22 14:51:39 -04007201 /*
7202 * Create Receive Queue (RQ)
7203 */
James Smartda0436e2009-05-22 14:51:39 -04007204
7205 /* Create Receive Queue for header */
7206 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
7207 phba->sli4_hba.rq_ecount);
7208 if (!qdesc) {
7209 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7210 "0506 Failed allocate receive HRQ\n");
James Smart67d12732012-08-03 12:36:13 -04007211 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007212 }
7213 phba->sli4_hba.hdr_rq = qdesc;
7214
7215 /* Create Receive Queue for data */
7216 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
7217 phba->sli4_hba.rq_ecount);
7218 if (!qdesc) {
7219 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7220 "0507 Failed allocate receive DRQ\n");
James Smart67d12732012-08-03 12:36:13 -04007221 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007222 }
7223 phba->sli4_hba.dat_rq = qdesc;
7224
James Smart1ba981f2014-02-20 09:56:45 -05007225 /* Create the Queues needed for Flash Optimized Fabric operations */
7226 if (phba->cfg_fof)
7227 lpfc_fof_queue_create(phba);
James Smartda0436e2009-05-22 14:51:39 -04007228 return 0;
7229
James Smartda0436e2009-05-22 14:51:39 -04007230out_error:
James Smart67d12732012-08-03 12:36:13 -04007231 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007232 return -ENOMEM;
7233}
7234
7235/**
7236 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
7237 * @phba: pointer to lpfc hba data structure.
7238 *
7239 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
7240 * operation.
7241 *
7242 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007243 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007244 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007245 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007246 **/
James Smart5350d872011-10-10 21:33:49 -04007247void
James Smartda0436e2009-05-22 14:51:39 -04007248lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
7249{
James Smart67d12732012-08-03 12:36:13 -04007250 int idx;
7251
James Smart1ba981f2014-02-20 09:56:45 -05007252 if (phba->cfg_fof)
7253 lpfc_fof_queue_destroy(phba);
7254
James Smart67d12732012-08-03 12:36:13 -04007255 if (phba->sli4_hba.hba_eq != NULL) {
7256 /* Release HBA event queue */
7257 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7258 if (phba->sli4_hba.hba_eq[idx] != NULL) {
7259 lpfc_sli4_queue_free(
7260 phba->sli4_hba.hba_eq[idx]);
7261 phba->sli4_hba.hba_eq[idx] = NULL;
7262 }
7263 }
7264 kfree(phba->sli4_hba.hba_eq);
7265 phba->sli4_hba.hba_eq = NULL;
7266 }
7267
7268 if (phba->sli4_hba.fcp_cq != NULL) {
7269 /* Release FCP completion queue */
7270 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7271 if (phba->sli4_hba.fcp_cq[idx] != NULL) {
7272 lpfc_sli4_queue_free(
7273 phba->sli4_hba.fcp_cq[idx]);
7274 phba->sli4_hba.fcp_cq[idx] = NULL;
7275 }
7276 }
7277 kfree(phba->sli4_hba.fcp_cq);
7278 phba->sli4_hba.fcp_cq = NULL;
7279 }
7280
7281 if (phba->sli4_hba.fcp_wq != NULL) {
7282 /* Release FCP work queue */
7283 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7284 if (phba->sli4_hba.fcp_wq[idx] != NULL) {
7285 lpfc_sli4_queue_free(
7286 phba->sli4_hba.fcp_wq[idx]);
7287 phba->sli4_hba.fcp_wq[idx] = NULL;
7288 }
7289 }
7290 kfree(phba->sli4_hba.fcp_wq);
7291 phba->sli4_hba.fcp_wq = NULL;
7292 }
7293
7294 /* Release FCP CQ mapping array */
7295 if (phba->sli4_hba.fcp_cq_map != NULL) {
7296 kfree(phba->sli4_hba.fcp_cq_map);
7297 phba->sli4_hba.fcp_cq_map = NULL;
7298 }
James Smartda0436e2009-05-22 14:51:39 -04007299
7300 /* Release mailbox command work queue */
James Smart67d12732012-08-03 12:36:13 -04007301 if (phba->sli4_hba.mbx_wq != NULL) {
7302 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
7303 phba->sli4_hba.mbx_wq = NULL;
7304 }
James Smartda0436e2009-05-22 14:51:39 -04007305
7306 /* Release ELS work queue */
James Smart67d12732012-08-03 12:36:13 -04007307 if (phba->sli4_hba.els_wq != NULL) {
7308 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
7309 phba->sli4_hba.els_wq = NULL;
7310 }
James Smartda0436e2009-05-22 14:51:39 -04007311
7312 /* Release unsolicited receive queue */
James Smart67d12732012-08-03 12:36:13 -04007313 if (phba->sli4_hba.hdr_rq != NULL) {
7314 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
7315 phba->sli4_hba.hdr_rq = NULL;
7316 }
7317 if (phba->sli4_hba.dat_rq != NULL) {
7318 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
7319 phba->sli4_hba.dat_rq = NULL;
7320 }
James Smartda0436e2009-05-22 14:51:39 -04007321
James Smartda0436e2009-05-22 14:51:39 -04007322 /* Release ELS complete queue */
James Smart67d12732012-08-03 12:36:13 -04007323 if (phba->sli4_hba.els_cq != NULL) {
7324 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
7325 phba->sli4_hba.els_cq = NULL;
7326 }
James Smartda0436e2009-05-22 14:51:39 -04007327
7328 /* Release mailbox command complete queue */
James Smart67d12732012-08-03 12:36:13 -04007329 if (phba->sli4_hba.mbx_cq != NULL) {
7330 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
7331 phba->sli4_hba.mbx_cq = NULL;
7332 }
James Smartda0436e2009-05-22 14:51:39 -04007333
7334 return;
7335}
7336
7337/**
7338 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
7339 * @phba: pointer to lpfc hba data structure.
7340 *
7341 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
7342 * operation.
7343 *
7344 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007345 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007346 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007347 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007348 **/
7349int
7350lpfc_sli4_queue_setup(struct lpfc_hba *phba)
7351{
James Smart2a76a282012-08-03 12:35:54 -04007352 struct lpfc_sli *psli = &phba->sli;
7353 struct lpfc_sli_ring *pring;
James Smartda0436e2009-05-22 14:51:39 -04007354 int rc = -ENOMEM;
7355 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
7356 int fcp_cq_index = 0;
James Smart962bc512013-01-03 15:44:00 -05007357 uint32_t shdr_status, shdr_add_status;
7358 union lpfc_sli4_cfg_shdr *shdr;
7359 LPFC_MBOXQ_t *mboxq;
7360 uint32_t length;
7361
7362 /* Check for dual-ULP support */
7363 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7364 if (!mboxq) {
7365 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7366 "3249 Unable to allocate memory for "
7367 "QUERY_FW_CFG mailbox command\n");
7368 return -ENOMEM;
7369 }
7370 length = (sizeof(struct lpfc_mbx_query_fw_config) -
7371 sizeof(struct lpfc_sli4_cfg_mhdr));
7372 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7373 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
7374 length, LPFC_SLI4_MBX_EMBED);
7375
7376 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7377
7378 shdr = (union lpfc_sli4_cfg_shdr *)
7379 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7380 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7381 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7382 if (shdr_status || shdr_add_status || rc) {
7383 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7384 "3250 QUERY_FW_CFG mailbox failed with status "
7385 "x%x add_status x%x, mbx status x%x\n",
7386 shdr_status, shdr_add_status, rc);
7387 if (rc != MBX_TIMEOUT)
7388 mempool_free(mboxq, phba->mbox_mem_pool);
7389 rc = -ENXIO;
7390 goto out_error;
7391 }
7392
7393 phba->sli4_hba.fw_func_mode =
7394 mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
7395 phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
7396 phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
7397 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7398 "3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
7399 "ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
7400 phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
7401
7402 if (rc != MBX_TIMEOUT)
7403 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04007404
7405 /*
James Smart67d12732012-08-03 12:36:13 -04007406 * Set up HBA Event Queues (EQs)
James Smartda0436e2009-05-22 14:51:39 -04007407 */
7408
James Smart67d12732012-08-03 12:36:13 -04007409 /* Set up HBA event queue */
7410 if (phba->cfg_fcp_io_channel && !phba->sli4_hba.hba_eq) {
James Smart2e90f4b2011-12-13 13:22:37 -05007411 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7412 "3147 Fast-path EQs not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007413 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007414 goto out_error;
James Smart2e90f4b2011-12-13 13:22:37 -05007415 }
James Smart67d12732012-08-03 12:36:13 -04007416 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
7417 if (!phba->sli4_hba.hba_eq[fcp_eqidx]) {
James Smartda0436e2009-05-22 14:51:39 -04007418 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7419 "0522 Fast-path EQ (%d) not "
7420 "allocated\n", fcp_eqidx);
James Smart1b511972011-12-13 13:23:09 -05007421 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007422 goto out_destroy_hba_eq;
James Smartda0436e2009-05-22 14:51:39 -04007423 }
James Smart67d12732012-08-03 12:36:13 -04007424 rc = lpfc_eq_create(phba, phba->sli4_hba.hba_eq[fcp_eqidx],
James Smartbf8dae82012-08-03 12:36:24 -04007425 (phba->cfg_fcp_imax / phba->cfg_fcp_io_channel));
James Smartda0436e2009-05-22 14:51:39 -04007426 if (rc) {
7427 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7428 "0523 Failed setup of fast-path EQ "
James Smarta2fc4aef2014-09-03 12:57:55 -04007429 "(%d), rc = 0x%x\n", fcp_eqidx,
7430 (uint32_t)rc);
James Smart67d12732012-08-03 12:36:13 -04007431 goto out_destroy_hba_eq;
James Smartda0436e2009-05-22 14:51:39 -04007432 }
7433 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04007434 "2584 HBA EQ setup: "
James Smartda0436e2009-05-22 14:51:39 -04007435 "queue[%d]-id=%d\n", fcp_eqidx,
James Smart67d12732012-08-03 12:36:13 -04007436 phba->sli4_hba.hba_eq[fcp_eqidx]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007437 }
7438
James Smart67d12732012-08-03 12:36:13 -04007439 /* Set up fast-path FCP Response Complete Queue */
7440 if (!phba->sli4_hba.fcp_cq) {
7441 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7442 "3148 Fast-path FCP CQ array not "
7443 "allocated\n");
7444 rc = -ENOMEM;
7445 goto out_destroy_hba_eq;
7446 }
7447
7448 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_io_channel; fcp_cqidx++) {
7449 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
7450 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7451 "0526 Fast-path FCP CQ (%d) not "
7452 "allocated\n", fcp_cqidx);
7453 rc = -ENOMEM;
7454 goto out_destroy_fcp_cq;
7455 }
7456 rc = lpfc_cq_create(phba, phba->sli4_hba.fcp_cq[fcp_cqidx],
7457 phba->sli4_hba.hba_eq[fcp_cqidx], LPFC_WCQ, LPFC_FCP);
7458 if (rc) {
7459 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7460 "0527 Failed setup of fast-path FCP "
James Smarta2fc4aef2014-09-03 12:57:55 -04007461 "CQ (%d), rc = 0x%x\n", fcp_cqidx,
7462 (uint32_t)rc);
James Smart67d12732012-08-03 12:36:13 -04007463 goto out_destroy_fcp_cq;
7464 }
7465
7466 /* Setup fcp_cq_map for fast lookup */
7467 phba->sli4_hba.fcp_cq_map[fcp_cqidx] =
7468 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id;
7469
7470 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7471 "2588 FCP CQ setup: cq[%d]-id=%d, "
7472 "parent seq[%d]-id=%d\n",
7473 fcp_cqidx,
7474 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
7475 fcp_cqidx,
7476 phba->sli4_hba.hba_eq[fcp_cqidx]->queue_id);
7477 }
7478
7479 /* Set up fast-path FCP Work Queue */
7480 if (!phba->sli4_hba.fcp_wq) {
7481 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7482 "3149 Fast-path FCP WQ array not "
7483 "allocated\n");
7484 rc = -ENOMEM;
7485 goto out_destroy_fcp_cq;
7486 }
7487
7488 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_io_channel; fcp_wqidx++) {
7489 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
7490 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7491 "0534 Fast-path FCP WQ (%d) not "
7492 "allocated\n", fcp_wqidx);
7493 rc = -ENOMEM;
7494 goto out_destroy_fcp_wq;
7495 }
7496 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
7497 phba->sli4_hba.fcp_cq[fcp_wqidx],
7498 LPFC_FCP);
7499 if (rc) {
7500 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7501 "0535 Failed setup of fast-path FCP "
James Smarta2fc4aef2014-09-03 12:57:55 -04007502 "WQ (%d), rc = 0x%x\n", fcp_wqidx,
7503 (uint32_t)rc);
James Smart67d12732012-08-03 12:36:13 -04007504 goto out_destroy_fcp_wq;
7505 }
7506
7507 /* Bind this WQ to the next FCP ring */
7508 pring = &psli->ring[MAX_SLI3_CONFIGURED_RINGS + fcp_wqidx];
7509 pring->sli.sli4.wqp = (void *)phba->sli4_hba.fcp_wq[fcp_wqidx];
7510 phba->sli4_hba.fcp_cq[fcp_wqidx]->pring = pring;
7511
7512 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7513 "2591 FCP WQ setup: wq[%d]-id=%d, "
7514 "parent cq[%d]-id=%d\n",
7515 fcp_wqidx,
7516 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
7517 fcp_cq_index,
7518 phba->sli4_hba.fcp_cq[fcp_wqidx]->queue_id);
7519 }
James Smartda0436e2009-05-22 14:51:39 -04007520 /*
7521 * Set up Complete Queues (CQs)
7522 */
7523
7524 /* Set up slow-path MBOX Complete Queue as the first CQ */
7525 if (!phba->sli4_hba.mbx_cq) {
7526 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7527 "0528 Mailbox CQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007528 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007529 goto out_destroy_fcp_wq;
James Smartda0436e2009-05-22 14:51:39 -04007530 }
James Smart67d12732012-08-03 12:36:13 -04007531 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq,
7532 phba->sli4_hba.hba_eq[0], LPFC_MCQ, LPFC_MBOX);
James Smartda0436e2009-05-22 14:51:39 -04007533 if (rc) {
7534 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7535 "0529 Failed setup of slow-path mailbox CQ: "
James Smarta2fc4aef2014-09-03 12:57:55 -04007536 "rc = 0x%x\n", (uint32_t)rc);
James Smart67d12732012-08-03 12:36:13 -04007537 goto out_destroy_fcp_wq;
James Smartda0436e2009-05-22 14:51:39 -04007538 }
7539 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7540 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
7541 phba->sli4_hba.mbx_cq->queue_id,
James Smart67d12732012-08-03 12:36:13 -04007542 phba->sli4_hba.hba_eq[0]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007543
7544 /* Set up slow-path ELS Complete Queue */
7545 if (!phba->sli4_hba.els_cq) {
7546 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7547 "0530 ELS CQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007548 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04007549 goto out_destroy_mbx_cq;
7550 }
James Smart67d12732012-08-03 12:36:13 -04007551 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq,
7552 phba->sli4_hba.hba_eq[0], LPFC_WCQ, LPFC_ELS);
James Smartda0436e2009-05-22 14:51:39 -04007553 if (rc) {
7554 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7555 "0531 Failed setup of slow-path ELS CQ: "
James Smarta2fc4aef2014-09-03 12:57:55 -04007556 "rc = 0x%x\n", (uint32_t)rc);
James Smartda0436e2009-05-22 14:51:39 -04007557 goto out_destroy_mbx_cq;
7558 }
7559 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7560 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
7561 phba->sli4_hba.els_cq->queue_id,
James Smart67d12732012-08-03 12:36:13 -04007562 phba->sli4_hba.hba_eq[0]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007563
7564 /*
7565 * Set up all the Work Queues (WQs)
7566 */
7567
7568 /* Set up Mailbox Command Queue */
7569 if (!phba->sli4_hba.mbx_wq) {
7570 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7571 "0538 Slow-path MQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007572 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007573 goto out_destroy_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04007574 }
7575 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
7576 phba->sli4_hba.mbx_cq, LPFC_MBOX);
7577 if (rc) {
7578 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7579 "0539 Failed setup of slow-path MQ: "
7580 "rc = 0x%x\n", rc);
James Smart67d12732012-08-03 12:36:13 -04007581 goto out_destroy_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04007582 }
7583 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7584 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
7585 phba->sli4_hba.mbx_wq->queue_id,
7586 phba->sli4_hba.mbx_cq->queue_id);
7587
7588 /* Set up slow-path ELS Work Queue */
7589 if (!phba->sli4_hba.els_wq) {
7590 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7591 "0536 Slow-path ELS WQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007592 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04007593 goto out_destroy_mbx_wq;
7594 }
7595 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
7596 phba->sli4_hba.els_cq, LPFC_ELS);
7597 if (rc) {
7598 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7599 "0537 Failed setup of slow-path ELS WQ: "
James Smarta2fc4aef2014-09-03 12:57:55 -04007600 "rc = 0x%x\n", (uint32_t)rc);
James Smartda0436e2009-05-22 14:51:39 -04007601 goto out_destroy_mbx_wq;
7602 }
James Smart2a76a282012-08-03 12:35:54 -04007603
7604 /* Bind this WQ to the ELS ring */
7605 pring = &psli->ring[LPFC_ELS_RING];
7606 pring->sli.sli4.wqp = (void *)phba->sli4_hba.els_wq;
7607 phba->sli4_hba.els_cq->pring = pring;
7608
James Smartda0436e2009-05-22 14:51:39 -04007609 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7610 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
7611 phba->sli4_hba.els_wq->queue_id,
7612 phba->sli4_hba.els_cq->queue_id);
7613
James Smartda0436e2009-05-22 14:51:39 -04007614 /*
7615 * Create Receive Queue (RQ)
7616 */
7617 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
7618 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7619 "0540 Receive Queue not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007620 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007621 goto out_destroy_els_wq;
James Smartda0436e2009-05-22 14:51:39 -04007622 }
James Smart73d91e52011-10-10 21:32:10 -04007623
7624 lpfc_rq_adjust_repost(phba, phba->sli4_hba.hdr_rq, LPFC_ELS_HBQ);
7625 lpfc_rq_adjust_repost(phba, phba->sli4_hba.dat_rq, LPFC_ELS_HBQ);
7626
James Smartda0436e2009-05-22 14:51:39 -04007627 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04007628 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04007629 if (rc) {
7630 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7631 "0541 Failed setup of Receive Queue: "
James Smarta2fc4aef2014-09-03 12:57:55 -04007632 "rc = 0x%x\n", (uint32_t)rc);
James Smartda0436e2009-05-22 14:51:39 -04007633 goto out_destroy_fcp_wq;
7634 }
James Smart73d91e52011-10-10 21:32:10 -04007635
James Smartda0436e2009-05-22 14:51:39 -04007636 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7637 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
7638 "parent cq-id=%d\n",
7639 phba->sli4_hba.hdr_rq->queue_id,
7640 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04007641 phba->sli4_hba.els_cq->queue_id);
James Smart1ba981f2014-02-20 09:56:45 -05007642
7643 if (phba->cfg_fof) {
7644 rc = lpfc_fof_queue_setup(phba);
7645 if (rc) {
7646 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7647 "0549 Failed setup of FOF Queues: "
7648 "rc = 0x%x\n", rc);
7649 goto out_destroy_els_rq;
7650 }
7651 }
James Smartda0436e2009-05-22 14:51:39 -04007652 return 0;
7653
James Smart1ba981f2014-02-20 09:56:45 -05007654out_destroy_els_rq:
7655 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
James Smart2e90f4b2011-12-13 13:22:37 -05007656out_destroy_els_wq:
James Smartda0436e2009-05-22 14:51:39 -04007657 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7658out_destroy_mbx_wq:
7659 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
James Smart2e90f4b2011-12-13 13:22:37 -05007660out_destroy_els_cq:
James Smartda0436e2009-05-22 14:51:39 -04007661 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
7662out_destroy_mbx_cq:
7663 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
James Smart67d12732012-08-03 12:36:13 -04007664out_destroy_fcp_wq:
7665 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
7666 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
7667out_destroy_fcp_cq:
7668 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
7669 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
7670out_destroy_hba_eq:
James Smartda0436e2009-05-22 14:51:39 -04007671 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
James Smart67d12732012-08-03 12:36:13 -04007672 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[fcp_eqidx]);
James Smartda0436e2009-05-22 14:51:39 -04007673out_error:
7674 return rc;
7675}
7676
7677/**
7678 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
7679 * @phba: pointer to lpfc hba data structure.
7680 *
7681 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
7682 * operation.
7683 *
7684 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007685 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007686 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007687 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007688 **/
7689void
7690lpfc_sli4_queue_unset(struct lpfc_hba *phba)
7691{
7692 int fcp_qidx;
7693
James Smart1ba981f2014-02-20 09:56:45 -05007694 /* Unset the queues created for Flash Optimized Fabric operations */
7695 if (phba->cfg_fof)
7696 lpfc_fof_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007697 /* Unset mailbox command work queue */
7698 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
7699 /* Unset ELS work queue */
7700 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7701 /* Unset unsolicited receive queue */
7702 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
7703 /* Unset FCP work queue */
James Smart67d12732012-08-03 12:36:13 -04007704 if (phba->sli4_hba.fcp_wq) {
7705 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
7706 fcp_qidx++)
7707 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
7708 }
James Smartda0436e2009-05-22 14:51:39 -04007709 /* Unset mailbox command complete queue */
7710 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
7711 /* Unset ELS complete queue */
7712 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
James Smartda0436e2009-05-22 14:51:39 -04007713 /* Unset FCP response complete queue */
James Smart2e90f4b2011-12-13 13:22:37 -05007714 if (phba->sli4_hba.fcp_cq) {
James Smart67d12732012-08-03 12:36:13 -04007715 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
7716 fcp_qidx++)
James Smart2e90f4b2011-12-13 13:22:37 -05007717 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05007718 }
James Smartda0436e2009-05-22 14:51:39 -04007719 /* Unset fast-path event queue */
James Smart67d12732012-08-03 12:36:13 -04007720 if (phba->sli4_hba.hba_eq) {
7721 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
James Smart2e90f4b2011-12-13 13:22:37 -05007722 fcp_qidx++)
James Smart67d12732012-08-03 12:36:13 -04007723 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[fcp_qidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05007724 }
James Smartda0436e2009-05-22 14:51:39 -04007725}
7726
7727/**
7728 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
7729 * @phba: pointer to lpfc hba data structure.
7730 *
7731 * This routine is invoked to allocate and set up a pool of completion queue
7732 * events. The body of the completion queue event is a completion queue entry
7733 * CQE. For now, this pool is used for the interrupt service routine to queue
7734 * the following HBA completion queue events for the worker thread to process:
7735 * - Mailbox asynchronous events
7736 * - Receive queue completion unsolicited events
7737 * Later, this can be used for all the slow-path events.
7738 *
7739 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007740 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007741 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04007742 **/
7743static int
7744lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
7745{
7746 struct lpfc_cq_event *cq_event;
7747 int i;
7748
7749 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
7750 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
7751 if (!cq_event)
7752 goto out_pool_create_fail;
7753 list_add_tail(&cq_event->list,
7754 &phba->sli4_hba.sp_cqe_event_pool);
7755 }
7756 return 0;
7757
7758out_pool_create_fail:
7759 lpfc_sli4_cq_event_pool_destroy(phba);
7760 return -ENOMEM;
7761}
7762
7763/**
7764 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
7765 * @phba: pointer to lpfc hba data structure.
7766 *
7767 * This routine is invoked to free the pool of completion queue events at
7768 * driver unload time. Note that, it is the responsibility of the driver
7769 * cleanup routine to free all the outstanding completion-queue events
7770 * allocated from this pool back into the pool before invoking this routine
7771 * to destroy the pool.
7772 **/
7773static void
7774lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
7775{
7776 struct lpfc_cq_event *cq_event, *next_cq_event;
7777
7778 list_for_each_entry_safe(cq_event, next_cq_event,
7779 &phba->sli4_hba.sp_cqe_event_pool, list) {
7780 list_del(&cq_event->list);
7781 kfree(cq_event);
7782 }
7783}
7784
7785/**
7786 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
7787 * @phba: pointer to lpfc hba data structure.
7788 *
7789 * This routine is the lock free version of the API invoked to allocate a
7790 * completion-queue event from the free pool.
7791 *
7792 * Return: Pointer to the newly allocated completion-queue event if successful
7793 * NULL otherwise.
7794 **/
7795struct lpfc_cq_event *
7796__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
7797{
7798 struct lpfc_cq_event *cq_event = NULL;
7799
7800 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
7801 struct lpfc_cq_event, list);
7802 return cq_event;
7803}
7804
7805/**
7806 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
7807 * @phba: pointer to lpfc hba data structure.
7808 *
7809 * This routine is the lock version of the API invoked to allocate a
7810 * completion-queue event from the free pool.
7811 *
7812 * Return: Pointer to the newly allocated completion-queue event if successful
7813 * NULL otherwise.
7814 **/
7815struct lpfc_cq_event *
7816lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
7817{
7818 struct lpfc_cq_event *cq_event;
7819 unsigned long iflags;
7820
7821 spin_lock_irqsave(&phba->hbalock, iflags);
7822 cq_event = __lpfc_sli4_cq_event_alloc(phba);
7823 spin_unlock_irqrestore(&phba->hbalock, iflags);
7824 return cq_event;
7825}
7826
7827/**
7828 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
7829 * @phba: pointer to lpfc hba data structure.
7830 * @cq_event: pointer to the completion queue event to be freed.
7831 *
7832 * This routine is the lock free version of the API invoked to release a
7833 * completion-queue event back into the free pool.
7834 **/
7835void
7836__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
7837 struct lpfc_cq_event *cq_event)
7838{
7839 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
7840}
7841
7842/**
7843 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
7844 * @phba: pointer to lpfc hba data structure.
7845 * @cq_event: pointer to the completion queue event to be freed.
7846 *
7847 * This routine is the lock version of the API invoked to release a
7848 * completion-queue event back into the free pool.
7849 **/
7850void
7851lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
7852 struct lpfc_cq_event *cq_event)
7853{
7854 unsigned long iflags;
7855 spin_lock_irqsave(&phba->hbalock, iflags);
7856 __lpfc_sli4_cq_event_release(phba, cq_event);
7857 spin_unlock_irqrestore(&phba->hbalock, iflags);
7858}
7859
7860/**
7861 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
7862 * @phba: pointer to lpfc hba data structure.
7863 *
7864 * This routine is to free all the pending completion-queue events to the
7865 * back into the free pool for device reset.
7866 **/
7867static void
7868lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
7869{
7870 LIST_HEAD(cqelist);
7871 struct lpfc_cq_event *cqe;
7872 unsigned long iflags;
7873
7874 /* Retrieve all the pending WCQEs from pending WCQE lists */
7875 spin_lock_irqsave(&phba->hbalock, iflags);
7876 /* Pending FCP XRI abort events */
7877 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
7878 &cqelist);
7879 /* Pending ELS XRI abort events */
7880 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
7881 &cqelist);
7882 /* Pending asynnc events */
7883 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
7884 &cqelist);
7885 spin_unlock_irqrestore(&phba->hbalock, iflags);
7886
7887 while (!list_empty(&cqelist)) {
7888 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
7889 lpfc_sli4_cq_event_release(phba, cqe);
7890 }
7891}
7892
7893/**
7894 * lpfc_pci_function_reset - Reset pci function.
7895 * @phba: pointer to lpfc hba data structure.
7896 *
7897 * This routine is invoked to request a PCI function reset. It will destroys
7898 * all resources assigned to the PCI function which originates this request.
7899 *
7900 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007901 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007902 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007903 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007904 **/
7905int
7906lpfc_pci_function_reset(struct lpfc_hba *phba)
7907{
7908 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05007909 uint32_t rc = 0, if_type;
James Smartda0436e2009-05-22 14:51:39 -04007910 uint32_t shdr_status, shdr_add_status;
James Smart2f6fa2c2014-09-03 12:57:08 -04007911 uint32_t rdy_chk;
7912 uint32_t port_reset = 0;
James Smartda0436e2009-05-22 14:51:39 -04007913 union lpfc_sli4_cfg_shdr *shdr;
James Smart2fcee4b2010-12-15 17:57:46 -05007914 struct lpfc_register reg_data;
James Smart2b81f942012-03-01 22:37:18 -05007915 uint16_t devid;
James Smartda0436e2009-05-22 14:51:39 -04007916
James Smart2fcee4b2010-12-15 17:57:46 -05007917 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7918 switch (if_type) {
7919 case LPFC_SLI_INTF_IF_TYPE_0:
7920 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
7921 GFP_KERNEL);
7922 if (!mboxq) {
7923 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7924 "0494 Unable to allocate memory for "
7925 "issuing SLI_FUNCTION_RESET mailbox "
7926 "command\n");
7927 return -ENOMEM;
7928 }
7929
7930 /* Setup PCI function reset mailbox-ioctl command */
7931 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7932 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
7933 LPFC_SLI4_MBX_EMBED);
7934 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7935 shdr = (union lpfc_sli4_cfg_shdr *)
7936 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7937 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7938 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
7939 &shdr->response);
7940 if (rc != MBX_TIMEOUT)
7941 mempool_free(mboxq, phba->mbox_mem_pool);
7942 if (shdr_status || shdr_add_status || rc) {
7943 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7944 "0495 SLI_FUNCTION_RESET mailbox "
7945 "failed with status x%x add_status x%x,"
7946 " mbx status x%x\n",
7947 shdr_status, shdr_add_status, rc);
7948 rc = -ENXIO;
7949 }
7950 break;
7951 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart2f6fa2c2014-09-03 12:57:08 -04007952wait:
7953 /*
7954 * Poll the Port Status Register and wait for RDY for
7955 * up to 30 seconds. If the port doesn't respond, treat
7956 * it as an error.
7957 */
7958 for (rdy_chk = 0; rdy_chk < 3000; rdy_chk++) {
7959 if (lpfc_readl(phba->sli4_hba.u.if_type2.
7960 STATUSregaddr, &reg_data.word0)) {
7961 rc = -ENODEV;
7962 goto out;
7963 }
7964 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
7965 break;
7966 msleep(20);
7967 }
7968
7969 if (!bf_get(lpfc_sliport_status_rdy, &reg_data)) {
7970 phba->work_status[0] = readl(
7971 phba->sli4_hba.u.if_type2.ERR1regaddr);
7972 phba->work_status[1] = readl(
7973 phba->sli4_hba.u.if_type2.ERR2regaddr);
7974 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7975 "2890 Port not ready, port status reg "
7976 "0x%x error 1=0x%x, error 2=0x%x\n",
7977 reg_data.word0,
7978 phba->work_status[0],
7979 phba->work_status[1]);
7980 rc = -ENODEV;
7981 goto out;
7982 }
7983
7984 if (!port_reset) {
7985 /*
7986 * Reset the port now
7987 */
James Smart2fcee4b2010-12-15 17:57:46 -05007988 reg_data.word0 = 0;
7989 bf_set(lpfc_sliport_ctrl_end, &reg_data,
7990 LPFC_SLIPORT_LITTLE_ENDIAN);
7991 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
7992 LPFC_SLIPORT_INIT_PORT);
7993 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
7994 CTRLregaddr);
James Smart8fcb8ac2012-03-01 22:35:58 -05007995 /* flush */
James Smart2b81f942012-03-01 22:37:18 -05007996 pci_read_config_word(phba->pcidev,
7997 PCI_DEVICE_ID, &devid);
James Smart2fcee4b2010-12-15 17:57:46 -05007998
James Smart2f6fa2c2014-09-03 12:57:08 -04007999 port_reset = 1;
8000 msleep(20);
8001 goto wait;
8002 } else if (bf_get(lpfc_sliport_status_rn, &reg_data)) {
8003 rc = -ENODEV;
8004 goto out;
James Smart2fcee4b2010-12-15 17:57:46 -05008005 }
8006 break;
James Smart2f6fa2c2014-09-03 12:57:08 -04008007
James Smart2fcee4b2010-12-15 17:57:46 -05008008 case LPFC_SLI_INTF_IF_TYPE_1:
8009 default:
8010 break;
James Smartda0436e2009-05-22 14:51:39 -04008011 }
8012
James Smart73d91e52011-10-10 21:32:10 -04008013out:
James Smart2fcee4b2010-12-15 17:57:46 -05008014 /* Catch the not-ready port failure after a port reset. */
James Smart2f6fa2c2014-09-03 12:57:08 -04008015 if (rc) {
James Smart229adb02013-04-17 20:16:51 -04008016 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8017 "3317 HBA not functional: IP Reset Failed "
James Smart2f6fa2c2014-09-03 12:57:08 -04008018 "try: echo fw_reset > board_mode\n");
James Smart2fcee4b2010-12-15 17:57:46 -05008019 rc = -ENODEV;
James Smart229adb02013-04-17 20:16:51 -04008020 }
James Smart2fcee4b2010-12-15 17:57:46 -05008021
James Smartda0436e2009-05-22 14:51:39 -04008022 return rc;
8023}
8024
8025/**
James Smartda0436e2009-05-22 14:51:39 -04008026 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
8027 * @phba: pointer to lpfc hba data structure.
8028 *
8029 * This routine is invoked to set up the PCI device memory space for device
8030 * with SLI-4 interface spec.
8031 *
8032 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008033 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008034 * other values - error
8035 **/
8036static int
8037lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
8038{
8039 struct pci_dev *pdev;
8040 unsigned long bar0map_len, bar1map_len, bar2map_len;
8041 int error = -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -05008042 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04008043
8044 /* Obtain PCI device reference */
8045 if (!phba->pcidev)
8046 return error;
8047 else
8048 pdev = phba->pcidev;
8049
8050 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05008051 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
8052 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
8053 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
8054 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smartda0436e2009-05-22 14:51:39 -04008055 return error;
Michael Reed8e685972009-09-18 12:02:05 -05008056 }
8057 }
James Smartda0436e2009-05-22 14:51:39 -04008058
James Smart2fcee4b2010-12-15 17:57:46 -05008059 /*
8060 * The BARs and register set definitions and offset locations are
8061 * dependent on the if_type.
8062 */
8063 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
8064 &phba->sli4_hba.sli_intf.word0)) {
8065 return error;
8066 }
8067
8068 /* There is no SLI3 failback for SLI4 devices. */
8069 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
8070 LPFC_SLI_INTF_VALID) {
8071 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8072 "2894 SLI_INTF reg contents invalid "
8073 "sli_intf reg 0x%x\n",
8074 phba->sli4_hba.sli_intf.word0);
8075 return error;
8076 }
8077
8078 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
8079 /*
8080 * Get the bus address of SLI4 device Bar regions and the
8081 * number of bytes required by each mapping. The mapping of the
8082 * particular PCI BARs regions is dependent on the type of
8083 * SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -04008084 */
James Smartf5ca6f22013-09-06 12:21:09 -04008085 if (pci_resource_start(pdev, PCI_64BIT_BAR0)) {
8086 phba->pci_bar0_map = pci_resource_start(pdev, PCI_64BIT_BAR0);
8087 bar0map_len = pci_resource_len(pdev, PCI_64BIT_BAR0);
James Smart2fcee4b2010-12-15 17:57:46 -05008088
8089 /*
8090 * Map SLI4 PCI Config Space Register base to a kernel virtual
8091 * addr
8092 */
8093 phba->sli4_hba.conf_regs_memmap_p =
8094 ioremap(phba->pci_bar0_map, bar0map_len);
8095 if (!phba->sli4_hba.conf_regs_memmap_p) {
8096 dev_printk(KERN_ERR, &pdev->dev,
8097 "ioremap failed for SLI4 PCI config "
8098 "registers.\n");
8099 goto out;
8100 }
James Smartf5ca6f22013-09-06 12:21:09 -04008101 phba->pci_bar0_memmap_p = phba->sli4_hba.conf_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05008102 /* Set up BAR0 PCI config space register memory map */
8103 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smart1dfb5a42010-02-12 14:40:50 -05008104 } else {
8105 phba->pci_bar0_map = pci_resource_start(pdev, 1);
8106 bar0map_len = pci_resource_len(pdev, 1);
James Smart2fcee4b2010-12-15 17:57:46 -05008107 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8108 dev_printk(KERN_ERR, &pdev->dev,
8109 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
8110 goto out;
8111 }
8112 phba->sli4_hba.conf_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04008113 ioremap(phba->pci_bar0_map, bar0map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05008114 if (!phba->sli4_hba.conf_regs_memmap_p) {
8115 dev_printk(KERN_ERR, &pdev->dev,
8116 "ioremap failed for SLI4 PCI config "
8117 "registers.\n");
8118 goto out;
8119 }
8120 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smartda0436e2009-05-22 14:51:39 -04008121 }
8122
James Smartc31098c2011-04-16 11:03:33 -04008123 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
James Smartf5ca6f22013-09-06 12:21:09 -04008124 (pci_resource_start(pdev, PCI_64BIT_BAR2))) {
James Smart2fcee4b2010-12-15 17:57:46 -05008125 /*
8126 * Map SLI4 if type 0 HBA Control Register base to a kernel
8127 * virtual address and setup the registers.
8128 */
James Smartf5ca6f22013-09-06 12:21:09 -04008129 phba->pci_bar1_map = pci_resource_start(pdev, PCI_64BIT_BAR2);
8130 bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
James Smart2fcee4b2010-12-15 17:57:46 -05008131 phba->sli4_hba.ctrl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04008132 ioremap(phba->pci_bar1_map, bar1map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05008133 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
8134 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04008135 "ioremap failed for SLI4 HBA control registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05008136 goto out_iounmap_conf;
8137 }
James Smartf5ca6f22013-09-06 12:21:09 -04008138 phba->pci_bar2_memmap_p = phba->sli4_hba.ctrl_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05008139 lpfc_sli4_bar1_register_memmap(phba);
James Smartda0436e2009-05-22 14:51:39 -04008140 }
8141
James Smartc31098c2011-04-16 11:03:33 -04008142 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
James Smartf5ca6f22013-09-06 12:21:09 -04008143 (pci_resource_start(pdev, PCI_64BIT_BAR4))) {
James Smart2fcee4b2010-12-15 17:57:46 -05008144 /*
8145 * Map SLI4 if type 0 HBA Doorbell Register base to a kernel
8146 * virtual address and setup the registers.
8147 */
James Smartf5ca6f22013-09-06 12:21:09 -04008148 phba->pci_bar2_map = pci_resource_start(pdev, PCI_64BIT_BAR4);
8149 bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
James Smart2fcee4b2010-12-15 17:57:46 -05008150 phba->sli4_hba.drbl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04008151 ioremap(phba->pci_bar2_map, bar2map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05008152 if (!phba->sli4_hba.drbl_regs_memmap_p) {
8153 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04008154 "ioremap failed for SLI4 HBA doorbell registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05008155 goto out_iounmap_ctrl;
8156 }
James Smartf5ca6f22013-09-06 12:21:09 -04008157 phba->pci_bar4_memmap_p = phba->sli4_hba.drbl_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05008158 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
8159 if (error)
8160 goto out_iounmap_all;
James Smartda0436e2009-05-22 14:51:39 -04008161 }
8162
James Smartda0436e2009-05-22 14:51:39 -04008163 return 0;
8164
8165out_iounmap_all:
8166 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
8167out_iounmap_ctrl:
8168 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
8169out_iounmap_conf:
8170 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8171out:
8172 return error;
8173}
8174
8175/**
8176 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
8177 * @phba: pointer to lpfc hba data structure.
8178 *
8179 * This routine is invoked to unset the PCI device memory space for device
8180 * with SLI-4 interface spec.
8181 **/
8182static void
8183lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
8184{
James Smart2e90f4b2011-12-13 13:22:37 -05008185 uint32_t if_type;
8186 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smartda0436e2009-05-22 14:51:39 -04008187
James Smart2e90f4b2011-12-13 13:22:37 -05008188 switch (if_type) {
8189 case LPFC_SLI_INTF_IF_TYPE_0:
8190 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
8191 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
8192 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8193 break;
8194 case LPFC_SLI_INTF_IF_TYPE_2:
8195 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8196 break;
8197 case LPFC_SLI_INTF_IF_TYPE_1:
8198 default:
8199 dev_printk(KERN_ERR, &phba->pcidev->dev,
8200 "FATAL - unsupported SLI4 interface type - %d\n",
8201 if_type);
8202 break;
8203 }
James Smartda0436e2009-05-22 14:51:39 -04008204}
8205
8206/**
James Smart3772a992009-05-22 14:50:54 -04008207 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
8208 * @phba: pointer to lpfc hba data structure.
8209 *
8210 * This routine is invoked to enable the MSI-X interrupt vectors to device
Alexander Gordeev4f871e12014-09-03 12:56:29 -04008211 * with SLI-3 interface specs. The kernel function pci_enable_msix_exact()
8212 * is called to enable the MSI-X vectors. Note that pci_enable_msix_exact(),
8213 * once invoked, enables either all or nothing, depending on the current
James Smart3772a992009-05-22 14:50:54 -04008214 * availability of PCI vector resources. The device driver is responsible
8215 * for calling the individual request_irq() to register each MSI-X vector
8216 * with a interrupt handler, which is done in this function. Note that
8217 * later when device is unloading, the driver should always call free_irq()
8218 * on all MSI-X vectors it has done request_irq() on before calling
8219 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
8220 * will be left with MSI-X enabled and leaks its vectors.
8221 *
8222 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008223 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008224 * other values - error
8225 **/
8226static int
8227lpfc_sli_enable_msix(struct lpfc_hba *phba)
8228{
8229 int rc, i;
8230 LPFC_MBOXQ_t *pmb;
8231
8232 /* Set up MSI-X multi-message vectors */
8233 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8234 phba->msix_entries[i].entry = i;
8235
8236 /* Configure MSI-X capability structure */
Alexander Gordeev4f871e12014-09-03 12:56:29 -04008237 rc = pci_enable_msix_exact(phba->pcidev, phba->msix_entries,
8238 LPFC_MSIX_VECTORS);
James Smart3772a992009-05-22 14:50:54 -04008239 if (rc) {
8240 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8241 "0420 PCI enable MSI-X failed (%d)\n", rc);
Alexander Gordeev029165a2014-07-16 20:05:15 +02008242 goto vec_fail_out;
James Smart3772a992009-05-22 14:50:54 -04008243 }
8244 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8245 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8246 "0477 MSI-X entry[%d]: vector=x%x "
8247 "message=%d\n", i,
8248 phba->msix_entries[i].vector,
8249 phba->msix_entries[i].entry);
8250 /*
8251 * Assign MSI-X vectors to interrupt handlers
8252 */
8253
8254 /* vector-0 is associated to slow-path handler */
8255 rc = request_irq(phba->msix_entries[0].vector,
8256 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
8257 LPFC_SP_DRIVER_HANDLER_NAME, phba);
8258 if (rc) {
8259 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8260 "0421 MSI-X slow-path request_irq failed "
8261 "(%d)\n", rc);
8262 goto msi_fail_out;
8263 }
8264
8265 /* vector-1 is associated to fast-path handler */
8266 rc = request_irq(phba->msix_entries[1].vector,
8267 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
8268 LPFC_FP_DRIVER_HANDLER_NAME, phba);
8269
8270 if (rc) {
8271 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8272 "0429 MSI-X fast-path request_irq failed "
8273 "(%d)\n", rc);
8274 goto irq_fail_out;
8275 }
8276
8277 /*
8278 * Configure HBA MSI-X attention conditions to messages
8279 */
8280 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8281
8282 if (!pmb) {
8283 rc = -ENOMEM;
8284 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8285 "0474 Unable to allocate memory for issuing "
8286 "MBOX_CONFIG_MSI command\n");
8287 goto mem_fail_out;
8288 }
8289 rc = lpfc_config_msi(phba, pmb);
8290 if (rc)
8291 goto mbx_fail_out;
8292 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
8293 if (rc != MBX_SUCCESS) {
8294 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
8295 "0351 Config MSI mailbox command failed, "
8296 "mbxCmd x%x, mbxStatus x%x\n",
8297 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
8298 goto mbx_fail_out;
8299 }
8300
8301 /* Free memory allocated for mailbox command */
8302 mempool_free(pmb, phba->mbox_mem_pool);
8303 return rc;
8304
8305mbx_fail_out:
8306 /* Free memory allocated for mailbox command */
8307 mempool_free(pmb, phba->mbox_mem_pool);
8308
8309mem_fail_out:
8310 /* free the irq already requested */
8311 free_irq(phba->msix_entries[1].vector, phba);
8312
8313irq_fail_out:
8314 /* free the irq already requested */
8315 free_irq(phba->msix_entries[0].vector, phba);
8316
8317msi_fail_out:
8318 /* Unconfigure MSI-X capability structure */
8319 pci_disable_msix(phba->pcidev);
Alexander Gordeev029165a2014-07-16 20:05:15 +02008320
8321vec_fail_out:
James Smart3772a992009-05-22 14:50:54 -04008322 return rc;
8323}
8324
8325/**
8326 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
8327 * @phba: pointer to lpfc hba data structure.
8328 *
8329 * This routine is invoked to release the MSI-X vectors and then disable the
8330 * MSI-X interrupt mode to device with SLI-3 interface spec.
8331 **/
8332static void
8333lpfc_sli_disable_msix(struct lpfc_hba *phba)
8334{
8335 int i;
8336
8337 /* Free up MSI-X multi-message vectors */
8338 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8339 free_irq(phba->msix_entries[i].vector, phba);
8340 /* Disable MSI-X */
8341 pci_disable_msix(phba->pcidev);
8342
8343 return;
8344}
8345
8346/**
8347 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
8348 * @phba: pointer to lpfc hba data structure.
8349 *
8350 * This routine is invoked to enable the MSI interrupt mode to device with
8351 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
8352 * enable the MSI vector. The device driver is responsible for calling the
8353 * request_irq() to register MSI vector with a interrupt the handler, which
8354 * is done in this function.
8355 *
8356 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008357 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008358 * other values - error
8359 */
8360static int
8361lpfc_sli_enable_msi(struct lpfc_hba *phba)
8362{
8363 int rc;
8364
8365 rc = pci_enable_msi(phba->pcidev);
8366 if (!rc)
8367 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8368 "0462 PCI enable MSI mode success.\n");
8369 else {
8370 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8371 "0471 PCI enable MSI mode failed (%d)\n", rc);
8372 return rc;
8373 }
8374
8375 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
8376 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8377 if (rc) {
8378 pci_disable_msi(phba->pcidev);
8379 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8380 "0478 MSI request_irq failed (%d)\n", rc);
8381 }
8382 return rc;
8383}
8384
8385/**
8386 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
8387 * @phba: pointer to lpfc hba data structure.
8388 *
8389 * This routine is invoked to disable the MSI interrupt mode to device with
8390 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
8391 * done request_irq() on before calling pci_disable_msi(). Failure to do so
8392 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
8393 * its vector.
8394 */
8395static void
8396lpfc_sli_disable_msi(struct lpfc_hba *phba)
8397{
8398 free_irq(phba->pcidev->irq, phba);
8399 pci_disable_msi(phba->pcidev);
8400 return;
8401}
8402
8403/**
8404 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
8405 * @phba: pointer to lpfc hba data structure.
8406 *
8407 * This routine is invoked to enable device interrupt and associate driver's
8408 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
8409 * spec. Depends on the interrupt mode configured to the driver, the driver
8410 * will try to fallback from the configured interrupt mode to an interrupt
8411 * mode which is supported by the platform, kernel, and device in the order
8412 * of:
8413 * MSI-X -> MSI -> IRQ.
8414 *
8415 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008416 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008417 * other values - error
8418 **/
8419static uint32_t
8420lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
8421{
8422 uint32_t intr_mode = LPFC_INTR_ERROR;
8423 int retval;
8424
8425 if (cfg_mode == 2) {
8426 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
8427 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
8428 if (!retval) {
8429 /* Now, try to enable MSI-X interrupt mode */
8430 retval = lpfc_sli_enable_msix(phba);
8431 if (!retval) {
8432 /* Indicate initialization to MSI-X mode */
8433 phba->intr_type = MSIX;
8434 intr_mode = 2;
8435 }
8436 }
8437 }
8438
8439 /* Fallback to MSI if MSI-X initialization failed */
8440 if (cfg_mode >= 1 && phba->intr_type == NONE) {
8441 retval = lpfc_sli_enable_msi(phba);
8442 if (!retval) {
8443 /* Indicate initialization to MSI mode */
8444 phba->intr_type = MSI;
8445 intr_mode = 1;
8446 }
8447 }
8448
8449 /* Fallback to INTx if both MSI-X/MSI initalization failed */
8450 if (phba->intr_type == NONE) {
8451 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
8452 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8453 if (!retval) {
8454 /* Indicate initialization to INTx mode */
8455 phba->intr_type = INTx;
8456 intr_mode = 0;
8457 }
8458 }
8459 return intr_mode;
8460}
8461
8462/**
8463 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
8464 * @phba: pointer to lpfc hba data structure.
8465 *
8466 * This routine is invoked to disable device interrupt and disassociate the
8467 * driver's interrupt handler(s) from interrupt vector(s) to device with
8468 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
8469 * release the interrupt vector(s) for the message signaled interrupt.
8470 **/
8471static void
8472lpfc_sli_disable_intr(struct lpfc_hba *phba)
8473{
8474 /* Disable the currently initialized interrupt mode */
8475 if (phba->intr_type == MSIX)
8476 lpfc_sli_disable_msix(phba);
8477 else if (phba->intr_type == MSI)
8478 lpfc_sli_disable_msi(phba);
8479 else if (phba->intr_type == INTx)
8480 free_irq(phba->pcidev->irq, phba);
8481
8482 /* Reset interrupt management states */
8483 phba->intr_type = NONE;
8484 phba->sli.slistat.sli_intr = 0;
8485
8486 return;
8487}
8488
8489/**
James Smart7bb03bb2013-04-17 20:19:16 -04008490 * lpfc_find_next_cpu - Find next available CPU that matches the phys_id
8491 * @phba: pointer to lpfc hba data structure.
8492 *
8493 * Find next available CPU to use for IRQ to CPU affinity.
8494 */
8495static int
8496lpfc_find_next_cpu(struct lpfc_hba *phba, uint32_t phys_id)
8497{
8498 struct lpfc_vector_map_info *cpup;
8499 int cpu;
8500
8501 cpup = phba->sli4_hba.cpu_map;
8502 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8503 /* CPU must be online */
8504 if (cpu_online(cpu)) {
8505 if ((cpup->irq == LPFC_VECTOR_MAP_EMPTY) &&
8506 (lpfc_used_cpu[cpu] == LPFC_VECTOR_MAP_EMPTY) &&
8507 (cpup->phys_id == phys_id)) {
8508 return cpu;
8509 }
8510 }
8511 cpup++;
8512 }
8513
8514 /*
8515 * If we get here, we have used ALL CPUs for the specific
8516 * phys_id. Now we need to clear out lpfc_used_cpu and start
8517 * reusing CPUs.
8518 */
8519
8520 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8521 if (lpfc_used_cpu[cpu] == phys_id)
8522 lpfc_used_cpu[cpu] = LPFC_VECTOR_MAP_EMPTY;
8523 }
8524
8525 cpup = phba->sli4_hba.cpu_map;
8526 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8527 /* CPU must be online */
8528 if (cpu_online(cpu)) {
8529 if ((cpup->irq == LPFC_VECTOR_MAP_EMPTY) &&
8530 (cpup->phys_id == phys_id)) {
8531 return cpu;
8532 }
8533 }
8534 cpup++;
8535 }
8536 return LPFC_VECTOR_MAP_EMPTY;
8537}
8538
8539/**
8540 * lpfc_sli4_set_affinity - Set affinity for HBA IRQ vectors
8541 * @phba: pointer to lpfc hba data structure.
8542 * @vectors: number of HBA vectors
8543 *
8544 * Affinitize MSIX IRQ vectors to CPUs. Try to equally spread vector
8545 * affinization across multple physical CPUs (numa nodes).
8546 * In addition, this routine will assign an IO channel for each CPU
8547 * to use when issuing I/Os.
8548 */
8549static int
8550lpfc_sli4_set_affinity(struct lpfc_hba *phba, int vectors)
8551{
8552 int i, idx, saved_chann, used_chann, cpu, phys_id;
James Smartec2087a2013-09-06 12:20:36 -04008553 int max_phys_id, min_phys_id;
8554 int num_io_channel, first_cpu, chan;
James Smart7bb03bb2013-04-17 20:19:16 -04008555 struct lpfc_vector_map_info *cpup;
8556#ifdef CONFIG_X86
8557 struct cpuinfo_x86 *cpuinfo;
8558#endif
8559 struct cpumask *mask;
8560 uint8_t chann[LPFC_FCP_IO_CHAN_MAX+1];
8561
8562 /* If there is no mapping, just return */
8563 if (!phba->cfg_fcp_cpu_map)
8564 return 1;
8565
8566 /* Init cpu_map array */
8567 memset(phba->sli4_hba.cpu_map, 0xff,
8568 (sizeof(struct lpfc_vector_map_info) *
8569 phba->sli4_hba.num_present_cpu));
8570
8571 max_phys_id = 0;
James Smartec2087a2013-09-06 12:20:36 -04008572 min_phys_id = 0xff;
James Smart7bb03bb2013-04-17 20:19:16 -04008573 phys_id = 0;
8574 num_io_channel = 0;
8575 first_cpu = LPFC_VECTOR_MAP_EMPTY;
8576
8577 /* Update CPU map with physical id and core id of each CPU */
8578 cpup = phba->sli4_hba.cpu_map;
8579 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8580#ifdef CONFIG_X86
8581 cpuinfo = &cpu_data(cpu);
8582 cpup->phys_id = cpuinfo->phys_proc_id;
8583 cpup->core_id = cpuinfo->cpu_core_id;
8584#else
8585 /* No distinction between CPUs for other platforms */
8586 cpup->phys_id = 0;
8587 cpup->core_id = 0;
8588#endif
8589
8590 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8591 "3328 CPU physid %d coreid %d\n",
8592 cpup->phys_id, cpup->core_id);
8593
8594 if (cpup->phys_id > max_phys_id)
8595 max_phys_id = cpup->phys_id;
James Smartec2087a2013-09-06 12:20:36 -04008596 if (cpup->phys_id < min_phys_id)
8597 min_phys_id = cpup->phys_id;
James Smart7bb03bb2013-04-17 20:19:16 -04008598 cpup++;
8599 }
8600
James Smartec2087a2013-09-06 12:20:36 -04008601 phys_id = min_phys_id;
James Smart7bb03bb2013-04-17 20:19:16 -04008602 /* Now associate the HBA vectors with specific CPUs */
8603 for (idx = 0; idx < vectors; idx++) {
8604 cpup = phba->sli4_hba.cpu_map;
8605 cpu = lpfc_find_next_cpu(phba, phys_id);
8606 if (cpu == LPFC_VECTOR_MAP_EMPTY) {
8607
8608 /* Try for all phys_id's */
8609 for (i = 1; i < max_phys_id; i++) {
8610 phys_id++;
8611 if (phys_id > max_phys_id)
James Smartec2087a2013-09-06 12:20:36 -04008612 phys_id = min_phys_id;
James Smart7bb03bb2013-04-17 20:19:16 -04008613 cpu = lpfc_find_next_cpu(phba, phys_id);
8614 if (cpu == LPFC_VECTOR_MAP_EMPTY)
8615 continue;
8616 goto found;
8617 }
8618
James Smartec2087a2013-09-06 12:20:36 -04008619 /* Use round robin for scheduling */
8620 phba->cfg_fcp_io_sched = LPFC_FCP_SCHED_ROUND_ROBIN;
8621 chan = 0;
8622 cpup = phba->sli4_hba.cpu_map;
8623 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
8624 cpup->channel_id = chan;
8625 cpup++;
8626 chan++;
8627 if (chan >= phba->cfg_fcp_io_channel)
8628 chan = 0;
8629 }
8630
James Smart7bb03bb2013-04-17 20:19:16 -04008631 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8632 "3329 Cannot set affinity:"
8633 "Error mapping vector %d (%d)\n",
8634 idx, vectors);
8635 return 0;
8636 }
8637found:
8638 cpup += cpu;
8639 if (phba->cfg_fcp_cpu_map == LPFC_DRIVER_CPU_MAP)
8640 lpfc_used_cpu[cpu] = phys_id;
8641
8642 /* Associate vector with selected CPU */
8643 cpup->irq = phba->sli4_hba.msix_entries[idx].vector;
8644
8645 /* Associate IO channel with selected CPU */
8646 cpup->channel_id = idx;
8647 num_io_channel++;
8648
8649 if (first_cpu == LPFC_VECTOR_MAP_EMPTY)
8650 first_cpu = cpu;
8651
8652 /* Now affinitize to the selected CPU */
8653 mask = &cpup->maskbits;
8654 cpumask_clear(mask);
8655 cpumask_set_cpu(cpu, mask);
8656 i = irq_set_affinity_hint(phba->sli4_hba.msix_entries[idx].
8657 vector, mask);
8658
8659 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8660 "3330 Set Affinity: CPU %d channel %d "
8661 "irq %d (%x)\n",
8662 cpu, cpup->channel_id,
8663 phba->sli4_hba.msix_entries[idx].vector, i);
8664
8665 /* Spread vector mapping across multple physical CPU nodes */
8666 phys_id++;
8667 if (phys_id > max_phys_id)
James Smartec2087a2013-09-06 12:20:36 -04008668 phys_id = min_phys_id;
James Smart7bb03bb2013-04-17 20:19:16 -04008669 }
8670
8671 /*
8672 * Finally fill in the IO channel for any remaining CPUs.
8673 * At this point, all IO channels have been assigned to a specific
8674 * MSIx vector, mapped to a specific CPU.
8675 * Base the remaining IO channel assigned, to IO channels already
8676 * assigned to other CPUs on the same phys_id.
8677 */
James Smartec2087a2013-09-06 12:20:36 -04008678 for (i = min_phys_id; i <= max_phys_id; i++) {
James Smart7bb03bb2013-04-17 20:19:16 -04008679 /*
8680 * If there are no io channels already mapped to
8681 * this phys_id, just round robin thru the io_channels.
8682 * Setup chann[] for round robin.
8683 */
8684 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++)
8685 chann[idx] = idx;
8686
8687 saved_chann = 0;
8688 used_chann = 0;
8689
8690 /*
8691 * First build a list of IO channels already assigned
8692 * to this phys_id before reassigning the same IO
8693 * channels to the remaining CPUs.
8694 */
8695 cpup = phba->sli4_hba.cpu_map;
8696 cpu = first_cpu;
8697 cpup += cpu;
8698 for (idx = 0; idx < phba->sli4_hba.num_present_cpu;
8699 idx++) {
8700 if (cpup->phys_id == i) {
8701 /*
8702 * Save any IO channels that are
8703 * already mapped to this phys_id.
8704 */
8705 if (cpup->irq != LPFC_VECTOR_MAP_EMPTY) {
8706 chann[saved_chann] =
8707 cpup->channel_id;
8708 saved_chann++;
8709 goto out;
8710 }
8711
8712 /* See if we are using round-robin */
8713 if (saved_chann == 0)
8714 saved_chann =
8715 phba->cfg_fcp_io_channel;
8716
8717 /* Associate next IO channel with CPU */
8718 cpup->channel_id = chann[used_chann];
8719 num_io_channel++;
8720 used_chann++;
8721 if (used_chann == saved_chann)
8722 used_chann = 0;
8723
8724 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8725 "3331 Set IO_CHANN "
8726 "CPU %d channel %d\n",
8727 idx, cpup->channel_id);
8728 }
8729out:
8730 cpu++;
8731 if (cpu >= phba->sli4_hba.num_present_cpu) {
8732 cpup = phba->sli4_hba.cpu_map;
8733 cpu = 0;
8734 } else {
8735 cpup++;
8736 }
8737 }
8738 }
8739
8740 if (phba->sli4_hba.num_online_cpu != phba->sli4_hba.num_present_cpu) {
8741 cpup = phba->sli4_hba.cpu_map;
8742 for (idx = 0; idx < phba->sli4_hba.num_present_cpu; idx++) {
8743 if (cpup->channel_id == LPFC_VECTOR_MAP_EMPTY) {
8744 cpup->channel_id = 0;
8745 num_io_channel++;
8746
8747 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8748 "3332 Assign IO_CHANN "
8749 "CPU %d channel %d\n",
8750 idx, cpup->channel_id);
8751 }
8752 cpup++;
8753 }
8754 }
8755
8756 /* Sanity check */
8757 if (num_io_channel != phba->sli4_hba.num_present_cpu)
8758 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8759 "3333 Set affinity mismatch:"
James Smartec2087a2013-09-06 12:20:36 -04008760 "%d chann != %d cpus: %d vectors\n",
James Smart7bb03bb2013-04-17 20:19:16 -04008761 num_io_channel, phba->sli4_hba.num_present_cpu,
8762 vectors);
8763
James Smartec2087a2013-09-06 12:20:36 -04008764 /* Enable using cpu affinity for scheduling */
James Smart7bb03bb2013-04-17 20:19:16 -04008765 phba->cfg_fcp_io_sched = LPFC_FCP_SCHED_BY_CPU;
8766 return 1;
8767}
8768
8769
8770/**
James Smartda0436e2009-05-22 14:51:39 -04008771 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
8772 * @phba: pointer to lpfc hba data structure.
8773 *
8774 * This routine is invoked to enable the MSI-X interrupt vectors to device
Alexander Gordeev4f871e12014-09-03 12:56:29 -04008775 * with SLI-4 interface spec. The kernel function pci_enable_msix_range()
8776 * is called to enable the MSI-X vectors. The device driver is responsible
8777 * for calling the individual request_irq() to register each MSI-X vector
8778 * with a interrupt handler, which is done in this function. Note that
8779 * later when device is unloading, the driver should always call free_irq()
8780 * on all MSI-X vectors it has done request_irq() on before calling
8781 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
8782 * will be left with MSI-X enabled and leaks its vectors.
James Smartda0436e2009-05-22 14:51:39 -04008783 *
8784 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008785 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008786 * other values - error
8787 **/
8788static int
8789lpfc_sli4_enable_msix(struct lpfc_hba *phba)
8790{
James Smart75baf692010-06-08 18:31:21 -04008791 int vectors, rc, index;
James Smartda0436e2009-05-22 14:51:39 -04008792
8793 /* Set up MSI-X multi-message vectors */
James Smart82c3e9b2012-09-29 11:29:50 -04008794 for (index = 0; index < phba->cfg_fcp_io_channel; index++)
James Smartda0436e2009-05-22 14:51:39 -04008795 phba->sli4_hba.msix_entries[index].entry = index;
8796
8797 /* Configure MSI-X capability structure */
James Smart82c3e9b2012-09-29 11:29:50 -04008798 vectors = phba->cfg_fcp_io_channel;
James Smart1ba981f2014-02-20 09:56:45 -05008799 if (phba->cfg_fof) {
8800 phba->sli4_hba.msix_entries[index].entry = index;
8801 vectors++;
8802 }
Alexander Gordeev4f871e12014-09-03 12:56:29 -04008803 rc = pci_enable_msix_range(phba->pcidev, phba->sli4_hba.msix_entries,
8804 2, vectors);
8805 if (rc < 0) {
James Smartda0436e2009-05-22 14:51:39 -04008806 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8807 "0484 PCI enable MSI-X failed (%d)\n", rc);
Alexander Gordeev029165a2014-07-16 20:05:15 +02008808 goto vec_fail_out;
James Smartda0436e2009-05-22 14:51:39 -04008809 }
Alexander Gordeev4f871e12014-09-03 12:56:29 -04008810 vectors = rc;
James Smart75baf692010-06-08 18:31:21 -04008811
James Smartda0436e2009-05-22 14:51:39 -04008812 /* Log MSI-X vector assignment */
James Smart75baf692010-06-08 18:31:21 -04008813 for (index = 0; index < vectors; index++)
James Smartda0436e2009-05-22 14:51:39 -04008814 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8815 "0489 MSI-X entry[%d]: vector=x%x "
8816 "message=%d\n", index,
8817 phba->sli4_hba.msix_entries[index].vector,
8818 phba->sli4_hba.msix_entries[index].entry);
James Smart67d12732012-08-03 12:36:13 -04008819
James Smart7bb03bb2013-04-17 20:19:16 -04008820 /* Assign MSI-X vectors to interrupt handlers */
James Smart67d12732012-08-03 12:36:13 -04008821 for (index = 0; index < vectors; index++) {
James Smart4305f182012-08-03 12:36:33 -04008822 memset(&phba->sli4_hba.handler_name[index], 0, 16);
James Smarta2fc4aef2014-09-03 12:57:55 -04008823 snprintf((char *)&phba->sli4_hba.handler_name[index],
8824 LPFC_SLI4_HANDLER_NAME_SZ,
James Smart4305f182012-08-03 12:36:33 -04008825 LPFC_DRIVER_HANDLER_NAME"%d", index);
James Smartda0436e2009-05-22 14:51:39 -04008826
James Smart67d12732012-08-03 12:36:13 -04008827 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8828 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
James Smartba20c852012-08-03 12:36:52 -04008829 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].fcp_eq_in_use, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008830 if (phba->cfg_fof && (index == (vectors - 1)))
8831 rc = request_irq(
8832 phba->sli4_hba.msix_entries[index].vector,
8833 &lpfc_sli4_fof_intr_handler, IRQF_SHARED,
8834 (char *)&phba->sli4_hba.handler_name[index],
8835 &phba->sli4_hba.fcp_eq_hdl[index]);
8836 else
8837 rc = request_irq(
8838 phba->sli4_hba.msix_entries[index].vector,
James Smart67d12732012-08-03 12:36:13 -04008839 &lpfc_sli4_hba_intr_handler, IRQF_SHARED,
James Smart4305f182012-08-03 12:36:33 -04008840 (char *)&phba->sli4_hba.handler_name[index],
James Smart67d12732012-08-03 12:36:13 -04008841 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -04008842 if (rc) {
8843 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8844 "0486 MSI-X fast-path (%d) "
8845 "request_irq failed (%d)\n", index, rc);
8846 goto cfg_fail_out;
8847 }
8848 }
8849
James Smart1ba981f2014-02-20 09:56:45 -05008850 if (phba->cfg_fof)
8851 vectors--;
8852
James Smart82c3e9b2012-09-29 11:29:50 -04008853 if (vectors != phba->cfg_fcp_io_channel) {
8854 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8855 "3238 Reducing IO channels to match number of "
8856 "MSI-X vectors, requested %d got %d\n",
8857 phba->cfg_fcp_io_channel, vectors);
8858 phba->cfg_fcp_io_channel = vectors;
8859 }
James Smart7bb03bb2013-04-17 20:19:16 -04008860
8861 lpfc_sli4_set_affinity(phba, vectors);
James Smartda0436e2009-05-22 14:51:39 -04008862 return rc;
8863
8864cfg_fail_out:
8865 /* free the irq already requested */
James Smartacbd8612013-09-06 12:21:19 -04008866 for (--index; index >= 0; index--) {
8867 irq_set_affinity_hint(phba->sli4_hba.msix_entries[index].
8868 vector, NULL);
James Smart67d12732012-08-03 12:36:13 -04008869 free_irq(phba->sli4_hba.msix_entries[index].vector,
8870 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartacbd8612013-09-06 12:21:19 -04008871 }
James Smartda0436e2009-05-22 14:51:39 -04008872
James Smartda0436e2009-05-22 14:51:39 -04008873 /* Unconfigure MSI-X capability structure */
8874 pci_disable_msix(phba->pcidev);
Alexander Gordeev029165a2014-07-16 20:05:15 +02008875
8876vec_fail_out:
James Smartda0436e2009-05-22 14:51:39 -04008877 return rc;
8878}
8879
8880/**
8881 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
8882 * @phba: pointer to lpfc hba data structure.
8883 *
8884 * This routine is invoked to release the MSI-X vectors and then disable the
8885 * MSI-X interrupt mode to device with SLI-4 interface spec.
8886 **/
8887static void
8888lpfc_sli4_disable_msix(struct lpfc_hba *phba)
8889{
8890 int index;
8891
8892 /* Free up MSI-X multi-message vectors */
James Smartacbd8612013-09-06 12:21:19 -04008893 for (index = 0; index < phba->cfg_fcp_io_channel; index++) {
8894 irq_set_affinity_hint(phba->sli4_hba.msix_entries[index].
8895 vector, NULL);
James Smartda0436e2009-05-22 14:51:39 -04008896 free_irq(phba->sli4_hba.msix_entries[index].vector,
James Smart67d12732012-08-03 12:36:13 -04008897 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartacbd8612013-09-06 12:21:19 -04008898 }
James Smart1ba981f2014-02-20 09:56:45 -05008899 if (phba->cfg_fof) {
8900 free_irq(phba->sli4_hba.msix_entries[index].vector,
8901 &phba->sli4_hba.fcp_eq_hdl[index]);
8902 }
James Smartda0436e2009-05-22 14:51:39 -04008903 /* Disable MSI-X */
8904 pci_disable_msix(phba->pcidev);
8905
8906 return;
8907}
8908
8909/**
8910 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
8911 * @phba: pointer to lpfc hba data structure.
8912 *
8913 * This routine is invoked to enable the MSI interrupt mode to device with
8914 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
8915 * to enable the MSI vector. The device driver is responsible for calling
8916 * the request_irq() to register MSI vector with a interrupt the handler,
8917 * which is done in this function.
8918 *
8919 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008920 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008921 * other values - error
8922 **/
8923static int
8924lpfc_sli4_enable_msi(struct lpfc_hba *phba)
8925{
8926 int rc, index;
8927
8928 rc = pci_enable_msi(phba->pcidev);
8929 if (!rc)
8930 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8931 "0487 PCI enable MSI mode success.\n");
8932 else {
8933 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8934 "0488 PCI enable MSI mode failed (%d)\n", rc);
8935 return rc;
8936 }
8937
8938 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
8939 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8940 if (rc) {
8941 pci_disable_msi(phba->pcidev);
8942 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8943 "0490 MSI request_irq failed (%d)\n", rc);
James Smart75baf692010-06-08 18:31:21 -04008944 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008945 }
8946
James Smart67d12732012-08-03 12:36:13 -04008947 for (index = 0; index < phba->cfg_fcp_io_channel; index++) {
James Smartda0436e2009-05-22 14:51:39 -04008948 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8949 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
8950 }
8951
James Smart1ba981f2014-02-20 09:56:45 -05008952 if (phba->cfg_fof) {
8953 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8954 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
8955 }
James Smart75baf692010-06-08 18:31:21 -04008956 return 0;
James Smartda0436e2009-05-22 14:51:39 -04008957}
8958
8959/**
8960 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
8961 * @phba: pointer to lpfc hba data structure.
8962 *
8963 * This routine is invoked to disable the MSI interrupt mode to device with
8964 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
8965 * done request_irq() on before calling pci_disable_msi(). Failure to do so
8966 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
8967 * its vector.
8968 **/
8969static void
8970lpfc_sli4_disable_msi(struct lpfc_hba *phba)
8971{
8972 free_irq(phba->pcidev->irq, phba);
8973 pci_disable_msi(phba->pcidev);
8974 return;
8975}
8976
8977/**
8978 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
8979 * @phba: pointer to lpfc hba data structure.
8980 *
8981 * This routine is invoked to enable device interrupt and associate driver's
8982 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
8983 * interface spec. Depends on the interrupt mode configured to the driver,
8984 * the driver will try to fallback from the configured interrupt mode to an
8985 * interrupt mode which is supported by the platform, kernel, and device in
8986 * the order of:
8987 * MSI-X -> MSI -> IRQ.
8988 *
8989 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008990 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008991 * other values - error
8992 **/
8993static uint32_t
8994lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
8995{
8996 uint32_t intr_mode = LPFC_INTR_ERROR;
8997 int retval, index;
8998
8999 if (cfg_mode == 2) {
9000 /* Preparation before conf_msi mbox cmd */
9001 retval = 0;
9002 if (!retval) {
9003 /* Now, try to enable MSI-X interrupt mode */
9004 retval = lpfc_sli4_enable_msix(phba);
9005 if (!retval) {
9006 /* Indicate initialization to MSI-X mode */
9007 phba->intr_type = MSIX;
9008 intr_mode = 2;
9009 }
9010 }
9011 }
9012
9013 /* Fallback to MSI if MSI-X initialization failed */
9014 if (cfg_mode >= 1 && phba->intr_type == NONE) {
9015 retval = lpfc_sli4_enable_msi(phba);
9016 if (!retval) {
9017 /* Indicate initialization to MSI mode */
9018 phba->intr_type = MSI;
9019 intr_mode = 1;
9020 }
9021 }
9022
9023 /* Fallback to INTx if both MSI-X/MSI initalization failed */
9024 if (phba->intr_type == NONE) {
9025 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
9026 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
9027 if (!retval) {
9028 /* Indicate initialization to INTx mode */
9029 phba->intr_type = INTx;
9030 intr_mode = 0;
James Smart67d12732012-08-03 12:36:13 -04009031 for (index = 0; index < phba->cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04009032 index++) {
9033 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
9034 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
James Smartba20c852012-08-03 12:36:52 -04009035 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].
9036 fcp_eq_in_use, 1);
James Smartda0436e2009-05-22 14:51:39 -04009037 }
James Smart1ba981f2014-02-20 09:56:45 -05009038 if (phba->cfg_fof) {
9039 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
9040 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
9041 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].
9042 fcp_eq_in_use, 1);
9043 }
James Smartda0436e2009-05-22 14:51:39 -04009044 }
9045 }
9046 return intr_mode;
9047}
9048
9049/**
9050 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
9051 * @phba: pointer to lpfc hba data structure.
9052 *
9053 * This routine is invoked to disable device interrupt and disassociate
9054 * the driver's interrupt handler(s) from interrupt vector(s) to device
9055 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
9056 * will release the interrupt vector(s) for the message signaled interrupt.
9057 **/
9058static void
9059lpfc_sli4_disable_intr(struct lpfc_hba *phba)
9060{
9061 /* Disable the currently initialized interrupt mode */
9062 if (phba->intr_type == MSIX)
9063 lpfc_sli4_disable_msix(phba);
9064 else if (phba->intr_type == MSI)
9065 lpfc_sli4_disable_msi(phba);
9066 else if (phba->intr_type == INTx)
9067 free_irq(phba->pcidev->irq, phba);
9068
9069 /* Reset interrupt management states */
9070 phba->intr_type = NONE;
9071 phba->sli.slistat.sli_intr = 0;
9072
9073 return;
9074}
9075
9076/**
James Smart3772a992009-05-22 14:50:54 -04009077 * lpfc_unset_hba - Unset SLI3 hba device initialization
9078 * @phba: pointer to lpfc hba data structure.
9079 *
9080 * This routine is invoked to unset the HBA device initialization steps to
9081 * a device with SLI-3 interface spec.
9082 **/
9083static void
9084lpfc_unset_hba(struct lpfc_hba *phba)
9085{
9086 struct lpfc_vport *vport = phba->pport;
9087 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
9088
9089 spin_lock_irq(shost->host_lock);
9090 vport->load_flag |= FC_UNLOADING;
9091 spin_unlock_irq(shost->host_lock);
9092
James Smart72859902012-01-18 16:25:38 -05009093 kfree(phba->vpi_bmask);
9094 kfree(phba->vpi_ids);
9095
James Smart3772a992009-05-22 14:50:54 -04009096 lpfc_stop_hba_timers(phba);
9097
9098 phba->pport->work_port_events = 0;
9099
9100 lpfc_sli_hba_down(phba);
9101
9102 lpfc_sli_brdrestart(phba);
9103
9104 lpfc_sli_disable_intr(phba);
9105
9106 return;
9107}
9108
9109/**
James Smart5af5eee2010-10-22 11:06:38 -04009110 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
9111 * @phba: Pointer to HBA context object.
9112 *
9113 * This function is called in the SLI4 code path to wait for completion
9114 * of device's XRIs exchange busy. It will check the XRI exchange busy
9115 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
9116 * that, it will check the XRI exchange busy on outstanding FCP and ELS
9117 * I/Os every 30 seconds, log error message, and wait forever. Only when
9118 * all XRI exchange busy complete, the driver unload shall proceed with
9119 * invoking the function reset ioctl mailbox command to the CNA and the
9120 * the rest of the driver unload resource release.
9121 **/
9122static void
9123lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
9124{
9125 int wait_time = 0;
9126 int fcp_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
9127 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
9128
9129 while (!fcp_xri_cmpl || !els_xri_cmpl) {
9130 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
9131 if (!fcp_xri_cmpl)
9132 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9133 "2877 FCP XRI exchange busy "
9134 "wait time: %d seconds.\n",
9135 wait_time/1000);
9136 if (!els_xri_cmpl)
9137 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9138 "2878 ELS XRI exchange busy "
9139 "wait time: %d seconds.\n",
9140 wait_time/1000);
9141 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
9142 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
9143 } else {
9144 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
9145 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
9146 }
9147 fcp_xri_cmpl =
9148 list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
9149 els_xri_cmpl =
9150 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
9151 }
9152}
9153
9154/**
James Smartda0436e2009-05-22 14:51:39 -04009155 * lpfc_sli4_hba_unset - Unset the fcoe hba
9156 * @phba: Pointer to HBA context object.
9157 *
9158 * This function is called in the SLI4 code path to reset the HBA's FCoE
9159 * function. The caller is not required to hold any lock. This routine
9160 * issues PCI function reset mailbox command to reset the FCoE function.
9161 * At the end of the function, it calls lpfc_hba_down_post function to
9162 * free any pending commands.
9163 **/
9164static void
9165lpfc_sli4_hba_unset(struct lpfc_hba *phba)
9166{
9167 int wait_cnt = 0;
9168 LPFC_MBOXQ_t *mboxq;
James Smart912e3ac2011-05-24 11:42:11 -04009169 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -04009170
9171 lpfc_stop_hba_timers(phba);
9172 phba->sli4_hba.intr_enable = 0;
9173
9174 /*
9175 * Gracefully wait out the potential current outstanding asynchronous
9176 * mailbox command.
9177 */
9178
9179 /* First, block any pending async mailbox command from posted */
9180 spin_lock_irq(&phba->hbalock);
9181 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
9182 spin_unlock_irq(&phba->hbalock);
9183 /* Now, trying to wait it out if we can */
9184 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9185 msleep(10);
9186 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
9187 break;
9188 }
9189 /* Forcefully release the outstanding mailbox command if timed out */
9190 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9191 spin_lock_irq(&phba->hbalock);
9192 mboxq = phba->sli.mbox_active;
9193 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
9194 __lpfc_mbox_cmpl_put(phba, mboxq);
9195 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9196 phba->sli.mbox_active = NULL;
9197 spin_unlock_irq(&phba->hbalock);
9198 }
9199
James Smart5af5eee2010-10-22 11:06:38 -04009200 /* Abort all iocbs associated with the hba */
9201 lpfc_sli_hba_iocb_abort(phba);
9202
9203 /* Wait for completion of device XRI exchange busy */
9204 lpfc_sli4_xri_exchange_busy_wait(phba);
9205
James Smartda0436e2009-05-22 14:51:39 -04009206 /* Disable PCI subsystem interrupt */
9207 lpfc_sli4_disable_intr(phba);
9208
James Smart912e3ac2011-05-24 11:42:11 -04009209 /* Disable SR-IOV if enabled */
9210 if (phba->cfg_sriov_nr_virtfn)
9211 pci_disable_sriov(pdev);
9212
James Smartda0436e2009-05-22 14:51:39 -04009213 /* Stop kthread signal shall trigger work_done one more time */
9214 kthread_stop(phba->worker_thread);
9215
James Smart3677a3a2010-09-29 11:19:14 -04009216 /* Reset SLI4 HBA FCoE function */
9217 lpfc_pci_function_reset(phba);
James Smart5350d872011-10-10 21:33:49 -04009218 lpfc_sli4_queue_destroy(phba);
James Smart3677a3a2010-09-29 11:19:14 -04009219
James Smartda0436e2009-05-22 14:51:39 -04009220 /* Stop the SLI4 device port */
9221 phba->pport->work_port_events = 0;
9222}
9223
James Smart28baac72010-02-12 14:42:03 -05009224 /**
9225 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
9226 * @phba: Pointer to HBA context object.
9227 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
9228 *
9229 * This function is called in the SLI4 code path to read the port's
9230 * sli4 capabilities.
9231 *
9232 * This function may be be called from any context that can block-wait
9233 * for the completion. The expectation is that this routine is called
9234 * typically from probe_one or from the online routine.
9235 **/
9236int
9237lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9238{
9239 int rc;
9240 struct lpfc_mqe *mqe;
9241 struct lpfc_pc_sli4_params *sli4_params;
9242 uint32_t mbox_tmo;
9243
9244 rc = 0;
9245 mqe = &mboxq->u.mqe;
9246
9247 /* Read the port's SLI4 Parameters port capabilities */
James Smartfedd3b72011-02-16 12:39:24 -05009248 lpfc_pc_sli4_params(mboxq);
James Smart28baac72010-02-12 14:42:03 -05009249 if (!phba->sli4_hba.intr_enable)
9250 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9251 else {
James Smarta183a152011-10-10 21:32:43 -04009252 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart28baac72010-02-12 14:42:03 -05009253 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
9254 }
9255
9256 if (unlikely(rc))
9257 return 1;
9258
9259 sli4_params = &phba->sli4_hba.pc_sli4_params;
9260 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
9261 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
9262 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
9263 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
9264 &mqe->un.sli4_params);
9265 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
9266 &mqe->un.sli4_params);
9267 sli4_params->proto_types = mqe->un.sli4_params.word3;
9268 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
9269 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
9270 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
9271 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
9272 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
9273 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
9274 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
9275 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
9276 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
9277 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
9278 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
9279 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
9280 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
9281 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
9282 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
9283 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
9284 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
9285 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
9286 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
9287 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
James Smart05580562011-05-24 11:40:48 -04009288
9289 /* Make sure that sge_supp_len can be handled by the driver */
9290 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
9291 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
9292
James Smart28baac72010-02-12 14:42:03 -05009293 return rc;
9294}
9295
James Smartda0436e2009-05-22 14:51:39 -04009296/**
James Smartfedd3b72011-02-16 12:39:24 -05009297 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
9298 * @phba: Pointer to HBA context object.
9299 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
9300 *
9301 * This function is called in the SLI4 code path to read the port's
9302 * sli4 capabilities.
9303 *
9304 * This function may be be called from any context that can block-wait
9305 * for the completion. The expectation is that this routine is called
9306 * typically from probe_one or from the online routine.
9307 **/
9308int
9309lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9310{
9311 int rc;
9312 struct lpfc_mqe *mqe = &mboxq->u.mqe;
9313 struct lpfc_pc_sli4_params *sli4_params;
James Smarta183a152011-10-10 21:32:43 -04009314 uint32_t mbox_tmo;
James Smartfedd3b72011-02-16 12:39:24 -05009315 int length;
9316 struct lpfc_sli4_parameters *mbx_sli4_parameters;
9317
James Smart6d368e52011-05-24 11:44:12 -04009318 /*
9319 * By default, the driver assumes the SLI4 port requires RPI
9320 * header postings. The SLI4_PARAM response will correct this
9321 * assumption.
9322 */
9323 phba->sli4_hba.rpi_hdrs_in_use = 1;
9324
James Smartfedd3b72011-02-16 12:39:24 -05009325 /* Read the port's SLI4 Config Parameters */
9326 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
9327 sizeof(struct lpfc_sli4_cfg_mhdr));
9328 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
9329 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
9330 length, LPFC_SLI4_MBX_EMBED);
9331 if (!phba->sli4_hba.intr_enable)
9332 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -04009333 else {
9334 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
9335 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
9336 }
James Smartfedd3b72011-02-16 12:39:24 -05009337 if (unlikely(rc))
9338 return rc;
9339 sli4_params = &phba->sli4_hba.pc_sli4_params;
9340 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
9341 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
9342 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
9343 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
9344 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
9345 mbx_sli4_parameters);
9346 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
9347 mbx_sli4_parameters);
9348 if (bf_get(cfg_phwq, mbx_sli4_parameters))
9349 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
9350 else
9351 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
9352 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
9353 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
James Smart1ba981f2014-02-20 09:56:45 -05009354 sli4_params->oas_supported = bf_get(cfg_oas, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -05009355 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
9356 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
9357 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
9358 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
James Smart0c651872013-07-15 18:33:23 -04009359 sli4_params->wqsize = bf_get(cfg_wqsize, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -05009360 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
9361 mbx_sli4_parameters);
9362 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
9363 mbx_sli4_parameters);
James Smart6d368e52011-05-24 11:44:12 -04009364 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
9365 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
James Smart05580562011-05-24 11:40:48 -04009366
9367 /* Make sure that sge_supp_len can be handled by the driver */
9368 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
9369 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
9370
James Smartfedd3b72011-02-16 12:39:24 -05009371 return 0;
9372}
9373
9374/**
James Smart3772a992009-05-22 14:50:54 -04009375 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
9376 * @pdev: pointer to PCI device
9377 * @pid: pointer to PCI device identifier
9378 *
9379 * This routine is to be called to attach a device with SLI-3 interface spec
9380 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
9381 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
9382 * information of the device and driver to see if the driver state that it can
9383 * support this kind of device. If the match is successful, the driver core
9384 * invokes this routine. If this routine determines it can claim the HBA, it
9385 * does all the initialization that it needs to do to handle the HBA properly.
9386 *
9387 * Return code
9388 * 0 - driver can claim the device
9389 * negative value - driver can not claim the device
9390 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009391static int
James Smart3772a992009-05-22 14:50:54 -04009392lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
9393{
9394 struct lpfc_hba *phba;
9395 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04009396 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -04009397 int error;
9398 uint32_t cfg_mode, intr_mode;
9399
9400 /* Allocate memory for HBA structure */
9401 phba = lpfc_hba_alloc(pdev);
9402 if (!phba)
9403 return -ENOMEM;
9404
9405 /* Perform generic PCI device enabling operation */
9406 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -04009407 if (error)
James Smart3772a992009-05-22 14:50:54 -04009408 goto out_free_phba;
James Smart3772a992009-05-22 14:50:54 -04009409
9410 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
9411 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
9412 if (error)
9413 goto out_disable_pci_dev;
9414
9415 /* Set up SLI-3 specific device PCI memory space */
9416 error = lpfc_sli_pci_mem_setup(phba);
9417 if (error) {
9418 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9419 "1402 Failed to set up pci memory space.\n");
9420 goto out_disable_pci_dev;
9421 }
9422
9423 /* Set up phase-1 common device driver resources */
9424 error = lpfc_setup_driver_resource_phase1(phba);
9425 if (error) {
9426 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9427 "1403 Failed to set up driver resource.\n");
9428 goto out_unset_pci_mem_s3;
9429 }
9430
9431 /* Set up SLI-3 specific device driver resources */
9432 error = lpfc_sli_driver_resource_setup(phba);
9433 if (error) {
9434 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9435 "1404 Failed to set up driver resource.\n");
9436 goto out_unset_pci_mem_s3;
9437 }
9438
9439 /* Initialize and populate the iocb list per host */
9440 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
9441 if (error) {
9442 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9443 "1405 Failed to initialize iocb list.\n");
9444 goto out_unset_driver_resource_s3;
9445 }
9446
9447 /* Set up common device driver resources */
9448 error = lpfc_setup_driver_resource_phase2(phba);
9449 if (error) {
9450 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9451 "1406 Failed to set up driver resource.\n");
9452 goto out_free_iocb_list;
9453 }
9454
James Smart079b5c92011-08-21 21:48:49 -04009455 /* Get the default values for Model Name and Description */
9456 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
9457
James Smart3772a992009-05-22 14:50:54 -04009458 /* Create SCSI host to the physical port */
9459 error = lpfc_create_shost(phba);
9460 if (error) {
9461 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9462 "1407 Failed to create scsi host.\n");
9463 goto out_unset_driver_resource;
9464 }
9465
9466 /* Configure sysfs attributes */
9467 vport = phba->pport;
9468 error = lpfc_alloc_sysfs_attr(vport);
9469 if (error) {
9470 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9471 "1476 Failed to allocate sysfs attr\n");
9472 goto out_destroy_shost;
9473 }
9474
James Smart6669f9b2009-10-02 15:16:45 -04009475 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -04009476 /* Now, trying to enable interrupt and bring up the device */
9477 cfg_mode = phba->cfg_use_msi;
9478 while (true) {
9479 /* Put device to a known state before enabling interrupt */
9480 lpfc_stop_port(phba);
9481 /* Configure and enable interrupt */
9482 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
9483 if (intr_mode == LPFC_INTR_ERROR) {
9484 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9485 "0431 Failed to enable interrupt.\n");
9486 error = -ENODEV;
9487 goto out_free_sysfs_attr;
9488 }
9489 /* SLI-3 HBA setup */
9490 if (lpfc_sli_hba_setup(phba)) {
9491 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9492 "1477 Failed to set up hba\n");
9493 error = -ENODEV;
9494 goto out_remove_device;
9495 }
9496
9497 /* Wait 50ms for the interrupts of previous mailbox commands */
9498 msleep(50);
9499 /* Check active interrupts on message signaled interrupts */
9500 if (intr_mode == 0 ||
9501 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
9502 /* Log the current active interrupt mode */
9503 phba->intr_mode = intr_mode;
9504 lpfc_log_intr_mode(phba, intr_mode);
9505 break;
9506 } else {
9507 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9508 "0447 Configure interrupt mode (%d) "
9509 "failed active interrupt test.\n",
9510 intr_mode);
9511 /* Disable the current interrupt mode */
9512 lpfc_sli_disable_intr(phba);
9513 /* Try next level of interrupt mode */
9514 cfg_mode = --intr_mode;
9515 }
9516 }
9517
9518 /* Perform post initialization setup */
9519 lpfc_post_init_setup(phba);
9520
9521 /* Check if there are static vports to be created. */
9522 lpfc_create_static_vport(phba);
9523
9524 return 0;
9525
9526out_remove_device:
9527 lpfc_unset_hba(phba);
9528out_free_sysfs_attr:
9529 lpfc_free_sysfs_attr(vport);
9530out_destroy_shost:
9531 lpfc_destroy_shost(phba);
9532out_unset_driver_resource:
9533 lpfc_unset_driver_resource_phase2(phba);
9534out_free_iocb_list:
9535 lpfc_free_iocb_list(phba);
9536out_unset_driver_resource_s3:
9537 lpfc_sli_driver_resource_unset(phba);
9538out_unset_pci_mem_s3:
9539 lpfc_sli_pci_mem_unset(phba);
9540out_disable_pci_dev:
9541 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04009542 if (shost)
9543 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -04009544out_free_phba:
9545 lpfc_hba_free(phba);
9546 return error;
9547}
9548
9549/**
9550 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -04009551 * @pdev: pointer to PCI device
9552 *
James Smart3772a992009-05-22 14:50:54 -04009553 * This routine is to be called to disattach a device with SLI-3 interface
9554 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
9555 * removed from PCI bus, it performs all the necessary cleanup for the HBA
9556 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -04009557 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009558static void
James Smart3772a992009-05-22 14:50:54 -04009559lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -05009560{
James Smart2e0fef82007-06-17 19:56:36 -05009561 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9562 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -05009563 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -05009564 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -05009565 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05009566 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
9567
James Smart549e55c2007-08-02 11:09:51 -04009568 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04009569 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04009570 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009571
James Smart858c9f62007-06-17 19:56:39 -05009572 lpfc_free_sysfs_attr(vport);
9573
James Smarteada2722008-12-04 22:39:13 -05009574 /* Release all the vports against this physical port */
9575 vports = lpfc_create_vport_work_array(phba);
9576 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -04009577 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
9578 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
9579 continue;
James Smarteada2722008-12-04 22:39:13 -05009580 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -04009581 }
James Smarteada2722008-12-04 22:39:13 -05009582 lpfc_destroy_vport_work_array(phba, vports);
9583
9584 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -05009585 fc_remove_host(shost);
9586 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04009587 lpfc_cleanup(vport);
9588
James Smart2e0fef82007-06-17 19:56:36 -05009589 /*
9590 * Bring down the SLI Layer. This step disable all interrupts,
9591 * clears the rings, discards all mailbox commands, and resets
9592 * the HBA.
9593 */
James Smarta257bf92009-04-06 18:48:10 -04009594
Justin P. Mattock48e34d02010-12-30 15:07:58 -08009595 /* HBA interrupt will be disabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -05009596 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -04009597 /* Stop kthread signal shall trigger work_done one more time */
9598 kthread_stop(phba->worker_thread);
9599 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -05009600 lpfc_sli_brdrestart(phba);
9601
James Smart72859902012-01-18 16:25:38 -05009602 kfree(phba->vpi_bmask);
9603 kfree(phba->vpi_ids);
9604
James Smart3772a992009-05-22 14:50:54 -04009605 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05009606 spin_lock_irq(&phba->hbalock);
9607 list_del_init(&vport->listentry);
9608 spin_unlock_irq(&phba->hbalock);
9609
James Smart858c9f62007-06-17 19:56:39 -05009610 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05009611
James Smart912e3ac2011-05-24 11:42:11 -04009612 /* Disable SR-IOV if enabled */
9613 if (phba->cfg_sriov_nr_virtfn)
9614 pci_disable_sriov(pdev);
9615
James Smart5b75da22008-12-04 22:39:35 -05009616 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009617 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -05009618
James Smart858c9f62007-06-17 19:56:39 -05009619 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05009620
9621 /*
9622 * Call scsi_free before mem_free since scsi bufs are released to their
9623 * corresponding pools here.
9624 */
9625 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -04009626 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -05009627
James Smart34b02dc2008-08-24 21:49:55 -04009628 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
9629 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -05009630
James Smart2e0fef82007-06-17 19:56:36 -05009631 /* Free resources associated with SLI2 interface */
9632 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -04009633 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -05009634
9635 /* unmap adapter SLIM and Control Registers */
9636 iounmap(phba->ctrl_regs_memmap_p);
9637 iounmap(phba->slim_memmap_p);
9638
James Smart3772a992009-05-22 14:50:54 -04009639 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -05009640
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05009641 pci_release_selected_regions(pdev, bars);
James Smart2e0fef82007-06-17 19:56:36 -05009642 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05009643}
9644
Linas Vepstas8d63f372007-02-14 14:28:36 -06009645/**
James Smart3772a992009-05-22 14:50:54 -04009646 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05009647 * @pdev: pointer to PCI device
9648 * @msg: power management message
9649 *
James Smart3772a992009-05-22 14:50:54 -04009650 * This routine is to be called from the kernel's PCI subsystem to support
9651 * system Power Management (PM) to device with SLI-3 interface spec. When
9652 * PM invokes this method, it quiesces the device by stopping the driver's
9653 * worker thread for the device, turning off device's interrupt and DMA,
9654 * and bring the device offline. Note that as the driver implements the
9655 * minimum PM requirements to a power-aware driver's PM support for the
9656 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
9657 * to the suspend() method call will be treated as SUSPEND and the driver will
9658 * fully reinitialize its device during resume() method call, the driver will
9659 * set device to PCI_D3hot state in PCI config space instead of setting it
9660 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -05009661 *
9662 * Return code
James Smart3772a992009-05-22 14:50:54 -04009663 * 0 - driver suspended the device
9664 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05009665 **/
9666static int
James Smart3772a992009-05-22 14:50:54 -04009667lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -05009668{
9669 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9670 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9671
9672 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9673 "0473 PCI device Power Management suspend.\n");
9674
9675 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -04009676 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3a55b532008-12-04 22:38:54 -05009677 lpfc_offline(phba);
9678 kthread_stop(phba->worker_thread);
9679
9680 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -04009681 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -05009682
9683 /* Save device state to PCI config space */
9684 pci_save_state(pdev);
9685 pci_set_power_state(pdev, PCI_D3hot);
9686
9687 return 0;
9688}
9689
9690/**
James Smart3772a992009-05-22 14:50:54 -04009691 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05009692 * @pdev: pointer to PCI device
9693 *
James Smart3772a992009-05-22 14:50:54 -04009694 * This routine is to be called from the kernel's PCI subsystem to support
9695 * system Power Management (PM) to device with SLI-3 interface spec. When PM
9696 * invokes this method, it restores the device's PCI config space state and
9697 * fully reinitializes the device and brings it online. Note that as the
9698 * driver implements the minimum PM requirements to a power-aware driver's
9699 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
9700 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
9701 * driver will fully reinitialize its device during resume() method call,
9702 * the device will be set to PCI_D0 directly in PCI config space before
9703 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -05009704 *
9705 * Return code
James Smart3772a992009-05-22 14:50:54 -04009706 * 0 - driver suspended the device
9707 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05009708 **/
9709static int
James Smart3772a992009-05-22 14:50:54 -04009710lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -05009711{
9712 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9713 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -05009714 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05009715 int error;
9716
9717 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9718 "0452 PCI device Power Management resume.\n");
9719
9720 /* Restore device state from PCI config space */
9721 pci_set_power_state(pdev, PCI_D0);
9722 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -04009723
James Smart1dfb5a42010-02-12 14:40:50 -05009724 /*
9725 * As the new kernel behavior of pci_restore_state() API call clears
9726 * device saved_state flag, need to save the restored state again.
9727 */
9728 pci_save_state(pdev);
9729
James Smart3a55b532008-12-04 22:38:54 -05009730 if (pdev->is_busmaster)
9731 pci_set_master(pdev);
9732
9733 /* Startup the kernel thread for this host adapter. */
9734 phba->worker_thread = kthread_run(lpfc_do_work, phba,
9735 "lpfc_worker_%d", phba->brd_no);
9736 if (IS_ERR(phba->worker_thread)) {
9737 error = PTR_ERR(phba->worker_thread);
9738 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9739 "0434 PM resume failed to start worker "
9740 "thread: error=x%x.\n", error);
9741 return error;
9742 }
9743
James Smart5b75da22008-12-04 22:39:35 -05009744 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009745 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05009746 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -05009747 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -05009748 "0430 PM resume Failed to enable interrupt\n");
9749 return -EIO;
9750 } else
9751 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05009752
9753 /* Restart HBA and bring it online */
9754 lpfc_sli_brdrestart(phba);
9755 lpfc_online(phba);
9756
James Smart5b75da22008-12-04 22:39:35 -05009757 /* Log the current active interrupt mode */
9758 lpfc_log_intr_mode(phba, phba->intr_mode);
9759
James Smart3a55b532008-12-04 22:38:54 -05009760 return 0;
9761}
9762
9763/**
James Smart891478a2009-11-18 15:40:23 -05009764 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
9765 * @phba: pointer to lpfc hba data structure.
9766 *
9767 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -04009768 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -05009769 **/
9770static void
9771lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
9772{
9773 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9774 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -04009775
9776 /*
9777 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
9778 * and let the SCSI mid-layer to retry them to recover.
9779 */
James Smartdb55fba2014-04-04 13:52:02 -04009780 lpfc_sli_abort_fcp_rings(phba);
James Smart891478a2009-11-18 15:40:23 -05009781}
9782
9783/**
James Smart0d878412009-10-02 15:16:56 -04009784 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
9785 * @phba: pointer to lpfc hba data structure.
9786 *
9787 * This routine is called to prepare the SLI3 device for PCI slot reset. It
9788 * disables the device interrupt and pci device, and aborts the internal FCP
9789 * pending I/Os.
9790 **/
9791static void
9792lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
9793{
James Smart0d878412009-10-02 15:16:56 -04009794 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05009795 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -04009796
James Smart75baf692010-06-08 18:31:21 -04009797 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -04009798 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -04009799
James Smarte2af0d22010-03-15 11:25:32 -04009800 /* Block all SCSI devices' I/Os on the host */
9801 lpfc_scsi_dev_block(phba);
9802
James Smartea714f32013-04-17 20:18:47 -04009803 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
9804 lpfc_sli_flush_fcp_rings(phba);
9805
James Smarte2af0d22010-03-15 11:25:32 -04009806 /* stop all timers */
9807 lpfc_stop_hba_timers(phba);
9808
James Smart0d878412009-10-02 15:16:56 -04009809 /* Disable interrupt and pci device */
9810 lpfc_sli_disable_intr(phba);
9811 pci_disable_device(phba->pcidev);
James Smart0d878412009-10-02 15:16:56 -04009812}
9813
9814/**
9815 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
9816 * @phba: pointer to lpfc hba data structure.
9817 *
9818 * This routine is called to prepare the SLI3 device for PCI slot permanently
9819 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
9820 * pending I/Os.
9821 **/
9822static void
James Smart75baf692010-06-08 18:31:21 -04009823lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
James Smart0d878412009-10-02 15:16:56 -04009824{
9825 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05009826 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -04009827 /* Block all SCSI devices' I/Os on the host */
9828 lpfc_scsi_dev_block(phba);
9829
9830 /* stop all timers */
9831 lpfc_stop_hba_timers(phba);
9832
James Smart0d878412009-10-02 15:16:56 -04009833 /* Clean up all driver's outstanding SCSI I/Os */
9834 lpfc_sli_flush_fcp_rings(phba);
9835}
9836
9837/**
James Smart3772a992009-05-22 14:50:54 -04009838 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -04009839 * @pdev: pointer to PCI device.
9840 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009841 *
James Smart3772a992009-05-22 14:50:54 -04009842 * This routine is called from the PCI subsystem for I/O error handling to
9843 * device with SLI-3 interface spec. This function is called by the PCI
9844 * subsystem after a PCI bus error affecting this device has been detected.
9845 * When this function is invoked, it will need to stop all the I/Os and
9846 * interrupt(s) to the device. Once that is done, it will return
9847 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
9848 * as desired.
James Smarte59058c2008-08-24 21:49:00 -04009849 *
9850 * Return codes
James Smart0d878412009-10-02 15:16:56 -04009851 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -04009852 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
9853 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -04009854 **/
James Smart3772a992009-05-22 14:50:54 -04009855static pci_ers_result_t
9856lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009857{
James Smart51ef4c22007-08-02 11:10:31 -04009858 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9859 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009860
James Smart0d878412009-10-02 15:16:56 -04009861 switch (state) {
9862 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -05009863 /* Non-fatal error, prepare for recovery */
9864 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -04009865 return PCI_ERS_RESULT_CAN_RECOVER;
9866 case pci_channel_io_frozen:
9867 /* Fatal error, prepare for slot reset */
9868 lpfc_sli_prep_dev_for_reset(phba);
9869 return PCI_ERS_RESULT_NEED_RESET;
9870 case pci_channel_io_perm_failure:
9871 /* Permanent failure, prepare for device down */
James Smart75baf692010-06-08 18:31:21 -04009872 lpfc_sli_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009873 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -04009874 default:
9875 /* Unknown state, prepare and request slot reset */
9876 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9877 "0472 Unknown PCI error state: x%x\n", state);
9878 lpfc_sli_prep_dev_for_reset(phba);
9879 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -04009880 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06009881}
9882
9883/**
James Smart3772a992009-05-22 14:50:54 -04009884 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -04009885 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009886 *
James Smart3772a992009-05-22 14:50:54 -04009887 * This routine is called from the PCI subsystem for error handling to
9888 * device with SLI-3 interface spec. This is called after PCI bus has been
9889 * reset to restart the PCI card from scratch, as if from a cold-boot.
9890 * During the PCI subsystem error recovery, after driver returns
9891 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
9892 * recovery and then call this routine before calling the .resume method
9893 * to recover the device. This function will initialize the HBA device,
9894 * enable the interrupt, but it will just put the HBA to offline state
9895 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -04009896 *
9897 * Return codes
James Smart3772a992009-05-22 14:50:54 -04009898 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
9899 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -06009900 */
James Smart3772a992009-05-22 14:50:54 -04009901static pci_ers_result_t
9902lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009903{
James Smart51ef4c22007-08-02 11:10:31 -04009904 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9905 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009906 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -05009907 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009908
9909 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +11009910 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -06009911 printk(KERN_ERR "lpfc: Cannot re-enable "
9912 "PCI device after reset.\n");
9913 return PCI_ERS_RESULT_DISCONNECT;
9914 }
9915
James Smart97207482008-12-04 22:39:19 -05009916 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05009917
9918 /*
9919 * As the new kernel behavior of pci_restore_state() API call clears
9920 * device saved_state flag, need to save the restored state again.
9921 */
9922 pci_save_state(pdev);
9923
James Smart97207482008-12-04 22:39:19 -05009924 if (pdev->is_busmaster)
9925 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009926
James Smart92d7f7b2007-06-17 19:56:38 -05009927 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04009928 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05009929 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009930
James Smart5b75da22008-12-04 22:39:35 -05009931 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009932 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05009933 if (intr_mode == LPFC_INTR_ERROR) {
9934 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9935 "0427 Cannot re-enable interrupt after "
9936 "slot reset.\n");
9937 return PCI_ERS_RESULT_DISCONNECT;
9938 } else
9939 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009940
James Smart75baf692010-06-08 18:31:21 -04009941 /* Take device offline, it will perform cleanup */
James Smart618a5232012-06-12 13:54:36 -04009942 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009943 lpfc_offline(phba);
9944 lpfc_sli_brdrestart(phba);
9945
James Smart5b75da22008-12-04 22:39:35 -05009946 /* Log the current active interrupt mode */
9947 lpfc_log_intr_mode(phba, phba->intr_mode);
9948
Linas Vepstas8d63f372007-02-14 14:28:36 -06009949 return PCI_ERS_RESULT_RECOVERED;
9950}
9951
9952/**
James Smart3772a992009-05-22 14:50:54 -04009953 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -04009954 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -06009955 *
James Smart3772a992009-05-22 14:50:54 -04009956 * This routine is called from the PCI subsystem for error handling to device
9957 * with SLI-3 interface spec. It is called when kernel error recovery tells
9958 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
9959 * error recovery. After this call, traffic can start to flow from this device
9960 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009961 */
James Smart3772a992009-05-22 14:50:54 -04009962static void
9963lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009964{
James Smart51ef4c22007-08-02 11:10:31 -04009965 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9966 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009967
James Smarte2af0d22010-03-15 11:25:32 -04009968 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -04009969 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -04009970
9971 /* Clean up Advanced Error Reporting (AER) if needed */
9972 if (phba->hba_flag & HBA_AER_ENABLED)
9973 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009974}
9975
James Smart3772a992009-05-22 14:50:54 -04009976/**
James Smartda0436e2009-05-22 14:51:39 -04009977 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
9978 * @phba: pointer to lpfc hba data structure.
9979 *
9980 * returns the number of ELS/CT IOCBs to reserve
9981 **/
9982int
9983lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
9984{
9985 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
9986
James Smartf1126682009-06-10 17:22:44 -04009987 if (phba->sli_rev == LPFC_SLI_REV4) {
9988 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -04009989 return 10;
James Smartf1126682009-06-10 17:22:44 -04009990 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -04009991 return 25;
James Smartf1126682009-06-10 17:22:44 -04009992 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -04009993 return 50;
James Smartf1126682009-06-10 17:22:44 -04009994 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -04009995 return 100;
James Smart8a9d2e82012-05-09 21:16:12 -04009996 else if (max_xri <= 1536)
James Smart6a9c52c2009-10-02 15:16:51 -04009997 return 150;
James Smart8a9d2e82012-05-09 21:16:12 -04009998 else if (max_xri <= 2048)
9999 return 200;
10000 else
10001 return 250;
James Smartf1126682009-06-10 17:22:44 -040010002 } else
10003 return 0;
James Smartda0436e2009-05-22 14:51:39 -040010004}
10005
10006/**
James Smart52d52442011-05-24 11:42:45 -040010007 * lpfc_write_firmware - attempt to write a firmware image to the port
James Smart52d52442011-05-24 11:42:45 -040010008 * @fw: pointer to firmware image returned from request_firmware.
James Smartce396282012-09-29 11:30:56 -040010009 * @phba: pointer to lpfc hba data structure.
James Smart52d52442011-05-24 11:42:45 -040010010 *
James Smart52d52442011-05-24 11:42:45 -040010011 **/
James Smartce396282012-09-29 11:30:56 -040010012static void
10013lpfc_write_firmware(const struct firmware *fw, void *context)
James Smart52d52442011-05-24 11:42:45 -040010014{
James Smartce396282012-09-29 11:30:56 -040010015 struct lpfc_hba *phba = (struct lpfc_hba *)context;
James Smart6b5151f2012-01-18 16:24:06 -050010016 char fwrev[FW_REV_STR_SIZE];
James Smartce396282012-09-29 11:30:56 -040010017 struct lpfc_grp_hdr *image;
James Smart52d52442011-05-24 11:42:45 -040010018 struct list_head dma_buffer_list;
10019 int i, rc = 0;
10020 struct lpfc_dmabuf *dmabuf, *next;
10021 uint32_t offset = 0, temp_offset = 0;
10022
James Smartc71ab862012-10-31 14:44:33 -040010023 /* It can be null in no-wait mode, sanity check */
James Smartce396282012-09-29 11:30:56 -040010024 if (!fw) {
10025 rc = -ENXIO;
10026 goto out;
10027 }
10028 image = (struct lpfc_grp_hdr *)fw->data;
10029
James Smart52d52442011-05-24 11:42:45 -040010030 INIT_LIST_HEAD(&dma_buffer_list);
James Smart079b5c92011-08-21 21:48:49 -040010031 if ((be32_to_cpu(image->magic_number) != LPFC_GROUP_OJECT_MAGIC_NUM) ||
10032 (bf_get_be32(lpfc_grp_hdr_file_type, image) !=
10033 LPFC_FILE_TYPE_GROUP) ||
10034 (bf_get_be32(lpfc_grp_hdr_id, image) != LPFC_FILE_ID_GROUP) ||
10035 (be32_to_cpu(image->size) != fw->size)) {
James Smart52d52442011-05-24 11:42:45 -040010036 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10037 "3022 Invalid FW image found. "
James Smart079b5c92011-08-21 21:48:49 -040010038 "Magic:%x Type:%x ID:%x\n",
10039 be32_to_cpu(image->magic_number),
10040 bf_get_be32(lpfc_grp_hdr_file_type, image),
10041 bf_get_be32(lpfc_grp_hdr_id, image));
James Smartce396282012-09-29 11:30:56 -040010042 rc = -EINVAL;
10043 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010044 }
10045 lpfc_decode_firmware_rev(phba, fwrev, 1);
James Smart88a2cfb2011-07-22 18:36:33 -040010046 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
James Smart52d52442011-05-24 11:42:45 -040010047 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartce396282012-09-29 11:30:56 -040010048 "3023 Updating Firmware, Current Version:%s "
James Smart52d52442011-05-24 11:42:45 -040010049 "New Version:%s\n",
James Smart88a2cfb2011-07-22 18:36:33 -040010050 fwrev, image->revision);
James Smart52d52442011-05-24 11:42:45 -040010051 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
10052 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
10053 GFP_KERNEL);
10054 if (!dmabuf) {
10055 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040010056 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010057 }
10058 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
10059 SLI4_PAGE_SIZE,
10060 &dmabuf->phys,
10061 GFP_KERNEL);
10062 if (!dmabuf->virt) {
10063 kfree(dmabuf);
10064 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040010065 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010066 }
10067 list_add_tail(&dmabuf->list, &dma_buffer_list);
10068 }
10069 while (offset < fw->size) {
10070 temp_offset = offset;
10071 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
James Smart079b5c92011-08-21 21:48:49 -040010072 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
James Smart52d52442011-05-24 11:42:45 -040010073 memcpy(dmabuf->virt,
10074 fw->data + temp_offset,
James Smart079b5c92011-08-21 21:48:49 -040010075 fw->size - temp_offset);
10076 temp_offset = fw->size;
James Smart52d52442011-05-24 11:42:45 -040010077 break;
10078 }
James Smart52d52442011-05-24 11:42:45 -040010079 memcpy(dmabuf->virt, fw->data + temp_offset,
10080 SLI4_PAGE_SIZE);
James Smart88a2cfb2011-07-22 18:36:33 -040010081 temp_offset += SLI4_PAGE_SIZE;
James Smart52d52442011-05-24 11:42:45 -040010082 }
10083 rc = lpfc_wr_object(phba, &dma_buffer_list,
10084 (fw->size - offset), &offset);
James Smartce396282012-09-29 11:30:56 -040010085 if (rc)
10086 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010087 }
10088 rc = offset;
10089 }
James Smartce396282012-09-29 11:30:56 -040010090
10091release_out:
James Smart52d52442011-05-24 11:42:45 -040010092 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
10093 list_del(&dmabuf->list);
10094 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
10095 dmabuf->virt, dmabuf->phys);
10096 kfree(dmabuf);
10097 }
James Smartce396282012-09-29 11:30:56 -040010098 release_firmware(fw);
10099out:
10100 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartc71ab862012-10-31 14:44:33 -040010101 "3024 Firmware update done: %d.\n", rc);
James Smartce396282012-09-29 11:30:56 -040010102 return;
James Smart52d52442011-05-24 11:42:45 -040010103}
10104
10105/**
James Smartc71ab862012-10-31 14:44:33 -040010106 * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
10107 * @phba: pointer to lpfc hba data structure.
10108 *
10109 * This routine is called to perform Linux generic firmware upgrade on device
10110 * that supports such feature.
10111 **/
10112int
10113lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
10114{
10115 uint8_t file_name[ELX_MODEL_NAME_SIZE];
10116 int ret;
10117 const struct firmware *fw;
10118
10119 /* Only supported on SLI4 interface type 2 for now */
10120 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
10121 LPFC_SLI_INTF_IF_TYPE_2)
10122 return -EPERM;
10123
10124 snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
10125
10126 if (fw_upgrade == INT_FW_UPGRADE) {
10127 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
10128 file_name, &phba->pcidev->dev,
10129 GFP_KERNEL, (void *)phba,
10130 lpfc_write_firmware);
10131 } else if (fw_upgrade == RUN_FW_UPGRADE) {
10132 ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
10133 if (!ret)
10134 lpfc_write_firmware(fw, (void *)phba);
10135 } else {
10136 ret = -EINVAL;
10137 }
10138
10139 return ret;
10140}
10141
10142/**
James Smartda0436e2009-05-22 14:51:39 -040010143 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
10144 * @pdev: pointer to PCI device
10145 * @pid: pointer to PCI device identifier
10146 *
10147 * This routine is called from the kernel's PCI subsystem to device with
10148 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
10149 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
10150 * information of the device and driver to see if the driver state that it
10151 * can support this kind of device. If the match is successful, the driver
10152 * core invokes this routine. If this routine determines it can claim the HBA,
10153 * it does all the initialization that it needs to do to handle the HBA
10154 * properly.
10155 *
10156 * Return code
10157 * 0 - driver can claim the device
10158 * negative value - driver can not claim the device
10159 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010160static int
James Smartda0436e2009-05-22 14:51:39 -040010161lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
10162{
10163 struct lpfc_hba *phba;
10164 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -040010165 struct Scsi_Host *shost = NULL;
James Smartce396282012-09-29 11:30:56 -040010166 int error, ret;
James Smartda0436e2009-05-22 14:51:39 -040010167 uint32_t cfg_mode, intr_mode;
James Smart67d12732012-08-03 12:36:13 -040010168 int adjusted_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -040010169
10170 /* Allocate memory for HBA structure */
10171 phba = lpfc_hba_alloc(pdev);
10172 if (!phba)
10173 return -ENOMEM;
10174
10175 /* Perform generic PCI device enabling operation */
10176 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -040010177 if (error)
James Smartda0436e2009-05-22 14:51:39 -040010178 goto out_free_phba;
James Smartda0436e2009-05-22 14:51:39 -040010179
10180 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
10181 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
10182 if (error)
10183 goto out_disable_pci_dev;
10184
10185 /* Set up SLI-4 specific device PCI memory space */
10186 error = lpfc_sli4_pci_mem_setup(phba);
10187 if (error) {
10188 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10189 "1410 Failed to set up pci memory space.\n");
10190 goto out_disable_pci_dev;
10191 }
10192
10193 /* Set up phase-1 common device driver resources */
10194 error = lpfc_setup_driver_resource_phase1(phba);
10195 if (error) {
10196 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10197 "1411 Failed to set up driver resource.\n");
10198 goto out_unset_pci_mem_s4;
10199 }
10200
10201 /* Set up SLI-4 Specific device driver resources */
10202 error = lpfc_sli4_driver_resource_setup(phba);
10203 if (error) {
10204 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10205 "1412 Failed to set up driver resource.\n");
10206 goto out_unset_pci_mem_s4;
10207 }
10208
10209 /* Initialize and populate the iocb list per host */
James Smart2a9bf3d2010-06-07 15:24:45 -040010210
10211 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10212 "2821 initialize iocb list %d.\n",
10213 phba->cfg_iocb_cnt*1024);
10214 error = lpfc_init_iocb_list(phba, phba->cfg_iocb_cnt*1024);
10215
James Smartda0436e2009-05-22 14:51:39 -040010216 if (error) {
10217 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10218 "1413 Failed to initialize iocb list.\n");
10219 goto out_unset_driver_resource_s4;
10220 }
10221
James Smart19ca7602010-11-20 23:11:55 -050010222 INIT_LIST_HEAD(&phba->active_rrq_list);
James Smart7d791df2011-07-22 18:37:52 -040010223 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
James Smart19ca7602010-11-20 23:11:55 -050010224
James Smartda0436e2009-05-22 14:51:39 -040010225 /* Set up common device driver resources */
10226 error = lpfc_setup_driver_resource_phase2(phba);
10227 if (error) {
10228 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10229 "1414 Failed to set up driver resource.\n");
10230 goto out_free_iocb_list;
10231 }
10232
James Smart079b5c92011-08-21 21:48:49 -040010233 /* Get the default values for Model Name and Description */
10234 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
10235
James Smartda0436e2009-05-22 14:51:39 -040010236 /* Create SCSI host to the physical port */
10237 error = lpfc_create_shost(phba);
10238 if (error) {
10239 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10240 "1415 Failed to create scsi host.\n");
10241 goto out_unset_driver_resource;
10242 }
10243
10244 /* Configure sysfs attributes */
10245 vport = phba->pport;
10246 error = lpfc_alloc_sysfs_attr(vport);
10247 if (error) {
10248 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10249 "1416 Failed to allocate sysfs attr\n");
10250 goto out_destroy_shost;
10251 }
10252
James Smart6669f9b2009-10-02 15:16:45 -040010253 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -040010254 /* Now, trying to enable interrupt and bring up the device */
10255 cfg_mode = phba->cfg_use_msi;
James Smartda0436e2009-05-22 14:51:39 -040010256
James Smart7b15db32013-01-03 15:43:29 -050010257 /* Put device to a known state before enabling interrupt */
10258 lpfc_stop_port(phba);
10259 /* Configure and enable interrupt */
10260 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
10261 if (intr_mode == LPFC_INTR_ERROR) {
10262 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10263 "0426 Failed to enable interrupt.\n");
10264 error = -ENODEV;
10265 goto out_free_sysfs_attr;
James Smartda0436e2009-05-22 14:51:39 -040010266 }
James Smart7b15db32013-01-03 15:43:29 -050010267 /* Default to single EQ for non-MSI-X */
10268 if (phba->intr_type != MSIX)
10269 adjusted_fcp_io_channel = 1;
10270 else
10271 adjusted_fcp_io_channel = phba->cfg_fcp_io_channel;
10272 phba->cfg_fcp_io_channel = adjusted_fcp_io_channel;
10273 /* Set up SLI-4 HBA */
10274 if (lpfc_sli4_hba_setup(phba)) {
10275 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10276 "1421 Failed to set up hba\n");
10277 error = -ENODEV;
10278 goto out_disable_intr;
10279 }
10280
10281 /* Log the current active interrupt mode */
10282 phba->intr_mode = intr_mode;
10283 lpfc_log_intr_mode(phba, intr_mode);
James Smartda0436e2009-05-22 14:51:39 -040010284
10285 /* Perform post initialization setup */
10286 lpfc_post_init_setup(phba);
10287
James Smartc71ab862012-10-31 14:44:33 -040010288 /* check for firmware upgrade or downgrade */
10289 if (phba->cfg_request_firmware_upgrade)
10290 ret = lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
James Smart52d52442011-05-24 11:42:45 -040010291
James Smart1c6834a2009-07-19 10:01:26 -040010292 /* Check if there are static vports to be created. */
10293 lpfc_create_static_vport(phba);
James Smartda0436e2009-05-22 14:51:39 -040010294 return 0;
10295
10296out_disable_intr:
10297 lpfc_sli4_disable_intr(phba);
10298out_free_sysfs_attr:
10299 lpfc_free_sysfs_attr(vport);
10300out_destroy_shost:
10301 lpfc_destroy_shost(phba);
10302out_unset_driver_resource:
10303 lpfc_unset_driver_resource_phase2(phba);
10304out_free_iocb_list:
10305 lpfc_free_iocb_list(phba);
10306out_unset_driver_resource_s4:
10307 lpfc_sli4_driver_resource_unset(phba);
10308out_unset_pci_mem_s4:
10309 lpfc_sli4_pci_mem_unset(phba);
10310out_disable_pci_dev:
10311 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040010312 if (shost)
10313 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -040010314out_free_phba:
10315 lpfc_hba_free(phba);
10316 return error;
10317}
10318
10319/**
10320 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
10321 * @pdev: pointer to PCI device
10322 *
10323 * This routine is called from the kernel's PCI subsystem to device with
10324 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
10325 * removed from PCI bus, it performs all the necessary cleanup for the HBA
10326 * device to be removed from the PCI subsystem properly.
10327 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010328static void
James Smartda0436e2009-05-22 14:51:39 -040010329lpfc_pci_remove_one_s4(struct pci_dev *pdev)
10330{
10331 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10332 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
10333 struct lpfc_vport **vports;
10334 struct lpfc_hba *phba = vport->phba;
10335 int i;
10336
10337 /* Mark the device unloading flag */
10338 spin_lock_irq(&phba->hbalock);
10339 vport->load_flag |= FC_UNLOADING;
10340 spin_unlock_irq(&phba->hbalock);
10341
10342 /* Free the HBA sysfs attributes */
10343 lpfc_free_sysfs_attr(vport);
10344
10345 /* Release all the vports against this physical port */
10346 vports = lpfc_create_vport_work_array(phba);
10347 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040010348 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
10349 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
10350 continue;
James Smartda0436e2009-05-22 14:51:39 -040010351 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040010352 }
James Smartda0436e2009-05-22 14:51:39 -040010353 lpfc_destroy_vport_work_array(phba, vports);
10354
10355 /* Remove FC host and then SCSI host with the physical port */
10356 fc_remove_host(shost);
10357 scsi_remove_host(shost);
10358
10359 /* Perform cleanup on the physical port */
10360 lpfc_cleanup(vport);
10361
10362 /*
10363 * Bring down the SLI Layer. This step disables all interrupts,
10364 * clears the rings, discards all mailbox commands, and resets
10365 * the HBA FCoE function.
10366 */
10367 lpfc_debugfs_terminate(vport);
10368 lpfc_sli4_hba_unset(phba);
10369
10370 spin_lock_irq(&phba->hbalock);
10371 list_del_init(&vport->listentry);
10372 spin_unlock_irq(&phba->hbalock);
10373
James Smart3677a3a2010-09-29 11:19:14 -040010374 /* Perform scsi free before driver resource_unset since scsi
James Smartda0436e2009-05-22 14:51:39 -040010375 * buffers are released to their corresponding pools here.
10376 */
10377 lpfc_scsi_free(phba);
James Smart67d12732012-08-03 12:36:13 -040010378
James Smartda0436e2009-05-22 14:51:39 -040010379 lpfc_sli4_driver_resource_unset(phba);
10380
10381 /* Unmap adapter Control and Doorbell registers */
10382 lpfc_sli4_pci_mem_unset(phba);
10383
10384 /* Release PCI resources and disable device's PCI function */
10385 scsi_host_put(shost);
10386 lpfc_disable_pci_dev(phba);
10387
10388 /* Finally, free the driver's device data structure */
10389 lpfc_hba_free(phba);
10390
10391 return;
10392}
10393
10394/**
10395 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
10396 * @pdev: pointer to PCI device
10397 * @msg: power management message
10398 *
10399 * This routine is called from the kernel's PCI subsystem to support system
10400 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
10401 * this method, it quiesces the device by stopping the driver's worker
10402 * thread for the device, turning off device's interrupt and DMA, and bring
10403 * the device offline. Note that as the driver implements the minimum PM
10404 * requirements to a power-aware driver's PM support for suspend/resume -- all
10405 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
10406 * method call will be treated as SUSPEND and the driver will fully
10407 * reinitialize its device during resume() method call, the driver will set
10408 * device to PCI_D3hot state in PCI config space instead of setting it
10409 * according to the @msg provided by the PM.
10410 *
10411 * Return code
10412 * 0 - driver suspended the device
10413 * Error otherwise
10414 **/
10415static int
10416lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
10417{
10418 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10419 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10420
10421 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart75baf692010-06-08 18:31:21 -040010422 "2843 PCI device Power Management suspend.\n");
James Smartda0436e2009-05-22 14:51:39 -040010423
10424 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040010425 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smartda0436e2009-05-22 14:51:39 -040010426 lpfc_offline(phba);
10427 kthread_stop(phba->worker_thread);
10428
10429 /* Disable interrupt from device */
10430 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040010431 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -040010432
10433 /* Save device state to PCI config space */
10434 pci_save_state(pdev);
10435 pci_set_power_state(pdev, PCI_D3hot);
10436
10437 return 0;
10438}
10439
10440/**
10441 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
10442 * @pdev: pointer to PCI device
10443 *
10444 * This routine is called from the kernel's PCI subsystem to support system
10445 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
10446 * this method, it restores the device's PCI config space state and fully
10447 * reinitializes the device and brings it online. Note that as the driver
10448 * implements the minimum PM requirements to a power-aware driver's PM for
10449 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
10450 * to the suspend() method call will be treated as SUSPEND and the driver
10451 * will fully reinitialize its device during resume() method call, the device
10452 * will be set to PCI_D0 directly in PCI config space before restoring the
10453 * state.
10454 *
10455 * Return code
10456 * 0 - driver suspended the device
10457 * Error otherwise
10458 **/
10459static int
10460lpfc_pci_resume_one_s4(struct pci_dev *pdev)
10461{
10462 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10463 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10464 uint32_t intr_mode;
10465 int error;
10466
10467 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10468 "0292 PCI device Power Management resume.\n");
10469
10470 /* Restore device state from PCI config space */
10471 pci_set_power_state(pdev, PCI_D0);
10472 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050010473
10474 /*
10475 * As the new kernel behavior of pci_restore_state() API call clears
10476 * device saved_state flag, need to save the restored state again.
10477 */
10478 pci_save_state(pdev);
10479
James Smartda0436e2009-05-22 14:51:39 -040010480 if (pdev->is_busmaster)
10481 pci_set_master(pdev);
10482
10483 /* Startup the kernel thread for this host adapter. */
10484 phba->worker_thread = kthread_run(lpfc_do_work, phba,
10485 "lpfc_worker_%d", phba->brd_no);
10486 if (IS_ERR(phba->worker_thread)) {
10487 error = PTR_ERR(phba->worker_thread);
10488 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10489 "0293 PM resume failed to start worker "
10490 "thread: error=x%x.\n", error);
10491 return error;
10492 }
10493
10494 /* Configure and enable interrupt */
10495 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
10496 if (intr_mode == LPFC_INTR_ERROR) {
10497 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10498 "0294 PM resume Failed to enable interrupt\n");
10499 return -EIO;
10500 } else
10501 phba->intr_mode = intr_mode;
10502
10503 /* Restart HBA and bring it online */
10504 lpfc_sli_brdrestart(phba);
10505 lpfc_online(phba);
10506
10507 /* Log the current active interrupt mode */
10508 lpfc_log_intr_mode(phba, phba->intr_mode);
10509
10510 return 0;
10511}
10512
10513/**
James Smart75baf692010-06-08 18:31:21 -040010514 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
10515 * @phba: pointer to lpfc hba data structure.
10516 *
10517 * This routine is called to prepare the SLI4 device for PCI slot recover. It
10518 * aborts all the outstanding SCSI I/Os to the pci device.
10519 **/
10520static void
10521lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
10522{
James Smart75baf692010-06-08 18:31:21 -040010523 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10524 "2828 PCI channel I/O abort preparing for recovery\n");
10525 /*
10526 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
10527 * and let the SCSI mid-layer to retry them to recover.
10528 */
James Smartdb55fba2014-04-04 13:52:02 -040010529 lpfc_sli_abort_fcp_rings(phba);
James Smart75baf692010-06-08 18:31:21 -040010530}
10531
10532/**
10533 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
10534 * @phba: pointer to lpfc hba data structure.
10535 *
10536 * This routine is called to prepare the SLI4 device for PCI slot reset. It
10537 * disables the device interrupt and pci device, and aborts the internal FCP
10538 * pending I/Os.
10539 **/
10540static void
10541lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
10542{
10543 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10544 "2826 PCI channel disable preparing for reset\n");
10545
10546 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040010547 lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
James Smart75baf692010-06-08 18:31:21 -040010548
10549 /* Block all SCSI devices' I/Os on the host */
10550 lpfc_scsi_dev_block(phba);
10551
James Smartea714f32013-04-17 20:18:47 -040010552 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
10553 lpfc_sli_flush_fcp_rings(phba);
10554
James Smart75baf692010-06-08 18:31:21 -040010555 /* stop all timers */
10556 lpfc_stop_hba_timers(phba);
10557
10558 /* Disable interrupt and pci device */
10559 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040010560 lpfc_sli4_queue_destroy(phba);
James Smart75baf692010-06-08 18:31:21 -040010561 pci_disable_device(phba->pcidev);
James Smart75baf692010-06-08 18:31:21 -040010562}
10563
10564/**
10565 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
10566 * @phba: pointer to lpfc hba data structure.
10567 *
10568 * This routine is called to prepare the SLI4 device for PCI slot permanently
10569 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
10570 * pending I/Os.
10571 **/
10572static void
10573lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
10574{
10575 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10576 "2827 PCI channel permanent disable for failure\n");
10577
10578 /* Block all SCSI devices' I/Os on the host */
10579 lpfc_scsi_dev_block(phba);
10580
10581 /* stop all timers */
10582 lpfc_stop_hba_timers(phba);
10583
10584 /* Clean up all driver's outstanding SCSI I/Os */
10585 lpfc_sli_flush_fcp_rings(phba);
10586}
10587
10588/**
James Smartda0436e2009-05-22 14:51:39 -040010589 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
10590 * @pdev: pointer to PCI device.
10591 * @state: the current PCI connection state.
10592 *
10593 * This routine is called from the PCI subsystem for error handling to device
10594 * with SLI-4 interface spec. This function is called by the PCI subsystem
10595 * after a PCI bus error affecting this device has been detected. When this
10596 * function is invoked, it will need to stop all the I/Os and interrupt(s)
10597 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
10598 * for the PCI subsystem to perform proper recovery as desired.
10599 *
10600 * Return codes
10601 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10602 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10603 **/
10604static pci_ers_result_t
10605lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
10606{
James Smart75baf692010-06-08 18:31:21 -040010607 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10608 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10609
10610 switch (state) {
10611 case pci_channel_io_normal:
10612 /* Non-fatal error, prepare for recovery */
10613 lpfc_sli4_prep_dev_for_recover(phba);
10614 return PCI_ERS_RESULT_CAN_RECOVER;
10615 case pci_channel_io_frozen:
10616 /* Fatal error, prepare for slot reset */
10617 lpfc_sli4_prep_dev_for_reset(phba);
10618 return PCI_ERS_RESULT_NEED_RESET;
10619 case pci_channel_io_perm_failure:
10620 /* Permanent failure, prepare for device down */
10621 lpfc_sli4_prep_dev_for_perm_failure(phba);
10622 return PCI_ERS_RESULT_DISCONNECT;
10623 default:
10624 /* Unknown state, prepare and request slot reset */
10625 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10626 "2825 Unknown PCI error state: x%x\n", state);
10627 lpfc_sli4_prep_dev_for_reset(phba);
10628 return PCI_ERS_RESULT_NEED_RESET;
10629 }
James Smartda0436e2009-05-22 14:51:39 -040010630}
10631
10632/**
10633 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
10634 * @pdev: pointer to PCI device.
10635 *
10636 * This routine is called from the PCI subsystem for error handling to device
10637 * with SLI-4 interface spec. It is called after PCI bus has been reset to
10638 * restart the PCI card from scratch, as if from a cold-boot. During the
10639 * PCI subsystem error recovery, after the driver returns
10640 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
10641 * recovery and then call this routine before calling the .resume method to
10642 * recover the device. This function will initialize the HBA device, enable
10643 * the interrupt, but it will just put the HBA to offline state without
10644 * passing any I/O traffic.
10645 *
10646 * Return codes
10647 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10648 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10649 */
10650static pci_ers_result_t
10651lpfc_io_slot_reset_s4(struct pci_dev *pdev)
10652{
James Smart75baf692010-06-08 18:31:21 -040010653 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10654 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10655 struct lpfc_sli *psli = &phba->sli;
10656 uint32_t intr_mode;
10657
10658 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
10659 if (pci_enable_device_mem(pdev)) {
10660 printk(KERN_ERR "lpfc: Cannot re-enable "
10661 "PCI device after reset.\n");
10662 return PCI_ERS_RESULT_DISCONNECT;
10663 }
10664
10665 pci_restore_state(pdev);
James Smart0a96e972011-07-22 18:37:28 -040010666
10667 /*
10668 * As the new kernel behavior of pci_restore_state() API call clears
10669 * device saved_state flag, need to save the restored state again.
10670 */
10671 pci_save_state(pdev);
10672
James Smart75baf692010-06-08 18:31:21 -040010673 if (pdev->is_busmaster)
10674 pci_set_master(pdev);
10675
10676 spin_lock_irq(&phba->hbalock);
10677 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
10678 spin_unlock_irq(&phba->hbalock);
10679
10680 /* Configure and enable interrupt */
10681 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
10682 if (intr_mode == LPFC_INTR_ERROR) {
10683 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10684 "2824 Cannot re-enable interrupt after "
10685 "slot reset.\n");
10686 return PCI_ERS_RESULT_DISCONNECT;
10687 } else
10688 phba->intr_mode = intr_mode;
10689
10690 /* Log the current active interrupt mode */
10691 lpfc_log_intr_mode(phba, phba->intr_mode);
10692
James Smartda0436e2009-05-22 14:51:39 -040010693 return PCI_ERS_RESULT_RECOVERED;
10694}
10695
10696/**
10697 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
10698 * @pdev: pointer to PCI device
10699 *
10700 * This routine is called from the PCI subsystem for error handling to device
10701 * with SLI-4 interface spec. It is called when kernel error recovery tells
10702 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
10703 * error recovery. After this call, traffic can start to flow from this device
10704 * again.
10705 **/
10706static void
10707lpfc_io_resume_s4(struct pci_dev *pdev)
10708{
James Smart75baf692010-06-08 18:31:21 -040010709 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10710 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10711
10712 /*
10713 * In case of slot reset, as function reset is performed through
10714 * mailbox command which needs DMA to be enabled, this operation
10715 * has to be moved to the io resume phase. Taking device offline
10716 * will perform the necessary cleanup.
10717 */
10718 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
10719 /* Perform device reset */
James Smart618a5232012-06-12 13:54:36 -040010720 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040010721 lpfc_offline(phba);
10722 lpfc_sli_brdrestart(phba);
10723 /* Bring the device back online */
10724 lpfc_online(phba);
10725 }
10726
10727 /* Clean up Advanced Error Reporting (AER) if needed */
10728 if (phba->hba_flag & HBA_AER_ENABLED)
10729 pci_cleanup_aer_uncorrect_error_status(pdev);
James Smartda0436e2009-05-22 14:51:39 -040010730}
10731
10732/**
James Smart3772a992009-05-22 14:50:54 -040010733 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
10734 * @pdev: pointer to PCI device
10735 * @pid: pointer to PCI device identifier
10736 *
10737 * This routine is to be registered to the kernel's PCI subsystem. When an
10738 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
10739 * at PCI device-specific information of the device and driver to see if the
10740 * driver state that it can support this kind of device. If the match is
10741 * successful, the driver core invokes this routine. This routine dispatches
10742 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
10743 * do all the initialization that it needs to do to handle the HBA device
10744 * properly.
10745 *
10746 * Return code
10747 * 0 - driver can claim the device
10748 * negative value - driver can not claim the device
10749 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010750static int
James Smart3772a992009-05-22 14:50:54 -040010751lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
10752{
10753 int rc;
James Smart8fa38512009-07-19 10:01:03 -040010754 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -040010755
James Smart28baac72010-02-12 14:42:03 -050010756 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -040010757 return -ENODEV;
10758
James Smart8fa38512009-07-19 10:01:03 -040010759 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -050010760 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -040010761 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040010762 else
James Smart3772a992009-05-22 14:50:54 -040010763 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040010764
James Smart3772a992009-05-22 14:50:54 -040010765 return rc;
10766}
10767
10768/**
10769 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
10770 * @pdev: pointer to PCI device
10771 *
10772 * This routine is to be registered to the kernel's PCI subsystem. When an
10773 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
10774 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
10775 * remove routine, which will perform all the necessary cleanup for the
10776 * device to be removed from the PCI subsystem properly.
10777 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010778static void
James Smart3772a992009-05-22 14:50:54 -040010779lpfc_pci_remove_one(struct pci_dev *pdev)
10780{
10781 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10782 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10783
10784 switch (phba->pci_dev_grp) {
10785 case LPFC_PCI_DEV_LP:
10786 lpfc_pci_remove_one_s3(pdev);
10787 break;
James Smartda0436e2009-05-22 14:51:39 -040010788 case LPFC_PCI_DEV_OC:
10789 lpfc_pci_remove_one_s4(pdev);
10790 break;
James Smart3772a992009-05-22 14:50:54 -040010791 default:
10792 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10793 "1424 Invalid PCI device group: 0x%x\n",
10794 phba->pci_dev_grp);
10795 break;
10796 }
10797 return;
10798}
10799
10800/**
10801 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
10802 * @pdev: pointer to PCI device
10803 * @msg: power management message
10804 *
10805 * This routine is to be registered to the kernel's PCI subsystem to support
10806 * system Power Management (PM). When PM invokes this method, it dispatches
10807 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
10808 * suspend the device.
10809 *
10810 * Return code
10811 * 0 - driver suspended the device
10812 * Error otherwise
10813 **/
10814static int
10815lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
10816{
10817 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10818 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10819 int rc = -ENODEV;
10820
10821 switch (phba->pci_dev_grp) {
10822 case LPFC_PCI_DEV_LP:
10823 rc = lpfc_pci_suspend_one_s3(pdev, msg);
10824 break;
James Smartda0436e2009-05-22 14:51:39 -040010825 case LPFC_PCI_DEV_OC:
10826 rc = lpfc_pci_suspend_one_s4(pdev, msg);
10827 break;
James Smart3772a992009-05-22 14:50:54 -040010828 default:
10829 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10830 "1425 Invalid PCI device group: 0x%x\n",
10831 phba->pci_dev_grp);
10832 break;
10833 }
10834 return rc;
10835}
10836
10837/**
10838 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
10839 * @pdev: pointer to PCI device
10840 *
10841 * This routine is to be registered to the kernel's PCI subsystem to support
10842 * system Power Management (PM). When PM invokes this method, it dispatches
10843 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
10844 * resume the device.
10845 *
10846 * Return code
10847 * 0 - driver suspended the device
10848 * Error otherwise
10849 **/
10850static int
10851lpfc_pci_resume_one(struct pci_dev *pdev)
10852{
10853 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10854 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10855 int rc = -ENODEV;
10856
10857 switch (phba->pci_dev_grp) {
10858 case LPFC_PCI_DEV_LP:
10859 rc = lpfc_pci_resume_one_s3(pdev);
10860 break;
James Smartda0436e2009-05-22 14:51:39 -040010861 case LPFC_PCI_DEV_OC:
10862 rc = lpfc_pci_resume_one_s4(pdev);
10863 break;
James Smart3772a992009-05-22 14:50:54 -040010864 default:
10865 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10866 "1426 Invalid PCI device group: 0x%x\n",
10867 phba->pci_dev_grp);
10868 break;
10869 }
10870 return rc;
10871}
10872
10873/**
10874 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
10875 * @pdev: pointer to PCI device.
10876 * @state: the current PCI connection state.
10877 *
10878 * This routine is registered to the PCI subsystem for error handling. This
10879 * function is called by the PCI subsystem after a PCI bus error affecting
10880 * this device has been detected. When this routine is invoked, it dispatches
10881 * the action to the proper SLI-3 or SLI-4 device error detected handling
10882 * routine, which will perform the proper error detected operation.
10883 *
10884 * Return codes
10885 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10886 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10887 **/
10888static pci_ers_result_t
10889lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
10890{
10891 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10892 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10893 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
10894
10895 switch (phba->pci_dev_grp) {
10896 case LPFC_PCI_DEV_LP:
10897 rc = lpfc_io_error_detected_s3(pdev, state);
10898 break;
James Smartda0436e2009-05-22 14:51:39 -040010899 case LPFC_PCI_DEV_OC:
10900 rc = lpfc_io_error_detected_s4(pdev, state);
10901 break;
James Smart3772a992009-05-22 14:50:54 -040010902 default:
10903 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10904 "1427 Invalid PCI device group: 0x%x\n",
10905 phba->pci_dev_grp);
10906 break;
10907 }
10908 return rc;
10909}
10910
10911/**
10912 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
10913 * @pdev: pointer to PCI device.
10914 *
10915 * This routine is registered to the PCI subsystem for error handling. This
10916 * function is called after PCI bus has been reset to restart the PCI card
10917 * from scratch, as if from a cold-boot. When this routine is invoked, it
10918 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
10919 * routine, which will perform the proper device reset.
10920 *
10921 * Return codes
10922 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10923 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10924 **/
10925static pci_ers_result_t
10926lpfc_io_slot_reset(struct pci_dev *pdev)
10927{
10928 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10929 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10930 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
10931
10932 switch (phba->pci_dev_grp) {
10933 case LPFC_PCI_DEV_LP:
10934 rc = lpfc_io_slot_reset_s3(pdev);
10935 break;
James Smartda0436e2009-05-22 14:51:39 -040010936 case LPFC_PCI_DEV_OC:
10937 rc = lpfc_io_slot_reset_s4(pdev);
10938 break;
James Smart3772a992009-05-22 14:50:54 -040010939 default:
10940 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10941 "1428 Invalid PCI device group: 0x%x\n",
10942 phba->pci_dev_grp);
10943 break;
10944 }
10945 return rc;
10946}
10947
10948/**
10949 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
10950 * @pdev: pointer to PCI device
10951 *
10952 * This routine is registered to the PCI subsystem for error handling. It
10953 * is called when kernel error recovery tells the lpfc driver that it is
10954 * OK to resume normal PCI operation after PCI bus error recovery. When
10955 * this routine is invoked, it dispatches the action to the proper SLI-3
10956 * or SLI-4 device io_resume routine, which will resume the device operation.
10957 **/
10958static void
10959lpfc_io_resume(struct pci_dev *pdev)
10960{
10961 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10962 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10963
10964 switch (phba->pci_dev_grp) {
10965 case LPFC_PCI_DEV_LP:
10966 lpfc_io_resume_s3(pdev);
10967 break;
James Smartda0436e2009-05-22 14:51:39 -040010968 case LPFC_PCI_DEV_OC:
10969 lpfc_io_resume_s4(pdev);
10970 break;
James Smart3772a992009-05-22 14:50:54 -040010971 default:
10972 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10973 "1429 Invalid PCI device group: 0x%x\n",
10974 phba->pci_dev_grp);
10975 break;
10976 }
10977 return;
10978}
10979
James Smart1ba981f2014-02-20 09:56:45 -050010980/**
10981 * lpfc_sli4_oas_verify - Verify OAS is supported by this adapter
10982 * @phba: pointer to lpfc hba data structure.
10983 *
10984 * This routine checks to see if OAS is supported for this adapter. If
10985 * supported, the configure Flash Optimized Fabric flag is set. Otherwise,
10986 * the enable oas flag is cleared and the pool created for OAS device data
10987 * is destroyed.
10988 *
10989 **/
10990void
10991lpfc_sli4_oas_verify(struct lpfc_hba *phba)
10992{
10993
10994 if (!phba->cfg_EnableXLane)
10995 return;
10996
10997 if (phba->sli4_hba.pc_sli4_params.oas_supported) {
10998 phba->cfg_fof = 1;
10999 } else {
James Smartf38fa0b2014-04-04 13:52:21 -040011000 phba->cfg_fof = 0;
James Smart1ba981f2014-02-20 09:56:45 -050011001 if (phba->device_data_mem_pool)
11002 mempool_destroy(phba->device_data_mem_pool);
11003 phba->device_data_mem_pool = NULL;
11004 }
11005
11006 return;
11007}
11008
11009/**
11010 * lpfc_fof_queue_setup - Set up all the fof queues
11011 * @phba: pointer to lpfc hba data structure.
11012 *
11013 * This routine is invoked to set up all the fof queues for the FC HBA
11014 * operation.
11015 *
11016 * Return codes
11017 * 0 - successful
11018 * -ENOMEM - No available memory
11019 **/
11020int
11021lpfc_fof_queue_setup(struct lpfc_hba *phba)
11022{
11023 struct lpfc_sli *psli = &phba->sli;
11024 int rc;
11025
11026 rc = lpfc_eq_create(phba, phba->sli4_hba.fof_eq, LPFC_MAX_IMAX);
11027 if (rc)
11028 return -ENOMEM;
11029
James Smartf38fa0b2014-04-04 13:52:21 -040011030 if (phba->cfg_fof) {
James Smart1ba981f2014-02-20 09:56:45 -050011031
11032 rc = lpfc_cq_create(phba, phba->sli4_hba.oas_cq,
11033 phba->sli4_hba.fof_eq, LPFC_WCQ, LPFC_FCP);
11034 if (rc)
11035 goto out_oas_cq;
11036
11037 rc = lpfc_wq_create(phba, phba->sli4_hba.oas_wq,
11038 phba->sli4_hba.oas_cq, LPFC_FCP);
11039 if (rc)
11040 goto out_oas_wq;
11041
11042 phba->sli4_hba.oas_cq->pring = &psli->ring[LPFC_FCP_OAS_RING];
11043 phba->sli4_hba.oas_ring = &psli->ring[LPFC_FCP_OAS_RING];
11044 }
11045
11046 return 0;
11047
11048out_oas_wq:
James Smartf38fa0b2014-04-04 13:52:21 -040011049 lpfc_cq_destroy(phba, phba->sli4_hba.oas_cq);
James Smart1ba981f2014-02-20 09:56:45 -050011050out_oas_cq:
11051 lpfc_eq_destroy(phba, phba->sli4_hba.fof_eq);
11052 return rc;
11053
11054}
11055
11056/**
11057 * lpfc_fof_queue_create - Create all the fof queues
11058 * @phba: pointer to lpfc hba data structure.
11059 *
11060 * This routine is invoked to allocate all the fof queues for the FC HBA
11061 * operation. For each SLI4 queue type, the parameters such as queue entry
11062 * count (queue depth) shall be taken from the module parameter. For now,
11063 * we just use some constant number as place holder.
11064 *
11065 * Return codes
11066 * 0 - successful
11067 * -ENOMEM - No availble memory
11068 * -EIO - The mailbox failed to complete successfully.
11069 **/
11070int
11071lpfc_fof_queue_create(struct lpfc_hba *phba)
11072{
11073 struct lpfc_queue *qdesc;
11074
11075 /* Create FOF EQ */
11076 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
11077 phba->sli4_hba.eq_ecount);
11078 if (!qdesc)
11079 goto out_error;
11080
11081 phba->sli4_hba.fof_eq = qdesc;
11082
James Smartf38fa0b2014-04-04 13:52:21 -040011083 if (phba->cfg_fof) {
James Smart1ba981f2014-02-20 09:56:45 -050011084
11085 /* Create OAS CQ */
11086 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
11087 phba->sli4_hba.cq_ecount);
11088 if (!qdesc)
11089 goto out_error;
11090
11091 phba->sli4_hba.oas_cq = qdesc;
11092
11093 /* Create OAS WQ */
11094 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
11095 phba->sli4_hba.wq_ecount);
11096 if (!qdesc)
11097 goto out_error;
11098
11099 phba->sli4_hba.oas_wq = qdesc;
11100
11101 }
11102 return 0;
11103
11104out_error:
11105 lpfc_fof_queue_destroy(phba);
11106 return -ENOMEM;
11107}
11108
11109/**
11110 * lpfc_fof_queue_destroy - Destroy all the fof queues
11111 * @phba: pointer to lpfc hba data structure.
11112 *
11113 * This routine is invoked to release all the SLI4 queues with the FC HBA
11114 * operation.
11115 *
11116 * Return codes
11117 * 0 - successful
11118 **/
11119int
11120lpfc_fof_queue_destroy(struct lpfc_hba *phba)
11121{
11122 /* Release FOF Event queue */
11123 if (phba->sli4_hba.fof_eq != NULL) {
11124 lpfc_sli4_queue_free(phba->sli4_hba.fof_eq);
11125 phba->sli4_hba.fof_eq = NULL;
11126 }
11127
11128 /* Release OAS Completion queue */
11129 if (phba->sli4_hba.oas_cq != NULL) {
11130 lpfc_sli4_queue_free(phba->sli4_hba.oas_cq);
11131 phba->sli4_hba.oas_cq = NULL;
11132 }
11133
11134 /* Release OAS Work queue */
11135 if (phba->sli4_hba.oas_wq != NULL) {
11136 lpfc_sli4_queue_free(phba->sli4_hba.oas_wq);
11137 phba->sli4_hba.oas_wq = NULL;
11138 }
11139 return 0;
11140}
11141
dea31012005-04-17 16:05:31 -050011142static struct pci_device_id lpfc_id_table[] = {
11143 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
11144 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -040011145 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
11146 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011147 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
11148 PCI_ANY_ID, PCI_ANY_ID, },
11149 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
11150 PCI_ANY_ID, PCI_ANY_ID, },
11151 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
11152 PCI_ANY_ID, PCI_ANY_ID, },
11153 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
11154 PCI_ANY_ID, PCI_ANY_ID, },
11155 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
11156 PCI_ANY_ID, PCI_ANY_ID, },
11157 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
11158 PCI_ANY_ID, PCI_ANY_ID, },
11159 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
11160 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050011161 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
11162 PCI_ANY_ID, PCI_ANY_ID, },
11163 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
11164 PCI_ANY_ID, PCI_ANY_ID, },
11165 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
11166 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011167 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
11168 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050011169 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
11170 PCI_ANY_ID, PCI_ANY_ID, },
11171 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
11172 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011173 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
11174 PCI_ANY_ID, PCI_ANY_ID, },
11175 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
11176 PCI_ANY_ID, PCI_ANY_ID, },
11177 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
11178 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -040011179 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
11180 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050011181 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
11182 PCI_ANY_ID, PCI_ANY_ID, },
11183 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
11184 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011185 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
11186 PCI_ANY_ID, PCI_ANY_ID, },
11187 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
11188 PCI_ANY_ID, PCI_ANY_ID, },
11189 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
11190 PCI_ANY_ID, PCI_ANY_ID, },
11191 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
11192 PCI_ANY_ID, PCI_ANY_ID, },
11193 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
11194 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050011195 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
11196 PCI_ANY_ID, PCI_ANY_ID, },
11197 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
11198 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -040011199 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
11200 PCI_ANY_ID, PCI_ANY_ID, },
11201 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
11202 PCI_ANY_ID, PCI_ANY_ID, },
11203 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
11204 PCI_ANY_ID, PCI_ANY_ID, },
11205 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
11206 PCI_ANY_ID, PCI_ANY_ID, },
11207 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
11208 PCI_ANY_ID, PCI_ANY_ID, },
11209 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
11210 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -040011211 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
11212 PCI_ANY_ID, PCI_ANY_ID, },
11213 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
11214 PCI_ANY_ID, PCI_ANY_ID, },
11215 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
11216 PCI_ANY_ID, PCI_ANY_ID, },
James Smart3772a992009-05-22 14:50:54 -040011217 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
11218 PCI_ANY_ID, PCI_ANY_ID, },
James Smarta747c9c2009-11-18 15:41:10 -050011219 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
11220 PCI_ANY_ID, PCI_ANY_ID, },
11221 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
James Smart6669f9b2009-10-02 15:16:45 -040011222 PCI_ANY_ID, PCI_ANY_ID, },
James Smart98fc5dd2010-06-07 15:24:29 -040011223 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BALIUS,
11224 PCI_ANY_ID, PCI_ANY_ID, },
James Smart085c6472010-11-20 23:11:37 -050011225 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC,
11226 PCI_ANY_ID, PCI_ANY_ID, },
11227 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE,
11228 PCI_ANY_ID, PCI_ANY_ID, },
James Smartc0c11512011-05-24 11:41:34 -040011229 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC_VF,
11230 PCI_ANY_ID, PCI_ANY_ID, },
11231 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE_VF,
11232 PCI_ANY_ID, PCI_ANY_ID, },
James Smartf8cafd32012-08-03 12:42:51 -040011233 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SKYHAWK,
11234 PCI_ANY_ID, PCI_ANY_ID, },
11235 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SKYHAWK_VF,
11236 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011237 { 0 }
11238};
11239
11240MODULE_DEVICE_TABLE(pci, lpfc_id_table);
11241
Stephen Hemmingera55b2d22012-09-07 09:33:16 -070011242static const struct pci_error_handlers lpfc_err_handler = {
Linas Vepstas8d63f372007-02-14 14:28:36 -060011243 .error_detected = lpfc_io_error_detected,
11244 .slot_reset = lpfc_io_slot_reset,
11245 .resume = lpfc_io_resume,
11246};
11247
dea31012005-04-17 16:05:31 -050011248static struct pci_driver lpfc_driver = {
11249 .name = LPFC_DRIVER_NAME,
11250 .id_table = lpfc_id_table,
11251 .probe = lpfc_pci_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011252 .remove = lpfc_pci_remove_one,
James Smart3a55b532008-12-04 22:38:54 -050011253 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -040011254 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -050011255 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -050011256};
11257
James Smart3ef6d242012-01-18 16:23:48 -050011258static const struct file_operations lpfc_mgmt_fop = {
Al Viro858feac2013-04-14 22:39:37 -040011259 .owner = THIS_MODULE,
James Smart3ef6d242012-01-18 16:23:48 -050011260};
11261
11262static struct miscdevice lpfc_mgmt_dev = {
11263 .minor = MISC_DYNAMIC_MINOR,
11264 .name = "lpfcmgmt",
11265 .fops = &lpfc_mgmt_fop,
11266};
11267
James Smarte59058c2008-08-24 21:49:00 -040011268/**
James Smart3621a712009-04-06 18:47:14 -040011269 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -040011270 *
11271 * This routine is to be invoked when the lpfc module is loaded into the
11272 * kernel. The special kernel macro module_init() is used to indicate the
11273 * role of this routine to the kernel as lpfc module entry point.
11274 *
11275 * Return codes
11276 * 0 - successful
11277 * -ENOMEM - FC attach transport failed
11278 * all others - failed
11279 */
dea31012005-04-17 16:05:31 -050011280static int __init
11281lpfc_init(void)
11282{
James Smart7bb03bb2013-04-17 20:19:16 -040011283 int cpu;
dea31012005-04-17 16:05:31 -050011284 int error = 0;
11285
11286 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040011287 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -050011288
James Smart3ef6d242012-01-18 16:23:48 -050011289 error = misc_register(&lpfc_mgmt_dev);
11290 if (error)
11291 printk(KERN_ERR "Could not register lpfcmgmt device, "
11292 "misc_register returned with status %d", error);
11293
James Smart7ee5d432007-10-27 13:37:17 -040011294 if (lpfc_enable_npiv) {
11295 lpfc_transport_functions.vport_create = lpfc_vport_create;
11296 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
11297 }
dea31012005-04-17 16:05:31 -050011298 lpfc_transport_template =
11299 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -040011300 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -050011301 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -040011302 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -040011303 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -040011304 fc_attach_transport(&lpfc_vport_transport_functions);
11305 if (lpfc_vport_transport_template == NULL) {
11306 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040011307 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -040011308 }
James Smart7ee5d432007-10-27 13:37:17 -040011309 }
James Smart7bb03bb2013-04-17 20:19:16 -040011310
11311 /* Initialize in case vector mapping is needed */
James Smartb246de12013-05-31 17:03:07 -040011312 lpfc_used_cpu = NULL;
11313 lpfc_present_cpu = 0;
11314 for_each_present_cpu(cpu)
11315 lpfc_present_cpu++;
James Smart7bb03bb2013-04-17 20:19:16 -040011316
dea31012005-04-17 16:05:31 -050011317 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -050011318 if (error) {
dea31012005-04-17 16:05:31 -050011319 fc_release_transport(lpfc_transport_template);
James Smartd7c255b2008-08-24 21:50:00 -040011320 if (lpfc_enable_npiv)
11321 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -050011322 }
dea31012005-04-17 16:05:31 -050011323
11324 return error;
11325}
11326
James Smarte59058c2008-08-24 21:49:00 -040011327/**
James Smart3621a712009-04-06 18:47:14 -040011328 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -040011329 *
11330 * This routine is invoked when the lpfc module is removed from the kernel.
11331 * The special kernel macro module_exit() is used to indicate the role of
11332 * this routine to the kernel as lpfc module exit point.
11333 */
dea31012005-04-17 16:05:31 -050011334static void __exit
11335lpfc_exit(void)
11336{
James Smart3ef6d242012-01-18 16:23:48 -050011337 misc_deregister(&lpfc_mgmt_dev);
dea31012005-04-17 16:05:31 -050011338 pci_unregister_driver(&lpfc_driver);
11339 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040011340 if (lpfc_enable_npiv)
11341 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -050011342 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040011343 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
11344 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050011345 (1L << _dump_buf_data_order), _dump_buf_data);
11346 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
11347 }
11348
11349 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -040011350 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
11351 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050011352 (1L << _dump_buf_dif_order), _dump_buf_dif);
11353 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
11354 }
James Smartb246de12013-05-31 17:03:07 -040011355 kfree(lpfc_used_cpu);
dea31012005-04-17 16:05:31 -050011356}
11357
11358module_init(lpfc_init);
11359module_exit(lpfc_exit);
11360MODULE_LICENSE("GPL");
11361MODULE_DESCRIPTION(LPFC_MODULE_DESC);
11362MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
11363MODULE_VERSION("0:" LPFC_DRIVER_VERSION);