blob: 9f3efdb9e83377737f2c744f34f4a05ec7d486d5 [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 Smart92c13f22013-05-31 17:05:45 -04004 * Copyright (C) 2004-2013 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/delay.h>
24#include <linux/dma-mapping.h>
25#include <linux/idr.h>
26#include <linux/interrupt.h>
Paul Gortmakeracf3368f2011-05-27 09:47:43 -040027#include <linux/module.h>
dea31012005-04-17 16:05:31 -050028#include <linux/kthread.h>
29#include <linux/pci.h>
30#include <linux/spinlock.h>
James Smart92d7f7b2007-06-17 19:56:38 -050031#include <linux/ctype.h>
James Smart0d878412009-10-02 15:16:56 -040032#include <linux/aer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
James Smart52d52442011-05-24 11:42:45 -040034#include <linux/firmware.h>
James Smart3ef6d242012-01-18 16:23:48 -050035#include <linux/miscdevice.h>
James Smart7bb03bb2013-04-17 20:19:16 -040036#include <linux/percpu.h>
dea31012005-04-17 16:05:31 -050037
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040038#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050039#include <scsi/scsi_device.h>
40#include <scsi/scsi_host.h>
41#include <scsi/scsi_transport_fc.h>
42
James Smartda0436e2009-05-22 14:51:39 -040043#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050044#include "lpfc_hw.h"
45#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040046#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040047#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050048#include "lpfc_disc.h"
49#include "lpfc_scsi.h"
50#include "lpfc.h"
51#include "lpfc_logmsg.h"
52#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050053#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050054#include "lpfc_version.h"
55
James Smart81301a92008-12-04 22:39:46 -050056char *_dump_buf_data;
57unsigned long _dump_buf_data_order;
58char *_dump_buf_dif;
59unsigned long _dump_buf_dif_order;
60spinlock_t _dump_buf_lock;
61
James Smart7bb03bb2013-04-17 20:19:16 -040062/* Used when mapping IRQ vectors in a driver centric manner */
James Smartb246de12013-05-31 17:03:07 -040063uint16_t *lpfc_used_cpu;
64uint32_t lpfc_present_cpu;
James Smart7bb03bb2013-04-17 20:19:16 -040065
dea31012005-04-17 16:05:31 -050066static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
67static int lpfc_post_rcv_buf(struct lpfc_hba *);
James Smart5350d872011-10-10 21:33:49 -040068static int lpfc_sli4_queue_verify(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040069static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
70static int lpfc_setup_endian_order(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040071static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
James Smart8a9d2e82012-05-09 21:16:12 -040072static void lpfc_free_els_sgl_list(struct lpfc_hba *);
73static void lpfc_init_sgl_list(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040074static int lpfc_init_active_sgl_array(struct lpfc_hba *);
75static void lpfc_free_active_sgl(struct lpfc_hba *);
76static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
77static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
78static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
79static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
80static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
James Smart618a5232012-06-12 13:54:36 -040081static void lpfc_sli4_disable_intr(struct lpfc_hba *);
82static uint32_t lpfc_sli4_enable_intr(struct lpfc_hba *, uint32_t);
James Smart1ba981f2014-02-20 09:56:45 -050083static void lpfc_sli4_oas_verify(struct lpfc_hba *phba);
dea31012005-04-17 16:05:31 -050084
85static struct scsi_transport_template *lpfc_transport_template = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050086static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea31012005-04-17 16:05:31 -050087static DEFINE_IDR(lpfc_hba_index);
88
James Smarte59058c2008-08-24 21:49:00 -040089/**
James Smart3621a712009-04-06 18:47:14 -040090 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
James Smarte59058c2008-08-24 21:49:00 -040091 * @phba: pointer to lpfc hba data structure.
92 *
93 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
94 * mailbox command. It retrieves the revision information from the HBA and
95 * collects the Vital Product Data (VPD) about the HBA for preparing the
96 * configuration of the HBA.
97 *
98 * Return codes:
99 * 0 - success.
100 * -ERESTART - requests the SLI layer to reset the HBA and try again.
101 * Any other value - indicates an error.
102 **/
dea31012005-04-17 16:05:31 -0500103int
James Smart2e0fef82007-06-17 19:56:36 -0500104lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500105{
106 lpfc_vpd_t *vp = &phba->vpd;
107 int i = 0, rc;
108 LPFC_MBOXQ_t *pmb;
109 MAILBOX_t *mb;
110 char *lpfc_vpd_data = NULL;
111 uint16_t offset = 0;
112 static char licensed[56] =
113 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -0400114 static int init_key = 1;
dea31012005-04-17 16:05:31 -0500115
116 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
117 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500118 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500119 return -ENOMEM;
120 }
121
James Smart04c68492009-05-22 14:52:52 -0400122 mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -0500123 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500124
125 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -0400126 if (init_key) {
127 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -0500128
James Smart65a29c12006-07-06 15:50:50 -0400129 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
130 *ptext = cpu_to_be32(*ptext);
131 init_key = 0;
132 }
dea31012005-04-17 16:05:31 -0500133
134 lpfc_read_nv(phba, pmb);
135 memset((char*)mb->un.varRDnvp.rsvd3, 0,
136 sizeof (mb->un.varRDnvp.rsvd3));
137 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
138 sizeof (licensed));
139
140 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
141
142 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500143 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smarte8b62012007-08-02 11:10:09 -0400144 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500145 "error, mbxCmd x%x READ_NVPARM, "
146 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500147 mb->mbxCommand, mb->mbxStatus);
148 mempool_free(pmb, phba->mbox_mem_pool);
149 return -ERESTART;
150 }
151 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500152 sizeof(phba->wwnn));
153 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
154 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500155 }
156
James Smart92d7f7b2007-06-17 19:56:38 -0500157 phba->sli3_options = 0x0;
158
dea31012005-04-17 16:05:31 -0500159 /* Setup and issue mailbox READ REV command */
160 lpfc_read_rev(phba, pmb);
161 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
162 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500163 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400164 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500165 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500166 mb->mbxCommand, mb->mbxStatus);
167 mempool_free( pmb, phba->mbox_mem_pool);
168 return -ERESTART;
169 }
170
James Smart92d7f7b2007-06-17 19:56:38 -0500171
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500172 /*
173 * The value of rr must be 1 since the driver set the cv field to 1.
174 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500175 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500176 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500177 vp->rev.rBit = 0;
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500178 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400179 "0440 Adapter failed to init, READ_REV has "
180 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500181 mempool_free(pmb, phba->mbox_mem_pool);
182 return -ERESTART;
dea31012005-04-17 16:05:31 -0500183 }
184
James Smart495a7142008-06-14 22:52:59 -0400185 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
186 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -0500187 return -EINVAL;
James Smart495a7142008-06-14 22:52:59 -0400188 }
James Smarted957682007-06-17 19:56:37 -0500189
dea31012005-04-17 16:05:31 -0500190 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500191 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500192 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500193 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
194 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
195 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
196 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500197 vp->rev.biuRev = mb->un.varRdRev.biuRev;
198 vp->rev.smRev = mb->un.varRdRev.smRev;
199 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
200 vp->rev.endecRev = mb->un.varRdRev.endecRev;
201 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
202 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
203 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
204 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
205 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
206 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
207
James Smart92d7f7b2007-06-17 19:56:38 -0500208 /* If the sli feature level is less then 9, we must
209 * tear down all RPIs and VPIs on link down if NPIV
210 * is enabled.
211 */
212 if (vp->rev.feaLevelHigh < 9)
213 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
214
dea31012005-04-17 16:05:31 -0500215 if (lpfc_is_LC_HBA(phba->pcidev->device))
216 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
217 sizeof (phba->RandomData));
218
dea31012005-04-17 16:05:31 -0500219 /* Get adapter VPD information */
dea31012005-04-17 16:05:31 -0500220 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
221 if (!lpfc_vpd_data)
James Smartd7c255b2008-08-24 21:50:00 -0400222 goto out_free_mbox;
dea31012005-04-17 16:05:31 -0500223 do {
James Smarta0c87cb2009-07-19 10:01:10 -0400224 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
dea31012005-04-17 16:05:31 -0500225 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
226
227 if (rc != MBX_SUCCESS) {
228 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400229 "0441 VPD not present on adapter, "
dea31012005-04-17 16:05:31 -0500230 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500231 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500232 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500233 }
James Smart04c68492009-05-22 14:52:52 -0400234 /* dump mem may return a zero when finished or we got a
235 * mailbox error, either way we are done.
236 */
237 if (mb->un.varDmp.word_cnt == 0)
238 break;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500239 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
240 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
James Smartd7c255b2008-08-24 21:50:00 -0400241 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
242 lpfc_vpd_data + offset,
James Smart92d7f7b2007-06-17 19:56:38 -0500243 mb->un.varDmp.word_cnt);
dea31012005-04-17 16:05:31 -0500244 offset += mb->un.varDmp.word_cnt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500245 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
246 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500247
248 kfree(lpfc_vpd_data);
dea31012005-04-17 16:05:31 -0500249out_free_mbox:
250 mempool_free(pmb, phba->mbox_mem_pool);
251 return 0;
252}
253
James Smarte59058c2008-08-24 21:49:00 -0400254/**
James Smart3621a712009-04-06 18:47:14 -0400255 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
James Smarte59058c2008-08-24 21:49:00 -0400256 * @phba: pointer to lpfc hba data structure.
257 * @pmboxq: pointer to the driver internal queue element for mailbox command.
258 *
259 * This is the completion handler for driver's configuring asynchronous event
260 * mailbox command to the device. If the mailbox command returns successfully,
261 * it will set internal async event support flag to 1; otherwise, it will
262 * set internal async event support flag to 0.
263 **/
James Smart57127f12007-10-27 13:37:05 -0400264static void
265lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
266{
James Smart04c68492009-05-22 14:52:52 -0400267 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
James Smart57127f12007-10-27 13:37:05 -0400268 phba->temp_sensor_support = 1;
269 else
270 phba->temp_sensor_support = 0;
271 mempool_free(pmboxq, phba->mbox_mem_pool);
272 return;
273}
274
James Smarte59058c2008-08-24 21:49:00 -0400275/**
James Smart3621a712009-04-06 18:47:14 -0400276 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
James Smart97207482008-12-04 22:39:19 -0500277 * @phba: pointer to lpfc hba data structure.
278 * @pmboxq: pointer to the driver internal queue element for mailbox command.
279 *
280 * This is the completion handler for dump mailbox command for getting
281 * wake up parameters. When this command complete, the response contain
282 * Option rom version of the HBA. This function translate the version number
283 * into a human readable string and store it in OptionROMVersion.
284 **/
285static void
286lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
287{
288 struct prog_id *prg;
289 uint32_t prog_id_word;
290 char dist = ' ';
291 /* character array used for decoding dist type. */
292 char dist_char[] = "nabx";
293
James Smart04c68492009-05-22 14:52:52 -0400294 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
James Smart9f1e1b52008-12-04 22:39:40 -0500295 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500296 return;
James Smart9f1e1b52008-12-04 22:39:40 -0500297 }
James Smart97207482008-12-04 22:39:19 -0500298
299 prg = (struct prog_id *) &prog_id_word;
300
301 /* word 7 contain option rom version */
James Smart04c68492009-05-22 14:52:52 -0400302 prog_id_word = pmboxq->u.mb.un.varWords[7];
James Smart97207482008-12-04 22:39:19 -0500303
304 /* Decode the Option rom version word to a readable string */
305 if (prg->dist < 4)
306 dist = dist_char[prg->dist];
307
308 if ((prg->dist == 3) && (prg->num == 0))
309 sprintf(phba->OptionROMVersion, "%d.%d%d",
310 prg->ver, prg->rev, prg->lev);
311 else
312 sprintf(phba->OptionROMVersion, "%d.%d%d%c%d",
313 prg->ver, prg->rev, prg->lev,
314 dist, prg->num);
James Smart9f1e1b52008-12-04 22:39:40 -0500315 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500316 return;
317}
318
319/**
James Smart05580562011-05-24 11:40:48 -0400320 * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
321 * cfg_soft_wwnn, cfg_soft_wwpn
322 * @vport: pointer to lpfc vport data structure.
323 *
324 *
325 * Return codes
326 * None.
327 **/
328void
329lpfc_update_vport_wwn(struct lpfc_vport *vport)
330{
331 /* If the soft name exists then update it using the service params */
332 if (vport->phba->cfg_soft_wwnn)
333 u64_to_wwn(vport->phba->cfg_soft_wwnn,
334 vport->fc_sparam.nodeName.u.wwn);
335 if (vport->phba->cfg_soft_wwpn)
336 u64_to_wwn(vport->phba->cfg_soft_wwpn,
337 vport->fc_sparam.portName.u.wwn);
338
339 /*
340 * If the name is empty or there exists a soft name
341 * then copy the service params name, otherwise use the fc name
342 */
343 if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
344 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
345 sizeof(struct lpfc_name));
346 else
347 memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
348 sizeof(struct lpfc_name));
349
350 if (vport->fc_portname.u.wwn[0] == 0 || vport->phba->cfg_soft_wwpn)
351 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
352 sizeof(struct lpfc_name));
353 else
354 memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
355 sizeof(struct lpfc_name));
356}
357
358/**
James Smart3621a712009-04-06 18:47:14 -0400359 * lpfc_config_port_post - Perform lpfc initialization after config port
James Smarte59058c2008-08-24 21:49:00 -0400360 * @phba: pointer to lpfc hba data structure.
361 *
362 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
363 * command call. It performs all internal resource and state setups on the
364 * port: post IOCB buffers, enable appropriate host interrupt attentions,
365 * ELS ring timers, etc.
366 *
367 * Return codes
368 * 0 - success.
369 * Any other value - error.
370 **/
dea31012005-04-17 16:05:31 -0500371int
James Smart2e0fef82007-06-17 19:56:36 -0500372lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500373{
James Smart2e0fef82007-06-17 19:56:36 -0500374 struct lpfc_vport *vport = phba->pport;
James Smarta257bf92009-04-06 18:48:10 -0400375 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -0500376 LPFC_MBOXQ_t *pmb;
377 MAILBOX_t *mb;
378 struct lpfc_dmabuf *mp;
379 struct lpfc_sli *psli = &phba->sli;
380 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500381 int i, j;
382 int rc;
dea31012005-04-17 16:05:31 -0500383
James Smart7af67052007-10-27 13:38:11 -0400384 spin_lock_irq(&phba->hbalock);
385 /*
386 * If the Config port completed correctly the HBA is not
387 * over heated any more.
388 */
389 if (phba->over_temp_state == HBA_OVER_TEMP)
390 phba->over_temp_state = HBA_NORMAL_TEMP;
391 spin_unlock_irq(&phba->hbalock);
392
dea31012005-04-17 16:05:31 -0500393 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
394 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500395 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500396 return -ENOMEM;
397 }
James Smart04c68492009-05-22 14:52:52 -0400398 mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500399
dea31012005-04-17 16:05:31 -0500400 /* Get login parameters for NID. */
James Smart9f1177a2010-02-26 14:12:57 -0500401 rc = lpfc_read_sparam(phba, pmb, 0);
402 if (rc) {
403 mempool_free(pmb, phba->mbox_mem_pool);
404 return -ENOMEM;
405 }
406
James Smarted957682007-06-17 19:56:37 -0500407 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500408 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500409 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400410 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500411 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500412 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500413 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500414 mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart9f1177a2010-02-26 14:12:57 -0500415 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500416 lpfc_mbuf_free(phba, mp->virt, mp->phys);
417 kfree(mp);
418 return -EIO;
419 }
420
421 mp = (struct lpfc_dmabuf *) pmb->context1;
422
James Smart2e0fef82007-06-17 19:56:36 -0500423 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500424 lpfc_mbuf_free(phba, mp->virt, mp->phys);
425 kfree(mp);
426 pmb->context1 = NULL;
James Smart05580562011-05-24 11:40:48 -0400427 lpfc_update_vport_wwn(vport);
James Smarta257bf92009-04-06 18:48:10 -0400428
429 /* Update the fc_host data structures with new wwn. */
430 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
431 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart21e9a0a2009-05-22 14:53:21 -0400432 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smarta257bf92009-04-06 18:48:10 -0400433
dea31012005-04-17 16:05:31 -0500434 /* If no serial number in VPD data, use low 6 bytes of WWNN */
435 /* This should be consolidated into parse_vpd ? - mr */
436 if (phba->SerialNumber[0] == 0) {
437 uint8_t *outptr;
438
James Smart2e0fef82007-06-17 19:56:36 -0500439 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500440 for (i = 0; i < 12; i++) {
441 status = *outptr++;
442 j = ((status & 0xf0) >> 4);
443 if (j <= 9)
444 phba->SerialNumber[i] =
445 (char)((uint8_t) 0x30 + (uint8_t) j);
446 else
447 phba->SerialNumber[i] =
448 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
449 i++;
450 j = (status & 0xf);
451 if (j <= 9)
452 phba->SerialNumber[i] =
453 (char)((uint8_t) 0x30 + (uint8_t) j);
454 else
455 phba->SerialNumber[i] =
456 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
457 }
458 }
459
dea31012005-04-17 16:05:31 -0500460 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500461 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500462 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500463 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400464 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500465 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500466 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500467 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500468 mempool_free( pmb, phba->mbox_mem_pool);
469 return -EIO;
470 }
471
James Smarta0c87cb2009-07-19 10:01:10 -0400472 /* Check if the port is disabled */
473 lpfc_sli_read_link_ste(phba);
474
dea31012005-04-17 16:05:31 -0500475 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -0400476 i = (mb->un.varRdConfig.max_xri + 1);
477 if (phba->cfg_hba_queue_depth > i) {
478 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
479 "3359 HBA queue depth changed from %d to %d\n",
480 phba->cfg_hba_queue_depth, i);
481 phba->cfg_hba_queue_depth = i;
482 }
483
484 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
485 i = (mb->un.varRdConfig.max_xri >> 3);
486 if (phba->pport->cfg_lun_queue_depth > i) {
487 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
488 "3360 LUN queue depth changed from %d to %d\n",
489 phba->pport->cfg_lun_queue_depth, i);
490 phba->pport->cfg_lun_queue_depth = i;
491 }
dea31012005-04-17 16:05:31 -0500492
493 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500494
495 /* Get the default values for Model Name and Description */
496 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
497
James Smart2e0fef82007-06-17 19:56:36 -0500498 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500499
James Smart0b727fe2007-10-27 13:37:25 -0400500 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smart7e56aa22012-08-03 12:35:34 -0400501 if (psli->ring[psli->extra_ring].sli.sli3.cmdringaddr)
James Smarta4bc3372006-12-02 13:34:16 -0500502 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
James Smart7e56aa22012-08-03 12:35:34 -0400503 if (psli->ring[psli->fcp_ring].sli.sli3.cmdringaddr)
dea31012005-04-17 16:05:31 -0500504 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
James Smart7e56aa22012-08-03 12:35:34 -0400505 if (psli->ring[psli->next_ring].sli.sli3.cmdringaddr)
dea31012005-04-17 16:05:31 -0500506 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
507
508 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500509 if (phba->sli_rev != 3)
510 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500511
James Smart93996272008-08-24 21:50:30 -0400512 /*
513 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
514 */
515 if (phba->intr_type == MSIX) {
516 rc = lpfc_config_msi(phba, pmb);
517 if (rc) {
518 mempool_free(pmb, phba->mbox_mem_pool);
519 return -EIO;
520 }
521 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
522 if (rc != MBX_SUCCESS) {
523 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
524 "0352 Config MSI mailbox command "
525 "failed, mbxCmd x%x, mbxStatus x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400526 pmb->u.mb.mbxCommand,
527 pmb->u.mb.mbxStatus);
James Smart93996272008-08-24 21:50:30 -0400528 mempool_free(pmb, phba->mbox_mem_pool);
529 return -EIO;
530 }
531 }
532
James Smart04c68492009-05-22 14:52:52 -0400533 spin_lock_irq(&phba->hbalock);
James Smart93996272008-08-24 21:50:30 -0400534 /* Initialize ERATT handling flag */
535 phba->hba_flag &= ~HBA_ERATT_HANDLED;
536
dea31012005-04-17 16:05:31 -0500537 /* Enable appropriate host interrupts */
James Smart9940b972011-03-11 16:06:12 -0500538 if (lpfc_readl(phba->HCregaddr, &status)) {
539 spin_unlock_irq(&phba->hbalock);
540 return -EIO;
541 }
dea31012005-04-17 16:05:31 -0500542 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
543 if (psli->num_rings > 0)
544 status |= HC_R0INT_ENA;
545 if (psli->num_rings > 1)
546 status |= HC_R1INT_ENA;
547 if (psli->num_rings > 2)
548 status |= HC_R2INT_ENA;
549 if (psli->num_rings > 3)
550 status |= HC_R3INT_ENA;
551
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500552 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
553 (phba->cfg_poll & DISABLE_FCP_RING_INT))
James Smart93996272008-08-24 21:50:30 -0400554 status &= ~(HC_R0INT_ENA);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500555
dea31012005-04-17 16:05:31 -0500556 writel(status, phba->HCregaddr);
557 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500558 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500559
James Smart93996272008-08-24 21:50:30 -0400560 /* Set up ring-0 (ELS) timer */
561 timeout = phba->fc_ratov * 2;
James Smart256ec0d2013-04-17 20:14:58 -0400562 mod_timer(&vport->els_tmofunc,
563 jiffies + msecs_to_jiffies(1000 * timeout));
James Smart93996272008-08-24 21:50:30 -0400564 /* Set up heart beat (HB) timer */
James Smart256ec0d2013-04-17 20:14:58 -0400565 mod_timer(&phba->hb_tmofunc,
566 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -0500567 phba->hb_outstanding = 0;
568 phba->last_completion_time = jiffies;
James Smart93996272008-08-24 21:50:30 -0400569 /* Set up error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -0400570 mod_timer(&phba->eratt_poll,
571 jiffies + msecs_to_jiffies(1000 * LPFC_ERATT_POLL_INTERVAL));
dea31012005-04-17 16:05:31 -0500572
James Smarta0c87cb2009-07-19 10:01:10 -0400573 if (phba->hba_flag & LINK_DISABLED) {
574 lpfc_printf_log(phba,
575 KERN_ERR, LOG_INIT,
576 "2598 Adapter Link is disabled.\n");
577 lpfc_down_link(phba, pmb);
578 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
579 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
580 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
581 lpfc_printf_log(phba,
582 KERN_ERR, LOG_INIT,
583 "2599 Adapter failed to issue DOWN_LINK"
584 " mbox command rc 0x%x\n", rc);
585
586 mempool_free(pmb, phba->mbox_mem_pool);
587 return -EIO;
588 }
James Smarte40a02c2010-02-26 14:13:54 -0500589 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart026abb82011-12-13 13:20:45 -0500590 mempool_free(pmb, phba->mbox_mem_pool);
591 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
592 if (rc)
593 return rc;
dea31012005-04-17 16:05:31 -0500594 }
595 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400596 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500597 if (!pmb) {
598 phba->link_state = LPFC_HBA_ERROR;
599 return -ENOMEM;
600 }
601
James Smart57127f12007-10-27 13:37:05 -0400602 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
603 pmb->mbox_cmpl = lpfc_config_async_cmpl;
604 pmb->vport = phba->pport;
605 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500606
James Smart57127f12007-10-27 13:37:05 -0400607 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
608 lpfc_printf_log(phba,
609 KERN_ERR,
610 LOG_INIT,
611 "0456 Adapter failed to issue "
James Smarte4e74272009-07-19 10:01:38 -0400612 "ASYNCEVT_ENABLE mbox status x%x\n",
James Smart57127f12007-10-27 13:37:05 -0400613 rc);
614 mempool_free(pmb, phba->mbox_mem_pool);
615 }
James Smart97207482008-12-04 22:39:19 -0500616
617 /* Get Option rom version */
618 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500619 if (!pmb) {
620 phba->link_state = LPFC_HBA_ERROR;
621 return -ENOMEM;
622 }
623
James Smart97207482008-12-04 22:39:19 -0500624 lpfc_dump_wakeup_param(phba, pmb);
625 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
626 pmb->vport = phba->pport;
627 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
628
629 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
630 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
James Smarte4e74272009-07-19 10:01:38 -0400631 "to get Option ROM version status x%x\n", rc);
James Smart97207482008-12-04 22:39:19 -0500632 mempool_free(pmb, phba->mbox_mem_pool);
633 }
634
James Smartd7c255b2008-08-24 21:50:00 -0400635 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400636}
637
James Smarte59058c2008-08-24 21:49:00 -0400638/**
James Smart84d1b002010-02-12 14:42:33 -0500639 * lpfc_hba_init_link - Initialize the FC link
640 * @phba: pointer to lpfc hba data structure.
James Smart6e7288d2010-06-07 15:23:35 -0400641 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500642 *
643 * This routine will issue the INIT_LINK mailbox command call.
644 * It is available to other drivers through the lpfc_hba data
645 * structure for use as a delayed link up mechanism with the
646 * module parameter lpfc_suppress_link_up.
647 *
648 * Return code
649 * 0 - success
650 * Any other value - error
651 **/
652int
James Smart6e7288d2010-06-07 15:23:35 -0400653lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500654{
James Smart1b511972011-12-13 13:23:09 -0500655 return lpfc_hba_init_link_fc_topology(phba, phba->cfg_topology, flag);
656}
657
658/**
659 * lpfc_hba_init_link_fc_topology - Initialize FC link with desired topology
660 * @phba: pointer to lpfc hba data structure.
661 * @fc_topology: desired fc topology.
662 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
663 *
664 * This routine will issue the INIT_LINK mailbox command call.
665 * It is available to other drivers through the lpfc_hba data
666 * structure for use as a delayed link up mechanism with the
667 * module parameter lpfc_suppress_link_up.
668 *
669 * Return code
670 * 0 - success
671 * Any other value - error
672 **/
673int
674lpfc_hba_init_link_fc_topology(struct lpfc_hba *phba, uint32_t fc_topology,
675 uint32_t flag)
676{
James Smart84d1b002010-02-12 14:42:33 -0500677 struct lpfc_vport *vport = phba->pport;
678 LPFC_MBOXQ_t *pmb;
679 MAILBOX_t *mb;
680 int rc;
681
682 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
683 if (!pmb) {
684 phba->link_state = LPFC_HBA_ERROR;
685 return -ENOMEM;
686 }
687 mb = &pmb->u.mb;
688 pmb->vport = vport;
689
James Smart026abb82011-12-13 13:20:45 -0500690 if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_MAX) ||
691 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G) &&
692 !(phba->lmt & LMT_1Gb)) ||
693 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G) &&
694 !(phba->lmt & LMT_2Gb)) ||
695 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G) &&
696 !(phba->lmt & LMT_4Gb)) ||
697 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G) &&
698 !(phba->lmt & LMT_8Gb)) ||
699 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G) &&
700 !(phba->lmt & LMT_10Gb)) ||
701 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G) &&
702 !(phba->lmt & LMT_16Gb))) {
703 /* Reset link speed to auto */
704 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
705 "1302 Invalid speed for this board:%d "
706 "Reset link speed to auto.\n",
707 phba->cfg_link_speed);
708 phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
709 }
James Smart1b511972011-12-13 13:23:09 -0500710 lpfc_init_link(phba, pmb, fc_topology, phba->cfg_link_speed);
James Smart84d1b002010-02-12 14:42:33 -0500711 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart1b511972011-12-13 13:23:09 -0500712 if (phba->sli_rev < LPFC_SLI_REV4)
713 lpfc_set_loopback_flag(phba);
James Smart6e7288d2010-06-07 15:23:35 -0400714 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart76a95d72010-11-20 23:11:48 -0500715 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
James Smart84d1b002010-02-12 14:42:33 -0500716 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
717 "0498 Adapter failed to init, mbxCmd x%x "
718 "INIT_LINK, mbxStatus x%x\n",
719 mb->mbxCommand, mb->mbxStatus);
James Smart76a95d72010-11-20 23:11:48 -0500720 if (phba->sli_rev <= LPFC_SLI_REV3) {
721 /* Clear all interrupt enable conditions */
722 writel(0, phba->HCregaddr);
723 readl(phba->HCregaddr); /* flush */
724 /* Clear all pending interrupts */
725 writel(0xffffffff, phba->HAregaddr);
726 readl(phba->HAregaddr); /* flush */
727 }
James Smart84d1b002010-02-12 14:42:33 -0500728 phba->link_state = LPFC_HBA_ERROR;
James Smart6e7288d2010-06-07 15:23:35 -0400729 if (rc != MBX_BUSY || flag == MBX_POLL)
James Smart84d1b002010-02-12 14:42:33 -0500730 mempool_free(pmb, phba->mbox_mem_pool);
731 return -EIO;
732 }
James Smarte40a02c2010-02-26 14:13:54 -0500733 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
James Smart6e7288d2010-06-07 15:23:35 -0400734 if (flag == MBX_POLL)
735 mempool_free(pmb, phba->mbox_mem_pool);
James Smart84d1b002010-02-12 14:42:33 -0500736
737 return 0;
738}
739
740/**
741 * lpfc_hba_down_link - this routine downs the FC link
James Smart6e7288d2010-06-07 15:23:35 -0400742 * @phba: pointer to lpfc hba data structure.
743 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500744 *
745 * This routine will issue the DOWN_LINK mailbox command call.
746 * It is available to other drivers through the lpfc_hba data
747 * structure for use to stop the link.
748 *
749 * Return code
750 * 0 - success
751 * Any other value - error
752 **/
753int
James Smart6e7288d2010-06-07 15:23:35 -0400754lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500755{
756 LPFC_MBOXQ_t *pmb;
757 int rc;
758
759 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
760 if (!pmb) {
761 phba->link_state = LPFC_HBA_ERROR;
762 return -ENOMEM;
763 }
764
765 lpfc_printf_log(phba,
766 KERN_ERR, LOG_INIT,
767 "0491 Adapter Link is disabled.\n");
768 lpfc_down_link(phba, pmb);
769 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6e7288d2010-06-07 15:23:35 -0400770 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart84d1b002010-02-12 14:42:33 -0500771 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
772 lpfc_printf_log(phba,
773 KERN_ERR, LOG_INIT,
774 "2522 Adapter failed to issue DOWN_LINK"
775 " mbox command rc 0x%x\n", rc);
776
777 mempool_free(pmb, phba->mbox_mem_pool);
778 return -EIO;
779 }
James Smart6e7288d2010-06-07 15:23:35 -0400780 if (flag == MBX_POLL)
781 mempool_free(pmb, phba->mbox_mem_pool);
782
James Smart84d1b002010-02-12 14:42:33 -0500783 return 0;
784}
785
786/**
James Smart3621a712009-04-06 18:47:14 -0400787 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400788 * @phba: pointer to lpfc HBA data structure.
789 *
790 * This routine will do LPFC uninitialization before the HBA is reset when
791 * bringing down the SLI Layer.
792 *
793 * Return codes
794 * 0 - success.
795 * Any other value - error.
796 **/
dea31012005-04-17 16:05:31 -0500797int
James Smart2e0fef82007-06-17 19:56:36 -0500798lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500799{
James Smart1b32f6a2008-02-08 18:49:39 -0500800 struct lpfc_vport **vports;
801 int i;
James Smart3772a992009-05-22 14:50:54 -0400802
803 if (phba->sli_rev <= LPFC_SLI_REV3) {
804 /* Disable interrupts */
805 writel(0, phba->HCregaddr);
806 readl(phba->HCregaddr); /* flush */
807 }
dea31012005-04-17 16:05:31 -0500808
James Smart1b32f6a2008-02-08 18:49:39 -0500809 if (phba->pport->load_flag & FC_UNLOADING)
810 lpfc_cleanup_discovery_resources(phba->pport);
811 else {
812 vports = lpfc_create_vport_work_array(phba);
813 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400814 for (i = 0; i <= phba->max_vports &&
815 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500816 lpfc_cleanup_discovery_resources(vports[i]);
817 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500818 }
819 return 0;
dea31012005-04-17 16:05:31 -0500820}
821
James Smarte59058c2008-08-24 21:49:00 -0400822/**
James Smartbcece5f2014-04-04 13:51:34 -0400823 * lpfc_hba_free_post_buf - Perform lpfc uninitialization after HBA reset
824 * @phba: pointer to lpfc HBA data structure.
825 *
826 * This routine will cleanup posted ELS buffers after the HBA is reset
827 * when bringing down the SLI Layer.
828 *
829 *
830 * Return codes
831 * void.
832 **/
833static void
834lpfc_hba_free_post_buf(struct lpfc_hba *phba)
835{
836 struct lpfc_sli *psli = &phba->sli;
837 struct lpfc_sli_ring *pring;
838 struct lpfc_dmabuf *mp, *next_mp;
James Smart07eab622014-04-04 13:51:44 -0400839 LIST_HEAD(buflist);
840 int count;
James Smartbcece5f2014-04-04 13:51:34 -0400841
842 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
843 lpfc_sli_hbqbuf_free_all(phba);
844 else {
845 /* Cleanup preposted buffers on the ELS ring */
James Smartbcece5f2014-04-04 13:51:34 -0400846 pring = &psli->ring[LPFC_ELS_RING];
James Smart07eab622014-04-04 13:51:44 -0400847 spin_lock_irq(&phba->hbalock);
848 list_splice_init(&pring->postbufq, &buflist);
849 spin_unlock_irq(&phba->hbalock);
850
851 count = 0;
852 list_for_each_entry_safe(mp, next_mp, &buflist, list) {
James Smartbcece5f2014-04-04 13:51:34 -0400853 list_del(&mp->list);
James Smart07eab622014-04-04 13:51:44 -0400854 count++;
James Smartbcece5f2014-04-04 13:51:34 -0400855 lpfc_mbuf_free(phba, mp->virt, mp->phys);
856 kfree(mp);
857 }
James Smart07eab622014-04-04 13:51:44 -0400858
859 spin_lock_irq(&phba->hbalock);
860 pring->postbufq_cnt -= count;
James Smartbcece5f2014-04-04 13:51:34 -0400861 spin_unlock_irq(&phba->hbalock);
862 }
863}
864
865/**
866 * lpfc_hba_clean_txcmplq - Perform lpfc uninitialization after HBA reset
867 * @phba: pointer to lpfc HBA data structure.
868 *
869 * This routine will cleanup the txcmplq after the HBA is reset when bringing
870 * down the SLI Layer.
871 *
872 * Return codes
873 * void
874 **/
875static void
876lpfc_hba_clean_txcmplq(struct lpfc_hba *phba)
877{
878 struct lpfc_sli *psli = &phba->sli;
879 struct lpfc_sli_ring *pring;
880 LIST_HEAD(completions);
881 int i;
882
883
884
885 for (i = 0; i < psli->num_rings; i++) {
886 pring = &psli->ring[i];
887 if (phba->sli_rev >= LPFC_SLI_REV4)
888 spin_lock_irq(&pring->ring_lock);
889 else
890 spin_lock_irq(&phba->hbalock);
891 /* At this point in time the HBA is either reset or DOA. Either
892 * way, nothing should be on txcmplq as it will NEVER complete.
893 */
894 list_splice_init(&pring->txcmplq, &completions);
895
896 if (phba->sli_rev >= LPFC_SLI_REV4)
897 spin_unlock_irq(&pring->ring_lock);
898 else
899 spin_unlock_irq(&phba->hbalock);
900
901 /* Cancel all the IOCBs from the completions list */
902 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
903 IOERR_SLI_ABORTED);
904 lpfc_sli_abort_iocb_ring(phba, pring);
905 }
906}
907
908/**
James Smart3772a992009-05-22 14:50:54 -0400909 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smartbcece5f2014-04-04 13:51:34 -0400910 int i;
James Smarte59058c2008-08-24 21:49:00 -0400911 * @phba: pointer to lpfc HBA data structure.
912 *
913 * This routine will do uninitialization after the HBA is reset when bring
914 * down the SLI Layer.
915 *
916 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200917 * 0 - success.
James Smarte59058c2008-08-24 21:49:00 -0400918 * Any other value - error.
919 **/
James Smart3772a992009-05-22 14:50:54 -0400920static int
921lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -0500922{
James Smartbcece5f2014-04-04 13:51:34 -0400923 lpfc_hba_free_post_buf(phba);
924 lpfc_hba_clean_txcmplq(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500925 return 0;
926}
James Smart5af5eee2010-10-22 11:06:38 -0400927
James Smartda0436e2009-05-22 14:51:39 -0400928/**
929 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
930 * @phba: pointer to lpfc HBA data structure.
931 *
932 * This routine will do uninitialization after the HBA is reset when bring
933 * down the SLI Layer.
934 *
935 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200936 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400937 * Any other value - error.
938 **/
939static int
940lpfc_hba_down_post_s4(struct lpfc_hba *phba)
941{
942 struct lpfc_scsi_buf *psb, *psb_next;
943 LIST_HEAD(aborts);
James Smartda0436e2009-05-22 14:51:39 -0400944 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500945 struct lpfc_sglq *sglq_entry = NULL;
946
James Smartbcece5f2014-04-04 13:51:34 -0400947 lpfc_hba_free_post_buf(phba);
948 lpfc_hba_clean_txcmplq(phba);
949
James Smartda0436e2009-05-22 14:51:39 -0400950 /* At this point in time the HBA is either reset or DOA. Either
951 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
952 * on the lpfc_sgl_list so that it can either be freed if the
953 * driver is unloading or reposted if the driver is restarting
954 * the port.
955 */
956 spin_lock_irq(&phba->hbalock); /* required for lpfc_sgl_list and */
957 /* scsl_buf_list */
958 /* abts_sgl_list_lock required because worker thread uses this
959 * list.
960 */
961 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -0500962 list_for_each_entry(sglq_entry,
963 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
964 sglq_entry->state = SGL_FREED;
965
James Smartda0436e2009-05-22 14:51:39 -0400966 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
967 &phba->sli4_hba.lpfc_sgl_list);
968 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
969 /* abts_scsi_buf_list_lock required because worker thread uses this
970 * list.
971 */
972 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
973 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
974 &aborts);
975 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
976 spin_unlock_irq(&phba->hbalock);
977
978 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
979 psb->pCmd = NULL;
980 psb->status = IOSTAT_SUCCESS;
981 }
James Smarta40fc5f2013-04-17 20:17:40 -0400982 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
983 list_splice(&aborts, &phba->lpfc_scsi_buf_list_put);
984 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400985 return 0;
986}
987
988/**
989 * lpfc_hba_down_post - Wrapper func for hba down post routine
990 * @phba: pointer to lpfc HBA data structure.
991 *
992 * This routine wraps the actual SLI3 or SLI4 routine for performing
993 * uninitialization after the HBA is reset when bring down the SLI Layer.
994 *
995 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200996 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400997 * Any other value - error.
998 **/
999int
1000lpfc_hba_down_post(struct lpfc_hba *phba)
1001{
1002 return (*phba->lpfc_hba_down_post)(phba);
1003}
Jamie Wellnitz41415862006-02-28 19:25:27 -05001004
James Smarte59058c2008-08-24 21:49:00 -04001005/**
James Smart3621a712009-04-06 18:47:14 -04001006 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001007 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1008 *
1009 * This is the HBA-timer timeout handler registered to the lpfc driver. When
1010 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
1011 * work-port-events bitmap and the worker thread is notified. This timeout
1012 * event will be used by the worker thread to invoke the actual timeout
1013 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
1014 * be performed in the timeout handler and the HBA timeout event bit shall
1015 * be cleared by the worker thread after it has taken the event bitmap out.
1016 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001017static void
James Smart858c9f62007-06-17 19:56:39 -05001018lpfc_hb_timeout(unsigned long ptr)
1019{
1020 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -04001021 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -05001022 unsigned long iflag;
1023
1024 phba = (struct lpfc_hba *)ptr;
James Smart93996272008-08-24 21:50:30 -04001025
1026 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -05001027 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04001028 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
1029 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -05001030 phba->pport->work_port_events |= WORKER_HB_TMO;
1031 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
1032
James Smart93996272008-08-24 21:50:30 -04001033 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -04001034 if (!tmo_posted)
1035 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -05001036 return;
1037}
1038
James Smarte59058c2008-08-24 21:49:00 -04001039/**
James Smart19ca7602010-11-20 23:11:55 -05001040 * lpfc_rrq_timeout - The RRQ-timer timeout handler
1041 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1042 *
1043 * This is the RRQ-timer timeout handler registered to the lpfc driver. When
1044 * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
1045 * work-port-events bitmap and the worker thread is notified. This timeout
1046 * event will be used by the worker thread to invoke the actual timeout
1047 * handler routine, lpfc_rrq_handler. Any periodical operations will
1048 * be performed in the timeout handler and the RRQ timeout event bit shall
1049 * be cleared by the worker thread after it has taken the event bitmap out.
1050 **/
1051static void
1052lpfc_rrq_timeout(unsigned long ptr)
1053{
1054 struct lpfc_hba *phba;
James Smart19ca7602010-11-20 23:11:55 -05001055 unsigned long iflag;
1056
1057 phba = (struct lpfc_hba *)ptr;
1058 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001059 if (!(phba->pport->load_flag & FC_UNLOADING))
1060 phba->hba_flag |= HBA_RRQ_ACTIVE;
1061 else
1062 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart19ca7602010-11-20 23:11:55 -05001063 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001064
1065 if (!(phba->pport->load_flag & FC_UNLOADING))
1066 lpfc_worker_wake_up(phba);
James Smart19ca7602010-11-20 23:11:55 -05001067}
1068
1069/**
James Smart3621a712009-04-06 18:47:14 -04001070 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -04001071 * @phba: pointer to lpfc hba data structure.
1072 * @pmboxq: pointer to the driver internal queue element for mailbox command.
1073 *
1074 * This is the callback function to the lpfc heart-beat mailbox command.
1075 * If configured, the lpfc driver issues the heart-beat mailbox command to
1076 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
1077 * heart-beat mailbox command is issued, the driver shall set up heart-beat
1078 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
1079 * heart-beat outstanding state. Once the mailbox command comes back and
1080 * no error conditions detected, the heart-beat mailbox command timer is
1081 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1082 * state is cleared for the next heart-beat. If the timer expired with the
1083 * heart-beat outstanding state set, the driver will put the HBA offline.
1084 **/
James Smart858c9f62007-06-17 19:56:39 -05001085static void
1086lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1087{
1088 unsigned long drvr_flag;
1089
1090 spin_lock_irqsave(&phba->hbalock, drvr_flag);
1091 phba->hb_outstanding = 0;
1092 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1093
James Smart93996272008-08-24 21:50:30 -04001094 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -05001095 mempool_free(pmboxq, phba->mbox_mem_pool);
1096 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1097 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -04001098 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -05001099 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001100 jiffies +
1101 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001102 return;
1103}
1104
James Smarte59058c2008-08-24 21:49:00 -04001105/**
James Smart3621a712009-04-06 18:47:14 -04001106 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001107 * @phba: pointer to lpfc hba data structure.
1108 *
1109 * This is the actual HBA-timer timeout handler to be invoked by the worker
1110 * thread whenever the HBA timer fired and HBA-timeout event posted. This
1111 * handler performs any periodic operations needed for the device. If such
1112 * periodic event has already been attended to either in the interrupt handler
1113 * or by processing slow-ring or fast-ring events within the HBA-timer
1114 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1115 * the timer for the next timeout period. If lpfc heart-beat mailbox command
1116 * is configured and there is no heart-beat mailbox command outstanding, a
1117 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1118 * has been a heart-beat mailbox command outstanding, the HBA shall be put
1119 * to offline.
1120 **/
James Smart858c9f62007-06-17 19:56:39 -05001121void
1122lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1123{
James Smart45ed1192009-10-02 15:17:02 -04001124 struct lpfc_vport **vports;
James Smart858c9f62007-06-17 19:56:39 -05001125 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -05001126 struct lpfc_dmabuf *buf_ptr;
James Smart45ed1192009-10-02 15:17:02 -04001127 int retval, i;
James Smart858c9f62007-06-17 19:56:39 -05001128 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -05001129 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -05001130
James Smart45ed1192009-10-02 15:17:02 -04001131 vports = lpfc_create_vport_work_array(phba);
1132 if (vports != NULL)
1133 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
1134 lpfc_rcv_seq_check_edtov(vports[i]);
1135 lpfc_destroy_vport_work_array(phba, vports);
1136
James Smart858c9f62007-06-17 19:56:39 -05001137 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -04001138 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -05001139 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1140 return;
1141
1142 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -05001143
James Smart256ec0d2013-04-17 20:14:58 -04001144 if (time_after(phba->last_completion_time +
1145 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL),
1146 jiffies)) {
James Smart858c9f62007-06-17 19:56:39 -05001147 spin_unlock_irq(&phba->pport->work_port_lock);
1148 if (!phba->hb_outstanding)
1149 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001150 jiffies +
1151 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001152 else
1153 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001154 jiffies +
1155 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001156 return;
1157 }
1158 spin_unlock_irq(&phba->pport->work_port_lock);
1159
James Smart0ff10d42008-01-11 01:52:36 -05001160 if (phba->elsbuf_cnt &&
1161 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1162 spin_lock_irq(&phba->hbalock);
1163 list_splice_init(&phba->elsbuf, &completions);
1164 phba->elsbuf_cnt = 0;
1165 phba->elsbuf_prev_cnt = 0;
1166 spin_unlock_irq(&phba->hbalock);
1167
1168 while (!list_empty(&completions)) {
1169 list_remove_head(&completions, buf_ptr,
1170 struct lpfc_dmabuf, list);
1171 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1172 kfree(buf_ptr);
1173 }
1174 }
1175 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1176
James Smart858c9f62007-06-17 19:56:39 -05001177 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -05001178 if (phba->cfg_enable_hba_heartbeat) {
1179 if (!phba->hb_outstanding) {
James Smartbc739052010-08-04 16:11:18 -04001180 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1181 (list_empty(&psli->mboxq))) {
1182 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1183 GFP_KERNEL);
1184 if (!pmboxq) {
1185 mod_timer(&phba->hb_tmofunc,
1186 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001187 msecs_to_jiffies(1000 *
1188 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001189 return;
1190 }
James Smart13815c82008-01-11 01:52:48 -05001191
James Smartbc739052010-08-04 16:11:18 -04001192 lpfc_heart_beat(phba, pmboxq);
1193 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1194 pmboxq->vport = phba->pport;
1195 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1196 MBX_NOWAIT);
James Smart13815c82008-01-11 01:52:48 -05001197
James Smartbc739052010-08-04 16:11:18 -04001198 if (retval != MBX_BUSY &&
1199 retval != MBX_SUCCESS) {
1200 mempool_free(pmboxq,
1201 phba->mbox_mem_pool);
1202 mod_timer(&phba->hb_tmofunc,
1203 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001204 msecs_to_jiffies(1000 *
1205 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001206 return;
1207 }
1208 phba->skipped_hb = 0;
1209 phba->hb_outstanding = 1;
1210 } else if (time_before_eq(phba->last_completion_time,
1211 phba->skipped_hb)) {
1212 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1213 "2857 Last completion time not "
1214 " updated in %d ms\n",
1215 jiffies_to_msecs(jiffies
1216 - phba->last_completion_time));
1217 } else
1218 phba->skipped_hb = jiffies;
1219
James Smart858c9f62007-06-17 19:56:39 -05001220 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001221 jiffies +
1222 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001223 return;
James Smart13815c82008-01-11 01:52:48 -05001224 } else {
1225 /*
1226 * If heart beat timeout called with hb_outstanding set
James Smartdcf2a4e2010-09-29 11:18:53 -04001227 * we need to give the hb mailbox cmd a chance to
1228 * complete or TMO.
James Smart13815c82008-01-11 01:52:48 -05001229 */
James Smartdcf2a4e2010-09-29 11:18:53 -04001230 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1231 "0459 Adapter heartbeat still out"
1232 "standing:last compl time was %d ms.\n",
1233 jiffies_to_msecs(jiffies
1234 - phba->last_completion_time));
1235 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001236 jiffies +
1237 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001238 }
James Smart858c9f62007-06-17 19:56:39 -05001239 }
1240}
1241
James Smarte59058c2008-08-24 21:49:00 -04001242/**
James Smart3621a712009-04-06 18:47:14 -04001243 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -04001244 * @phba: pointer to lpfc hba data structure.
1245 *
1246 * This routine is called to bring the HBA offline when HBA hardware error
1247 * other than Port Error 6 has been detected.
1248 **/
James Smart09372822008-01-11 01:52:54 -05001249static void
1250lpfc_offline_eratt(struct lpfc_hba *phba)
1251{
1252 struct lpfc_sli *psli = &phba->sli;
1253
1254 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001255 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -05001256 spin_unlock_irq(&phba->hbalock);
James Smart618a5232012-06-12 13:54:36 -04001257 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart09372822008-01-11 01:52:54 -05001258
1259 lpfc_offline(phba);
1260 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001261 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001262 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001263 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001264 lpfc_hba_down_post(phba);
1265 lpfc_sli_brdready(phba, HS_MBRDY);
1266 lpfc_unblock_mgmt_io(phba);
1267 phba->link_state = LPFC_HBA_ERROR;
1268 return;
1269}
1270
James Smarte59058c2008-08-24 21:49:00 -04001271/**
James Smartda0436e2009-05-22 14:51:39 -04001272 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1273 * @phba: pointer to lpfc hba data structure.
1274 *
1275 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1276 * other than Port Error 6 has been detected.
1277 **/
James Smarta88dbb62013-04-17 20:18:39 -04001278void
James Smartda0436e2009-05-22 14:51:39 -04001279lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1280{
James Smart618a5232012-06-12 13:54:36 -04001281 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smartda0436e2009-05-22 14:51:39 -04001282 lpfc_offline(phba);
1283 lpfc_sli4_brdreset(phba);
1284 lpfc_hba_down_post(phba);
1285 lpfc_sli4_post_status_check(phba);
1286 lpfc_unblock_mgmt_io(phba);
1287 phba->link_state = LPFC_HBA_ERROR;
1288}
1289
1290/**
James Smarta257bf92009-04-06 18:48:10 -04001291 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1292 * @phba: pointer to lpfc hba data structure.
1293 *
1294 * This routine is invoked to handle the deferred HBA hardware error
1295 * conditions. This type of error is indicated by HBA by setting ER1
1296 * and another ER bit in the host status register. The driver will
1297 * wait until the ER1 bit clears before handling the error condition.
1298 **/
1299static void
1300lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1301{
1302 uint32_t old_host_status = phba->work_hs;
1303 struct lpfc_sli_ring *pring;
1304 struct lpfc_sli *psli = &phba->sli;
1305
James Smartf4b4c682009-05-22 14:53:12 -04001306 /* If the pci channel is offline, ignore possible errors,
1307 * since we cannot communicate with the pci card anyway.
1308 */
1309 if (pci_channel_offline(phba->pcidev)) {
1310 spin_lock_irq(&phba->hbalock);
1311 phba->hba_flag &= ~DEFER_ERATT;
1312 spin_unlock_irq(&phba->hbalock);
1313 return;
1314 }
1315
James Smarta257bf92009-04-06 18:48:10 -04001316 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1317 "0479 Deferred Adapter Hardware Error "
1318 "Data: x%x x%x x%x\n",
1319 phba->work_hs,
1320 phba->work_status[0], phba->work_status[1]);
1321
1322 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001323 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001324 spin_unlock_irq(&phba->hbalock);
1325
1326
1327 /*
1328 * Firmware stops when it triggred erratt. That could cause the I/Os
1329 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1330 * SCSI layer retry it after re-establishing link.
1331 */
1332 pring = &psli->ring[psli->fcp_ring];
1333 lpfc_sli_abort_iocb_ring(phba, pring);
1334
1335 /*
1336 * There was a firmware error. Take the hba offline and then
1337 * attempt to restart it.
1338 */
James Smart618a5232012-06-12 13:54:36 -04001339 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smarta257bf92009-04-06 18:48:10 -04001340 lpfc_offline(phba);
1341
1342 /* Wait for the ER1 bit to clear.*/
1343 while (phba->work_hs & HS_FFER1) {
1344 msleep(100);
James Smart9940b972011-03-11 16:06:12 -05001345 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1346 phba->work_hs = UNPLUG_ERR ;
1347 break;
1348 }
James Smarta257bf92009-04-06 18:48:10 -04001349 /* If driver is unloading let the worker thread continue */
1350 if (phba->pport->load_flag & FC_UNLOADING) {
1351 phba->work_hs = 0;
1352 break;
1353 }
1354 }
1355
1356 /*
1357 * This is to ptrotect against a race condition in which
1358 * first write to the host attention register clear the
1359 * host status register.
1360 */
1361 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1362 phba->work_hs = old_host_status & ~HS_FFER1;
1363
James Smart3772a992009-05-22 14:50:54 -04001364 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001365 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001366 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001367 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1368 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1369}
1370
James Smart3772a992009-05-22 14:50:54 -04001371static void
1372lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1373{
1374 struct lpfc_board_event_header board_event;
1375 struct Scsi_Host *shost;
1376
1377 board_event.event_type = FC_REG_BOARD_EVENT;
1378 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1379 shost = lpfc_shost_from_vport(phba->pport);
1380 fc_host_post_vendor_event(shost, fc_get_event_number(),
1381 sizeof(board_event),
1382 (char *) &board_event,
1383 LPFC_NL_VENDOR_ID);
1384}
1385
James Smarta257bf92009-04-06 18:48:10 -04001386/**
James Smart3772a992009-05-22 14:50:54 -04001387 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001388 * @phba: pointer to lpfc hba data structure.
1389 *
1390 * This routine is invoked to handle the following HBA hardware error
1391 * conditions:
1392 * 1 - HBA error attention interrupt
1393 * 2 - DMA ring index out of range
1394 * 3 - Mailbox command came back as unknown
1395 **/
James Smart3772a992009-05-22 14:50:54 -04001396static void
1397lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001398{
James Smart2e0fef82007-06-17 19:56:36 -05001399 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001400 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001401 struct lpfc_sli_ring *pring;
James Smartd2873e42006-08-18 17:46:43 -04001402 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001403 unsigned long temperature;
1404 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001405 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001406
Linas Vepstas8d63f372007-02-14 14:28:36 -06001407 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001408 * since we cannot communicate with the pci card anyway.
1409 */
1410 if (pci_channel_offline(phba->pcidev)) {
1411 spin_lock_irq(&phba->hbalock);
1412 phba->hba_flag &= ~DEFER_ERATT;
1413 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001414 return;
James Smart3772a992009-05-22 14:50:54 -04001415 }
1416
James Smart13815c82008-01-11 01:52:48 -05001417 /* If resets are disabled then leave the HBA alone and return */
1418 if (!phba->cfg_enable_hba_reset)
1419 return;
dea31012005-04-17 16:05:31 -05001420
James Smartea2151b2008-09-07 11:52:10 -04001421 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001422 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001423
James Smarta257bf92009-04-06 18:48:10 -04001424 if (phba->hba_flag & DEFER_ERATT)
1425 lpfc_handle_deferred_eratt(phba);
1426
James Smartdcf2a4e2010-09-29 11:18:53 -04001427 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1428 if (phba->work_hs & HS_FFER6)
1429 /* Re-establishing Link */
1430 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1431 "1301 Re-establishing Link "
1432 "Data: x%x x%x x%x\n",
1433 phba->work_hs, phba->work_status[0],
1434 phba->work_status[1]);
1435 if (phba->work_hs & HS_FFER8)
1436 /* Device Zeroization */
1437 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1438 "2861 Host Authentication device "
1439 "zeroization Data:x%x x%x x%x\n",
1440 phba->work_hs, phba->work_status[0],
1441 phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001442
James Smart92d7f7b2007-06-17 19:56:38 -05001443 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001444 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001445 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001446
1447 /*
1448 * Firmware stops when it triggled erratt with HS_FFER6.
1449 * That could cause the I/Os dropped by the firmware.
1450 * Error iocb (I/O) on txcmplq and let the SCSI layer
1451 * retry it after re-establishing link.
1452 */
1453 pring = &psli->ring[psli->fcp_ring];
1454 lpfc_sli_abort_iocb_ring(phba, pring);
1455
dea31012005-04-17 16:05:31 -05001456 /*
1457 * There was a firmware error. Take the hba offline and then
1458 * attempt to restart it.
1459 */
James Smart618a5232012-06-12 13:54:36 -04001460 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
dea31012005-04-17 16:05:31 -05001461 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001462 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001463 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001464 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001465 return;
1466 }
James Smart46fa3112007-04-25 09:51:45 -04001467 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001468 } else if (phba->work_hs & HS_CRIT_TEMP) {
1469 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1470 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1471 temp_event_data.event_code = LPFC_CRIT_TEMP;
1472 temp_event_data.data = (uint32_t)temperature;
1473
1474 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001475 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001476 "(%ld), taking this port offline "
1477 "Data: x%x x%x x%x\n",
1478 temperature, phba->work_hs,
1479 phba->work_status[0], phba->work_status[1]);
1480
1481 shost = lpfc_shost_from_vport(phba->pport);
1482 fc_host_post_vendor_event(shost, fc_get_event_number(),
1483 sizeof(temp_event_data),
1484 (char *) &temp_event_data,
1485 SCSI_NL_VID_TYPE_PCI
1486 | PCI_VENDOR_ID_EMULEX);
1487
James Smart7af67052007-10-27 13:38:11 -04001488 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001489 phba->over_temp_state = HBA_OVER_TEMP;
1490 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001491 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001492
dea31012005-04-17 16:05:31 -05001493 } else {
1494 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001495 * failure is a value other than FFER6. Do not call the offline
1496 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001497 */
1498 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001499 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001500 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001501 phba->work_hs,
dea31012005-04-17 16:05:31 -05001502 phba->work_status[0], phba->work_status[1]);
1503
James Smartd2873e42006-08-18 17:46:43 -04001504 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001505 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001506 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001507 sizeof(event_data), (char *) &event_data,
1508 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1509
James Smart09372822008-01-11 01:52:54 -05001510 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001511 }
James Smart93996272008-08-24 21:50:30 -04001512 return;
dea31012005-04-17 16:05:31 -05001513}
1514
James Smarte59058c2008-08-24 21:49:00 -04001515/**
James Smart618a5232012-06-12 13:54:36 -04001516 * lpfc_sli4_port_sta_fn_reset - The SLI4 function reset due to port status reg
1517 * @phba: pointer to lpfc hba data structure.
1518 * @mbx_action: flag for mailbox shutdown action.
1519 *
1520 * This routine is invoked to perform an SLI4 port PCI function reset in
1521 * response to port status register polling attention. It waits for port
1522 * status register (ERR, RDY, RN) bits before proceeding with function reset.
1523 * During this process, interrupt vectors are freed and later requested
1524 * for handling possible port resource change.
1525 **/
1526static int
James Smarte10b2022014-02-20 09:57:43 -05001527lpfc_sli4_port_sta_fn_reset(struct lpfc_hba *phba, int mbx_action,
1528 bool en_rn_msg)
James Smart618a5232012-06-12 13:54:36 -04001529{
1530 int rc;
1531 uint32_t intr_mode;
1532
1533 /*
1534 * On error status condition, driver need to wait for port
1535 * ready before performing reset.
1536 */
1537 rc = lpfc_sli4_pdev_status_reg_wait(phba);
1538 if (!rc) {
1539 /* need reset: attempt for port recovery */
James Smarte10b2022014-02-20 09:57:43 -05001540 if (en_rn_msg)
1541 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1542 "2887 Reset Needed: Attempting Port "
1543 "Recovery...\n");
James Smart618a5232012-06-12 13:54:36 -04001544 lpfc_offline_prep(phba, mbx_action);
1545 lpfc_offline(phba);
1546 /* release interrupt for possible resource change */
1547 lpfc_sli4_disable_intr(phba);
1548 lpfc_sli_brdrestart(phba);
1549 /* request and enable interrupt */
1550 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
1551 if (intr_mode == LPFC_INTR_ERROR) {
1552 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1553 "3175 Failed to enable interrupt\n");
1554 return -EIO;
1555 } else {
1556 phba->intr_mode = intr_mode;
1557 }
1558 rc = lpfc_online(phba);
1559 if (rc == 0)
1560 lpfc_unblock_mgmt_io(phba);
1561 }
1562 return rc;
1563}
1564
1565/**
James Smartda0436e2009-05-22 14:51:39 -04001566 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1567 * @phba: pointer to lpfc hba data structure.
1568 *
1569 * This routine is invoked to handle the SLI4 HBA hardware error attention
1570 * conditions.
1571 **/
1572static void
1573lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1574{
1575 struct lpfc_vport *vport = phba->pport;
1576 uint32_t event_data;
1577 struct Scsi_Host *shost;
James Smart2fcee4b2010-12-15 17:57:46 -05001578 uint32_t if_type;
James Smart2e90f4b2011-12-13 13:22:37 -05001579 struct lpfc_register portstat_reg = {0};
1580 uint32_t reg_err1, reg_err2;
1581 uint32_t uerrlo_reg, uemasklo_reg;
1582 uint32_t pci_rd_rc1, pci_rd_rc2;
James Smarte10b2022014-02-20 09:57:43 -05001583 bool en_rn_msg = true;
James Smart73d91e52011-10-10 21:32:10 -04001584 int rc;
James Smartda0436e2009-05-22 14:51:39 -04001585
1586 /* If the pci channel is offline, ignore possible errors, since
1587 * we cannot communicate with the pci card anyway.
1588 */
1589 if (pci_channel_offline(phba->pcidev))
1590 return;
1591 /* If resets are disabled then leave the HBA alone and return */
1592 if (!phba->cfg_enable_hba_reset)
1593 return;
1594
James Smart2fcee4b2010-12-15 17:57:46 -05001595 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1596 switch (if_type) {
1597 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart2e90f4b2011-12-13 13:22:37 -05001598 pci_rd_rc1 = lpfc_readl(
1599 phba->sli4_hba.u.if_type0.UERRLOregaddr,
1600 &uerrlo_reg);
1601 pci_rd_rc2 = lpfc_readl(
1602 phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
1603 &uemasklo_reg);
1604 /* consider PCI bus read error as pci_channel_offline */
1605 if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
1606 return;
James Smart2fcee4b2010-12-15 17:57:46 -05001607 lpfc_sli4_offline_eratt(phba);
1608 break;
1609 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart2e90f4b2011-12-13 13:22:37 -05001610 pci_rd_rc1 = lpfc_readl(
1611 phba->sli4_hba.u.if_type2.STATUSregaddr,
1612 &portstat_reg.word0);
1613 /* consider PCI bus read error as pci_channel_offline */
James Smart6b5151f2012-01-18 16:24:06 -05001614 if (pci_rd_rc1 == -EIO) {
1615 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1616 "3151 PCI bus read access failure: x%x\n",
1617 readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
James Smart2e90f4b2011-12-13 13:22:37 -05001618 return;
James Smart6b5151f2012-01-18 16:24:06 -05001619 }
James Smart2e90f4b2011-12-13 13:22:37 -05001620 reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
1621 reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
James Smart2fcee4b2010-12-15 17:57:46 -05001622 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
1623 /* TODO: Register for Overtemp async events. */
1624 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1625 "2889 Port Overtemperature event, "
James Smart026abb82011-12-13 13:20:45 -05001626 "taking port offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001627 spin_lock_irq(&phba->hbalock);
1628 phba->over_temp_state = HBA_OVER_TEMP;
1629 spin_unlock_irq(&phba->hbalock);
1630 lpfc_sli4_offline_eratt(phba);
James Smart2e90f4b2011-12-13 13:22:37 -05001631 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001632 }
James Smart2e90f4b2011-12-13 13:22:37 -05001633 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smarte10b2022014-02-20 09:57:43 -05001634 reg_err2 == SLIPORT_ERR2_REG_FW_RESTART) {
James Smart2e90f4b2011-12-13 13:22:37 -05001635 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte10b2022014-02-20 09:57:43 -05001636 "3143 Port Down: Firmware Update "
1637 "Detected\n");
1638 en_rn_msg = false;
1639 } else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smart2e90f4b2011-12-13 13:22:37 -05001640 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1641 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1642 "3144 Port Down: Debug Dump\n");
1643 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1644 reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
1645 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1646 "3145 Port Down: Provisioning\n");
James Smart618a5232012-06-12 13:54:36 -04001647
1648 /* Check port status register for function reset */
James Smarte10b2022014-02-20 09:57:43 -05001649 rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT,
1650 en_rn_msg);
James Smart618a5232012-06-12 13:54:36 -04001651 if (rc == 0) {
1652 /* don't report event on forced debug dump */
1653 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1654 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1655 return;
1656 else
1657 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001658 }
James Smart618a5232012-06-12 13:54:36 -04001659 /* fall through for not able to recover */
James Smart6b5151f2012-01-18 16:24:06 -05001660 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1661 "3152 Unrecoverable error, bring the port "
1662 "offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001663 lpfc_sli4_offline_eratt(phba);
1664 break;
1665 case LPFC_SLI_INTF_IF_TYPE_1:
1666 default:
1667 break;
1668 }
James Smart2e90f4b2011-12-13 13:22:37 -05001669 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1670 "3123 Report dump event to upper layer\n");
1671 /* Send an internal error event to mgmt application */
1672 lpfc_board_errevt_to_mgmt(phba);
1673
1674 event_data = FC_REG_DUMP_EVENT;
1675 shost = lpfc_shost_from_vport(vport);
1676 fc_host_post_vendor_event(shost, fc_get_event_number(),
1677 sizeof(event_data), (char *) &event_data,
1678 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
James Smartda0436e2009-05-22 14:51:39 -04001679}
1680
1681/**
1682 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1683 * @phba: pointer to lpfc HBA data structure.
1684 *
1685 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1686 * routine from the API jump table function pointer from the lpfc_hba struct.
1687 *
1688 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001689 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001690 * Any other value - error.
1691 **/
1692void
1693lpfc_handle_eratt(struct lpfc_hba *phba)
1694{
1695 (*phba->lpfc_handle_eratt)(phba);
1696}
1697
1698/**
James Smart3621a712009-04-06 18:47:14 -04001699 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04001700 * @phba: pointer to lpfc hba data structure.
1701 *
1702 * This routine is invoked from the worker thread to handle a HBA host
1703 * attention link event.
1704 **/
dea31012005-04-17 16:05:31 -05001705void
James Smart2e0fef82007-06-17 19:56:36 -05001706lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001707{
James Smart2e0fef82007-06-17 19:56:36 -05001708 struct lpfc_vport *vport = phba->pport;
1709 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001710 LPFC_MBOXQ_t *pmb;
1711 volatile uint32_t control;
1712 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05001713 int rc = 0;
dea31012005-04-17 16:05:31 -05001714
1715 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001716 if (!pmb) {
1717 rc = 1;
dea31012005-04-17 16:05:31 -05001718 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05001719 }
dea31012005-04-17 16:05:31 -05001720
1721 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001722 if (!mp) {
1723 rc = 2;
dea31012005-04-17 16:05:31 -05001724 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05001725 }
dea31012005-04-17 16:05:31 -05001726
1727 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05001728 if (!mp->virt) {
1729 rc = 3;
dea31012005-04-17 16:05:31 -05001730 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05001731 }
dea31012005-04-17 16:05:31 -05001732
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05001733 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04001734 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05001735
1736 psli->slistat.link_event++;
James Smart76a95d72010-11-20 23:11:48 -05001737 lpfc_read_topology(phba, pmb, mp);
1738 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smart2e0fef82007-06-17 19:56:36 -05001739 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04001740 /* Block ELS IOCBs until we have processed this mbox command */
1741 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04001742 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05001743 if (rc == MBX_NOT_FINISHED) {
1744 rc = 4;
James Smart14691152006-12-02 13:34:28 -05001745 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05001746 }
dea31012005-04-17 16:05:31 -05001747
1748 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05001749 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001750 writel(HA_LATT, phba->HAregaddr);
1751 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001752 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001753
1754 return;
1755
James Smart14691152006-12-02 13:34:28 -05001756lpfc_handle_latt_free_mbuf:
James Smart0d2b6b82008-06-14 22:52:47 -04001757 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05001758 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001759lpfc_handle_latt_free_mp:
1760 kfree(mp);
1761lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04001762 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001763lpfc_handle_latt_err_exit:
1764 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001765 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001766 psli->sli_flag |= LPFC_PROCESS_LA;
1767 control = readl(phba->HCregaddr);
1768 control |= HC_LAINT_ENA;
1769 writel(control, phba->HCregaddr);
1770 readl(phba->HCregaddr); /* flush */
1771
1772 /* Clear Link Attention in HA REG */
1773 writel(HA_LATT, phba->HAregaddr);
1774 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001775 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001776 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001777 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001778
James Smart09372822008-01-11 01:52:54 -05001779 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1780 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05001781
1782 return;
1783}
1784
James Smarte59058c2008-08-24 21:49:00 -04001785/**
James Smart3621a712009-04-06 18:47:14 -04001786 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04001787 * @phba: pointer to lpfc hba data structure.
1788 * @vpd: pointer to the vital product data.
1789 * @len: length of the vital product data in bytes.
1790 *
1791 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1792 * an array of characters. In this routine, the ModelName, ProgramType, and
1793 * ModelDesc, etc. fields of the phba data structure will be populated.
1794 *
1795 * Return codes
1796 * 0 - pointer to the VPD passed in is NULL
1797 * 1 - success
1798 **/
James Smart3772a992009-05-22 14:50:54 -04001799int
James Smart2e0fef82007-06-17 19:56:36 -05001800lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05001801{
1802 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05001803 int Length;
dea31012005-04-17 16:05:31 -05001804 int i, j;
1805 int finished = 0;
1806 int index = 0;
1807
1808 if (!vpd)
1809 return 0;
1810
1811 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05001812 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001813 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05001814 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1815 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001816 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05001817 switch (vpd[index]) {
1818 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001819 case 0x91:
dea31012005-04-17 16:05:31 -05001820 index += 1;
1821 lenlo = vpd[index];
1822 index += 1;
1823 lenhi = vpd[index];
1824 index += 1;
1825 i = ((((unsigned short)lenhi) << 8) + lenlo);
1826 index += i;
1827 break;
1828 case 0x90:
1829 index += 1;
1830 lenlo = vpd[index];
1831 index += 1;
1832 lenhi = vpd[index];
1833 index += 1;
1834 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001835 if (Length > len - index)
1836 Length = len - index;
dea31012005-04-17 16:05:31 -05001837 while (Length > 0) {
1838 /* Look for Serial Number */
1839 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1840 index += 2;
1841 i = vpd[index];
1842 index += 1;
1843 j = 0;
1844 Length -= (3+i);
1845 while(i--) {
1846 phba->SerialNumber[j++] = vpd[index++];
1847 if (j == 31)
1848 break;
1849 }
1850 phba->SerialNumber[j] = 0;
1851 continue;
1852 }
1853 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1854 phba->vpd_flag |= VPD_MODEL_DESC;
1855 index += 2;
1856 i = vpd[index];
1857 index += 1;
1858 j = 0;
1859 Length -= (3+i);
1860 while(i--) {
1861 phba->ModelDesc[j++] = vpd[index++];
1862 if (j == 255)
1863 break;
1864 }
1865 phba->ModelDesc[j] = 0;
1866 continue;
1867 }
1868 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1869 phba->vpd_flag |= VPD_MODEL_NAME;
1870 index += 2;
1871 i = vpd[index];
1872 index += 1;
1873 j = 0;
1874 Length -= (3+i);
1875 while(i--) {
1876 phba->ModelName[j++] = vpd[index++];
1877 if (j == 79)
1878 break;
1879 }
1880 phba->ModelName[j] = 0;
1881 continue;
1882 }
1883 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1884 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1885 index += 2;
1886 i = vpd[index];
1887 index += 1;
1888 j = 0;
1889 Length -= (3+i);
1890 while(i--) {
1891 phba->ProgramType[j++] = vpd[index++];
1892 if (j == 255)
1893 break;
1894 }
1895 phba->ProgramType[j] = 0;
1896 continue;
1897 }
1898 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1899 phba->vpd_flag |= VPD_PORT;
1900 index += 2;
1901 i = vpd[index];
1902 index += 1;
1903 j = 0;
1904 Length -= (3+i);
1905 while(i--) {
James Smartcd1c8302011-10-10 21:33:25 -04001906 if ((phba->sli_rev == LPFC_SLI_REV4) &&
1907 (phba->sli4_hba.pport_name_sta ==
1908 LPFC_SLI4_PPNAME_GET)) {
1909 j++;
1910 index++;
1911 } else
1912 phba->Port[j++] = vpd[index++];
1913 if (j == 19)
1914 break;
dea31012005-04-17 16:05:31 -05001915 }
James Smartcd1c8302011-10-10 21:33:25 -04001916 if ((phba->sli_rev != LPFC_SLI_REV4) ||
1917 (phba->sli4_hba.pport_name_sta ==
1918 LPFC_SLI4_PPNAME_NON))
1919 phba->Port[j] = 0;
dea31012005-04-17 16:05:31 -05001920 continue;
1921 }
1922 else {
1923 index += 2;
1924 i = vpd[index];
1925 index += 1;
1926 index += i;
1927 Length -= (3 + i);
1928 }
1929 }
1930 finished = 0;
1931 break;
1932 case 0x78:
1933 finished = 1;
1934 break;
1935 default:
1936 index ++;
1937 break;
1938 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001939 }
dea31012005-04-17 16:05:31 -05001940
1941 return(1);
1942}
1943
James Smarte59058c2008-08-24 21:49:00 -04001944/**
James Smart3621a712009-04-06 18:47:14 -04001945 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04001946 * @phba: pointer to lpfc hba data structure.
1947 * @mdp: pointer to the data structure to hold the derived model name.
1948 * @descp: pointer to the data structure to hold the derived description.
1949 *
1950 * This routine retrieves HBA's description based on its registered PCI device
1951 * ID. The @descp passed into this function points to an array of 256 chars. It
1952 * shall be returned with the model name, maximum speed, and the host bus type.
1953 * The @mdp passed into this function points to an array of 80 chars. When the
1954 * function returns, the @mdp will be filled with the model name.
1955 **/
dea31012005-04-17 16:05:31 -05001956static void
James Smart2e0fef82007-06-17 19:56:36 -05001957lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05001958{
1959 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05001960 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001961 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04001962 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04001963 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001964 struct {
James Smarta747c9c2009-11-18 15:41:10 -05001965 char *name;
1966 char *bus;
1967 char *function;
1968 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001969
1970 if (mdp && mdp[0] != '\0'
1971 && descp && descp[0] != '\0')
1972 return;
1973
James Smartc0c11512011-05-24 11:41:34 -04001974 if (phba->lmt & LMT_16Gb)
1975 max_speed = 16;
1976 else if (phba->lmt & LMT_10Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001977 max_speed = 10;
1978 else if (phba->lmt & LMT_8Gb)
1979 max_speed = 8;
1980 else if (phba->lmt & LMT_4Gb)
1981 max_speed = 4;
1982 else if (phba->lmt & LMT_2Gb)
1983 max_speed = 2;
James Smart4169d862012-09-29 11:32:09 -04001984 else if (phba->lmt & LMT_1Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001985 max_speed = 1;
James Smart4169d862012-09-29 11:32:09 -04001986 else
1987 max_speed = 0;
dea31012005-04-17 16:05:31 -05001988
1989 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05001990
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001991 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001992 case PCI_DEVICE_ID_FIREFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001993 m = (typeof(m)){"LP6000", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001994 break;
dea31012005-04-17 16:05:31 -05001995 case PCI_DEVICE_ID_SUPERFLY:
1996 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smarta747c9c2009-11-18 15:41:10 -05001997 m = (typeof(m)){"LP7000", "PCI",
1998 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001999 else
James Smarta747c9c2009-11-18 15:41:10 -05002000 m = (typeof(m)){"LP7000E", "PCI",
2001 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002002 break;
2003 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002004 m = (typeof(m)){"LP8000", "PCI",
2005 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002006 break;
2007 case PCI_DEVICE_ID_CENTAUR:
2008 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smarta747c9c2009-11-18 15:41:10 -05002009 m = (typeof(m)){"LP9002", "PCI",
2010 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002011 else
James Smarta747c9c2009-11-18 15:41:10 -05002012 m = (typeof(m)){"LP9000", "PCI",
2013 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002014 break;
2015 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002016 m = (typeof(m)){"LP952", "PCI",
2017 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002018 break;
2019 case PCI_DEVICE_ID_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05002020 m = (typeof(m)){"LP9802", "PCI-X",
2021 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002022 break;
2023 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05002024 m = (typeof(m)){"LP10000", "PCI-X",
2025 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002026 break;
2027 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05002028 m = (typeof(m)){"LPX1000", "PCI-X",
2029 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002030 break;
2031 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002032 m = (typeof(m)){"LP982", "PCI-X",
2033 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002034 break;
2035 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002036 m = (typeof(m)){"LP1050", "PCI-X",
2037 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002038 break;
2039 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05002040 m = (typeof(m)){"LP11000", "PCI-X2",
2041 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002042 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002043 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002044 m = (typeof(m)){"LP11000-SP", "PCI-X2",
2045 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002046 break;
2047 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002048 m = (typeof(m)){"LP11002-SP", "PCI-X2",
2049 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002050 break;
2051 case PCI_DEVICE_ID_NEPTUNE:
James Smarta747c9c2009-11-18 15:41:10 -05002052 m = (typeof(m)){"LPe1000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002053 break;
2054 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002055 m = (typeof(m)){"LPe1000-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002056 break;
2057 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002058 m = (typeof(m)){"LPe1002-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002059 break;
dea31012005-04-17 16:05:31 -05002060 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05002061 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002062 break;
2063 case PCI_DEVICE_ID_BSMB:
James Smarta747c9c2009-11-18 15:41:10 -05002064 m = (typeof(m)){"LP111", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002065 break;
2066 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05002067 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002068 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002069 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002070 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002071 break;
2072 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002073 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04002074 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002075 break;
dea31012005-04-17 16:05:31 -05002076 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05002077 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002078 break;
2079 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05002080 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002081 break;
2082 case PCI_DEVICE_ID_LP101:
James Smarta747c9c2009-11-18 15:41:10 -05002083 m = (typeof(m)){"LP101", "PCI-X", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002084 break;
2085 case PCI_DEVICE_ID_LP10000S:
James Smarta747c9c2009-11-18 15:41:10 -05002086 m = (typeof(m)){"LP10000-S", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002087 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002088 case PCI_DEVICE_ID_LP11000S:
James Smarta747c9c2009-11-18 15:41:10 -05002089 m = (typeof(m)){"LP11000-S", "PCI-X2", "Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05002090 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002091 case PCI_DEVICE_ID_LPE11000S:
James Smarta747c9c2009-11-18 15:41:10 -05002092 m = (typeof(m)){"LPe11000-S", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002093 break;
James Smartb87eab32007-04-25 09:53:28 -04002094 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05002095 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002096 break;
2097 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05002098 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002099 break;
2100 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05002101 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002102 break;
2103 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002104 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002105 break;
2106 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002107 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002108 break;
2109 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05002110 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002111 break;
James Smart84774a42008-08-24 21:50:06 -04002112 case PCI_DEVICE_ID_HORNET:
James Smarta747c9c2009-11-18 15:41:10 -05002113 m = (typeof(m)){"LP21000", "PCIe", "FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002114 GE = 1;
2115 break;
2116 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05002117 m = (typeof(m)){"LPev12000", "PCIe IOV",
2118 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002119 break;
2120 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05002121 m = (typeof(m)){"LPev12000", "PCIe IOV",
2122 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002123 break;
2124 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05002125 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
2126 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002127 break;
James Smartda0436e2009-05-22 14:51:39 -04002128 case PCI_DEVICE_ID_TIGERSHARK:
2129 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002130 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04002131 break;
James Smarta747c9c2009-11-18 15:41:10 -05002132 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04002133 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002134 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2135 break;
2136 case PCI_DEVICE_ID_FALCON:
2137 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2138 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04002139 break;
James Smart98fc5dd2010-06-07 15:24:29 -04002140 case PCI_DEVICE_ID_BALIUS:
2141 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
2142 "Fibre Channel Adapter"};
2143 break;
James Smart085c6472010-11-20 23:11:37 -05002144 case PCI_DEVICE_ID_LANCER_FC:
James Smartc0c11512011-05-24 11:41:34 -04002145 case PCI_DEVICE_ID_LANCER_FC_VF:
2146 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
James Smart085c6472010-11-20 23:11:37 -05002147 break;
2148 case PCI_DEVICE_ID_LANCER_FCOE:
James Smartc0c11512011-05-24 11:41:34 -04002149 case PCI_DEVICE_ID_LANCER_FCOE_VF:
James Smart085c6472010-11-20 23:11:37 -05002150 oneConnect = 1;
James Smart079b5c92011-08-21 21:48:49 -04002151 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
James Smart085c6472010-11-20 23:11:37 -05002152 break;
James Smartf8cafd32012-08-03 12:42:51 -04002153 case PCI_DEVICE_ID_SKYHAWK:
2154 case PCI_DEVICE_ID_SKYHAWK_VF:
2155 oneConnect = 1;
2156 m = (typeof(m)){"OCe14000", "PCIe", "FCoE"};
2157 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002158 default:
James Smarta747c9c2009-11-18 15:41:10 -05002159 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002160 break;
dea31012005-04-17 16:05:31 -05002161 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002162
2163 if (mdp && mdp[0] == '\0')
2164 snprintf(mdp, 79,"%s", m.name);
James Smartc0c11512011-05-24 11:41:34 -04002165 /*
2166 * oneConnect hba requires special processing, they are all initiators
James Smartda0436e2009-05-22 14:51:39 -04002167 * and we put the port number on the end
2168 */
2169 if (descp && descp[0] == '\0') {
2170 if (oneConnect)
2171 snprintf(descp, 255,
James Smart4169d862012-09-29 11:32:09 -04002172 "Emulex OneConnect %s, %s Initiator %s",
James Smarta747c9c2009-11-18 15:41:10 -05002173 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04002174 phba->Port);
James Smart4169d862012-09-29 11:32:09 -04002175 else if (max_speed == 0)
2176 snprintf(descp, 255,
2177 "Emulex %s %s %s ",
2178 m.name, m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002179 else
2180 snprintf(descp, 255,
2181 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05002182 m.name, max_speed, (GE) ? "GE" : "Gb",
2183 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002184 }
dea31012005-04-17 16:05:31 -05002185}
2186
James Smarte59058c2008-08-24 21:49:00 -04002187/**
James Smart3621a712009-04-06 18:47:14 -04002188 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04002189 * @phba: pointer to lpfc hba data structure.
2190 * @pring: pointer to a IOCB ring.
2191 * @cnt: the number of IOCBs to be posted to the IOCB ring.
2192 *
2193 * This routine posts a given number of IOCBs with the associated DMA buffer
2194 * descriptors specified by the cnt argument to the given IOCB ring.
2195 *
2196 * Return codes
2197 * The number of IOCBs NOT able to be posted to the IOCB ring.
2198 **/
dea31012005-04-17 16:05:31 -05002199int
James Smart495a7142008-06-14 22:52:59 -04002200lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05002201{
2202 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002203 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05002204 struct lpfc_dmabuf *mp1, *mp2;
2205
2206 cnt += pring->missbufcnt;
2207
2208 /* While there are buffers to post */
2209 while (cnt > 0) {
2210 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002211 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002212 if (iocb == NULL) {
2213 pring->missbufcnt = cnt;
2214 return cnt;
2215 }
dea31012005-04-17 16:05:31 -05002216 icmd = &iocb->iocb;
2217
2218 /* 2 buffers can be posted per command */
2219 /* Allocate buffer to post */
2220 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2221 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04002222 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2223 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002224 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002225 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002226 pring->missbufcnt = cnt;
2227 return cnt;
2228 }
2229
2230 INIT_LIST_HEAD(&mp1->list);
2231 /* Allocate buffer to post */
2232 if (cnt > 1) {
2233 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2234 if (mp2)
2235 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2236 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04002237 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002238 kfree(mp2);
dea31012005-04-17 16:05:31 -05002239 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2240 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002241 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002242 pring->missbufcnt = cnt;
2243 return cnt;
2244 }
2245
2246 INIT_LIST_HEAD(&mp2->list);
2247 } else {
2248 mp2 = NULL;
2249 }
2250
2251 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2252 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2253 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2254 icmd->ulpBdeCount = 1;
2255 cnt--;
2256 if (mp2) {
2257 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2258 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2259 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2260 cnt--;
2261 icmd->ulpBdeCount = 2;
2262 }
2263
2264 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2265 icmd->ulpLe = 1;
2266
James Smart3772a992009-05-22 14:50:54 -04002267 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2268 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05002269 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2270 kfree(mp1);
2271 cnt++;
2272 if (mp2) {
2273 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2274 kfree(mp2);
2275 cnt++;
2276 }
James Bottomley604a3e32005-10-29 10:28:33 -05002277 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002278 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05002279 return cnt;
2280 }
dea31012005-04-17 16:05:31 -05002281 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05002282 if (mp2)
dea31012005-04-17 16:05:31 -05002283 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05002284 }
2285 pring->missbufcnt = 0;
2286 return 0;
2287}
2288
James Smarte59058c2008-08-24 21:49:00 -04002289/**
James Smart3621a712009-04-06 18:47:14 -04002290 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04002291 * @phba: pointer to lpfc hba data structure.
2292 *
2293 * This routine posts initial receive IOCB buffers to the ELS ring. The
2294 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
2295 * set to 64 IOCBs.
2296 *
2297 * Return codes
2298 * 0 - success (currently always success)
2299 **/
dea31012005-04-17 16:05:31 -05002300static int
James Smart2e0fef82007-06-17 19:56:36 -05002301lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002302{
2303 struct lpfc_sli *psli = &phba->sli;
2304
2305 /* Ring 0, ELS / CT buffers */
James Smart495a7142008-06-14 22:52:59 -04002306 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05002307 /* Ring 2 - FCP no buffers needed */
2308
2309 return 0;
2310}
2311
2312#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2313
James Smarte59058c2008-08-24 21:49:00 -04002314/**
James Smart3621a712009-04-06 18:47:14 -04002315 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04002316 * @HashResultPointer: pointer to an array as hash table.
2317 *
2318 * This routine sets up the initial values to the array of hash table entries
2319 * for the LC HBAs.
2320 **/
dea31012005-04-17 16:05:31 -05002321static void
2322lpfc_sha_init(uint32_t * HashResultPointer)
2323{
2324 HashResultPointer[0] = 0x67452301;
2325 HashResultPointer[1] = 0xEFCDAB89;
2326 HashResultPointer[2] = 0x98BADCFE;
2327 HashResultPointer[3] = 0x10325476;
2328 HashResultPointer[4] = 0xC3D2E1F0;
2329}
2330
James Smarte59058c2008-08-24 21:49:00 -04002331/**
James Smart3621a712009-04-06 18:47:14 -04002332 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04002333 * @HashResultPointer: pointer to an initial/result hash table.
2334 * @HashWorkingPointer: pointer to an working hash table.
2335 *
2336 * This routine iterates an initial hash table pointed by @HashResultPointer
2337 * with the values from the working hash table pointeed by @HashWorkingPointer.
2338 * The results are putting back to the initial hash table, returned through
2339 * the @HashResultPointer as the result hash table.
2340 **/
dea31012005-04-17 16:05:31 -05002341static void
2342lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2343{
2344 int t;
2345 uint32_t TEMP;
2346 uint32_t A, B, C, D, E;
2347 t = 16;
2348 do {
2349 HashWorkingPointer[t] =
2350 S(1,
2351 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2352 8] ^
2353 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2354 } while (++t <= 79);
2355 t = 0;
2356 A = HashResultPointer[0];
2357 B = HashResultPointer[1];
2358 C = HashResultPointer[2];
2359 D = HashResultPointer[3];
2360 E = HashResultPointer[4];
2361
2362 do {
2363 if (t < 20) {
2364 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2365 } else if (t < 40) {
2366 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2367 } else if (t < 60) {
2368 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2369 } else {
2370 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2371 }
2372 TEMP += S(5, A) + E + HashWorkingPointer[t];
2373 E = D;
2374 D = C;
2375 C = S(30, B);
2376 B = A;
2377 A = TEMP;
2378 } while (++t <= 79);
2379
2380 HashResultPointer[0] += A;
2381 HashResultPointer[1] += B;
2382 HashResultPointer[2] += C;
2383 HashResultPointer[3] += D;
2384 HashResultPointer[4] += E;
2385
2386}
2387
James Smarte59058c2008-08-24 21:49:00 -04002388/**
James Smart3621a712009-04-06 18:47:14 -04002389 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002390 * @RandomChallenge: pointer to the entry of host challenge random number array.
2391 * @HashWorking: pointer to the entry of the working hash array.
2392 *
2393 * This routine calculates the working hash array referred by @HashWorking
2394 * from the challenge random numbers associated with the host, referred by
2395 * @RandomChallenge. The result is put into the entry of the working hash
2396 * array and returned by reference through @HashWorking.
2397 **/
dea31012005-04-17 16:05:31 -05002398static void
2399lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2400{
2401 *HashWorking = (*RandomChallenge ^ *HashWorking);
2402}
2403
James Smarte59058c2008-08-24 21:49:00 -04002404/**
James Smart3621a712009-04-06 18:47:14 -04002405 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002406 * @phba: pointer to lpfc hba data structure.
2407 * @hbainit: pointer to an array of unsigned 32-bit integers.
2408 *
2409 * This routine performs the special handling for LC HBA initialization.
2410 **/
dea31012005-04-17 16:05:31 -05002411void
2412lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2413{
2414 int t;
2415 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002416 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002417
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002418 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002419 if (!HashWorking)
2420 return;
2421
dea31012005-04-17 16:05:31 -05002422 HashWorking[0] = HashWorking[78] = *pwwnn++;
2423 HashWorking[1] = HashWorking[79] = *pwwnn;
2424
2425 for (t = 0; t < 7; t++)
2426 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2427
2428 lpfc_sha_init(hbainit);
2429 lpfc_sha_iterate(hbainit, HashWorking);
2430 kfree(HashWorking);
2431}
2432
James Smarte59058c2008-08-24 21:49:00 -04002433/**
James Smart3621a712009-04-06 18:47:14 -04002434 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002435 * @vport: pointer to a virtual N_Port data structure.
2436 *
2437 * This routine performs the necessary cleanups before deleting the @vport.
2438 * It invokes the discovery state machine to perform necessary state
2439 * transitions and to release the ndlps associated with the @vport. Note,
2440 * the physical port is treated as @vport 0.
2441 **/
James Smart87af33f2007-10-27 13:37:43 -04002442void
James Smart2e0fef82007-06-17 19:56:36 -05002443lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002444{
James Smart87af33f2007-10-27 13:37:43 -04002445 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002446 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002447 int i = 0;
dea31012005-04-17 16:05:31 -05002448
James Smart87af33f2007-10-27 13:37:43 -04002449 if (phba->link_state > LPFC_LINK_DOWN)
2450 lpfc_port_link_failure(vport);
2451
2452 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002453 if (!NLP_CHK_NODE_ACT(ndlp)) {
2454 ndlp = lpfc_enable_node(vport, ndlp,
2455 NLP_STE_UNUSED_NODE);
2456 if (!ndlp)
2457 continue;
2458 spin_lock_irq(&phba->ndlp_lock);
2459 NLP_SET_FREE_REQ(ndlp);
2460 spin_unlock_irq(&phba->ndlp_lock);
2461 /* Trigger the release of the ndlp memory */
2462 lpfc_nlp_put(ndlp);
2463 continue;
2464 }
2465 spin_lock_irq(&phba->ndlp_lock);
2466 if (NLP_CHK_FREE_REQ(ndlp)) {
2467 /* The ndlp should not be in memory free mode already */
2468 spin_unlock_irq(&phba->ndlp_lock);
2469 continue;
2470 } else
2471 /* Indicate request for freeing ndlp memory */
2472 NLP_SET_FREE_REQ(ndlp);
2473 spin_unlock_irq(&phba->ndlp_lock);
2474
James Smart58da1ff2008-04-07 10:15:56 -04002475 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2476 ndlp->nlp_DID == Fabric_DID) {
2477 /* Just free up ndlp with Fabric_DID for vports */
2478 lpfc_nlp_put(ndlp);
2479 continue;
2480 }
2481
James Smarteff4a012012-01-18 16:25:25 -05002482 /* take care of nodes in unused state before the state
2483 * machine taking action.
2484 */
2485 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
2486 lpfc_nlp_put(ndlp);
2487 continue;
2488 }
2489
James Smart87af33f2007-10-27 13:37:43 -04002490 if (ndlp->nlp_type & NLP_FABRIC)
2491 lpfc_disc_state_machine(vport, ndlp, NULL,
2492 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002493
James Smart87af33f2007-10-27 13:37:43 -04002494 lpfc_disc_state_machine(vport, ndlp, NULL,
2495 NLP_EVT_DEVICE_RM);
2496 }
2497
James Smarta8adb832007-10-27 13:37:53 -04002498 /* At this point, ALL ndlp's should be gone
2499 * because of the previous NLP_EVT_DEVICE_RM.
2500 * Lets wait for this to happen, if needed.
2501 */
James Smart87af33f2007-10-27 13:37:43 -04002502 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002503 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002504 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002505 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002506 list_for_each_entry_safe(ndlp, next_ndlp,
2507 &vport->fc_nodes, nlp_listp) {
2508 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2509 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002510 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002511 "usgmap:x%x refcnt:%d\n",
2512 ndlp->nlp_DID, (void *)ndlp,
2513 ndlp->nlp_usg_map,
2514 atomic_read(
2515 &ndlp->kref.refcount));
2516 }
James Smarta8adb832007-10-27 13:37:53 -04002517 break;
James Smart87af33f2007-10-27 13:37:43 -04002518 }
James Smarta8adb832007-10-27 13:37:53 -04002519
2520 /* Wait for any activity on ndlps to settle */
2521 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002522 }
James Smart1151e3e2011-02-16 12:39:35 -05002523 lpfc_cleanup_vports_rrqs(vport, NULL);
dea31012005-04-17 16:05:31 -05002524}
2525
James Smarte59058c2008-08-24 21:49:00 -04002526/**
James Smart3621a712009-04-06 18:47:14 -04002527 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002528 * @vport: pointer to a virtual N_Port data structure.
2529 *
2530 * This routine stops all the timers associated with a @vport. This function
2531 * is invoked before disabling or deleting a @vport. Note that the physical
2532 * port is treated as @vport 0.
2533 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002534void
2535lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002536{
James Smart92d7f7b2007-06-17 19:56:38 -05002537 del_timer_sync(&vport->els_tmofunc);
2538 del_timer_sync(&vport->fc_fdmitmo);
James Smart92494142011-02-16 12:39:44 -05002539 del_timer_sync(&vport->delayed_disc_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002540 lpfc_can_disctmo(vport);
2541 return;
dea31012005-04-17 16:05:31 -05002542}
2543
James Smarte59058c2008-08-24 21:49:00 -04002544/**
James Smartecfd03c2010-02-12 14:41:27 -05002545 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2546 * @phba: pointer to lpfc hba data structure.
2547 *
2548 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2549 * caller of this routine should already hold the host lock.
2550 **/
2551void
2552__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2553{
James Smart5ac6b302010-10-22 11:05:36 -04002554 /* Clear pending FCF rediscovery wait flag */
2555 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2556
James Smartecfd03c2010-02-12 14:41:27 -05002557 /* Now, try to stop the timer */
2558 del_timer(&phba->fcf.redisc_wait);
2559}
2560
2561/**
2562 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2563 * @phba: pointer to lpfc hba data structure.
2564 *
2565 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2566 * checks whether the FCF rediscovery wait timer is pending with the host
2567 * lock held before proceeding with disabling the timer and clearing the
2568 * wait timer pendig flag.
2569 **/
2570void
2571lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2572{
2573 spin_lock_irq(&phba->hbalock);
2574 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2575 /* FCF rediscovery timer already fired or stopped */
2576 spin_unlock_irq(&phba->hbalock);
2577 return;
2578 }
2579 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart5ac6b302010-10-22 11:05:36 -04002580 /* Clear failover in progress flags */
2581 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05002582 spin_unlock_irq(&phba->hbalock);
2583}
2584
2585/**
James Smart3772a992009-05-22 14:50:54 -04002586 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002587 * @phba: pointer to lpfc hba data structure.
2588 *
2589 * This routine stops all the timers associated with a HBA. This function is
2590 * invoked before either putting a HBA offline or unloading the driver.
2591 **/
James Smart3772a992009-05-22 14:50:54 -04002592void
2593lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002594{
James Smart51ef4c22007-08-02 11:10:31 -04002595 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05002596 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002597 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002598 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002599 del_timer_sync(&phba->hb_tmofunc);
James Smart1151e3e2011-02-16 12:39:35 -05002600 if (phba->sli_rev == LPFC_SLI_REV4) {
2601 del_timer_sync(&phba->rrq_tmr);
2602 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2603 }
James Smart3772a992009-05-22 14:50:54 -04002604 phba->hb_outstanding = 0;
2605
2606 switch (phba->pci_dev_grp) {
2607 case LPFC_PCI_DEV_LP:
2608 /* Stop any LightPulse device specific driver timers */
2609 del_timer_sync(&phba->fcp_poll_timer);
2610 break;
2611 case LPFC_PCI_DEV_OC:
2612 /* Stop any OneConnect device sepcific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05002613 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04002614 break;
2615 default:
2616 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2617 "0297 Invalid device group (x%x)\n",
2618 phba->pci_dev_grp);
2619 break;
2620 }
James Smart2e0fef82007-06-17 19:56:36 -05002621 return;
dea31012005-04-17 16:05:31 -05002622}
2623
James Smarte59058c2008-08-24 21:49:00 -04002624/**
James Smart3621a712009-04-06 18:47:14 -04002625 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002626 * @phba: pointer to lpfc hba data structure.
2627 *
2628 * This routine marks a HBA's management interface as blocked. Once the HBA's
2629 * management interface is marked as blocked, all the user space access to
2630 * the HBA, whether they are from sysfs interface or libdfc interface will
2631 * all be blocked. The HBA is set to block the management interface when the
2632 * driver prepares the HBA interface for online or offline.
2633 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002634static void
James Smart618a5232012-06-12 13:54:36 -04002635lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
Adrian Bunka6ababd2007-11-05 18:07:33 +01002636{
2637 unsigned long iflag;
James Smart6e7288d2010-06-07 15:23:35 -04002638 uint8_t actcmd = MBX_HEARTBEAT;
2639 unsigned long timeout;
2640
Adrian Bunka6ababd2007-11-05 18:07:33 +01002641 spin_lock_irqsave(&phba->hbalock, iflag);
2642 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
James Smart618a5232012-06-12 13:54:36 -04002643 spin_unlock_irqrestore(&phba->hbalock, iflag);
2644 if (mbx_action == LPFC_MBX_NO_WAIT)
2645 return;
2646 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
2647 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002648 if (phba->sli.mbox_active) {
James Smart6e7288d2010-06-07 15:23:35 -04002649 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
James Smarta183a152011-10-10 21:32:43 -04002650 /* Determine how long we might wait for the active mailbox
2651 * command to be gracefully completed by firmware.
2652 */
2653 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
2654 phba->sli.mbox_active) * 1000) + jiffies;
2655 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002656 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002657
James Smart6e7288d2010-06-07 15:23:35 -04002658 /* Wait for the outstnading mailbox command to complete */
2659 while (phba->sli.mbox_active) {
2660 /* Check active mailbox complete status every 2ms */
2661 msleep(2);
2662 if (time_after(jiffies, timeout)) {
2663 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2664 "2813 Mgmt IO is Blocked %x "
2665 "- mbox cmd %x still active\n",
2666 phba->sli.sli_flag, actcmd);
2667 break;
2668 }
2669 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002670}
2671
James Smarte59058c2008-08-24 21:49:00 -04002672/**
James Smart6b5151f2012-01-18 16:24:06 -05002673 * lpfc_sli4_node_prep - Assign RPIs for active nodes.
2674 * @phba: pointer to lpfc hba data structure.
2675 *
2676 * Allocate RPIs for all active remote nodes. This is needed whenever
2677 * an SLI4 adapter is reset and the driver is not unloading. Its purpose
2678 * is to fixup the temporary rpi assignments.
2679 **/
2680void
2681lpfc_sli4_node_prep(struct lpfc_hba *phba)
2682{
2683 struct lpfc_nodelist *ndlp, *next_ndlp;
2684 struct lpfc_vport **vports;
2685 int i;
2686
2687 if (phba->sli_rev != LPFC_SLI_REV4)
2688 return;
2689
2690 vports = lpfc_create_vport_work_array(phba);
2691 if (vports != NULL) {
2692 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2693 if (vports[i]->load_flag & FC_UNLOADING)
2694 continue;
2695
2696 list_for_each_entry_safe(ndlp, next_ndlp,
2697 &vports[i]->fc_nodes,
2698 nlp_listp) {
2699 if (NLP_CHK_NODE_ACT(ndlp))
2700 ndlp->nlp_rpi =
2701 lpfc_sli4_alloc_rpi(phba);
2702 }
2703 }
2704 }
2705 lpfc_destroy_vport_work_array(phba, vports);
2706}
2707
2708/**
James Smart3621a712009-04-06 18:47:14 -04002709 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04002710 * @phba: pointer to lpfc hba data structure.
2711 *
2712 * This routine initializes the HBA and brings a HBA online. During this
2713 * process, the management interface is blocked to prevent user space access
2714 * to the HBA interfering with the driver initialization.
2715 *
2716 * Return codes
2717 * 0 - successful
2718 * 1 - failed
2719 **/
dea31012005-04-17 16:05:31 -05002720int
James Smart2e0fef82007-06-17 19:56:36 -05002721lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002722{
Julia Lawall372bd282008-12-16 16:15:08 +01002723 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04002724 struct lpfc_vport **vports;
2725 int i;
James Smart16a3a202013-04-17 20:14:38 -04002726 bool vpis_cleared = false;
James Smart2e0fef82007-06-17 19:56:36 -05002727
dea31012005-04-17 16:05:31 -05002728 if (!phba)
2729 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01002730 vport = phba->pport;
dea31012005-04-17 16:05:31 -05002731
James Smart2e0fef82007-06-17 19:56:36 -05002732 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05002733 return 0;
2734
James Smarted957682007-06-17 19:56:37 -05002735 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002736 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05002737
James Smart618a5232012-06-12 13:54:36 -04002738 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05002739
James Smart46fa3112007-04-25 09:51:45 -04002740 if (!lpfc_sli_queue_setup(phba)) {
2741 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002742 return 1;
James Smart46fa3112007-04-25 09:51:45 -04002743 }
2744
James Smartda0436e2009-05-22 14:51:39 -04002745 if (phba->sli_rev == LPFC_SLI_REV4) {
2746 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2747 lpfc_unblock_mgmt_io(phba);
2748 return 1;
2749 }
James Smart16a3a202013-04-17 20:14:38 -04002750 spin_lock_irq(&phba->hbalock);
2751 if (!phba->sli4_hba.max_cfg_param.vpi_used)
2752 vpis_cleared = true;
2753 spin_unlock_irq(&phba->hbalock);
James Smartda0436e2009-05-22 14:51:39 -04002754 } else {
2755 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2756 lpfc_unblock_mgmt_io(phba);
2757 return 1;
2758 }
James Smart46fa3112007-04-25 09:51:45 -04002759 }
dea31012005-04-17 16:05:31 -05002760
James Smart549e55c2007-08-02 11:09:51 -04002761 vports = lpfc_create_vport_work_array(phba);
2762 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002763 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002764 struct Scsi_Host *shost;
2765 shost = lpfc_shost_from_vport(vports[i]);
2766 spin_lock_irq(shost->host_lock);
2767 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2768 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2769 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart16a3a202013-04-17 20:14:38 -04002770 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart1c6834a2009-07-19 10:01:26 -04002771 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart16a3a202013-04-17 20:14:38 -04002772 if ((vpis_cleared) &&
2773 (vports[i]->port_type !=
2774 LPFC_PHYSICAL_PORT))
2775 vports[i]->vpi = 0;
2776 }
James Smart549e55c2007-08-02 11:09:51 -04002777 spin_unlock_irq(shost->host_lock);
2778 }
James Smart09372822008-01-11 01:52:54 -05002779 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002780
James Smart46fa3112007-04-25 09:51:45 -04002781 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002782 return 0;
2783}
2784
James Smarte59058c2008-08-24 21:49:00 -04002785/**
James Smart3621a712009-04-06 18:47:14 -04002786 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04002787 * @phba: pointer to lpfc hba data structure.
2788 *
2789 * This routine marks a HBA's management interface as not blocked. Once the
2790 * HBA's management interface is marked as not blocked, all the user space
2791 * access to the HBA, whether they are from sysfs interface or libdfc
2792 * interface will be allowed. The HBA is set to block the management interface
2793 * when the driver prepares the HBA interface for online or offline and then
2794 * set to unblock the management interface afterwards.
2795 **/
James Smart46fa3112007-04-25 09:51:45 -04002796void
James Smart46fa3112007-04-25 09:51:45 -04002797lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2798{
2799 unsigned long iflag;
2800
James Smart2e0fef82007-06-17 19:56:36 -05002801 spin_lock_irqsave(&phba->hbalock, iflag);
2802 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2803 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04002804}
2805
James Smarte59058c2008-08-24 21:49:00 -04002806/**
James Smart3621a712009-04-06 18:47:14 -04002807 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04002808 * @phba: pointer to lpfc hba data structure.
2809 *
2810 * This routine is invoked to prepare a HBA to be brought offline. It performs
2811 * unregistration login to all the nodes on all vports and flushes the mailbox
2812 * queue to make it ready to be brought offline.
2813 **/
James Smart46fa3112007-04-25 09:51:45 -04002814void
James Smart618a5232012-06-12 13:54:36 -04002815lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
James Smart46fa3112007-04-25 09:51:45 -04002816{
James Smart2e0fef82007-06-17 19:56:36 -05002817 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04002818 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04002819 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05002820 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04002821 int i;
dea31012005-04-17 16:05:31 -05002822
James Smart2e0fef82007-06-17 19:56:36 -05002823 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002824 return;
dea31012005-04-17 16:05:31 -05002825
James Smart618a5232012-06-12 13:54:36 -04002826 lpfc_block_mgmt_io(phba, mbx_action);
dea31012005-04-17 16:05:31 -05002827
2828 lpfc_linkdown(phba);
2829
James Smart87af33f2007-10-27 13:37:43 -04002830 /* Issue an unreg_login to all nodes on all vports */
2831 vports = lpfc_create_vport_work_array(phba);
2832 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04002833 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04002834 if (vports[i]->load_flag & FC_UNLOADING)
2835 continue;
James Smart72100cc2010-02-12 14:43:01 -05002836 shost = lpfc_shost_from_vport(vports[i]);
2837 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002838 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002839 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2840 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05002841 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002842
James Smart87af33f2007-10-27 13:37:43 -04002843 shost = lpfc_shost_from_vport(vports[i]);
2844 list_for_each_entry_safe(ndlp, next_ndlp,
2845 &vports[i]->fc_nodes,
2846 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002847 if (!NLP_CHK_NODE_ACT(ndlp))
2848 continue;
James Smart87af33f2007-10-27 13:37:43 -04002849 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2850 continue;
2851 if (ndlp->nlp_type & NLP_FABRIC) {
2852 lpfc_disc_state_machine(vports[i], ndlp,
2853 NULL, NLP_EVT_DEVICE_RECOVERY);
2854 lpfc_disc_state_machine(vports[i], ndlp,
2855 NULL, NLP_EVT_DEVICE_RM);
2856 }
2857 spin_lock_irq(shost->host_lock);
2858 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart401ee0c2012-03-01 22:35:34 -05002859 spin_unlock_irq(shost->host_lock);
James Smart6b5151f2012-01-18 16:24:06 -05002860 /*
2861 * Whenever an SLI4 port goes offline, free the
James Smart401ee0c2012-03-01 22:35:34 -05002862 * RPI. Get a new RPI when the adapter port
2863 * comes back online.
James Smart6b5151f2012-01-18 16:24:06 -05002864 */
2865 if (phba->sli_rev == LPFC_SLI_REV4)
2866 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smart87af33f2007-10-27 13:37:43 -04002867 lpfc_unreg_rpi(vports[i], ndlp);
2868 }
2869 }
2870 }
James Smart09372822008-01-11 01:52:54 -05002871 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002872
James Smart618a5232012-06-12 13:54:36 -04002873 lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
James Smart46fa3112007-04-25 09:51:45 -04002874}
2875
James Smarte59058c2008-08-24 21:49:00 -04002876/**
James Smart3621a712009-04-06 18:47:14 -04002877 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04002878 * @phba: pointer to lpfc hba data structure.
2879 *
2880 * This routine actually brings a HBA offline. It stops all the timers
2881 * associated with the HBA, brings down the SLI layer, and eventually
2882 * marks the HBA as in offline state for the upper layer protocol.
2883 **/
James Smart46fa3112007-04-25 09:51:45 -04002884void
James Smart2e0fef82007-06-17 19:56:36 -05002885lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04002886{
James Smart549e55c2007-08-02 11:09:51 -04002887 struct Scsi_Host *shost;
2888 struct lpfc_vport **vports;
2889 int i;
James Smart46fa3112007-04-25 09:51:45 -04002890
James Smart549e55c2007-08-02 11:09:51 -04002891 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002892 return;
James Smart688a8862006-07-06 15:49:56 -04002893
James Smartda0436e2009-05-22 14:51:39 -04002894 /* stop port and all timers associated with this hba */
2895 lpfc_stop_port(phba);
James Smart51ef4c22007-08-02 11:10:31 -04002896 vports = lpfc_create_vport_work_array(phba);
2897 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002898 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04002899 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05002900 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05002901 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002902 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05002903 /* Bring down the SLI Layer and cleanup. The HBA is offline
2904 now. */
2905 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002906 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04002907 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002908 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04002909 vports = lpfc_create_vport_work_array(phba);
2910 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002911 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002912 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04002913 spin_lock_irq(shost->host_lock);
2914 vports[i]->work_port_events = 0;
2915 vports[i]->fc_flag |= FC_OFFLINE_MODE;
2916 spin_unlock_irq(shost->host_lock);
2917 }
James Smart09372822008-01-11 01:52:54 -05002918 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002919}
2920
James Smarte59058c2008-08-24 21:49:00 -04002921/**
James Smart3621a712009-04-06 18:47:14 -04002922 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04002923 * @phba: pointer to lpfc hba data structure.
2924 *
2925 * This routine is to free all the SCSI buffers and IOCBs from the driver
2926 * list back to kernel. It is called from lpfc_pci_remove_one to free
2927 * the internal resources before the device is removed from the system.
James Smarte59058c2008-08-24 21:49:00 -04002928 **/
James Smart8a9d2e82012-05-09 21:16:12 -04002929static void
James Smart2e0fef82007-06-17 19:56:36 -05002930lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002931{
2932 struct lpfc_scsi_buf *sb, *sb_next;
2933 struct lpfc_iocbq *io, *io_next;
2934
James Smart2e0fef82007-06-17 19:56:36 -05002935 spin_lock_irq(&phba->hbalock);
James Smarta40fc5f2013-04-17 20:17:40 -04002936
dea31012005-04-17 16:05:31 -05002937 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smarta40fc5f2013-04-17 20:17:40 -04002938
2939 spin_lock(&phba->scsi_buf_list_put_lock);
2940 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_put,
2941 list) {
dea31012005-04-17 16:05:31 -05002942 list_del(&sb->list);
2943 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05002944 sb->dma_handle);
dea31012005-04-17 16:05:31 -05002945 kfree(sb);
2946 phba->total_scsi_bufs--;
2947 }
James Smarta40fc5f2013-04-17 20:17:40 -04002948 spin_unlock(&phba->scsi_buf_list_put_lock);
2949
2950 spin_lock(&phba->scsi_buf_list_get_lock);
2951 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_get,
2952 list) {
2953 list_del(&sb->list);
2954 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
2955 sb->dma_handle);
2956 kfree(sb);
2957 phba->total_scsi_bufs--;
2958 }
2959 spin_unlock(&phba->scsi_buf_list_get_lock);
dea31012005-04-17 16:05:31 -05002960
2961 /* Release all the lpfc_iocbq entries maintained by this host. */
2962 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2963 list_del(&io->list);
2964 kfree(io);
2965 phba->total_iocbq_bufs--;
2966 }
James Smart6d368e52011-05-24 11:44:12 -04002967
James Smart2e0fef82007-06-17 19:56:36 -05002968 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04002969}
2970
2971/**
2972 * lpfc_sli4_xri_sgl_update - update xri-sgl sizing and mapping
2973 * @phba: pointer to lpfc hba data structure.
2974 *
2975 * This routine first calculates the sizes of the current els and allocated
2976 * scsi sgl lists, and then goes through all sgls to updates the physical
2977 * XRIs assigned due to port function reset. During port initialization, the
2978 * current els and allocated scsi sgl lists are 0s.
2979 *
2980 * Return codes
2981 * 0 - successful (for now, it always returns 0)
2982 **/
2983int
2984lpfc_sli4_xri_sgl_update(struct lpfc_hba *phba)
2985{
2986 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
2987 struct lpfc_scsi_buf *psb = NULL, *psb_next = NULL;
2988 uint16_t i, lxri, xri_cnt, els_xri_cnt, scsi_xri_cnt;
2989 LIST_HEAD(els_sgl_list);
2990 LIST_HEAD(scsi_sgl_list);
2991 int rc;
2992
2993 /*
2994 * update on pci function's els xri-sgl list
2995 */
2996 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
2997 if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
2998 /* els xri-sgl expanded */
2999 xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
3000 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3001 "3157 ELS xri-sgl count increased from "
3002 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3003 els_xri_cnt);
3004 /* allocate the additional els sgls */
3005 for (i = 0; i < xri_cnt; i++) {
3006 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3007 GFP_KERNEL);
3008 if (sglq_entry == NULL) {
3009 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3010 "2562 Failure to allocate an "
3011 "ELS sgl entry:%d\n", i);
3012 rc = -ENOMEM;
3013 goto out_free_mem;
3014 }
3015 sglq_entry->buff_type = GEN_BUFF_TYPE;
3016 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
3017 &sglq_entry->phys);
3018 if (sglq_entry->virt == NULL) {
3019 kfree(sglq_entry);
3020 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3021 "2563 Failure to allocate an "
3022 "ELS mbuf:%d\n", i);
3023 rc = -ENOMEM;
3024 goto out_free_mem;
3025 }
3026 sglq_entry->sgl = sglq_entry->virt;
3027 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
3028 sglq_entry->state = SGL_FREED;
3029 list_add_tail(&sglq_entry->list, &els_sgl_list);
3030 }
James Smart38c20672013-03-01 16:37:44 -05003031 spin_lock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003032 list_splice_init(&els_sgl_list, &phba->sli4_hba.lpfc_sgl_list);
James Smart38c20672013-03-01 16:37:44 -05003033 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003034 } else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
3035 /* els xri-sgl shrinked */
3036 xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
3037 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3038 "3158 ELS xri-sgl count decreased from "
3039 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3040 els_xri_cnt);
3041 spin_lock_irq(&phba->hbalock);
3042 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &els_sgl_list);
3043 spin_unlock_irq(&phba->hbalock);
3044 /* release extra els sgls from list */
3045 for (i = 0; i < xri_cnt; i++) {
3046 list_remove_head(&els_sgl_list,
3047 sglq_entry, struct lpfc_sglq, list);
3048 if (sglq_entry) {
3049 lpfc_mbuf_free(phba, sglq_entry->virt,
3050 sglq_entry->phys);
3051 kfree(sglq_entry);
3052 }
3053 }
3054 spin_lock_irq(&phba->hbalock);
3055 list_splice_init(&els_sgl_list, &phba->sli4_hba.lpfc_sgl_list);
3056 spin_unlock_irq(&phba->hbalock);
3057 } else
3058 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3059 "3163 ELS xri-sgl count unchanged: %d\n",
3060 els_xri_cnt);
3061 phba->sli4_hba.els_xri_cnt = els_xri_cnt;
3062
3063 /* update xris to els sgls on the list */
3064 sglq_entry = NULL;
3065 sglq_entry_next = NULL;
3066 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
3067 &phba->sli4_hba.lpfc_sgl_list, list) {
3068 lxri = lpfc_sli4_next_xritag(phba);
3069 if (lxri == NO_XRI) {
3070 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3071 "2400 Failed to allocate xri for "
3072 "ELS sgl\n");
3073 rc = -ENOMEM;
3074 goto out_free_mem;
3075 }
3076 sglq_entry->sli4_lxritag = lxri;
3077 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3078 }
3079
3080 /*
3081 * update on pci function's allocated scsi xri-sgl list
3082 */
3083 phba->total_scsi_bufs = 0;
3084
3085 /* maximum number of xris available for scsi buffers */
3086 phba->sli4_hba.scsi_xri_max = phba->sli4_hba.max_cfg_param.max_xri -
3087 els_xri_cnt;
3088
3089 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3090 "2401 Current allocated SCSI xri-sgl count:%d, "
3091 "maximum SCSI xri count:%d\n",
3092 phba->sli4_hba.scsi_xri_cnt,
3093 phba->sli4_hba.scsi_xri_max);
3094
James Smarta40fc5f2013-04-17 20:17:40 -04003095 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart164cecd2013-09-06 12:22:38 -04003096 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003097 list_splice_init(&phba->lpfc_scsi_buf_list_get, &scsi_sgl_list);
3098 list_splice(&phba->lpfc_scsi_buf_list_put, &scsi_sgl_list);
James Smart164cecd2013-09-06 12:22:38 -04003099 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003100 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003101
3102 if (phba->sli4_hba.scsi_xri_cnt > phba->sli4_hba.scsi_xri_max) {
3103 /* max scsi xri shrinked below the allocated scsi buffers */
3104 scsi_xri_cnt = phba->sli4_hba.scsi_xri_cnt -
3105 phba->sli4_hba.scsi_xri_max;
3106 /* release the extra allocated scsi buffers */
3107 for (i = 0; i < scsi_xri_cnt; i++) {
3108 list_remove_head(&scsi_sgl_list, psb,
3109 struct lpfc_scsi_buf, list);
3110 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, psb->data,
3111 psb->dma_handle);
3112 kfree(psb);
3113 }
James Smarta40fc5f2013-04-17 20:17:40 -04003114 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003115 phba->sli4_hba.scsi_xri_cnt -= scsi_xri_cnt;
James Smarta40fc5f2013-04-17 20:17:40 -04003116 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003117 }
3118
3119 /* update xris associated to remaining allocated scsi buffers */
3120 psb = NULL;
3121 psb_next = NULL;
3122 list_for_each_entry_safe(psb, psb_next, &scsi_sgl_list, list) {
3123 lxri = lpfc_sli4_next_xritag(phba);
3124 if (lxri == NO_XRI) {
3125 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3126 "2560 Failed to allocate xri for "
3127 "scsi buffer\n");
3128 rc = -ENOMEM;
3129 goto out_free_mem;
3130 }
3131 psb->cur_iocbq.sli4_lxritag = lxri;
3132 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3133 }
James Smarta40fc5f2013-04-17 20:17:40 -04003134 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart164cecd2013-09-06 12:22:38 -04003135 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003136 list_splice_init(&scsi_sgl_list, &phba->lpfc_scsi_buf_list_get);
3137 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
James Smart164cecd2013-09-06 12:22:38 -04003138 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003139 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003140
dea31012005-04-17 16:05:31 -05003141 return 0;
James Smart8a9d2e82012-05-09 21:16:12 -04003142
3143out_free_mem:
3144 lpfc_free_els_sgl_list(phba);
3145 lpfc_scsi_free(phba);
3146 return rc;
dea31012005-04-17 16:05:31 -05003147}
3148
James Smarte59058c2008-08-24 21:49:00 -04003149/**
James Smart3621a712009-04-06 18:47:14 -04003150 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04003151 * @phba: pointer to lpfc hba data structure.
3152 * @instance: a unique integer ID to this FC port.
3153 * @dev: pointer to the device data structure.
3154 *
3155 * This routine creates a FC port for the upper layer protocol. The FC port
3156 * can be created on top of either a physical port or a virtual port provided
3157 * by the HBA. This routine also allocates a SCSI host data structure (shost)
3158 * and associates the FC port created before adding the shost into the SCSI
3159 * layer.
3160 *
3161 * Return codes
3162 * @vport - pointer to the virtual N_Port data structure.
3163 * NULL - port create failed.
3164 **/
James Smart2e0fef82007-06-17 19:56:36 -05003165struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04003166lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04003167{
James Smart2e0fef82007-06-17 19:56:36 -05003168 struct lpfc_vport *vport;
3169 struct Scsi_Host *shost;
3170 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04003171
James Smart3de2a652007-08-02 11:09:59 -04003172 if (dev != &phba->pcidev->dev)
3173 shost = scsi_host_alloc(&lpfc_vport_template,
3174 sizeof(struct lpfc_vport));
3175 else
3176 shost = scsi_host_alloc(&lpfc_template,
3177 sizeof(struct lpfc_vport));
James Smart2e0fef82007-06-17 19:56:36 -05003178 if (!shost)
3179 goto out;
James Smart47a86172007-04-25 09:53:22 -04003180
James Smart2e0fef82007-06-17 19:56:36 -05003181 vport = (struct lpfc_vport *) shost->hostdata;
3182 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05003183 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05003184 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05003185 vport->fc_rscn_flush = 0;
James Smart47a86172007-04-25 09:53:22 -04003186
James Smart3de2a652007-08-02 11:09:59 -04003187 lpfc_get_vport_cfgparam(vport);
James Smart2e0fef82007-06-17 19:56:36 -05003188 shost->unique_id = instance;
3189 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04003190 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05003191 shost->this_id = -1;
3192 shost->max_cmd_len = 16;
James Smartda0436e2009-05-22 14:51:39 -04003193 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart28baac72010-02-12 14:42:03 -05003194 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04003195 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smartda0436e2009-05-22 14:51:39 -04003196 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
3197 }
James Smart81301a92008-12-04 22:39:46 -05003198
James Smart47a86172007-04-25 09:53:22 -04003199 /*
James Smart2e0fef82007-06-17 19:56:36 -05003200 * Set initial can_queue value since 0 is no longer supported and
3201 * scsi_add_host will fail. This will be adjusted later based on the
3202 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04003203 */
James Smart2e0fef82007-06-17 19:56:36 -05003204 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04003205 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05003206 shost->transportt = lpfc_vport_transport_template;
3207 vport->port_type = LPFC_NPIV_PORT;
3208 } else {
3209 shost->transportt = lpfc_transport_template;
3210 vport->port_type = LPFC_PHYSICAL_PORT;
3211 }
James Smart47a86172007-04-25 09:53:22 -04003212
James Smart2e0fef82007-06-17 19:56:36 -05003213 /* Initialize all internally managed lists. */
3214 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04003215 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05003216 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04003217
James Smart2e0fef82007-06-17 19:56:36 -05003218 init_timer(&vport->fc_disctmo);
3219 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05003220 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04003221
James Smart2e0fef82007-06-17 19:56:36 -05003222 init_timer(&vport->fc_fdmitmo);
3223 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
James Smart92d7f7b2007-06-17 19:56:38 -05003224 vport->fc_fdmitmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04003225
James Smart2e0fef82007-06-17 19:56:36 -05003226 init_timer(&vport->els_tmofunc);
3227 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05003228 vport->els_tmofunc.data = (unsigned long)vport;
James Smart92494142011-02-16 12:39:44 -05003229
3230 init_timer(&vport->delayed_disc_tmo);
3231 vport->delayed_disc_tmo.function = lpfc_delayed_disc_tmo;
3232 vport->delayed_disc_tmo.data = (unsigned long)vport;
3233
James Bottomleyd139b9b2009-11-05 13:33:12 -06003234 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05003235 if (error)
3236 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04003237
James Smart549e55c2007-08-02 11:09:51 -04003238 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003239 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04003240 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003241 return vport;
James Smart47a86172007-04-25 09:53:22 -04003242
James Smart2e0fef82007-06-17 19:56:36 -05003243out_put_shost:
3244 scsi_host_put(shost);
3245out:
3246 return NULL;
James Smart47a86172007-04-25 09:53:22 -04003247}
3248
James Smarte59058c2008-08-24 21:49:00 -04003249/**
James Smart3621a712009-04-06 18:47:14 -04003250 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04003251 * @vport: pointer to an lpfc virtual N_Port data structure.
3252 *
3253 * This routine destroys a FC port from the upper layer protocol. All the
3254 * resources associated with the port are released.
3255 **/
James Smart2e0fef82007-06-17 19:56:36 -05003256void
3257destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04003258{
James Smart92d7f7b2007-06-17 19:56:38 -05003259 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3260 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003261
James Smart858c9f62007-06-17 19:56:39 -05003262 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05003263 fc_remove_host(shost);
3264 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04003265
James Smart92d7f7b2007-06-17 19:56:38 -05003266 spin_lock_irq(&phba->hbalock);
3267 list_del_init(&vport->listentry);
3268 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04003269
James Smart92d7f7b2007-06-17 19:56:38 -05003270 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04003271 return;
James Smart47a86172007-04-25 09:53:22 -04003272}
3273
James Smarte59058c2008-08-24 21:49:00 -04003274/**
James Smart3621a712009-04-06 18:47:14 -04003275 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04003276 *
3277 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
3278 * uses the kernel idr facility to perform the task.
3279 *
3280 * Return codes:
3281 * instance - a unique integer ID allocated as the new instance.
3282 * -1 - lpfc get instance failed.
3283 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003284int
3285lpfc_get_instance(void)
3286{
Tejun Heoab516032013-02-27 17:04:44 -08003287 int ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003288
Tejun Heoab516032013-02-27 17:04:44 -08003289 ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
3290 return ret < 0 ? -1 : ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003291}
3292
James Smarte59058c2008-08-24 21:49:00 -04003293/**
James Smart3621a712009-04-06 18:47:14 -04003294 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04003295 * @shost: pointer to SCSI host data structure.
3296 * @time: elapsed time of the scan in jiffies.
3297 *
3298 * This routine is called by the SCSI layer with a SCSI host to determine
3299 * whether the scan host is finished.
3300 *
3301 * Note: there is no scan_start function as adapter initialization will have
3302 * asynchronously kicked off the link initialization.
3303 *
3304 * Return codes
3305 * 0 - SCSI host scan is not over yet.
3306 * 1 - SCSI host scan is over.
3307 **/
James Smart47a86172007-04-25 09:53:22 -04003308int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
3309{
James Smart2e0fef82007-06-17 19:56:36 -05003310 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3311 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05003312 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04003313
James Smart858c9f62007-06-17 19:56:39 -05003314 spin_lock_irq(shost->host_lock);
3315
James Smart51ef4c22007-08-02 11:10:31 -04003316 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05003317 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003318 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05003319 }
James Smart256ec0d2013-04-17 20:14:58 -04003320 if (time >= msecs_to_jiffies(30 * 1000)) {
James Smart2e0fef82007-06-17 19:56:36 -05003321 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003322 "0461 Scanning longer than 30 "
3323 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003324 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003325 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003326 }
James Smart256ec0d2013-04-17 20:14:58 -04003327 if (time >= msecs_to_jiffies(15 * 1000) &&
3328 phba->link_state <= LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -05003329 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003330 "0465 Link down longer than 15 "
3331 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003332 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05003333 goto finished;
James Smart47a86172007-04-25 09:53:22 -04003334 }
3335
James Smart2e0fef82007-06-17 19:56:36 -05003336 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05003337 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003338 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05003339 goto finished;
James Smart256ec0d2013-04-17 20:14:58 -04003340 if (vport->fc_map_cnt == 0 && time < msecs_to_jiffies(2 * 1000))
James Smart858c9f62007-06-17 19:56:39 -05003341 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003342 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05003343 goto finished;
3344
3345 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003346
3347finished:
James Smart858c9f62007-06-17 19:56:39 -05003348 spin_unlock_irq(shost->host_lock);
3349 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05003350}
3351
James Smarte59058c2008-08-24 21:49:00 -04003352/**
James Smart3621a712009-04-06 18:47:14 -04003353 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04003354 * @shost: pointer to SCSI host data structure.
3355 *
3356 * This routine initializes a given SCSI host attributes on a FC port. The
3357 * SCSI host can be either on top of a physical port or a virtual port.
3358 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003359void lpfc_host_attrib_init(struct Scsi_Host *shost)
3360{
3361 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3362 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003363 /*
James Smart2e0fef82007-06-17 19:56:36 -05003364 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04003365 */
3366
James Smart2e0fef82007-06-17 19:56:36 -05003367 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
3368 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04003369 fc_host_supported_classes(shost) = FC_COS_CLASS3;
3370
3371 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003372 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003373 fc_host_supported_fc4s(shost)[2] = 1;
3374 fc_host_supported_fc4s(shost)[7] = 1;
3375
James Smart92d7f7b2007-06-17 19:56:38 -05003376 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
3377 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04003378
3379 fc_host_supported_speeds(shost) = 0;
James Smart88a2cfb2011-07-22 18:36:33 -04003380 if (phba->lmt & LMT_16Gb)
3381 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
James Smart47a86172007-04-25 09:53:22 -04003382 if (phba->lmt & LMT_10Gb)
3383 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04003384 if (phba->lmt & LMT_8Gb)
3385 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04003386 if (phba->lmt & LMT_4Gb)
3387 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
3388 if (phba->lmt & LMT_2Gb)
3389 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
3390 if (phba->lmt & LMT_1Gb)
3391 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
3392
3393 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05003394 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
3395 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04003396
Mike Christie0af5d702010-09-15 16:52:31 -05003397 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
3398
James Smart47a86172007-04-25 09:53:22 -04003399 /* This value is also unchanging */
3400 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003401 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003402 fc_host_active_fc4s(shost)[2] = 1;
3403 fc_host_active_fc4s(shost)[7] = 1;
3404
James Smart92d7f7b2007-06-17 19:56:38 -05003405 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04003406 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04003407 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04003408 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04003409}
dea31012005-04-17 16:05:31 -05003410
James Smarte59058c2008-08-24 21:49:00 -04003411/**
James Smartda0436e2009-05-22 14:51:39 -04003412 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04003413 * @phba: pointer to lpfc hba data structure.
3414 *
James Smartda0436e2009-05-22 14:51:39 -04003415 * This routine is invoked to stop an SLI3 device port, it stops the device
3416 * from generating interrupts and stops the device driver's timers for the
3417 * device.
James Smarte59058c2008-08-24 21:49:00 -04003418 **/
James Smartdb2378e2008-02-08 18:49:51 -05003419static void
James Smartda0436e2009-05-22 14:51:39 -04003420lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05003421{
James Smartda0436e2009-05-22 14:51:39 -04003422 /* Clear all interrupt enable conditions */
3423 writel(0, phba->HCregaddr);
3424 readl(phba->HCregaddr); /* flush */
3425 /* Clear all pending interrupts */
3426 writel(0xffffffff, phba->HAregaddr);
3427 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04003428
James Smartda0436e2009-05-22 14:51:39 -04003429 /* Reset some HBA SLI setup states */
3430 lpfc_stop_hba_timers(phba);
3431 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05003432}
3433
James Smarte59058c2008-08-24 21:49:00 -04003434/**
James Smartda0436e2009-05-22 14:51:39 -04003435 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05003436 * @phba: pointer to lpfc hba data structure.
3437 *
James Smartda0436e2009-05-22 14:51:39 -04003438 * This routine is invoked to stop an SLI4 device port, it stops the device
3439 * from generating interrupts and stops the device driver's timers for the
3440 * device.
3441 **/
3442static void
3443lpfc_stop_port_s4(struct lpfc_hba *phba)
3444{
3445 /* Reset some HBA SLI4 setup states */
3446 lpfc_stop_hba_timers(phba);
3447 phba->pport->work_port_events = 0;
3448 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04003449}
3450
3451/**
3452 * lpfc_stop_port - Wrapper function for stopping hba port
3453 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05003454 *
James Smartda0436e2009-05-22 14:51:39 -04003455 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
3456 * the API jump table function pointer from the lpfc_hba struct.
3457 **/
3458void
3459lpfc_stop_port(struct lpfc_hba *phba)
3460{
3461 phba->lpfc_stop_port(phba);
3462}
3463
3464/**
James Smartecfd03c2010-02-12 14:41:27 -05003465 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
3466 * @phba: Pointer to hba for which this call is being executed.
3467 *
3468 * This routine starts the timer waiting for the FCF rediscovery to complete.
3469 **/
3470void
3471lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
3472{
3473 unsigned long fcf_redisc_wait_tmo =
3474 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
3475 /* Start fcf rediscovery wait period timer */
3476 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
3477 spin_lock_irq(&phba->hbalock);
3478 /* Allow action to new fcf asynchronous event */
3479 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
3480 /* Mark the FCF rediscovery pending state */
3481 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
3482 spin_unlock_irq(&phba->hbalock);
3483}
3484
3485/**
3486 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
3487 * @ptr: Map to lpfc_hba data structure pointer.
3488 *
3489 * This routine is invoked when waiting for FCF table rediscover has been
3490 * timed out. If new FCF record(s) has (have) been discovered during the
3491 * wait period, a new FCF event shall be added to the FCOE async event
3492 * list, and then worker thread shall be waked up for processing from the
3493 * worker thread context.
3494 **/
3495void
3496lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
3497{
3498 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
3499
3500 /* Don't send FCF rediscovery event if timer cancelled */
3501 spin_lock_irq(&phba->hbalock);
3502 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
3503 spin_unlock_irq(&phba->hbalock);
3504 return;
3505 }
3506 /* Clear FCF rediscovery timer pending flag */
3507 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
3508 /* FCF rediscovery event to worker thread */
3509 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
3510 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003511 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04003512 "2776 FCF rediscover quiescent timer expired\n");
James Smartecfd03c2010-02-12 14:41:27 -05003513 /* wake up worker thread */
3514 lpfc_worker_wake_up(phba);
3515}
3516
3517/**
James Smartda0436e2009-05-22 14:51:39 -04003518 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
3519 * @phba: pointer to lpfc hba data structure.
3520 * @acqe_link: pointer to the async link completion queue entry.
3521 *
3522 * This routine is to parse the SLI4 link-attention link fault code and
3523 * translate it into the base driver's read link attention mailbox command
3524 * status.
3525 *
3526 * Return: Link-attention status in terms of base driver's coding.
3527 **/
3528static uint16_t
3529lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
3530 struct lpfc_acqe_link *acqe_link)
3531{
3532 uint16_t latt_fault;
3533
3534 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
3535 case LPFC_ASYNC_LINK_FAULT_NONE:
3536 case LPFC_ASYNC_LINK_FAULT_LOCAL:
3537 case LPFC_ASYNC_LINK_FAULT_REMOTE:
3538 latt_fault = 0;
3539 break;
3540 default:
3541 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3542 "0398 Invalid link fault code: x%x\n",
3543 bf_get(lpfc_acqe_link_fault, acqe_link));
3544 latt_fault = MBXERR_ERROR;
3545 break;
3546 }
3547 return latt_fault;
3548}
3549
3550/**
3551 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
3552 * @phba: pointer to lpfc hba data structure.
3553 * @acqe_link: pointer to the async link completion queue entry.
3554 *
3555 * This routine is to parse the SLI4 link attention type and translate it
3556 * into the base driver's link attention type coding.
3557 *
3558 * Return: Link attention type in terms of base driver's coding.
3559 **/
3560static uint8_t
3561lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
3562 struct lpfc_acqe_link *acqe_link)
3563{
3564 uint8_t att_type;
3565
3566 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
3567 case LPFC_ASYNC_LINK_STATUS_DOWN:
3568 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
James Smart76a95d72010-11-20 23:11:48 -05003569 att_type = LPFC_ATT_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04003570 break;
3571 case LPFC_ASYNC_LINK_STATUS_UP:
3572 /* Ignore physical link up events - wait for logical link up */
James Smart76a95d72010-11-20 23:11:48 -05003573 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003574 break;
3575 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
James Smart76a95d72010-11-20 23:11:48 -05003576 att_type = LPFC_ATT_LINK_UP;
James Smartda0436e2009-05-22 14:51:39 -04003577 break;
3578 default:
3579 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3580 "0399 Invalid link attention type: x%x\n",
3581 bf_get(lpfc_acqe_link_status, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003582 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003583 break;
3584 }
3585 return att_type;
3586}
3587
3588/**
3589 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
3590 * @phba: pointer to lpfc hba data structure.
3591 * @acqe_link: pointer to the async link completion queue entry.
3592 *
3593 * This routine is to parse the SLI4 link-attention link speed and translate
3594 * it into the base driver's link-attention link speed coding.
3595 *
3596 * Return: Link-attention link speed in terms of base driver's coding.
3597 **/
3598static uint8_t
3599lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
3600 struct lpfc_acqe_link *acqe_link)
3601{
3602 uint8_t link_speed;
3603
3604 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
3605 case LPFC_ASYNC_LINK_SPEED_ZERO:
James Smartda0436e2009-05-22 14:51:39 -04003606 case LPFC_ASYNC_LINK_SPEED_10MBPS:
James Smartda0436e2009-05-22 14:51:39 -04003607 case LPFC_ASYNC_LINK_SPEED_100MBPS:
James Smart76a95d72010-11-20 23:11:48 -05003608 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003609 break;
3610 case LPFC_ASYNC_LINK_SPEED_1GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003611 link_speed = LPFC_LINK_SPEED_1GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003612 break;
3613 case LPFC_ASYNC_LINK_SPEED_10GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003614 link_speed = LPFC_LINK_SPEED_10GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003615 break;
3616 default:
3617 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3618 "0483 Invalid link-attention link speed: x%x\n",
3619 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003620 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003621 break;
3622 }
3623 return link_speed;
3624}
3625
3626/**
James Smart8b68cd52012-09-29 11:32:37 -04003627 * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
3628 * @phba: pointer to lpfc hba data structure.
3629 *
3630 * This routine is to get an SLI3 FC port's link speed in Mbps.
3631 *
3632 * Return: link speed in terms of Mbps.
3633 **/
3634uint32_t
3635lpfc_sli_port_speed_get(struct lpfc_hba *phba)
3636{
3637 uint32_t link_speed;
3638
3639 if (!lpfc_is_link_up(phba))
3640 return 0;
3641
3642 switch (phba->fc_linkspeed) {
3643 case LPFC_LINK_SPEED_1GHZ:
3644 link_speed = 1000;
3645 break;
3646 case LPFC_LINK_SPEED_2GHZ:
3647 link_speed = 2000;
3648 break;
3649 case LPFC_LINK_SPEED_4GHZ:
3650 link_speed = 4000;
3651 break;
3652 case LPFC_LINK_SPEED_8GHZ:
3653 link_speed = 8000;
3654 break;
3655 case LPFC_LINK_SPEED_10GHZ:
3656 link_speed = 10000;
3657 break;
3658 case LPFC_LINK_SPEED_16GHZ:
3659 link_speed = 16000;
3660 break;
3661 default:
3662 link_speed = 0;
3663 }
3664 return link_speed;
3665}
3666
3667/**
3668 * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
3669 * @phba: pointer to lpfc hba data structure.
3670 * @evt_code: asynchronous event code.
3671 * @speed_code: asynchronous event link speed code.
3672 *
3673 * This routine is to parse the giving SLI4 async event link speed code into
3674 * value of Mbps for the link speed.
3675 *
3676 * Return: link speed in terms of Mbps.
3677 **/
3678static uint32_t
3679lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
3680 uint8_t speed_code)
3681{
3682 uint32_t port_speed;
3683
3684 switch (evt_code) {
3685 case LPFC_TRAILER_CODE_LINK:
3686 switch (speed_code) {
3687 case LPFC_EVT_CODE_LINK_NO_LINK:
3688 port_speed = 0;
3689 break;
3690 case LPFC_EVT_CODE_LINK_10_MBIT:
3691 port_speed = 10;
3692 break;
3693 case LPFC_EVT_CODE_LINK_100_MBIT:
3694 port_speed = 100;
3695 break;
3696 case LPFC_EVT_CODE_LINK_1_GBIT:
3697 port_speed = 1000;
3698 break;
3699 case LPFC_EVT_CODE_LINK_10_GBIT:
3700 port_speed = 10000;
3701 break;
3702 default:
3703 port_speed = 0;
3704 }
3705 break;
3706 case LPFC_TRAILER_CODE_FC:
3707 switch (speed_code) {
3708 case LPFC_EVT_CODE_FC_NO_LINK:
3709 port_speed = 0;
3710 break;
3711 case LPFC_EVT_CODE_FC_1_GBAUD:
3712 port_speed = 1000;
3713 break;
3714 case LPFC_EVT_CODE_FC_2_GBAUD:
3715 port_speed = 2000;
3716 break;
3717 case LPFC_EVT_CODE_FC_4_GBAUD:
3718 port_speed = 4000;
3719 break;
3720 case LPFC_EVT_CODE_FC_8_GBAUD:
3721 port_speed = 8000;
3722 break;
3723 case LPFC_EVT_CODE_FC_10_GBAUD:
3724 port_speed = 10000;
3725 break;
3726 case LPFC_EVT_CODE_FC_16_GBAUD:
3727 port_speed = 16000;
3728 break;
3729 default:
3730 port_speed = 0;
3731 }
3732 break;
3733 default:
3734 port_speed = 0;
3735 }
3736 return port_speed;
3737}
3738
3739/**
James Smart70f3c072010-12-15 17:57:33 -05003740 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
James Smartda0436e2009-05-22 14:51:39 -04003741 * @phba: pointer to lpfc hba data structure.
3742 * @acqe_link: pointer to the async link completion queue entry.
3743 *
James Smart70f3c072010-12-15 17:57:33 -05003744 * This routine is to handle the SLI4 asynchronous FCoE link event.
James Smartda0436e2009-05-22 14:51:39 -04003745 **/
3746static void
3747lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3748 struct lpfc_acqe_link *acqe_link)
3749{
3750 struct lpfc_dmabuf *mp;
3751 LPFC_MBOXQ_t *pmb;
3752 MAILBOX_t *mb;
James Smart76a95d72010-11-20 23:11:48 -05003753 struct lpfc_mbx_read_top *la;
James Smartda0436e2009-05-22 14:51:39 -04003754 uint8_t att_type;
James Smart76a95d72010-11-20 23:11:48 -05003755 int rc;
James Smartda0436e2009-05-22 14:51:39 -04003756
3757 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05003758 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
James Smartda0436e2009-05-22 14:51:39 -04003759 return;
James Smart32b97932009-07-19 10:01:21 -04003760 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003761 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3762 if (!pmb) {
3763 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3764 "0395 The mboxq allocation failed\n");
3765 return;
3766 }
3767 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3768 if (!mp) {
3769 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3770 "0396 The lpfc_dmabuf allocation failed\n");
3771 goto out_free_pmb;
3772 }
3773 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3774 if (!mp->virt) {
3775 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3776 "0397 The mbuf allocation failed\n");
3777 goto out_free_dmabuf;
3778 }
3779
3780 /* Cleanup any outstanding ELS commands */
3781 lpfc_els_flush_all_cmd(phba);
3782
3783 /* Block ELS IOCBs until we have done process link event */
3784 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3785
3786 /* Update link event statistics */
3787 phba->sli.slistat.link_event++;
3788
James Smart76a95d72010-11-20 23:11:48 -05003789 /* Create lpfc_handle_latt mailbox command from link ACQE */
3790 lpfc_read_topology(phba, pmb, mp);
3791 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smartda0436e2009-05-22 14:51:39 -04003792 pmb->vport = phba->pport;
3793
James Smartda0436e2009-05-22 14:51:39 -04003794 /* Keep the link status for extra SLI4 state machine reference */
3795 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04003796 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
3797 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smartda0436e2009-05-22 14:51:39 -04003798 phba->sli4_hba.link_state.duplex =
3799 bf_get(lpfc_acqe_link_duplex, acqe_link);
3800 phba->sli4_hba.link_state.status =
3801 bf_get(lpfc_acqe_link_status, acqe_link);
James Smart70f3c072010-12-15 17:57:33 -05003802 phba->sli4_hba.link_state.type =
3803 bf_get(lpfc_acqe_link_type, acqe_link);
3804 phba->sli4_hba.link_state.number =
3805 bf_get(lpfc_acqe_link_number, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04003806 phba->sli4_hba.link_state.fault =
3807 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05003808 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04003809 bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
3810
James Smart70f3c072010-12-15 17:57:33 -05003811 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartc31098c2011-04-16 11:03:33 -04003812 "2900 Async FC/FCoE Link event - Speed:%dGBit "
3813 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
3814 "Logical speed:%dMbps Fault:%d\n",
James Smart70f3c072010-12-15 17:57:33 -05003815 phba->sli4_hba.link_state.speed,
3816 phba->sli4_hba.link_state.topology,
3817 phba->sli4_hba.link_state.status,
3818 phba->sli4_hba.link_state.type,
3819 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04003820 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05003821 phba->sli4_hba.link_state.fault);
James Smart76a95d72010-11-20 23:11:48 -05003822 /*
3823 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
3824 * topology info. Note: Optional for non FC-AL ports.
3825 */
3826 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3827 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3828 if (rc == MBX_NOT_FINISHED)
3829 goto out_free_dmabuf;
3830 return;
3831 }
3832 /*
3833 * For FCoE Mode: fill in all the topology information we need and call
3834 * the READ_TOPOLOGY completion routine to continue without actually
3835 * sending the READ_TOPOLOGY mailbox command to the port.
3836 */
3837 /* Parse and translate status field */
3838 mb = &pmb->u.mb;
3839 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
3840
3841 /* Parse and translate link attention fields */
3842 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
3843 la->eventTag = acqe_link->event_tag;
3844 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
3845 bf_set(lpfc_mbx_read_top_link_spd, la,
3846 lpfc_sli4_parse_latt_link_speed(phba, acqe_link));
3847
3848 /* Fake the the following irrelvant fields */
3849 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
3850 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
3851 bf_set(lpfc_mbx_read_top_il, la, 0);
3852 bf_set(lpfc_mbx_read_top_pb, la, 0);
3853 bf_set(lpfc_mbx_read_top_fa, la, 0);
3854 bf_set(lpfc_mbx_read_top_mm, la, 0);
James Smartda0436e2009-05-22 14:51:39 -04003855
3856 /* Invoke the lpfc_handle_latt mailbox command callback function */
James Smart76a95d72010-11-20 23:11:48 -05003857 lpfc_mbx_cmpl_read_topology(phba, pmb);
James Smartda0436e2009-05-22 14:51:39 -04003858
3859 return;
3860
3861out_free_dmabuf:
3862 kfree(mp);
3863out_free_pmb:
3864 mempool_free(pmb, phba->mbox_mem_pool);
3865}
3866
3867/**
James Smart70f3c072010-12-15 17:57:33 -05003868 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
3869 * @phba: pointer to lpfc hba data structure.
3870 * @acqe_fc: pointer to the async fc completion queue entry.
3871 *
3872 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
3873 * that the event was received and then issue a read_topology mailbox command so
3874 * that the rest of the driver will treat it the same as SLI3.
3875 **/
3876static void
3877lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
3878{
3879 struct lpfc_dmabuf *mp;
3880 LPFC_MBOXQ_t *pmb;
3881 int rc;
3882
3883 if (bf_get(lpfc_trailer_type, acqe_fc) !=
3884 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
3885 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3886 "2895 Non FC link Event detected.(%d)\n",
3887 bf_get(lpfc_trailer_type, acqe_fc));
3888 return;
3889 }
3890 /* Keep the link status for extra SLI4 state machine reference */
3891 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04003892 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
3893 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
James Smart70f3c072010-12-15 17:57:33 -05003894 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
3895 phba->sli4_hba.link_state.topology =
3896 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
3897 phba->sli4_hba.link_state.status =
3898 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
3899 phba->sli4_hba.link_state.type =
3900 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
3901 phba->sli4_hba.link_state.number =
3902 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
3903 phba->sli4_hba.link_state.fault =
3904 bf_get(lpfc_acqe_link_fault, acqe_fc);
3905 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04003906 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
James Smart70f3c072010-12-15 17:57:33 -05003907 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3908 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
3909 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
3910 "%dMbps Fault:%d\n",
3911 phba->sli4_hba.link_state.speed,
3912 phba->sli4_hba.link_state.topology,
3913 phba->sli4_hba.link_state.status,
3914 phba->sli4_hba.link_state.type,
3915 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04003916 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05003917 phba->sli4_hba.link_state.fault);
3918 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3919 if (!pmb) {
3920 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3921 "2897 The mboxq allocation failed\n");
3922 return;
3923 }
3924 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3925 if (!mp) {
3926 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3927 "2898 The lpfc_dmabuf allocation failed\n");
3928 goto out_free_pmb;
3929 }
3930 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3931 if (!mp->virt) {
3932 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3933 "2899 The mbuf allocation failed\n");
3934 goto out_free_dmabuf;
3935 }
3936
3937 /* Cleanup any outstanding ELS commands */
3938 lpfc_els_flush_all_cmd(phba);
3939
3940 /* Block ELS IOCBs until we have done process link event */
3941 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3942
3943 /* Update link event statistics */
3944 phba->sli.slistat.link_event++;
3945
3946 /* Create lpfc_handle_latt mailbox command from link ACQE */
3947 lpfc_read_topology(phba, pmb, mp);
3948 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
3949 pmb->vport = phba->pport;
3950
3951 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3952 if (rc == MBX_NOT_FINISHED)
3953 goto out_free_dmabuf;
3954 return;
3955
3956out_free_dmabuf:
3957 kfree(mp);
3958out_free_pmb:
3959 mempool_free(pmb, phba->mbox_mem_pool);
3960}
3961
3962/**
3963 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
3964 * @phba: pointer to lpfc hba data structure.
3965 * @acqe_fc: pointer to the async SLI completion queue entry.
3966 *
3967 * This routine is to handle the SLI4 asynchronous SLI events.
3968 **/
3969static void
3970lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
3971{
James Smart4b8bae02012-06-12 13:55:07 -04003972 char port_name;
James Smart8c1312e2012-10-31 14:45:09 -04003973 char message[128];
James Smart4b8bae02012-06-12 13:55:07 -04003974 uint8_t status;
3975 struct lpfc_acqe_misconfigured_event *misconfigured;
3976
3977 /* special case misconfigured event as it contains data for all ports */
3978 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3979 LPFC_SLI_INTF_IF_TYPE_2) ||
3980 (bf_get(lpfc_trailer_type, acqe_sli) !=
3981 LPFC_SLI_EVENT_TYPE_MISCONFIGURED)) {
3982 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3983 "2901 Async SLI event - Event Data1:x%08x Event Data2:"
3984 "x%08x SLI Event Type:%d\n",
3985 acqe_sli->event_data1, acqe_sli->event_data2,
3986 bf_get(lpfc_trailer_type, acqe_sli));
3987 return;
3988 }
3989
3990 port_name = phba->Port[0];
3991 if (port_name == 0x00)
3992 port_name = '?'; /* get port name is empty */
3993
3994 misconfigured = (struct lpfc_acqe_misconfigured_event *)
3995 &acqe_sli->event_data1;
3996
3997 /* fetch the status for this port */
3998 switch (phba->sli4_hba.lnk_info.lnk_no) {
3999 case LPFC_LINK_NUMBER_0:
4000 status = bf_get(lpfc_sli_misconfigured_port0,
4001 &misconfigured->theEvent);
4002 break;
4003 case LPFC_LINK_NUMBER_1:
4004 status = bf_get(lpfc_sli_misconfigured_port1,
4005 &misconfigured->theEvent);
4006 break;
4007 case LPFC_LINK_NUMBER_2:
4008 status = bf_get(lpfc_sli_misconfigured_port2,
4009 &misconfigured->theEvent);
4010 break;
4011 case LPFC_LINK_NUMBER_3:
4012 status = bf_get(lpfc_sli_misconfigured_port3,
4013 &misconfigured->theEvent);
4014 break;
4015 default:
4016 status = ~LPFC_SLI_EVENT_STATUS_VALID;
4017 break;
4018 }
4019
4020 switch (status) {
4021 case LPFC_SLI_EVENT_STATUS_VALID:
4022 return; /* no message if the sfp is okay */
4023 case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
James Smart292098b2012-09-29 11:31:41 -04004024 sprintf(message, "Optics faulted/incorrectly installed/not " \
4025 "installed - Reseat optics, if issue not "
4026 "resolved, replace.");
James Smart4b8bae02012-06-12 13:55:07 -04004027 break;
4028 case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
4029 sprintf(message,
James Smart292098b2012-09-29 11:31:41 -04004030 "Optics of two types installed - Remove one optic or " \
4031 "install matching pair of optics.");
James Smart4b8bae02012-06-12 13:55:07 -04004032 break;
4033 case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
James Smart292098b2012-09-29 11:31:41 -04004034 sprintf(message, "Incompatible optics - Replace with " \
4035 "compatible optics for card to function.");
James Smart4b8bae02012-06-12 13:55:07 -04004036 break;
4037 default:
4038 /* firmware is reporting a status we don't know about */
4039 sprintf(message, "Unknown event status x%02x", status);
4040 break;
4041 }
4042
4043 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4044 "3176 Misconfigured Physical Port - "
4045 "Port Name %c %s\n", port_name, message);
James Smart70f3c072010-12-15 17:57:33 -05004046}
4047
4048/**
James Smartfc2b9892010-02-26 14:15:29 -05004049 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
4050 * @vport: pointer to vport data structure.
4051 *
4052 * This routine is to perform Clear Virtual Link (CVL) on a vport in
4053 * response to a CVL event.
4054 *
4055 * Return the pointer to the ndlp with the vport if successful, otherwise
4056 * return NULL.
4057 **/
4058static struct lpfc_nodelist *
4059lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
4060{
4061 struct lpfc_nodelist *ndlp;
4062 struct Scsi_Host *shost;
4063 struct lpfc_hba *phba;
4064
4065 if (!vport)
4066 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05004067 phba = vport->phba;
4068 if (!phba)
4069 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04004070 ndlp = lpfc_findnode_did(vport, Fabric_DID);
4071 if (!ndlp) {
4072 /* Cannot find existing Fabric ndlp, so allocate a new one */
4073 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
4074 if (!ndlp)
4075 return 0;
4076 lpfc_nlp_init(vport, ndlp, Fabric_DID);
4077 /* Set the node type */
4078 ndlp->nlp_type |= NLP_FABRIC;
4079 /* Put ndlp onto node list */
4080 lpfc_enqueue_node(vport, ndlp);
4081 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4082 /* re-setup ndlp without removing from node list */
4083 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
4084 if (!ndlp)
4085 return 0;
4086 }
James Smart63e801c2010-11-20 23:14:19 -05004087 if ((phba->pport->port_state < LPFC_FLOGI) &&
4088 (phba->pport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05004089 return NULL;
4090 /* If virtual link is not yet instantiated ignore CVL */
James Smart63e801c2010-11-20 23:14:19 -05004091 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
4092 && (vport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05004093 return NULL;
4094 shost = lpfc_shost_from_vport(vport);
4095 if (!shost)
4096 return NULL;
4097 lpfc_linkdown_port(vport);
4098 lpfc_cleanup_pending_mbox(vport);
4099 spin_lock_irq(shost->host_lock);
4100 vport->fc_flag |= FC_VPORT_CVL_RCVD;
4101 spin_unlock_irq(shost->host_lock);
4102
4103 return ndlp;
4104}
4105
4106/**
4107 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
4108 * @vport: pointer to lpfc hba data structure.
4109 *
4110 * This routine is to perform Clear Virtual Link (CVL) on all vports in
4111 * response to a FCF dead event.
4112 **/
4113static void
4114lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
4115{
4116 struct lpfc_vport **vports;
4117 int i;
4118
4119 vports = lpfc_create_vport_work_array(phba);
4120 if (vports)
4121 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
4122 lpfc_sli4_perform_vport_cvl(vports[i]);
4123 lpfc_destroy_vport_work_array(phba, vports);
4124}
4125
4126/**
James Smart76a95d72010-11-20 23:11:48 -05004127 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
James Smartda0436e2009-05-22 14:51:39 -04004128 * @phba: pointer to lpfc hba data structure.
4129 * @acqe_link: pointer to the async fcoe completion queue entry.
4130 *
4131 * This routine is to handle the SLI4 asynchronous fcoe event.
4132 **/
4133static void
James Smart76a95d72010-11-20 23:11:48 -05004134lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
James Smart70f3c072010-12-15 17:57:33 -05004135 struct lpfc_acqe_fip *acqe_fip)
James Smartda0436e2009-05-22 14:51:39 -04004136{
James Smart70f3c072010-12-15 17:57:33 -05004137 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004138 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04004139 struct lpfc_vport *vport;
4140 struct lpfc_nodelist *ndlp;
4141 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05004142 int active_vlink_present;
4143 struct lpfc_vport **vports;
4144 int i;
James Smartda0436e2009-05-22 14:51:39 -04004145
James Smart70f3c072010-12-15 17:57:33 -05004146 phba->fc_eventTag = acqe_fip->event_tag;
4147 phba->fcoe_eventtag = acqe_fip->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004148 switch (event_type) {
James Smart70f3c072010-12-15 17:57:33 -05004149 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
4150 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
4151 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
James Smart999d8132010-03-15 11:24:56 -04004152 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4153 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004154 "2546 New FCF event, evt_tag:x%x, "
4155 "index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004156 acqe_fip->event_tag,
4157 acqe_fip->index);
James Smart999d8132010-03-15 11:24:56 -04004158 else
4159 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
4160 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004161 "2788 FCF param modified event, "
4162 "evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004163 acqe_fip->event_tag,
4164 acqe_fip->index);
James Smart38b92ef2010-08-04 16:11:39 -04004165 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004166 /*
4167 * During period of FCF discovery, read the FCF
4168 * table record indexed by the event to update
James Smarta93ff372010-10-22 11:06:08 -04004169 * FCF roundrobin failover eligible FCF bmask.
James Smart0c9ab6f2010-02-26 14:15:57 -05004170 */
4171 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4172 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004173 "2779 Read FCF (x%x) for updating "
4174 "roundrobin FCF failover bmask\n",
James Smart70f3c072010-12-15 17:57:33 -05004175 acqe_fip->index);
4176 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004177 }
James Smart38b92ef2010-08-04 16:11:39 -04004178
4179 /* If the FCF discovery is in progress, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04004180 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04004181 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart38b92ef2010-08-04 16:11:39 -04004182 spin_unlock_irq(&phba->hbalock);
4183 break;
4184 }
4185 /* If fast FCF failover rescan event is pending, do nothing */
4186 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
4187 spin_unlock_irq(&phba->hbalock);
4188 break;
4189 }
4190
James Smartc2b97122013-05-31 17:05:36 -04004191 /* If the FCF has been in discovered state, do nothing. */
4192 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
James Smart3804dc82010-07-14 15:31:37 -04004193 spin_unlock_irq(&phba->hbalock);
4194 break;
4195 }
4196 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04004197
James Smart0c9ab6f2010-02-26 14:15:57 -05004198 /* Otherwise, scan the entire FCF table and re-discover SAN */
4199 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004200 "2770 Start FCF table scan per async FCF "
4201 "event, evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004202 acqe_fip->event_tag, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004203 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
4204 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04004205 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05004206 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4207 "2547 Issue FCF scan read FCF mailbox "
James Smarta93ff372010-10-22 11:06:08 -04004208 "command failed (x%x)\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004209 break;
4210
James Smart70f3c072010-12-15 17:57:33 -05004211 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
James Smartda0436e2009-05-22 14:51:39 -04004212 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04004213 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004214 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
4215 acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04004216 break;
4217
James Smart70f3c072010-12-15 17:57:33 -05004218 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
James Smart80c17842012-03-01 22:35:45 -05004219 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05004220 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004221 "2549 FCF (x%x) disconnected from network, "
James Smart70f3c072010-12-15 17:57:33 -05004222 "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart38b92ef2010-08-04 16:11:39 -04004223 /*
4224 * If we are in the middle of FCF failover process, clear
4225 * the corresponding FCF bit in the roundrobin bitmap.
James Smartda0436e2009-05-22 14:51:39 -04004226 */
James Smartfc2b9892010-02-26 14:15:29 -05004227 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004228 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05004229 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004230 /* Update FLOGI FCF failover eligible FCF bmask */
James Smart70f3c072010-12-15 17:57:33 -05004231 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004232 break;
4233 }
James Smart38b92ef2010-08-04 16:11:39 -04004234 spin_unlock_irq(&phba->hbalock);
4235
4236 /* If the event is not for currently used fcf do nothing */
James Smart70f3c072010-12-15 17:57:33 -05004237 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
James Smart38b92ef2010-08-04 16:11:39 -04004238 break;
4239
4240 /*
4241 * Otherwise, request the port to rediscover the entire FCF
4242 * table for a fast recovery from case that the current FCF
4243 * is no longer valid as we are not in the middle of FCF
4244 * failover process already.
4245 */
James Smartc2b97122013-05-31 17:05:36 -04004246 spin_lock_irq(&phba->hbalock);
4247 /* Mark the fast failover process in progress */
4248 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
4249 spin_unlock_irq(&phba->hbalock);
4250
4251 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
4252 "2771 Start FCF fast failover process due to "
4253 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
4254 "\n", acqe_fip->event_tag, acqe_fip->index);
4255 rc = lpfc_sli4_redisc_fcf_table(phba);
4256 if (rc) {
4257 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4258 LOG_DISCOVERY,
4259 "2772 Issue FCF rediscover mabilbox "
4260 "command failed, fail through to FCF "
4261 "dead event\n");
4262 spin_lock_irq(&phba->hbalock);
4263 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
4264 spin_unlock_irq(&phba->hbalock);
4265 /*
4266 * Last resort will fail over by treating this
4267 * as a link down to FCF registration.
4268 */
4269 lpfc_sli4_fcf_dead_failthrough(phba);
4270 } else {
4271 /* Reset FCF roundrobin bmask for new discovery */
4272 lpfc_sli4_clear_fcf_rr_bmask(phba);
4273 /*
4274 * Handling fast FCF failover to a DEAD FCF event is
4275 * considered equalivant to receiving CVL to all vports.
4276 */
4277 lpfc_sli4_perform_all_vport_cvl(phba);
4278 }
James Smartda0436e2009-05-22 14:51:39 -04004279 break;
James Smart70f3c072010-12-15 17:57:33 -05004280 case LPFC_FIP_EVENT_TYPE_CVL:
James Smart80c17842012-03-01 22:35:45 -05004281 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05004282 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6669f9b2009-10-02 15:16:45 -04004283 "2718 Clear Virtual Link Received for VPI 0x%x"
James Smart70f3c072010-12-15 17:57:33 -05004284 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart6d368e52011-05-24 11:44:12 -04004285
James Smart6669f9b2009-10-02 15:16:45 -04004286 vport = lpfc_find_vport_by_vpid(phba,
James Smart5248a742011-07-22 18:37:06 -04004287 acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004288 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004289 if (!ndlp)
4290 break;
James Smart695a8142010-01-26 23:08:03 -05004291 active_vlink_present = 0;
4292
4293 vports = lpfc_create_vport_work_array(phba);
4294 if (vports) {
4295 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
4296 i++) {
4297 if ((!(vports[i]->fc_flag &
4298 FC_VPORT_CVL_RCVD)) &&
4299 (vports[i]->port_state > LPFC_FDISC)) {
4300 active_vlink_present = 1;
4301 break;
4302 }
4303 }
4304 lpfc_destroy_vport_work_array(phba, vports);
4305 }
4306
4307 if (active_vlink_present) {
4308 /*
4309 * If there are other active VLinks present,
4310 * re-instantiate the Vlink using FDISC.
4311 */
James Smart256ec0d2013-04-17 20:14:58 -04004312 mod_timer(&ndlp->nlp_delayfunc,
4313 jiffies + msecs_to_jiffies(1000));
James Smartfc2b9892010-02-26 14:15:29 -05004314 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004315 spin_lock_irq(shost->host_lock);
4316 ndlp->nlp_flag |= NLP_DELAY_TMO;
4317 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05004318 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
4319 vport->port_state = LPFC_FDISC;
4320 } else {
James Smartecfd03c2010-02-12 14:41:27 -05004321 /*
4322 * Otherwise, we request port to rediscover
4323 * the entire FCF table for a fast recovery
4324 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05004325 * is no longer valid if we are not already
4326 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05004327 */
James Smartfc2b9892010-02-26 14:15:29 -05004328 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004329 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05004330 spin_unlock_irq(&phba->hbalock);
4331 break;
4332 }
4333 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05004334 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004335 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004336 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4337 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004338 "2773 Start FCF failover per CVL, "
James Smart70f3c072010-12-15 17:57:33 -05004339 "evt_tag:x%x\n", acqe_fip->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05004340 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05004341 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004342 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4343 LOG_DISCOVERY,
4344 "2774 Issue FCF rediscover "
4345 "mabilbox command failed, "
4346 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05004347 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004348 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004349 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05004350 /*
4351 * Last resort will be re-try on the
4352 * the current registered FCF entry.
4353 */
4354 lpfc_retry_pport_discovery(phba);
James Smart38b92ef2010-08-04 16:11:39 -04004355 } else
4356 /*
4357 * Reset FCF roundrobin bmask for new
4358 * discovery.
4359 */
James Smart7d791df2011-07-22 18:37:52 -04004360 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart6669f9b2009-10-02 15:16:45 -04004361 }
4362 break;
James Smartda0436e2009-05-22 14:51:39 -04004363 default:
4364 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4365 "0288 Unknown FCoE event type 0x%x event tag "
James Smart70f3c072010-12-15 17:57:33 -05004366 "0x%x\n", event_type, acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04004367 break;
4368 }
4369}
4370
4371/**
4372 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
4373 * @phba: pointer to lpfc hba data structure.
4374 * @acqe_link: pointer to the async dcbx completion queue entry.
4375 *
4376 * This routine is to handle the SLI4 asynchronous dcbx event.
4377 **/
4378static void
4379lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
4380 struct lpfc_acqe_dcbx *acqe_dcbx)
4381{
James Smart4d9ab992009-10-02 15:16:39 -04004382 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004383 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4384 "0290 The SLI4 DCBX asynchronous event is not "
4385 "handled yet\n");
4386}
4387
4388/**
James Smartb19a0612010-04-06 14:48:51 -04004389 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
4390 * @phba: pointer to lpfc hba data structure.
4391 * @acqe_link: pointer to the async grp5 completion queue entry.
4392 *
4393 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
4394 * is an asynchronous notified of a logical link speed change. The Port
4395 * reports the logical link speed in units of 10Mbps.
4396 **/
4397static void
4398lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
4399 struct lpfc_acqe_grp5 *acqe_grp5)
4400{
4401 uint16_t prev_ll_spd;
4402
4403 phba->fc_eventTag = acqe_grp5->event_tag;
4404 phba->fcoe_eventtag = acqe_grp5->event_tag;
4405 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
4406 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04004407 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
James Smartb19a0612010-04-06 14:48:51 -04004408 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4409 "2789 GRP5 Async Event: Updating logical link speed "
James Smart8b68cd52012-09-29 11:32:37 -04004410 "from %dMbps to %dMbps\n", prev_ll_spd,
4411 phba->sli4_hba.link_state.logical_speed);
James Smartb19a0612010-04-06 14:48:51 -04004412}
4413
4414/**
James Smartda0436e2009-05-22 14:51:39 -04004415 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
4416 * @phba: pointer to lpfc hba data structure.
4417 *
4418 * This routine is invoked by the worker thread to process all the pending
4419 * SLI4 asynchronous events.
4420 **/
4421void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
4422{
4423 struct lpfc_cq_event *cq_event;
4424
4425 /* First, declare the async event has been handled */
4426 spin_lock_irq(&phba->hbalock);
4427 phba->hba_flag &= ~ASYNC_EVENT;
4428 spin_unlock_irq(&phba->hbalock);
4429 /* Now, handle all the async events */
4430 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
4431 /* Get the first event from the head of the event queue */
4432 spin_lock_irq(&phba->hbalock);
4433 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
4434 cq_event, struct lpfc_cq_event, list);
4435 spin_unlock_irq(&phba->hbalock);
4436 /* Process the asynchronous event */
4437 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
4438 case LPFC_TRAILER_CODE_LINK:
4439 lpfc_sli4_async_link_evt(phba,
4440 &cq_event->cqe.acqe_link);
4441 break;
4442 case LPFC_TRAILER_CODE_FCOE:
James Smart70f3c072010-12-15 17:57:33 -05004443 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004444 break;
4445 case LPFC_TRAILER_CODE_DCBX:
4446 lpfc_sli4_async_dcbx_evt(phba,
4447 &cq_event->cqe.acqe_dcbx);
4448 break;
James Smartb19a0612010-04-06 14:48:51 -04004449 case LPFC_TRAILER_CODE_GRP5:
4450 lpfc_sli4_async_grp5_evt(phba,
4451 &cq_event->cqe.acqe_grp5);
4452 break;
James Smart70f3c072010-12-15 17:57:33 -05004453 case LPFC_TRAILER_CODE_FC:
4454 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
4455 break;
4456 case LPFC_TRAILER_CODE_SLI:
4457 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
4458 break;
James Smartda0436e2009-05-22 14:51:39 -04004459 default:
4460 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4461 "1804 Invalid asynchrous event code: "
4462 "x%x\n", bf_get(lpfc_trailer_code,
4463 &cq_event->cqe.mcqe_cmpl));
4464 break;
4465 }
4466 /* Free the completion event processed to the free pool */
4467 lpfc_sli4_cq_event_release(phba, cq_event);
4468 }
4469}
4470
4471/**
James Smartecfd03c2010-02-12 14:41:27 -05004472 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
4473 * @phba: pointer to lpfc hba data structure.
4474 *
4475 * This routine is invoked by the worker thread to process FCF table
4476 * rediscovery pending completion event.
4477 **/
4478void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
4479{
4480 int rc;
4481
4482 spin_lock_irq(&phba->hbalock);
4483 /* Clear FCF rediscovery timeout event */
4484 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
4485 /* Clear driver fast failover FCF record flag */
4486 phba->fcf.failover_rec.flag = 0;
4487 /* Set state for FCF fast failover */
4488 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
4489 spin_unlock_irq(&phba->hbalock);
4490
4491 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05004492 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004493 "2777 Start post-quiescent FCF table scan\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05004494 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05004495 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05004496 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4497 "2747 Issue FCF scan read FCF mailbox "
4498 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05004499}
4500
4501/**
James Smartda0436e2009-05-22 14:51:39 -04004502 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
4503 * @phba: pointer to lpfc hba data structure.
4504 * @dev_grp: The HBA PCI-Device group number.
4505 *
4506 * This routine is invoked to set up the per HBA PCI-Device group function
4507 * API jump table entries.
4508 *
4509 * Return: 0 if success, otherwise -ENODEV
4510 **/
4511int
4512lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05004513{
4514 int rc;
4515
James Smartda0436e2009-05-22 14:51:39 -04004516 /* Set up lpfc PCI-device group */
4517 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05004518
James Smartda0436e2009-05-22 14:51:39 -04004519 /* The LPFC_PCI_DEV_OC uses SLI4 */
4520 if (dev_grp == LPFC_PCI_DEV_OC)
4521 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05004522
James Smartda0436e2009-05-22 14:51:39 -04004523 /* Set up device INIT API function jump table */
4524 rc = lpfc_init_api_table_setup(phba, dev_grp);
4525 if (rc)
4526 return -ENODEV;
4527 /* Set up SCSI API function jump table */
4528 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
4529 if (rc)
4530 return -ENODEV;
4531 /* Set up SLI API function jump table */
4532 rc = lpfc_sli_api_table_setup(phba, dev_grp);
4533 if (rc)
4534 return -ENODEV;
4535 /* Set up MBOX API function jump table */
4536 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
4537 if (rc)
4538 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004539
James Smartda0436e2009-05-22 14:51:39 -04004540 return 0;
James Smart5b75da22008-12-04 22:39:35 -05004541}
4542
4543/**
James Smart3621a712009-04-06 18:47:14 -04004544 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05004545 * @phba: pointer to lpfc hba data structure.
4546 * @intr_mode: active interrupt mode adopted.
4547 *
4548 * This routine it invoked to log the currently used active interrupt mode
4549 * to the device.
James Smart3772a992009-05-22 14:50:54 -04004550 **/
4551static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05004552{
4553 switch (intr_mode) {
4554 case 0:
4555 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4556 "0470 Enable INTx interrupt mode.\n");
4557 break;
4558 case 1:
4559 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4560 "0481 Enabled MSI interrupt mode.\n");
4561 break;
4562 case 2:
4563 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4564 "0480 Enabled MSI-X interrupt mode.\n");
4565 break;
4566 default:
4567 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4568 "0482 Illegal interrupt mode.\n");
4569 break;
4570 }
4571 return;
4572}
4573
James Smart5b75da22008-12-04 22:39:35 -05004574/**
James Smart3772a992009-05-22 14:50:54 -04004575 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05004576 * @phba: pointer to lpfc hba data structure.
4577 *
James Smart3772a992009-05-22 14:50:54 -04004578 * This routine is invoked to enable the PCI device that is common to all
4579 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004580 *
4581 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004582 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004583 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05004584 **/
James Smart3772a992009-05-22 14:50:54 -04004585static int
4586lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004587{
James Smart3772a992009-05-22 14:50:54 -04004588 struct pci_dev *pdev;
James Smart079b5c92011-08-21 21:48:49 -04004589 int bars = 0;
James Smart5b75da22008-12-04 22:39:35 -05004590
James Smart3772a992009-05-22 14:50:54 -04004591 /* Obtain PCI device reference */
4592 if (!phba->pcidev)
4593 goto out_error;
4594 else
4595 pdev = phba->pcidev;
4596 /* Select PCI BARs */
4597 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4598 /* Enable PCI device */
4599 if (pci_enable_device_mem(pdev))
4600 goto out_error;
4601 /* Request PCI resource for the device */
4602 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
4603 goto out_disable_device;
4604 /* Set up device as PCI master and save state for EEH */
4605 pci_set_master(pdev);
4606 pci_try_set_mwi(pdev);
4607 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05004608
James Smart05580562011-05-24 11:40:48 -04004609 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
Jon Mason453193e2013-09-11 15:38:13 -07004610 if (pci_is_pcie(pdev))
James Smart05580562011-05-24 11:40:48 -04004611 pdev->needs_freset = 1;
4612
James Smart3772a992009-05-22 14:50:54 -04004613 return 0;
James Smart5b75da22008-12-04 22:39:35 -05004614
James Smart3772a992009-05-22 14:50:54 -04004615out_disable_device:
4616 pci_disable_device(pdev);
4617out_error:
James Smart079b5c92011-08-21 21:48:49 -04004618 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4619 "1401 Failed to enable pci device, bars:x%x\n", bars);
James Smart3772a992009-05-22 14:50:54 -04004620 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004621}
4622
4623/**
James Smart3772a992009-05-22 14:50:54 -04004624 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05004625 * @phba: pointer to lpfc hba data structure.
4626 *
James Smart3772a992009-05-22 14:50:54 -04004627 * This routine is invoked to disable the PCI device that is common to all
4628 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004629 **/
4630static void
James Smart3772a992009-05-22 14:50:54 -04004631lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004632{
James Smart3772a992009-05-22 14:50:54 -04004633 struct pci_dev *pdev;
4634 int bars;
James Smart5b75da22008-12-04 22:39:35 -05004635
James Smart3772a992009-05-22 14:50:54 -04004636 /* Obtain PCI device reference */
4637 if (!phba->pcidev)
4638 return;
4639 else
4640 pdev = phba->pcidev;
4641 /* Select PCI BARs */
4642 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4643 /* Release PCI resource and disable PCI device */
4644 pci_release_selected_regions(pdev, bars);
4645 pci_disable_device(pdev);
James Smart5b75da22008-12-04 22:39:35 -05004646
4647 return;
4648}
4649
4650/**
James Smart3772a992009-05-22 14:50:54 -04004651 * lpfc_reset_hba - Reset a hba
4652 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04004653 *
James Smart3772a992009-05-22 14:50:54 -04004654 * This routine is invoked to reset a hba device. It brings the HBA
4655 * offline, performs a board restart, and then brings the board back
4656 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
4657 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04004658 **/
James Smart3772a992009-05-22 14:50:54 -04004659void
4660lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004661{
James Smart3772a992009-05-22 14:50:54 -04004662 /* If resets are disabled then set error state and return. */
4663 if (!phba->cfg_enable_hba_reset) {
4664 phba->link_state = LPFC_HBA_ERROR;
4665 return;
4666 }
James Smartee620212014-04-04 13:51:53 -04004667 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
4668 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
4669 else
4670 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart3772a992009-05-22 14:50:54 -04004671 lpfc_offline(phba);
4672 lpfc_sli_brdrestart(phba);
4673 lpfc_online(phba);
4674 lpfc_unblock_mgmt_io(phba);
4675}
dea31012005-04-17 16:05:31 -05004676
James Smart3772a992009-05-22 14:50:54 -04004677/**
James Smart0a96e972011-07-22 18:37:28 -04004678 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
4679 * @phba: pointer to lpfc hba data structure.
4680 *
4681 * This function enables the PCI SR-IOV virtual functions to a physical
4682 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4683 * enable the number of virtual functions to the physical function. As
4684 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4685 * API call does not considered as an error condition for most of the device.
4686 **/
4687uint16_t
4688lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
4689{
4690 struct pci_dev *pdev = phba->pcidev;
4691 uint16_t nr_virtfn;
4692 int pos;
4693
James Smart0a96e972011-07-22 18:37:28 -04004694 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
4695 if (pos == 0)
4696 return 0;
4697
4698 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
4699 return nr_virtfn;
4700}
4701
4702/**
James Smart912e3ac2011-05-24 11:42:11 -04004703 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
4704 * @phba: pointer to lpfc hba data structure.
4705 * @nr_vfn: number of virtual functions to be enabled.
4706 *
4707 * This function enables the PCI SR-IOV virtual functions to a physical
4708 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4709 * enable the number of virtual functions to the physical function. As
4710 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4711 * API call does not considered as an error condition for most of the device.
4712 **/
4713int
4714lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
4715{
4716 struct pci_dev *pdev = phba->pcidev;
James Smart0a96e972011-07-22 18:37:28 -04004717 uint16_t max_nr_vfn;
James Smart912e3ac2011-05-24 11:42:11 -04004718 int rc;
4719
James Smart0a96e972011-07-22 18:37:28 -04004720 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
4721 if (nr_vfn > max_nr_vfn) {
4722 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4723 "3057 Requested vfs (%d) greater than "
4724 "supported vfs (%d)", nr_vfn, max_nr_vfn);
4725 return -EINVAL;
4726 }
4727
James Smart912e3ac2011-05-24 11:42:11 -04004728 rc = pci_enable_sriov(pdev, nr_vfn);
4729 if (rc) {
4730 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4731 "2806 Failed to enable sriov on this device "
4732 "with vfn number nr_vf:%d, rc:%d\n",
4733 nr_vfn, rc);
4734 } else
4735 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4736 "2807 Successful enable sriov on this device "
4737 "with vfn number nr_vf:%d\n", nr_vfn);
4738 return rc;
4739}
4740
4741/**
James Smart3772a992009-05-22 14:50:54 -04004742 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
4743 * @phba: pointer to lpfc hba data structure.
4744 *
4745 * This routine is invoked to set up the driver internal resources specific to
4746 * support the SLI-3 HBA device it attached to.
4747 *
4748 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004749 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004750 * other values - error
4751 **/
4752static int
4753lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
4754{
4755 struct lpfc_sli *psli;
James Smart912e3ac2011-05-24 11:42:11 -04004756 int rc;
dea31012005-04-17 16:05:31 -05004757
James Smart3772a992009-05-22 14:50:54 -04004758 /*
4759 * Initialize timers used by driver
4760 */
dea31012005-04-17 16:05:31 -05004761
James Smart3772a992009-05-22 14:50:54 -04004762 /* Heartbeat timer */
4763 init_timer(&phba->hb_tmofunc);
4764 phba->hb_tmofunc.function = lpfc_hb_timeout;
4765 phba->hb_tmofunc.data = (unsigned long)phba;
4766
4767 psli = &phba->sli;
4768 /* MBOX heartbeat timer */
4769 init_timer(&psli->mbox_tmo);
4770 psli->mbox_tmo.function = lpfc_mbox_timeout;
4771 psli->mbox_tmo.data = (unsigned long) phba;
4772 /* FCP polling mode timer */
4773 init_timer(&phba->fcp_poll_timer);
4774 phba->fcp_poll_timer.function = lpfc_poll_timeout;
4775 phba->fcp_poll_timer.data = (unsigned long) phba;
4776 /* Fabric block timer */
4777 init_timer(&phba->fabric_block_timer);
4778 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4779 phba->fabric_block_timer.data = (unsigned long) phba;
4780 /* EA polling mode timer */
4781 init_timer(&phba->eratt_poll);
4782 phba->eratt_poll.function = lpfc_poll_eratt;
4783 phba->eratt_poll.data = (unsigned long) phba;
4784
4785 /* Host attention work mask setup */
4786 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
4787 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
4788
4789 /* Get all the module params for configuring this host */
4790 lpfc_get_cfgparam(phba);
James Smart49198b32010-04-06 15:04:33 -04004791 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
4792 phba->menlo_flag |= HBA_MENLO_SUPPORT;
4793 /* check for menlo minimum sg count */
4794 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
4795 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
4796 }
4797
James Smart2a76a282012-08-03 12:35:54 -04004798 if (!phba->sli.ring)
4799 phba->sli.ring = (struct lpfc_sli_ring *)
4800 kzalloc(LPFC_SLI3_MAX_RING *
4801 sizeof(struct lpfc_sli_ring), GFP_KERNEL);
4802 if (!phba->sli.ring)
4803 return -ENOMEM;
4804
James Smart3772a992009-05-22 14:50:54 -04004805 /*
James Smart96f70772013-04-17 20:16:15 -04004806 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smart3772a992009-05-22 14:50:54 -04004807 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart3772a992009-05-22 14:50:54 -04004808 */
James Smart3772a992009-05-22 14:50:54 -04004809
James Smart96f70772013-04-17 20:16:15 -04004810 /* Initialize the host templates the configured values. */
James Smart3772a992009-05-22 14:50:54 -04004811 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4812 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4813
James Smart96f70772013-04-17 20:16:15 -04004814 /* There are going to be 2 reserved BDEs: 1 FCP cmnd + 1 FCP rsp */
4815 if (phba->cfg_enable_bg) {
4816 /*
4817 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
4818 * the FCP rsp, and a BDE for each. Sice we have no control
4819 * over how many protection data segments the SCSI Layer
4820 * will hand us (ie: there could be one for every block
4821 * in the IO), we just allocate enough BDEs to accomidate
4822 * our max amount and we need to limit lpfc_sg_seg_cnt to
4823 * minimize the risk of running out.
4824 */
4825 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4826 sizeof(struct fcp_rsp) +
4827 (LPFC_MAX_SG_SEG_CNT * sizeof(struct ulp_bde64));
4828
4829 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SEG_CNT_DIF)
4830 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT_DIF;
4831
4832 /* Total BDEs in BPL for scsi_sg_list and scsi_sg_prot_list */
4833 phba->cfg_total_seg_cnt = LPFC_MAX_SG_SEG_CNT;
4834 } else {
4835 /*
4836 * The scsi_buf for a regular I/O will hold the FCP cmnd,
4837 * the FCP rsp, a BDE for each, and a BDE for up to
4838 * cfg_sg_seg_cnt data segments.
4839 */
4840 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4841 sizeof(struct fcp_rsp) +
4842 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
4843
4844 /* Total BDEs in BPL for scsi_sg_list */
4845 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
4846 }
4847
4848 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
4849 "9088 sg_tablesize:%d dmabuf_size:%d total_bde:%d\n",
4850 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
4851 phba->cfg_total_seg_cnt);
4852
James Smart3772a992009-05-22 14:50:54 -04004853 phba->max_vpi = LPFC_MAX_VPI;
4854 /* This will be set to correct value after config_port mbox */
4855 phba->max_vports = 0;
4856
4857 /*
4858 * Initialize the SLI Layer to run with lpfc HBAs.
4859 */
4860 lpfc_sli_setup(phba);
4861 lpfc_sli_queue_setup(phba);
4862
4863 /* Allocate device driver memory */
4864 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
4865 return -ENOMEM;
4866
James Smart912e3ac2011-05-24 11:42:11 -04004867 /*
4868 * Enable sr-iov virtual functions if supported and configured
4869 * through the module parameter.
4870 */
4871 if (phba->cfg_sriov_nr_virtfn > 0) {
4872 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
4873 phba->cfg_sriov_nr_virtfn);
4874 if (rc) {
4875 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4876 "2808 Requested number of SR-IOV "
4877 "virtual functions (%d) is not "
4878 "supported\n",
4879 phba->cfg_sriov_nr_virtfn);
4880 phba->cfg_sriov_nr_virtfn = 0;
4881 }
4882 }
4883
James Smart3772a992009-05-22 14:50:54 -04004884 return 0;
4885}
4886
4887/**
4888 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
4889 * @phba: pointer to lpfc hba data structure.
4890 *
4891 * This routine is invoked to unset the driver internal resources set up
4892 * specific for supporting the SLI-3 HBA device it attached to.
4893 **/
4894static void
4895lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
4896{
4897 /* Free device driver memory allocated */
4898 lpfc_mem_free_all(phba);
4899
4900 return;
4901}
4902
4903/**
James Smartda0436e2009-05-22 14:51:39 -04004904 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
4905 * @phba: pointer to lpfc hba data structure.
4906 *
4907 * This routine is invoked to set up the driver internal resources specific to
4908 * support the SLI-4 HBA device it attached to.
4909 *
4910 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004911 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004912 * other values - error
4913 **/
4914static int
4915lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
4916{
James Smart7bb03bb2013-04-17 20:19:16 -04004917 struct lpfc_vector_map_info *cpup;
James Smartda0436e2009-05-22 14:51:39 -04004918 struct lpfc_sli *psli;
James Smart28baac72010-02-12 14:42:03 -05004919 LPFC_MBOXQ_t *mboxq;
James Smart96f70772013-04-17 20:16:15 -04004920 int rc, i, hbq_count, max_buf_size;
James Smart28baac72010-02-12 14:42:03 -05004921 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
4922 struct lpfc_mqe *mqe;
James Smart09294d42013-04-17 20:16:05 -04004923 int longs;
James Smart1ba981f2014-02-20 09:56:45 -05004924 int fof_vectors = 0;
James Smartda0436e2009-05-22 14:51:39 -04004925
James Smart716d3bc2013-09-06 12:18:28 -04004926 /* Get all the module params for configuring this host */
4927 lpfc_get_cfgparam(phba);
4928
James Smartda0436e2009-05-22 14:51:39 -04004929 /* Before proceed, wait for POST done and device ready */
4930 rc = lpfc_sli4_post_status_check(phba);
4931 if (rc)
4932 return -ENODEV;
4933
4934 /*
4935 * Initialize timers used by driver
4936 */
4937
4938 /* Heartbeat timer */
4939 init_timer(&phba->hb_tmofunc);
4940 phba->hb_tmofunc.function = lpfc_hb_timeout;
4941 phba->hb_tmofunc.data = (unsigned long)phba;
James Smart19ca7602010-11-20 23:11:55 -05004942 init_timer(&phba->rrq_tmr);
4943 phba->rrq_tmr.function = lpfc_rrq_timeout;
4944 phba->rrq_tmr.data = (unsigned long)phba;
James Smartda0436e2009-05-22 14:51:39 -04004945
4946 psli = &phba->sli;
4947 /* MBOX heartbeat timer */
4948 init_timer(&psli->mbox_tmo);
4949 psli->mbox_tmo.function = lpfc_mbox_timeout;
4950 psli->mbox_tmo.data = (unsigned long) phba;
4951 /* Fabric block timer */
4952 init_timer(&phba->fabric_block_timer);
4953 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4954 phba->fabric_block_timer.data = (unsigned long) phba;
4955 /* EA polling mode timer */
4956 init_timer(&phba->eratt_poll);
4957 phba->eratt_poll.function = lpfc_poll_eratt;
4958 phba->eratt_poll.data = (unsigned long) phba;
James Smartecfd03c2010-02-12 14:41:27 -05004959 /* FCF rediscover timer */
4960 init_timer(&phba->fcf.redisc_wait);
4961 phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
4962 phba->fcf.redisc_wait.data = (unsigned long)phba;
4963
James Smartda0436e2009-05-22 14:51:39 -04004964 /*
James Smart7ad20aa2011-05-24 11:44:28 -04004965 * Control structure for handling external multi-buffer mailbox
4966 * command pass-through.
4967 */
4968 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
4969 sizeof(struct lpfc_mbox_ext_buf_ctx));
4970 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
4971
James Smartda0436e2009-05-22 14:51:39 -04004972 phba->max_vpi = LPFC_MAX_VPI;
James Smart67d12732012-08-03 12:36:13 -04004973
James Smartda0436e2009-05-22 14:51:39 -04004974 /* This will be set to correct value after the read_config mbox */
4975 phba->max_vports = 0;
4976
4977 /* Program the default value of vlan_id and fc_map */
4978 phba->valid_vlan = 0;
4979 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4980 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4981 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4982
4983 /*
James Smart2a76a282012-08-03 12:35:54 -04004984 * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
4985 * we will associate a new ring, for each FCP fastpath EQ/CQ/WQ tuple.
4986 */
4987 if (!phba->sli.ring)
4988 phba->sli.ring = kzalloc(
James Smart67d12732012-08-03 12:36:13 -04004989 (LPFC_SLI3_MAX_RING + phba->cfg_fcp_io_channel) *
James Smart2a76a282012-08-03 12:35:54 -04004990 sizeof(struct lpfc_sli_ring), GFP_KERNEL);
4991 if (!phba->sli.ring)
4992 return -ENOMEM;
James Smart085c6472010-11-20 23:11:37 -05004993
James Smart09294d42013-04-17 20:16:05 -04004994 /*
4995 * It doesn't matter what family our adapter is in, we are
4996 * limited to 2 Pages, 512 SGEs, for our SGL.
4997 * There are going to be 2 reserved SGEs: 1 FCP cmnd + 1 FCP rsp
4998 */
4999 max_buf_size = (2 * SLI4_PAGE_SIZE);
5000 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SGL_SEG_CNT - 2)
5001 phba->cfg_sg_seg_cnt = LPFC_MAX_SGL_SEG_CNT - 2;
James Smart09294d42013-04-17 20:16:05 -04005002
James Smartda0436e2009-05-22 14:51:39 -04005003 /*
James Smart96f70772013-04-17 20:16:15 -04005004 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smartda0436e2009-05-22 14:51:39 -04005005 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart28baac72010-02-12 14:42:03 -05005006 */
James Smart085c6472010-11-20 23:11:37 -05005007
James Smart96f70772013-04-17 20:16:15 -04005008 if (phba->cfg_enable_bg) {
5009 /*
5010 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
5011 * the FCP rsp, and a SGE for each. Sice we have no control
5012 * over how many protection data segments the SCSI Layer
5013 * will hand us (ie: there could be one for every block
5014 * in the IO), we just allocate enough SGEs to accomidate
5015 * our max amount and we need to limit lpfc_sg_seg_cnt to
5016 * minimize the risk of running out.
5017 */
5018 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5019 sizeof(struct fcp_rsp) + max_buf_size;
5020
5021 /* Total SGEs for scsi_sg_list and scsi_sg_prot_list */
5022 phba->cfg_total_seg_cnt = LPFC_MAX_SGL_SEG_CNT;
5023
5024 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SLI4_SEG_CNT_DIF)
5025 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SLI4_SEG_CNT_DIF;
5026 } else {
5027 /*
5028 * The scsi_buf for a regular I/O will hold the FCP cmnd,
5029 * the FCP rsp, a SGE for each, and a SGE for up to
5030 * cfg_sg_seg_cnt data segments.
5031 */
5032 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5033 sizeof(struct fcp_rsp) +
5034 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge));
5035
5036 /* Total SGEs for scsi_sg_list */
5037 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
5038 /*
5039 * NOTE: if (phba->cfg_sg_seg_cnt + 2) <= 256 we only need
5040 * to post 1 page for the SGL.
5041 */
James Smart085c6472010-11-20 23:11:37 -05005042 }
James Smartacd68592012-01-18 16:25:09 -05005043
James Smart96f70772013-04-17 20:16:15 -04005044 /* Initialize the host templates with the updated values. */
5045 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5046 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5047
5048 if (phba->cfg_sg_dma_buf_size <= LPFC_MIN_SG_SLI4_BUF_SZ)
5049 phba->cfg_sg_dma_buf_size = LPFC_MIN_SG_SLI4_BUF_SZ;
5050 else
5051 phba->cfg_sg_dma_buf_size =
5052 SLI4_PAGE_ALIGN(phba->cfg_sg_dma_buf_size);
5053
5054 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
5055 "9087 sg_tablesize:%d dmabuf_size:%d total_sge:%d\n",
5056 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
5057 phba->cfg_total_seg_cnt);
James Smartda0436e2009-05-22 14:51:39 -04005058
5059 /* Initialize buffer queue management fields */
5060 hbq_count = lpfc_sli_hbq_count();
5061 for (i = 0; i < hbq_count; ++i)
5062 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
5063 INIT_LIST_HEAD(&phba->rb_pend_list);
5064 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
5065 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
5066
5067 /*
5068 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
5069 */
5070 /* Initialize the Abort scsi buffer list used by driver */
5071 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
5072 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
5073 /* This abort list used by worker thread */
5074 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
5075
5076 /*
James Smart6d368e52011-05-24 11:44:12 -04005077 * Initialize driver internal slow-path work queues
James Smartda0436e2009-05-22 14:51:39 -04005078 */
5079
5080 /* Driver internel slow-path CQ Event pool */
5081 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
5082 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04005083 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04005084 /* Asynchronous event CQ Event work queue list */
5085 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
5086 /* Fast-path XRI aborted CQ Event work queue list */
5087 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
5088 /* Slow-path XRI aborted CQ Event work queue list */
5089 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
5090 /* Receive queue CQ Event work queue list */
5091 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
5092
James Smart6d368e52011-05-24 11:44:12 -04005093 /* Initialize extent block lists. */
5094 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
5095 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
5096 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
5097 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
5098
James Smartda0436e2009-05-22 14:51:39 -04005099 /* Initialize the driver internal SLI layer lists. */
5100 lpfc_sli_setup(phba);
5101 lpfc_sli_queue_setup(phba);
5102
5103 /* Allocate device driver memory */
5104 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
5105 if (rc)
5106 return -ENOMEM;
5107
James Smart2fcee4b2010-12-15 17:57:46 -05005108 /* IF Type 2 ports get initialized now. */
5109 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
5110 LPFC_SLI_INTF_IF_TYPE_2) {
5111 rc = lpfc_pci_function_reset(phba);
5112 if (unlikely(rc))
5113 return -ENODEV;
5114 }
5115
James Smartda0436e2009-05-22 14:51:39 -04005116 /* Create the bootstrap mailbox command */
5117 rc = lpfc_create_bootstrap_mbox(phba);
5118 if (unlikely(rc))
5119 goto out_free_mem;
5120
5121 /* Set up the host's endian order with the device. */
5122 rc = lpfc_setup_endian_order(phba);
5123 if (unlikely(rc))
5124 goto out_free_bsmbx;
5125
5126 /* Set up the hba's configuration parameters. */
5127 rc = lpfc_sli4_read_config(phba);
5128 if (unlikely(rc))
5129 goto out_free_bsmbx;
James Smartcff261f2013-12-17 20:29:47 -05005130 rc = lpfc_mem_alloc_active_rrq_pool_s4(phba);
5131 if (unlikely(rc))
5132 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04005133
James Smart2fcee4b2010-12-15 17:57:46 -05005134 /* IF Type 0 ports get initialized now. */
5135 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
5136 LPFC_SLI_INTF_IF_TYPE_0) {
5137 rc = lpfc_pci_function_reset(phba);
5138 if (unlikely(rc))
5139 goto out_free_bsmbx;
5140 }
James Smartda0436e2009-05-22 14:51:39 -04005141
James Smartcb5172e2010-03-15 11:25:07 -04005142 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
5143 GFP_KERNEL);
5144 if (!mboxq) {
5145 rc = -ENOMEM;
5146 goto out_free_bsmbx;
5147 }
5148
James Smartfedd3b72011-02-16 12:39:24 -05005149 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
James Smartcb5172e2010-03-15 11:25:07 -04005150 lpfc_supported_pages(mboxq);
5151 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smartfedd3b72011-02-16 12:39:24 -05005152 if (!rc) {
5153 mqe = &mboxq->u.mqe;
5154 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
5155 LPFC_MAX_SUPPORTED_PAGES);
5156 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
5157 switch (pn_page[i]) {
5158 case LPFC_SLI4_PARAMETERS:
5159 phba->sli4_hba.pc_sli4_params.supported = 1;
5160 break;
5161 default:
5162 break;
5163 }
5164 }
5165 /* Read the port's SLI4 Parameters capabilities if supported. */
5166 if (phba->sli4_hba.pc_sli4_params.supported)
5167 rc = lpfc_pc_sli4_params_get(phba, mboxq);
5168 if (rc) {
5169 mempool_free(mboxq, phba->mbox_mem_pool);
5170 rc = -EIO;
5171 goto out_free_bsmbx;
James Smartcb5172e2010-03-15 11:25:07 -04005172 }
5173 }
James Smartfedd3b72011-02-16 12:39:24 -05005174 /*
5175 * Get sli4 parameters that override parameters from Port capabilities.
James Smart6d368e52011-05-24 11:44:12 -04005176 * If this call fails, it isn't critical unless the SLI4 parameters come
5177 * back in conflict.
James Smartfedd3b72011-02-16 12:39:24 -05005178 */
James Smart6d368e52011-05-24 11:44:12 -04005179 rc = lpfc_get_sli4_parameters(phba, mboxq);
5180 if (rc) {
5181 if (phba->sli4_hba.extents_in_use &&
5182 phba->sli4_hba.rpi_hdrs_in_use) {
5183 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5184 "2999 Unsupported SLI4 Parameters "
5185 "Extents and RPI headers enabled.\n");
5186 goto out_free_bsmbx;
5187 }
5188 }
James Smartcb5172e2010-03-15 11:25:07 -04005189 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1ba981f2014-02-20 09:56:45 -05005190
5191 /* Verify OAS is supported */
5192 lpfc_sli4_oas_verify(phba);
5193 if (phba->cfg_fof)
5194 fof_vectors = 1;
5195
James Smart5350d872011-10-10 21:33:49 -04005196 /* Verify all the SLI4 queues */
5197 rc = lpfc_sli4_queue_verify(phba);
James Smartda0436e2009-05-22 14:51:39 -04005198 if (rc)
5199 goto out_free_bsmbx;
5200
5201 /* Create driver internal CQE event pool */
5202 rc = lpfc_sli4_cq_event_pool_create(phba);
5203 if (rc)
James Smart5350d872011-10-10 21:33:49 -04005204 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04005205
James Smart8a9d2e82012-05-09 21:16:12 -04005206 /* Initialize sgl lists per host */
5207 lpfc_init_sgl_list(phba);
5208
5209 /* Allocate and initialize active sgl array */
James Smartda0436e2009-05-22 14:51:39 -04005210 rc = lpfc_init_active_sgl_array(phba);
5211 if (rc) {
5212 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5213 "1430 Failed to initialize sgl list.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04005214 goto out_destroy_cq_event_pool;
James Smartda0436e2009-05-22 14:51:39 -04005215 }
James Smartda0436e2009-05-22 14:51:39 -04005216 rc = lpfc_sli4_init_rpi_hdrs(phba);
5217 if (rc) {
5218 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5219 "1432 Failed to initialize rpi headers.\n");
5220 goto out_free_active_sgl;
5221 }
5222
James Smarta93ff372010-10-22 11:06:08 -04005223 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
James Smart0c9ab6f2010-02-26 14:15:57 -05005224 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
5225 phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
5226 GFP_KERNEL);
5227 if (!phba->fcf.fcf_rr_bmask) {
5228 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5229 "2759 Failed allocate memory for FCF round "
5230 "robin failover bmask\n");
James Smart05580562011-05-24 11:40:48 -04005231 rc = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -05005232 goto out_remove_rpi_hdrs;
5233 }
5234
James Smart67d12732012-08-03 12:36:13 -04005235 phba->sli4_hba.fcp_eq_hdl =
5236 kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
James Smart1ba981f2014-02-20 09:56:45 -05005237 (fof_vectors + phba->cfg_fcp_io_channel)),
5238 GFP_KERNEL);
James Smart67d12732012-08-03 12:36:13 -04005239 if (!phba->sli4_hba.fcp_eq_hdl) {
5240 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5241 "2572 Failed allocate memory for "
5242 "fast-path per-EQ handle array\n");
5243 rc = -ENOMEM;
5244 goto out_free_fcf_rr_bmask;
James Smartda0436e2009-05-22 14:51:39 -04005245 }
5246
5247 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
James Smart1ba981f2014-02-20 09:56:45 -05005248 (fof_vectors +
5249 phba->cfg_fcp_io_channel)), GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005250 if (!phba->sli4_hba.msix_entries) {
5251 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5252 "2573 Failed allocate memory for msi-x "
5253 "interrupt vector entries\n");
James Smart05580562011-05-24 11:40:48 -04005254 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04005255 goto out_free_fcp_eq_hdl;
5256 }
5257
James Smart7bb03bb2013-04-17 20:19:16 -04005258 phba->sli4_hba.cpu_map = kzalloc((sizeof(struct lpfc_vector_map_info) *
5259 phba->sli4_hba.num_present_cpu),
5260 GFP_KERNEL);
5261 if (!phba->sli4_hba.cpu_map) {
5262 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5263 "3327 Failed allocate memory for msi-x "
5264 "interrupt vector mapping\n");
5265 rc = -ENOMEM;
5266 goto out_free_msix;
5267 }
James Smartb246de12013-05-31 17:03:07 -04005268 if (lpfc_used_cpu == NULL) {
5269 lpfc_used_cpu = kzalloc((sizeof(uint16_t) * lpfc_present_cpu),
5270 GFP_KERNEL);
5271 if (!lpfc_used_cpu) {
5272 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5273 "3335 Failed allocate memory for msi-x "
5274 "interrupt vector mapping\n");
5275 kfree(phba->sli4_hba.cpu_map);
5276 rc = -ENOMEM;
5277 goto out_free_msix;
5278 }
5279 for (i = 0; i < lpfc_present_cpu; i++)
5280 lpfc_used_cpu[i] = LPFC_VECTOR_MAP_EMPTY;
5281 }
5282
James Smart7bb03bb2013-04-17 20:19:16 -04005283 /* Initialize io channels for round robin */
5284 cpup = phba->sli4_hba.cpu_map;
5285 rc = 0;
5286 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
5287 cpup->channel_id = rc;
5288 rc++;
5289 if (rc >= phba->cfg_fcp_io_channel)
5290 rc = 0;
5291 }
5292
James Smart912e3ac2011-05-24 11:42:11 -04005293 /*
5294 * Enable sr-iov virtual functions if supported and configured
5295 * through the module parameter.
5296 */
5297 if (phba->cfg_sriov_nr_virtfn > 0) {
5298 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
5299 phba->cfg_sriov_nr_virtfn);
5300 if (rc) {
5301 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5302 "3020 Requested number of SR-IOV "
5303 "virtual functions (%d) is not "
5304 "supported\n",
5305 phba->cfg_sriov_nr_virtfn);
5306 phba->cfg_sriov_nr_virtfn = 0;
5307 }
5308 }
5309
James Smart5248a742011-07-22 18:37:06 -04005310 return 0;
James Smartda0436e2009-05-22 14:51:39 -04005311
James Smart7bb03bb2013-04-17 20:19:16 -04005312out_free_msix:
5313 kfree(phba->sli4_hba.msix_entries);
James Smartda0436e2009-05-22 14:51:39 -04005314out_free_fcp_eq_hdl:
5315 kfree(phba->sli4_hba.fcp_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05005316out_free_fcf_rr_bmask:
5317 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04005318out_remove_rpi_hdrs:
5319 lpfc_sli4_remove_rpi_hdrs(phba);
5320out_free_active_sgl:
5321 lpfc_free_active_sgl(phba);
James Smartda0436e2009-05-22 14:51:39 -04005322out_destroy_cq_event_pool:
5323 lpfc_sli4_cq_event_pool_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04005324out_free_bsmbx:
5325 lpfc_destroy_bootstrap_mbox(phba);
5326out_free_mem:
5327 lpfc_mem_free(phba);
5328 return rc;
5329}
5330
5331/**
5332 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
5333 * @phba: pointer to lpfc hba data structure.
5334 *
5335 * This routine is invoked to unset the driver internal resources set up
5336 * specific for supporting the SLI-4 HBA device it attached to.
5337 **/
5338static void
5339lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
5340{
5341 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
5342
James Smart7bb03bb2013-04-17 20:19:16 -04005343 /* Free memory allocated for msi-x interrupt vector to CPU mapping */
5344 kfree(phba->sli4_hba.cpu_map);
5345 phba->sli4_hba.num_present_cpu = 0;
5346 phba->sli4_hba.num_online_cpu = 0;
James Smart76fd07a2014-02-20 09:57:18 -05005347 phba->sli4_hba.curr_disp_cpu = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04005348
James Smartda0436e2009-05-22 14:51:39 -04005349 /* Free memory allocated for msi-x interrupt vector entries */
5350 kfree(phba->sli4_hba.msix_entries);
5351
5352 /* Free memory allocated for fast-path work queue handles */
5353 kfree(phba->sli4_hba.fcp_eq_hdl);
5354
5355 /* Free the allocated rpi headers. */
5356 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04005357 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04005358
James Smart0c9ab6f2010-02-26 14:15:57 -05005359 /* Free eligible FCF index bmask */
5360 kfree(phba->fcf.fcf_rr_bmask);
5361
James Smartda0436e2009-05-22 14:51:39 -04005362 /* Free the ELS sgl list */
5363 lpfc_free_active_sgl(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04005364 lpfc_free_els_sgl_list(phba);
James Smartda0436e2009-05-22 14:51:39 -04005365
James Smartda0436e2009-05-22 14:51:39 -04005366 /* Free the completion queue EQ event pool */
5367 lpfc_sli4_cq_event_release_all(phba);
5368 lpfc_sli4_cq_event_pool_destroy(phba);
5369
James Smart6d368e52011-05-24 11:44:12 -04005370 /* Release resource identifiers. */
5371 lpfc_sli4_dealloc_resource_identifiers(phba);
5372
James Smartda0436e2009-05-22 14:51:39 -04005373 /* Free the bsmbx region. */
5374 lpfc_destroy_bootstrap_mbox(phba);
5375
5376 /* Free the SLI Layer memory with SLI4 HBAs */
5377 lpfc_mem_free_all(phba);
5378
5379 /* Free the current connect table */
5380 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04005381 &phba->fcf_conn_rec_list, list) {
5382 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04005383 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04005384 }
James Smartda0436e2009-05-22 14:51:39 -04005385
5386 return;
5387}
5388
5389/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005390 * lpfc_init_api_table_setup - Set up init api function jump table
James Smart3772a992009-05-22 14:50:54 -04005391 * @phba: The hba struct for which this call is being executed.
5392 * @dev_grp: The HBA PCI-Device group number.
5393 *
5394 * This routine sets up the device INIT interface API function jump table
5395 * in @phba struct.
5396 *
5397 * Returns: 0 - success, -ENODEV - failure.
5398 **/
5399int
5400lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5401{
James Smart84d1b002010-02-12 14:42:33 -05005402 phba->lpfc_hba_init_link = lpfc_hba_init_link;
5403 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart7f860592011-03-11 16:05:52 -05005404 phba->lpfc_selective_reset = lpfc_selective_reset;
James Smart3772a992009-05-22 14:50:54 -04005405 switch (dev_grp) {
5406 case LPFC_PCI_DEV_LP:
5407 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
5408 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
5409 phba->lpfc_stop_port = lpfc_stop_port_s3;
5410 break;
James Smartda0436e2009-05-22 14:51:39 -04005411 case LPFC_PCI_DEV_OC:
5412 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
5413 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
5414 phba->lpfc_stop_port = lpfc_stop_port_s4;
5415 break;
James Smart3772a992009-05-22 14:50:54 -04005416 default:
5417 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5418 "1431 Invalid HBA PCI-device group: 0x%x\n",
5419 dev_grp);
5420 return -ENODEV;
5421 break;
5422 }
5423 return 0;
5424}
5425
5426/**
5427 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
5428 * @phba: pointer to lpfc hba data structure.
5429 *
5430 * This routine is invoked to set up the driver internal resources before the
5431 * device specific resource setup to support the HBA device it attached to.
5432 *
5433 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005434 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005435 * other values - error
5436 **/
5437static int
5438lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
5439{
5440 /*
5441 * Driver resources common to all SLI revisions
5442 */
James Smartea2151b2008-09-07 11:52:10 -04005443 atomic_set(&phba->fast_event_count, 0);
James Smart2e0fef82007-06-17 19:56:36 -05005444 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005445
James Smarte47c9092008-02-08 18:49:26 -05005446 /* Initialize ndlp management spinlock */
5447 spin_lock_init(&phba->ndlp_lock);
5448
James Smart2e0fef82007-06-17 19:56:36 -05005449 INIT_LIST_HEAD(&phba->port_list);
James Smart3772a992009-05-22 14:50:54 -04005450 INIT_LIST_HEAD(&phba->work_list);
James Smart84774a42008-08-24 21:50:06 -04005451 init_waitqueue_head(&phba->wait_4_mlo_m_q);
dea31012005-04-17 16:05:31 -05005452
James Smart3772a992009-05-22 14:50:54 -04005453 /* Initialize the wait queue head for the kernel thread */
5454 init_waitqueue_head(&phba->work_waitq);
James Smart858c9f62007-06-17 19:56:39 -05005455
James Smart3772a992009-05-22 14:50:54 -04005456 /* Initialize the scsi buffer list used by driver for scsi IO */
James Smarta40fc5f2013-04-17 20:17:40 -04005457 spin_lock_init(&phba->scsi_buf_list_get_lock);
5458 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_get);
5459 spin_lock_init(&phba->scsi_buf_list_put_lock);
5460 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
dea31012005-04-17 16:05:31 -05005461
James Smart3772a992009-05-22 14:50:54 -04005462 /* Initialize the fabric iocb list */
5463 INIT_LIST_HEAD(&phba->fabric_iocb_list);
dea31012005-04-17 16:05:31 -05005464
James Smart3772a992009-05-22 14:50:54 -04005465 /* Initialize list to save ELS buffers */
5466 INIT_LIST_HEAD(&phba->elsbuf);
dea31012005-04-17 16:05:31 -05005467
James Smart3772a992009-05-22 14:50:54 -04005468 /* Initialize FCF connection rec list */
5469 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
dea31012005-04-17 16:05:31 -05005470
James Smart1ba981f2014-02-20 09:56:45 -05005471 /* Initialize OAS configuration list */
5472 spin_lock_init(&phba->devicelock);
5473 INIT_LIST_HEAD(&phba->luns);
5474
James Smart3772a992009-05-22 14:50:54 -04005475 return 0;
5476}
dea31012005-04-17 16:05:31 -05005477
James Smart3772a992009-05-22 14:50:54 -04005478/**
5479 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
5480 * @phba: pointer to lpfc hba data structure.
5481 *
5482 * This routine is invoked to set up the driver internal resources after the
5483 * device specific resource setup to support the HBA device it attached to.
5484 *
5485 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005486 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005487 * other values - error
5488 **/
5489static int
5490lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
5491{
5492 int error;
5493
5494 /* Startup the kernel thread for this host adapter. */
5495 phba->worker_thread = kthread_run(lpfc_do_work, phba,
5496 "lpfc_worker_%d", phba->brd_no);
5497 if (IS_ERR(phba->worker_thread)) {
5498 error = PTR_ERR(phba->worker_thread);
5499 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005500 }
5501
James Smart3772a992009-05-22 14:50:54 -04005502 return 0;
5503}
5504
5505/**
5506 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
5507 * @phba: pointer to lpfc hba data structure.
5508 *
5509 * This routine is invoked to unset the driver internal resources set up after
5510 * the device specific resource setup for supporting the HBA device it
5511 * attached to.
5512 **/
5513static void
5514lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
5515{
5516 /* Stop kernel worker thread */
5517 kthread_stop(phba->worker_thread);
5518}
5519
5520/**
5521 * lpfc_free_iocb_list - Free iocb list.
5522 * @phba: pointer to lpfc hba data structure.
5523 *
5524 * This routine is invoked to free the driver's IOCB list and memory.
5525 **/
5526static void
5527lpfc_free_iocb_list(struct lpfc_hba *phba)
5528{
5529 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
5530
5531 spin_lock_irq(&phba->hbalock);
5532 list_for_each_entry_safe(iocbq_entry, iocbq_next,
5533 &phba->lpfc_iocb_list, list) {
5534 list_del(&iocbq_entry->list);
5535 kfree(iocbq_entry);
5536 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005537 }
James Smart3772a992009-05-22 14:50:54 -04005538 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005539
James Smart3772a992009-05-22 14:50:54 -04005540 return;
5541}
dea31012005-04-17 16:05:31 -05005542
James Smart3772a992009-05-22 14:50:54 -04005543/**
5544 * lpfc_init_iocb_list - Allocate and initialize iocb list.
5545 * @phba: pointer to lpfc hba data structure.
5546 *
5547 * This routine is invoked to allocate and initizlize the driver's IOCB
5548 * list and set up the IOCB tag array accordingly.
5549 *
5550 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005551 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005552 * other values - error
5553 **/
5554static int
5555lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
5556{
5557 struct lpfc_iocbq *iocbq_entry = NULL;
5558 uint16_t iotag;
5559 int i;
dea31012005-04-17 16:05:31 -05005560
5561 /* Initialize and populate the iocb list per host. */
5562 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04005563 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07005564 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05005565 if (iocbq_entry == NULL) {
5566 printk(KERN_ERR "%s: only allocated %d iocbs of "
5567 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07005568 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05005569 goto out_free_iocbq;
5570 }
5571
James Bottomley604a3e32005-10-29 10:28:33 -05005572 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
5573 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04005574 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05005575 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04005576 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05005577 goto out_free_iocbq;
5578 }
James Smart6d368e52011-05-24 11:44:12 -04005579 iocbq_entry->sli4_lxritag = NO_XRI;
James Smart3772a992009-05-22 14:50:54 -04005580 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05005581
5582 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005583 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
5584 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05005585 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005586 }
5587
James Smart3772a992009-05-22 14:50:54 -04005588 return 0;
5589
5590out_free_iocbq:
5591 lpfc_free_iocb_list(phba);
5592
5593 return -ENOMEM;
5594}
5595
5596/**
James Smart8a9d2e82012-05-09 21:16:12 -04005597 * lpfc_free_sgl_list - Free a given sgl list.
James Smartda0436e2009-05-22 14:51:39 -04005598 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -04005599 * @sglq_list: pointer to the head of sgl list.
James Smartda0436e2009-05-22 14:51:39 -04005600 *
James Smart8a9d2e82012-05-09 21:16:12 -04005601 * This routine is invoked to free a give sgl list and memory.
James Smartda0436e2009-05-22 14:51:39 -04005602 **/
James Smart8a9d2e82012-05-09 21:16:12 -04005603void
5604lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
James Smartda0436e2009-05-22 14:51:39 -04005605{
5606 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart8a9d2e82012-05-09 21:16:12 -04005607
5608 list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
5609 list_del(&sglq_entry->list);
5610 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
5611 kfree(sglq_entry);
5612 }
5613}
5614
5615/**
5616 * lpfc_free_els_sgl_list - Free els sgl list.
5617 * @phba: pointer to lpfc hba data structure.
5618 *
5619 * This routine is invoked to free the driver's els sgl list and memory.
5620 **/
5621static void
5622lpfc_free_els_sgl_list(struct lpfc_hba *phba)
5623{
James Smartda0436e2009-05-22 14:51:39 -04005624 LIST_HEAD(sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04005625
James Smart8a9d2e82012-05-09 21:16:12 -04005626 /* Retrieve all els sgls from driver list */
James Smartda0436e2009-05-22 14:51:39 -04005627 spin_lock_irq(&phba->hbalock);
5628 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
5629 spin_unlock_irq(&phba->hbalock);
5630
James Smart8a9d2e82012-05-09 21:16:12 -04005631 /* Now free the sgl list */
5632 lpfc_free_sgl_list(phba, &sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04005633}
5634
5635/**
5636 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
5637 * @phba: pointer to lpfc hba data structure.
5638 *
5639 * This routine is invoked to allocate the driver's active sgl memory.
5640 * This array will hold the sglq_entry's for active IOs.
5641 **/
5642static int
5643lpfc_init_active_sgl_array(struct lpfc_hba *phba)
5644{
5645 int size;
5646 size = sizeof(struct lpfc_sglq *);
5647 size *= phba->sli4_hba.max_cfg_param.max_xri;
5648
5649 phba->sli4_hba.lpfc_sglq_active_list =
5650 kzalloc(size, GFP_KERNEL);
5651 if (!phba->sli4_hba.lpfc_sglq_active_list)
5652 return -ENOMEM;
5653 return 0;
5654}
5655
5656/**
5657 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
5658 * @phba: pointer to lpfc hba data structure.
5659 *
5660 * This routine is invoked to walk through the array of active sglq entries
5661 * and free all of the resources.
5662 * This is just a place holder for now.
5663 **/
5664static void
5665lpfc_free_active_sgl(struct lpfc_hba *phba)
5666{
5667 kfree(phba->sli4_hba.lpfc_sglq_active_list);
5668}
5669
5670/**
5671 * lpfc_init_sgl_list - Allocate and initialize sgl list.
5672 * @phba: pointer to lpfc hba data structure.
5673 *
5674 * This routine is invoked to allocate and initizlize the driver's sgl
5675 * list and set up the sgl xritag tag array accordingly.
5676 *
James Smartda0436e2009-05-22 14:51:39 -04005677 **/
James Smart8a9d2e82012-05-09 21:16:12 -04005678static void
James Smartda0436e2009-05-22 14:51:39 -04005679lpfc_init_sgl_list(struct lpfc_hba *phba)
5680{
James Smartda0436e2009-05-22 14:51:39 -04005681 /* Initialize and populate the sglq list per host/VF. */
5682 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
5683 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
5684
James Smart8a9d2e82012-05-09 21:16:12 -04005685 /* els xri-sgl book keeping */
5686 phba->sli4_hba.els_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04005687
James Smart8a9d2e82012-05-09 21:16:12 -04005688 /* scsi xri-buffer book keeping */
James Smartda0436e2009-05-22 14:51:39 -04005689 phba->sli4_hba.scsi_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04005690}
5691
5692/**
5693 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
5694 * @phba: pointer to lpfc hba data structure.
5695 *
5696 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -04005697 * port for those SLI4 ports that do not support extents. This routine
James Smartda0436e2009-05-22 14:51:39 -04005698 * posts a PAGE_SIZE memory region to the port to hold up to
James Smart88a2cfb2011-07-22 18:36:33 -04005699 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
5700 * and should be called only when interrupts are disabled.
James Smartda0436e2009-05-22 14:51:39 -04005701 *
5702 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005703 * 0 - successful
James Smart88a2cfb2011-07-22 18:36:33 -04005704 * -ERROR - otherwise.
James Smartda0436e2009-05-22 14:51:39 -04005705 **/
5706int
5707lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
5708{
5709 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04005710 struct lpfc_rpi_hdr *rpi_hdr;
5711
5712 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
James Smartff78d8f2011-12-13 13:21:35 -05005713 if (!phba->sli4_hba.rpi_hdrs_in_use)
James Smart6d368e52011-05-24 11:44:12 -04005714 return rc;
James Smart6d368e52011-05-24 11:44:12 -04005715 if (phba->sli4_hba.extents_in_use)
5716 return -EIO;
James Smartda0436e2009-05-22 14:51:39 -04005717
5718 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
5719 if (!rpi_hdr) {
5720 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5721 "0391 Error during rpi post operation\n");
5722 lpfc_sli4_remove_rpis(phba);
5723 rc = -ENODEV;
5724 }
5725
5726 return rc;
5727}
5728
5729/**
5730 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
5731 * @phba: pointer to lpfc hba data structure.
5732 *
5733 * This routine is invoked to allocate a single 4KB memory region to
5734 * support rpis and stores them in the phba. This single region
5735 * provides support for up to 64 rpis. The region is used globally
5736 * by the device.
5737 *
5738 * Returns:
5739 * A valid rpi hdr on success.
5740 * A NULL pointer on any failure.
5741 **/
5742struct lpfc_rpi_hdr *
5743lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
5744{
5745 uint16_t rpi_limit, curr_rpi_range;
5746 struct lpfc_dmabuf *dmabuf;
5747 struct lpfc_rpi_hdr *rpi_hdr;
James Smart9589b0622011-04-16 11:03:17 -04005748 uint32_t rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04005749
James Smart6d368e52011-05-24 11:44:12 -04005750 /*
5751 * If the SLI4 port supports extents, posting the rpi header isn't
5752 * required. Set the expected maximum count and let the actual value
5753 * get set when extents are fully allocated.
5754 */
5755 if (!phba->sli4_hba.rpi_hdrs_in_use)
5756 return NULL;
5757 if (phba->sli4_hba.extents_in_use)
5758 return NULL;
5759
5760 /* The limit on the logical index is just the max_rpi count. */
James Smartda0436e2009-05-22 14:51:39 -04005761 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
James Smart6d368e52011-05-24 11:44:12 -04005762 phba->sli4_hba.max_cfg_param.max_rpi - 1;
James Smartda0436e2009-05-22 14:51:39 -04005763
5764 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04005765 /*
5766 * Establish the starting RPI in this header block. The starting
5767 * rpi is normalized to a zero base because the physical rpi is
5768 * port based.
5769 */
James Smart97f2ecf2012-03-01 22:35:23 -05005770 curr_rpi_range = phba->sli4_hba.next_rpi;
James Smartda0436e2009-05-22 14:51:39 -04005771 spin_unlock_irq(&phba->hbalock);
5772
5773 /*
5774 * The port has a limited number of rpis. The increment here
5775 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
5776 * and to allow the full max_rpi range per port.
5777 */
5778 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
James Smart9589b0622011-04-16 11:03:17 -04005779 rpi_count = rpi_limit - curr_rpi_range;
5780 else
5781 rpi_count = LPFC_RPI_HDR_COUNT;
James Smartda0436e2009-05-22 14:51:39 -04005782
James Smart6d368e52011-05-24 11:44:12 -04005783 if (!rpi_count)
5784 return NULL;
James Smartda0436e2009-05-22 14:51:39 -04005785 /*
5786 * First allocate the protocol header region for the port. The
5787 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
5788 */
5789 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5790 if (!dmabuf)
5791 return NULL;
5792
5793 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5794 LPFC_HDR_TEMPLATE_SIZE,
5795 &dmabuf->phys,
5796 GFP_KERNEL);
5797 if (!dmabuf->virt) {
5798 rpi_hdr = NULL;
5799 goto err_free_dmabuf;
5800 }
5801
5802 memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
5803 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
5804 rpi_hdr = NULL;
5805 goto err_free_coherent;
5806 }
5807
5808 /* Save the rpi header data for cleanup later. */
5809 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
5810 if (!rpi_hdr)
5811 goto err_free_coherent;
5812
5813 rpi_hdr->dmabuf = dmabuf;
5814 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
5815 rpi_hdr->page_count = 1;
5816 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04005817
5818 /* The rpi_hdr stores the logical index only. */
5819 rpi_hdr->start_rpi = curr_rpi_range;
James Smartda0436e2009-05-22 14:51:39 -04005820 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
5821
5822 /*
James Smart6d368e52011-05-24 11:44:12 -04005823 * The next_rpi stores the next logical module-64 rpi value used
5824 * to post physical rpis in subsequent rpi postings.
James Smartda0436e2009-05-22 14:51:39 -04005825 */
James Smart9589b0622011-04-16 11:03:17 -04005826 phba->sli4_hba.next_rpi += rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04005827 spin_unlock_irq(&phba->hbalock);
5828 return rpi_hdr;
5829
5830 err_free_coherent:
5831 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
5832 dmabuf->virt, dmabuf->phys);
5833 err_free_dmabuf:
5834 kfree(dmabuf);
5835 return NULL;
5836}
5837
5838/**
5839 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
5840 * @phba: pointer to lpfc hba data structure.
5841 *
5842 * This routine is invoked to remove all memory resources allocated
James Smart6d368e52011-05-24 11:44:12 -04005843 * to support rpis for SLI4 ports not supporting extents. This routine
5844 * presumes the caller has released all rpis consumed by fabric or port
5845 * logins and is prepared to have the header pages removed.
James Smartda0436e2009-05-22 14:51:39 -04005846 **/
5847void
5848lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
5849{
5850 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
5851
James Smart6d368e52011-05-24 11:44:12 -04005852 if (!phba->sli4_hba.rpi_hdrs_in_use)
5853 goto exit;
5854
James Smartda0436e2009-05-22 14:51:39 -04005855 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
5856 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
5857 list_del(&rpi_hdr->list);
5858 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
5859 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
5860 kfree(rpi_hdr->dmabuf);
5861 kfree(rpi_hdr);
5862 }
James Smart6d368e52011-05-24 11:44:12 -04005863 exit:
5864 /* There are no rpis available to the port now. */
5865 phba->sli4_hba.next_rpi = 0;
James Smartda0436e2009-05-22 14:51:39 -04005866}
5867
5868/**
James Smart3772a992009-05-22 14:50:54 -04005869 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
5870 * @pdev: pointer to pci device data structure.
5871 *
5872 * This routine is invoked to allocate the driver hba data structure for an
5873 * HBA device. If the allocation is successful, the phba reference to the
5874 * PCI device data structure is set.
5875 *
5876 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005877 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04005878 * NULL - error
5879 **/
5880static struct lpfc_hba *
5881lpfc_hba_alloc(struct pci_dev *pdev)
5882{
5883 struct lpfc_hba *phba;
5884
5885 /* Allocate memory for HBA structure */
5886 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
5887 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02005888 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04005889 return NULL;
5890 }
5891
5892 /* Set reference to PCI device in HBA structure */
5893 phba->pcidev = pdev;
5894
5895 /* Assign an unused board number */
5896 phba->brd_no = lpfc_get_instance();
5897 if (phba->brd_no < 0) {
5898 kfree(phba);
5899 return NULL;
5900 }
5901
James Smart4fede782010-01-26 23:08:55 -05005902 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04005903 INIT_LIST_HEAD(&phba->ct_ev_waiters);
5904
James Smart3772a992009-05-22 14:50:54 -04005905 return phba;
5906}
5907
5908/**
5909 * lpfc_hba_free - Free driver hba data structure with a device.
5910 * @phba: pointer to lpfc hba data structure.
5911 *
5912 * This routine is invoked to free the driver hba data structure with an
5913 * HBA device.
5914 **/
5915static void
5916lpfc_hba_free(struct lpfc_hba *phba)
5917{
5918 /* Release the driver assigned board number */
5919 idr_remove(&lpfc_hba_index, phba->brd_no);
5920
James Smart2a76a282012-08-03 12:35:54 -04005921 /* Free memory allocated with sli rings */
5922 kfree(phba->sli.ring);
5923 phba->sli.ring = NULL;
5924
James Smart3772a992009-05-22 14:50:54 -04005925 kfree(phba);
5926 return;
5927}
5928
5929/**
5930 * lpfc_create_shost - Create hba physical port with associated scsi host.
5931 * @phba: pointer to lpfc hba data structure.
5932 *
5933 * This routine is invoked to create HBA physical port and associate a SCSI
5934 * host with it.
5935 *
5936 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005937 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005938 * other values - error
5939 **/
5940static int
5941lpfc_create_shost(struct lpfc_hba *phba)
5942{
5943 struct lpfc_vport *vport;
5944 struct Scsi_Host *shost;
5945
5946 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05005947 phba->fc_edtov = FF_DEF_EDTOV;
5948 phba->fc_ratov = FF_DEF_RATOV;
5949 phba->fc_altov = FF_DEF_ALTOV;
5950 phba->fc_arbtov = FF_DEF_ARBTOV;
5951
James Smartd7c47992010-06-08 18:31:54 -04005952 atomic_set(&phba->sdev_cnt, 0);
James Smart3de2a652007-08-02 11:09:59 -04005953 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05005954 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04005955 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05005956
5957 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05005958 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05005959 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04005960 /* Put reference to SCSI host to driver's device private data */
5961 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05005962
James Smart3772a992009-05-22 14:50:54 -04005963 return 0;
5964}
dea31012005-04-17 16:05:31 -05005965
James Smart3772a992009-05-22 14:50:54 -04005966/**
5967 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
5968 * @phba: pointer to lpfc hba data structure.
5969 *
5970 * This routine is invoked to destroy HBA physical port and the associated
5971 * SCSI host.
5972 **/
5973static void
5974lpfc_destroy_shost(struct lpfc_hba *phba)
5975{
5976 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04005977
James Smart3772a992009-05-22 14:50:54 -04005978 /* Destroy physical port that associated with the SCSI host */
5979 destroy_port(vport);
5980
5981 return;
5982}
5983
5984/**
5985 * lpfc_setup_bg - Setup Block guard structures and debug areas.
5986 * @phba: pointer to lpfc hba data structure.
5987 * @shost: the shost to be used to detect Block guard settings.
5988 *
5989 * This routine sets up the local Block guard protocol settings for @shost.
5990 * This routine also allocates memory for debugging bg buffers.
5991 **/
5992static void
5993lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
5994{
James Smartbbeb79b2012-06-12 13:54:27 -04005995 uint32_t old_mask;
5996 uint32_t old_guard;
5997
James Smart3772a992009-05-22 14:50:54 -04005998 int pagecnt = 10;
5999 if (lpfc_prot_mask && lpfc_prot_guard) {
6000 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6001 "1478 Registering BlockGuard with the "
6002 "SCSI layer\n");
James Smartbbeb79b2012-06-12 13:54:27 -04006003
6004 old_mask = lpfc_prot_mask;
6005 old_guard = lpfc_prot_guard;
6006
6007 /* Only allow supported values */
6008 lpfc_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
6009 SHOST_DIX_TYPE0_PROTECTION |
6010 SHOST_DIX_TYPE1_PROTECTION);
6011 lpfc_prot_guard &= (SHOST_DIX_GUARD_IP | SHOST_DIX_GUARD_CRC);
6012
6013 /* DIF Type 1 protection for profiles AST1/C1 is end to end */
6014 if (lpfc_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
6015 lpfc_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
6016
6017 if (lpfc_prot_mask && lpfc_prot_guard) {
6018 if ((old_mask != lpfc_prot_mask) ||
6019 (old_guard != lpfc_prot_guard))
6020 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6021 "1475 Registering BlockGuard with the "
6022 "SCSI layer: mask %d guard %d\n",
6023 lpfc_prot_mask, lpfc_prot_guard);
6024
6025 scsi_host_set_prot(shost, lpfc_prot_mask);
6026 scsi_host_set_guard(shost, lpfc_prot_guard);
6027 } else
6028 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6029 "1479 Not Registering BlockGuard with the SCSI "
6030 "layer, Bad protection parameters: %d %d\n",
6031 old_mask, old_guard);
James Smart98c9ea52007-10-27 13:37:33 -04006032 }
James Smartbbeb79b2012-06-12 13:54:27 -04006033
James Smart81301a92008-12-04 22:39:46 -05006034 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05006035 while (pagecnt) {
6036 spin_lock_init(&_dump_buf_lock);
6037 _dump_buf_data =
6038 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
6039 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04006040 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6041 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04006042 "_dump_buf_data at 0x%p\n",
6043 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05006044 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04006045 memset(_dump_buf_data, 0,
6046 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05006047 break;
James Smart3772a992009-05-22 14:50:54 -04006048 } else
James Smart81301a92008-12-04 22:39:46 -05006049 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05006050 }
James Smart81301a92008-12-04 22:39:46 -05006051 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04006052 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6053 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04006054 "memory for hexdump\n");
6055 } else
James Smart6a9c52c2009-10-02 15:16:51 -04006056 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6057 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05006058 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05006059 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05006060 while (pagecnt) {
6061 _dump_buf_dif =
6062 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
6063 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04006064 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6065 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04006066 "_dump_buf_dif at 0x%p\n",
6067 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05006068 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04006069 memset(_dump_buf_dif, 0,
6070 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05006071 break;
James Smart3772a992009-05-22 14:50:54 -04006072 } else
James Smart81301a92008-12-04 22:39:46 -05006073 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05006074 }
James Smart81301a92008-12-04 22:39:46 -05006075 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04006076 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6077 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04006078 "memory for hexdump\n");
6079 } else
James Smart6a9c52c2009-10-02 15:16:51 -04006080 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6081 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04006082 _dump_buf_dif);
6083}
6084
6085/**
6086 * lpfc_post_init_setup - Perform necessary device post initialization setup.
6087 * @phba: pointer to lpfc hba data structure.
6088 *
6089 * This routine is invoked to perform all the necessary post initialization
6090 * setup for the device.
6091 **/
6092static void
6093lpfc_post_init_setup(struct lpfc_hba *phba)
6094{
6095 struct Scsi_Host *shost;
6096 struct lpfc_adapter_event_header adapter_event;
6097
6098 /* Get the default values for Model Name and Description */
6099 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
6100
6101 /*
6102 * hba setup may have changed the hba_queue_depth so we need to
6103 * adjust the value of can_queue.
6104 */
6105 shost = pci_get_drvdata(phba->pcidev);
6106 shost->can_queue = phba->cfg_hba_queue_depth - 10;
6107 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
6108 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05006109
6110 lpfc_host_attrib_init(shost);
6111
James Smart2e0fef82007-06-17 19:56:36 -05006112 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
6113 spin_lock_irq(shost->host_lock);
6114 lpfc_poll_start_timer(phba);
6115 spin_unlock_irq(shost->host_lock);
6116 }
James Smart8f6d98d2006-08-01 07:34:00 -04006117
James Smart93996272008-08-24 21:50:30 -04006118 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6119 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04006120 /* Send board arrival event to upper layer */
6121 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
6122 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
6123 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04006124 sizeof(adapter_event),
6125 (char *) &adapter_event,
6126 LPFC_NL_VENDOR_ID);
6127 return;
6128}
6129
6130/**
6131 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
6132 * @phba: pointer to lpfc hba data structure.
6133 *
6134 * This routine is invoked to set up the PCI device memory space for device
6135 * with SLI-3 interface spec.
6136 *
6137 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006138 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006139 * other values - error
6140 **/
6141static int
6142lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
6143{
6144 struct pci_dev *pdev;
6145 unsigned long bar0map_len, bar2map_len;
6146 int i, hbq_count;
6147 void *ptr;
6148 int error = -ENODEV;
6149
6150 /* Obtain PCI device reference */
6151 if (!phba->pcidev)
6152 return error;
6153 else
6154 pdev = phba->pcidev;
6155
6156 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05006157 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
6158 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
6159 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
6160 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smart3772a992009-05-22 14:50:54 -04006161 return error;
Michael Reed8e685972009-09-18 12:02:05 -05006162 }
6163 }
James Smart3772a992009-05-22 14:50:54 -04006164
6165 /* Get the bus address of Bar0 and Bar2 and the number of bytes
6166 * required by each mapping.
6167 */
6168 phba->pci_bar0_map = pci_resource_start(pdev, 0);
6169 bar0map_len = pci_resource_len(pdev, 0);
6170
6171 phba->pci_bar2_map = pci_resource_start(pdev, 2);
6172 bar2map_len = pci_resource_len(pdev, 2);
6173
6174 /* Map HBA SLIM to a kernel virtual address. */
6175 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
6176 if (!phba->slim_memmap_p) {
6177 dev_printk(KERN_ERR, &pdev->dev,
6178 "ioremap failed for SLIM memory.\n");
6179 goto out;
6180 }
6181
6182 /* Map HBA Control Registers to a kernel virtual address. */
6183 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
6184 if (!phba->ctrl_regs_memmap_p) {
6185 dev_printk(KERN_ERR, &pdev->dev,
6186 "ioremap failed for HBA control registers.\n");
6187 goto out_iounmap_slim;
6188 }
6189
6190 /* Allocate memory for SLI-2 structures */
6191 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
6192 SLI2_SLIM_SIZE,
6193 &phba->slim2p.phys,
6194 GFP_KERNEL);
6195 if (!phba->slim2p.virt)
6196 goto out_iounmap;
6197
6198 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
6199 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04006200 phba->mbox_ext = (phba->slim2p.virt +
6201 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04006202 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
6203 phba->IOCBs = (phba->slim2p.virt +
6204 offsetof(struct lpfc_sli2_slim, IOCBs));
6205
6206 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
6207 lpfc_sli_hbq_size(),
6208 &phba->hbqslimp.phys,
6209 GFP_KERNEL);
6210 if (!phba->hbqslimp.virt)
6211 goto out_free_slim;
6212
6213 hbq_count = lpfc_sli_hbq_count();
6214 ptr = phba->hbqslimp.virt;
6215 for (i = 0; i < hbq_count; ++i) {
6216 phba->hbqs[i].hbq_virt = ptr;
6217 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
6218 ptr += (lpfc_hbq_defs[i]->entry_count *
6219 sizeof(struct lpfc_hbq_entry));
6220 }
6221 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
6222 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
6223
6224 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
6225
6226 INIT_LIST_HEAD(&phba->rb_pend_list);
6227
6228 phba->MBslimaddr = phba->slim_memmap_p;
6229 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
6230 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
6231 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
6232 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04006233
dea31012005-04-17 16:05:31 -05006234 return 0;
6235
dea31012005-04-17 16:05:31 -05006236out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04006237 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6238 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05006239out_iounmap:
6240 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05006241out_iounmap_slim:
dea31012005-04-17 16:05:31 -05006242 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05006243out:
6244 return error;
6245}
6246
James Smarte59058c2008-08-24 21:49:00 -04006247/**
James Smart3772a992009-05-22 14:50:54 -04006248 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
6249 * @phba: pointer to lpfc hba data structure.
6250 *
6251 * This routine is invoked to unset the PCI device memory space for device
6252 * with SLI-3 interface spec.
6253 **/
6254static void
6255lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
6256{
6257 struct pci_dev *pdev;
6258
6259 /* Obtain PCI device reference */
6260 if (!phba->pcidev)
6261 return;
6262 else
6263 pdev = phba->pcidev;
6264
6265 /* Free coherent DMA memory allocated */
6266 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
6267 phba->hbqslimp.virt, phba->hbqslimp.phys);
6268 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6269 phba->slim2p.virt, phba->slim2p.phys);
6270
6271 /* I/O memory unmap */
6272 iounmap(phba->ctrl_regs_memmap_p);
6273 iounmap(phba->slim_memmap_p);
6274
6275 return;
6276}
6277
6278/**
James Smartda0436e2009-05-22 14:51:39 -04006279 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
6280 * @phba: pointer to lpfc hba data structure.
6281 *
6282 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
6283 * done and check status.
6284 *
6285 * Return 0 if successful, otherwise -ENODEV.
6286 **/
6287int
6288lpfc_sli4_post_status_check(struct lpfc_hba *phba)
6289{
James Smart2fcee4b2010-12-15 17:57:46 -05006290 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
6291 struct lpfc_register reg_data;
6292 int i, port_error = 0;
6293 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04006294
James Smart9940b972011-03-11 16:06:12 -05006295 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
6296 memset(&reg_data, 0, sizeof(reg_data));
James Smart2fcee4b2010-12-15 17:57:46 -05006297 if (!phba->sli4_hba.PSMPHRregaddr)
James Smartda0436e2009-05-22 14:51:39 -04006298 return -ENODEV;
6299
James Smartda0436e2009-05-22 14:51:39 -04006300 /* Wait up to 30 seconds for the SLI Port POST done and ready */
6301 for (i = 0; i < 3000; i++) {
James Smart9940b972011-03-11 16:06:12 -05006302 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
6303 &portsmphr_reg.word0) ||
6304 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
James Smart2fcee4b2010-12-15 17:57:46 -05006305 /* Port has a fatal POST error, break out */
James Smartda0436e2009-05-22 14:51:39 -04006306 port_error = -ENODEV;
6307 break;
6308 }
James Smart2fcee4b2010-12-15 17:57:46 -05006309 if (LPFC_POST_STAGE_PORT_READY ==
6310 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
James Smartda0436e2009-05-22 14:51:39 -04006311 break;
James Smartda0436e2009-05-22 14:51:39 -04006312 msleep(10);
6313 }
6314
James Smart2fcee4b2010-12-15 17:57:46 -05006315 /*
6316 * If there was a port error during POST, then don't proceed with
6317 * other register reads as the data may not be valid. Just exit.
6318 */
6319 if (port_error) {
James Smartda0436e2009-05-22 14:51:39 -04006320 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05006321 "1408 Port Failed POST - portsmphr=0x%x, "
6322 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
6323 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
6324 portsmphr_reg.word0,
6325 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
6326 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
6327 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
6328 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
6329 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
6330 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
6331 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
6332 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
6333 } else {
James Smart28baac72010-02-12 14:42:03 -05006334 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05006335 "2534 Device Info: SLIFamily=0x%x, "
6336 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
6337 "SLIHint_2=0x%x, FT=0x%x\n",
James Smart28baac72010-02-12 14:42:03 -05006338 bf_get(lpfc_sli_intf_sli_family,
6339 &phba->sli4_hba.sli_intf),
6340 bf_get(lpfc_sli_intf_slirev,
6341 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05006342 bf_get(lpfc_sli_intf_if_type,
James Smart28baac72010-02-12 14:42:03 -05006343 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05006344 bf_get(lpfc_sli_intf_sli_hint1,
6345 &phba->sli4_hba.sli_intf),
6346 bf_get(lpfc_sli_intf_sli_hint2,
6347 &phba->sli4_hba.sli_intf),
6348 bf_get(lpfc_sli_intf_func_type,
James Smart28baac72010-02-12 14:42:03 -05006349 &phba->sli4_hba.sli_intf));
James Smart2fcee4b2010-12-15 17:57:46 -05006350 /*
6351 * Check for other Port errors during the initialization
6352 * process. Fail the load if the port did not come up
6353 * correctly.
6354 */
6355 if_type = bf_get(lpfc_sli_intf_if_type,
6356 &phba->sli4_hba.sli_intf);
6357 switch (if_type) {
6358 case LPFC_SLI_INTF_IF_TYPE_0:
6359 phba->sli4_hba.ue_mask_lo =
6360 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
6361 phba->sli4_hba.ue_mask_hi =
6362 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
6363 uerrlo_reg.word0 =
6364 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
6365 uerrhi_reg.word0 =
6366 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
6367 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
6368 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
6369 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6370 "1422 Unrecoverable Error "
6371 "Detected during POST "
6372 "uerr_lo_reg=0x%x, "
6373 "uerr_hi_reg=0x%x, "
6374 "ue_mask_lo_reg=0x%x, "
6375 "ue_mask_hi_reg=0x%x\n",
6376 uerrlo_reg.word0,
6377 uerrhi_reg.word0,
6378 phba->sli4_hba.ue_mask_lo,
6379 phba->sli4_hba.ue_mask_hi);
6380 port_error = -ENODEV;
6381 }
6382 break;
6383 case LPFC_SLI_INTF_IF_TYPE_2:
6384 /* Final checks. The port status should be clean. */
James Smart9940b972011-03-11 16:06:12 -05006385 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
6386 &reg_data.word0) ||
James Smart05580562011-05-24 11:40:48 -04006387 (bf_get(lpfc_sliport_status_err, &reg_data) &&
6388 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
James Smart2fcee4b2010-12-15 17:57:46 -05006389 phba->work_status[0] =
6390 readl(phba->sli4_hba.u.if_type2.
6391 ERR1regaddr);
6392 phba->work_status[1] =
6393 readl(phba->sli4_hba.u.if_type2.
6394 ERR2regaddr);
6395 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05006396 "2888 Unrecoverable port error "
6397 "following POST: port status reg "
6398 "0x%x, port_smphr reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05006399 "error 1=0x%x, error 2=0x%x\n",
6400 reg_data.word0,
6401 portsmphr_reg.word0,
6402 phba->work_status[0],
6403 phba->work_status[1]);
6404 port_error = -ENODEV;
6405 }
6406 break;
6407 case LPFC_SLI_INTF_IF_TYPE_1:
6408 default:
6409 break;
6410 }
James Smart28baac72010-02-12 14:42:03 -05006411 }
James Smartda0436e2009-05-22 14:51:39 -04006412 return port_error;
6413}
6414
6415/**
6416 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
6417 * @phba: pointer to lpfc hba data structure.
James Smart2fcee4b2010-12-15 17:57:46 -05006418 * @if_type: The SLI4 interface type getting configured.
James Smartda0436e2009-05-22 14:51:39 -04006419 *
6420 * This routine is invoked to set up SLI4 BAR0 PCI config space register
6421 * memory map.
6422 **/
6423static void
James Smart2fcee4b2010-12-15 17:57:46 -05006424lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04006425{
James Smart2fcee4b2010-12-15 17:57:46 -05006426 switch (if_type) {
6427 case LPFC_SLI_INTF_IF_TYPE_0:
6428 phba->sli4_hba.u.if_type0.UERRLOregaddr =
6429 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
6430 phba->sli4_hba.u.if_type0.UERRHIregaddr =
6431 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
6432 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
6433 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
6434 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
6435 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
6436 phba->sli4_hba.SLIINTFregaddr =
6437 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
6438 break;
6439 case LPFC_SLI_INTF_IF_TYPE_2:
6440 phba->sli4_hba.u.if_type2.ERR1regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006441 phba->sli4_hba.conf_regs_memmap_p +
6442 LPFC_CTL_PORT_ER1_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006443 phba->sli4_hba.u.if_type2.ERR2regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006444 phba->sli4_hba.conf_regs_memmap_p +
6445 LPFC_CTL_PORT_ER2_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006446 phba->sli4_hba.u.if_type2.CTRLregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006447 phba->sli4_hba.conf_regs_memmap_p +
6448 LPFC_CTL_PORT_CTL_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006449 phba->sli4_hba.u.if_type2.STATUSregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006450 phba->sli4_hba.conf_regs_memmap_p +
6451 LPFC_CTL_PORT_STA_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006452 phba->sli4_hba.SLIINTFregaddr =
6453 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
6454 phba->sli4_hba.PSMPHRregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006455 phba->sli4_hba.conf_regs_memmap_p +
6456 LPFC_CTL_PORT_SEM_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006457 phba->sli4_hba.RQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05006458 phba->sli4_hba.conf_regs_memmap_p +
6459 LPFC_ULP0_RQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05006460 phba->sli4_hba.WQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05006461 phba->sli4_hba.conf_regs_memmap_p +
6462 LPFC_ULP0_WQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05006463 phba->sli4_hba.EQCQDBregaddr =
6464 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
6465 phba->sli4_hba.MQDBregaddr =
6466 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
6467 phba->sli4_hba.BMBXregaddr =
6468 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
6469 break;
6470 case LPFC_SLI_INTF_IF_TYPE_1:
6471 default:
6472 dev_printk(KERN_ERR, &phba->pcidev->dev,
6473 "FATAL - unsupported SLI4 interface type - %d\n",
6474 if_type);
6475 break;
6476 }
James Smartda0436e2009-05-22 14:51:39 -04006477}
6478
6479/**
6480 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
6481 * @phba: pointer to lpfc hba data structure.
6482 *
6483 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
6484 * memory map.
6485 **/
6486static void
6487lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
6488{
James Smart2fcee4b2010-12-15 17:57:46 -05006489 phba->sli4_hba.PSMPHRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
6490 LPFC_SLIPORT_IF0_SMPHR;
James Smartda0436e2009-05-22 14:51:39 -04006491 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006492 LPFC_HST_ISR0;
James Smartda0436e2009-05-22 14:51:39 -04006493 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006494 LPFC_HST_IMR0;
James Smartda0436e2009-05-22 14:51:39 -04006495 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006496 LPFC_HST_ISCR0;
James Smartda0436e2009-05-22 14:51:39 -04006497}
6498
6499/**
6500 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
6501 * @phba: pointer to lpfc hba data structure.
6502 * @vf: virtual function number
6503 *
6504 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
6505 * based on the given viftual function number, @vf.
6506 *
6507 * Return 0 if successful, otherwise -ENODEV.
6508 **/
6509static int
6510lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
6511{
6512 if (vf > LPFC_VIR_FUNC_MAX)
6513 return -ENODEV;
6514
6515 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05006516 vf * LPFC_VFR_PAGE_SIZE +
6517 LPFC_ULP0_RQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04006518 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05006519 vf * LPFC_VFR_PAGE_SIZE +
6520 LPFC_ULP0_WQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04006521 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6522 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
6523 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6524 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
6525 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6526 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
6527 return 0;
6528}
6529
6530/**
6531 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
6532 * @phba: pointer to lpfc hba data structure.
6533 *
6534 * This routine is invoked to create the bootstrap mailbox
6535 * region consistent with the SLI-4 interface spec. This
6536 * routine allocates all memory necessary to communicate
6537 * mailbox commands to the port and sets up all alignment
6538 * needs. No locks are expected to be held when calling
6539 * this routine.
6540 *
6541 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006542 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04006543 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04006544 **/
6545static int
6546lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
6547{
6548 uint32_t bmbx_size;
6549 struct lpfc_dmabuf *dmabuf;
6550 struct dma_address *dma_address;
6551 uint32_t pa_addr;
6552 uint64_t phys_addr;
6553
6554 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
6555 if (!dmabuf)
6556 return -ENOMEM;
6557
6558 /*
6559 * The bootstrap mailbox region is comprised of 2 parts
6560 * plus an alignment restriction of 16 bytes.
6561 */
6562 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
6563 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
6564 bmbx_size,
6565 &dmabuf->phys,
6566 GFP_KERNEL);
6567 if (!dmabuf->virt) {
6568 kfree(dmabuf);
6569 return -ENOMEM;
6570 }
6571 memset(dmabuf->virt, 0, bmbx_size);
6572
6573 /*
6574 * Initialize the bootstrap mailbox pointers now so that the register
6575 * operations are simple later. The mailbox dma address is required
6576 * to be 16-byte aligned. Also align the virtual memory as each
6577 * maibox is copied into the bmbx mailbox region before issuing the
6578 * command to the port.
6579 */
6580 phba->sli4_hba.bmbx.dmabuf = dmabuf;
6581 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
6582
6583 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
6584 LPFC_ALIGN_16_BYTE);
6585 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
6586 LPFC_ALIGN_16_BYTE);
6587
6588 /*
6589 * Set the high and low physical addresses now. The SLI4 alignment
6590 * requirement is 16 bytes and the mailbox is posted to the port
6591 * as two 30-bit addresses. The other data is a bit marking whether
6592 * the 30-bit address is the high or low address.
6593 * Upcast bmbx aphys to 64bits so shift instruction compiles
6594 * clean on 32 bit machines.
6595 */
6596 dma_address = &phba->sli4_hba.bmbx.dma_address;
6597 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
6598 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
6599 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
6600 LPFC_BMBX_BIT1_ADDR_HI);
6601
6602 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
6603 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
6604 LPFC_BMBX_BIT1_ADDR_LO);
6605 return 0;
6606}
6607
6608/**
6609 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
6610 * @phba: pointer to lpfc hba data structure.
6611 *
6612 * This routine is invoked to teardown the bootstrap mailbox
6613 * region and release all host resources. This routine requires
6614 * the caller to ensure all mailbox commands recovered, no
6615 * additional mailbox comands are sent, and interrupts are disabled
6616 * before calling this routine.
6617 *
6618 **/
6619static void
6620lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
6621{
6622 dma_free_coherent(&phba->pcidev->dev,
6623 phba->sli4_hba.bmbx.bmbx_size,
6624 phba->sli4_hba.bmbx.dmabuf->virt,
6625 phba->sli4_hba.bmbx.dmabuf->phys);
6626
6627 kfree(phba->sli4_hba.bmbx.dmabuf);
6628 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
6629}
6630
6631/**
6632 * lpfc_sli4_read_config - Get the config parameters.
6633 * @phba: pointer to lpfc hba data structure.
6634 *
6635 * This routine is invoked to read the configuration parameters from the HBA.
6636 * The configuration parameters are used to set the base and maximum values
6637 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
6638 * allocation for the port.
6639 *
6640 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006641 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006642 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006643 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006644 **/
James Smartff78d8f2011-12-13 13:21:35 -05006645int
James Smartda0436e2009-05-22 14:51:39 -04006646lpfc_sli4_read_config(struct lpfc_hba *phba)
6647{
6648 LPFC_MBOXQ_t *pmb;
6649 struct lpfc_mbx_read_config *rd_config;
James Smart912e3ac2011-05-24 11:42:11 -04006650 union lpfc_sli4_cfg_shdr *shdr;
6651 uint32_t shdr_status, shdr_add_status;
6652 struct lpfc_mbx_get_func_cfg *get_func_cfg;
6653 struct lpfc_rsrc_desc_fcfcoe *desc;
James Smart8aa134a2012-08-14 14:25:29 -04006654 char *pdesc_0;
James Smart912e3ac2011-05-24 11:42:11 -04006655 uint32_t desc_count;
James Smart8aa134a2012-08-14 14:25:29 -04006656 int length, i, rc = 0, rc2;
James Smartda0436e2009-05-22 14:51:39 -04006657
6658 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6659 if (!pmb) {
6660 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6661 "2011 Unable to allocate memory for issuing "
6662 "SLI_CONFIG_SPECIAL mailbox command\n");
6663 return -ENOMEM;
6664 }
6665
6666 lpfc_read_config(phba, pmb);
6667
6668 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6669 if (rc != MBX_SUCCESS) {
6670 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6671 "2012 Mailbox failed , mbxCmd x%x "
6672 "READ_CONFIG, mbxStatus x%x\n",
6673 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6674 bf_get(lpfc_mqe_status, &pmb->u.mqe));
6675 rc = -EIO;
6676 } else {
6677 rd_config = &pmb->u.mqe.un.rd_config;
James Smartff78d8f2011-12-13 13:21:35 -05006678 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
6679 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
6680 phba->sli4_hba.lnk_info.lnk_tp =
6681 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
6682 phba->sli4_hba.lnk_info.lnk_no =
6683 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
6684 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6685 "3081 lnk_type:%d, lnk_numb:%d\n",
6686 phba->sli4_hba.lnk_info.lnk_tp,
6687 phba->sli4_hba.lnk_info.lnk_no);
6688 } else
6689 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6690 "3082 Mailbox (x%x) returned ldv:x0\n",
6691 bf_get(lpfc_mqe_command, &pmb->u.mqe));
James Smart6d368e52011-05-24 11:44:12 -04006692 phba->sli4_hba.extents_in_use =
6693 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04006694 phba->sli4_hba.max_cfg_param.max_xri =
6695 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
6696 phba->sli4_hba.max_cfg_param.xri_base =
6697 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
6698 phba->sli4_hba.max_cfg_param.max_vpi =
6699 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
6700 phba->sli4_hba.max_cfg_param.vpi_base =
6701 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
6702 phba->sli4_hba.max_cfg_param.max_rpi =
6703 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
6704 phba->sli4_hba.max_cfg_param.rpi_base =
6705 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
6706 phba->sli4_hba.max_cfg_param.max_vfi =
6707 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
6708 phba->sli4_hba.max_cfg_param.vfi_base =
6709 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
6710 phba->sli4_hba.max_cfg_param.max_fcfi =
6711 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04006712 phba->sli4_hba.max_cfg_param.max_eq =
6713 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
6714 phba->sli4_hba.max_cfg_param.max_rq =
6715 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
6716 phba->sli4_hba.max_cfg_param.max_wq =
6717 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
6718 phba->sli4_hba.max_cfg_param.max_cq =
6719 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
6720 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
6721 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
6722 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
6723 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
James Smart5ffc2662009-11-18 15:39:44 -05006724 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
6725 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04006726 phba->max_vports = phba->max_vpi;
6727 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart6d368e52011-05-24 11:44:12 -04006728 "2003 cfg params Extents? %d "
6729 "XRI(B:%d M:%d), "
James Smartda0436e2009-05-22 14:51:39 -04006730 "VPI(B:%d M:%d) "
6731 "VFI(B:%d M:%d) "
6732 "RPI(B:%d M:%d) "
James Smart6d368e52011-05-24 11:44:12 -04006733 "FCFI(Count:%d)\n",
6734 phba->sli4_hba.extents_in_use,
James Smartda0436e2009-05-22 14:51:39 -04006735 phba->sli4_hba.max_cfg_param.xri_base,
6736 phba->sli4_hba.max_cfg_param.max_xri,
6737 phba->sli4_hba.max_cfg_param.vpi_base,
6738 phba->sli4_hba.max_cfg_param.max_vpi,
6739 phba->sli4_hba.max_cfg_param.vfi_base,
6740 phba->sli4_hba.max_cfg_param.max_vfi,
6741 phba->sli4_hba.max_cfg_param.rpi_base,
6742 phba->sli4_hba.max_cfg_param.max_rpi,
James Smartda0436e2009-05-22 14:51:39 -04006743 phba->sli4_hba.max_cfg_param.max_fcfi);
6744 }
James Smart912e3ac2011-05-24 11:42:11 -04006745
6746 if (rc)
6747 goto read_cfg_out;
James Smartda0436e2009-05-22 14:51:39 -04006748
6749 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -04006750 length = phba->sli4_hba.max_cfg_param.max_xri -
6751 lpfc_sli4_get_els_iocb_cnt(phba);
6752 if (phba->cfg_hba_queue_depth > length) {
6753 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6754 "3361 HBA queue depth changed from %d to %d\n",
6755 phba->cfg_hba_queue_depth, length);
6756 phba->cfg_hba_queue_depth = length;
6757 }
James Smart912e3ac2011-05-24 11:42:11 -04006758
6759 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
6760 LPFC_SLI_INTF_IF_TYPE_2)
6761 goto read_cfg_out;
6762
6763 /* get the pf# and vf# for SLI4 if_type 2 port */
6764 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
6765 sizeof(struct lpfc_sli4_cfg_mhdr));
6766 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
6767 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
6768 length, LPFC_SLI4_MBX_EMBED);
6769
James Smart8aa134a2012-08-14 14:25:29 -04006770 rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart912e3ac2011-05-24 11:42:11 -04006771 shdr = (union lpfc_sli4_cfg_shdr *)
6772 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
6773 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6774 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart8aa134a2012-08-14 14:25:29 -04006775 if (rc2 || shdr_status || shdr_add_status) {
James Smart912e3ac2011-05-24 11:42:11 -04006776 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6777 "3026 Mailbox failed , mbxCmd x%x "
6778 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
6779 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6780 bf_get(lpfc_mqe_status, &pmb->u.mqe));
James Smart912e3ac2011-05-24 11:42:11 -04006781 goto read_cfg_out;
6782 }
6783
6784 /* search for fc_fcoe resrouce descriptor */
6785 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
6786 desc_count = get_func_cfg->func_cfg.rsrc_desc_count;
6787
James Smart8aa134a2012-08-14 14:25:29 -04006788 pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
6789 desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
6790 length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
6791 if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
6792 length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
6793 else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
6794 goto read_cfg_out;
6795
James Smart912e3ac2011-05-24 11:42:11 -04006796 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
James Smart8aa134a2012-08-14 14:25:29 -04006797 desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
James Smart912e3ac2011-05-24 11:42:11 -04006798 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
James Smart8aa134a2012-08-14 14:25:29 -04006799 bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
James Smart912e3ac2011-05-24 11:42:11 -04006800 phba->sli4_hba.iov.pf_number =
6801 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
6802 phba->sli4_hba.iov.vf_number =
6803 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
6804 break;
6805 }
6806 }
6807
6808 if (i < LPFC_RSRC_DESC_MAX_NUM)
6809 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6810 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
6811 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
6812 phba->sli4_hba.iov.vf_number);
James Smart8aa134a2012-08-14 14:25:29 -04006813 else
James Smart912e3ac2011-05-24 11:42:11 -04006814 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6815 "3028 GET_FUNCTION_CONFIG: failed to find "
6816 "Resrouce Descriptor:x%x\n",
6817 LPFC_RSRC_DESC_TYPE_FCFCOE);
James Smart912e3ac2011-05-24 11:42:11 -04006818
6819read_cfg_out:
6820 mempool_free(pmb, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04006821 return rc;
6822}
6823
6824/**
James Smart2fcee4b2010-12-15 17:57:46 -05006825 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
James Smartda0436e2009-05-22 14:51:39 -04006826 * @phba: pointer to lpfc hba data structure.
6827 *
James Smart2fcee4b2010-12-15 17:57:46 -05006828 * This routine is invoked to setup the port-side endian order when
6829 * the port if_type is 0. This routine has no function for other
6830 * if_types.
James Smartda0436e2009-05-22 14:51:39 -04006831 *
6832 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006833 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006834 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006835 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006836 **/
6837static int
6838lpfc_setup_endian_order(struct lpfc_hba *phba)
6839{
6840 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05006841 uint32_t if_type, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04006842 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
6843 HOST_ENDIAN_HIGH_WORD1};
6844
James Smart2fcee4b2010-12-15 17:57:46 -05006845 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
6846 switch (if_type) {
6847 case LPFC_SLI_INTF_IF_TYPE_0:
6848 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6849 GFP_KERNEL);
6850 if (!mboxq) {
6851 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6852 "0492 Unable to allocate memory for "
6853 "issuing SLI_CONFIG_SPECIAL mailbox "
6854 "command\n");
6855 return -ENOMEM;
6856 }
James Smartda0436e2009-05-22 14:51:39 -04006857
James Smart2fcee4b2010-12-15 17:57:46 -05006858 /*
6859 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
6860 * two words to contain special data values and no other data.
6861 */
6862 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
6863 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
6864 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6865 if (rc != MBX_SUCCESS) {
6866 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6867 "0493 SLI_CONFIG_SPECIAL mailbox "
6868 "failed with status x%x\n",
6869 rc);
6870 rc = -EIO;
6871 }
6872 mempool_free(mboxq, phba->mbox_mem_pool);
6873 break;
6874 case LPFC_SLI_INTF_IF_TYPE_2:
6875 case LPFC_SLI_INTF_IF_TYPE_1:
6876 default:
6877 break;
James Smartda0436e2009-05-22 14:51:39 -04006878 }
James Smartda0436e2009-05-22 14:51:39 -04006879 return rc;
6880}
6881
6882/**
James Smart5350d872011-10-10 21:33:49 -04006883 * lpfc_sli4_queue_verify - Verify and update EQ and CQ counts
James Smartda0436e2009-05-22 14:51:39 -04006884 * @phba: pointer to lpfc hba data structure.
6885 *
James Smart5350d872011-10-10 21:33:49 -04006886 * This routine is invoked to check the user settable queue counts for EQs and
6887 * CQs. after this routine is called the counts will be set to valid values that
6888 * adhere to the constraints of the system's interrupt vectors and the port's
6889 * queue resources.
James Smartda0436e2009-05-22 14:51:39 -04006890 *
6891 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006892 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006893 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04006894 **/
6895static int
James Smart5350d872011-10-10 21:33:49 -04006896lpfc_sli4_queue_verify(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04006897{
James Smart67d12732012-08-03 12:36:13 -04006898 int cfg_fcp_io_channel;
James Smart90695ee2012-08-14 14:25:36 -04006899 uint32_t cpu;
6900 uint32_t i = 0;
James Smart1ba981f2014-02-20 09:56:45 -05006901 int fof_vectors = phba->cfg_fof ? 1 : 0;
James Smartda0436e2009-05-22 14:51:39 -04006902
6903 /*
James Smart67d12732012-08-03 12:36:13 -04006904 * Sanity check for configured queue parameters against the run-time
James Smartda0436e2009-05-22 14:51:39 -04006905 * device parameters
6906 */
6907
James Smart67d12732012-08-03 12:36:13 -04006908 /* Sanity check on HBA EQ parameters */
6909 cfg_fcp_io_channel = phba->cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04006910
James Smart7bb03bb2013-04-17 20:19:16 -04006911 /* It doesn't make sense to have more io channels then online CPUs */
6912 for_each_present_cpu(cpu) {
6913 if (cpu_online(cpu))
6914 i++;
James Smart90695ee2012-08-14 14:25:36 -04006915 }
James Smart7bb03bb2013-04-17 20:19:16 -04006916 phba->sli4_hba.num_online_cpu = i;
James Smartb246de12013-05-31 17:03:07 -04006917 phba->sli4_hba.num_present_cpu = lpfc_present_cpu;
James Smart76fd07a2014-02-20 09:57:18 -05006918 phba->sli4_hba.curr_disp_cpu = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04006919
James Smart90695ee2012-08-14 14:25:36 -04006920 if (i < cfg_fcp_io_channel) {
James Smart82c3e9b2012-09-29 11:29:50 -04006921 lpfc_printf_log(phba,
6922 KERN_ERR, LOG_INIT,
James Smart90695ee2012-08-14 14:25:36 -04006923 "3188 Reducing IO channels to match number of "
James Smart7bb03bb2013-04-17 20:19:16 -04006924 "online CPUs: from %d to %d\n",
6925 cfg_fcp_io_channel, i);
James Smart90695ee2012-08-14 14:25:36 -04006926 cfg_fcp_io_channel = i;
6927 }
6928
James Smart1ba981f2014-02-20 09:56:45 -05006929 if (cfg_fcp_io_channel + fof_vectors >
James Smart67d12732012-08-03 12:36:13 -04006930 phba->sli4_hba.max_cfg_param.max_eq) {
James Smart82c3e9b2012-09-29 11:29:50 -04006931 if (phba->sli4_hba.max_cfg_param.max_eq <
6932 LPFC_FCP_IO_CHAN_MIN) {
James Smartda0436e2009-05-22 14:51:39 -04006933 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6934 "2574 Not enough EQs (%d) from the "
6935 "pci function for supporting FCP "
6936 "EQs (%d)\n",
6937 phba->sli4_hba.max_cfg_param.max_eq,
James Smart67d12732012-08-03 12:36:13 -04006938 phba->cfg_fcp_io_channel);
James Smartda0436e2009-05-22 14:51:39 -04006939 goto out_error;
6940 }
James Smart82c3e9b2012-09-29 11:29:50 -04006941 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6942 "2575 Reducing IO channels to match number of "
6943 "available EQs: from %d to %d\n",
6944 cfg_fcp_io_channel,
6945 phba->sli4_hba.max_cfg_param.max_eq);
James Smart1ba981f2014-02-20 09:56:45 -05006946 cfg_fcp_io_channel = phba->sli4_hba.max_cfg_param.max_eq -
6947 fof_vectors;
James Smartda0436e2009-05-22 14:51:39 -04006948 }
James Smart67d12732012-08-03 12:36:13 -04006949
James Smartda0436e2009-05-22 14:51:39 -04006950 /* The actual number of FCP event queues adopted */
James Smart67d12732012-08-03 12:36:13 -04006951 phba->cfg_fcp_io_channel = cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04006952
James Smartda0436e2009-05-22 14:51:39 -04006953 /* Get EQ depth from module parameter, fake the default for now */
6954 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
6955 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
6956
James Smart5350d872011-10-10 21:33:49 -04006957 /* Get CQ depth from module parameter, fake the default for now */
6958 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
6959 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
6960
6961 return 0;
6962out_error:
6963 return -ENOMEM;
6964}
6965
6966/**
6967 * lpfc_sli4_queue_create - Create all the SLI4 queues
6968 * @phba: pointer to lpfc hba data structure.
6969 *
6970 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
6971 * operation. For each SLI4 queue type, the parameters such as queue entry
6972 * count (queue depth) shall be taken from the module parameter. For now,
6973 * we just use some constant number as place holder.
6974 *
6975 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07006976 * 0 - successful
James Smart5350d872011-10-10 21:33:49 -04006977 * -ENOMEM - No availble memory
6978 * -EIO - The mailbox failed to complete successfully.
6979 **/
6980int
6981lpfc_sli4_queue_create(struct lpfc_hba *phba)
6982{
6983 struct lpfc_queue *qdesc;
James Smart67d12732012-08-03 12:36:13 -04006984 int idx;
James Smart5350d872011-10-10 21:33:49 -04006985
6986 /*
James Smart67d12732012-08-03 12:36:13 -04006987 * Create HBA Record arrays.
James Smart5350d872011-10-10 21:33:49 -04006988 */
James Smart67d12732012-08-03 12:36:13 -04006989 if (!phba->cfg_fcp_io_channel)
6990 return -ERANGE;
James Smart5350d872011-10-10 21:33:49 -04006991
James Smart67d12732012-08-03 12:36:13 -04006992 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
6993 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
6994 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
6995 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
6996 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
6997 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
6998
6999 phba->sli4_hba.hba_eq = kzalloc((sizeof(struct lpfc_queue *) *
7000 phba->cfg_fcp_io_channel), GFP_KERNEL);
7001 if (!phba->sli4_hba.hba_eq) {
James Smartda0436e2009-05-22 14:51:39 -04007002 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04007003 "2576 Failed allocate memory for "
7004 "fast-path EQ record array\n");
James Smartda0436e2009-05-22 14:51:39 -04007005 goto out_error;
7006 }
James Smart67d12732012-08-03 12:36:13 -04007007
7008 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
7009 phba->cfg_fcp_io_channel), GFP_KERNEL);
7010 if (!phba->sli4_hba.fcp_cq) {
7011 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7012 "2577 Failed allocate memory for fast-path "
7013 "CQ record array\n");
7014 goto out_error;
7015 }
7016
7017 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
7018 phba->cfg_fcp_io_channel), GFP_KERNEL);
7019 if (!phba->sli4_hba.fcp_wq) {
7020 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7021 "2578 Failed allocate memory for fast-path "
7022 "WQ record array\n");
7023 goto out_error;
7024 }
James Smartda0436e2009-05-22 14:51:39 -04007025
James Smart5350d872011-10-10 21:33:49 -04007026 /*
James Smart67d12732012-08-03 12:36:13 -04007027 * Since the first EQ can have multiple CQs associated with it,
7028 * this array is used to quickly see if we have a FCP fast-path
7029 * CQ match.
James Smart5350d872011-10-10 21:33:49 -04007030 */
James Smart67d12732012-08-03 12:36:13 -04007031 phba->sli4_hba.fcp_cq_map = kzalloc((sizeof(uint16_t) *
7032 phba->cfg_fcp_io_channel), GFP_KERNEL);
7033 if (!phba->sli4_hba.fcp_cq_map) {
7034 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7035 "2545 Failed allocate memory for fast-path "
7036 "CQ map\n");
7037 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007038 }
James Smart67d12732012-08-03 12:36:13 -04007039
7040 /*
7041 * Create HBA Event Queues (EQs). The cfg_fcp_io_channel specifies
7042 * how many EQs to create.
7043 */
7044 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7045
7046 /* Create EQs */
James Smartda0436e2009-05-22 14:51:39 -04007047 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
7048 phba->sli4_hba.eq_ecount);
7049 if (!qdesc) {
7050 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04007051 "0497 Failed allocate EQ (%d)\n", idx);
7052 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007053 }
James Smart67d12732012-08-03 12:36:13 -04007054 phba->sli4_hba.hba_eq[idx] = qdesc;
7055
7056 /* Create Fast Path FCP CQs */
7057 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7058 phba->sli4_hba.cq_ecount);
7059 if (!qdesc) {
7060 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7061 "0499 Failed allocate fast-path FCP "
7062 "CQ (%d)\n", idx);
7063 goto out_error;
7064 }
7065 phba->sli4_hba.fcp_cq[idx] = qdesc;
7066
7067 /* Create Fast Path FCP WQs */
7068 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
7069 phba->sli4_hba.wq_ecount);
7070 if (!qdesc) {
7071 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7072 "0503 Failed allocate fast-path FCP "
7073 "WQ (%d)\n", idx);
7074 goto out_error;
7075 }
7076 phba->sli4_hba.fcp_wq[idx] = qdesc;
James Smartda0436e2009-05-22 14:51:39 -04007077 }
7078
James Smart67d12732012-08-03 12:36:13 -04007079
James Smartda0436e2009-05-22 14:51:39 -04007080 /*
James Smart67d12732012-08-03 12:36:13 -04007081 * Create Slow Path Completion Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04007082 */
7083
James Smartda0436e2009-05-22 14:51:39 -04007084 /* Create slow-path Mailbox Command Complete Queue */
7085 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7086 phba->sli4_hba.cq_ecount);
7087 if (!qdesc) {
7088 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7089 "0500 Failed allocate slow-path mailbox CQ\n");
James Smart67d12732012-08-03 12:36:13 -04007090 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007091 }
7092 phba->sli4_hba.mbx_cq = qdesc;
7093
7094 /* Create slow-path ELS Complete Queue */
7095 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7096 phba->sli4_hba.cq_ecount);
7097 if (!qdesc) {
7098 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7099 "0501 Failed allocate slow-path ELS CQ\n");
James Smart67d12732012-08-03 12:36:13 -04007100 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007101 }
7102 phba->sli4_hba.els_cq = qdesc;
7103
James Smartda0436e2009-05-22 14:51:39 -04007104
James Smart5350d872011-10-10 21:33:49 -04007105 /*
James Smart67d12732012-08-03 12:36:13 -04007106 * Create Slow Path Work Queues (WQs)
James Smart5350d872011-10-10 21:33:49 -04007107 */
James Smartda0436e2009-05-22 14:51:39 -04007108
7109 /* Create Mailbox Command Queue */
James Smartda0436e2009-05-22 14:51:39 -04007110
7111 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
7112 phba->sli4_hba.mq_ecount);
7113 if (!qdesc) {
7114 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7115 "0505 Failed allocate slow-path MQ\n");
James Smart67d12732012-08-03 12:36:13 -04007116 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007117 }
7118 phba->sli4_hba.mbx_wq = qdesc;
7119
7120 /*
James Smart67d12732012-08-03 12:36:13 -04007121 * Create ELS Work Queues
James Smartda0436e2009-05-22 14:51:39 -04007122 */
James Smartda0436e2009-05-22 14:51:39 -04007123
7124 /* Create slow-path ELS Work Queue */
7125 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
7126 phba->sli4_hba.wq_ecount);
7127 if (!qdesc) {
7128 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7129 "0504 Failed allocate slow-path ELS WQ\n");
James Smart67d12732012-08-03 12:36:13 -04007130 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007131 }
7132 phba->sli4_hba.els_wq = qdesc;
7133
James Smartda0436e2009-05-22 14:51:39 -04007134 /*
7135 * Create Receive Queue (RQ)
7136 */
James Smartda0436e2009-05-22 14:51:39 -04007137
7138 /* Create Receive Queue for header */
7139 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
7140 phba->sli4_hba.rq_ecount);
7141 if (!qdesc) {
7142 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7143 "0506 Failed allocate receive HRQ\n");
James Smart67d12732012-08-03 12:36:13 -04007144 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007145 }
7146 phba->sli4_hba.hdr_rq = qdesc;
7147
7148 /* Create Receive Queue for data */
7149 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
7150 phba->sli4_hba.rq_ecount);
7151 if (!qdesc) {
7152 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7153 "0507 Failed allocate receive DRQ\n");
James Smart67d12732012-08-03 12:36:13 -04007154 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007155 }
7156 phba->sli4_hba.dat_rq = qdesc;
7157
James Smart1ba981f2014-02-20 09:56:45 -05007158 /* Create the Queues needed for Flash Optimized Fabric operations */
7159 if (phba->cfg_fof)
7160 lpfc_fof_queue_create(phba);
James Smartda0436e2009-05-22 14:51:39 -04007161 return 0;
7162
James Smartda0436e2009-05-22 14:51:39 -04007163out_error:
James Smart67d12732012-08-03 12:36:13 -04007164 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007165 return -ENOMEM;
7166}
7167
7168/**
7169 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
7170 * @phba: pointer to lpfc hba data structure.
7171 *
7172 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
7173 * operation.
7174 *
7175 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007176 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007177 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007178 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007179 **/
James Smart5350d872011-10-10 21:33:49 -04007180void
James Smartda0436e2009-05-22 14:51:39 -04007181lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
7182{
James Smart67d12732012-08-03 12:36:13 -04007183 int idx;
7184
James Smart1ba981f2014-02-20 09:56:45 -05007185 if (phba->cfg_fof)
7186 lpfc_fof_queue_destroy(phba);
7187
James Smart67d12732012-08-03 12:36:13 -04007188 if (phba->sli4_hba.hba_eq != NULL) {
7189 /* Release HBA event queue */
7190 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7191 if (phba->sli4_hba.hba_eq[idx] != NULL) {
7192 lpfc_sli4_queue_free(
7193 phba->sli4_hba.hba_eq[idx]);
7194 phba->sli4_hba.hba_eq[idx] = NULL;
7195 }
7196 }
7197 kfree(phba->sli4_hba.hba_eq);
7198 phba->sli4_hba.hba_eq = NULL;
7199 }
7200
7201 if (phba->sli4_hba.fcp_cq != NULL) {
7202 /* Release FCP completion queue */
7203 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7204 if (phba->sli4_hba.fcp_cq[idx] != NULL) {
7205 lpfc_sli4_queue_free(
7206 phba->sli4_hba.fcp_cq[idx]);
7207 phba->sli4_hba.fcp_cq[idx] = NULL;
7208 }
7209 }
7210 kfree(phba->sli4_hba.fcp_cq);
7211 phba->sli4_hba.fcp_cq = NULL;
7212 }
7213
7214 if (phba->sli4_hba.fcp_wq != NULL) {
7215 /* Release FCP work queue */
7216 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7217 if (phba->sli4_hba.fcp_wq[idx] != NULL) {
7218 lpfc_sli4_queue_free(
7219 phba->sli4_hba.fcp_wq[idx]);
7220 phba->sli4_hba.fcp_wq[idx] = NULL;
7221 }
7222 }
7223 kfree(phba->sli4_hba.fcp_wq);
7224 phba->sli4_hba.fcp_wq = NULL;
7225 }
7226
7227 /* Release FCP CQ mapping array */
7228 if (phba->sli4_hba.fcp_cq_map != NULL) {
7229 kfree(phba->sli4_hba.fcp_cq_map);
7230 phba->sli4_hba.fcp_cq_map = NULL;
7231 }
James Smartda0436e2009-05-22 14:51:39 -04007232
7233 /* Release mailbox command work queue */
James Smart67d12732012-08-03 12:36:13 -04007234 if (phba->sli4_hba.mbx_wq != NULL) {
7235 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
7236 phba->sli4_hba.mbx_wq = NULL;
7237 }
James Smartda0436e2009-05-22 14:51:39 -04007238
7239 /* Release ELS work queue */
James Smart67d12732012-08-03 12:36:13 -04007240 if (phba->sli4_hba.els_wq != NULL) {
7241 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
7242 phba->sli4_hba.els_wq = NULL;
7243 }
James Smartda0436e2009-05-22 14:51:39 -04007244
7245 /* Release unsolicited receive queue */
James Smart67d12732012-08-03 12:36:13 -04007246 if (phba->sli4_hba.hdr_rq != NULL) {
7247 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
7248 phba->sli4_hba.hdr_rq = NULL;
7249 }
7250 if (phba->sli4_hba.dat_rq != NULL) {
7251 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
7252 phba->sli4_hba.dat_rq = NULL;
7253 }
James Smartda0436e2009-05-22 14:51:39 -04007254
James Smartda0436e2009-05-22 14:51:39 -04007255 /* Release ELS complete queue */
James Smart67d12732012-08-03 12:36:13 -04007256 if (phba->sli4_hba.els_cq != NULL) {
7257 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
7258 phba->sli4_hba.els_cq = NULL;
7259 }
James Smartda0436e2009-05-22 14:51:39 -04007260
7261 /* Release mailbox command complete queue */
James Smart67d12732012-08-03 12:36:13 -04007262 if (phba->sli4_hba.mbx_cq != NULL) {
7263 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
7264 phba->sli4_hba.mbx_cq = NULL;
7265 }
James Smartda0436e2009-05-22 14:51:39 -04007266
7267 return;
7268}
7269
7270/**
7271 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
7272 * @phba: pointer to lpfc hba data structure.
7273 *
7274 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
7275 * operation.
7276 *
7277 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007278 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007279 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007280 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007281 **/
7282int
7283lpfc_sli4_queue_setup(struct lpfc_hba *phba)
7284{
James Smart2a76a282012-08-03 12:35:54 -04007285 struct lpfc_sli *psli = &phba->sli;
7286 struct lpfc_sli_ring *pring;
James Smartda0436e2009-05-22 14:51:39 -04007287 int rc = -ENOMEM;
7288 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
7289 int fcp_cq_index = 0;
James Smart962bc512013-01-03 15:44:00 -05007290 uint32_t shdr_status, shdr_add_status;
7291 union lpfc_sli4_cfg_shdr *shdr;
7292 LPFC_MBOXQ_t *mboxq;
7293 uint32_t length;
7294
7295 /* Check for dual-ULP support */
7296 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7297 if (!mboxq) {
7298 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7299 "3249 Unable to allocate memory for "
7300 "QUERY_FW_CFG mailbox command\n");
7301 return -ENOMEM;
7302 }
7303 length = (sizeof(struct lpfc_mbx_query_fw_config) -
7304 sizeof(struct lpfc_sli4_cfg_mhdr));
7305 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7306 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
7307 length, LPFC_SLI4_MBX_EMBED);
7308
7309 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7310
7311 shdr = (union lpfc_sli4_cfg_shdr *)
7312 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7313 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7314 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7315 if (shdr_status || shdr_add_status || rc) {
7316 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7317 "3250 QUERY_FW_CFG mailbox failed with status "
7318 "x%x add_status x%x, mbx status x%x\n",
7319 shdr_status, shdr_add_status, rc);
7320 if (rc != MBX_TIMEOUT)
7321 mempool_free(mboxq, phba->mbox_mem_pool);
7322 rc = -ENXIO;
7323 goto out_error;
7324 }
7325
7326 phba->sli4_hba.fw_func_mode =
7327 mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
7328 phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
7329 phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
7330 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7331 "3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
7332 "ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
7333 phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
7334
7335 if (rc != MBX_TIMEOUT)
7336 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04007337
7338 /*
James Smart67d12732012-08-03 12:36:13 -04007339 * Set up HBA Event Queues (EQs)
James Smartda0436e2009-05-22 14:51:39 -04007340 */
7341
James Smart67d12732012-08-03 12:36:13 -04007342 /* Set up HBA event queue */
7343 if (phba->cfg_fcp_io_channel && !phba->sli4_hba.hba_eq) {
James Smart2e90f4b2011-12-13 13:22:37 -05007344 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7345 "3147 Fast-path EQs not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007346 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007347 goto out_error;
James Smart2e90f4b2011-12-13 13:22:37 -05007348 }
James Smart67d12732012-08-03 12:36:13 -04007349 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
7350 if (!phba->sli4_hba.hba_eq[fcp_eqidx]) {
James Smartda0436e2009-05-22 14:51:39 -04007351 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7352 "0522 Fast-path EQ (%d) not "
7353 "allocated\n", fcp_eqidx);
James Smart1b511972011-12-13 13:23:09 -05007354 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007355 goto out_destroy_hba_eq;
James Smartda0436e2009-05-22 14:51:39 -04007356 }
James Smart67d12732012-08-03 12:36:13 -04007357 rc = lpfc_eq_create(phba, phba->sli4_hba.hba_eq[fcp_eqidx],
James Smartbf8dae82012-08-03 12:36:24 -04007358 (phba->cfg_fcp_imax / phba->cfg_fcp_io_channel));
James Smartda0436e2009-05-22 14:51:39 -04007359 if (rc) {
7360 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7361 "0523 Failed setup of fast-path EQ "
7362 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
James Smart67d12732012-08-03 12:36:13 -04007363 goto out_destroy_hba_eq;
James Smartda0436e2009-05-22 14:51:39 -04007364 }
7365 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04007366 "2584 HBA EQ setup: "
James Smartda0436e2009-05-22 14:51:39 -04007367 "queue[%d]-id=%d\n", fcp_eqidx,
James Smart67d12732012-08-03 12:36:13 -04007368 phba->sli4_hba.hba_eq[fcp_eqidx]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007369 }
7370
James Smart67d12732012-08-03 12:36:13 -04007371 /* Set up fast-path FCP Response Complete Queue */
7372 if (!phba->sli4_hba.fcp_cq) {
7373 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7374 "3148 Fast-path FCP CQ array not "
7375 "allocated\n");
7376 rc = -ENOMEM;
7377 goto out_destroy_hba_eq;
7378 }
7379
7380 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_io_channel; fcp_cqidx++) {
7381 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
7382 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7383 "0526 Fast-path FCP CQ (%d) not "
7384 "allocated\n", fcp_cqidx);
7385 rc = -ENOMEM;
7386 goto out_destroy_fcp_cq;
7387 }
7388 rc = lpfc_cq_create(phba, phba->sli4_hba.fcp_cq[fcp_cqidx],
7389 phba->sli4_hba.hba_eq[fcp_cqidx], LPFC_WCQ, LPFC_FCP);
7390 if (rc) {
7391 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7392 "0527 Failed setup of fast-path FCP "
7393 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
7394 goto out_destroy_fcp_cq;
7395 }
7396
7397 /* Setup fcp_cq_map for fast lookup */
7398 phba->sli4_hba.fcp_cq_map[fcp_cqidx] =
7399 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id;
7400
7401 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7402 "2588 FCP CQ setup: cq[%d]-id=%d, "
7403 "parent seq[%d]-id=%d\n",
7404 fcp_cqidx,
7405 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
7406 fcp_cqidx,
7407 phba->sli4_hba.hba_eq[fcp_cqidx]->queue_id);
7408 }
7409
7410 /* Set up fast-path FCP Work Queue */
7411 if (!phba->sli4_hba.fcp_wq) {
7412 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7413 "3149 Fast-path FCP WQ array not "
7414 "allocated\n");
7415 rc = -ENOMEM;
7416 goto out_destroy_fcp_cq;
7417 }
7418
7419 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_io_channel; fcp_wqidx++) {
7420 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
7421 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7422 "0534 Fast-path FCP WQ (%d) not "
7423 "allocated\n", fcp_wqidx);
7424 rc = -ENOMEM;
7425 goto out_destroy_fcp_wq;
7426 }
7427 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
7428 phba->sli4_hba.fcp_cq[fcp_wqidx],
7429 LPFC_FCP);
7430 if (rc) {
7431 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7432 "0535 Failed setup of fast-path FCP "
7433 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
7434 goto out_destroy_fcp_wq;
7435 }
7436
7437 /* Bind this WQ to the next FCP ring */
7438 pring = &psli->ring[MAX_SLI3_CONFIGURED_RINGS + fcp_wqidx];
7439 pring->sli.sli4.wqp = (void *)phba->sli4_hba.fcp_wq[fcp_wqidx];
7440 phba->sli4_hba.fcp_cq[fcp_wqidx]->pring = pring;
7441
7442 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7443 "2591 FCP WQ setup: wq[%d]-id=%d, "
7444 "parent cq[%d]-id=%d\n",
7445 fcp_wqidx,
7446 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
7447 fcp_cq_index,
7448 phba->sli4_hba.fcp_cq[fcp_wqidx]->queue_id);
7449 }
James Smartda0436e2009-05-22 14:51:39 -04007450 /*
7451 * Set up Complete Queues (CQs)
7452 */
7453
7454 /* Set up slow-path MBOX Complete Queue as the first CQ */
7455 if (!phba->sli4_hba.mbx_cq) {
7456 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7457 "0528 Mailbox CQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007458 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007459 goto out_destroy_fcp_wq;
James Smartda0436e2009-05-22 14:51:39 -04007460 }
James Smart67d12732012-08-03 12:36:13 -04007461 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq,
7462 phba->sli4_hba.hba_eq[0], LPFC_MCQ, LPFC_MBOX);
James Smartda0436e2009-05-22 14:51:39 -04007463 if (rc) {
7464 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7465 "0529 Failed setup of slow-path mailbox CQ: "
7466 "rc = 0x%x\n", rc);
James Smart67d12732012-08-03 12:36:13 -04007467 goto out_destroy_fcp_wq;
James Smartda0436e2009-05-22 14:51:39 -04007468 }
7469 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7470 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
7471 phba->sli4_hba.mbx_cq->queue_id,
James Smart67d12732012-08-03 12:36:13 -04007472 phba->sli4_hba.hba_eq[0]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007473
7474 /* Set up slow-path ELS Complete Queue */
7475 if (!phba->sli4_hba.els_cq) {
7476 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7477 "0530 ELS CQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007478 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04007479 goto out_destroy_mbx_cq;
7480 }
James Smart67d12732012-08-03 12:36:13 -04007481 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq,
7482 phba->sli4_hba.hba_eq[0], LPFC_WCQ, LPFC_ELS);
James Smartda0436e2009-05-22 14:51:39 -04007483 if (rc) {
7484 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7485 "0531 Failed setup of slow-path ELS CQ: "
7486 "rc = 0x%x\n", rc);
7487 goto out_destroy_mbx_cq;
7488 }
7489 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7490 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
7491 phba->sli4_hba.els_cq->queue_id,
James Smart67d12732012-08-03 12:36:13 -04007492 phba->sli4_hba.hba_eq[0]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007493
7494 /*
7495 * Set up all the Work Queues (WQs)
7496 */
7497
7498 /* Set up Mailbox Command Queue */
7499 if (!phba->sli4_hba.mbx_wq) {
7500 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7501 "0538 Slow-path MQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007502 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007503 goto out_destroy_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04007504 }
7505 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
7506 phba->sli4_hba.mbx_cq, LPFC_MBOX);
7507 if (rc) {
7508 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7509 "0539 Failed setup of slow-path MQ: "
7510 "rc = 0x%x\n", rc);
James Smart67d12732012-08-03 12:36:13 -04007511 goto out_destroy_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04007512 }
7513 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7514 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
7515 phba->sli4_hba.mbx_wq->queue_id,
7516 phba->sli4_hba.mbx_cq->queue_id);
7517
7518 /* Set up slow-path ELS Work Queue */
7519 if (!phba->sli4_hba.els_wq) {
7520 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7521 "0536 Slow-path ELS WQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007522 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04007523 goto out_destroy_mbx_wq;
7524 }
7525 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
7526 phba->sli4_hba.els_cq, LPFC_ELS);
7527 if (rc) {
7528 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7529 "0537 Failed setup of slow-path ELS WQ: "
7530 "rc = 0x%x\n", rc);
7531 goto out_destroy_mbx_wq;
7532 }
James Smart2a76a282012-08-03 12:35:54 -04007533
7534 /* Bind this WQ to the ELS ring */
7535 pring = &psli->ring[LPFC_ELS_RING];
7536 pring->sli.sli4.wqp = (void *)phba->sli4_hba.els_wq;
7537 phba->sli4_hba.els_cq->pring = pring;
7538
James Smartda0436e2009-05-22 14:51:39 -04007539 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7540 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
7541 phba->sli4_hba.els_wq->queue_id,
7542 phba->sli4_hba.els_cq->queue_id);
7543
James Smartda0436e2009-05-22 14:51:39 -04007544 /*
7545 * Create Receive Queue (RQ)
7546 */
7547 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
7548 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7549 "0540 Receive Queue not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007550 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007551 goto out_destroy_els_wq;
James Smartda0436e2009-05-22 14:51:39 -04007552 }
James Smart73d91e52011-10-10 21:32:10 -04007553
7554 lpfc_rq_adjust_repost(phba, phba->sli4_hba.hdr_rq, LPFC_ELS_HBQ);
7555 lpfc_rq_adjust_repost(phba, phba->sli4_hba.dat_rq, LPFC_ELS_HBQ);
7556
James Smartda0436e2009-05-22 14:51:39 -04007557 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04007558 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04007559 if (rc) {
7560 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7561 "0541 Failed setup of Receive Queue: "
7562 "rc = 0x%x\n", rc);
7563 goto out_destroy_fcp_wq;
7564 }
James Smart73d91e52011-10-10 21:32:10 -04007565
James Smartda0436e2009-05-22 14:51:39 -04007566 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7567 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
7568 "parent cq-id=%d\n",
7569 phba->sli4_hba.hdr_rq->queue_id,
7570 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04007571 phba->sli4_hba.els_cq->queue_id);
James Smart1ba981f2014-02-20 09:56:45 -05007572
7573 if (phba->cfg_fof) {
7574 rc = lpfc_fof_queue_setup(phba);
7575 if (rc) {
7576 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7577 "0549 Failed setup of FOF Queues: "
7578 "rc = 0x%x\n", rc);
7579 goto out_destroy_els_rq;
7580 }
7581 }
James Smartda0436e2009-05-22 14:51:39 -04007582 return 0;
7583
James Smart1ba981f2014-02-20 09:56:45 -05007584out_destroy_els_rq:
7585 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
James Smart2e90f4b2011-12-13 13:22:37 -05007586out_destroy_els_wq:
James Smartda0436e2009-05-22 14:51:39 -04007587 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7588out_destroy_mbx_wq:
7589 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
James Smart2e90f4b2011-12-13 13:22:37 -05007590out_destroy_els_cq:
James Smartda0436e2009-05-22 14:51:39 -04007591 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
7592out_destroy_mbx_cq:
7593 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
James Smart67d12732012-08-03 12:36:13 -04007594out_destroy_fcp_wq:
7595 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
7596 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
7597out_destroy_fcp_cq:
7598 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
7599 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
7600out_destroy_hba_eq:
James Smartda0436e2009-05-22 14:51:39 -04007601 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
James Smart67d12732012-08-03 12:36:13 -04007602 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[fcp_eqidx]);
James Smartda0436e2009-05-22 14:51:39 -04007603out_error:
7604 return rc;
7605}
7606
7607/**
7608 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
7609 * @phba: pointer to lpfc hba data structure.
7610 *
7611 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
7612 * operation.
7613 *
7614 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007615 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007616 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007617 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007618 **/
7619void
7620lpfc_sli4_queue_unset(struct lpfc_hba *phba)
7621{
7622 int fcp_qidx;
7623
James Smart1ba981f2014-02-20 09:56:45 -05007624 /* Unset the queues created for Flash Optimized Fabric operations */
7625 if (phba->cfg_fof)
7626 lpfc_fof_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007627 /* Unset mailbox command work queue */
7628 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
7629 /* Unset ELS work queue */
7630 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7631 /* Unset unsolicited receive queue */
7632 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
7633 /* Unset FCP work queue */
James Smart67d12732012-08-03 12:36:13 -04007634 if (phba->sli4_hba.fcp_wq) {
7635 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
7636 fcp_qidx++)
7637 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
7638 }
James Smartda0436e2009-05-22 14:51:39 -04007639 /* Unset mailbox command complete queue */
7640 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
7641 /* Unset ELS complete queue */
7642 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
James Smartda0436e2009-05-22 14:51:39 -04007643 /* Unset FCP response complete queue */
James Smart2e90f4b2011-12-13 13:22:37 -05007644 if (phba->sli4_hba.fcp_cq) {
James Smart67d12732012-08-03 12:36:13 -04007645 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
7646 fcp_qidx++)
James Smart2e90f4b2011-12-13 13:22:37 -05007647 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05007648 }
James Smartda0436e2009-05-22 14:51:39 -04007649 /* Unset fast-path event queue */
James Smart67d12732012-08-03 12:36:13 -04007650 if (phba->sli4_hba.hba_eq) {
7651 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
James Smart2e90f4b2011-12-13 13:22:37 -05007652 fcp_qidx++)
James Smart67d12732012-08-03 12:36:13 -04007653 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[fcp_qidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05007654 }
James Smartda0436e2009-05-22 14:51:39 -04007655}
7656
7657/**
7658 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
7659 * @phba: pointer to lpfc hba data structure.
7660 *
7661 * This routine is invoked to allocate and set up a pool of completion queue
7662 * events. The body of the completion queue event is a completion queue entry
7663 * CQE. For now, this pool is used for the interrupt service routine to queue
7664 * the following HBA completion queue events for the worker thread to process:
7665 * - Mailbox asynchronous events
7666 * - Receive queue completion unsolicited events
7667 * Later, this can be used for all the slow-path events.
7668 *
7669 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007670 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007671 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04007672 **/
7673static int
7674lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
7675{
7676 struct lpfc_cq_event *cq_event;
7677 int i;
7678
7679 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
7680 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
7681 if (!cq_event)
7682 goto out_pool_create_fail;
7683 list_add_tail(&cq_event->list,
7684 &phba->sli4_hba.sp_cqe_event_pool);
7685 }
7686 return 0;
7687
7688out_pool_create_fail:
7689 lpfc_sli4_cq_event_pool_destroy(phba);
7690 return -ENOMEM;
7691}
7692
7693/**
7694 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
7695 * @phba: pointer to lpfc hba data structure.
7696 *
7697 * This routine is invoked to free the pool of completion queue events at
7698 * driver unload time. Note that, it is the responsibility of the driver
7699 * cleanup routine to free all the outstanding completion-queue events
7700 * allocated from this pool back into the pool before invoking this routine
7701 * to destroy the pool.
7702 **/
7703static void
7704lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
7705{
7706 struct lpfc_cq_event *cq_event, *next_cq_event;
7707
7708 list_for_each_entry_safe(cq_event, next_cq_event,
7709 &phba->sli4_hba.sp_cqe_event_pool, list) {
7710 list_del(&cq_event->list);
7711 kfree(cq_event);
7712 }
7713}
7714
7715/**
7716 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
7717 * @phba: pointer to lpfc hba data structure.
7718 *
7719 * This routine is the lock free version of the API invoked to allocate a
7720 * completion-queue event from the free pool.
7721 *
7722 * Return: Pointer to the newly allocated completion-queue event if successful
7723 * NULL otherwise.
7724 **/
7725struct lpfc_cq_event *
7726__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
7727{
7728 struct lpfc_cq_event *cq_event = NULL;
7729
7730 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
7731 struct lpfc_cq_event, list);
7732 return cq_event;
7733}
7734
7735/**
7736 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
7737 * @phba: pointer to lpfc hba data structure.
7738 *
7739 * This routine is the lock version of the API invoked to allocate a
7740 * completion-queue event from the free pool.
7741 *
7742 * Return: Pointer to the newly allocated completion-queue event if successful
7743 * NULL otherwise.
7744 **/
7745struct lpfc_cq_event *
7746lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
7747{
7748 struct lpfc_cq_event *cq_event;
7749 unsigned long iflags;
7750
7751 spin_lock_irqsave(&phba->hbalock, iflags);
7752 cq_event = __lpfc_sli4_cq_event_alloc(phba);
7753 spin_unlock_irqrestore(&phba->hbalock, iflags);
7754 return cq_event;
7755}
7756
7757/**
7758 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
7759 * @phba: pointer to lpfc hba data structure.
7760 * @cq_event: pointer to the completion queue event to be freed.
7761 *
7762 * This routine is the lock free version of the API invoked to release a
7763 * completion-queue event back into the free pool.
7764 **/
7765void
7766__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
7767 struct lpfc_cq_event *cq_event)
7768{
7769 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
7770}
7771
7772/**
7773 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
7774 * @phba: pointer to lpfc hba data structure.
7775 * @cq_event: pointer to the completion queue event to be freed.
7776 *
7777 * This routine is the lock version of the API invoked to release a
7778 * completion-queue event back into the free pool.
7779 **/
7780void
7781lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
7782 struct lpfc_cq_event *cq_event)
7783{
7784 unsigned long iflags;
7785 spin_lock_irqsave(&phba->hbalock, iflags);
7786 __lpfc_sli4_cq_event_release(phba, cq_event);
7787 spin_unlock_irqrestore(&phba->hbalock, iflags);
7788}
7789
7790/**
7791 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
7792 * @phba: pointer to lpfc hba data structure.
7793 *
7794 * This routine is to free all the pending completion-queue events to the
7795 * back into the free pool for device reset.
7796 **/
7797static void
7798lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
7799{
7800 LIST_HEAD(cqelist);
7801 struct lpfc_cq_event *cqe;
7802 unsigned long iflags;
7803
7804 /* Retrieve all the pending WCQEs from pending WCQE lists */
7805 spin_lock_irqsave(&phba->hbalock, iflags);
7806 /* Pending FCP XRI abort events */
7807 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
7808 &cqelist);
7809 /* Pending ELS XRI abort events */
7810 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
7811 &cqelist);
7812 /* Pending asynnc events */
7813 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
7814 &cqelist);
7815 spin_unlock_irqrestore(&phba->hbalock, iflags);
7816
7817 while (!list_empty(&cqelist)) {
7818 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
7819 lpfc_sli4_cq_event_release(phba, cqe);
7820 }
7821}
7822
7823/**
7824 * lpfc_pci_function_reset - Reset pci function.
7825 * @phba: pointer to lpfc hba data structure.
7826 *
7827 * This routine is invoked to request a PCI function reset. It will destroys
7828 * all resources assigned to the PCI function which originates this request.
7829 *
7830 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007831 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007832 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007833 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007834 **/
7835int
7836lpfc_pci_function_reset(struct lpfc_hba *phba)
7837{
7838 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05007839 uint32_t rc = 0, if_type;
James Smartda0436e2009-05-22 14:51:39 -04007840 uint32_t shdr_status, shdr_add_status;
James Smart2fcee4b2010-12-15 17:57:46 -05007841 uint32_t rdy_chk, num_resets = 0, reset_again = 0;
James Smartda0436e2009-05-22 14:51:39 -04007842 union lpfc_sli4_cfg_shdr *shdr;
James Smart2fcee4b2010-12-15 17:57:46 -05007843 struct lpfc_register reg_data;
James Smart2b81f942012-03-01 22:37:18 -05007844 uint16_t devid;
James Smartda0436e2009-05-22 14:51:39 -04007845
James Smart2fcee4b2010-12-15 17:57:46 -05007846 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7847 switch (if_type) {
7848 case LPFC_SLI_INTF_IF_TYPE_0:
7849 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
7850 GFP_KERNEL);
7851 if (!mboxq) {
7852 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7853 "0494 Unable to allocate memory for "
7854 "issuing SLI_FUNCTION_RESET mailbox "
7855 "command\n");
7856 return -ENOMEM;
7857 }
7858
7859 /* Setup PCI function reset mailbox-ioctl command */
7860 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7861 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
7862 LPFC_SLI4_MBX_EMBED);
7863 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7864 shdr = (union lpfc_sli4_cfg_shdr *)
7865 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7866 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7867 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
7868 &shdr->response);
7869 if (rc != MBX_TIMEOUT)
7870 mempool_free(mboxq, phba->mbox_mem_pool);
7871 if (shdr_status || shdr_add_status || rc) {
7872 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7873 "0495 SLI_FUNCTION_RESET mailbox "
7874 "failed with status x%x add_status x%x,"
7875 " mbx status x%x\n",
7876 shdr_status, shdr_add_status, rc);
7877 rc = -ENXIO;
7878 }
7879 break;
7880 case LPFC_SLI_INTF_IF_TYPE_2:
7881 for (num_resets = 0;
7882 num_resets < MAX_IF_TYPE_2_RESETS;
7883 num_resets++) {
7884 reg_data.word0 = 0;
7885 bf_set(lpfc_sliport_ctrl_end, &reg_data,
7886 LPFC_SLIPORT_LITTLE_ENDIAN);
7887 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
7888 LPFC_SLIPORT_INIT_PORT);
7889 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
7890 CTRLregaddr);
James Smart8fcb8ac2012-03-01 22:35:58 -05007891 /* flush */
James Smart2b81f942012-03-01 22:37:18 -05007892 pci_read_config_word(phba->pcidev,
7893 PCI_DEVICE_ID, &devid);
James Smart2fcee4b2010-12-15 17:57:46 -05007894 /*
7895 * Poll the Port Status Register and wait for RDY for
7896 * up to 10 seconds. If the port doesn't respond, treat
7897 * it as an error. If the port responds with RN, start
7898 * the loop again.
7899 */
7900 for (rdy_chk = 0; rdy_chk < 1000; rdy_chk++) {
James Smart73d91e52011-10-10 21:32:10 -04007901 msleep(10);
James Smart9940b972011-03-11 16:06:12 -05007902 if (lpfc_readl(phba->sli4_hba.u.if_type2.
7903 STATUSregaddr, &reg_data.word0)) {
7904 rc = -ENODEV;
James Smart73d91e52011-10-10 21:32:10 -04007905 goto out;
James Smart9940b972011-03-11 16:06:12 -05007906 }
James Smart6b5151f2012-01-18 16:24:06 -05007907 if (bf_get(lpfc_sliport_status_rn, &reg_data))
7908 reset_again++;
James Smart2fcee4b2010-12-15 17:57:46 -05007909 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
7910 break;
James Smart2fcee4b2010-12-15 17:57:46 -05007911 }
7912
7913 /*
7914 * If the port responds to the init request with
7915 * reset needed, delay for a bit and restart the loop.
7916 */
James Smart6b5151f2012-01-18 16:24:06 -05007917 if (reset_again && (rdy_chk < 1000)) {
James Smart2fcee4b2010-12-15 17:57:46 -05007918 msleep(10);
7919 reset_again = 0;
7920 continue;
7921 }
7922
7923 /* Detect any port errors. */
James Smart2fcee4b2010-12-15 17:57:46 -05007924 if ((bf_get(lpfc_sliport_status_err, &reg_data)) ||
7925 (rdy_chk >= 1000)) {
7926 phba->work_status[0] = readl(
7927 phba->sli4_hba.u.if_type2.ERR1regaddr);
7928 phba->work_status[1] = readl(
7929 phba->sli4_hba.u.if_type2.ERR2regaddr);
7930 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05007931 "2890 Port error detected during port "
James Smart8a9d2e82012-05-09 21:16:12 -04007932 "reset(%d): wait_tmo:%d ms, "
7933 "port status reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05007934 "error 1=0x%x, error 2=0x%x\n",
James Smart8a9d2e82012-05-09 21:16:12 -04007935 num_resets, rdy_chk*10,
7936 reg_data.word0,
James Smart2fcee4b2010-12-15 17:57:46 -05007937 phba->work_status[0],
7938 phba->work_status[1]);
7939 rc = -ENODEV;
7940 }
7941
7942 /*
7943 * Terminate the outer loop provided the Port indicated
7944 * ready within 10 seconds.
7945 */
7946 if (rdy_chk < 1000)
7947 break;
7948 }
James Smart05580562011-05-24 11:40:48 -04007949 /* delay driver action following IF_TYPE_2 function reset */
7950 msleep(100);
James Smart2fcee4b2010-12-15 17:57:46 -05007951 break;
7952 case LPFC_SLI_INTF_IF_TYPE_1:
7953 default:
7954 break;
James Smartda0436e2009-05-22 14:51:39 -04007955 }
7956
James Smart73d91e52011-10-10 21:32:10 -04007957out:
James Smart2fcee4b2010-12-15 17:57:46 -05007958 /* Catch the not-ready port failure after a port reset. */
James Smart229adb02013-04-17 20:16:51 -04007959 if (num_resets >= MAX_IF_TYPE_2_RESETS) {
7960 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7961 "3317 HBA not functional: IP Reset Failed "
7962 "after (%d) retries, try: "
7963 "echo fw_reset > board_mode\n", num_resets);
James Smart2fcee4b2010-12-15 17:57:46 -05007964 rc = -ENODEV;
James Smart229adb02013-04-17 20:16:51 -04007965 }
James Smart2fcee4b2010-12-15 17:57:46 -05007966
James Smartda0436e2009-05-22 14:51:39 -04007967 return rc;
7968}
7969
7970/**
James Smartda0436e2009-05-22 14:51:39 -04007971 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
7972 * @phba: pointer to lpfc hba data structure.
7973 *
7974 * This routine is invoked to set up the PCI device memory space for device
7975 * with SLI-4 interface spec.
7976 *
7977 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007978 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007979 * other values - error
7980 **/
7981static int
7982lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
7983{
7984 struct pci_dev *pdev;
7985 unsigned long bar0map_len, bar1map_len, bar2map_len;
7986 int error = -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -05007987 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04007988
7989 /* Obtain PCI device reference */
7990 if (!phba->pcidev)
7991 return error;
7992 else
7993 pdev = phba->pcidev;
7994
7995 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05007996 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
7997 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
7998 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
7999 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smartda0436e2009-05-22 14:51:39 -04008000 return error;
Michael Reed8e685972009-09-18 12:02:05 -05008001 }
8002 }
James Smartda0436e2009-05-22 14:51:39 -04008003
James Smart2fcee4b2010-12-15 17:57:46 -05008004 /*
8005 * The BARs and register set definitions and offset locations are
8006 * dependent on the if_type.
8007 */
8008 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
8009 &phba->sli4_hba.sli_intf.word0)) {
8010 return error;
8011 }
8012
8013 /* There is no SLI3 failback for SLI4 devices. */
8014 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
8015 LPFC_SLI_INTF_VALID) {
8016 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8017 "2894 SLI_INTF reg contents invalid "
8018 "sli_intf reg 0x%x\n",
8019 phba->sli4_hba.sli_intf.word0);
8020 return error;
8021 }
8022
8023 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
8024 /*
8025 * Get the bus address of SLI4 device Bar regions and the
8026 * number of bytes required by each mapping. The mapping of the
8027 * particular PCI BARs regions is dependent on the type of
8028 * SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -04008029 */
James Smartf5ca6f22013-09-06 12:21:09 -04008030 if (pci_resource_start(pdev, PCI_64BIT_BAR0)) {
8031 phba->pci_bar0_map = pci_resource_start(pdev, PCI_64BIT_BAR0);
8032 bar0map_len = pci_resource_len(pdev, PCI_64BIT_BAR0);
James Smart2fcee4b2010-12-15 17:57:46 -05008033
8034 /*
8035 * Map SLI4 PCI Config Space Register base to a kernel virtual
8036 * addr
8037 */
8038 phba->sli4_hba.conf_regs_memmap_p =
8039 ioremap(phba->pci_bar0_map, bar0map_len);
8040 if (!phba->sli4_hba.conf_regs_memmap_p) {
8041 dev_printk(KERN_ERR, &pdev->dev,
8042 "ioremap failed for SLI4 PCI config "
8043 "registers.\n");
8044 goto out;
8045 }
James Smartf5ca6f22013-09-06 12:21:09 -04008046 phba->pci_bar0_memmap_p = phba->sli4_hba.conf_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05008047 /* Set up BAR0 PCI config space register memory map */
8048 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smart1dfb5a42010-02-12 14:40:50 -05008049 } else {
8050 phba->pci_bar0_map = pci_resource_start(pdev, 1);
8051 bar0map_len = pci_resource_len(pdev, 1);
James Smart2fcee4b2010-12-15 17:57:46 -05008052 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8053 dev_printk(KERN_ERR, &pdev->dev,
8054 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
8055 goto out;
8056 }
8057 phba->sli4_hba.conf_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04008058 ioremap(phba->pci_bar0_map, bar0map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05008059 if (!phba->sli4_hba.conf_regs_memmap_p) {
8060 dev_printk(KERN_ERR, &pdev->dev,
8061 "ioremap failed for SLI4 PCI config "
8062 "registers.\n");
8063 goto out;
8064 }
8065 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smartda0436e2009-05-22 14:51:39 -04008066 }
8067
James Smartc31098c2011-04-16 11:03:33 -04008068 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
James Smartf5ca6f22013-09-06 12:21:09 -04008069 (pci_resource_start(pdev, PCI_64BIT_BAR2))) {
James Smart2fcee4b2010-12-15 17:57:46 -05008070 /*
8071 * Map SLI4 if type 0 HBA Control Register base to a kernel
8072 * virtual address and setup the registers.
8073 */
James Smartf5ca6f22013-09-06 12:21:09 -04008074 phba->pci_bar1_map = pci_resource_start(pdev, PCI_64BIT_BAR2);
8075 bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
James Smart2fcee4b2010-12-15 17:57:46 -05008076 phba->sli4_hba.ctrl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04008077 ioremap(phba->pci_bar1_map, bar1map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05008078 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
8079 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04008080 "ioremap failed for SLI4 HBA control registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05008081 goto out_iounmap_conf;
8082 }
James Smartf5ca6f22013-09-06 12:21:09 -04008083 phba->pci_bar2_memmap_p = phba->sli4_hba.ctrl_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05008084 lpfc_sli4_bar1_register_memmap(phba);
James Smartda0436e2009-05-22 14:51:39 -04008085 }
8086
James Smartc31098c2011-04-16 11:03:33 -04008087 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
James Smartf5ca6f22013-09-06 12:21:09 -04008088 (pci_resource_start(pdev, PCI_64BIT_BAR4))) {
James Smart2fcee4b2010-12-15 17:57:46 -05008089 /*
8090 * Map SLI4 if type 0 HBA Doorbell Register base to a kernel
8091 * virtual address and setup the registers.
8092 */
James Smartf5ca6f22013-09-06 12:21:09 -04008093 phba->pci_bar2_map = pci_resource_start(pdev, PCI_64BIT_BAR4);
8094 bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
James Smart2fcee4b2010-12-15 17:57:46 -05008095 phba->sli4_hba.drbl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04008096 ioremap(phba->pci_bar2_map, bar2map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05008097 if (!phba->sli4_hba.drbl_regs_memmap_p) {
8098 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04008099 "ioremap failed for SLI4 HBA doorbell registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05008100 goto out_iounmap_ctrl;
8101 }
James Smartf5ca6f22013-09-06 12:21:09 -04008102 phba->pci_bar4_memmap_p = phba->sli4_hba.drbl_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05008103 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
8104 if (error)
8105 goto out_iounmap_all;
James Smartda0436e2009-05-22 14:51:39 -04008106 }
8107
James Smartda0436e2009-05-22 14:51:39 -04008108 return 0;
8109
8110out_iounmap_all:
8111 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
8112out_iounmap_ctrl:
8113 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
8114out_iounmap_conf:
8115 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8116out:
8117 return error;
8118}
8119
8120/**
8121 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
8122 * @phba: pointer to lpfc hba data structure.
8123 *
8124 * This routine is invoked to unset the PCI device memory space for device
8125 * with SLI-4 interface spec.
8126 **/
8127static void
8128lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
8129{
James Smart2e90f4b2011-12-13 13:22:37 -05008130 uint32_t if_type;
8131 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smartda0436e2009-05-22 14:51:39 -04008132
James Smart2e90f4b2011-12-13 13:22:37 -05008133 switch (if_type) {
8134 case LPFC_SLI_INTF_IF_TYPE_0:
8135 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
8136 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
8137 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8138 break;
8139 case LPFC_SLI_INTF_IF_TYPE_2:
8140 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8141 break;
8142 case LPFC_SLI_INTF_IF_TYPE_1:
8143 default:
8144 dev_printk(KERN_ERR, &phba->pcidev->dev,
8145 "FATAL - unsupported SLI4 interface type - %d\n",
8146 if_type);
8147 break;
8148 }
James Smartda0436e2009-05-22 14:51:39 -04008149}
8150
8151/**
James Smart3772a992009-05-22 14:50:54 -04008152 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
8153 * @phba: pointer to lpfc hba data structure.
8154 *
8155 * This routine is invoked to enable the MSI-X interrupt vectors to device
8156 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
8157 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
8158 * invoked, enables either all or nothing, depending on the current
8159 * availability of PCI vector resources. The device driver is responsible
8160 * for calling the individual request_irq() to register each MSI-X vector
8161 * with a interrupt handler, which is done in this function. Note that
8162 * later when device is unloading, the driver should always call free_irq()
8163 * on all MSI-X vectors it has done request_irq() on before calling
8164 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
8165 * will be left with MSI-X enabled and leaks its vectors.
8166 *
8167 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008168 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008169 * other values - error
8170 **/
8171static int
8172lpfc_sli_enable_msix(struct lpfc_hba *phba)
8173{
8174 int rc, i;
8175 LPFC_MBOXQ_t *pmb;
8176
8177 /* Set up MSI-X multi-message vectors */
8178 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8179 phba->msix_entries[i].entry = i;
8180
8181 /* Configure MSI-X capability structure */
8182 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
8183 ARRAY_SIZE(phba->msix_entries));
8184 if (rc) {
8185 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8186 "0420 PCI enable MSI-X failed (%d)\n", rc);
8187 goto msi_fail_out;
8188 }
8189 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8190 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8191 "0477 MSI-X entry[%d]: vector=x%x "
8192 "message=%d\n", i,
8193 phba->msix_entries[i].vector,
8194 phba->msix_entries[i].entry);
8195 /*
8196 * Assign MSI-X vectors to interrupt handlers
8197 */
8198
8199 /* vector-0 is associated to slow-path handler */
8200 rc = request_irq(phba->msix_entries[0].vector,
8201 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
8202 LPFC_SP_DRIVER_HANDLER_NAME, phba);
8203 if (rc) {
8204 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8205 "0421 MSI-X slow-path request_irq failed "
8206 "(%d)\n", rc);
8207 goto msi_fail_out;
8208 }
8209
8210 /* vector-1 is associated to fast-path handler */
8211 rc = request_irq(phba->msix_entries[1].vector,
8212 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
8213 LPFC_FP_DRIVER_HANDLER_NAME, phba);
8214
8215 if (rc) {
8216 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8217 "0429 MSI-X fast-path request_irq failed "
8218 "(%d)\n", rc);
8219 goto irq_fail_out;
8220 }
8221
8222 /*
8223 * Configure HBA MSI-X attention conditions to messages
8224 */
8225 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8226
8227 if (!pmb) {
8228 rc = -ENOMEM;
8229 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8230 "0474 Unable to allocate memory for issuing "
8231 "MBOX_CONFIG_MSI command\n");
8232 goto mem_fail_out;
8233 }
8234 rc = lpfc_config_msi(phba, pmb);
8235 if (rc)
8236 goto mbx_fail_out;
8237 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
8238 if (rc != MBX_SUCCESS) {
8239 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
8240 "0351 Config MSI mailbox command failed, "
8241 "mbxCmd x%x, mbxStatus x%x\n",
8242 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
8243 goto mbx_fail_out;
8244 }
8245
8246 /* Free memory allocated for mailbox command */
8247 mempool_free(pmb, phba->mbox_mem_pool);
8248 return rc;
8249
8250mbx_fail_out:
8251 /* Free memory allocated for mailbox command */
8252 mempool_free(pmb, phba->mbox_mem_pool);
8253
8254mem_fail_out:
8255 /* free the irq already requested */
8256 free_irq(phba->msix_entries[1].vector, phba);
8257
8258irq_fail_out:
8259 /* free the irq already requested */
8260 free_irq(phba->msix_entries[0].vector, phba);
8261
8262msi_fail_out:
8263 /* Unconfigure MSI-X capability structure */
8264 pci_disable_msix(phba->pcidev);
8265 return rc;
8266}
8267
8268/**
8269 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
8270 * @phba: pointer to lpfc hba data structure.
8271 *
8272 * This routine is invoked to release the MSI-X vectors and then disable the
8273 * MSI-X interrupt mode to device with SLI-3 interface spec.
8274 **/
8275static void
8276lpfc_sli_disable_msix(struct lpfc_hba *phba)
8277{
8278 int i;
8279
8280 /* Free up MSI-X multi-message vectors */
8281 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8282 free_irq(phba->msix_entries[i].vector, phba);
8283 /* Disable MSI-X */
8284 pci_disable_msix(phba->pcidev);
8285
8286 return;
8287}
8288
8289/**
8290 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
8291 * @phba: pointer to lpfc hba data structure.
8292 *
8293 * This routine is invoked to enable the MSI interrupt mode to device with
8294 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
8295 * enable the MSI vector. The device driver is responsible for calling the
8296 * request_irq() to register MSI vector with a interrupt the handler, which
8297 * is done in this function.
8298 *
8299 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008300 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008301 * other values - error
8302 */
8303static int
8304lpfc_sli_enable_msi(struct lpfc_hba *phba)
8305{
8306 int rc;
8307
8308 rc = pci_enable_msi(phba->pcidev);
8309 if (!rc)
8310 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8311 "0462 PCI enable MSI mode success.\n");
8312 else {
8313 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8314 "0471 PCI enable MSI mode failed (%d)\n", rc);
8315 return rc;
8316 }
8317
8318 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
8319 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8320 if (rc) {
8321 pci_disable_msi(phba->pcidev);
8322 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8323 "0478 MSI request_irq failed (%d)\n", rc);
8324 }
8325 return rc;
8326}
8327
8328/**
8329 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
8330 * @phba: pointer to lpfc hba data structure.
8331 *
8332 * This routine is invoked to disable the MSI interrupt mode to device with
8333 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
8334 * done request_irq() on before calling pci_disable_msi(). Failure to do so
8335 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
8336 * its vector.
8337 */
8338static void
8339lpfc_sli_disable_msi(struct lpfc_hba *phba)
8340{
8341 free_irq(phba->pcidev->irq, phba);
8342 pci_disable_msi(phba->pcidev);
8343 return;
8344}
8345
8346/**
8347 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
8348 * @phba: pointer to lpfc hba data structure.
8349 *
8350 * This routine is invoked to enable device interrupt and associate driver's
8351 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
8352 * spec. Depends on the interrupt mode configured to the driver, the driver
8353 * will try to fallback from the configured interrupt mode to an interrupt
8354 * mode which is supported by the platform, kernel, and device in the order
8355 * of:
8356 * MSI-X -> MSI -> IRQ.
8357 *
8358 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008359 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008360 * other values - error
8361 **/
8362static uint32_t
8363lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
8364{
8365 uint32_t intr_mode = LPFC_INTR_ERROR;
8366 int retval;
8367
8368 if (cfg_mode == 2) {
8369 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
8370 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
8371 if (!retval) {
8372 /* Now, try to enable MSI-X interrupt mode */
8373 retval = lpfc_sli_enable_msix(phba);
8374 if (!retval) {
8375 /* Indicate initialization to MSI-X mode */
8376 phba->intr_type = MSIX;
8377 intr_mode = 2;
8378 }
8379 }
8380 }
8381
8382 /* Fallback to MSI if MSI-X initialization failed */
8383 if (cfg_mode >= 1 && phba->intr_type == NONE) {
8384 retval = lpfc_sli_enable_msi(phba);
8385 if (!retval) {
8386 /* Indicate initialization to MSI mode */
8387 phba->intr_type = MSI;
8388 intr_mode = 1;
8389 }
8390 }
8391
8392 /* Fallback to INTx if both MSI-X/MSI initalization failed */
8393 if (phba->intr_type == NONE) {
8394 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
8395 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8396 if (!retval) {
8397 /* Indicate initialization to INTx mode */
8398 phba->intr_type = INTx;
8399 intr_mode = 0;
8400 }
8401 }
8402 return intr_mode;
8403}
8404
8405/**
8406 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
8407 * @phba: pointer to lpfc hba data structure.
8408 *
8409 * This routine is invoked to disable device interrupt and disassociate the
8410 * driver's interrupt handler(s) from interrupt vector(s) to device with
8411 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
8412 * release the interrupt vector(s) for the message signaled interrupt.
8413 **/
8414static void
8415lpfc_sli_disable_intr(struct lpfc_hba *phba)
8416{
8417 /* Disable the currently initialized interrupt mode */
8418 if (phba->intr_type == MSIX)
8419 lpfc_sli_disable_msix(phba);
8420 else if (phba->intr_type == MSI)
8421 lpfc_sli_disable_msi(phba);
8422 else if (phba->intr_type == INTx)
8423 free_irq(phba->pcidev->irq, phba);
8424
8425 /* Reset interrupt management states */
8426 phba->intr_type = NONE;
8427 phba->sli.slistat.sli_intr = 0;
8428
8429 return;
8430}
8431
8432/**
James Smart7bb03bb2013-04-17 20:19:16 -04008433 * lpfc_find_next_cpu - Find next available CPU that matches the phys_id
8434 * @phba: pointer to lpfc hba data structure.
8435 *
8436 * Find next available CPU to use for IRQ to CPU affinity.
8437 */
8438static int
8439lpfc_find_next_cpu(struct lpfc_hba *phba, uint32_t phys_id)
8440{
8441 struct lpfc_vector_map_info *cpup;
8442 int cpu;
8443
8444 cpup = phba->sli4_hba.cpu_map;
8445 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8446 /* CPU must be online */
8447 if (cpu_online(cpu)) {
8448 if ((cpup->irq == LPFC_VECTOR_MAP_EMPTY) &&
8449 (lpfc_used_cpu[cpu] == LPFC_VECTOR_MAP_EMPTY) &&
8450 (cpup->phys_id == phys_id)) {
8451 return cpu;
8452 }
8453 }
8454 cpup++;
8455 }
8456
8457 /*
8458 * If we get here, we have used ALL CPUs for the specific
8459 * phys_id. Now we need to clear out lpfc_used_cpu and start
8460 * reusing CPUs.
8461 */
8462
8463 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8464 if (lpfc_used_cpu[cpu] == phys_id)
8465 lpfc_used_cpu[cpu] = LPFC_VECTOR_MAP_EMPTY;
8466 }
8467
8468 cpup = phba->sli4_hba.cpu_map;
8469 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8470 /* CPU must be online */
8471 if (cpu_online(cpu)) {
8472 if ((cpup->irq == LPFC_VECTOR_MAP_EMPTY) &&
8473 (cpup->phys_id == phys_id)) {
8474 return cpu;
8475 }
8476 }
8477 cpup++;
8478 }
8479 return LPFC_VECTOR_MAP_EMPTY;
8480}
8481
8482/**
8483 * lpfc_sli4_set_affinity - Set affinity for HBA IRQ vectors
8484 * @phba: pointer to lpfc hba data structure.
8485 * @vectors: number of HBA vectors
8486 *
8487 * Affinitize MSIX IRQ vectors to CPUs. Try to equally spread vector
8488 * affinization across multple physical CPUs (numa nodes).
8489 * In addition, this routine will assign an IO channel for each CPU
8490 * to use when issuing I/Os.
8491 */
8492static int
8493lpfc_sli4_set_affinity(struct lpfc_hba *phba, int vectors)
8494{
8495 int i, idx, saved_chann, used_chann, cpu, phys_id;
James Smartec2087a2013-09-06 12:20:36 -04008496 int max_phys_id, min_phys_id;
8497 int num_io_channel, first_cpu, chan;
James Smart7bb03bb2013-04-17 20:19:16 -04008498 struct lpfc_vector_map_info *cpup;
8499#ifdef CONFIG_X86
8500 struct cpuinfo_x86 *cpuinfo;
8501#endif
8502 struct cpumask *mask;
8503 uint8_t chann[LPFC_FCP_IO_CHAN_MAX+1];
8504
8505 /* If there is no mapping, just return */
8506 if (!phba->cfg_fcp_cpu_map)
8507 return 1;
8508
8509 /* Init cpu_map array */
8510 memset(phba->sli4_hba.cpu_map, 0xff,
8511 (sizeof(struct lpfc_vector_map_info) *
8512 phba->sli4_hba.num_present_cpu));
8513
8514 max_phys_id = 0;
James Smartec2087a2013-09-06 12:20:36 -04008515 min_phys_id = 0xff;
James Smart7bb03bb2013-04-17 20:19:16 -04008516 phys_id = 0;
8517 num_io_channel = 0;
8518 first_cpu = LPFC_VECTOR_MAP_EMPTY;
8519
8520 /* Update CPU map with physical id and core id of each CPU */
8521 cpup = phba->sli4_hba.cpu_map;
8522 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8523#ifdef CONFIG_X86
8524 cpuinfo = &cpu_data(cpu);
8525 cpup->phys_id = cpuinfo->phys_proc_id;
8526 cpup->core_id = cpuinfo->cpu_core_id;
8527#else
8528 /* No distinction between CPUs for other platforms */
8529 cpup->phys_id = 0;
8530 cpup->core_id = 0;
8531#endif
8532
8533 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8534 "3328 CPU physid %d coreid %d\n",
8535 cpup->phys_id, cpup->core_id);
8536
8537 if (cpup->phys_id > max_phys_id)
8538 max_phys_id = cpup->phys_id;
James Smartec2087a2013-09-06 12:20:36 -04008539 if (cpup->phys_id < min_phys_id)
8540 min_phys_id = cpup->phys_id;
James Smart7bb03bb2013-04-17 20:19:16 -04008541 cpup++;
8542 }
8543
James Smartec2087a2013-09-06 12:20:36 -04008544 phys_id = min_phys_id;
James Smart7bb03bb2013-04-17 20:19:16 -04008545 /* Now associate the HBA vectors with specific CPUs */
8546 for (idx = 0; idx < vectors; idx++) {
8547 cpup = phba->sli4_hba.cpu_map;
8548 cpu = lpfc_find_next_cpu(phba, phys_id);
8549 if (cpu == LPFC_VECTOR_MAP_EMPTY) {
8550
8551 /* Try for all phys_id's */
8552 for (i = 1; i < max_phys_id; i++) {
8553 phys_id++;
8554 if (phys_id > max_phys_id)
James Smartec2087a2013-09-06 12:20:36 -04008555 phys_id = min_phys_id;
James Smart7bb03bb2013-04-17 20:19:16 -04008556 cpu = lpfc_find_next_cpu(phba, phys_id);
8557 if (cpu == LPFC_VECTOR_MAP_EMPTY)
8558 continue;
8559 goto found;
8560 }
8561
James Smartec2087a2013-09-06 12:20:36 -04008562 /* Use round robin for scheduling */
8563 phba->cfg_fcp_io_sched = LPFC_FCP_SCHED_ROUND_ROBIN;
8564 chan = 0;
8565 cpup = phba->sli4_hba.cpu_map;
8566 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
8567 cpup->channel_id = chan;
8568 cpup++;
8569 chan++;
8570 if (chan >= phba->cfg_fcp_io_channel)
8571 chan = 0;
8572 }
8573
James Smart7bb03bb2013-04-17 20:19:16 -04008574 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8575 "3329 Cannot set affinity:"
8576 "Error mapping vector %d (%d)\n",
8577 idx, vectors);
8578 return 0;
8579 }
8580found:
8581 cpup += cpu;
8582 if (phba->cfg_fcp_cpu_map == LPFC_DRIVER_CPU_MAP)
8583 lpfc_used_cpu[cpu] = phys_id;
8584
8585 /* Associate vector with selected CPU */
8586 cpup->irq = phba->sli4_hba.msix_entries[idx].vector;
8587
8588 /* Associate IO channel with selected CPU */
8589 cpup->channel_id = idx;
8590 num_io_channel++;
8591
8592 if (first_cpu == LPFC_VECTOR_MAP_EMPTY)
8593 first_cpu = cpu;
8594
8595 /* Now affinitize to the selected CPU */
8596 mask = &cpup->maskbits;
8597 cpumask_clear(mask);
8598 cpumask_set_cpu(cpu, mask);
8599 i = irq_set_affinity_hint(phba->sli4_hba.msix_entries[idx].
8600 vector, mask);
8601
8602 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8603 "3330 Set Affinity: CPU %d channel %d "
8604 "irq %d (%x)\n",
8605 cpu, cpup->channel_id,
8606 phba->sli4_hba.msix_entries[idx].vector, i);
8607
8608 /* Spread vector mapping across multple physical CPU nodes */
8609 phys_id++;
8610 if (phys_id > max_phys_id)
James Smartec2087a2013-09-06 12:20:36 -04008611 phys_id = min_phys_id;
James Smart7bb03bb2013-04-17 20:19:16 -04008612 }
8613
8614 /*
8615 * Finally fill in the IO channel for any remaining CPUs.
8616 * At this point, all IO channels have been assigned to a specific
8617 * MSIx vector, mapped to a specific CPU.
8618 * Base the remaining IO channel assigned, to IO channels already
8619 * assigned to other CPUs on the same phys_id.
8620 */
James Smartec2087a2013-09-06 12:20:36 -04008621 for (i = min_phys_id; i <= max_phys_id; i++) {
James Smart7bb03bb2013-04-17 20:19:16 -04008622 /*
8623 * If there are no io channels already mapped to
8624 * this phys_id, just round robin thru the io_channels.
8625 * Setup chann[] for round robin.
8626 */
8627 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++)
8628 chann[idx] = idx;
8629
8630 saved_chann = 0;
8631 used_chann = 0;
8632
8633 /*
8634 * First build a list of IO channels already assigned
8635 * to this phys_id before reassigning the same IO
8636 * channels to the remaining CPUs.
8637 */
8638 cpup = phba->sli4_hba.cpu_map;
8639 cpu = first_cpu;
8640 cpup += cpu;
8641 for (idx = 0; idx < phba->sli4_hba.num_present_cpu;
8642 idx++) {
8643 if (cpup->phys_id == i) {
8644 /*
8645 * Save any IO channels that are
8646 * already mapped to this phys_id.
8647 */
8648 if (cpup->irq != LPFC_VECTOR_MAP_EMPTY) {
8649 chann[saved_chann] =
8650 cpup->channel_id;
8651 saved_chann++;
8652 goto out;
8653 }
8654
8655 /* See if we are using round-robin */
8656 if (saved_chann == 0)
8657 saved_chann =
8658 phba->cfg_fcp_io_channel;
8659
8660 /* Associate next IO channel with CPU */
8661 cpup->channel_id = chann[used_chann];
8662 num_io_channel++;
8663 used_chann++;
8664 if (used_chann == saved_chann)
8665 used_chann = 0;
8666
8667 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8668 "3331 Set IO_CHANN "
8669 "CPU %d channel %d\n",
8670 idx, cpup->channel_id);
8671 }
8672out:
8673 cpu++;
8674 if (cpu >= phba->sli4_hba.num_present_cpu) {
8675 cpup = phba->sli4_hba.cpu_map;
8676 cpu = 0;
8677 } else {
8678 cpup++;
8679 }
8680 }
8681 }
8682
8683 if (phba->sli4_hba.num_online_cpu != phba->sli4_hba.num_present_cpu) {
8684 cpup = phba->sli4_hba.cpu_map;
8685 for (idx = 0; idx < phba->sli4_hba.num_present_cpu; idx++) {
8686 if (cpup->channel_id == LPFC_VECTOR_MAP_EMPTY) {
8687 cpup->channel_id = 0;
8688 num_io_channel++;
8689
8690 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8691 "3332 Assign IO_CHANN "
8692 "CPU %d channel %d\n",
8693 idx, cpup->channel_id);
8694 }
8695 cpup++;
8696 }
8697 }
8698
8699 /* Sanity check */
8700 if (num_io_channel != phba->sli4_hba.num_present_cpu)
8701 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8702 "3333 Set affinity mismatch:"
James Smartec2087a2013-09-06 12:20:36 -04008703 "%d chann != %d cpus: %d vectors\n",
James Smart7bb03bb2013-04-17 20:19:16 -04008704 num_io_channel, phba->sli4_hba.num_present_cpu,
8705 vectors);
8706
James Smartec2087a2013-09-06 12:20:36 -04008707 /* Enable using cpu affinity for scheduling */
James Smart7bb03bb2013-04-17 20:19:16 -04008708 phba->cfg_fcp_io_sched = LPFC_FCP_SCHED_BY_CPU;
8709 return 1;
8710}
8711
8712
8713/**
James Smartda0436e2009-05-22 14:51:39 -04008714 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
8715 * @phba: pointer to lpfc hba data structure.
8716 *
8717 * This routine is invoked to enable the MSI-X interrupt vectors to device
8718 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
8719 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
8720 * enables either all or nothing, depending on the current availability of
8721 * PCI vector resources. The device driver is responsible for calling the
8722 * individual request_irq() to register each MSI-X vector with a interrupt
8723 * handler, which is done in this function. Note that later when device is
8724 * unloading, the driver should always call free_irq() on all MSI-X vectors
8725 * it has done request_irq() on before calling pci_disable_msix(). Failure
8726 * to do so results in a BUG_ON() and a device will be left with MSI-X
8727 * enabled and leaks its vectors.
8728 *
8729 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008730 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008731 * other values - error
8732 **/
8733static int
8734lpfc_sli4_enable_msix(struct lpfc_hba *phba)
8735{
James Smart75baf692010-06-08 18:31:21 -04008736 int vectors, rc, index;
James Smartda0436e2009-05-22 14:51:39 -04008737
8738 /* Set up MSI-X multi-message vectors */
James Smart82c3e9b2012-09-29 11:29:50 -04008739 for (index = 0; index < phba->cfg_fcp_io_channel; index++)
James Smartda0436e2009-05-22 14:51:39 -04008740 phba->sli4_hba.msix_entries[index].entry = index;
8741
8742 /* Configure MSI-X capability structure */
James Smart82c3e9b2012-09-29 11:29:50 -04008743 vectors = phba->cfg_fcp_io_channel;
James Smart1ba981f2014-02-20 09:56:45 -05008744 if (phba->cfg_fof) {
8745 phba->sli4_hba.msix_entries[index].entry = index;
8746 vectors++;
8747 }
James Smart75baf692010-06-08 18:31:21 -04008748enable_msix_vectors:
James Smartda0436e2009-05-22 14:51:39 -04008749 rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
James Smart75baf692010-06-08 18:31:21 -04008750 vectors);
8751 if (rc > 1) {
8752 vectors = rc;
8753 goto enable_msix_vectors;
8754 } else if (rc) {
James Smartda0436e2009-05-22 14:51:39 -04008755 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8756 "0484 PCI enable MSI-X failed (%d)\n", rc);
8757 goto msi_fail_out;
8758 }
James Smart75baf692010-06-08 18:31:21 -04008759
James Smartda0436e2009-05-22 14:51:39 -04008760 /* Log MSI-X vector assignment */
James Smart75baf692010-06-08 18:31:21 -04008761 for (index = 0; index < vectors; index++)
James Smartda0436e2009-05-22 14:51:39 -04008762 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8763 "0489 MSI-X entry[%d]: vector=x%x "
8764 "message=%d\n", index,
8765 phba->sli4_hba.msix_entries[index].vector,
8766 phba->sli4_hba.msix_entries[index].entry);
James Smart67d12732012-08-03 12:36:13 -04008767
James Smart7bb03bb2013-04-17 20:19:16 -04008768 /* Assign MSI-X vectors to interrupt handlers */
James Smart67d12732012-08-03 12:36:13 -04008769 for (index = 0; index < vectors; index++) {
James Smart4305f182012-08-03 12:36:33 -04008770 memset(&phba->sli4_hba.handler_name[index], 0, 16);
8771 sprintf((char *)&phba->sli4_hba.handler_name[index],
8772 LPFC_DRIVER_HANDLER_NAME"%d", index);
James Smartda0436e2009-05-22 14:51:39 -04008773
James Smart67d12732012-08-03 12:36:13 -04008774 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8775 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
James Smartba20c852012-08-03 12:36:52 -04008776 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].fcp_eq_in_use, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008777 if (phba->cfg_fof && (index == (vectors - 1)))
8778 rc = request_irq(
8779 phba->sli4_hba.msix_entries[index].vector,
8780 &lpfc_sli4_fof_intr_handler, IRQF_SHARED,
8781 (char *)&phba->sli4_hba.handler_name[index],
8782 &phba->sli4_hba.fcp_eq_hdl[index]);
8783 else
8784 rc = request_irq(
8785 phba->sli4_hba.msix_entries[index].vector,
James Smart67d12732012-08-03 12:36:13 -04008786 &lpfc_sli4_hba_intr_handler, IRQF_SHARED,
James Smart4305f182012-08-03 12:36:33 -04008787 (char *)&phba->sli4_hba.handler_name[index],
James Smart67d12732012-08-03 12:36:13 -04008788 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -04008789 if (rc) {
8790 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8791 "0486 MSI-X fast-path (%d) "
8792 "request_irq failed (%d)\n", index, rc);
8793 goto cfg_fail_out;
8794 }
8795 }
8796
James Smart1ba981f2014-02-20 09:56:45 -05008797 if (phba->cfg_fof)
8798 vectors--;
8799
James Smart82c3e9b2012-09-29 11:29:50 -04008800 if (vectors != phba->cfg_fcp_io_channel) {
8801 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8802 "3238 Reducing IO channels to match number of "
8803 "MSI-X vectors, requested %d got %d\n",
8804 phba->cfg_fcp_io_channel, vectors);
8805 phba->cfg_fcp_io_channel = vectors;
8806 }
James Smart7bb03bb2013-04-17 20:19:16 -04008807
8808 lpfc_sli4_set_affinity(phba, vectors);
James Smartda0436e2009-05-22 14:51:39 -04008809 return rc;
8810
8811cfg_fail_out:
8812 /* free the irq already requested */
James Smartacbd8612013-09-06 12:21:19 -04008813 for (--index; index >= 0; index--) {
8814 irq_set_affinity_hint(phba->sli4_hba.msix_entries[index].
8815 vector, NULL);
James Smart67d12732012-08-03 12:36:13 -04008816 free_irq(phba->sli4_hba.msix_entries[index].vector,
8817 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartacbd8612013-09-06 12:21:19 -04008818 }
James Smartda0436e2009-05-22 14:51:39 -04008819
8820msi_fail_out:
8821 /* Unconfigure MSI-X capability structure */
8822 pci_disable_msix(phba->pcidev);
8823 return rc;
8824}
8825
8826/**
8827 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
8828 * @phba: pointer to lpfc hba data structure.
8829 *
8830 * This routine is invoked to release the MSI-X vectors and then disable the
8831 * MSI-X interrupt mode to device with SLI-4 interface spec.
8832 **/
8833static void
8834lpfc_sli4_disable_msix(struct lpfc_hba *phba)
8835{
8836 int index;
8837
8838 /* Free up MSI-X multi-message vectors */
James Smartacbd8612013-09-06 12:21:19 -04008839 for (index = 0; index < phba->cfg_fcp_io_channel; index++) {
8840 irq_set_affinity_hint(phba->sli4_hba.msix_entries[index].
8841 vector, NULL);
James Smartda0436e2009-05-22 14:51:39 -04008842 free_irq(phba->sli4_hba.msix_entries[index].vector,
James Smart67d12732012-08-03 12:36:13 -04008843 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartacbd8612013-09-06 12:21:19 -04008844 }
James Smart1ba981f2014-02-20 09:56:45 -05008845 if (phba->cfg_fof) {
8846 free_irq(phba->sli4_hba.msix_entries[index].vector,
8847 &phba->sli4_hba.fcp_eq_hdl[index]);
8848 }
James Smartda0436e2009-05-22 14:51:39 -04008849 /* Disable MSI-X */
8850 pci_disable_msix(phba->pcidev);
8851
8852 return;
8853}
8854
8855/**
8856 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
8857 * @phba: pointer to lpfc hba data structure.
8858 *
8859 * This routine is invoked to enable the MSI interrupt mode to device with
8860 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
8861 * to enable the MSI vector. The device driver is responsible for calling
8862 * the request_irq() to register MSI vector with a interrupt the handler,
8863 * which is done in this function.
8864 *
8865 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008866 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008867 * other values - error
8868 **/
8869static int
8870lpfc_sli4_enable_msi(struct lpfc_hba *phba)
8871{
8872 int rc, index;
8873
8874 rc = pci_enable_msi(phba->pcidev);
8875 if (!rc)
8876 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8877 "0487 PCI enable MSI mode success.\n");
8878 else {
8879 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8880 "0488 PCI enable MSI mode failed (%d)\n", rc);
8881 return rc;
8882 }
8883
8884 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
8885 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8886 if (rc) {
8887 pci_disable_msi(phba->pcidev);
8888 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8889 "0490 MSI request_irq failed (%d)\n", rc);
James Smart75baf692010-06-08 18:31:21 -04008890 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008891 }
8892
James Smart67d12732012-08-03 12:36:13 -04008893 for (index = 0; index < phba->cfg_fcp_io_channel; index++) {
James Smartda0436e2009-05-22 14:51:39 -04008894 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8895 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
8896 }
8897
James Smart1ba981f2014-02-20 09:56:45 -05008898 if (phba->cfg_fof) {
8899 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8900 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
8901 }
James Smart75baf692010-06-08 18:31:21 -04008902 return 0;
James Smartda0436e2009-05-22 14:51:39 -04008903}
8904
8905/**
8906 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
8907 * @phba: pointer to lpfc hba data structure.
8908 *
8909 * This routine is invoked to disable the MSI interrupt mode to device with
8910 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
8911 * done request_irq() on before calling pci_disable_msi(). Failure to do so
8912 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
8913 * its vector.
8914 **/
8915static void
8916lpfc_sli4_disable_msi(struct lpfc_hba *phba)
8917{
8918 free_irq(phba->pcidev->irq, phba);
8919 pci_disable_msi(phba->pcidev);
8920 return;
8921}
8922
8923/**
8924 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
8925 * @phba: pointer to lpfc hba data structure.
8926 *
8927 * This routine is invoked to enable device interrupt and associate driver's
8928 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
8929 * interface spec. Depends on the interrupt mode configured to the driver,
8930 * the driver will try to fallback from the configured interrupt mode to an
8931 * interrupt mode which is supported by the platform, kernel, and device in
8932 * the order of:
8933 * MSI-X -> MSI -> IRQ.
8934 *
8935 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008936 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008937 * other values - error
8938 **/
8939static uint32_t
8940lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
8941{
8942 uint32_t intr_mode = LPFC_INTR_ERROR;
8943 int retval, index;
8944
8945 if (cfg_mode == 2) {
8946 /* Preparation before conf_msi mbox cmd */
8947 retval = 0;
8948 if (!retval) {
8949 /* Now, try to enable MSI-X interrupt mode */
8950 retval = lpfc_sli4_enable_msix(phba);
8951 if (!retval) {
8952 /* Indicate initialization to MSI-X mode */
8953 phba->intr_type = MSIX;
8954 intr_mode = 2;
8955 }
8956 }
8957 }
8958
8959 /* Fallback to MSI if MSI-X initialization failed */
8960 if (cfg_mode >= 1 && phba->intr_type == NONE) {
8961 retval = lpfc_sli4_enable_msi(phba);
8962 if (!retval) {
8963 /* Indicate initialization to MSI mode */
8964 phba->intr_type = MSI;
8965 intr_mode = 1;
8966 }
8967 }
8968
8969 /* Fallback to INTx if both MSI-X/MSI initalization failed */
8970 if (phba->intr_type == NONE) {
8971 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
8972 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8973 if (!retval) {
8974 /* Indicate initialization to INTx mode */
8975 phba->intr_type = INTx;
8976 intr_mode = 0;
James Smart67d12732012-08-03 12:36:13 -04008977 for (index = 0; index < phba->cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04008978 index++) {
8979 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8980 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
James Smartba20c852012-08-03 12:36:52 -04008981 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].
8982 fcp_eq_in_use, 1);
James Smartda0436e2009-05-22 14:51:39 -04008983 }
James Smart1ba981f2014-02-20 09:56:45 -05008984 if (phba->cfg_fof) {
8985 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8986 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
8987 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].
8988 fcp_eq_in_use, 1);
8989 }
James Smartda0436e2009-05-22 14:51:39 -04008990 }
8991 }
8992 return intr_mode;
8993}
8994
8995/**
8996 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
8997 * @phba: pointer to lpfc hba data structure.
8998 *
8999 * This routine is invoked to disable device interrupt and disassociate
9000 * the driver's interrupt handler(s) from interrupt vector(s) to device
9001 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
9002 * will release the interrupt vector(s) for the message signaled interrupt.
9003 **/
9004static void
9005lpfc_sli4_disable_intr(struct lpfc_hba *phba)
9006{
9007 /* Disable the currently initialized interrupt mode */
9008 if (phba->intr_type == MSIX)
9009 lpfc_sli4_disable_msix(phba);
9010 else if (phba->intr_type == MSI)
9011 lpfc_sli4_disable_msi(phba);
9012 else if (phba->intr_type == INTx)
9013 free_irq(phba->pcidev->irq, phba);
9014
9015 /* Reset interrupt management states */
9016 phba->intr_type = NONE;
9017 phba->sli.slistat.sli_intr = 0;
9018
9019 return;
9020}
9021
9022/**
James Smart3772a992009-05-22 14:50:54 -04009023 * lpfc_unset_hba - Unset SLI3 hba device initialization
9024 * @phba: pointer to lpfc hba data structure.
9025 *
9026 * This routine is invoked to unset the HBA device initialization steps to
9027 * a device with SLI-3 interface spec.
9028 **/
9029static void
9030lpfc_unset_hba(struct lpfc_hba *phba)
9031{
9032 struct lpfc_vport *vport = phba->pport;
9033 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
9034
9035 spin_lock_irq(shost->host_lock);
9036 vport->load_flag |= FC_UNLOADING;
9037 spin_unlock_irq(shost->host_lock);
9038
James Smart72859902012-01-18 16:25:38 -05009039 kfree(phba->vpi_bmask);
9040 kfree(phba->vpi_ids);
9041
James Smart3772a992009-05-22 14:50:54 -04009042 lpfc_stop_hba_timers(phba);
9043
9044 phba->pport->work_port_events = 0;
9045
9046 lpfc_sli_hba_down(phba);
9047
9048 lpfc_sli_brdrestart(phba);
9049
9050 lpfc_sli_disable_intr(phba);
9051
9052 return;
9053}
9054
9055/**
James Smart5af5eee2010-10-22 11:06:38 -04009056 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
9057 * @phba: Pointer to HBA context object.
9058 *
9059 * This function is called in the SLI4 code path to wait for completion
9060 * of device's XRIs exchange busy. It will check the XRI exchange busy
9061 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
9062 * that, it will check the XRI exchange busy on outstanding FCP and ELS
9063 * I/Os every 30 seconds, log error message, and wait forever. Only when
9064 * all XRI exchange busy complete, the driver unload shall proceed with
9065 * invoking the function reset ioctl mailbox command to the CNA and the
9066 * the rest of the driver unload resource release.
9067 **/
9068static void
9069lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
9070{
9071 int wait_time = 0;
9072 int fcp_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
9073 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
9074
9075 while (!fcp_xri_cmpl || !els_xri_cmpl) {
9076 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
9077 if (!fcp_xri_cmpl)
9078 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9079 "2877 FCP XRI exchange busy "
9080 "wait time: %d seconds.\n",
9081 wait_time/1000);
9082 if (!els_xri_cmpl)
9083 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9084 "2878 ELS XRI exchange busy "
9085 "wait time: %d seconds.\n",
9086 wait_time/1000);
9087 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
9088 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
9089 } else {
9090 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
9091 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
9092 }
9093 fcp_xri_cmpl =
9094 list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
9095 els_xri_cmpl =
9096 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
9097 }
9098}
9099
9100/**
James Smartda0436e2009-05-22 14:51:39 -04009101 * lpfc_sli4_hba_unset - Unset the fcoe hba
9102 * @phba: Pointer to HBA context object.
9103 *
9104 * This function is called in the SLI4 code path to reset the HBA's FCoE
9105 * function. The caller is not required to hold any lock. This routine
9106 * issues PCI function reset mailbox command to reset the FCoE function.
9107 * At the end of the function, it calls lpfc_hba_down_post function to
9108 * free any pending commands.
9109 **/
9110static void
9111lpfc_sli4_hba_unset(struct lpfc_hba *phba)
9112{
9113 int wait_cnt = 0;
9114 LPFC_MBOXQ_t *mboxq;
James Smart912e3ac2011-05-24 11:42:11 -04009115 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -04009116
9117 lpfc_stop_hba_timers(phba);
9118 phba->sli4_hba.intr_enable = 0;
9119
9120 /*
9121 * Gracefully wait out the potential current outstanding asynchronous
9122 * mailbox command.
9123 */
9124
9125 /* First, block any pending async mailbox command from posted */
9126 spin_lock_irq(&phba->hbalock);
9127 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
9128 spin_unlock_irq(&phba->hbalock);
9129 /* Now, trying to wait it out if we can */
9130 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9131 msleep(10);
9132 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
9133 break;
9134 }
9135 /* Forcefully release the outstanding mailbox command if timed out */
9136 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9137 spin_lock_irq(&phba->hbalock);
9138 mboxq = phba->sli.mbox_active;
9139 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
9140 __lpfc_mbox_cmpl_put(phba, mboxq);
9141 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9142 phba->sli.mbox_active = NULL;
9143 spin_unlock_irq(&phba->hbalock);
9144 }
9145
James Smart5af5eee2010-10-22 11:06:38 -04009146 /* Abort all iocbs associated with the hba */
9147 lpfc_sli_hba_iocb_abort(phba);
9148
9149 /* Wait for completion of device XRI exchange busy */
9150 lpfc_sli4_xri_exchange_busy_wait(phba);
9151
James Smartda0436e2009-05-22 14:51:39 -04009152 /* Disable PCI subsystem interrupt */
9153 lpfc_sli4_disable_intr(phba);
9154
James Smart912e3ac2011-05-24 11:42:11 -04009155 /* Disable SR-IOV if enabled */
9156 if (phba->cfg_sriov_nr_virtfn)
9157 pci_disable_sriov(pdev);
9158
James Smartda0436e2009-05-22 14:51:39 -04009159 /* Stop kthread signal shall trigger work_done one more time */
9160 kthread_stop(phba->worker_thread);
9161
James Smart3677a3a2010-09-29 11:19:14 -04009162 /* Reset SLI4 HBA FCoE function */
9163 lpfc_pci_function_reset(phba);
James Smart5350d872011-10-10 21:33:49 -04009164 lpfc_sli4_queue_destroy(phba);
James Smart3677a3a2010-09-29 11:19:14 -04009165
James Smartda0436e2009-05-22 14:51:39 -04009166 /* Stop the SLI4 device port */
9167 phba->pport->work_port_events = 0;
9168}
9169
James Smart28baac72010-02-12 14:42:03 -05009170 /**
9171 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
9172 * @phba: Pointer to HBA context object.
9173 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
9174 *
9175 * This function is called in the SLI4 code path to read the port's
9176 * sli4 capabilities.
9177 *
9178 * This function may be be called from any context that can block-wait
9179 * for the completion. The expectation is that this routine is called
9180 * typically from probe_one or from the online routine.
9181 **/
9182int
9183lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9184{
9185 int rc;
9186 struct lpfc_mqe *mqe;
9187 struct lpfc_pc_sli4_params *sli4_params;
9188 uint32_t mbox_tmo;
9189
9190 rc = 0;
9191 mqe = &mboxq->u.mqe;
9192
9193 /* Read the port's SLI4 Parameters port capabilities */
James Smartfedd3b72011-02-16 12:39:24 -05009194 lpfc_pc_sli4_params(mboxq);
James Smart28baac72010-02-12 14:42:03 -05009195 if (!phba->sli4_hba.intr_enable)
9196 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9197 else {
James Smarta183a152011-10-10 21:32:43 -04009198 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart28baac72010-02-12 14:42:03 -05009199 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
9200 }
9201
9202 if (unlikely(rc))
9203 return 1;
9204
9205 sli4_params = &phba->sli4_hba.pc_sli4_params;
9206 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
9207 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
9208 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
9209 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
9210 &mqe->un.sli4_params);
9211 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
9212 &mqe->un.sli4_params);
9213 sli4_params->proto_types = mqe->un.sli4_params.word3;
9214 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
9215 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
9216 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
9217 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
9218 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
9219 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
9220 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
9221 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
9222 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
9223 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
9224 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
9225 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
9226 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
9227 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
9228 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
9229 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
9230 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
9231 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
9232 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
9233 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
James Smart05580562011-05-24 11:40:48 -04009234
9235 /* Make sure that sge_supp_len can be handled by the driver */
9236 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
9237 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
9238
James Smart28baac72010-02-12 14:42:03 -05009239 return rc;
9240}
9241
James Smartda0436e2009-05-22 14:51:39 -04009242/**
James Smartfedd3b72011-02-16 12:39:24 -05009243 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
9244 * @phba: Pointer to HBA context object.
9245 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
9246 *
9247 * This function is called in the SLI4 code path to read the port's
9248 * sli4 capabilities.
9249 *
9250 * This function may be be called from any context that can block-wait
9251 * for the completion. The expectation is that this routine is called
9252 * typically from probe_one or from the online routine.
9253 **/
9254int
9255lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9256{
9257 int rc;
9258 struct lpfc_mqe *mqe = &mboxq->u.mqe;
9259 struct lpfc_pc_sli4_params *sli4_params;
James Smarta183a152011-10-10 21:32:43 -04009260 uint32_t mbox_tmo;
James Smartfedd3b72011-02-16 12:39:24 -05009261 int length;
9262 struct lpfc_sli4_parameters *mbx_sli4_parameters;
9263
James Smart6d368e52011-05-24 11:44:12 -04009264 /*
9265 * By default, the driver assumes the SLI4 port requires RPI
9266 * header postings. The SLI4_PARAM response will correct this
9267 * assumption.
9268 */
9269 phba->sli4_hba.rpi_hdrs_in_use = 1;
9270
James Smartfedd3b72011-02-16 12:39:24 -05009271 /* Read the port's SLI4 Config Parameters */
9272 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
9273 sizeof(struct lpfc_sli4_cfg_mhdr));
9274 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
9275 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
9276 length, LPFC_SLI4_MBX_EMBED);
9277 if (!phba->sli4_hba.intr_enable)
9278 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -04009279 else {
9280 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
9281 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
9282 }
James Smartfedd3b72011-02-16 12:39:24 -05009283 if (unlikely(rc))
9284 return rc;
9285 sli4_params = &phba->sli4_hba.pc_sli4_params;
9286 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
9287 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
9288 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
9289 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
9290 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
9291 mbx_sli4_parameters);
9292 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
9293 mbx_sli4_parameters);
9294 if (bf_get(cfg_phwq, mbx_sli4_parameters))
9295 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
9296 else
9297 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
9298 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
9299 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
James Smart1ba981f2014-02-20 09:56:45 -05009300 sli4_params->oas_supported = bf_get(cfg_oas, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -05009301 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
9302 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
9303 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
9304 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
James Smart0c651872013-07-15 18:33:23 -04009305 sli4_params->wqsize = bf_get(cfg_wqsize, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -05009306 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
9307 mbx_sli4_parameters);
9308 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
9309 mbx_sli4_parameters);
James Smart6d368e52011-05-24 11:44:12 -04009310 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
9311 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
James Smart05580562011-05-24 11:40:48 -04009312
9313 /* Make sure that sge_supp_len can be handled by the driver */
9314 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
9315 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
9316
James Smartfedd3b72011-02-16 12:39:24 -05009317 return 0;
9318}
9319
9320/**
James Smart3772a992009-05-22 14:50:54 -04009321 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
9322 * @pdev: pointer to PCI device
9323 * @pid: pointer to PCI device identifier
9324 *
9325 * This routine is to be called to attach a device with SLI-3 interface spec
9326 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
9327 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
9328 * information of the device and driver to see if the driver state that it can
9329 * support this kind of device. If the match is successful, the driver core
9330 * invokes this routine. If this routine determines it can claim the HBA, it
9331 * does all the initialization that it needs to do to handle the HBA properly.
9332 *
9333 * Return code
9334 * 0 - driver can claim the device
9335 * negative value - driver can not claim the device
9336 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009337static int
James Smart3772a992009-05-22 14:50:54 -04009338lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
9339{
9340 struct lpfc_hba *phba;
9341 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04009342 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -04009343 int error;
9344 uint32_t cfg_mode, intr_mode;
9345
9346 /* Allocate memory for HBA structure */
9347 phba = lpfc_hba_alloc(pdev);
9348 if (!phba)
9349 return -ENOMEM;
9350
9351 /* Perform generic PCI device enabling operation */
9352 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -04009353 if (error)
James Smart3772a992009-05-22 14:50:54 -04009354 goto out_free_phba;
James Smart3772a992009-05-22 14:50:54 -04009355
9356 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
9357 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
9358 if (error)
9359 goto out_disable_pci_dev;
9360
9361 /* Set up SLI-3 specific device PCI memory space */
9362 error = lpfc_sli_pci_mem_setup(phba);
9363 if (error) {
9364 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9365 "1402 Failed to set up pci memory space.\n");
9366 goto out_disable_pci_dev;
9367 }
9368
9369 /* Set up phase-1 common device driver resources */
9370 error = lpfc_setup_driver_resource_phase1(phba);
9371 if (error) {
9372 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9373 "1403 Failed to set up driver resource.\n");
9374 goto out_unset_pci_mem_s3;
9375 }
9376
9377 /* Set up SLI-3 specific device driver resources */
9378 error = lpfc_sli_driver_resource_setup(phba);
9379 if (error) {
9380 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9381 "1404 Failed to set up driver resource.\n");
9382 goto out_unset_pci_mem_s3;
9383 }
9384
9385 /* Initialize and populate the iocb list per host */
9386 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
9387 if (error) {
9388 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9389 "1405 Failed to initialize iocb list.\n");
9390 goto out_unset_driver_resource_s3;
9391 }
9392
9393 /* Set up common device driver resources */
9394 error = lpfc_setup_driver_resource_phase2(phba);
9395 if (error) {
9396 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9397 "1406 Failed to set up driver resource.\n");
9398 goto out_free_iocb_list;
9399 }
9400
James Smart079b5c92011-08-21 21:48:49 -04009401 /* Get the default values for Model Name and Description */
9402 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
9403
James Smart3772a992009-05-22 14:50:54 -04009404 /* Create SCSI host to the physical port */
9405 error = lpfc_create_shost(phba);
9406 if (error) {
9407 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9408 "1407 Failed to create scsi host.\n");
9409 goto out_unset_driver_resource;
9410 }
9411
9412 /* Configure sysfs attributes */
9413 vport = phba->pport;
9414 error = lpfc_alloc_sysfs_attr(vport);
9415 if (error) {
9416 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9417 "1476 Failed to allocate sysfs attr\n");
9418 goto out_destroy_shost;
9419 }
9420
James Smart6669f9b2009-10-02 15:16:45 -04009421 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -04009422 /* Now, trying to enable interrupt and bring up the device */
9423 cfg_mode = phba->cfg_use_msi;
9424 while (true) {
9425 /* Put device to a known state before enabling interrupt */
9426 lpfc_stop_port(phba);
9427 /* Configure and enable interrupt */
9428 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
9429 if (intr_mode == LPFC_INTR_ERROR) {
9430 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9431 "0431 Failed to enable interrupt.\n");
9432 error = -ENODEV;
9433 goto out_free_sysfs_attr;
9434 }
9435 /* SLI-3 HBA setup */
9436 if (lpfc_sli_hba_setup(phba)) {
9437 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9438 "1477 Failed to set up hba\n");
9439 error = -ENODEV;
9440 goto out_remove_device;
9441 }
9442
9443 /* Wait 50ms for the interrupts of previous mailbox commands */
9444 msleep(50);
9445 /* Check active interrupts on message signaled interrupts */
9446 if (intr_mode == 0 ||
9447 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
9448 /* Log the current active interrupt mode */
9449 phba->intr_mode = intr_mode;
9450 lpfc_log_intr_mode(phba, intr_mode);
9451 break;
9452 } else {
9453 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9454 "0447 Configure interrupt mode (%d) "
9455 "failed active interrupt test.\n",
9456 intr_mode);
9457 /* Disable the current interrupt mode */
9458 lpfc_sli_disable_intr(phba);
9459 /* Try next level of interrupt mode */
9460 cfg_mode = --intr_mode;
9461 }
9462 }
9463
9464 /* Perform post initialization setup */
9465 lpfc_post_init_setup(phba);
9466
9467 /* Check if there are static vports to be created. */
9468 lpfc_create_static_vport(phba);
9469
9470 return 0;
9471
9472out_remove_device:
9473 lpfc_unset_hba(phba);
9474out_free_sysfs_attr:
9475 lpfc_free_sysfs_attr(vport);
9476out_destroy_shost:
9477 lpfc_destroy_shost(phba);
9478out_unset_driver_resource:
9479 lpfc_unset_driver_resource_phase2(phba);
9480out_free_iocb_list:
9481 lpfc_free_iocb_list(phba);
9482out_unset_driver_resource_s3:
9483 lpfc_sli_driver_resource_unset(phba);
9484out_unset_pci_mem_s3:
9485 lpfc_sli_pci_mem_unset(phba);
9486out_disable_pci_dev:
9487 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04009488 if (shost)
9489 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -04009490out_free_phba:
9491 lpfc_hba_free(phba);
9492 return error;
9493}
9494
9495/**
9496 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -04009497 * @pdev: pointer to PCI device
9498 *
James Smart3772a992009-05-22 14:50:54 -04009499 * This routine is to be called to disattach a device with SLI-3 interface
9500 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
9501 * removed from PCI bus, it performs all the necessary cleanup for the HBA
9502 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -04009503 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009504static void
James Smart3772a992009-05-22 14:50:54 -04009505lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -05009506{
James Smart2e0fef82007-06-17 19:56:36 -05009507 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9508 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -05009509 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -05009510 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -05009511 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05009512 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
9513
James Smart549e55c2007-08-02 11:09:51 -04009514 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04009515 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04009516 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009517
James Smart858c9f62007-06-17 19:56:39 -05009518 lpfc_free_sysfs_attr(vport);
9519
James Smarteada2722008-12-04 22:39:13 -05009520 /* Release all the vports against this physical port */
9521 vports = lpfc_create_vport_work_array(phba);
9522 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -04009523 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
9524 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
9525 continue;
James Smarteada2722008-12-04 22:39:13 -05009526 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -04009527 }
James Smarteada2722008-12-04 22:39:13 -05009528 lpfc_destroy_vport_work_array(phba, vports);
9529
9530 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -05009531 fc_remove_host(shost);
9532 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04009533 lpfc_cleanup(vport);
9534
James Smart2e0fef82007-06-17 19:56:36 -05009535 /*
9536 * Bring down the SLI Layer. This step disable all interrupts,
9537 * clears the rings, discards all mailbox commands, and resets
9538 * the HBA.
9539 */
James Smarta257bf92009-04-06 18:48:10 -04009540
Justin P. Mattock48e34d02010-12-30 15:07:58 -08009541 /* HBA interrupt will be disabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -05009542 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -04009543 /* Stop kthread signal shall trigger work_done one more time */
9544 kthread_stop(phba->worker_thread);
9545 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -05009546 lpfc_sli_brdrestart(phba);
9547
James Smart72859902012-01-18 16:25:38 -05009548 kfree(phba->vpi_bmask);
9549 kfree(phba->vpi_ids);
9550
James Smart3772a992009-05-22 14:50:54 -04009551 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05009552 spin_lock_irq(&phba->hbalock);
9553 list_del_init(&vport->listentry);
9554 spin_unlock_irq(&phba->hbalock);
9555
James Smart858c9f62007-06-17 19:56:39 -05009556 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05009557
James Smart912e3ac2011-05-24 11:42:11 -04009558 /* Disable SR-IOV if enabled */
9559 if (phba->cfg_sriov_nr_virtfn)
9560 pci_disable_sriov(pdev);
9561
James Smart5b75da22008-12-04 22:39:35 -05009562 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009563 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -05009564
James Smart858c9f62007-06-17 19:56:39 -05009565 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05009566
9567 /*
9568 * Call scsi_free before mem_free since scsi bufs are released to their
9569 * corresponding pools here.
9570 */
9571 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -04009572 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -05009573
James Smart34b02dc2008-08-24 21:49:55 -04009574 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
9575 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -05009576
James Smart2e0fef82007-06-17 19:56:36 -05009577 /* Free resources associated with SLI2 interface */
9578 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -04009579 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -05009580
9581 /* unmap adapter SLIM and Control Registers */
9582 iounmap(phba->ctrl_regs_memmap_p);
9583 iounmap(phba->slim_memmap_p);
9584
James Smart3772a992009-05-22 14:50:54 -04009585 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -05009586
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05009587 pci_release_selected_regions(pdev, bars);
James Smart2e0fef82007-06-17 19:56:36 -05009588 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05009589}
9590
Linas Vepstas8d63f372007-02-14 14:28:36 -06009591/**
James Smart3772a992009-05-22 14:50:54 -04009592 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05009593 * @pdev: pointer to PCI device
9594 * @msg: power management message
9595 *
James Smart3772a992009-05-22 14:50:54 -04009596 * This routine is to be called from the kernel's PCI subsystem to support
9597 * system Power Management (PM) to device with SLI-3 interface spec. When
9598 * PM invokes this method, it quiesces the device by stopping the driver's
9599 * worker thread for the device, turning off device's interrupt and DMA,
9600 * and bring the device offline. Note that as the driver implements the
9601 * minimum PM requirements to a power-aware driver's PM support for the
9602 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
9603 * to the suspend() method call will be treated as SUSPEND and the driver will
9604 * fully reinitialize its device during resume() method call, the driver will
9605 * set device to PCI_D3hot state in PCI config space instead of setting it
9606 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -05009607 *
9608 * Return code
James Smart3772a992009-05-22 14:50:54 -04009609 * 0 - driver suspended the device
9610 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05009611 **/
9612static int
James Smart3772a992009-05-22 14:50:54 -04009613lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -05009614{
9615 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9616 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9617
9618 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9619 "0473 PCI device Power Management suspend.\n");
9620
9621 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -04009622 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3a55b532008-12-04 22:38:54 -05009623 lpfc_offline(phba);
9624 kthread_stop(phba->worker_thread);
9625
9626 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -04009627 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -05009628
9629 /* Save device state to PCI config space */
9630 pci_save_state(pdev);
9631 pci_set_power_state(pdev, PCI_D3hot);
9632
9633 return 0;
9634}
9635
9636/**
James Smart3772a992009-05-22 14:50:54 -04009637 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05009638 * @pdev: pointer to PCI device
9639 *
James Smart3772a992009-05-22 14:50:54 -04009640 * This routine is to be called from the kernel's PCI subsystem to support
9641 * system Power Management (PM) to device with SLI-3 interface spec. When PM
9642 * invokes this method, it restores the device's PCI config space state and
9643 * fully reinitializes the device and brings it online. Note that as the
9644 * driver implements the minimum PM requirements to a power-aware driver's
9645 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
9646 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
9647 * driver will fully reinitialize its device during resume() method call,
9648 * the device will be set to PCI_D0 directly in PCI config space before
9649 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -05009650 *
9651 * Return code
James Smart3772a992009-05-22 14:50:54 -04009652 * 0 - driver suspended the device
9653 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05009654 **/
9655static int
James Smart3772a992009-05-22 14:50:54 -04009656lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -05009657{
9658 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9659 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -05009660 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05009661 int error;
9662
9663 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9664 "0452 PCI device Power Management resume.\n");
9665
9666 /* Restore device state from PCI config space */
9667 pci_set_power_state(pdev, PCI_D0);
9668 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -04009669
James Smart1dfb5a42010-02-12 14:40:50 -05009670 /*
9671 * As the new kernel behavior of pci_restore_state() API call clears
9672 * device saved_state flag, need to save the restored state again.
9673 */
9674 pci_save_state(pdev);
9675
James Smart3a55b532008-12-04 22:38:54 -05009676 if (pdev->is_busmaster)
9677 pci_set_master(pdev);
9678
9679 /* Startup the kernel thread for this host adapter. */
9680 phba->worker_thread = kthread_run(lpfc_do_work, phba,
9681 "lpfc_worker_%d", phba->brd_no);
9682 if (IS_ERR(phba->worker_thread)) {
9683 error = PTR_ERR(phba->worker_thread);
9684 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9685 "0434 PM resume failed to start worker "
9686 "thread: error=x%x.\n", error);
9687 return error;
9688 }
9689
James Smart5b75da22008-12-04 22:39:35 -05009690 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009691 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05009692 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -05009693 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -05009694 "0430 PM resume Failed to enable interrupt\n");
9695 return -EIO;
9696 } else
9697 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05009698
9699 /* Restart HBA and bring it online */
9700 lpfc_sli_brdrestart(phba);
9701 lpfc_online(phba);
9702
James Smart5b75da22008-12-04 22:39:35 -05009703 /* Log the current active interrupt mode */
9704 lpfc_log_intr_mode(phba, phba->intr_mode);
9705
James Smart3a55b532008-12-04 22:38:54 -05009706 return 0;
9707}
9708
9709/**
James Smart891478a2009-11-18 15:40:23 -05009710 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
9711 * @phba: pointer to lpfc hba data structure.
9712 *
9713 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -04009714 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -05009715 **/
9716static void
9717lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
9718{
James Smarte2af0d22010-03-15 11:25:32 -04009719 struct lpfc_sli *psli = &phba->sli;
9720 struct lpfc_sli_ring *pring;
9721
James Smart891478a2009-11-18 15:40:23 -05009722 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9723 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -04009724
9725 /*
9726 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
9727 * and let the SCSI mid-layer to retry them to recover.
9728 */
9729 pring = &psli->ring[psli->fcp_ring];
9730 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart891478a2009-11-18 15:40:23 -05009731}
9732
9733/**
James Smart0d878412009-10-02 15:16:56 -04009734 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
9735 * @phba: pointer to lpfc hba data structure.
9736 *
9737 * This routine is called to prepare the SLI3 device for PCI slot reset. It
9738 * disables the device interrupt and pci device, and aborts the internal FCP
9739 * pending I/Os.
9740 **/
9741static void
9742lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
9743{
James Smart0d878412009-10-02 15:16:56 -04009744 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05009745 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -04009746
James Smart75baf692010-06-08 18:31:21 -04009747 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -04009748 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -04009749
James Smarte2af0d22010-03-15 11:25:32 -04009750 /* Block all SCSI devices' I/Os on the host */
9751 lpfc_scsi_dev_block(phba);
9752
James Smartea714f32013-04-17 20:18:47 -04009753 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
9754 lpfc_sli_flush_fcp_rings(phba);
9755
James Smarte2af0d22010-03-15 11:25:32 -04009756 /* stop all timers */
9757 lpfc_stop_hba_timers(phba);
9758
James Smart0d878412009-10-02 15:16:56 -04009759 /* Disable interrupt and pci device */
9760 lpfc_sli_disable_intr(phba);
9761 pci_disable_device(phba->pcidev);
James Smart0d878412009-10-02 15:16:56 -04009762}
9763
9764/**
9765 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
9766 * @phba: pointer to lpfc hba data structure.
9767 *
9768 * This routine is called to prepare the SLI3 device for PCI slot permanently
9769 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
9770 * pending I/Os.
9771 **/
9772static void
James Smart75baf692010-06-08 18:31:21 -04009773lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
James Smart0d878412009-10-02 15:16:56 -04009774{
9775 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05009776 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -04009777 /* Block all SCSI devices' I/Os on the host */
9778 lpfc_scsi_dev_block(phba);
9779
9780 /* stop all timers */
9781 lpfc_stop_hba_timers(phba);
9782
James Smart0d878412009-10-02 15:16:56 -04009783 /* Clean up all driver's outstanding SCSI I/Os */
9784 lpfc_sli_flush_fcp_rings(phba);
9785}
9786
9787/**
James Smart3772a992009-05-22 14:50:54 -04009788 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -04009789 * @pdev: pointer to PCI device.
9790 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009791 *
James Smart3772a992009-05-22 14:50:54 -04009792 * This routine is called from the PCI subsystem for I/O error handling to
9793 * device with SLI-3 interface spec. This function is called by the PCI
9794 * subsystem after a PCI bus error affecting this device has been detected.
9795 * When this function is invoked, it will need to stop all the I/Os and
9796 * interrupt(s) to the device. Once that is done, it will return
9797 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
9798 * as desired.
James Smarte59058c2008-08-24 21:49:00 -04009799 *
9800 * Return codes
James Smart0d878412009-10-02 15:16:56 -04009801 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -04009802 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
9803 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -04009804 **/
James Smart3772a992009-05-22 14:50:54 -04009805static pci_ers_result_t
9806lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009807{
James Smart51ef4c22007-08-02 11:10:31 -04009808 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9809 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009810
James Smart0d878412009-10-02 15:16:56 -04009811 switch (state) {
9812 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -05009813 /* Non-fatal error, prepare for recovery */
9814 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -04009815 return PCI_ERS_RESULT_CAN_RECOVER;
9816 case pci_channel_io_frozen:
9817 /* Fatal error, prepare for slot reset */
9818 lpfc_sli_prep_dev_for_reset(phba);
9819 return PCI_ERS_RESULT_NEED_RESET;
9820 case pci_channel_io_perm_failure:
9821 /* Permanent failure, prepare for device down */
James Smart75baf692010-06-08 18:31:21 -04009822 lpfc_sli_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009823 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -04009824 default:
9825 /* Unknown state, prepare and request slot reset */
9826 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9827 "0472 Unknown PCI error state: x%x\n", state);
9828 lpfc_sli_prep_dev_for_reset(phba);
9829 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -04009830 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06009831}
9832
9833/**
James Smart3772a992009-05-22 14:50:54 -04009834 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -04009835 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009836 *
James Smart3772a992009-05-22 14:50:54 -04009837 * This routine is called from the PCI subsystem for error handling to
9838 * device with SLI-3 interface spec. This is called after PCI bus has been
9839 * reset to restart the PCI card from scratch, as if from a cold-boot.
9840 * During the PCI subsystem error recovery, after driver returns
9841 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
9842 * recovery and then call this routine before calling the .resume method
9843 * to recover the device. This function will initialize the HBA device,
9844 * enable the interrupt, but it will just put the HBA to offline state
9845 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -04009846 *
9847 * Return codes
James Smart3772a992009-05-22 14:50:54 -04009848 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
9849 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -06009850 */
James Smart3772a992009-05-22 14:50:54 -04009851static pci_ers_result_t
9852lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009853{
James Smart51ef4c22007-08-02 11:10:31 -04009854 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9855 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009856 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -05009857 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009858
9859 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +11009860 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -06009861 printk(KERN_ERR "lpfc: Cannot re-enable "
9862 "PCI device after reset.\n");
9863 return PCI_ERS_RESULT_DISCONNECT;
9864 }
9865
James Smart97207482008-12-04 22:39:19 -05009866 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05009867
9868 /*
9869 * As the new kernel behavior of pci_restore_state() API call clears
9870 * device saved_state flag, need to save the restored state again.
9871 */
9872 pci_save_state(pdev);
9873
James Smart97207482008-12-04 22:39:19 -05009874 if (pdev->is_busmaster)
9875 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009876
James Smart92d7f7b2007-06-17 19:56:38 -05009877 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04009878 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05009879 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009880
James Smart5b75da22008-12-04 22:39:35 -05009881 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009882 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05009883 if (intr_mode == LPFC_INTR_ERROR) {
9884 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9885 "0427 Cannot re-enable interrupt after "
9886 "slot reset.\n");
9887 return PCI_ERS_RESULT_DISCONNECT;
9888 } else
9889 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009890
James Smart75baf692010-06-08 18:31:21 -04009891 /* Take device offline, it will perform cleanup */
James Smart618a5232012-06-12 13:54:36 -04009892 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009893 lpfc_offline(phba);
9894 lpfc_sli_brdrestart(phba);
9895
James Smart5b75da22008-12-04 22:39:35 -05009896 /* Log the current active interrupt mode */
9897 lpfc_log_intr_mode(phba, phba->intr_mode);
9898
Linas Vepstas8d63f372007-02-14 14:28:36 -06009899 return PCI_ERS_RESULT_RECOVERED;
9900}
9901
9902/**
James Smart3772a992009-05-22 14:50:54 -04009903 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -04009904 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -06009905 *
James Smart3772a992009-05-22 14:50:54 -04009906 * This routine is called from the PCI subsystem for error handling to device
9907 * with SLI-3 interface spec. It is called when kernel error recovery tells
9908 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
9909 * error recovery. After this call, traffic can start to flow from this device
9910 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009911 */
James Smart3772a992009-05-22 14:50:54 -04009912static void
9913lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009914{
James Smart51ef4c22007-08-02 11:10:31 -04009915 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9916 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009917
James Smarte2af0d22010-03-15 11:25:32 -04009918 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -04009919 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -04009920
9921 /* Clean up Advanced Error Reporting (AER) if needed */
9922 if (phba->hba_flag & HBA_AER_ENABLED)
9923 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009924}
9925
James Smart3772a992009-05-22 14:50:54 -04009926/**
James Smartda0436e2009-05-22 14:51:39 -04009927 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
9928 * @phba: pointer to lpfc hba data structure.
9929 *
9930 * returns the number of ELS/CT IOCBs to reserve
9931 **/
9932int
9933lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
9934{
9935 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
9936
James Smartf1126682009-06-10 17:22:44 -04009937 if (phba->sli_rev == LPFC_SLI_REV4) {
9938 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -04009939 return 10;
James Smartf1126682009-06-10 17:22:44 -04009940 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -04009941 return 25;
James Smartf1126682009-06-10 17:22:44 -04009942 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -04009943 return 50;
James Smartf1126682009-06-10 17:22:44 -04009944 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -04009945 return 100;
James Smart8a9d2e82012-05-09 21:16:12 -04009946 else if (max_xri <= 1536)
James Smart6a9c52c2009-10-02 15:16:51 -04009947 return 150;
James Smart8a9d2e82012-05-09 21:16:12 -04009948 else if (max_xri <= 2048)
9949 return 200;
9950 else
9951 return 250;
James Smartf1126682009-06-10 17:22:44 -04009952 } else
9953 return 0;
James Smartda0436e2009-05-22 14:51:39 -04009954}
9955
9956/**
James Smart52d52442011-05-24 11:42:45 -04009957 * lpfc_write_firmware - attempt to write a firmware image to the port
James Smart52d52442011-05-24 11:42:45 -04009958 * @fw: pointer to firmware image returned from request_firmware.
James Smartce396282012-09-29 11:30:56 -04009959 * @phba: pointer to lpfc hba data structure.
James Smart52d52442011-05-24 11:42:45 -04009960 *
James Smart52d52442011-05-24 11:42:45 -04009961 **/
James Smartce396282012-09-29 11:30:56 -04009962static void
9963lpfc_write_firmware(const struct firmware *fw, void *context)
James Smart52d52442011-05-24 11:42:45 -04009964{
James Smartce396282012-09-29 11:30:56 -04009965 struct lpfc_hba *phba = (struct lpfc_hba *)context;
James Smart6b5151f2012-01-18 16:24:06 -05009966 char fwrev[FW_REV_STR_SIZE];
James Smartce396282012-09-29 11:30:56 -04009967 struct lpfc_grp_hdr *image;
James Smart52d52442011-05-24 11:42:45 -04009968 struct list_head dma_buffer_list;
9969 int i, rc = 0;
9970 struct lpfc_dmabuf *dmabuf, *next;
9971 uint32_t offset = 0, temp_offset = 0;
9972
James Smartc71ab862012-10-31 14:44:33 -04009973 /* It can be null in no-wait mode, sanity check */
James Smartce396282012-09-29 11:30:56 -04009974 if (!fw) {
9975 rc = -ENXIO;
9976 goto out;
9977 }
9978 image = (struct lpfc_grp_hdr *)fw->data;
9979
James Smart52d52442011-05-24 11:42:45 -04009980 INIT_LIST_HEAD(&dma_buffer_list);
James Smart079b5c92011-08-21 21:48:49 -04009981 if ((be32_to_cpu(image->magic_number) != LPFC_GROUP_OJECT_MAGIC_NUM) ||
9982 (bf_get_be32(lpfc_grp_hdr_file_type, image) !=
9983 LPFC_FILE_TYPE_GROUP) ||
9984 (bf_get_be32(lpfc_grp_hdr_id, image) != LPFC_FILE_ID_GROUP) ||
9985 (be32_to_cpu(image->size) != fw->size)) {
James Smart52d52442011-05-24 11:42:45 -04009986 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9987 "3022 Invalid FW image found. "
James Smart079b5c92011-08-21 21:48:49 -04009988 "Magic:%x Type:%x ID:%x\n",
9989 be32_to_cpu(image->magic_number),
9990 bf_get_be32(lpfc_grp_hdr_file_type, image),
9991 bf_get_be32(lpfc_grp_hdr_id, image));
James Smartce396282012-09-29 11:30:56 -04009992 rc = -EINVAL;
9993 goto release_out;
James Smart52d52442011-05-24 11:42:45 -04009994 }
9995 lpfc_decode_firmware_rev(phba, fwrev, 1);
James Smart88a2cfb2011-07-22 18:36:33 -04009996 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
James Smart52d52442011-05-24 11:42:45 -04009997 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartce396282012-09-29 11:30:56 -04009998 "3023 Updating Firmware, Current Version:%s "
James Smart52d52442011-05-24 11:42:45 -04009999 "New Version:%s\n",
James Smart88a2cfb2011-07-22 18:36:33 -040010000 fwrev, image->revision);
James Smart52d52442011-05-24 11:42:45 -040010001 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
10002 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
10003 GFP_KERNEL);
10004 if (!dmabuf) {
10005 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040010006 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010007 }
10008 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
10009 SLI4_PAGE_SIZE,
10010 &dmabuf->phys,
10011 GFP_KERNEL);
10012 if (!dmabuf->virt) {
10013 kfree(dmabuf);
10014 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040010015 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010016 }
10017 list_add_tail(&dmabuf->list, &dma_buffer_list);
10018 }
10019 while (offset < fw->size) {
10020 temp_offset = offset;
10021 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
James Smart079b5c92011-08-21 21:48:49 -040010022 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
James Smart52d52442011-05-24 11:42:45 -040010023 memcpy(dmabuf->virt,
10024 fw->data + temp_offset,
James Smart079b5c92011-08-21 21:48:49 -040010025 fw->size - temp_offset);
10026 temp_offset = fw->size;
James Smart52d52442011-05-24 11:42:45 -040010027 break;
10028 }
James Smart52d52442011-05-24 11:42:45 -040010029 memcpy(dmabuf->virt, fw->data + temp_offset,
10030 SLI4_PAGE_SIZE);
James Smart88a2cfb2011-07-22 18:36:33 -040010031 temp_offset += SLI4_PAGE_SIZE;
James Smart52d52442011-05-24 11:42:45 -040010032 }
10033 rc = lpfc_wr_object(phba, &dma_buffer_list,
10034 (fw->size - offset), &offset);
James Smartce396282012-09-29 11:30:56 -040010035 if (rc)
10036 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010037 }
10038 rc = offset;
10039 }
James Smartce396282012-09-29 11:30:56 -040010040
10041release_out:
James Smart52d52442011-05-24 11:42:45 -040010042 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
10043 list_del(&dmabuf->list);
10044 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
10045 dmabuf->virt, dmabuf->phys);
10046 kfree(dmabuf);
10047 }
James Smartce396282012-09-29 11:30:56 -040010048 release_firmware(fw);
10049out:
10050 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartc71ab862012-10-31 14:44:33 -040010051 "3024 Firmware update done: %d.\n", rc);
James Smartce396282012-09-29 11:30:56 -040010052 return;
James Smart52d52442011-05-24 11:42:45 -040010053}
10054
10055/**
James Smartc71ab862012-10-31 14:44:33 -040010056 * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
10057 * @phba: pointer to lpfc hba data structure.
10058 *
10059 * This routine is called to perform Linux generic firmware upgrade on device
10060 * that supports such feature.
10061 **/
10062int
10063lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
10064{
10065 uint8_t file_name[ELX_MODEL_NAME_SIZE];
10066 int ret;
10067 const struct firmware *fw;
10068
10069 /* Only supported on SLI4 interface type 2 for now */
10070 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
10071 LPFC_SLI_INTF_IF_TYPE_2)
10072 return -EPERM;
10073
10074 snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
10075
10076 if (fw_upgrade == INT_FW_UPGRADE) {
10077 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
10078 file_name, &phba->pcidev->dev,
10079 GFP_KERNEL, (void *)phba,
10080 lpfc_write_firmware);
10081 } else if (fw_upgrade == RUN_FW_UPGRADE) {
10082 ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
10083 if (!ret)
10084 lpfc_write_firmware(fw, (void *)phba);
10085 } else {
10086 ret = -EINVAL;
10087 }
10088
10089 return ret;
10090}
10091
10092/**
James Smartda0436e2009-05-22 14:51:39 -040010093 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
10094 * @pdev: pointer to PCI device
10095 * @pid: pointer to PCI device identifier
10096 *
10097 * This routine is called from the kernel's PCI subsystem to device with
10098 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
10099 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
10100 * information of the device and driver to see if the driver state that it
10101 * can support this kind of device. If the match is successful, the driver
10102 * core invokes this routine. If this routine determines it can claim the HBA,
10103 * it does all the initialization that it needs to do to handle the HBA
10104 * properly.
10105 *
10106 * Return code
10107 * 0 - driver can claim the device
10108 * negative value - driver can not claim the device
10109 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010110static int
James Smartda0436e2009-05-22 14:51:39 -040010111lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
10112{
10113 struct lpfc_hba *phba;
10114 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -040010115 struct Scsi_Host *shost = NULL;
James Smartce396282012-09-29 11:30:56 -040010116 int error, ret;
James Smartda0436e2009-05-22 14:51:39 -040010117 uint32_t cfg_mode, intr_mode;
James Smart67d12732012-08-03 12:36:13 -040010118 int adjusted_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -040010119
10120 /* Allocate memory for HBA structure */
10121 phba = lpfc_hba_alloc(pdev);
10122 if (!phba)
10123 return -ENOMEM;
10124
10125 /* Perform generic PCI device enabling operation */
10126 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -040010127 if (error)
James Smartda0436e2009-05-22 14:51:39 -040010128 goto out_free_phba;
James Smartda0436e2009-05-22 14:51:39 -040010129
10130 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
10131 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
10132 if (error)
10133 goto out_disable_pci_dev;
10134
10135 /* Set up SLI-4 specific device PCI memory space */
10136 error = lpfc_sli4_pci_mem_setup(phba);
10137 if (error) {
10138 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10139 "1410 Failed to set up pci memory space.\n");
10140 goto out_disable_pci_dev;
10141 }
10142
10143 /* Set up phase-1 common device driver resources */
10144 error = lpfc_setup_driver_resource_phase1(phba);
10145 if (error) {
10146 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10147 "1411 Failed to set up driver resource.\n");
10148 goto out_unset_pci_mem_s4;
10149 }
10150
10151 /* Set up SLI-4 Specific device driver resources */
10152 error = lpfc_sli4_driver_resource_setup(phba);
10153 if (error) {
10154 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10155 "1412 Failed to set up driver resource.\n");
10156 goto out_unset_pci_mem_s4;
10157 }
10158
10159 /* Initialize and populate the iocb list per host */
James Smart2a9bf3d2010-06-07 15:24:45 -040010160
10161 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10162 "2821 initialize iocb list %d.\n",
10163 phba->cfg_iocb_cnt*1024);
10164 error = lpfc_init_iocb_list(phba, phba->cfg_iocb_cnt*1024);
10165
James Smartda0436e2009-05-22 14:51:39 -040010166 if (error) {
10167 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10168 "1413 Failed to initialize iocb list.\n");
10169 goto out_unset_driver_resource_s4;
10170 }
10171
James Smart19ca7602010-11-20 23:11:55 -050010172 INIT_LIST_HEAD(&phba->active_rrq_list);
James Smart7d791df2011-07-22 18:37:52 -040010173 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
James Smart19ca7602010-11-20 23:11:55 -050010174
James Smartda0436e2009-05-22 14:51:39 -040010175 /* Set up common device driver resources */
10176 error = lpfc_setup_driver_resource_phase2(phba);
10177 if (error) {
10178 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10179 "1414 Failed to set up driver resource.\n");
10180 goto out_free_iocb_list;
10181 }
10182
James Smart079b5c92011-08-21 21:48:49 -040010183 /* Get the default values for Model Name and Description */
10184 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
10185
James Smartda0436e2009-05-22 14:51:39 -040010186 /* Create SCSI host to the physical port */
10187 error = lpfc_create_shost(phba);
10188 if (error) {
10189 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10190 "1415 Failed to create scsi host.\n");
10191 goto out_unset_driver_resource;
10192 }
10193
10194 /* Configure sysfs attributes */
10195 vport = phba->pport;
10196 error = lpfc_alloc_sysfs_attr(vport);
10197 if (error) {
10198 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10199 "1416 Failed to allocate sysfs attr\n");
10200 goto out_destroy_shost;
10201 }
10202
James Smart6669f9b2009-10-02 15:16:45 -040010203 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -040010204 /* Now, trying to enable interrupt and bring up the device */
10205 cfg_mode = phba->cfg_use_msi;
James Smartda0436e2009-05-22 14:51:39 -040010206
James Smart7b15db32013-01-03 15:43:29 -050010207 /* Put device to a known state before enabling interrupt */
10208 lpfc_stop_port(phba);
10209 /* Configure and enable interrupt */
10210 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
10211 if (intr_mode == LPFC_INTR_ERROR) {
10212 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10213 "0426 Failed to enable interrupt.\n");
10214 error = -ENODEV;
10215 goto out_free_sysfs_attr;
James Smartda0436e2009-05-22 14:51:39 -040010216 }
James Smart7b15db32013-01-03 15:43:29 -050010217 /* Default to single EQ for non-MSI-X */
10218 if (phba->intr_type != MSIX)
10219 adjusted_fcp_io_channel = 1;
10220 else
10221 adjusted_fcp_io_channel = phba->cfg_fcp_io_channel;
10222 phba->cfg_fcp_io_channel = adjusted_fcp_io_channel;
10223 /* Set up SLI-4 HBA */
10224 if (lpfc_sli4_hba_setup(phba)) {
10225 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10226 "1421 Failed to set up hba\n");
10227 error = -ENODEV;
10228 goto out_disable_intr;
10229 }
10230
10231 /* Log the current active interrupt mode */
10232 phba->intr_mode = intr_mode;
10233 lpfc_log_intr_mode(phba, intr_mode);
James Smartda0436e2009-05-22 14:51:39 -040010234
10235 /* Perform post initialization setup */
10236 lpfc_post_init_setup(phba);
10237
James Smartc71ab862012-10-31 14:44:33 -040010238 /* check for firmware upgrade or downgrade */
10239 if (phba->cfg_request_firmware_upgrade)
10240 ret = lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
James Smart52d52442011-05-24 11:42:45 -040010241
James Smart1c6834a2009-07-19 10:01:26 -040010242 /* Check if there are static vports to be created. */
10243 lpfc_create_static_vport(phba);
James Smartda0436e2009-05-22 14:51:39 -040010244 return 0;
10245
10246out_disable_intr:
10247 lpfc_sli4_disable_intr(phba);
10248out_free_sysfs_attr:
10249 lpfc_free_sysfs_attr(vport);
10250out_destroy_shost:
10251 lpfc_destroy_shost(phba);
10252out_unset_driver_resource:
10253 lpfc_unset_driver_resource_phase2(phba);
10254out_free_iocb_list:
10255 lpfc_free_iocb_list(phba);
10256out_unset_driver_resource_s4:
10257 lpfc_sli4_driver_resource_unset(phba);
10258out_unset_pci_mem_s4:
10259 lpfc_sli4_pci_mem_unset(phba);
10260out_disable_pci_dev:
10261 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040010262 if (shost)
10263 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -040010264out_free_phba:
10265 lpfc_hba_free(phba);
10266 return error;
10267}
10268
10269/**
10270 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
10271 * @pdev: pointer to PCI device
10272 *
10273 * This routine is called from the kernel's PCI subsystem to device with
10274 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
10275 * removed from PCI bus, it performs all the necessary cleanup for the HBA
10276 * device to be removed from the PCI subsystem properly.
10277 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010278static void
James Smartda0436e2009-05-22 14:51:39 -040010279lpfc_pci_remove_one_s4(struct pci_dev *pdev)
10280{
10281 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10282 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
10283 struct lpfc_vport **vports;
10284 struct lpfc_hba *phba = vport->phba;
10285 int i;
10286
10287 /* Mark the device unloading flag */
10288 spin_lock_irq(&phba->hbalock);
10289 vport->load_flag |= FC_UNLOADING;
10290 spin_unlock_irq(&phba->hbalock);
10291
10292 /* Free the HBA sysfs attributes */
10293 lpfc_free_sysfs_attr(vport);
10294
10295 /* Release all the vports against this physical port */
10296 vports = lpfc_create_vport_work_array(phba);
10297 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040010298 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
10299 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
10300 continue;
James Smartda0436e2009-05-22 14:51:39 -040010301 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040010302 }
James Smartda0436e2009-05-22 14:51:39 -040010303 lpfc_destroy_vport_work_array(phba, vports);
10304
10305 /* Remove FC host and then SCSI host with the physical port */
10306 fc_remove_host(shost);
10307 scsi_remove_host(shost);
10308
10309 /* Perform cleanup on the physical port */
10310 lpfc_cleanup(vport);
10311
10312 /*
10313 * Bring down the SLI Layer. This step disables all interrupts,
10314 * clears the rings, discards all mailbox commands, and resets
10315 * the HBA FCoE function.
10316 */
10317 lpfc_debugfs_terminate(vport);
10318 lpfc_sli4_hba_unset(phba);
10319
10320 spin_lock_irq(&phba->hbalock);
10321 list_del_init(&vport->listentry);
10322 spin_unlock_irq(&phba->hbalock);
10323
James Smart3677a3a2010-09-29 11:19:14 -040010324 /* Perform scsi free before driver resource_unset since scsi
James Smartda0436e2009-05-22 14:51:39 -040010325 * buffers are released to their corresponding pools here.
10326 */
10327 lpfc_scsi_free(phba);
James Smart67d12732012-08-03 12:36:13 -040010328
James Smartda0436e2009-05-22 14:51:39 -040010329 lpfc_sli4_driver_resource_unset(phba);
10330
10331 /* Unmap adapter Control and Doorbell registers */
10332 lpfc_sli4_pci_mem_unset(phba);
10333
10334 /* Release PCI resources and disable device's PCI function */
10335 scsi_host_put(shost);
10336 lpfc_disable_pci_dev(phba);
10337
10338 /* Finally, free the driver's device data structure */
10339 lpfc_hba_free(phba);
10340
10341 return;
10342}
10343
10344/**
10345 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
10346 * @pdev: pointer to PCI device
10347 * @msg: power management message
10348 *
10349 * This routine is called from the kernel's PCI subsystem to support system
10350 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
10351 * this method, it quiesces the device by stopping the driver's worker
10352 * thread for the device, turning off device's interrupt and DMA, and bring
10353 * the device offline. Note that as the driver implements the minimum PM
10354 * requirements to a power-aware driver's PM support for suspend/resume -- all
10355 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
10356 * method call will be treated as SUSPEND and the driver will fully
10357 * reinitialize its device during resume() method call, the driver will set
10358 * device to PCI_D3hot state in PCI config space instead of setting it
10359 * according to the @msg provided by the PM.
10360 *
10361 * Return code
10362 * 0 - driver suspended the device
10363 * Error otherwise
10364 **/
10365static int
10366lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
10367{
10368 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10369 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10370
10371 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart75baf692010-06-08 18:31:21 -040010372 "2843 PCI device Power Management suspend.\n");
James Smartda0436e2009-05-22 14:51:39 -040010373
10374 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040010375 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smartda0436e2009-05-22 14:51:39 -040010376 lpfc_offline(phba);
10377 kthread_stop(phba->worker_thread);
10378
10379 /* Disable interrupt from device */
10380 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040010381 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -040010382
10383 /* Save device state to PCI config space */
10384 pci_save_state(pdev);
10385 pci_set_power_state(pdev, PCI_D3hot);
10386
10387 return 0;
10388}
10389
10390/**
10391 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
10392 * @pdev: pointer to PCI device
10393 *
10394 * This routine is called from the kernel's PCI subsystem to support system
10395 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
10396 * this method, it restores the device's PCI config space state and fully
10397 * reinitializes the device and brings it online. Note that as the driver
10398 * implements the minimum PM requirements to a power-aware driver's PM for
10399 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
10400 * to the suspend() method call will be treated as SUSPEND and the driver
10401 * will fully reinitialize its device during resume() method call, the device
10402 * will be set to PCI_D0 directly in PCI config space before restoring the
10403 * state.
10404 *
10405 * Return code
10406 * 0 - driver suspended the device
10407 * Error otherwise
10408 **/
10409static int
10410lpfc_pci_resume_one_s4(struct pci_dev *pdev)
10411{
10412 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10413 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10414 uint32_t intr_mode;
10415 int error;
10416
10417 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10418 "0292 PCI device Power Management resume.\n");
10419
10420 /* Restore device state from PCI config space */
10421 pci_set_power_state(pdev, PCI_D0);
10422 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050010423
10424 /*
10425 * As the new kernel behavior of pci_restore_state() API call clears
10426 * device saved_state flag, need to save the restored state again.
10427 */
10428 pci_save_state(pdev);
10429
James Smartda0436e2009-05-22 14:51:39 -040010430 if (pdev->is_busmaster)
10431 pci_set_master(pdev);
10432
10433 /* Startup the kernel thread for this host adapter. */
10434 phba->worker_thread = kthread_run(lpfc_do_work, phba,
10435 "lpfc_worker_%d", phba->brd_no);
10436 if (IS_ERR(phba->worker_thread)) {
10437 error = PTR_ERR(phba->worker_thread);
10438 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10439 "0293 PM resume failed to start worker "
10440 "thread: error=x%x.\n", error);
10441 return error;
10442 }
10443
10444 /* Configure and enable interrupt */
10445 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
10446 if (intr_mode == LPFC_INTR_ERROR) {
10447 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10448 "0294 PM resume Failed to enable interrupt\n");
10449 return -EIO;
10450 } else
10451 phba->intr_mode = intr_mode;
10452
10453 /* Restart HBA and bring it online */
10454 lpfc_sli_brdrestart(phba);
10455 lpfc_online(phba);
10456
10457 /* Log the current active interrupt mode */
10458 lpfc_log_intr_mode(phba, phba->intr_mode);
10459
10460 return 0;
10461}
10462
10463/**
James Smart75baf692010-06-08 18:31:21 -040010464 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
10465 * @phba: pointer to lpfc hba data structure.
10466 *
10467 * This routine is called to prepare the SLI4 device for PCI slot recover. It
10468 * aborts all the outstanding SCSI I/Os to the pci device.
10469 **/
10470static void
10471lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
10472{
10473 struct lpfc_sli *psli = &phba->sli;
10474 struct lpfc_sli_ring *pring;
10475
10476 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10477 "2828 PCI channel I/O abort preparing for recovery\n");
10478 /*
10479 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
10480 * and let the SCSI mid-layer to retry them to recover.
10481 */
10482 pring = &psli->ring[psli->fcp_ring];
10483 lpfc_sli_abort_iocb_ring(phba, pring);
10484}
10485
10486/**
10487 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
10488 * @phba: pointer to lpfc hba data structure.
10489 *
10490 * This routine is called to prepare the SLI4 device for PCI slot reset. It
10491 * disables the device interrupt and pci device, and aborts the internal FCP
10492 * pending I/Os.
10493 **/
10494static void
10495lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
10496{
10497 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10498 "2826 PCI channel disable preparing for reset\n");
10499
10500 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040010501 lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
James Smart75baf692010-06-08 18:31:21 -040010502
10503 /* Block all SCSI devices' I/Os on the host */
10504 lpfc_scsi_dev_block(phba);
10505
James Smartea714f32013-04-17 20:18:47 -040010506 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
10507 lpfc_sli_flush_fcp_rings(phba);
10508
James Smart75baf692010-06-08 18:31:21 -040010509 /* stop all timers */
10510 lpfc_stop_hba_timers(phba);
10511
10512 /* Disable interrupt and pci device */
10513 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040010514 lpfc_sli4_queue_destroy(phba);
James Smart75baf692010-06-08 18:31:21 -040010515 pci_disable_device(phba->pcidev);
James Smart75baf692010-06-08 18:31:21 -040010516}
10517
10518/**
10519 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
10520 * @phba: pointer to lpfc hba data structure.
10521 *
10522 * This routine is called to prepare the SLI4 device for PCI slot permanently
10523 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
10524 * pending I/Os.
10525 **/
10526static void
10527lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
10528{
10529 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10530 "2827 PCI channel permanent disable for failure\n");
10531
10532 /* Block all SCSI devices' I/Os on the host */
10533 lpfc_scsi_dev_block(phba);
10534
10535 /* stop all timers */
10536 lpfc_stop_hba_timers(phba);
10537
10538 /* Clean up all driver's outstanding SCSI I/Os */
10539 lpfc_sli_flush_fcp_rings(phba);
10540}
10541
10542/**
James Smartda0436e2009-05-22 14:51:39 -040010543 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
10544 * @pdev: pointer to PCI device.
10545 * @state: the current PCI connection state.
10546 *
10547 * This routine is called from the PCI subsystem for error handling to device
10548 * with SLI-4 interface spec. This function is called by the PCI subsystem
10549 * after a PCI bus error affecting this device has been detected. When this
10550 * function is invoked, it will need to stop all the I/Os and interrupt(s)
10551 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
10552 * for the PCI subsystem to perform proper recovery as desired.
10553 *
10554 * Return codes
10555 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10556 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10557 **/
10558static pci_ers_result_t
10559lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
10560{
James Smart75baf692010-06-08 18:31:21 -040010561 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10562 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10563
10564 switch (state) {
10565 case pci_channel_io_normal:
10566 /* Non-fatal error, prepare for recovery */
10567 lpfc_sli4_prep_dev_for_recover(phba);
10568 return PCI_ERS_RESULT_CAN_RECOVER;
10569 case pci_channel_io_frozen:
10570 /* Fatal error, prepare for slot reset */
10571 lpfc_sli4_prep_dev_for_reset(phba);
10572 return PCI_ERS_RESULT_NEED_RESET;
10573 case pci_channel_io_perm_failure:
10574 /* Permanent failure, prepare for device down */
10575 lpfc_sli4_prep_dev_for_perm_failure(phba);
10576 return PCI_ERS_RESULT_DISCONNECT;
10577 default:
10578 /* Unknown state, prepare and request slot reset */
10579 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10580 "2825 Unknown PCI error state: x%x\n", state);
10581 lpfc_sli4_prep_dev_for_reset(phba);
10582 return PCI_ERS_RESULT_NEED_RESET;
10583 }
James Smartda0436e2009-05-22 14:51:39 -040010584}
10585
10586/**
10587 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
10588 * @pdev: pointer to PCI device.
10589 *
10590 * This routine is called from the PCI subsystem for error handling to device
10591 * with SLI-4 interface spec. It is called after PCI bus has been reset to
10592 * restart the PCI card from scratch, as if from a cold-boot. During the
10593 * PCI subsystem error recovery, after the driver returns
10594 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
10595 * recovery and then call this routine before calling the .resume method to
10596 * recover the device. This function will initialize the HBA device, enable
10597 * the interrupt, but it will just put the HBA to offline state without
10598 * passing any I/O traffic.
10599 *
10600 * Return codes
10601 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10602 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10603 */
10604static pci_ers_result_t
10605lpfc_io_slot_reset_s4(struct pci_dev *pdev)
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 struct lpfc_sli *psli = &phba->sli;
10610 uint32_t intr_mode;
10611
10612 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
10613 if (pci_enable_device_mem(pdev)) {
10614 printk(KERN_ERR "lpfc: Cannot re-enable "
10615 "PCI device after reset.\n");
10616 return PCI_ERS_RESULT_DISCONNECT;
10617 }
10618
10619 pci_restore_state(pdev);
James Smart0a96e972011-07-22 18:37:28 -040010620
10621 /*
10622 * As the new kernel behavior of pci_restore_state() API call clears
10623 * device saved_state flag, need to save the restored state again.
10624 */
10625 pci_save_state(pdev);
10626
James Smart75baf692010-06-08 18:31:21 -040010627 if (pdev->is_busmaster)
10628 pci_set_master(pdev);
10629
10630 spin_lock_irq(&phba->hbalock);
10631 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
10632 spin_unlock_irq(&phba->hbalock);
10633
10634 /* Configure and enable interrupt */
10635 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
10636 if (intr_mode == LPFC_INTR_ERROR) {
10637 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10638 "2824 Cannot re-enable interrupt after "
10639 "slot reset.\n");
10640 return PCI_ERS_RESULT_DISCONNECT;
10641 } else
10642 phba->intr_mode = intr_mode;
10643
10644 /* Log the current active interrupt mode */
10645 lpfc_log_intr_mode(phba, phba->intr_mode);
10646
James Smartda0436e2009-05-22 14:51:39 -040010647 return PCI_ERS_RESULT_RECOVERED;
10648}
10649
10650/**
10651 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
10652 * @pdev: pointer to PCI device
10653 *
10654 * This routine is called from the PCI subsystem for error handling to device
10655 * with SLI-4 interface spec. It is called when kernel error recovery tells
10656 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
10657 * error recovery. After this call, traffic can start to flow from this device
10658 * again.
10659 **/
10660static void
10661lpfc_io_resume_s4(struct pci_dev *pdev)
10662{
James Smart75baf692010-06-08 18:31:21 -040010663 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10664 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10665
10666 /*
10667 * In case of slot reset, as function reset is performed through
10668 * mailbox command which needs DMA to be enabled, this operation
10669 * has to be moved to the io resume phase. Taking device offline
10670 * will perform the necessary cleanup.
10671 */
10672 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
10673 /* Perform device reset */
James Smart618a5232012-06-12 13:54:36 -040010674 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040010675 lpfc_offline(phba);
10676 lpfc_sli_brdrestart(phba);
10677 /* Bring the device back online */
10678 lpfc_online(phba);
10679 }
10680
10681 /* Clean up Advanced Error Reporting (AER) if needed */
10682 if (phba->hba_flag & HBA_AER_ENABLED)
10683 pci_cleanup_aer_uncorrect_error_status(pdev);
James Smartda0436e2009-05-22 14:51:39 -040010684}
10685
10686/**
James Smart3772a992009-05-22 14:50:54 -040010687 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
10688 * @pdev: pointer to PCI device
10689 * @pid: pointer to PCI device identifier
10690 *
10691 * This routine is to be registered to the kernel's PCI subsystem. When an
10692 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
10693 * at PCI device-specific information of the device and driver to see if the
10694 * driver state that it can support this kind of device. If the match is
10695 * successful, the driver core invokes this routine. This routine dispatches
10696 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
10697 * do all the initialization that it needs to do to handle the HBA device
10698 * properly.
10699 *
10700 * Return code
10701 * 0 - driver can claim the device
10702 * negative value - driver can not claim the device
10703 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010704static int
James Smart3772a992009-05-22 14:50:54 -040010705lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
10706{
10707 int rc;
James Smart8fa38512009-07-19 10:01:03 -040010708 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -040010709
James Smart28baac72010-02-12 14:42:03 -050010710 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -040010711 return -ENODEV;
10712
James Smart8fa38512009-07-19 10:01:03 -040010713 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -050010714 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -040010715 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040010716 else
James Smart3772a992009-05-22 14:50:54 -040010717 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040010718
James Smart3772a992009-05-22 14:50:54 -040010719 return rc;
10720}
10721
10722/**
10723 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
10724 * @pdev: pointer to PCI device
10725 *
10726 * This routine is to be registered to the kernel's PCI subsystem. When an
10727 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
10728 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
10729 * remove routine, which will perform all the necessary cleanup for the
10730 * device to be removed from the PCI subsystem properly.
10731 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010732static void
James Smart3772a992009-05-22 14:50:54 -040010733lpfc_pci_remove_one(struct pci_dev *pdev)
10734{
10735 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10736 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10737
10738 switch (phba->pci_dev_grp) {
10739 case LPFC_PCI_DEV_LP:
10740 lpfc_pci_remove_one_s3(pdev);
10741 break;
James Smartda0436e2009-05-22 14:51:39 -040010742 case LPFC_PCI_DEV_OC:
10743 lpfc_pci_remove_one_s4(pdev);
10744 break;
James Smart3772a992009-05-22 14:50:54 -040010745 default:
10746 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10747 "1424 Invalid PCI device group: 0x%x\n",
10748 phba->pci_dev_grp);
10749 break;
10750 }
10751 return;
10752}
10753
10754/**
10755 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
10756 * @pdev: pointer to PCI device
10757 * @msg: power management message
10758 *
10759 * This routine is to be registered to the kernel's PCI subsystem to support
10760 * system Power Management (PM). When PM invokes this method, it dispatches
10761 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
10762 * suspend the device.
10763 *
10764 * Return code
10765 * 0 - driver suspended the device
10766 * Error otherwise
10767 **/
10768static int
10769lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
10770{
10771 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10772 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10773 int rc = -ENODEV;
10774
10775 switch (phba->pci_dev_grp) {
10776 case LPFC_PCI_DEV_LP:
10777 rc = lpfc_pci_suspend_one_s3(pdev, msg);
10778 break;
James Smartda0436e2009-05-22 14:51:39 -040010779 case LPFC_PCI_DEV_OC:
10780 rc = lpfc_pci_suspend_one_s4(pdev, msg);
10781 break;
James Smart3772a992009-05-22 14:50:54 -040010782 default:
10783 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10784 "1425 Invalid PCI device group: 0x%x\n",
10785 phba->pci_dev_grp);
10786 break;
10787 }
10788 return rc;
10789}
10790
10791/**
10792 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
10793 * @pdev: pointer to PCI device
10794 *
10795 * This routine is to be registered to the kernel's PCI subsystem to support
10796 * system Power Management (PM). When PM invokes this method, it dispatches
10797 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
10798 * resume the device.
10799 *
10800 * Return code
10801 * 0 - driver suspended the device
10802 * Error otherwise
10803 **/
10804static int
10805lpfc_pci_resume_one(struct pci_dev *pdev)
10806{
10807 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10808 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10809 int rc = -ENODEV;
10810
10811 switch (phba->pci_dev_grp) {
10812 case LPFC_PCI_DEV_LP:
10813 rc = lpfc_pci_resume_one_s3(pdev);
10814 break;
James Smartda0436e2009-05-22 14:51:39 -040010815 case LPFC_PCI_DEV_OC:
10816 rc = lpfc_pci_resume_one_s4(pdev);
10817 break;
James Smart3772a992009-05-22 14:50:54 -040010818 default:
10819 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10820 "1426 Invalid PCI device group: 0x%x\n",
10821 phba->pci_dev_grp);
10822 break;
10823 }
10824 return rc;
10825}
10826
10827/**
10828 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
10829 * @pdev: pointer to PCI device.
10830 * @state: the current PCI connection state.
10831 *
10832 * This routine is registered to the PCI subsystem for error handling. This
10833 * function is called by the PCI subsystem after a PCI bus error affecting
10834 * this device has been detected. When this routine is invoked, it dispatches
10835 * the action to the proper SLI-3 or SLI-4 device error detected handling
10836 * routine, which will perform the proper error detected operation.
10837 *
10838 * Return codes
10839 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10840 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10841 **/
10842static pci_ers_result_t
10843lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
10844{
10845 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10846 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10847 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
10848
10849 switch (phba->pci_dev_grp) {
10850 case LPFC_PCI_DEV_LP:
10851 rc = lpfc_io_error_detected_s3(pdev, state);
10852 break;
James Smartda0436e2009-05-22 14:51:39 -040010853 case LPFC_PCI_DEV_OC:
10854 rc = lpfc_io_error_detected_s4(pdev, state);
10855 break;
James Smart3772a992009-05-22 14:50:54 -040010856 default:
10857 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10858 "1427 Invalid PCI device group: 0x%x\n",
10859 phba->pci_dev_grp);
10860 break;
10861 }
10862 return rc;
10863}
10864
10865/**
10866 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
10867 * @pdev: pointer to PCI device.
10868 *
10869 * This routine is registered to the PCI subsystem for error handling. This
10870 * function is called after PCI bus has been reset to restart the PCI card
10871 * from scratch, as if from a cold-boot. When this routine is invoked, it
10872 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
10873 * routine, which will perform the proper device reset.
10874 *
10875 * Return codes
10876 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10877 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10878 **/
10879static pci_ers_result_t
10880lpfc_io_slot_reset(struct pci_dev *pdev)
10881{
10882 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10883 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10884 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
10885
10886 switch (phba->pci_dev_grp) {
10887 case LPFC_PCI_DEV_LP:
10888 rc = lpfc_io_slot_reset_s3(pdev);
10889 break;
James Smartda0436e2009-05-22 14:51:39 -040010890 case LPFC_PCI_DEV_OC:
10891 rc = lpfc_io_slot_reset_s4(pdev);
10892 break;
James Smart3772a992009-05-22 14:50:54 -040010893 default:
10894 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10895 "1428 Invalid PCI device group: 0x%x\n",
10896 phba->pci_dev_grp);
10897 break;
10898 }
10899 return rc;
10900}
10901
10902/**
10903 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
10904 * @pdev: pointer to PCI device
10905 *
10906 * This routine is registered to the PCI subsystem for error handling. It
10907 * is called when kernel error recovery tells the lpfc driver that it is
10908 * OK to resume normal PCI operation after PCI bus error recovery. When
10909 * this routine is invoked, it dispatches the action to the proper SLI-3
10910 * or SLI-4 device io_resume routine, which will resume the device operation.
10911 **/
10912static void
10913lpfc_io_resume(struct pci_dev *pdev)
10914{
10915 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10916 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10917
10918 switch (phba->pci_dev_grp) {
10919 case LPFC_PCI_DEV_LP:
10920 lpfc_io_resume_s3(pdev);
10921 break;
James Smartda0436e2009-05-22 14:51:39 -040010922 case LPFC_PCI_DEV_OC:
10923 lpfc_io_resume_s4(pdev);
10924 break;
James Smart3772a992009-05-22 14:50:54 -040010925 default:
10926 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10927 "1429 Invalid PCI device group: 0x%x\n",
10928 phba->pci_dev_grp);
10929 break;
10930 }
10931 return;
10932}
10933
James Smart1ba981f2014-02-20 09:56:45 -050010934/**
10935 * lpfc_sli4_oas_verify - Verify OAS is supported by this adapter
10936 * @phba: pointer to lpfc hba data structure.
10937 *
10938 * This routine checks to see if OAS is supported for this adapter. If
10939 * supported, the configure Flash Optimized Fabric flag is set. Otherwise,
10940 * the enable oas flag is cleared and the pool created for OAS device data
10941 * is destroyed.
10942 *
10943 **/
10944void
10945lpfc_sli4_oas_verify(struct lpfc_hba *phba)
10946{
10947
10948 if (!phba->cfg_EnableXLane)
10949 return;
10950
10951 if (phba->sli4_hba.pc_sli4_params.oas_supported) {
10952 phba->cfg_fof = 1;
10953 } else {
10954 phba->cfg_EnableXLane = 0;
10955 if (phba->device_data_mem_pool)
10956 mempool_destroy(phba->device_data_mem_pool);
10957 phba->device_data_mem_pool = NULL;
10958 }
10959
10960 return;
10961}
10962
10963/**
10964 * lpfc_fof_queue_setup - Set up all the fof queues
10965 * @phba: pointer to lpfc hba data structure.
10966 *
10967 * This routine is invoked to set up all the fof queues for the FC HBA
10968 * operation.
10969 *
10970 * Return codes
10971 * 0 - successful
10972 * -ENOMEM - No available memory
10973 **/
10974int
10975lpfc_fof_queue_setup(struct lpfc_hba *phba)
10976{
10977 struct lpfc_sli *psli = &phba->sli;
10978 int rc;
10979
10980 rc = lpfc_eq_create(phba, phba->sli4_hba.fof_eq, LPFC_MAX_IMAX);
10981 if (rc)
10982 return -ENOMEM;
10983
10984 if (phba->cfg_EnableXLane) {
10985
10986 rc = lpfc_cq_create(phba, phba->sli4_hba.oas_cq,
10987 phba->sli4_hba.fof_eq, LPFC_WCQ, LPFC_FCP);
10988 if (rc)
10989 goto out_oas_cq;
10990
10991 rc = lpfc_wq_create(phba, phba->sli4_hba.oas_wq,
10992 phba->sli4_hba.oas_cq, LPFC_FCP);
10993 if (rc)
10994 goto out_oas_wq;
10995
10996 phba->sli4_hba.oas_cq->pring = &psli->ring[LPFC_FCP_OAS_RING];
10997 phba->sli4_hba.oas_ring = &psli->ring[LPFC_FCP_OAS_RING];
10998 }
10999
11000 return 0;
11001
11002out_oas_wq:
11003 if (phba->cfg_EnableXLane)
11004 lpfc_cq_destroy(phba, phba->sli4_hba.oas_cq);
11005out_oas_cq:
11006 lpfc_eq_destroy(phba, phba->sli4_hba.fof_eq);
11007 return rc;
11008
11009}
11010
11011/**
11012 * lpfc_fof_queue_create - Create all the fof queues
11013 * @phba: pointer to lpfc hba data structure.
11014 *
11015 * This routine is invoked to allocate all the fof queues for the FC HBA
11016 * operation. For each SLI4 queue type, the parameters such as queue entry
11017 * count (queue depth) shall be taken from the module parameter. For now,
11018 * we just use some constant number as place holder.
11019 *
11020 * Return codes
11021 * 0 - successful
11022 * -ENOMEM - No availble memory
11023 * -EIO - The mailbox failed to complete successfully.
11024 **/
11025int
11026lpfc_fof_queue_create(struct lpfc_hba *phba)
11027{
11028 struct lpfc_queue *qdesc;
11029
11030 /* Create FOF EQ */
11031 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
11032 phba->sli4_hba.eq_ecount);
11033 if (!qdesc)
11034 goto out_error;
11035
11036 phba->sli4_hba.fof_eq = qdesc;
11037
11038 if (phba->cfg_EnableXLane) {
11039
11040 /* Create OAS CQ */
11041 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
11042 phba->sli4_hba.cq_ecount);
11043 if (!qdesc)
11044 goto out_error;
11045
11046 phba->sli4_hba.oas_cq = qdesc;
11047
11048 /* Create OAS WQ */
11049 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
11050 phba->sli4_hba.wq_ecount);
11051 if (!qdesc)
11052 goto out_error;
11053
11054 phba->sli4_hba.oas_wq = qdesc;
11055
11056 }
11057 return 0;
11058
11059out_error:
11060 lpfc_fof_queue_destroy(phba);
11061 return -ENOMEM;
11062}
11063
11064/**
11065 * lpfc_fof_queue_destroy - Destroy all the fof queues
11066 * @phba: pointer to lpfc hba data structure.
11067 *
11068 * This routine is invoked to release all the SLI4 queues with the FC HBA
11069 * operation.
11070 *
11071 * Return codes
11072 * 0 - successful
11073 **/
11074int
11075lpfc_fof_queue_destroy(struct lpfc_hba *phba)
11076{
11077 /* Release FOF Event queue */
11078 if (phba->sli4_hba.fof_eq != NULL) {
11079 lpfc_sli4_queue_free(phba->sli4_hba.fof_eq);
11080 phba->sli4_hba.fof_eq = NULL;
11081 }
11082
11083 /* Release OAS Completion queue */
11084 if (phba->sli4_hba.oas_cq != NULL) {
11085 lpfc_sli4_queue_free(phba->sli4_hba.oas_cq);
11086 phba->sli4_hba.oas_cq = NULL;
11087 }
11088
11089 /* Release OAS Work queue */
11090 if (phba->sli4_hba.oas_wq != NULL) {
11091 lpfc_sli4_queue_free(phba->sli4_hba.oas_wq);
11092 phba->sli4_hba.oas_wq = NULL;
11093 }
11094 return 0;
11095}
11096
dea31012005-04-17 16:05:31 -050011097static struct pci_device_id lpfc_id_table[] = {
11098 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
11099 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -040011100 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
11101 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011102 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
11103 PCI_ANY_ID, PCI_ANY_ID, },
11104 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
11105 PCI_ANY_ID, PCI_ANY_ID, },
11106 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
11107 PCI_ANY_ID, PCI_ANY_ID, },
11108 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
11109 PCI_ANY_ID, PCI_ANY_ID, },
11110 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
11111 PCI_ANY_ID, PCI_ANY_ID, },
11112 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
11113 PCI_ANY_ID, PCI_ANY_ID, },
11114 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
11115 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050011116 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
11117 PCI_ANY_ID, PCI_ANY_ID, },
11118 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
11119 PCI_ANY_ID, PCI_ANY_ID, },
11120 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
11121 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011122 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
11123 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050011124 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
11125 PCI_ANY_ID, PCI_ANY_ID, },
11126 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
11127 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011128 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
11129 PCI_ANY_ID, PCI_ANY_ID, },
11130 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
11131 PCI_ANY_ID, PCI_ANY_ID, },
11132 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
11133 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -040011134 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
11135 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050011136 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
11137 PCI_ANY_ID, PCI_ANY_ID, },
11138 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
11139 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011140 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
11141 PCI_ANY_ID, PCI_ANY_ID, },
11142 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
11143 PCI_ANY_ID, PCI_ANY_ID, },
11144 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
11145 PCI_ANY_ID, PCI_ANY_ID, },
11146 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
11147 PCI_ANY_ID, PCI_ANY_ID, },
11148 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
11149 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050011150 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
11151 PCI_ANY_ID, PCI_ANY_ID, },
11152 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
11153 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -040011154 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
11155 PCI_ANY_ID, PCI_ANY_ID, },
11156 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
11157 PCI_ANY_ID, PCI_ANY_ID, },
11158 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
11159 PCI_ANY_ID, PCI_ANY_ID, },
11160 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
11161 PCI_ANY_ID, PCI_ANY_ID, },
11162 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
11163 PCI_ANY_ID, PCI_ANY_ID, },
11164 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
11165 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -040011166 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
11167 PCI_ANY_ID, PCI_ANY_ID, },
11168 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
11169 PCI_ANY_ID, PCI_ANY_ID, },
11170 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
11171 PCI_ANY_ID, PCI_ANY_ID, },
James Smart3772a992009-05-22 14:50:54 -040011172 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
11173 PCI_ANY_ID, PCI_ANY_ID, },
James Smarta747c9c2009-11-18 15:41:10 -050011174 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
11175 PCI_ANY_ID, PCI_ANY_ID, },
11176 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
James Smart6669f9b2009-10-02 15:16:45 -040011177 PCI_ANY_ID, PCI_ANY_ID, },
James Smart98fc5dd2010-06-07 15:24:29 -040011178 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BALIUS,
11179 PCI_ANY_ID, PCI_ANY_ID, },
James Smart085c6472010-11-20 23:11:37 -050011180 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC,
11181 PCI_ANY_ID, PCI_ANY_ID, },
11182 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE,
11183 PCI_ANY_ID, PCI_ANY_ID, },
James Smartc0c11512011-05-24 11:41:34 -040011184 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC_VF,
11185 PCI_ANY_ID, PCI_ANY_ID, },
11186 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE_VF,
11187 PCI_ANY_ID, PCI_ANY_ID, },
James Smartf8cafd32012-08-03 12:42:51 -040011188 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SKYHAWK,
11189 PCI_ANY_ID, PCI_ANY_ID, },
11190 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SKYHAWK_VF,
11191 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011192 { 0 }
11193};
11194
11195MODULE_DEVICE_TABLE(pci, lpfc_id_table);
11196
Stephen Hemmingera55b2d22012-09-07 09:33:16 -070011197static const struct pci_error_handlers lpfc_err_handler = {
Linas Vepstas8d63f372007-02-14 14:28:36 -060011198 .error_detected = lpfc_io_error_detected,
11199 .slot_reset = lpfc_io_slot_reset,
11200 .resume = lpfc_io_resume,
11201};
11202
dea31012005-04-17 16:05:31 -050011203static struct pci_driver lpfc_driver = {
11204 .name = LPFC_DRIVER_NAME,
11205 .id_table = lpfc_id_table,
11206 .probe = lpfc_pci_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011207 .remove = lpfc_pci_remove_one,
James Smart3a55b532008-12-04 22:38:54 -050011208 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -040011209 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -050011210 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -050011211};
11212
James Smart3ef6d242012-01-18 16:23:48 -050011213static const struct file_operations lpfc_mgmt_fop = {
Al Viro858feac2013-04-14 22:39:37 -040011214 .owner = THIS_MODULE,
James Smart3ef6d242012-01-18 16:23:48 -050011215};
11216
11217static struct miscdevice lpfc_mgmt_dev = {
11218 .minor = MISC_DYNAMIC_MINOR,
11219 .name = "lpfcmgmt",
11220 .fops = &lpfc_mgmt_fop,
11221};
11222
James Smarte59058c2008-08-24 21:49:00 -040011223/**
James Smart3621a712009-04-06 18:47:14 -040011224 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -040011225 *
11226 * This routine is to be invoked when the lpfc module is loaded into the
11227 * kernel. The special kernel macro module_init() is used to indicate the
11228 * role of this routine to the kernel as lpfc module entry point.
11229 *
11230 * Return codes
11231 * 0 - successful
11232 * -ENOMEM - FC attach transport failed
11233 * all others - failed
11234 */
dea31012005-04-17 16:05:31 -050011235static int __init
11236lpfc_init(void)
11237{
James Smart7bb03bb2013-04-17 20:19:16 -040011238 int cpu;
dea31012005-04-17 16:05:31 -050011239 int error = 0;
11240
11241 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040011242 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -050011243
James Smart3ef6d242012-01-18 16:23:48 -050011244 error = misc_register(&lpfc_mgmt_dev);
11245 if (error)
11246 printk(KERN_ERR "Could not register lpfcmgmt device, "
11247 "misc_register returned with status %d", error);
11248
James Smart7ee5d432007-10-27 13:37:17 -040011249 if (lpfc_enable_npiv) {
11250 lpfc_transport_functions.vport_create = lpfc_vport_create;
11251 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
11252 }
dea31012005-04-17 16:05:31 -050011253 lpfc_transport_template =
11254 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -040011255 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -050011256 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -040011257 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -040011258 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -040011259 fc_attach_transport(&lpfc_vport_transport_functions);
11260 if (lpfc_vport_transport_template == NULL) {
11261 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040011262 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -040011263 }
James Smart7ee5d432007-10-27 13:37:17 -040011264 }
James Smart7bb03bb2013-04-17 20:19:16 -040011265
11266 /* Initialize in case vector mapping is needed */
James Smartb246de12013-05-31 17:03:07 -040011267 lpfc_used_cpu = NULL;
11268 lpfc_present_cpu = 0;
11269 for_each_present_cpu(cpu)
11270 lpfc_present_cpu++;
James Smart7bb03bb2013-04-17 20:19:16 -040011271
dea31012005-04-17 16:05:31 -050011272 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -050011273 if (error) {
dea31012005-04-17 16:05:31 -050011274 fc_release_transport(lpfc_transport_template);
James Smartd7c255b2008-08-24 21:50:00 -040011275 if (lpfc_enable_npiv)
11276 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -050011277 }
dea31012005-04-17 16:05:31 -050011278
11279 return error;
11280}
11281
James Smarte59058c2008-08-24 21:49:00 -040011282/**
James Smart3621a712009-04-06 18:47:14 -040011283 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -040011284 *
11285 * This routine is invoked when the lpfc module is removed from the kernel.
11286 * The special kernel macro module_exit() is used to indicate the role of
11287 * this routine to the kernel as lpfc module exit point.
11288 */
dea31012005-04-17 16:05:31 -050011289static void __exit
11290lpfc_exit(void)
11291{
James Smart3ef6d242012-01-18 16:23:48 -050011292 misc_deregister(&lpfc_mgmt_dev);
dea31012005-04-17 16:05:31 -050011293 pci_unregister_driver(&lpfc_driver);
11294 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040011295 if (lpfc_enable_npiv)
11296 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -050011297 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040011298 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
11299 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050011300 (1L << _dump_buf_data_order), _dump_buf_data);
11301 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
11302 }
11303
11304 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -040011305 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
11306 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050011307 (1L << _dump_buf_dif_order), _dump_buf_dif);
11308 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
11309 }
James Smartb246de12013-05-31 17:03:07 -040011310 kfree(lpfc_used_cpu);
dea31012005-04-17 16:05:31 -050011311}
11312
11313module_init(lpfc_init);
11314module_exit(lpfc_exit);
11315MODULE_LICENSE("GPL");
11316MODULE_DESCRIPTION(LPFC_MODULE_DESC);
11317MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
11318MODULE_VERSION("0:" LPFC_DRIVER_VERSION);