blob: 38a4119d6294bd7d58af09b944e827c7e931b262 [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smart16a59fb2014-04-04 13:52:41 -04004 * Copyright (C) 2004-2014 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/delay.h>
24#include <linux/dma-mapping.h>
25#include <linux/idr.h>
26#include <linux/interrupt.h>
Paul Gortmakeracf3368f2011-05-27 09:47:43 -040027#include <linux/module.h>
dea31012005-04-17 16:05:31 -050028#include <linux/kthread.h>
29#include <linux/pci.h>
30#include <linux/spinlock.h>
James Smart92d7f7b2007-06-17 19:56:38 -050031#include <linux/ctype.h>
James Smart0d878412009-10-02 15:16:56 -040032#include <linux/aer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
James Smart52d52442011-05-24 11:42:45 -040034#include <linux/firmware.h>
James Smart3ef6d242012-01-18 16:23:48 -050035#include <linux/miscdevice.h>
James Smart7bb03bb2013-04-17 20:19:16 -040036#include <linux/percpu.h>
dea31012005-04-17 16:05:31 -050037
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040038#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050039#include <scsi/scsi_device.h>
40#include <scsi/scsi_host.h>
41#include <scsi/scsi_transport_fc.h>
42
James Smartda0436e2009-05-22 14:51:39 -040043#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050044#include "lpfc_hw.h"
45#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040046#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040047#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050048#include "lpfc_disc.h"
49#include "lpfc_scsi.h"
50#include "lpfc.h"
51#include "lpfc_logmsg.h"
52#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050053#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050054#include "lpfc_version.h"
55
James Smart81301a92008-12-04 22:39:46 -050056char *_dump_buf_data;
57unsigned long _dump_buf_data_order;
58char *_dump_buf_dif;
59unsigned long _dump_buf_dif_order;
60spinlock_t _dump_buf_lock;
61
James Smart7bb03bb2013-04-17 20:19:16 -040062/* Used when mapping IRQ vectors in a driver centric manner */
James Smartb246de12013-05-31 17:03:07 -040063uint16_t *lpfc_used_cpu;
64uint32_t lpfc_present_cpu;
James Smart7bb03bb2013-04-17 20:19:16 -040065
dea31012005-04-17 16:05:31 -050066static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
67static int lpfc_post_rcv_buf(struct lpfc_hba *);
James Smart5350d872011-10-10 21:33:49 -040068static int lpfc_sli4_queue_verify(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040069static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
70static int lpfc_setup_endian_order(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040071static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
James Smart8a9d2e82012-05-09 21:16:12 -040072static void lpfc_free_els_sgl_list(struct lpfc_hba *);
73static void lpfc_init_sgl_list(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040074static int lpfc_init_active_sgl_array(struct lpfc_hba *);
75static void lpfc_free_active_sgl(struct lpfc_hba *);
76static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
77static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
78static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
79static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
80static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
James Smart618a5232012-06-12 13:54:36 -040081static void lpfc_sli4_disable_intr(struct lpfc_hba *);
82static uint32_t lpfc_sli4_enable_intr(struct lpfc_hba *, uint32_t);
James Smart1ba981f2014-02-20 09:56:45 -050083static void lpfc_sli4_oas_verify(struct lpfc_hba *phba);
dea31012005-04-17 16:05:31 -050084
85static struct scsi_transport_template *lpfc_transport_template = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050086static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea31012005-04-17 16:05:31 -050087static DEFINE_IDR(lpfc_hba_index);
88
James Smarte59058c2008-08-24 21:49:00 -040089/**
James Smart3621a712009-04-06 18:47:14 -040090 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
James Smarte59058c2008-08-24 21:49:00 -040091 * @phba: pointer to lpfc hba data structure.
92 *
93 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
94 * mailbox command. It retrieves the revision information from the HBA and
95 * collects the Vital Product Data (VPD) about the HBA for preparing the
96 * configuration of the HBA.
97 *
98 * Return codes:
99 * 0 - success.
100 * -ERESTART - requests the SLI layer to reset the HBA and try again.
101 * Any other value - indicates an error.
102 **/
dea31012005-04-17 16:05:31 -0500103int
James Smart2e0fef82007-06-17 19:56:36 -0500104lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500105{
106 lpfc_vpd_t *vp = &phba->vpd;
107 int i = 0, rc;
108 LPFC_MBOXQ_t *pmb;
109 MAILBOX_t *mb;
110 char *lpfc_vpd_data = NULL;
111 uint16_t offset = 0;
112 static char licensed[56] =
113 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -0400114 static int init_key = 1;
dea31012005-04-17 16:05:31 -0500115
116 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
117 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500118 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500119 return -ENOMEM;
120 }
121
James Smart04c68492009-05-22 14:52:52 -0400122 mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -0500123 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500124
125 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -0400126 if (init_key) {
127 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -0500128
James Smart65a29c12006-07-06 15:50:50 -0400129 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
130 *ptext = cpu_to_be32(*ptext);
131 init_key = 0;
132 }
dea31012005-04-17 16:05:31 -0500133
134 lpfc_read_nv(phba, pmb);
135 memset((char*)mb->un.varRDnvp.rsvd3, 0,
136 sizeof (mb->un.varRDnvp.rsvd3));
137 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
138 sizeof (licensed));
139
140 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
141
142 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500143 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smarte8b62012007-08-02 11:10:09 -0400144 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500145 "error, mbxCmd x%x READ_NVPARM, "
146 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500147 mb->mbxCommand, mb->mbxStatus);
148 mempool_free(pmb, phba->mbox_mem_pool);
149 return -ERESTART;
150 }
151 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500152 sizeof(phba->wwnn));
153 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
154 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500155 }
156
James Smart92d7f7b2007-06-17 19:56:38 -0500157 phba->sli3_options = 0x0;
158
dea31012005-04-17 16:05:31 -0500159 /* Setup and issue mailbox READ REV command */
160 lpfc_read_rev(phba, pmb);
161 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
162 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500163 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400164 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500165 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500166 mb->mbxCommand, mb->mbxStatus);
167 mempool_free( pmb, phba->mbox_mem_pool);
168 return -ERESTART;
169 }
170
James Smart92d7f7b2007-06-17 19:56:38 -0500171
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500172 /*
173 * The value of rr must be 1 since the driver set the cv field to 1.
174 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500175 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500176 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500177 vp->rev.rBit = 0;
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500178 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400179 "0440 Adapter failed to init, READ_REV has "
180 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500181 mempool_free(pmb, phba->mbox_mem_pool);
182 return -ERESTART;
dea31012005-04-17 16:05:31 -0500183 }
184
James Smart495a7142008-06-14 22:52:59 -0400185 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
186 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -0500187 return -EINVAL;
James Smart495a7142008-06-14 22:52:59 -0400188 }
James Smarted957682007-06-17 19:56:37 -0500189
dea31012005-04-17 16:05:31 -0500190 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500191 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500192 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500193 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
194 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
195 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
196 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500197 vp->rev.biuRev = mb->un.varRdRev.biuRev;
198 vp->rev.smRev = mb->un.varRdRev.smRev;
199 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
200 vp->rev.endecRev = mb->un.varRdRev.endecRev;
201 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
202 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
203 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
204 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
205 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
206 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
207
James Smart92d7f7b2007-06-17 19:56:38 -0500208 /* If the sli feature level is less then 9, we must
209 * tear down all RPIs and VPIs on link down if NPIV
210 * is enabled.
211 */
212 if (vp->rev.feaLevelHigh < 9)
213 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
214
dea31012005-04-17 16:05:31 -0500215 if (lpfc_is_LC_HBA(phba->pcidev->device))
216 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
217 sizeof (phba->RandomData));
218
dea31012005-04-17 16:05:31 -0500219 /* Get adapter VPD information */
dea31012005-04-17 16:05:31 -0500220 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
221 if (!lpfc_vpd_data)
James Smartd7c255b2008-08-24 21:50:00 -0400222 goto out_free_mbox;
dea31012005-04-17 16:05:31 -0500223 do {
James Smarta0c87cb2009-07-19 10:01:10 -0400224 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
dea31012005-04-17 16:05:31 -0500225 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
226
227 if (rc != MBX_SUCCESS) {
228 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400229 "0441 VPD not present on adapter, "
dea31012005-04-17 16:05:31 -0500230 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500231 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500232 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500233 }
James Smart04c68492009-05-22 14:52:52 -0400234 /* dump mem may return a zero when finished or we got a
235 * mailbox error, either way we are done.
236 */
237 if (mb->un.varDmp.word_cnt == 0)
238 break;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500239 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
240 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
James Smartd7c255b2008-08-24 21:50:00 -0400241 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
242 lpfc_vpd_data + offset,
James Smart92d7f7b2007-06-17 19:56:38 -0500243 mb->un.varDmp.word_cnt);
dea31012005-04-17 16:05:31 -0500244 offset += mb->un.varDmp.word_cnt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500245 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
246 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500247
248 kfree(lpfc_vpd_data);
dea31012005-04-17 16:05:31 -0500249out_free_mbox:
250 mempool_free(pmb, phba->mbox_mem_pool);
251 return 0;
252}
253
James Smarte59058c2008-08-24 21:49:00 -0400254/**
James Smart3621a712009-04-06 18:47:14 -0400255 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
James Smarte59058c2008-08-24 21:49:00 -0400256 * @phba: pointer to lpfc hba data structure.
257 * @pmboxq: pointer to the driver internal queue element for mailbox command.
258 *
259 * This is the completion handler for driver's configuring asynchronous event
260 * mailbox command to the device. If the mailbox command returns successfully,
261 * it will set internal async event support flag to 1; otherwise, it will
262 * set internal async event support flag to 0.
263 **/
James Smart57127f12007-10-27 13:37:05 -0400264static void
265lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
266{
James Smart04c68492009-05-22 14:52:52 -0400267 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
James Smart57127f12007-10-27 13:37:05 -0400268 phba->temp_sensor_support = 1;
269 else
270 phba->temp_sensor_support = 0;
271 mempool_free(pmboxq, phba->mbox_mem_pool);
272 return;
273}
274
James Smarte59058c2008-08-24 21:49:00 -0400275/**
James Smart3621a712009-04-06 18:47:14 -0400276 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
James Smart97207482008-12-04 22:39:19 -0500277 * @phba: pointer to lpfc hba data structure.
278 * @pmboxq: pointer to the driver internal queue element for mailbox command.
279 *
280 * This is the completion handler for dump mailbox command for getting
281 * wake up parameters. When this command complete, the response contain
282 * Option rom version of the HBA. This function translate the version number
283 * into a human readable string and store it in OptionROMVersion.
284 **/
285static void
286lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
287{
288 struct prog_id *prg;
289 uint32_t prog_id_word;
290 char dist = ' ';
291 /* character array used for decoding dist type. */
292 char dist_char[] = "nabx";
293
James Smart04c68492009-05-22 14:52:52 -0400294 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
James Smart9f1e1b52008-12-04 22:39:40 -0500295 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500296 return;
James Smart9f1e1b52008-12-04 22:39:40 -0500297 }
James Smart97207482008-12-04 22:39:19 -0500298
299 prg = (struct prog_id *) &prog_id_word;
300
301 /* word 7 contain option rom version */
James Smart04c68492009-05-22 14:52:52 -0400302 prog_id_word = pmboxq->u.mb.un.varWords[7];
James Smart97207482008-12-04 22:39:19 -0500303
304 /* Decode the Option rom version word to a readable string */
305 if (prg->dist < 4)
306 dist = dist_char[prg->dist];
307
308 if ((prg->dist == 3) && (prg->num == 0))
309 sprintf(phba->OptionROMVersion, "%d.%d%d",
310 prg->ver, prg->rev, prg->lev);
311 else
312 sprintf(phba->OptionROMVersion, "%d.%d%d%c%d",
313 prg->ver, prg->rev, prg->lev,
314 dist, prg->num);
James Smart9f1e1b52008-12-04 22:39:40 -0500315 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500316 return;
317}
318
319/**
James Smart05580562011-05-24 11:40:48 -0400320 * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
321 * cfg_soft_wwnn, cfg_soft_wwpn
322 * @vport: pointer to lpfc vport data structure.
323 *
324 *
325 * Return codes
326 * None.
327 **/
328void
329lpfc_update_vport_wwn(struct lpfc_vport *vport)
330{
331 /* If the soft name exists then update it using the service params */
332 if (vport->phba->cfg_soft_wwnn)
333 u64_to_wwn(vport->phba->cfg_soft_wwnn,
334 vport->fc_sparam.nodeName.u.wwn);
335 if (vport->phba->cfg_soft_wwpn)
336 u64_to_wwn(vport->phba->cfg_soft_wwpn,
337 vport->fc_sparam.portName.u.wwn);
338
339 /*
340 * If the name is empty or there exists a soft name
341 * then copy the service params name, otherwise use the fc name
342 */
343 if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
344 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
345 sizeof(struct lpfc_name));
346 else
347 memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
348 sizeof(struct lpfc_name));
349
350 if (vport->fc_portname.u.wwn[0] == 0 || vport->phba->cfg_soft_wwpn)
351 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
352 sizeof(struct lpfc_name));
353 else
354 memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
355 sizeof(struct lpfc_name));
356}
357
358/**
James Smart3621a712009-04-06 18:47:14 -0400359 * lpfc_config_port_post - Perform lpfc initialization after config port
James Smarte59058c2008-08-24 21:49:00 -0400360 * @phba: pointer to lpfc hba data structure.
361 *
362 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
363 * command call. It performs all internal resource and state setups on the
364 * port: post IOCB buffers, enable appropriate host interrupt attentions,
365 * ELS ring timers, etc.
366 *
367 * Return codes
368 * 0 - success.
369 * Any other value - error.
370 **/
dea31012005-04-17 16:05:31 -0500371int
James Smart2e0fef82007-06-17 19:56:36 -0500372lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500373{
James Smart2e0fef82007-06-17 19:56:36 -0500374 struct lpfc_vport *vport = phba->pport;
James Smarta257bf92009-04-06 18:48:10 -0400375 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -0500376 LPFC_MBOXQ_t *pmb;
377 MAILBOX_t *mb;
378 struct lpfc_dmabuf *mp;
379 struct lpfc_sli *psli = &phba->sli;
380 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500381 int i, j;
382 int rc;
dea31012005-04-17 16:05:31 -0500383
James Smart7af67052007-10-27 13:38:11 -0400384 spin_lock_irq(&phba->hbalock);
385 /*
386 * If the Config port completed correctly the HBA is not
387 * over heated any more.
388 */
389 if (phba->over_temp_state == HBA_OVER_TEMP)
390 phba->over_temp_state = HBA_NORMAL_TEMP;
391 spin_unlock_irq(&phba->hbalock);
392
dea31012005-04-17 16:05:31 -0500393 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
394 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500395 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500396 return -ENOMEM;
397 }
James Smart04c68492009-05-22 14:52:52 -0400398 mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500399
dea31012005-04-17 16:05:31 -0500400 /* Get login parameters for NID. */
James Smart9f1177a2010-02-26 14:12:57 -0500401 rc = lpfc_read_sparam(phba, pmb, 0);
402 if (rc) {
403 mempool_free(pmb, phba->mbox_mem_pool);
404 return -ENOMEM;
405 }
406
James Smarted957682007-06-17 19:56:37 -0500407 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500408 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500409 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400410 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500411 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500412 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500413 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500414 mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart9f1177a2010-02-26 14:12:57 -0500415 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500416 lpfc_mbuf_free(phba, mp->virt, mp->phys);
417 kfree(mp);
418 return -EIO;
419 }
420
421 mp = (struct lpfc_dmabuf *) pmb->context1;
422
James Smart2e0fef82007-06-17 19:56:36 -0500423 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500424 lpfc_mbuf_free(phba, mp->virt, mp->phys);
425 kfree(mp);
426 pmb->context1 = NULL;
James Smart05580562011-05-24 11:40:48 -0400427 lpfc_update_vport_wwn(vport);
James Smarta257bf92009-04-06 18:48:10 -0400428
429 /* Update the fc_host data structures with new wwn. */
430 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
431 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart21e9a0a2009-05-22 14:53:21 -0400432 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smarta257bf92009-04-06 18:48:10 -0400433
dea31012005-04-17 16:05:31 -0500434 /* If no serial number in VPD data, use low 6 bytes of WWNN */
435 /* This should be consolidated into parse_vpd ? - mr */
436 if (phba->SerialNumber[0] == 0) {
437 uint8_t *outptr;
438
James Smart2e0fef82007-06-17 19:56:36 -0500439 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500440 for (i = 0; i < 12; i++) {
441 status = *outptr++;
442 j = ((status & 0xf0) >> 4);
443 if (j <= 9)
444 phba->SerialNumber[i] =
445 (char)((uint8_t) 0x30 + (uint8_t) j);
446 else
447 phba->SerialNumber[i] =
448 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
449 i++;
450 j = (status & 0xf);
451 if (j <= 9)
452 phba->SerialNumber[i] =
453 (char)((uint8_t) 0x30 + (uint8_t) j);
454 else
455 phba->SerialNumber[i] =
456 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
457 }
458 }
459
dea31012005-04-17 16:05:31 -0500460 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500461 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500462 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500463 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400464 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500465 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500466 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500467 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500468 mempool_free( pmb, phba->mbox_mem_pool);
469 return -EIO;
470 }
471
James Smarta0c87cb2009-07-19 10:01:10 -0400472 /* Check if the port is disabled */
473 lpfc_sli_read_link_ste(phba);
474
dea31012005-04-17 16:05:31 -0500475 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -0400476 i = (mb->un.varRdConfig.max_xri + 1);
477 if (phba->cfg_hba_queue_depth > i) {
478 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
479 "3359 HBA queue depth changed from %d to %d\n",
480 phba->cfg_hba_queue_depth, i);
481 phba->cfg_hba_queue_depth = i;
482 }
483
484 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
485 i = (mb->un.varRdConfig.max_xri >> 3);
486 if (phba->pport->cfg_lun_queue_depth > i) {
487 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
488 "3360 LUN queue depth changed from %d to %d\n",
489 phba->pport->cfg_lun_queue_depth, i);
490 phba->pport->cfg_lun_queue_depth = i;
491 }
dea31012005-04-17 16:05:31 -0500492
493 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500494
495 /* Get the default values for Model Name and Description */
496 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
497
James Smart2e0fef82007-06-17 19:56:36 -0500498 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500499
James Smart0b727fe2007-10-27 13:37:25 -0400500 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smart7e56aa22012-08-03 12:35:34 -0400501 if (psli->ring[psli->extra_ring].sli.sli3.cmdringaddr)
James Smarta4bc3372006-12-02 13:34:16 -0500502 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
James Smart7e56aa22012-08-03 12:35:34 -0400503 if (psli->ring[psli->fcp_ring].sli.sli3.cmdringaddr)
dea31012005-04-17 16:05:31 -0500504 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
James Smart7e56aa22012-08-03 12:35:34 -0400505 if (psli->ring[psli->next_ring].sli.sli3.cmdringaddr)
dea31012005-04-17 16:05:31 -0500506 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
507
508 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500509 if (phba->sli_rev != 3)
510 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500511
James Smart93996272008-08-24 21:50:30 -0400512 /*
513 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
514 */
515 if (phba->intr_type == MSIX) {
516 rc = lpfc_config_msi(phba, pmb);
517 if (rc) {
518 mempool_free(pmb, phba->mbox_mem_pool);
519 return -EIO;
520 }
521 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
522 if (rc != MBX_SUCCESS) {
523 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
524 "0352 Config MSI mailbox command "
525 "failed, mbxCmd x%x, mbxStatus x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400526 pmb->u.mb.mbxCommand,
527 pmb->u.mb.mbxStatus);
James Smart93996272008-08-24 21:50:30 -0400528 mempool_free(pmb, phba->mbox_mem_pool);
529 return -EIO;
530 }
531 }
532
James Smart04c68492009-05-22 14:52:52 -0400533 spin_lock_irq(&phba->hbalock);
James Smart93996272008-08-24 21:50:30 -0400534 /* Initialize ERATT handling flag */
535 phba->hba_flag &= ~HBA_ERATT_HANDLED;
536
dea31012005-04-17 16:05:31 -0500537 /* Enable appropriate host interrupts */
James Smart9940b972011-03-11 16:06:12 -0500538 if (lpfc_readl(phba->HCregaddr, &status)) {
539 spin_unlock_irq(&phba->hbalock);
540 return -EIO;
541 }
dea31012005-04-17 16:05:31 -0500542 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
543 if (psli->num_rings > 0)
544 status |= HC_R0INT_ENA;
545 if (psli->num_rings > 1)
546 status |= HC_R1INT_ENA;
547 if (psli->num_rings > 2)
548 status |= HC_R2INT_ENA;
549 if (psli->num_rings > 3)
550 status |= HC_R3INT_ENA;
551
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500552 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
553 (phba->cfg_poll & DISABLE_FCP_RING_INT))
James Smart93996272008-08-24 21:50:30 -0400554 status &= ~(HC_R0INT_ENA);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500555
dea31012005-04-17 16:05:31 -0500556 writel(status, phba->HCregaddr);
557 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500558 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500559
James Smart93996272008-08-24 21:50:30 -0400560 /* Set up ring-0 (ELS) timer */
561 timeout = phba->fc_ratov * 2;
James Smart256ec0d2013-04-17 20:14:58 -0400562 mod_timer(&vport->els_tmofunc,
563 jiffies + msecs_to_jiffies(1000 * timeout));
James Smart93996272008-08-24 21:50:30 -0400564 /* Set up heart beat (HB) timer */
James Smart256ec0d2013-04-17 20:14:58 -0400565 mod_timer(&phba->hb_tmofunc,
566 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -0500567 phba->hb_outstanding = 0;
568 phba->last_completion_time = jiffies;
James Smart93996272008-08-24 21:50:30 -0400569 /* Set up error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -0400570 mod_timer(&phba->eratt_poll,
571 jiffies + msecs_to_jiffies(1000 * LPFC_ERATT_POLL_INTERVAL));
dea31012005-04-17 16:05:31 -0500572
James Smarta0c87cb2009-07-19 10:01:10 -0400573 if (phba->hba_flag & LINK_DISABLED) {
574 lpfc_printf_log(phba,
575 KERN_ERR, LOG_INIT,
576 "2598 Adapter Link is disabled.\n");
577 lpfc_down_link(phba, pmb);
578 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
579 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
580 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
581 lpfc_printf_log(phba,
582 KERN_ERR, LOG_INIT,
583 "2599 Adapter failed to issue DOWN_LINK"
584 " mbox command rc 0x%x\n", rc);
585
586 mempool_free(pmb, phba->mbox_mem_pool);
587 return -EIO;
588 }
James Smarte40a02c2010-02-26 14:13:54 -0500589 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart026abb82011-12-13 13:20:45 -0500590 mempool_free(pmb, phba->mbox_mem_pool);
591 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
592 if (rc)
593 return rc;
dea31012005-04-17 16:05:31 -0500594 }
595 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400596 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500597 if (!pmb) {
598 phba->link_state = LPFC_HBA_ERROR;
599 return -ENOMEM;
600 }
601
James Smart57127f12007-10-27 13:37:05 -0400602 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
603 pmb->mbox_cmpl = lpfc_config_async_cmpl;
604 pmb->vport = phba->pport;
605 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500606
James Smart57127f12007-10-27 13:37:05 -0400607 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
608 lpfc_printf_log(phba,
609 KERN_ERR,
610 LOG_INIT,
611 "0456 Adapter failed to issue "
James Smarte4e74272009-07-19 10:01:38 -0400612 "ASYNCEVT_ENABLE mbox status x%x\n",
James Smart57127f12007-10-27 13:37:05 -0400613 rc);
614 mempool_free(pmb, phba->mbox_mem_pool);
615 }
James Smart97207482008-12-04 22:39:19 -0500616
617 /* Get Option rom version */
618 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500619 if (!pmb) {
620 phba->link_state = LPFC_HBA_ERROR;
621 return -ENOMEM;
622 }
623
James Smart97207482008-12-04 22:39:19 -0500624 lpfc_dump_wakeup_param(phba, pmb);
625 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
626 pmb->vport = phba->pport;
627 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
628
629 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
630 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
James Smarte4e74272009-07-19 10:01:38 -0400631 "to get Option ROM version status x%x\n", rc);
James Smart97207482008-12-04 22:39:19 -0500632 mempool_free(pmb, phba->mbox_mem_pool);
633 }
634
James Smartd7c255b2008-08-24 21:50:00 -0400635 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400636}
637
James Smarte59058c2008-08-24 21:49:00 -0400638/**
James Smart84d1b002010-02-12 14:42:33 -0500639 * lpfc_hba_init_link - Initialize the FC link
640 * @phba: pointer to lpfc hba data structure.
James Smart6e7288d2010-06-07 15:23:35 -0400641 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500642 *
643 * This routine will issue the INIT_LINK mailbox command call.
644 * It is available to other drivers through the lpfc_hba data
645 * structure for use as a delayed link up mechanism with the
646 * module parameter lpfc_suppress_link_up.
647 *
648 * Return code
649 * 0 - success
650 * Any other value - error
651 **/
652int
James Smart6e7288d2010-06-07 15:23:35 -0400653lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500654{
James Smart1b511972011-12-13 13:23:09 -0500655 return lpfc_hba_init_link_fc_topology(phba, phba->cfg_topology, flag);
656}
657
658/**
659 * lpfc_hba_init_link_fc_topology - Initialize FC link with desired topology
660 * @phba: pointer to lpfc hba data structure.
661 * @fc_topology: desired fc topology.
662 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
663 *
664 * This routine will issue the INIT_LINK mailbox command call.
665 * It is available to other drivers through the lpfc_hba data
666 * structure for use as a delayed link up mechanism with the
667 * module parameter lpfc_suppress_link_up.
668 *
669 * Return code
670 * 0 - success
671 * Any other value - error
672 **/
673int
674lpfc_hba_init_link_fc_topology(struct lpfc_hba *phba, uint32_t fc_topology,
675 uint32_t flag)
676{
James Smart84d1b002010-02-12 14:42:33 -0500677 struct lpfc_vport *vport = phba->pport;
678 LPFC_MBOXQ_t *pmb;
679 MAILBOX_t *mb;
680 int rc;
681
682 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
683 if (!pmb) {
684 phba->link_state = LPFC_HBA_ERROR;
685 return -ENOMEM;
686 }
687 mb = &pmb->u.mb;
688 pmb->vport = vport;
689
James Smart026abb82011-12-13 13:20:45 -0500690 if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_MAX) ||
691 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G) &&
692 !(phba->lmt & LMT_1Gb)) ||
693 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G) &&
694 !(phba->lmt & LMT_2Gb)) ||
695 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G) &&
696 !(phba->lmt & LMT_4Gb)) ||
697 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G) &&
698 !(phba->lmt & LMT_8Gb)) ||
699 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G) &&
700 !(phba->lmt & LMT_10Gb)) ||
701 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G) &&
702 !(phba->lmt & LMT_16Gb))) {
703 /* Reset link speed to auto */
704 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
705 "1302 Invalid speed for this board:%d "
706 "Reset link speed to auto.\n",
707 phba->cfg_link_speed);
708 phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
709 }
James Smart1b511972011-12-13 13:23:09 -0500710 lpfc_init_link(phba, pmb, fc_topology, phba->cfg_link_speed);
James Smart84d1b002010-02-12 14:42:33 -0500711 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart1b511972011-12-13 13:23:09 -0500712 if (phba->sli_rev < LPFC_SLI_REV4)
713 lpfc_set_loopback_flag(phba);
James Smart6e7288d2010-06-07 15:23:35 -0400714 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart76a95d72010-11-20 23:11:48 -0500715 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
James Smart84d1b002010-02-12 14:42:33 -0500716 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
717 "0498 Adapter failed to init, mbxCmd x%x "
718 "INIT_LINK, mbxStatus x%x\n",
719 mb->mbxCommand, mb->mbxStatus);
James Smart76a95d72010-11-20 23:11:48 -0500720 if (phba->sli_rev <= LPFC_SLI_REV3) {
721 /* Clear all interrupt enable conditions */
722 writel(0, phba->HCregaddr);
723 readl(phba->HCregaddr); /* flush */
724 /* Clear all pending interrupts */
725 writel(0xffffffff, phba->HAregaddr);
726 readl(phba->HAregaddr); /* flush */
727 }
James Smart84d1b002010-02-12 14:42:33 -0500728 phba->link_state = LPFC_HBA_ERROR;
James Smart6e7288d2010-06-07 15:23:35 -0400729 if (rc != MBX_BUSY || flag == MBX_POLL)
James Smart84d1b002010-02-12 14:42:33 -0500730 mempool_free(pmb, phba->mbox_mem_pool);
731 return -EIO;
732 }
James Smarte40a02c2010-02-26 14:13:54 -0500733 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
James Smart6e7288d2010-06-07 15:23:35 -0400734 if (flag == MBX_POLL)
735 mempool_free(pmb, phba->mbox_mem_pool);
James Smart84d1b002010-02-12 14:42:33 -0500736
737 return 0;
738}
739
740/**
741 * lpfc_hba_down_link - this routine downs the FC link
James Smart6e7288d2010-06-07 15:23:35 -0400742 * @phba: pointer to lpfc hba data structure.
743 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500744 *
745 * This routine will issue the DOWN_LINK mailbox command call.
746 * It is available to other drivers through the lpfc_hba data
747 * structure for use to stop the link.
748 *
749 * Return code
750 * 0 - success
751 * Any other value - error
752 **/
753int
James Smart6e7288d2010-06-07 15:23:35 -0400754lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500755{
756 LPFC_MBOXQ_t *pmb;
757 int rc;
758
759 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
760 if (!pmb) {
761 phba->link_state = LPFC_HBA_ERROR;
762 return -ENOMEM;
763 }
764
765 lpfc_printf_log(phba,
766 KERN_ERR, LOG_INIT,
767 "0491 Adapter Link is disabled.\n");
768 lpfc_down_link(phba, pmb);
769 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6e7288d2010-06-07 15:23:35 -0400770 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart84d1b002010-02-12 14:42:33 -0500771 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
772 lpfc_printf_log(phba,
773 KERN_ERR, LOG_INIT,
774 "2522 Adapter failed to issue DOWN_LINK"
775 " mbox command rc 0x%x\n", rc);
776
777 mempool_free(pmb, phba->mbox_mem_pool);
778 return -EIO;
779 }
James Smart6e7288d2010-06-07 15:23:35 -0400780 if (flag == MBX_POLL)
781 mempool_free(pmb, phba->mbox_mem_pool);
782
James Smart84d1b002010-02-12 14:42:33 -0500783 return 0;
784}
785
786/**
James Smart3621a712009-04-06 18:47:14 -0400787 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400788 * @phba: pointer to lpfc HBA data structure.
789 *
790 * This routine will do LPFC uninitialization before the HBA is reset when
791 * bringing down the SLI Layer.
792 *
793 * Return codes
794 * 0 - success.
795 * Any other value - error.
796 **/
dea31012005-04-17 16:05:31 -0500797int
James Smart2e0fef82007-06-17 19:56:36 -0500798lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500799{
James Smart1b32f6a2008-02-08 18:49:39 -0500800 struct lpfc_vport **vports;
801 int i;
James Smart3772a992009-05-22 14:50:54 -0400802
803 if (phba->sli_rev <= LPFC_SLI_REV3) {
804 /* Disable interrupts */
805 writel(0, phba->HCregaddr);
806 readl(phba->HCregaddr); /* flush */
807 }
dea31012005-04-17 16:05:31 -0500808
James Smart1b32f6a2008-02-08 18:49:39 -0500809 if (phba->pport->load_flag & FC_UNLOADING)
810 lpfc_cleanup_discovery_resources(phba->pport);
811 else {
812 vports = lpfc_create_vport_work_array(phba);
813 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400814 for (i = 0; i <= phba->max_vports &&
815 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500816 lpfc_cleanup_discovery_resources(vports[i]);
817 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500818 }
819 return 0;
dea31012005-04-17 16:05:31 -0500820}
821
James Smarte59058c2008-08-24 21:49:00 -0400822/**
James Smart68e814f2014-05-21 08:04:59 -0400823 * lpfc_sli4_free_sp_events - Cleanup sp_queue_events to free
824 * rspiocb which got deferred
825 *
826 * @phba: pointer to lpfc HBA data structure.
827 *
828 * This routine will cleanup completed slow path events after HBA is reset
829 * when bringing down the SLI Layer.
830 *
831 *
832 * Return codes
833 * void.
834 **/
835static void
836lpfc_sli4_free_sp_events(struct lpfc_hba *phba)
837{
838 struct lpfc_iocbq *rspiocbq;
839 struct hbq_dmabuf *dmabuf;
840 struct lpfc_cq_event *cq_event;
841
842 spin_lock_irq(&phba->hbalock);
843 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
844 spin_unlock_irq(&phba->hbalock);
845
846 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
847 /* Get the response iocb from the head of work queue */
848 spin_lock_irq(&phba->hbalock);
849 list_remove_head(&phba->sli4_hba.sp_queue_event,
850 cq_event, struct lpfc_cq_event, list);
851 spin_unlock_irq(&phba->hbalock);
852
853 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
854 case CQE_CODE_COMPL_WQE:
855 rspiocbq = container_of(cq_event, struct lpfc_iocbq,
856 cq_event);
857 lpfc_sli_release_iocbq(phba, rspiocbq);
858 break;
859 case CQE_CODE_RECEIVE:
860 case CQE_CODE_RECEIVE_V1:
861 dmabuf = container_of(cq_event, struct hbq_dmabuf,
862 cq_event);
863 lpfc_in_buf_free(phba, &dmabuf->dbuf);
864 }
865 }
866}
867
868/**
James Smartbcece5f2014-04-04 13:51:34 -0400869 * lpfc_hba_free_post_buf - Perform lpfc uninitialization after HBA reset
870 * @phba: pointer to lpfc HBA data structure.
871 *
872 * This routine will cleanup posted ELS buffers after the HBA is reset
873 * when bringing down the SLI Layer.
874 *
875 *
876 * Return codes
877 * void.
878 **/
879static void
880lpfc_hba_free_post_buf(struct lpfc_hba *phba)
881{
882 struct lpfc_sli *psli = &phba->sli;
883 struct lpfc_sli_ring *pring;
884 struct lpfc_dmabuf *mp, *next_mp;
James Smart07eab622014-04-04 13:51:44 -0400885 LIST_HEAD(buflist);
886 int count;
James Smartbcece5f2014-04-04 13:51:34 -0400887
888 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
889 lpfc_sli_hbqbuf_free_all(phba);
890 else {
891 /* Cleanup preposted buffers on the ELS ring */
James Smartbcece5f2014-04-04 13:51:34 -0400892 pring = &psli->ring[LPFC_ELS_RING];
James Smart07eab622014-04-04 13:51:44 -0400893 spin_lock_irq(&phba->hbalock);
894 list_splice_init(&pring->postbufq, &buflist);
895 spin_unlock_irq(&phba->hbalock);
896
897 count = 0;
898 list_for_each_entry_safe(mp, next_mp, &buflist, list) {
James Smartbcece5f2014-04-04 13:51:34 -0400899 list_del(&mp->list);
James Smart07eab622014-04-04 13:51:44 -0400900 count++;
James Smartbcece5f2014-04-04 13:51:34 -0400901 lpfc_mbuf_free(phba, mp->virt, mp->phys);
902 kfree(mp);
903 }
James Smart07eab622014-04-04 13:51:44 -0400904
905 spin_lock_irq(&phba->hbalock);
906 pring->postbufq_cnt -= count;
James Smartbcece5f2014-04-04 13:51:34 -0400907 spin_unlock_irq(&phba->hbalock);
908 }
909}
910
911/**
912 * lpfc_hba_clean_txcmplq - Perform lpfc uninitialization after HBA reset
913 * @phba: pointer to lpfc HBA data structure.
914 *
915 * This routine will cleanup the txcmplq after the HBA is reset when bringing
916 * down the SLI Layer.
917 *
918 * Return codes
919 * void
920 **/
921static void
922lpfc_hba_clean_txcmplq(struct lpfc_hba *phba)
923{
924 struct lpfc_sli *psli = &phba->sli;
925 struct lpfc_sli_ring *pring;
926 LIST_HEAD(completions);
927 int i;
928
James Smartbcece5f2014-04-04 13:51:34 -0400929 for (i = 0; i < psli->num_rings; i++) {
930 pring = &psli->ring[i];
931 if (phba->sli_rev >= LPFC_SLI_REV4)
932 spin_lock_irq(&pring->ring_lock);
933 else
934 spin_lock_irq(&phba->hbalock);
935 /* At this point in time the HBA is either reset or DOA. Either
936 * way, nothing should be on txcmplq as it will NEVER complete.
937 */
938 list_splice_init(&pring->txcmplq, &completions);
James Smartdb55fba2014-04-04 13:52:02 -0400939 pring->txcmplq_cnt = 0;
James Smartbcece5f2014-04-04 13:51:34 -0400940
941 if (phba->sli_rev >= LPFC_SLI_REV4)
942 spin_unlock_irq(&pring->ring_lock);
943 else
944 spin_unlock_irq(&phba->hbalock);
945
946 /* Cancel all the IOCBs from the completions list */
947 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
948 IOERR_SLI_ABORTED);
949 lpfc_sli_abort_iocb_ring(phba, pring);
950 }
951}
952
953/**
James Smart3772a992009-05-22 14:50:54 -0400954 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smartbcece5f2014-04-04 13:51:34 -0400955 int i;
James Smarte59058c2008-08-24 21:49:00 -0400956 * @phba: pointer to lpfc HBA data structure.
957 *
958 * This routine will do uninitialization after the HBA is reset when bring
959 * down the SLI Layer.
960 *
961 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200962 * 0 - success.
James Smarte59058c2008-08-24 21:49:00 -0400963 * Any other value - error.
964 **/
James Smart3772a992009-05-22 14:50:54 -0400965static int
966lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -0500967{
James Smartbcece5f2014-04-04 13:51:34 -0400968 lpfc_hba_free_post_buf(phba);
969 lpfc_hba_clean_txcmplq(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500970 return 0;
971}
James Smart5af5eee2010-10-22 11:06:38 -0400972
James Smartda0436e2009-05-22 14:51:39 -0400973/**
974 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
975 * @phba: pointer to lpfc HBA data structure.
976 *
977 * This routine will do uninitialization after the HBA is reset when bring
978 * down the SLI Layer.
979 *
980 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200981 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400982 * Any other value - error.
983 **/
984static int
985lpfc_hba_down_post_s4(struct lpfc_hba *phba)
986{
987 struct lpfc_scsi_buf *psb, *psb_next;
988 LIST_HEAD(aborts);
James Smartda0436e2009-05-22 14:51:39 -0400989 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500990 struct lpfc_sglq *sglq_entry = NULL;
991
James Smartbcece5f2014-04-04 13:51:34 -0400992 lpfc_hba_free_post_buf(phba);
993 lpfc_hba_clean_txcmplq(phba);
994
James Smartda0436e2009-05-22 14:51:39 -0400995 /* At this point in time the HBA is either reset or DOA. Either
996 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
997 * on the lpfc_sgl_list so that it can either be freed if the
998 * driver is unloading or reposted if the driver is restarting
999 * the port.
1000 */
1001 spin_lock_irq(&phba->hbalock); /* required for lpfc_sgl_list and */
1002 /* scsl_buf_list */
1003 /* abts_sgl_list_lock required because worker thread uses this
1004 * list.
1005 */
1006 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -05001007 list_for_each_entry(sglq_entry,
1008 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
1009 sglq_entry->state = SGL_FREED;
1010
James Smartda0436e2009-05-22 14:51:39 -04001011 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
1012 &phba->sli4_hba.lpfc_sgl_list);
1013 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
1014 /* abts_scsi_buf_list_lock required because worker thread uses this
1015 * list.
1016 */
1017 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
1018 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
1019 &aborts);
1020 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
1021 spin_unlock_irq(&phba->hbalock);
1022
1023 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
1024 psb->pCmd = NULL;
1025 psb->status = IOSTAT_SUCCESS;
1026 }
James Smarta40fc5f2013-04-17 20:17:40 -04001027 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
1028 list_splice(&aborts, &phba->lpfc_scsi_buf_list_put);
1029 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
James Smart68e814f2014-05-21 08:04:59 -04001030
1031 lpfc_sli4_free_sp_events(phba);
James Smartda0436e2009-05-22 14:51:39 -04001032 return 0;
1033}
1034
1035/**
1036 * lpfc_hba_down_post - Wrapper func for hba down post routine
1037 * @phba: pointer to lpfc HBA data structure.
1038 *
1039 * This routine wraps the actual SLI3 or SLI4 routine for performing
1040 * uninitialization after the HBA is reset when bring down the SLI Layer.
1041 *
1042 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001043 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001044 * Any other value - error.
1045 **/
1046int
1047lpfc_hba_down_post(struct lpfc_hba *phba)
1048{
1049 return (*phba->lpfc_hba_down_post)(phba);
1050}
Jamie Wellnitz41415862006-02-28 19:25:27 -05001051
James Smarte59058c2008-08-24 21:49:00 -04001052/**
James Smart3621a712009-04-06 18:47:14 -04001053 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001054 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1055 *
1056 * This is the HBA-timer timeout handler registered to the lpfc driver. When
1057 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
1058 * work-port-events bitmap and the worker thread is notified. This timeout
1059 * event will be used by the worker thread to invoke the actual timeout
1060 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
1061 * be performed in the timeout handler and the HBA timeout event bit shall
1062 * be cleared by the worker thread after it has taken the event bitmap out.
1063 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001064static void
James Smart858c9f62007-06-17 19:56:39 -05001065lpfc_hb_timeout(unsigned long ptr)
1066{
1067 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -04001068 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -05001069 unsigned long iflag;
1070
1071 phba = (struct lpfc_hba *)ptr;
James Smart93996272008-08-24 21:50:30 -04001072
1073 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -05001074 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04001075 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
1076 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -05001077 phba->pport->work_port_events |= WORKER_HB_TMO;
1078 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
1079
James Smart93996272008-08-24 21:50:30 -04001080 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -04001081 if (!tmo_posted)
1082 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -05001083 return;
1084}
1085
James Smarte59058c2008-08-24 21:49:00 -04001086/**
James Smart19ca7602010-11-20 23:11:55 -05001087 * lpfc_rrq_timeout - The RRQ-timer timeout handler
1088 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
1089 *
1090 * This is the RRQ-timer timeout handler registered to the lpfc driver. When
1091 * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
1092 * work-port-events bitmap and the worker thread is notified. This timeout
1093 * event will be used by the worker thread to invoke the actual timeout
1094 * handler routine, lpfc_rrq_handler. Any periodical operations will
1095 * be performed in the timeout handler and the RRQ timeout event bit shall
1096 * be cleared by the worker thread after it has taken the event bitmap out.
1097 **/
1098static void
1099lpfc_rrq_timeout(unsigned long ptr)
1100{
1101 struct lpfc_hba *phba;
James Smart19ca7602010-11-20 23:11:55 -05001102 unsigned long iflag;
1103
1104 phba = (struct lpfc_hba *)ptr;
1105 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001106 if (!(phba->pport->load_flag & FC_UNLOADING))
1107 phba->hba_flag |= HBA_RRQ_ACTIVE;
1108 else
1109 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart19ca7602010-11-20 23:11:55 -05001110 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
James Smart06918ac2014-02-20 09:57:57 -05001111
1112 if (!(phba->pport->load_flag & FC_UNLOADING))
1113 lpfc_worker_wake_up(phba);
James Smart19ca7602010-11-20 23:11:55 -05001114}
1115
1116/**
James Smart3621a712009-04-06 18:47:14 -04001117 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -04001118 * @phba: pointer to lpfc hba data structure.
1119 * @pmboxq: pointer to the driver internal queue element for mailbox command.
1120 *
1121 * This is the callback function to the lpfc heart-beat mailbox command.
1122 * If configured, the lpfc driver issues the heart-beat mailbox command to
1123 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
1124 * heart-beat mailbox command is issued, the driver shall set up heart-beat
1125 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
1126 * heart-beat outstanding state. Once the mailbox command comes back and
1127 * no error conditions detected, the heart-beat mailbox command timer is
1128 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1129 * state is cleared for the next heart-beat. If the timer expired with the
1130 * heart-beat outstanding state set, the driver will put the HBA offline.
1131 **/
James Smart858c9f62007-06-17 19:56:39 -05001132static void
1133lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1134{
1135 unsigned long drvr_flag;
1136
1137 spin_lock_irqsave(&phba->hbalock, drvr_flag);
1138 phba->hb_outstanding = 0;
1139 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1140
James Smart93996272008-08-24 21:50:30 -04001141 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -05001142 mempool_free(pmboxq, phba->mbox_mem_pool);
1143 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1144 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -04001145 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -05001146 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001147 jiffies +
1148 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001149 return;
1150}
1151
James Smarte59058c2008-08-24 21:49:00 -04001152/**
James Smart3621a712009-04-06 18:47:14 -04001153 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001154 * @phba: pointer to lpfc hba data structure.
1155 *
1156 * This is the actual HBA-timer timeout handler to be invoked by the worker
1157 * thread whenever the HBA timer fired and HBA-timeout event posted. This
1158 * handler performs any periodic operations needed for the device. If such
1159 * periodic event has already been attended to either in the interrupt handler
1160 * or by processing slow-ring or fast-ring events within the HBA-timer
1161 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1162 * the timer for the next timeout period. If lpfc heart-beat mailbox command
1163 * is configured and there is no heart-beat mailbox command outstanding, a
1164 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1165 * has been a heart-beat mailbox command outstanding, the HBA shall be put
1166 * to offline.
1167 **/
James Smart858c9f62007-06-17 19:56:39 -05001168void
1169lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1170{
James Smart45ed1192009-10-02 15:17:02 -04001171 struct lpfc_vport **vports;
James Smart858c9f62007-06-17 19:56:39 -05001172 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -05001173 struct lpfc_dmabuf *buf_ptr;
James Smart45ed1192009-10-02 15:17:02 -04001174 int retval, i;
James Smart858c9f62007-06-17 19:56:39 -05001175 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -05001176 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -05001177
James Smart45ed1192009-10-02 15:17:02 -04001178 vports = lpfc_create_vport_work_array(phba);
1179 if (vports != NULL)
1180 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
1181 lpfc_rcv_seq_check_edtov(vports[i]);
1182 lpfc_destroy_vport_work_array(phba, vports);
1183
James Smart858c9f62007-06-17 19:56:39 -05001184 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -04001185 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -05001186 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1187 return;
1188
1189 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -05001190
James Smart256ec0d2013-04-17 20:14:58 -04001191 if (time_after(phba->last_completion_time +
1192 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL),
1193 jiffies)) {
James Smart858c9f62007-06-17 19:56:39 -05001194 spin_unlock_irq(&phba->pport->work_port_lock);
1195 if (!phba->hb_outstanding)
1196 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001197 jiffies +
1198 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001199 else
1200 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001201 jiffies +
1202 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001203 return;
1204 }
1205 spin_unlock_irq(&phba->pport->work_port_lock);
1206
James Smart0ff10d42008-01-11 01:52:36 -05001207 if (phba->elsbuf_cnt &&
1208 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1209 spin_lock_irq(&phba->hbalock);
1210 list_splice_init(&phba->elsbuf, &completions);
1211 phba->elsbuf_cnt = 0;
1212 phba->elsbuf_prev_cnt = 0;
1213 spin_unlock_irq(&phba->hbalock);
1214
1215 while (!list_empty(&completions)) {
1216 list_remove_head(&completions, buf_ptr,
1217 struct lpfc_dmabuf, list);
1218 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1219 kfree(buf_ptr);
1220 }
1221 }
1222 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1223
James Smart858c9f62007-06-17 19:56:39 -05001224 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -05001225 if (phba->cfg_enable_hba_heartbeat) {
1226 if (!phba->hb_outstanding) {
James Smartbc739052010-08-04 16:11:18 -04001227 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1228 (list_empty(&psli->mboxq))) {
1229 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1230 GFP_KERNEL);
1231 if (!pmboxq) {
1232 mod_timer(&phba->hb_tmofunc,
1233 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001234 msecs_to_jiffies(1000 *
1235 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001236 return;
1237 }
James Smart13815c82008-01-11 01:52:48 -05001238
James Smartbc739052010-08-04 16:11:18 -04001239 lpfc_heart_beat(phba, pmboxq);
1240 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1241 pmboxq->vport = phba->pport;
1242 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1243 MBX_NOWAIT);
James Smart13815c82008-01-11 01:52:48 -05001244
James Smartbc739052010-08-04 16:11:18 -04001245 if (retval != MBX_BUSY &&
1246 retval != MBX_SUCCESS) {
1247 mempool_free(pmboxq,
1248 phba->mbox_mem_pool);
1249 mod_timer(&phba->hb_tmofunc,
1250 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001251 msecs_to_jiffies(1000 *
1252 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001253 return;
1254 }
1255 phba->skipped_hb = 0;
1256 phba->hb_outstanding = 1;
1257 } else if (time_before_eq(phba->last_completion_time,
1258 phba->skipped_hb)) {
1259 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1260 "2857 Last completion time not "
1261 " updated in %d ms\n",
1262 jiffies_to_msecs(jiffies
1263 - phba->last_completion_time));
1264 } else
1265 phba->skipped_hb = jiffies;
1266
James Smart858c9f62007-06-17 19:56:39 -05001267 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001268 jiffies +
1269 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001270 return;
James Smart13815c82008-01-11 01:52:48 -05001271 } else {
1272 /*
1273 * If heart beat timeout called with hb_outstanding set
James Smartdcf2a4e2010-09-29 11:18:53 -04001274 * we need to give the hb mailbox cmd a chance to
1275 * complete or TMO.
James Smart13815c82008-01-11 01:52:48 -05001276 */
James Smartdcf2a4e2010-09-29 11:18:53 -04001277 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1278 "0459 Adapter heartbeat still out"
1279 "standing:last compl time was %d ms.\n",
1280 jiffies_to_msecs(jiffies
1281 - phba->last_completion_time));
1282 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001283 jiffies +
1284 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001285 }
James Smart858c9f62007-06-17 19:56:39 -05001286 }
1287}
1288
James Smarte59058c2008-08-24 21:49:00 -04001289/**
James Smart3621a712009-04-06 18:47:14 -04001290 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -04001291 * @phba: pointer to lpfc hba data structure.
1292 *
1293 * This routine is called to bring the HBA offline when HBA hardware error
1294 * other than Port Error 6 has been detected.
1295 **/
James Smart09372822008-01-11 01:52:54 -05001296static void
1297lpfc_offline_eratt(struct lpfc_hba *phba)
1298{
1299 struct lpfc_sli *psli = &phba->sli;
1300
1301 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001302 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -05001303 spin_unlock_irq(&phba->hbalock);
James Smart618a5232012-06-12 13:54:36 -04001304 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart09372822008-01-11 01:52:54 -05001305
1306 lpfc_offline(phba);
1307 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001308 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001309 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001310 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001311 lpfc_hba_down_post(phba);
1312 lpfc_sli_brdready(phba, HS_MBRDY);
1313 lpfc_unblock_mgmt_io(phba);
1314 phba->link_state = LPFC_HBA_ERROR;
1315 return;
1316}
1317
James Smarte59058c2008-08-24 21:49:00 -04001318/**
James Smartda0436e2009-05-22 14:51:39 -04001319 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1320 * @phba: pointer to lpfc hba data structure.
1321 *
1322 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1323 * other than Port Error 6 has been detected.
1324 **/
James Smarta88dbb62013-04-17 20:18:39 -04001325void
James Smartda0436e2009-05-22 14:51:39 -04001326lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1327{
James Smart618a5232012-06-12 13:54:36 -04001328 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smartda0436e2009-05-22 14:51:39 -04001329 lpfc_offline(phba);
1330 lpfc_sli4_brdreset(phba);
1331 lpfc_hba_down_post(phba);
1332 lpfc_sli4_post_status_check(phba);
1333 lpfc_unblock_mgmt_io(phba);
1334 phba->link_state = LPFC_HBA_ERROR;
1335}
1336
1337/**
James Smarta257bf92009-04-06 18:48:10 -04001338 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1339 * @phba: pointer to lpfc hba data structure.
1340 *
1341 * This routine is invoked to handle the deferred HBA hardware error
1342 * conditions. This type of error is indicated by HBA by setting ER1
1343 * and another ER bit in the host status register. The driver will
1344 * wait until the ER1 bit clears before handling the error condition.
1345 **/
1346static void
1347lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1348{
1349 uint32_t old_host_status = phba->work_hs;
James Smarta257bf92009-04-06 18:48:10 -04001350 struct lpfc_sli *psli = &phba->sli;
1351
James Smartf4b4c682009-05-22 14:53:12 -04001352 /* If the pci channel is offline, ignore possible errors,
1353 * since we cannot communicate with the pci card anyway.
1354 */
1355 if (pci_channel_offline(phba->pcidev)) {
1356 spin_lock_irq(&phba->hbalock);
1357 phba->hba_flag &= ~DEFER_ERATT;
1358 spin_unlock_irq(&phba->hbalock);
1359 return;
1360 }
1361
James Smarta257bf92009-04-06 18:48:10 -04001362 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1363 "0479 Deferred Adapter Hardware Error "
1364 "Data: x%x x%x x%x\n",
1365 phba->work_hs,
1366 phba->work_status[0], phba->work_status[1]);
1367
1368 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001369 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001370 spin_unlock_irq(&phba->hbalock);
1371
1372
1373 /*
1374 * Firmware stops when it triggred erratt. That could cause the I/Os
1375 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1376 * SCSI layer retry it after re-establishing link.
1377 */
James Smartdb55fba2014-04-04 13:52:02 -04001378 lpfc_sli_abort_fcp_rings(phba);
James Smarta257bf92009-04-06 18:48:10 -04001379
1380 /*
1381 * There was a firmware error. Take the hba offline and then
1382 * attempt to restart it.
1383 */
James Smart618a5232012-06-12 13:54:36 -04001384 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smarta257bf92009-04-06 18:48:10 -04001385 lpfc_offline(phba);
1386
1387 /* Wait for the ER1 bit to clear.*/
1388 while (phba->work_hs & HS_FFER1) {
1389 msleep(100);
James Smart9940b972011-03-11 16:06:12 -05001390 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1391 phba->work_hs = UNPLUG_ERR ;
1392 break;
1393 }
James Smarta257bf92009-04-06 18:48:10 -04001394 /* If driver is unloading let the worker thread continue */
1395 if (phba->pport->load_flag & FC_UNLOADING) {
1396 phba->work_hs = 0;
1397 break;
1398 }
1399 }
1400
1401 /*
1402 * This is to ptrotect against a race condition in which
1403 * first write to the host attention register clear the
1404 * host status register.
1405 */
1406 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1407 phba->work_hs = old_host_status & ~HS_FFER1;
1408
James Smart3772a992009-05-22 14:50:54 -04001409 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001410 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001411 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001412 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1413 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1414}
1415
James Smart3772a992009-05-22 14:50:54 -04001416static void
1417lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1418{
1419 struct lpfc_board_event_header board_event;
1420 struct Scsi_Host *shost;
1421
1422 board_event.event_type = FC_REG_BOARD_EVENT;
1423 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1424 shost = lpfc_shost_from_vport(phba->pport);
1425 fc_host_post_vendor_event(shost, fc_get_event_number(),
1426 sizeof(board_event),
1427 (char *) &board_event,
1428 LPFC_NL_VENDOR_ID);
1429}
1430
James Smarta257bf92009-04-06 18:48:10 -04001431/**
James Smart3772a992009-05-22 14:50:54 -04001432 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001433 * @phba: pointer to lpfc hba data structure.
1434 *
1435 * This routine is invoked to handle the following HBA hardware error
1436 * conditions:
1437 * 1 - HBA error attention interrupt
1438 * 2 - DMA ring index out of range
1439 * 3 - Mailbox command came back as unknown
1440 **/
James Smart3772a992009-05-22 14:50:54 -04001441static void
1442lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001443{
James Smart2e0fef82007-06-17 19:56:36 -05001444 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001445 struct lpfc_sli *psli = &phba->sli;
James Smartd2873e42006-08-18 17:46:43 -04001446 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001447 unsigned long temperature;
1448 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001449 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001450
Linas Vepstas8d63f372007-02-14 14:28:36 -06001451 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001452 * since we cannot communicate with the pci card anyway.
1453 */
1454 if (pci_channel_offline(phba->pcidev)) {
1455 spin_lock_irq(&phba->hbalock);
1456 phba->hba_flag &= ~DEFER_ERATT;
1457 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001458 return;
James Smart3772a992009-05-22 14:50:54 -04001459 }
1460
James Smart13815c82008-01-11 01:52:48 -05001461 /* If resets are disabled then leave the HBA alone and return */
1462 if (!phba->cfg_enable_hba_reset)
1463 return;
dea31012005-04-17 16:05:31 -05001464
James Smartea2151b2008-09-07 11:52:10 -04001465 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001466 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001467
James Smarta257bf92009-04-06 18:48:10 -04001468 if (phba->hba_flag & DEFER_ERATT)
1469 lpfc_handle_deferred_eratt(phba);
1470
James Smartdcf2a4e2010-09-29 11:18:53 -04001471 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1472 if (phba->work_hs & HS_FFER6)
1473 /* Re-establishing Link */
1474 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1475 "1301 Re-establishing Link "
1476 "Data: x%x x%x x%x\n",
1477 phba->work_hs, phba->work_status[0],
1478 phba->work_status[1]);
1479 if (phba->work_hs & HS_FFER8)
1480 /* Device Zeroization */
1481 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1482 "2861 Host Authentication device "
1483 "zeroization Data:x%x x%x x%x\n",
1484 phba->work_hs, phba->work_status[0],
1485 phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001486
James Smart92d7f7b2007-06-17 19:56:38 -05001487 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001488 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001489 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001490
1491 /*
1492 * Firmware stops when it triggled erratt with HS_FFER6.
1493 * That could cause the I/Os dropped by the firmware.
1494 * Error iocb (I/O) on txcmplq and let the SCSI layer
1495 * retry it after re-establishing link.
1496 */
James Smartdb55fba2014-04-04 13:52:02 -04001497 lpfc_sli_abort_fcp_rings(phba);
dea31012005-04-17 16:05:31 -05001498
dea31012005-04-17 16:05:31 -05001499 /*
1500 * There was a firmware error. Take the hba offline and then
1501 * attempt to restart it.
1502 */
James Smart618a5232012-06-12 13:54:36 -04001503 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
dea31012005-04-17 16:05:31 -05001504 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001505 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001506 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001507 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001508 return;
1509 }
James Smart46fa3112007-04-25 09:51:45 -04001510 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001511 } else if (phba->work_hs & HS_CRIT_TEMP) {
1512 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1513 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1514 temp_event_data.event_code = LPFC_CRIT_TEMP;
1515 temp_event_data.data = (uint32_t)temperature;
1516
1517 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001518 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001519 "(%ld), taking this port offline "
1520 "Data: x%x x%x x%x\n",
1521 temperature, phba->work_hs,
1522 phba->work_status[0], phba->work_status[1]);
1523
1524 shost = lpfc_shost_from_vport(phba->pport);
1525 fc_host_post_vendor_event(shost, fc_get_event_number(),
1526 sizeof(temp_event_data),
1527 (char *) &temp_event_data,
1528 SCSI_NL_VID_TYPE_PCI
1529 | PCI_VENDOR_ID_EMULEX);
1530
James Smart7af67052007-10-27 13:38:11 -04001531 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001532 phba->over_temp_state = HBA_OVER_TEMP;
1533 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001534 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001535
dea31012005-04-17 16:05:31 -05001536 } else {
1537 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001538 * failure is a value other than FFER6. Do not call the offline
1539 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001540 */
1541 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001542 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001543 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001544 phba->work_hs,
dea31012005-04-17 16:05:31 -05001545 phba->work_status[0], phba->work_status[1]);
1546
James Smartd2873e42006-08-18 17:46:43 -04001547 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001548 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001549 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001550 sizeof(event_data), (char *) &event_data,
1551 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1552
James Smart09372822008-01-11 01:52:54 -05001553 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001554 }
James Smart93996272008-08-24 21:50:30 -04001555 return;
dea31012005-04-17 16:05:31 -05001556}
1557
James Smarte59058c2008-08-24 21:49:00 -04001558/**
James Smart618a5232012-06-12 13:54:36 -04001559 * lpfc_sli4_port_sta_fn_reset - The SLI4 function reset due to port status reg
1560 * @phba: pointer to lpfc hba data structure.
1561 * @mbx_action: flag for mailbox shutdown action.
1562 *
1563 * This routine is invoked to perform an SLI4 port PCI function reset in
1564 * response to port status register polling attention. It waits for port
1565 * status register (ERR, RDY, RN) bits before proceeding with function reset.
1566 * During this process, interrupt vectors are freed and later requested
1567 * for handling possible port resource change.
1568 **/
1569static int
James Smarte10b2022014-02-20 09:57:43 -05001570lpfc_sli4_port_sta_fn_reset(struct lpfc_hba *phba, int mbx_action,
1571 bool en_rn_msg)
James Smart618a5232012-06-12 13:54:36 -04001572{
1573 int rc;
1574 uint32_t intr_mode;
1575
1576 /*
1577 * On error status condition, driver need to wait for port
1578 * ready before performing reset.
1579 */
1580 rc = lpfc_sli4_pdev_status_reg_wait(phba);
1581 if (!rc) {
1582 /* need reset: attempt for port recovery */
James Smarte10b2022014-02-20 09:57:43 -05001583 if (en_rn_msg)
1584 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1585 "2887 Reset Needed: Attempting Port "
1586 "Recovery...\n");
James Smart618a5232012-06-12 13:54:36 -04001587 lpfc_offline_prep(phba, mbx_action);
1588 lpfc_offline(phba);
1589 /* release interrupt for possible resource change */
1590 lpfc_sli4_disable_intr(phba);
1591 lpfc_sli_brdrestart(phba);
1592 /* request and enable interrupt */
1593 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
1594 if (intr_mode == LPFC_INTR_ERROR) {
1595 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1596 "3175 Failed to enable interrupt\n");
1597 return -EIO;
1598 } else {
1599 phba->intr_mode = intr_mode;
1600 }
1601 rc = lpfc_online(phba);
1602 if (rc == 0)
1603 lpfc_unblock_mgmt_io(phba);
1604 }
1605 return rc;
1606}
1607
1608/**
James Smartda0436e2009-05-22 14:51:39 -04001609 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1610 * @phba: pointer to lpfc hba data structure.
1611 *
1612 * This routine is invoked to handle the SLI4 HBA hardware error attention
1613 * conditions.
1614 **/
1615static void
1616lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1617{
1618 struct lpfc_vport *vport = phba->pport;
1619 uint32_t event_data;
1620 struct Scsi_Host *shost;
James Smart2fcee4b2010-12-15 17:57:46 -05001621 uint32_t if_type;
James Smart2e90f4b2011-12-13 13:22:37 -05001622 struct lpfc_register portstat_reg = {0};
1623 uint32_t reg_err1, reg_err2;
1624 uint32_t uerrlo_reg, uemasklo_reg;
1625 uint32_t pci_rd_rc1, pci_rd_rc2;
James Smarte10b2022014-02-20 09:57:43 -05001626 bool en_rn_msg = true;
James Smart73d91e52011-10-10 21:32:10 -04001627 int rc;
James Smartda0436e2009-05-22 14:51:39 -04001628
1629 /* If the pci channel is offline, ignore possible errors, since
1630 * we cannot communicate with the pci card anyway.
1631 */
1632 if (pci_channel_offline(phba->pcidev))
1633 return;
1634 /* If resets are disabled then leave the HBA alone and return */
1635 if (!phba->cfg_enable_hba_reset)
1636 return;
1637
James Smart2fcee4b2010-12-15 17:57:46 -05001638 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1639 switch (if_type) {
1640 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart2e90f4b2011-12-13 13:22:37 -05001641 pci_rd_rc1 = lpfc_readl(
1642 phba->sli4_hba.u.if_type0.UERRLOregaddr,
1643 &uerrlo_reg);
1644 pci_rd_rc2 = lpfc_readl(
1645 phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
1646 &uemasklo_reg);
1647 /* consider PCI bus read error as pci_channel_offline */
1648 if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
1649 return;
James Smart2fcee4b2010-12-15 17:57:46 -05001650 lpfc_sli4_offline_eratt(phba);
1651 break;
1652 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart2e90f4b2011-12-13 13:22:37 -05001653 pci_rd_rc1 = lpfc_readl(
1654 phba->sli4_hba.u.if_type2.STATUSregaddr,
1655 &portstat_reg.word0);
1656 /* consider PCI bus read error as pci_channel_offline */
James Smart6b5151f2012-01-18 16:24:06 -05001657 if (pci_rd_rc1 == -EIO) {
1658 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1659 "3151 PCI bus read access failure: x%x\n",
1660 readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
James Smart2e90f4b2011-12-13 13:22:37 -05001661 return;
James Smart6b5151f2012-01-18 16:24:06 -05001662 }
James Smart2e90f4b2011-12-13 13:22:37 -05001663 reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
1664 reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
James Smart2fcee4b2010-12-15 17:57:46 -05001665 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
1666 /* TODO: Register for Overtemp async events. */
1667 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1668 "2889 Port Overtemperature event, "
James Smart026abb82011-12-13 13:20:45 -05001669 "taking port offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001670 spin_lock_irq(&phba->hbalock);
1671 phba->over_temp_state = HBA_OVER_TEMP;
1672 spin_unlock_irq(&phba->hbalock);
1673 lpfc_sli4_offline_eratt(phba);
James Smart2e90f4b2011-12-13 13:22:37 -05001674 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001675 }
James Smart2e90f4b2011-12-13 13:22:37 -05001676 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smarte10b2022014-02-20 09:57:43 -05001677 reg_err2 == SLIPORT_ERR2_REG_FW_RESTART) {
James Smart2e90f4b2011-12-13 13:22:37 -05001678 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte10b2022014-02-20 09:57:43 -05001679 "3143 Port Down: Firmware Update "
1680 "Detected\n");
1681 en_rn_msg = false;
1682 } else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smart2e90f4b2011-12-13 13:22:37 -05001683 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1684 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1685 "3144 Port Down: Debug Dump\n");
1686 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1687 reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
1688 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1689 "3145 Port Down: Provisioning\n");
James Smart618a5232012-06-12 13:54:36 -04001690
1691 /* Check port status register for function reset */
James Smarte10b2022014-02-20 09:57:43 -05001692 rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT,
1693 en_rn_msg);
James Smart618a5232012-06-12 13:54:36 -04001694 if (rc == 0) {
1695 /* don't report event on forced debug dump */
1696 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1697 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1698 return;
1699 else
1700 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001701 }
James Smart618a5232012-06-12 13:54:36 -04001702 /* fall through for not able to recover */
James Smart6b5151f2012-01-18 16:24:06 -05001703 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1704 "3152 Unrecoverable error, bring the port "
1705 "offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001706 lpfc_sli4_offline_eratt(phba);
1707 break;
1708 case LPFC_SLI_INTF_IF_TYPE_1:
1709 default:
1710 break;
1711 }
James Smart2e90f4b2011-12-13 13:22:37 -05001712 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1713 "3123 Report dump event to upper layer\n");
1714 /* Send an internal error event to mgmt application */
1715 lpfc_board_errevt_to_mgmt(phba);
1716
1717 event_data = FC_REG_DUMP_EVENT;
1718 shost = lpfc_shost_from_vport(vport);
1719 fc_host_post_vendor_event(shost, fc_get_event_number(),
1720 sizeof(event_data), (char *) &event_data,
1721 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
James Smartda0436e2009-05-22 14:51:39 -04001722}
1723
1724/**
1725 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1726 * @phba: pointer to lpfc HBA data structure.
1727 *
1728 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1729 * routine from the API jump table function pointer from the lpfc_hba struct.
1730 *
1731 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001732 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001733 * Any other value - error.
1734 **/
1735void
1736lpfc_handle_eratt(struct lpfc_hba *phba)
1737{
1738 (*phba->lpfc_handle_eratt)(phba);
1739}
1740
1741/**
James Smart3621a712009-04-06 18:47:14 -04001742 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04001743 * @phba: pointer to lpfc hba data structure.
1744 *
1745 * This routine is invoked from the worker thread to handle a HBA host
1746 * attention link event.
1747 **/
dea31012005-04-17 16:05:31 -05001748void
James Smart2e0fef82007-06-17 19:56:36 -05001749lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001750{
James Smart2e0fef82007-06-17 19:56:36 -05001751 struct lpfc_vport *vport = phba->pport;
1752 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001753 LPFC_MBOXQ_t *pmb;
1754 volatile uint32_t control;
1755 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05001756 int rc = 0;
dea31012005-04-17 16:05:31 -05001757
1758 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001759 if (!pmb) {
1760 rc = 1;
dea31012005-04-17 16:05:31 -05001761 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05001762 }
dea31012005-04-17 16:05:31 -05001763
1764 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001765 if (!mp) {
1766 rc = 2;
dea31012005-04-17 16:05:31 -05001767 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05001768 }
dea31012005-04-17 16:05:31 -05001769
1770 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05001771 if (!mp->virt) {
1772 rc = 3;
dea31012005-04-17 16:05:31 -05001773 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05001774 }
dea31012005-04-17 16:05:31 -05001775
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05001776 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04001777 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05001778
1779 psli->slistat.link_event++;
James Smart76a95d72010-11-20 23:11:48 -05001780 lpfc_read_topology(phba, pmb, mp);
1781 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smart2e0fef82007-06-17 19:56:36 -05001782 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04001783 /* Block ELS IOCBs until we have processed this mbox command */
1784 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04001785 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05001786 if (rc == MBX_NOT_FINISHED) {
1787 rc = 4;
James Smart14691152006-12-02 13:34:28 -05001788 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05001789 }
dea31012005-04-17 16:05:31 -05001790
1791 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05001792 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001793 writel(HA_LATT, phba->HAregaddr);
1794 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001795 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001796
1797 return;
1798
James Smart14691152006-12-02 13:34:28 -05001799lpfc_handle_latt_free_mbuf:
James Smart0d2b6b82008-06-14 22:52:47 -04001800 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05001801 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001802lpfc_handle_latt_free_mp:
1803 kfree(mp);
1804lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04001805 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001806lpfc_handle_latt_err_exit:
1807 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001808 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001809 psli->sli_flag |= LPFC_PROCESS_LA;
1810 control = readl(phba->HCregaddr);
1811 control |= HC_LAINT_ENA;
1812 writel(control, phba->HCregaddr);
1813 readl(phba->HCregaddr); /* flush */
1814
1815 /* Clear Link Attention in HA REG */
1816 writel(HA_LATT, phba->HAregaddr);
1817 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001818 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001819 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001820 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001821
James Smart09372822008-01-11 01:52:54 -05001822 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1823 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05001824
1825 return;
1826}
1827
James Smarte59058c2008-08-24 21:49:00 -04001828/**
James Smart3621a712009-04-06 18:47:14 -04001829 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04001830 * @phba: pointer to lpfc hba data structure.
1831 * @vpd: pointer to the vital product data.
1832 * @len: length of the vital product data in bytes.
1833 *
1834 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1835 * an array of characters. In this routine, the ModelName, ProgramType, and
1836 * ModelDesc, etc. fields of the phba data structure will be populated.
1837 *
1838 * Return codes
1839 * 0 - pointer to the VPD passed in is NULL
1840 * 1 - success
1841 **/
James Smart3772a992009-05-22 14:50:54 -04001842int
James Smart2e0fef82007-06-17 19:56:36 -05001843lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05001844{
1845 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05001846 int Length;
dea31012005-04-17 16:05:31 -05001847 int i, j;
1848 int finished = 0;
1849 int index = 0;
1850
1851 if (!vpd)
1852 return 0;
1853
1854 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05001855 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001856 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05001857 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1858 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001859 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05001860 switch (vpd[index]) {
1861 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001862 case 0x91:
dea31012005-04-17 16:05:31 -05001863 index += 1;
1864 lenlo = vpd[index];
1865 index += 1;
1866 lenhi = vpd[index];
1867 index += 1;
1868 i = ((((unsigned short)lenhi) << 8) + lenlo);
1869 index += i;
1870 break;
1871 case 0x90:
1872 index += 1;
1873 lenlo = vpd[index];
1874 index += 1;
1875 lenhi = vpd[index];
1876 index += 1;
1877 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001878 if (Length > len - index)
1879 Length = len - index;
dea31012005-04-17 16:05:31 -05001880 while (Length > 0) {
1881 /* Look for Serial Number */
1882 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1883 index += 2;
1884 i = vpd[index];
1885 index += 1;
1886 j = 0;
1887 Length -= (3+i);
1888 while(i--) {
1889 phba->SerialNumber[j++] = vpd[index++];
1890 if (j == 31)
1891 break;
1892 }
1893 phba->SerialNumber[j] = 0;
1894 continue;
1895 }
1896 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1897 phba->vpd_flag |= VPD_MODEL_DESC;
1898 index += 2;
1899 i = vpd[index];
1900 index += 1;
1901 j = 0;
1902 Length -= (3+i);
1903 while(i--) {
1904 phba->ModelDesc[j++] = vpd[index++];
1905 if (j == 255)
1906 break;
1907 }
1908 phba->ModelDesc[j] = 0;
1909 continue;
1910 }
1911 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1912 phba->vpd_flag |= VPD_MODEL_NAME;
1913 index += 2;
1914 i = vpd[index];
1915 index += 1;
1916 j = 0;
1917 Length -= (3+i);
1918 while(i--) {
1919 phba->ModelName[j++] = vpd[index++];
1920 if (j == 79)
1921 break;
1922 }
1923 phba->ModelName[j] = 0;
1924 continue;
1925 }
1926 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1927 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1928 index += 2;
1929 i = vpd[index];
1930 index += 1;
1931 j = 0;
1932 Length -= (3+i);
1933 while(i--) {
1934 phba->ProgramType[j++] = vpd[index++];
1935 if (j == 255)
1936 break;
1937 }
1938 phba->ProgramType[j] = 0;
1939 continue;
1940 }
1941 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1942 phba->vpd_flag |= VPD_PORT;
1943 index += 2;
1944 i = vpd[index];
1945 index += 1;
1946 j = 0;
1947 Length -= (3+i);
1948 while(i--) {
James Smartcd1c8302011-10-10 21:33:25 -04001949 if ((phba->sli_rev == LPFC_SLI_REV4) &&
1950 (phba->sli4_hba.pport_name_sta ==
1951 LPFC_SLI4_PPNAME_GET)) {
1952 j++;
1953 index++;
1954 } else
1955 phba->Port[j++] = vpd[index++];
1956 if (j == 19)
1957 break;
dea31012005-04-17 16:05:31 -05001958 }
James Smartcd1c8302011-10-10 21:33:25 -04001959 if ((phba->sli_rev != LPFC_SLI_REV4) ||
1960 (phba->sli4_hba.pport_name_sta ==
1961 LPFC_SLI4_PPNAME_NON))
1962 phba->Port[j] = 0;
dea31012005-04-17 16:05:31 -05001963 continue;
1964 }
1965 else {
1966 index += 2;
1967 i = vpd[index];
1968 index += 1;
1969 index += i;
1970 Length -= (3 + i);
1971 }
1972 }
1973 finished = 0;
1974 break;
1975 case 0x78:
1976 finished = 1;
1977 break;
1978 default:
1979 index ++;
1980 break;
1981 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001982 }
dea31012005-04-17 16:05:31 -05001983
1984 return(1);
1985}
1986
James Smarte59058c2008-08-24 21:49:00 -04001987/**
James Smart3621a712009-04-06 18:47:14 -04001988 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04001989 * @phba: pointer to lpfc hba data structure.
1990 * @mdp: pointer to the data structure to hold the derived model name.
1991 * @descp: pointer to the data structure to hold the derived description.
1992 *
1993 * This routine retrieves HBA's description based on its registered PCI device
1994 * ID. The @descp passed into this function points to an array of 256 chars. It
1995 * shall be returned with the model name, maximum speed, and the host bus type.
1996 * The @mdp passed into this function points to an array of 80 chars. When the
1997 * function returns, the @mdp will be filled with the model name.
1998 **/
dea31012005-04-17 16:05:31 -05001999static void
James Smart2e0fef82007-06-17 19:56:36 -05002000lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05002001{
2002 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05002003 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002004 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04002005 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04002006 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002007 struct {
James Smarta747c9c2009-11-18 15:41:10 -05002008 char *name;
2009 char *bus;
2010 char *function;
2011 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002012
2013 if (mdp && mdp[0] != '\0'
2014 && descp && descp[0] != '\0')
2015 return;
2016
James Smartc0c11512011-05-24 11:41:34 -04002017 if (phba->lmt & LMT_16Gb)
2018 max_speed = 16;
2019 else if (phba->lmt & LMT_10Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002020 max_speed = 10;
2021 else if (phba->lmt & LMT_8Gb)
2022 max_speed = 8;
2023 else if (phba->lmt & LMT_4Gb)
2024 max_speed = 4;
2025 else if (phba->lmt & LMT_2Gb)
2026 max_speed = 2;
James Smart4169d862012-09-29 11:32:09 -04002027 else if (phba->lmt & LMT_1Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002028 max_speed = 1;
James Smart4169d862012-09-29 11:32:09 -04002029 else
2030 max_speed = 0;
dea31012005-04-17 16:05:31 -05002031
2032 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05002033
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002034 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002035 case PCI_DEVICE_ID_FIREFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002036 m = (typeof(m)){"LP6000", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002037 break;
dea31012005-04-17 16:05:31 -05002038 case PCI_DEVICE_ID_SUPERFLY:
2039 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smarta747c9c2009-11-18 15:41:10 -05002040 m = (typeof(m)){"LP7000", "PCI",
2041 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002042 else
James Smarta747c9c2009-11-18 15:41:10 -05002043 m = (typeof(m)){"LP7000E", "PCI",
2044 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002045 break;
2046 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002047 m = (typeof(m)){"LP8000", "PCI",
2048 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002049 break;
2050 case PCI_DEVICE_ID_CENTAUR:
2051 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smarta747c9c2009-11-18 15:41:10 -05002052 m = (typeof(m)){"LP9002", "PCI",
2053 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002054 else
James Smarta747c9c2009-11-18 15:41:10 -05002055 m = (typeof(m)){"LP9000", "PCI",
2056 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002057 break;
2058 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002059 m = (typeof(m)){"LP952", "PCI",
2060 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002061 break;
2062 case PCI_DEVICE_ID_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05002063 m = (typeof(m)){"LP9802", "PCI-X",
2064 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002065 break;
2066 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05002067 m = (typeof(m)){"LP10000", "PCI-X",
2068 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002069 break;
2070 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05002071 m = (typeof(m)){"LPX1000", "PCI-X",
2072 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002073 break;
2074 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002075 m = (typeof(m)){"LP982", "PCI-X",
2076 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002077 break;
2078 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05002079 m = (typeof(m)){"LP1050", "PCI-X",
2080 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002081 break;
2082 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05002083 m = (typeof(m)){"LP11000", "PCI-X2",
2084 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002085 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002086 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002087 m = (typeof(m)){"LP11000-SP", "PCI-X2",
2088 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002089 break;
2090 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002091 m = (typeof(m)){"LP11002-SP", "PCI-X2",
2092 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002093 break;
2094 case PCI_DEVICE_ID_NEPTUNE:
James Smarta747c9c2009-11-18 15:41:10 -05002095 m = (typeof(m)){"LPe1000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002096 break;
2097 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002098 m = (typeof(m)){"LPe1000-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002099 break;
2100 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002101 m = (typeof(m)){"LPe1002-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002102 break;
dea31012005-04-17 16:05:31 -05002103 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05002104 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002105 break;
2106 case PCI_DEVICE_ID_BSMB:
James Smarta747c9c2009-11-18 15:41:10 -05002107 m = (typeof(m)){"LP111", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002108 break;
2109 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05002110 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002111 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002112 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002113 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002114 break;
2115 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002116 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04002117 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002118 break;
dea31012005-04-17 16:05:31 -05002119 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05002120 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002121 break;
2122 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05002123 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002124 break;
2125 case PCI_DEVICE_ID_LP101:
James Smarta747c9c2009-11-18 15:41:10 -05002126 m = (typeof(m)){"LP101", "PCI-X", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002127 break;
2128 case PCI_DEVICE_ID_LP10000S:
James Smarta747c9c2009-11-18 15:41:10 -05002129 m = (typeof(m)){"LP10000-S", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002130 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002131 case PCI_DEVICE_ID_LP11000S:
James Smarta747c9c2009-11-18 15:41:10 -05002132 m = (typeof(m)){"LP11000-S", "PCI-X2", "Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05002133 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002134 case PCI_DEVICE_ID_LPE11000S:
James Smarta747c9c2009-11-18 15:41:10 -05002135 m = (typeof(m)){"LPe11000-S", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002136 break;
James Smartb87eab32007-04-25 09:53:28 -04002137 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05002138 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002139 break;
2140 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05002141 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002142 break;
2143 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05002144 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002145 break;
2146 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002147 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002148 break;
2149 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002150 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002151 break;
2152 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05002153 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002154 break;
James Smart84774a42008-08-24 21:50:06 -04002155 case PCI_DEVICE_ID_HORNET:
James Smarta747c9c2009-11-18 15:41:10 -05002156 m = (typeof(m)){"LP21000", "PCIe", "FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002157 GE = 1;
2158 break;
2159 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05002160 m = (typeof(m)){"LPev12000", "PCIe IOV",
2161 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002162 break;
2163 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05002164 m = (typeof(m)){"LPev12000", "PCIe IOV",
2165 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002166 break;
2167 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05002168 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
2169 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002170 break;
James Smartda0436e2009-05-22 14:51:39 -04002171 case PCI_DEVICE_ID_TIGERSHARK:
2172 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002173 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04002174 break;
James Smarta747c9c2009-11-18 15:41:10 -05002175 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04002176 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002177 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2178 break;
2179 case PCI_DEVICE_ID_FALCON:
2180 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2181 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04002182 break;
James Smart98fc5dd2010-06-07 15:24:29 -04002183 case PCI_DEVICE_ID_BALIUS:
2184 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
2185 "Fibre Channel Adapter"};
2186 break;
James Smart085c6472010-11-20 23:11:37 -05002187 case PCI_DEVICE_ID_LANCER_FC:
James Smartc0c11512011-05-24 11:41:34 -04002188 case PCI_DEVICE_ID_LANCER_FC_VF:
2189 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
James Smart085c6472010-11-20 23:11:37 -05002190 break;
2191 case PCI_DEVICE_ID_LANCER_FCOE:
James Smartc0c11512011-05-24 11:41:34 -04002192 case PCI_DEVICE_ID_LANCER_FCOE_VF:
James Smart085c6472010-11-20 23:11:37 -05002193 oneConnect = 1;
James Smart079b5c92011-08-21 21:48:49 -04002194 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
James Smart085c6472010-11-20 23:11:37 -05002195 break;
James Smartf8cafd32012-08-03 12:42:51 -04002196 case PCI_DEVICE_ID_SKYHAWK:
2197 case PCI_DEVICE_ID_SKYHAWK_VF:
2198 oneConnect = 1;
2199 m = (typeof(m)){"OCe14000", "PCIe", "FCoE"};
2200 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002201 default:
James Smarta747c9c2009-11-18 15:41:10 -05002202 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002203 break;
dea31012005-04-17 16:05:31 -05002204 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002205
2206 if (mdp && mdp[0] == '\0')
2207 snprintf(mdp, 79,"%s", m.name);
James Smartc0c11512011-05-24 11:41:34 -04002208 /*
2209 * oneConnect hba requires special processing, they are all initiators
James Smartda0436e2009-05-22 14:51:39 -04002210 * and we put the port number on the end
2211 */
2212 if (descp && descp[0] == '\0') {
2213 if (oneConnect)
2214 snprintf(descp, 255,
James Smart4169d862012-09-29 11:32:09 -04002215 "Emulex OneConnect %s, %s Initiator %s",
James Smarta747c9c2009-11-18 15:41:10 -05002216 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04002217 phba->Port);
James Smart4169d862012-09-29 11:32:09 -04002218 else if (max_speed == 0)
2219 snprintf(descp, 255,
2220 "Emulex %s %s %s ",
2221 m.name, m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002222 else
2223 snprintf(descp, 255,
2224 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05002225 m.name, max_speed, (GE) ? "GE" : "Gb",
2226 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002227 }
dea31012005-04-17 16:05:31 -05002228}
2229
James Smarte59058c2008-08-24 21:49:00 -04002230/**
James Smart3621a712009-04-06 18:47:14 -04002231 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04002232 * @phba: pointer to lpfc hba data structure.
2233 * @pring: pointer to a IOCB ring.
2234 * @cnt: the number of IOCBs to be posted to the IOCB ring.
2235 *
2236 * This routine posts a given number of IOCBs with the associated DMA buffer
2237 * descriptors specified by the cnt argument to the given IOCB ring.
2238 *
2239 * Return codes
2240 * The number of IOCBs NOT able to be posted to the IOCB ring.
2241 **/
dea31012005-04-17 16:05:31 -05002242int
James Smart495a7142008-06-14 22:52:59 -04002243lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05002244{
2245 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002246 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05002247 struct lpfc_dmabuf *mp1, *mp2;
2248
2249 cnt += pring->missbufcnt;
2250
2251 /* While there are buffers to post */
2252 while (cnt > 0) {
2253 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002254 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002255 if (iocb == NULL) {
2256 pring->missbufcnt = cnt;
2257 return cnt;
2258 }
dea31012005-04-17 16:05:31 -05002259 icmd = &iocb->iocb;
2260
2261 /* 2 buffers can be posted per command */
2262 /* Allocate buffer to post */
2263 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2264 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04002265 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2266 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002267 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002268 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002269 pring->missbufcnt = cnt;
2270 return cnt;
2271 }
2272
2273 INIT_LIST_HEAD(&mp1->list);
2274 /* Allocate buffer to post */
2275 if (cnt > 1) {
2276 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2277 if (mp2)
2278 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2279 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04002280 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002281 kfree(mp2);
dea31012005-04-17 16:05:31 -05002282 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2283 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002284 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002285 pring->missbufcnt = cnt;
2286 return cnt;
2287 }
2288
2289 INIT_LIST_HEAD(&mp2->list);
2290 } else {
2291 mp2 = NULL;
2292 }
2293
2294 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2295 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2296 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2297 icmd->ulpBdeCount = 1;
2298 cnt--;
2299 if (mp2) {
2300 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2301 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2302 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2303 cnt--;
2304 icmd->ulpBdeCount = 2;
2305 }
2306
2307 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2308 icmd->ulpLe = 1;
2309
James Smart3772a992009-05-22 14:50:54 -04002310 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2311 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05002312 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2313 kfree(mp1);
2314 cnt++;
2315 if (mp2) {
2316 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2317 kfree(mp2);
2318 cnt++;
2319 }
James Bottomley604a3e32005-10-29 10:28:33 -05002320 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002321 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05002322 return cnt;
2323 }
dea31012005-04-17 16:05:31 -05002324 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05002325 if (mp2)
dea31012005-04-17 16:05:31 -05002326 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05002327 }
2328 pring->missbufcnt = 0;
2329 return 0;
2330}
2331
James Smarte59058c2008-08-24 21:49:00 -04002332/**
James Smart3621a712009-04-06 18:47:14 -04002333 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04002334 * @phba: pointer to lpfc hba data structure.
2335 *
2336 * This routine posts initial receive IOCB buffers to the ELS ring. The
2337 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
2338 * set to 64 IOCBs.
2339 *
2340 * Return codes
2341 * 0 - success (currently always success)
2342 **/
dea31012005-04-17 16:05:31 -05002343static int
James Smart2e0fef82007-06-17 19:56:36 -05002344lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002345{
2346 struct lpfc_sli *psli = &phba->sli;
2347
2348 /* Ring 0, ELS / CT buffers */
James Smart495a7142008-06-14 22:52:59 -04002349 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05002350 /* Ring 2 - FCP no buffers needed */
2351
2352 return 0;
2353}
2354
2355#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2356
James Smarte59058c2008-08-24 21:49:00 -04002357/**
James Smart3621a712009-04-06 18:47:14 -04002358 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04002359 * @HashResultPointer: pointer to an array as hash table.
2360 *
2361 * This routine sets up the initial values to the array of hash table entries
2362 * for the LC HBAs.
2363 **/
dea31012005-04-17 16:05:31 -05002364static void
2365lpfc_sha_init(uint32_t * HashResultPointer)
2366{
2367 HashResultPointer[0] = 0x67452301;
2368 HashResultPointer[1] = 0xEFCDAB89;
2369 HashResultPointer[2] = 0x98BADCFE;
2370 HashResultPointer[3] = 0x10325476;
2371 HashResultPointer[4] = 0xC3D2E1F0;
2372}
2373
James Smarte59058c2008-08-24 21:49:00 -04002374/**
James Smart3621a712009-04-06 18:47:14 -04002375 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04002376 * @HashResultPointer: pointer to an initial/result hash table.
2377 * @HashWorkingPointer: pointer to an working hash table.
2378 *
2379 * This routine iterates an initial hash table pointed by @HashResultPointer
2380 * with the values from the working hash table pointeed by @HashWorkingPointer.
2381 * The results are putting back to the initial hash table, returned through
2382 * the @HashResultPointer as the result hash table.
2383 **/
dea31012005-04-17 16:05:31 -05002384static void
2385lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2386{
2387 int t;
2388 uint32_t TEMP;
2389 uint32_t A, B, C, D, E;
2390 t = 16;
2391 do {
2392 HashWorkingPointer[t] =
2393 S(1,
2394 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2395 8] ^
2396 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2397 } while (++t <= 79);
2398 t = 0;
2399 A = HashResultPointer[0];
2400 B = HashResultPointer[1];
2401 C = HashResultPointer[2];
2402 D = HashResultPointer[3];
2403 E = HashResultPointer[4];
2404
2405 do {
2406 if (t < 20) {
2407 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2408 } else if (t < 40) {
2409 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2410 } else if (t < 60) {
2411 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2412 } else {
2413 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2414 }
2415 TEMP += S(5, A) + E + HashWorkingPointer[t];
2416 E = D;
2417 D = C;
2418 C = S(30, B);
2419 B = A;
2420 A = TEMP;
2421 } while (++t <= 79);
2422
2423 HashResultPointer[0] += A;
2424 HashResultPointer[1] += B;
2425 HashResultPointer[2] += C;
2426 HashResultPointer[3] += D;
2427 HashResultPointer[4] += E;
2428
2429}
2430
James Smarte59058c2008-08-24 21:49:00 -04002431/**
James Smart3621a712009-04-06 18:47:14 -04002432 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002433 * @RandomChallenge: pointer to the entry of host challenge random number array.
2434 * @HashWorking: pointer to the entry of the working hash array.
2435 *
2436 * This routine calculates the working hash array referred by @HashWorking
2437 * from the challenge random numbers associated with the host, referred by
2438 * @RandomChallenge. The result is put into the entry of the working hash
2439 * array and returned by reference through @HashWorking.
2440 **/
dea31012005-04-17 16:05:31 -05002441static void
2442lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2443{
2444 *HashWorking = (*RandomChallenge ^ *HashWorking);
2445}
2446
James Smarte59058c2008-08-24 21:49:00 -04002447/**
James Smart3621a712009-04-06 18:47:14 -04002448 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002449 * @phba: pointer to lpfc hba data structure.
2450 * @hbainit: pointer to an array of unsigned 32-bit integers.
2451 *
2452 * This routine performs the special handling for LC HBA initialization.
2453 **/
dea31012005-04-17 16:05:31 -05002454void
2455lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2456{
2457 int t;
2458 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002459 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002460
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002461 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002462 if (!HashWorking)
2463 return;
2464
dea31012005-04-17 16:05:31 -05002465 HashWorking[0] = HashWorking[78] = *pwwnn++;
2466 HashWorking[1] = HashWorking[79] = *pwwnn;
2467
2468 for (t = 0; t < 7; t++)
2469 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2470
2471 lpfc_sha_init(hbainit);
2472 lpfc_sha_iterate(hbainit, HashWorking);
2473 kfree(HashWorking);
2474}
2475
James Smarte59058c2008-08-24 21:49:00 -04002476/**
James Smart3621a712009-04-06 18:47:14 -04002477 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002478 * @vport: pointer to a virtual N_Port data structure.
2479 *
2480 * This routine performs the necessary cleanups before deleting the @vport.
2481 * It invokes the discovery state machine to perform necessary state
2482 * transitions and to release the ndlps associated with the @vport. Note,
2483 * the physical port is treated as @vport 0.
2484 **/
James Smart87af33f2007-10-27 13:37:43 -04002485void
James Smart2e0fef82007-06-17 19:56:36 -05002486lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002487{
James Smart87af33f2007-10-27 13:37:43 -04002488 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002489 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002490 int i = 0;
dea31012005-04-17 16:05:31 -05002491
James Smart87af33f2007-10-27 13:37:43 -04002492 if (phba->link_state > LPFC_LINK_DOWN)
2493 lpfc_port_link_failure(vport);
2494
2495 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002496 if (!NLP_CHK_NODE_ACT(ndlp)) {
2497 ndlp = lpfc_enable_node(vport, ndlp,
2498 NLP_STE_UNUSED_NODE);
2499 if (!ndlp)
2500 continue;
2501 spin_lock_irq(&phba->ndlp_lock);
2502 NLP_SET_FREE_REQ(ndlp);
2503 spin_unlock_irq(&phba->ndlp_lock);
2504 /* Trigger the release of the ndlp memory */
2505 lpfc_nlp_put(ndlp);
2506 continue;
2507 }
2508 spin_lock_irq(&phba->ndlp_lock);
2509 if (NLP_CHK_FREE_REQ(ndlp)) {
2510 /* The ndlp should not be in memory free mode already */
2511 spin_unlock_irq(&phba->ndlp_lock);
2512 continue;
2513 } else
2514 /* Indicate request for freeing ndlp memory */
2515 NLP_SET_FREE_REQ(ndlp);
2516 spin_unlock_irq(&phba->ndlp_lock);
2517
James Smart58da1ff2008-04-07 10:15:56 -04002518 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2519 ndlp->nlp_DID == Fabric_DID) {
2520 /* Just free up ndlp with Fabric_DID for vports */
2521 lpfc_nlp_put(ndlp);
2522 continue;
2523 }
2524
James Smarteff4a012012-01-18 16:25:25 -05002525 /* take care of nodes in unused state before the state
2526 * machine taking action.
2527 */
2528 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
2529 lpfc_nlp_put(ndlp);
2530 continue;
2531 }
2532
James Smart87af33f2007-10-27 13:37:43 -04002533 if (ndlp->nlp_type & NLP_FABRIC)
2534 lpfc_disc_state_machine(vport, ndlp, NULL,
2535 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002536
James Smart87af33f2007-10-27 13:37:43 -04002537 lpfc_disc_state_machine(vport, ndlp, NULL,
2538 NLP_EVT_DEVICE_RM);
2539 }
2540
James Smarta8adb832007-10-27 13:37:53 -04002541 /* At this point, ALL ndlp's should be gone
2542 * because of the previous NLP_EVT_DEVICE_RM.
2543 * Lets wait for this to happen, if needed.
2544 */
James Smart87af33f2007-10-27 13:37:43 -04002545 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002546 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002547 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002548 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002549 list_for_each_entry_safe(ndlp, next_ndlp,
2550 &vport->fc_nodes, nlp_listp) {
2551 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2552 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002553 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002554 "usgmap:x%x refcnt:%d\n",
2555 ndlp->nlp_DID, (void *)ndlp,
2556 ndlp->nlp_usg_map,
2557 atomic_read(
2558 &ndlp->kref.refcount));
2559 }
James Smarta8adb832007-10-27 13:37:53 -04002560 break;
James Smart87af33f2007-10-27 13:37:43 -04002561 }
James Smarta8adb832007-10-27 13:37:53 -04002562
2563 /* Wait for any activity on ndlps to settle */
2564 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002565 }
James Smart1151e3e2011-02-16 12:39:35 -05002566 lpfc_cleanup_vports_rrqs(vport, NULL);
dea31012005-04-17 16:05:31 -05002567}
2568
James Smarte59058c2008-08-24 21:49:00 -04002569/**
James Smart3621a712009-04-06 18:47:14 -04002570 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002571 * @vport: pointer to a virtual N_Port data structure.
2572 *
2573 * This routine stops all the timers associated with a @vport. This function
2574 * is invoked before disabling or deleting a @vport. Note that the physical
2575 * port is treated as @vport 0.
2576 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002577void
2578lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002579{
James Smart92d7f7b2007-06-17 19:56:38 -05002580 del_timer_sync(&vport->els_tmofunc);
2581 del_timer_sync(&vport->fc_fdmitmo);
James Smart92494142011-02-16 12:39:44 -05002582 del_timer_sync(&vport->delayed_disc_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002583 lpfc_can_disctmo(vport);
2584 return;
dea31012005-04-17 16:05:31 -05002585}
2586
James Smarte59058c2008-08-24 21:49:00 -04002587/**
James Smartecfd03c2010-02-12 14:41:27 -05002588 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2589 * @phba: pointer to lpfc hba data structure.
2590 *
2591 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2592 * caller of this routine should already hold the host lock.
2593 **/
2594void
2595__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2596{
James Smart5ac6b302010-10-22 11:05:36 -04002597 /* Clear pending FCF rediscovery wait flag */
2598 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2599
James Smartecfd03c2010-02-12 14:41:27 -05002600 /* Now, try to stop the timer */
2601 del_timer(&phba->fcf.redisc_wait);
2602}
2603
2604/**
2605 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2606 * @phba: pointer to lpfc hba data structure.
2607 *
2608 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2609 * checks whether the FCF rediscovery wait timer is pending with the host
2610 * lock held before proceeding with disabling the timer and clearing the
2611 * wait timer pendig flag.
2612 **/
2613void
2614lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2615{
2616 spin_lock_irq(&phba->hbalock);
2617 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2618 /* FCF rediscovery timer already fired or stopped */
2619 spin_unlock_irq(&phba->hbalock);
2620 return;
2621 }
2622 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart5ac6b302010-10-22 11:05:36 -04002623 /* Clear failover in progress flags */
2624 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05002625 spin_unlock_irq(&phba->hbalock);
2626}
2627
2628/**
James Smart3772a992009-05-22 14:50:54 -04002629 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002630 * @phba: pointer to lpfc hba data structure.
2631 *
2632 * This routine stops all the timers associated with a HBA. This function is
2633 * invoked before either putting a HBA offline or unloading the driver.
2634 **/
James Smart3772a992009-05-22 14:50:54 -04002635void
2636lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002637{
James Smart51ef4c22007-08-02 11:10:31 -04002638 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05002639 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002640 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002641 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002642 del_timer_sync(&phba->hb_tmofunc);
James Smart1151e3e2011-02-16 12:39:35 -05002643 if (phba->sli_rev == LPFC_SLI_REV4) {
2644 del_timer_sync(&phba->rrq_tmr);
2645 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2646 }
James Smart3772a992009-05-22 14:50:54 -04002647 phba->hb_outstanding = 0;
2648
2649 switch (phba->pci_dev_grp) {
2650 case LPFC_PCI_DEV_LP:
2651 /* Stop any LightPulse device specific driver timers */
2652 del_timer_sync(&phba->fcp_poll_timer);
2653 break;
2654 case LPFC_PCI_DEV_OC:
2655 /* Stop any OneConnect device sepcific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05002656 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04002657 break;
2658 default:
2659 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2660 "0297 Invalid device group (x%x)\n",
2661 phba->pci_dev_grp);
2662 break;
2663 }
James Smart2e0fef82007-06-17 19:56:36 -05002664 return;
dea31012005-04-17 16:05:31 -05002665}
2666
James Smarte59058c2008-08-24 21:49:00 -04002667/**
James Smart3621a712009-04-06 18:47:14 -04002668 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002669 * @phba: pointer to lpfc hba data structure.
2670 *
2671 * This routine marks a HBA's management interface as blocked. Once the HBA's
2672 * management interface is marked as blocked, all the user space access to
2673 * the HBA, whether they are from sysfs interface or libdfc interface will
2674 * all be blocked. The HBA is set to block the management interface when the
2675 * driver prepares the HBA interface for online or offline.
2676 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002677static void
James Smart618a5232012-06-12 13:54:36 -04002678lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
Adrian Bunka6ababd2007-11-05 18:07:33 +01002679{
2680 unsigned long iflag;
James Smart6e7288d2010-06-07 15:23:35 -04002681 uint8_t actcmd = MBX_HEARTBEAT;
2682 unsigned long timeout;
2683
Adrian Bunka6ababd2007-11-05 18:07:33 +01002684 spin_lock_irqsave(&phba->hbalock, iflag);
2685 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
James Smart618a5232012-06-12 13:54:36 -04002686 spin_unlock_irqrestore(&phba->hbalock, iflag);
2687 if (mbx_action == LPFC_MBX_NO_WAIT)
2688 return;
2689 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
2690 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002691 if (phba->sli.mbox_active) {
James Smart6e7288d2010-06-07 15:23:35 -04002692 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
James Smarta183a152011-10-10 21:32:43 -04002693 /* Determine how long we might wait for the active mailbox
2694 * command to be gracefully completed by firmware.
2695 */
2696 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
2697 phba->sli.mbox_active) * 1000) + jiffies;
2698 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002699 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002700
James Smart6e7288d2010-06-07 15:23:35 -04002701 /* Wait for the outstnading mailbox command to complete */
2702 while (phba->sli.mbox_active) {
2703 /* Check active mailbox complete status every 2ms */
2704 msleep(2);
2705 if (time_after(jiffies, timeout)) {
2706 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2707 "2813 Mgmt IO is Blocked %x "
2708 "- mbox cmd %x still active\n",
2709 phba->sli.sli_flag, actcmd);
2710 break;
2711 }
2712 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002713}
2714
James Smarte59058c2008-08-24 21:49:00 -04002715/**
James Smart6b5151f2012-01-18 16:24:06 -05002716 * lpfc_sli4_node_prep - Assign RPIs for active nodes.
2717 * @phba: pointer to lpfc hba data structure.
2718 *
2719 * Allocate RPIs for all active remote nodes. This is needed whenever
2720 * an SLI4 adapter is reset and the driver is not unloading. Its purpose
2721 * is to fixup the temporary rpi assignments.
2722 **/
2723void
2724lpfc_sli4_node_prep(struct lpfc_hba *phba)
2725{
2726 struct lpfc_nodelist *ndlp, *next_ndlp;
2727 struct lpfc_vport **vports;
2728 int i;
2729
2730 if (phba->sli_rev != LPFC_SLI_REV4)
2731 return;
2732
2733 vports = lpfc_create_vport_work_array(phba);
2734 if (vports != NULL) {
2735 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2736 if (vports[i]->load_flag & FC_UNLOADING)
2737 continue;
2738
2739 list_for_each_entry_safe(ndlp, next_ndlp,
2740 &vports[i]->fc_nodes,
2741 nlp_listp) {
2742 if (NLP_CHK_NODE_ACT(ndlp))
2743 ndlp->nlp_rpi =
2744 lpfc_sli4_alloc_rpi(phba);
2745 }
2746 }
2747 }
2748 lpfc_destroy_vport_work_array(phba, vports);
2749}
2750
2751/**
James Smart3621a712009-04-06 18:47:14 -04002752 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04002753 * @phba: pointer to lpfc hba data structure.
2754 *
2755 * This routine initializes the HBA and brings a HBA online. During this
2756 * process, the management interface is blocked to prevent user space access
2757 * to the HBA interfering with the driver initialization.
2758 *
2759 * Return codes
2760 * 0 - successful
2761 * 1 - failed
2762 **/
dea31012005-04-17 16:05:31 -05002763int
James Smart2e0fef82007-06-17 19:56:36 -05002764lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002765{
Julia Lawall372bd282008-12-16 16:15:08 +01002766 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04002767 struct lpfc_vport **vports;
2768 int i;
James Smart16a3a202013-04-17 20:14:38 -04002769 bool vpis_cleared = false;
James Smart2e0fef82007-06-17 19:56:36 -05002770
dea31012005-04-17 16:05:31 -05002771 if (!phba)
2772 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01002773 vport = phba->pport;
dea31012005-04-17 16:05:31 -05002774
James Smart2e0fef82007-06-17 19:56:36 -05002775 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05002776 return 0;
2777
James Smarted957682007-06-17 19:56:37 -05002778 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002779 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05002780
James Smart618a5232012-06-12 13:54:36 -04002781 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05002782
James Smart46fa3112007-04-25 09:51:45 -04002783 if (!lpfc_sli_queue_setup(phba)) {
2784 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002785 return 1;
James Smart46fa3112007-04-25 09:51:45 -04002786 }
2787
James Smartda0436e2009-05-22 14:51:39 -04002788 if (phba->sli_rev == LPFC_SLI_REV4) {
2789 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2790 lpfc_unblock_mgmt_io(phba);
2791 return 1;
2792 }
James Smart16a3a202013-04-17 20:14:38 -04002793 spin_lock_irq(&phba->hbalock);
2794 if (!phba->sli4_hba.max_cfg_param.vpi_used)
2795 vpis_cleared = true;
2796 spin_unlock_irq(&phba->hbalock);
James Smartda0436e2009-05-22 14:51:39 -04002797 } else {
2798 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2799 lpfc_unblock_mgmt_io(phba);
2800 return 1;
2801 }
James Smart46fa3112007-04-25 09:51:45 -04002802 }
dea31012005-04-17 16:05:31 -05002803
James Smart549e55c2007-08-02 11:09:51 -04002804 vports = lpfc_create_vport_work_array(phba);
2805 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002806 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002807 struct Scsi_Host *shost;
2808 shost = lpfc_shost_from_vport(vports[i]);
2809 spin_lock_irq(shost->host_lock);
2810 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2811 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2812 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart16a3a202013-04-17 20:14:38 -04002813 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart1c6834a2009-07-19 10:01:26 -04002814 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart16a3a202013-04-17 20:14:38 -04002815 if ((vpis_cleared) &&
2816 (vports[i]->port_type !=
2817 LPFC_PHYSICAL_PORT))
2818 vports[i]->vpi = 0;
2819 }
James Smart549e55c2007-08-02 11:09:51 -04002820 spin_unlock_irq(shost->host_lock);
2821 }
James Smart09372822008-01-11 01:52:54 -05002822 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002823
James Smart46fa3112007-04-25 09:51:45 -04002824 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002825 return 0;
2826}
2827
James Smarte59058c2008-08-24 21:49:00 -04002828/**
James Smart3621a712009-04-06 18:47:14 -04002829 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04002830 * @phba: pointer to lpfc hba data structure.
2831 *
2832 * This routine marks a HBA's management interface as not blocked. Once the
2833 * HBA's management interface is marked as not blocked, all the user space
2834 * access to the HBA, whether they are from sysfs interface or libdfc
2835 * interface will be allowed. The HBA is set to block the management interface
2836 * when the driver prepares the HBA interface for online or offline and then
2837 * set to unblock the management interface afterwards.
2838 **/
James Smart46fa3112007-04-25 09:51:45 -04002839void
James Smart46fa3112007-04-25 09:51:45 -04002840lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2841{
2842 unsigned long iflag;
2843
James Smart2e0fef82007-06-17 19:56:36 -05002844 spin_lock_irqsave(&phba->hbalock, iflag);
2845 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2846 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04002847}
2848
James Smarte59058c2008-08-24 21:49:00 -04002849/**
James Smart3621a712009-04-06 18:47:14 -04002850 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04002851 * @phba: pointer to lpfc hba data structure.
2852 *
2853 * This routine is invoked to prepare a HBA to be brought offline. It performs
2854 * unregistration login to all the nodes on all vports and flushes the mailbox
2855 * queue to make it ready to be brought offline.
2856 **/
James Smart46fa3112007-04-25 09:51:45 -04002857void
James Smart618a5232012-06-12 13:54:36 -04002858lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
James Smart46fa3112007-04-25 09:51:45 -04002859{
James Smart2e0fef82007-06-17 19:56:36 -05002860 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04002861 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04002862 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05002863 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04002864 int i;
dea31012005-04-17 16:05:31 -05002865
James Smart2e0fef82007-06-17 19:56:36 -05002866 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002867 return;
dea31012005-04-17 16:05:31 -05002868
James Smart618a5232012-06-12 13:54:36 -04002869 lpfc_block_mgmt_io(phba, mbx_action);
dea31012005-04-17 16:05:31 -05002870
2871 lpfc_linkdown(phba);
2872
James Smart87af33f2007-10-27 13:37:43 -04002873 /* Issue an unreg_login to all nodes on all vports */
2874 vports = lpfc_create_vport_work_array(phba);
2875 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04002876 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04002877 if (vports[i]->load_flag & FC_UNLOADING)
2878 continue;
James Smart72100cc2010-02-12 14:43:01 -05002879 shost = lpfc_shost_from_vport(vports[i]);
2880 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002881 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002882 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2883 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05002884 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002885
James Smart87af33f2007-10-27 13:37:43 -04002886 shost = lpfc_shost_from_vport(vports[i]);
2887 list_for_each_entry_safe(ndlp, next_ndlp,
2888 &vports[i]->fc_nodes,
2889 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002890 if (!NLP_CHK_NODE_ACT(ndlp))
2891 continue;
James Smart87af33f2007-10-27 13:37:43 -04002892 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2893 continue;
2894 if (ndlp->nlp_type & NLP_FABRIC) {
2895 lpfc_disc_state_machine(vports[i], ndlp,
2896 NULL, NLP_EVT_DEVICE_RECOVERY);
2897 lpfc_disc_state_machine(vports[i], ndlp,
2898 NULL, NLP_EVT_DEVICE_RM);
2899 }
2900 spin_lock_irq(shost->host_lock);
2901 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart401ee0c2012-03-01 22:35:34 -05002902 spin_unlock_irq(shost->host_lock);
James Smart6b5151f2012-01-18 16:24:06 -05002903 /*
2904 * Whenever an SLI4 port goes offline, free the
James Smart401ee0c2012-03-01 22:35:34 -05002905 * RPI. Get a new RPI when the adapter port
2906 * comes back online.
James Smart6b5151f2012-01-18 16:24:06 -05002907 */
2908 if (phba->sli_rev == LPFC_SLI_REV4)
2909 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smart87af33f2007-10-27 13:37:43 -04002910 lpfc_unreg_rpi(vports[i], ndlp);
2911 }
2912 }
2913 }
James Smart09372822008-01-11 01:52:54 -05002914 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002915
James Smart618a5232012-06-12 13:54:36 -04002916 lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
James Smart46fa3112007-04-25 09:51:45 -04002917}
2918
James Smarte59058c2008-08-24 21:49:00 -04002919/**
James Smart3621a712009-04-06 18:47:14 -04002920 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04002921 * @phba: pointer to lpfc hba data structure.
2922 *
2923 * This routine actually brings a HBA offline. It stops all the timers
2924 * associated with the HBA, brings down the SLI layer, and eventually
2925 * marks the HBA as in offline state for the upper layer protocol.
2926 **/
James Smart46fa3112007-04-25 09:51:45 -04002927void
James Smart2e0fef82007-06-17 19:56:36 -05002928lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04002929{
James Smart549e55c2007-08-02 11:09:51 -04002930 struct Scsi_Host *shost;
2931 struct lpfc_vport **vports;
2932 int i;
James Smart46fa3112007-04-25 09:51:45 -04002933
James Smart549e55c2007-08-02 11:09:51 -04002934 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002935 return;
James Smart688a8862006-07-06 15:49:56 -04002936
James Smartda0436e2009-05-22 14:51:39 -04002937 /* stop port and all timers associated with this hba */
2938 lpfc_stop_port(phba);
James Smart51ef4c22007-08-02 11:10:31 -04002939 vports = lpfc_create_vport_work_array(phba);
2940 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002941 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04002942 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05002943 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05002944 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002945 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05002946 /* Bring down the SLI Layer and cleanup. The HBA is offline
2947 now. */
2948 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002949 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04002950 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002951 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04002952 vports = lpfc_create_vport_work_array(phba);
2953 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002954 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002955 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04002956 spin_lock_irq(shost->host_lock);
2957 vports[i]->work_port_events = 0;
2958 vports[i]->fc_flag |= FC_OFFLINE_MODE;
2959 spin_unlock_irq(shost->host_lock);
2960 }
James Smart09372822008-01-11 01:52:54 -05002961 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002962}
2963
James Smarte59058c2008-08-24 21:49:00 -04002964/**
James Smart3621a712009-04-06 18:47:14 -04002965 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04002966 * @phba: pointer to lpfc hba data structure.
2967 *
2968 * This routine is to free all the SCSI buffers and IOCBs from the driver
2969 * list back to kernel. It is called from lpfc_pci_remove_one to free
2970 * the internal resources before the device is removed from the system.
James Smarte59058c2008-08-24 21:49:00 -04002971 **/
James Smart8a9d2e82012-05-09 21:16:12 -04002972static void
James Smart2e0fef82007-06-17 19:56:36 -05002973lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002974{
2975 struct lpfc_scsi_buf *sb, *sb_next;
2976 struct lpfc_iocbq *io, *io_next;
2977
James Smart2e0fef82007-06-17 19:56:36 -05002978 spin_lock_irq(&phba->hbalock);
James Smarta40fc5f2013-04-17 20:17:40 -04002979
dea31012005-04-17 16:05:31 -05002980 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smarta40fc5f2013-04-17 20:17:40 -04002981
2982 spin_lock(&phba->scsi_buf_list_put_lock);
2983 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_put,
2984 list) {
dea31012005-04-17 16:05:31 -05002985 list_del(&sb->list);
2986 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05002987 sb->dma_handle);
dea31012005-04-17 16:05:31 -05002988 kfree(sb);
2989 phba->total_scsi_bufs--;
2990 }
James Smarta40fc5f2013-04-17 20:17:40 -04002991 spin_unlock(&phba->scsi_buf_list_put_lock);
2992
2993 spin_lock(&phba->scsi_buf_list_get_lock);
2994 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_get,
2995 list) {
2996 list_del(&sb->list);
2997 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
2998 sb->dma_handle);
2999 kfree(sb);
3000 phba->total_scsi_bufs--;
3001 }
3002 spin_unlock(&phba->scsi_buf_list_get_lock);
dea31012005-04-17 16:05:31 -05003003
3004 /* Release all the lpfc_iocbq entries maintained by this host. */
3005 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
3006 list_del(&io->list);
3007 kfree(io);
3008 phba->total_iocbq_bufs--;
3009 }
James Smart6d368e52011-05-24 11:44:12 -04003010
James Smart2e0fef82007-06-17 19:56:36 -05003011 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003012}
3013
3014/**
3015 * lpfc_sli4_xri_sgl_update - update xri-sgl sizing and mapping
3016 * @phba: pointer to lpfc hba data structure.
3017 *
3018 * This routine first calculates the sizes of the current els and allocated
3019 * scsi sgl lists, and then goes through all sgls to updates the physical
3020 * XRIs assigned due to port function reset. During port initialization, the
3021 * current els and allocated scsi sgl lists are 0s.
3022 *
3023 * Return codes
3024 * 0 - successful (for now, it always returns 0)
3025 **/
3026int
3027lpfc_sli4_xri_sgl_update(struct lpfc_hba *phba)
3028{
3029 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
3030 struct lpfc_scsi_buf *psb = NULL, *psb_next = NULL;
3031 uint16_t i, lxri, xri_cnt, els_xri_cnt, scsi_xri_cnt;
3032 LIST_HEAD(els_sgl_list);
3033 LIST_HEAD(scsi_sgl_list);
3034 int rc;
3035
3036 /*
3037 * update on pci function's els xri-sgl list
3038 */
3039 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
3040 if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
3041 /* els xri-sgl expanded */
3042 xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
3043 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3044 "3157 ELS xri-sgl count increased from "
3045 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3046 els_xri_cnt);
3047 /* allocate the additional els sgls */
3048 for (i = 0; i < xri_cnt; i++) {
3049 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
3050 GFP_KERNEL);
3051 if (sglq_entry == NULL) {
3052 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3053 "2562 Failure to allocate an "
3054 "ELS sgl entry:%d\n", i);
3055 rc = -ENOMEM;
3056 goto out_free_mem;
3057 }
3058 sglq_entry->buff_type = GEN_BUFF_TYPE;
3059 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
3060 &sglq_entry->phys);
3061 if (sglq_entry->virt == NULL) {
3062 kfree(sglq_entry);
3063 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3064 "2563 Failure to allocate an "
3065 "ELS mbuf:%d\n", i);
3066 rc = -ENOMEM;
3067 goto out_free_mem;
3068 }
3069 sglq_entry->sgl = sglq_entry->virt;
3070 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
3071 sglq_entry->state = SGL_FREED;
3072 list_add_tail(&sglq_entry->list, &els_sgl_list);
3073 }
James Smart38c20672013-03-01 16:37:44 -05003074 spin_lock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003075 list_splice_init(&els_sgl_list, &phba->sli4_hba.lpfc_sgl_list);
James Smart38c20672013-03-01 16:37:44 -05003076 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04003077 } else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
3078 /* els xri-sgl shrinked */
3079 xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
3080 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3081 "3158 ELS xri-sgl count decreased from "
3082 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
3083 els_xri_cnt);
3084 spin_lock_irq(&phba->hbalock);
3085 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &els_sgl_list);
3086 spin_unlock_irq(&phba->hbalock);
3087 /* release extra els sgls from list */
3088 for (i = 0; i < xri_cnt; i++) {
3089 list_remove_head(&els_sgl_list,
3090 sglq_entry, struct lpfc_sglq, list);
3091 if (sglq_entry) {
3092 lpfc_mbuf_free(phba, sglq_entry->virt,
3093 sglq_entry->phys);
3094 kfree(sglq_entry);
3095 }
3096 }
3097 spin_lock_irq(&phba->hbalock);
3098 list_splice_init(&els_sgl_list, &phba->sli4_hba.lpfc_sgl_list);
3099 spin_unlock_irq(&phba->hbalock);
3100 } else
3101 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3102 "3163 ELS xri-sgl count unchanged: %d\n",
3103 els_xri_cnt);
3104 phba->sli4_hba.els_xri_cnt = els_xri_cnt;
3105
3106 /* update xris to els sgls on the list */
3107 sglq_entry = NULL;
3108 sglq_entry_next = NULL;
3109 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
3110 &phba->sli4_hba.lpfc_sgl_list, list) {
3111 lxri = lpfc_sli4_next_xritag(phba);
3112 if (lxri == NO_XRI) {
3113 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3114 "2400 Failed to allocate xri for "
3115 "ELS sgl\n");
3116 rc = -ENOMEM;
3117 goto out_free_mem;
3118 }
3119 sglq_entry->sli4_lxritag = lxri;
3120 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3121 }
3122
3123 /*
3124 * update on pci function's allocated scsi xri-sgl list
3125 */
3126 phba->total_scsi_bufs = 0;
3127
3128 /* maximum number of xris available for scsi buffers */
3129 phba->sli4_hba.scsi_xri_max = phba->sli4_hba.max_cfg_param.max_xri -
3130 els_xri_cnt;
3131
3132 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3133 "2401 Current allocated SCSI xri-sgl count:%d, "
3134 "maximum SCSI xri count:%d\n",
3135 phba->sli4_hba.scsi_xri_cnt,
3136 phba->sli4_hba.scsi_xri_max);
3137
James Smarta40fc5f2013-04-17 20:17:40 -04003138 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart164cecd2013-09-06 12:22:38 -04003139 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003140 list_splice_init(&phba->lpfc_scsi_buf_list_get, &scsi_sgl_list);
3141 list_splice(&phba->lpfc_scsi_buf_list_put, &scsi_sgl_list);
James Smart164cecd2013-09-06 12:22:38 -04003142 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003143 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003144
3145 if (phba->sli4_hba.scsi_xri_cnt > phba->sli4_hba.scsi_xri_max) {
3146 /* max scsi xri shrinked below the allocated scsi buffers */
3147 scsi_xri_cnt = phba->sli4_hba.scsi_xri_cnt -
3148 phba->sli4_hba.scsi_xri_max;
3149 /* release the extra allocated scsi buffers */
3150 for (i = 0; i < scsi_xri_cnt; i++) {
3151 list_remove_head(&scsi_sgl_list, psb,
3152 struct lpfc_scsi_buf, list);
3153 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, psb->data,
3154 psb->dma_handle);
3155 kfree(psb);
3156 }
James Smarta40fc5f2013-04-17 20:17:40 -04003157 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003158 phba->sli4_hba.scsi_xri_cnt -= scsi_xri_cnt;
James Smarta40fc5f2013-04-17 20:17:40 -04003159 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003160 }
3161
3162 /* update xris associated to remaining allocated scsi buffers */
3163 psb = NULL;
3164 psb_next = NULL;
3165 list_for_each_entry_safe(psb, psb_next, &scsi_sgl_list, list) {
3166 lxri = lpfc_sli4_next_xritag(phba);
3167 if (lxri == NO_XRI) {
3168 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3169 "2560 Failed to allocate xri for "
3170 "scsi buffer\n");
3171 rc = -ENOMEM;
3172 goto out_free_mem;
3173 }
3174 psb->cur_iocbq.sli4_lxritag = lxri;
3175 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3176 }
James Smarta40fc5f2013-04-17 20:17:40 -04003177 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart164cecd2013-09-06 12:22:38 -04003178 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003179 list_splice_init(&scsi_sgl_list, &phba->lpfc_scsi_buf_list_get);
3180 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
James Smart164cecd2013-09-06 12:22:38 -04003181 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003182 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003183
dea31012005-04-17 16:05:31 -05003184 return 0;
James Smart8a9d2e82012-05-09 21:16:12 -04003185
3186out_free_mem:
3187 lpfc_free_els_sgl_list(phba);
3188 lpfc_scsi_free(phba);
3189 return rc;
dea31012005-04-17 16:05:31 -05003190}
3191
James Smarte59058c2008-08-24 21:49:00 -04003192/**
James Smart3621a712009-04-06 18:47:14 -04003193 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04003194 * @phba: pointer to lpfc hba data structure.
3195 * @instance: a unique integer ID to this FC port.
3196 * @dev: pointer to the device data structure.
3197 *
3198 * This routine creates a FC port for the upper layer protocol. The FC port
3199 * can be created on top of either a physical port or a virtual port provided
3200 * by the HBA. This routine also allocates a SCSI host data structure (shost)
3201 * and associates the FC port created before adding the shost into the SCSI
3202 * layer.
3203 *
3204 * Return codes
3205 * @vport - pointer to the virtual N_Port data structure.
3206 * NULL - port create failed.
3207 **/
James Smart2e0fef82007-06-17 19:56:36 -05003208struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04003209lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04003210{
James Smart2e0fef82007-06-17 19:56:36 -05003211 struct lpfc_vport *vport;
3212 struct Scsi_Host *shost;
3213 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04003214
James Smart3de2a652007-08-02 11:09:59 -04003215 if (dev != &phba->pcidev->dev)
3216 shost = scsi_host_alloc(&lpfc_vport_template,
3217 sizeof(struct lpfc_vport));
3218 else
3219 shost = scsi_host_alloc(&lpfc_template,
3220 sizeof(struct lpfc_vport));
James Smart2e0fef82007-06-17 19:56:36 -05003221 if (!shost)
3222 goto out;
James Smart47a86172007-04-25 09:53:22 -04003223
James Smart2e0fef82007-06-17 19:56:36 -05003224 vport = (struct lpfc_vport *) shost->hostdata;
3225 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05003226 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05003227 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05003228 vport->fc_rscn_flush = 0;
James Smart47a86172007-04-25 09:53:22 -04003229
James Smart3de2a652007-08-02 11:09:59 -04003230 lpfc_get_vport_cfgparam(vport);
James Smart2e0fef82007-06-17 19:56:36 -05003231 shost->unique_id = instance;
3232 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04003233 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05003234 shost->this_id = -1;
3235 shost->max_cmd_len = 16;
James Smartda0436e2009-05-22 14:51:39 -04003236 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart28baac72010-02-12 14:42:03 -05003237 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04003238 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smartda0436e2009-05-22 14:51:39 -04003239 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
3240 }
James Smart81301a92008-12-04 22:39:46 -05003241
James Smart47a86172007-04-25 09:53:22 -04003242 /*
James Smart2e0fef82007-06-17 19:56:36 -05003243 * Set initial can_queue value since 0 is no longer supported and
3244 * scsi_add_host will fail. This will be adjusted later based on the
3245 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04003246 */
James Smart2e0fef82007-06-17 19:56:36 -05003247 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04003248 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05003249 shost->transportt = lpfc_vport_transport_template;
3250 vport->port_type = LPFC_NPIV_PORT;
3251 } else {
3252 shost->transportt = lpfc_transport_template;
3253 vport->port_type = LPFC_PHYSICAL_PORT;
3254 }
James Smart47a86172007-04-25 09:53:22 -04003255
James Smart2e0fef82007-06-17 19:56:36 -05003256 /* Initialize all internally managed lists. */
3257 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04003258 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05003259 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04003260
James Smart2e0fef82007-06-17 19:56:36 -05003261 init_timer(&vport->fc_disctmo);
3262 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05003263 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04003264
James Smart2e0fef82007-06-17 19:56:36 -05003265 init_timer(&vport->fc_fdmitmo);
3266 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
James Smart92d7f7b2007-06-17 19:56:38 -05003267 vport->fc_fdmitmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04003268
James Smart2e0fef82007-06-17 19:56:36 -05003269 init_timer(&vport->els_tmofunc);
3270 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05003271 vport->els_tmofunc.data = (unsigned long)vport;
James Smart92494142011-02-16 12:39:44 -05003272
3273 init_timer(&vport->delayed_disc_tmo);
3274 vport->delayed_disc_tmo.function = lpfc_delayed_disc_tmo;
3275 vport->delayed_disc_tmo.data = (unsigned long)vport;
3276
James Bottomleyd139b9b2009-11-05 13:33:12 -06003277 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05003278 if (error)
3279 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04003280
James Smart549e55c2007-08-02 11:09:51 -04003281 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003282 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04003283 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003284 return vport;
James Smart47a86172007-04-25 09:53:22 -04003285
James Smart2e0fef82007-06-17 19:56:36 -05003286out_put_shost:
3287 scsi_host_put(shost);
3288out:
3289 return NULL;
James Smart47a86172007-04-25 09:53:22 -04003290}
3291
James Smarte59058c2008-08-24 21:49:00 -04003292/**
James Smart3621a712009-04-06 18:47:14 -04003293 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04003294 * @vport: pointer to an lpfc virtual N_Port data structure.
3295 *
3296 * This routine destroys a FC port from the upper layer protocol. All the
3297 * resources associated with the port are released.
3298 **/
James Smart2e0fef82007-06-17 19:56:36 -05003299void
3300destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04003301{
James Smart92d7f7b2007-06-17 19:56:38 -05003302 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3303 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003304
James Smart858c9f62007-06-17 19:56:39 -05003305 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05003306 fc_remove_host(shost);
3307 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04003308
James Smart92d7f7b2007-06-17 19:56:38 -05003309 spin_lock_irq(&phba->hbalock);
3310 list_del_init(&vport->listentry);
3311 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04003312
James Smart92d7f7b2007-06-17 19:56:38 -05003313 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04003314 return;
James Smart47a86172007-04-25 09:53:22 -04003315}
3316
James Smarte59058c2008-08-24 21:49:00 -04003317/**
James Smart3621a712009-04-06 18:47:14 -04003318 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04003319 *
3320 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
3321 * uses the kernel idr facility to perform the task.
3322 *
3323 * Return codes:
3324 * instance - a unique integer ID allocated as the new instance.
3325 * -1 - lpfc get instance failed.
3326 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003327int
3328lpfc_get_instance(void)
3329{
Tejun Heoab516032013-02-27 17:04:44 -08003330 int ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003331
Tejun Heoab516032013-02-27 17:04:44 -08003332 ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
3333 return ret < 0 ? -1 : ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003334}
3335
James Smarte59058c2008-08-24 21:49:00 -04003336/**
James Smart3621a712009-04-06 18:47:14 -04003337 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04003338 * @shost: pointer to SCSI host data structure.
3339 * @time: elapsed time of the scan in jiffies.
3340 *
3341 * This routine is called by the SCSI layer with a SCSI host to determine
3342 * whether the scan host is finished.
3343 *
3344 * Note: there is no scan_start function as adapter initialization will have
3345 * asynchronously kicked off the link initialization.
3346 *
3347 * Return codes
3348 * 0 - SCSI host scan is not over yet.
3349 * 1 - SCSI host scan is over.
3350 **/
James Smart47a86172007-04-25 09:53:22 -04003351int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
3352{
James Smart2e0fef82007-06-17 19:56:36 -05003353 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3354 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05003355 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04003356
James Smart858c9f62007-06-17 19:56:39 -05003357 spin_lock_irq(shost->host_lock);
3358
James Smart51ef4c22007-08-02 11:10:31 -04003359 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05003360 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003361 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05003362 }
James Smart256ec0d2013-04-17 20:14:58 -04003363 if (time >= msecs_to_jiffies(30 * 1000)) {
James Smart2e0fef82007-06-17 19:56:36 -05003364 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003365 "0461 Scanning longer than 30 "
3366 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003367 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003368 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003369 }
James Smart256ec0d2013-04-17 20:14:58 -04003370 if (time >= msecs_to_jiffies(15 * 1000) &&
3371 phba->link_state <= LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -05003372 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003373 "0465 Link down longer than 15 "
3374 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003375 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05003376 goto finished;
James Smart47a86172007-04-25 09:53:22 -04003377 }
3378
James Smart2e0fef82007-06-17 19:56:36 -05003379 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05003380 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003381 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05003382 goto finished;
James Smart256ec0d2013-04-17 20:14:58 -04003383 if (vport->fc_map_cnt == 0 && time < msecs_to_jiffies(2 * 1000))
James Smart858c9f62007-06-17 19:56:39 -05003384 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003385 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05003386 goto finished;
3387
3388 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003389
3390finished:
James Smart858c9f62007-06-17 19:56:39 -05003391 spin_unlock_irq(shost->host_lock);
3392 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05003393}
3394
James Smarte59058c2008-08-24 21:49:00 -04003395/**
James Smart3621a712009-04-06 18:47:14 -04003396 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04003397 * @shost: pointer to SCSI host data structure.
3398 *
3399 * This routine initializes a given SCSI host attributes on a FC port. The
3400 * SCSI host can be either on top of a physical port or a virtual port.
3401 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003402void lpfc_host_attrib_init(struct Scsi_Host *shost)
3403{
3404 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3405 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003406 /*
James Smart2e0fef82007-06-17 19:56:36 -05003407 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04003408 */
3409
James Smart2e0fef82007-06-17 19:56:36 -05003410 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
3411 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04003412 fc_host_supported_classes(shost) = FC_COS_CLASS3;
3413
3414 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003415 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003416 fc_host_supported_fc4s(shost)[2] = 1;
3417 fc_host_supported_fc4s(shost)[7] = 1;
3418
James Smart92d7f7b2007-06-17 19:56:38 -05003419 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
3420 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04003421
3422 fc_host_supported_speeds(shost) = 0;
James Smart88a2cfb2011-07-22 18:36:33 -04003423 if (phba->lmt & LMT_16Gb)
3424 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
James Smart47a86172007-04-25 09:53:22 -04003425 if (phba->lmt & LMT_10Gb)
3426 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04003427 if (phba->lmt & LMT_8Gb)
3428 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04003429 if (phba->lmt & LMT_4Gb)
3430 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
3431 if (phba->lmt & LMT_2Gb)
3432 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
3433 if (phba->lmt & LMT_1Gb)
3434 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
3435
3436 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05003437 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
3438 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04003439
Mike Christie0af5d702010-09-15 16:52:31 -05003440 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
3441
James Smart47a86172007-04-25 09:53:22 -04003442 /* This value is also unchanging */
3443 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003444 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003445 fc_host_active_fc4s(shost)[2] = 1;
3446 fc_host_active_fc4s(shost)[7] = 1;
3447
James Smart92d7f7b2007-06-17 19:56:38 -05003448 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04003449 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04003450 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04003451 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04003452}
dea31012005-04-17 16:05:31 -05003453
James Smarte59058c2008-08-24 21:49:00 -04003454/**
James Smartda0436e2009-05-22 14:51:39 -04003455 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04003456 * @phba: pointer to lpfc hba data structure.
3457 *
James Smartda0436e2009-05-22 14:51:39 -04003458 * This routine is invoked to stop an SLI3 device port, it stops the device
3459 * from generating interrupts and stops the device driver's timers for the
3460 * device.
James Smarte59058c2008-08-24 21:49:00 -04003461 **/
James Smartdb2378e2008-02-08 18:49:51 -05003462static void
James Smartda0436e2009-05-22 14:51:39 -04003463lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05003464{
James Smartda0436e2009-05-22 14:51:39 -04003465 /* Clear all interrupt enable conditions */
3466 writel(0, phba->HCregaddr);
3467 readl(phba->HCregaddr); /* flush */
3468 /* Clear all pending interrupts */
3469 writel(0xffffffff, phba->HAregaddr);
3470 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04003471
James Smartda0436e2009-05-22 14:51:39 -04003472 /* Reset some HBA SLI setup states */
3473 lpfc_stop_hba_timers(phba);
3474 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05003475}
3476
James Smarte59058c2008-08-24 21:49:00 -04003477/**
James Smartda0436e2009-05-22 14:51:39 -04003478 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05003479 * @phba: pointer to lpfc hba data structure.
3480 *
James Smartda0436e2009-05-22 14:51:39 -04003481 * This routine is invoked to stop an SLI4 device port, it stops the device
3482 * from generating interrupts and stops the device driver's timers for the
3483 * device.
3484 **/
3485static void
3486lpfc_stop_port_s4(struct lpfc_hba *phba)
3487{
3488 /* Reset some HBA SLI4 setup states */
3489 lpfc_stop_hba_timers(phba);
3490 phba->pport->work_port_events = 0;
3491 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04003492}
3493
3494/**
3495 * lpfc_stop_port - Wrapper function for stopping hba port
3496 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05003497 *
James Smartda0436e2009-05-22 14:51:39 -04003498 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
3499 * the API jump table function pointer from the lpfc_hba struct.
3500 **/
3501void
3502lpfc_stop_port(struct lpfc_hba *phba)
3503{
3504 phba->lpfc_stop_port(phba);
3505}
3506
3507/**
James Smartecfd03c2010-02-12 14:41:27 -05003508 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
3509 * @phba: Pointer to hba for which this call is being executed.
3510 *
3511 * This routine starts the timer waiting for the FCF rediscovery to complete.
3512 **/
3513void
3514lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
3515{
3516 unsigned long fcf_redisc_wait_tmo =
3517 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
3518 /* Start fcf rediscovery wait period timer */
3519 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
3520 spin_lock_irq(&phba->hbalock);
3521 /* Allow action to new fcf asynchronous event */
3522 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
3523 /* Mark the FCF rediscovery pending state */
3524 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
3525 spin_unlock_irq(&phba->hbalock);
3526}
3527
3528/**
3529 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
3530 * @ptr: Map to lpfc_hba data structure pointer.
3531 *
3532 * This routine is invoked when waiting for FCF table rediscover has been
3533 * timed out. If new FCF record(s) has (have) been discovered during the
3534 * wait period, a new FCF event shall be added to the FCOE async event
3535 * list, and then worker thread shall be waked up for processing from the
3536 * worker thread context.
3537 **/
3538void
3539lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
3540{
3541 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
3542
3543 /* Don't send FCF rediscovery event if timer cancelled */
3544 spin_lock_irq(&phba->hbalock);
3545 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
3546 spin_unlock_irq(&phba->hbalock);
3547 return;
3548 }
3549 /* Clear FCF rediscovery timer pending flag */
3550 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
3551 /* FCF rediscovery event to worker thread */
3552 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
3553 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003554 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04003555 "2776 FCF rediscover quiescent timer expired\n");
James Smartecfd03c2010-02-12 14:41:27 -05003556 /* wake up worker thread */
3557 lpfc_worker_wake_up(phba);
3558}
3559
3560/**
James Smartda0436e2009-05-22 14:51:39 -04003561 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
3562 * @phba: pointer to lpfc hba data structure.
3563 * @acqe_link: pointer to the async link completion queue entry.
3564 *
3565 * This routine is to parse the SLI4 link-attention link fault code and
3566 * translate it into the base driver's read link attention mailbox command
3567 * status.
3568 *
3569 * Return: Link-attention status in terms of base driver's coding.
3570 **/
3571static uint16_t
3572lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
3573 struct lpfc_acqe_link *acqe_link)
3574{
3575 uint16_t latt_fault;
3576
3577 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
3578 case LPFC_ASYNC_LINK_FAULT_NONE:
3579 case LPFC_ASYNC_LINK_FAULT_LOCAL:
3580 case LPFC_ASYNC_LINK_FAULT_REMOTE:
3581 latt_fault = 0;
3582 break;
3583 default:
3584 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3585 "0398 Invalid link fault code: x%x\n",
3586 bf_get(lpfc_acqe_link_fault, acqe_link));
3587 latt_fault = MBXERR_ERROR;
3588 break;
3589 }
3590 return latt_fault;
3591}
3592
3593/**
3594 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
3595 * @phba: pointer to lpfc hba data structure.
3596 * @acqe_link: pointer to the async link completion queue entry.
3597 *
3598 * This routine is to parse the SLI4 link attention type and translate it
3599 * into the base driver's link attention type coding.
3600 *
3601 * Return: Link attention type in terms of base driver's coding.
3602 **/
3603static uint8_t
3604lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
3605 struct lpfc_acqe_link *acqe_link)
3606{
3607 uint8_t att_type;
3608
3609 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
3610 case LPFC_ASYNC_LINK_STATUS_DOWN:
3611 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
James Smart76a95d72010-11-20 23:11:48 -05003612 att_type = LPFC_ATT_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04003613 break;
3614 case LPFC_ASYNC_LINK_STATUS_UP:
3615 /* Ignore physical link up events - wait for logical link up */
James Smart76a95d72010-11-20 23:11:48 -05003616 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003617 break;
3618 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
James Smart76a95d72010-11-20 23:11:48 -05003619 att_type = LPFC_ATT_LINK_UP;
James Smartda0436e2009-05-22 14:51:39 -04003620 break;
3621 default:
3622 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3623 "0399 Invalid link attention type: x%x\n",
3624 bf_get(lpfc_acqe_link_status, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003625 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003626 break;
3627 }
3628 return att_type;
3629}
3630
3631/**
3632 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
3633 * @phba: pointer to lpfc hba data structure.
3634 * @acqe_link: pointer to the async link completion queue entry.
3635 *
3636 * This routine is to parse the SLI4 link-attention link speed and translate
3637 * it into the base driver's link-attention link speed coding.
3638 *
3639 * Return: Link-attention link speed in terms of base driver's coding.
3640 **/
3641static uint8_t
3642lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
3643 struct lpfc_acqe_link *acqe_link)
3644{
3645 uint8_t link_speed;
3646
3647 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
3648 case LPFC_ASYNC_LINK_SPEED_ZERO:
James Smartda0436e2009-05-22 14:51:39 -04003649 case LPFC_ASYNC_LINK_SPEED_10MBPS:
James Smartda0436e2009-05-22 14:51:39 -04003650 case LPFC_ASYNC_LINK_SPEED_100MBPS:
James Smart76a95d72010-11-20 23:11:48 -05003651 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003652 break;
3653 case LPFC_ASYNC_LINK_SPEED_1GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003654 link_speed = LPFC_LINK_SPEED_1GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003655 break;
3656 case LPFC_ASYNC_LINK_SPEED_10GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003657 link_speed = LPFC_LINK_SPEED_10GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003658 break;
3659 default:
3660 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3661 "0483 Invalid link-attention link speed: x%x\n",
3662 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003663 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003664 break;
3665 }
3666 return link_speed;
3667}
3668
3669/**
James Smart8b68cd52012-09-29 11:32:37 -04003670 * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
3671 * @phba: pointer to lpfc hba data structure.
3672 *
3673 * This routine is to get an SLI3 FC port's link speed in Mbps.
3674 *
3675 * Return: link speed in terms of Mbps.
3676 **/
3677uint32_t
3678lpfc_sli_port_speed_get(struct lpfc_hba *phba)
3679{
3680 uint32_t link_speed;
3681
3682 if (!lpfc_is_link_up(phba))
3683 return 0;
3684
3685 switch (phba->fc_linkspeed) {
3686 case LPFC_LINK_SPEED_1GHZ:
3687 link_speed = 1000;
3688 break;
3689 case LPFC_LINK_SPEED_2GHZ:
3690 link_speed = 2000;
3691 break;
3692 case LPFC_LINK_SPEED_4GHZ:
3693 link_speed = 4000;
3694 break;
3695 case LPFC_LINK_SPEED_8GHZ:
3696 link_speed = 8000;
3697 break;
3698 case LPFC_LINK_SPEED_10GHZ:
3699 link_speed = 10000;
3700 break;
3701 case LPFC_LINK_SPEED_16GHZ:
3702 link_speed = 16000;
3703 break;
3704 default:
3705 link_speed = 0;
3706 }
3707 return link_speed;
3708}
3709
3710/**
3711 * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
3712 * @phba: pointer to lpfc hba data structure.
3713 * @evt_code: asynchronous event code.
3714 * @speed_code: asynchronous event link speed code.
3715 *
3716 * This routine is to parse the giving SLI4 async event link speed code into
3717 * value of Mbps for the link speed.
3718 *
3719 * Return: link speed in terms of Mbps.
3720 **/
3721static uint32_t
3722lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
3723 uint8_t speed_code)
3724{
3725 uint32_t port_speed;
3726
3727 switch (evt_code) {
3728 case LPFC_TRAILER_CODE_LINK:
3729 switch (speed_code) {
3730 case LPFC_EVT_CODE_LINK_NO_LINK:
3731 port_speed = 0;
3732 break;
3733 case LPFC_EVT_CODE_LINK_10_MBIT:
3734 port_speed = 10;
3735 break;
3736 case LPFC_EVT_CODE_LINK_100_MBIT:
3737 port_speed = 100;
3738 break;
3739 case LPFC_EVT_CODE_LINK_1_GBIT:
3740 port_speed = 1000;
3741 break;
3742 case LPFC_EVT_CODE_LINK_10_GBIT:
3743 port_speed = 10000;
3744 break;
3745 default:
3746 port_speed = 0;
3747 }
3748 break;
3749 case LPFC_TRAILER_CODE_FC:
3750 switch (speed_code) {
3751 case LPFC_EVT_CODE_FC_NO_LINK:
3752 port_speed = 0;
3753 break;
3754 case LPFC_EVT_CODE_FC_1_GBAUD:
3755 port_speed = 1000;
3756 break;
3757 case LPFC_EVT_CODE_FC_2_GBAUD:
3758 port_speed = 2000;
3759 break;
3760 case LPFC_EVT_CODE_FC_4_GBAUD:
3761 port_speed = 4000;
3762 break;
3763 case LPFC_EVT_CODE_FC_8_GBAUD:
3764 port_speed = 8000;
3765 break;
3766 case LPFC_EVT_CODE_FC_10_GBAUD:
3767 port_speed = 10000;
3768 break;
3769 case LPFC_EVT_CODE_FC_16_GBAUD:
3770 port_speed = 16000;
3771 break;
3772 default:
3773 port_speed = 0;
3774 }
3775 break;
3776 default:
3777 port_speed = 0;
3778 }
3779 return port_speed;
3780}
3781
3782/**
James Smart70f3c072010-12-15 17:57:33 -05003783 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
James Smartda0436e2009-05-22 14:51:39 -04003784 * @phba: pointer to lpfc hba data structure.
3785 * @acqe_link: pointer to the async link completion queue entry.
3786 *
James Smart70f3c072010-12-15 17:57:33 -05003787 * This routine is to handle the SLI4 asynchronous FCoE link event.
James Smartda0436e2009-05-22 14:51:39 -04003788 **/
3789static void
3790lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3791 struct lpfc_acqe_link *acqe_link)
3792{
3793 struct lpfc_dmabuf *mp;
3794 LPFC_MBOXQ_t *pmb;
3795 MAILBOX_t *mb;
James Smart76a95d72010-11-20 23:11:48 -05003796 struct lpfc_mbx_read_top *la;
James Smartda0436e2009-05-22 14:51:39 -04003797 uint8_t att_type;
James Smart76a95d72010-11-20 23:11:48 -05003798 int rc;
James Smartda0436e2009-05-22 14:51:39 -04003799
3800 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05003801 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
James Smartda0436e2009-05-22 14:51:39 -04003802 return;
James Smart32b97932009-07-19 10:01:21 -04003803 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003804 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3805 if (!pmb) {
3806 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3807 "0395 The mboxq allocation failed\n");
3808 return;
3809 }
3810 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3811 if (!mp) {
3812 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3813 "0396 The lpfc_dmabuf allocation failed\n");
3814 goto out_free_pmb;
3815 }
3816 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3817 if (!mp->virt) {
3818 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3819 "0397 The mbuf allocation failed\n");
3820 goto out_free_dmabuf;
3821 }
3822
3823 /* Cleanup any outstanding ELS commands */
3824 lpfc_els_flush_all_cmd(phba);
3825
3826 /* Block ELS IOCBs until we have done process link event */
3827 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3828
3829 /* Update link event statistics */
3830 phba->sli.slistat.link_event++;
3831
James Smart76a95d72010-11-20 23:11:48 -05003832 /* Create lpfc_handle_latt mailbox command from link ACQE */
3833 lpfc_read_topology(phba, pmb, mp);
3834 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smartda0436e2009-05-22 14:51:39 -04003835 pmb->vport = phba->pport;
3836
James Smartda0436e2009-05-22 14:51:39 -04003837 /* Keep the link status for extra SLI4 state machine reference */
3838 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04003839 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
3840 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smartda0436e2009-05-22 14:51:39 -04003841 phba->sli4_hba.link_state.duplex =
3842 bf_get(lpfc_acqe_link_duplex, acqe_link);
3843 phba->sli4_hba.link_state.status =
3844 bf_get(lpfc_acqe_link_status, acqe_link);
James Smart70f3c072010-12-15 17:57:33 -05003845 phba->sli4_hba.link_state.type =
3846 bf_get(lpfc_acqe_link_type, acqe_link);
3847 phba->sli4_hba.link_state.number =
3848 bf_get(lpfc_acqe_link_number, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04003849 phba->sli4_hba.link_state.fault =
3850 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05003851 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04003852 bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
3853
James Smart70f3c072010-12-15 17:57:33 -05003854 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartc31098c2011-04-16 11:03:33 -04003855 "2900 Async FC/FCoE Link event - Speed:%dGBit "
3856 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
3857 "Logical speed:%dMbps Fault:%d\n",
James Smart70f3c072010-12-15 17:57:33 -05003858 phba->sli4_hba.link_state.speed,
3859 phba->sli4_hba.link_state.topology,
3860 phba->sli4_hba.link_state.status,
3861 phba->sli4_hba.link_state.type,
3862 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04003863 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05003864 phba->sli4_hba.link_state.fault);
James Smart76a95d72010-11-20 23:11:48 -05003865 /*
3866 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
3867 * topology info. Note: Optional for non FC-AL ports.
3868 */
3869 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3870 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3871 if (rc == MBX_NOT_FINISHED)
3872 goto out_free_dmabuf;
3873 return;
3874 }
3875 /*
3876 * For FCoE Mode: fill in all the topology information we need and call
3877 * the READ_TOPOLOGY completion routine to continue without actually
3878 * sending the READ_TOPOLOGY mailbox command to the port.
3879 */
3880 /* Parse and translate status field */
3881 mb = &pmb->u.mb;
3882 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
3883
3884 /* Parse and translate link attention fields */
3885 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
3886 la->eventTag = acqe_link->event_tag;
3887 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
3888 bf_set(lpfc_mbx_read_top_link_spd, la,
3889 lpfc_sli4_parse_latt_link_speed(phba, acqe_link));
3890
3891 /* Fake the the following irrelvant fields */
3892 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
3893 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
3894 bf_set(lpfc_mbx_read_top_il, la, 0);
3895 bf_set(lpfc_mbx_read_top_pb, la, 0);
3896 bf_set(lpfc_mbx_read_top_fa, la, 0);
3897 bf_set(lpfc_mbx_read_top_mm, la, 0);
James Smartda0436e2009-05-22 14:51:39 -04003898
3899 /* Invoke the lpfc_handle_latt mailbox command callback function */
James Smart76a95d72010-11-20 23:11:48 -05003900 lpfc_mbx_cmpl_read_topology(phba, pmb);
James Smartda0436e2009-05-22 14:51:39 -04003901
3902 return;
3903
3904out_free_dmabuf:
3905 kfree(mp);
3906out_free_pmb:
3907 mempool_free(pmb, phba->mbox_mem_pool);
3908}
3909
3910/**
James Smart70f3c072010-12-15 17:57:33 -05003911 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
3912 * @phba: pointer to lpfc hba data structure.
3913 * @acqe_fc: pointer to the async fc completion queue entry.
3914 *
3915 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
3916 * that the event was received and then issue a read_topology mailbox command so
3917 * that the rest of the driver will treat it the same as SLI3.
3918 **/
3919static void
3920lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
3921{
3922 struct lpfc_dmabuf *mp;
3923 LPFC_MBOXQ_t *pmb;
3924 int rc;
3925
3926 if (bf_get(lpfc_trailer_type, acqe_fc) !=
3927 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
3928 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3929 "2895 Non FC link Event detected.(%d)\n",
3930 bf_get(lpfc_trailer_type, acqe_fc));
3931 return;
3932 }
3933 /* Keep the link status for extra SLI4 state machine reference */
3934 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04003935 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
3936 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
James Smart70f3c072010-12-15 17:57:33 -05003937 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
3938 phba->sli4_hba.link_state.topology =
3939 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
3940 phba->sli4_hba.link_state.status =
3941 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
3942 phba->sli4_hba.link_state.type =
3943 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
3944 phba->sli4_hba.link_state.number =
3945 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
3946 phba->sli4_hba.link_state.fault =
3947 bf_get(lpfc_acqe_link_fault, acqe_fc);
3948 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04003949 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
James Smart70f3c072010-12-15 17:57:33 -05003950 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3951 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
3952 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
3953 "%dMbps Fault:%d\n",
3954 phba->sli4_hba.link_state.speed,
3955 phba->sli4_hba.link_state.topology,
3956 phba->sli4_hba.link_state.status,
3957 phba->sli4_hba.link_state.type,
3958 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04003959 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05003960 phba->sli4_hba.link_state.fault);
3961 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3962 if (!pmb) {
3963 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3964 "2897 The mboxq allocation failed\n");
3965 return;
3966 }
3967 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3968 if (!mp) {
3969 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3970 "2898 The lpfc_dmabuf allocation failed\n");
3971 goto out_free_pmb;
3972 }
3973 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3974 if (!mp->virt) {
3975 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3976 "2899 The mbuf allocation failed\n");
3977 goto out_free_dmabuf;
3978 }
3979
3980 /* Cleanup any outstanding ELS commands */
3981 lpfc_els_flush_all_cmd(phba);
3982
3983 /* Block ELS IOCBs until we have done process link event */
3984 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3985
3986 /* Update link event statistics */
3987 phba->sli.slistat.link_event++;
3988
3989 /* Create lpfc_handle_latt mailbox command from link ACQE */
3990 lpfc_read_topology(phba, pmb, mp);
3991 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
3992 pmb->vport = phba->pport;
3993
3994 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3995 if (rc == MBX_NOT_FINISHED)
3996 goto out_free_dmabuf;
3997 return;
3998
3999out_free_dmabuf:
4000 kfree(mp);
4001out_free_pmb:
4002 mempool_free(pmb, phba->mbox_mem_pool);
4003}
4004
4005/**
4006 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
4007 * @phba: pointer to lpfc hba data structure.
4008 * @acqe_fc: pointer to the async SLI completion queue entry.
4009 *
4010 * This routine is to handle the SLI4 asynchronous SLI events.
4011 **/
4012static void
4013lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
4014{
James Smart4b8bae02012-06-12 13:55:07 -04004015 char port_name;
James Smart8c1312e2012-10-31 14:45:09 -04004016 char message[128];
James Smart4b8bae02012-06-12 13:55:07 -04004017 uint8_t status;
4018 struct lpfc_acqe_misconfigured_event *misconfigured;
4019
4020 /* special case misconfigured event as it contains data for all ports */
4021 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
4022 LPFC_SLI_INTF_IF_TYPE_2) ||
4023 (bf_get(lpfc_trailer_type, acqe_sli) !=
4024 LPFC_SLI_EVENT_TYPE_MISCONFIGURED)) {
4025 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4026 "2901 Async SLI event - Event Data1:x%08x Event Data2:"
4027 "x%08x SLI Event Type:%d\n",
4028 acqe_sli->event_data1, acqe_sli->event_data2,
4029 bf_get(lpfc_trailer_type, acqe_sli));
4030 return;
4031 }
4032
4033 port_name = phba->Port[0];
4034 if (port_name == 0x00)
4035 port_name = '?'; /* get port name is empty */
4036
4037 misconfigured = (struct lpfc_acqe_misconfigured_event *)
4038 &acqe_sli->event_data1;
4039
4040 /* fetch the status for this port */
4041 switch (phba->sli4_hba.lnk_info.lnk_no) {
4042 case LPFC_LINK_NUMBER_0:
4043 status = bf_get(lpfc_sli_misconfigured_port0,
4044 &misconfigured->theEvent);
4045 break;
4046 case LPFC_LINK_NUMBER_1:
4047 status = bf_get(lpfc_sli_misconfigured_port1,
4048 &misconfigured->theEvent);
4049 break;
4050 case LPFC_LINK_NUMBER_2:
4051 status = bf_get(lpfc_sli_misconfigured_port2,
4052 &misconfigured->theEvent);
4053 break;
4054 case LPFC_LINK_NUMBER_3:
4055 status = bf_get(lpfc_sli_misconfigured_port3,
4056 &misconfigured->theEvent);
4057 break;
4058 default:
4059 status = ~LPFC_SLI_EVENT_STATUS_VALID;
4060 break;
4061 }
4062
4063 switch (status) {
4064 case LPFC_SLI_EVENT_STATUS_VALID:
4065 return; /* no message if the sfp is okay */
4066 case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
James Smart292098b2012-09-29 11:31:41 -04004067 sprintf(message, "Optics faulted/incorrectly installed/not " \
4068 "installed - Reseat optics, if issue not "
4069 "resolved, replace.");
James Smart4b8bae02012-06-12 13:55:07 -04004070 break;
4071 case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
4072 sprintf(message,
James Smart292098b2012-09-29 11:31:41 -04004073 "Optics of two types installed - Remove one optic or " \
4074 "install matching pair of optics.");
James Smart4b8bae02012-06-12 13:55:07 -04004075 break;
4076 case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
James Smart292098b2012-09-29 11:31:41 -04004077 sprintf(message, "Incompatible optics - Replace with " \
4078 "compatible optics for card to function.");
James Smart4b8bae02012-06-12 13:55:07 -04004079 break;
4080 default:
4081 /* firmware is reporting a status we don't know about */
4082 sprintf(message, "Unknown event status x%02x", status);
4083 break;
4084 }
4085
4086 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4087 "3176 Misconfigured Physical Port - "
4088 "Port Name %c %s\n", port_name, message);
James Smart70f3c072010-12-15 17:57:33 -05004089}
4090
4091/**
James Smartfc2b9892010-02-26 14:15:29 -05004092 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
4093 * @vport: pointer to vport data structure.
4094 *
4095 * This routine is to perform Clear Virtual Link (CVL) on a vport in
4096 * response to a CVL event.
4097 *
4098 * Return the pointer to the ndlp with the vport if successful, otherwise
4099 * return NULL.
4100 **/
4101static struct lpfc_nodelist *
4102lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
4103{
4104 struct lpfc_nodelist *ndlp;
4105 struct Scsi_Host *shost;
4106 struct lpfc_hba *phba;
4107
4108 if (!vport)
4109 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05004110 phba = vport->phba;
4111 if (!phba)
4112 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04004113 ndlp = lpfc_findnode_did(vport, Fabric_DID);
4114 if (!ndlp) {
4115 /* Cannot find existing Fabric ndlp, so allocate a new one */
4116 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
4117 if (!ndlp)
4118 return 0;
4119 lpfc_nlp_init(vport, ndlp, Fabric_DID);
4120 /* Set the node type */
4121 ndlp->nlp_type |= NLP_FABRIC;
4122 /* Put ndlp onto node list */
4123 lpfc_enqueue_node(vport, ndlp);
4124 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4125 /* re-setup ndlp without removing from node list */
4126 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
4127 if (!ndlp)
4128 return 0;
4129 }
James Smart63e801c2010-11-20 23:14:19 -05004130 if ((phba->pport->port_state < LPFC_FLOGI) &&
4131 (phba->pport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05004132 return NULL;
4133 /* If virtual link is not yet instantiated ignore CVL */
James Smart63e801c2010-11-20 23:14:19 -05004134 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
4135 && (vport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05004136 return NULL;
4137 shost = lpfc_shost_from_vport(vport);
4138 if (!shost)
4139 return NULL;
4140 lpfc_linkdown_port(vport);
4141 lpfc_cleanup_pending_mbox(vport);
4142 spin_lock_irq(shost->host_lock);
4143 vport->fc_flag |= FC_VPORT_CVL_RCVD;
4144 spin_unlock_irq(shost->host_lock);
4145
4146 return ndlp;
4147}
4148
4149/**
4150 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
4151 * @vport: pointer to lpfc hba data structure.
4152 *
4153 * This routine is to perform Clear Virtual Link (CVL) on all vports in
4154 * response to a FCF dead event.
4155 **/
4156static void
4157lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
4158{
4159 struct lpfc_vport **vports;
4160 int i;
4161
4162 vports = lpfc_create_vport_work_array(phba);
4163 if (vports)
4164 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
4165 lpfc_sli4_perform_vport_cvl(vports[i]);
4166 lpfc_destroy_vport_work_array(phba, vports);
4167}
4168
4169/**
James Smart76a95d72010-11-20 23:11:48 -05004170 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
James Smartda0436e2009-05-22 14:51:39 -04004171 * @phba: pointer to lpfc hba data structure.
4172 * @acqe_link: pointer to the async fcoe completion queue entry.
4173 *
4174 * This routine is to handle the SLI4 asynchronous fcoe event.
4175 **/
4176static void
James Smart76a95d72010-11-20 23:11:48 -05004177lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
James Smart70f3c072010-12-15 17:57:33 -05004178 struct lpfc_acqe_fip *acqe_fip)
James Smartda0436e2009-05-22 14:51:39 -04004179{
James Smart70f3c072010-12-15 17:57:33 -05004180 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004181 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04004182 struct lpfc_vport *vport;
4183 struct lpfc_nodelist *ndlp;
4184 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05004185 int active_vlink_present;
4186 struct lpfc_vport **vports;
4187 int i;
James Smartda0436e2009-05-22 14:51:39 -04004188
James Smart70f3c072010-12-15 17:57:33 -05004189 phba->fc_eventTag = acqe_fip->event_tag;
4190 phba->fcoe_eventtag = acqe_fip->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004191 switch (event_type) {
James Smart70f3c072010-12-15 17:57:33 -05004192 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
4193 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
4194 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
James Smart999d8132010-03-15 11:24:56 -04004195 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4196 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004197 "2546 New FCF event, evt_tag:x%x, "
4198 "index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004199 acqe_fip->event_tag,
4200 acqe_fip->index);
James Smart999d8132010-03-15 11:24:56 -04004201 else
4202 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
4203 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004204 "2788 FCF param modified event, "
4205 "evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004206 acqe_fip->event_tag,
4207 acqe_fip->index);
James Smart38b92ef2010-08-04 16:11:39 -04004208 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004209 /*
4210 * During period of FCF discovery, read the FCF
4211 * table record indexed by the event to update
James Smarta93ff372010-10-22 11:06:08 -04004212 * FCF roundrobin failover eligible FCF bmask.
James Smart0c9ab6f2010-02-26 14:15:57 -05004213 */
4214 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4215 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004216 "2779 Read FCF (x%x) for updating "
4217 "roundrobin FCF failover bmask\n",
James Smart70f3c072010-12-15 17:57:33 -05004218 acqe_fip->index);
4219 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004220 }
James Smart38b92ef2010-08-04 16:11:39 -04004221
4222 /* If the FCF discovery is in progress, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04004223 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04004224 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart38b92ef2010-08-04 16:11:39 -04004225 spin_unlock_irq(&phba->hbalock);
4226 break;
4227 }
4228 /* If fast FCF failover rescan event is pending, do nothing */
4229 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
4230 spin_unlock_irq(&phba->hbalock);
4231 break;
4232 }
4233
James Smartc2b97122013-05-31 17:05:36 -04004234 /* If the FCF has been in discovered state, do nothing. */
4235 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
James Smart3804dc82010-07-14 15:31:37 -04004236 spin_unlock_irq(&phba->hbalock);
4237 break;
4238 }
4239 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04004240
James Smart0c9ab6f2010-02-26 14:15:57 -05004241 /* Otherwise, scan the entire FCF table and re-discover SAN */
4242 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004243 "2770 Start FCF table scan per async FCF "
4244 "event, evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004245 acqe_fip->event_tag, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004246 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
4247 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04004248 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05004249 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4250 "2547 Issue FCF scan read FCF mailbox "
James Smarta93ff372010-10-22 11:06:08 -04004251 "command failed (x%x)\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004252 break;
4253
James Smart70f3c072010-12-15 17:57:33 -05004254 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
James Smartda0436e2009-05-22 14:51:39 -04004255 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04004256 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004257 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
4258 acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04004259 break;
4260
James Smart70f3c072010-12-15 17:57:33 -05004261 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
James Smart80c17842012-03-01 22:35:45 -05004262 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05004263 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004264 "2549 FCF (x%x) disconnected from network, "
James Smart70f3c072010-12-15 17:57:33 -05004265 "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart38b92ef2010-08-04 16:11:39 -04004266 /*
4267 * If we are in the middle of FCF failover process, clear
4268 * the corresponding FCF bit in the roundrobin bitmap.
James Smartda0436e2009-05-22 14:51:39 -04004269 */
James Smartfc2b9892010-02-26 14:15:29 -05004270 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004271 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05004272 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004273 /* Update FLOGI FCF failover eligible FCF bmask */
James Smart70f3c072010-12-15 17:57:33 -05004274 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004275 break;
4276 }
James Smart38b92ef2010-08-04 16:11:39 -04004277 spin_unlock_irq(&phba->hbalock);
4278
4279 /* If the event is not for currently used fcf do nothing */
James Smart70f3c072010-12-15 17:57:33 -05004280 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
James Smart38b92ef2010-08-04 16:11:39 -04004281 break;
4282
4283 /*
4284 * Otherwise, request the port to rediscover the entire FCF
4285 * table for a fast recovery from case that the current FCF
4286 * is no longer valid as we are not in the middle of FCF
4287 * failover process already.
4288 */
James Smartc2b97122013-05-31 17:05:36 -04004289 spin_lock_irq(&phba->hbalock);
4290 /* Mark the fast failover process in progress */
4291 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
4292 spin_unlock_irq(&phba->hbalock);
4293
4294 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
4295 "2771 Start FCF fast failover process due to "
4296 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
4297 "\n", acqe_fip->event_tag, acqe_fip->index);
4298 rc = lpfc_sli4_redisc_fcf_table(phba);
4299 if (rc) {
4300 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4301 LOG_DISCOVERY,
4302 "2772 Issue FCF rediscover mabilbox "
4303 "command failed, fail through to FCF "
4304 "dead event\n");
4305 spin_lock_irq(&phba->hbalock);
4306 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
4307 spin_unlock_irq(&phba->hbalock);
4308 /*
4309 * Last resort will fail over by treating this
4310 * as a link down to FCF registration.
4311 */
4312 lpfc_sli4_fcf_dead_failthrough(phba);
4313 } else {
4314 /* Reset FCF roundrobin bmask for new discovery */
4315 lpfc_sli4_clear_fcf_rr_bmask(phba);
4316 /*
4317 * Handling fast FCF failover to a DEAD FCF event is
4318 * considered equalivant to receiving CVL to all vports.
4319 */
4320 lpfc_sli4_perform_all_vport_cvl(phba);
4321 }
James Smartda0436e2009-05-22 14:51:39 -04004322 break;
James Smart70f3c072010-12-15 17:57:33 -05004323 case LPFC_FIP_EVENT_TYPE_CVL:
James Smart80c17842012-03-01 22:35:45 -05004324 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05004325 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6669f9b2009-10-02 15:16:45 -04004326 "2718 Clear Virtual Link Received for VPI 0x%x"
James Smart70f3c072010-12-15 17:57:33 -05004327 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart6d368e52011-05-24 11:44:12 -04004328
James Smart6669f9b2009-10-02 15:16:45 -04004329 vport = lpfc_find_vport_by_vpid(phba,
James Smart5248a742011-07-22 18:37:06 -04004330 acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004331 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004332 if (!ndlp)
4333 break;
James Smart695a8142010-01-26 23:08:03 -05004334 active_vlink_present = 0;
4335
4336 vports = lpfc_create_vport_work_array(phba);
4337 if (vports) {
4338 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
4339 i++) {
4340 if ((!(vports[i]->fc_flag &
4341 FC_VPORT_CVL_RCVD)) &&
4342 (vports[i]->port_state > LPFC_FDISC)) {
4343 active_vlink_present = 1;
4344 break;
4345 }
4346 }
4347 lpfc_destroy_vport_work_array(phba, vports);
4348 }
4349
4350 if (active_vlink_present) {
4351 /*
4352 * If there are other active VLinks present,
4353 * re-instantiate the Vlink using FDISC.
4354 */
James Smart256ec0d2013-04-17 20:14:58 -04004355 mod_timer(&ndlp->nlp_delayfunc,
4356 jiffies + msecs_to_jiffies(1000));
James Smartfc2b9892010-02-26 14:15:29 -05004357 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004358 spin_lock_irq(shost->host_lock);
4359 ndlp->nlp_flag |= NLP_DELAY_TMO;
4360 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05004361 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
4362 vport->port_state = LPFC_FDISC;
4363 } else {
James Smartecfd03c2010-02-12 14:41:27 -05004364 /*
4365 * Otherwise, we request port to rediscover
4366 * the entire FCF table for a fast recovery
4367 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05004368 * is no longer valid if we are not already
4369 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05004370 */
James Smartfc2b9892010-02-26 14:15:29 -05004371 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004372 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05004373 spin_unlock_irq(&phba->hbalock);
4374 break;
4375 }
4376 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05004377 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004378 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004379 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4380 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004381 "2773 Start FCF failover per CVL, "
James Smart70f3c072010-12-15 17:57:33 -05004382 "evt_tag:x%x\n", acqe_fip->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05004383 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05004384 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004385 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4386 LOG_DISCOVERY,
4387 "2774 Issue FCF rediscover "
4388 "mabilbox command failed, "
4389 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05004390 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004391 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004392 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05004393 /*
4394 * Last resort will be re-try on the
4395 * the current registered FCF entry.
4396 */
4397 lpfc_retry_pport_discovery(phba);
James Smart38b92ef2010-08-04 16:11:39 -04004398 } else
4399 /*
4400 * Reset FCF roundrobin bmask for new
4401 * discovery.
4402 */
James Smart7d791df2011-07-22 18:37:52 -04004403 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart6669f9b2009-10-02 15:16:45 -04004404 }
4405 break;
James Smartda0436e2009-05-22 14:51:39 -04004406 default:
4407 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4408 "0288 Unknown FCoE event type 0x%x event tag "
James Smart70f3c072010-12-15 17:57:33 -05004409 "0x%x\n", event_type, acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04004410 break;
4411 }
4412}
4413
4414/**
4415 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
4416 * @phba: pointer to lpfc hba data structure.
4417 * @acqe_link: pointer to the async dcbx completion queue entry.
4418 *
4419 * This routine is to handle the SLI4 asynchronous dcbx event.
4420 **/
4421static void
4422lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
4423 struct lpfc_acqe_dcbx *acqe_dcbx)
4424{
James Smart4d9ab992009-10-02 15:16:39 -04004425 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004426 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4427 "0290 The SLI4 DCBX asynchronous event is not "
4428 "handled yet\n");
4429}
4430
4431/**
James Smartb19a0612010-04-06 14:48:51 -04004432 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
4433 * @phba: pointer to lpfc hba data structure.
4434 * @acqe_link: pointer to the async grp5 completion queue entry.
4435 *
4436 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
4437 * is an asynchronous notified of a logical link speed change. The Port
4438 * reports the logical link speed in units of 10Mbps.
4439 **/
4440static void
4441lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
4442 struct lpfc_acqe_grp5 *acqe_grp5)
4443{
4444 uint16_t prev_ll_spd;
4445
4446 phba->fc_eventTag = acqe_grp5->event_tag;
4447 phba->fcoe_eventtag = acqe_grp5->event_tag;
4448 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
4449 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04004450 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
James Smartb19a0612010-04-06 14:48:51 -04004451 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4452 "2789 GRP5 Async Event: Updating logical link speed "
James Smart8b68cd52012-09-29 11:32:37 -04004453 "from %dMbps to %dMbps\n", prev_ll_spd,
4454 phba->sli4_hba.link_state.logical_speed);
James Smartb19a0612010-04-06 14:48:51 -04004455}
4456
4457/**
James Smartda0436e2009-05-22 14:51:39 -04004458 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
4459 * @phba: pointer to lpfc hba data structure.
4460 *
4461 * This routine is invoked by the worker thread to process all the pending
4462 * SLI4 asynchronous events.
4463 **/
4464void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
4465{
4466 struct lpfc_cq_event *cq_event;
4467
4468 /* First, declare the async event has been handled */
4469 spin_lock_irq(&phba->hbalock);
4470 phba->hba_flag &= ~ASYNC_EVENT;
4471 spin_unlock_irq(&phba->hbalock);
4472 /* Now, handle all the async events */
4473 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
4474 /* Get the first event from the head of the event queue */
4475 spin_lock_irq(&phba->hbalock);
4476 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
4477 cq_event, struct lpfc_cq_event, list);
4478 spin_unlock_irq(&phba->hbalock);
4479 /* Process the asynchronous event */
4480 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
4481 case LPFC_TRAILER_CODE_LINK:
4482 lpfc_sli4_async_link_evt(phba,
4483 &cq_event->cqe.acqe_link);
4484 break;
4485 case LPFC_TRAILER_CODE_FCOE:
James Smart70f3c072010-12-15 17:57:33 -05004486 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004487 break;
4488 case LPFC_TRAILER_CODE_DCBX:
4489 lpfc_sli4_async_dcbx_evt(phba,
4490 &cq_event->cqe.acqe_dcbx);
4491 break;
James Smartb19a0612010-04-06 14:48:51 -04004492 case LPFC_TRAILER_CODE_GRP5:
4493 lpfc_sli4_async_grp5_evt(phba,
4494 &cq_event->cqe.acqe_grp5);
4495 break;
James Smart70f3c072010-12-15 17:57:33 -05004496 case LPFC_TRAILER_CODE_FC:
4497 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
4498 break;
4499 case LPFC_TRAILER_CODE_SLI:
4500 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
4501 break;
James Smartda0436e2009-05-22 14:51:39 -04004502 default:
4503 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4504 "1804 Invalid asynchrous event code: "
4505 "x%x\n", bf_get(lpfc_trailer_code,
4506 &cq_event->cqe.mcqe_cmpl));
4507 break;
4508 }
4509 /* Free the completion event processed to the free pool */
4510 lpfc_sli4_cq_event_release(phba, cq_event);
4511 }
4512}
4513
4514/**
James Smartecfd03c2010-02-12 14:41:27 -05004515 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
4516 * @phba: pointer to lpfc hba data structure.
4517 *
4518 * This routine is invoked by the worker thread to process FCF table
4519 * rediscovery pending completion event.
4520 **/
4521void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
4522{
4523 int rc;
4524
4525 spin_lock_irq(&phba->hbalock);
4526 /* Clear FCF rediscovery timeout event */
4527 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
4528 /* Clear driver fast failover FCF record flag */
4529 phba->fcf.failover_rec.flag = 0;
4530 /* Set state for FCF fast failover */
4531 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
4532 spin_unlock_irq(&phba->hbalock);
4533
4534 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05004535 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004536 "2777 Start post-quiescent FCF table scan\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05004537 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05004538 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05004539 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4540 "2747 Issue FCF scan read FCF mailbox "
4541 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05004542}
4543
4544/**
James Smartda0436e2009-05-22 14:51:39 -04004545 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
4546 * @phba: pointer to lpfc hba data structure.
4547 * @dev_grp: The HBA PCI-Device group number.
4548 *
4549 * This routine is invoked to set up the per HBA PCI-Device group function
4550 * API jump table entries.
4551 *
4552 * Return: 0 if success, otherwise -ENODEV
4553 **/
4554int
4555lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05004556{
4557 int rc;
4558
James Smartda0436e2009-05-22 14:51:39 -04004559 /* Set up lpfc PCI-device group */
4560 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05004561
James Smartda0436e2009-05-22 14:51:39 -04004562 /* The LPFC_PCI_DEV_OC uses SLI4 */
4563 if (dev_grp == LPFC_PCI_DEV_OC)
4564 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05004565
James Smartda0436e2009-05-22 14:51:39 -04004566 /* Set up device INIT API function jump table */
4567 rc = lpfc_init_api_table_setup(phba, dev_grp);
4568 if (rc)
4569 return -ENODEV;
4570 /* Set up SCSI API function jump table */
4571 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
4572 if (rc)
4573 return -ENODEV;
4574 /* Set up SLI API function jump table */
4575 rc = lpfc_sli_api_table_setup(phba, dev_grp);
4576 if (rc)
4577 return -ENODEV;
4578 /* Set up MBOX API function jump table */
4579 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
4580 if (rc)
4581 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004582
James Smartda0436e2009-05-22 14:51:39 -04004583 return 0;
James Smart5b75da22008-12-04 22:39:35 -05004584}
4585
4586/**
James Smart3621a712009-04-06 18:47:14 -04004587 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05004588 * @phba: pointer to lpfc hba data structure.
4589 * @intr_mode: active interrupt mode adopted.
4590 *
4591 * This routine it invoked to log the currently used active interrupt mode
4592 * to the device.
James Smart3772a992009-05-22 14:50:54 -04004593 **/
4594static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05004595{
4596 switch (intr_mode) {
4597 case 0:
4598 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4599 "0470 Enable INTx interrupt mode.\n");
4600 break;
4601 case 1:
4602 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4603 "0481 Enabled MSI interrupt mode.\n");
4604 break;
4605 case 2:
4606 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4607 "0480 Enabled MSI-X interrupt mode.\n");
4608 break;
4609 default:
4610 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4611 "0482 Illegal interrupt mode.\n");
4612 break;
4613 }
4614 return;
4615}
4616
James Smart5b75da22008-12-04 22:39:35 -05004617/**
James Smart3772a992009-05-22 14:50:54 -04004618 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05004619 * @phba: pointer to lpfc hba data structure.
4620 *
James Smart3772a992009-05-22 14:50:54 -04004621 * This routine is invoked to enable the PCI device that is common to all
4622 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004623 *
4624 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004625 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004626 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05004627 **/
James Smart3772a992009-05-22 14:50:54 -04004628static int
4629lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004630{
James Smart3772a992009-05-22 14:50:54 -04004631 struct pci_dev *pdev;
James Smart079b5c92011-08-21 21:48:49 -04004632 int bars = 0;
James Smart5b75da22008-12-04 22:39:35 -05004633
James Smart3772a992009-05-22 14:50:54 -04004634 /* Obtain PCI device reference */
4635 if (!phba->pcidev)
4636 goto out_error;
4637 else
4638 pdev = phba->pcidev;
4639 /* Select PCI BARs */
4640 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4641 /* Enable PCI device */
4642 if (pci_enable_device_mem(pdev))
4643 goto out_error;
4644 /* Request PCI resource for the device */
4645 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
4646 goto out_disable_device;
4647 /* Set up device as PCI master and save state for EEH */
4648 pci_set_master(pdev);
4649 pci_try_set_mwi(pdev);
4650 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05004651
James Smart05580562011-05-24 11:40:48 -04004652 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
Jon Mason453193e2013-09-11 15:38:13 -07004653 if (pci_is_pcie(pdev))
James Smart05580562011-05-24 11:40:48 -04004654 pdev->needs_freset = 1;
4655
James Smart3772a992009-05-22 14:50:54 -04004656 return 0;
James Smart5b75da22008-12-04 22:39:35 -05004657
James Smart3772a992009-05-22 14:50:54 -04004658out_disable_device:
4659 pci_disable_device(pdev);
4660out_error:
James Smart079b5c92011-08-21 21:48:49 -04004661 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4662 "1401 Failed to enable pci device, bars:x%x\n", bars);
James Smart3772a992009-05-22 14:50:54 -04004663 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004664}
4665
4666/**
James Smart3772a992009-05-22 14:50:54 -04004667 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05004668 * @phba: pointer to lpfc hba data structure.
4669 *
James Smart3772a992009-05-22 14:50:54 -04004670 * This routine is invoked to disable the PCI device that is common to all
4671 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004672 **/
4673static void
James Smart3772a992009-05-22 14:50:54 -04004674lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004675{
James Smart3772a992009-05-22 14:50:54 -04004676 struct pci_dev *pdev;
4677 int bars;
James Smart5b75da22008-12-04 22:39:35 -05004678
James Smart3772a992009-05-22 14:50:54 -04004679 /* Obtain PCI device reference */
4680 if (!phba->pcidev)
4681 return;
4682 else
4683 pdev = phba->pcidev;
4684 /* Select PCI BARs */
4685 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4686 /* Release PCI resource and disable PCI device */
4687 pci_release_selected_regions(pdev, bars);
4688 pci_disable_device(pdev);
James Smart5b75da22008-12-04 22:39:35 -05004689
4690 return;
4691}
4692
4693/**
James Smart3772a992009-05-22 14:50:54 -04004694 * lpfc_reset_hba - Reset a hba
4695 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04004696 *
James Smart3772a992009-05-22 14:50:54 -04004697 * This routine is invoked to reset a hba device. It brings the HBA
4698 * offline, performs a board restart, and then brings the board back
4699 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
4700 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04004701 **/
James Smart3772a992009-05-22 14:50:54 -04004702void
4703lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004704{
James Smart3772a992009-05-22 14:50:54 -04004705 /* If resets are disabled then set error state and return. */
4706 if (!phba->cfg_enable_hba_reset) {
4707 phba->link_state = LPFC_HBA_ERROR;
4708 return;
4709 }
James Smartee620212014-04-04 13:51:53 -04004710 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
4711 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
4712 else
4713 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart3772a992009-05-22 14:50:54 -04004714 lpfc_offline(phba);
4715 lpfc_sli_brdrestart(phba);
4716 lpfc_online(phba);
4717 lpfc_unblock_mgmt_io(phba);
4718}
dea31012005-04-17 16:05:31 -05004719
James Smart3772a992009-05-22 14:50:54 -04004720/**
James Smart0a96e972011-07-22 18:37:28 -04004721 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
4722 * @phba: pointer to lpfc hba data structure.
4723 *
4724 * This function enables the PCI SR-IOV virtual functions to a physical
4725 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4726 * enable the number of virtual functions to the physical function. As
4727 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4728 * API call does not considered as an error condition for most of the device.
4729 **/
4730uint16_t
4731lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
4732{
4733 struct pci_dev *pdev = phba->pcidev;
4734 uint16_t nr_virtfn;
4735 int pos;
4736
James Smart0a96e972011-07-22 18:37:28 -04004737 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
4738 if (pos == 0)
4739 return 0;
4740
4741 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
4742 return nr_virtfn;
4743}
4744
4745/**
James Smart912e3ac2011-05-24 11:42:11 -04004746 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
4747 * @phba: pointer to lpfc hba data structure.
4748 * @nr_vfn: number of virtual functions to be enabled.
4749 *
4750 * This function enables the PCI SR-IOV virtual functions to a physical
4751 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4752 * enable the number of virtual functions to the physical function. As
4753 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4754 * API call does not considered as an error condition for most of the device.
4755 **/
4756int
4757lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
4758{
4759 struct pci_dev *pdev = phba->pcidev;
James Smart0a96e972011-07-22 18:37:28 -04004760 uint16_t max_nr_vfn;
James Smart912e3ac2011-05-24 11:42:11 -04004761 int rc;
4762
James Smart0a96e972011-07-22 18:37:28 -04004763 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
4764 if (nr_vfn > max_nr_vfn) {
4765 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4766 "3057 Requested vfs (%d) greater than "
4767 "supported vfs (%d)", nr_vfn, max_nr_vfn);
4768 return -EINVAL;
4769 }
4770
James Smart912e3ac2011-05-24 11:42:11 -04004771 rc = pci_enable_sriov(pdev, nr_vfn);
4772 if (rc) {
4773 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4774 "2806 Failed to enable sriov on this device "
4775 "with vfn number nr_vf:%d, rc:%d\n",
4776 nr_vfn, rc);
4777 } else
4778 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4779 "2807 Successful enable sriov on this device "
4780 "with vfn number nr_vf:%d\n", nr_vfn);
4781 return rc;
4782}
4783
4784/**
James Smart3772a992009-05-22 14:50:54 -04004785 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
4786 * @phba: pointer to lpfc hba data structure.
4787 *
4788 * This routine is invoked to set up the driver internal resources specific to
4789 * support the SLI-3 HBA device it attached to.
4790 *
4791 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004792 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004793 * other values - error
4794 **/
4795static int
4796lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
4797{
4798 struct lpfc_sli *psli;
James Smart912e3ac2011-05-24 11:42:11 -04004799 int rc;
dea31012005-04-17 16:05:31 -05004800
James Smart3772a992009-05-22 14:50:54 -04004801 /*
4802 * Initialize timers used by driver
4803 */
dea31012005-04-17 16:05:31 -05004804
James Smart3772a992009-05-22 14:50:54 -04004805 /* Heartbeat timer */
4806 init_timer(&phba->hb_tmofunc);
4807 phba->hb_tmofunc.function = lpfc_hb_timeout;
4808 phba->hb_tmofunc.data = (unsigned long)phba;
4809
4810 psli = &phba->sli;
4811 /* MBOX heartbeat timer */
4812 init_timer(&psli->mbox_tmo);
4813 psli->mbox_tmo.function = lpfc_mbox_timeout;
4814 psli->mbox_tmo.data = (unsigned long) phba;
4815 /* FCP polling mode timer */
4816 init_timer(&phba->fcp_poll_timer);
4817 phba->fcp_poll_timer.function = lpfc_poll_timeout;
4818 phba->fcp_poll_timer.data = (unsigned long) phba;
4819 /* Fabric block timer */
4820 init_timer(&phba->fabric_block_timer);
4821 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4822 phba->fabric_block_timer.data = (unsigned long) phba;
4823 /* EA polling mode timer */
4824 init_timer(&phba->eratt_poll);
4825 phba->eratt_poll.function = lpfc_poll_eratt;
4826 phba->eratt_poll.data = (unsigned long) phba;
4827
4828 /* Host attention work mask setup */
4829 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
4830 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
4831
4832 /* Get all the module params for configuring this host */
4833 lpfc_get_cfgparam(phba);
James Smart49198b32010-04-06 15:04:33 -04004834 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
4835 phba->menlo_flag |= HBA_MENLO_SUPPORT;
4836 /* check for menlo minimum sg count */
4837 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
4838 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
4839 }
4840
James Smart2a76a282012-08-03 12:35:54 -04004841 if (!phba->sli.ring)
4842 phba->sli.ring = (struct lpfc_sli_ring *)
4843 kzalloc(LPFC_SLI3_MAX_RING *
4844 sizeof(struct lpfc_sli_ring), GFP_KERNEL);
4845 if (!phba->sli.ring)
4846 return -ENOMEM;
4847
James Smart3772a992009-05-22 14:50:54 -04004848 /*
James Smart96f70772013-04-17 20:16:15 -04004849 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smart3772a992009-05-22 14:50:54 -04004850 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart3772a992009-05-22 14:50:54 -04004851 */
James Smart3772a992009-05-22 14:50:54 -04004852
James Smart96f70772013-04-17 20:16:15 -04004853 /* Initialize the host templates the configured values. */
James Smart3772a992009-05-22 14:50:54 -04004854 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4855 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4856
James Smart96f70772013-04-17 20:16:15 -04004857 /* There are going to be 2 reserved BDEs: 1 FCP cmnd + 1 FCP rsp */
4858 if (phba->cfg_enable_bg) {
4859 /*
4860 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
4861 * the FCP rsp, and a BDE for each. Sice we have no control
4862 * over how many protection data segments the SCSI Layer
4863 * will hand us (ie: there could be one for every block
4864 * in the IO), we just allocate enough BDEs to accomidate
4865 * our max amount and we need to limit lpfc_sg_seg_cnt to
4866 * minimize the risk of running out.
4867 */
4868 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4869 sizeof(struct fcp_rsp) +
4870 (LPFC_MAX_SG_SEG_CNT * sizeof(struct ulp_bde64));
4871
4872 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SEG_CNT_DIF)
4873 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT_DIF;
4874
4875 /* Total BDEs in BPL for scsi_sg_list and scsi_sg_prot_list */
4876 phba->cfg_total_seg_cnt = LPFC_MAX_SG_SEG_CNT;
4877 } else {
4878 /*
4879 * The scsi_buf for a regular I/O will hold the FCP cmnd,
4880 * the FCP rsp, a BDE for each, and a BDE for up to
4881 * cfg_sg_seg_cnt data segments.
4882 */
4883 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4884 sizeof(struct fcp_rsp) +
4885 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
4886
4887 /* Total BDEs in BPL for scsi_sg_list */
4888 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
4889 }
4890
4891 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
4892 "9088 sg_tablesize:%d dmabuf_size:%d total_bde:%d\n",
4893 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
4894 phba->cfg_total_seg_cnt);
4895
James Smart3772a992009-05-22 14:50:54 -04004896 phba->max_vpi = LPFC_MAX_VPI;
4897 /* This will be set to correct value after config_port mbox */
4898 phba->max_vports = 0;
4899
4900 /*
4901 * Initialize the SLI Layer to run with lpfc HBAs.
4902 */
4903 lpfc_sli_setup(phba);
4904 lpfc_sli_queue_setup(phba);
4905
4906 /* Allocate device driver memory */
4907 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
4908 return -ENOMEM;
4909
James Smart912e3ac2011-05-24 11:42:11 -04004910 /*
4911 * Enable sr-iov virtual functions if supported and configured
4912 * through the module parameter.
4913 */
4914 if (phba->cfg_sriov_nr_virtfn > 0) {
4915 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
4916 phba->cfg_sriov_nr_virtfn);
4917 if (rc) {
4918 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4919 "2808 Requested number of SR-IOV "
4920 "virtual functions (%d) is not "
4921 "supported\n",
4922 phba->cfg_sriov_nr_virtfn);
4923 phba->cfg_sriov_nr_virtfn = 0;
4924 }
4925 }
4926
James Smart3772a992009-05-22 14:50:54 -04004927 return 0;
4928}
4929
4930/**
4931 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
4932 * @phba: pointer to lpfc hba data structure.
4933 *
4934 * This routine is invoked to unset the driver internal resources set up
4935 * specific for supporting the SLI-3 HBA device it attached to.
4936 **/
4937static void
4938lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
4939{
4940 /* Free device driver memory allocated */
4941 lpfc_mem_free_all(phba);
4942
4943 return;
4944}
4945
4946/**
James Smartda0436e2009-05-22 14:51:39 -04004947 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
4948 * @phba: pointer to lpfc hba data structure.
4949 *
4950 * This routine is invoked to set up the driver internal resources specific to
4951 * support the SLI-4 HBA device it attached to.
4952 *
4953 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004954 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004955 * other values - error
4956 **/
4957static int
4958lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
4959{
James Smart7bb03bb2013-04-17 20:19:16 -04004960 struct lpfc_vector_map_info *cpup;
James Smartda0436e2009-05-22 14:51:39 -04004961 struct lpfc_sli *psli;
James Smart28baac72010-02-12 14:42:03 -05004962 LPFC_MBOXQ_t *mboxq;
James Smart96f70772013-04-17 20:16:15 -04004963 int rc, i, hbq_count, max_buf_size;
James Smart28baac72010-02-12 14:42:03 -05004964 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
4965 struct lpfc_mqe *mqe;
James Smart09294d42013-04-17 20:16:05 -04004966 int longs;
James Smart1ba981f2014-02-20 09:56:45 -05004967 int fof_vectors = 0;
James Smartda0436e2009-05-22 14:51:39 -04004968
James Smart716d3bc2013-09-06 12:18:28 -04004969 /* Get all the module params for configuring this host */
4970 lpfc_get_cfgparam(phba);
4971
James Smartda0436e2009-05-22 14:51:39 -04004972 /* Before proceed, wait for POST done and device ready */
4973 rc = lpfc_sli4_post_status_check(phba);
4974 if (rc)
4975 return -ENODEV;
4976
4977 /*
4978 * Initialize timers used by driver
4979 */
4980
4981 /* Heartbeat timer */
4982 init_timer(&phba->hb_tmofunc);
4983 phba->hb_tmofunc.function = lpfc_hb_timeout;
4984 phba->hb_tmofunc.data = (unsigned long)phba;
James Smart19ca7602010-11-20 23:11:55 -05004985 init_timer(&phba->rrq_tmr);
4986 phba->rrq_tmr.function = lpfc_rrq_timeout;
4987 phba->rrq_tmr.data = (unsigned long)phba;
James Smartda0436e2009-05-22 14:51:39 -04004988
4989 psli = &phba->sli;
4990 /* MBOX heartbeat timer */
4991 init_timer(&psli->mbox_tmo);
4992 psli->mbox_tmo.function = lpfc_mbox_timeout;
4993 psli->mbox_tmo.data = (unsigned long) phba;
4994 /* Fabric block timer */
4995 init_timer(&phba->fabric_block_timer);
4996 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4997 phba->fabric_block_timer.data = (unsigned long) phba;
4998 /* EA polling mode timer */
4999 init_timer(&phba->eratt_poll);
5000 phba->eratt_poll.function = lpfc_poll_eratt;
5001 phba->eratt_poll.data = (unsigned long) phba;
James Smartecfd03c2010-02-12 14:41:27 -05005002 /* FCF rediscover timer */
5003 init_timer(&phba->fcf.redisc_wait);
5004 phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
5005 phba->fcf.redisc_wait.data = (unsigned long)phba;
5006
James Smartda0436e2009-05-22 14:51:39 -04005007 /*
James Smart7ad20aa2011-05-24 11:44:28 -04005008 * Control structure for handling external multi-buffer mailbox
5009 * command pass-through.
5010 */
5011 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
5012 sizeof(struct lpfc_mbox_ext_buf_ctx));
5013 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
5014
James Smartda0436e2009-05-22 14:51:39 -04005015 phba->max_vpi = LPFC_MAX_VPI;
James Smart67d12732012-08-03 12:36:13 -04005016
James Smartda0436e2009-05-22 14:51:39 -04005017 /* This will be set to correct value after the read_config mbox */
5018 phba->max_vports = 0;
5019
5020 /* Program the default value of vlan_id and fc_map */
5021 phba->valid_vlan = 0;
5022 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5023 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5024 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5025
5026 /*
James Smart2a76a282012-08-03 12:35:54 -04005027 * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
5028 * we will associate a new ring, for each FCP fastpath EQ/CQ/WQ tuple.
5029 */
5030 if (!phba->sli.ring)
5031 phba->sli.ring = kzalloc(
James Smart67d12732012-08-03 12:36:13 -04005032 (LPFC_SLI3_MAX_RING + phba->cfg_fcp_io_channel) *
James Smart2a76a282012-08-03 12:35:54 -04005033 sizeof(struct lpfc_sli_ring), GFP_KERNEL);
5034 if (!phba->sli.ring)
5035 return -ENOMEM;
James Smart085c6472010-11-20 23:11:37 -05005036
James Smart09294d42013-04-17 20:16:05 -04005037 /*
5038 * It doesn't matter what family our adapter is in, we are
5039 * limited to 2 Pages, 512 SGEs, for our SGL.
5040 * There are going to be 2 reserved SGEs: 1 FCP cmnd + 1 FCP rsp
5041 */
5042 max_buf_size = (2 * SLI4_PAGE_SIZE);
5043 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SGL_SEG_CNT - 2)
5044 phba->cfg_sg_seg_cnt = LPFC_MAX_SGL_SEG_CNT - 2;
James Smart09294d42013-04-17 20:16:05 -04005045
James Smartda0436e2009-05-22 14:51:39 -04005046 /*
James Smart96f70772013-04-17 20:16:15 -04005047 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smartda0436e2009-05-22 14:51:39 -04005048 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart28baac72010-02-12 14:42:03 -05005049 */
James Smart085c6472010-11-20 23:11:37 -05005050
James Smart96f70772013-04-17 20:16:15 -04005051 if (phba->cfg_enable_bg) {
5052 /*
5053 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
5054 * the FCP rsp, and a SGE for each. Sice we have no control
5055 * over how many protection data segments the SCSI Layer
5056 * will hand us (ie: there could be one for every block
5057 * in the IO), we just allocate enough SGEs to accomidate
5058 * our max amount and we need to limit lpfc_sg_seg_cnt to
5059 * minimize the risk of running out.
5060 */
5061 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5062 sizeof(struct fcp_rsp) + max_buf_size;
5063
5064 /* Total SGEs for scsi_sg_list and scsi_sg_prot_list */
5065 phba->cfg_total_seg_cnt = LPFC_MAX_SGL_SEG_CNT;
5066
5067 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SLI4_SEG_CNT_DIF)
5068 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SLI4_SEG_CNT_DIF;
5069 } else {
5070 /*
5071 * The scsi_buf for a regular I/O will hold the FCP cmnd,
5072 * the FCP rsp, a SGE for each, and a SGE for up to
5073 * cfg_sg_seg_cnt data segments.
5074 */
5075 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
5076 sizeof(struct fcp_rsp) +
5077 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge));
5078
5079 /* Total SGEs for scsi_sg_list */
5080 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
5081 /*
5082 * NOTE: if (phba->cfg_sg_seg_cnt + 2) <= 256 we only need
5083 * to post 1 page for the SGL.
5084 */
James Smart085c6472010-11-20 23:11:37 -05005085 }
James Smartacd68592012-01-18 16:25:09 -05005086
James Smart96f70772013-04-17 20:16:15 -04005087 /* Initialize the host templates with the updated values. */
5088 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5089 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
5090
5091 if (phba->cfg_sg_dma_buf_size <= LPFC_MIN_SG_SLI4_BUF_SZ)
5092 phba->cfg_sg_dma_buf_size = LPFC_MIN_SG_SLI4_BUF_SZ;
5093 else
5094 phba->cfg_sg_dma_buf_size =
5095 SLI4_PAGE_ALIGN(phba->cfg_sg_dma_buf_size);
5096
5097 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
5098 "9087 sg_tablesize:%d dmabuf_size:%d total_sge:%d\n",
5099 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
5100 phba->cfg_total_seg_cnt);
James Smartda0436e2009-05-22 14:51:39 -04005101
5102 /* Initialize buffer queue management fields */
5103 hbq_count = lpfc_sli_hbq_count();
5104 for (i = 0; i < hbq_count; ++i)
5105 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
5106 INIT_LIST_HEAD(&phba->rb_pend_list);
5107 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
5108 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
5109
5110 /*
5111 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
5112 */
5113 /* Initialize the Abort scsi buffer list used by driver */
5114 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
5115 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
5116 /* This abort list used by worker thread */
5117 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
5118
5119 /*
James Smart6d368e52011-05-24 11:44:12 -04005120 * Initialize driver internal slow-path work queues
James Smartda0436e2009-05-22 14:51:39 -04005121 */
5122
5123 /* Driver internel slow-path CQ Event pool */
5124 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
5125 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04005126 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04005127 /* Asynchronous event CQ Event work queue list */
5128 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
5129 /* Fast-path XRI aborted CQ Event work queue list */
5130 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
5131 /* Slow-path XRI aborted CQ Event work queue list */
5132 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
5133 /* Receive queue CQ Event work queue list */
5134 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
5135
James Smart6d368e52011-05-24 11:44:12 -04005136 /* Initialize extent block lists. */
5137 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
5138 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
5139 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
5140 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
5141
James Smartda0436e2009-05-22 14:51:39 -04005142 /* Initialize the driver internal SLI layer lists. */
5143 lpfc_sli_setup(phba);
5144 lpfc_sli_queue_setup(phba);
5145
5146 /* Allocate device driver memory */
5147 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
5148 if (rc)
5149 return -ENOMEM;
5150
James Smart2fcee4b2010-12-15 17:57:46 -05005151 /* IF Type 2 ports get initialized now. */
5152 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
5153 LPFC_SLI_INTF_IF_TYPE_2) {
5154 rc = lpfc_pci_function_reset(phba);
5155 if (unlikely(rc))
5156 return -ENODEV;
5157 }
5158
James Smartda0436e2009-05-22 14:51:39 -04005159 /* Create the bootstrap mailbox command */
5160 rc = lpfc_create_bootstrap_mbox(phba);
5161 if (unlikely(rc))
5162 goto out_free_mem;
5163
5164 /* Set up the host's endian order with the device. */
5165 rc = lpfc_setup_endian_order(phba);
5166 if (unlikely(rc))
5167 goto out_free_bsmbx;
5168
5169 /* Set up the hba's configuration parameters. */
5170 rc = lpfc_sli4_read_config(phba);
5171 if (unlikely(rc))
5172 goto out_free_bsmbx;
James Smartcff261f2013-12-17 20:29:47 -05005173 rc = lpfc_mem_alloc_active_rrq_pool_s4(phba);
5174 if (unlikely(rc))
5175 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04005176
James Smart2fcee4b2010-12-15 17:57:46 -05005177 /* IF Type 0 ports get initialized now. */
5178 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
5179 LPFC_SLI_INTF_IF_TYPE_0) {
5180 rc = lpfc_pci_function_reset(phba);
5181 if (unlikely(rc))
5182 goto out_free_bsmbx;
5183 }
James Smartda0436e2009-05-22 14:51:39 -04005184
James Smartcb5172e2010-03-15 11:25:07 -04005185 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
5186 GFP_KERNEL);
5187 if (!mboxq) {
5188 rc = -ENOMEM;
5189 goto out_free_bsmbx;
5190 }
5191
James Smartfedd3b72011-02-16 12:39:24 -05005192 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
James Smartcb5172e2010-03-15 11:25:07 -04005193 lpfc_supported_pages(mboxq);
5194 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smartfedd3b72011-02-16 12:39:24 -05005195 if (!rc) {
5196 mqe = &mboxq->u.mqe;
5197 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
5198 LPFC_MAX_SUPPORTED_PAGES);
5199 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
5200 switch (pn_page[i]) {
5201 case LPFC_SLI4_PARAMETERS:
5202 phba->sli4_hba.pc_sli4_params.supported = 1;
5203 break;
5204 default:
5205 break;
5206 }
5207 }
5208 /* Read the port's SLI4 Parameters capabilities if supported. */
5209 if (phba->sli4_hba.pc_sli4_params.supported)
5210 rc = lpfc_pc_sli4_params_get(phba, mboxq);
5211 if (rc) {
5212 mempool_free(mboxq, phba->mbox_mem_pool);
5213 rc = -EIO;
5214 goto out_free_bsmbx;
James Smartcb5172e2010-03-15 11:25:07 -04005215 }
5216 }
James Smartfedd3b72011-02-16 12:39:24 -05005217 /*
5218 * Get sli4 parameters that override parameters from Port capabilities.
James Smart6d368e52011-05-24 11:44:12 -04005219 * If this call fails, it isn't critical unless the SLI4 parameters come
5220 * back in conflict.
James Smartfedd3b72011-02-16 12:39:24 -05005221 */
James Smart6d368e52011-05-24 11:44:12 -04005222 rc = lpfc_get_sli4_parameters(phba, mboxq);
5223 if (rc) {
5224 if (phba->sli4_hba.extents_in_use &&
5225 phba->sli4_hba.rpi_hdrs_in_use) {
5226 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5227 "2999 Unsupported SLI4 Parameters "
5228 "Extents and RPI headers enabled.\n");
5229 goto out_free_bsmbx;
5230 }
5231 }
James Smartcb5172e2010-03-15 11:25:07 -04005232 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1ba981f2014-02-20 09:56:45 -05005233
5234 /* Verify OAS is supported */
5235 lpfc_sli4_oas_verify(phba);
5236 if (phba->cfg_fof)
5237 fof_vectors = 1;
5238
James Smart5350d872011-10-10 21:33:49 -04005239 /* Verify all the SLI4 queues */
5240 rc = lpfc_sli4_queue_verify(phba);
James Smartda0436e2009-05-22 14:51:39 -04005241 if (rc)
5242 goto out_free_bsmbx;
5243
5244 /* Create driver internal CQE event pool */
5245 rc = lpfc_sli4_cq_event_pool_create(phba);
5246 if (rc)
James Smart5350d872011-10-10 21:33:49 -04005247 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04005248
James Smart8a9d2e82012-05-09 21:16:12 -04005249 /* Initialize sgl lists per host */
5250 lpfc_init_sgl_list(phba);
5251
5252 /* Allocate and initialize active sgl array */
James Smartda0436e2009-05-22 14:51:39 -04005253 rc = lpfc_init_active_sgl_array(phba);
5254 if (rc) {
5255 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5256 "1430 Failed to initialize sgl list.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04005257 goto out_destroy_cq_event_pool;
James Smartda0436e2009-05-22 14:51:39 -04005258 }
James Smartda0436e2009-05-22 14:51:39 -04005259 rc = lpfc_sli4_init_rpi_hdrs(phba);
5260 if (rc) {
5261 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5262 "1432 Failed to initialize rpi headers.\n");
5263 goto out_free_active_sgl;
5264 }
5265
James Smarta93ff372010-10-22 11:06:08 -04005266 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
James Smart0c9ab6f2010-02-26 14:15:57 -05005267 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
5268 phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
5269 GFP_KERNEL);
5270 if (!phba->fcf.fcf_rr_bmask) {
5271 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5272 "2759 Failed allocate memory for FCF round "
5273 "robin failover bmask\n");
James Smart05580562011-05-24 11:40:48 -04005274 rc = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -05005275 goto out_remove_rpi_hdrs;
5276 }
5277
James Smart67d12732012-08-03 12:36:13 -04005278 phba->sli4_hba.fcp_eq_hdl =
5279 kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
James Smart1ba981f2014-02-20 09:56:45 -05005280 (fof_vectors + phba->cfg_fcp_io_channel)),
5281 GFP_KERNEL);
James Smart67d12732012-08-03 12:36:13 -04005282 if (!phba->sli4_hba.fcp_eq_hdl) {
5283 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5284 "2572 Failed allocate memory for "
5285 "fast-path per-EQ handle array\n");
5286 rc = -ENOMEM;
5287 goto out_free_fcf_rr_bmask;
James Smartda0436e2009-05-22 14:51:39 -04005288 }
5289
5290 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
James Smart1ba981f2014-02-20 09:56:45 -05005291 (fof_vectors +
5292 phba->cfg_fcp_io_channel)), GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005293 if (!phba->sli4_hba.msix_entries) {
5294 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5295 "2573 Failed allocate memory for msi-x "
5296 "interrupt vector entries\n");
James Smart05580562011-05-24 11:40:48 -04005297 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04005298 goto out_free_fcp_eq_hdl;
5299 }
5300
James Smart7bb03bb2013-04-17 20:19:16 -04005301 phba->sli4_hba.cpu_map = kzalloc((sizeof(struct lpfc_vector_map_info) *
5302 phba->sli4_hba.num_present_cpu),
5303 GFP_KERNEL);
5304 if (!phba->sli4_hba.cpu_map) {
5305 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5306 "3327 Failed allocate memory for msi-x "
5307 "interrupt vector mapping\n");
5308 rc = -ENOMEM;
5309 goto out_free_msix;
5310 }
James Smartb246de12013-05-31 17:03:07 -04005311 if (lpfc_used_cpu == NULL) {
5312 lpfc_used_cpu = kzalloc((sizeof(uint16_t) * lpfc_present_cpu),
5313 GFP_KERNEL);
5314 if (!lpfc_used_cpu) {
5315 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5316 "3335 Failed allocate memory for msi-x "
5317 "interrupt vector mapping\n");
5318 kfree(phba->sli4_hba.cpu_map);
5319 rc = -ENOMEM;
5320 goto out_free_msix;
5321 }
5322 for (i = 0; i < lpfc_present_cpu; i++)
5323 lpfc_used_cpu[i] = LPFC_VECTOR_MAP_EMPTY;
5324 }
5325
James Smart7bb03bb2013-04-17 20:19:16 -04005326 /* Initialize io channels for round robin */
5327 cpup = phba->sli4_hba.cpu_map;
5328 rc = 0;
5329 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
5330 cpup->channel_id = rc;
5331 rc++;
5332 if (rc >= phba->cfg_fcp_io_channel)
5333 rc = 0;
5334 }
5335
James Smart912e3ac2011-05-24 11:42:11 -04005336 /*
5337 * Enable sr-iov virtual functions if supported and configured
5338 * through the module parameter.
5339 */
5340 if (phba->cfg_sriov_nr_virtfn > 0) {
5341 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
5342 phba->cfg_sriov_nr_virtfn);
5343 if (rc) {
5344 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5345 "3020 Requested number of SR-IOV "
5346 "virtual functions (%d) is not "
5347 "supported\n",
5348 phba->cfg_sriov_nr_virtfn);
5349 phba->cfg_sriov_nr_virtfn = 0;
5350 }
5351 }
5352
James Smart5248a742011-07-22 18:37:06 -04005353 return 0;
James Smartda0436e2009-05-22 14:51:39 -04005354
James Smart7bb03bb2013-04-17 20:19:16 -04005355out_free_msix:
5356 kfree(phba->sli4_hba.msix_entries);
James Smartda0436e2009-05-22 14:51:39 -04005357out_free_fcp_eq_hdl:
5358 kfree(phba->sli4_hba.fcp_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05005359out_free_fcf_rr_bmask:
5360 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04005361out_remove_rpi_hdrs:
5362 lpfc_sli4_remove_rpi_hdrs(phba);
5363out_free_active_sgl:
5364 lpfc_free_active_sgl(phba);
James Smartda0436e2009-05-22 14:51:39 -04005365out_destroy_cq_event_pool:
5366 lpfc_sli4_cq_event_pool_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04005367out_free_bsmbx:
5368 lpfc_destroy_bootstrap_mbox(phba);
5369out_free_mem:
5370 lpfc_mem_free(phba);
5371 return rc;
5372}
5373
5374/**
5375 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
5376 * @phba: pointer to lpfc hba data structure.
5377 *
5378 * This routine is invoked to unset the driver internal resources set up
5379 * specific for supporting the SLI-4 HBA device it attached to.
5380 **/
5381static void
5382lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
5383{
5384 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
5385
James Smart7bb03bb2013-04-17 20:19:16 -04005386 /* Free memory allocated for msi-x interrupt vector to CPU mapping */
5387 kfree(phba->sli4_hba.cpu_map);
5388 phba->sli4_hba.num_present_cpu = 0;
5389 phba->sli4_hba.num_online_cpu = 0;
James Smart76fd07a2014-02-20 09:57:18 -05005390 phba->sli4_hba.curr_disp_cpu = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04005391
James Smartda0436e2009-05-22 14:51:39 -04005392 /* Free memory allocated for msi-x interrupt vector entries */
5393 kfree(phba->sli4_hba.msix_entries);
5394
5395 /* Free memory allocated for fast-path work queue handles */
5396 kfree(phba->sli4_hba.fcp_eq_hdl);
5397
5398 /* Free the allocated rpi headers. */
5399 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04005400 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04005401
James Smart0c9ab6f2010-02-26 14:15:57 -05005402 /* Free eligible FCF index bmask */
5403 kfree(phba->fcf.fcf_rr_bmask);
5404
James Smartda0436e2009-05-22 14:51:39 -04005405 /* Free the ELS sgl list */
5406 lpfc_free_active_sgl(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04005407 lpfc_free_els_sgl_list(phba);
James Smartda0436e2009-05-22 14:51:39 -04005408
James Smartda0436e2009-05-22 14:51:39 -04005409 /* Free the completion queue EQ event pool */
5410 lpfc_sli4_cq_event_release_all(phba);
5411 lpfc_sli4_cq_event_pool_destroy(phba);
5412
James Smart6d368e52011-05-24 11:44:12 -04005413 /* Release resource identifiers. */
5414 lpfc_sli4_dealloc_resource_identifiers(phba);
5415
James Smartda0436e2009-05-22 14:51:39 -04005416 /* Free the bsmbx region. */
5417 lpfc_destroy_bootstrap_mbox(phba);
5418
5419 /* Free the SLI Layer memory with SLI4 HBAs */
5420 lpfc_mem_free_all(phba);
5421
5422 /* Free the current connect table */
5423 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04005424 &phba->fcf_conn_rec_list, list) {
5425 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04005426 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04005427 }
James Smartda0436e2009-05-22 14:51:39 -04005428
5429 return;
5430}
5431
5432/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005433 * lpfc_init_api_table_setup - Set up init api function jump table
James Smart3772a992009-05-22 14:50:54 -04005434 * @phba: The hba struct for which this call is being executed.
5435 * @dev_grp: The HBA PCI-Device group number.
5436 *
5437 * This routine sets up the device INIT interface API function jump table
5438 * in @phba struct.
5439 *
5440 * Returns: 0 - success, -ENODEV - failure.
5441 **/
5442int
5443lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5444{
James Smart84d1b002010-02-12 14:42:33 -05005445 phba->lpfc_hba_init_link = lpfc_hba_init_link;
5446 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart7f860592011-03-11 16:05:52 -05005447 phba->lpfc_selective_reset = lpfc_selective_reset;
James Smart3772a992009-05-22 14:50:54 -04005448 switch (dev_grp) {
5449 case LPFC_PCI_DEV_LP:
5450 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
5451 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
5452 phba->lpfc_stop_port = lpfc_stop_port_s3;
5453 break;
James Smartda0436e2009-05-22 14:51:39 -04005454 case LPFC_PCI_DEV_OC:
5455 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
5456 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
5457 phba->lpfc_stop_port = lpfc_stop_port_s4;
5458 break;
James Smart3772a992009-05-22 14:50:54 -04005459 default:
5460 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5461 "1431 Invalid HBA PCI-device group: 0x%x\n",
5462 dev_grp);
5463 return -ENODEV;
5464 break;
5465 }
5466 return 0;
5467}
5468
5469/**
5470 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
5471 * @phba: pointer to lpfc hba data structure.
5472 *
5473 * This routine is invoked to set up the driver internal resources before the
5474 * device specific resource setup to support the HBA device it attached to.
5475 *
5476 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005477 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005478 * other values - error
5479 **/
5480static int
5481lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
5482{
5483 /*
5484 * Driver resources common to all SLI revisions
5485 */
James Smartea2151b2008-09-07 11:52:10 -04005486 atomic_set(&phba->fast_event_count, 0);
James Smart2e0fef82007-06-17 19:56:36 -05005487 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005488
James Smarte47c9092008-02-08 18:49:26 -05005489 /* Initialize ndlp management spinlock */
5490 spin_lock_init(&phba->ndlp_lock);
5491
James Smart2e0fef82007-06-17 19:56:36 -05005492 INIT_LIST_HEAD(&phba->port_list);
James Smart3772a992009-05-22 14:50:54 -04005493 INIT_LIST_HEAD(&phba->work_list);
James Smart84774a42008-08-24 21:50:06 -04005494 init_waitqueue_head(&phba->wait_4_mlo_m_q);
dea31012005-04-17 16:05:31 -05005495
James Smart3772a992009-05-22 14:50:54 -04005496 /* Initialize the wait queue head for the kernel thread */
5497 init_waitqueue_head(&phba->work_waitq);
James Smart858c9f62007-06-17 19:56:39 -05005498
James Smart3772a992009-05-22 14:50:54 -04005499 /* Initialize the scsi buffer list used by driver for scsi IO */
James Smarta40fc5f2013-04-17 20:17:40 -04005500 spin_lock_init(&phba->scsi_buf_list_get_lock);
5501 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_get);
5502 spin_lock_init(&phba->scsi_buf_list_put_lock);
5503 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
dea31012005-04-17 16:05:31 -05005504
James Smart3772a992009-05-22 14:50:54 -04005505 /* Initialize the fabric iocb list */
5506 INIT_LIST_HEAD(&phba->fabric_iocb_list);
dea31012005-04-17 16:05:31 -05005507
James Smart3772a992009-05-22 14:50:54 -04005508 /* Initialize list to save ELS buffers */
5509 INIT_LIST_HEAD(&phba->elsbuf);
dea31012005-04-17 16:05:31 -05005510
James Smart3772a992009-05-22 14:50:54 -04005511 /* Initialize FCF connection rec list */
5512 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
dea31012005-04-17 16:05:31 -05005513
James Smart1ba981f2014-02-20 09:56:45 -05005514 /* Initialize OAS configuration list */
5515 spin_lock_init(&phba->devicelock);
5516 INIT_LIST_HEAD(&phba->luns);
5517
James Smart3772a992009-05-22 14:50:54 -04005518 return 0;
5519}
dea31012005-04-17 16:05:31 -05005520
James Smart3772a992009-05-22 14:50:54 -04005521/**
5522 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
5523 * @phba: pointer to lpfc hba data structure.
5524 *
5525 * This routine is invoked to set up the driver internal resources after the
5526 * device specific resource setup to support the HBA device it attached to.
5527 *
5528 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005529 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005530 * other values - error
5531 **/
5532static int
5533lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
5534{
5535 int error;
5536
5537 /* Startup the kernel thread for this host adapter. */
5538 phba->worker_thread = kthread_run(lpfc_do_work, phba,
5539 "lpfc_worker_%d", phba->brd_no);
5540 if (IS_ERR(phba->worker_thread)) {
5541 error = PTR_ERR(phba->worker_thread);
5542 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005543 }
5544
James Smart3772a992009-05-22 14:50:54 -04005545 return 0;
5546}
5547
5548/**
5549 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
5550 * @phba: pointer to lpfc hba data structure.
5551 *
5552 * This routine is invoked to unset the driver internal resources set up after
5553 * the device specific resource setup for supporting the HBA device it
5554 * attached to.
5555 **/
5556static void
5557lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
5558{
5559 /* Stop kernel worker thread */
5560 kthread_stop(phba->worker_thread);
5561}
5562
5563/**
5564 * lpfc_free_iocb_list - Free iocb list.
5565 * @phba: pointer to lpfc hba data structure.
5566 *
5567 * This routine is invoked to free the driver's IOCB list and memory.
5568 **/
5569static void
5570lpfc_free_iocb_list(struct lpfc_hba *phba)
5571{
5572 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
5573
5574 spin_lock_irq(&phba->hbalock);
5575 list_for_each_entry_safe(iocbq_entry, iocbq_next,
5576 &phba->lpfc_iocb_list, list) {
5577 list_del(&iocbq_entry->list);
5578 kfree(iocbq_entry);
5579 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005580 }
James Smart3772a992009-05-22 14:50:54 -04005581 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005582
James Smart3772a992009-05-22 14:50:54 -04005583 return;
5584}
dea31012005-04-17 16:05:31 -05005585
James Smart3772a992009-05-22 14:50:54 -04005586/**
5587 * lpfc_init_iocb_list - Allocate and initialize iocb list.
5588 * @phba: pointer to lpfc hba data structure.
5589 *
5590 * This routine is invoked to allocate and initizlize the driver's IOCB
5591 * list and set up the IOCB tag array accordingly.
5592 *
5593 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005594 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005595 * other values - error
5596 **/
5597static int
5598lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
5599{
5600 struct lpfc_iocbq *iocbq_entry = NULL;
5601 uint16_t iotag;
5602 int i;
dea31012005-04-17 16:05:31 -05005603
5604 /* Initialize and populate the iocb list per host. */
5605 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04005606 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07005607 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05005608 if (iocbq_entry == NULL) {
5609 printk(KERN_ERR "%s: only allocated %d iocbs of "
5610 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07005611 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05005612 goto out_free_iocbq;
5613 }
5614
James Bottomley604a3e32005-10-29 10:28:33 -05005615 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
5616 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04005617 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05005618 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04005619 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05005620 goto out_free_iocbq;
5621 }
James Smart6d368e52011-05-24 11:44:12 -04005622 iocbq_entry->sli4_lxritag = NO_XRI;
James Smart3772a992009-05-22 14:50:54 -04005623 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05005624
5625 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005626 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
5627 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05005628 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005629 }
5630
James Smart3772a992009-05-22 14:50:54 -04005631 return 0;
5632
5633out_free_iocbq:
5634 lpfc_free_iocb_list(phba);
5635
5636 return -ENOMEM;
5637}
5638
5639/**
James Smart8a9d2e82012-05-09 21:16:12 -04005640 * lpfc_free_sgl_list - Free a given sgl list.
James Smartda0436e2009-05-22 14:51:39 -04005641 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -04005642 * @sglq_list: pointer to the head of sgl list.
James Smartda0436e2009-05-22 14:51:39 -04005643 *
James Smart8a9d2e82012-05-09 21:16:12 -04005644 * This routine is invoked to free a give sgl list and memory.
James Smartda0436e2009-05-22 14:51:39 -04005645 **/
James Smart8a9d2e82012-05-09 21:16:12 -04005646void
5647lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
James Smartda0436e2009-05-22 14:51:39 -04005648{
5649 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart8a9d2e82012-05-09 21:16:12 -04005650
5651 list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
5652 list_del(&sglq_entry->list);
5653 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
5654 kfree(sglq_entry);
5655 }
5656}
5657
5658/**
5659 * lpfc_free_els_sgl_list - Free els sgl list.
5660 * @phba: pointer to lpfc hba data structure.
5661 *
5662 * This routine is invoked to free the driver's els sgl list and memory.
5663 **/
5664static void
5665lpfc_free_els_sgl_list(struct lpfc_hba *phba)
5666{
James Smartda0436e2009-05-22 14:51:39 -04005667 LIST_HEAD(sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04005668
James Smart8a9d2e82012-05-09 21:16:12 -04005669 /* Retrieve all els sgls from driver list */
James Smartda0436e2009-05-22 14:51:39 -04005670 spin_lock_irq(&phba->hbalock);
5671 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
5672 spin_unlock_irq(&phba->hbalock);
5673
James Smart8a9d2e82012-05-09 21:16:12 -04005674 /* Now free the sgl list */
5675 lpfc_free_sgl_list(phba, &sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04005676}
5677
5678/**
5679 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
5680 * @phba: pointer to lpfc hba data structure.
5681 *
5682 * This routine is invoked to allocate the driver's active sgl memory.
5683 * This array will hold the sglq_entry's for active IOs.
5684 **/
5685static int
5686lpfc_init_active_sgl_array(struct lpfc_hba *phba)
5687{
5688 int size;
5689 size = sizeof(struct lpfc_sglq *);
5690 size *= phba->sli4_hba.max_cfg_param.max_xri;
5691
5692 phba->sli4_hba.lpfc_sglq_active_list =
5693 kzalloc(size, GFP_KERNEL);
5694 if (!phba->sli4_hba.lpfc_sglq_active_list)
5695 return -ENOMEM;
5696 return 0;
5697}
5698
5699/**
5700 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
5701 * @phba: pointer to lpfc hba data structure.
5702 *
5703 * This routine is invoked to walk through the array of active sglq entries
5704 * and free all of the resources.
5705 * This is just a place holder for now.
5706 **/
5707static void
5708lpfc_free_active_sgl(struct lpfc_hba *phba)
5709{
5710 kfree(phba->sli4_hba.lpfc_sglq_active_list);
5711}
5712
5713/**
5714 * lpfc_init_sgl_list - Allocate and initialize sgl list.
5715 * @phba: pointer to lpfc hba data structure.
5716 *
5717 * This routine is invoked to allocate and initizlize the driver's sgl
5718 * list and set up the sgl xritag tag array accordingly.
5719 *
James Smartda0436e2009-05-22 14:51:39 -04005720 **/
James Smart8a9d2e82012-05-09 21:16:12 -04005721static void
James Smartda0436e2009-05-22 14:51:39 -04005722lpfc_init_sgl_list(struct lpfc_hba *phba)
5723{
James Smartda0436e2009-05-22 14:51:39 -04005724 /* Initialize and populate the sglq list per host/VF. */
5725 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
5726 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
5727
James Smart8a9d2e82012-05-09 21:16:12 -04005728 /* els xri-sgl book keeping */
5729 phba->sli4_hba.els_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04005730
James Smart8a9d2e82012-05-09 21:16:12 -04005731 /* scsi xri-buffer book keeping */
James Smartda0436e2009-05-22 14:51:39 -04005732 phba->sli4_hba.scsi_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04005733}
5734
5735/**
5736 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
5737 * @phba: pointer to lpfc hba data structure.
5738 *
5739 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -04005740 * port for those SLI4 ports that do not support extents. This routine
James Smartda0436e2009-05-22 14:51:39 -04005741 * posts a PAGE_SIZE memory region to the port to hold up to
James Smart88a2cfb2011-07-22 18:36:33 -04005742 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
5743 * and should be called only when interrupts are disabled.
James Smartda0436e2009-05-22 14:51:39 -04005744 *
5745 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005746 * 0 - successful
James Smart88a2cfb2011-07-22 18:36:33 -04005747 * -ERROR - otherwise.
James Smartda0436e2009-05-22 14:51:39 -04005748 **/
5749int
5750lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
5751{
5752 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04005753 struct lpfc_rpi_hdr *rpi_hdr;
5754
5755 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
James Smartff78d8f2011-12-13 13:21:35 -05005756 if (!phba->sli4_hba.rpi_hdrs_in_use)
James Smart6d368e52011-05-24 11:44:12 -04005757 return rc;
James Smart6d368e52011-05-24 11:44:12 -04005758 if (phba->sli4_hba.extents_in_use)
5759 return -EIO;
James Smartda0436e2009-05-22 14:51:39 -04005760
5761 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
5762 if (!rpi_hdr) {
5763 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5764 "0391 Error during rpi post operation\n");
5765 lpfc_sli4_remove_rpis(phba);
5766 rc = -ENODEV;
5767 }
5768
5769 return rc;
5770}
5771
5772/**
5773 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
5774 * @phba: pointer to lpfc hba data structure.
5775 *
5776 * This routine is invoked to allocate a single 4KB memory region to
5777 * support rpis and stores them in the phba. This single region
5778 * provides support for up to 64 rpis. The region is used globally
5779 * by the device.
5780 *
5781 * Returns:
5782 * A valid rpi hdr on success.
5783 * A NULL pointer on any failure.
5784 **/
5785struct lpfc_rpi_hdr *
5786lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
5787{
5788 uint16_t rpi_limit, curr_rpi_range;
5789 struct lpfc_dmabuf *dmabuf;
5790 struct lpfc_rpi_hdr *rpi_hdr;
James Smart9589b0622011-04-16 11:03:17 -04005791 uint32_t rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04005792
James Smart6d368e52011-05-24 11:44:12 -04005793 /*
5794 * If the SLI4 port supports extents, posting the rpi header isn't
5795 * required. Set the expected maximum count and let the actual value
5796 * get set when extents are fully allocated.
5797 */
5798 if (!phba->sli4_hba.rpi_hdrs_in_use)
5799 return NULL;
5800 if (phba->sli4_hba.extents_in_use)
5801 return NULL;
5802
5803 /* The limit on the logical index is just the max_rpi count. */
James Smartda0436e2009-05-22 14:51:39 -04005804 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
James Smart6d368e52011-05-24 11:44:12 -04005805 phba->sli4_hba.max_cfg_param.max_rpi - 1;
James Smartda0436e2009-05-22 14:51:39 -04005806
5807 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04005808 /*
5809 * Establish the starting RPI in this header block. The starting
5810 * rpi is normalized to a zero base because the physical rpi is
5811 * port based.
5812 */
James Smart97f2ecf2012-03-01 22:35:23 -05005813 curr_rpi_range = phba->sli4_hba.next_rpi;
James Smartda0436e2009-05-22 14:51:39 -04005814 spin_unlock_irq(&phba->hbalock);
5815
5816 /*
5817 * The port has a limited number of rpis. The increment here
5818 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
5819 * and to allow the full max_rpi range per port.
5820 */
5821 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
James Smart9589b0622011-04-16 11:03:17 -04005822 rpi_count = rpi_limit - curr_rpi_range;
5823 else
5824 rpi_count = LPFC_RPI_HDR_COUNT;
James Smartda0436e2009-05-22 14:51:39 -04005825
James Smart6d368e52011-05-24 11:44:12 -04005826 if (!rpi_count)
5827 return NULL;
James Smartda0436e2009-05-22 14:51:39 -04005828 /*
5829 * First allocate the protocol header region for the port. The
5830 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
5831 */
5832 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5833 if (!dmabuf)
5834 return NULL;
5835
5836 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5837 LPFC_HDR_TEMPLATE_SIZE,
5838 &dmabuf->phys,
5839 GFP_KERNEL);
5840 if (!dmabuf->virt) {
5841 rpi_hdr = NULL;
5842 goto err_free_dmabuf;
5843 }
5844
5845 memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
5846 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
5847 rpi_hdr = NULL;
5848 goto err_free_coherent;
5849 }
5850
5851 /* Save the rpi header data for cleanup later. */
5852 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
5853 if (!rpi_hdr)
5854 goto err_free_coherent;
5855
5856 rpi_hdr->dmabuf = dmabuf;
5857 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
5858 rpi_hdr->page_count = 1;
5859 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04005860
5861 /* The rpi_hdr stores the logical index only. */
5862 rpi_hdr->start_rpi = curr_rpi_range;
James Smartda0436e2009-05-22 14:51:39 -04005863 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
5864
5865 /*
James Smart6d368e52011-05-24 11:44:12 -04005866 * The next_rpi stores the next logical module-64 rpi value used
5867 * to post physical rpis in subsequent rpi postings.
James Smartda0436e2009-05-22 14:51:39 -04005868 */
James Smart9589b0622011-04-16 11:03:17 -04005869 phba->sli4_hba.next_rpi += rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04005870 spin_unlock_irq(&phba->hbalock);
5871 return rpi_hdr;
5872
5873 err_free_coherent:
5874 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
5875 dmabuf->virt, dmabuf->phys);
5876 err_free_dmabuf:
5877 kfree(dmabuf);
5878 return NULL;
5879}
5880
5881/**
5882 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
5883 * @phba: pointer to lpfc hba data structure.
5884 *
5885 * This routine is invoked to remove all memory resources allocated
James Smart6d368e52011-05-24 11:44:12 -04005886 * to support rpis for SLI4 ports not supporting extents. This routine
5887 * presumes the caller has released all rpis consumed by fabric or port
5888 * logins and is prepared to have the header pages removed.
James Smartda0436e2009-05-22 14:51:39 -04005889 **/
5890void
5891lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
5892{
5893 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
5894
James Smart6d368e52011-05-24 11:44:12 -04005895 if (!phba->sli4_hba.rpi_hdrs_in_use)
5896 goto exit;
5897
James Smartda0436e2009-05-22 14:51:39 -04005898 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
5899 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
5900 list_del(&rpi_hdr->list);
5901 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
5902 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
5903 kfree(rpi_hdr->dmabuf);
5904 kfree(rpi_hdr);
5905 }
James Smart6d368e52011-05-24 11:44:12 -04005906 exit:
5907 /* There are no rpis available to the port now. */
5908 phba->sli4_hba.next_rpi = 0;
James Smartda0436e2009-05-22 14:51:39 -04005909}
5910
5911/**
James Smart3772a992009-05-22 14:50:54 -04005912 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
5913 * @pdev: pointer to pci device data structure.
5914 *
5915 * This routine is invoked to allocate the driver hba data structure for an
5916 * HBA device. If the allocation is successful, the phba reference to the
5917 * PCI device data structure is set.
5918 *
5919 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005920 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04005921 * NULL - error
5922 **/
5923static struct lpfc_hba *
5924lpfc_hba_alloc(struct pci_dev *pdev)
5925{
5926 struct lpfc_hba *phba;
5927
5928 /* Allocate memory for HBA structure */
5929 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
5930 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02005931 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04005932 return NULL;
5933 }
5934
5935 /* Set reference to PCI device in HBA structure */
5936 phba->pcidev = pdev;
5937
5938 /* Assign an unused board number */
5939 phba->brd_no = lpfc_get_instance();
5940 if (phba->brd_no < 0) {
5941 kfree(phba);
5942 return NULL;
5943 }
5944
James Smart4fede782010-01-26 23:08:55 -05005945 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04005946 INIT_LIST_HEAD(&phba->ct_ev_waiters);
5947
James Smart3772a992009-05-22 14:50:54 -04005948 return phba;
5949}
5950
5951/**
5952 * lpfc_hba_free - Free driver hba data structure with a device.
5953 * @phba: pointer to lpfc hba data structure.
5954 *
5955 * This routine is invoked to free the driver hba data structure with an
5956 * HBA device.
5957 **/
5958static void
5959lpfc_hba_free(struct lpfc_hba *phba)
5960{
5961 /* Release the driver assigned board number */
5962 idr_remove(&lpfc_hba_index, phba->brd_no);
5963
James Smart2a76a282012-08-03 12:35:54 -04005964 /* Free memory allocated with sli rings */
5965 kfree(phba->sli.ring);
5966 phba->sli.ring = NULL;
5967
James Smart3772a992009-05-22 14:50:54 -04005968 kfree(phba);
5969 return;
5970}
5971
5972/**
5973 * lpfc_create_shost - Create hba physical port with associated scsi host.
5974 * @phba: pointer to lpfc hba data structure.
5975 *
5976 * This routine is invoked to create HBA physical port and associate a SCSI
5977 * host with it.
5978 *
5979 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005980 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005981 * other values - error
5982 **/
5983static int
5984lpfc_create_shost(struct lpfc_hba *phba)
5985{
5986 struct lpfc_vport *vport;
5987 struct Scsi_Host *shost;
5988
5989 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05005990 phba->fc_edtov = FF_DEF_EDTOV;
5991 phba->fc_ratov = FF_DEF_RATOV;
5992 phba->fc_altov = FF_DEF_ALTOV;
5993 phba->fc_arbtov = FF_DEF_ARBTOV;
5994
James Smartd7c47992010-06-08 18:31:54 -04005995 atomic_set(&phba->sdev_cnt, 0);
James Smart3de2a652007-08-02 11:09:59 -04005996 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05005997 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04005998 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05005999
6000 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05006001 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05006002 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04006003 /* Put reference to SCSI host to driver's device private data */
6004 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05006005
James Smart3772a992009-05-22 14:50:54 -04006006 return 0;
6007}
dea31012005-04-17 16:05:31 -05006008
James Smart3772a992009-05-22 14:50:54 -04006009/**
6010 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
6011 * @phba: pointer to lpfc hba data structure.
6012 *
6013 * This routine is invoked to destroy HBA physical port and the associated
6014 * SCSI host.
6015 **/
6016static void
6017lpfc_destroy_shost(struct lpfc_hba *phba)
6018{
6019 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04006020
James Smart3772a992009-05-22 14:50:54 -04006021 /* Destroy physical port that associated with the SCSI host */
6022 destroy_port(vport);
6023
6024 return;
6025}
6026
6027/**
6028 * lpfc_setup_bg - Setup Block guard structures and debug areas.
6029 * @phba: pointer to lpfc hba data structure.
6030 * @shost: the shost to be used to detect Block guard settings.
6031 *
6032 * This routine sets up the local Block guard protocol settings for @shost.
6033 * This routine also allocates memory for debugging bg buffers.
6034 **/
6035static void
6036lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
6037{
James Smartbbeb79b2012-06-12 13:54:27 -04006038 uint32_t old_mask;
6039 uint32_t old_guard;
6040
James Smart3772a992009-05-22 14:50:54 -04006041 int pagecnt = 10;
6042 if (lpfc_prot_mask && lpfc_prot_guard) {
6043 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6044 "1478 Registering BlockGuard with the "
6045 "SCSI layer\n");
James Smartbbeb79b2012-06-12 13:54:27 -04006046
6047 old_mask = lpfc_prot_mask;
6048 old_guard = lpfc_prot_guard;
6049
6050 /* Only allow supported values */
6051 lpfc_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
6052 SHOST_DIX_TYPE0_PROTECTION |
6053 SHOST_DIX_TYPE1_PROTECTION);
6054 lpfc_prot_guard &= (SHOST_DIX_GUARD_IP | SHOST_DIX_GUARD_CRC);
6055
6056 /* DIF Type 1 protection for profiles AST1/C1 is end to end */
6057 if (lpfc_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
6058 lpfc_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
6059
6060 if (lpfc_prot_mask && lpfc_prot_guard) {
6061 if ((old_mask != lpfc_prot_mask) ||
6062 (old_guard != lpfc_prot_guard))
6063 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6064 "1475 Registering BlockGuard with the "
6065 "SCSI layer: mask %d guard %d\n",
6066 lpfc_prot_mask, lpfc_prot_guard);
6067
6068 scsi_host_set_prot(shost, lpfc_prot_mask);
6069 scsi_host_set_guard(shost, lpfc_prot_guard);
6070 } else
6071 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6072 "1479 Not Registering BlockGuard with the SCSI "
6073 "layer, Bad protection parameters: %d %d\n",
6074 old_mask, old_guard);
James Smart98c9ea52007-10-27 13:37:33 -04006075 }
James Smartbbeb79b2012-06-12 13:54:27 -04006076
James Smart81301a92008-12-04 22:39:46 -05006077 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05006078 while (pagecnt) {
6079 spin_lock_init(&_dump_buf_lock);
6080 _dump_buf_data =
6081 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
6082 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04006083 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6084 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04006085 "_dump_buf_data at 0x%p\n",
6086 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05006087 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04006088 memset(_dump_buf_data, 0,
6089 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05006090 break;
James Smart3772a992009-05-22 14:50:54 -04006091 } else
James Smart81301a92008-12-04 22:39:46 -05006092 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05006093 }
James Smart81301a92008-12-04 22:39:46 -05006094 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04006095 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6096 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04006097 "memory for hexdump\n");
6098 } else
James Smart6a9c52c2009-10-02 15:16:51 -04006099 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6100 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05006101 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05006102 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05006103 while (pagecnt) {
6104 _dump_buf_dif =
6105 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
6106 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04006107 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6108 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04006109 "_dump_buf_dif at 0x%p\n",
6110 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05006111 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04006112 memset(_dump_buf_dif, 0,
6113 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05006114 break;
James Smart3772a992009-05-22 14:50:54 -04006115 } else
James Smart81301a92008-12-04 22:39:46 -05006116 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05006117 }
James Smart81301a92008-12-04 22:39:46 -05006118 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04006119 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6120 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04006121 "memory for hexdump\n");
6122 } else
James Smart6a9c52c2009-10-02 15:16:51 -04006123 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6124 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04006125 _dump_buf_dif);
6126}
6127
6128/**
6129 * lpfc_post_init_setup - Perform necessary device post initialization setup.
6130 * @phba: pointer to lpfc hba data structure.
6131 *
6132 * This routine is invoked to perform all the necessary post initialization
6133 * setup for the device.
6134 **/
6135static void
6136lpfc_post_init_setup(struct lpfc_hba *phba)
6137{
6138 struct Scsi_Host *shost;
6139 struct lpfc_adapter_event_header adapter_event;
6140
6141 /* Get the default values for Model Name and Description */
6142 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
6143
6144 /*
6145 * hba setup may have changed the hba_queue_depth so we need to
6146 * adjust the value of can_queue.
6147 */
6148 shost = pci_get_drvdata(phba->pcidev);
6149 shost->can_queue = phba->cfg_hba_queue_depth - 10;
6150 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
6151 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05006152
6153 lpfc_host_attrib_init(shost);
6154
James Smart2e0fef82007-06-17 19:56:36 -05006155 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
6156 spin_lock_irq(shost->host_lock);
6157 lpfc_poll_start_timer(phba);
6158 spin_unlock_irq(shost->host_lock);
6159 }
James Smart8f6d98d2006-08-01 07:34:00 -04006160
James Smart93996272008-08-24 21:50:30 -04006161 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6162 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04006163 /* Send board arrival event to upper layer */
6164 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
6165 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
6166 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04006167 sizeof(adapter_event),
6168 (char *) &adapter_event,
6169 LPFC_NL_VENDOR_ID);
6170 return;
6171}
6172
6173/**
6174 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
6175 * @phba: pointer to lpfc hba data structure.
6176 *
6177 * This routine is invoked to set up the PCI device memory space for device
6178 * with SLI-3 interface spec.
6179 *
6180 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006181 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006182 * other values - error
6183 **/
6184static int
6185lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
6186{
6187 struct pci_dev *pdev;
6188 unsigned long bar0map_len, bar2map_len;
6189 int i, hbq_count;
6190 void *ptr;
6191 int error = -ENODEV;
6192
6193 /* Obtain PCI device reference */
6194 if (!phba->pcidev)
6195 return error;
6196 else
6197 pdev = phba->pcidev;
6198
6199 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05006200 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
6201 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
6202 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
6203 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smart3772a992009-05-22 14:50:54 -04006204 return error;
Michael Reed8e685972009-09-18 12:02:05 -05006205 }
6206 }
James Smart3772a992009-05-22 14:50:54 -04006207
6208 /* Get the bus address of Bar0 and Bar2 and the number of bytes
6209 * required by each mapping.
6210 */
6211 phba->pci_bar0_map = pci_resource_start(pdev, 0);
6212 bar0map_len = pci_resource_len(pdev, 0);
6213
6214 phba->pci_bar2_map = pci_resource_start(pdev, 2);
6215 bar2map_len = pci_resource_len(pdev, 2);
6216
6217 /* Map HBA SLIM to a kernel virtual address. */
6218 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
6219 if (!phba->slim_memmap_p) {
6220 dev_printk(KERN_ERR, &pdev->dev,
6221 "ioremap failed for SLIM memory.\n");
6222 goto out;
6223 }
6224
6225 /* Map HBA Control Registers to a kernel virtual address. */
6226 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
6227 if (!phba->ctrl_regs_memmap_p) {
6228 dev_printk(KERN_ERR, &pdev->dev,
6229 "ioremap failed for HBA control registers.\n");
6230 goto out_iounmap_slim;
6231 }
6232
6233 /* Allocate memory for SLI-2 structures */
6234 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
6235 SLI2_SLIM_SIZE,
6236 &phba->slim2p.phys,
6237 GFP_KERNEL);
6238 if (!phba->slim2p.virt)
6239 goto out_iounmap;
6240
6241 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
6242 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04006243 phba->mbox_ext = (phba->slim2p.virt +
6244 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04006245 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
6246 phba->IOCBs = (phba->slim2p.virt +
6247 offsetof(struct lpfc_sli2_slim, IOCBs));
6248
6249 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
6250 lpfc_sli_hbq_size(),
6251 &phba->hbqslimp.phys,
6252 GFP_KERNEL);
6253 if (!phba->hbqslimp.virt)
6254 goto out_free_slim;
6255
6256 hbq_count = lpfc_sli_hbq_count();
6257 ptr = phba->hbqslimp.virt;
6258 for (i = 0; i < hbq_count; ++i) {
6259 phba->hbqs[i].hbq_virt = ptr;
6260 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
6261 ptr += (lpfc_hbq_defs[i]->entry_count *
6262 sizeof(struct lpfc_hbq_entry));
6263 }
6264 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
6265 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
6266
6267 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
6268
6269 INIT_LIST_HEAD(&phba->rb_pend_list);
6270
6271 phba->MBslimaddr = phba->slim_memmap_p;
6272 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
6273 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
6274 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
6275 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04006276
dea31012005-04-17 16:05:31 -05006277 return 0;
6278
dea31012005-04-17 16:05:31 -05006279out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04006280 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6281 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05006282out_iounmap:
6283 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05006284out_iounmap_slim:
dea31012005-04-17 16:05:31 -05006285 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05006286out:
6287 return error;
6288}
6289
James Smarte59058c2008-08-24 21:49:00 -04006290/**
James Smart3772a992009-05-22 14:50:54 -04006291 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
6292 * @phba: pointer to lpfc hba data structure.
6293 *
6294 * This routine is invoked to unset the PCI device memory space for device
6295 * with SLI-3 interface spec.
6296 **/
6297static void
6298lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
6299{
6300 struct pci_dev *pdev;
6301
6302 /* Obtain PCI device reference */
6303 if (!phba->pcidev)
6304 return;
6305 else
6306 pdev = phba->pcidev;
6307
6308 /* Free coherent DMA memory allocated */
6309 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
6310 phba->hbqslimp.virt, phba->hbqslimp.phys);
6311 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6312 phba->slim2p.virt, phba->slim2p.phys);
6313
6314 /* I/O memory unmap */
6315 iounmap(phba->ctrl_regs_memmap_p);
6316 iounmap(phba->slim_memmap_p);
6317
6318 return;
6319}
6320
6321/**
James Smartda0436e2009-05-22 14:51:39 -04006322 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
6323 * @phba: pointer to lpfc hba data structure.
6324 *
6325 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
6326 * done and check status.
6327 *
6328 * Return 0 if successful, otherwise -ENODEV.
6329 **/
6330int
6331lpfc_sli4_post_status_check(struct lpfc_hba *phba)
6332{
James Smart2fcee4b2010-12-15 17:57:46 -05006333 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
6334 struct lpfc_register reg_data;
6335 int i, port_error = 0;
6336 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04006337
James Smart9940b972011-03-11 16:06:12 -05006338 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
6339 memset(&reg_data, 0, sizeof(reg_data));
James Smart2fcee4b2010-12-15 17:57:46 -05006340 if (!phba->sli4_hba.PSMPHRregaddr)
James Smartda0436e2009-05-22 14:51:39 -04006341 return -ENODEV;
6342
James Smartda0436e2009-05-22 14:51:39 -04006343 /* Wait up to 30 seconds for the SLI Port POST done and ready */
6344 for (i = 0; i < 3000; i++) {
James Smart9940b972011-03-11 16:06:12 -05006345 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
6346 &portsmphr_reg.word0) ||
6347 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
James Smart2fcee4b2010-12-15 17:57:46 -05006348 /* Port has a fatal POST error, break out */
James Smartda0436e2009-05-22 14:51:39 -04006349 port_error = -ENODEV;
6350 break;
6351 }
James Smart2fcee4b2010-12-15 17:57:46 -05006352 if (LPFC_POST_STAGE_PORT_READY ==
6353 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
James Smartda0436e2009-05-22 14:51:39 -04006354 break;
James Smartda0436e2009-05-22 14:51:39 -04006355 msleep(10);
6356 }
6357
James Smart2fcee4b2010-12-15 17:57:46 -05006358 /*
6359 * If there was a port error during POST, then don't proceed with
6360 * other register reads as the data may not be valid. Just exit.
6361 */
6362 if (port_error) {
James Smartda0436e2009-05-22 14:51:39 -04006363 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05006364 "1408 Port Failed POST - portsmphr=0x%x, "
6365 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
6366 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
6367 portsmphr_reg.word0,
6368 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
6369 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
6370 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
6371 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
6372 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
6373 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
6374 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
6375 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
6376 } else {
James Smart28baac72010-02-12 14:42:03 -05006377 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05006378 "2534 Device Info: SLIFamily=0x%x, "
6379 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
6380 "SLIHint_2=0x%x, FT=0x%x\n",
James Smart28baac72010-02-12 14:42:03 -05006381 bf_get(lpfc_sli_intf_sli_family,
6382 &phba->sli4_hba.sli_intf),
6383 bf_get(lpfc_sli_intf_slirev,
6384 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05006385 bf_get(lpfc_sli_intf_if_type,
James Smart28baac72010-02-12 14:42:03 -05006386 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05006387 bf_get(lpfc_sli_intf_sli_hint1,
6388 &phba->sli4_hba.sli_intf),
6389 bf_get(lpfc_sli_intf_sli_hint2,
6390 &phba->sli4_hba.sli_intf),
6391 bf_get(lpfc_sli_intf_func_type,
James Smart28baac72010-02-12 14:42:03 -05006392 &phba->sli4_hba.sli_intf));
James Smart2fcee4b2010-12-15 17:57:46 -05006393 /*
6394 * Check for other Port errors during the initialization
6395 * process. Fail the load if the port did not come up
6396 * correctly.
6397 */
6398 if_type = bf_get(lpfc_sli_intf_if_type,
6399 &phba->sli4_hba.sli_intf);
6400 switch (if_type) {
6401 case LPFC_SLI_INTF_IF_TYPE_0:
6402 phba->sli4_hba.ue_mask_lo =
6403 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
6404 phba->sli4_hba.ue_mask_hi =
6405 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
6406 uerrlo_reg.word0 =
6407 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
6408 uerrhi_reg.word0 =
6409 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
6410 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
6411 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
6412 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6413 "1422 Unrecoverable Error "
6414 "Detected during POST "
6415 "uerr_lo_reg=0x%x, "
6416 "uerr_hi_reg=0x%x, "
6417 "ue_mask_lo_reg=0x%x, "
6418 "ue_mask_hi_reg=0x%x\n",
6419 uerrlo_reg.word0,
6420 uerrhi_reg.word0,
6421 phba->sli4_hba.ue_mask_lo,
6422 phba->sli4_hba.ue_mask_hi);
6423 port_error = -ENODEV;
6424 }
6425 break;
6426 case LPFC_SLI_INTF_IF_TYPE_2:
6427 /* Final checks. The port status should be clean. */
James Smart9940b972011-03-11 16:06:12 -05006428 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
6429 &reg_data.word0) ||
James Smart05580562011-05-24 11:40:48 -04006430 (bf_get(lpfc_sliport_status_err, &reg_data) &&
6431 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
James Smart2fcee4b2010-12-15 17:57:46 -05006432 phba->work_status[0] =
6433 readl(phba->sli4_hba.u.if_type2.
6434 ERR1regaddr);
6435 phba->work_status[1] =
6436 readl(phba->sli4_hba.u.if_type2.
6437 ERR2regaddr);
6438 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05006439 "2888 Unrecoverable port error "
6440 "following POST: port status reg "
6441 "0x%x, port_smphr reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05006442 "error 1=0x%x, error 2=0x%x\n",
6443 reg_data.word0,
6444 portsmphr_reg.word0,
6445 phba->work_status[0],
6446 phba->work_status[1]);
6447 port_error = -ENODEV;
6448 }
6449 break;
6450 case LPFC_SLI_INTF_IF_TYPE_1:
6451 default:
6452 break;
6453 }
James Smart28baac72010-02-12 14:42:03 -05006454 }
James Smartda0436e2009-05-22 14:51:39 -04006455 return port_error;
6456}
6457
6458/**
6459 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
6460 * @phba: pointer to lpfc hba data structure.
James Smart2fcee4b2010-12-15 17:57:46 -05006461 * @if_type: The SLI4 interface type getting configured.
James Smartda0436e2009-05-22 14:51:39 -04006462 *
6463 * This routine is invoked to set up SLI4 BAR0 PCI config space register
6464 * memory map.
6465 **/
6466static void
James Smart2fcee4b2010-12-15 17:57:46 -05006467lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04006468{
James Smart2fcee4b2010-12-15 17:57:46 -05006469 switch (if_type) {
6470 case LPFC_SLI_INTF_IF_TYPE_0:
6471 phba->sli4_hba.u.if_type0.UERRLOregaddr =
6472 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
6473 phba->sli4_hba.u.if_type0.UERRHIregaddr =
6474 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
6475 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
6476 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
6477 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
6478 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
6479 phba->sli4_hba.SLIINTFregaddr =
6480 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
6481 break;
6482 case LPFC_SLI_INTF_IF_TYPE_2:
6483 phba->sli4_hba.u.if_type2.ERR1regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006484 phba->sli4_hba.conf_regs_memmap_p +
6485 LPFC_CTL_PORT_ER1_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006486 phba->sli4_hba.u.if_type2.ERR2regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006487 phba->sli4_hba.conf_regs_memmap_p +
6488 LPFC_CTL_PORT_ER2_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006489 phba->sli4_hba.u.if_type2.CTRLregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006490 phba->sli4_hba.conf_regs_memmap_p +
6491 LPFC_CTL_PORT_CTL_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006492 phba->sli4_hba.u.if_type2.STATUSregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006493 phba->sli4_hba.conf_regs_memmap_p +
6494 LPFC_CTL_PORT_STA_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006495 phba->sli4_hba.SLIINTFregaddr =
6496 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
6497 phba->sli4_hba.PSMPHRregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006498 phba->sli4_hba.conf_regs_memmap_p +
6499 LPFC_CTL_PORT_SEM_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006500 phba->sli4_hba.RQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05006501 phba->sli4_hba.conf_regs_memmap_p +
6502 LPFC_ULP0_RQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05006503 phba->sli4_hba.WQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05006504 phba->sli4_hba.conf_regs_memmap_p +
6505 LPFC_ULP0_WQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05006506 phba->sli4_hba.EQCQDBregaddr =
6507 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
6508 phba->sli4_hba.MQDBregaddr =
6509 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
6510 phba->sli4_hba.BMBXregaddr =
6511 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
6512 break;
6513 case LPFC_SLI_INTF_IF_TYPE_1:
6514 default:
6515 dev_printk(KERN_ERR, &phba->pcidev->dev,
6516 "FATAL - unsupported SLI4 interface type - %d\n",
6517 if_type);
6518 break;
6519 }
James Smartda0436e2009-05-22 14:51:39 -04006520}
6521
6522/**
6523 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
6524 * @phba: pointer to lpfc hba data structure.
6525 *
6526 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
6527 * memory map.
6528 **/
6529static void
6530lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
6531{
James Smart2fcee4b2010-12-15 17:57:46 -05006532 phba->sli4_hba.PSMPHRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
6533 LPFC_SLIPORT_IF0_SMPHR;
James Smartda0436e2009-05-22 14:51:39 -04006534 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006535 LPFC_HST_ISR0;
James Smartda0436e2009-05-22 14:51:39 -04006536 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006537 LPFC_HST_IMR0;
James Smartda0436e2009-05-22 14:51:39 -04006538 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006539 LPFC_HST_ISCR0;
James Smartda0436e2009-05-22 14:51:39 -04006540}
6541
6542/**
6543 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
6544 * @phba: pointer to lpfc hba data structure.
6545 * @vf: virtual function number
6546 *
6547 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
6548 * based on the given viftual function number, @vf.
6549 *
6550 * Return 0 if successful, otherwise -ENODEV.
6551 **/
6552static int
6553lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
6554{
6555 if (vf > LPFC_VIR_FUNC_MAX)
6556 return -ENODEV;
6557
6558 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05006559 vf * LPFC_VFR_PAGE_SIZE +
6560 LPFC_ULP0_RQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04006561 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05006562 vf * LPFC_VFR_PAGE_SIZE +
6563 LPFC_ULP0_WQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04006564 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6565 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
6566 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6567 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
6568 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6569 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
6570 return 0;
6571}
6572
6573/**
6574 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
6575 * @phba: pointer to lpfc hba data structure.
6576 *
6577 * This routine is invoked to create the bootstrap mailbox
6578 * region consistent with the SLI-4 interface spec. This
6579 * routine allocates all memory necessary to communicate
6580 * mailbox commands to the port and sets up all alignment
6581 * needs. No locks are expected to be held when calling
6582 * this routine.
6583 *
6584 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006585 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04006586 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04006587 **/
6588static int
6589lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
6590{
6591 uint32_t bmbx_size;
6592 struct lpfc_dmabuf *dmabuf;
6593 struct dma_address *dma_address;
6594 uint32_t pa_addr;
6595 uint64_t phys_addr;
6596
6597 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
6598 if (!dmabuf)
6599 return -ENOMEM;
6600
6601 /*
6602 * The bootstrap mailbox region is comprised of 2 parts
6603 * plus an alignment restriction of 16 bytes.
6604 */
6605 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
6606 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
6607 bmbx_size,
6608 &dmabuf->phys,
6609 GFP_KERNEL);
6610 if (!dmabuf->virt) {
6611 kfree(dmabuf);
6612 return -ENOMEM;
6613 }
6614 memset(dmabuf->virt, 0, bmbx_size);
6615
6616 /*
6617 * Initialize the bootstrap mailbox pointers now so that the register
6618 * operations are simple later. The mailbox dma address is required
6619 * to be 16-byte aligned. Also align the virtual memory as each
6620 * maibox is copied into the bmbx mailbox region before issuing the
6621 * command to the port.
6622 */
6623 phba->sli4_hba.bmbx.dmabuf = dmabuf;
6624 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
6625
6626 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
6627 LPFC_ALIGN_16_BYTE);
6628 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
6629 LPFC_ALIGN_16_BYTE);
6630
6631 /*
6632 * Set the high and low physical addresses now. The SLI4 alignment
6633 * requirement is 16 bytes and the mailbox is posted to the port
6634 * as two 30-bit addresses. The other data is a bit marking whether
6635 * the 30-bit address is the high or low address.
6636 * Upcast bmbx aphys to 64bits so shift instruction compiles
6637 * clean on 32 bit machines.
6638 */
6639 dma_address = &phba->sli4_hba.bmbx.dma_address;
6640 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
6641 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
6642 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
6643 LPFC_BMBX_BIT1_ADDR_HI);
6644
6645 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
6646 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
6647 LPFC_BMBX_BIT1_ADDR_LO);
6648 return 0;
6649}
6650
6651/**
6652 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
6653 * @phba: pointer to lpfc hba data structure.
6654 *
6655 * This routine is invoked to teardown the bootstrap mailbox
6656 * region and release all host resources. This routine requires
6657 * the caller to ensure all mailbox commands recovered, no
6658 * additional mailbox comands are sent, and interrupts are disabled
6659 * before calling this routine.
6660 *
6661 **/
6662static void
6663lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
6664{
6665 dma_free_coherent(&phba->pcidev->dev,
6666 phba->sli4_hba.bmbx.bmbx_size,
6667 phba->sli4_hba.bmbx.dmabuf->virt,
6668 phba->sli4_hba.bmbx.dmabuf->phys);
6669
6670 kfree(phba->sli4_hba.bmbx.dmabuf);
6671 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
6672}
6673
6674/**
6675 * lpfc_sli4_read_config - Get the config parameters.
6676 * @phba: pointer to lpfc hba data structure.
6677 *
6678 * This routine is invoked to read the configuration parameters from the HBA.
6679 * The configuration parameters are used to set the base and maximum values
6680 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
6681 * allocation for the port.
6682 *
6683 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006684 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006685 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006686 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006687 **/
James Smartff78d8f2011-12-13 13:21:35 -05006688int
James Smartda0436e2009-05-22 14:51:39 -04006689lpfc_sli4_read_config(struct lpfc_hba *phba)
6690{
6691 LPFC_MBOXQ_t *pmb;
6692 struct lpfc_mbx_read_config *rd_config;
James Smart912e3ac2011-05-24 11:42:11 -04006693 union lpfc_sli4_cfg_shdr *shdr;
6694 uint32_t shdr_status, shdr_add_status;
6695 struct lpfc_mbx_get_func_cfg *get_func_cfg;
6696 struct lpfc_rsrc_desc_fcfcoe *desc;
James Smart8aa134a2012-08-14 14:25:29 -04006697 char *pdesc_0;
James Smart912e3ac2011-05-24 11:42:11 -04006698 uint32_t desc_count;
James Smart8aa134a2012-08-14 14:25:29 -04006699 int length, i, rc = 0, rc2;
James Smartda0436e2009-05-22 14:51:39 -04006700
6701 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6702 if (!pmb) {
6703 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6704 "2011 Unable to allocate memory for issuing "
6705 "SLI_CONFIG_SPECIAL mailbox command\n");
6706 return -ENOMEM;
6707 }
6708
6709 lpfc_read_config(phba, pmb);
6710
6711 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6712 if (rc != MBX_SUCCESS) {
6713 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6714 "2012 Mailbox failed , mbxCmd x%x "
6715 "READ_CONFIG, mbxStatus x%x\n",
6716 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6717 bf_get(lpfc_mqe_status, &pmb->u.mqe));
6718 rc = -EIO;
6719 } else {
6720 rd_config = &pmb->u.mqe.un.rd_config;
James Smartff78d8f2011-12-13 13:21:35 -05006721 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
6722 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
6723 phba->sli4_hba.lnk_info.lnk_tp =
6724 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
6725 phba->sli4_hba.lnk_info.lnk_no =
6726 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
6727 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6728 "3081 lnk_type:%d, lnk_numb:%d\n",
6729 phba->sli4_hba.lnk_info.lnk_tp,
6730 phba->sli4_hba.lnk_info.lnk_no);
6731 } else
6732 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6733 "3082 Mailbox (x%x) returned ldv:x0\n",
6734 bf_get(lpfc_mqe_command, &pmb->u.mqe));
James Smart6d368e52011-05-24 11:44:12 -04006735 phba->sli4_hba.extents_in_use =
6736 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04006737 phba->sli4_hba.max_cfg_param.max_xri =
6738 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
6739 phba->sli4_hba.max_cfg_param.xri_base =
6740 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
6741 phba->sli4_hba.max_cfg_param.max_vpi =
6742 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
6743 phba->sli4_hba.max_cfg_param.vpi_base =
6744 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
6745 phba->sli4_hba.max_cfg_param.max_rpi =
6746 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
6747 phba->sli4_hba.max_cfg_param.rpi_base =
6748 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
6749 phba->sli4_hba.max_cfg_param.max_vfi =
6750 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
6751 phba->sli4_hba.max_cfg_param.vfi_base =
6752 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
6753 phba->sli4_hba.max_cfg_param.max_fcfi =
6754 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04006755 phba->sli4_hba.max_cfg_param.max_eq =
6756 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
6757 phba->sli4_hba.max_cfg_param.max_rq =
6758 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
6759 phba->sli4_hba.max_cfg_param.max_wq =
6760 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
6761 phba->sli4_hba.max_cfg_param.max_cq =
6762 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
6763 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
6764 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
6765 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
6766 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
James Smart5ffc2662009-11-18 15:39:44 -05006767 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
6768 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04006769 phba->max_vports = phba->max_vpi;
6770 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart6d368e52011-05-24 11:44:12 -04006771 "2003 cfg params Extents? %d "
6772 "XRI(B:%d M:%d), "
James Smartda0436e2009-05-22 14:51:39 -04006773 "VPI(B:%d M:%d) "
6774 "VFI(B:%d M:%d) "
6775 "RPI(B:%d M:%d) "
James Smart6d368e52011-05-24 11:44:12 -04006776 "FCFI(Count:%d)\n",
6777 phba->sli4_hba.extents_in_use,
James Smartda0436e2009-05-22 14:51:39 -04006778 phba->sli4_hba.max_cfg_param.xri_base,
6779 phba->sli4_hba.max_cfg_param.max_xri,
6780 phba->sli4_hba.max_cfg_param.vpi_base,
6781 phba->sli4_hba.max_cfg_param.max_vpi,
6782 phba->sli4_hba.max_cfg_param.vfi_base,
6783 phba->sli4_hba.max_cfg_param.max_vfi,
6784 phba->sli4_hba.max_cfg_param.rpi_base,
6785 phba->sli4_hba.max_cfg_param.max_rpi,
James Smartda0436e2009-05-22 14:51:39 -04006786 phba->sli4_hba.max_cfg_param.max_fcfi);
6787 }
James Smart912e3ac2011-05-24 11:42:11 -04006788
6789 if (rc)
6790 goto read_cfg_out;
James Smartda0436e2009-05-22 14:51:39 -04006791
6792 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -04006793 length = phba->sli4_hba.max_cfg_param.max_xri -
6794 lpfc_sli4_get_els_iocb_cnt(phba);
6795 if (phba->cfg_hba_queue_depth > length) {
6796 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6797 "3361 HBA queue depth changed from %d to %d\n",
6798 phba->cfg_hba_queue_depth, length);
6799 phba->cfg_hba_queue_depth = length;
6800 }
James Smart912e3ac2011-05-24 11:42:11 -04006801
6802 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
6803 LPFC_SLI_INTF_IF_TYPE_2)
6804 goto read_cfg_out;
6805
6806 /* get the pf# and vf# for SLI4 if_type 2 port */
6807 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
6808 sizeof(struct lpfc_sli4_cfg_mhdr));
6809 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
6810 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
6811 length, LPFC_SLI4_MBX_EMBED);
6812
James Smart8aa134a2012-08-14 14:25:29 -04006813 rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart912e3ac2011-05-24 11:42:11 -04006814 shdr = (union lpfc_sli4_cfg_shdr *)
6815 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
6816 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6817 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart8aa134a2012-08-14 14:25:29 -04006818 if (rc2 || shdr_status || shdr_add_status) {
James Smart912e3ac2011-05-24 11:42:11 -04006819 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6820 "3026 Mailbox failed , mbxCmd x%x "
6821 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
6822 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6823 bf_get(lpfc_mqe_status, &pmb->u.mqe));
James Smart912e3ac2011-05-24 11:42:11 -04006824 goto read_cfg_out;
6825 }
6826
6827 /* search for fc_fcoe resrouce descriptor */
6828 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
6829 desc_count = get_func_cfg->func_cfg.rsrc_desc_count;
6830
James Smart8aa134a2012-08-14 14:25:29 -04006831 pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
6832 desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
6833 length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
6834 if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
6835 length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
6836 else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
6837 goto read_cfg_out;
6838
James Smart912e3ac2011-05-24 11:42:11 -04006839 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
James Smart8aa134a2012-08-14 14:25:29 -04006840 desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
James Smart912e3ac2011-05-24 11:42:11 -04006841 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
James Smart8aa134a2012-08-14 14:25:29 -04006842 bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
James Smart912e3ac2011-05-24 11:42:11 -04006843 phba->sli4_hba.iov.pf_number =
6844 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
6845 phba->sli4_hba.iov.vf_number =
6846 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
6847 break;
6848 }
6849 }
6850
6851 if (i < LPFC_RSRC_DESC_MAX_NUM)
6852 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6853 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
6854 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
6855 phba->sli4_hba.iov.vf_number);
James Smart8aa134a2012-08-14 14:25:29 -04006856 else
James Smart912e3ac2011-05-24 11:42:11 -04006857 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6858 "3028 GET_FUNCTION_CONFIG: failed to find "
6859 "Resrouce Descriptor:x%x\n",
6860 LPFC_RSRC_DESC_TYPE_FCFCOE);
James Smart912e3ac2011-05-24 11:42:11 -04006861
6862read_cfg_out:
6863 mempool_free(pmb, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04006864 return rc;
6865}
6866
6867/**
James Smart2fcee4b2010-12-15 17:57:46 -05006868 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
James Smartda0436e2009-05-22 14:51:39 -04006869 * @phba: pointer to lpfc hba data structure.
6870 *
James Smart2fcee4b2010-12-15 17:57:46 -05006871 * This routine is invoked to setup the port-side endian order when
6872 * the port if_type is 0. This routine has no function for other
6873 * if_types.
James Smartda0436e2009-05-22 14:51:39 -04006874 *
6875 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006876 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006877 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006878 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006879 **/
6880static int
6881lpfc_setup_endian_order(struct lpfc_hba *phba)
6882{
6883 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05006884 uint32_t if_type, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04006885 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
6886 HOST_ENDIAN_HIGH_WORD1};
6887
James Smart2fcee4b2010-12-15 17:57:46 -05006888 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
6889 switch (if_type) {
6890 case LPFC_SLI_INTF_IF_TYPE_0:
6891 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6892 GFP_KERNEL);
6893 if (!mboxq) {
6894 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6895 "0492 Unable to allocate memory for "
6896 "issuing SLI_CONFIG_SPECIAL mailbox "
6897 "command\n");
6898 return -ENOMEM;
6899 }
James Smartda0436e2009-05-22 14:51:39 -04006900
James Smart2fcee4b2010-12-15 17:57:46 -05006901 /*
6902 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
6903 * two words to contain special data values and no other data.
6904 */
6905 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
6906 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
6907 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6908 if (rc != MBX_SUCCESS) {
6909 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6910 "0493 SLI_CONFIG_SPECIAL mailbox "
6911 "failed with status x%x\n",
6912 rc);
6913 rc = -EIO;
6914 }
6915 mempool_free(mboxq, phba->mbox_mem_pool);
6916 break;
6917 case LPFC_SLI_INTF_IF_TYPE_2:
6918 case LPFC_SLI_INTF_IF_TYPE_1:
6919 default:
6920 break;
James Smartda0436e2009-05-22 14:51:39 -04006921 }
James Smartda0436e2009-05-22 14:51:39 -04006922 return rc;
6923}
6924
6925/**
James Smart5350d872011-10-10 21:33:49 -04006926 * lpfc_sli4_queue_verify - Verify and update EQ and CQ counts
James Smartda0436e2009-05-22 14:51:39 -04006927 * @phba: pointer to lpfc hba data structure.
6928 *
James Smart5350d872011-10-10 21:33:49 -04006929 * This routine is invoked to check the user settable queue counts for EQs and
6930 * CQs. after this routine is called the counts will be set to valid values that
6931 * adhere to the constraints of the system's interrupt vectors and the port's
6932 * queue resources.
James Smartda0436e2009-05-22 14:51:39 -04006933 *
6934 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006935 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006936 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04006937 **/
6938static int
James Smart5350d872011-10-10 21:33:49 -04006939lpfc_sli4_queue_verify(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04006940{
James Smart67d12732012-08-03 12:36:13 -04006941 int cfg_fcp_io_channel;
James Smart90695ee2012-08-14 14:25:36 -04006942 uint32_t cpu;
6943 uint32_t i = 0;
James Smart1ba981f2014-02-20 09:56:45 -05006944 int fof_vectors = phba->cfg_fof ? 1 : 0;
James Smartda0436e2009-05-22 14:51:39 -04006945
6946 /*
James Smart67d12732012-08-03 12:36:13 -04006947 * Sanity check for configured queue parameters against the run-time
James Smartda0436e2009-05-22 14:51:39 -04006948 * device parameters
6949 */
6950
James Smart67d12732012-08-03 12:36:13 -04006951 /* Sanity check on HBA EQ parameters */
6952 cfg_fcp_io_channel = phba->cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04006953
James Smart7bb03bb2013-04-17 20:19:16 -04006954 /* It doesn't make sense to have more io channels then online CPUs */
6955 for_each_present_cpu(cpu) {
6956 if (cpu_online(cpu))
6957 i++;
James Smart90695ee2012-08-14 14:25:36 -04006958 }
James Smart7bb03bb2013-04-17 20:19:16 -04006959 phba->sli4_hba.num_online_cpu = i;
James Smartb246de12013-05-31 17:03:07 -04006960 phba->sli4_hba.num_present_cpu = lpfc_present_cpu;
James Smart76fd07a2014-02-20 09:57:18 -05006961 phba->sli4_hba.curr_disp_cpu = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04006962
James Smart90695ee2012-08-14 14:25:36 -04006963 if (i < cfg_fcp_io_channel) {
James Smart82c3e9b2012-09-29 11:29:50 -04006964 lpfc_printf_log(phba,
6965 KERN_ERR, LOG_INIT,
James Smart90695ee2012-08-14 14:25:36 -04006966 "3188 Reducing IO channels to match number of "
James Smart7bb03bb2013-04-17 20:19:16 -04006967 "online CPUs: from %d to %d\n",
6968 cfg_fcp_io_channel, i);
James Smart90695ee2012-08-14 14:25:36 -04006969 cfg_fcp_io_channel = i;
6970 }
6971
James Smart1ba981f2014-02-20 09:56:45 -05006972 if (cfg_fcp_io_channel + fof_vectors >
James Smart67d12732012-08-03 12:36:13 -04006973 phba->sli4_hba.max_cfg_param.max_eq) {
James Smart82c3e9b2012-09-29 11:29:50 -04006974 if (phba->sli4_hba.max_cfg_param.max_eq <
6975 LPFC_FCP_IO_CHAN_MIN) {
James Smartda0436e2009-05-22 14:51:39 -04006976 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6977 "2574 Not enough EQs (%d) from the "
6978 "pci function for supporting FCP "
6979 "EQs (%d)\n",
6980 phba->sli4_hba.max_cfg_param.max_eq,
James Smart67d12732012-08-03 12:36:13 -04006981 phba->cfg_fcp_io_channel);
James Smartda0436e2009-05-22 14:51:39 -04006982 goto out_error;
6983 }
James Smart82c3e9b2012-09-29 11:29:50 -04006984 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6985 "2575 Reducing IO channels to match number of "
6986 "available EQs: from %d to %d\n",
6987 cfg_fcp_io_channel,
6988 phba->sli4_hba.max_cfg_param.max_eq);
James Smart1ba981f2014-02-20 09:56:45 -05006989 cfg_fcp_io_channel = phba->sli4_hba.max_cfg_param.max_eq -
6990 fof_vectors;
James Smartda0436e2009-05-22 14:51:39 -04006991 }
James Smart67d12732012-08-03 12:36:13 -04006992
James Smartda0436e2009-05-22 14:51:39 -04006993 /* The actual number of FCP event queues adopted */
James Smart67d12732012-08-03 12:36:13 -04006994 phba->cfg_fcp_io_channel = cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04006995
James Smartda0436e2009-05-22 14:51:39 -04006996 /* Get EQ depth from module parameter, fake the default for now */
6997 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
6998 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
6999
James Smart5350d872011-10-10 21:33:49 -04007000 /* Get CQ depth from module parameter, fake the default for now */
7001 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
7002 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
7003
7004 return 0;
7005out_error:
7006 return -ENOMEM;
7007}
7008
7009/**
7010 * lpfc_sli4_queue_create - Create all the SLI4 queues
7011 * @phba: pointer to lpfc hba data structure.
7012 *
7013 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
7014 * operation. For each SLI4 queue type, the parameters such as queue entry
7015 * count (queue depth) shall be taken from the module parameter. For now,
7016 * we just use some constant number as place holder.
7017 *
7018 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07007019 * 0 - successful
James Smart5350d872011-10-10 21:33:49 -04007020 * -ENOMEM - No availble memory
7021 * -EIO - The mailbox failed to complete successfully.
7022 **/
7023int
7024lpfc_sli4_queue_create(struct lpfc_hba *phba)
7025{
7026 struct lpfc_queue *qdesc;
James Smart67d12732012-08-03 12:36:13 -04007027 int idx;
James Smart5350d872011-10-10 21:33:49 -04007028
7029 /*
James Smart67d12732012-08-03 12:36:13 -04007030 * Create HBA Record arrays.
James Smart5350d872011-10-10 21:33:49 -04007031 */
James Smart67d12732012-08-03 12:36:13 -04007032 if (!phba->cfg_fcp_io_channel)
7033 return -ERANGE;
James Smart5350d872011-10-10 21:33:49 -04007034
James Smart67d12732012-08-03 12:36:13 -04007035 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
7036 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
7037 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
7038 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
7039 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
7040 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
7041
7042 phba->sli4_hba.hba_eq = kzalloc((sizeof(struct lpfc_queue *) *
7043 phba->cfg_fcp_io_channel), GFP_KERNEL);
7044 if (!phba->sli4_hba.hba_eq) {
James Smartda0436e2009-05-22 14:51:39 -04007045 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04007046 "2576 Failed allocate memory for "
7047 "fast-path EQ record array\n");
James Smartda0436e2009-05-22 14:51:39 -04007048 goto out_error;
7049 }
James Smart67d12732012-08-03 12:36:13 -04007050
7051 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
7052 phba->cfg_fcp_io_channel), GFP_KERNEL);
7053 if (!phba->sli4_hba.fcp_cq) {
7054 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7055 "2577 Failed allocate memory for fast-path "
7056 "CQ record array\n");
7057 goto out_error;
7058 }
7059
7060 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
7061 phba->cfg_fcp_io_channel), GFP_KERNEL);
7062 if (!phba->sli4_hba.fcp_wq) {
7063 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7064 "2578 Failed allocate memory for fast-path "
7065 "WQ record array\n");
7066 goto out_error;
7067 }
James Smartda0436e2009-05-22 14:51:39 -04007068
James Smart5350d872011-10-10 21:33:49 -04007069 /*
James Smart67d12732012-08-03 12:36:13 -04007070 * Since the first EQ can have multiple CQs associated with it,
7071 * this array is used to quickly see if we have a FCP fast-path
7072 * CQ match.
James Smart5350d872011-10-10 21:33:49 -04007073 */
James Smart67d12732012-08-03 12:36:13 -04007074 phba->sli4_hba.fcp_cq_map = kzalloc((sizeof(uint16_t) *
7075 phba->cfg_fcp_io_channel), GFP_KERNEL);
7076 if (!phba->sli4_hba.fcp_cq_map) {
7077 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7078 "2545 Failed allocate memory for fast-path "
7079 "CQ map\n");
7080 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007081 }
James Smart67d12732012-08-03 12:36:13 -04007082
7083 /*
7084 * Create HBA Event Queues (EQs). The cfg_fcp_io_channel specifies
7085 * how many EQs to create.
7086 */
7087 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7088
7089 /* Create EQs */
James Smartda0436e2009-05-22 14:51:39 -04007090 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
7091 phba->sli4_hba.eq_ecount);
7092 if (!qdesc) {
7093 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04007094 "0497 Failed allocate EQ (%d)\n", idx);
7095 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007096 }
James Smart67d12732012-08-03 12:36:13 -04007097 phba->sli4_hba.hba_eq[idx] = qdesc;
7098
7099 /* Create Fast Path FCP CQs */
7100 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7101 phba->sli4_hba.cq_ecount);
7102 if (!qdesc) {
7103 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7104 "0499 Failed allocate fast-path FCP "
7105 "CQ (%d)\n", idx);
7106 goto out_error;
7107 }
7108 phba->sli4_hba.fcp_cq[idx] = qdesc;
7109
7110 /* Create Fast Path FCP WQs */
7111 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
7112 phba->sli4_hba.wq_ecount);
7113 if (!qdesc) {
7114 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7115 "0503 Failed allocate fast-path FCP "
7116 "WQ (%d)\n", idx);
7117 goto out_error;
7118 }
7119 phba->sli4_hba.fcp_wq[idx] = qdesc;
James Smartda0436e2009-05-22 14:51:39 -04007120 }
7121
James Smart67d12732012-08-03 12:36:13 -04007122
James Smartda0436e2009-05-22 14:51:39 -04007123 /*
James Smart67d12732012-08-03 12:36:13 -04007124 * Create Slow Path Completion Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04007125 */
7126
James Smartda0436e2009-05-22 14:51:39 -04007127 /* Create slow-path Mailbox Command Complete Queue */
7128 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7129 phba->sli4_hba.cq_ecount);
7130 if (!qdesc) {
7131 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7132 "0500 Failed allocate slow-path mailbox CQ\n");
James Smart67d12732012-08-03 12:36:13 -04007133 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007134 }
7135 phba->sli4_hba.mbx_cq = qdesc;
7136
7137 /* Create slow-path ELS Complete Queue */
7138 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7139 phba->sli4_hba.cq_ecount);
7140 if (!qdesc) {
7141 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7142 "0501 Failed allocate slow-path ELS CQ\n");
James Smart67d12732012-08-03 12:36:13 -04007143 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007144 }
7145 phba->sli4_hba.els_cq = qdesc;
7146
James Smartda0436e2009-05-22 14:51:39 -04007147
James Smart5350d872011-10-10 21:33:49 -04007148 /*
James Smart67d12732012-08-03 12:36:13 -04007149 * Create Slow Path Work Queues (WQs)
James Smart5350d872011-10-10 21:33:49 -04007150 */
James Smartda0436e2009-05-22 14:51:39 -04007151
7152 /* Create Mailbox Command Queue */
James Smartda0436e2009-05-22 14:51:39 -04007153
7154 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
7155 phba->sli4_hba.mq_ecount);
7156 if (!qdesc) {
7157 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7158 "0505 Failed allocate slow-path MQ\n");
James Smart67d12732012-08-03 12:36:13 -04007159 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007160 }
7161 phba->sli4_hba.mbx_wq = qdesc;
7162
7163 /*
James Smart67d12732012-08-03 12:36:13 -04007164 * Create ELS Work Queues
James Smartda0436e2009-05-22 14:51:39 -04007165 */
James Smartda0436e2009-05-22 14:51:39 -04007166
7167 /* Create slow-path ELS Work Queue */
7168 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
7169 phba->sli4_hba.wq_ecount);
7170 if (!qdesc) {
7171 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7172 "0504 Failed allocate slow-path ELS WQ\n");
James Smart67d12732012-08-03 12:36:13 -04007173 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007174 }
7175 phba->sli4_hba.els_wq = qdesc;
7176
James Smartda0436e2009-05-22 14:51:39 -04007177 /*
7178 * Create Receive Queue (RQ)
7179 */
James Smartda0436e2009-05-22 14:51:39 -04007180
7181 /* Create Receive Queue for header */
7182 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
7183 phba->sli4_hba.rq_ecount);
7184 if (!qdesc) {
7185 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7186 "0506 Failed allocate receive HRQ\n");
James Smart67d12732012-08-03 12:36:13 -04007187 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007188 }
7189 phba->sli4_hba.hdr_rq = qdesc;
7190
7191 /* Create Receive Queue for data */
7192 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
7193 phba->sli4_hba.rq_ecount);
7194 if (!qdesc) {
7195 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7196 "0507 Failed allocate receive DRQ\n");
James Smart67d12732012-08-03 12:36:13 -04007197 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007198 }
7199 phba->sli4_hba.dat_rq = qdesc;
7200
James Smart1ba981f2014-02-20 09:56:45 -05007201 /* Create the Queues needed for Flash Optimized Fabric operations */
7202 if (phba->cfg_fof)
7203 lpfc_fof_queue_create(phba);
James Smartda0436e2009-05-22 14:51:39 -04007204 return 0;
7205
James Smartda0436e2009-05-22 14:51:39 -04007206out_error:
James Smart67d12732012-08-03 12:36:13 -04007207 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007208 return -ENOMEM;
7209}
7210
7211/**
7212 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
7213 * @phba: pointer to lpfc hba data structure.
7214 *
7215 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
7216 * operation.
7217 *
7218 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007219 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007220 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007221 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007222 **/
James Smart5350d872011-10-10 21:33:49 -04007223void
James Smartda0436e2009-05-22 14:51:39 -04007224lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
7225{
James Smart67d12732012-08-03 12:36:13 -04007226 int idx;
7227
James Smart1ba981f2014-02-20 09:56:45 -05007228 if (phba->cfg_fof)
7229 lpfc_fof_queue_destroy(phba);
7230
James Smart67d12732012-08-03 12:36:13 -04007231 if (phba->sli4_hba.hba_eq != NULL) {
7232 /* Release HBA event queue */
7233 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7234 if (phba->sli4_hba.hba_eq[idx] != NULL) {
7235 lpfc_sli4_queue_free(
7236 phba->sli4_hba.hba_eq[idx]);
7237 phba->sli4_hba.hba_eq[idx] = NULL;
7238 }
7239 }
7240 kfree(phba->sli4_hba.hba_eq);
7241 phba->sli4_hba.hba_eq = NULL;
7242 }
7243
7244 if (phba->sli4_hba.fcp_cq != NULL) {
7245 /* Release FCP completion queue */
7246 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7247 if (phba->sli4_hba.fcp_cq[idx] != NULL) {
7248 lpfc_sli4_queue_free(
7249 phba->sli4_hba.fcp_cq[idx]);
7250 phba->sli4_hba.fcp_cq[idx] = NULL;
7251 }
7252 }
7253 kfree(phba->sli4_hba.fcp_cq);
7254 phba->sli4_hba.fcp_cq = NULL;
7255 }
7256
7257 if (phba->sli4_hba.fcp_wq != NULL) {
7258 /* Release FCP work queue */
7259 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7260 if (phba->sli4_hba.fcp_wq[idx] != NULL) {
7261 lpfc_sli4_queue_free(
7262 phba->sli4_hba.fcp_wq[idx]);
7263 phba->sli4_hba.fcp_wq[idx] = NULL;
7264 }
7265 }
7266 kfree(phba->sli4_hba.fcp_wq);
7267 phba->sli4_hba.fcp_wq = NULL;
7268 }
7269
7270 /* Release FCP CQ mapping array */
7271 if (phba->sli4_hba.fcp_cq_map != NULL) {
7272 kfree(phba->sli4_hba.fcp_cq_map);
7273 phba->sli4_hba.fcp_cq_map = NULL;
7274 }
James Smartda0436e2009-05-22 14:51:39 -04007275
7276 /* Release mailbox command work queue */
James Smart67d12732012-08-03 12:36:13 -04007277 if (phba->sli4_hba.mbx_wq != NULL) {
7278 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
7279 phba->sli4_hba.mbx_wq = NULL;
7280 }
James Smartda0436e2009-05-22 14:51:39 -04007281
7282 /* Release ELS work queue */
James Smart67d12732012-08-03 12:36:13 -04007283 if (phba->sli4_hba.els_wq != NULL) {
7284 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
7285 phba->sli4_hba.els_wq = NULL;
7286 }
James Smartda0436e2009-05-22 14:51:39 -04007287
7288 /* Release unsolicited receive queue */
James Smart67d12732012-08-03 12:36:13 -04007289 if (phba->sli4_hba.hdr_rq != NULL) {
7290 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
7291 phba->sli4_hba.hdr_rq = NULL;
7292 }
7293 if (phba->sli4_hba.dat_rq != NULL) {
7294 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
7295 phba->sli4_hba.dat_rq = NULL;
7296 }
James Smartda0436e2009-05-22 14:51:39 -04007297
James Smartda0436e2009-05-22 14:51:39 -04007298 /* Release ELS complete queue */
James Smart67d12732012-08-03 12:36:13 -04007299 if (phba->sli4_hba.els_cq != NULL) {
7300 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
7301 phba->sli4_hba.els_cq = NULL;
7302 }
James Smartda0436e2009-05-22 14:51:39 -04007303
7304 /* Release mailbox command complete queue */
James Smart67d12732012-08-03 12:36:13 -04007305 if (phba->sli4_hba.mbx_cq != NULL) {
7306 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
7307 phba->sli4_hba.mbx_cq = NULL;
7308 }
James Smartda0436e2009-05-22 14:51:39 -04007309
7310 return;
7311}
7312
7313/**
7314 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
7315 * @phba: pointer to lpfc hba data structure.
7316 *
7317 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
7318 * operation.
7319 *
7320 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007321 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007322 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007323 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007324 **/
7325int
7326lpfc_sli4_queue_setup(struct lpfc_hba *phba)
7327{
James Smart2a76a282012-08-03 12:35:54 -04007328 struct lpfc_sli *psli = &phba->sli;
7329 struct lpfc_sli_ring *pring;
James Smartda0436e2009-05-22 14:51:39 -04007330 int rc = -ENOMEM;
7331 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
7332 int fcp_cq_index = 0;
James Smart962bc512013-01-03 15:44:00 -05007333 uint32_t shdr_status, shdr_add_status;
7334 union lpfc_sli4_cfg_shdr *shdr;
7335 LPFC_MBOXQ_t *mboxq;
7336 uint32_t length;
7337
7338 /* Check for dual-ULP support */
7339 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7340 if (!mboxq) {
7341 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7342 "3249 Unable to allocate memory for "
7343 "QUERY_FW_CFG mailbox command\n");
7344 return -ENOMEM;
7345 }
7346 length = (sizeof(struct lpfc_mbx_query_fw_config) -
7347 sizeof(struct lpfc_sli4_cfg_mhdr));
7348 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7349 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
7350 length, LPFC_SLI4_MBX_EMBED);
7351
7352 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7353
7354 shdr = (union lpfc_sli4_cfg_shdr *)
7355 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7356 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7357 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7358 if (shdr_status || shdr_add_status || rc) {
7359 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7360 "3250 QUERY_FW_CFG mailbox failed with status "
7361 "x%x add_status x%x, mbx status x%x\n",
7362 shdr_status, shdr_add_status, rc);
7363 if (rc != MBX_TIMEOUT)
7364 mempool_free(mboxq, phba->mbox_mem_pool);
7365 rc = -ENXIO;
7366 goto out_error;
7367 }
7368
7369 phba->sli4_hba.fw_func_mode =
7370 mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
7371 phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
7372 phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
7373 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7374 "3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
7375 "ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
7376 phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
7377
7378 if (rc != MBX_TIMEOUT)
7379 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04007380
7381 /*
James Smart67d12732012-08-03 12:36:13 -04007382 * Set up HBA Event Queues (EQs)
James Smartda0436e2009-05-22 14:51:39 -04007383 */
7384
James Smart67d12732012-08-03 12:36:13 -04007385 /* Set up HBA event queue */
7386 if (phba->cfg_fcp_io_channel && !phba->sli4_hba.hba_eq) {
James Smart2e90f4b2011-12-13 13:22:37 -05007387 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7388 "3147 Fast-path EQs not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007389 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007390 goto out_error;
James Smart2e90f4b2011-12-13 13:22:37 -05007391 }
James Smart67d12732012-08-03 12:36:13 -04007392 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
7393 if (!phba->sli4_hba.hba_eq[fcp_eqidx]) {
James Smartda0436e2009-05-22 14:51:39 -04007394 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7395 "0522 Fast-path EQ (%d) not "
7396 "allocated\n", fcp_eqidx);
James Smart1b511972011-12-13 13:23:09 -05007397 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007398 goto out_destroy_hba_eq;
James Smartda0436e2009-05-22 14:51:39 -04007399 }
James Smart67d12732012-08-03 12:36:13 -04007400 rc = lpfc_eq_create(phba, phba->sli4_hba.hba_eq[fcp_eqidx],
James Smartbf8dae82012-08-03 12:36:24 -04007401 (phba->cfg_fcp_imax / phba->cfg_fcp_io_channel));
James Smartda0436e2009-05-22 14:51:39 -04007402 if (rc) {
7403 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7404 "0523 Failed setup of fast-path EQ "
7405 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
James Smart67d12732012-08-03 12:36:13 -04007406 goto out_destroy_hba_eq;
James Smartda0436e2009-05-22 14:51:39 -04007407 }
7408 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04007409 "2584 HBA EQ setup: "
James Smartda0436e2009-05-22 14:51:39 -04007410 "queue[%d]-id=%d\n", fcp_eqidx,
James Smart67d12732012-08-03 12:36:13 -04007411 phba->sli4_hba.hba_eq[fcp_eqidx]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007412 }
7413
James Smart67d12732012-08-03 12:36:13 -04007414 /* Set up fast-path FCP Response Complete Queue */
7415 if (!phba->sli4_hba.fcp_cq) {
7416 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7417 "3148 Fast-path FCP CQ array not "
7418 "allocated\n");
7419 rc = -ENOMEM;
7420 goto out_destroy_hba_eq;
7421 }
7422
7423 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_io_channel; fcp_cqidx++) {
7424 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
7425 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7426 "0526 Fast-path FCP CQ (%d) not "
7427 "allocated\n", fcp_cqidx);
7428 rc = -ENOMEM;
7429 goto out_destroy_fcp_cq;
7430 }
7431 rc = lpfc_cq_create(phba, phba->sli4_hba.fcp_cq[fcp_cqidx],
7432 phba->sli4_hba.hba_eq[fcp_cqidx], LPFC_WCQ, LPFC_FCP);
7433 if (rc) {
7434 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7435 "0527 Failed setup of fast-path FCP "
7436 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
7437 goto out_destroy_fcp_cq;
7438 }
7439
7440 /* Setup fcp_cq_map for fast lookup */
7441 phba->sli4_hba.fcp_cq_map[fcp_cqidx] =
7442 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id;
7443
7444 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7445 "2588 FCP CQ setup: cq[%d]-id=%d, "
7446 "parent seq[%d]-id=%d\n",
7447 fcp_cqidx,
7448 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
7449 fcp_cqidx,
7450 phba->sli4_hba.hba_eq[fcp_cqidx]->queue_id);
7451 }
7452
7453 /* Set up fast-path FCP Work Queue */
7454 if (!phba->sli4_hba.fcp_wq) {
7455 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7456 "3149 Fast-path FCP WQ array not "
7457 "allocated\n");
7458 rc = -ENOMEM;
7459 goto out_destroy_fcp_cq;
7460 }
7461
7462 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_io_channel; fcp_wqidx++) {
7463 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
7464 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7465 "0534 Fast-path FCP WQ (%d) not "
7466 "allocated\n", fcp_wqidx);
7467 rc = -ENOMEM;
7468 goto out_destroy_fcp_wq;
7469 }
7470 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
7471 phba->sli4_hba.fcp_cq[fcp_wqidx],
7472 LPFC_FCP);
7473 if (rc) {
7474 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7475 "0535 Failed setup of fast-path FCP "
7476 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
7477 goto out_destroy_fcp_wq;
7478 }
7479
7480 /* Bind this WQ to the next FCP ring */
7481 pring = &psli->ring[MAX_SLI3_CONFIGURED_RINGS + fcp_wqidx];
7482 pring->sli.sli4.wqp = (void *)phba->sli4_hba.fcp_wq[fcp_wqidx];
7483 phba->sli4_hba.fcp_cq[fcp_wqidx]->pring = pring;
7484
7485 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7486 "2591 FCP WQ setup: wq[%d]-id=%d, "
7487 "parent cq[%d]-id=%d\n",
7488 fcp_wqidx,
7489 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
7490 fcp_cq_index,
7491 phba->sli4_hba.fcp_cq[fcp_wqidx]->queue_id);
7492 }
James Smartda0436e2009-05-22 14:51:39 -04007493 /*
7494 * Set up Complete Queues (CQs)
7495 */
7496
7497 /* Set up slow-path MBOX Complete Queue as the first CQ */
7498 if (!phba->sli4_hba.mbx_cq) {
7499 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7500 "0528 Mailbox CQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007501 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007502 goto out_destroy_fcp_wq;
James Smartda0436e2009-05-22 14:51:39 -04007503 }
James Smart67d12732012-08-03 12:36:13 -04007504 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq,
7505 phba->sli4_hba.hba_eq[0], LPFC_MCQ, LPFC_MBOX);
James Smartda0436e2009-05-22 14:51:39 -04007506 if (rc) {
7507 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7508 "0529 Failed setup of slow-path mailbox CQ: "
7509 "rc = 0x%x\n", rc);
James Smart67d12732012-08-03 12:36:13 -04007510 goto out_destroy_fcp_wq;
James Smartda0436e2009-05-22 14:51:39 -04007511 }
7512 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7513 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
7514 phba->sli4_hba.mbx_cq->queue_id,
James Smart67d12732012-08-03 12:36:13 -04007515 phba->sli4_hba.hba_eq[0]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007516
7517 /* Set up slow-path ELS Complete Queue */
7518 if (!phba->sli4_hba.els_cq) {
7519 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7520 "0530 ELS CQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007521 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04007522 goto out_destroy_mbx_cq;
7523 }
James Smart67d12732012-08-03 12:36:13 -04007524 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq,
7525 phba->sli4_hba.hba_eq[0], LPFC_WCQ, LPFC_ELS);
James Smartda0436e2009-05-22 14:51:39 -04007526 if (rc) {
7527 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7528 "0531 Failed setup of slow-path ELS CQ: "
7529 "rc = 0x%x\n", rc);
7530 goto out_destroy_mbx_cq;
7531 }
7532 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7533 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
7534 phba->sli4_hba.els_cq->queue_id,
James Smart67d12732012-08-03 12:36:13 -04007535 phba->sli4_hba.hba_eq[0]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007536
7537 /*
7538 * Set up all the Work Queues (WQs)
7539 */
7540
7541 /* Set up Mailbox Command Queue */
7542 if (!phba->sli4_hba.mbx_wq) {
7543 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7544 "0538 Slow-path MQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007545 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007546 goto out_destroy_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04007547 }
7548 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
7549 phba->sli4_hba.mbx_cq, LPFC_MBOX);
7550 if (rc) {
7551 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7552 "0539 Failed setup of slow-path MQ: "
7553 "rc = 0x%x\n", rc);
James Smart67d12732012-08-03 12:36:13 -04007554 goto out_destroy_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04007555 }
7556 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7557 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
7558 phba->sli4_hba.mbx_wq->queue_id,
7559 phba->sli4_hba.mbx_cq->queue_id);
7560
7561 /* Set up slow-path ELS Work Queue */
7562 if (!phba->sli4_hba.els_wq) {
7563 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7564 "0536 Slow-path ELS WQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007565 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04007566 goto out_destroy_mbx_wq;
7567 }
7568 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
7569 phba->sli4_hba.els_cq, LPFC_ELS);
7570 if (rc) {
7571 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7572 "0537 Failed setup of slow-path ELS WQ: "
7573 "rc = 0x%x\n", rc);
7574 goto out_destroy_mbx_wq;
7575 }
James Smart2a76a282012-08-03 12:35:54 -04007576
7577 /* Bind this WQ to the ELS ring */
7578 pring = &psli->ring[LPFC_ELS_RING];
7579 pring->sli.sli4.wqp = (void *)phba->sli4_hba.els_wq;
7580 phba->sli4_hba.els_cq->pring = pring;
7581
James Smartda0436e2009-05-22 14:51:39 -04007582 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7583 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
7584 phba->sli4_hba.els_wq->queue_id,
7585 phba->sli4_hba.els_cq->queue_id);
7586
James Smartda0436e2009-05-22 14:51:39 -04007587 /*
7588 * Create Receive Queue (RQ)
7589 */
7590 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
7591 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7592 "0540 Receive Queue not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007593 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007594 goto out_destroy_els_wq;
James Smartda0436e2009-05-22 14:51:39 -04007595 }
James Smart73d91e52011-10-10 21:32:10 -04007596
7597 lpfc_rq_adjust_repost(phba, phba->sli4_hba.hdr_rq, LPFC_ELS_HBQ);
7598 lpfc_rq_adjust_repost(phba, phba->sli4_hba.dat_rq, LPFC_ELS_HBQ);
7599
James Smartda0436e2009-05-22 14:51:39 -04007600 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04007601 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04007602 if (rc) {
7603 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7604 "0541 Failed setup of Receive Queue: "
7605 "rc = 0x%x\n", rc);
7606 goto out_destroy_fcp_wq;
7607 }
James Smart73d91e52011-10-10 21:32:10 -04007608
James Smartda0436e2009-05-22 14:51:39 -04007609 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7610 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
7611 "parent cq-id=%d\n",
7612 phba->sli4_hba.hdr_rq->queue_id,
7613 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04007614 phba->sli4_hba.els_cq->queue_id);
James Smart1ba981f2014-02-20 09:56:45 -05007615
7616 if (phba->cfg_fof) {
7617 rc = lpfc_fof_queue_setup(phba);
7618 if (rc) {
7619 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7620 "0549 Failed setup of FOF Queues: "
7621 "rc = 0x%x\n", rc);
7622 goto out_destroy_els_rq;
7623 }
7624 }
James Smartda0436e2009-05-22 14:51:39 -04007625 return 0;
7626
James Smart1ba981f2014-02-20 09:56:45 -05007627out_destroy_els_rq:
7628 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
James Smart2e90f4b2011-12-13 13:22:37 -05007629out_destroy_els_wq:
James Smartda0436e2009-05-22 14:51:39 -04007630 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7631out_destroy_mbx_wq:
7632 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
James Smart2e90f4b2011-12-13 13:22:37 -05007633out_destroy_els_cq:
James Smartda0436e2009-05-22 14:51:39 -04007634 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
7635out_destroy_mbx_cq:
7636 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
James Smart67d12732012-08-03 12:36:13 -04007637out_destroy_fcp_wq:
7638 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
7639 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
7640out_destroy_fcp_cq:
7641 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
7642 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
7643out_destroy_hba_eq:
James Smartda0436e2009-05-22 14:51:39 -04007644 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
James Smart67d12732012-08-03 12:36:13 -04007645 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[fcp_eqidx]);
James Smartda0436e2009-05-22 14:51:39 -04007646out_error:
7647 return rc;
7648}
7649
7650/**
7651 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
7652 * @phba: pointer to lpfc hba data structure.
7653 *
7654 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
7655 * operation.
7656 *
7657 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007658 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007659 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007660 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007661 **/
7662void
7663lpfc_sli4_queue_unset(struct lpfc_hba *phba)
7664{
7665 int fcp_qidx;
7666
James Smart1ba981f2014-02-20 09:56:45 -05007667 /* Unset the queues created for Flash Optimized Fabric operations */
7668 if (phba->cfg_fof)
7669 lpfc_fof_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007670 /* Unset mailbox command work queue */
7671 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
7672 /* Unset ELS work queue */
7673 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7674 /* Unset unsolicited receive queue */
7675 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
7676 /* Unset FCP work queue */
James Smart67d12732012-08-03 12:36:13 -04007677 if (phba->sli4_hba.fcp_wq) {
7678 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
7679 fcp_qidx++)
7680 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
7681 }
James Smartda0436e2009-05-22 14:51:39 -04007682 /* Unset mailbox command complete queue */
7683 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
7684 /* Unset ELS complete queue */
7685 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
James Smartda0436e2009-05-22 14:51:39 -04007686 /* Unset FCP response complete queue */
James Smart2e90f4b2011-12-13 13:22:37 -05007687 if (phba->sli4_hba.fcp_cq) {
James Smart67d12732012-08-03 12:36:13 -04007688 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
7689 fcp_qidx++)
James Smart2e90f4b2011-12-13 13:22:37 -05007690 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05007691 }
James Smartda0436e2009-05-22 14:51:39 -04007692 /* Unset fast-path event queue */
James Smart67d12732012-08-03 12:36:13 -04007693 if (phba->sli4_hba.hba_eq) {
7694 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
James Smart2e90f4b2011-12-13 13:22:37 -05007695 fcp_qidx++)
James Smart67d12732012-08-03 12:36:13 -04007696 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[fcp_qidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05007697 }
James Smartda0436e2009-05-22 14:51:39 -04007698}
7699
7700/**
7701 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
7702 * @phba: pointer to lpfc hba data structure.
7703 *
7704 * This routine is invoked to allocate and set up a pool of completion queue
7705 * events. The body of the completion queue event is a completion queue entry
7706 * CQE. For now, this pool is used for the interrupt service routine to queue
7707 * the following HBA completion queue events for the worker thread to process:
7708 * - Mailbox asynchronous events
7709 * - Receive queue completion unsolicited events
7710 * Later, this can be used for all the slow-path events.
7711 *
7712 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007713 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007714 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04007715 **/
7716static int
7717lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
7718{
7719 struct lpfc_cq_event *cq_event;
7720 int i;
7721
7722 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
7723 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
7724 if (!cq_event)
7725 goto out_pool_create_fail;
7726 list_add_tail(&cq_event->list,
7727 &phba->sli4_hba.sp_cqe_event_pool);
7728 }
7729 return 0;
7730
7731out_pool_create_fail:
7732 lpfc_sli4_cq_event_pool_destroy(phba);
7733 return -ENOMEM;
7734}
7735
7736/**
7737 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
7738 * @phba: pointer to lpfc hba data structure.
7739 *
7740 * This routine is invoked to free the pool of completion queue events at
7741 * driver unload time. Note that, it is the responsibility of the driver
7742 * cleanup routine to free all the outstanding completion-queue events
7743 * allocated from this pool back into the pool before invoking this routine
7744 * to destroy the pool.
7745 **/
7746static void
7747lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
7748{
7749 struct lpfc_cq_event *cq_event, *next_cq_event;
7750
7751 list_for_each_entry_safe(cq_event, next_cq_event,
7752 &phba->sli4_hba.sp_cqe_event_pool, list) {
7753 list_del(&cq_event->list);
7754 kfree(cq_event);
7755 }
7756}
7757
7758/**
7759 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
7760 * @phba: pointer to lpfc hba data structure.
7761 *
7762 * This routine is the lock free version of the API invoked to allocate a
7763 * completion-queue event from the free pool.
7764 *
7765 * Return: Pointer to the newly allocated completion-queue event if successful
7766 * NULL otherwise.
7767 **/
7768struct lpfc_cq_event *
7769__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
7770{
7771 struct lpfc_cq_event *cq_event = NULL;
7772
7773 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
7774 struct lpfc_cq_event, list);
7775 return cq_event;
7776}
7777
7778/**
7779 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
7780 * @phba: pointer to lpfc hba data structure.
7781 *
7782 * This routine is the lock version of the API invoked to allocate a
7783 * completion-queue event from the free pool.
7784 *
7785 * Return: Pointer to the newly allocated completion-queue event if successful
7786 * NULL otherwise.
7787 **/
7788struct lpfc_cq_event *
7789lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
7790{
7791 struct lpfc_cq_event *cq_event;
7792 unsigned long iflags;
7793
7794 spin_lock_irqsave(&phba->hbalock, iflags);
7795 cq_event = __lpfc_sli4_cq_event_alloc(phba);
7796 spin_unlock_irqrestore(&phba->hbalock, iflags);
7797 return cq_event;
7798}
7799
7800/**
7801 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
7802 * @phba: pointer to lpfc hba data structure.
7803 * @cq_event: pointer to the completion queue event to be freed.
7804 *
7805 * This routine is the lock free version of the API invoked to release a
7806 * completion-queue event back into the free pool.
7807 **/
7808void
7809__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
7810 struct lpfc_cq_event *cq_event)
7811{
7812 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
7813}
7814
7815/**
7816 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
7817 * @phba: pointer to lpfc hba data structure.
7818 * @cq_event: pointer to the completion queue event to be freed.
7819 *
7820 * This routine is the lock version of the API invoked to release a
7821 * completion-queue event back into the free pool.
7822 **/
7823void
7824lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
7825 struct lpfc_cq_event *cq_event)
7826{
7827 unsigned long iflags;
7828 spin_lock_irqsave(&phba->hbalock, iflags);
7829 __lpfc_sli4_cq_event_release(phba, cq_event);
7830 spin_unlock_irqrestore(&phba->hbalock, iflags);
7831}
7832
7833/**
7834 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
7835 * @phba: pointer to lpfc hba data structure.
7836 *
7837 * This routine is to free all the pending completion-queue events to the
7838 * back into the free pool for device reset.
7839 **/
7840static void
7841lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
7842{
7843 LIST_HEAD(cqelist);
7844 struct lpfc_cq_event *cqe;
7845 unsigned long iflags;
7846
7847 /* Retrieve all the pending WCQEs from pending WCQE lists */
7848 spin_lock_irqsave(&phba->hbalock, iflags);
7849 /* Pending FCP XRI abort events */
7850 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
7851 &cqelist);
7852 /* Pending ELS XRI abort events */
7853 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
7854 &cqelist);
7855 /* Pending asynnc events */
7856 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
7857 &cqelist);
7858 spin_unlock_irqrestore(&phba->hbalock, iflags);
7859
7860 while (!list_empty(&cqelist)) {
7861 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
7862 lpfc_sli4_cq_event_release(phba, cqe);
7863 }
7864}
7865
7866/**
7867 * lpfc_pci_function_reset - Reset pci function.
7868 * @phba: pointer to lpfc hba data structure.
7869 *
7870 * This routine is invoked to request a PCI function reset. It will destroys
7871 * all resources assigned to the PCI function which originates this request.
7872 *
7873 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007874 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007875 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007876 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007877 **/
7878int
7879lpfc_pci_function_reset(struct lpfc_hba *phba)
7880{
7881 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05007882 uint32_t rc = 0, if_type;
James Smartda0436e2009-05-22 14:51:39 -04007883 uint32_t shdr_status, shdr_add_status;
James Smart2fcee4b2010-12-15 17:57:46 -05007884 uint32_t rdy_chk, num_resets = 0, reset_again = 0;
James Smartda0436e2009-05-22 14:51:39 -04007885 union lpfc_sli4_cfg_shdr *shdr;
James Smart2fcee4b2010-12-15 17:57:46 -05007886 struct lpfc_register reg_data;
James Smart2b81f942012-03-01 22:37:18 -05007887 uint16_t devid;
James Smartda0436e2009-05-22 14:51:39 -04007888
James Smart2fcee4b2010-12-15 17:57:46 -05007889 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7890 switch (if_type) {
7891 case LPFC_SLI_INTF_IF_TYPE_0:
7892 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
7893 GFP_KERNEL);
7894 if (!mboxq) {
7895 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7896 "0494 Unable to allocate memory for "
7897 "issuing SLI_FUNCTION_RESET mailbox "
7898 "command\n");
7899 return -ENOMEM;
7900 }
7901
7902 /* Setup PCI function reset mailbox-ioctl command */
7903 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7904 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
7905 LPFC_SLI4_MBX_EMBED);
7906 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7907 shdr = (union lpfc_sli4_cfg_shdr *)
7908 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7909 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7910 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
7911 &shdr->response);
7912 if (rc != MBX_TIMEOUT)
7913 mempool_free(mboxq, phba->mbox_mem_pool);
7914 if (shdr_status || shdr_add_status || rc) {
7915 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7916 "0495 SLI_FUNCTION_RESET mailbox "
7917 "failed with status x%x add_status x%x,"
7918 " mbx status x%x\n",
7919 shdr_status, shdr_add_status, rc);
7920 rc = -ENXIO;
7921 }
7922 break;
7923 case LPFC_SLI_INTF_IF_TYPE_2:
7924 for (num_resets = 0;
7925 num_resets < MAX_IF_TYPE_2_RESETS;
7926 num_resets++) {
7927 reg_data.word0 = 0;
7928 bf_set(lpfc_sliport_ctrl_end, &reg_data,
7929 LPFC_SLIPORT_LITTLE_ENDIAN);
7930 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
7931 LPFC_SLIPORT_INIT_PORT);
7932 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
7933 CTRLregaddr);
James Smart8fcb8ac2012-03-01 22:35:58 -05007934 /* flush */
James Smart2b81f942012-03-01 22:37:18 -05007935 pci_read_config_word(phba->pcidev,
7936 PCI_DEVICE_ID, &devid);
James Smart2fcee4b2010-12-15 17:57:46 -05007937 /*
7938 * Poll the Port Status Register and wait for RDY for
7939 * up to 10 seconds. If the port doesn't respond, treat
7940 * it as an error. If the port responds with RN, start
7941 * the loop again.
7942 */
7943 for (rdy_chk = 0; rdy_chk < 1000; rdy_chk++) {
James Smart73d91e52011-10-10 21:32:10 -04007944 msleep(10);
James Smart9940b972011-03-11 16:06:12 -05007945 if (lpfc_readl(phba->sli4_hba.u.if_type2.
7946 STATUSregaddr, &reg_data.word0)) {
7947 rc = -ENODEV;
James Smart73d91e52011-10-10 21:32:10 -04007948 goto out;
James Smart9940b972011-03-11 16:06:12 -05007949 }
James Smart6b5151f2012-01-18 16:24:06 -05007950 if (bf_get(lpfc_sliport_status_rn, &reg_data))
7951 reset_again++;
James Smart2fcee4b2010-12-15 17:57:46 -05007952 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
7953 break;
James Smart2fcee4b2010-12-15 17:57:46 -05007954 }
7955
7956 /*
7957 * If the port responds to the init request with
7958 * reset needed, delay for a bit and restart the loop.
7959 */
James Smart6b5151f2012-01-18 16:24:06 -05007960 if (reset_again && (rdy_chk < 1000)) {
James Smart2fcee4b2010-12-15 17:57:46 -05007961 msleep(10);
7962 reset_again = 0;
7963 continue;
7964 }
7965
7966 /* Detect any port errors. */
James Smart2fcee4b2010-12-15 17:57:46 -05007967 if ((bf_get(lpfc_sliport_status_err, &reg_data)) ||
7968 (rdy_chk >= 1000)) {
7969 phba->work_status[0] = readl(
7970 phba->sli4_hba.u.if_type2.ERR1regaddr);
7971 phba->work_status[1] = readl(
7972 phba->sli4_hba.u.if_type2.ERR2regaddr);
7973 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05007974 "2890 Port error detected during port "
James Smart8a9d2e82012-05-09 21:16:12 -04007975 "reset(%d): wait_tmo:%d ms, "
7976 "port status reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05007977 "error 1=0x%x, error 2=0x%x\n",
James Smart8a9d2e82012-05-09 21:16:12 -04007978 num_resets, rdy_chk*10,
7979 reg_data.word0,
James Smart2fcee4b2010-12-15 17:57:46 -05007980 phba->work_status[0],
7981 phba->work_status[1]);
7982 rc = -ENODEV;
7983 }
7984
7985 /*
7986 * Terminate the outer loop provided the Port indicated
7987 * ready within 10 seconds.
7988 */
7989 if (rdy_chk < 1000)
7990 break;
7991 }
James Smart05580562011-05-24 11:40:48 -04007992 /* delay driver action following IF_TYPE_2 function reset */
7993 msleep(100);
James Smart2fcee4b2010-12-15 17:57:46 -05007994 break;
7995 case LPFC_SLI_INTF_IF_TYPE_1:
7996 default:
7997 break;
James Smartda0436e2009-05-22 14:51:39 -04007998 }
7999
James Smart73d91e52011-10-10 21:32:10 -04008000out:
James Smart2fcee4b2010-12-15 17:57:46 -05008001 /* Catch the not-ready port failure after a port reset. */
James Smart229adb02013-04-17 20:16:51 -04008002 if (num_resets >= MAX_IF_TYPE_2_RESETS) {
8003 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8004 "3317 HBA not functional: IP Reset Failed "
8005 "after (%d) retries, try: "
8006 "echo fw_reset > board_mode\n", num_resets);
James Smart2fcee4b2010-12-15 17:57:46 -05008007 rc = -ENODEV;
James Smart229adb02013-04-17 20:16:51 -04008008 }
James Smart2fcee4b2010-12-15 17:57:46 -05008009
James Smartda0436e2009-05-22 14:51:39 -04008010 return rc;
8011}
8012
8013/**
James Smartda0436e2009-05-22 14:51:39 -04008014 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
8015 * @phba: pointer to lpfc hba data structure.
8016 *
8017 * This routine is invoked to set up the PCI device memory space for device
8018 * with SLI-4 interface spec.
8019 *
8020 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008021 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008022 * other values - error
8023 **/
8024static int
8025lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
8026{
8027 struct pci_dev *pdev;
8028 unsigned long bar0map_len, bar1map_len, bar2map_len;
8029 int error = -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -05008030 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04008031
8032 /* Obtain PCI device reference */
8033 if (!phba->pcidev)
8034 return error;
8035 else
8036 pdev = phba->pcidev;
8037
8038 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05008039 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
8040 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
8041 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
8042 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smartda0436e2009-05-22 14:51:39 -04008043 return error;
Michael Reed8e685972009-09-18 12:02:05 -05008044 }
8045 }
James Smartda0436e2009-05-22 14:51:39 -04008046
James Smart2fcee4b2010-12-15 17:57:46 -05008047 /*
8048 * The BARs and register set definitions and offset locations are
8049 * dependent on the if_type.
8050 */
8051 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
8052 &phba->sli4_hba.sli_intf.word0)) {
8053 return error;
8054 }
8055
8056 /* There is no SLI3 failback for SLI4 devices. */
8057 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
8058 LPFC_SLI_INTF_VALID) {
8059 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8060 "2894 SLI_INTF reg contents invalid "
8061 "sli_intf reg 0x%x\n",
8062 phba->sli4_hba.sli_intf.word0);
8063 return error;
8064 }
8065
8066 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
8067 /*
8068 * Get the bus address of SLI4 device Bar regions and the
8069 * number of bytes required by each mapping. The mapping of the
8070 * particular PCI BARs regions is dependent on the type of
8071 * SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -04008072 */
James Smartf5ca6f22013-09-06 12:21:09 -04008073 if (pci_resource_start(pdev, PCI_64BIT_BAR0)) {
8074 phba->pci_bar0_map = pci_resource_start(pdev, PCI_64BIT_BAR0);
8075 bar0map_len = pci_resource_len(pdev, PCI_64BIT_BAR0);
James Smart2fcee4b2010-12-15 17:57:46 -05008076
8077 /*
8078 * Map SLI4 PCI Config Space Register base to a kernel virtual
8079 * addr
8080 */
8081 phba->sli4_hba.conf_regs_memmap_p =
8082 ioremap(phba->pci_bar0_map, bar0map_len);
8083 if (!phba->sli4_hba.conf_regs_memmap_p) {
8084 dev_printk(KERN_ERR, &pdev->dev,
8085 "ioremap failed for SLI4 PCI config "
8086 "registers.\n");
8087 goto out;
8088 }
James Smartf5ca6f22013-09-06 12:21:09 -04008089 phba->pci_bar0_memmap_p = phba->sli4_hba.conf_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05008090 /* Set up BAR0 PCI config space register memory map */
8091 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smart1dfb5a42010-02-12 14:40:50 -05008092 } else {
8093 phba->pci_bar0_map = pci_resource_start(pdev, 1);
8094 bar0map_len = pci_resource_len(pdev, 1);
James Smart2fcee4b2010-12-15 17:57:46 -05008095 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8096 dev_printk(KERN_ERR, &pdev->dev,
8097 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
8098 goto out;
8099 }
8100 phba->sli4_hba.conf_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04008101 ioremap(phba->pci_bar0_map, bar0map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05008102 if (!phba->sli4_hba.conf_regs_memmap_p) {
8103 dev_printk(KERN_ERR, &pdev->dev,
8104 "ioremap failed for SLI4 PCI config "
8105 "registers.\n");
8106 goto out;
8107 }
8108 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smartda0436e2009-05-22 14:51:39 -04008109 }
8110
James Smartc31098c2011-04-16 11:03:33 -04008111 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
James Smartf5ca6f22013-09-06 12:21:09 -04008112 (pci_resource_start(pdev, PCI_64BIT_BAR2))) {
James Smart2fcee4b2010-12-15 17:57:46 -05008113 /*
8114 * Map SLI4 if type 0 HBA Control Register base to a kernel
8115 * virtual address and setup the registers.
8116 */
James Smartf5ca6f22013-09-06 12:21:09 -04008117 phba->pci_bar1_map = pci_resource_start(pdev, PCI_64BIT_BAR2);
8118 bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
James Smart2fcee4b2010-12-15 17:57:46 -05008119 phba->sli4_hba.ctrl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04008120 ioremap(phba->pci_bar1_map, bar1map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05008121 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
8122 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04008123 "ioremap failed for SLI4 HBA control registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05008124 goto out_iounmap_conf;
8125 }
James Smartf5ca6f22013-09-06 12:21:09 -04008126 phba->pci_bar2_memmap_p = phba->sli4_hba.ctrl_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05008127 lpfc_sli4_bar1_register_memmap(phba);
James Smartda0436e2009-05-22 14:51:39 -04008128 }
8129
James Smartc31098c2011-04-16 11:03:33 -04008130 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
James Smartf5ca6f22013-09-06 12:21:09 -04008131 (pci_resource_start(pdev, PCI_64BIT_BAR4))) {
James Smart2fcee4b2010-12-15 17:57:46 -05008132 /*
8133 * Map SLI4 if type 0 HBA Doorbell Register base to a kernel
8134 * virtual address and setup the registers.
8135 */
James Smartf5ca6f22013-09-06 12:21:09 -04008136 phba->pci_bar2_map = pci_resource_start(pdev, PCI_64BIT_BAR4);
8137 bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
James Smart2fcee4b2010-12-15 17:57:46 -05008138 phba->sli4_hba.drbl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04008139 ioremap(phba->pci_bar2_map, bar2map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05008140 if (!phba->sli4_hba.drbl_regs_memmap_p) {
8141 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04008142 "ioremap failed for SLI4 HBA doorbell registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05008143 goto out_iounmap_ctrl;
8144 }
James Smartf5ca6f22013-09-06 12:21:09 -04008145 phba->pci_bar4_memmap_p = phba->sli4_hba.drbl_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05008146 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
8147 if (error)
8148 goto out_iounmap_all;
James Smartda0436e2009-05-22 14:51:39 -04008149 }
8150
James Smartda0436e2009-05-22 14:51:39 -04008151 return 0;
8152
8153out_iounmap_all:
8154 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
8155out_iounmap_ctrl:
8156 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
8157out_iounmap_conf:
8158 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8159out:
8160 return error;
8161}
8162
8163/**
8164 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
8165 * @phba: pointer to lpfc hba data structure.
8166 *
8167 * This routine is invoked to unset the PCI device memory space for device
8168 * with SLI-4 interface spec.
8169 **/
8170static void
8171lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
8172{
James Smart2e90f4b2011-12-13 13:22:37 -05008173 uint32_t if_type;
8174 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smartda0436e2009-05-22 14:51:39 -04008175
James Smart2e90f4b2011-12-13 13:22:37 -05008176 switch (if_type) {
8177 case LPFC_SLI_INTF_IF_TYPE_0:
8178 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
8179 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
8180 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8181 break;
8182 case LPFC_SLI_INTF_IF_TYPE_2:
8183 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8184 break;
8185 case LPFC_SLI_INTF_IF_TYPE_1:
8186 default:
8187 dev_printk(KERN_ERR, &phba->pcidev->dev,
8188 "FATAL - unsupported SLI4 interface type - %d\n",
8189 if_type);
8190 break;
8191 }
James Smartda0436e2009-05-22 14:51:39 -04008192}
8193
8194/**
James Smart3772a992009-05-22 14:50:54 -04008195 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
8196 * @phba: pointer to lpfc hba data structure.
8197 *
8198 * This routine is invoked to enable the MSI-X interrupt vectors to device
8199 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
8200 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
8201 * invoked, enables either all or nothing, depending on the current
8202 * availability of PCI vector resources. The device driver is responsible
8203 * for calling the individual request_irq() to register each MSI-X vector
8204 * with a interrupt handler, which is done in this function. Note that
8205 * later when device is unloading, the driver should always call free_irq()
8206 * on all MSI-X vectors it has done request_irq() on before calling
8207 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
8208 * will be left with MSI-X enabled and leaks its vectors.
8209 *
8210 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008211 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008212 * other values - error
8213 **/
8214static int
8215lpfc_sli_enable_msix(struct lpfc_hba *phba)
8216{
8217 int rc, i;
8218 LPFC_MBOXQ_t *pmb;
8219
8220 /* Set up MSI-X multi-message vectors */
8221 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8222 phba->msix_entries[i].entry = i;
8223
8224 /* Configure MSI-X capability structure */
8225 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
8226 ARRAY_SIZE(phba->msix_entries));
8227 if (rc) {
8228 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8229 "0420 PCI enable MSI-X failed (%d)\n", rc);
8230 goto msi_fail_out;
8231 }
8232 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8233 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8234 "0477 MSI-X entry[%d]: vector=x%x "
8235 "message=%d\n", i,
8236 phba->msix_entries[i].vector,
8237 phba->msix_entries[i].entry);
8238 /*
8239 * Assign MSI-X vectors to interrupt handlers
8240 */
8241
8242 /* vector-0 is associated to slow-path handler */
8243 rc = request_irq(phba->msix_entries[0].vector,
8244 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
8245 LPFC_SP_DRIVER_HANDLER_NAME, phba);
8246 if (rc) {
8247 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8248 "0421 MSI-X slow-path request_irq failed "
8249 "(%d)\n", rc);
8250 goto msi_fail_out;
8251 }
8252
8253 /* vector-1 is associated to fast-path handler */
8254 rc = request_irq(phba->msix_entries[1].vector,
8255 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
8256 LPFC_FP_DRIVER_HANDLER_NAME, phba);
8257
8258 if (rc) {
8259 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8260 "0429 MSI-X fast-path request_irq failed "
8261 "(%d)\n", rc);
8262 goto irq_fail_out;
8263 }
8264
8265 /*
8266 * Configure HBA MSI-X attention conditions to messages
8267 */
8268 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8269
8270 if (!pmb) {
8271 rc = -ENOMEM;
8272 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8273 "0474 Unable to allocate memory for issuing "
8274 "MBOX_CONFIG_MSI command\n");
8275 goto mem_fail_out;
8276 }
8277 rc = lpfc_config_msi(phba, pmb);
8278 if (rc)
8279 goto mbx_fail_out;
8280 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
8281 if (rc != MBX_SUCCESS) {
8282 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
8283 "0351 Config MSI mailbox command failed, "
8284 "mbxCmd x%x, mbxStatus x%x\n",
8285 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
8286 goto mbx_fail_out;
8287 }
8288
8289 /* Free memory allocated for mailbox command */
8290 mempool_free(pmb, phba->mbox_mem_pool);
8291 return rc;
8292
8293mbx_fail_out:
8294 /* Free memory allocated for mailbox command */
8295 mempool_free(pmb, phba->mbox_mem_pool);
8296
8297mem_fail_out:
8298 /* free the irq already requested */
8299 free_irq(phba->msix_entries[1].vector, phba);
8300
8301irq_fail_out:
8302 /* free the irq already requested */
8303 free_irq(phba->msix_entries[0].vector, phba);
8304
8305msi_fail_out:
8306 /* Unconfigure MSI-X capability structure */
8307 pci_disable_msix(phba->pcidev);
8308 return rc;
8309}
8310
8311/**
8312 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
8313 * @phba: pointer to lpfc hba data structure.
8314 *
8315 * This routine is invoked to release the MSI-X vectors and then disable the
8316 * MSI-X interrupt mode to device with SLI-3 interface spec.
8317 **/
8318static void
8319lpfc_sli_disable_msix(struct lpfc_hba *phba)
8320{
8321 int i;
8322
8323 /* Free up MSI-X multi-message vectors */
8324 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8325 free_irq(phba->msix_entries[i].vector, phba);
8326 /* Disable MSI-X */
8327 pci_disable_msix(phba->pcidev);
8328
8329 return;
8330}
8331
8332/**
8333 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
8334 * @phba: pointer to lpfc hba data structure.
8335 *
8336 * This routine is invoked to enable the MSI interrupt mode to device with
8337 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
8338 * enable the MSI vector. The device driver is responsible for calling the
8339 * request_irq() to register MSI vector with a interrupt the handler, which
8340 * is done in this function.
8341 *
8342 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008343 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008344 * other values - error
8345 */
8346static int
8347lpfc_sli_enable_msi(struct lpfc_hba *phba)
8348{
8349 int rc;
8350
8351 rc = pci_enable_msi(phba->pcidev);
8352 if (!rc)
8353 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8354 "0462 PCI enable MSI mode success.\n");
8355 else {
8356 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8357 "0471 PCI enable MSI mode failed (%d)\n", rc);
8358 return rc;
8359 }
8360
8361 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
8362 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8363 if (rc) {
8364 pci_disable_msi(phba->pcidev);
8365 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8366 "0478 MSI request_irq failed (%d)\n", rc);
8367 }
8368 return rc;
8369}
8370
8371/**
8372 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
8373 * @phba: pointer to lpfc hba data structure.
8374 *
8375 * This routine is invoked to disable the MSI interrupt mode to device with
8376 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
8377 * done request_irq() on before calling pci_disable_msi(). Failure to do so
8378 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
8379 * its vector.
8380 */
8381static void
8382lpfc_sli_disable_msi(struct lpfc_hba *phba)
8383{
8384 free_irq(phba->pcidev->irq, phba);
8385 pci_disable_msi(phba->pcidev);
8386 return;
8387}
8388
8389/**
8390 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
8391 * @phba: pointer to lpfc hba data structure.
8392 *
8393 * This routine is invoked to enable device interrupt and associate driver's
8394 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
8395 * spec. Depends on the interrupt mode configured to the driver, the driver
8396 * will try to fallback from the configured interrupt mode to an interrupt
8397 * mode which is supported by the platform, kernel, and device in the order
8398 * of:
8399 * MSI-X -> MSI -> IRQ.
8400 *
8401 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008402 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008403 * other values - error
8404 **/
8405static uint32_t
8406lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
8407{
8408 uint32_t intr_mode = LPFC_INTR_ERROR;
8409 int retval;
8410
8411 if (cfg_mode == 2) {
8412 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
8413 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
8414 if (!retval) {
8415 /* Now, try to enable MSI-X interrupt mode */
8416 retval = lpfc_sli_enable_msix(phba);
8417 if (!retval) {
8418 /* Indicate initialization to MSI-X mode */
8419 phba->intr_type = MSIX;
8420 intr_mode = 2;
8421 }
8422 }
8423 }
8424
8425 /* Fallback to MSI if MSI-X initialization failed */
8426 if (cfg_mode >= 1 && phba->intr_type == NONE) {
8427 retval = lpfc_sli_enable_msi(phba);
8428 if (!retval) {
8429 /* Indicate initialization to MSI mode */
8430 phba->intr_type = MSI;
8431 intr_mode = 1;
8432 }
8433 }
8434
8435 /* Fallback to INTx if both MSI-X/MSI initalization failed */
8436 if (phba->intr_type == NONE) {
8437 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
8438 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8439 if (!retval) {
8440 /* Indicate initialization to INTx mode */
8441 phba->intr_type = INTx;
8442 intr_mode = 0;
8443 }
8444 }
8445 return intr_mode;
8446}
8447
8448/**
8449 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
8450 * @phba: pointer to lpfc hba data structure.
8451 *
8452 * This routine is invoked to disable device interrupt and disassociate the
8453 * driver's interrupt handler(s) from interrupt vector(s) to device with
8454 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
8455 * release the interrupt vector(s) for the message signaled interrupt.
8456 **/
8457static void
8458lpfc_sli_disable_intr(struct lpfc_hba *phba)
8459{
8460 /* Disable the currently initialized interrupt mode */
8461 if (phba->intr_type == MSIX)
8462 lpfc_sli_disable_msix(phba);
8463 else if (phba->intr_type == MSI)
8464 lpfc_sli_disable_msi(phba);
8465 else if (phba->intr_type == INTx)
8466 free_irq(phba->pcidev->irq, phba);
8467
8468 /* Reset interrupt management states */
8469 phba->intr_type = NONE;
8470 phba->sli.slistat.sli_intr = 0;
8471
8472 return;
8473}
8474
8475/**
James Smart7bb03bb2013-04-17 20:19:16 -04008476 * lpfc_find_next_cpu - Find next available CPU that matches the phys_id
8477 * @phba: pointer to lpfc hba data structure.
8478 *
8479 * Find next available CPU to use for IRQ to CPU affinity.
8480 */
8481static int
8482lpfc_find_next_cpu(struct lpfc_hba *phba, uint32_t phys_id)
8483{
8484 struct lpfc_vector_map_info *cpup;
8485 int cpu;
8486
8487 cpup = phba->sli4_hba.cpu_map;
8488 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8489 /* CPU must be online */
8490 if (cpu_online(cpu)) {
8491 if ((cpup->irq == LPFC_VECTOR_MAP_EMPTY) &&
8492 (lpfc_used_cpu[cpu] == LPFC_VECTOR_MAP_EMPTY) &&
8493 (cpup->phys_id == phys_id)) {
8494 return cpu;
8495 }
8496 }
8497 cpup++;
8498 }
8499
8500 /*
8501 * If we get here, we have used ALL CPUs for the specific
8502 * phys_id. Now we need to clear out lpfc_used_cpu and start
8503 * reusing CPUs.
8504 */
8505
8506 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8507 if (lpfc_used_cpu[cpu] == phys_id)
8508 lpfc_used_cpu[cpu] = LPFC_VECTOR_MAP_EMPTY;
8509 }
8510
8511 cpup = phba->sli4_hba.cpu_map;
8512 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8513 /* CPU must be online */
8514 if (cpu_online(cpu)) {
8515 if ((cpup->irq == LPFC_VECTOR_MAP_EMPTY) &&
8516 (cpup->phys_id == phys_id)) {
8517 return cpu;
8518 }
8519 }
8520 cpup++;
8521 }
8522 return LPFC_VECTOR_MAP_EMPTY;
8523}
8524
8525/**
8526 * lpfc_sli4_set_affinity - Set affinity for HBA IRQ vectors
8527 * @phba: pointer to lpfc hba data structure.
8528 * @vectors: number of HBA vectors
8529 *
8530 * Affinitize MSIX IRQ vectors to CPUs. Try to equally spread vector
8531 * affinization across multple physical CPUs (numa nodes).
8532 * In addition, this routine will assign an IO channel for each CPU
8533 * to use when issuing I/Os.
8534 */
8535static int
8536lpfc_sli4_set_affinity(struct lpfc_hba *phba, int vectors)
8537{
8538 int i, idx, saved_chann, used_chann, cpu, phys_id;
James Smartec2087a2013-09-06 12:20:36 -04008539 int max_phys_id, min_phys_id;
8540 int num_io_channel, first_cpu, chan;
James Smart7bb03bb2013-04-17 20:19:16 -04008541 struct lpfc_vector_map_info *cpup;
8542#ifdef CONFIG_X86
8543 struct cpuinfo_x86 *cpuinfo;
8544#endif
8545 struct cpumask *mask;
8546 uint8_t chann[LPFC_FCP_IO_CHAN_MAX+1];
8547
8548 /* If there is no mapping, just return */
8549 if (!phba->cfg_fcp_cpu_map)
8550 return 1;
8551
8552 /* Init cpu_map array */
8553 memset(phba->sli4_hba.cpu_map, 0xff,
8554 (sizeof(struct lpfc_vector_map_info) *
8555 phba->sli4_hba.num_present_cpu));
8556
8557 max_phys_id = 0;
James Smartec2087a2013-09-06 12:20:36 -04008558 min_phys_id = 0xff;
James Smart7bb03bb2013-04-17 20:19:16 -04008559 phys_id = 0;
8560 num_io_channel = 0;
8561 first_cpu = LPFC_VECTOR_MAP_EMPTY;
8562
8563 /* Update CPU map with physical id and core id of each CPU */
8564 cpup = phba->sli4_hba.cpu_map;
8565 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8566#ifdef CONFIG_X86
8567 cpuinfo = &cpu_data(cpu);
8568 cpup->phys_id = cpuinfo->phys_proc_id;
8569 cpup->core_id = cpuinfo->cpu_core_id;
8570#else
8571 /* No distinction between CPUs for other platforms */
8572 cpup->phys_id = 0;
8573 cpup->core_id = 0;
8574#endif
8575
8576 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8577 "3328 CPU physid %d coreid %d\n",
8578 cpup->phys_id, cpup->core_id);
8579
8580 if (cpup->phys_id > max_phys_id)
8581 max_phys_id = cpup->phys_id;
James Smartec2087a2013-09-06 12:20:36 -04008582 if (cpup->phys_id < min_phys_id)
8583 min_phys_id = cpup->phys_id;
James Smart7bb03bb2013-04-17 20:19:16 -04008584 cpup++;
8585 }
8586
James Smartec2087a2013-09-06 12:20:36 -04008587 phys_id = min_phys_id;
James Smart7bb03bb2013-04-17 20:19:16 -04008588 /* Now associate the HBA vectors with specific CPUs */
8589 for (idx = 0; idx < vectors; idx++) {
8590 cpup = phba->sli4_hba.cpu_map;
8591 cpu = lpfc_find_next_cpu(phba, phys_id);
8592 if (cpu == LPFC_VECTOR_MAP_EMPTY) {
8593
8594 /* Try for all phys_id's */
8595 for (i = 1; i < max_phys_id; i++) {
8596 phys_id++;
8597 if (phys_id > max_phys_id)
James Smartec2087a2013-09-06 12:20:36 -04008598 phys_id = min_phys_id;
James Smart7bb03bb2013-04-17 20:19:16 -04008599 cpu = lpfc_find_next_cpu(phba, phys_id);
8600 if (cpu == LPFC_VECTOR_MAP_EMPTY)
8601 continue;
8602 goto found;
8603 }
8604
James Smartec2087a2013-09-06 12:20:36 -04008605 /* Use round robin for scheduling */
8606 phba->cfg_fcp_io_sched = LPFC_FCP_SCHED_ROUND_ROBIN;
8607 chan = 0;
8608 cpup = phba->sli4_hba.cpu_map;
8609 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
8610 cpup->channel_id = chan;
8611 cpup++;
8612 chan++;
8613 if (chan >= phba->cfg_fcp_io_channel)
8614 chan = 0;
8615 }
8616
James Smart7bb03bb2013-04-17 20:19:16 -04008617 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8618 "3329 Cannot set affinity:"
8619 "Error mapping vector %d (%d)\n",
8620 idx, vectors);
8621 return 0;
8622 }
8623found:
8624 cpup += cpu;
8625 if (phba->cfg_fcp_cpu_map == LPFC_DRIVER_CPU_MAP)
8626 lpfc_used_cpu[cpu] = phys_id;
8627
8628 /* Associate vector with selected CPU */
8629 cpup->irq = phba->sli4_hba.msix_entries[idx].vector;
8630
8631 /* Associate IO channel with selected CPU */
8632 cpup->channel_id = idx;
8633 num_io_channel++;
8634
8635 if (first_cpu == LPFC_VECTOR_MAP_EMPTY)
8636 first_cpu = cpu;
8637
8638 /* Now affinitize to the selected CPU */
8639 mask = &cpup->maskbits;
8640 cpumask_clear(mask);
8641 cpumask_set_cpu(cpu, mask);
8642 i = irq_set_affinity_hint(phba->sli4_hba.msix_entries[idx].
8643 vector, mask);
8644
8645 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8646 "3330 Set Affinity: CPU %d channel %d "
8647 "irq %d (%x)\n",
8648 cpu, cpup->channel_id,
8649 phba->sli4_hba.msix_entries[idx].vector, i);
8650
8651 /* Spread vector mapping across multple physical CPU nodes */
8652 phys_id++;
8653 if (phys_id > max_phys_id)
James Smartec2087a2013-09-06 12:20:36 -04008654 phys_id = min_phys_id;
James Smart7bb03bb2013-04-17 20:19:16 -04008655 }
8656
8657 /*
8658 * Finally fill in the IO channel for any remaining CPUs.
8659 * At this point, all IO channels have been assigned to a specific
8660 * MSIx vector, mapped to a specific CPU.
8661 * Base the remaining IO channel assigned, to IO channels already
8662 * assigned to other CPUs on the same phys_id.
8663 */
James Smartec2087a2013-09-06 12:20:36 -04008664 for (i = min_phys_id; i <= max_phys_id; i++) {
James Smart7bb03bb2013-04-17 20:19:16 -04008665 /*
8666 * If there are no io channels already mapped to
8667 * this phys_id, just round robin thru the io_channels.
8668 * Setup chann[] for round robin.
8669 */
8670 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++)
8671 chann[idx] = idx;
8672
8673 saved_chann = 0;
8674 used_chann = 0;
8675
8676 /*
8677 * First build a list of IO channels already assigned
8678 * to this phys_id before reassigning the same IO
8679 * channels to the remaining CPUs.
8680 */
8681 cpup = phba->sli4_hba.cpu_map;
8682 cpu = first_cpu;
8683 cpup += cpu;
8684 for (idx = 0; idx < phba->sli4_hba.num_present_cpu;
8685 idx++) {
8686 if (cpup->phys_id == i) {
8687 /*
8688 * Save any IO channels that are
8689 * already mapped to this phys_id.
8690 */
8691 if (cpup->irq != LPFC_VECTOR_MAP_EMPTY) {
8692 chann[saved_chann] =
8693 cpup->channel_id;
8694 saved_chann++;
8695 goto out;
8696 }
8697
8698 /* See if we are using round-robin */
8699 if (saved_chann == 0)
8700 saved_chann =
8701 phba->cfg_fcp_io_channel;
8702
8703 /* Associate next IO channel with CPU */
8704 cpup->channel_id = chann[used_chann];
8705 num_io_channel++;
8706 used_chann++;
8707 if (used_chann == saved_chann)
8708 used_chann = 0;
8709
8710 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8711 "3331 Set IO_CHANN "
8712 "CPU %d channel %d\n",
8713 idx, cpup->channel_id);
8714 }
8715out:
8716 cpu++;
8717 if (cpu >= phba->sli4_hba.num_present_cpu) {
8718 cpup = phba->sli4_hba.cpu_map;
8719 cpu = 0;
8720 } else {
8721 cpup++;
8722 }
8723 }
8724 }
8725
8726 if (phba->sli4_hba.num_online_cpu != phba->sli4_hba.num_present_cpu) {
8727 cpup = phba->sli4_hba.cpu_map;
8728 for (idx = 0; idx < phba->sli4_hba.num_present_cpu; idx++) {
8729 if (cpup->channel_id == LPFC_VECTOR_MAP_EMPTY) {
8730 cpup->channel_id = 0;
8731 num_io_channel++;
8732
8733 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8734 "3332 Assign IO_CHANN "
8735 "CPU %d channel %d\n",
8736 idx, cpup->channel_id);
8737 }
8738 cpup++;
8739 }
8740 }
8741
8742 /* Sanity check */
8743 if (num_io_channel != phba->sli4_hba.num_present_cpu)
8744 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8745 "3333 Set affinity mismatch:"
James Smartec2087a2013-09-06 12:20:36 -04008746 "%d chann != %d cpus: %d vectors\n",
James Smart7bb03bb2013-04-17 20:19:16 -04008747 num_io_channel, phba->sli4_hba.num_present_cpu,
8748 vectors);
8749
James Smartec2087a2013-09-06 12:20:36 -04008750 /* Enable using cpu affinity for scheduling */
James Smart7bb03bb2013-04-17 20:19:16 -04008751 phba->cfg_fcp_io_sched = LPFC_FCP_SCHED_BY_CPU;
8752 return 1;
8753}
8754
8755
8756/**
James Smartda0436e2009-05-22 14:51:39 -04008757 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
8758 * @phba: pointer to lpfc hba data structure.
8759 *
8760 * This routine is invoked to enable the MSI-X interrupt vectors to device
8761 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
8762 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
8763 * enables either all or nothing, depending on the current availability of
8764 * PCI vector resources. The device driver is responsible for calling the
8765 * individual request_irq() to register each MSI-X vector with a interrupt
8766 * handler, which is done in this function. Note that later when device is
8767 * unloading, the driver should always call free_irq() on all MSI-X vectors
8768 * it has done request_irq() on before calling pci_disable_msix(). Failure
8769 * to do so results in a BUG_ON() and a device will be left with MSI-X
8770 * enabled and leaks its vectors.
8771 *
8772 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008773 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008774 * other values - error
8775 **/
8776static int
8777lpfc_sli4_enable_msix(struct lpfc_hba *phba)
8778{
James Smart75baf692010-06-08 18:31:21 -04008779 int vectors, rc, index;
James Smartda0436e2009-05-22 14:51:39 -04008780
8781 /* Set up MSI-X multi-message vectors */
James Smart82c3e9b2012-09-29 11:29:50 -04008782 for (index = 0; index < phba->cfg_fcp_io_channel; index++)
James Smartda0436e2009-05-22 14:51:39 -04008783 phba->sli4_hba.msix_entries[index].entry = index;
8784
8785 /* Configure MSI-X capability structure */
James Smart82c3e9b2012-09-29 11:29:50 -04008786 vectors = phba->cfg_fcp_io_channel;
James Smart1ba981f2014-02-20 09:56:45 -05008787 if (phba->cfg_fof) {
8788 phba->sli4_hba.msix_entries[index].entry = index;
8789 vectors++;
8790 }
James Smart75baf692010-06-08 18:31:21 -04008791enable_msix_vectors:
James Smartda0436e2009-05-22 14:51:39 -04008792 rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
James Smart75baf692010-06-08 18:31:21 -04008793 vectors);
8794 if (rc > 1) {
8795 vectors = rc;
8796 goto enable_msix_vectors;
8797 } else if (rc) {
James Smartda0436e2009-05-22 14:51:39 -04008798 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8799 "0484 PCI enable MSI-X failed (%d)\n", rc);
8800 goto msi_fail_out;
8801 }
James Smart75baf692010-06-08 18:31:21 -04008802
James Smartda0436e2009-05-22 14:51:39 -04008803 /* Log MSI-X vector assignment */
James Smart75baf692010-06-08 18:31:21 -04008804 for (index = 0; index < vectors; index++)
James Smartda0436e2009-05-22 14:51:39 -04008805 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8806 "0489 MSI-X entry[%d]: vector=x%x "
8807 "message=%d\n", index,
8808 phba->sli4_hba.msix_entries[index].vector,
8809 phba->sli4_hba.msix_entries[index].entry);
James Smart67d12732012-08-03 12:36:13 -04008810
James Smart7bb03bb2013-04-17 20:19:16 -04008811 /* Assign MSI-X vectors to interrupt handlers */
James Smart67d12732012-08-03 12:36:13 -04008812 for (index = 0; index < vectors; index++) {
James Smart4305f182012-08-03 12:36:33 -04008813 memset(&phba->sli4_hba.handler_name[index], 0, 16);
8814 sprintf((char *)&phba->sli4_hba.handler_name[index],
8815 LPFC_DRIVER_HANDLER_NAME"%d", index);
James Smartda0436e2009-05-22 14:51:39 -04008816
James Smart67d12732012-08-03 12:36:13 -04008817 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8818 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
James Smartba20c852012-08-03 12:36:52 -04008819 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].fcp_eq_in_use, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008820 if (phba->cfg_fof && (index == (vectors - 1)))
8821 rc = request_irq(
8822 phba->sli4_hba.msix_entries[index].vector,
8823 &lpfc_sli4_fof_intr_handler, IRQF_SHARED,
8824 (char *)&phba->sli4_hba.handler_name[index],
8825 &phba->sli4_hba.fcp_eq_hdl[index]);
8826 else
8827 rc = request_irq(
8828 phba->sli4_hba.msix_entries[index].vector,
James Smart67d12732012-08-03 12:36:13 -04008829 &lpfc_sli4_hba_intr_handler, IRQF_SHARED,
James Smart4305f182012-08-03 12:36:33 -04008830 (char *)&phba->sli4_hba.handler_name[index],
James Smart67d12732012-08-03 12:36:13 -04008831 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -04008832 if (rc) {
8833 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8834 "0486 MSI-X fast-path (%d) "
8835 "request_irq failed (%d)\n", index, rc);
8836 goto cfg_fail_out;
8837 }
8838 }
8839
James Smart1ba981f2014-02-20 09:56:45 -05008840 if (phba->cfg_fof)
8841 vectors--;
8842
James Smart82c3e9b2012-09-29 11:29:50 -04008843 if (vectors != phba->cfg_fcp_io_channel) {
8844 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8845 "3238 Reducing IO channels to match number of "
8846 "MSI-X vectors, requested %d got %d\n",
8847 phba->cfg_fcp_io_channel, vectors);
8848 phba->cfg_fcp_io_channel = vectors;
8849 }
James Smart7bb03bb2013-04-17 20:19:16 -04008850
8851 lpfc_sli4_set_affinity(phba, vectors);
James Smartda0436e2009-05-22 14:51:39 -04008852 return rc;
8853
8854cfg_fail_out:
8855 /* free the irq already requested */
James Smartacbd8612013-09-06 12:21:19 -04008856 for (--index; index >= 0; index--) {
8857 irq_set_affinity_hint(phba->sli4_hba.msix_entries[index].
8858 vector, NULL);
James Smart67d12732012-08-03 12:36:13 -04008859 free_irq(phba->sli4_hba.msix_entries[index].vector,
8860 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartacbd8612013-09-06 12:21:19 -04008861 }
James Smartda0436e2009-05-22 14:51:39 -04008862
8863msi_fail_out:
8864 /* Unconfigure MSI-X capability structure */
8865 pci_disable_msix(phba->pcidev);
8866 return rc;
8867}
8868
8869/**
8870 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
8871 * @phba: pointer to lpfc hba data structure.
8872 *
8873 * This routine is invoked to release the MSI-X vectors and then disable the
8874 * MSI-X interrupt mode to device with SLI-4 interface spec.
8875 **/
8876static void
8877lpfc_sli4_disable_msix(struct lpfc_hba *phba)
8878{
8879 int index;
8880
8881 /* Free up MSI-X multi-message vectors */
James Smartacbd8612013-09-06 12:21:19 -04008882 for (index = 0; index < phba->cfg_fcp_io_channel; index++) {
8883 irq_set_affinity_hint(phba->sli4_hba.msix_entries[index].
8884 vector, NULL);
James Smartda0436e2009-05-22 14:51:39 -04008885 free_irq(phba->sli4_hba.msix_entries[index].vector,
James Smart67d12732012-08-03 12:36:13 -04008886 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartacbd8612013-09-06 12:21:19 -04008887 }
James Smart1ba981f2014-02-20 09:56:45 -05008888 if (phba->cfg_fof) {
8889 free_irq(phba->sli4_hba.msix_entries[index].vector,
8890 &phba->sli4_hba.fcp_eq_hdl[index]);
8891 }
James Smartda0436e2009-05-22 14:51:39 -04008892 /* Disable MSI-X */
8893 pci_disable_msix(phba->pcidev);
8894
8895 return;
8896}
8897
8898/**
8899 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
8900 * @phba: pointer to lpfc hba data structure.
8901 *
8902 * This routine is invoked to enable the MSI interrupt mode to device with
8903 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
8904 * to enable the MSI vector. The device driver is responsible for calling
8905 * the request_irq() to register MSI vector with a interrupt the handler,
8906 * which is done in this function.
8907 *
8908 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008909 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008910 * other values - error
8911 **/
8912static int
8913lpfc_sli4_enable_msi(struct lpfc_hba *phba)
8914{
8915 int rc, index;
8916
8917 rc = pci_enable_msi(phba->pcidev);
8918 if (!rc)
8919 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8920 "0487 PCI enable MSI mode success.\n");
8921 else {
8922 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8923 "0488 PCI enable MSI mode failed (%d)\n", rc);
8924 return rc;
8925 }
8926
8927 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
8928 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8929 if (rc) {
8930 pci_disable_msi(phba->pcidev);
8931 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8932 "0490 MSI request_irq failed (%d)\n", rc);
James Smart75baf692010-06-08 18:31:21 -04008933 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008934 }
8935
James Smart67d12732012-08-03 12:36:13 -04008936 for (index = 0; index < phba->cfg_fcp_io_channel; index++) {
James Smartda0436e2009-05-22 14:51:39 -04008937 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8938 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
8939 }
8940
James Smart1ba981f2014-02-20 09:56:45 -05008941 if (phba->cfg_fof) {
8942 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8943 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
8944 }
James Smart75baf692010-06-08 18:31:21 -04008945 return 0;
James Smartda0436e2009-05-22 14:51:39 -04008946}
8947
8948/**
8949 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
8950 * @phba: pointer to lpfc hba data structure.
8951 *
8952 * This routine is invoked to disable the MSI interrupt mode to device with
8953 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
8954 * done request_irq() on before calling pci_disable_msi(). Failure to do so
8955 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
8956 * its vector.
8957 **/
8958static void
8959lpfc_sli4_disable_msi(struct lpfc_hba *phba)
8960{
8961 free_irq(phba->pcidev->irq, phba);
8962 pci_disable_msi(phba->pcidev);
8963 return;
8964}
8965
8966/**
8967 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
8968 * @phba: pointer to lpfc hba data structure.
8969 *
8970 * This routine is invoked to enable device interrupt and associate driver's
8971 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
8972 * interface spec. Depends on the interrupt mode configured to the driver,
8973 * the driver will try to fallback from the configured interrupt mode to an
8974 * interrupt mode which is supported by the platform, kernel, and device in
8975 * the order of:
8976 * MSI-X -> MSI -> IRQ.
8977 *
8978 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008979 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008980 * other values - error
8981 **/
8982static uint32_t
8983lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
8984{
8985 uint32_t intr_mode = LPFC_INTR_ERROR;
8986 int retval, index;
8987
8988 if (cfg_mode == 2) {
8989 /* Preparation before conf_msi mbox cmd */
8990 retval = 0;
8991 if (!retval) {
8992 /* Now, try to enable MSI-X interrupt mode */
8993 retval = lpfc_sli4_enable_msix(phba);
8994 if (!retval) {
8995 /* Indicate initialization to MSI-X mode */
8996 phba->intr_type = MSIX;
8997 intr_mode = 2;
8998 }
8999 }
9000 }
9001
9002 /* Fallback to MSI if MSI-X initialization failed */
9003 if (cfg_mode >= 1 && phba->intr_type == NONE) {
9004 retval = lpfc_sli4_enable_msi(phba);
9005 if (!retval) {
9006 /* Indicate initialization to MSI mode */
9007 phba->intr_type = MSI;
9008 intr_mode = 1;
9009 }
9010 }
9011
9012 /* Fallback to INTx if both MSI-X/MSI initalization failed */
9013 if (phba->intr_type == NONE) {
9014 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
9015 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
9016 if (!retval) {
9017 /* Indicate initialization to INTx mode */
9018 phba->intr_type = INTx;
9019 intr_mode = 0;
James Smart67d12732012-08-03 12:36:13 -04009020 for (index = 0; index < phba->cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04009021 index++) {
9022 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
9023 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
James Smartba20c852012-08-03 12:36:52 -04009024 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].
9025 fcp_eq_in_use, 1);
James Smartda0436e2009-05-22 14:51:39 -04009026 }
James Smart1ba981f2014-02-20 09:56:45 -05009027 if (phba->cfg_fof) {
9028 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
9029 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
9030 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].
9031 fcp_eq_in_use, 1);
9032 }
James Smartda0436e2009-05-22 14:51:39 -04009033 }
9034 }
9035 return intr_mode;
9036}
9037
9038/**
9039 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
9040 * @phba: pointer to lpfc hba data structure.
9041 *
9042 * This routine is invoked to disable device interrupt and disassociate
9043 * the driver's interrupt handler(s) from interrupt vector(s) to device
9044 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
9045 * will release the interrupt vector(s) for the message signaled interrupt.
9046 **/
9047static void
9048lpfc_sli4_disable_intr(struct lpfc_hba *phba)
9049{
9050 /* Disable the currently initialized interrupt mode */
9051 if (phba->intr_type == MSIX)
9052 lpfc_sli4_disable_msix(phba);
9053 else if (phba->intr_type == MSI)
9054 lpfc_sli4_disable_msi(phba);
9055 else if (phba->intr_type == INTx)
9056 free_irq(phba->pcidev->irq, phba);
9057
9058 /* Reset interrupt management states */
9059 phba->intr_type = NONE;
9060 phba->sli.slistat.sli_intr = 0;
9061
9062 return;
9063}
9064
9065/**
James Smart3772a992009-05-22 14:50:54 -04009066 * lpfc_unset_hba - Unset SLI3 hba device initialization
9067 * @phba: pointer to lpfc hba data structure.
9068 *
9069 * This routine is invoked to unset the HBA device initialization steps to
9070 * a device with SLI-3 interface spec.
9071 **/
9072static void
9073lpfc_unset_hba(struct lpfc_hba *phba)
9074{
9075 struct lpfc_vport *vport = phba->pport;
9076 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
9077
9078 spin_lock_irq(shost->host_lock);
9079 vport->load_flag |= FC_UNLOADING;
9080 spin_unlock_irq(shost->host_lock);
9081
James Smart72859902012-01-18 16:25:38 -05009082 kfree(phba->vpi_bmask);
9083 kfree(phba->vpi_ids);
9084
James Smart3772a992009-05-22 14:50:54 -04009085 lpfc_stop_hba_timers(phba);
9086
9087 phba->pport->work_port_events = 0;
9088
9089 lpfc_sli_hba_down(phba);
9090
9091 lpfc_sli_brdrestart(phba);
9092
9093 lpfc_sli_disable_intr(phba);
9094
9095 return;
9096}
9097
9098/**
James Smart5af5eee2010-10-22 11:06:38 -04009099 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
9100 * @phba: Pointer to HBA context object.
9101 *
9102 * This function is called in the SLI4 code path to wait for completion
9103 * of device's XRIs exchange busy. It will check the XRI exchange busy
9104 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
9105 * that, it will check the XRI exchange busy on outstanding FCP and ELS
9106 * I/Os every 30 seconds, log error message, and wait forever. Only when
9107 * all XRI exchange busy complete, the driver unload shall proceed with
9108 * invoking the function reset ioctl mailbox command to the CNA and the
9109 * the rest of the driver unload resource release.
9110 **/
9111static void
9112lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
9113{
9114 int wait_time = 0;
9115 int fcp_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
9116 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
9117
9118 while (!fcp_xri_cmpl || !els_xri_cmpl) {
9119 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
9120 if (!fcp_xri_cmpl)
9121 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9122 "2877 FCP XRI exchange busy "
9123 "wait time: %d seconds.\n",
9124 wait_time/1000);
9125 if (!els_xri_cmpl)
9126 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9127 "2878 ELS XRI exchange busy "
9128 "wait time: %d seconds.\n",
9129 wait_time/1000);
9130 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
9131 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
9132 } else {
9133 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
9134 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
9135 }
9136 fcp_xri_cmpl =
9137 list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
9138 els_xri_cmpl =
9139 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
9140 }
9141}
9142
9143/**
James Smartda0436e2009-05-22 14:51:39 -04009144 * lpfc_sli4_hba_unset - Unset the fcoe hba
9145 * @phba: Pointer to HBA context object.
9146 *
9147 * This function is called in the SLI4 code path to reset the HBA's FCoE
9148 * function. The caller is not required to hold any lock. This routine
9149 * issues PCI function reset mailbox command to reset the FCoE function.
9150 * At the end of the function, it calls lpfc_hba_down_post function to
9151 * free any pending commands.
9152 **/
9153static void
9154lpfc_sli4_hba_unset(struct lpfc_hba *phba)
9155{
9156 int wait_cnt = 0;
9157 LPFC_MBOXQ_t *mboxq;
James Smart912e3ac2011-05-24 11:42:11 -04009158 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -04009159
9160 lpfc_stop_hba_timers(phba);
9161 phba->sli4_hba.intr_enable = 0;
9162
9163 /*
9164 * Gracefully wait out the potential current outstanding asynchronous
9165 * mailbox command.
9166 */
9167
9168 /* First, block any pending async mailbox command from posted */
9169 spin_lock_irq(&phba->hbalock);
9170 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
9171 spin_unlock_irq(&phba->hbalock);
9172 /* Now, trying to wait it out if we can */
9173 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9174 msleep(10);
9175 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
9176 break;
9177 }
9178 /* Forcefully release the outstanding mailbox command if timed out */
9179 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9180 spin_lock_irq(&phba->hbalock);
9181 mboxq = phba->sli.mbox_active;
9182 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
9183 __lpfc_mbox_cmpl_put(phba, mboxq);
9184 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9185 phba->sli.mbox_active = NULL;
9186 spin_unlock_irq(&phba->hbalock);
9187 }
9188
James Smart5af5eee2010-10-22 11:06:38 -04009189 /* Abort all iocbs associated with the hba */
9190 lpfc_sli_hba_iocb_abort(phba);
9191
9192 /* Wait for completion of device XRI exchange busy */
9193 lpfc_sli4_xri_exchange_busy_wait(phba);
9194
James Smartda0436e2009-05-22 14:51:39 -04009195 /* Disable PCI subsystem interrupt */
9196 lpfc_sli4_disable_intr(phba);
9197
James Smart912e3ac2011-05-24 11:42:11 -04009198 /* Disable SR-IOV if enabled */
9199 if (phba->cfg_sriov_nr_virtfn)
9200 pci_disable_sriov(pdev);
9201
James Smartda0436e2009-05-22 14:51:39 -04009202 /* Stop kthread signal shall trigger work_done one more time */
9203 kthread_stop(phba->worker_thread);
9204
James Smart3677a3a2010-09-29 11:19:14 -04009205 /* Reset SLI4 HBA FCoE function */
9206 lpfc_pci_function_reset(phba);
James Smart5350d872011-10-10 21:33:49 -04009207 lpfc_sli4_queue_destroy(phba);
James Smart3677a3a2010-09-29 11:19:14 -04009208
James Smartda0436e2009-05-22 14:51:39 -04009209 /* Stop the SLI4 device port */
9210 phba->pport->work_port_events = 0;
9211}
9212
James Smart28baac72010-02-12 14:42:03 -05009213 /**
9214 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
9215 * @phba: Pointer to HBA context object.
9216 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
9217 *
9218 * This function is called in the SLI4 code path to read the port's
9219 * sli4 capabilities.
9220 *
9221 * This function may be be called from any context that can block-wait
9222 * for the completion. The expectation is that this routine is called
9223 * typically from probe_one or from the online routine.
9224 **/
9225int
9226lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9227{
9228 int rc;
9229 struct lpfc_mqe *mqe;
9230 struct lpfc_pc_sli4_params *sli4_params;
9231 uint32_t mbox_tmo;
9232
9233 rc = 0;
9234 mqe = &mboxq->u.mqe;
9235
9236 /* Read the port's SLI4 Parameters port capabilities */
James Smartfedd3b72011-02-16 12:39:24 -05009237 lpfc_pc_sli4_params(mboxq);
James Smart28baac72010-02-12 14:42:03 -05009238 if (!phba->sli4_hba.intr_enable)
9239 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9240 else {
James Smarta183a152011-10-10 21:32:43 -04009241 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart28baac72010-02-12 14:42:03 -05009242 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
9243 }
9244
9245 if (unlikely(rc))
9246 return 1;
9247
9248 sli4_params = &phba->sli4_hba.pc_sli4_params;
9249 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
9250 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
9251 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
9252 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
9253 &mqe->un.sli4_params);
9254 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
9255 &mqe->un.sli4_params);
9256 sli4_params->proto_types = mqe->un.sli4_params.word3;
9257 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
9258 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
9259 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
9260 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
9261 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
9262 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
9263 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
9264 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
9265 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
9266 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
9267 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
9268 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
9269 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
9270 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
9271 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
9272 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
9273 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
9274 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
9275 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
9276 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
James Smart05580562011-05-24 11:40:48 -04009277
9278 /* Make sure that sge_supp_len can be handled by the driver */
9279 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
9280 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
9281
James Smart28baac72010-02-12 14:42:03 -05009282 return rc;
9283}
9284
James Smartda0436e2009-05-22 14:51:39 -04009285/**
James Smartfedd3b72011-02-16 12:39:24 -05009286 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
9287 * @phba: Pointer to HBA context object.
9288 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
9289 *
9290 * This function is called in the SLI4 code path to read the port's
9291 * sli4 capabilities.
9292 *
9293 * This function may be be called from any context that can block-wait
9294 * for the completion. The expectation is that this routine is called
9295 * typically from probe_one or from the online routine.
9296 **/
9297int
9298lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9299{
9300 int rc;
9301 struct lpfc_mqe *mqe = &mboxq->u.mqe;
9302 struct lpfc_pc_sli4_params *sli4_params;
James Smarta183a152011-10-10 21:32:43 -04009303 uint32_t mbox_tmo;
James Smartfedd3b72011-02-16 12:39:24 -05009304 int length;
9305 struct lpfc_sli4_parameters *mbx_sli4_parameters;
9306
James Smart6d368e52011-05-24 11:44:12 -04009307 /*
9308 * By default, the driver assumes the SLI4 port requires RPI
9309 * header postings. The SLI4_PARAM response will correct this
9310 * assumption.
9311 */
9312 phba->sli4_hba.rpi_hdrs_in_use = 1;
9313
James Smartfedd3b72011-02-16 12:39:24 -05009314 /* Read the port's SLI4 Config Parameters */
9315 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
9316 sizeof(struct lpfc_sli4_cfg_mhdr));
9317 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
9318 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
9319 length, LPFC_SLI4_MBX_EMBED);
9320 if (!phba->sli4_hba.intr_enable)
9321 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -04009322 else {
9323 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
9324 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
9325 }
James Smartfedd3b72011-02-16 12:39:24 -05009326 if (unlikely(rc))
9327 return rc;
9328 sli4_params = &phba->sli4_hba.pc_sli4_params;
9329 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
9330 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
9331 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
9332 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
9333 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
9334 mbx_sli4_parameters);
9335 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
9336 mbx_sli4_parameters);
9337 if (bf_get(cfg_phwq, mbx_sli4_parameters))
9338 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
9339 else
9340 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
9341 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
9342 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
James Smart1ba981f2014-02-20 09:56:45 -05009343 sli4_params->oas_supported = bf_get(cfg_oas, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -05009344 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
9345 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
9346 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
9347 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
James Smart0c651872013-07-15 18:33:23 -04009348 sli4_params->wqsize = bf_get(cfg_wqsize, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -05009349 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
9350 mbx_sli4_parameters);
9351 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
9352 mbx_sli4_parameters);
James Smart6d368e52011-05-24 11:44:12 -04009353 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
9354 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
James Smart05580562011-05-24 11:40:48 -04009355
9356 /* Make sure that sge_supp_len can be handled by the driver */
9357 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
9358 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
9359
James Smartfedd3b72011-02-16 12:39:24 -05009360 return 0;
9361}
9362
9363/**
James Smart3772a992009-05-22 14:50:54 -04009364 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
9365 * @pdev: pointer to PCI device
9366 * @pid: pointer to PCI device identifier
9367 *
9368 * This routine is to be called to attach a device with SLI-3 interface spec
9369 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
9370 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
9371 * information of the device and driver to see if the driver state that it can
9372 * support this kind of device. If the match is successful, the driver core
9373 * invokes this routine. If this routine determines it can claim the HBA, it
9374 * does all the initialization that it needs to do to handle the HBA properly.
9375 *
9376 * Return code
9377 * 0 - driver can claim the device
9378 * negative value - driver can not claim the device
9379 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009380static int
James Smart3772a992009-05-22 14:50:54 -04009381lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
9382{
9383 struct lpfc_hba *phba;
9384 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04009385 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -04009386 int error;
9387 uint32_t cfg_mode, intr_mode;
9388
9389 /* Allocate memory for HBA structure */
9390 phba = lpfc_hba_alloc(pdev);
9391 if (!phba)
9392 return -ENOMEM;
9393
9394 /* Perform generic PCI device enabling operation */
9395 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -04009396 if (error)
James Smart3772a992009-05-22 14:50:54 -04009397 goto out_free_phba;
James Smart3772a992009-05-22 14:50:54 -04009398
9399 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
9400 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
9401 if (error)
9402 goto out_disable_pci_dev;
9403
9404 /* Set up SLI-3 specific device PCI memory space */
9405 error = lpfc_sli_pci_mem_setup(phba);
9406 if (error) {
9407 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9408 "1402 Failed to set up pci memory space.\n");
9409 goto out_disable_pci_dev;
9410 }
9411
9412 /* Set up phase-1 common device driver resources */
9413 error = lpfc_setup_driver_resource_phase1(phba);
9414 if (error) {
9415 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9416 "1403 Failed to set up driver resource.\n");
9417 goto out_unset_pci_mem_s3;
9418 }
9419
9420 /* Set up SLI-3 specific device driver resources */
9421 error = lpfc_sli_driver_resource_setup(phba);
9422 if (error) {
9423 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9424 "1404 Failed to set up driver resource.\n");
9425 goto out_unset_pci_mem_s3;
9426 }
9427
9428 /* Initialize and populate the iocb list per host */
9429 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
9430 if (error) {
9431 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9432 "1405 Failed to initialize iocb list.\n");
9433 goto out_unset_driver_resource_s3;
9434 }
9435
9436 /* Set up common device driver resources */
9437 error = lpfc_setup_driver_resource_phase2(phba);
9438 if (error) {
9439 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9440 "1406 Failed to set up driver resource.\n");
9441 goto out_free_iocb_list;
9442 }
9443
James Smart079b5c92011-08-21 21:48:49 -04009444 /* Get the default values for Model Name and Description */
9445 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
9446
James Smart3772a992009-05-22 14:50:54 -04009447 /* Create SCSI host to the physical port */
9448 error = lpfc_create_shost(phba);
9449 if (error) {
9450 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9451 "1407 Failed to create scsi host.\n");
9452 goto out_unset_driver_resource;
9453 }
9454
9455 /* Configure sysfs attributes */
9456 vport = phba->pport;
9457 error = lpfc_alloc_sysfs_attr(vport);
9458 if (error) {
9459 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9460 "1476 Failed to allocate sysfs attr\n");
9461 goto out_destroy_shost;
9462 }
9463
James Smart6669f9b2009-10-02 15:16:45 -04009464 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -04009465 /* Now, trying to enable interrupt and bring up the device */
9466 cfg_mode = phba->cfg_use_msi;
9467 while (true) {
9468 /* Put device to a known state before enabling interrupt */
9469 lpfc_stop_port(phba);
9470 /* Configure and enable interrupt */
9471 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
9472 if (intr_mode == LPFC_INTR_ERROR) {
9473 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9474 "0431 Failed to enable interrupt.\n");
9475 error = -ENODEV;
9476 goto out_free_sysfs_attr;
9477 }
9478 /* SLI-3 HBA setup */
9479 if (lpfc_sli_hba_setup(phba)) {
9480 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9481 "1477 Failed to set up hba\n");
9482 error = -ENODEV;
9483 goto out_remove_device;
9484 }
9485
9486 /* Wait 50ms for the interrupts of previous mailbox commands */
9487 msleep(50);
9488 /* Check active interrupts on message signaled interrupts */
9489 if (intr_mode == 0 ||
9490 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
9491 /* Log the current active interrupt mode */
9492 phba->intr_mode = intr_mode;
9493 lpfc_log_intr_mode(phba, intr_mode);
9494 break;
9495 } else {
9496 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9497 "0447 Configure interrupt mode (%d) "
9498 "failed active interrupt test.\n",
9499 intr_mode);
9500 /* Disable the current interrupt mode */
9501 lpfc_sli_disable_intr(phba);
9502 /* Try next level of interrupt mode */
9503 cfg_mode = --intr_mode;
9504 }
9505 }
9506
9507 /* Perform post initialization setup */
9508 lpfc_post_init_setup(phba);
9509
9510 /* Check if there are static vports to be created. */
9511 lpfc_create_static_vport(phba);
9512
9513 return 0;
9514
9515out_remove_device:
9516 lpfc_unset_hba(phba);
9517out_free_sysfs_attr:
9518 lpfc_free_sysfs_attr(vport);
9519out_destroy_shost:
9520 lpfc_destroy_shost(phba);
9521out_unset_driver_resource:
9522 lpfc_unset_driver_resource_phase2(phba);
9523out_free_iocb_list:
9524 lpfc_free_iocb_list(phba);
9525out_unset_driver_resource_s3:
9526 lpfc_sli_driver_resource_unset(phba);
9527out_unset_pci_mem_s3:
9528 lpfc_sli_pci_mem_unset(phba);
9529out_disable_pci_dev:
9530 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04009531 if (shost)
9532 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -04009533out_free_phba:
9534 lpfc_hba_free(phba);
9535 return error;
9536}
9537
9538/**
9539 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -04009540 * @pdev: pointer to PCI device
9541 *
James Smart3772a992009-05-22 14:50:54 -04009542 * This routine is to be called to disattach a device with SLI-3 interface
9543 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
9544 * removed from PCI bus, it performs all the necessary cleanup for the HBA
9545 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -04009546 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009547static void
James Smart3772a992009-05-22 14:50:54 -04009548lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -05009549{
James Smart2e0fef82007-06-17 19:56:36 -05009550 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9551 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -05009552 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -05009553 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -05009554 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05009555 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
9556
James Smart549e55c2007-08-02 11:09:51 -04009557 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04009558 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04009559 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009560
James Smart858c9f62007-06-17 19:56:39 -05009561 lpfc_free_sysfs_attr(vport);
9562
James Smarteada2722008-12-04 22:39:13 -05009563 /* Release all the vports against this physical port */
9564 vports = lpfc_create_vport_work_array(phba);
9565 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -04009566 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
9567 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
9568 continue;
James Smarteada2722008-12-04 22:39:13 -05009569 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -04009570 }
James Smarteada2722008-12-04 22:39:13 -05009571 lpfc_destroy_vport_work_array(phba, vports);
9572
9573 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -05009574 fc_remove_host(shost);
9575 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04009576 lpfc_cleanup(vport);
9577
James Smart2e0fef82007-06-17 19:56:36 -05009578 /*
9579 * Bring down the SLI Layer. This step disable all interrupts,
9580 * clears the rings, discards all mailbox commands, and resets
9581 * the HBA.
9582 */
James Smarta257bf92009-04-06 18:48:10 -04009583
Justin P. Mattock48e34d02010-12-30 15:07:58 -08009584 /* HBA interrupt will be disabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -05009585 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -04009586 /* Stop kthread signal shall trigger work_done one more time */
9587 kthread_stop(phba->worker_thread);
9588 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -05009589 lpfc_sli_brdrestart(phba);
9590
James Smart72859902012-01-18 16:25:38 -05009591 kfree(phba->vpi_bmask);
9592 kfree(phba->vpi_ids);
9593
James Smart3772a992009-05-22 14:50:54 -04009594 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05009595 spin_lock_irq(&phba->hbalock);
9596 list_del_init(&vport->listentry);
9597 spin_unlock_irq(&phba->hbalock);
9598
James Smart858c9f62007-06-17 19:56:39 -05009599 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05009600
James Smart912e3ac2011-05-24 11:42:11 -04009601 /* Disable SR-IOV if enabled */
9602 if (phba->cfg_sriov_nr_virtfn)
9603 pci_disable_sriov(pdev);
9604
James Smart5b75da22008-12-04 22:39:35 -05009605 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009606 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -05009607
James Smart858c9f62007-06-17 19:56:39 -05009608 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05009609
9610 /*
9611 * Call scsi_free before mem_free since scsi bufs are released to their
9612 * corresponding pools here.
9613 */
9614 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -04009615 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -05009616
James Smart34b02dc2008-08-24 21:49:55 -04009617 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
9618 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -05009619
James Smart2e0fef82007-06-17 19:56:36 -05009620 /* Free resources associated with SLI2 interface */
9621 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -04009622 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -05009623
9624 /* unmap adapter SLIM and Control Registers */
9625 iounmap(phba->ctrl_regs_memmap_p);
9626 iounmap(phba->slim_memmap_p);
9627
James Smart3772a992009-05-22 14:50:54 -04009628 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -05009629
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05009630 pci_release_selected_regions(pdev, bars);
James Smart2e0fef82007-06-17 19:56:36 -05009631 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05009632}
9633
Linas Vepstas8d63f372007-02-14 14:28:36 -06009634/**
James Smart3772a992009-05-22 14:50:54 -04009635 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05009636 * @pdev: pointer to PCI device
9637 * @msg: power management message
9638 *
James Smart3772a992009-05-22 14:50:54 -04009639 * This routine is to be called from the kernel's PCI subsystem to support
9640 * system Power Management (PM) to device with SLI-3 interface spec. When
9641 * PM invokes this method, it quiesces the device by stopping the driver's
9642 * worker thread for the device, turning off device's interrupt and DMA,
9643 * and bring the device offline. Note that as the driver implements the
9644 * minimum PM requirements to a power-aware driver's PM support for the
9645 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
9646 * to the suspend() method call will be treated as SUSPEND and the driver will
9647 * fully reinitialize its device during resume() method call, the driver will
9648 * set device to PCI_D3hot state in PCI config space instead of setting it
9649 * according to the @msg provided by the PM.
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_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
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;
9660
9661 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9662 "0473 PCI device Power Management suspend.\n");
9663
9664 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -04009665 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3a55b532008-12-04 22:38:54 -05009666 lpfc_offline(phba);
9667 kthread_stop(phba->worker_thread);
9668
9669 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -04009670 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -05009671
9672 /* Save device state to PCI config space */
9673 pci_save_state(pdev);
9674 pci_set_power_state(pdev, PCI_D3hot);
9675
9676 return 0;
9677}
9678
9679/**
James Smart3772a992009-05-22 14:50:54 -04009680 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05009681 * @pdev: pointer to PCI device
9682 *
James Smart3772a992009-05-22 14:50:54 -04009683 * This routine is to be called from the kernel's PCI subsystem to support
9684 * system Power Management (PM) to device with SLI-3 interface spec. When PM
9685 * invokes this method, it restores the device's PCI config space state and
9686 * fully reinitializes the device and brings it online. Note that as the
9687 * driver implements the minimum PM requirements to a power-aware driver's
9688 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
9689 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
9690 * driver will fully reinitialize its device during resume() method call,
9691 * the device will be set to PCI_D0 directly in PCI config space before
9692 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -05009693 *
9694 * Return code
James Smart3772a992009-05-22 14:50:54 -04009695 * 0 - driver suspended the device
9696 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05009697 **/
9698static int
James Smart3772a992009-05-22 14:50:54 -04009699lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -05009700{
9701 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9702 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -05009703 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05009704 int error;
9705
9706 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9707 "0452 PCI device Power Management resume.\n");
9708
9709 /* Restore device state from PCI config space */
9710 pci_set_power_state(pdev, PCI_D0);
9711 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -04009712
James Smart1dfb5a42010-02-12 14:40:50 -05009713 /*
9714 * As the new kernel behavior of pci_restore_state() API call clears
9715 * device saved_state flag, need to save the restored state again.
9716 */
9717 pci_save_state(pdev);
9718
James Smart3a55b532008-12-04 22:38:54 -05009719 if (pdev->is_busmaster)
9720 pci_set_master(pdev);
9721
9722 /* Startup the kernel thread for this host adapter. */
9723 phba->worker_thread = kthread_run(lpfc_do_work, phba,
9724 "lpfc_worker_%d", phba->brd_no);
9725 if (IS_ERR(phba->worker_thread)) {
9726 error = PTR_ERR(phba->worker_thread);
9727 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9728 "0434 PM resume failed to start worker "
9729 "thread: error=x%x.\n", error);
9730 return error;
9731 }
9732
James Smart5b75da22008-12-04 22:39:35 -05009733 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009734 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05009735 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -05009736 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -05009737 "0430 PM resume Failed to enable interrupt\n");
9738 return -EIO;
9739 } else
9740 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05009741
9742 /* Restart HBA and bring it online */
9743 lpfc_sli_brdrestart(phba);
9744 lpfc_online(phba);
9745
James Smart5b75da22008-12-04 22:39:35 -05009746 /* Log the current active interrupt mode */
9747 lpfc_log_intr_mode(phba, phba->intr_mode);
9748
James Smart3a55b532008-12-04 22:38:54 -05009749 return 0;
9750}
9751
9752/**
James Smart891478a2009-11-18 15:40:23 -05009753 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
9754 * @phba: pointer to lpfc hba data structure.
9755 *
9756 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -04009757 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -05009758 **/
9759static void
9760lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
9761{
9762 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9763 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -04009764
9765 /*
9766 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
9767 * and let the SCSI mid-layer to retry them to recover.
9768 */
James Smartdb55fba2014-04-04 13:52:02 -04009769 lpfc_sli_abort_fcp_rings(phba);
James Smart891478a2009-11-18 15:40:23 -05009770}
9771
9772/**
James Smart0d878412009-10-02 15:16:56 -04009773 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
9774 * @phba: pointer to lpfc hba data structure.
9775 *
9776 * This routine is called to prepare the SLI3 device for PCI slot reset. It
9777 * disables the device interrupt and pci device, and aborts the internal FCP
9778 * pending I/Os.
9779 **/
9780static void
9781lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
9782{
James Smart0d878412009-10-02 15:16:56 -04009783 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05009784 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -04009785
James Smart75baf692010-06-08 18:31:21 -04009786 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -04009787 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -04009788
James Smarte2af0d22010-03-15 11:25:32 -04009789 /* Block all SCSI devices' I/Os on the host */
9790 lpfc_scsi_dev_block(phba);
9791
James Smartea714f32013-04-17 20:18:47 -04009792 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
9793 lpfc_sli_flush_fcp_rings(phba);
9794
James Smarte2af0d22010-03-15 11:25:32 -04009795 /* stop all timers */
9796 lpfc_stop_hba_timers(phba);
9797
James Smart0d878412009-10-02 15:16:56 -04009798 /* Disable interrupt and pci device */
9799 lpfc_sli_disable_intr(phba);
9800 pci_disable_device(phba->pcidev);
James Smart0d878412009-10-02 15:16:56 -04009801}
9802
9803/**
9804 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
9805 * @phba: pointer to lpfc hba data structure.
9806 *
9807 * This routine is called to prepare the SLI3 device for PCI slot permanently
9808 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
9809 * pending I/Os.
9810 **/
9811static void
James Smart75baf692010-06-08 18:31:21 -04009812lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
James Smart0d878412009-10-02 15:16:56 -04009813{
9814 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05009815 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -04009816 /* Block all SCSI devices' I/Os on the host */
9817 lpfc_scsi_dev_block(phba);
9818
9819 /* stop all timers */
9820 lpfc_stop_hba_timers(phba);
9821
James Smart0d878412009-10-02 15:16:56 -04009822 /* Clean up all driver's outstanding SCSI I/Os */
9823 lpfc_sli_flush_fcp_rings(phba);
9824}
9825
9826/**
James Smart3772a992009-05-22 14:50:54 -04009827 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -04009828 * @pdev: pointer to PCI device.
9829 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009830 *
James Smart3772a992009-05-22 14:50:54 -04009831 * This routine is called from the PCI subsystem for I/O error handling to
9832 * device with SLI-3 interface spec. This function is called by the PCI
9833 * subsystem after a PCI bus error affecting this device has been detected.
9834 * When this function is invoked, it will need to stop all the I/Os and
9835 * interrupt(s) to the device. Once that is done, it will return
9836 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
9837 * as desired.
James Smarte59058c2008-08-24 21:49:00 -04009838 *
9839 * Return codes
James Smart0d878412009-10-02 15:16:56 -04009840 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -04009841 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
9842 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -04009843 **/
James Smart3772a992009-05-22 14:50:54 -04009844static pci_ers_result_t
9845lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009846{
James Smart51ef4c22007-08-02 11:10:31 -04009847 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9848 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009849
James Smart0d878412009-10-02 15:16:56 -04009850 switch (state) {
9851 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -05009852 /* Non-fatal error, prepare for recovery */
9853 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -04009854 return PCI_ERS_RESULT_CAN_RECOVER;
9855 case pci_channel_io_frozen:
9856 /* Fatal error, prepare for slot reset */
9857 lpfc_sli_prep_dev_for_reset(phba);
9858 return PCI_ERS_RESULT_NEED_RESET;
9859 case pci_channel_io_perm_failure:
9860 /* Permanent failure, prepare for device down */
James Smart75baf692010-06-08 18:31:21 -04009861 lpfc_sli_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009862 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -04009863 default:
9864 /* Unknown state, prepare and request slot reset */
9865 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9866 "0472 Unknown PCI error state: x%x\n", state);
9867 lpfc_sli_prep_dev_for_reset(phba);
9868 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -04009869 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06009870}
9871
9872/**
James Smart3772a992009-05-22 14:50:54 -04009873 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -04009874 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009875 *
James Smart3772a992009-05-22 14:50:54 -04009876 * This routine is called from the PCI subsystem for error handling to
9877 * device with SLI-3 interface spec. This is called after PCI bus has been
9878 * reset to restart the PCI card from scratch, as if from a cold-boot.
9879 * During the PCI subsystem error recovery, after driver returns
9880 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
9881 * recovery and then call this routine before calling the .resume method
9882 * to recover the device. This function will initialize the HBA device,
9883 * enable the interrupt, but it will just put the HBA to offline state
9884 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -04009885 *
9886 * Return codes
James Smart3772a992009-05-22 14:50:54 -04009887 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
9888 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -06009889 */
James Smart3772a992009-05-22 14:50:54 -04009890static pci_ers_result_t
9891lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009892{
James Smart51ef4c22007-08-02 11:10:31 -04009893 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9894 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009895 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -05009896 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009897
9898 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +11009899 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -06009900 printk(KERN_ERR "lpfc: Cannot re-enable "
9901 "PCI device after reset.\n");
9902 return PCI_ERS_RESULT_DISCONNECT;
9903 }
9904
James Smart97207482008-12-04 22:39:19 -05009905 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05009906
9907 /*
9908 * As the new kernel behavior of pci_restore_state() API call clears
9909 * device saved_state flag, need to save the restored state again.
9910 */
9911 pci_save_state(pdev);
9912
James Smart97207482008-12-04 22:39:19 -05009913 if (pdev->is_busmaster)
9914 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009915
James Smart92d7f7b2007-06-17 19:56:38 -05009916 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04009917 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05009918 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009919
James Smart5b75da22008-12-04 22:39:35 -05009920 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009921 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05009922 if (intr_mode == LPFC_INTR_ERROR) {
9923 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9924 "0427 Cannot re-enable interrupt after "
9925 "slot reset.\n");
9926 return PCI_ERS_RESULT_DISCONNECT;
9927 } else
9928 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009929
James Smart75baf692010-06-08 18:31:21 -04009930 /* Take device offline, it will perform cleanup */
James Smart618a5232012-06-12 13:54:36 -04009931 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009932 lpfc_offline(phba);
9933 lpfc_sli_brdrestart(phba);
9934
James Smart5b75da22008-12-04 22:39:35 -05009935 /* Log the current active interrupt mode */
9936 lpfc_log_intr_mode(phba, phba->intr_mode);
9937
Linas Vepstas8d63f372007-02-14 14:28:36 -06009938 return PCI_ERS_RESULT_RECOVERED;
9939}
9940
9941/**
James Smart3772a992009-05-22 14:50:54 -04009942 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -04009943 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -06009944 *
James Smart3772a992009-05-22 14:50:54 -04009945 * This routine is called from the PCI subsystem for error handling to device
9946 * with SLI-3 interface spec. It is called when kernel error recovery tells
9947 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
9948 * error recovery. After this call, traffic can start to flow from this device
9949 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009950 */
James Smart3772a992009-05-22 14:50:54 -04009951static void
9952lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009953{
James Smart51ef4c22007-08-02 11:10:31 -04009954 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9955 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009956
James Smarte2af0d22010-03-15 11:25:32 -04009957 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -04009958 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -04009959
9960 /* Clean up Advanced Error Reporting (AER) if needed */
9961 if (phba->hba_flag & HBA_AER_ENABLED)
9962 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009963}
9964
James Smart3772a992009-05-22 14:50:54 -04009965/**
James Smartda0436e2009-05-22 14:51:39 -04009966 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
9967 * @phba: pointer to lpfc hba data structure.
9968 *
9969 * returns the number of ELS/CT IOCBs to reserve
9970 **/
9971int
9972lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
9973{
9974 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
9975
James Smartf1126682009-06-10 17:22:44 -04009976 if (phba->sli_rev == LPFC_SLI_REV4) {
9977 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -04009978 return 10;
James Smartf1126682009-06-10 17:22:44 -04009979 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -04009980 return 25;
James Smartf1126682009-06-10 17:22:44 -04009981 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -04009982 return 50;
James Smartf1126682009-06-10 17:22:44 -04009983 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -04009984 return 100;
James Smart8a9d2e82012-05-09 21:16:12 -04009985 else if (max_xri <= 1536)
James Smart6a9c52c2009-10-02 15:16:51 -04009986 return 150;
James Smart8a9d2e82012-05-09 21:16:12 -04009987 else if (max_xri <= 2048)
9988 return 200;
9989 else
9990 return 250;
James Smartf1126682009-06-10 17:22:44 -04009991 } else
9992 return 0;
James Smartda0436e2009-05-22 14:51:39 -04009993}
9994
9995/**
James Smart52d52442011-05-24 11:42:45 -04009996 * lpfc_write_firmware - attempt to write a firmware image to the port
James Smart52d52442011-05-24 11:42:45 -04009997 * @fw: pointer to firmware image returned from request_firmware.
James Smartce396282012-09-29 11:30:56 -04009998 * @phba: pointer to lpfc hba data structure.
James Smart52d52442011-05-24 11:42:45 -04009999 *
James Smart52d52442011-05-24 11:42:45 -040010000 **/
James Smartce396282012-09-29 11:30:56 -040010001static void
10002lpfc_write_firmware(const struct firmware *fw, void *context)
James Smart52d52442011-05-24 11:42:45 -040010003{
James Smartce396282012-09-29 11:30:56 -040010004 struct lpfc_hba *phba = (struct lpfc_hba *)context;
James Smart6b5151f2012-01-18 16:24:06 -050010005 char fwrev[FW_REV_STR_SIZE];
James Smartce396282012-09-29 11:30:56 -040010006 struct lpfc_grp_hdr *image;
James Smart52d52442011-05-24 11:42:45 -040010007 struct list_head dma_buffer_list;
10008 int i, rc = 0;
10009 struct lpfc_dmabuf *dmabuf, *next;
10010 uint32_t offset = 0, temp_offset = 0;
10011
James Smartc71ab862012-10-31 14:44:33 -040010012 /* It can be null in no-wait mode, sanity check */
James Smartce396282012-09-29 11:30:56 -040010013 if (!fw) {
10014 rc = -ENXIO;
10015 goto out;
10016 }
10017 image = (struct lpfc_grp_hdr *)fw->data;
10018
James Smart52d52442011-05-24 11:42:45 -040010019 INIT_LIST_HEAD(&dma_buffer_list);
James Smart079b5c92011-08-21 21:48:49 -040010020 if ((be32_to_cpu(image->magic_number) != LPFC_GROUP_OJECT_MAGIC_NUM) ||
10021 (bf_get_be32(lpfc_grp_hdr_file_type, image) !=
10022 LPFC_FILE_TYPE_GROUP) ||
10023 (bf_get_be32(lpfc_grp_hdr_id, image) != LPFC_FILE_ID_GROUP) ||
10024 (be32_to_cpu(image->size) != fw->size)) {
James Smart52d52442011-05-24 11:42:45 -040010025 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10026 "3022 Invalid FW image found. "
James Smart079b5c92011-08-21 21:48:49 -040010027 "Magic:%x Type:%x ID:%x\n",
10028 be32_to_cpu(image->magic_number),
10029 bf_get_be32(lpfc_grp_hdr_file_type, image),
10030 bf_get_be32(lpfc_grp_hdr_id, image));
James Smartce396282012-09-29 11:30:56 -040010031 rc = -EINVAL;
10032 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010033 }
10034 lpfc_decode_firmware_rev(phba, fwrev, 1);
James Smart88a2cfb2011-07-22 18:36:33 -040010035 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
James Smart52d52442011-05-24 11:42:45 -040010036 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartce396282012-09-29 11:30:56 -040010037 "3023 Updating Firmware, Current Version:%s "
James Smart52d52442011-05-24 11:42:45 -040010038 "New Version:%s\n",
James Smart88a2cfb2011-07-22 18:36:33 -040010039 fwrev, image->revision);
James Smart52d52442011-05-24 11:42:45 -040010040 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
10041 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
10042 GFP_KERNEL);
10043 if (!dmabuf) {
10044 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040010045 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010046 }
10047 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
10048 SLI4_PAGE_SIZE,
10049 &dmabuf->phys,
10050 GFP_KERNEL);
10051 if (!dmabuf->virt) {
10052 kfree(dmabuf);
10053 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -040010054 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010055 }
10056 list_add_tail(&dmabuf->list, &dma_buffer_list);
10057 }
10058 while (offset < fw->size) {
10059 temp_offset = offset;
10060 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
James Smart079b5c92011-08-21 21:48:49 -040010061 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
James Smart52d52442011-05-24 11:42:45 -040010062 memcpy(dmabuf->virt,
10063 fw->data + temp_offset,
James Smart079b5c92011-08-21 21:48:49 -040010064 fw->size - temp_offset);
10065 temp_offset = fw->size;
James Smart52d52442011-05-24 11:42:45 -040010066 break;
10067 }
James Smart52d52442011-05-24 11:42:45 -040010068 memcpy(dmabuf->virt, fw->data + temp_offset,
10069 SLI4_PAGE_SIZE);
James Smart88a2cfb2011-07-22 18:36:33 -040010070 temp_offset += SLI4_PAGE_SIZE;
James Smart52d52442011-05-24 11:42:45 -040010071 }
10072 rc = lpfc_wr_object(phba, &dma_buffer_list,
10073 (fw->size - offset), &offset);
James Smartce396282012-09-29 11:30:56 -040010074 if (rc)
10075 goto release_out;
James Smart52d52442011-05-24 11:42:45 -040010076 }
10077 rc = offset;
10078 }
James Smartce396282012-09-29 11:30:56 -040010079
10080release_out:
James Smart52d52442011-05-24 11:42:45 -040010081 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
10082 list_del(&dmabuf->list);
10083 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
10084 dmabuf->virt, dmabuf->phys);
10085 kfree(dmabuf);
10086 }
James Smartce396282012-09-29 11:30:56 -040010087 release_firmware(fw);
10088out:
10089 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartc71ab862012-10-31 14:44:33 -040010090 "3024 Firmware update done: %d.\n", rc);
James Smartce396282012-09-29 11:30:56 -040010091 return;
James Smart52d52442011-05-24 11:42:45 -040010092}
10093
10094/**
James Smartc71ab862012-10-31 14:44:33 -040010095 * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
10096 * @phba: pointer to lpfc hba data structure.
10097 *
10098 * This routine is called to perform Linux generic firmware upgrade on device
10099 * that supports such feature.
10100 **/
10101int
10102lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
10103{
10104 uint8_t file_name[ELX_MODEL_NAME_SIZE];
10105 int ret;
10106 const struct firmware *fw;
10107
10108 /* Only supported on SLI4 interface type 2 for now */
10109 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
10110 LPFC_SLI_INTF_IF_TYPE_2)
10111 return -EPERM;
10112
10113 snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
10114
10115 if (fw_upgrade == INT_FW_UPGRADE) {
10116 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
10117 file_name, &phba->pcidev->dev,
10118 GFP_KERNEL, (void *)phba,
10119 lpfc_write_firmware);
10120 } else if (fw_upgrade == RUN_FW_UPGRADE) {
10121 ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
10122 if (!ret)
10123 lpfc_write_firmware(fw, (void *)phba);
10124 } else {
10125 ret = -EINVAL;
10126 }
10127
10128 return ret;
10129}
10130
10131/**
James Smartda0436e2009-05-22 14:51:39 -040010132 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
10133 * @pdev: pointer to PCI device
10134 * @pid: pointer to PCI device identifier
10135 *
10136 * This routine is called from the kernel's PCI subsystem to device with
10137 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
10138 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
10139 * information of the device and driver to see if the driver state that it
10140 * can support this kind of device. If the match is successful, the driver
10141 * core invokes this routine. If this routine determines it can claim the HBA,
10142 * it does all the initialization that it needs to do to handle the HBA
10143 * properly.
10144 *
10145 * Return code
10146 * 0 - driver can claim the device
10147 * negative value - driver can not claim the device
10148 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010149static int
James Smartda0436e2009-05-22 14:51:39 -040010150lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
10151{
10152 struct lpfc_hba *phba;
10153 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -040010154 struct Scsi_Host *shost = NULL;
James Smartce396282012-09-29 11:30:56 -040010155 int error, ret;
James Smartda0436e2009-05-22 14:51:39 -040010156 uint32_t cfg_mode, intr_mode;
James Smart67d12732012-08-03 12:36:13 -040010157 int adjusted_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -040010158
10159 /* Allocate memory for HBA structure */
10160 phba = lpfc_hba_alloc(pdev);
10161 if (!phba)
10162 return -ENOMEM;
10163
10164 /* Perform generic PCI device enabling operation */
10165 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -040010166 if (error)
James Smartda0436e2009-05-22 14:51:39 -040010167 goto out_free_phba;
James Smartda0436e2009-05-22 14:51:39 -040010168
10169 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
10170 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
10171 if (error)
10172 goto out_disable_pci_dev;
10173
10174 /* Set up SLI-4 specific device PCI memory space */
10175 error = lpfc_sli4_pci_mem_setup(phba);
10176 if (error) {
10177 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10178 "1410 Failed to set up pci memory space.\n");
10179 goto out_disable_pci_dev;
10180 }
10181
10182 /* Set up phase-1 common device driver resources */
10183 error = lpfc_setup_driver_resource_phase1(phba);
10184 if (error) {
10185 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10186 "1411 Failed to set up driver resource.\n");
10187 goto out_unset_pci_mem_s4;
10188 }
10189
10190 /* Set up SLI-4 Specific device driver resources */
10191 error = lpfc_sli4_driver_resource_setup(phba);
10192 if (error) {
10193 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10194 "1412 Failed to set up driver resource.\n");
10195 goto out_unset_pci_mem_s4;
10196 }
10197
10198 /* Initialize and populate the iocb list per host */
James Smart2a9bf3d2010-06-07 15:24:45 -040010199
10200 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10201 "2821 initialize iocb list %d.\n",
10202 phba->cfg_iocb_cnt*1024);
10203 error = lpfc_init_iocb_list(phba, phba->cfg_iocb_cnt*1024);
10204
James Smartda0436e2009-05-22 14:51:39 -040010205 if (error) {
10206 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10207 "1413 Failed to initialize iocb list.\n");
10208 goto out_unset_driver_resource_s4;
10209 }
10210
James Smart19ca7602010-11-20 23:11:55 -050010211 INIT_LIST_HEAD(&phba->active_rrq_list);
James Smart7d791df2011-07-22 18:37:52 -040010212 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
James Smart19ca7602010-11-20 23:11:55 -050010213
James Smartda0436e2009-05-22 14:51:39 -040010214 /* Set up common device driver resources */
10215 error = lpfc_setup_driver_resource_phase2(phba);
10216 if (error) {
10217 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10218 "1414 Failed to set up driver resource.\n");
10219 goto out_free_iocb_list;
10220 }
10221
James Smart079b5c92011-08-21 21:48:49 -040010222 /* Get the default values for Model Name and Description */
10223 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
10224
James Smartda0436e2009-05-22 14:51:39 -040010225 /* Create SCSI host to the physical port */
10226 error = lpfc_create_shost(phba);
10227 if (error) {
10228 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10229 "1415 Failed to create scsi host.\n");
10230 goto out_unset_driver_resource;
10231 }
10232
10233 /* Configure sysfs attributes */
10234 vport = phba->pport;
10235 error = lpfc_alloc_sysfs_attr(vport);
10236 if (error) {
10237 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10238 "1416 Failed to allocate sysfs attr\n");
10239 goto out_destroy_shost;
10240 }
10241
James Smart6669f9b2009-10-02 15:16:45 -040010242 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -040010243 /* Now, trying to enable interrupt and bring up the device */
10244 cfg_mode = phba->cfg_use_msi;
James Smartda0436e2009-05-22 14:51:39 -040010245
James Smart7b15db32013-01-03 15:43:29 -050010246 /* Put device to a known state before enabling interrupt */
10247 lpfc_stop_port(phba);
10248 /* Configure and enable interrupt */
10249 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
10250 if (intr_mode == LPFC_INTR_ERROR) {
10251 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10252 "0426 Failed to enable interrupt.\n");
10253 error = -ENODEV;
10254 goto out_free_sysfs_attr;
James Smartda0436e2009-05-22 14:51:39 -040010255 }
James Smart7b15db32013-01-03 15:43:29 -050010256 /* Default to single EQ for non-MSI-X */
10257 if (phba->intr_type != MSIX)
10258 adjusted_fcp_io_channel = 1;
10259 else
10260 adjusted_fcp_io_channel = phba->cfg_fcp_io_channel;
10261 phba->cfg_fcp_io_channel = adjusted_fcp_io_channel;
10262 /* Set up SLI-4 HBA */
10263 if (lpfc_sli4_hba_setup(phba)) {
10264 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10265 "1421 Failed to set up hba\n");
10266 error = -ENODEV;
10267 goto out_disable_intr;
10268 }
10269
10270 /* Log the current active interrupt mode */
10271 phba->intr_mode = intr_mode;
10272 lpfc_log_intr_mode(phba, intr_mode);
James Smartda0436e2009-05-22 14:51:39 -040010273
10274 /* Perform post initialization setup */
10275 lpfc_post_init_setup(phba);
10276
James Smartc71ab862012-10-31 14:44:33 -040010277 /* check for firmware upgrade or downgrade */
10278 if (phba->cfg_request_firmware_upgrade)
10279 ret = lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
James Smart52d52442011-05-24 11:42:45 -040010280
James Smart1c6834a2009-07-19 10:01:26 -040010281 /* Check if there are static vports to be created. */
10282 lpfc_create_static_vport(phba);
James Smartda0436e2009-05-22 14:51:39 -040010283 return 0;
10284
10285out_disable_intr:
10286 lpfc_sli4_disable_intr(phba);
10287out_free_sysfs_attr:
10288 lpfc_free_sysfs_attr(vport);
10289out_destroy_shost:
10290 lpfc_destroy_shost(phba);
10291out_unset_driver_resource:
10292 lpfc_unset_driver_resource_phase2(phba);
10293out_free_iocb_list:
10294 lpfc_free_iocb_list(phba);
10295out_unset_driver_resource_s4:
10296 lpfc_sli4_driver_resource_unset(phba);
10297out_unset_pci_mem_s4:
10298 lpfc_sli4_pci_mem_unset(phba);
10299out_disable_pci_dev:
10300 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040010301 if (shost)
10302 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -040010303out_free_phba:
10304 lpfc_hba_free(phba);
10305 return error;
10306}
10307
10308/**
10309 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
10310 * @pdev: pointer to PCI device
10311 *
10312 * This routine is called from the kernel's PCI subsystem to device with
10313 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
10314 * removed from PCI bus, it performs all the necessary cleanup for the HBA
10315 * device to be removed from the PCI subsystem properly.
10316 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010317static void
James Smartda0436e2009-05-22 14:51:39 -040010318lpfc_pci_remove_one_s4(struct pci_dev *pdev)
10319{
10320 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10321 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
10322 struct lpfc_vport **vports;
10323 struct lpfc_hba *phba = vport->phba;
10324 int i;
10325
10326 /* Mark the device unloading flag */
10327 spin_lock_irq(&phba->hbalock);
10328 vport->load_flag |= FC_UNLOADING;
10329 spin_unlock_irq(&phba->hbalock);
10330
10331 /* Free the HBA sysfs attributes */
10332 lpfc_free_sysfs_attr(vport);
10333
10334 /* Release all the vports against this physical port */
10335 vports = lpfc_create_vport_work_array(phba);
10336 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040010337 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
10338 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
10339 continue;
James Smartda0436e2009-05-22 14:51:39 -040010340 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040010341 }
James Smartda0436e2009-05-22 14:51:39 -040010342 lpfc_destroy_vport_work_array(phba, vports);
10343
10344 /* Remove FC host and then SCSI host with the physical port */
10345 fc_remove_host(shost);
10346 scsi_remove_host(shost);
10347
10348 /* Perform cleanup on the physical port */
10349 lpfc_cleanup(vport);
10350
10351 /*
10352 * Bring down the SLI Layer. This step disables all interrupts,
10353 * clears the rings, discards all mailbox commands, and resets
10354 * the HBA FCoE function.
10355 */
10356 lpfc_debugfs_terminate(vport);
10357 lpfc_sli4_hba_unset(phba);
10358
10359 spin_lock_irq(&phba->hbalock);
10360 list_del_init(&vport->listentry);
10361 spin_unlock_irq(&phba->hbalock);
10362
James Smart3677a3a2010-09-29 11:19:14 -040010363 /* Perform scsi free before driver resource_unset since scsi
James Smartda0436e2009-05-22 14:51:39 -040010364 * buffers are released to their corresponding pools here.
10365 */
10366 lpfc_scsi_free(phba);
James Smart67d12732012-08-03 12:36:13 -040010367
James Smartda0436e2009-05-22 14:51:39 -040010368 lpfc_sli4_driver_resource_unset(phba);
10369
10370 /* Unmap adapter Control and Doorbell registers */
10371 lpfc_sli4_pci_mem_unset(phba);
10372
10373 /* Release PCI resources and disable device's PCI function */
10374 scsi_host_put(shost);
10375 lpfc_disable_pci_dev(phba);
10376
10377 /* Finally, free the driver's device data structure */
10378 lpfc_hba_free(phba);
10379
10380 return;
10381}
10382
10383/**
10384 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
10385 * @pdev: pointer to PCI device
10386 * @msg: power management message
10387 *
10388 * This routine is called from the kernel's PCI subsystem to support system
10389 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
10390 * this method, it quiesces the device by stopping the driver's worker
10391 * thread for the device, turning off device's interrupt and DMA, and bring
10392 * the device offline. Note that as the driver implements the minimum PM
10393 * requirements to a power-aware driver's PM support for suspend/resume -- all
10394 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
10395 * method call will be treated as SUSPEND and the driver will fully
10396 * reinitialize its device during resume() method call, the driver will set
10397 * device to PCI_D3hot state in PCI config space instead of setting it
10398 * according to the @msg provided by the PM.
10399 *
10400 * Return code
10401 * 0 - driver suspended the device
10402 * Error otherwise
10403 **/
10404static int
10405lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
10406{
10407 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10408 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10409
10410 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart75baf692010-06-08 18:31:21 -040010411 "2843 PCI device Power Management suspend.\n");
James Smartda0436e2009-05-22 14:51:39 -040010412
10413 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040010414 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smartda0436e2009-05-22 14:51:39 -040010415 lpfc_offline(phba);
10416 kthread_stop(phba->worker_thread);
10417
10418 /* Disable interrupt from device */
10419 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040010420 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -040010421
10422 /* Save device state to PCI config space */
10423 pci_save_state(pdev);
10424 pci_set_power_state(pdev, PCI_D3hot);
10425
10426 return 0;
10427}
10428
10429/**
10430 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
10431 * @pdev: pointer to PCI device
10432 *
10433 * This routine is called from the kernel's PCI subsystem to support system
10434 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
10435 * this method, it restores the device's PCI config space state and fully
10436 * reinitializes the device and brings it online. Note that as the driver
10437 * implements the minimum PM requirements to a power-aware driver's PM for
10438 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
10439 * to the suspend() method call will be treated as SUSPEND and the driver
10440 * will fully reinitialize its device during resume() method call, the device
10441 * will be set to PCI_D0 directly in PCI config space before restoring the
10442 * state.
10443 *
10444 * Return code
10445 * 0 - driver suspended the device
10446 * Error otherwise
10447 **/
10448static int
10449lpfc_pci_resume_one_s4(struct pci_dev *pdev)
10450{
10451 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10452 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10453 uint32_t intr_mode;
10454 int error;
10455
10456 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10457 "0292 PCI device Power Management resume.\n");
10458
10459 /* Restore device state from PCI config space */
10460 pci_set_power_state(pdev, PCI_D0);
10461 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050010462
10463 /*
10464 * As the new kernel behavior of pci_restore_state() API call clears
10465 * device saved_state flag, need to save the restored state again.
10466 */
10467 pci_save_state(pdev);
10468
James Smartda0436e2009-05-22 14:51:39 -040010469 if (pdev->is_busmaster)
10470 pci_set_master(pdev);
10471
10472 /* Startup the kernel thread for this host adapter. */
10473 phba->worker_thread = kthread_run(lpfc_do_work, phba,
10474 "lpfc_worker_%d", phba->brd_no);
10475 if (IS_ERR(phba->worker_thread)) {
10476 error = PTR_ERR(phba->worker_thread);
10477 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10478 "0293 PM resume failed to start worker "
10479 "thread: error=x%x.\n", error);
10480 return error;
10481 }
10482
10483 /* Configure and enable interrupt */
10484 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
10485 if (intr_mode == LPFC_INTR_ERROR) {
10486 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10487 "0294 PM resume Failed to enable interrupt\n");
10488 return -EIO;
10489 } else
10490 phba->intr_mode = intr_mode;
10491
10492 /* Restart HBA and bring it online */
10493 lpfc_sli_brdrestart(phba);
10494 lpfc_online(phba);
10495
10496 /* Log the current active interrupt mode */
10497 lpfc_log_intr_mode(phba, phba->intr_mode);
10498
10499 return 0;
10500}
10501
10502/**
James Smart75baf692010-06-08 18:31:21 -040010503 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
10504 * @phba: pointer to lpfc hba data structure.
10505 *
10506 * This routine is called to prepare the SLI4 device for PCI slot recover. It
10507 * aborts all the outstanding SCSI I/Os to the pci device.
10508 **/
10509static void
10510lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
10511{
James Smart75baf692010-06-08 18:31:21 -040010512 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10513 "2828 PCI channel I/O abort preparing for recovery\n");
10514 /*
10515 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
10516 * and let the SCSI mid-layer to retry them to recover.
10517 */
James Smartdb55fba2014-04-04 13:52:02 -040010518 lpfc_sli_abort_fcp_rings(phba);
James Smart75baf692010-06-08 18:31:21 -040010519}
10520
10521/**
10522 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
10523 * @phba: pointer to lpfc hba data structure.
10524 *
10525 * This routine is called to prepare the SLI4 device for PCI slot reset. It
10526 * disables the device interrupt and pci device, and aborts the internal FCP
10527 * pending I/Os.
10528 **/
10529static void
10530lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
10531{
10532 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10533 "2826 PCI channel disable preparing for reset\n");
10534
10535 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040010536 lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
James Smart75baf692010-06-08 18:31:21 -040010537
10538 /* Block all SCSI devices' I/Os on the host */
10539 lpfc_scsi_dev_block(phba);
10540
James Smartea714f32013-04-17 20:18:47 -040010541 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
10542 lpfc_sli_flush_fcp_rings(phba);
10543
James Smart75baf692010-06-08 18:31:21 -040010544 /* stop all timers */
10545 lpfc_stop_hba_timers(phba);
10546
10547 /* Disable interrupt and pci device */
10548 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040010549 lpfc_sli4_queue_destroy(phba);
James Smart75baf692010-06-08 18:31:21 -040010550 pci_disable_device(phba->pcidev);
James Smart75baf692010-06-08 18:31:21 -040010551}
10552
10553/**
10554 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
10555 * @phba: pointer to lpfc hba data structure.
10556 *
10557 * This routine is called to prepare the SLI4 device for PCI slot permanently
10558 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
10559 * pending I/Os.
10560 **/
10561static void
10562lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
10563{
10564 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10565 "2827 PCI channel permanent disable for failure\n");
10566
10567 /* Block all SCSI devices' I/Os on the host */
10568 lpfc_scsi_dev_block(phba);
10569
10570 /* stop all timers */
10571 lpfc_stop_hba_timers(phba);
10572
10573 /* Clean up all driver's outstanding SCSI I/Os */
10574 lpfc_sli_flush_fcp_rings(phba);
10575}
10576
10577/**
James Smartda0436e2009-05-22 14:51:39 -040010578 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
10579 * @pdev: pointer to PCI device.
10580 * @state: the current PCI connection state.
10581 *
10582 * This routine is called from the PCI subsystem for error handling to device
10583 * with SLI-4 interface spec. This function is called by the PCI subsystem
10584 * after a PCI bus error affecting this device has been detected. When this
10585 * function is invoked, it will need to stop all the I/Os and interrupt(s)
10586 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
10587 * for the PCI subsystem to perform proper recovery as desired.
10588 *
10589 * Return codes
10590 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10591 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10592 **/
10593static pci_ers_result_t
10594lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
10595{
James Smart75baf692010-06-08 18:31:21 -040010596 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10597 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10598
10599 switch (state) {
10600 case pci_channel_io_normal:
10601 /* Non-fatal error, prepare for recovery */
10602 lpfc_sli4_prep_dev_for_recover(phba);
10603 return PCI_ERS_RESULT_CAN_RECOVER;
10604 case pci_channel_io_frozen:
10605 /* Fatal error, prepare for slot reset */
10606 lpfc_sli4_prep_dev_for_reset(phba);
10607 return PCI_ERS_RESULT_NEED_RESET;
10608 case pci_channel_io_perm_failure:
10609 /* Permanent failure, prepare for device down */
10610 lpfc_sli4_prep_dev_for_perm_failure(phba);
10611 return PCI_ERS_RESULT_DISCONNECT;
10612 default:
10613 /* Unknown state, prepare and request slot reset */
10614 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10615 "2825 Unknown PCI error state: x%x\n", state);
10616 lpfc_sli4_prep_dev_for_reset(phba);
10617 return PCI_ERS_RESULT_NEED_RESET;
10618 }
James Smartda0436e2009-05-22 14:51:39 -040010619}
10620
10621/**
10622 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
10623 * @pdev: pointer to PCI device.
10624 *
10625 * This routine is called from the PCI subsystem for error handling to device
10626 * with SLI-4 interface spec. It is called after PCI bus has been reset to
10627 * restart the PCI card from scratch, as if from a cold-boot. During the
10628 * PCI subsystem error recovery, after the driver returns
10629 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
10630 * recovery and then call this routine before calling the .resume method to
10631 * recover the device. This function will initialize the HBA device, enable
10632 * the interrupt, but it will just put the HBA to offline state without
10633 * passing any I/O traffic.
10634 *
10635 * Return codes
10636 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10637 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10638 */
10639static pci_ers_result_t
10640lpfc_io_slot_reset_s4(struct pci_dev *pdev)
10641{
James Smart75baf692010-06-08 18:31:21 -040010642 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10643 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10644 struct lpfc_sli *psli = &phba->sli;
10645 uint32_t intr_mode;
10646
10647 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
10648 if (pci_enable_device_mem(pdev)) {
10649 printk(KERN_ERR "lpfc: Cannot re-enable "
10650 "PCI device after reset.\n");
10651 return PCI_ERS_RESULT_DISCONNECT;
10652 }
10653
10654 pci_restore_state(pdev);
James Smart0a96e972011-07-22 18:37:28 -040010655
10656 /*
10657 * As the new kernel behavior of pci_restore_state() API call clears
10658 * device saved_state flag, need to save the restored state again.
10659 */
10660 pci_save_state(pdev);
10661
James Smart75baf692010-06-08 18:31:21 -040010662 if (pdev->is_busmaster)
10663 pci_set_master(pdev);
10664
10665 spin_lock_irq(&phba->hbalock);
10666 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
10667 spin_unlock_irq(&phba->hbalock);
10668
10669 /* Configure and enable interrupt */
10670 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
10671 if (intr_mode == LPFC_INTR_ERROR) {
10672 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10673 "2824 Cannot re-enable interrupt after "
10674 "slot reset.\n");
10675 return PCI_ERS_RESULT_DISCONNECT;
10676 } else
10677 phba->intr_mode = intr_mode;
10678
10679 /* Log the current active interrupt mode */
10680 lpfc_log_intr_mode(phba, phba->intr_mode);
10681
James Smartda0436e2009-05-22 14:51:39 -040010682 return PCI_ERS_RESULT_RECOVERED;
10683}
10684
10685/**
10686 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
10687 * @pdev: pointer to PCI device
10688 *
10689 * This routine is called from the PCI subsystem for error handling to device
10690 * with SLI-4 interface spec. It is called when kernel error recovery tells
10691 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
10692 * error recovery. After this call, traffic can start to flow from this device
10693 * again.
10694 **/
10695static void
10696lpfc_io_resume_s4(struct pci_dev *pdev)
10697{
James Smart75baf692010-06-08 18:31:21 -040010698 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10699 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10700
10701 /*
10702 * In case of slot reset, as function reset is performed through
10703 * mailbox command which needs DMA to be enabled, this operation
10704 * has to be moved to the io resume phase. Taking device offline
10705 * will perform the necessary cleanup.
10706 */
10707 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
10708 /* Perform device reset */
James Smart618a5232012-06-12 13:54:36 -040010709 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040010710 lpfc_offline(phba);
10711 lpfc_sli_brdrestart(phba);
10712 /* Bring the device back online */
10713 lpfc_online(phba);
10714 }
10715
10716 /* Clean up Advanced Error Reporting (AER) if needed */
10717 if (phba->hba_flag & HBA_AER_ENABLED)
10718 pci_cleanup_aer_uncorrect_error_status(pdev);
James Smartda0436e2009-05-22 14:51:39 -040010719}
10720
10721/**
James Smart3772a992009-05-22 14:50:54 -040010722 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
10723 * @pdev: pointer to PCI device
10724 * @pid: pointer to PCI device identifier
10725 *
10726 * This routine is to be registered to the kernel's PCI subsystem. When an
10727 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
10728 * at PCI device-specific information of the device and driver to see if the
10729 * driver state that it can support this kind of device. If the match is
10730 * successful, the driver core invokes this routine. This routine dispatches
10731 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
10732 * do all the initialization that it needs to do to handle the HBA device
10733 * properly.
10734 *
10735 * Return code
10736 * 0 - driver can claim the device
10737 * negative value - driver can not claim the device
10738 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010739static int
James Smart3772a992009-05-22 14:50:54 -040010740lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
10741{
10742 int rc;
James Smart8fa38512009-07-19 10:01:03 -040010743 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -040010744
James Smart28baac72010-02-12 14:42:03 -050010745 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -040010746 return -ENODEV;
10747
James Smart8fa38512009-07-19 10:01:03 -040010748 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -050010749 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -040010750 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040010751 else
James Smart3772a992009-05-22 14:50:54 -040010752 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040010753
James Smart3772a992009-05-22 14:50:54 -040010754 return rc;
10755}
10756
10757/**
10758 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
10759 * @pdev: pointer to PCI device
10760 *
10761 * This routine is to be registered to the kernel's PCI subsystem. When an
10762 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
10763 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
10764 * remove routine, which will perform all the necessary cleanup for the
10765 * device to be removed from the PCI subsystem properly.
10766 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010767static void
James Smart3772a992009-05-22 14:50:54 -040010768lpfc_pci_remove_one(struct pci_dev *pdev)
10769{
10770 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10771 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10772
10773 switch (phba->pci_dev_grp) {
10774 case LPFC_PCI_DEV_LP:
10775 lpfc_pci_remove_one_s3(pdev);
10776 break;
James Smartda0436e2009-05-22 14:51:39 -040010777 case LPFC_PCI_DEV_OC:
10778 lpfc_pci_remove_one_s4(pdev);
10779 break;
James Smart3772a992009-05-22 14:50:54 -040010780 default:
10781 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10782 "1424 Invalid PCI device group: 0x%x\n",
10783 phba->pci_dev_grp);
10784 break;
10785 }
10786 return;
10787}
10788
10789/**
10790 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
10791 * @pdev: pointer to PCI device
10792 * @msg: power management message
10793 *
10794 * This routine is to be registered to the kernel's PCI subsystem to support
10795 * system Power Management (PM). When PM invokes this method, it dispatches
10796 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
10797 * suspend the device.
10798 *
10799 * Return code
10800 * 0 - driver suspended the device
10801 * Error otherwise
10802 **/
10803static int
10804lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
10805{
10806 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10807 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10808 int rc = -ENODEV;
10809
10810 switch (phba->pci_dev_grp) {
10811 case LPFC_PCI_DEV_LP:
10812 rc = lpfc_pci_suspend_one_s3(pdev, msg);
10813 break;
James Smartda0436e2009-05-22 14:51:39 -040010814 case LPFC_PCI_DEV_OC:
10815 rc = lpfc_pci_suspend_one_s4(pdev, msg);
10816 break;
James Smart3772a992009-05-22 14:50:54 -040010817 default:
10818 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10819 "1425 Invalid PCI device group: 0x%x\n",
10820 phba->pci_dev_grp);
10821 break;
10822 }
10823 return rc;
10824}
10825
10826/**
10827 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
10828 * @pdev: pointer to PCI device
10829 *
10830 * This routine is to be registered to the kernel's PCI subsystem to support
10831 * system Power Management (PM). When PM invokes this method, it dispatches
10832 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
10833 * resume the device.
10834 *
10835 * Return code
10836 * 0 - driver suspended the device
10837 * Error otherwise
10838 **/
10839static int
10840lpfc_pci_resume_one(struct pci_dev *pdev)
10841{
10842 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10843 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10844 int rc = -ENODEV;
10845
10846 switch (phba->pci_dev_grp) {
10847 case LPFC_PCI_DEV_LP:
10848 rc = lpfc_pci_resume_one_s3(pdev);
10849 break;
James Smartda0436e2009-05-22 14:51:39 -040010850 case LPFC_PCI_DEV_OC:
10851 rc = lpfc_pci_resume_one_s4(pdev);
10852 break;
James Smart3772a992009-05-22 14:50:54 -040010853 default:
10854 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10855 "1426 Invalid PCI device group: 0x%x\n",
10856 phba->pci_dev_grp);
10857 break;
10858 }
10859 return rc;
10860}
10861
10862/**
10863 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
10864 * @pdev: pointer to PCI device.
10865 * @state: the current PCI connection state.
10866 *
10867 * This routine is registered to the PCI subsystem for error handling. This
10868 * function is called by the PCI subsystem after a PCI bus error affecting
10869 * this device has been detected. When this routine is invoked, it dispatches
10870 * the action to the proper SLI-3 or SLI-4 device error detected handling
10871 * routine, which will perform the proper error detected operation.
10872 *
10873 * Return codes
10874 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10875 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10876 **/
10877static pci_ers_result_t
10878lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
10879{
10880 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10881 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10882 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
10883
10884 switch (phba->pci_dev_grp) {
10885 case LPFC_PCI_DEV_LP:
10886 rc = lpfc_io_error_detected_s3(pdev, state);
10887 break;
James Smartda0436e2009-05-22 14:51:39 -040010888 case LPFC_PCI_DEV_OC:
10889 rc = lpfc_io_error_detected_s4(pdev, state);
10890 break;
James Smart3772a992009-05-22 14:50:54 -040010891 default:
10892 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10893 "1427 Invalid PCI device group: 0x%x\n",
10894 phba->pci_dev_grp);
10895 break;
10896 }
10897 return rc;
10898}
10899
10900/**
10901 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
10902 * @pdev: pointer to PCI device.
10903 *
10904 * This routine is registered to the PCI subsystem for error handling. This
10905 * function is called after PCI bus has been reset to restart the PCI card
10906 * from scratch, as if from a cold-boot. When this routine is invoked, it
10907 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
10908 * routine, which will perform the proper device reset.
10909 *
10910 * Return codes
10911 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10912 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10913 **/
10914static pci_ers_result_t
10915lpfc_io_slot_reset(struct pci_dev *pdev)
10916{
10917 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10918 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10919 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
10920
10921 switch (phba->pci_dev_grp) {
10922 case LPFC_PCI_DEV_LP:
10923 rc = lpfc_io_slot_reset_s3(pdev);
10924 break;
James Smartda0436e2009-05-22 14:51:39 -040010925 case LPFC_PCI_DEV_OC:
10926 rc = lpfc_io_slot_reset_s4(pdev);
10927 break;
James Smart3772a992009-05-22 14:50:54 -040010928 default:
10929 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10930 "1428 Invalid PCI device group: 0x%x\n",
10931 phba->pci_dev_grp);
10932 break;
10933 }
10934 return rc;
10935}
10936
10937/**
10938 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
10939 * @pdev: pointer to PCI device
10940 *
10941 * This routine is registered to the PCI subsystem for error handling. It
10942 * is called when kernel error recovery tells the lpfc driver that it is
10943 * OK to resume normal PCI operation after PCI bus error recovery. When
10944 * this routine is invoked, it dispatches the action to the proper SLI-3
10945 * or SLI-4 device io_resume routine, which will resume the device operation.
10946 **/
10947static void
10948lpfc_io_resume(struct pci_dev *pdev)
10949{
10950 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10951 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10952
10953 switch (phba->pci_dev_grp) {
10954 case LPFC_PCI_DEV_LP:
10955 lpfc_io_resume_s3(pdev);
10956 break;
James Smartda0436e2009-05-22 14:51:39 -040010957 case LPFC_PCI_DEV_OC:
10958 lpfc_io_resume_s4(pdev);
10959 break;
James Smart3772a992009-05-22 14:50:54 -040010960 default:
10961 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10962 "1429 Invalid PCI device group: 0x%x\n",
10963 phba->pci_dev_grp);
10964 break;
10965 }
10966 return;
10967}
10968
James Smart1ba981f2014-02-20 09:56:45 -050010969/**
10970 * lpfc_sli4_oas_verify - Verify OAS is supported by this adapter
10971 * @phba: pointer to lpfc hba data structure.
10972 *
10973 * This routine checks to see if OAS is supported for this adapter. If
10974 * supported, the configure Flash Optimized Fabric flag is set. Otherwise,
10975 * the enable oas flag is cleared and the pool created for OAS device data
10976 * is destroyed.
10977 *
10978 **/
10979void
10980lpfc_sli4_oas_verify(struct lpfc_hba *phba)
10981{
10982
10983 if (!phba->cfg_EnableXLane)
10984 return;
10985
10986 if (phba->sli4_hba.pc_sli4_params.oas_supported) {
10987 phba->cfg_fof = 1;
10988 } else {
James Smartf38fa0b2014-04-04 13:52:21 -040010989 phba->cfg_fof = 0;
James Smart1ba981f2014-02-20 09:56:45 -050010990 if (phba->device_data_mem_pool)
10991 mempool_destroy(phba->device_data_mem_pool);
10992 phba->device_data_mem_pool = NULL;
10993 }
10994
10995 return;
10996}
10997
10998/**
10999 * lpfc_fof_queue_setup - Set up all the fof queues
11000 * @phba: pointer to lpfc hba data structure.
11001 *
11002 * This routine is invoked to set up all the fof queues for the FC HBA
11003 * operation.
11004 *
11005 * Return codes
11006 * 0 - successful
11007 * -ENOMEM - No available memory
11008 **/
11009int
11010lpfc_fof_queue_setup(struct lpfc_hba *phba)
11011{
11012 struct lpfc_sli *psli = &phba->sli;
11013 int rc;
11014
11015 rc = lpfc_eq_create(phba, phba->sli4_hba.fof_eq, LPFC_MAX_IMAX);
11016 if (rc)
11017 return -ENOMEM;
11018
James Smartf38fa0b2014-04-04 13:52:21 -040011019 if (phba->cfg_fof) {
James Smart1ba981f2014-02-20 09:56:45 -050011020
11021 rc = lpfc_cq_create(phba, phba->sli4_hba.oas_cq,
11022 phba->sli4_hba.fof_eq, LPFC_WCQ, LPFC_FCP);
11023 if (rc)
11024 goto out_oas_cq;
11025
11026 rc = lpfc_wq_create(phba, phba->sli4_hba.oas_wq,
11027 phba->sli4_hba.oas_cq, LPFC_FCP);
11028 if (rc)
11029 goto out_oas_wq;
11030
11031 phba->sli4_hba.oas_cq->pring = &psli->ring[LPFC_FCP_OAS_RING];
11032 phba->sli4_hba.oas_ring = &psli->ring[LPFC_FCP_OAS_RING];
11033 }
11034
11035 return 0;
11036
11037out_oas_wq:
James Smartf38fa0b2014-04-04 13:52:21 -040011038 lpfc_cq_destroy(phba, phba->sli4_hba.oas_cq);
James Smart1ba981f2014-02-20 09:56:45 -050011039out_oas_cq:
11040 lpfc_eq_destroy(phba, phba->sli4_hba.fof_eq);
11041 return rc;
11042
11043}
11044
11045/**
11046 * lpfc_fof_queue_create - Create all the fof queues
11047 * @phba: pointer to lpfc hba data structure.
11048 *
11049 * This routine is invoked to allocate all the fof queues for the FC HBA
11050 * operation. For each SLI4 queue type, the parameters such as queue entry
11051 * count (queue depth) shall be taken from the module parameter. For now,
11052 * we just use some constant number as place holder.
11053 *
11054 * Return codes
11055 * 0 - successful
11056 * -ENOMEM - No availble memory
11057 * -EIO - The mailbox failed to complete successfully.
11058 **/
11059int
11060lpfc_fof_queue_create(struct lpfc_hba *phba)
11061{
11062 struct lpfc_queue *qdesc;
11063
11064 /* Create FOF EQ */
11065 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
11066 phba->sli4_hba.eq_ecount);
11067 if (!qdesc)
11068 goto out_error;
11069
11070 phba->sli4_hba.fof_eq = qdesc;
11071
James Smartf38fa0b2014-04-04 13:52:21 -040011072 if (phba->cfg_fof) {
James Smart1ba981f2014-02-20 09:56:45 -050011073
11074 /* Create OAS CQ */
11075 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
11076 phba->sli4_hba.cq_ecount);
11077 if (!qdesc)
11078 goto out_error;
11079
11080 phba->sli4_hba.oas_cq = qdesc;
11081
11082 /* Create OAS WQ */
11083 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
11084 phba->sli4_hba.wq_ecount);
11085 if (!qdesc)
11086 goto out_error;
11087
11088 phba->sli4_hba.oas_wq = qdesc;
11089
11090 }
11091 return 0;
11092
11093out_error:
11094 lpfc_fof_queue_destroy(phba);
11095 return -ENOMEM;
11096}
11097
11098/**
11099 * lpfc_fof_queue_destroy - Destroy all the fof queues
11100 * @phba: pointer to lpfc hba data structure.
11101 *
11102 * This routine is invoked to release all the SLI4 queues with the FC HBA
11103 * operation.
11104 *
11105 * Return codes
11106 * 0 - successful
11107 **/
11108int
11109lpfc_fof_queue_destroy(struct lpfc_hba *phba)
11110{
11111 /* Release FOF Event queue */
11112 if (phba->sli4_hba.fof_eq != NULL) {
11113 lpfc_sli4_queue_free(phba->sli4_hba.fof_eq);
11114 phba->sli4_hba.fof_eq = NULL;
11115 }
11116
11117 /* Release OAS Completion queue */
11118 if (phba->sli4_hba.oas_cq != NULL) {
11119 lpfc_sli4_queue_free(phba->sli4_hba.oas_cq);
11120 phba->sli4_hba.oas_cq = NULL;
11121 }
11122
11123 /* Release OAS Work queue */
11124 if (phba->sli4_hba.oas_wq != NULL) {
11125 lpfc_sli4_queue_free(phba->sli4_hba.oas_wq);
11126 phba->sli4_hba.oas_wq = NULL;
11127 }
11128 return 0;
11129}
11130
dea31012005-04-17 16:05:31 -050011131static struct pci_device_id lpfc_id_table[] = {
11132 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
11133 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -040011134 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
11135 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011136 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
11137 PCI_ANY_ID, PCI_ANY_ID, },
11138 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
11139 PCI_ANY_ID, PCI_ANY_ID, },
11140 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
11141 PCI_ANY_ID, PCI_ANY_ID, },
11142 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
11143 PCI_ANY_ID, PCI_ANY_ID, },
11144 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
11145 PCI_ANY_ID, PCI_ANY_ID, },
11146 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
11147 PCI_ANY_ID, PCI_ANY_ID, },
11148 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
11149 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050011150 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
11151 PCI_ANY_ID, PCI_ANY_ID, },
11152 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
11153 PCI_ANY_ID, PCI_ANY_ID, },
11154 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
11155 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011156 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
11157 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050011158 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
11159 PCI_ANY_ID, PCI_ANY_ID, },
11160 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
11161 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011162 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
11163 PCI_ANY_ID, PCI_ANY_ID, },
11164 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
11165 PCI_ANY_ID, PCI_ANY_ID, },
11166 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
11167 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -040011168 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
11169 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050011170 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
11171 PCI_ANY_ID, PCI_ANY_ID, },
11172 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
11173 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011174 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
11175 PCI_ANY_ID, PCI_ANY_ID, },
11176 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
11177 PCI_ANY_ID, PCI_ANY_ID, },
11178 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
11179 PCI_ANY_ID, PCI_ANY_ID, },
11180 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
11181 PCI_ANY_ID, PCI_ANY_ID, },
11182 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
11183 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050011184 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
11185 PCI_ANY_ID, PCI_ANY_ID, },
11186 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
11187 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -040011188 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
11189 PCI_ANY_ID, PCI_ANY_ID, },
11190 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
11191 PCI_ANY_ID, PCI_ANY_ID, },
11192 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
11193 PCI_ANY_ID, PCI_ANY_ID, },
11194 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
11195 PCI_ANY_ID, PCI_ANY_ID, },
11196 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
11197 PCI_ANY_ID, PCI_ANY_ID, },
11198 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
11199 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -040011200 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
11201 PCI_ANY_ID, PCI_ANY_ID, },
11202 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
11203 PCI_ANY_ID, PCI_ANY_ID, },
11204 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
11205 PCI_ANY_ID, PCI_ANY_ID, },
James Smart3772a992009-05-22 14:50:54 -040011206 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
11207 PCI_ANY_ID, PCI_ANY_ID, },
James Smarta747c9c2009-11-18 15:41:10 -050011208 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
11209 PCI_ANY_ID, PCI_ANY_ID, },
11210 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
James Smart6669f9b2009-10-02 15:16:45 -040011211 PCI_ANY_ID, PCI_ANY_ID, },
James Smart98fc5dd2010-06-07 15:24:29 -040011212 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BALIUS,
11213 PCI_ANY_ID, PCI_ANY_ID, },
James Smart085c6472010-11-20 23:11:37 -050011214 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC,
11215 PCI_ANY_ID, PCI_ANY_ID, },
11216 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE,
11217 PCI_ANY_ID, PCI_ANY_ID, },
James Smartc0c11512011-05-24 11:41:34 -040011218 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC_VF,
11219 PCI_ANY_ID, PCI_ANY_ID, },
11220 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE_VF,
11221 PCI_ANY_ID, PCI_ANY_ID, },
James Smartf8cafd32012-08-03 12:42:51 -040011222 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SKYHAWK,
11223 PCI_ANY_ID, PCI_ANY_ID, },
11224 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SKYHAWK_VF,
11225 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011226 { 0 }
11227};
11228
11229MODULE_DEVICE_TABLE(pci, lpfc_id_table);
11230
Stephen Hemmingera55b2d22012-09-07 09:33:16 -070011231static const struct pci_error_handlers lpfc_err_handler = {
Linas Vepstas8d63f372007-02-14 14:28:36 -060011232 .error_detected = lpfc_io_error_detected,
11233 .slot_reset = lpfc_io_slot_reset,
11234 .resume = lpfc_io_resume,
11235};
11236
dea31012005-04-17 16:05:31 -050011237static struct pci_driver lpfc_driver = {
11238 .name = LPFC_DRIVER_NAME,
11239 .id_table = lpfc_id_table,
11240 .probe = lpfc_pci_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011241 .remove = lpfc_pci_remove_one,
James Smart3a55b532008-12-04 22:38:54 -050011242 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -040011243 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -050011244 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -050011245};
11246
James Smart3ef6d242012-01-18 16:23:48 -050011247static const struct file_operations lpfc_mgmt_fop = {
Al Viro858feac2013-04-14 22:39:37 -040011248 .owner = THIS_MODULE,
James Smart3ef6d242012-01-18 16:23:48 -050011249};
11250
11251static struct miscdevice lpfc_mgmt_dev = {
11252 .minor = MISC_DYNAMIC_MINOR,
11253 .name = "lpfcmgmt",
11254 .fops = &lpfc_mgmt_fop,
11255};
11256
James Smarte59058c2008-08-24 21:49:00 -040011257/**
James Smart3621a712009-04-06 18:47:14 -040011258 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -040011259 *
11260 * This routine is to be invoked when the lpfc module is loaded into the
11261 * kernel. The special kernel macro module_init() is used to indicate the
11262 * role of this routine to the kernel as lpfc module entry point.
11263 *
11264 * Return codes
11265 * 0 - successful
11266 * -ENOMEM - FC attach transport failed
11267 * all others - failed
11268 */
dea31012005-04-17 16:05:31 -050011269static int __init
11270lpfc_init(void)
11271{
James Smart7bb03bb2013-04-17 20:19:16 -040011272 int cpu;
dea31012005-04-17 16:05:31 -050011273 int error = 0;
11274
11275 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040011276 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -050011277
James Smart3ef6d242012-01-18 16:23:48 -050011278 error = misc_register(&lpfc_mgmt_dev);
11279 if (error)
11280 printk(KERN_ERR "Could not register lpfcmgmt device, "
11281 "misc_register returned with status %d", error);
11282
James Smart7ee5d432007-10-27 13:37:17 -040011283 if (lpfc_enable_npiv) {
11284 lpfc_transport_functions.vport_create = lpfc_vport_create;
11285 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
11286 }
dea31012005-04-17 16:05:31 -050011287 lpfc_transport_template =
11288 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -040011289 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -050011290 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -040011291 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -040011292 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -040011293 fc_attach_transport(&lpfc_vport_transport_functions);
11294 if (lpfc_vport_transport_template == NULL) {
11295 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040011296 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -040011297 }
James Smart7ee5d432007-10-27 13:37:17 -040011298 }
James Smart7bb03bb2013-04-17 20:19:16 -040011299
11300 /* Initialize in case vector mapping is needed */
James Smartb246de12013-05-31 17:03:07 -040011301 lpfc_used_cpu = NULL;
11302 lpfc_present_cpu = 0;
11303 for_each_present_cpu(cpu)
11304 lpfc_present_cpu++;
James Smart7bb03bb2013-04-17 20:19:16 -040011305
dea31012005-04-17 16:05:31 -050011306 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -050011307 if (error) {
dea31012005-04-17 16:05:31 -050011308 fc_release_transport(lpfc_transport_template);
James Smartd7c255b2008-08-24 21:50:00 -040011309 if (lpfc_enable_npiv)
11310 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -050011311 }
dea31012005-04-17 16:05:31 -050011312
11313 return error;
11314}
11315
James Smarte59058c2008-08-24 21:49:00 -040011316/**
James Smart3621a712009-04-06 18:47:14 -040011317 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -040011318 *
11319 * This routine is invoked when the lpfc module is removed from the kernel.
11320 * The special kernel macro module_exit() is used to indicate the role of
11321 * this routine to the kernel as lpfc module exit point.
11322 */
dea31012005-04-17 16:05:31 -050011323static void __exit
11324lpfc_exit(void)
11325{
James Smart3ef6d242012-01-18 16:23:48 -050011326 misc_deregister(&lpfc_mgmt_dev);
dea31012005-04-17 16:05:31 -050011327 pci_unregister_driver(&lpfc_driver);
11328 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040011329 if (lpfc_enable_npiv)
11330 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -050011331 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040011332 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
11333 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050011334 (1L << _dump_buf_data_order), _dump_buf_data);
11335 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
11336 }
11337
11338 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -040011339 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
11340 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050011341 (1L << _dump_buf_dif_order), _dump_buf_dif);
11342 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
11343 }
James Smartb246de12013-05-31 17:03:07 -040011344 kfree(lpfc_used_cpu);
dea31012005-04-17 16:05:31 -050011345}
11346
11347module_init(lpfc_init);
11348module_exit(lpfc_exit);
11349MODULE_LICENSE("GPL");
11350MODULE_DESCRIPTION(LPFC_MODULE_DESC);
11351MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
11352MODULE_VERSION("0:" LPFC_DRIVER_VERSION);