blob: f622c88113447fd1b1252bb67f7c378e24f83b4a [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smart92c13f22013-05-31 17:05:45 -04004 * Copyright (C) 2004-2013 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/delay.h>
24#include <linux/dma-mapping.h>
25#include <linux/idr.h>
26#include <linux/interrupt.h>
Paul Gortmakeracf3368f2011-05-27 09:47:43 -040027#include <linux/module.h>
dea31012005-04-17 16:05:31 -050028#include <linux/kthread.h>
29#include <linux/pci.h>
30#include <linux/spinlock.h>
James Smart92d7f7b2007-06-17 19:56:38 -050031#include <linux/ctype.h>
James Smart0d878412009-10-02 15:16:56 -040032#include <linux/aer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
James Smart52d52442011-05-24 11:42:45 -040034#include <linux/firmware.h>
James Smart3ef6d242012-01-18 16:23:48 -050035#include <linux/miscdevice.h>
James Smart7bb03bb2013-04-17 20:19:16 -040036#include <linux/percpu.h>
dea31012005-04-17 16:05:31 -050037
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040038#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050039#include <scsi/scsi_device.h>
40#include <scsi/scsi_host.h>
41#include <scsi/scsi_transport_fc.h>
42
James Smartda0436e2009-05-22 14:51:39 -040043#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050044#include "lpfc_hw.h"
45#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040046#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040047#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050048#include "lpfc_disc.h"
49#include "lpfc_scsi.h"
50#include "lpfc.h"
51#include "lpfc_logmsg.h"
52#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050053#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050054#include "lpfc_version.h"
55
James Smart81301a92008-12-04 22:39:46 -050056char *_dump_buf_data;
57unsigned long _dump_buf_data_order;
58char *_dump_buf_dif;
59unsigned long _dump_buf_dif_order;
60spinlock_t _dump_buf_lock;
61
James Smart7bb03bb2013-04-17 20:19:16 -040062/* Used when mapping IRQ vectors in a driver centric manner */
James Smartb246de12013-05-31 17:03:07 -040063uint16_t *lpfc_used_cpu;
64uint32_t lpfc_present_cpu;
James Smart7bb03bb2013-04-17 20:19:16 -040065
dea31012005-04-17 16:05:31 -050066static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
67static int lpfc_post_rcv_buf(struct lpfc_hba *);
James Smart5350d872011-10-10 21:33:49 -040068static int lpfc_sli4_queue_verify(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040069static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
70static int lpfc_setup_endian_order(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040071static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
James Smart8a9d2e82012-05-09 21:16:12 -040072static void lpfc_free_els_sgl_list(struct lpfc_hba *);
73static void lpfc_init_sgl_list(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040074static int lpfc_init_active_sgl_array(struct lpfc_hba *);
75static void lpfc_free_active_sgl(struct lpfc_hba *);
76static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
77static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
78static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
79static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
80static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
James Smart618a5232012-06-12 13:54:36 -040081static void lpfc_sli4_disable_intr(struct lpfc_hba *);
82static uint32_t lpfc_sli4_enable_intr(struct lpfc_hba *, uint32_t);
James Smart1ba981f2014-02-20 09:56:45 -050083static void lpfc_sli4_oas_verify(struct lpfc_hba *phba);
dea31012005-04-17 16:05:31 -050084
85static struct scsi_transport_template *lpfc_transport_template = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050086static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea31012005-04-17 16:05:31 -050087static DEFINE_IDR(lpfc_hba_index);
88
James Smarte59058c2008-08-24 21:49:00 -040089/**
James Smart3621a712009-04-06 18:47:14 -040090 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
James Smarte59058c2008-08-24 21:49:00 -040091 * @phba: pointer to lpfc hba data structure.
92 *
93 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
94 * mailbox command. It retrieves the revision information from the HBA and
95 * collects the Vital Product Data (VPD) about the HBA for preparing the
96 * configuration of the HBA.
97 *
98 * Return codes:
99 * 0 - success.
100 * -ERESTART - requests the SLI layer to reset the HBA and try again.
101 * Any other value - indicates an error.
102 **/
dea31012005-04-17 16:05:31 -0500103int
James Smart2e0fef82007-06-17 19:56:36 -0500104lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500105{
106 lpfc_vpd_t *vp = &phba->vpd;
107 int i = 0, rc;
108 LPFC_MBOXQ_t *pmb;
109 MAILBOX_t *mb;
110 char *lpfc_vpd_data = NULL;
111 uint16_t offset = 0;
112 static char licensed[56] =
113 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -0400114 static int init_key = 1;
dea31012005-04-17 16:05:31 -0500115
116 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
117 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500118 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500119 return -ENOMEM;
120 }
121
James Smart04c68492009-05-22 14:52:52 -0400122 mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -0500123 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500124
125 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -0400126 if (init_key) {
127 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -0500128
James Smart65a29c12006-07-06 15:50:50 -0400129 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
130 *ptext = cpu_to_be32(*ptext);
131 init_key = 0;
132 }
dea31012005-04-17 16:05:31 -0500133
134 lpfc_read_nv(phba, pmb);
135 memset((char*)mb->un.varRDnvp.rsvd3, 0,
136 sizeof (mb->un.varRDnvp.rsvd3));
137 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
138 sizeof (licensed));
139
140 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
141
142 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500143 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smarte8b62012007-08-02 11:10:09 -0400144 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500145 "error, mbxCmd x%x READ_NVPARM, "
146 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500147 mb->mbxCommand, mb->mbxStatus);
148 mempool_free(pmb, phba->mbox_mem_pool);
149 return -ERESTART;
150 }
151 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500152 sizeof(phba->wwnn));
153 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
154 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500155 }
156
James Smart92d7f7b2007-06-17 19:56:38 -0500157 phba->sli3_options = 0x0;
158
dea31012005-04-17 16:05:31 -0500159 /* Setup and issue mailbox READ REV command */
160 lpfc_read_rev(phba, pmb);
161 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
162 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500163 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400164 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500165 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500166 mb->mbxCommand, mb->mbxStatus);
167 mempool_free( pmb, phba->mbox_mem_pool);
168 return -ERESTART;
169 }
170
James Smart92d7f7b2007-06-17 19:56:38 -0500171
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500172 /*
173 * The value of rr must be 1 since the driver set the cv field to 1.
174 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500175 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500176 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500177 vp->rev.rBit = 0;
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500178 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400179 "0440 Adapter failed to init, READ_REV has "
180 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500181 mempool_free(pmb, phba->mbox_mem_pool);
182 return -ERESTART;
dea31012005-04-17 16:05:31 -0500183 }
184
James Smart495a7142008-06-14 22:52:59 -0400185 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
186 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -0500187 return -EINVAL;
James Smart495a7142008-06-14 22:52:59 -0400188 }
James Smarted957682007-06-17 19:56:37 -0500189
dea31012005-04-17 16:05:31 -0500190 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500191 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500192 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500193 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
194 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
195 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
196 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500197 vp->rev.biuRev = mb->un.varRdRev.biuRev;
198 vp->rev.smRev = mb->un.varRdRev.smRev;
199 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
200 vp->rev.endecRev = mb->un.varRdRev.endecRev;
201 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
202 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
203 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
204 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
205 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
206 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
207
James Smart92d7f7b2007-06-17 19:56:38 -0500208 /* If the sli feature level is less then 9, we must
209 * tear down all RPIs and VPIs on link down if NPIV
210 * is enabled.
211 */
212 if (vp->rev.feaLevelHigh < 9)
213 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
214
dea31012005-04-17 16:05:31 -0500215 if (lpfc_is_LC_HBA(phba->pcidev->device))
216 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
217 sizeof (phba->RandomData));
218
dea31012005-04-17 16:05:31 -0500219 /* Get adapter VPD information */
dea31012005-04-17 16:05:31 -0500220 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
221 if (!lpfc_vpd_data)
James Smartd7c255b2008-08-24 21:50:00 -0400222 goto out_free_mbox;
dea31012005-04-17 16:05:31 -0500223 do {
James Smarta0c87cb2009-07-19 10:01:10 -0400224 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
dea31012005-04-17 16:05:31 -0500225 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
226
227 if (rc != MBX_SUCCESS) {
228 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400229 "0441 VPD not present on adapter, "
dea31012005-04-17 16:05:31 -0500230 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500231 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500232 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500233 }
James Smart04c68492009-05-22 14:52:52 -0400234 /* dump mem may return a zero when finished or we got a
235 * mailbox error, either way we are done.
236 */
237 if (mb->un.varDmp.word_cnt == 0)
238 break;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500239 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
240 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
James Smartd7c255b2008-08-24 21:50:00 -0400241 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
242 lpfc_vpd_data + offset,
James Smart92d7f7b2007-06-17 19:56:38 -0500243 mb->un.varDmp.word_cnt);
dea31012005-04-17 16:05:31 -0500244 offset += mb->un.varDmp.word_cnt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500245 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
246 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500247
248 kfree(lpfc_vpd_data);
dea31012005-04-17 16:05:31 -0500249out_free_mbox:
250 mempool_free(pmb, phba->mbox_mem_pool);
251 return 0;
252}
253
James Smarte59058c2008-08-24 21:49:00 -0400254/**
James Smart3621a712009-04-06 18:47:14 -0400255 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
James Smarte59058c2008-08-24 21:49:00 -0400256 * @phba: pointer to lpfc hba data structure.
257 * @pmboxq: pointer to the driver internal queue element for mailbox command.
258 *
259 * This is the completion handler for driver's configuring asynchronous event
260 * mailbox command to the device. If the mailbox command returns successfully,
261 * it will set internal async event support flag to 1; otherwise, it will
262 * set internal async event support flag to 0.
263 **/
James Smart57127f12007-10-27 13:37:05 -0400264static void
265lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
266{
James Smart04c68492009-05-22 14:52:52 -0400267 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
James Smart57127f12007-10-27 13:37:05 -0400268 phba->temp_sensor_support = 1;
269 else
270 phba->temp_sensor_support = 0;
271 mempool_free(pmboxq, phba->mbox_mem_pool);
272 return;
273}
274
James Smarte59058c2008-08-24 21:49:00 -0400275/**
James Smart3621a712009-04-06 18:47:14 -0400276 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
James Smart97207482008-12-04 22:39:19 -0500277 * @phba: pointer to lpfc hba data structure.
278 * @pmboxq: pointer to the driver internal queue element for mailbox command.
279 *
280 * This is the completion handler for dump mailbox command for getting
281 * wake up parameters. When this command complete, the response contain
282 * Option rom version of the HBA. This function translate the version number
283 * into a human readable string and store it in OptionROMVersion.
284 **/
285static void
286lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
287{
288 struct prog_id *prg;
289 uint32_t prog_id_word;
290 char dist = ' ';
291 /* character array used for decoding dist type. */
292 char dist_char[] = "nabx";
293
James Smart04c68492009-05-22 14:52:52 -0400294 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
James Smart9f1e1b52008-12-04 22:39:40 -0500295 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500296 return;
James Smart9f1e1b52008-12-04 22:39:40 -0500297 }
James Smart97207482008-12-04 22:39:19 -0500298
299 prg = (struct prog_id *) &prog_id_word;
300
301 /* word 7 contain option rom version */
James Smart04c68492009-05-22 14:52:52 -0400302 prog_id_word = pmboxq->u.mb.un.varWords[7];
James Smart97207482008-12-04 22:39:19 -0500303
304 /* Decode the Option rom version word to a readable string */
305 if (prg->dist < 4)
306 dist = dist_char[prg->dist];
307
308 if ((prg->dist == 3) && (prg->num == 0))
309 sprintf(phba->OptionROMVersion, "%d.%d%d",
310 prg->ver, prg->rev, prg->lev);
311 else
312 sprintf(phba->OptionROMVersion, "%d.%d%d%c%d",
313 prg->ver, prg->rev, prg->lev,
314 dist, prg->num);
James Smart9f1e1b52008-12-04 22:39:40 -0500315 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500316 return;
317}
318
319/**
James Smart05580562011-05-24 11:40:48 -0400320 * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
321 * cfg_soft_wwnn, cfg_soft_wwpn
322 * @vport: pointer to lpfc vport data structure.
323 *
324 *
325 * Return codes
326 * None.
327 **/
328void
329lpfc_update_vport_wwn(struct lpfc_vport *vport)
330{
331 /* If the soft name exists then update it using the service params */
332 if (vport->phba->cfg_soft_wwnn)
333 u64_to_wwn(vport->phba->cfg_soft_wwnn,
334 vport->fc_sparam.nodeName.u.wwn);
335 if (vport->phba->cfg_soft_wwpn)
336 u64_to_wwn(vport->phba->cfg_soft_wwpn,
337 vport->fc_sparam.portName.u.wwn);
338
339 /*
340 * If the name is empty or there exists a soft name
341 * then copy the service params name, otherwise use the fc name
342 */
343 if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
344 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
345 sizeof(struct lpfc_name));
346 else
347 memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
348 sizeof(struct lpfc_name));
349
350 if (vport->fc_portname.u.wwn[0] == 0 || vport->phba->cfg_soft_wwpn)
351 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
352 sizeof(struct lpfc_name));
353 else
354 memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
355 sizeof(struct lpfc_name));
356}
357
358/**
James Smart3621a712009-04-06 18:47:14 -0400359 * lpfc_config_port_post - Perform lpfc initialization after config port
James Smarte59058c2008-08-24 21:49:00 -0400360 * @phba: pointer to lpfc hba data structure.
361 *
362 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
363 * command call. It performs all internal resource and state setups on the
364 * port: post IOCB buffers, enable appropriate host interrupt attentions,
365 * ELS ring timers, etc.
366 *
367 * Return codes
368 * 0 - success.
369 * Any other value - error.
370 **/
dea31012005-04-17 16:05:31 -0500371int
James Smart2e0fef82007-06-17 19:56:36 -0500372lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500373{
James Smart2e0fef82007-06-17 19:56:36 -0500374 struct lpfc_vport *vport = phba->pport;
James Smarta257bf92009-04-06 18:48:10 -0400375 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -0500376 LPFC_MBOXQ_t *pmb;
377 MAILBOX_t *mb;
378 struct lpfc_dmabuf *mp;
379 struct lpfc_sli *psli = &phba->sli;
380 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500381 int i, j;
382 int rc;
dea31012005-04-17 16:05:31 -0500383
James Smart7af67052007-10-27 13:38:11 -0400384 spin_lock_irq(&phba->hbalock);
385 /*
386 * If the Config port completed correctly the HBA is not
387 * over heated any more.
388 */
389 if (phba->over_temp_state == HBA_OVER_TEMP)
390 phba->over_temp_state = HBA_NORMAL_TEMP;
391 spin_unlock_irq(&phba->hbalock);
392
dea31012005-04-17 16:05:31 -0500393 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
394 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500395 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500396 return -ENOMEM;
397 }
James Smart04c68492009-05-22 14:52:52 -0400398 mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500399
dea31012005-04-17 16:05:31 -0500400 /* Get login parameters for NID. */
James Smart9f1177a2010-02-26 14:12:57 -0500401 rc = lpfc_read_sparam(phba, pmb, 0);
402 if (rc) {
403 mempool_free(pmb, phba->mbox_mem_pool);
404 return -ENOMEM;
405 }
406
James Smarted957682007-06-17 19:56:37 -0500407 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500408 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500409 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400410 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500411 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500412 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500413 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500414 mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart9f1177a2010-02-26 14:12:57 -0500415 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500416 lpfc_mbuf_free(phba, mp->virt, mp->phys);
417 kfree(mp);
418 return -EIO;
419 }
420
421 mp = (struct lpfc_dmabuf *) pmb->context1;
422
James Smart2e0fef82007-06-17 19:56:36 -0500423 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500424 lpfc_mbuf_free(phba, mp->virt, mp->phys);
425 kfree(mp);
426 pmb->context1 = NULL;
James Smart05580562011-05-24 11:40:48 -0400427 lpfc_update_vport_wwn(vport);
James Smarta257bf92009-04-06 18:48:10 -0400428
429 /* Update the fc_host data structures with new wwn. */
430 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
431 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart21e9a0a2009-05-22 14:53:21 -0400432 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smarta257bf92009-04-06 18:48:10 -0400433
dea31012005-04-17 16:05:31 -0500434 /* If no serial number in VPD data, use low 6 bytes of WWNN */
435 /* This should be consolidated into parse_vpd ? - mr */
436 if (phba->SerialNumber[0] == 0) {
437 uint8_t *outptr;
438
James Smart2e0fef82007-06-17 19:56:36 -0500439 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500440 for (i = 0; i < 12; i++) {
441 status = *outptr++;
442 j = ((status & 0xf0) >> 4);
443 if (j <= 9)
444 phba->SerialNumber[i] =
445 (char)((uint8_t) 0x30 + (uint8_t) j);
446 else
447 phba->SerialNumber[i] =
448 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
449 i++;
450 j = (status & 0xf);
451 if (j <= 9)
452 phba->SerialNumber[i] =
453 (char)((uint8_t) 0x30 + (uint8_t) j);
454 else
455 phba->SerialNumber[i] =
456 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
457 }
458 }
459
dea31012005-04-17 16:05:31 -0500460 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500461 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500462 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500463 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400464 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500465 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500466 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500467 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500468 mempool_free( pmb, phba->mbox_mem_pool);
469 return -EIO;
470 }
471
James Smarta0c87cb2009-07-19 10:01:10 -0400472 /* Check if the port is disabled */
473 lpfc_sli_read_link_ste(phba);
474
dea31012005-04-17 16:05:31 -0500475 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -0400476 i = (mb->un.varRdConfig.max_xri + 1);
477 if (phba->cfg_hba_queue_depth > i) {
478 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
479 "3359 HBA queue depth changed from %d to %d\n",
480 phba->cfg_hba_queue_depth, i);
481 phba->cfg_hba_queue_depth = i;
482 }
483
484 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
485 i = (mb->un.varRdConfig.max_xri >> 3);
486 if (phba->pport->cfg_lun_queue_depth > i) {
487 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
488 "3360 LUN queue depth changed from %d to %d\n",
489 phba->pport->cfg_lun_queue_depth, i);
490 phba->pport->cfg_lun_queue_depth = i;
491 }
dea31012005-04-17 16:05:31 -0500492
493 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500494
495 /* Get the default values for Model Name and Description */
496 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
497
James Smart2e0fef82007-06-17 19:56:36 -0500498 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500499
James Smart0b727fe2007-10-27 13:37:25 -0400500 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smart7e56aa22012-08-03 12:35:34 -0400501 if (psli->ring[psli->extra_ring].sli.sli3.cmdringaddr)
James Smarta4bc3372006-12-02 13:34:16 -0500502 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
James Smart7e56aa22012-08-03 12:35:34 -0400503 if (psli->ring[psli->fcp_ring].sli.sli3.cmdringaddr)
dea31012005-04-17 16:05:31 -0500504 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
James Smart7e56aa22012-08-03 12:35:34 -0400505 if (psli->ring[psli->next_ring].sli.sli3.cmdringaddr)
dea31012005-04-17 16:05:31 -0500506 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
507
508 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500509 if (phba->sli_rev != 3)
510 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500511
James Smart93996272008-08-24 21:50:30 -0400512 /*
513 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
514 */
515 if (phba->intr_type == MSIX) {
516 rc = lpfc_config_msi(phba, pmb);
517 if (rc) {
518 mempool_free(pmb, phba->mbox_mem_pool);
519 return -EIO;
520 }
521 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
522 if (rc != MBX_SUCCESS) {
523 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
524 "0352 Config MSI mailbox command "
525 "failed, mbxCmd x%x, mbxStatus x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400526 pmb->u.mb.mbxCommand,
527 pmb->u.mb.mbxStatus);
James Smart93996272008-08-24 21:50:30 -0400528 mempool_free(pmb, phba->mbox_mem_pool);
529 return -EIO;
530 }
531 }
532
James Smart04c68492009-05-22 14:52:52 -0400533 spin_lock_irq(&phba->hbalock);
James Smart93996272008-08-24 21:50:30 -0400534 /* Initialize ERATT handling flag */
535 phba->hba_flag &= ~HBA_ERATT_HANDLED;
536
dea31012005-04-17 16:05:31 -0500537 /* Enable appropriate host interrupts */
James Smart9940b972011-03-11 16:06:12 -0500538 if (lpfc_readl(phba->HCregaddr, &status)) {
539 spin_unlock_irq(&phba->hbalock);
540 return -EIO;
541 }
dea31012005-04-17 16:05:31 -0500542 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
543 if (psli->num_rings > 0)
544 status |= HC_R0INT_ENA;
545 if (psli->num_rings > 1)
546 status |= HC_R1INT_ENA;
547 if (psli->num_rings > 2)
548 status |= HC_R2INT_ENA;
549 if (psli->num_rings > 3)
550 status |= HC_R3INT_ENA;
551
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500552 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
553 (phba->cfg_poll & DISABLE_FCP_RING_INT))
James Smart93996272008-08-24 21:50:30 -0400554 status &= ~(HC_R0INT_ENA);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500555
dea31012005-04-17 16:05:31 -0500556 writel(status, phba->HCregaddr);
557 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500558 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500559
James Smart93996272008-08-24 21:50:30 -0400560 /* Set up ring-0 (ELS) timer */
561 timeout = phba->fc_ratov * 2;
James Smart256ec0d2013-04-17 20:14:58 -0400562 mod_timer(&vport->els_tmofunc,
563 jiffies + msecs_to_jiffies(1000 * timeout));
James Smart93996272008-08-24 21:50:30 -0400564 /* Set up heart beat (HB) timer */
James Smart256ec0d2013-04-17 20:14:58 -0400565 mod_timer(&phba->hb_tmofunc,
566 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -0500567 phba->hb_outstanding = 0;
568 phba->last_completion_time = jiffies;
James Smart93996272008-08-24 21:50:30 -0400569 /* Set up error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -0400570 mod_timer(&phba->eratt_poll,
571 jiffies + msecs_to_jiffies(1000 * LPFC_ERATT_POLL_INTERVAL));
dea31012005-04-17 16:05:31 -0500572
James Smarta0c87cb2009-07-19 10:01:10 -0400573 if (phba->hba_flag & LINK_DISABLED) {
574 lpfc_printf_log(phba,
575 KERN_ERR, LOG_INIT,
576 "2598 Adapter Link is disabled.\n");
577 lpfc_down_link(phba, pmb);
578 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
579 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
580 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
581 lpfc_printf_log(phba,
582 KERN_ERR, LOG_INIT,
583 "2599 Adapter failed to issue DOWN_LINK"
584 " mbox command rc 0x%x\n", rc);
585
586 mempool_free(pmb, phba->mbox_mem_pool);
587 return -EIO;
588 }
James Smarte40a02c2010-02-26 14:13:54 -0500589 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart026abb82011-12-13 13:20:45 -0500590 mempool_free(pmb, phba->mbox_mem_pool);
591 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
592 if (rc)
593 return rc;
dea31012005-04-17 16:05:31 -0500594 }
595 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400596 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500597 if (!pmb) {
598 phba->link_state = LPFC_HBA_ERROR;
599 return -ENOMEM;
600 }
601
James Smart57127f12007-10-27 13:37:05 -0400602 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
603 pmb->mbox_cmpl = lpfc_config_async_cmpl;
604 pmb->vport = phba->pport;
605 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500606
James Smart57127f12007-10-27 13:37:05 -0400607 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
608 lpfc_printf_log(phba,
609 KERN_ERR,
610 LOG_INIT,
611 "0456 Adapter failed to issue "
James Smarte4e74272009-07-19 10:01:38 -0400612 "ASYNCEVT_ENABLE mbox status x%x\n",
James Smart57127f12007-10-27 13:37:05 -0400613 rc);
614 mempool_free(pmb, phba->mbox_mem_pool);
615 }
James Smart97207482008-12-04 22:39:19 -0500616
617 /* Get Option rom version */
618 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500619 if (!pmb) {
620 phba->link_state = LPFC_HBA_ERROR;
621 return -ENOMEM;
622 }
623
James Smart97207482008-12-04 22:39:19 -0500624 lpfc_dump_wakeup_param(phba, pmb);
625 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
626 pmb->vport = phba->pport;
627 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
628
629 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
630 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
James Smarte4e74272009-07-19 10:01:38 -0400631 "to get Option ROM version status x%x\n", rc);
James Smart97207482008-12-04 22:39:19 -0500632 mempool_free(pmb, phba->mbox_mem_pool);
633 }
634
James Smartd7c255b2008-08-24 21:50:00 -0400635 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400636}
637
James Smarte59058c2008-08-24 21:49:00 -0400638/**
James Smart84d1b002010-02-12 14:42:33 -0500639 * lpfc_hba_init_link - Initialize the FC link
640 * @phba: pointer to lpfc hba data structure.
James Smart6e7288d2010-06-07 15:23:35 -0400641 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500642 *
643 * This routine will issue the INIT_LINK mailbox command call.
644 * It is available to other drivers through the lpfc_hba data
645 * structure for use as a delayed link up mechanism with the
646 * module parameter lpfc_suppress_link_up.
647 *
648 * Return code
649 * 0 - success
650 * Any other value - error
651 **/
652int
James Smart6e7288d2010-06-07 15:23:35 -0400653lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500654{
James Smart1b511972011-12-13 13:23:09 -0500655 return lpfc_hba_init_link_fc_topology(phba, phba->cfg_topology, flag);
656}
657
658/**
659 * lpfc_hba_init_link_fc_topology - Initialize FC link with desired topology
660 * @phba: pointer to lpfc hba data structure.
661 * @fc_topology: desired fc topology.
662 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
663 *
664 * This routine will issue the INIT_LINK mailbox command call.
665 * It is available to other drivers through the lpfc_hba data
666 * structure for use as a delayed link up mechanism with the
667 * module parameter lpfc_suppress_link_up.
668 *
669 * Return code
670 * 0 - success
671 * Any other value - error
672 **/
673int
674lpfc_hba_init_link_fc_topology(struct lpfc_hba *phba, uint32_t fc_topology,
675 uint32_t flag)
676{
James Smart84d1b002010-02-12 14:42:33 -0500677 struct lpfc_vport *vport = phba->pport;
678 LPFC_MBOXQ_t *pmb;
679 MAILBOX_t *mb;
680 int rc;
681
682 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
683 if (!pmb) {
684 phba->link_state = LPFC_HBA_ERROR;
685 return -ENOMEM;
686 }
687 mb = &pmb->u.mb;
688 pmb->vport = vport;
689
James Smart026abb82011-12-13 13:20:45 -0500690 if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_MAX) ||
691 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G) &&
692 !(phba->lmt & LMT_1Gb)) ||
693 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G) &&
694 !(phba->lmt & LMT_2Gb)) ||
695 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G) &&
696 !(phba->lmt & LMT_4Gb)) ||
697 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G) &&
698 !(phba->lmt & LMT_8Gb)) ||
699 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G) &&
700 !(phba->lmt & LMT_10Gb)) ||
701 ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G) &&
702 !(phba->lmt & LMT_16Gb))) {
703 /* Reset link speed to auto */
704 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
705 "1302 Invalid speed for this board:%d "
706 "Reset link speed to auto.\n",
707 phba->cfg_link_speed);
708 phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
709 }
James Smart1b511972011-12-13 13:23:09 -0500710 lpfc_init_link(phba, pmb, fc_topology, phba->cfg_link_speed);
James Smart84d1b002010-02-12 14:42:33 -0500711 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart1b511972011-12-13 13:23:09 -0500712 if (phba->sli_rev < LPFC_SLI_REV4)
713 lpfc_set_loopback_flag(phba);
James Smart6e7288d2010-06-07 15:23:35 -0400714 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart76a95d72010-11-20 23:11:48 -0500715 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
James Smart84d1b002010-02-12 14:42:33 -0500716 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
717 "0498 Adapter failed to init, mbxCmd x%x "
718 "INIT_LINK, mbxStatus x%x\n",
719 mb->mbxCommand, mb->mbxStatus);
James Smart76a95d72010-11-20 23:11:48 -0500720 if (phba->sli_rev <= LPFC_SLI_REV3) {
721 /* Clear all interrupt enable conditions */
722 writel(0, phba->HCregaddr);
723 readl(phba->HCregaddr); /* flush */
724 /* Clear all pending interrupts */
725 writel(0xffffffff, phba->HAregaddr);
726 readl(phba->HAregaddr); /* flush */
727 }
James Smart84d1b002010-02-12 14:42:33 -0500728 phba->link_state = LPFC_HBA_ERROR;
James Smart6e7288d2010-06-07 15:23:35 -0400729 if (rc != MBX_BUSY || flag == MBX_POLL)
James Smart84d1b002010-02-12 14:42:33 -0500730 mempool_free(pmb, phba->mbox_mem_pool);
731 return -EIO;
732 }
James Smarte40a02c2010-02-26 14:13:54 -0500733 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
James Smart6e7288d2010-06-07 15:23:35 -0400734 if (flag == MBX_POLL)
735 mempool_free(pmb, phba->mbox_mem_pool);
James Smart84d1b002010-02-12 14:42:33 -0500736
737 return 0;
738}
739
740/**
741 * lpfc_hba_down_link - this routine downs the FC link
James Smart6e7288d2010-06-07 15:23:35 -0400742 * @phba: pointer to lpfc hba data structure.
743 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500744 *
745 * This routine will issue the DOWN_LINK mailbox command call.
746 * It is available to other drivers through the lpfc_hba data
747 * structure for use to stop the link.
748 *
749 * Return code
750 * 0 - success
751 * Any other value - error
752 **/
753int
James Smart6e7288d2010-06-07 15:23:35 -0400754lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500755{
756 LPFC_MBOXQ_t *pmb;
757 int rc;
758
759 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
760 if (!pmb) {
761 phba->link_state = LPFC_HBA_ERROR;
762 return -ENOMEM;
763 }
764
765 lpfc_printf_log(phba,
766 KERN_ERR, LOG_INIT,
767 "0491 Adapter Link is disabled.\n");
768 lpfc_down_link(phba, pmb);
769 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6e7288d2010-06-07 15:23:35 -0400770 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart84d1b002010-02-12 14:42:33 -0500771 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
772 lpfc_printf_log(phba,
773 KERN_ERR, LOG_INIT,
774 "2522 Adapter failed to issue DOWN_LINK"
775 " mbox command rc 0x%x\n", rc);
776
777 mempool_free(pmb, phba->mbox_mem_pool);
778 return -EIO;
779 }
James Smart6e7288d2010-06-07 15:23:35 -0400780 if (flag == MBX_POLL)
781 mempool_free(pmb, phba->mbox_mem_pool);
782
James Smart84d1b002010-02-12 14:42:33 -0500783 return 0;
784}
785
786/**
James Smart3621a712009-04-06 18:47:14 -0400787 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400788 * @phba: pointer to lpfc HBA data structure.
789 *
790 * This routine will do LPFC uninitialization before the HBA is reset when
791 * bringing down the SLI Layer.
792 *
793 * Return codes
794 * 0 - success.
795 * Any other value - error.
796 **/
dea31012005-04-17 16:05:31 -0500797int
James Smart2e0fef82007-06-17 19:56:36 -0500798lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500799{
James Smart1b32f6a2008-02-08 18:49:39 -0500800 struct lpfc_vport **vports;
801 int i;
James Smart3772a992009-05-22 14:50:54 -0400802
803 if (phba->sli_rev <= LPFC_SLI_REV3) {
804 /* Disable interrupts */
805 writel(0, phba->HCregaddr);
806 readl(phba->HCregaddr); /* flush */
807 }
dea31012005-04-17 16:05:31 -0500808
James Smart1b32f6a2008-02-08 18:49:39 -0500809 if (phba->pport->load_flag & FC_UNLOADING)
810 lpfc_cleanup_discovery_resources(phba->pport);
811 else {
812 vports = lpfc_create_vport_work_array(phba);
813 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400814 for (i = 0; i <= phba->max_vports &&
815 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500816 lpfc_cleanup_discovery_resources(vports[i]);
817 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500818 }
819 return 0;
dea31012005-04-17 16:05:31 -0500820}
821
James Smarte59058c2008-08-24 21:49:00 -0400822/**
James Smart3772a992009-05-22 14:50:54 -0400823 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400824 * @phba: pointer to lpfc HBA data structure.
825 *
826 * This routine will do uninitialization after the HBA is reset when bring
827 * down the SLI Layer.
828 *
829 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200830 * 0 - success.
James Smarte59058c2008-08-24 21:49:00 -0400831 * Any other value - error.
832 **/
James Smart3772a992009-05-22 14:50:54 -0400833static int
834lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -0500835{
836 struct lpfc_sli *psli = &phba->sli;
837 struct lpfc_sli_ring *pring;
838 struct lpfc_dmabuf *mp, *next_mp;
James Smart09372822008-01-11 01:52:54 -0500839 LIST_HEAD(completions);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500840 int i;
841
James Smart92d7f7b2007-06-17 19:56:38 -0500842 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
843 lpfc_sli_hbqbuf_free_all(phba);
844 else {
845 /* Cleanup preposted buffers on the ELS ring */
846 pring = &psli->ring[LPFC_ELS_RING];
847 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
848 list_del(&mp->list);
849 pring->postbufq_cnt--;
850 lpfc_mbuf_free(phba, mp->virt, mp->phys);
851 kfree(mp);
852 }
Jamie Wellnitz41415862006-02-28 19:25:27 -0500853 }
854
James Smart09372822008-01-11 01:52:54 -0500855 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500856 for (i = 0; i < psli->num_rings; i++) {
857 pring = &psli->ring[i];
James Smart09372822008-01-11 01:52:54 -0500858
859 /* At this point in time the HBA is either reset or DOA. Either
860 * way, nothing should be on txcmplq as it will NEVER complete.
861 */
862 list_splice_init(&pring->txcmplq, &completions);
James Smart09372822008-01-11 01:52:54 -0500863 spin_unlock_irq(&phba->hbalock);
864
James Smarta257bf92009-04-06 18:48:10 -0400865 /* Cancel all the IOCBs from the completions list */
866 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
867 IOERR_SLI_ABORTED);
James Smart09372822008-01-11 01:52:54 -0500868
Jamie Wellnitz41415862006-02-28 19:25:27 -0500869 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart09372822008-01-11 01:52:54 -0500870 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500871 }
James Smart09372822008-01-11 01:52:54 -0500872 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500873
874 return 0;
875}
James Smart5af5eee2010-10-22 11:06:38 -0400876
James Smartda0436e2009-05-22 14:51:39 -0400877/**
878 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
879 * @phba: pointer to lpfc HBA data structure.
880 *
881 * This routine will do uninitialization after the HBA is reset when bring
882 * down the SLI Layer.
883 *
884 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200885 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400886 * Any other value - error.
887 **/
888static int
889lpfc_hba_down_post_s4(struct lpfc_hba *phba)
890{
891 struct lpfc_scsi_buf *psb, *psb_next;
892 LIST_HEAD(aborts);
893 int ret;
894 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500895 struct lpfc_sglq *sglq_entry = NULL;
896
James Smartda0436e2009-05-22 14:51:39 -0400897 ret = lpfc_hba_down_post_s3(phba);
898 if (ret)
899 return ret;
900 /* At this point in time the HBA is either reset or DOA. Either
901 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
902 * on the lpfc_sgl_list so that it can either be freed if the
903 * driver is unloading or reposted if the driver is restarting
904 * the port.
905 */
906 spin_lock_irq(&phba->hbalock); /* required for lpfc_sgl_list and */
907 /* scsl_buf_list */
908 /* abts_sgl_list_lock required because worker thread uses this
909 * list.
910 */
911 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -0500912 list_for_each_entry(sglq_entry,
913 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
914 sglq_entry->state = SGL_FREED;
915
James Smartda0436e2009-05-22 14:51:39 -0400916 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
917 &phba->sli4_hba.lpfc_sgl_list);
918 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
919 /* abts_scsi_buf_list_lock required because worker thread uses this
920 * list.
921 */
922 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
923 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
924 &aborts);
925 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
926 spin_unlock_irq(&phba->hbalock);
927
928 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
929 psb->pCmd = NULL;
930 psb->status = IOSTAT_SUCCESS;
931 }
James Smarta40fc5f2013-04-17 20:17:40 -0400932 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
933 list_splice(&aborts, &phba->lpfc_scsi_buf_list_put);
934 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400935 return 0;
936}
937
938/**
939 * lpfc_hba_down_post - Wrapper func for hba down post routine
940 * @phba: pointer to lpfc HBA data structure.
941 *
942 * This routine wraps the actual SLI3 or SLI4 routine for performing
943 * uninitialization after the HBA is reset when bring down the SLI Layer.
944 *
945 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200946 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400947 * Any other value - error.
948 **/
949int
950lpfc_hba_down_post(struct lpfc_hba *phba)
951{
952 return (*phba->lpfc_hba_down_post)(phba);
953}
Jamie Wellnitz41415862006-02-28 19:25:27 -0500954
James Smarte59058c2008-08-24 21:49:00 -0400955/**
James Smart3621a712009-04-06 18:47:14 -0400956 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -0400957 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
958 *
959 * This is the HBA-timer timeout handler registered to the lpfc driver. When
960 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
961 * work-port-events bitmap and the worker thread is notified. This timeout
962 * event will be used by the worker thread to invoke the actual timeout
963 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
964 * be performed in the timeout handler and the HBA timeout event bit shall
965 * be cleared by the worker thread after it has taken the event bitmap out.
966 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100967static void
James Smart858c9f62007-06-17 19:56:39 -0500968lpfc_hb_timeout(unsigned long ptr)
969{
970 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400971 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -0500972 unsigned long iflag;
973
974 phba = (struct lpfc_hba *)ptr;
James Smart93996272008-08-24 21:50:30 -0400975
976 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -0500977 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -0400978 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
979 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -0500980 phba->pport->work_port_events |= WORKER_HB_TMO;
981 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
982
James Smart93996272008-08-24 21:50:30 -0400983 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -0400984 if (!tmo_posted)
985 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -0500986 return;
987}
988
James Smarte59058c2008-08-24 21:49:00 -0400989/**
James Smart19ca7602010-11-20 23:11:55 -0500990 * lpfc_rrq_timeout - The RRQ-timer timeout handler
991 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
992 *
993 * This is the RRQ-timer timeout handler registered to the lpfc driver. When
994 * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
995 * work-port-events bitmap and the worker thread is notified. This timeout
996 * event will be used by the worker thread to invoke the actual timeout
997 * handler routine, lpfc_rrq_handler. Any periodical operations will
998 * be performed in the timeout handler and the RRQ timeout event bit shall
999 * be cleared by the worker thread after it has taken the event bitmap out.
1000 **/
1001static void
1002lpfc_rrq_timeout(unsigned long ptr)
1003{
1004 struct lpfc_hba *phba;
James Smart19ca7602010-11-20 23:11:55 -05001005 unsigned long iflag;
1006
1007 phba = (struct lpfc_hba *)ptr;
1008 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -05001009 phba->hba_flag |= HBA_RRQ_ACTIVE;
James Smart19ca7602010-11-20 23:11:55 -05001010 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -05001011 lpfc_worker_wake_up(phba);
James Smart19ca7602010-11-20 23:11:55 -05001012}
1013
1014/**
James Smart3621a712009-04-06 18:47:14 -04001015 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -04001016 * @phba: pointer to lpfc hba data structure.
1017 * @pmboxq: pointer to the driver internal queue element for mailbox command.
1018 *
1019 * This is the callback function to the lpfc heart-beat mailbox command.
1020 * If configured, the lpfc driver issues the heart-beat mailbox command to
1021 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
1022 * heart-beat mailbox command is issued, the driver shall set up heart-beat
1023 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
1024 * heart-beat outstanding state. Once the mailbox command comes back and
1025 * no error conditions detected, the heart-beat mailbox command timer is
1026 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1027 * state is cleared for the next heart-beat. If the timer expired with the
1028 * heart-beat outstanding state set, the driver will put the HBA offline.
1029 **/
James Smart858c9f62007-06-17 19:56:39 -05001030static void
1031lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1032{
1033 unsigned long drvr_flag;
1034
1035 spin_lock_irqsave(&phba->hbalock, drvr_flag);
1036 phba->hb_outstanding = 0;
1037 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1038
James Smart93996272008-08-24 21:50:30 -04001039 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -05001040 mempool_free(pmboxq, phba->mbox_mem_pool);
1041 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1042 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -04001043 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -05001044 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001045 jiffies +
1046 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001047 return;
1048}
1049
James Smarte59058c2008-08-24 21:49:00 -04001050/**
James Smart3621a712009-04-06 18:47:14 -04001051 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001052 * @phba: pointer to lpfc hba data structure.
1053 *
1054 * This is the actual HBA-timer timeout handler to be invoked by the worker
1055 * thread whenever the HBA timer fired and HBA-timeout event posted. This
1056 * handler performs any periodic operations needed for the device. If such
1057 * periodic event has already been attended to either in the interrupt handler
1058 * or by processing slow-ring or fast-ring events within the HBA-timer
1059 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1060 * the timer for the next timeout period. If lpfc heart-beat mailbox command
1061 * is configured and there is no heart-beat mailbox command outstanding, a
1062 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1063 * has been a heart-beat mailbox command outstanding, the HBA shall be put
1064 * to offline.
1065 **/
James Smart858c9f62007-06-17 19:56:39 -05001066void
1067lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1068{
James Smart45ed1192009-10-02 15:17:02 -04001069 struct lpfc_vport **vports;
James Smart858c9f62007-06-17 19:56:39 -05001070 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -05001071 struct lpfc_dmabuf *buf_ptr;
James Smart45ed1192009-10-02 15:17:02 -04001072 int retval, i;
James Smart858c9f62007-06-17 19:56:39 -05001073 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -05001074 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -05001075
James Smart45ed1192009-10-02 15:17:02 -04001076 vports = lpfc_create_vport_work_array(phba);
1077 if (vports != NULL)
1078 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
1079 lpfc_rcv_seq_check_edtov(vports[i]);
1080 lpfc_destroy_vport_work_array(phba, vports);
1081
James Smart858c9f62007-06-17 19:56:39 -05001082 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -04001083 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -05001084 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1085 return;
1086
1087 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -05001088
James Smart256ec0d2013-04-17 20:14:58 -04001089 if (time_after(phba->last_completion_time +
1090 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL),
1091 jiffies)) {
James Smart858c9f62007-06-17 19:56:39 -05001092 spin_unlock_irq(&phba->pport->work_port_lock);
1093 if (!phba->hb_outstanding)
1094 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001095 jiffies +
1096 msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smart858c9f62007-06-17 19:56:39 -05001097 else
1098 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001099 jiffies +
1100 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001101 return;
1102 }
1103 spin_unlock_irq(&phba->pport->work_port_lock);
1104
James Smart0ff10d42008-01-11 01:52:36 -05001105 if (phba->elsbuf_cnt &&
1106 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1107 spin_lock_irq(&phba->hbalock);
1108 list_splice_init(&phba->elsbuf, &completions);
1109 phba->elsbuf_cnt = 0;
1110 phba->elsbuf_prev_cnt = 0;
1111 spin_unlock_irq(&phba->hbalock);
1112
1113 while (!list_empty(&completions)) {
1114 list_remove_head(&completions, buf_ptr,
1115 struct lpfc_dmabuf, list);
1116 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1117 kfree(buf_ptr);
1118 }
1119 }
1120 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1121
James Smart858c9f62007-06-17 19:56:39 -05001122 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -05001123 if (phba->cfg_enable_hba_heartbeat) {
1124 if (!phba->hb_outstanding) {
James Smartbc739052010-08-04 16:11:18 -04001125 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1126 (list_empty(&psli->mboxq))) {
1127 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1128 GFP_KERNEL);
1129 if (!pmboxq) {
1130 mod_timer(&phba->hb_tmofunc,
1131 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001132 msecs_to_jiffies(1000 *
1133 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001134 return;
1135 }
James Smart13815c82008-01-11 01:52:48 -05001136
James Smartbc739052010-08-04 16:11:18 -04001137 lpfc_heart_beat(phba, pmboxq);
1138 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1139 pmboxq->vport = phba->pport;
1140 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1141 MBX_NOWAIT);
James Smart13815c82008-01-11 01:52:48 -05001142
James Smartbc739052010-08-04 16:11:18 -04001143 if (retval != MBX_BUSY &&
1144 retval != MBX_SUCCESS) {
1145 mempool_free(pmboxq,
1146 phba->mbox_mem_pool);
1147 mod_timer(&phba->hb_tmofunc,
1148 jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04001149 msecs_to_jiffies(1000 *
1150 LPFC_HB_MBOX_INTERVAL));
James Smartbc739052010-08-04 16:11:18 -04001151 return;
1152 }
1153 phba->skipped_hb = 0;
1154 phba->hb_outstanding = 1;
1155 } else if (time_before_eq(phba->last_completion_time,
1156 phba->skipped_hb)) {
1157 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1158 "2857 Last completion time not "
1159 " updated in %d ms\n",
1160 jiffies_to_msecs(jiffies
1161 - phba->last_completion_time));
1162 } else
1163 phba->skipped_hb = jiffies;
1164
James Smart858c9f62007-06-17 19:56:39 -05001165 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001166 jiffies +
1167 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001168 return;
James Smart13815c82008-01-11 01:52:48 -05001169 } else {
1170 /*
1171 * If heart beat timeout called with hb_outstanding set
James Smartdcf2a4e2010-09-29 11:18:53 -04001172 * we need to give the hb mailbox cmd a chance to
1173 * complete or TMO.
James Smart13815c82008-01-11 01:52:48 -05001174 */
James Smartdcf2a4e2010-09-29 11:18:53 -04001175 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1176 "0459 Adapter heartbeat still out"
1177 "standing:last compl time was %d ms.\n",
1178 jiffies_to_msecs(jiffies
1179 - phba->last_completion_time));
1180 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04001181 jiffies +
1182 msecs_to_jiffies(1000 * LPFC_HB_MBOX_TIMEOUT));
James Smart858c9f62007-06-17 19:56:39 -05001183 }
James Smart858c9f62007-06-17 19:56:39 -05001184 }
1185}
1186
James Smarte59058c2008-08-24 21:49:00 -04001187/**
James Smart3621a712009-04-06 18:47:14 -04001188 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -04001189 * @phba: pointer to lpfc hba data structure.
1190 *
1191 * This routine is called to bring the HBA offline when HBA hardware error
1192 * other than Port Error 6 has been detected.
1193 **/
James Smart09372822008-01-11 01:52:54 -05001194static void
1195lpfc_offline_eratt(struct lpfc_hba *phba)
1196{
1197 struct lpfc_sli *psli = &phba->sli;
1198
1199 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001200 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -05001201 spin_unlock_irq(&phba->hbalock);
James Smart618a5232012-06-12 13:54:36 -04001202 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smart09372822008-01-11 01:52:54 -05001203
1204 lpfc_offline(phba);
1205 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001206 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001207 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001208 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001209 lpfc_hba_down_post(phba);
1210 lpfc_sli_brdready(phba, HS_MBRDY);
1211 lpfc_unblock_mgmt_io(phba);
1212 phba->link_state = LPFC_HBA_ERROR;
1213 return;
1214}
1215
James Smarte59058c2008-08-24 21:49:00 -04001216/**
James Smartda0436e2009-05-22 14:51:39 -04001217 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1218 * @phba: pointer to lpfc hba data structure.
1219 *
1220 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1221 * other than Port Error 6 has been detected.
1222 **/
James Smarta88dbb62013-04-17 20:18:39 -04001223void
James Smartda0436e2009-05-22 14:51:39 -04001224lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1225{
James Smart618a5232012-06-12 13:54:36 -04001226 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
James Smartda0436e2009-05-22 14:51:39 -04001227 lpfc_offline(phba);
1228 lpfc_sli4_brdreset(phba);
1229 lpfc_hba_down_post(phba);
1230 lpfc_sli4_post_status_check(phba);
1231 lpfc_unblock_mgmt_io(phba);
1232 phba->link_state = LPFC_HBA_ERROR;
1233}
1234
1235/**
James Smarta257bf92009-04-06 18:48:10 -04001236 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1237 * @phba: pointer to lpfc hba data structure.
1238 *
1239 * This routine is invoked to handle the deferred HBA hardware error
1240 * conditions. This type of error is indicated by HBA by setting ER1
1241 * and another ER bit in the host status register. The driver will
1242 * wait until the ER1 bit clears before handling the error condition.
1243 **/
1244static void
1245lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1246{
1247 uint32_t old_host_status = phba->work_hs;
1248 struct lpfc_sli_ring *pring;
1249 struct lpfc_sli *psli = &phba->sli;
1250
James Smartf4b4c682009-05-22 14:53:12 -04001251 /* If the pci channel is offline, ignore possible errors,
1252 * since we cannot communicate with the pci card anyway.
1253 */
1254 if (pci_channel_offline(phba->pcidev)) {
1255 spin_lock_irq(&phba->hbalock);
1256 phba->hba_flag &= ~DEFER_ERATT;
1257 spin_unlock_irq(&phba->hbalock);
1258 return;
1259 }
1260
James Smarta257bf92009-04-06 18:48:10 -04001261 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1262 "0479 Deferred Adapter Hardware Error "
1263 "Data: x%x x%x x%x\n",
1264 phba->work_hs,
1265 phba->work_status[0], phba->work_status[1]);
1266
1267 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001268 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001269 spin_unlock_irq(&phba->hbalock);
1270
1271
1272 /*
1273 * Firmware stops when it triggred erratt. That could cause the I/Os
1274 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1275 * SCSI layer retry it after re-establishing link.
1276 */
1277 pring = &psli->ring[psli->fcp_ring];
1278 lpfc_sli_abort_iocb_ring(phba, pring);
1279
1280 /*
1281 * There was a firmware error. Take the hba offline and then
1282 * attempt to restart it.
1283 */
James Smart618a5232012-06-12 13:54:36 -04001284 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smarta257bf92009-04-06 18:48:10 -04001285 lpfc_offline(phba);
1286
1287 /* Wait for the ER1 bit to clear.*/
1288 while (phba->work_hs & HS_FFER1) {
1289 msleep(100);
James Smart9940b972011-03-11 16:06:12 -05001290 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1291 phba->work_hs = UNPLUG_ERR ;
1292 break;
1293 }
James Smarta257bf92009-04-06 18:48:10 -04001294 /* If driver is unloading let the worker thread continue */
1295 if (phba->pport->load_flag & FC_UNLOADING) {
1296 phba->work_hs = 0;
1297 break;
1298 }
1299 }
1300
1301 /*
1302 * This is to ptrotect against a race condition in which
1303 * first write to the host attention register clear the
1304 * host status register.
1305 */
1306 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1307 phba->work_hs = old_host_status & ~HS_FFER1;
1308
James Smart3772a992009-05-22 14:50:54 -04001309 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001310 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001311 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001312 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1313 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1314}
1315
James Smart3772a992009-05-22 14:50:54 -04001316static void
1317lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1318{
1319 struct lpfc_board_event_header board_event;
1320 struct Scsi_Host *shost;
1321
1322 board_event.event_type = FC_REG_BOARD_EVENT;
1323 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1324 shost = lpfc_shost_from_vport(phba->pport);
1325 fc_host_post_vendor_event(shost, fc_get_event_number(),
1326 sizeof(board_event),
1327 (char *) &board_event,
1328 LPFC_NL_VENDOR_ID);
1329}
1330
James Smarta257bf92009-04-06 18:48:10 -04001331/**
James Smart3772a992009-05-22 14:50:54 -04001332 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001333 * @phba: pointer to lpfc hba data structure.
1334 *
1335 * This routine is invoked to handle the following HBA hardware error
1336 * conditions:
1337 * 1 - HBA error attention interrupt
1338 * 2 - DMA ring index out of range
1339 * 3 - Mailbox command came back as unknown
1340 **/
James Smart3772a992009-05-22 14:50:54 -04001341static void
1342lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001343{
James Smart2e0fef82007-06-17 19:56:36 -05001344 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001345 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001346 struct lpfc_sli_ring *pring;
James Smartd2873e42006-08-18 17:46:43 -04001347 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001348 unsigned long temperature;
1349 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001350 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001351
Linas Vepstas8d63f372007-02-14 14:28:36 -06001352 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001353 * 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);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001359 return;
James Smart3772a992009-05-22 14:50:54 -04001360 }
1361
James Smart13815c82008-01-11 01:52:48 -05001362 /* If resets are disabled then leave the HBA alone and return */
1363 if (!phba->cfg_enable_hba_reset)
1364 return;
dea31012005-04-17 16:05:31 -05001365
James Smartea2151b2008-09-07 11:52:10 -04001366 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001367 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001368
James Smarta257bf92009-04-06 18:48:10 -04001369 if (phba->hba_flag & DEFER_ERATT)
1370 lpfc_handle_deferred_eratt(phba);
1371
James Smartdcf2a4e2010-09-29 11:18:53 -04001372 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1373 if (phba->work_hs & HS_FFER6)
1374 /* Re-establishing Link */
1375 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1376 "1301 Re-establishing Link "
1377 "Data: x%x x%x x%x\n",
1378 phba->work_hs, phba->work_status[0],
1379 phba->work_status[1]);
1380 if (phba->work_hs & HS_FFER8)
1381 /* Device Zeroization */
1382 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1383 "2861 Host Authentication device "
1384 "zeroization Data:x%x x%x x%x\n",
1385 phba->work_hs, phba->work_status[0],
1386 phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001387
James Smart92d7f7b2007-06-17 19:56:38 -05001388 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001389 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001390 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001391
1392 /*
1393 * Firmware stops when it triggled erratt with HS_FFER6.
1394 * That could cause the I/Os dropped by the firmware.
1395 * Error iocb (I/O) on txcmplq and let the SCSI layer
1396 * retry it after re-establishing link.
1397 */
1398 pring = &psli->ring[psli->fcp_ring];
1399 lpfc_sli_abort_iocb_ring(phba, pring);
1400
dea31012005-04-17 16:05:31 -05001401 /*
1402 * There was a firmware error. Take the hba offline and then
1403 * attempt to restart it.
1404 */
James Smart618a5232012-06-12 13:54:36 -04001405 lpfc_offline_prep(phba, LPFC_MBX_NO_WAIT);
dea31012005-04-17 16:05:31 -05001406 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001407 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001408 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001409 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001410 return;
1411 }
James Smart46fa3112007-04-25 09:51:45 -04001412 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001413 } else if (phba->work_hs & HS_CRIT_TEMP) {
1414 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1415 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1416 temp_event_data.event_code = LPFC_CRIT_TEMP;
1417 temp_event_data.data = (uint32_t)temperature;
1418
1419 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001420 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001421 "(%ld), taking this port offline "
1422 "Data: x%x x%x x%x\n",
1423 temperature, phba->work_hs,
1424 phba->work_status[0], phba->work_status[1]);
1425
1426 shost = lpfc_shost_from_vport(phba->pport);
1427 fc_host_post_vendor_event(shost, fc_get_event_number(),
1428 sizeof(temp_event_data),
1429 (char *) &temp_event_data,
1430 SCSI_NL_VID_TYPE_PCI
1431 | PCI_VENDOR_ID_EMULEX);
1432
James Smart7af67052007-10-27 13:38:11 -04001433 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001434 phba->over_temp_state = HBA_OVER_TEMP;
1435 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001436 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001437
dea31012005-04-17 16:05:31 -05001438 } else {
1439 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001440 * failure is a value other than FFER6. Do not call the offline
1441 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001442 */
1443 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001444 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001445 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001446 phba->work_hs,
dea31012005-04-17 16:05:31 -05001447 phba->work_status[0], phba->work_status[1]);
1448
James Smartd2873e42006-08-18 17:46:43 -04001449 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001450 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001451 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001452 sizeof(event_data), (char *) &event_data,
1453 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1454
James Smart09372822008-01-11 01:52:54 -05001455 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001456 }
James Smart93996272008-08-24 21:50:30 -04001457 return;
dea31012005-04-17 16:05:31 -05001458}
1459
James Smarte59058c2008-08-24 21:49:00 -04001460/**
James Smart618a5232012-06-12 13:54:36 -04001461 * lpfc_sli4_port_sta_fn_reset - The SLI4 function reset due to port status reg
1462 * @phba: pointer to lpfc hba data structure.
1463 * @mbx_action: flag for mailbox shutdown action.
1464 *
1465 * This routine is invoked to perform an SLI4 port PCI function reset in
1466 * response to port status register polling attention. It waits for port
1467 * status register (ERR, RDY, RN) bits before proceeding with function reset.
1468 * During this process, interrupt vectors are freed and later requested
1469 * for handling possible port resource change.
1470 **/
1471static int
James Smarte10b2022014-02-20 09:57:43 -05001472lpfc_sli4_port_sta_fn_reset(struct lpfc_hba *phba, int mbx_action,
1473 bool en_rn_msg)
James Smart618a5232012-06-12 13:54:36 -04001474{
1475 int rc;
1476 uint32_t intr_mode;
1477
1478 /*
1479 * On error status condition, driver need to wait for port
1480 * ready before performing reset.
1481 */
1482 rc = lpfc_sli4_pdev_status_reg_wait(phba);
1483 if (!rc) {
1484 /* need reset: attempt for port recovery */
James Smarte10b2022014-02-20 09:57:43 -05001485 if (en_rn_msg)
1486 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1487 "2887 Reset Needed: Attempting Port "
1488 "Recovery...\n");
James Smart618a5232012-06-12 13:54:36 -04001489 lpfc_offline_prep(phba, mbx_action);
1490 lpfc_offline(phba);
1491 /* release interrupt for possible resource change */
1492 lpfc_sli4_disable_intr(phba);
1493 lpfc_sli_brdrestart(phba);
1494 /* request and enable interrupt */
1495 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
1496 if (intr_mode == LPFC_INTR_ERROR) {
1497 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1498 "3175 Failed to enable interrupt\n");
1499 return -EIO;
1500 } else {
1501 phba->intr_mode = intr_mode;
1502 }
1503 rc = lpfc_online(phba);
1504 if (rc == 0)
1505 lpfc_unblock_mgmt_io(phba);
1506 }
1507 return rc;
1508}
1509
1510/**
James Smartda0436e2009-05-22 14:51:39 -04001511 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1512 * @phba: pointer to lpfc hba data structure.
1513 *
1514 * This routine is invoked to handle the SLI4 HBA hardware error attention
1515 * conditions.
1516 **/
1517static void
1518lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1519{
1520 struct lpfc_vport *vport = phba->pport;
1521 uint32_t event_data;
1522 struct Scsi_Host *shost;
James Smart2fcee4b2010-12-15 17:57:46 -05001523 uint32_t if_type;
James Smart2e90f4b2011-12-13 13:22:37 -05001524 struct lpfc_register portstat_reg = {0};
1525 uint32_t reg_err1, reg_err2;
1526 uint32_t uerrlo_reg, uemasklo_reg;
1527 uint32_t pci_rd_rc1, pci_rd_rc2;
James Smarte10b2022014-02-20 09:57:43 -05001528 bool en_rn_msg = true;
James Smart73d91e52011-10-10 21:32:10 -04001529 int rc;
James Smartda0436e2009-05-22 14:51:39 -04001530
1531 /* If the pci channel is offline, ignore possible errors, since
1532 * we cannot communicate with the pci card anyway.
1533 */
1534 if (pci_channel_offline(phba->pcidev))
1535 return;
1536 /* If resets are disabled then leave the HBA alone and return */
1537 if (!phba->cfg_enable_hba_reset)
1538 return;
1539
James Smart2fcee4b2010-12-15 17:57:46 -05001540 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1541 switch (if_type) {
1542 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart2e90f4b2011-12-13 13:22:37 -05001543 pci_rd_rc1 = lpfc_readl(
1544 phba->sli4_hba.u.if_type0.UERRLOregaddr,
1545 &uerrlo_reg);
1546 pci_rd_rc2 = lpfc_readl(
1547 phba->sli4_hba.u.if_type0.UEMASKLOregaddr,
1548 &uemasklo_reg);
1549 /* consider PCI bus read error as pci_channel_offline */
1550 if (pci_rd_rc1 == -EIO && pci_rd_rc2 == -EIO)
1551 return;
James Smart2fcee4b2010-12-15 17:57:46 -05001552 lpfc_sli4_offline_eratt(phba);
1553 break;
1554 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart2e90f4b2011-12-13 13:22:37 -05001555 pci_rd_rc1 = lpfc_readl(
1556 phba->sli4_hba.u.if_type2.STATUSregaddr,
1557 &portstat_reg.word0);
1558 /* consider PCI bus read error as pci_channel_offline */
James Smart6b5151f2012-01-18 16:24:06 -05001559 if (pci_rd_rc1 == -EIO) {
1560 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1561 "3151 PCI bus read access failure: x%x\n",
1562 readl(phba->sli4_hba.u.if_type2.STATUSregaddr));
James Smart2e90f4b2011-12-13 13:22:37 -05001563 return;
James Smart6b5151f2012-01-18 16:24:06 -05001564 }
James Smart2e90f4b2011-12-13 13:22:37 -05001565 reg_err1 = readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
1566 reg_err2 = readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
James Smart2fcee4b2010-12-15 17:57:46 -05001567 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
1568 /* TODO: Register for Overtemp async events. */
1569 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1570 "2889 Port Overtemperature event, "
James Smart026abb82011-12-13 13:20:45 -05001571 "taking port offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001572 spin_lock_irq(&phba->hbalock);
1573 phba->over_temp_state = HBA_OVER_TEMP;
1574 spin_unlock_irq(&phba->hbalock);
1575 lpfc_sli4_offline_eratt(phba);
James Smart2e90f4b2011-12-13 13:22:37 -05001576 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001577 }
James Smart2e90f4b2011-12-13 13:22:37 -05001578 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smarte10b2022014-02-20 09:57:43 -05001579 reg_err2 == SLIPORT_ERR2_REG_FW_RESTART) {
James Smart2e90f4b2011-12-13 13:22:37 -05001580 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte10b2022014-02-20 09:57:43 -05001581 "3143 Port Down: Firmware Update "
1582 "Detected\n");
1583 en_rn_msg = false;
1584 } else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
James Smart2e90f4b2011-12-13 13:22:37 -05001585 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1586 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1587 "3144 Port Down: Debug Dump\n");
1588 else if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1589 reg_err2 == SLIPORT_ERR2_REG_FUNC_PROVISON)
1590 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1591 "3145 Port Down: Provisioning\n");
James Smart618a5232012-06-12 13:54:36 -04001592
1593 /* Check port status register for function reset */
James Smarte10b2022014-02-20 09:57:43 -05001594 rc = lpfc_sli4_port_sta_fn_reset(phba, LPFC_MBX_NO_WAIT,
1595 en_rn_msg);
James Smart618a5232012-06-12 13:54:36 -04001596 if (rc == 0) {
1597 /* don't report event on forced debug dump */
1598 if (reg_err1 == SLIPORT_ERR1_REG_ERR_CODE_2 &&
1599 reg_err2 == SLIPORT_ERR2_REG_FORCED_DUMP)
1600 return;
1601 else
1602 break;
James Smart2fcee4b2010-12-15 17:57:46 -05001603 }
James Smart618a5232012-06-12 13:54:36 -04001604 /* fall through for not able to recover */
James Smart6b5151f2012-01-18 16:24:06 -05001605 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1606 "3152 Unrecoverable error, bring the port "
1607 "offline\n");
James Smart2fcee4b2010-12-15 17:57:46 -05001608 lpfc_sli4_offline_eratt(phba);
1609 break;
1610 case LPFC_SLI_INTF_IF_TYPE_1:
1611 default:
1612 break;
1613 }
James Smart2e90f4b2011-12-13 13:22:37 -05001614 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1615 "3123 Report dump event to upper layer\n");
1616 /* Send an internal error event to mgmt application */
1617 lpfc_board_errevt_to_mgmt(phba);
1618
1619 event_data = FC_REG_DUMP_EVENT;
1620 shost = lpfc_shost_from_vport(vport);
1621 fc_host_post_vendor_event(shost, fc_get_event_number(),
1622 sizeof(event_data), (char *) &event_data,
1623 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
James Smartda0436e2009-05-22 14:51:39 -04001624}
1625
1626/**
1627 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1628 * @phba: pointer to lpfc HBA data structure.
1629 *
1630 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1631 * routine from the API jump table function pointer from the lpfc_hba struct.
1632 *
1633 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001634 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001635 * Any other value - error.
1636 **/
1637void
1638lpfc_handle_eratt(struct lpfc_hba *phba)
1639{
1640 (*phba->lpfc_handle_eratt)(phba);
1641}
1642
1643/**
James Smart3621a712009-04-06 18:47:14 -04001644 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04001645 * @phba: pointer to lpfc hba data structure.
1646 *
1647 * This routine is invoked from the worker thread to handle a HBA host
1648 * attention link event.
1649 **/
dea31012005-04-17 16:05:31 -05001650void
James Smart2e0fef82007-06-17 19:56:36 -05001651lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001652{
James Smart2e0fef82007-06-17 19:56:36 -05001653 struct lpfc_vport *vport = phba->pport;
1654 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001655 LPFC_MBOXQ_t *pmb;
1656 volatile uint32_t control;
1657 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05001658 int rc = 0;
dea31012005-04-17 16:05:31 -05001659
1660 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001661 if (!pmb) {
1662 rc = 1;
dea31012005-04-17 16:05:31 -05001663 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05001664 }
dea31012005-04-17 16:05:31 -05001665
1666 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001667 if (!mp) {
1668 rc = 2;
dea31012005-04-17 16:05:31 -05001669 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05001670 }
dea31012005-04-17 16:05:31 -05001671
1672 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05001673 if (!mp->virt) {
1674 rc = 3;
dea31012005-04-17 16:05:31 -05001675 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05001676 }
dea31012005-04-17 16:05:31 -05001677
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05001678 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04001679 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05001680
1681 psli->slistat.link_event++;
James Smart76a95d72010-11-20 23:11:48 -05001682 lpfc_read_topology(phba, pmb, mp);
1683 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smart2e0fef82007-06-17 19:56:36 -05001684 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04001685 /* Block ELS IOCBs until we have processed this mbox command */
1686 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04001687 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05001688 if (rc == MBX_NOT_FINISHED) {
1689 rc = 4;
James Smart14691152006-12-02 13:34:28 -05001690 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05001691 }
dea31012005-04-17 16:05:31 -05001692
1693 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05001694 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001695 writel(HA_LATT, phba->HAregaddr);
1696 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001697 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001698
1699 return;
1700
James Smart14691152006-12-02 13:34:28 -05001701lpfc_handle_latt_free_mbuf:
James Smart0d2b6b82008-06-14 22:52:47 -04001702 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05001703 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001704lpfc_handle_latt_free_mp:
1705 kfree(mp);
1706lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04001707 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001708lpfc_handle_latt_err_exit:
1709 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001710 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001711 psli->sli_flag |= LPFC_PROCESS_LA;
1712 control = readl(phba->HCregaddr);
1713 control |= HC_LAINT_ENA;
1714 writel(control, phba->HCregaddr);
1715 readl(phba->HCregaddr); /* flush */
1716
1717 /* Clear Link Attention in HA REG */
1718 writel(HA_LATT, phba->HAregaddr);
1719 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001720 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001721 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001722 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001723
James Smart09372822008-01-11 01:52:54 -05001724 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1725 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05001726
1727 return;
1728}
1729
James Smarte59058c2008-08-24 21:49:00 -04001730/**
James Smart3621a712009-04-06 18:47:14 -04001731 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04001732 * @phba: pointer to lpfc hba data structure.
1733 * @vpd: pointer to the vital product data.
1734 * @len: length of the vital product data in bytes.
1735 *
1736 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1737 * an array of characters. In this routine, the ModelName, ProgramType, and
1738 * ModelDesc, etc. fields of the phba data structure will be populated.
1739 *
1740 * Return codes
1741 * 0 - pointer to the VPD passed in is NULL
1742 * 1 - success
1743 **/
James Smart3772a992009-05-22 14:50:54 -04001744int
James Smart2e0fef82007-06-17 19:56:36 -05001745lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05001746{
1747 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05001748 int Length;
dea31012005-04-17 16:05:31 -05001749 int i, j;
1750 int finished = 0;
1751 int index = 0;
1752
1753 if (!vpd)
1754 return 0;
1755
1756 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05001757 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001758 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05001759 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1760 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001761 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05001762 switch (vpd[index]) {
1763 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001764 case 0x91:
dea31012005-04-17 16:05:31 -05001765 index += 1;
1766 lenlo = vpd[index];
1767 index += 1;
1768 lenhi = vpd[index];
1769 index += 1;
1770 i = ((((unsigned short)lenhi) << 8) + lenlo);
1771 index += i;
1772 break;
1773 case 0x90:
1774 index += 1;
1775 lenlo = vpd[index];
1776 index += 1;
1777 lenhi = vpd[index];
1778 index += 1;
1779 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001780 if (Length > len - index)
1781 Length = len - index;
dea31012005-04-17 16:05:31 -05001782 while (Length > 0) {
1783 /* Look for Serial Number */
1784 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1785 index += 2;
1786 i = vpd[index];
1787 index += 1;
1788 j = 0;
1789 Length -= (3+i);
1790 while(i--) {
1791 phba->SerialNumber[j++] = vpd[index++];
1792 if (j == 31)
1793 break;
1794 }
1795 phba->SerialNumber[j] = 0;
1796 continue;
1797 }
1798 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1799 phba->vpd_flag |= VPD_MODEL_DESC;
1800 index += 2;
1801 i = vpd[index];
1802 index += 1;
1803 j = 0;
1804 Length -= (3+i);
1805 while(i--) {
1806 phba->ModelDesc[j++] = vpd[index++];
1807 if (j == 255)
1808 break;
1809 }
1810 phba->ModelDesc[j] = 0;
1811 continue;
1812 }
1813 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1814 phba->vpd_flag |= VPD_MODEL_NAME;
1815 index += 2;
1816 i = vpd[index];
1817 index += 1;
1818 j = 0;
1819 Length -= (3+i);
1820 while(i--) {
1821 phba->ModelName[j++] = vpd[index++];
1822 if (j == 79)
1823 break;
1824 }
1825 phba->ModelName[j] = 0;
1826 continue;
1827 }
1828 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1829 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1830 index += 2;
1831 i = vpd[index];
1832 index += 1;
1833 j = 0;
1834 Length -= (3+i);
1835 while(i--) {
1836 phba->ProgramType[j++] = vpd[index++];
1837 if (j == 255)
1838 break;
1839 }
1840 phba->ProgramType[j] = 0;
1841 continue;
1842 }
1843 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1844 phba->vpd_flag |= VPD_PORT;
1845 index += 2;
1846 i = vpd[index];
1847 index += 1;
1848 j = 0;
1849 Length -= (3+i);
1850 while(i--) {
James Smartcd1c8302011-10-10 21:33:25 -04001851 if ((phba->sli_rev == LPFC_SLI_REV4) &&
1852 (phba->sli4_hba.pport_name_sta ==
1853 LPFC_SLI4_PPNAME_GET)) {
1854 j++;
1855 index++;
1856 } else
1857 phba->Port[j++] = vpd[index++];
1858 if (j == 19)
1859 break;
dea31012005-04-17 16:05:31 -05001860 }
James Smartcd1c8302011-10-10 21:33:25 -04001861 if ((phba->sli_rev != LPFC_SLI_REV4) ||
1862 (phba->sli4_hba.pport_name_sta ==
1863 LPFC_SLI4_PPNAME_NON))
1864 phba->Port[j] = 0;
dea31012005-04-17 16:05:31 -05001865 continue;
1866 }
1867 else {
1868 index += 2;
1869 i = vpd[index];
1870 index += 1;
1871 index += i;
1872 Length -= (3 + i);
1873 }
1874 }
1875 finished = 0;
1876 break;
1877 case 0x78:
1878 finished = 1;
1879 break;
1880 default:
1881 index ++;
1882 break;
1883 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001884 }
dea31012005-04-17 16:05:31 -05001885
1886 return(1);
1887}
1888
James Smarte59058c2008-08-24 21:49:00 -04001889/**
James Smart3621a712009-04-06 18:47:14 -04001890 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04001891 * @phba: pointer to lpfc hba data structure.
1892 * @mdp: pointer to the data structure to hold the derived model name.
1893 * @descp: pointer to the data structure to hold the derived description.
1894 *
1895 * This routine retrieves HBA's description based on its registered PCI device
1896 * ID. The @descp passed into this function points to an array of 256 chars. It
1897 * shall be returned with the model name, maximum speed, and the host bus type.
1898 * The @mdp passed into this function points to an array of 80 chars. When the
1899 * function returns, the @mdp will be filled with the model name.
1900 **/
dea31012005-04-17 16:05:31 -05001901static void
James Smart2e0fef82007-06-17 19:56:36 -05001902lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05001903{
1904 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05001905 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001906 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04001907 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04001908 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001909 struct {
James Smarta747c9c2009-11-18 15:41:10 -05001910 char *name;
1911 char *bus;
1912 char *function;
1913 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001914
1915 if (mdp && mdp[0] != '\0'
1916 && descp && descp[0] != '\0')
1917 return;
1918
James Smartc0c11512011-05-24 11:41:34 -04001919 if (phba->lmt & LMT_16Gb)
1920 max_speed = 16;
1921 else if (phba->lmt & LMT_10Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001922 max_speed = 10;
1923 else if (phba->lmt & LMT_8Gb)
1924 max_speed = 8;
1925 else if (phba->lmt & LMT_4Gb)
1926 max_speed = 4;
1927 else if (phba->lmt & LMT_2Gb)
1928 max_speed = 2;
James Smart4169d862012-09-29 11:32:09 -04001929 else if (phba->lmt & LMT_1Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001930 max_speed = 1;
James Smart4169d862012-09-29 11:32:09 -04001931 else
1932 max_speed = 0;
dea31012005-04-17 16:05:31 -05001933
1934 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05001935
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001936 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001937 case PCI_DEVICE_ID_FIREFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001938 m = (typeof(m)){"LP6000", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001939 break;
dea31012005-04-17 16:05:31 -05001940 case PCI_DEVICE_ID_SUPERFLY:
1941 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smarta747c9c2009-11-18 15:41:10 -05001942 m = (typeof(m)){"LP7000", "PCI",
1943 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001944 else
James Smarta747c9c2009-11-18 15:41:10 -05001945 m = (typeof(m)){"LP7000E", "PCI",
1946 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001947 break;
1948 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001949 m = (typeof(m)){"LP8000", "PCI",
1950 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001951 break;
1952 case PCI_DEVICE_ID_CENTAUR:
1953 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smarta747c9c2009-11-18 15:41:10 -05001954 m = (typeof(m)){"LP9002", "PCI",
1955 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001956 else
James Smarta747c9c2009-11-18 15:41:10 -05001957 m = (typeof(m)){"LP9000", "PCI",
1958 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001959 break;
1960 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001961 m = (typeof(m)){"LP952", "PCI",
1962 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001963 break;
1964 case PCI_DEVICE_ID_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05001965 m = (typeof(m)){"LP9802", "PCI-X",
1966 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001967 break;
1968 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05001969 m = (typeof(m)){"LP10000", "PCI-X",
1970 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001971 break;
1972 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05001973 m = (typeof(m)){"LPX1000", "PCI-X",
1974 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001975 break;
1976 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001977 m = (typeof(m)){"LP982", "PCI-X",
1978 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001979 break;
1980 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001981 m = (typeof(m)){"LP1050", "PCI-X",
1982 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001983 break;
1984 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05001985 m = (typeof(m)){"LP11000", "PCI-X2",
1986 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001987 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001988 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001989 m = (typeof(m)){"LP11000-SP", "PCI-X2",
1990 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001991 break;
1992 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001993 m = (typeof(m)){"LP11002-SP", "PCI-X2",
1994 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001995 break;
1996 case PCI_DEVICE_ID_NEPTUNE:
James Smarta747c9c2009-11-18 15:41:10 -05001997 m = (typeof(m)){"LPe1000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001998 break;
1999 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002000 m = (typeof(m)){"LPe1000-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002001 break;
2002 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002003 m = (typeof(m)){"LPe1002-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002004 break;
dea31012005-04-17 16:05:31 -05002005 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05002006 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002007 break;
2008 case PCI_DEVICE_ID_BSMB:
James Smarta747c9c2009-11-18 15:41:10 -05002009 m = (typeof(m)){"LP111", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002010 break;
2011 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05002012 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002013 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002014 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002015 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002016 break;
2017 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002018 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04002019 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002020 break;
dea31012005-04-17 16:05:31 -05002021 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05002022 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002023 break;
2024 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05002025 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002026 break;
2027 case PCI_DEVICE_ID_LP101:
James Smarta747c9c2009-11-18 15:41:10 -05002028 m = (typeof(m)){"LP101", "PCI-X", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05002029 break;
2030 case PCI_DEVICE_ID_LP10000S:
James Smarta747c9c2009-11-18 15:41:10 -05002031 m = (typeof(m)){"LP10000-S", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04002032 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002033 case PCI_DEVICE_ID_LP11000S:
James Smarta747c9c2009-11-18 15:41:10 -05002034 m = (typeof(m)){"LP11000-S", "PCI-X2", "Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05002035 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002036 case PCI_DEVICE_ID_LPE11000S:
James Smarta747c9c2009-11-18 15:41:10 -05002037 m = (typeof(m)){"LPe11000-S", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002038 break;
James Smartb87eab32007-04-25 09:53:28 -04002039 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05002040 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002041 break;
2042 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05002043 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002044 break;
2045 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05002046 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002047 break;
2048 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002049 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002050 break;
2051 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05002052 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002053 break;
2054 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05002055 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04002056 break;
James Smart84774a42008-08-24 21:50:06 -04002057 case PCI_DEVICE_ID_HORNET:
James Smarta747c9c2009-11-18 15:41:10 -05002058 m = (typeof(m)){"LP21000", "PCIe", "FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002059 GE = 1;
2060 break;
2061 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05002062 m = (typeof(m)){"LPev12000", "PCIe IOV",
2063 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002064 break;
2065 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05002066 m = (typeof(m)){"LPev12000", "PCIe IOV",
2067 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002068 break;
2069 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05002070 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
2071 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04002072 break;
James Smartda0436e2009-05-22 14:51:39 -04002073 case PCI_DEVICE_ID_TIGERSHARK:
2074 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002075 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04002076 break;
James Smarta747c9c2009-11-18 15:41:10 -05002077 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04002078 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05002079 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
2080 break;
2081 case PCI_DEVICE_ID_FALCON:
2082 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
2083 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04002084 break;
James Smart98fc5dd2010-06-07 15:24:29 -04002085 case PCI_DEVICE_ID_BALIUS:
2086 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
2087 "Fibre Channel Adapter"};
2088 break;
James Smart085c6472010-11-20 23:11:37 -05002089 case PCI_DEVICE_ID_LANCER_FC:
James Smartc0c11512011-05-24 11:41:34 -04002090 case PCI_DEVICE_ID_LANCER_FC_VF:
2091 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
James Smart085c6472010-11-20 23:11:37 -05002092 break;
2093 case PCI_DEVICE_ID_LANCER_FCOE:
James Smartc0c11512011-05-24 11:41:34 -04002094 case PCI_DEVICE_ID_LANCER_FCOE_VF:
James Smart085c6472010-11-20 23:11:37 -05002095 oneConnect = 1;
James Smart079b5c92011-08-21 21:48:49 -04002096 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
James Smart085c6472010-11-20 23:11:37 -05002097 break;
James Smartf8cafd32012-08-03 12:42:51 -04002098 case PCI_DEVICE_ID_SKYHAWK:
2099 case PCI_DEVICE_ID_SKYHAWK_VF:
2100 oneConnect = 1;
2101 m = (typeof(m)){"OCe14000", "PCIe", "FCoE"};
2102 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05002103 default:
James Smarta747c9c2009-11-18 15:41:10 -05002104 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05002105 break;
dea31012005-04-17 16:05:31 -05002106 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05002107
2108 if (mdp && mdp[0] == '\0')
2109 snprintf(mdp, 79,"%s", m.name);
James Smartc0c11512011-05-24 11:41:34 -04002110 /*
2111 * oneConnect hba requires special processing, they are all initiators
James Smartda0436e2009-05-22 14:51:39 -04002112 * and we put the port number on the end
2113 */
2114 if (descp && descp[0] == '\0') {
2115 if (oneConnect)
2116 snprintf(descp, 255,
James Smart4169d862012-09-29 11:32:09 -04002117 "Emulex OneConnect %s, %s Initiator %s",
James Smarta747c9c2009-11-18 15:41:10 -05002118 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04002119 phba->Port);
James Smart4169d862012-09-29 11:32:09 -04002120 else if (max_speed == 0)
2121 snprintf(descp, 255,
2122 "Emulex %s %s %s ",
2123 m.name, m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002124 else
2125 snprintf(descp, 255,
2126 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05002127 m.name, max_speed, (GE) ? "GE" : "Gb",
2128 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002129 }
dea31012005-04-17 16:05:31 -05002130}
2131
James Smarte59058c2008-08-24 21:49:00 -04002132/**
James Smart3621a712009-04-06 18:47:14 -04002133 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04002134 * @phba: pointer to lpfc hba data structure.
2135 * @pring: pointer to a IOCB ring.
2136 * @cnt: the number of IOCBs to be posted to the IOCB ring.
2137 *
2138 * This routine posts a given number of IOCBs with the associated DMA buffer
2139 * descriptors specified by the cnt argument to the given IOCB ring.
2140 *
2141 * Return codes
2142 * The number of IOCBs NOT able to be posted to the IOCB ring.
2143 **/
dea31012005-04-17 16:05:31 -05002144int
James Smart495a7142008-06-14 22:52:59 -04002145lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05002146{
2147 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002148 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05002149 struct lpfc_dmabuf *mp1, *mp2;
2150
2151 cnt += pring->missbufcnt;
2152
2153 /* While there are buffers to post */
2154 while (cnt > 0) {
2155 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002156 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002157 if (iocb == NULL) {
2158 pring->missbufcnt = cnt;
2159 return cnt;
2160 }
dea31012005-04-17 16:05:31 -05002161 icmd = &iocb->iocb;
2162
2163 /* 2 buffers can be posted per command */
2164 /* Allocate buffer to post */
2165 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2166 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04002167 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2168 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002169 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002170 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002171 pring->missbufcnt = cnt;
2172 return cnt;
2173 }
2174
2175 INIT_LIST_HEAD(&mp1->list);
2176 /* Allocate buffer to post */
2177 if (cnt > 1) {
2178 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2179 if (mp2)
2180 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2181 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04002182 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002183 kfree(mp2);
dea31012005-04-17 16:05:31 -05002184 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2185 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002186 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002187 pring->missbufcnt = cnt;
2188 return cnt;
2189 }
2190
2191 INIT_LIST_HEAD(&mp2->list);
2192 } else {
2193 mp2 = NULL;
2194 }
2195
2196 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2197 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2198 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2199 icmd->ulpBdeCount = 1;
2200 cnt--;
2201 if (mp2) {
2202 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2203 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2204 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2205 cnt--;
2206 icmd->ulpBdeCount = 2;
2207 }
2208
2209 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2210 icmd->ulpLe = 1;
2211
James Smart3772a992009-05-22 14:50:54 -04002212 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2213 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05002214 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2215 kfree(mp1);
2216 cnt++;
2217 if (mp2) {
2218 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2219 kfree(mp2);
2220 cnt++;
2221 }
James Bottomley604a3e32005-10-29 10:28:33 -05002222 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002223 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05002224 return cnt;
2225 }
dea31012005-04-17 16:05:31 -05002226 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05002227 if (mp2)
dea31012005-04-17 16:05:31 -05002228 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05002229 }
2230 pring->missbufcnt = 0;
2231 return 0;
2232}
2233
James Smarte59058c2008-08-24 21:49:00 -04002234/**
James Smart3621a712009-04-06 18:47:14 -04002235 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04002236 * @phba: pointer to lpfc hba data structure.
2237 *
2238 * This routine posts initial receive IOCB buffers to the ELS ring. The
2239 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
2240 * set to 64 IOCBs.
2241 *
2242 * Return codes
2243 * 0 - success (currently always success)
2244 **/
dea31012005-04-17 16:05:31 -05002245static int
James Smart2e0fef82007-06-17 19:56:36 -05002246lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002247{
2248 struct lpfc_sli *psli = &phba->sli;
2249
2250 /* Ring 0, ELS / CT buffers */
James Smart495a7142008-06-14 22:52:59 -04002251 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05002252 /* Ring 2 - FCP no buffers needed */
2253
2254 return 0;
2255}
2256
2257#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2258
James Smarte59058c2008-08-24 21:49:00 -04002259/**
James Smart3621a712009-04-06 18:47:14 -04002260 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04002261 * @HashResultPointer: pointer to an array as hash table.
2262 *
2263 * This routine sets up the initial values to the array of hash table entries
2264 * for the LC HBAs.
2265 **/
dea31012005-04-17 16:05:31 -05002266static void
2267lpfc_sha_init(uint32_t * HashResultPointer)
2268{
2269 HashResultPointer[0] = 0x67452301;
2270 HashResultPointer[1] = 0xEFCDAB89;
2271 HashResultPointer[2] = 0x98BADCFE;
2272 HashResultPointer[3] = 0x10325476;
2273 HashResultPointer[4] = 0xC3D2E1F0;
2274}
2275
James Smarte59058c2008-08-24 21:49:00 -04002276/**
James Smart3621a712009-04-06 18:47:14 -04002277 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04002278 * @HashResultPointer: pointer to an initial/result hash table.
2279 * @HashWorkingPointer: pointer to an working hash table.
2280 *
2281 * This routine iterates an initial hash table pointed by @HashResultPointer
2282 * with the values from the working hash table pointeed by @HashWorkingPointer.
2283 * The results are putting back to the initial hash table, returned through
2284 * the @HashResultPointer as the result hash table.
2285 **/
dea31012005-04-17 16:05:31 -05002286static void
2287lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2288{
2289 int t;
2290 uint32_t TEMP;
2291 uint32_t A, B, C, D, E;
2292 t = 16;
2293 do {
2294 HashWorkingPointer[t] =
2295 S(1,
2296 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2297 8] ^
2298 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2299 } while (++t <= 79);
2300 t = 0;
2301 A = HashResultPointer[0];
2302 B = HashResultPointer[1];
2303 C = HashResultPointer[2];
2304 D = HashResultPointer[3];
2305 E = HashResultPointer[4];
2306
2307 do {
2308 if (t < 20) {
2309 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2310 } else if (t < 40) {
2311 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2312 } else if (t < 60) {
2313 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2314 } else {
2315 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2316 }
2317 TEMP += S(5, A) + E + HashWorkingPointer[t];
2318 E = D;
2319 D = C;
2320 C = S(30, B);
2321 B = A;
2322 A = TEMP;
2323 } while (++t <= 79);
2324
2325 HashResultPointer[0] += A;
2326 HashResultPointer[1] += B;
2327 HashResultPointer[2] += C;
2328 HashResultPointer[3] += D;
2329 HashResultPointer[4] += E;
2330
2331}
2332
James Smarte59058c2008-08-24 21:49:00 -04002333/**
James Smart3621a712009-04-06 18:47:14 -04002334 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002335 * @RandomChallenge: pointer to the entry of host challenge random number array.
2336 * @HashWorking: pointer to the entry of the working hash array.
2337 *
2338 * This routine calculates the working hash array referred by @HashWorking
2339 * from the challenge random numbers associated with the host, referred by
2340 * @RandomChallenge. The result is put into the entry of the working hash
2341 * array and returned by reference through @HashWorking.
2342 **/
dea31012005-04-17 16:05:31 -05002343static void
2344lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2345{
2346 *HashWorking = (*RandomChallenge ^ *HashWorking);
2347}
2348
James Smarte59058c2008-08-24 21:49:00 -04002349/**
James Smart3621a712009-04-06 18:47:14 -04002350 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002351 * @phba: pointer to lpfc hba data structure.
2352 * @hbainit: pointer to an array of unsigned 32-bit integers.
2353 *
2354 * This routine performs the special handling for LC HBA initialization.
2355 **/
dea31012005-04-17 16:05:31 -05002356void
2357lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2358{
2359 int t;
2360 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002361 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002362
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002363 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002364 if (!HashWorking)
2365 return;
2366
dea31012005-04-17 16:05:31 -05002367 HashWorking[0] = HashWorking[78] = *pwwnn++;
2368 HashWorking[1] = HashWorking[79] = *pwwnn;
2369
2370 for (t = 0; t < 7; t++)
2371 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2372
2373 lpfc_sha_init(hbainit);
2374 lpfc_sha_iterate(hbainit, HashWorking);
2375 kfree(HashWorking);
2376}
2377
James Smarte59058c2008-08-24 21:49:00 -04002378/**
James Smart3621a712009-04-06 18:47:14 -04002379 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002380 * @vport: pointer to a virtual N_Port data structure.
2381 *
2382 * This routine performs the necessary cleanups before deleting the @vport.
2383 * It invokes the discovery state machine to perform necessary state
2384 * transitions and to release the ndlps associated with the @vport. Note,
2385 * the physical port is treated as @vport 0.
2386 **/
James Smart87af33f2007-10-27 13:37:43 -04002387void
James Smart2e0fef82007-06-17 19:56:36 -05002388lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002389{
James Smart87af33f2007-10-27 13:37:43 -04002390 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002391 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002392 int i = 0;
dea31012005-04-17 16:05:31 -05002393
James Smart87af33f2007-10-27 13:37:43 -04002394 if (phba->link_state > LPFC_LINK_DOWN)
2395 lpfc_port_link_failure(vport);
2396
2397 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002398 if (!NLP_CHK_NODE_ACT(ndlp)) {
2399 ndlp = lpfc_enable_node(vport, ndlp,
2400 NLP_STE_UNUSED_NODE);
2401 if (!ndlp)
2402 continue;
2403 spin_lock_irq(&phba->ndlp_lock);
2404 NLP_SET_FREE_REQ(ndlp);
2405 spin_unlock_irq(&phba->ndlp_lock);
2406 /* Trigger the release of the ndlp memory */
2407 lpfc_nlp_put(ndlp);
2408 continue;
2409 }
2410 spin_lock_irq(&phba->ndlp_lock);
2411 if (NLP_CHK_FREE_REQ(ndlp)) {
2412 /* The ndlp should not be in memory free mode already */
2413 spin_unlock_irq(&phba->ndlp_lock);
2414 continue;
2415 } else
2416 /* Indicate request for freeing ndlp memory */
2417 NLP_SET_FREE_REQ(ndlp);
2418 spin_unlock_irq(&phba->ndlp_lock);
2419
James Smart58da1ff2008-04-07 10:15:56 -04002420 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2421 ndlp->nlp_DID == Fabric_DID) {
2422 /* Just free up ndlp with Fabric_DID for vports */
2423 lpfc_nlp_put(ndlp);
2424 continue;
2425 }
2426
James Smarteff4a012012-01-18 16:25:25 -05002427 /* take care of nodes in unused state before the state
2428 * machine taking action.
2429 */
2430 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
2431 lpfc_nlp_put(ndlp);
2432 continue;
2433 }
2434
James Smart87af33f2007-10-27 13:37:43 -04002435 if (ndlp->nlp_type & NLP_FABRIC)
2436 lpfc_disc_state_machine(vport, ndlp, NULL,
2437 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002438
James Smart87af33f2007-10-27 13:37:43 -04002439 lpfc_disc_state_machine(vport, ndlp, NULL,
2440 NLP_EVT_DEVICE_RM);
2441 }
2442
James Smarta8adb832007-10-27 13:37:53 -04002443 /* At this point, ALL ndlp's should be gone
2444 * because of the previous NLP_EVT_DEVICE_RM.
2445 * Lets wait for this to happen, if needed.
2446 */
James Smart87af33f2007-10-27 13:37:43 -04002447 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002448 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002449 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002450 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002451 list_for_each_entry_safe(ndlp, next_ndlp,
2452 &vport->fc_nodes, nlp_listp) {
2453 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2454 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002455 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002456 "usgmap:x%x refcnt:%d\n",
2457 ndlp->nlp_DID, (void *)ndlp,
2458 ndlp->nlp_usg_map,
2459 atomic_read(
2460 &ndlp->kref.refcount));
2461 }
James Smarta8adb832007-10-27 13:37:53 -04002462 break;
James Smart87af33f2007-10-27 13:37:43 -04002463 }
James Smarta8adb832007-10-27 13:37:53 -04002464
2465 /* Wait for any activity on ndlps to settle */
2466 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002467 }
James Smart1151e3e2011-02-16 12:39:35 -05002468 lpfc_cleanup_vports_rrqs(vport, NULL);
dea31012005-04-17 16:05:31 -05002469}
2470
James Smarte59058c2008-08-24 21:49:00 -04002471/**
James Smart3621a712009-04-06 18:47:14 -04002472 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002473 * @vport: pointer to a virtual N_Port data structure.
2474 *
2475 * This routine stops all the timers associated with a @vport. This function
2476 * is invoked before disabling or deleting a @vport. Note that the physical
2477 * port is treated as @vport 0.
2478 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002479void
2480lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002481{
James Smart92d7f7b2007-06-17 19:56:38 -05002482 del_timer_sync(&vport->els_tmofunc);
2483 del_timer_sync(&vport->fc_fdmitmo);
James Smart92494142011-02-16 12:39:44 -05002484 del_timer_sync(&vport->delayed_disc_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002485 lpfc_can_disctmo(vport);
2486 return;
dea31012005-04-17 16:05:31 -05002487}
2488
James Smarte59058c2008-08-24 21:49:00 -04002489/**
James Smartecfd03c2010-02-12 14:41:27 -05002490 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2491 * @phba: pointer to lpfc hba data structure.
2492 *
2493 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2494 * caller of this routine should already hold the host lock.
2495 **/
2496void
2497__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2498{
James Smart5ac6b302010-10-22 11:05:36 -04002499 /* Clear pending FCF rediscovery wait flag */
2500 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2501
James Smartecfd03c2010-02-12 14:41:27 -05002502 /* Now, try to stop the timer */
2503 del_timer(&phba->fcf.redisc_wait);
2504}
2505
2506/**
2507 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2508 * @phba: pointer to lpfc hba data structure.
2509 *
2510 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2511 * checks whether the FCF rediscovery wait timer is pending with the host
2512 * lock held before proceeding with disabling the timer and clearing the
2513 * wait timer pendig flag.
2514 **/
2515void
2516lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2517{
2518 spin_lock_irq(&phba->hbalock);
2519 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2520 /* FCF rediscovery timer already fired or stopped */
2521 spin_unlock_irq(&phba->hbalock);
2522 return;
2523 }
2524 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart5ac6b302010-10-22 11:05:36 -04002525 /* Clear failover in progress flags */
2526 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05002527 spin_unlock_irq(&phba->hbalock);
2528}
2529
2530/**
James Smart3772a992009-05-22 14:50:54 -04002531 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002532 * @phba: pointer to lpfc hba data structure.
2533 *
2534 * This routine stops all the timers associated with a HBA. This function is
2535 * invoked before either putting a HBA offline or unloading the driver.
2536 **/
James Smart3772a992009-05-22 14:50:54 -04002537void
2538lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002539{
James Smart51ef4c22007-08-02 11:10:31 -04002540 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05002541 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002542 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002543 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002544 del_timer_sync(&phba->hb_tmofunc);
James Smart1151e3e2011-02-16 12:39:35 -05002545 if (phba->sli_rev == LPFC_SLI_REV4) {
2546 del_timer_sync(&phba->rrq_tmr);
2547 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2548 }
James Smart3772a992009-05-22 14:50:54 -04002549 phba->hb_outstanding = 0;
2550
2551 switch (phba->pci_dev_grp) {
2552 case LPFC_PCI_DEV_LP:
2553 /* Stop any LightPulse device specific driver timers */
2554 del_timer_sync(&phba->fcp_poll_timer);
2555 break;
2556 case LPFC_PCI_DEV_OC:
2557 /* Stop any OneConnect device sepcific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05002558 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04002559 break;
2560 default:
2561 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2562 "0297 Invalid device group (x%x)\n",
2563 phba->pci_dev_grp);
2564 break;
2565 }
James Smart2e0fef82007-06-17 19:56:36 -05002566 return;
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_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002571 * @phba: pointer to lpfc hba data structure.
2572 *
2573 * This routine marks a HBA's management interface as blocked. Once the HBA's
2574 * management interface is marked as blocked, all the user space access to
2575 * the HBA, whether they are from sysfs interface or libdfc interface will
2576 * all be blocked. The HBA is set to block the management interface when the
2577 * driver prepares the HBA interface for online or offline.
2578 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002579static void
James Smart618a5232012-06-12 13:54:36 -04002580lpfc_block_mgmt_io(struct lpfc_hba *phba, int mbx_action)
Adrian Bunka6ababd2007-11-05 18:07:33 +01002581{
2582 unsigned long iflag;
James Smart6e7288d2010-06-07 15:23:35 -04002583 uint8_t actcmd = MBX_HEARTBEAT;
2584 unsigned long timeout;
2585
Adrian Bunka6ababd2007-11-05 18:07:33 +01002586 spin_lock_irqsave(&phba->hbalock, iflag);
2587 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
James Smart618a5232012-06-12 13:54:36 -04002588 spin_unlock_irqrestore(&phba->hbalock, iflag);
2589 if (mbx_action == LPFC_MBX_NO_WAIT)
2590 return;
2591 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
2592 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002593 if (phba->sli.mbox_active) {
James Smart6e7288d2010-06-07 15:23:35 -04002594 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
James Smarta183a152011-10-10 21:32:43 -04002595 /* Determine how long we might wait for the active mailbox
2596 * command to be gracefully completed by firmware.
2597 */
2598 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
2599 phba->sli.mbox_active) * 1000) + jiffies;
2600 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002601 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002602
James Smart6e7288d2010-06-07 15:23:35 -04002603 /* Wait for the outstnading mailbox command to complete */
2604 while (phba->sli.mbox_active) {
2605 /* Check active mailbox complete status every 2ms */
2606 msleep(2);
2607 if (time_after(jiffies, timeout)) {
2608 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2609 "2813 Mgmt IO is Blocked %x "
2610 "- mbox cmd %x still active\n",
2611 phba->sli.sli_flag, actcmd);
2612 break;
2613 }
2614 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002615}
2616
James Smarte59058c2008-08-24 21:49:00 -04002617/**
James Smart6b5151f2012-01-18 16:24:06 -05002618 * lpfc_sli4_node_prep - Assign RPIs for active nodes.
2619 * @phba: pointer to lpfc hba data structure.
2620 *
2621 * Allocate RPIs for all active remote nodes. This is needed whenever
2622 * an SLI4 adapter is reset and the driver is not unloading. Its purpose
2623 * is to fixup the temporary rpi assignments.
2624 **/
2625void
2626lpfc_sli4_node_prep(struct lpfc_hba *phba)
2627{
2628 struct lpfc_nodelist *ndlp, *next_ndlp;
2629 struct lpfc_vport **vports;
2630 int i;
2631
2632 if (phba->sli_rev != LPFC_SLI_REV4)
2633 return;
2634
2635 vports = lpfc_create_vport_work_array(phba);
2636 if (vports != NULL) {
2637 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2638 if (vports[i]->load_flag & FC_UNLOADING)
2639 continue;
2640
2641 list_for_each_entry_safe(ndlp, next_ndlp,
2642 &vports[i]->fc_nodes,
2643 nlp_listp) {
2644 if (NLP_CHK_NODE_ACT(ndlp))
2645 ndlp->nlp_rpi =
2646 lpfc_sli4_alloc_rpi(phba);
2647 }
2648 }
2649 }
2650 lpfc_destroy_vport_work_array(phba, vports);
2651}
2652
2653/**
James Smart3621a712009-04-06 18:47:14 -04002654 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04002655 * @phba: pointer to lpfc hba data structure.
2656 *
2657 * This routine initializes the HBA and brings a HBA online. During this
2658 * process, the management interface is blocked to prevent user space access
2659 * to the HBA interfering with the driver initialization.
2660 *
2661 * Return codes
2662 * 0 - successful
2663 * 1 - failed
2664 **/
dea31012005-04-17 16:05:31 -05002665int
James Smart2e0fef82007-06-17 19:56:36 -05002666lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002667{
Julia Lawall372bd282008-12-16 16:15:08 +01002668 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04002669 struct lpfc_vport **vports;
2670 int i;
James Smart16a3a202013-04-17 20:14:38 -04002671 bool vpis_cleared = false;
James Smart2e0fef82007-06-17 19:56:36 -05002672
dea31012005-04-17 16:05:31 -05002673 if (!phba)
2674 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01002675 vport = phba->pport;
dea31012005-04-17 16:05:31 -05002676
James Smart2e0fef82007-06-17 19:56:36 -05002677 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05002678 return 0;
2679
James Smarted957682007-06-17 19:56:37 -05002680 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002681 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05002682
James Smart618a5232012-06-12 13:54:36 -04002683 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05002684
James Smart46fa3112007-04-25 09:51:45 -04002685 if (!lpfc_sli_queue_setup(phba)) {
2686 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002687 return 1;
James Smart46fa3112007-04-25 09:51:45 -04002688 }
2689
James Smartda0436e2009-05-22 14:51:39 -04002690 if (phba->sli_rev == LPFC_SLI_REV4) {
2691 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2692 lpfc_unblock_mgmt_io(phba);
2693 return 1;
2694 }
James Smart16a3a202013-04-17 20:14:38 -04002695 spin_lock_irq(&phba->hbalock);
2696 if (!phba->sli4_hba.max_cfg_param.vpi_used)
2697 vpis_cleared = true;
2698 spin_unlock_irq(&phba->hbalock);
James Smartda0436e2009-05-22 14:51:39 -04002699 } else {
2700 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2701 lpfc_unblock_mgmt_io(phba);
2702 return 1;
2703 }
James Smart46fa3112007-04-25 09:51:45 -04002704 }
dea31012005-04-17 16:05:31 -05002705
James Smart549e55c2007-08-02 11:09:51 -04002706 vports = lpfc_create_vport_work_array(phba);
2707 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002708 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002709 struct Scsi_Host *shost;
2710 shost = lpfc_shost_from_vport(vports[i]);
2711 spin_lock_irq(shost->host_lock);
2712 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2713 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2714 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart16a3a202013-04-17 20:14:38 -04002715 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart1c6834a2009-07-19 10:01:26 -04002716 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart16a3a202013-04-17 20:14:38 -04002717 if ((vpis_cleared) &&
2718 (vports[i]->port_type !=
2719 LPFC_PHYSICAL_PORT))
2720 vports[i]->vpi = 0;
2721 }
James Smart549e55c2007-08-02 11:09:51 -04002722 spin_unlock_irq(shost->host_lock);
2723 }
James Smart09372822008-01-11 01:52:54 -05002724 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002725
James Smart46fa3112007-04-25 09:51:45 -04002726 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002727 return 0;
2728}
2729
James Smarte59058c2008-08-24 21:49:00 -04002730/**
James Smart3621a712009-04-06 18:47:14 -04002731 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04002732 * @phba: pointer to lpfc hba data structure.
2733 *
2734 * This routine marks a HBA's management interface as not blocked. Once the
2735 * HBA's management interface is marked as not blocked, all the user space
2736 * access to the HBA, whether they are from sysfs interface or libdfc
2737 * interface will be allowed. The HBA is set to block the management interface
2738 * when the driver prepares the HBA interface for online or offline and then
2739 * set to unblock the management interface afterwards.
2740 **/
James Smart46fa3112007-04-25 09:51:45 -04002741void
James Smart46fa3112007-04-25 09:51:45 -04002742lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2743{
2744 unsigned long iflag;
2745
James Smart2e0fef82007-06-17 19:56:36 -05002746 spin_lock_irqsave(&phba->hbalock, iflag);
2747 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2748 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04002749}
2750
James Smarte59058c2008-08-24 21:49:00 -04002751/**
James Smart3621a712009-04-06 18:47:14 -04002752 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04002753 * @phba: pointer to lpfc hba data structure.
2754 *
2755 * This routine is invoked to prepare a HBA to be brought offline. It performs
2756 * unregistration login to all the nodes on all vports and flushes the mailbox
2757 * queue to make it ready to be brought offline.
2758 **/
James Smart46fa3112007-04-25 09:51:45 -04002759void
James Smart618a5232012-06-12 13:54:36 -04002760lpfc_offline_prep(struct lpfc_hba *phba, int mbx_action)
James Smart46fa3112007-04-25 09:51:45 -04002761{
James Smart2e0fef82007-06-17 19:56:36 -05002762 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04002763 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04002764 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05002765 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04002766 int i;
dea31012005-04-17 16:05:31 -05002767
James Smart2e0fef82007-06-17 19:56:36 -05002768 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002769 return;
dea31012005-04-17 16:05:31 -05002770
James Smart618a5232012-06-12 13:54:36 -04002771 lpfc_block_mgmt_io(phba, mbx_action);
dea31012005-04-17 16:05:31 -05002772
2773 lpfc_linkdown(phba);
2774
James Smart87af33f2007-10-27 13:37:43 -04002775 /* Issue an unreg_login to all nodes on all vports */
2776 vports = lpfc_create_vport_work_array(phba);
2777 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04002778 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04002779 if (vports[i]->load_flag & FC_UNLOADING)
2780 continue;
James Smart72100cc2010-02-12 14:43:01 -05002781 shost = lpfc_shost_from_vport(vports[i]);
2782 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002783 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002784 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2785 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05002786 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002787
James Smart87af33f2007-10-27 13:37:43 -04002788 shost = lpfc_shost_from_vport(vports[i]);
2789 list_for_each_entry_safe(ndlp, next_ndlp,
2790 &vports[i]->fc_nodes,
2791 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002792 if (!NLP_CHK_NODE_ACT(ndlp))
2793 continue;
James Smart87af33f2007-10-27 13:37:43 -04002794 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2795 continue;
2796 if (ndlp->nlp_type & NLP_FABRIC) {
2797 lpfc_disc_state_machine(vports[i], ndlp,
2798 NULL, NLP_EVT_DEVICE_RECOVERY);
2799 lpfc_disc_state_machine(vports[i], ndlp,
2800 NULL, NLP_EVT_DEVICE_RM);
2801 }
2802 spin_lock_irq(shost->host_lock);
2803 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart401ee0c2012-03-01 22:35:34 -05002804 spin_unlock_irq(shost->host_lock);
James Smart6b5151f2012-01-18 16:24:06 -05002805 /*
2806 * Whenever an SLI4 port goes offline, free the
James Smart401ee0c2012-03-01 22:35:34 -05002807 * RPI. Get a new RPI when the adapter port
2808 * comes back online.
James Smart6b5151f2012-01-18 16:24:06 -05002809 */
2810 if (phba->sli_rev == LPFC_SLI_REV4)
2811 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smart87af33f2007-10-27 13:37:43 -04002812 lpfc_unreg_rpi(vports[i], ndlp);
2813 }
2814 }
2815 }
James Smart09372822008-01-11 01:52:54 -05002816 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002817
James Smart618a5232012-06-12 13:54:36 -04002818 lpfc_sli_mbox_sys_shutdown(phba, mbx_action);
James Smart46fa3112007-04-25 09:51:45 -04002819}
2820
James Smarte59058c2008-08-24 21:49:00 -04002821/**
James Smart3621a712009-04-06 18:47:14 -04002822 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04002823 * @phba: pointer to lpfc hba data structure.
2824 *
2825 * This routine actually brings a HBA offline. It stops all the timers
2826 * associated with the HBA, brings down the SLI layer, and eventually
2827 * marks the HBA as in offline state for the upper layer protocol.
2828 **/
James Smart46fa3112007-04-25 09:51:45 -04002829void
James Smart2e0fef82007-06-17 19:56:36 -05002830lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04002831{
James Smart549e55c2007-08-02 11:09:51 -04002832 struct Scsi_Host *shost;
2833 struct lpfc_vport **vports;
2834 int i;
James Smart46fa3112007-04-25 09:51:45 -04002835
James Smart549e55c2007-08-02 11:09:51 -04002836 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002837 return;
James Smart688a8862006-07-06 15:49:56 -04002838
James Smartda0436e2009-05-22 14:51:39 -04002839 /* stop port and all timers associated with this hba */
2840 lpfc_stop_port(phba);
James Smart51ef4c22007-08-02 11:10:31 -04002841 vports = lpfc_create_vport_work_array(phba);
2842 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002843 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04002844 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05002845 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05002846 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002847 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05002848 /* Bring down the SLI Layer and cleanup. The HBA is offline
2849 now. */
2850 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002851 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04002852 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002853 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04002854 vports = lpfc_create_vport_work_array(phba);
2855 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002856 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002857 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04002858 spin_lock_irq(shost->host_lock);
2859 vports[i]->work_port_events = 0;
2860 vports[i]->fc_flag |= FC_OFFLINE_MODE;
2861 spin_unlock_irq(shost->host_lock);
2862 }
James Smart09372822008-01-11 01:52:54 -05002863 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002864}
2865
James Smarte59058c2008-08-24 21:49:00 -04002866/**
James Smart3621a712009-04-06 18:47:14 -04002867 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04002868 * @phba: pointer to lpfc hba data structure.
2869 *
2870 * This routine is to free all the SCSI buffers and IOCBs from the driver
2871 * list back to kernel. It is called from lpfc_pci_remove_one to free
2872 * the internal resources before the device is removed from the system.
James Smarte59058c2008-08-24 21:49:00 -04002873 **/
James Smart8a9d2e82012-05-09 21:16:12 -04002874static void
James Smart2e0fef82007-06-17 19:56:36 -05002875lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002876{
2877 struct lpfc_scsi_buf *sb, *sb_next;
2878 struct lpfc_iocbq *io, *io_next;
2879
James Smart2e0fef82007-06-17 19:56:36 -05002880 spin_lock_irq(&phba->hbalock);
James Smarta40fc5f2013-04-17 20:17:40 -04002881
dea31012005-04-17 16:05:31 -05002882 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smarta40fc5f2013-04-17 20:17:40 -04002883
2884 spin_lock(&phba->scsi_buf_list_put_lock);
2885 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_put,
2886 list) {
dea31012005-04-17 16:05:31 -05002887 list_del(&sb->list);
2888 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05002889 sb->dma_handle);
dea31012005-04-17 16:05:31 -05002890 kfree(sb);
2891 phba->total_scsi_bufs--;
2892 }
James Smarta40fc5f2013-04-17 20:17:40 -04002893 spin_unlock(&phba->scsi_buf_list_put_lock);
2894
2895 spin_lock(&phba->scsi_buf_list_get_lock);
2896 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list_get,
2897 list) {
2898 list_del(&sb->list);
2899 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
2900 sb->dma_handle);
2901 kfree(sb);
2902 phba->total_scsi_bufs--;
2903 }
2904 spin_unlock(&phba->scsi_buf_list_get_lock);
dea31012005-04-17 16:05:31 -05002905
2906 /* Release all the lpfc_iocbq entries maintained by this host. */
2907 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2908 list_del(&io->list);
2909 kfree(io);
2910 phba->total_iocbq_bufs--;
2911 }
James Smart6d368e52011-05-24 11:44:12 -04002912
James Smart2e0fef82007-06-17 19:56:36 -05002913 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04002914}
2915
2916/**
2917 * lpfc_sli4_xri_sgl_update - update xri-sgl sizing and mapping
2918 * @phba: pointer to lpfc hba data structure.
2919 *
2920 * This routine first calculates the sizes of the current els and allocated
2921 * scsi sgl lists, and then goes through all sgls to updates the physical
2922 * XRIs assigned due to port function reset. During port initialization, the
2923 * current els and allocated scsi sgl lists are 0s.
2924 *
2925 * Return codes
2926 * 0 - successful (for now, it always returns 0)
2927 **/
2928int
2929lpfc_sli4_xri_sgl_update(struct lpfc_hba *phba)
2930{
2931 struct lpfc_sglq *sglq_entry = NULL, *sglq_entry_next = NULL;
2932 struct lpfc_scsi_buf *psb = NULL, *psb_next = NULL;
2933 uint16_t i, lxri, xri_cnt, els_xri_cnt, scsi_xri_cnt;
2934 LIST_HEAD(els_sgl_list);
2935 LIST_HEAD(scsi_sgl_list);
2936 int rc;
2937
2938 /*
2939 * update on pci function's els xri-sgl list
2940 */
2941 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
2942 if (els_xri_cnt > phba->sli4_hba.els_xri_cnt) {
2943 /* els xri-sgl expanded */
2944 xri_cnt = els_xri_cnt - phba->sli4_hba.els_xri_cnt;
2945 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2946 "3157 ELS xri-sgl count increased from "
2947 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
2948 els_xri_cnt);
2949 /* allocate the additional els sgls */
2950 for (i = 0; i < xri_cnt; i++) {
2951 sglq_entry = kzalloc(sizeof(struct lpfc_sglq),
2952 GFP_KERNEL);
2953 if (sglq_entry == NULL) {
2954 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2955 "2562 Failure to allocate an "
2956 "ELS sgl entry:%d\n", i);
2957 rc = -ENOMEM;
2958 goto out_free_mem;
2959 }
2960 sglq_entry->buff_type = GEN_BUFF_TYPE;
2961 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0,
2962 &sglq_entry->phys);
2963 if (sglq_entry->virt == NULL) {
2964 kfree(sglq_entry);
2965 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2966 "2563 Failure to allocate an "
2967 "ELS mbuf:%d\n", i);
2968 rc = -ENOMEM;
2969 goto out_free_mem;
2970 }
2971 sglq_entry->sgl = sglq_entry->virt;
2972 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
2973 sglq_entry->state = SGL_FREED;
2974 list_add_tail(&sglq_entry->list, &els_sgl_list);
2975 }
James Smart38c20672013-03-01 16:37:44 -05002976 spin_lock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04002977 list_splice_init(&els_sgl_list, &phba->sli4_hba.lpfc_sgl_list);
James Smart38c20672013-03-01 16:37:44 -05002978 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04002979 } else if (els_xri_cnt < phba->sli4_hba.els_xri_cnt) {
2980 /* els xri-sgl shrinked */
2981 xri_cnt = phba->sli4_hba.els_xri_cnt - els_xri_cnt;
2982 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2983 "3158 ELS xri-sgl count decreased from "
2984 "%d to %d\n", phba->sli4_hba.els_xri_cnt,
2985 els_xri_cnt);
2986 spin_lock_irq(&phba->hbalock);
2987 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &els_sgl_list);
2988 spin_unlock_irq(&phba->hbalock);
2989 /* release extra els sgls from list */
2990 for (i = 0; i < xri_cnt; i++) {
2991 list_remove_head(&els_sgl_list,
2992 sglq_entry, struct lpfc_sglq, list);
2993 if (sglq_entry) {
2994 lpfc_mbuf_free(phba, sglq_entry->virt,
2995 sglq_entry->phys);
2996 kfree(sglq_entry);
2997 }
2998 }
2999 spin_lock_irq(&phba->hbalock);
3000 list_splice_init(&els_sgl_list, &phba->sli4_hba.lpfc_sgl_list);
3001 spin_unlock_irq(&phba->hbalock);
3002 } else
3003 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3004 "3163 ELS xri-sgl count unchanged: %d\n",
3005 els_xri_cnt);
3006 phba->sli4_hba.els_xri_cnt = els_xri_cnt;
3007
3008 /* update xris to els sgls on the list */
3009 sglq_entry = NULL;
3010 sglq_entry_next = NULL;
3011 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
3012 &phba->sli4_hba.lpfc_sgl_list, list) {
3013 lxri = lpfc_sli4_next_xritag(phba);
3014 if (lxri == NO_XRI) {
3015 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3016 "2400 Failed to allocate xri for "
3017 "ELS sgl\n");
3018 rc = -ENOMEM;
3019 goto out_free_mem;
3020 }
3021 sglq_entry->sli4_lxritag = lxri;
3022 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3023 }
3024
3025 /*
3026 * update on pci function's allocated scsi xri-sgl list
3027 */
3028 phba->total_scsi_bufs = 0;
3029
3030 /* maximum number of xris available for scsi buffers */
3031 phba->sli4_hba.scsi_xri_max = phba->sli4_hba.max_cfg_param.max_xri -
3032 els_xri_cnt;
3033
3034 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3035 "2401 Current allocated SCSI xri-sgl count:%d, "
3036 "maximum SCSI xri count:%d\n",
3037 phba->sli4_hba.scsi_xri_cnt,
3038 phba->sli4_hba.scsi_xri_max);
3039
James Smarta40fc5f2013-04-17 20:17:40 -04003040 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart164cecd2013-09-06 12:22:38 -04003041 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003042 list_splice_init(&phba->lpfc_scsi_buf_list_get, &scsi_sgl_list);
3043 list_splice(&phba->lpfc_scsi_buf_list_put, &scsi_sgl_list);
James Smart164cecd2013-09-06 12:22:38 -04003044 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003045 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003046
3047 if (phba->sli4_hba.scsi_xri_cnt > phba->sli4_hba.scsi_xri_max) {
3048 /* max scsi xri shrinked below the allocated scsi buffers */
3049 scsi_xri_cnt = phba->sli4_hba.scsi_xri_cnt -
3050 phba->sli4_hba.scsi_xri_max;
3051 /* release the extra allocated scsi buffers */
3052 for (i = 0; i < scsi_xri_cnt; i++) {
3053 list_remove_head(&scsi_sgl_list, psb,
3054 struct lpfc_scsi_buf, list);
3055 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, psb->data,
3056 psb->dma_handle);
3057 kfree(psb);
3058 }
James Smarta40fc5f2013-04-17 20:17:40 -04003059 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003060 phba->sli4_hba.scsi_xri_cnt -= scsi_xri_cnt;
James Smarta40fc5f2013-04-17 20:17:40 -04003061 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003062 }
3063
3064 /* update xris associated to remaining allocated scsi buffers */
3065 psb = NULL;
3066 psb_next = NULL;
3067 list_for_each_entry_safe(psb, psb_next, &scsi_sgl_list, list) {
3068 lxri = lpfc_sli4_next_xritag(phba);
3069 if (lxri == NO_XRI) {
3070 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3071 "2560 Failed to allocate xri for "
3072 "scsi buffer\n");
3073 rc = -ENOMEM;
3074 goto out_free_mem;
3075 }
3076 psb->cur_iocbq.sli4_lxritag = lxri;
3077 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
3078 }
James Smarta40fc5f2013-04-17 20:17:40 -04003079 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart164cecd2013-09-06 12:22:38 -04003080 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003081 list_splice_init(&scsi_sgl_list, &phba->lpfc_scsi_buf_list_get);
3082 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
James Smart164cecd2013-09-06 12:22:38 -04003083 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04003084 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04003085
dea31012005-04-17 16:05:31 -05003086 return 0;
James Smart8a9d2e82012-05-09 21:16:12 -04003087
3088out_free_mem:
3089 lpfc_free_els_sgl_list(phba);
3090 lpfc_scsi_free(phba);
3091 return rc;
dea31012005-04-17 16:05:31 -05003092}
3093
James Smarte59058c2008-08-24 21:49:00 -04003094/**
James Smart3621a712009-04-06 18:47:14 -04003095 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04003096 * @phba: pointer to lpfc hba data structure.
3097 * @instance: a unique integer ID to this FC port.
3098 * @dev: pointer to the device data structure.
3099 *
3100 * This routine creates a FC port for the upper layer protocol. The FC port
3101 * can be created on top of either a physical port or a virtual port provided
3102 * by the HBA. This routine also allocates a SCSI host data structure (shost)
3103 * and associates the FC port created before adding the shost into the SCSI
3104 * layer.
3105 *
3106 * Return codes
3107 * @vport - pointer to the virtual N_Port data structure.
3108 * NULL - port create failed.
3109 **/
James Smart2e0fef82007-06-17 19:56:36 -05003110struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04003111lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04003112{
James Smart2e0fef82007-06-17 19:56:36 -05003113 struct lpfc_vport *vport;
3114 struct Scsi_Host *shost;
3115 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04003116
James Smart3de2a652007-08-02 11:09:59 -04003117 if (dev != &phba->pcidev->dev)
3118 shost = scsi_host_alloc(&lpfc_vport_template,
3119 sizeof(struct lpfc_vport));
3120 else
3121 shost = scsi_host_alloc(&lpfc_template,
3122 sizeof(struct lpfc_vport));
James Smart2e0fef82007-06-17 19:56:36 -05003123 if (!shost)
3124 goto out;
James Smart47a86172007-04-25 09:53:22 -04003125
James Smart2e0fef82007-06-17 19:56:36 -05003126 vport = (struct lpfc_vport *) shost->hostdata;
3127 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05003128 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05003129 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05003130 vport->fc_rscn_flush = 0;
James Smart47a86172007-04-25 09:53:22 -04003131
James Smart3de2a652007-08-02 11:09:59 -04003132 lpfc_get_vport_cfgparam(vport);
James Smart2e0fef82007-06-17 19:56:36 -05003133 shost->unique_id = instance;
3134 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04003135 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05003136 shost->this_id = -1;
3137 shost->max_cmd_len = 16;
James Smartda0436e2009-05-22 14:51:39 -04003138 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart28baac72010-02-12 14:42:03 -05003139 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04003140 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smartda0436e2009-05-22 14:51:39 -04003141 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
3142 }
James Smart81301a92008-12-04 22:39:46 -05003143
James Smart47a86172007-04-25 09:53:22 -04003144 /*
James Smart2e0fef82007-06-17 19:56:36 -05003145 * Set initial can_queue value since 0 is no longer supported and
3146 * scsi_add_host will fail. This will be adjusted later based on the
3147 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04003148 */
James Smart2e0fef82007-06-17 19:56:36 -05003149 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04003150 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05003151 shost->transportt = lpfc_vport_transport_template;
3152 vport->port_type = LPFC_NPIV_PORT;
3153 } else {
3154 shost->transportt = lpfc_transport_template;
3155 vport->port_type = LPFC_PHYSICAL_PORT;
3156 }
James Smart47a86172007-04-25 09:53:22 -04003157
James Smart2e0fef82007-06-17 19:56:36 -05003158 /* Initialize all internally managed lists. */
3159 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04003160 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05003161 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04003162
James Smart2e0fef82007-06-17 19:56:36 -05003163 init_timer(&vport->fc_disctmo);
3164 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05003165 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04003166
James Smart2e0fef82007-06-17 19:56:36 -05003167 init_timer(&vport->fc_fdmitmo);
3168 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
James Smart92d7f7b2007-06-17 19:56:38 -05003169 vport->fc_fdmitmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04003170
James Smart2e0fef82007-06-17 19:56:36 -05003171 init_timer(&vport->els_tmofunc);
3172 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05003173 vport->els_tmofunc.data = (unsigned long)vport;
James Smart92494142011-02-16 12:39:44 -05003174
3175 init_timer(&vport->delayed_disc_tmo);
3176 vport->delayed_disc_tmo.function = lpfc_delayed_disc_tmo;
3177 vport->delayed_disc_tmo.data = (unsigned long)vport;
3178
James Bottomleyd139b9b2009-11-05 13:33:12 -06003179 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05003180 if (error)
3181 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04003182
James Smart549e55c2007-08-02 11:09:51 -04003183 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003184 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04003185 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05003186 return vport;
James Smart47a86172007-04-25 09:53:22 -04003187
James Smart2e0fef82007-06-17 19:56:36 -05003188out_put_shost:
3189 scsi_host_put(shost);
3190out:
3191 return NULL;
James Smart47a86172007-04-25 09:53:22 -04003192}
3193
James Smarte59058c2008-08-24 21:49:00 -04003194/**
James Smart3621a712009-04-06 18:47:14 -04003195 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04003196 * @vport: pointer to an lpfc virtual N_Port data structure.
3197 *
3198 * This routine destroys a FC port from the upper layer protocol. All the
3199 * resources associated with the port are released.
3200 **/
James Smart2e0fef82007-06-17 19:56:36 -05003201void
3202destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04003203{
James Smart92d7f7b2007-06-17 19:56:38 -05003204 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3205 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003206
James Smart858c9f62007-06-17 19:56:39 -05003207 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05003208 fc_remove_host(shost);
3209 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04003210
James Smart92d7f7b2007-06-17 19:56:38 -05003211 spin_lock_irq(&phba->hbalock);
3212 list_del_init(&vport->listentry);
3213 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04003214
James Smart92d7f7b2007-06-17 19:56:38 -05003215 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04003216 return;
James Smart47a86172007-04-25 09:53:22 -04003217}
3218
James Smarte59058c2008-08-24 21:49:00 -04003219/**
James Smart3621a712009-04-06 18:47:14 -04003220 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04003221 *
3222 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
3223 * uses the kernel idr facility to perform the task.
3224 *
3225 * Return codes:
3226 * instance - a unique integer ID allocated as the new instance.
3227 * -1 - lpfc get instance failed.
3228 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003229int
3230lpfc_get_instance(void)
3231{
Tejun Heoab516032013-02-27 17:04:44 -08003232 int ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003233
Tejun Heoab516032013-02-27 17:04:44 -08003234 ret = idr_alloc(&lpfc_hba_index, NULL, 0, 0, GFP_KERNEL);
3235 return ret < 0 ? -1 : ret;
James Smart92d7f7b2007-06-17 19:56:38 -05003236}
3237
James Smarte59058c2008-08-24 21:49:00 -04003238/**
James Smart3621a712009-04-06 18:47:14 -04003239 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04003240 * @shost: pointer to SCSI host data structure.
3241 * @time: elapsed time of the scan in jiffies.
3242 *
3243 * This routine is called by the SCSI layer with a SCSI host to determine
3244 * whether the scan host is finished.
3245 *
3246 * Note: there is no scan_start function as adapter initialization will have
3247 * asynchronously kicked off the link initialization.
3248 *
3249 * Return codes
3250 * 0 - SCSI host scan is not over yet.
3251 * 1 - SCSI host scan is over.
3252 **/
James Smart47a86172007-04-25 09:53:22 -04003253int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
3254{
James Smart2e0fef82007-06-17 19:56:36 -05003255 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3256 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05003257 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04003258
James Smart858c9f62007-06-17 19:56:39 -05003259 spin_lock_irq(shost->host_lock);
3260
James Smart51ef4c22007-08-02 11:10:31 -04003261 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05003262 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003263 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05003264 }
James Smart256ec0d2013-04-17 20:14:58 -04003265 if (time >= msecs_to_jiffies(30 * 1000)) {
James Smart2e0fef82007-06-17 19:56:36 -05003266 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003267 "0461 Scanning longer than 30 "
3268 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003269 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003270 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003271 }
James Smart256ec0d2013-04-17 20:14:58 -04003272 if (time >= msecs_to_jiffies(15 * 1000) &&
3273 phba->link_state <= LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -05003274 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003275 "0465 Link down longer than 15 "
3276 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05003277 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05003278 goto finished;
James Smart47a86172007-04-25 09:53:22 -04003279 }
3280
James Smart2e0fef82007-06-17 19:56:36 -05003281 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05003282 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003283 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05003284 goto finished;
James Smart256ec0d2013-04-17 20:14:58 -04003285 if (vport->fc_map_cnt == 0 && time < msecs_to_jiffies(2 * 1000))
James Smart858c9f62007-06-17 19:56:39 -05003286 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05003287 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05003288 goto finished;
3289
3290 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04003291
3292finished:
James Smart858c9f62007-06-17 19:56:39 -05003293 spin_unlock_irq(shost->host_lock);
3294 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05003295}
3296
James Smarte59058c2008-08-24 21:49:00 -04003297/**
James Smart3621a712009-04-06 18:47:14 -04003298 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04003299 * @shost: pointer to SCSI host data structure.
3300 *
3301 * This routine initializes a given SCSI host attributes on a FC port. The
3302 * SCSI host can be either on top of a physical port or a virtual port.
3303 **/
James Smart92d7f7b2007-06-17 19:56:38 -05003304void lpfc_host_attrib_init(struct Scsi_Host *shost)
3305{
3306 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3307 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04003308 /*
James Smart2e0fef82007-06-17 19:56:36 -05003309 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04003310 */
3311
James Smart2e0fef82007-06-17 19:56:36 -05003312 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
3313 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04003314 fc_host_supported_classes(shost) = FC_COS_CLASS3;
3315
3316 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003317 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003318 fc_host_supported_fc4s(shost)[2] = 1;
3319 fc_host_supported_fc4s(shost)[7] = 1;
3320
James Smart92d7f7b2007-06-17 19:56:38 -05003321 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
3322 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04003323
3324 fc_host_supported_speeds(shost) = 0;
James Smart88a2cfb2011-07-22 18:36:33 -04003325 if (phba->lmt & LMT_16Gb)
3326 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
James Smart47a86172007-04-25 09:53:22 -04003327 if (phba->lmt & LMT_10Gb)
3328 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04003329 if (phba->lmt & LMT_8Gb)
3330 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04003331 if (phba->lmt & LMT_4Gb)
3332 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
3333 if (phba->lmt & LMT_2Gb)
3334 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
3335 if (phba->lmt & LMT_1Gb)
3336 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
3337
3338 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05003339 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
3340 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04003341
Mike Christie0af5d702010-09-15 16:52:31 -05003342 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
3343
James Smart47a86172007-04-25 09:53:22 -04003344 /* This value is also unchanging */
3345 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05003346 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04003347 fc_host_active_fc4s(shost)[2] = 1;
3348 fc_host_active_fc4s(shost)[7] = 1;
3349
James Smart92d7f7b2007-06-17 19:56:38 -05003350 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04003351 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04003352 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04003353 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04003354}
dea31012005-04-17 16:05:31 -05003355
James Smarte59058c2008-08-24 21:49:00 -04003356/**
James Smartda0436e2009-05-22 14:51:39 -04003357 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04003358 * @phba: pointer to lpfc hba data structure.
3359 *
James Smartda0436e2009-05-22 14:51:39 -04003360 * This routine is invoked to stop an SLI3 device port, it stops the device
3361 * from generating interrupts and stops the device driver's timers for the
3362 * device.
James Smarte59058c2008-08-24 21:49:00 -04003363 **/
James Smartdb2378e2008-02-08 18:49:51 -05003364static void
James Smartda0436e2009-05-22 14:51:39 -04003365lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05003366{
James Smartda0436e2009-05-22 14:51:39 -04003367 /* Clear all interrupt enable conditions */
3368 writel(0, phba->HCregaddr);
3369 readl(phba->HCregaddr); /* flush */
3370 /* Clear all pending interrupts */
3371 writel(0xffffffff, phba->HAregaddr);
3372 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04003373
James Smartda0436e2009-05-22 14:51:39 -04003374 /* Reset some HBA SLI setup states */
3375 lpfc_stop_hba_timers(phba);
3376 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05003377}
3378
James Smarte59058c2008-08-24 21:49:00 -04003379/**
James Smartda0436e2009-05-22 14:51:39 -04003380 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05003381 * @phba: pointer to lpfc hba data structure.
3382 *
James Smartda0436e2009-05-22 14:51:39 -04003383 * This routine is invoked to stop an SLI4 device port, it stops the device
3384 * from generating interrupts and stops the device driver's timers for the
3385 * device.
3386 **/
3387static void
3388lpfc_stop_port_s4(struct lpfc_hba *phba)
3389{
3390 /* Reset some HBA SLI4 setup states */
3391 lpfc_stop_hba_timers(phba);
3392 phba->pport->work_port_events = 0;
3393 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04003394}
3395
3396/**
3397 * lpfc_stop_port - Wrapper function for stopping hba port
3398 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05003399 *
James Smartda0436e2009-05-22 14:51:39 -04003400 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
3401 * the API jump table function pointer from the lpfc_hba struct.
3402 **/
3403void
3404lpfc_stop_port(struct lpfc_hba *phba)
3405{
3406 phba->lpfc_stop_port(phba);
3407}
3408
3409/**
James Smartecfd03c2010-02-12 14:41:27 -05003410 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
3411 * @phba: Pointer to hba for which this call is being executed.
3412 *
3413 * This routine starts the timer waiting for the FCF rediscovery to complete.
3414 **/
3415void
3416lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
3417{
3418 unsigned long fcf_redisc_wait_tmo =
3419 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
3420 /* Start fcf rediscovery wait period timer */
3421 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
3422 spin_lock_irq(&phba->hbalock);
3423 /* Allow action to new fcf asynchronous event */
3424 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
3425 /* Mark the FCF rediscovery pending state */
3426 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
3427 spin_unlock_irq(&phba->hbalock);
3428}
3429
3430/**
3431 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
3432 * @ptr: Map to lpfc_hba data structure pointer.
3433 *
3434 * This routine is invoked when waiting for FCF table rediscover has been
3435 * timed out. If new FCF record(s) has (have) been discovered during the
3436 * wait period, a new FCF event shall be added to the FCOE async event
3437 * list, and then worker thread shall be waked up for processing from the
3438 * worker thread context.
3439 **/
3440void
3441lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
3442{
3443 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
3444
3445 /* Don't send FCF rediscovery event if timer cancelled */
3446 spin_lock_irq(&phba->hbalock);
3447 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
3448 spin_unlock_irq(&phba->hbalock);
3449 return;
3450 }
3451 /* Clear FCF rediscovery timer pending flag */
3452 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
3453 /* FCF rediscovery event to worker thread */
3454 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
3455 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003456 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04003457 "2776 FCF rediscover quiescent timer expired\n");
James Smartecfd03c2010-02-12 14:41:27 -05003458 /* wake up worker thread */
3459 lpfc_worker_wake_up(phba);
3460}
3461
3462/**
James Smartda0436e2009-05-22 14:51:39 -04003463 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
3464 * @phba: pointer to lpfc hba data structure.
3465 * @acqe_link: pointer to the async link completion queue entry.
3466 *
3467 * This routine is to parse the SLI4 link-attention link fault code and
3468 * translate it into the base driver's read link attention mailbox command
3469 * status.
3470 *
3471 * Return: Link-attention status in terms of base driver's coding.
3472 **/
3473static uint16_t
3474lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
3475 struct lpfc_acqe_link *acqe_link)
3476{
3477 uint16_t latt_fault;
3478
3479 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
3480 case LPFC_ASYNC_LINK_FAULT_NONE:
3481 case LPFC_ASYNC_LINK_FAULT_LOCAL:
3482 case LPFC_ASYNC_LINK_FAULT_REMOTE:
3483 latt_fault = 0;
3484 break;
3485 default:
3486 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3487 "0398 Invalid link fault code: x%x\n",
3488 bf_get(lpfc_acqe_link_fault, acqe_link));
3489 latt_fault = MBXERR_ERROR;
3490 break;
3491 }
3492 return latt_fault;
3493}
3494
3495/**
3496 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
3497 * @phba: pointer to lpfc hba data structure.
3498 * @acqe_link: pointer to the async link completion queue entry.
3499 *
3500 * This routine is to parse the SLI4 link attention type and translate it
3501 * into the base driver's link attention type coding.
3502 *
3503 * Return: Link attention type in terms of base driver's coding.
3504 **/
3505static uint8_t
3506lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
3507 struct lpfc_acqe_link *acqe_link)
3508{
3509 uint8_t att_type;
3510
3511 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
3512 case LPFC_ASYNC_LINK_STATUS_DOWN:
3513 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
James Smart76a95d72010-11-20 23:11:48 -05003514 att_type = LPFC_ATT_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04003515 break;
3516 case LPFC_ASYNC_LINK_STATUS_UP:
3517 /* Ignore physical link up events - wait for logical link up */
James Smart76a95d72010-11-20 23:11:48 -05003518 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003519 break;
3520 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
James Smart76a95d72010-11-20 23:11:48 -05003521 att_type = LPFC_ATT_LINK_UP;
James Smartda0436e2009-05-22 14:51:39 -04003522 break;
3523 default:
3524 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3525 "0399 Invalid link attention type: x%x\n",
3526 bf_get(lpfc_acqe_link_status, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003527 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003528 break;
3529 }
3530 return att_type;
3531}
3532
3533/**
3534 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
3535 * @phba: pointer to lpfc hba data structure.
3536 * @acqe_link: pointer to the async link completion queue entry.
3537 *
3538 * This routine is to parse the SLI4 link-attention link speed and translate
3539 * it into the base driver's link-attention link speed coding.
3540 *
3541 * Return: Link-attention link speed in terms of base driver's coding.
3542 **/
3543static uint8_t
3544lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
3545 struct lpfc_acqe_link *acqe_link)
3546{
3547 uint8_t link_speed;
3548
3549 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
3550 case LPFC_ASYNC_LINK_SPEED_ZERO:
James Smartda0436e2009-05-22 14:51:39 -04003551 case LPFC_ASYNC_LINK_SPEED_10MBPS:
James Smartda0436e2009-05-22 14:51:39 -04003552 case LPFC_ASYNC_LINK_SPEED_100MBPS:
James Smart76a95d72010-11-20 23:11:48 -05003553 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003554 break;
3555 case LPFC_ASYNC_LINK_SPEED_1GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003556 link_speed = LPFC_LINK_SPEED_1GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003557 break;
3558 case LPFC_ASYNC_LINK_SPEED_10GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003559 link_speed = LPFC_LINK_SPEED_10GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003560 break;
3561 default:
3562 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3563 "0483 Invalid link-attention link speed: x%x\n",
3564 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003565 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003566 break;
3567 }
3568 return link_speed;
3569}
3570
3571/**
James Smart8b68cd52012-09-29 11:32:37 -04003572 * lpfc_sli_port_speed_get - Get sli3 link speed code to link speed
3573 * @phba: pointer to lpfc hba data structure.
3574 *
3575 * This routine is to get an SLI3 FC port's link speed in Mbps.
3576 *
3577 * Return: link speed in terms of Mbps.
3578 **/
3579uint32_t
3580lpfc_sli_port_speed_get(struct lpfc_hba *phba)
3581{
3582 uint32_t link_speed;
3583
3584 if (!lpfc_is_link_up(phba))
3585 return 0;
3586
3587 switch (phba->fc_linkspeed) {
3588 case LPFC_LINK_SPEED_1GHZ:
3589 link_speed = 1000;
3590 break;
3591 case LPFC_LINK_SPEED_2GHZ:
3592 link_speed = 2000;
3593 break;
3594 case LPFC_LINK_SPEED_4GHZ:
3595 link_speed = 4000;
3596 break;
3597 case LPFC_LINK_SPEED_8GHZ:
3598 link_speed = 8000;
3599 break;
3600 case LPFC_LINK_SPEED_10GHZ:
3601 link_speed = 10000;
3602 break;
3603 case LPFC_LINK_SPEED_16GHZ:
3604 link_speed = 16000;
3605 break;
3606 default:
3607 link_speed = 0;
3608 }
3609 return link_speed;
3610}
3611
3612/**
3613 * lpfc_sli4_port_speed_parse - Parse async evt link speed code to link speed
3614 * @phba: pointer to lpfc hba data structure.
3615 * @evt_code: asynchronous event code.
3616 * @speed_code: asynchronous event link speed code.
3617 *
3618 * This routine is to parse the giving SLI4 async event link speed code into
3619 * value of Mbps for the link speed.
3620 *
3621 * Return: link speed in terms of Mbps.
3622 **/
3623static uint32_t
3624lpfc_sli4_port_speed_parse(struct lpfc_hba *phba, uint32_t evt_code,
3625 uint8_t speed_code)
3626{
3627 uint32_t port_speed;
3628
3629 switch (evt_code) {
3630 case LPFC_TRAILER_CODE_LINK:
3631 switch (speed_code) {
3632 case LPFC_EVT_CODE_LINK_NO_LINK:
3633 port_speed = 0;
3634 break;
3635 case LPFC_EVT_CODE_LINK_10_MBIT:
3636 port_speed = 10;
3637 break;
3638 case LPFC_EVT_CODE_LINK_100_MBIT:
3639 port_speed = 100;
3640 break;
3641 case LPFC_EVT_CODE_LINK_1_GBIT:
3642 port_speed = 1000;
3643 break;
3644 case LPFC_EVT_CODE_LINK_10_GBIT:
3645 port_speed = 10000;
3646 break;
3647 default:
3648 port_speed = 0;
3649 }
3650 break;
3651 case LPFC_TRAILER_CODE_FC:
3652 switch (speed_code) {
3653 case LPFC_EVT_CODE_FC_NO_LINK:
3654 port_speed = 0;
3655 break;
3656 case LPFC_EVT_CODE_FC_1_GBAUD:
3657 port_speed = 1000;
3658 break;
3659 case LPFC_EVT_CODE_FC_2_GBAUD:
3660 port_speed = 2000;
3661 break;
3662 case LPFC_EVT_CODE_FC_4_GBAUD:
3663 port_speed = 4000;
3664 break;
3665 case LPFC_EVT_CODE_FC_8_GBAUD:
3666 port_speed = 8000;
3667 break;
3668 case LPFC_EVT_CODE_FC_10_GBAUD:
3669 port_speed = 10000;
3670 break;
3671 case LPFC_EVT_CODE_FC_16_GBAUD:
3672 port_speed = 16000;
3673 break;
3674 default:
3675 port_speed = 0;
3676 }
3677 break;
3678 default:
3679 port_speed = 0;
3680 }
3681 return port_speed;
3682}
3683
3684/**
James Smart70f3c072010-12-15 17:57:33 -05003685 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
James Smartda0436e2009-05-22 14:51:39 -04003686 * @phba: pointer to lpfc hba data structure.
3687 * @acqe_link: pointer to the async link completion queue entry.
3688 *
James Smart70f3c072010-12-15 17:57:33 -05003689 * This routine is to handle the SLI4 asynchronous FCoE link event.
James Smartda0436e2009-05-22 14:51:39 -04003690 **/
3691static void
3692lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3693 struct lpfc_acqe_link *acqe_link)
3694{
3695 struct lpfc_dmabuf *mp;
3696 LPFC_MBOXQ_t *pmb;
3697 MAILBOX_t *mb;
James Smart76a95d72010-11-20 23:11:48 -05003698 struct lpfc_mbx_read_top *la;
James Smartda0436e2009-05-22 14:51:39 -04003699 uint8_t att_type;
James Smart76a95d72010-11-20 23:11:48 -05003700 int rc;
James Smartda0436e2009-05-22 14:51:39 -04003701
3702 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05003703 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
James Smartda0436e2009-05-22 14:51:39 -04003704 return;
James Smart32b97932009-07-19 10:01:21 -04003705 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003706 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3707 if (!pmb) {
3708 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3709 "0395 The mboxq allocation failed\n");
3710 return;
3711 }
3712 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3713 if (!mp) {
3714 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3715 "0396 The lpfc_dmabuf allocation failed\n");
3716 goto out_free_pmb;
3717 }
3718 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3719 if (!mp->virt) {
3720 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3721 "0397 The mbuf allocation failed\n");
3722 goto out_free_dmabuf;
3723 }
3724
3725 /* Cleanup any outstanding ELS commands */
3726 lpfc_els_flush_all_cmd(phba);
3727
3728 /* Block ELS IOCBs until we have done process link event */
3729 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3730
3731 /* Update link event statistics */
3732 phba->sli.slistat.link_event++;
3733
James Smart76a95d72010-11-20 23:11:48 -05003734 /* Create lpfc_handle_latt mailbox command from link ACQE */
3735 lpfc_read_topology(phba, pmb, mp);
3736 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smartda0436e2009-05-22 14:51:39 -04003737 pmb->vport = phba->pport;
3738
James Smartda0436e2009-05-22 14:51:39 -04003739 /* Keep the link status for extra SLI4 state machine reference */
3740 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04003741 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_LINK,
3742 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smartda0436e2009-05-22 14:51:39 -04003743 phba->sli4_hba.link_state.duplex =
3744 bf_get(lpfc_acqe_link_duplex, acqe_link);
3745 phba->sli4_hba.link_state.status =
3746 bf_get(lpfc_acqe_link_status, acqe_link);
James Smart70f3c072010-12-15 17:57:33 -05003747 phba->sli4_hba.link_state.type =
3748 bf_get(lpfc_acqe_link_type, acqe_link);
3749 phba->sli4_hba.link_state.number =
3750 bf_get(lpfc_acqe_link_number, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04003751 phba->sli4_hba.link_state.fault =
3752 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05003753 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04003754 bf_get(lpfc_acqe_logical_link_speed, acqe_link) * 10;
3755
James Smart70f3c072010-12-15 17:57:33 -05003756 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartc31098c2011-04-16 11:03:33 -04003757 "2900 Async FC/FCoE Link event - Speed:%dGBit "
3758 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
3759 "Logical speed:%dMbps Fault:%d\n",
James Smart70f3c072010-12-15 17:57:33 -05003760 phba->sli4_hba.link_state.speed,
3761 phba->sli4_hba.link_state.topology,
3762 phba->sli4_hba.link_state.status,
3763 phba->sli4_hba.link_state.type,
3764 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04003765 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05003766 phba->sli4_hba.link_state.fault);
James Smart76a95d72010-11-20 23:11:48 -05003767 /*
3768 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
3769 * topology info. Note: Optional for non FC-AL ports.
3770 */
3771 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3772 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3773 if (rc == MBX_NOT_FINISHED)
3774 goto out_free_dmabuf;
3775 return;
3776 }
3777 /*
3778 * For FCoE Mode: fill in all the topology information we need and call
3779 * the READ_TOPOLOGY completion routine to continue without actually
3780 * sending the READ_TOPOLOGY mailbox command to the port.
3781 */
3782 /* Parse and translate status field */
3783 mb = &pmb->u.mb;
3784 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
3785
3786 /* Parse and translate link attention fields */
3787 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
3788 la->eventTag = acqe_link->event_tag;
3789 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
3790 bf_set(lpfc_mbx_read_top_link_spd, la,
3791 lpfc_sli4_parse_latt_link_speed(phba, acqe_link));
3792
3793 /* Fake the the following irrelvant fields */
3794 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
3795 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
3796 bf_set(lpfc_mbx_read_top_il, la, 0);
3797 bf_set(lpfc_mbx_read_top_pb, la, 0);
3798 bf_set(lpfc_mbx_read_top_fa, la, 0);
3799 bf_set(lpfc_mbx_read_top_mm, la, 0);
James Smartda0436e2009-05-22 14:51:39 -04003800
3801 /* Invoke the lpfc_handle_latt mailbox command callback function */
James Smart76a95d72010-11-20 23:11:48 -05003802 lpfc_mbx_cmpl_read_topology(phba, pmb);
James Smartda0436e2009-05-22 14:51:39 -04003803
3804 return;
3805
3806out_free_dmabuf:
3807 kfree(mp);
3808out_free_pmb:
3809 mempool_free(pmb, phba->mbox_mem_pool);
3810}
3811
3812/**
James Smart70f3c072010-12-15 17:57:33 -05003813 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
3814 * @phba: pointer to lpfc hba data structure.
3815 * @acqe_fc: pointer to the async fc completion queue entry.
3816 *
3817 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
3818 * that the event was received and then issue a read_topology mailbox command so
3819 * that the rest of the driver will treat it the same as SLI3.
3820 **/
3821static void
3822lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
3823{
3824 struct lpfc_dmabuf *mp;
3825 LPFC_MBOXQ_t *pmb;
3826 int rc;
3827
3828 if (bf_get(lpfc_trailer_type, acqe_fc) !=
3829 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
3830 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3831 "2895 Non FC link Event detected.(%d)\n",
3832 bf_get(lpfc_trailer_type, acqe_fc));
3833 return;
3834 }
3835 /* Keep the link status for extra SLI4 state machine reference */
3836 phba->sli4_hba.link_state.speed =
James Smart8b68cd52012-09-29 11:32:37 -04003837 lpfc_sli4_port_speed_parse(phba, LPFC_TRAILER_CODE_FC,
3838 bf_get(lpfc_acqe_fc_la_speed, acqe_fc));
James Smart70f3c072010-12-15 17:57:33 -05003839 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
3840 phba->sli4_hba.link_state.topology =
3841 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
3842 phba->sli4_hba.link_state.status =
3843 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
3844 phba->sli4_hba.link_state.type =
3845 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
3846 phba->sli4_hba.link_state.number =
3847 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
3848 phba->sli4_hba.link_state.fault =
3849 bf_get(lpfc_acqe_link_fault, acqe_fc);
3850 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04003851 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc) * 10;
James Smart70f3c072010-12-15 17:57:33 -05003852 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3853 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
3854 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
3855 "%dMbps Fault:%d\n",
3856 phba->sli4_hba.link_state.speed,
3857 phba->sli4_hba.link_state.topology,
3858 phba->sli4_hba.link_state.status,
3859 phba->sli4_hba.link_state.type,
3860 phba->sli4_hba.link_state.number,
James Smart8b68cd52012-09-29 11:32:37 -04003861 phba->sli4_hba.link_state.logical_speed,
James Smart70f3c072010-12-15 17:57:33 -05003862 phba->sli4_hba.link_state.fault);
3863 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3864 if (!pmb) {
3865 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3866 "2897 The mboxq allocation failed\n");
3867 return;
3868 }
3869 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3870 if (!mp) {
3871 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3872 "2898 The lpfc_dmabuf allocation failed\n");
3873 goto out_free_pmb;
3874 }
3875 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3876 if (!mp->virt) {
3877 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3878 "2899 The mbuf allocation failed\n");
3879 goto out_free_dmabuf;
3880 }
3881
3882 /* Cleanup any outstanding ELS commands */
3883 lpfc_els_flush_all_cmd(phba);
3884
3885 /* Block ELS IOCBs until we have done process link event */
3886 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3887
3888 /* Update link event statistics */
3889 phba->sli.slistat.link_event++;
3890
3891 /* Create lpfc_handle_latt mailbox command from link ACQE */
3892 lpfc_read_topology(phba, pmb, mp);
3893 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
3894 pmb->vport = phba->pport;
3895
3896 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3897 if (rc == MBX_NOT_FINISHED)
3898 goto out_free_dmabuf;
3899 return;
3900
3901out_free_dmabuf:
3902 kfree(mp);
3903out_free_pmb:
3904 mempool_free(pmb, phba->mbox_mem_pool);
3905}
3906
3907/**
3908 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
3909 * @phba: pointer to lpfc hba data structure.
3910 * @acqe_fc: pointer to the async SLI completion queue entry.
3911 *
3912 * This routine is to handle the SLI4 asynchronous SLI events.
3913 **/
3914static void
3915lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
3916{
James Smart4b8bae02012-06-12 13:55:07 -04003917 char port_name;
James Smart8c1312e2012-10-31 14:45:09 -04003918 char message[128];
James Smart4b8bae02012-06-12 13:55:07 -04003919 uint8_t status;
3920 struct lpfc_acqe_misconfigured_event *misconfigured;
3921
3922 /* special case misconfigured event as it contains data for all ports */
3923 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3924 LPFC_SLI_INTF_IF_TYPE_2) ||
3925 (bf_get(lpfc_trailer_type, acqe_sli) !=
3926 LPFC_SLI_EVENT_TYPE_MISCONFIGURED)) {
3927 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3928 "2901 Async SLI event - Event Data1:x%08x Event Data2:"
3929 "x%08x SLI Event Type:%d\n",
3930 acqe_sli->event_data1, acqe_sli->event_data2,
3931 bf_get(lpfc_trailer_type, acqe_sli));
3932 return;
3933 }
3934
3935 port_name = phba->Port[0];
3936 if (port_name == 0x00)
3937 port_name = '?'; /* get port name is empty */
3938
3939 misconfigured = (struct lpfc_acqe_misconfigured_event *)
3940 &acqe_sli->event_data1;
3941
3942 /* fetch the status for this port */
3943 switch (phba->sli4_hba.lnk_info.lnk_no) {
3944 case LPFC_LINK_NUMBER_0:
3945 status = bf_get(lpfc_sli_misconfigured_port0,
3946 &misconfigured->theEvent);
3947 break;
3948 case LPFC_LINK_NUMBER_1:
3949 status = bf_get(lpfc_sli_misconfigured_port1,
3950 &misconfigured->theEvent);
3951 break;
3952 case LPFC_LINK_NUMBER_2:
3953 status = bf_get(lpfc_sli_misconfigured_port2,
3954 &misconfigured->theEvent);
3955 break;
3956 case LPFC_LINK_NUMBER_3:
3957 status = bf_get(lpfc_sli_misconfigured_port3,
3958 &misconfigured->theEvent);
3959 break;
3960 default:
3961 status = ~LPFC_SLI_EVENT_STATUS_VALID;
3962 break;
3963 }
3964
3965 switch (status) {
3966 case LPFC_SLI_EVENT_STATUS_VALID:
3967 return; /* no message if the sfp is okay */
3968 case LPFC_SLI_EVENT_STATUS_NOT_PRESENT:
James Smart292098b2012-09-29 11:31:41 -04003969 sprintf(message, "Optics faulted/incorrectly installed/not " \
3970 "installed - Reseat optics, if issue not "
3971 "resolved, replace.");
James Smart4b8bae02012-06-12 13:55:07 -04003972 break;
3973 case LPFC_SLI_EVENT_STATUS_WRONG_TYPE:
3974 sprintf(message,
James Smart292098b2012-09-29 11:31:41 -04003975 "Optics of two types installed - Remove one optic or " \
3976 "install matching pair of optics.");
James Smart4b8bae02012-06-12 13:55:07 -04003977 break;
3978 case LPFC_SLI_EVENT_STATUS_UNSUPPORTED:
James Smart292098b2012-09-29 11:31:41 -04003979 sprintf(message, "Incompatible optics - Replace with " \
3980 "compatible optics for card to function.");
James Smart4b8bae02012-06-12 13:55:07 -04003981 break;
3982 default:
3983 /* firmware is reporting a status we don't know about */
3984 sprintf(message, "Unknown event status x%02x", status);
3985 break;
3986 }
3987
3988 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3989 "3176 Misconfigured Physical Port - "
3990 "Port Name %c %s\n", port_name, message);
James Smart70f3c072010-12-15 17:57:33 -05003991}
3992
3993/**
James Smartfc2b9892010-02-26 14:15:29 -05003994 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
3995 * @vport: pointer to vport data structure.
3996 *
3997 * This routine is to perform Clear Virtual Link (CVL) on a vport in
3998 * response to a CVL event.
3999 *
4000 * Return the pointer to the ndlp with the vport if successful, otherwise
4001 * return NULL.
4002 **/
4003static struct lpfc_nodelist *
4004lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
4005{
4006 struct lpfc_nodelist *ndlp;
4007 struct Scsi_Host *shost;
4008 struct lpfc_hba *phba;
4009
4010 if (!vport)
4011 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05004012 phba = vport->phba;
4013 if (!phba)
4014 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04004015 ndlp = lpfc_findnode_did(vport, Fabric_DID);
4016 if (!ndlp) {
4017 /* Cannot find existing Fabric ndlp, so allocate a new one */
4018 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
4019 if (!ndlp)
4020 return 0;
4021 lpfc_nlp_init(vport, ndlp, Fabric_DID);
4022 /* Set the node type */
4023 ndlp->nlp_type |= NLP_FABRIC;
4024 /* Put ndlp onto node list */
4025 lpfc_enqueue_node(vport, ndlp);
4026 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4027 /* re-setup ndlp without removing from node list */
4028 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
4029 if (!ndlp)
4030 return 0;
4031 }
James Smart63e801c2010-11-20 23:14:19 -05004032 if ((phba->pport->port_state < LPFC_FLOGI) &&
4033 (phba->pport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05004034 return NULL;
4035 /* If virtual link is not yet instantiated ignore CVL */
James Smart63e801c2010-11-20 23:14:19 -05004036 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
4037 && (vport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05004038 return NULL;
4039 shost = lpfc_shost_from_vport(vport);
4040 if (!shost)
4041 return NULL;
4042 lpfc_linkdown_port(vport);
4043 lpfc_cleanup_pending_mbox(vport);
4044 spin_lock_irq(shost->host_lock);
4045 vport->fc_flag |= FC_VPORT_CVL_RCVD;
4046 spin_unlock_irq(shost->host_lock);
4047
4048 return ndlp;
4049}
4050
4051/**
4052 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
4053 * @vport: pointer to lpfc hba data structure.
4054 *
4055 * This routine is to perform Clear Virtual Link (CVL) on all vports in
4056 * response to a FCF dead event.
4057 **/
4058static void
4059lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
4060{
4061 struct lpfc_vport **vports;
4062 int i;
4063
4064 vports = lpfc_create_vport_work_array(phba);
4065 if (vports)
4066 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
4067 lpfc_sli4_perform_vport_cvl(vports[i]);
4068 lpfc_destroy_vport_work_array(phba, vports);
4069}
4070
4071/**
James Smart76a95d72010-11-20 23:11:48 -05004072 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
James Smartda0436e2009-05-22 14:51:39 -04004073 * @phba: pointer to lpfc hba data structure.
4074 * @acqe_link: pointer to the async fcoe completion queue entry.
4075 *
4076 * This routine is to handle the SLI4 asynchronous fcoe event.
4077 **/
4078static void
James Smart76a95d72010-11-20 23:11:48 -05004079lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
James Smart70f3c072010-12-15 17:57:33 -05004080 struct lpfc_acqe_fip *acqe_fip)
James Smartda0436e2009-05-22 14:51:39 -04004081{
James Smart70f3c072010-12-15 17:57:33 -05004082 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004083 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04004084 struct lpfc_vport *vport;
4085 struct lpfc_nodelist *ndlp;
4086 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05004087 int active_vlink_present;
4088 struct lpfc_vport **vports;
4089 int i;
James Smartda0436e2009-05-22 14:51:39 -04004090
James Smart70f3c072010-12-15 17:57:33 -05004091 phba->fc_eventTag = acqe_fip->event_tag;
4092 phba->fcoe_eventtag = acqe_fip->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004093 switch (event_type) {
James Smart70f3c072010-12-15 17:57:33 -05004094 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
4095 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
4096 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
James Smart999d8132010-03-15 11:24:56 -04004097 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4098 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004099 "2546 New FCF event, evt_tag:x%x, "
4100 "index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004101 acqe_fip->event_tag,
4102 acqe_fip->index);
James Smart999d8132010-03-15 11:24:56 -04004103 else
4104 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
4105 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004106 "2788 FCF param modified event, "
4107 "evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004108 acqe_fip->event_tag,
4109 acqe_fip->index);
James Smart38b92ef2010-08-04 16:11:39 -04004110 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004111 /*
4112 * During period of FCF discovery, read the FCF
4113 * table record indexed by the event to update
James Smarta93ff372010-10-22 11:06:08 -04004114 * FCF roundrobin failover eligible FCF bmask.
James Smart0c9ab6f2010-02-26 14:15:57 -05004115 */
4116 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4117 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004118 "2779 Read FCF (x%x) for updating "
4119 "roundrobin FCF failover bmask\n",
James Smart70f3c072010-12-15 17:57:33 -05004120 acqe_fip->index);
4121 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004122 }
James Smart38b92ef2010-08-04 16:11:39 -04004123
4124 /* If the FCF discovery is in progress, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04004125 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04004126 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart38b92ef2010-08-04 16:11:39 -04004127 spin_unlock_irq(&phba->hbalock);
4128 break;
4129 }
4130 /* If fast FCF failover rescan event is pending, do nothing */
4131 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
4132 spin_unlock_irq(&phba->hbalock);
4133 break;
4134 }
4135
James Smartc2b97122013-05-31 17:05:36 -04004136 /* If the FCF has been in discovered state, do nothing. */
4137 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
James Smart3804dc82010-07-14 15:31:37 -04004138 spin_unlock_irq(&phba->hbalock);
4139 break;
4140 }
4141 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04004142
James Smart0c9ab6f2010-02-26 14:15:57 -05004143 /* Otherwise, scan the entire FCF table and re-discover SAN */
4144 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004145 "2770 Start FCF table scan per async FCF "
4146 "event, evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004147 acqe_fip->event_tag, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05004148 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
4149 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04004150 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05004151 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4152 "2547 Issue FCF scan read FCF mailbox "
James Smarta93ff372010-10-22 11:06:08 -04004153 "command failed (x%x)\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004154 break;
4155
James Smart70f3c072010-12-15 17:57:33 -05004156 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
James Smartda0436e2009-05-22 14:51:39 -04004157 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04004158 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05004159 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
4160 acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04004161 break;
4162
James Smart70f3c072010-12-15 17:57:33 -05004163 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
James Smart80c17842012-03-01 22:35:45 -05004164 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05004165 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004166 "2549 FCF (x%x) disconnected from network, "
James Smart70f3c072010-12-15 17:57:33 -05004167 "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart38b92ef2010-08-04 16:11:39 -04004168 /*
4169 * If we are in the middle of FCF failover process, clear
4170 * the corresponding FCF bit in the roundrobin bitmap.
James Smartda0436e2009-05-22 14:51:39 -04004171 */
James Smartfc2b9892010-02-26 14:15:29 -05004172 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004173 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05004174 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004175 /* Update FLOGI FCF failover eligible FCF bmask */
James Smart70f3c072010-12-15 17:57:33 -05004176 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004177 break;
4178 }
James Smart38b92ef2010-08-04 16:11:39 -04004179 spin_unlock_irq(&phba->hbalock);
4180
4181 /* If the event is not for currently used fcf do nothing */
James Smart70f3c072010-12-15 17:57:33 -05004182 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
James Smart38b92ef2010-08-04 16:11:39 -04004183 break;
4184
4185 /*
4186 * Otherwise, request the port to rediscover the entire FCF
4187 * table for a fast recovery from case that the current FCF
4188 * is no longer valid as we are not in the middle of FCF
4189 * failover process already.
4190 */
James Smartc2b97122013-05-31 17:05:36 -04004191 spin_lock_irq(&phba->hbalock);
4192 /* Mark the fast failover process in progress */
4193 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
4194 spin_unlock_irq(&phba->hbalock);
4195
4196 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
4197 "2771 Start FCF fast failover process due to "
4198 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
4199 "\n", acqe_fip->event_tag, acqe_fip->index);
4200 rc = lpfc_sli4_redisc_fcf_table(phba);
4201 if (rc) {
4202 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4203 LOG_DISCOVERY,
4204 "2772 Issue FCF rediscover mabilbox "
4205 "command failed, fail through to FCF "
4206 "dead event\n");
4207 spin_lock_irq(&phba->hbalock);
4208 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
4209 spin_unlock_irq(&phba->hbalock);
4210 /*
4211 * Last resort will fail over by treating this
4212 * as a link down to FCF registration.
4213 */
4214 lpfc_sli4_fcf_dead_failthrough(phba);
4215 } else {
4216 /* Reset FCF roundrobin bmask for new discovery */
4217 lpfc_sli4_clear_fcf_rr_bmask(phba);
4218 /*
4219 * Handling fast FCF failover to a DEAD FCF event is
4220 * considered equalivant to receiving CVL to all vports.
4221 */
4222 lpfc_sli4_perform_all_vport_cvl(phba);
4223 }
James Smartda0436e2009-05-22 14:51:39 -04004224 break;
James Smart70f3c072010-12-15 17:57:33 -05004225 case LPFC_FIP_EVENT_TYPE_CVL:
James Smart80c17842012-03-01 22:35:45 -05004226 phba->fcoe_cvl_eventtag = acqe_fip->event_tag;
James Smart0c9ab6f2010-02-26 14:15:57 -05004227 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6669f9b2009-10-02 15:16:45 -04004228 "2718 Clear Virtual Link Received for VPI 0x%x"
James Smart70f3c072010-12-15 17:57:33 -05004229 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart6d368e52011-05-24 11:44:12 -04004230
James Smart6669f9b2009-10-02 15:16:45 -04004231 vport = lpfc_find_vport_by_vpid(phba,
James Smart5248a742011-07-22 18:37:06 -04004232 acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05004233 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004234 if (!ndlp)
4235 break;
James Smart695a8142010-01-26 23:08:03 -05004236 active_vlink_present = 0;
4237
4238 vports = lpfc_create_vport_work_array(phba);
4239 if (vports) {
4240 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
4241 i++) {
4242 if ((!(vports[i]->fc_flag &
4243 FC_VPORT_CVL_RCVD)) &&
4244 (vports[i]->port_state > LPFC_FDISC)) {
4245 active_vlink_present = 1;
4246 break;
4247 }
4248 }
4249 lpfc_destroy_vport_work_array(phba, vports);
4250 }
4251
4252 if (active_vlink_present) {
4253 /*
4254 * If there are other active VLinks present,
4255 * re-instantiate the Vlink using FDISC.
4256 */
James Smart256ec0d2013-04-17 20:14:58 -04004257 mod_timer(&ndlp->nlp_delayfunc,
4258 jiffies + msecs_to_jiffies(1000));
James Smartfc2b9892010-02-26 14:15:29 -05004259 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04004260 spin_lock_irq(shost->host_lock);
4261 ndlp->nlp_flag |= NLP_DELAY_TMO;
4262 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05004263 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
4264 vport->port_state = LPFC_FDISC;
4265 } else {
James Smartecfd03c2010-02-12 14:41:27 -05004266 /*
4267 * Otherwise, we request port to rediscover
4268 * the entire FCF table for a fast recovery
4269 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05004270 * is no longer valid if we are not already
4271 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05004272 */
James Smartfc2b9892010-02-26 14:15:29 -05004273 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004274 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05004275 spin_unlock_irq(&phba->hbalock);
4276 break;
4277 }
4278 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05004279 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004280 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004281 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
4282 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004283 "2773 Start FCF failover per CVL, "
James Smart70f3c072010-12-15 17:57:33 -05004284 "evt_tag:x%x\n", acqe_fip->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05004285 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05004286 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004287 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
4288 LOG_DISCOVERY,
4289 "2774 Issue FCF rediscover "
4290 "mabilbox command failed, "
4291 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05004292 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05004293 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05004294 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05004295 /*
4296 * Last resort will be re-try on the
4297 * the current registered FCF entry.
4298 */
4299 lpfc_retry_pport_discovery(phba);
James Smart38b92ef2010-08-04 16:11:39 -04004300 } else
4301 /*
4302 * Reset FCF roundrobin bmask for new
4303 * discovery.
4304 */
James Smart7d791df2011-07-22 18:37:52 -04004305 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart6669f9b2009-10-02 15:16:45 -04004306 }
4307 break;
James Smartda0436e2009-05-22 14:51:39 -04004308 default:
4309 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4310 "0288 Unknown FCoE event type 0x%x event tag "
James Smart70f3c072010-12-15 17:57:33 -05004311 "0x%x\n", event_type, acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04004312 break;
4313 }
4314}
4315
4316/**
4317 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
4318 * @phba: pointer to lpfc hba data structure.
4319 * @acqe_link: pointer to the async dcbx completion queue entry.
4320 *
4321 * This routine is to handle the SLI4 asynchronous dcbx event.
4322 **/
4323static void
4324lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
4325 struct lpfc_acqe_dcbx *acqe_dcbx)
4326{
James Smart4d9ab992009-10-02 15:16:39 -04004327 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04004328 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4329 "0290 The SLI4 DCBX asynchronous event is not "
4330 "handled yet\n");
4331}
4332
4333/**
James Smartb19a0612010-04-06 14:48:51 -04004334 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
4335 * @phba: pointer to lpfc hba data structure.
4336 * @acqe_link: pointer to the async grp5 completion queue entry.
4337 *
4338 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
4339 * is an asynchronous notified of a logical link speed change. The Port
4340 * reports the logical link speed in units of 10Mbps.
4341 **/
4342static void
4343lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
4344 struct lpfc_acqe_grp5 *acqe_grp5)
4345{
4346 uint16_t prev_ll_spd;
4347
4348 phba->fc_eventTag = acqe_grp5->event_tag;
4349 phba->fcoe_eventtag = acqe_grp5->event_tag;
4350 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
4351 phba->sli4_hba.link_state.logical_speed =
James Smart8b68cd52012-09-29 11:32:37 -04004352 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5)) * 10;
James Smartb19a0612010-04-06 14:48:51 -04004353 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4354 "2789 GRP5 Async Event: Updating logical link speed "
James Smart8b68cd52012-09-29 11:32:37 -04004355 "from %dMbps to %dMbps\n", prev_ll_spd,
4356 phba->sli4_hba.link_state.logical_speed);
James Smartb19a0612010-04-06 14:48:51 -04004357}
4358
4359/**
James Smartda0436e2009-05-22 14:51:39 -04004360 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
4361 * @phba: pointer to lpfc hba data structure.
4362 *
4363 * This routine is invoked by the worker thread to process all the pending
4364 * SLI4 asynchronous events.
4365 **/
4366void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
4367{
4368 struct lpfc_cq_event *cq_event;
4369
4370 /* First, declare the async event has been handled */
4371 spin_lock_irq(&phba->hbalock);
4372 phba->hba_flag &= ~ASYNC_EVENT;
4373 spin_unlock_irq(&phba->hbalock);
4374 /* Now, handle all the async events */
4375 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
4376 /* Get the first event from the head of the event queue */
4377 spin_lock_irq(&phba->hbalock);
4378 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
4379 cq_event, struct lpfc_cq_event, list);
4380 spin_unlock_irq(&phba->hbalock);
4381 /* Process the asynchronous event */
4382 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
4383 case LPFC_TRAILER_CODE_LINK:
4384 lpfc_sli4_async_link_evt(phba,
4385 &cq_event->cqe.acqe_link);
4386 break;
4387 case LPFC_TRAILER_CODE_FCOE:
James Smart70f3c072010-12-15 17:57:33 -05004388 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04004389 break;
4390 case LPFC_TRAILER_CODE_DCBX:
4391 lpfc_sli4_async_dcbx_evt(phba,
4392 &cq_event->cqe.acqe_dcbx);
4393 break;
James Smartb19a0612010-04-06 14:48:51 -04004394 case LPFC_TRAILER_CODE_GRP5:
4395 lpfc_sli4_async_grp5_evt(phba,
4396 &cq_event->cqe.acqe_grp5);
4397 break;
James Smart70f3c072010-12-15 17:57:33 -05004398 case LPFC_TRAILER_CODE_FC:
4399 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
4400 break;
4401 case LPFC_TRAILER_CODE_SLI:
4402 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
4403 break;
James Smartda0436e2009-05-22 14:51:39 -04004404 default:
4405 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4406 "1804 Invalid asynchrous event code: "
4407 "x%x\n", bf_get(lpfc_trailer_code,
4408 &cq_event->cqe.mcqe_cmpl));
4409 break;
4410 }
4411 /* Free the completion event processed to the free pool */
4412 lpfc_sli4_cq_event_release(phba, cq_event);
4413 }
4414}
4415
4416/**
James Smartecfd03c2010-02-12 14:41:27 -05004417 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
4418 * @phba: pointer to lpfc hba data structure.
4419 *
4420 * This routine is invoked by the worker thread to process FCF table
4421 * rediscovery pending completion event.
4422 **/
4423void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
4424{
4425 int rc;
4426
4427 spin_lock_irq(&phba->hbalock);
4428 /* Clear FCF rediscovery timeout event */
4429 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
4430 /* Clear driver fast failover FCF record flag */
4431 phba->fcf.failover_rec.flag = 0;
4432 /* Set state for FCF fast failover */
4433 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
4434 spin_unlock_irq(&phba->hbalock);
4435
4436 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05004437 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04004438 "2777 Start post-quiescent FCF table scan\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05004439 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05004440 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05004441 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
4442 "2747 Issue FCF scan read FCF mailbox "
4443 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05004444}
4445
4446/**
James Smartda0436e2009-05-22 14:51:39 -04004447 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
4448 * @phba: pointer to lpfc hba data structure.
4449 * @dev_grp: The HBA PCI-Device group number.
4450 *
4451 * This routine is invoked to set up the per HBA PCI-Device group function
4452 * API jump table entries.
4453 *
4454 * Return: 0 if success, otherwise -ENODEV
4455 **/
4456int
4457lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05004458{
4459 int rc;
4460
James Smartda0436e2009-05-22 14:51:39 -04004461 /* Set up lpfc PCI-device group */
4462 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05004463
James Smartda0436e2009-05-22 14:51:39 -04004464 /* The LPFC_PCI_DEV_OC uses SLI4 */
4465 if (dev_grp == LPFC_PCI_DEV_OC)
4466 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05004467
James Smartda0436e2009-05-22 14:51:39 -04004468 /* Set up device INIT API function jump table */
4469 rc = lpfc_init_api_table_setup(phba, dev_grp);
4470 if (rc)
4471 return -ENODEV;
4472 /* Set up SCSI API function jump table */
4473 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
4474 if (rc)
4475 return -ENODEV;
4476 /* Set up SLI API function jump table */
4477 rc = lpfc_sli_api_table_setup(phba, dev_grp);
4478 if (rc)
4479 return -ENODEV;
4480 /* Set up MBOX API function jump table */
4481 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
4482 if (rc)
4483 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004484
James Smartda0436e2009-05-22 14:51:39 -04004485 return 0;
James Smart5b75da22008-12-04 22:39:35 -05004486}
4487
4488/**
James Smart3621a712009-04-06 18:47:14 -04004489 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05004490 * @phba: pointer to lpfc hba data structure.
4491 * @intr_mode: active interrupt mode adopted.
4492 *
4493 * This routine it invoked to log the currently used active interrupt mode
4494 * to the device.
James Smart3772a992009-05-22 14:50:54 -04004495 **/
4496static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05004497{
4498 switch (intr_mode) {
4499 case 0:
4500 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4501 "0470 Enable INTx interrupt mode.\n");
4502 break;
4503 case 1:
4504 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4505 "0481 Enabled MSI interrupt mode.\n");
4506 break;
4507 case 2:
4508 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4509 "0480 Enabled MSI-X interrupt mode.\n");
4510 break;
4511 default:
4512 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4513 "0482 Illegal interrupt mode.\n");
4514 break;
4515 }
4516 return;
4517}
4518
James Smart5b75da22008-12-04 22:39:35 -05004519/**
James Smart3772a992009-05-22 14:50:54 -04004520 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05004521 * @phba: pointer to lpfc hba data structure.
4522 *
James Smart3772a992009-05-22 14:50:54 -04004523 * This routine is invoked to enable the PCI device that is common to all
4524 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004525 *
4526 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004527 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004528 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05004529 **/
James Smart3772a992009-05-22 14:50:54 -04004530static int
4531lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004532{
James Smart3772a992009-05-22 14:50:54 -04004533 struct pci_dev *pdev;
James Smart079b5c92011-08-21 21:48:49 -04004534 int bars = 0;
James Smart5b75da22008-12-04 22:39:35 -05004535
James Smart3772a992009-05-22 14:50:54 -04004536 /* Obtain PCI device reference */
4537 if (!phba->pcidev)
4538 goto out_error;
4539 else
4540 pdev = phba->pcidev;
4541 /* Select PCI BARs */
4542 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4543 /* Enable PCI device */
4544 if (pci_enable_device_mem(pdev))
4545 goto out_error;
4546 /* Request PCI resource for the device */
4547 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
4548 goto out_disable_device;
4549 /* Set up device as PCI master and save state for EEH */
4550 pci_set_master(pdev);
4551 pci_try_set_mwi(pdev);
4552 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05004553
James Smart05580562011-05-24 11:40:48 -04004554 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
Jon Mason453193e2013-09-11 15:38:13 -07004555 if (pci_is_pcie(pdev))
James Smart05580562011-05-24 11:40:48 -04004556 pdev->needs_freset = 1;
4557
James Smart3772a992009-05-22 14:50:54 -04004558 return 0;
James Smart5b75da22008-12-04 22:39:35 -05004559
James Smart3772a992009-05-22 14:50:54 -04004560out_disable_device:
4561 pci_disable_device(pdev);
4562out_error:
James Smart079b5c92011-08-21 21:48:49 -04004563 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4564 "1401 Failed to enable pci device, bars:x%x\n", bars);
James Smart3772a992009-05-22 14:50:54 -04004565 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004566}
4567
4568/**
James Smart3772a992009-05-22 14:50:54 -04004569 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05004570 * @phba: pointer to lpfc hba data structure.
4571 *
James Smart3772a992009-05-22 14:50:54 -04004572 * This routine is invoked to disable the PCI device that is common to all
4573 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004574 **/
4575static void
James Smart3772a992009-05-22 14:50:54 -04004576lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004577{
James Smart3772a992009-05-22 14:50:54 -04004578 struct pci_dev *pdev;
4579 int bars;
James Smart5b75da22008-12-04 22:39:35 -05004580
James Smart3772a992009-05-22 14:50:54 -04004581 /* Obtain PCI device reference */
4582 if (!phba->pcidev)
4583 return;
4584 else
4585 pdev = phba->pcidev;
4586 /* Select PCI BARs */
4587 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4588 /* Release PCI resource and disable PCI device */
4589 pci_release_selected_regions(pdev, bars);
4590 pci_disable_device(pdev);
James Smart5b75da22008-12-04 22:39:35 -05004591
4592 return;
4593}
4594
4595/**
James Smart3772a992009-05-22 14:50:54 -04004596 * lpfc_reset_hba - Reset a hba
4597 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04004598 *
James Smart3772a992009-05-22 14:50:54 -04004599 * This routine is invoked to reset a hba device. It brings the HBA
4600 * offline, performs a board restart, and then brings the board back
4601 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
4602 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04004603 **/
James Smart3772a992009-05-22 14:50:54 -04004604void
4605lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004606{
James Smart3772a992009-05-22 14:50:54 -04004607 /* If resets are disabled then set error state and return. */
4608 if (!phba->cfg_enable_hba_reset) {
4609 phba->link_state = LPFC_HBA_ERROR;
4610 return;
4611 }
James Smart618a5232012-06-12 13:54:36 -04004612 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3772a992009-05-22 14:50:54 -04004613 lpfc_offline(phba);
4614 lpfc_sli_brdrestart(phba);
4615 lpfc_online(phba);
4616 lpfc_unblock_mgmt_io(phba);
4617}
dea31012005-04-17 16:05:31 -05004618
James Smart3772a992009-05-22 14:50:54 -04004619/**
James Smart0a96e972011-07-22 18:37:28 -04004620 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
4621 * @phba: pointer to lpfc hba data structure.
4622 *
4623 * This function enables the PCI SR-IOV virtual functions to a physical
4624 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4625 * enable the number of virtual functions to the physical function. As
4626 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4627 * API call does not considered as an error condition for most of the device.
4628 **/
4629uint16_t
4630lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
4631{
4632 struct pci_dev *pdev = phba->pcidev;
4633 uint16_t nr_virtfn;
4634 int pos;
4635
James Smart0a96e972011-07-22 18:37:28 -04004636 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
4637 if (pos == 0)
4638 return 0;
4639
4640 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
4641 return nr_virtfn;
4642}
4643
4644/**
James Smart912e3ac2011-05-24 11:42:11 -04004645 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
4646 * @phba: pointer to lpfc hba data structure.
4647 * @nr_vfn: number of virtual functions to be enabled.
4648 *
4649 * This function enables the PCI SR-IOV virtual functions to a physical
4650 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4651 * enable the number of virtual functions to the physical function. As
4652 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4653 * API call does not considered as an error condition for most of the device.
4654 **/
4655int
4656lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
4657{
4658 struct pci_dev *pdev = phba->pcidev;
James Smart0a96e972011-07-22 18:37:28 -04004659 uint16_t max_nr_vfn;
James Smart912e3ac2011-05-24 11:42:11 -04004660 int rc;
4661
James Smart0a96e972011-07-22 18:37:28 -04004662 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
4663 if (nr_vfn > max_nr_vfn) {
4664 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4665 "3057 Requested vfs (%d) greater than "
4666 "supported vfs (%d)", nr_vfn, max_nr_vfn);
4667 return -EINVAL;
4668 }
4669
James Smart912e3ac2011-05-24 11:42:11 -04004670 rc = pci_enable_sriov(pdev, nr_vfn);
4671 if (rc) {
4672 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4673 "2806 Failed to enable sriov on this device "
4674 "with vfn number nr_vf:%d, rc:%d\n",
4675 nr_vfn, rc);
4676 } else
4677 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4678 "2807 Successful enable sriov on this device "
4679 "with vfn number nr_vf:%d\n", nr_vfn);
4680 return rc;
4681}
4682
4683/**
James Smart3772a992009-05-22 14:50:54 -04004684 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
4685 * @phba: pointer to lpfc hba data structure.
4686 *
4687 * This routine is invoked to set up the driver internal resources specific to
4688 * support the SLI-3 HBA device it attached to.
4689 *
4690 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004691 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004692 * other values - error
4693 **/
4694static int
4695lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
4696{
4697 struct lpfc_sli *psli;
James Smart912e3ac2011-05-24 11:42:11 -04004698 int rc;
dea31012005-04-17 16:05:31 -05004699
James Smart3772a992009-05-22 14:50:54 -04004700 /*
4701 * Initialize timers used by driver
4702 */
dea31012005-04-17 16:05:31 -05004703
James Smart3772a992009-05-22 14:50:54 -04004704 /* Heartbeat timer */
4705 init_timer(&phba->hb_tmofunc);
4706 phba->hb_tmofunc.function = lpfc_hb_timeout;
4707 phba->hb_tmofunc.data = (unsigned long)phba;
4708
4709 psli = &phba->sli;
4710 /* MBOX heartbeat timer */
4711 init_timer(&psli->mbox_tmo);
4712 psli->mbox_tmo.function = lpfc_mbox_timeout;
4713 psli->mbox_tmo.data = (unsigned long) phba;
4714 /* FCP polling mode timer */
4715 init_timer(&phba->fcp_poll_timer);
4716 phba->fcp_poll_timer.function = lpfc_poll_timeout;
4717 phba->fcp_poll_timer.data = (unsigned long) phba;
4718 /* Fabric block timer */
4719 init_timer(&phba->fabric_block_timer);
4720 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4721 phba->fabric_block_timer.data = (unsigned long) phba;
4722 /* EA polling mode timer */
4723 init_timer(&phba->eratt_poll);
4724 phba->eratt_poll.function = lpfc_poll_eratt;
4725 phba->eratt_poll.data = (unsigned long) phba;
4726
4727 /* Host attention work mask setup */
4728 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
4729 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
4730
4731 /* Get all the module params for configuring this host */
4732 lpfc_get_cfgparam(phba);
James Smart49198b32010-04-06 15:04:33 -04004733 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
4734 phba->menlo_flag |= HBA_MENLO_SUPPORT;
4735 /* check for menlo minimum sg count */
4736 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
4737 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
4738 }
4739
James Smart2a76a282012-08-03 12:35:54 -04004740 if (!phba->sli.ring)
4741 phba->sli.ring = (struct lpfc_sli_ring *)
4742 kzalloc(LPFC_SLI3_MAX_RING *
4743 sizeof(struct lpfc_sli_ring), GFP_KERNEL);
4744 if (!phba->sli.ring)
4745 return -ENOMEM;
4746
James Smart3772a992009-05-22 14:50:54 -04004747 /*
James Smart96f70772013-04-17 20:16:15 -04004748 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smart3772a992009-05-22 14:50:54 -04004749 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart3772a992009-05-22 14:50:54 -04004750 */
James Smart3772a992009-05-22 14:50:54 -04004751
James Smart96f70772013-04-17 20:16:15 -04004752 /* Initialize the host templates the configured values. */
James Smart3772a992009-05-22 14:50:54 -04004753 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4754 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4755
James Smart96f70772013-04-17 20:16:15 -04004756 /* There are going to be 2 reserved BDEs: 1 FCP cmnd + 1 FCP rsp */
4757 if (phba->cfg_enable_bg) {
4758 /*
4759 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
4760 * the FCP rsp, and a BDE for each. Sice we have no control
4761 * over how many protection data segments the SCSI Layer
4762 * will hand us (ie: there could be one for every block
4763 * in the IO), we just allocate enough BDEs to accomidate
4764 * our max amount and we need to limit lpfc_sg_seg_cnt to
4765 * minimize the risk of running out.
4766 */
4767 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4768 sizeof(struct fcp_rsp) +
4769 (LPFC_MAX_SG_SEG_CNT * sizeof(struct ulp_bde64));
4770
4771 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SEG_CNT_DIF)
4772 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT_DIF;
4773
4774 /* Total BDEs in BPL for scsi_sg_list and scsi_sg_prot_list */
4775 phba->cfg_total_seg_cnt = LPFC_MAX_SG_SEG_CNT;
4776 } else {
4777 /*
4778 * The scsi_buf for a regular I/O will hold the FCP cmnd,
4779 * the FCP rsp, a BDE for each, and a BDE for up to
4780 * cfg_sg_seg_cnt data segments.
4781 */
4782 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4783 sizeof(struct fcp_rsp) +
4784 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
4785
4786 /* Total BDEs in BPL for scsi_sg_list */
4787 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
4788 }
4789
4790 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
4791 "9088 sg_tablesize:%d dmabuf_size:%d total_bde:%d\n",
4792 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
4793 phba->cfg_total_seg_cnt);
4794
James Smart3772a992009-05-22 14:50:54 -04004795 phba->max_vpi = LPFC_MAX_VPI;
4796 /* This will be set to correct value after config_port mbox */
4797 phba->max_vports = 0;
4798
4799 /*
4800 * Initialize the SLI Layer to run with lpfc HBAs.
4801 */
4802 lpfc_sli_setup(phba);
4803 lpfc_sli_queue_setup(phba);
4804
4805 /* Allocate device driver memory */
4806 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
4807 return -ENOMEM;
4808
James Smart912e3ac2011-05-24 11:42:11 -04004809 /*
4810 * Enable sr-iov virtual functions if supported and configured
4811 * through the module parameter.
4812 */
4813 if (phba->cfg_sriov_nr_virtfn > 0) {
4814 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
4815 phba->cfg_sriov_nr_virtfn);
4816 if (rc) {
4817 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4818 "2808 Requested number of SR-IOV "
4819 "virtual functions (%d) is not "
4820 "supported\n",
4821 phba->cfg_sriov_nr_virtfn);
4822 phba->cfg_sriov_nr_virtfn = 0;
4823 }
4824 }
4825
James Smart3772a992009-05-22 14:50:54 -04004826 return 0;
4827}
4828
4829/**
4830 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
4831 * @phba: pointer to lpfc hba data structure.
4832 *
4833 * This routine is invoked to unset the driver internal resources set up
4834 * specific for supporting the SLI-3 HBA device it attached to.
4835 **/
4836static void
4837lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
4838{
4839 /* Free device driver memory allocated */
4840 lpfc_mem_free_all(phba);
4841
4842 return;
4843}
4844
4845/**
James Smartda0436e2009-05-22 14:51:39 -04004846 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
4847 * @phba: pointer to lpfc hba data structure.
4848 *
4849 * This routine is invoked to set up the driver internal resources specific to
4850 * support the SLI-4 HBA device it attached to.
4851 *
4852 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004853 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004854 * other values - error
4855 **/
4856static int
4857lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
4858{
James Smart7bb03bb2013-04-17 20:19:16 -04004859 struct lpfc_vector_map_info *cpup;
James Smartda0436e2009-05-22 14:51:39 -04004860 struct lpfc_sli *psli;
James Smart28baac72010-02-12 14:42:03 -05004861 LPFC_MBOXQ_t *mboxq;
James Smart96f70772013-04-17 20:16:15 -04004862 int rc, i, hbq_count, max_buf_size;
James Smart28baac72010-02-12 14:42:03 -05004863 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
4864 struct lpfc_mqe *mqe;
James Smart09294d42013-04-17 20:16:05 -04004865 int longs;
James Smart1ba981f2014-02-20 09:56:45 -05004866 int fof_vectors = 0;
James Smartda0436e2009-05-22 14:51:39 -04004867
James Smart716d3bc2013-09-06 12:18:28 -04004868 /* Get all the module params for configuring this host */
4869 lpfc_get_cfgparam(phba);
4870
James Smartda0436e2009-05-22 14:51:39 -04004871 /* Before proceed, wait for POST done and device ready */
4872 rc = lpfc_sli4_post_status_check(phba);
4873 if (rc)
4874 return -ENODEV;
4875
4876 /*
4877 * Initialize timers used by driver
4878 */
4879
4880 /* Heartbeat timer */
4881 init_timer(&phba->hb_tmofunc);
4882 phba->hb_tmofunc.function = lpfc_hb_timeout;
4883 phba->hb_tmofunc.data = (unsigned long)phba;
James Smart19ca7602010-11-20 23:11:55 -05004884 init_timer(&phba->rrq_tmr);
4885 phba->rrq_tmr.function = lpfc_rrq_timeout;
4886 phba->rrq_tmr.data = (unsigned long)phba;
James Smartda0436e2009-05-22 14:51:39 -04004887
4888 psli = &phba->sli;
4889 /* MBOX heartbeat timer */
4890 init_timer(&psli->mbox_tmo);
4891 psli->mbox_tmo.function = lpfc_mbox_timeout;
4892 psli->mbox_tmo.data = (unsigned long) phba;
4893 /* Fabric block timer */
4894 init_timer(&phba->fabric_block_timer);
4895 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4896 phba->fabric_block_timer.data = (unsigned long) phba;
4897 /* EA polling mode timer */
4898 init_timer(&phba->eratt_poll);
4899 phba->eratt_poll.function = lpfc_poll_eratt;
4900 phba->eratt_poll.data = (unsigned long) phba;
James Smartecfd03c2010-02-12 14:41:27 -05004901 /* FCF rediscover timer */
4902 init_timer(&phba->fcf.redisc_wait);
4903 phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
4904 phba->fcf.redisc_wait.data = (unsigned long)phba;
4905
James Smartda0436e2009-05-22 14:51:39 -04004906 /*
James Smart7ad20aa2011-05-24 11:44:28 -04004907 * Control structure for handling external multi-buffer mailbox
4908 * command pass-through.
4909 */
4910 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
4911 sizeof(struct lpfc_mbox_ext_buf_ctx));
4912 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
4913
James Smartda0436e2009-05-22 14:51:39 -04004914 phba->max_vpi = LPFC_MAX_VPI;
James Smart67d12732012-08-03 12:36:13 -04004915
James Smartda0436e2009-05-22 14:51:39 -04004916 /* This will be set to correct value after the read_config mbox */
4917 phba->max_vports = 0;
4918
4919 /* Program the default value of vlan_id and fc_map */
4920 phba->valid_vlan = 0;
4921 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4922 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4923 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4924
4925 /*
James Smart2a76a282012-08-03 12:35:54 -04004926 * For SLI4, instead of using ring 0 (LPFC_FCP_RING) for FCP commands
4927 * we will associate a new ring, for each FCP fastpath EQ/CQ/WQ tuple.
4928 */
4929 if (!phba->sli.ring)
4930 phba->sli.ring = kzalloc(
James Smart67d12732012-08-03 12:36:13 -04004931 (LPFC_SLI3_MAX_RING + phba->cfg_fcp_io_channel) *
James Smart2a76a282012-08-03 12:35:54 -04004932 sizeof(struct lpfc_sli_ring), GFP_KERNEL);
4933 if (!phba->sli.ring)
4934 return -ENOMEM;
James Smart085c6472010-11-20 23:11:37 -05004935
James Smart09294d42013-04-17 20:16:05 -04004936 /*
4937 * It doesn't matter what family our adapter is in, we are
4938 * limited to 2 Pages, 512 SGEs, for our SGL.
4939 * There are going to be 2 reserved SGEs: 1 FCP cmnd + 1 FCP rsp
4940 */
4941 max_buf_size = (2 * SLI4_PAGE_SIZE);
4942 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SGL_SEG_CNT - 2)
4943 phba->cfg_sg_seg_cnt = LPFC_MAX_SGL_SEG_CNT - 2;
James Smart09294d42013-04-17 20:16:05 -04004944
James Smartda0436e2009-05-22 14:51:39 -04004945 /*
James Smart96f70772013-04-17 20:16:15 -04004946 * Since lpfc_sg_seg_cnt is module parameter, the sg_dma_buf_size
James Smartda0436e2009-05-22 14:51:39 -04004947 * used to create the sg_dma_buf_pool must be dynamically calculated.
James Smart28baac72010-02-12 14:42:03 -05004948 */
James Smart085c6472010-11-20 23:11:37 -05004949
James Smart96f70772013-04-17 20:16:15 -04004950 if (phba->cfg_enable_bg) {
4951 /*
4952 * The scsi_buf for a T10-DIF I/O will hold the FCP cmnd,
4953 * the FCP rsp, and a SGE for each. Sice we have no control
4954 * over how many protection data segments the SCSI Layer
4955 * will hand us (ie: there could be one for every block
4956 * in the IO), we just allocate enough SGEs to accomidate
4957 * our max amount and we need to limit lpfc_sg_seg_cnt to
4958 * minimize the risk of running out.
4959 */
4960 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4961 sizeof(struct fcp_rsp) + max_buf_size;
4962
4963 /* Total SGEs for scsi_sg_list and scsi_sg_prot_list */
4964 phba->cfg_total_seg_cnt = LPFC_MAX_SGL_SEG_CNT;
4965
4966 if (phba->cfg_sg_seg_cnt > LPFC_MAX_SG_SLI4_SEG_CNT_DIF)
4967 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SLI4_SEG_CNT_DIF;
4968 } else {
4969 /*
4970 * The scsi_buf for a regular I/O will hold the FCP cmnd,
4971 * the FCP rsp, a SGE for each, and a SGE for up to
4972 * cfg_sg_seg_cnt data segments.
4973 */
4974 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4975 sizeof(struct fcp_rsp) +
4976 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge));
4977
4978 /* Total SGEs for scsi_sg_list */
4979 phba->cfg_total_seg_cnt = phba->cfg_sg_seg_cnt + 2;
4980 /*
4981 * NOTE: if (phba->cfg_sg_seg_cnt + 2) <= 256 we only need
4982 * to post 1 page for the SGL.
4983 */
James Smart085c6472010-11-20 23:11:37 -05004984 }
James Smartacd68592012-01-18 16:25:09 -05004985
James Smart96f70772013-04-17 20:16:15 -04004986 /* Initialize the host templates with the updated values. */
4987 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4988 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4989
4990 if (phba->cfg_sg_dma_buf_size <= LPFC_MIN_SG_SLI4_BUF_SZ)
4991 phba->cfg_sg_dma_buf_size = LPFC_MIN_SG_SLI4_BUF_SZ;
4992 else
4993 phba->cfg_sg_dma_buf_size =
4994 SLI4_PAGE_ALIGN(phba->cfg_sg_dma_buf_size);
4995
4996 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP,
4997 "9087 sg_tablesize:%d dmabuf_size:%d total_sge:%d\n",
4998 phba->cfg_sg_seg_cnt, phba->cfg_sg_dma_buf_size,
4999 phba->cfg_total_seg_cnt);
James Smartda0436e2009-05-22 14:51:39 -04005000
5001 /* Initialize buffer queue management fields */
5002 hbq_count = lpfc_sli_hbq_count();
5003 for (i = 0; i < hbq_count; ++i)
5004 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
5005 INIT_LIST_HEAD(&phba->rb_pend_list);
5006 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
5007 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
5008
5009 /*
5010 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
5011 */
5012 /* Initialize the Abort scsi buffer list used by driver */
5013 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
5014 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
5015 /* This abort list used by worker thread */
5016 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
5017
5018 /*
James Smart6d368e52011-05-24 11:44:12 -04005019 * Initialize driver internal slow-path work queues
James Smartda0436e2009-05-22 14:51:39 -04005020 */
5021
5022 /* Driver internel slow-path CQ Event pool */
5023 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
5024 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04005025 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04005026 /* Asynchronous event CQ Event work queue list */
5027 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
5028 /* Fast-path XRI aborted CQ Event work queue list */
5029 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
5030 /* Slow-path XRI aborted CQ Event work queue list */
5031 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
5032 /* Receive queue CQ Event work queue list */
5033 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
5034
James Smart6d368e52011-05-24 11:44:12 -04005035 /* Initialize extent block lists. */
5036 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
5037 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
5038 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
5039 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
5040
James Smartda0436e2009-05-22 14:51:39 -04005041 /* Initialize the driver internal SLI layer lists. */
5042 lpfc_sli_setup(phba);
5043 lpfc_sli_queue_setup(phba);
5044
5045 /* Allocate device driver memory */
5046 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
5047 if (rc)
5048 return -ENOMEM;
5049
James Smart2fcee4b2010-12-15 17:57:46 -05005050 /* IF Type 2 ports get initialized now. */
5051 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
5052 LPFC_SLI_INTF_IF_TYPE_2) {
5053 rc = lpfc_pci_function_reset(phba);
5054 if (unlikely(rc))
5055 return -ENODEV;
5056 }
5057
James Smartda0436e2009-05-22 14:51:39 -04005058 /* Create the bootstrap mailbox command */
5059 rc = lpfc_create_bootstrap_mbox(phba);
5060 if (unlikely(rc))
5061 goto out_free_mem;
5062
5063 /* Set up the host's endian order with the device. */
5064 rc = lpfc_setup_endian_order(phba);
5065 if (unlikely(rc))
5066 goto out_free_bsmbx;
5067
5068 /* Set up the hba's configuration parameters. */
5069 rc = lpfc_sli4_read_config(phba);
5070 if (unlikely(rc))
5071 goto out_free_bsmbx;
James Smartcff261f2013-12-17 20:29:47 -05005072 rc = lpfc_mem_alloc_active_rrq_pool_s4(phba);
5073 if (unlikely(rc))
5074 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04005075
James Smart2fcee4b2010-12-15 17:57:46 -05005076 /* IF Type 0 ports get initialized now. */
5077 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
5078 LPFC_SLI_INTF_IF_TYPE_0) {
5079 rc = lpfc_pci_function_reset(phba);
5080 if (unlikely(rc))
5081 goto out_free_bsmbx;
5082 }
James Smartda0436e2009-05-22 14:51:39 -04005083
James Smartcb5172e2010-03-15 11:25:07 -04005084 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
5085 GFP_KERNEL);
5086 if (!mboxq) {
5087 rc = -ENOMEM;
5088 goto out_free_bsmbx;
5089 }
5090
James Smartfedd3b72011-02-16 12:39:24 -05005091 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
James Smartcb5172e2010-03-15 11:25:07 -04005092 lpfc_supported_pages(mboxq);
5093 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smartfedd3b72011-02-16 12:39:24 -05005094 if (!rc) {
5095 mqe = &mboxq->u.mqe;
5096 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
5097 LPFC_MAX_SUPPORTED_PAGES);
5098 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
5099 switch (pn_page[i]) {
5100 case LPFC_SLI4_PARAMETERS:
5101 phba->sli4_hba.pc_sli4_params.supported = 1;
5102 break;
5103 default:
5104 break;
5105 }
5106 }
5107 /* Read the port's SLI4 Parameters capabilities if supported. */
5108 if (phba->sli4_hba.pc_sli4_params.supported)
5109 rc = lpfc_pc_sli4_params_get(phba, mboxq);
5110 if (rc) {
5111 mempool_free(mboxq, phba->mbox_mem_pool);
5112 rc = -EIO;
5113 goto out_free_bsmbx;
James Smartcb5172e2010-03-15 11:25:07 -04005114 }
5115 }
James Smartfedd3b72011-02-16 12:39:24 -05005116 /*
5117 * Get sli4 parameters that override parameters from Port capabilities.
James Smart6d368e52011-05-24 11:44:12 -04005118 * If this call fails, it isn't critical unless the SLI4 parameters come
5119 * back in conflict.
James Smartfedd3b72011-02-16 12:39:24 -05005120 */
James Smart6d368e52011-05-24 11:44:12 -04005121 rc = lpfc_get_sli4_parameters(phba, mboxq);
5122 if (rc) {
5123 if (phba->sli4_hba.extents_in_use &&
5124 phba->sli4_hba.rpi_hdrs_in_use) {
5125 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5126 "2999 Unsupported SLI4 Parameters "
5127 "Extents and RPI headers enabled.\n");
5128 goto out_free_bsmbx;
5129 }
5130 }
James Smartcb5172e2010-03-15 11:25:07 -04005131 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1ba981f2014-02-20 09:56:45 -05005132
5133 /* Verify OAS is supported */
5134 lpfc_sli4_oas_verify(phba);
5135 if (phba->cfg_fof)
5136 fof_vectors = 1;
5137
James Smart5350d872011-10-10 21:33:49 -04005138 /* Verify all the SLI4 queues */
5139 rc = lpfc_sli4_queue_verify(phba);
James Smartda0436e2009-05-22 14:51:39 -04005140 if (rc)
5141 goto out_free_bsmbx;
5142
5143 /* Create driver internal CQE event pool */
5144 rc = lpfc_sli4_cq_event_pool_create(phba);
5145 if (rc)
James Smart5350d872011-10-10 21:33:49 -04005146 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04005147
James Smart8a9d2e82012-05-09 21:16:12 -04005148 /* Initialize sgl lists per host */
5149 lpfc_init_sgl_list(phba);
5150
5151 /* Allocate and initialize active sgl array */
James Smartda0436e2009-05-22 14:51:39 -04005152 rc = lpfc_init_active_sgl_array(phba);
5153 if (rc) {
5154 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5155 "1430 Failed to initialize sgl list.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04005156 goto out_destroy_cq_event_pool;
James Smartda0436e2009-05-22 14:51:39 -04005157 }
James Smartda0436e2009-05-22 14:51:39 -04005158 rc = lpfc_sli4_init_rpi_hdrs(phba);
5159 if (rc) {
5160 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5161 "1432 Failed to initialize rpi headers.\n");
5162 goto out_free_active_sgl;
5163 }
5164
James Smarta93ff372010-10-22 11:06:08 -04005165 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
James Smart0c9ab6f2010-02-26 14:15:57 -05005166 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
5167 phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
5168 GFP_KERNEL);
5169 if (!phba->fcf.fcf_rr_bmask) {
5170 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5171 "2759 Failed allocate memory for FCF round "
5172 "robin failover bmask\n");
James Smart05580562011-05-24 11:40:48 -04005173 rc = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -05005174 goto out_remove_rpi_hdrs;
5175 }
5176
James Smart67d12732012-08-03 12:36:13 -04005177 phba->sli4_hba.fcp_eq_hdl =
5178 kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
James Smart1ba981f2014-02-20 09:56:45 -05005179 (fof_vectors + phba->cfg_fcp_io_channel)),
5180 GFP_KERNEL);
James Smart67d12732012-08-03 12:36:13 -04005181 if (!phba->sli4_hba.fcp_eq_hdl) {
5182 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5183 "2572 Failed allocate memory for "
5184 "fast-path per-EQ handle array\n");
5185 rc = -ENOMEM;
5186 goto out_free_fcf_rr_bmask;
James Smartda0436e2009-05-22 14:51:39 -04005187 }
5188
5189 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
James Smart1ba981f2014-02-20 09:56:45 -05005190 (fof_vectors +
5191 phba->cfg_fcp_io_channel)), GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005192 if (!phba->sli4_hba.msix_entries) {
5193 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5194 "2573 Failed allocate memory for msi-x "
5195 "interrupt vector entries\n");
James Smart05580562011-05-24 11:40:48 -04005196 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04005197 goto out_free_fcp_eq_hdl;
5198 }
5199
James Smart7bb03bb2013-04-17 20:19:16 -04005200 phba->sli4_hba.cpu_map = kzalloc((sizeof(struct lpfc_vector_map_info) *
5201 phba->sli4_hba.num_present_cpu),
5202 GFP_KERNEL);
5203 if (!phba->sli4_hba.cpu_map) {
5204 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5205 "3327 Failed allocate memory for msi-x "
5206 "interrupt vector mapping\n");
5207 rc = -ENOMEM;
5208 goto out_free_msix;
5209 }
James Smartb246de12013-05-31 17:03:07 -04005210 if (lpfc_used_cpu == NULL) {
5211 lpfc_used_cpu = kzalloc((sizeof(uint16_t) * lpfc_present_cpu),
5212 GFP_KERNEL);
5213 if (!lpfc_used_cpu) {
5214 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5215 "3335 Failed allocate memory for msi-x "
5216 "interrupt vector mapping\n");
5217 kfree(phba->sli4_hba.cpu_map);
5218 rc = -ENOMEM;
5219 goto out_free_msix;
5220 }
5221 for (i = 0; i < lpfc_present_cpu; i++)
5222 lpfc_used_cpu[i] = LPFC_VECTOR_MAP_EMPTY;
5223 }
5224
James Smart7bb03bb2013-04-17 20:19:16 -04005225 /* Initialize io channels for round robin */
5226 cpup = phba->sli4_hba.cpu_map;
5227 rc = 0;
5228 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
5229 cpup->channel_id = rc;
5230 rc++;
5231 if (rc >= phba->cfg_fcp_io_channel)
5232 rc = 0;
5233 }
5234
James Smart912e3ac2011-05-24 11:42:11 -04005235 /*
5236 * Enable sr-iov virtual functions if supported and configured
5237 * through the module parameter.
5238 */
5239 if (phba->cfg_sriov_nr_virtfn > 0) {
5240 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
5241 phba->cfg_sriov_nr_virtfn);
5242 if (rc) {
5243 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5244 "3020 Requested number of SR-IOV "
5245 "virtual functions (%d) is not "
5246 "supported\n",
5247 phba->cfg_sriov_nr_virtfn);
5248 phba->cfg_sriov_nr_virtfn = 0;
5249 }
5250 }
5251
James Smart5248a742011-07-22 18:37:06 -04005252 return 0;
James Smartda0436e2009-05-22 14:51:39 -04005253
James Smart7bb03bb2013-04-17 20:19:16 -04005254out_free_msix:
5255 kfree(phba->sli4_hba.msix_entries);
James Smartda0436e2009-05-22 14:51:39 -04005256out_free_fcp_eq_hdl:
5257 kfree(phba->sli4_hba.fcp_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05005258out_free_fcf_rr_bmask:
5259 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04005260out_remove_rpi_hdrs:
5261 lpfc_sli4_remove_rpi_hdrs(phba);
5262out_free_active_sgl:
5263 lpfc_free_active_sgl(phba);
James Smartda0436e2009-05-22 14:51:39 -04005264out_destroy_cq_event_pool:
5265 lpfc_sli4_cq_event_pool_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04005266out_free_bsmbx:
5267 lpfc_destroy_bootstrap_mbox(phba);
5268out_free_mem:
5269 lpfc_mem_free(phba);
5270 return rc;
5271}
5272
5273/**
5274 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
5275 * @phba: pointer to lpfc hba data structure.
5276 *
5277 * This routine is invoked to unset the driver internal resources set up
5278 * specific for supporting the SLI-4 HBA device it attached to.
5279 **/
5280static void
5281lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
5282{
5283 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
5284
James Smart7bb03bb2013-04-17 20:19:16 -04005285 /* Free memory allocated for msi-x interrupt vector to CPU mapping */
5286 kfree(phba->sli4_hba.cpu_map);
5287 phba->sli4_hba.num_present_cpu = 0;
5288 phba->sli4_hba.num_online_cpu = 0;
James Smart76fd07a2014-02-20 09:57:18 -05005289 phba->sli4_hba.curr_disp_cpu = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04005290
James Smartda0436e2009-05-22 14:51:39 -04005291 /* Free memory allocated for msi-x interrupt vector entries */
5292 kfree(phba->sli4_hba.msix_entries);
5293
5294 /* Free memory allocated for fast-path work queue handles */
5295 kfree(phba->sli4_hba.fcp_eq_hdl);
5296
5297 /* Free the allocated rpi headers. */
5298 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04005299 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04005300
James Smart0c9ab6f2010-02-26 14:15:57 -05005301 /* Free eligible FCF index bmask */
5302 kfree(phba->fcf.fcf_rr_bmask);
5303
James Smartda0436e2009-05-22 14:51:39 -04005304 /* Free the ELS sgl list */
5305 lpfc_free_active_sgl(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04005306 lpfc_free_els_sgl_list(phba);
James Smartda0436e2009-05-22 14:51:39 -04005307
James Smartda0436e2009-05-22 14:51:39 -04005308 /* Free the completion queue EQ event pool */
5309 lpfc_sli4_cq_event_release_all(phba);
5310 lpfc_sli4_cq_event_pool_destroy(phba);
5311
James Smart6d368e52011-05-24 11:44:12 -04005312 /* Release resource identifiers. */
5313 lpfc_sli4_dealloc_resource_identifiers(phba);
5314
James Smartda0436e2009-05-22 14:51:39 -04005315 /* Free the bsmbx region. */
5316 lpfc_destroy_bootstrap_mbox(phba);
5317
5318 /* Free the SLI Layer memory with SLI4 HBAs */
5319 lpfc_mem_free_all(phba);
5320
5321 /* Free the current connect table */
5322 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04005323 &phba->fcf_conn_rec_list, list) {
5324 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04005325 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04005326 }
James Smartda0436e2009-05-22 14:51:39 -04005327
5328 return;
5329}
5330
5331/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005332 * lpfc_init_api_table_setup - Set up init api function jump table
James Smart3772a992009-05-22 14:50:54 -04005333 * @phba: The hba struct for which this call is being executed.
5334 * @dev_grp: The HBA PCI-Device group number.
5335 *
5336 * This routine sets up the device INIT interface API function jump table
5337 * in @phba struct.
5338 *
5339 * Returns: 0 - success, -ENODEV - failure.
5340 **/
5341int
5342lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5343{
James Smart84d1b002010-02-12 14:42:33 -05005344 phba->lpfc_hba_init_link = lpfc_hba_init_link;
5345 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart7f860592011-03-11 16:05:52 -05005346 phba->lpfc_selective_reset = lpfc_selective_reset;
James Smart3772a992009-05-22 14:50:54 -04005347 switch (dev_grp) {
5348 case LPFC_PCI_DEV_LP:
5349 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
5350 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
5351 phba->lpfc_stop_port = lpfc_stop_port_s3;
5352 break;
James Smartda0436e2009-05-22 14:51:39 -04005353 case LPFC_PCI_DEV_OC:
5354 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
5355 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
5356 phba->lpfc_stop_port = lpfc_stop_port_s4;
5357 break;
James Smart3772a992009-05-22 14:50:54 -04005358 default:
5359 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5360 "1431 Invalid HBA PCI-device group: 0x%x\n",
5361 dev_grp);
5362 return -ENODEV;
5363 break;
5364 }
5365 return 0;
5366}
5367
5368/**
5369 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
5370 * @phba: pointer to lpfc hba data structure.
5371 *
5372 * This routine is invoked to set up the driver internal resources before the
5373 * device specific resource setup to support the HBA device it attached to.
5374 *
5375 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005376 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005377 * other values - error
5378 **/
5379static int
5380lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
5381{
5382 /*
5383 * Driver resources common to all SLI revisions
5384 */
James Smartea2151b2008-09-07 11:52:10 -04005385 atomic_set(&phba->fast_event_count, 0);
James Smart2e0fef82007-06-17 19:56:36 -05005386 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005387
James Smarte47c9092008-02-08 18:49:26 -05005388 /* Initialize ndlp management spinlock */
5389 spin_lock_init(&phba->ndlp_lock);
5390
James Smart2e0fef82007-06-17 19:56:36 -05005391 INIT_LIST_HEAD(&phba->port_list);
James Smart3772a992009-05-22 14:50:54 -04005392 INIT_LIST_HEAD(&phba->work_list);
James Smart84774a42008-08-24 21:50:06 -04005393 init_waitqueue_head(&phba->wait_4_mlo_m_q);
dea31012005-04-17 16:05:31 -05005394
James Smart3772a992009-05-22 14:50:54 -04005395 /* Initialize the wait queue head for the kernel thread */
5396 init_waitqueue_head(&phba->work_waitq);
James Smart858c9f62007-06-17 19:56:39 -05005397
James Smart3772a992009-05-22 14:50:54 -04005398 /* Initialize the scsi buffer list used by driver for scsi IO */
James Smarta40fc5f2013-04-17 20:17:40 -04005399 spin_lock_init(&phba->scsi_buf_list_get_lock);
5400 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_get);
5401 spin_lock_init(&phba->scsi_buf_list_put_lock);
5402 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
dea31012005-04-17 16:05:31 -05005403
James Smart3772a992009-05-22 14:50:54 -04005404 /* Initialize the fabric iocb list */
5405 INIT_LIST_HEAD(&phba->fabric_iocb_list);
dea31012005-04-17 16:05:31 -05005406
James Smart3772a992009-05-22 14:50:54 -04005407 /* Initialize list to save ELS buffers */
5408 INIT_LIST_HEAD(&phba->elsbuf);
dea31012005-04-17 16:05:31 -05005409
James Smart3772a992009-05-22 14:50:54 -04005410 /* Initialize FCF connection rec list */
5411 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
dea31012005-04-17 16:05:31 -05005412
James Smart1ba981f2014-02-20 09:56:45 -05005413 /* Initialize OAS configuration list */
5414 spin_lock_init(&phba->devicelock);
5415 INIT_LIST_HEAD(&phba->luns);
5416
James Smart3772a992009-05-22 14:50:54 -04005417 return 0;
5418}
dea31012005-04-17 16:05:31 -05005419
James Smart3772a992009-05-22 14:50:54 -04005420/**
5421 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
5422 * @phba: pointer to lpfc hba data structure.
5423 *
5424 * This routine is invoked to set up the driver internal resources after the
5425 * device specific resource setup to support the HBA device it attached to.
5426 *
5427 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005428 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005429 * other values - error
5430 **/
5431static int
5432lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
5433{
5434 int error;
5435
5436 /* Startup the kernel thread for this host adapter. */
5437 phba->worker_thread = kthread_run(lpfc_do_work, phba,
5438 "lpfc_worker_%d", phba->brd_no);
5439 if (IS_ERR(phba->worker_thread)) {
5440 error = PTR_ERR(phba->worker_thread);
5441 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005442 }
5443
James Smart3772a992009-05-22 14:50:54 -04005444 return 0;
5445}
5446
5447/**
5448 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
5449 * @phba: pointer to lpfc hba data structure.
5450 *
5451 * This routine is invoked to unset the driver internal resources set up after
5452 * the device specific resource setup for supporting the HBA device it
5453 * attached to.
5454 **/
5455static void
5456lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
5457{
5458 /* Stop kernel worker thread */
5459 kthread_stop(phba->worker_thread);
5460}
5461
5462/**
5463 * lpfc_free_iocb_list - Free iocb list.
5464 * @phba: pointer to lpfc hba data structure.
5465 *
5466 * This routine is invoked to free the driver's IOCB list and memory.
5467 **/
5468static void
5469lpfc_free_iocb_list(struct lpfc_hba *phba)
5470{
5471 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
5472
5473 spin_lock_irq(&phba->hbalock);
5474 list_for_each_entry_safe(iocbq_entry, iocbq_next,
5475 &phba->lpfc_iocb_list, list) {
5476 list_del(&iocbq_entry->list);
5477 kfree(iocbq_entry);
5478 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005479 }
James Smart3772a992009-05-22 14:50:54 -04005480 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005481
James Smart3772a992009-05-22 14:50:54 -04005482 return;
5483}
dea31012005-04-17 16:05:31 -05005484
James Smart3772a992009-05-22 14:50:54 -04005485/**
5486 * lpfc_init_iocb_list - Allocate and initialize iocb list.
5487 * @phba: pointer to lpfc hba data structure.
5488 *
5489 * This routine is invoked to allocate and initizlize the driver's IOCB
5490 * list and set up the IOCB tag array accordingly.
5491 *
5492 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005493 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005494 * other values - error
5495 **/
5496static int
5497lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
5498{
5499 struct lpfc_iocbq *iocbq_entry = NULL;
5500 uint16_t iotag;
5501 int i;
dea31012005-04-17 16:05:31 -05005502
5503 /* Initialize and populate the iocb list per host. */
5504 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04005505 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07005506 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05005507 if (iocbq_entry == NULL) {
5508 printk(KERN_ERR "%s: only allocated %d iocbs of "
5509 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07005510 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05005511 goto out_free_iocbq;
5512 }
5513
James Bottomley604a3e32005-10-29 10:28:33 -05005514 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
5515 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04005516 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05005517 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04005518 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05005519 goto out_free_iocbq;
5520 }
James Smart6d368e52011-05-24 11:44:12 -04005521 iocbq_entry->sli4_lxritag = NO_XRI;
James Smart3772a992009-05-22 14:50:54 -04005522 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05005523
5524 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005525 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
5526 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05005527 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005528 }
5529
James Smart3772a992009-05-22 14:50:54 -04005530 return 0;
5531
5532out_free_iocbq:
5533 lpfc_free_iocb_list(phba);
5534
5535 return -ENOMEM;
5536}
5537
5538/**
James Smart8a9d2e82012-05-09 21:16:12 -04005539 * lpfc_free_sgl_list - Free a given sgl list.
James Smartda0436e2009-05-22 14:51:39 -04005540 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -04005541 * @sglq_list: pointer to the head of sgl list.
James Smartda0436e2009-05-22 14:51:39 -04005542 *
James Smart8a9d2e82012-05-09 21:16:12 -04005543 * This routine is invoked to free a give sgl list and memory.
James Smartda0436e2009-05-22 14:51:39 -04005544 **/
James Smart8a9d2e82012-05-09 21:16:12 -04005545void
5546lpfc_free_sgl_list(struct lpfc_hba *phba, struct list_head *sglq_list)
James Smartda0436e2009-05-22 14:51:39 -04005547{
5548 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart8a9d2e82012-05-09 21:16:12 -04005549
5550 list_for_each_entry_safe(sglq_entry, sglq_next, sglq_list, list) {
5551 list_del(&sglq_entry->list);
5552 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
5553 kfree(sglq_entry);
5554 }
5555}
5556
5557/**
5558 * lpfc_free_els_sgl_list - Free els sgl list.
5559 * @phba: pointer to lpfc hba data structure.
5560 *
5561 * This routine is invoked to free the driver's els sgl list and memory.
5562 **/
5563static void
5564lpfc_free_els_sgl_list(struct lpfc_hba *phba)
5565{
James Smartda0436e2009-05-22 14:51:39 -04005566 LIST_HEAD(sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04005567
James Smart8a9d2e82012-05-09 21:16:12 -04005568 /* Retrieve all els sgls from driver list */
James Smartda0436e2009-05-22 14:51:39 -04005569 spin_lock_irq(&phba->hbalock);
5570 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
5571 spin_unlock_irq(&phba->hbalock);
5572
James Smart8a9d2e82012-05-09 21:16:12 -04005573 /* Now free the sgl list */
5574 lpfc_free_sgl_list(phba, &sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04005575}
5576
5577/**
5578 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
5579 * @phba: pointer to lpfc hba data structure.
5580 *
5581 * This routine is invoked to allocate the driver's active sgl memory.
5582 * This array will hold the sglq_entry's for active IOs.
5583 **/
5584static int
5585lpfc_init_active_sgl_array(struct lpfc_hba *phba)
5586{
5587 int size;
5588 size = sizeof(struct lpfc_sglq *);
5589 size *= phba->sli4_hba.max_cfg_param.max_xri;
5590
5591 phba->sli4_hba.lpfc_sglq_active_list =
5592 kzalloc(size, GFP_KERNEL);
5593 if (!phba->sli4_hba.lpfc_sglq_active_list)
5594 return -ENOMEM;
5595 return 0;
5596}
5597
5598/**
5599 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
5600 * @phba: pointer to lpfc hba data structure.
5601 *
5602 * This routine is invoked to walk through the array of active sglq entries
5603 * and free all of the resources.
5604 * This is just a place holder for now.
5605 **/
5606static void
5607lpfc_free_active_sgl(struct lpfc_hba *phba)
5608{
5609 kfree(phba->sli4_hba.lpfc_sglq_active_list);
5610}
5611
5612/**
5613 * lpfc_init_sgl_list - Allocate and initialize sgl list.
5614 * @phba: pointer to lpfc hba data structure.
5615 *
5616 * This routine is invoked to allocate and initizlize the driver's sgl
5617 * list and set up the sgl xritag tag array accordingly.
5618 *
James Smartda0436e2009-05-22 14:51:39 -04005619 **/
James Smart8a9d2e82012-05-09 21:16:12 -04005620static void
James Smartda0436e2009-05-22 14:51:39 -04005621lpfc_init_sgl_list(struct lpfc_hba *phba)
5622{
James Smartda0436e2009-05-22 14:51:39 -04005623 /* Initialize and populate the sglq list per host/VF. */
5624 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
5625 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
5626
James Smart8a9d2e82012-05-09 21:16:12 -04005627 /* els xri-sgl book keeping */
5628 phba->sli4_hba.els_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04005629
James Smart8a9d2e82012-05-09 21:16:12 -04005630 /* scsi xri-buffer book keeping */
James Smartda0436e2009-05-22 14:51:39 -04005631 phba->sli4_hba.scsi_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04005632}
5633
5634/**
5635 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
5636 * @phba: pointer to lpfc hba data structure.
5637 *
5638 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -04005639 * port for those SLI4 ports that do not support extents. This routine
James Smartda0436e2009-05-22 14:51:39 -04005640 * posts a PAGE_SIZE memory region to the port to hold up to
James Smart88a2cfb2011-07-22 18:36:33 -04005641 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
5642 * and should be called only when interrupts are disabled.
James Smartda0436e2009-05-22 14:51:39 -04005643 *
5644 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005645 * 0 - successful
James Smart88a2cfb2011-07-22 18:36:33 -04005646 * -ERROR - otherwise.
James Smartda0436e2009-05-22 14:51:39 -04005647 **/
5648int
5649lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
5650{
5651 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04005652 struct lpfc_rpi_hdr *rpi_hdr;
5653
5654 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
James Smartff78d8f2011-12-13 13:21:35 -05005655 if (!phba->sli4_hba.rpi_hdrs_in_use)
James Smart6d368e52011-05-24 11:44:12 -04005656 return rc;
James Smart6d368e52011-05-24 11:44:12 -04005657 if (phba->sli4_hba.extents_in_use)
5658 return -EIO;
James Smartda0436e2009-05-22 14:51:39 -04005659
5660 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
5661 if (!rpi_hdr) {
5662 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5663 "0391 Error during rpi post operation\n");
5664 lpfc_sli4_remove_rpis(phba);
5665 rc = -ENODEV;
5666 }
5667
5668 return rc;
5669}
5670
5671/**
5672 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
5673 * @phba: pointer to lpfc hba data structure.
5674 *
5675 * This routine is invoked to allocate a single 4KB memory region to
5676 * support rpis and stores them in the phba. This single region
5677 * provides support for up to 64 rpis. The region is used globally
5678 * by the device.
5679 *
5680 * Returns:
5681 * A valid rpi hdr on success.
5682 * A NULL pointer on any failure.
5683 **/
5684struct lpfc_rpi_hdr *
5685lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
5686{
5687 uint16_t rpi_limit, curr_rpi_range;
5688 struct lpfc_dmabuf *dmabuf;
5689 struct lpfc_rpi_hdr *rpi_hdr;
James Smart9589b0622011-04-16 11:03:17 -04005690 uint32_t rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04005691
James Smart6d368e52011-05-24 11:44:12 -04005692 /*
5693 * If the SLI4 port supports extents, posting the rpi header isn't
5694 * required. Set the expected maximum count and let the actual value
5695 * get set when extents are fully allocated.
5696 */
5697 if (!phba->sli4_hba.rpi_hdrs_in_use)
5698 return NULL;
5699 if (phba->sli4_hba.extents_in_use)
5700 return NULL;
5701
5702 /* The limit on the logical index is just the max_rpi count. */
James Smartda0436e2009-05-22 14:51:39 -04005703 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
James Smart6d368e52011-05-24 11:44:12 -04005704 phba->sli4_hba.max_cfg_param.max_rpi - 1;
James Smartda0436e2009-05-22 14:51:39 -04005705
5706 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04005707 /*
5708 * Establish the starting RPI in this header block. The starting
5709 * rpi is normalized to a zero base because the physical rpi is
5710 * port based.
5711 */
James Smart97f2ecf2012-03-01 22:35:23 -05005712 curr_rpi_range = phba->sli4_hba.next_rpi;
James Smartda0436e2009-05-22 14:51:39 -04005713 spin_unlock_irq(&phba->hbalock);
5714
5715 /*
5716 * The port has a limited number of rpis. The increment here
5717 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
5718 * and to allow the full max_rpi range per port.
5719 */
5720 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
James Smart9589b0622011-04-16 11:03:17 -04005721 rpi_count = rpi_limit - curr_rpi_range;
5722 else
5723 rpi_count = LPFC_RPI_HDR_COUNT;
James Smartda0436e2009-05-22 14:51:39 -04005724
James Smart6d368e52011-05-24 11:44:12 -04005725 if (!rpi_count)
5726 return NULL;
James Smartda0436e2009-05-22 14:51:39 -04005727 /*
5728 * First allocate the protocol header region for the port. The
5729 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
5730 */
5731 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5732 if (!dmabuf)
5733 return NULL;
5734
5735 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5736 LPFC_HDR_TEMPLATE_SIZE,
5737 &dmabuf->phys,
5738 GFP_KERNEL);
5739 if (!dmabuf->virt) {
5740 rpi_hdr = NULL;
5741 goto err_free_dmabuf;
5742 }
5743
5744 memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
5745 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
5746 rpi_hdr = NULL;
5747 goto err_free_coherent;
5748 }
5749
5750 /* Save the rpi header data for cleanup later. */
5751 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
5752 if (!rpi_hdr)
5753 goto err_free_coherent;
5754
5755 rpi_hdr->dmabuf = dmabuf;
5756 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
5757 rpi_hdr->page_count = 1;
5758 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04005759
5760 /* The rpi_hdr stores the logical index only. */
5761 rpi_hdr->start_rpi = curr_rpi_range;
James Smartda0436e2009-05-22 14:51:39 -04005762 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
5763
5764 /*
James Smart6d368e52011-05-24 11:44:12 -04005765 * The next_rpi stores the next logical module-64 rpi value used
5766 * to post physical rpis in subsequent rpi postings.
James Smartda0436e2009-05-22 14:51:39 -04005767 */
James Smart9589b0622011-04-16 11:03:17 -04005768 phba->sli4_hba.next_rpi += rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04005769 spin_unlock_irq(&phba->hbalock);
5770 return rpi_hdr;
5771
5772 err_free_coherent:
5773 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
5774 dmabuf->virt, dmabuf->phys);
5775 err_free_dmabuf:
5776 kfree(dmabuf);
5777 return NULL;
5778}
5779
5780/**
5781 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
5782 * @phba: pointer to lpfc hba data structure.
5783 *
5784 * This routine is invoked to remove all memory resources allocated
James Smart6d368e52011-05-24 11:44:12 -04005785 * to support rpis for SLI4 ports not supporting extents. This routine
5786 * presumes the caller has released all rpis consumed by fabric or port
5787 * logins and is prepared to have the header pages removed.
James Smartda0436e2009-05-22 14:51:39 -04005788 **/
5789void
5790lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
5791{
5792 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
5793
James Smart6d368e52011-05-24 11:44:12 -04005794 if (!phba->sli4_hba.rpi_hdrs_in_use)
5795 goto exit;
5796
James Smartda0436e2009-05-22 14:51:39 -04005797 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
5798 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
5799 list_del(&rpi_hdr->list);
5800 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
5801 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
5802 kfree(rpi_hdr->dmabuf);
5803 kfree(rpi_hdr);
5804 }
James Smart6d368e52011-05-24 11:44:12 -04005805 exit:
5806 /* There are no rpis available to the port now. */
5807 phba->sli4_hba.next_rpi = 0;
James Smartda0436e2009-05-22 14:51:39 -04005808}
5809
5810/**
James Smart3772a992009-05-22 14:50:54 -04005811 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
5812 * @pdev: pointer to pci device data structure.
5813 *
5814 * This routine is invoked to allocate the driver hba data structure for an
5815 * HBA device. If the allocation is successful, the phba reference to the
5816 * PCI device data structure is set.
5817 *
5818 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005819 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04005820 * NULL - error
5821 **/
5822static struct lpfc_hba *
5823lpfc_hba_alloc(struct pci_dev *pdev)
5824{
5825 struct lpfc_hba *phba;
5826
5827 /* Allocate memory for HBA structure */
5828 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
5829 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02005830 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04005831 return NULL;
5832 }
5833
5834 /* Set reference to PCI device in HBA structure */
5835 phba->pcidev = pdev;
5836
5837 /* Assign an unused board number */
5838 phba->brd_no = lpfc_get_instance();
5839 if (phba->brd_no < 0) {
5840 kfree(phba);
5841 return NULL;
5842 }
5843
James Smart4fede782010-01-26 23:08:55 -05005844 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04005845 INIT_LIST_HEAD(&phba->ct_ev_waiters);
5846
James Smart3772a992009-05-22 14:50:54 -04005847 return phba;
5848}
5849
5850/**
5851 * lpfc_hba_free - Free driver hba data structure with a device.
5852 * @phba: pointer to lpfc hba data structure.
5853 *
5854 * This routine is invoked to free the driver hba data structure with an
5855 * HBA device.
5856 **/
5857static void
5858lpfc_hba_free(struct lpfc_hba *phba)
5859{
5860 /* Release the driver assigned board number */
5861 idr_remove(&lpfc_hba_index, phba->brd_no);
5862
James Smart2a76a282012-08-03 12:35:54 -04005863 /* Free memory allocated with sli rings */
5864 kfree(phba->sli.ring);
5865 phba->sli.ring = NULL;
5866
James Smart3772a992009-05-22 14:50:54 -04005867 kfree(phba);
5868 return;
5869}
5870
5871/**
5872 * lpfc_create_shost - Create hba physical port with associated scsi host.
5873 * @phba: pointer to lpfc hba data structure.
5874 *
5875 * This routine is invoked to create HBA physical port and associate a SCSI
5876 * host with it.
5877 *
5878 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005879 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005880 * other values - error
5881 **/
5882static int
5883lpfc_create_shost(struct lpfc_hba *phba)
5884{
5885 struct lpfc_vport *vport;
5886 struct Scsi_Host *shost;
5887
5888 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05005889 phba->fc_edtov = FF_DEF_EDTOV;
5890 phba->fc_ratov = FF_DEF_RATOV;
5891 phba->fc_altov = FF_DEF_ALTOV;
5892 phba->fc_arbtov = FF_DEF_ARBTOV;
5893
James Smartd7c47992010-06-08 18:31:54 -04005894 atomic_set(&phba->sdev_cnt, 0);
James Smart3de2a652007-08-02 11:09:59 -04005895 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05005896 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04005897 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05005898
5899 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05005900 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05005901 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04005902 /* Put reference to SCSI host to driver's device private data */
5903 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05005904
James Smart3772a992009-05-22 14:50:54 -04005905 return 0;
5906}
dea31012005-04-17 16:05:31 -05005907
James Smart3772a992009-05-22 14:50:54 -04005908/**
5909 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
5910 * @phba: pointer to lpfc hba data structure.
5911 *
5912 * This routine is invoked to destroy HBA physical port and the associated
5913 * SCSI host.
5914 **/
5915static void
5916lpfc_destroy_shost(struct lpfc_hba *phba)
5917{
5918 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04005919
James Smart3772a992009-05-22 14:50:54 -04005920 /* Destroy physical port that associated with the SCSI host */
5921 destroy_port(vport);
5922
5923 return;
5924}
5925
5926/**
5927 * lpfc_setup_bg - Setup Block guard structures and debug areas.
5928 * @phba: pointer to lpfc hba data structure.
5929 * @shost: the shost to be used to detect Block guard settings.
5930 *
5931 * This routine sets up the local Block guard protocol settings for @shost.
5932 * This routine also allocates memory for debugging bg buffers.
5933 **/
5934static void
5935lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
5936{
James Smartbbeb79b2012-06-12 13:54:27 -04005937 uint32_t old_mask;
5938 uint32_t old_guard;
5939
James Smart3772a992009-05-22 14:50:54 -04005940 int pagecnt = 10;
5941 if (lpfc_prot_mask && lpfc_prot_guard) {
5942 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5943 "1478 Registering BlockGuard with the "
5944 "SCSI layer\n");
James Smartbbeb79b2012-06-12 13:54:27 -04005945
5946 old_mask = lpfc_prot_mask;
5947 old_guard = lpfc_prot_guard;
5948
5949 /* Only allow supported values */
5950 lpfc_prot_mask &= (SHOST_DIF_TYPE1_PROTECTION |
5951 SHOST_DIX_TYPE0_PROTECTION |
5952 SHOST_DIX_TYPE1_PROTECTION);
5953 lpfc_prot_guard &= (SHOST_DIX_GUARD_IP | SHOST_DIX_GUARD_CRC);
5954
5955 /* DIF Type 1 protection for profiles AST1/C1 is end to end */
5956 if (lpfc_prot_mask == SHOST_DIX_TYPE1_PROTECTION)
5957 lpfc_prot_mask |= SHOST_DIF_TYPE1_PROTECTION;
5958
5959 if (lpfc_prot_mask && lpfc_prot_guard) {
5960 if ((old_mask != lpfc_prot_mask) ||
5961 (old_guard != lpfc_prot_guard))
5962 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5963 "1475 Registering BlockGuard with the "
5964 "SCSI layer: mask %d guard %d\n",
5965 lpfc_prot_mask, lpfc_prot_guard);
5966
5967 scsi_host_set_prot(shost, lpfc_prot_mask);
5968 scsi_host_set_guard(shost, lpfc_prot_guard);
5969 } else
5970 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5971 "1479 Not Registering BlockGuard with the SCSI "
5972 "layer, Bad protection parameters: %d %d\n",
5973 old_mask, old_guard);
James Smart98c9ea52007-10-27 13:37:33 -04005974 }
James Smartbbeb79b2012-06-12 13:54:27 -04005975
James Smart81301a92008-12-04 22:39:46 -05005976 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05005977 while (pagecnt) {
5978 spin_lock_init(&_dump_buf_lock);
5979 _dump_buf_data =
5980 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
5981 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04005982 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5983 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04005984 "_dump_buf_data at 0x%p\n",
5985 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05005986 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04005987 memset(_dump_buf_data, 0,
5988 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05005989 break;
James Smart3772a992009-05-22 14:50:54 -04005990 } else
James Smart81301a92008-12-04 22:39:46 -05005991 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05005992 }
James Smart81301a92008-12-04 22:39:46 -05005993 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04005994 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5995 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04005996 "memory for hexdump\n");
5997 } else
James Smart6a9c52c2009-10-02 15:16:51 -04005998 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5999 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05006000 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05006001 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05006002 while (pagecnt) {
6003 _dump_buf_dif =
6004 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
6005 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04006006 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6007 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04006008 "_dump_buf_dif at 0x%p\n",
6009 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05006010 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04006011 memset(_dump_buf_dif, 0,
6012 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05006013 break;
James Smart3772a992009-05-22 14:50:54 -04006014 } else
James Smart81301a92008-12-04 22:39:46 -05006015 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05006016 }
James Smart81301a92008-12-04 22:39:46 -05006017 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04006018 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6019 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04006020 "memory for hexdump\n");
6021 } else
James Smart6a9c52c2009-10-02 15:16:51 -04006022 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
6023 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04006024 _dump_buf_dif);
6025}
6026
6027/**
6028 * lpfc_post_init_setup - Perform necessary device post initialization setup.
6029 * @phba: pointer to lpfc hba data structure.
6030 *
6031 * This routine is invoked to perform all the necessary post initialization
6032 * setup for the device.
6033 **/
6034static void
6035lpfc_post_init_setup(struct lpfc_hba *phba)
6036{
6037 struct Scsi_Host *shost;
6038 struct lpfc_adapter_event_header adapter_event;
6039
6040 /* Get the default values for Model Name and Description */
6041 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
6042
6043 /*
6044 * hba setup may have changed the hba_queue_depth so we need to
6045 * adjust the value of can_queue.
6046 */
6047 shost = pci_get_drvdata(phba->pcidev);
6048 shost->can_queue = phba->cfg_hba_queue_depth - 10;
6049 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
6050 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05006051
6052 lpfc_host_attrib_init(shost);
6053
James Smart2e0fef82007-06-17 19:56:36 -05006054 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
6055 spin_lock_irq(shost->host_lock);
6056 lpfc_poll_start_timer(phba);
6057 spin_unlock_irq(shost->host_lock);
6058 }
James Smart8f6d98d2006-08-01 07:34:00 -04006059
James Smart93996272008-08-24 21:50:30 -04006060 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6061 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04006062 /* Send board arrival event to upper layer */
6063 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
6064 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
6065 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04006066 sizeof(adapter_event),
6067 (char *) &adapter_event,
6068 LPFC_NL_VENDOR_ID);
6069 return;
6070}
6071
6072/**
6073 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
6074 * @phba: pointer to lpfc hba data structure.
6075 *
6076 * This routine is invoked to set up the PCI device memory space for device
6077 * with SLI-3 interface spec.
6078 *
6079 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006080 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006081 * other values - error
6082 **/
6083static int
6084lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
6085{
6086 struct pci_dev *pdev;
6087 unsigned long bar0map_len, bar2map_len;
6088 int i, hbq_count;
6089 void *ptr;
6090 int error = -ENODEV;
6091
6092 /* Obtain PCI device reference */
6093 if (!phba->pcidev)
6094 return error;
6095 else
6096 pdev = phba->pcidev;
6097
6098 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05006099 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
6100 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
6101 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
6102 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smart3772a992009-05-22 14:50:54 -04006103 return error;
Michael Reed8e685972009-09-18 12:02:05 -05006104 }
6105 }
James Smart3772a992009-05-22 14:50:54 -04006106
6107 /* Get the bus address of Bar0 and Bar2 and the number of bytes
6108 * required by each mapping.
6109 */
6110 phba->pci_bar0_map = pci_resource_start(pdev, 0);
6111 bar0map_len = pci_resource_len(pdev, 0);
6112
6113 phba->pci_bar2_map = pci_resource_start(pdev, 2);
6114 bar2map_len = pci_resource_len(pdev, 2);
6115
6116 /* Map HBA SLIM to a kernel virtual address. */
6117 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
6118 if (!phba->slim_memmap_p) {
6119 dev_printk(KERN_ERR, &pdev->dev,
6120 "ioremap failed for SLIM memory.\n");
6121 goto out;
6122 }
6123
6124 /* Map HBA Control Registers to a kernel virtual address. */
6125 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
6126 if (!phba->ctrl_regs_memmap_p) {
6127 dev_printk(KERN_ERR, &pdev->dev,
6128 "ioremap failed for HBA control registers.\n");
6129 goto out_iounmap_slim;
6130 }
6131
6132 /* Allocate memory for SLI-2 structures */
6133 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
6134 SLI2_SLIM_SIZE,
6135 &phba->slim2p.phys,
6136 GFP_KERNEL);
6137 if (!phba->slim2p.virt)
6138 goto out_iounmap;
6139
6140 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
6141 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04006142 phba->mbox_ext = (phba->slim2p.virt +
6143 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04006144 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
6145 phba->IOCBs = (phba->slim2p.virt +
6146 offsetof(struct lpfc_sli2_slim, IOCBs));
6147
6148 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
6149 lpfc_sli_hbq_size(),
6150 &phba->hbqslimp.phys,
6151 GFP_KERNEL);
6152 if (!phba->hbqslimp.virt)
6153 goto out_free_slim;
6154
6155 hbq_count = lpfc_sli_hbq_count();
6156 ptr = phba->hbqslimp.virt;
6157 for (i = 0; i < hbq_count; ++i) {
6158 phba->hbqs[i].hbq_virt = ptr;
6159 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
6160 ptr += (lpfc_hbq_defs[i]->entry_count *
6161 sizeof(struct lpfc_hbq_entry));
6162 }
6163 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
6164 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
6165
6166 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
6167
6168 INIT_LIST_HEAD(&phba->rb_pend_list);
6169
6170 phba->MBslimaddr = phba->slim_memmap_p;
6171 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
6172 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
6173 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
6174 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04006175
dea31012005-04-17 16:05:31 -05006176 return 0;
6177
dea31012005-04-17 16:05:31 -05006178out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04006179 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6180 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05006181out_iounmap:
6182 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05006183out_iounmap_slim:
dea31012005-04-17 16:05:31 -05006184 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05006185out:
6186 return error;
6187}
6188
James Smarte59058c2008-08-24 21:49:00 -04006189/**
James Smart3772a992009-05-22 14:50:54 -04006190 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
6191 * @phba: pointer to lpfc hba data structure.
6192 *
6193 * This routine is invoked to unset the PCI device memory space for device
6194 * with SLI-3 interface spec.
6195 **/
6196static void
6197lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
6198{
6199 struct pci_dev *pdev;
6200
6201 /* Obtain PCI device reference */
6202 if (!phba->pcidev)
6203 return;
6204 else
6205 pdev = phba->pcidev;
6206
6207 /* Free coherent DMA memory allocated */
6208 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
6209 phba->hbqslimp.virt, phba->hbqslimp.phys);
6210 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
6211 phba->slim2p.virt, phba->slim2p.phys);
6212
6213 /* I/O memory unmap */
6214 iounmap(phba->ctrl_regs_memmap_p);
6215 iounmap(phba->slim_memmap_p);
6216
6217 return;
6218}
6219
6220/**
James Smartda0436e2009-05-22 14:51:39 -04006221 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
6222 * @phba: pointer to lpfc hba data structure.
6223 *
6224 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
6225 * done and check status.
6226 *
6227 * Return 0 if successful, otherwise -ENODEV.
6228 **/
6229int
6230lpfc_sli4_post_status_check(struct lpfc_hba *phba)
6231{
James Smart2fcee4b2010-12-15 17:57:46 -05006232 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
6233 struct lpfc_register reg_data;
6234 int i, port_error = 0;
6235 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04006236
James Smart9940b972011-03-11 16:06:12 -05006237 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
6238 memset(&reg_data, 0, sizeof(reg_data));
James Smart2fcee4b2010-12-15 17:57:46 -05006239 if (!phba->sli4_hba.PSMPHRregaddr)
James Smartda0436e2009-05-22 14:51:39 -04006240 return -ENODEV;
6241
James Smartda0436e2009-05-22 14:51:39 -04006242 /* Wait up to 30 seconds for the SLI Port POST done and ready */
6243 for (i = 0; i < 3000; i++) {
James Smart9940b972011-03-11 16:06:12 -05006244 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
6245 &portsmphr_reg.word0) ||
6246 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
James Smart2fcee4b2010-12-15 17:57:46 -05006247 /* Port has a fatal POST error, break out */
James Smartda0436e2009-05-22 14:51:39 -04006248 port_error = -ENODEV;
6249 break;
6250 }
James Smart2fcee4b2010-12-15 17:57:46 -05006251 if (LPFC_POST_STAGE_PORT_READY ==
6252 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
James Smartda0436e2009-05-22 14:51:39 -04006253 break;
James Smartda0436e2009-05-22 14:51:39 -04006254 msleep(10);
6255 }
6256
James Smart2fcee4b2010-12-15 17:57:46 -05006257 /*
6258 * If there was a port error during POST, then don't proceed with
6259 * other register reads as the data may not be valid. Just exit.
6260 */
6261 if (port_error) {
James Smartda0436e2009-05-22 14:51:39 -04006262 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05006263 "1408 Port Failed POST - portsmphr=0x%x, "
6264 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
6265 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
6266 portsmphr_reg.word0,
6267 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
6268 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
6269 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
6270 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
6271 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
6272 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
6273 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
6274 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
6275 } else {
James Smart28baac72010-02-12 14:42:03 -05006276 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05006277 "2534 Device Info: SLIFamily=0x%x, "
6278 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
6279 "SLIHint_2=0x%x, FT=0x%x\n",
James Smart28baac72010-02-12 14:42:03 -05006280 bf_get(lpfc_sli_intf_sli_family,
6281 &phba->sli4_hba.sli_intf),
6282 bf_get(lpfc_sli_intf_slirev,
6283 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05006284 bf_get(lpfc_sli_intf_if_type,
James Smart28baac72010-02-12 14:42:03 -05006285 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05006286 bf_get(lpfc_sli_intf_sli_hint1,
6287 &phba->sli4_hba.sli_intf),
6288 bf_get(lpfc_sli_intf_sli_hint2,
6289 &phba->sli4_hba.sli_intf),
6290 bf_get(lpfc_sli_intf_func_type,
James Smart28baac72010-02-12 14:42:03 -05006291 &phba->sli4_hba.sli_intf));
James Smart2fcee4b2010-12-15 17:57:46 -05006292 /*
6293 * Check for other Port errors during the initialization
6294 * process. Fail the load if the port did not come up
6295 * correctly.
6296 */
6297 if_type = bf_get(lpfc_sli_intf_if_type,
6298 &phba->sli4_hba.sli_intf);
6299 switch (if_type) {
6300 case LPFC_SLI_INTF_IF_TYPE_0:
6301 phba->sli4_hba.ue_mask_lo =
6302 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
6303 phba->sli4_hba.ue_mask_hi =
6304 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
6305 uerrlo_reg.word0 =
6306 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
6307 uerrhi_reg.word0 =
6308 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
6309 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
6310 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
6311 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6312 "1422 Unrecoverable Error "
6313 "Detected during POST "
6314 "uerr_lo_reg=0x%x, "
6315 "uerr_hi_reg=0x%x, "
6316 "ue_mask_lo_reg=0x%x, "
6317 "ue_mask_hi_reg=0x%x\n",
6318 uerrlo_reg.word0,
6319 uerrhi_reg.word0,
6320 phba->sli4_hba.ue_mask_lo,
6321 phba->sli4_hba.ue_mask_hi);
6322 port_error = -ENODEV;
6323 }
6324 break;
6325 case LPFC_SLI_INTF_IF_TYPE_2:
6326 /* Final checks. The port status should be clean. */
James Smart9940b972011-03-11 16:06:12 -05006327 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
6328 &reg_data.word0) ||
James Smart05580562011-05-24 11:40:48 -04006329 (bf_get(lpfc_sliport_status_err, &reg_data) &&
6330 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
James Smart2fcee4b2010-12-15 17:57:46 -05006331 phba->work_status[0] =
6332 readl(phba->sli4_hba.u.if_type2.
6333 ERR1regaddr);
6334 phba->work_status[1] =
6335 readl(phba->sli4_hba.u.if_type2.
6336 ERR2regaddr);
6337 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05006338 "2888 Unrecoverable port error "
6339 "following POST: port status reg "
6340 "0x%x, port_smphr reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05006341 "error 1=0x%x, error 2=0x%x\n",
6342 reg_data.word0,
6343 portsmphr_reg.word0,
6344 phba->work_status[0],
6345 phba->work_status[1]);
6346 port_error = -ENODEV;
6347 }
6348 break;
6349 case LPFC_SLI_INTF_IF_TYPE_1:
6350 default:
6351 break;
6352 }
James Smart28baac72010-02-12 14:42:03 -05006353 }
James Smartda0436e2009-05-22 14:51:39 -04006354 return port_error;
6355}
6356
6357/**
6358 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
6359 * @phba: pointer to lpfc hba data structure.
James Smart2fcee4b2010-12-15 17:57:46 -05006360 * @if_type: The SLI4 interface type getting configured.
James Smartda0436e2009-05-22 14:51:39 -04006361 *
6362 * This routine is invoked to set up SLI4 BAR0 PCI config space register
6363 * memory map.
6364 **/
6365static void
James Smart2fcee4b2010-12-15 17:57:46 -05006366lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04006367{
James Smart2fcee4b2010-12-15 17:57:46 -05006368 switch (if_type) {
6369 case LPFC_SLI_INTF_IF_TYPE_0:
6370 phba->sli4_hba.u.if_type0.UERRLOregaddr =
6371 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
6372 phba->sli4_hba.u.if_type0.UERRHIregaddr =
6373 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
6374 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
6375 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
6376 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
6377 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
6378 phba->sli4_hba.SLIINTFregaddr =
6379 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
6380 break;
6381 case LPFC_SLI_INTF_IF_TYPE_2:
6382 phba->sli4_hba.u.if_type2.ERR1regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006383 phba->sli4_hba.conf_regs_memmap_p +
6384 LPFC_CTL_PORT_ER1_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006385 phba->sli4_hba.u.if_type2.ERR2regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006386 phba->sli4_hba.conf_regs_memmap_p +
6387 LPFC_CTL_PORT_ER2_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006388 phba->sli4_hba.u.if_type2.CTRLregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006389 phba->sli4_hba.conf_regs_memmap_p +
6390 LPFC_CTL_PORT_CTL_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006391 phba->sli4_hba.u.if_type2.STATUSregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006392 phba->sli4_hba.conf_regs_memmap_p +
6393 LPFC_CTL_PORT_STA_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006394 phba->sli4_hba.SLIINTFregaddr =
6395 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
6396 phba->sli4_hba.PSMPHRregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04006397 phba->sli4_hba.conf_regs_memmap_p +
6398 LPFC_CTL_PORT_SEM_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05006399 phba->sli4_hba.RQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05006400 phba->sli4_hba.conf_regs_memmap_p +
6401 LPFC_ULP0_RQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05006402 phba->sli4_hba.WQDBregaddr =
James Smart962bc512013-01-03 15:44:00 -05006403 phba->sli4_hba.conf_regs_memmap_p +
6404 LPFC_ULP0_WQ_DOORBELL;
James Smart2fcee4b2010-12-15 17:57:46 -05006405 phba->sli4_hba.EQCQDBregaddr =
6406 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
6407 phba->sli4_hba.MQDBregaddr =
6408 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
6409 phba->sli4_hba.BMBXregaddr =
6410 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
6411 break;
6412 case LPFC_SLI_INTF_IF_TYPE_1:
6413 default:
6414 dev_printk(KERN_ERR, &phba->pcidev->dev,
6415 "FATAL - unsupported SLI4 interface type - %d\n",
6416 if_type);
6417 break;
6418 }
James Smartda0436e2009-05-22 14:51:39 -04006419}
6420
6421/**
6422 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
6423 * @phba: pointer to lpfc hba data structure.
6424 *
6425 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
6426 * memory map.
6427 **/
6428static void
6429lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
6430{
James Smart2fcee4b2010-12-15 17:57:46 -05006431 phba->sli4_hba.PSMPHRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
6432 LPFC_SLIPORT_IF0_SMPHR;
James Smartda0436e2009-05-22 14:51:39 -04006433 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006434 LPFC_HST_ISR0;
James Smartda0436e2009-05-22 14:51:39 -04006435 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006436 LPFC_HST_IMR0;
James Smartda0436e2009-05-22 14:51:39 -04006437 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05006438 LPFC_HST_ISCR0;
James Smartda0436e2009-05-22 14:51:39 -04006439}
6440
6441/**
6442 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
6443 * @phba: pointer to lpfc hba data structure.
6444 * @vf: virtual function number
6445 *
6446 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
6447 * based on the given viftual function number, @vf.
6448 *
6449 * Return 0 if successful, otherwise -ENODEV.
6450 **/
6451static int
6452lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
6453{
6454 if (vf > LPFC_VIR_FUNC_MAX)
6455 return -ENODEV;
6456
6457 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05006458 vf * LPFC_VFR_PAGE_SIZE +
6459 LPFC_ULP0_RQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04006460 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
James Smart962bc512013-01-03 15:44:00 -05006461 vf * LPFC_VFR_PAGE_SIZE +
6462 LPFC_ULP0_WQ_DOORBELL);
James Smartda0436e2009-05-22 14:51:39 -04006463 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6464 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
6465 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6466 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
6467 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
6468 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
6469 return 0;
6470}
6471
6472/**
6473 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
6474 * @phba: pointer to lpfc hba data structure.
6475 *
6476 * This routine is invoked to create the bootstrap mailbox
6477 * region consistent with the SLI-4 interface spec. This
6478 * routine allocates all memory necessary to communicate
6479 * mailbox commands to the port and sets up all alignment
6480 * needs. No locks are expected to be held when calling
6481 * this routine.
6482 *
6483 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006484 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04006485 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04006486 **/
6487static int
6488lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
6489{
6490 uint32_t bmbx_size;
6491 struct lpfc_dmabuf *dmabuf;
6492 struct dma_address *dma_address;
6493 uint32_t pa_addr;
6494 uint64_t phys_addr;
6495
6496 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
6497 if (!dmabuf)
6498 return -ENOMEM;
6499
6500 /*
6501 * The bootstrap mailbox region is comprised of 2 parts
6502 * plus an alignment restriction of 16 bytes.
6503 */
6504 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
6505 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
6506 bmbx_size,
6507 &dmabuf->phys,
6508 GFP_KERNEL);
6509 if (!dmabuf->virt) {
6510 kfree(dmabuf);
6511 return -ENOMEM;
6512 }
6513 memset(dmabuf->virt, 0, bmbx_size);
6514
6515 /*
6516 * Initialize the bootstrap mailbox pointers now so that the register
6517 * operations are simple later. The mailbox dma address is required
6518 * to be 16-byte aligned. Also align the virtual memory as each
6519 * maibox is copied into the bmbx mailbox region before issuing the
6520 * command to the port.
6521 */
6522 phba->sli4_hba.bmbx.dmabuf = dmabuf;
6523 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
6524
6525 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
6526 LPFC_ALIGN_16_BYTE);
6527 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
6528 LPFC_ALIGN_16_BYTE);
6529
6530 /*
6531 * Set the high and low physical addresses now. The SLI4 alignment
6532 * requirement is 16 bytes and the mailbox is posted to the port
6533 * as two 30-bit addresses. The other data is a bit marking whether
6534 * the 30-bit address is the high or low address.
6535 * Upcast bmbx aphys to 64bits so shift instruction compiles
6536 * clean on 32 bit machines.
6537 */
6538 dma_address = &phba->sli4_hba.bmbx.dma_address;
6539 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
6540 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
6541 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
6542 LPFC_BMBX_BIT1_ADDR_HI);
6543
6544 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
6545 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
6546 LPFC_BMBX_BIT1_ADDR_LO);
6547 return 0;
6548}
6549
6550/**
6551 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
6552 * @phba: pointer to lpfc hba data structure.
6553 *
6554 * This routine is invoked to teardown the bootstrap mailbox
6555 * region and release all host resources. This routine requires
6556 * the caller to ensure all mailbox commands recovered, no
6557 * additional mailbox comands are sent, and interrupts are disabled
6558 * before calling this routine.
6559 *
6560 **/
6561static void
6562lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
6563{
6564 dma_free_coherent(&phba->pcidev->dev,
6565 phba->sli4_hba.bmbx.bmbx_size,
6566 phba->sli4_hba.bmbx.dmabuf->virt,
6567 phba->sli4_hba.bmbx.dmabuf->phys);
6568
6569 kfree(phba->sli4_hba.bmbx.dmabuf);
6570 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
6571}
6572
6573/**
6574 * lpfc_sli4_read_config - Get the config parameters.
6575 * @phba: pointer to lpfc hba data structure.
6576 *
6577 * This routine is invoked to read the configuration parameters from the HBA.
6578 * The configuration parameters are used to set the base and maximum values
6579 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
6580 * allocation for the port.
6581 *
6582 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006583 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006584 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006585 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006586 **/
James Smartff78d8f2011-12-13 13:21:35 -05006587int
James Smartda0436e2009-05-22 14:51:39 -04006588lpfc_sli4_read_config(struct lpfc_hba *phba)
6589{
6590 LPFC_MBOXQ_t *pmb;
6591 struct lpfc_mbx_read_config *rd_config;
James Smart912e3ac2011-05-24 11:42:11 -04006592 union lpfc_sli4_cfg_shdr *shdr;
6593 uint32_t shdr_status, shdr_add_status;
6594 struct lpfc_mbx_get_func_cfg *get_func_cfg;
6595 struct lpfc_rsrc_desc_fcfcoe *desc;
James Smart8aa134a2012-08-14 14:25:29 -04006596 char *pdesc_0;
James Smart912e3ac2011-05-24 11:42:11 -04006597 uint32_t desc_count;
James Smart8aa134a2012-08-14 14:25:29 -04006598 int length, i, rc = 0, rc2;
James Smartda0436e2009-05-22 14:51:39 -04006599
6600 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6601 if (!pmb) {
6602 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6603 "2011 Unable to allocate memory for issuing "
6604 "SLI_CONFIG_SPECIAL mailbox command\n");
6605 return -ENOMEM;
6606 }
6607
6608 lpfc_read_config(phba, pmb);
6609
6610 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6611 if (rc != MBX_SUCCESS) {
6612 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6613 "2012 Mailbox failed , mbxCmd x%x "
6614 "READ_CONFIG, mbxStatus x%x\n",
6615 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6616 bf_get(lpfc_mqe_status, &pmb->u.mqe));
6617 rc = -EIO;
6618 } else {
6619 rd_config = &pmb->u.mqe.un.rd_config;
James Smartff78d8f2011-12-13 13:21:35 -05006620 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
6621 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
6622 phba->sli4_hba.lnk_info.lnk_tp =
6623 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
6624 phba->sli4_hba.lnk_info.lnk_no =
6625 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
6626 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6627 "3081 lnk_type:%d, lnk_numb:%d\n",
6628 phba->sli4_hba.lnk_info.lnk_tp,
6629 phba->sli4_hba.lnk_info.lnk_no);
6630 } else
6631 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6632 "3082 Mailbox (x%x) returned ldv:x0\n",
6633 bf_get(lpfc_mqe_command, &pmb->u.mqe));
James Smart6d368e52011-05-24 11:44:12 -04006634 phba->sli4_hba.extents_in_use =
6635 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04006636 phba->sli4_hba.max_cfg_param.max_xri =
6637 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
6638 phba->sli4_hba.max_cfg_param.xri_base =
6639 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
6640 phba->sli4_hba.max_cfg_param.max_vpi =
6641 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
6642 phba->sli4_hba.max_cfg_param.vpi_base =
6643 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
6644 phba->sli4_hba.max_cfg_param.max_rpi =
6645 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
6646 phba->sli4_hba.max_cfg_param.rpi_base =
6647 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
6648 phba->sli4_hba.max_cfg_param.max_vfi =
6649 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
6650 phba->sli4_hba.max_cfg_param.vfi_base =
6651 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
6652 phba->sli4_hba.max_cfg_param.max_fcfi =
6653 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04006654 phba->sli4_hba.max_cfg_param.max_eq =
6655 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
6656 phba->sli4_hba.max_cfg_param.max_rq =
6657 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
6658 phba->sli4_hba.max_cfg_param.max_wq =
6659 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
6660 phba->sli4_hba.max_cfg_param.max_cq =
6661 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
6662 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
6663 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
6664 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
6665 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
James Smart5ffc2662009-11-18 15:39:44 -05006666 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
6667 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04006668 phba->max_vports = phba->max_vpi;
6669 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart6d368e52011-05-24 11:44:12 -04006670 "2003 cfg params Extents? %d "
6671 "XRI(B:%d M:%d), "
James Smartda0436e2009-05-22 14:51:39 -04006672 "VPI(B:%d M:%d) "
6673 "VFI(B:%d M:%d) "
6674 "RPI(B:%d M:%d) "
James Smart6d368e52011-05-24 11:44:12 -04006675 "FCFI(Count:%d)\n",
6676 phba->sli4_hba.extents_in_use,
James Smartda0436e2009-05-22 14:51:39 -04006677 phba->sli4_hba.max_cfg_param.xri_base,
6678 phba->sli4_hba.max_cfg_param.max_xri,
6679 phba->sli4_hba.max_cfg_param.vpi_base,
6680 phba->sli4_hba.max_cfg_param.max_vpi,
6681 phba->sli4_hba.max_cfg_param.vfi_base,
6682 phba->sli4_hba.max_cfg_param.max_vfi,
6683 phba->sli4_hba.max_cfg_param.rpi_base,
6684 phba->sli4_hba.max_cfg_param.max_rpi,
James Smartda0436e2009-05-22 14:51:39 -04006685 phba->sli4_hba.max_cfg_param.max_fcfi);
6686 }
James Smart912e3ac2011-05-24 11:42:11 -04006687
6688 if (rc)
6689 goto read_cfg_out;
James Smartda0436e2009-05-22 14:51:39 -04006690
6691 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smart572709e2013-07-15 18:32:43 -04006692 length = phba->sli4_hba.max_cfg_param.max_xri -
6693 lpfc_sli4_get_els_iocb_cnt(phba);
6694 if (phba->cfg_hba_queue_depth > length) {
6695 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6696 "3361 HBA queue depth changed from %d to %d\n",
6697 phba->cfg_hba_queue_depth, length);
6698 phba->cfg_hba_queue_depth = length;
6699 }
James Smart912e3ac2011-05-24 11:42:11 -04006700
6701 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
6702 LPFC_SLI_INTF_IF_TYPE_2)
6703 goto read_cfg_out;
6704
6705 /* get the pf# and vf# for SLI4 if_type 2 port */
6706 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
6707 sizeof(struct lpfc_sli4_cfg_mhdr));
6708 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
6709 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
6710 length, LPFC_SLI4_MBX_EMBED);
6711
James Smart8aa134a2012-08-14 14:25:29 -04006712 rc2 = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart912e3ac2011-05-24 11:42:11 -04006713 shdr = (union lpfc_sli4_cfg_shdr *)
6714 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
6715 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6716 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart8aa134a2012-08-14 14:25:29 -04006717 if (rc2 || shdr_status || shdr_add_status) {
James Smart912e3ac2011-05-24 11:42:11 -04006718 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6719 "3026 Mailbox failed , mbxCmd x%x "
6720 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
6721 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6722 bf_get(lpfc_mqe_status, &pmb->u.mqe));
James Smart912e3ac2011-05-24 11:42:11 -04006723 goto read_cfg_out;
6724 }
6725
6726 /* search for fc_fcoe resrouce descriptor */
6727 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
6728 desc_count = get_func_cfg->func_cfg.rsrc_desc_count;
6729
James Smart8aa134a2012-08-14 14:25:29 -04006730 pdesc_0 = (char *)&get_func_cfg->func_cfg.desc[0];
6731 desc = (struct lpfc_rsrc_desc_fcfcoe *)pdesc_0;
6732 length = bf_get(lpfc_rsrc_desc_fcfcoe_length, desc);
6733 if (length == LPFC_RSRC_DESC_TYPE_FCFCOE_V0_RSVD)
6734 length = LPFC_RSRC_DESC_TYPE_FCFCOE_V0_LENGTH;
6735 else if (length != LPFC_RSRC_DESC_TYPE_FCFCOE_V1_LENGTH)
6736 goto read_cfg_out;
6737
James Smart912e3ac2011-05-24 11:42:11 -04006738 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
James Smart8aa134a2012-08-14 14:25:29 -04006739 desc = (struct lpfc_rsrc_desc_fcfcoe *)(pdesc_0 + length * i);
James Smart912e3ac2011-05-24 11:42:11 -04006740 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
James Smart8aa134a2012-08-14 14:25:29 -04006741 bf_get(lpfc_rsrc_desc_fcfcoe_type, desc)) {
James Smart912e3ac2011-05-24 11:42:11 -04006742 phba->sli4_hba.iov.pf_number =
6743 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
6744 phba->sli4_hba.iov.vf_number =
6745 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
6746 break;
6747 }
6748 }
6749
6750 if (i < LPFC_RSRC_DESC_MAX_NUM)
6751 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6752 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
6753 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
6754 phba->sli4_hba.iov.vf_number);
James Smart8aa134a2012-08-14 14:25:29 -04006755 else
James Smart912e3ac2011-05-24 11:42:11 -04006756 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6757 "3028 GET_FUNCTION_CONFIG: failed to find "
6758 "Resrouce Descriptor:x%x\n",
6759 LPFC_RSRC_DESC_TYPE_FCFCOE);
James Smart912e3ac2011-05-24 11:42:11 -04006760
6761read_cfg_out:
6762 mempool_free(pmb, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04006763 return rc;
6764}
6765
6766/**
James Smart2fcee4b2010-12-15 17:57:46 -05006767 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
James Smartda0436e2009-05-22 14:51:39 -04006768 * @phba: pointer to lpfc hba data structure.
6769 *
James Smart2fcee4b2010-12-15 17:57:46 -05006770 * This routine is invoked to setup the port-side endian order when
6771 * the port if_type is 0. This routine has no function for other
6772 * if_types.
James Smartda0436e2009-05-22 14:51:39 -04006773 *
6774 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006775 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006776 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006777 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006778 **/
6779static int
6780lpfc_setup_endian_order(struct lpfc_hba *phba)
6781{
6782 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05006783 uint32_t if_type, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04006784 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
6785 HOST_ENDIAN_HIGH_WORD1};
6786
James Smart2fcee4b2010-12-15 17:57:46 -05006787 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
6788 switch (if_type) {
6789 case LPFC_SLI_INTF_IF_TYPE_0:
6790 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6791 GFP_KERNEL);
6792 if (!mboxq) {
6793 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6794 "0492 Unable to allocate memory for "
6795 "issuing SLI_CONFIG_SPECIAL mailbox "
6796 "command\n");
6797 return -ENOMEM;
6798 }
James Smartda0436e2009-05-22 14:51:39 -04006799
James Smart2fcee4b2010-12-15 17:57:46 -05006800 /*
6801 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
6802 * two words to contain special data values and no other data.
6803 */
6804 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
6805 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
6806 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6807 if (rc != MBX_SUCCESS) {
6808 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6809 "0493 SLI_CONFIG_SPECIAL mailbox "
6810 "failed with status x%x\n",
6811 rc);
6812 rc = -EIO;
6813 }
6814 mempool_free(mboxq, phba->mbox_mem_pool);
6815 break;
6816 case LPFC_SLI_INTF_IF_TYPE_2:
6817 case LPFC_SLI_INTF_IF_TYPE_1:
6818 default:
6819 break;
James Smartda0436e2009-05-22 14:51:39 -04006820 }
James Smartda0436e2009-05-22 14:51:39 -04006821 return rc;
6822}
6823
6824/**
James Smart5350d872011-10-10 21:33:49 -04006825 * lpfc_sli4_queue_verify - Verify and update EQ and CQ counts
James Smartda0436e2009-05-22 14:51:39 -04006826 * @phba: pointer to lpfc hba data structure.
6827 *
James Smart5350d872011-10-10 21:33:49 -04006828 * This routine is invoked to check the user settable queue counts for EQs and
6829 * CQs. after this routine is called the counts will be set to valid values that
6830 * adhere to the constraints of the system's interrupt vectors and the port's
6831 * queue resources.
James Smartda0436e2009-05-22 14:51:39 -04006832 *
6833 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006834 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006835 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04006836 **/
6837static int
James Smart5350d872011-10-10 21:33:49 -04006838lpfc_sli4_queue_verify(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04006839{
James Smart67d12732012-08-03 12:36:13 -04006840 int cfg_fcp_io_channel;
James Smart90695ee2012-08-14 14:25:36 -04006841 uint32_t cpu;
6842 uint32_t i = 0;
James Smart1ba981f2014-02-20 09:56:45 -05006843 int fof_vectors = phba->cfg_fof ? 1 : 0;
James Smartda0436e2009-05-22 14:51:39 -04006844
6845 /*
James Smart67d12732012-08-03 12:36:13 -04006846 * Sanity check for configured queue parameters against the run-time
James Smartda0436e2009-05-22 14:51:39 -04006847 * device parameters
6848 */
6849
James Smart67d12732012-08-03 12:36:13 -04006850 /* Sanity check on HBA EQ parameters */
6851 cfg_fcp_io_channel = phba->cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04006852
James Smart7bb03bb2013-04-17 20:19:16 -04006853 /* It doesn't make sense to have more io channels then online CPUs */
6854 for_each_present_cpu(cpu) {
6855 if (cpu_online(cpu))
6856 i++;
James Smart90695ee2012-08-14 14:25:36 -04006857 }
James Smart7bb03bb2013-04-17 20:19:16 -04006858 phba->sli4_hba.num_online_cpu = i;
James Smartb246de12013-05-31 17:03:07 -04006859 phba->sli4_hba.num_present_cpu = lpfc_present_cpu;
James Smart76fd07a2014-02-20 09:57:18 -05006860 phba->sli4_hba.curr_disp_cpu = 0;
James Smart7bb03bb2013-04-17 20:19:16 -04006861
James Smart90695ee2012-08-14 14:25:36 -04006862 if (i < cfg_fcp_io_channel) {
James Smart82c3e9b2012-09-29 11:29:50 -04006863 lpfc_printf_log(phba,
6864 KERN_ERR, LOG_INIT,
James Smart90695ee2012-08-14 14:25:36 -04006865 "3188 Reducing IO channels to match number of "
James Smart7bb03bb2013-04-17 20:19:16 -04006866 "online CPUs: from %d to %d\n",
6867 cfg_fcp_io_channel, i);
James Smart90695ee2012-08-14 14:25:36 -04006868 cfg_fcp_io_channel = i;
6869 }
6870
James Smart1ba981f2014-02-20 09:56:45 -05006871 if (cfg_fcp_io_channel + fof_vectors >
James Smart67d12732012-08-03 12:36:13 -04006872 phba->sli4_hba.max_cfg_param.max_eq) {
James Smart82c3e9b2012-09-29 11:29:50 -04006873 if (phba->sli4_hba.max_cfg_param.max_eq <
6874 LPFC_FCP_IO_CHAN_MIN) {
James Smartda0436e2009-05-22 14:51:39 -04006875 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6876 "2574 Not enough EQs (%d) from the "
6877 "pci function for supporting FCP "
6878 "EQs (%d)\n",
6879 phba->sli4_hba.max_cfg_param.max_eq,
James Smart67d12732012-08-03 12:36:13 -04006880 phba->cfg_fcp_io_channel);
James Smartda0436e2009-05-22 14:51:39 -04006881 goto out_error;
6882 }
James Smart82c3e9b2012-09-29 11:29:50 -04006883 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6884 "2575 Reducing IO channels to match number of "
6885 "available EQs: from %d to %d\n",
6886 cfg_fcp_io_channel,
6887 phba->sli4_hba.max_cfg_param.max_eq);
James Smart1ba981f2014-02-20 09:56:45 -05006888 cfg_fcp_io_channel = phba->sli4_hba.max_cfg_param.max_eq -
6889 fof_vectors;
James Smartda0436e2009-05-22 14:51:39 -04006890 }
James Smart67d12732012-08-03 12:36:13 -04006891
James Smartda0436e2009-05-22 14:51:39 -04006892 /* The actual number of FCP event queues adopted */
James Smart67d12732012-08-03 12:36:13 -04006893 phba->cfg_fcp_io_channel = cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04006894
James Smartda0436e2009-05-22 14:51:39 -04006895 /* Get EQ depth from module parameter, fake the default for now */
6896 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
6897 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
6898
James Smart5350d872011-10-10 21:33:49 -04006899 /* Get CQ depth from module parameter, fake the default for now */
6900 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
6901 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
6902
6903 return 0;
6904out_error:
6905 return -ENOMEM;
6906}
6907
6908/**
6909 * lpfc_sli4_queue_create - Create all the SLI4 queues
6910 * @phba: pointer to lpfc hba data structure.
6911 *
6912 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
6913 * operation. For each SLI4 queue type, the parameters such as queue entry
6914 * count (queue depth) shall be taken from the module parameter. For now,
6915 * we just use some constant number as place holder.
6916 *
6917 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07006918 * 0 - successful
James Smart5350d872011-10-10 21:33:49 -04006919 * -ENOMEM - No availble memory
6920 * -EIO - The mailbox failed to complete successfully.
6921 **/
6922int
6923lpfc_sli4_queue_create(struct lpfc_hba *phba)
6924{
6925 struct lpfc_queue *qdesc;
James Smart67d12732012-08-03 12:36:13 -04006926 int idx;
James Smart5350d872011-10-10 21:33:49 -04006927
6928 /*
James Smart67d12732012-08-03 12:36:13 -04006929 * Create HBA Record arrays.
James Smart5350d872011-10-10 21:33:49 -04006930 */
James Smart67d12732012-08-03 12:36:13 -04006931 if (!phba->cfg_fcp_io_channel)
6932 return -ERANGE;
James Smart5350d872011-10-10 21:33:49 -04006933
James Smart67d12732012-08-03 12:36:13 -04006934 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
6935 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
6936 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
6937 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
6938 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
6939 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
6940
6941 phba->sli4_hba.hba_eq = kzalloc((sizeof(struct lpfc_queue *) *
6942 phba->cfg_fcp_io_channel), GFP_KERNEL);
6943 if (!phba->sli4_hba.hba_eq) {
James Smartda0436e2009-05-22 14:51:39 -04006944 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04006945 "2576 Failed allocate memory for "
6946 "fast-path EQ record array\n");
James Smartda0436e2009-05-22 14:51:39 -04006947 goto out_error;
6948 }
James Smart67d12732012-08-03 12:36:13 -04006949
6950 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
6951 phba->cfg_fcp_io_channel), GFP_KERNEL);
6952 if (!phba->sli4_hba.fcp_cq) {
6953 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6954 "2577 Failed allocate memory for fast-path "
6955 "CQ record array\n");
6956 goto out_error;
6957 }
6958
6959 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
6960 phba->cfg_fcp_io_channel), GFP_KERNEL);
6961 if (!phba->sli4_hba.fcp_wq) {
6962 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6963 "2578 Failed allocate memory for fast-path "
6964 "WQ record array\n");
6965 goto out_error;
6966 }
James Smartda0436e2009-05-22 14:51:39 -04006967
James Smart5350d872011-10-10 21:33:49 -04006968 /*
James Smart67d12732012-08-03 12:36:13 -04006969 * Since the first EQ can have multiple CQs associated with it,
6970 * this array is used to quickly see if we have a FCP fast-path
6971 * CQ match.
James Smart5350d872011-10-10 21:33:49 -04006972 */
James Smart67d12732012-08-03 12:36:13 -04006973 phba->sli4_hba.fcp_cq_map = kzalloc((sizeof(uint16_t) *
6974 phba->cfg_fcp_io_channel), GFP_KERNEL);
6975 if (!phba->sli4_hba.fcp_cq_map) {
6976 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6977 "2545 Failed allocate memory for fast-path "
6978 "CQ map\n");
6979 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04006980 }
James Smart67d12732012-08-03 12:36:13 -04006981
6982 /*
6983 * Create HBA Event Queues (EQs). The cfg_fcp_io_channel specifies
6984 * how many EQs to create.
6985 */
6986 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
6987
6988 /* Create EQs */
James Smartda0436e2009-05-22 14:51:39 -04006989 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
6990 phba->sli4_hba.eq_ecount);
6991 if (!qdesc) {
6992 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04006993 "0497 Failed allocate EQ (%d)\n", idx);
6994 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04006995 }
James Smart67d12732012-08-03 12:36:13 -04006996 phba->sli4_hba.hba_eq[idx] = qdesc;
6997
6998 /* Create Fast Path FCP CQs */
6999 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7000 phba->sli4_hba.cq_ecount);
7001 if (!qdesc) {
7002 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7003 "0499 Failed allocate fast-path FCP "
7004 "CQ (%d)\n", idx);
7005 goto out_error;
7006 }
7007 phba->sli4_hba.fcp_cq[idx] = qdesc;
7008
7009 /* Create Fast Path FCP WQs */
7010 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
7011 phba->sli4_hba.wq_ecount);
7012 if (!qdesc) {
7013 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7014 "0503 Failed allocate fast-path FCP "
7015 "WQ (%d)\n", idx);
7016 goto out_error;
7017 }
7018 phba->sli4_hba.fcp_wq[idx] = qdesc;
James Smartda0436e2009-05-22 14:51:39 -04007019 }
7020
James Smart67d12732012-08-03 12:36:13 -04007021
James Smartda0436e2009-05-22 14:51:39 -04007022 /*
James Smart67d12732012-08-03 12:36:13 -04007023 * Create Slow Path Completion Queues (CQs)
James Smartda0436e2009-05-22 14:51:39 -04007024 */
7025
James Smartda0436e2009-05-22 14:51:39 -04007026 /* Create slow-path Mailbox Command Complete Queue */
7027 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7028 phba->sli4_hba.cq_ecount);
7029 if (!qdesc) {
7030 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7031 "0500 Failed allocate slow-path mailbox CQ\n");
James Smart67d12732012-08-03 12:36:13 -04007032 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007033 }
7034 phba->sli4_hba.mbx_cq = qdesc;
7035
7036 /* Create slow-path ELS Complete Queue */
7037 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
7038 phba->sli4_hba.cq_ecount);
7039 if (!qdesc) {
7040 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7041 "0501 Failed allocate slow-path ELS CQ\n");
James Smart67d12732012-08-03 12:36:13 -04007042 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007043 }
7044 phba->sli4_hba.els_cq = qdesc;
7045
James Smartda0436e2009-05-22 14:51:39 -04007046
James Smart5350d872011-10-10 21:33:49 -04007047 /*
James Smart67d12732012-08-03 12:36:13 -04007048 * Create Slow Path Work Queues (WQs)
James Smart5350d872011-10-10 21:33:49 -04007049 */
James Smartda0436e2009-05-22 14:51:39 -04007050
7051 /* Create Mailbox Command Queue */
James Smartda0436e2009-05-22 14:51:39 -04007052
7053 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
7054 phba->sli4_hba.mq_ecount);
7055 if (!qdesc) {
7056 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7057 "0505 Failed allocate slow-path MQ\n");
James Smart67d12732012-08-03 12:36:13 -04007058 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007059 }
7060 phba->sli4_hba.mbx_wq = qdesc;
7061
7062 /*
James Smart67d12732012-08-03 12:36:13 -04007063 * Create ELS Work Queues
James Smartda0436e2009-05-22 14:51:39 -04007064 */
James Smartda0436e2009-05-22 14:51:39 -04007065
7066 /* Create slow-path ELS Work Queue */
7067 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
7068 phba->sli4_hba.wq_ecount);
7069 if (!qdesc) {
7070 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7071 "0504 Failed allocate slow-path ELS WQ\n");
James Smart67d12732012-08-03 12:36:13 -04007072 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007073 }
7074 phba->sli4_hba.els_wq = qdesc;
7075
James Smartda0436e2009-05-22 14:51:39 -04007076 /*
7077 * Create Receive Queue (RQ)
7078 */
James Smartda0436e2009-05-22 14:51:39 -04007079
7080 /* Create Receive Queue for header */
7081 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
7082 phba->sli4_hba.rq_ecount);
7083 if (!qdesc) {
7084 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7085 "0506 Failed allocate receive HRQ\n");
James Smart67d12732012-08-03 12:36:13 -04007086 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007087 }
7088 phba->sli4_hba.hdr_rq = qdesc;
7089
7090 /* Create Receive Queue for data */
7091 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
7092 phba->sli4_hba.rq_ecount);
7093 if (!qdesc) {
7094 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7095 "0507 Failed allocate receive DRQ\n");
James Smart67d12732012-08-03 12:36:13 -04007096 goto out_error;
James Smartda0436e2009-05-22 14:51:39 -04007097 }
7098 phba->sli4_hba.dat_rq = qdesc;
7099
James Smart1ba981f2014-02-20 09:56:45 -05007100 /* Create the Queues needed for Flash Optimized Fabric operations */
7101 if (phba->cfg_fof)
7102 lpfc_fof_queue_create(phba);
James Smartda0436e2009-05-22 14:51:39 -04007103 return 0;
7104
James Smartda0436e2009-05-22 14:51:39 -04007105out_error:
James Smart67d12732012-08-03 12:36:13 -04007106 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007107 return -ENOMEM;
7108}
7109
7110/**
7111 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
7112 * @phba: pointer to lpfc hba data structure.
7113 *
7114 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
7115 * operation.
7116 *
7117 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007118 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007119 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007120 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007121 **/
James Smart5350d872011-10-10 21:33:49 -04007122void
James Smartda0436e2009-05-22 14:51:39 -04007123lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
7124{
James Smart67d12732012-08-03 12:36:13 -04007125 int idx;
7126
James Smart1ba981f2014-02-20 09:56:45 -05007127 if (phba->cfg_fof)
7128 lpfc_fof_queue_destroy(phba);
7129
James Smart67d12732012-08-03 12:36:13 -04007130 if (phba->sli4_hba.hba_eq != NULL) {
7131 /* Release HBA event queue */
7132 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7133 if (phba->sli4_hba.hba_eq[idx] != NULL) {
7134 lpfc_sli4_queue_free(
7135 phba->sli4_hba.hba_eq[idx]);
7136 phba->sli4_hba.hba_eq[idx] = NULL;
7137 }
7138 }
7139 kfree(phba->sli4_hba.hba_eq);
7140 phba->sli4_hba.hba_eq = NULL;
7141 }
7142
7143 if (phba->sli4_hba.fcp_cq != NULL) {
7144 /* Release FCP completion queue */
7145 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7146 if (phba->sli4_hba.fcp_cq[idx] != NULL) {
7147 lpfc_sli4_queue_free(
7148 phba->sli4_hba.fcp_cq[idx]);
7149 phba->sli4_hba.fcp_cq[idx] = NULL;
7150 }
7151 }
7152 kfree(phba->sli4_hba.fcp_cq);
7153 phba->sli4_hba.fcp_cq = NULL;
7154 }
7155
7156 if (phba->sli4_hba.fcp_wq != NULL) {
7157 /* Release FCP work queue */
7158 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++) {
7159 if (phba->sli4_hba.fcp_wq[idx] != NULL) {
7160 lpfc_sli4_queue_free(
7161 phba->sli4_hba.fcp_wq[idx]);
7162 phba->sli4_hba.fcp_wq[idx] = NULL;
7163 }
7164 }
7165 kfree(phba->sli4_hba.fcp_wq);
7166 phba->sli4_hba.fcp_wq = NULL;
7167 }
7168
7169 /* Release FCP CQ mapping array */
7170 if (phba->sli4_hba.fcp_cq_map != NULL) {
7171 kfree(phba->sli4_hba.fcp_cq_map);
7172 phba->sli4_hba.fcp_cq_map = NULL;
7173 }
James Smartda0436e2009-05-22 14:51:39 -04007174
7175 /* Release mailbox command work queue */
James Smart67d12732012-08-03 12:36:13 -04007176 if (phba->sli4_hba.mbx_wq != NULL) {
7177 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
7178 phba->sli4_hba.mbx_wq = NULL;
7179 }
James Smartda0436e2009-05-22 14:51:39 -04007180
7181 /* Release ELS work queue */
James Smart67d12732012-08-03 12:36:13 -04007182 if (phba->sli4_hba.els_wq != NULL) {
7183 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
7184 phba->sli4_hba.els_wq = NULL;
7185 }
James Smartda0436e2009-05-22 14:51:39 -04007186
7187 /* Release unsolicited receive queue */
James Smart67d12732012-08-03 12:36:13 -04007188 if (phba->sli4_hba.hdr_rq != NULL) {
7189 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
7190 phba->sli4_hba.hdr_rq = NULL;
7191 }
7192 if (phba->sli4_hba.dat_rq != NULL) {
7193 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
7194 phba->sli4_hba.dat_rq = NULL;
7195 }
James Smartda0436e2009-05-22 14:51:39 -04007196
James Smartda0436e2009-05-22 14:51:39 -04007197 /* Release ELS complete queue */
James Smart67d12732012-08-03 12:36:13 -04007198 if (phba->sli4_hba.els_cq != NULL) {
7199 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
7200 phba->sli4_hba.els_cq = NULL;
7201 }
James Smartda0436e2009-05-22 14:51:39 -04007202
7203 /* Release mailbox command complete queue */
James Smart67d12732012-08-03 12:36:13 -04007204 if (phba->sli4_hba.mbx_cq != NULL) {
7205 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
7206 phba->sli4_hba.mbx_cq = NULL;
7207 }
James Smartda0436e2009-05-22 14:51:39 -04007208
7209 return;
7210}
7211
7212/**
7213 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
7214 * @phba: pointer to lpfc hba data structure.
7215 *
7216 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
7217 * operation.
7218 *
7219 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007220 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007221 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007222 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007223 **/
7224int
7225lpfc_sli4_queue_setup(struct lpfc_hba *phba)
7226{
James Smart2a76a282012-08-03 12:35:54 -04007227 struct lpfc_sli *psli = &phba->sli;
7228 struct lpfc_sli_ring *pring;
James Smartda0436e2009-05-22 14:51:39 -04007229 int rc = -ENOMEM;
7230 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
7231 int fcp_cq_index = 0;
James Smart962bc512013-01-03 15:44:00 -05007232 uint32_t shdr_status, shdr_add_status;
7233 union lpfc_sli4_cfg_shdr *shdr;
7234 LPFC_MBOXQ_t *mboxq;
7235 uint32_t length;
7236
7237 /* Check for dual-ULP support */
7238 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7239 if (!mboxq) {
7240 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7241 "3249 Unable to allocate memory for "
7242 "QUERY_FW_CFG mailbox command\n");
7243 return -ENOMEM;
7244 }
7245 length = (sizeof(struct lpfc_mbx_query_fw_config) -
7246 sizeof(struct lpfc_sli4_cfg_mhdr));
7247 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7248 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
7249 length, LPFC_SLI4_MBX_EMBED);
7250
7251 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7252
7253 shdr = (union lpfc_sli4_cfg_shdr *)
7254 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7255 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7256 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7257 if (shdr_status || shdr_add_status || rc) {
7258 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7259 "3250 QUERY_FW_CFG mailbox failed with status "
7260 "x%x add_status x%x, mbx status x%x\n",
7261 shdr_status, shdr_add_status, rc);
7262 if (rc != MBX_TIMEOUT)
7263 mempool_free(mboxq, phba->mbox_mem_pool);
7264 rc = -ENXIO;
7265 goto out_error;
7266 }
7267
7268 phba->sli4_hba.fw_func_mode =
7269 mboxq->u.mqe.un.query_fw_cfg.rsp.function_mode;
7270 phba->sli4_hba.ulp0_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp0_mode;
7271 phba->sli4_hba.ulp1_mode = mboxq->u.mqe.un.query_fw_cfg.rsp.ulp1_mode;
7272 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7273 "3251 QUERY_FW_CFG: func_mode:x%x, ulp0_mode:x%x, "
7274 "ulp1_mode:x%x\n", phba->sli4_hba.fw_func_mode,
7275 phba->sli4_hba.ulp0_mode, phba->sli4_hba.ulp1_mode);
7276
7277 if (rc != MBX_TIMEOUT)
7278 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04007279
7280 /*
James Smart67d12732012-08-03 12:36:13 -04007281 * Set up HBA Event Queues (EQs)
James Smartda0436e2009-05-22 14:51:39 -04007282 */
7283
James Smart67d12732012-08-03 12:36:13 -04007284 /* Set up HBA event queue */
7285 if (phba->cfg_fcp_io_channel && !phba->sli4_hba.hba_eq) {
James Smart2e90f4b2011-12-13 13:22:37 -05007286 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7287 "3147 Fast-path EQs not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007288 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007289 goto out_error;
James Smart2e90f4b2011-12-13 13:22:37 -05007290 }
James Smart67d12732012-08-03 12:36:13 -04007291 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
7292 if (!phba->sli4_hba.hba_eq[fcp_eqidx]) {
James Smartda0436e2009-05-22 14:51:39 -04007293 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7294 "0522 Fast-path EQ (%d) not "
7295 "allocated\n", fcp_eqidx);
James Smart1b511972011-12-13 13:23:09 -05007296 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007297 goto out_destroy_hba_eq;
James Smartda0436e2009-05-22 14:51:39 -04007298 }
James Smart67d12732012-08-03 12:36:13 -04007299 rc = lpfc_eq_create(phba, phba->sli4_hba.hba_eq[fcp_eqidx],
James Smartbf8dae82012-08-03 12:36:24 -04007300 (phba->cfg_fcp_imax / phba->cfg_fcp_io_channel));
James Smartda0436e2009-05-22 14:51:39 -04007301 if (rc) {
7302 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7303 "0523 Failed setup of fast-path EQ "
7304 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
James Smart67d12732012-08-03 12:36:13 -04007305 goto out_destroy_hba_eq;
James Smartda0436e2009-05-22 14:51:39 -04007306 }
7307 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart67d12732012-08-03 12:36:13 -04007308 "2584 HBA EQ setup: "
James Smartda0436e2009-05-22 14:51:39 -04007309 "queue[%d]-id=%d\n", fcp_eqidx,
James Smart67d12732012-08-03 12:36:13 -04007310 phba->sli4_hba.hba_eq[fcp_eqidx]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007311 }
7312
James Smart67d12732012-08-03 12:36:13 -04007313 /* Set up fast-path FCP Response Complete Queue */
7314 if (!phba->sli4_hba.fcp_cq) {
7315 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7316 "3148 Fast-path FCP CQ array not "
7317 "allocated\n");
7318 rc = -ENOMEM;
7319 goto out_destroy_hba_eq;
7320 }
7321
7322 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_io_channel; fcp_cqidx++) {
7323 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
7324 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7325 "0526 Fast-path FCP CQ (%d) not "
7326 "allocated\n", fcp_cqidx);
7327 rc = -ENOMEM;
7328 goto out_destroy_fcp_cq;
7329 }
7330 rc = lpfc_cq_create(phba, phba->sli4_hba.fcp_cq[fcp_cqidx],
7331 phba->sli4_hba.hba_eq[fcp_cqidx], LPFC_WCQ, LPFC_FCP);
7332 if (rc) {
7333 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7334 "0527 Failed setup of fast-path FCP "
7335 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
7336 goto out_destroy_fcp_cq;
7337 }
7338
7339 /* Setup fcp_cq_map for fast lookup */
7340 phba->sli4_hba.fcp_cq_map[fcp_cqidx] =
7341 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id;
7342
7343 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7344 "2588 FCP CQ setup: cq[%d]-id=%d, "
7345 "parent seq[%d]-id=%d\n",
7346 fcp_cqidx,
7347 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
7348 fcp_cqidx,
7349 phba->sli4_hba.hba_eq[fcp_cqidx]->queue_id);
7350 }
7351
7352 /* Set up fast-path FCP Work Queue */
7353 if (!phba->sli4_hba.fcp_wq) {
7354 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7355 "3149 Fast-path FCP WQ array not "
7356 "allocated\n");
7357 rc = -ENOMEM;
7358 goto out_destroy_fcp_cq;
7359 }
7360
7361 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_io_channel; fcp_wqidx++) {
7362 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
7363 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7364 "0534 Fast-path FCP WQ (%d) not "
7365 "allocated\n", fcp_wqidx);
7366 rc = -ENOMEM;
7367 goto out_destroy_fcp_wq;
7368 }
7369 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
7370 phba->sli4_hba.fcp_cq[fcp_wqidx],
7371 LPFC_FCP);
7372 if (rc) {
7373 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7374 "0535 Failed setup of fast-path FCP "
7375 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
7376 goto out_destroy_fcp_wq;
7377 }
7378
7379 /* Bind this WQ to the next FCP ring */
7380 pring = &psli->ring[MAX_SLI3_CONFIGURED_RINGS + fcp_wqidx];
7381 pring->sli.sli4.wqp = (void *)phba->sli4_hba.fcp_wq[fcp_wqidx];
7382 phba->sli4_hba.fcp_cq[fcp_wqidx]->pring = pring;
7383
7384 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7385 "2591 FCP WQ setup: wq[%d]-id=%d, "
7386 "parent cq[%d]-id=%d\n",
7387 fcp_wqidx,
7388 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
7389 fcp_cq_index,
7390 phba->sli4_hba.fcp_cq[fcp_wqidx]->queue_id);
7391 }
James Smartda0436e2009-05-22 14:51:39 -04007392 /*
7393 * Set up Complete Queues (CQs)
7394 */
7395
7396 /* Set up slow-path MBOX Complete Queue as the first CQ */
7397 if (!phba->sli4_hba.mbx_cq) {
7398 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7399 "0528 Mailbox CQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007400 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007401 goto out_destroy_fcp_wq;
James Smartda0436e2009-05-22 14:51:39 -04007402 }
James Smart67d12732012-08-03 12:36:13 -04007403 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq,
7404 phba->sli4_hba.hba_eq[0], LPFC_MCQ, LPFC_MBOX);
James Smartda0436e2009-05-22 14:51:39 -04007405 if (rc) {
7406 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7407 "0529 Failed setup of slow-path mailbox CQ: "
7408 "rc = 0x%x\n", rc);
James Smart67d12732012-08-03 12:36:13 -04007409 goto out_destroy_fcp_wq;
James Smartda0436e2009-05-22 14:51:39 -04007410 }
7411 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7412 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
7413 phba->sli4_hba.mbx_cq->queue_id,
James Smart67d12732012-08-03 12:36:13 -04007414 phba->sli4_hba.hba_eq[0]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007415
7416 /* Set up slow-path ELS Complete Queue */
7417 if (!phba->sli4_hba.els_cq) {
7418 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7419 "0530 ELS CQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007420 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04007421 goto out_destroy_mbx_cq;
7422 }
James Smart67d12732012-08-03 12:36:13 -04007423 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq,
7424 phba->sli4_hba.hba_eq[0], LPFC_WCQ, LPFC_ELS);
James Smartda0436e2009-05-22 14:51:39 -04007425 if (rc) {
7426 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7427 "0531 Failed setup of slow-path ELS CQ: "
7428 "rc = 0x%x\n", rc);
7429 goto out_destroy_mbx_cq;
7430 }
7431 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7432 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
7433 phba->sli4_hba.els_cq->queue_id,
James Smart67d12732012-08-03 12:36:13 -04007434 phba->sli4_hba.hba_eq[0]->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04007435
7436 /*
7437 * Set up all the Work Queues (WQs)
7438 */
7439
7440 /* Set up Mailbox Command Queue */
7441 if (!phba->sli4_hba.mbx_wq) {
7442 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7443 "0538 Slow-path MQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007444 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007445 goto out_destroy_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04007446 }
7447 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
7448 phba->sli4_hba.mbx_cq, LPFC_MBOX);
7449 if (rc) {
7450 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7451 "0539 Failed setup of slow-path MQ: "
7452 "rc = 0x%x\n", rc);
James Smart67d12732012-08-03 12:36:13 -04007453 goto out_destroy_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04007454 }
7455 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7456 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
7457 phba->sli4_hba.mbx_wq->queue_id,
7458 phba->sli4_hba.mbx_cq->queue_id);
7459
7460 /* Set up slow-path ELS Work Queue */
7461 if (!phba->sli4_hba.els_wq) {
7462 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7463 "0536 Slow-path ELS WQ not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007464 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04007465 goto out_destroy_mbx_wq;
7466 }
7467 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
7468 phba->sli4_hba.els_cq, LPFC_ELS);
7469 if (rc) {
7470 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7471 "0537 Failed setup of slow-path ELS WQ: "
7472 "rc = 0x%x\n", rc);
7473 goto out_destroy_mbx_wq;
7474 }
James Smart2a76a282012-08-03 12:35:54 -04007475
7476 /* Bind this WQ to the ELS ring */
7477 pring = &psli->ring[LPFC_ELS_RING];
7478 pring->sli.sli4.wqp = (void *)phba->sli4_hba.els_wq;
7479 phba->sli4_hba.els_cq->pring = pring;
7480
James Smartda0436e2009-05-22 14:51:39 -04007481 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7482 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
7483 phba->sli4_hba.els_wq->queue_id,
7484 phba->sli4_hba.els_cq->queue_id);
7485
James Smartda0436e2009-05-22 14:51:39 -04007486 /*
7487 * Create Receive Queue (RQ)
7488 */
7489 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
7490 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7491 "0540 Receive Queue not allocated\n");
James Smart1b511972011-12-13 13:23:09 -05007492 rc = -ENOMEM;
James Smart67d12732012-08-03 12:36:13 -04007493 goto out_destroy_els_wq;
James Smartda0436e2009-05-22 14:51:39 -04007494 }
James Smart73d91e52011-10-10 21:32:10 -04007495
7496 lpfc_rq_adjust_repost(phba, phba->sli4_hba.hdr_rq, LPFC_ELS_HBQ);
7497 lpfc_rq_adjust_repost(phba, phba->sli4_hba.dat_rq, LPFC_ELS_HBQ);
7498
James Smartda0436e2009-05-22 14:51:39 -04007499 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04007500 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04007501 if (rc) {
7502 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7503 "0541 Failed setup of Receive Queue: "
7504 "rc = 0x%x\n", rc);
7505 goto out_destroy_fcp_wq;
7506 }
James Smart73d91e52011-10-10 21:32:10 -04007507
James Smartda0436e2009-05-22 14:51:39 -04007508 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7509 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
7510 "parent cq-id=%d\n",
7511 phba->sli4_hba.hdr_rq->queue_id,
7512 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04007513 phba->sli4_hba.els_cq->queue_id);
James Smart1ba981f2014-02-20 09:56:45 -05007514
7515 if (phba->cfg_fof) {
7516 rc = lpfc_fof_queue_setup(phba);
7517 if (rc) {
7518 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7519 "0549 Failed setup of FOF Queues: "
7520 "rc = 0x%x\n", rc);
7521 goto out_destroy_els_rq;
7522 }
7523 }
James Smartda0436e2009-05-22 14:51:39 -04007524 return 0;
7525
James Smart1ba981f2014-02-20 09:56:45 -05007526out_destroy_els_rq:
7527 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
James Smart2e90f4b2011-12-13 13:22:37 -05007528out_destroy_els_wq:
James Smartda0436e2009-05-22 14:51:39 -04007529 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7530out_destroy_mbx_wq:
7531 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
James Smart2e90f4b2011-12-13 13:22:37 -05007532out_destroy_els_cq:
James Smartda0436e2009-05-22 14:51:39 -04007533 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
7534out_destroy_mbx_cq:
7535 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
James Smart67d12732012-08-03 12:36:13 -04007536out_destroy_fcp_wq:
7537 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
7538 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
7539out_destroy_fcp_cq:
7540 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
7541 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
7542out_destroy_hba_eq:
James Smartda0436e2009-05-22 14:51:39 -04007543 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
James Smart67d12732012-08-03 12:36:13 -04007544 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[fcp_eqidx]);
James Smartda0436e2009-05-22 14:51:39 -04007545out_error:
7546 return rc;
7547}
7548
7549/**
7550 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
7551 * @phba: pointer to lpfc hba data structure.
7552 *
7553 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
7554 * operation.
7555 *
7556 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007557 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007558 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007559 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007560 **/
7561void
7562lpfc_sli4_queue_unset(struct lpfc_hba *phba)
7563{
7564 int fcp_qidx;
7565
James Smart1ba981f2014-02-20 09:56:45 -05007566 /* Unset the queues created for Flash Optimized Fabric operations */
7567 if (phba->cfg_fof)
7568 lpfc_fof_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007569 /* Unset mailbox command work queue */
7570 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
7571 /* Unset ELS work queue */
7572 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
7573 /* Unset unsolicited receive queue */
7574 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
7575 /* Unset FCP work queue */
James Smart67d12732012-08-03 12:36:13 -04007576 if (phba->sli4_hba.fcp_wq) {
7577 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
7578 fcp_qidx++)
7579 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
7580 }
James Smartda0436e2009-05-22 14:51:39 -04007581 /* Unset mailbox command complete queue */
7582 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
7583 /* Unset ELS complete queue */
7584 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
James Smartda0436e2009-05-22 14:51:39 -04007585 /* Unset FCP response complete queue */
James Smart2e90f4b2011-12-13 13:22:37 -05007586 if (phba->sli4_hba.fcp_cq) {
James Smart67d12732012-08-03 12:36:13 -04007587 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
7588 fcp_qidx++)
James Smart2e90f4b2011-12-13 13:22:37 -05007589 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05007590 }
James Smartda0436e2009-05-22 14:51:39 -04007591 /* Unset fast-path event queue */
James Smart67d12732012-08-03 12:36:13 -04007592 if (phba->sli4_hba.hba_eq) {
7593 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_io_channel;
James Smart2e90f4b2011-12-13 13:22:37 -05007594 fcp_qidx++)
James Smart67d12732012-08-03 12:36:13 -04007595 lpfc_eq_destroy(phba, phba->sli4_hba.hba_eq[fcp_qidx]);
James Smart2e90f4b2011-12-13 13:22:37 -05007596 }
James Smartda0436e2009-05-22 14:51:39 -04007597}
7598
7599/**
7600 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
7601 * @phba: pointer to lpfc hba data structure.
7602 *
7603 * This routine is invoked to allocate and set up a pool of completion queue
7604 * events. The body of the completion queue event is a completion queue entry
7605 * CQE. For now, this pool is used for the interrupt service routine to queue
7606 * the following HBA completion queue events for the worker thread to process:
7607 * - Mailbox asynchronous events
7608 * - Receive queue completion unsolicited events
7609 * Later, this can be used for all the slow-path events.
7610 *
7611 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007612 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007613 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04007614 **/
7615static int
7616lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
7617{
7618 struct lpfc_cq_event *cq_event;
7619 int i;
7620
7621 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
7622 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
7623 if (!cq_event)
7624 goto out_pool_create_fail;
7625 list_add_tail(&cq_event->list,
7626 &phba->sli4_hba.sp_cqe_event_pool);
7627 }
7628 return 0;
7629
7630out_pool_create_fail:
7631 lpfc_sli4_cq_event_pool_destroy(phba);
7632 return -ENOMEM;
7633}
7634
7635/**
7636 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
7637 * @phba: pointer to lpfc hba data structure.
7638 *
7639 * This routine is invoked to free the pool of completion queue events at
7640 * driver unload time. Note that, it is the responsibility of the driver
7641 * cleanup routine to free all the outstanding completion-queue events
7642 * allocated from this pool back into the pool before invoking this routine
7643 * to destroy the pool.
7644 **/
7645static void
7646lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
7647{
7648 struct lpfc_cq_event *cq_event, *next_cq_event;
7649
7650 list_for_each_entry_safe(cq_event, next_cq_event,
7651 &phba->sli4_hba.sp_cqe_event_pool, list) {
7652 list_del(&cq_event->list);
7653 kfree(cq_event);
7654 }
7655}
7656
7657/**
7658 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
7659 * @phba: pointer to lpfc hba data structure.
7660 *
7661 * This routine is the lock free version of the API invoked to allocate a
7662 * completion-queue event from the free pool.
7663 *
7664 * Return: Pointer to the newly allocated completion-queue event if successful
7665 * NULL otherwise.
7666 **/
7667struct lpfc_cq_event *
7668__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
7669{
7670 struct lpfc_cq_event *cq_event = NULL;
7671
7672 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
7673 struct lpfc_cq_event, list);
7674 return cq_event;
7675}
7676
7677/**
7678 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
7679 * @phba: pointer to lpfc hba data structure.
7680 *
7681 * This routine is the lock version of the API invoked to allocate a
7682 * completion-queue event from the free pool.
7683 *
7684 * Return: Pointer to the newly allocated completion-queue event if successful
7685 * NULL otherwise.
7686 **/
7687struct lpfc_cq_event *
7688lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
7689{
7690 struct lpfc_cq_event *cq_event;
7691 unsigned long iflags;
7692
7693 spin_lock_irqsave(&phba->hbalock, iflags);
7694 cq_event = __lpfc_sli4_cq_event_alloc(phba);
7695 spin_unlock_irqrestore(&phba->hbalock, iflags);
7696 return cq_event;
7697}
7698
7699/**
7700 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
7701 * @phba: pointer to lpfc hba data structure.
7702 * @cq_event: pointer to the completion queue event to be freed.
7703 *
7704 * This routine is the lock free version of the API invoked to release a
7705 * completion-queue event back into the free pool.
7706 **/
7707void
7708__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
7709 struct lpfc_cq_event *cq_event)
7710{
7711 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
7712}
7713
7714/**
7715 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
7716 * @phba: pointer to lpfc hba data structure.
7717 * @cq_event: pointer to the completion queue event to be freed.
7718 *
7719 * This routine is the lock version of the API invoked to release a
7720 * completion-queue event back into the free pool.
7721 **/
7722void
7723lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
7724 struct lpfc_cq_event *cq_event)
7725{
7726 unsigned long iflags;
7727 spin_lock_irqsave(&phba->hbalock, iflags);
7728 __lpfc_sli4_cq_event_release(phba, cq_event);
7729 spin_unlock_irqrestore(&phba->hbalock, iflags);
7730}
7731
7732/**
7733 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
7734 * @phba: pointer to lpfc hba data structure.
7735 *
7736 * This routine is to free all the pending completion-queue events to the
7737 * back into the free pool for device reset.
7738 **/
7739static void
7740lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
7741{
7742 LIST_HEAD(cqelist);
7743 struct lpfc_cq_event *cqe;
7744 unsigned long iflags;
7745
7746 /* Retrieve all the pending WCQEs from pending WCQE lists */
7747 spin_lock_irqsave(&phba->hbalock, iflags);
7748 /* Pending FCP XRI abort events */
7749 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
7750 &cqelist);
7751 /* Pending ELS XRI abort events */
7752 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
7753 &cqelist);
7754 /* Pending asynnc events */
7755 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
7756 &cqelist);
7757 spin_unlock_irqrestore(&phba->hbalock, iflags);
7758
7759 while (!list_empty(&cqelist)) {
7760 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
7761 lpfc_sli4_cq_event_release(phba, cqe);
7762 }
7763}
7764
7765/**
7766 * lpfc_pci_function_reset - Reset pci function.
7767 * @phba: pointer to lpfc hba data structure.
7768 *
7769 * This routine is invoked to request a PCI function reset. It will destroys
7770 * all resources assigned to the PCI function which originates this request.
7771 *
7772 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007773 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007774 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007775 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007776 **/
7777int
7778lpfc_pci_function_reset(struct lpfc_hba *phba)
7779{
7780 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05007781 uint32_t rc = 0, if_type;
James Smartda0436e2009-05-22 14:51:39 -04007782 uint32_t shdr_status, shdr_add_status;
James Smart2fcee4b2010-12-15 17:57:46 -05007783 uint32_t rdy_chk, num_resets = 0, reset_again = 0;
James Smartda0436e2009-05-22 14:51:39 -04007784 union lpfc_sli4_cfg_shdr *shdr;
James Smart2fcee4b2010-12-15 17:57:46 -05007785 struct lpfc_register reg_data;
James Smart2b81f942012-03-01 22:37:18 -05007786 uint16_t devid;
James Smartda0436e2009-05-22 14:51:39 -04007787
James Smart2fcee4b2010-12-15 17:57:46 -05007788 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7789 switch (if_type) {
7790 case LPFC_SLI_INTF_IF_TYPE_0:
7791 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
7792 GFP_KERNEL);
7793 if (!mboxq) {
7794 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7795 "0494 Unable to allocate memory for "
7796 "issuing SLI_FUNCTION_RESET mailbox "
7797 "command\n");
7798 return -ENOMEM;
7799 }
7800
7801 /* Setup PCI function reset mailbox-ioctl command */
7802 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7803 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
7804 LPFC_SLI4_MBX_EMBED);
7805 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7806 shdr = (union lpfc_sli4_cfg_shdr *)
7807 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7808 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7809 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
7810 &shdr->response);
7811 if (rc != MBX_TIMEOUT)
7812 mempool_free(mboxq, phba->mbox_mem_pool);
7813 if (shdr_status || shdr_add_status || rc) {
7814 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7815 "0495 SLI_FUNCTION_RESET mailbox "
7816 "failed with status x%x add_status x%x,"
7817 " mbx status x%x\n",
7818 shdr_status, shdr_add_status, rc);
7819 rc = -ENXIO;
7820 }
7821 break;
7822 case LPFC_SLI_INTF_IF_TYPE_2:
7823 for (num_resets = 0;
7824 num_resets < MAX_IF_TYPE_2_RESETS;
7825 num_resets++) {
7826 reg_data.word0 = 0;
7827 bf_set(lpfc_sliport_ctrl_end, &reg_data,
7828 LPFC_SLIPORT_LITTLE_ENDIAN);
7829 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
7830 LPFC_SLIPORT_INIT_PORT);
7831 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
7832 CTRLregaddr);
James Smart8fcb8ac2012-03-01 22:35:58 -05007833 /* flush */
James Smart2b81f942012-03-01 22:37:18 -05007834 pci_read_config_word(phba->pcidev,
7835 PCI_DEVICE_ID, &devid);
James Smart2fcee4b2010-12-15 17:57:46 -05007836 /*
7837 * Poll the Port Status Register and wait for RDY for
7838 * up to 10 seconds. If the port doesn't respond, treat
7839 * it as an error. If the port responds with RN, start
7840 * the loop again.
7841 */
7842 for (rdy_chk = 0; rdy_chk < 1000; rdy_chk++) {
James Smart73d91e52011-10-10 21:32:10 -04007843 msleep(10);
James Smart9940b972011-03-11 16:06:12 -05007844 if (lpfc_readl(phba->sli4_hba.u.if_type2.
7845 STATUSregaddr, &reg_data.word0)) {
7846 rc = -ENODEV;
James Smart73d91e52011-10-10 21:32:10 -04007847 goto out;
James Smart9940b972011-03-11 16:06:12 -05007848 }
James Smart6b5151f2012-01-18 16:24:06 -05007849 if (bf_get(lpfc_sliport_status_rn, &reg_data))
7850 reset_again++;
James Smart2fcee4b2010-12-15 17:57:46 -05007851 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
7852 break;
James Smart2fcee4b2010-12-15 17:57:46 -05007853 }
7854
7855 /*
7856 * If the port responds to the init request with
7857 * reset needed, delay for a bit and restart the loop.
7858 */
James Smart6b5151f2012-01-18 16:24:06 -05007859 if (reset_again && (rdy_chk < 1000)) {
James Smart2fcee4b2010-12-15 17:57:46 -05007860 msleep(10);
7861 reset_again = 0;
7862 continue;
7863 }
7864
7865 /* Detect any port errors. */
James Smart2fcee4b2010-12-15 17:57:46 -05007866 if ((bf_get(lpfc_sliport_status_err, &reg_data)) ||
7867 (rdy_chk >= 1000)) {
7868 phba->work_status[0] = readl(
7869 phba->sli4_hba.u.if_type2.ERR1regaddr);
7870 phba->work_status[1] = readl(
7871 phba->sli4_hba.u.if_type2.ERR2regaddr);
7872 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart8fcb8ac2012-03-01 22:35:58 -05007873 "2890 Port error detected during port "
James Smart8a9d2e82012-05-09 21:16:12 -04007874 "reset(%d): wait_tmo:%d ms, "
7875 "port status reg 0x%x, "
James Smart2fcee4b2010-12-15 17:57:46 -05007876 "error 1=0x%x, error 2=0x%x\n",
James Smart8a9d2e82012-05-09 21:16:12 -04007877 num_resets, rdy_chk*10,
7878 reg_data.word0,
James Smart2fcee4b2010-12-15 17:57:46 -05007879 phba->work_status[0],
7880 phba->work_status[1]);
7881 rc = -ENODEV;
7882 }
7883
7884 /*
7885 * Terminate the outer loop provided the Port indicated
7886 * ready within 10 seconds.
7887 */
7888 if (rdy_chk < 1000)
7889 break;
7890 }
James Smart05580562011-05-24 11:40:48 -04007891 /* delay driver action following IF_TYPE_2 function reset */
7892 msleep(100);
James Smart2fcee4b2010-12-15 17:57:46 -05007893 break;
7894 case LPFC_SLI_INTF_IF_TYPE_1:
7895 default:
7896 break;
James Smartda0436e2009-05-22 14:51:39 -04007897 }
7898
James Smart73d91e52011-10-10 21:32:10 -04007899out:
James Smart2fcee4b2010-12-15 17:57:46 -05007900 /* Catch the not-ready port failure after a port reset. */
James Smart229adb02013-04-17 20:16:51 -04007901 if (num_resets >= MAX_IF_TYPE_2_RESETS) {
7902 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7903 "3317 HBA not functional: IP Reset Failed "
7904 "after (%d) retries, try: "
7905 "echo fw_reset > board_mode\n", num_resets);
James Smart2fcee4b2010-12-15 17:57:46 -05007906 rc = -ENODEV;
James Smart229adb02013-04-17 20:16:51 -04007907 }
James Smart2fcee4b2010-12-15 17:57:46 -05007908
James Smartda0436e2009-05-22 14:51:39 -04007909 return rc;
7910}
7911
7912/**
James Smartda0436e2009-05-22 14:51:39 -04007913 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
7914 * @phba: pointer to lpfc hba data structure.
7915 *
7916 * This routine is invoked to set up the PCI device memory space for device
7917 * with SLI-4 interface spec.
7918 *
7919 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007920 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007921 * other values - error
7922 **/
7923static int
7924lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
7925{
7926 struct pci_dev *pdev;
7927 unsigned long bar0map_len, bar1map_len, bar2map_len;
7928 int error = -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -05007929 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04007930
7931 /* Obtain PCI device reference */
7932 if (!phba->pcidev)
7933 return error;
7934 else
7935 pdev = phba->pcidev;
7936
7937 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05007938 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
7939 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
7940 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
7941 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smartda0436e2009-05-22 14:51:39 -04007942 return error;
Michael Reed8e685972009-09-18 12:02:05 -05007943 }
7944 }
James Smartda0436e2009-05-22 14:51:39 -04007945
James Smart2fcee4b2010-12-15 17:57:46 -05007946 /*
7947 * The BARs and register set definitions and offset locations are
7948 * dependent on the if_type.
7949 */
7950 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
7951 &phba->sli4_hba.sli_intf.word0)) {
7952 return error;
7953 }
7954
7955 /* There is no SLI3 failback for SLI4 devices. */
7956 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
7957 LPFC_SLI_INTF_VALID) {
7958 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7959 "2894 SLI_INTF reg contents invalid "
7960 "sli_intf reg 0x%x\n",
7961 phba->sli4_hba.sli_intf.word0);
7962 return error;
7963 }
7964
7965 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7966 /*
7967 * Get the bus address of SLI4 device Bar regions and the
7968 * number of bytes required by each mapping. The mapping of the
7969 * particular PCI BARs regions is dependent on the type of
7970 * SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -04007971 */
James Smartf5ca6f22013-09-06 12:21:09 -04007972 if (pci_resource_start(pdev, PCI_64BIT_BAR0)) {
7973 phba->pci_bar0_map = pci_resource_start(pdev, PCI_64BIT_BAR0);
7974 bar0map_len = pci_resource_len(pdev, PCI_64BIT_BAR0);
James Smart2fcee4b2010-12-15 17:57:46 -05007975
7976 /*
7977 * Map SLI4 PCI Config Space Register base to a kernel virtual
7978 * addr
7979 */
7980 phba->sli4_hba.conf_regs_memmap_p =
7981 ioremap(phba->pci_bar0_map, bar0map_len);
7982 if (!phba->sli4_hba.conf_regs_memmap_p) {
7983 dev_printk(KERN_ERR, &pdev->dev,
7984 "ioremap failed for SLI4 PCI config "
7985 "registers.\n");
7986 goto out;
7987 }
James Smartf5ca6f22013-09-06 12:21:09 -04007988 phba->pci_bar0_memmap_p = phba->sli4_hba.conf_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05007989 /* Set up BAR0 PCI config space register memory map */
7990 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smart1dfb5a42010-02-12 14:40:50 -05007991 } else {
7992 phba->pci_bar0_map = pci_resource_start(pdev, 1);
7993 bar0map_len = pci_resource_len(pdev, 1);
James Smart2fcee4b2010-12-15 17:57:46 -05007994 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
7995 dev_printk(KERN_ERR, &pdev->dev,
7996 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
7997 goto out;
7998 }
7999 phba->sli4_hba.conf_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04008000 ioremap(phba->pci_bar0_map, bar0map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05008001 if (!phba->sli4_hba.conf_regs_memmap_p) {
8002 dev_printk(KERN_ERR, &pdev->dev,
8003 "ioremap failed for SLI4 PCI config "
8004 "registers.\n");
8005 goto out;
8006 }
8007 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smartda0436e2009-05-22 14:51:39 -04008008 }
8009
James Smartc31098c2011-04-16 11:03:33 -04008010 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
James Smartf5ca6f22013-09-06 12:21:09 -04008011 (pci_resource_start(pdev, PCI_64BIT_BAR2))) {
James Smart2fcee4b2010-12-15 17:57:46 -05008012 /*
8013 * Map SLI4 if type 0 HBA Control Register base to a kernel
8014 * virtual address and setup the registers.
8015 */
James Smartf5ca6f22013-09-06 12:21:09 -04008016 phba->pci_bar1_map = pci_resource_start(pdev, PCI_64BIT_BAR2);
8017 bar1map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
James Smart2fcee4b2010-12-15 17:57:46 -05008018 phba->sli4_hba.ctrl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04008019 ioremap(phba->pci_bar1_map, bar1map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05008020 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
8021 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04008022 "ioremap failed for SLI4 HBA control registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05008023 goto out_iounmap_conf;
8024 }
James Smartf5ca6f22013-09-06 12:21:09 -04008025 phba->pci_bar2_memmap_p = phba->sli4_hba.ctrl_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05008026 lpfc_sli4_bar1_register_memmap(phba);
James Smartda0436e2009-05-22 14:51:39 -04008027 }
8028
James Smartc31098c2011-04-16 11:03:33 -04008029 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
James Smartf5ca6f22013-09-06 12:21:09 -04008030 (pci_resource_start(pdev, PCI_64BIT_BAR4))) {
James Smart2fcee4b2010-12-15 17:57:46 -05008031 /*
8032 * Map SLI4 if type 0 HBA Doorbell Register base to a kernel
8033 * virtual address and setup the registers.
8034 */
James Smartf5ca6f22013-09-06 12:21:09 -04008035 phba->pci_bar2_map = pci_resource_start(pdev, PCI_64BIT_BAR4);
8036 bar2map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
James Smart2fcee4b2010-12-15 17:57:46 -05008037 phba->sli4_hba.drbl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04008038 ioremap(phba->pci_bar2_map, bar2map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05008039 if (!phba->sli4_hba.drbl_regs_memmap_p) {
8040 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04008041 "ioremap failed for SLI4 HBA doorbell registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05008042 goto out_iounmap_ctrl;
8043 }
James Smartf5ca6f22013-09-06 12:21:09 -04008044 phba->pci_bar4_memmap_p = phba->sli4_hba.drbl_regs_memmap_p;
James Smart2fcee4b2010-12-15 17:57:46 -05008045 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
8046 if (error)
8047 goto out_iounmap_all;
James Smartda0436e2009-05-22 14:51:39 -04008048 }
8049
James Smartda0436e2009-05-22 14:51:39 -04008050 return 0;
8051
8052out_iounmap_all:
8053 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
8054out_iounmap_ctrl:
8055 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
8056out_iounmap_conf:
8057 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8058out:
8059 return error;
8060}
8061
8062/**
8063 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
8064 * @phba: pointer to lpfc hba data structure.
8065 *
8066 * This routine is invoked to unset the PCI device memory space for device
8067 * with SLI-4 interface spec.
8068 **/
8069static void
8070lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
8071{
James Smart2e90f4b2011-12-13 13:22:37 -05008072 uint32_t if_type;
8073 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smartda0436e2009-05-22 14:51:39 -04008074
James Smart2e90f4b2011-12-13 13:22:37 -05008075 switch (if_type) {
8076 case LPFC_SLI_INTF_IF_TYPE_0:
8077 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
8078 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
8079 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8080 break;
8081 case LPFC_SLI_INTF_IF_TYPE_2:
8082 iounmap(phba->sli4_hba.conf_regs_memmap_p);
8083 break;
8084 case LPFC_SLI_INTF_IF_TYPE_1:
8085 default:
8086 dev_printk(KERN_ERR, &phba->pcidev->dev,
8087 "FATAL - unsupported SLI4 interface type - %d\n",
8088 if_type);
8089 break;
8090 }
James Smartda0436e2009-05-22 14:51:39 -04008091}
8092
8093/**
James Smart3772a992009-05-22 14:50:54 -04008094 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
8095 * @phba: pointer to lpfc hba data structure.
8096 *
8097 * This routine is invoked to enable the MSI-X interrupt vectors to device
8098 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
8099 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
8100 * invoked, enables either all or nothing, depending on the current
8101 * availability of PCI vector resources. The device driver is responsible
8102 * for calling the individual request_irq() to register each MSI-X vector
8103 * with a interrupt handler, which is done in this function. Note that
8104 * later when device is unloading, the driver should always call free_irq()
8105 * on all MSI-X vectors it has done request_irq() on before calling
8106 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
8107 * will be left with MSI-X enabled and leaks its vectors.
8108 *
8109 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008110 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008111 * other values - error
8112 **/
8113static int
8114lpfc_sli_enable_msix(struct lpfc_hba *phba)
8115{
8116 int rc, i;
8117 LPFC_MBOXQ_t *pmb;
8118
8119 /* Set up MSI-X multi-message vectors */
8120 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8121 phba->msix_entries[i].entry = i;
8122
8123 /* Configure MSI-X capability structure */
8124 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
8125 ARRAY_SIZE(phba->msix_entries));
8126 if (rc) {
8127 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8128 "0420 PCI enable MSI-X failed (%d)\n", rc);
8129 goto msi_fail_out;
8130 }
8131 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8132 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8133 "0477 MSI-X entry[%d]: vector=x%x "
8134 "message=%d\n", i,
8135 phba->msix_entries[i].vector,
8136 phba->msix_entries[i].entry);
8137 /*
8138 * Assign MSI-X vectors to interrupt handlers
8139 */
8140
8141 /* vector-0 is associated to slow-path handler */
8142 rc = request_irq(phba->msix_entries[0].vector,
8143 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
8144 LPFC_SP_DRIVER_HANDLER_NAME, phba);
8145 if (rc) {
8146 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8147 "0421 MSI-X slow-path request_irq failed "
8148 "(%d)\n", rc);
8149 goto msi_fail_out;
8150 }
8151
8152 /* vector-1 is associated to fast-path handler */
8153 rc = request_irq(phba->msix_entries[1].vector,
8154 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
8155 LPFC_FP_DRIVER_HANDLER_NAME, phba);
8156
8157 if (rc) {
8158 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8159 "0429 MSI-X fast-path request_irq failed "
8160 "(%d)\n", rc);
8161 goto irq_fail_out;
8162 }
8163
8164 /*
8165 * Configure HBA MSI-X attention conditions to messages
8166 */
8167 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8168
8169 if (!pmb) {
8170 rc = -ENOMEM;
8171 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8172 "0474 Unable to allocate memory for issuing "
8173 "MBOX_CONFIG_MSI command\n");
8174 goto mem_fail_out;
8175 }
8176 rc = lpfc_config_msi(phba, pmb);
8177 if (rc)
8178 goto mbx_fail_out;
8179 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
8180 if (rc != MBX_SUCCESS) {
8181 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
8182 "0351 Config MSI mailbox command failed, "
8183 "mbxCmd x%x, mbxStatus x%x\n",
8184 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
8185 goto mbx_fail_out;
8186 }
8187
8188 /* Free memory allocated for mailbox command */
8189 mempool_free(pmb, phba->mbox_mem_pool);
8190 return rc;
8191
8192mbx_fail_out:
8193 /* Free memory allocated for mailbox command */
8194 mempool_free(pmb, phba->mbox_mem_pool);
8195
8196mem_fail_out:
8197 /* free the irq already requested */
8198 free_irq(phba->msix_entries[1].vector, phba);
8199
8200irq_fail_out:
8201 /* free the irq already requested */
8202 free_irq(phba->msix_entries[0].vector, phba);
8203
8204msi_fail_out:
8205 /* Unconfigure MSI-X capability structure */
8206 pci_disable_msix(phba->pcidev);
8207 return rc;
8208}
8209
8210/**
8211 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
8212 * @phba: pointer to lpfc hba data structure.
8213 *
8214 * This routine is invoked to release the MSI-X vectors and then disable the
8215 * MSI-X interrupt mode to device with SLI-3 interface spec.
8216 **/
8217static void
8218lpfc_sli_disable_msix(struct lpfc_hba *phba)
8219{
8220 int i;
8221
8222 /* Free up MSI-X multi-message vectors */
8223 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
8224 free_irq(phba->msix_entries[i].vector, phba);
8225 /* Disable MSI-X */
8226 pci_disable_msix(phba->pcidev);
8227
8228 return;
8229}
8230
8231/**
8232 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
8233 * @phba: pointer to lpfc hba data structure.
8234 *
8235 * This routine is invoked to enable the MSI interrupt mode to device with
8236 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
8237 * enable the MSI vector. The device driver is responsible for calling the
8238 * request_irq() to register MSI vector with a interrupt the handler, which
8239 * is done in this function.
8240 *
8241 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008242 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008243 * other values - error
8244 */
8245static int
8246lpfc_sli_enable_msi(struct lpfc_hba *phba)
8247{
8248 int rc;
8249
8250 rc = pci_enable_msi(phba->pcidev);
8251 if (!rc)
8252 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8253 "0462 PCI enable MSI mode success.\n");
8254 else {
8255 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8256 "0471 PCI enable MSI mode failed (%d)\n", rc);
8257 return rc;
8258 }
8259
8260 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
8261 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8262 if (rc) {
8263 pci_disable_msi(phba->pcidev);
8264 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8265 "0478 MSI request_irq failed (%d)\n", rc);
8266 }
8267 return rc;
8268}
8269
8270/**
8271 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
8272 * @phba: pointer to lpfc hba data structure.
8273 *
8274 * This routine is invoked to disable the MSI interrupt mode to device with
8275 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
8276 * done request_irq() on before calling pci_disable_msi(). Failure to do so
8277 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
8278 * its vector.
8279 */
8280static void
8281lpfc_sli_disable_msi(struct lpfc_hba *phba)
8282{
8283 free_irq(phba->pcidev->irq, phba);
8284 pci_disable_msi(phba->pcidev);
8285 return;
8286}
8287
8288/**
8289 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
8290 * @phba: pointer to lpfc hba data structure.
8291 *
8292 * This routine is invoked to enable device interrupt and associate driver's
8293 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
8294 * spec. Depends on the interrupt mode configured to the driver, the driver
8295 * will try to fallback from the configured interrupt mode to an interrupt
8296 * mode which is supported by the platform, kernel, and device in the order
8297 * of:
8298 * MSI-X -> MSI -> IRQ.
8299 *
8300 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008301 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04008302 * other values - error
8303 **/
8304static uint32_t
8305lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
8306{
8307 uint32_t intr_mode = LPFC_INTR_ERROR;
8308 int retval;
8309
8310 if (cfg_mode == 2) {
8311 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
8312 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
8313 if (!retval) {
8314 /* Now, try to enable MSI-X interrupt mode */
8315 retval = lpfc_sli_enable_msix(phba);
8316 if (!retval) {
8317 /* Indicate initialization to MSI-X mode */
8318 phba->intr_type = MSIX;
8319 intr_mode = 2;
8320 }
8321 }
8322 }
8323
8324 /* Fallback to MSI if MSI-X initialization failed */
8325 if (cfg_mode >= 1 && phba->intr_type == NONE) {
8326 retval = lpfc_sli_enable_msi(phba);
8327 if (!retval) {
8328 /* Indicate initialization to MSI mode */
8329 phba->intr_type = MSI;
8330 intr_mode = 1;
8331 }
8332 }
8333
8334 /* Fallback to INTx if both MSI-X/MSI initalization failed */
8335 if (phba->intr_type == NONE) {
8336 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
8337 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8338 if (!retval) {
8339 /* Indicate initialization to INTx mode */
8340 phba->intr_type = INTx;
8341 intr_mode = 0;
8342 }
8343 }
8344 return intr_mode;
8345}
8346
8347/**
8348 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
8349 * @phba: pointer to lpfc hba data structure.
8350 *
8351 * This routine is invoked to disable device interrupt and disassociate the
8352 * driver's interrupt handler(s) from interrupt vector(s) to device with
8353 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
8354 * release the interrupt vector(s) for the message signaled interrupt.
8355 **/
8356static void
8357lpfc_sli_disable_intr(struct lpfc_hba *phba)
8358{
8359 /* Disable the currently initialized interrupt mode */
8360 if (phba->intr_type == MSIX)
8361 lpfc_sli_disable_msix(phba);
8362 else if (phba->intr_type == MSI)
8363 lpfc_sli_disable_msi(phba);
8364 else if (phba->intr_type == INTx)
8365 free_irq(phba->pcidev->irq, phba);
8366
8367 /* Reset interrupt management states */
8368 phba->intr_type = NONE;
8369 phba->sli.slistat.sli_intr = 0;
8370
8371 return;
8372}
8373
8374/**
James Smart7bb03bb2013-04-17 20:19:16 -04008375 * lpfc_find_next_cpu - Find next available CPU that matches the phys_id
8376 * @phba: pointer to lpfc hba data structure.
8377 *
8378 * Find next available CPU to use for IRQ to CPU affinity.
8379 */
8380static int
8381lpfc_find_next_cpu(struct lpfc_hba *phba, uint32_t phys_id)
8382{
8383 struct lpfc_vector_map_info *cpup;
8384 int cpu;
8385
8386 cpup = phba->sli4_hba.cpu_map;
8387 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8388 /* CPU must be online */
8389 if (cpu_online(cpu)) {
8390 if ((cpup->irq == LPFC_VECTOR_MAP_EMPTY) &&
8391 (lpfc_used_cpu[cpu] == LPFC_VECTOR_MAP_EMPTY) &&
8392 (cpup->phys_id == phys_id)) {
8393 return cpu;
8394 }
8395 }
8396 cpup++;
8397 }
8398
8399 /*
8400 * If we get here, we have used ALL CPUs for the specific
8401 * phys_id. Now we need to clear out lpfc_used_cpu and start
8402 * reusing CPUs.
8403 */
8404
8405 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8406 if (lpfc_used_cpu[cpu] == phys_id)
8407 lpfc_used_cpu[cpu] = LPFC_VECTOR_MAP_EMPTY;
8408 }
8409
8410 cpup = phba->sli4_hba.cpu_map;
8411 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8412 /* CPU must be online */
8413 if (cpu_online(cpu)) {
8414 if ((cpup->irq == LPFC_VECTOR_MAP_EMPTY) &&
8415 (cpup->phys_id == phys_id)) {
8416 return cpu;
8417 }
8418 }
8419 cpup++;
8420 }
8421 return LPFC_VECTOR_MAP_EMPTY;
8422}
8423
8424/**
8425 * lpfc_sli4_set_affinity - Set affinity for HBA IRQ vectors
8426 * @phba: pointer to lpfc hba data structure.
8427 * @vectors: number of HBA vectors
8428 *
8429 * Affinitize MSIX IRQ vectors to CPUs. Try to equally spread vector
8430 * affinization across multple physical CPUs (numa nodes).
8431 * In addition, this routine will assign an IO channel for each CPU
8432 * to use when issuing I/Os.
8433 */
8434static int
8435lpfc_sli4_set_affinity(struct lpfc_hba *phba, int vectors)
8436{
8437 int i, idx, saved_chann, used_chann, cpu, phys_id;
James Smartec2087a2013-09-06 12:20:36 -04008438 int max_phys_id, min_phys_id;
8439 int num_io_channel, first_cpu, chan;
James Smart7bb03bb2013-04-17 20:19:16 -04008440 struct lpfc_vector_map_info *cpup;
8441#ifdef CONFIG_X86
8442 struct cpuinfo_x86 *cpuinfo;
8443#endif
8444 struct cpumask *mask;
8445 uint8_t chann[LPFC_FCP_IO_CHAN_MAX+1];
8446
8447 /* If there is no mapping, just return */
8448 if (!phba->cfg_fcp_cpu_map)
8449 return 1;
8450
8451 /* Init cpu_map array */
8452 memset(phba->sli4_hba.cpu_map, 0xff,
8453 (sizeof(struct lpfc_vector_map_info) *
8454 phba->sli4_hba.num_present_cpu));
8455
8456 max_phys_id = 0;
James Smartec2087a2013-09-06 12:20:36 -04008457 min_phys_id = 0xff;
James Smart7bb03bb2013-04-17 20:19:16 -04008458 phys_id = 0;
8459 num_io_channel = 0;
8460 first_cpu = LPFC_VECTOR_MAP_EMPTY;
8461
8462 /* Update CPU map with physical id and core id of each CPU */
8463 cpup = phba->sli4_hba.cpu_map;
8464 for (cpu = 0; cpu < phba->sli4_hba.num_present_cpu; cpu++) {
8465#ifdef CONFIG_X86
8466 cpuinfo = &cpu_data(cpu);
8467 cpup->phys_id = cpuinfo->phys_proc_id;
8468 cpup->core_id = cpuinfo->cpu_core_id;
8469#else
8470 /* No distinction between CPUs for other platforms */
8471 cpup->phys_id = 0;
8472 cpup->core_id = 0;
8473#endif
8474
8475 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8476 "3328 CPU physid %d coreid %d\n",
8477 cpup->phys_id, cpup->core_id);
8478
8479 if (cpup->phys_id > max_phys_id)
8480 max_phys_id = cpup->phys_id;
James Smartec2087a2013-09-06 12:20:36 -04008481 if (cpup->phys_id < min_phys_id)
8482 min_phys_id = cpup->phys_id;
James Smart7bb03bb2013-04-17 20:19:16 -04008483 cpup++;
8484 }
8485
James Smartec2087a2013-09-06 12:20:36 -04008486 phys_id = min_phys_id;
James Smart7bb03bb2013-04-17 20:19:16 -04008487 /* Now associate the HBA vectors with specific CPUs */
8488 for (idx = 0; idx < vectors; idx++) {
8489 cpup = phba->sli4_hba.cpu_map;
8490 cpu = lpfc_find_next_cpu(phba, phys_id);
8491 if (cpu == LPFC_VECTOR_MAP_EMPTY) {
8492
8493 /* Try for all phys_id's */
8494 for (i = 1; i < max_phys_id; i++) {
8495 phys_id++;
8496 if (phys_id > max_phys_id)
James Smartec2087a2013-09-06 12:20:36 -04008497 phys_id = min_phys_id;
James Smart7bb03bb2013-04-17 20:19:16 -04008498 cpu = lpfc_find_next_cpu(phba, phys_id);
8499 if (cpu == LPFC_VECTOR_MAP_EMPTY)
8500 continue;
8501 goto found;
8502 }
8503
James Smartec2087a2013-09-06 12:20:36 -04008504 /* Use round robin for scheduling */
8505 phba->cfg_fcp_io_sched = LPFC_FCP_SCHED_ROUND_ROBIN;
8506 chan = 0;
8507 cpup = phba->sli4_hba.cpu_map;
8508 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
8509 cpup->channel_id = chan;
8510 cpup++;
8511 chan++;
8512 if (chan >= phba->cfg_fcp_io_channel)
8513 chan = 0;
8514 }
8515
James Smart7bb03bb2013-04-17 20:19:16 -04008516 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8517 "3329 Cannot set affinity:"
8518 "Error mapping vector %d (%d)\n",
8519 idx, vectors);
8520 return 0;
8521 }
8522found:
8523 cpup += cpu;
8524 if (phba->cfg_fcp_cpu_map == LPFC_DRIVER_CPU_MAP)
8525 lpfc_used_cpu[cpu] = phys_id;
8526
8527 /* Associate vector with selected CPU */
8528 cpup->irq = phba->sli4_hba.msix_entries[idx].vector;
8529
8530 /* Associate IO channel with selected CPU */
8531 cpup->channel_id = idx;
8532 num_io_channel++;
8533
8534 if (first_cpu == LPFC_VECTOR_MAP_EMPTY)
8535 first_cpu = cpu;
8536
8537 /* Now affinitize to the selected CPU */
8538 mask = &cpup->maskbits;
8539 cpumask_clear(mask);
8540 cpumask_set_cpu(cpu, mask);
8541 i = irq_set_affinity_hint(phba->sli4_hba.msix_entries[idx].
8542 vector, mask);
8543
8544 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8545 "3330 Set Affinity: CPU %d channel %d "
8546 "irq %d (%x)\n",
8547 cpu, cpup->channel_id,
8548 phba->sli4_hba.msix_entries[idx].vector, i);
8549
8550 /* Spread vector mapping across multple physical CPU nodes */
8551 phys_id++;
8552 if (phys_id > max_phys_id)
James Smartec2087a2013-09-06 12:20:36 -04008553 phys_id = min_phys_id;
James Smart7bb03bb2013-04-17 20:19:16 -04008554 }
8555
8556 /*
8557 * Finally fill in the IO channel for any remaining CPUs.
8558 * At this point, all IO channels have been assigned to a specific
8559 * MSIx vector, mapped to a specific CPU.
8560 * Base the remaining IO channel assigned, to IO channels already
8561 * assigned to other CPUs on the same phys_id.
8562 */
James Smartec2087a2013-09-06 12:20:36 -04008563 for (i = min_phys_id; i <= max_phys_id; i++) {
James Smart7bb03bb2013-04-17 20:19:16 -04008564 /*
8565 * If there are no io channels already mapped to
8566 * this phys_id, just round robin thru the io_channels.
8567 * Setup chann[] for round robin.
8568 */
8569 for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++)
8570 chann[idx] = idx;
8571
8572 saved_chann = 0;
8573 used_chann = 0;
8574
8575 /*
8576 * First build a list of IO channels already assigned
8577 * to this phys_id before reassigning the same IO
8578 * channels to the remaining CPUs.
8579 */
8580 cpup = phba->sli4_hba.cpu_map;
8581 cpu = first_cpu;
8582 cpup += cpu;
8583 for (idx = 0; idx < phba->sli4_hba.num_present_cpu;
8584 idx++) {
8585 if (cpup->phys_id == i) {
8586 /*
8587 * Save any IO channels that are
8588 * already mapped to this phys_id.
8589 */
8590 if (cpup->irq != LPFC_VECTOR_MAP_EMPTY) {
8591 chann[saved_chann] =
8592 cpup->channel_id;
8593 saved_chann++;
8594 goto out;
8595 }
8596
8597 /* See if we are using round-robin */
8598 if (saved_chann == 0)
8599 saved_chann =
8600 phba->cfg_fcp_io_channel;
8601
8602 /* Associate next IO channel with CPU */
8603 cpup->channel_id = chann[used_chann];
8604 num_io_channel++;
8605 used_chann++;
8606 if (used_chann == saved_chann)
8607 used_chann = 0;
8608
8609 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8610 "3331 Set IO_CHANN "
8611 "CPU %d channel %d\n",
8612 idx, cpup->channel_id);
8613 }
8614out:
8615 cpu++;
8616 if (cpu >= phba->sli4_hba.num_present_cpu) {
8617 cpup = phba->sli4_hba.cpu_map;
8618 cpu = 0;
8619 } else {
8620 cpup++;
8621 }
8622 }
8623 }
8624
8625 if (phba->sli4_hba.num_online_cpu != phba->sli4_hba.num_present_cpu) {
8626 cpup = phba->sli4_hba.cpu_map;
8627 for (idx = 0; idx < phba->sli4_hba.num_present_cpu; idx++) {
8628 if (cpup->channel_id == LPFC_VECTOR_MAP_EMPTY) {
8629 cpup->channel_id = 0;
8630 num_io_channel++;
8631
8632 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8633 "3332 Assign IO_CHANN "
8634 "CPU %d channel %d\n",
8635 idx, cpup->channel_id);
8636 }
8637 cpup++;
8638 }
8639 }
8640
8641 /* Sanity check */
8642 if (num_io_channel != phba->sli4_hba.num_present_cpu)
8643 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8644 "3333 Set affinity mismatch:"
James Smartec2087a2013-09-06 12:20:36 -04008645 "%d chann != %d cpus: %d vectors\n",
James Smart7bb03bb2013-04-17 20:19:16 -04008646 num_io_channel, phba->sli4_hba.num_present_cpu,
8647 vectors);
8648
James Smartec2087a2013-09-06 12:20:36 -04008649 /* Enable using cpu affinity for scheduling */
James Smart7bb03bb2013-04-17 20:19:16 -04008650 phba->cfg_fcp_io_sched = LPFC_FCP_SCHED_BY_CPU;
8651 return 1;
8652}
8653
8654
8655/**
James Smartda0436e2009-05-22 14:51:39 -04008656 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
8657 * @phba: pointer to lpfc hba data structure.
8658 *
8659 * This routine is invoked to enable the MSI-X interrupt vectors to device
8660 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
8661 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
8662 * enables either all or nothing, depending on the current availability of
8663 * PCI vector resources. The device driver is responsible for calling the
8664 * individual request_irq() to register each MSI-X vector with a interrupt
8665 * handler, which is done in this function. Note that later when device is
8666 * unloading, the driver should always call free_irq() on all MSI-X vectors
8667 * it has done request_irq() on before calling pci_disable_msix(). Failure
8668 * to do so results in a BUG_ON() and a device will be left with MSI-X
8669 * enabled and leaks its vectors.
8670 *
8671 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008672 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008673 * other values - error
8674 **/
8675static int
8676lpfc_sli4_enable_msix(struct lpfc_hba *phba)
8677{
James Smart75baf692010-06-08 18:31:21 -04008678 int vectors, rc, index;
James Smartda0436e2009-05-22 14:51:39 -04008679
8680 /* Set up MSI-X multi-message vectors */
James Smart82c3e9b2012-09-29 11:29:50 -04008681 for (index = 0; index < phba->cfg_fcp_io_channel; index++)
James Smartda0436e2009-05-22 14:51:39 -04008682 phba->sli4_hba.msix_entries[index].entry = index;
8683
8684 /* Configure MSI-X capability structure */
James Smart82c3e9b2012-09-29 11:29:50 -04008685 vectors = phba->cfg_fcp_io_channel;
James Smart1ba981f2014-02-20 09:56:45 -05008686 if (phba->cfg_fof) {
8687 phba->sli4_hba.msix_entries[index].entry = index;
8688 vectors++;
8689 }
James Smart75baf692010-06-08 18:31:21 -04008690enable_msix_vectors:
James Smartda0436e2009-05-22 14:51:39 -04008691 rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
James Smart75baf692010-06-08 18:31:21 -04008692 vectors);
8693 if (rc > 1) {
8694 vectors = rc;
8695 goto enable_msix_vectors;
8696 } else if (rc) {
James Smartda0436e2009-05-22 14:51:39 -04008697 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8698 "0484 PCI enable MSI-X failed (%d)\n", rc);
8699 goto msi_fail_out;
8700 }
James Smart75baf692010-06-08 18:31:21 -04008701
James Smartda0436e2009-05-22 14:51:39 -04008702 /* Log MSI-X vector assignment */
James Smart75baf692010-06-08 18:31:21 -04008703 for (index = 0; index < vectors; index++)
James Smartda0436e2009-05-22 14:51:39 -04008704 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8705 "0489 MSI-X entry[%d]: vector=x%x "
8706 "message=%d\n", index,
8707 phba->sli4_hba.msix_entries[index].vector,
8708 phba->sli4_hba.msix_entries[index].entry);
James Smart67d12732012-08-03 12:36:13 -04008709
James Smart7bb03bb2013-04-17 20:19:16 -04008710 /* Assign MSI-X vectors to interrupt handlers */
James Smart67d12732012-08-03 12:36:13 -04008711 for (index = 0; index < vectors; index++) {
James Smart4305f182012-08-03 12:36:33 -04008712 memset(&phba->sli4_hba.handler_name[index], 0, 16);
8713 sprintf((char *)&phba->sli4_hba.handler_name[index],
8714 LPFC_DRIVER_HANDLER_NAME"%d", index);
James Smartda0436e2009-05-22 14:51:39 -04008715
James Smart67d12732012-08-03 12:36:13 -04008716 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8717 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
James Smartba20c852012-08-03 12:36:52 -04008718 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].fcp_eq_in_use, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008719 if (phba->cfg_fof && (index == (vectors - 1)))
8720 rc = request_irq(
8721 phba->sli4_hba.msix_entries[index].vector,
8722 &lpfc_sli4_fof_intr_handler, IRQF_SHARED,
8723 (char *)&phba->sli4_hba.handler_name[index],
8724 &phba->sli4_hba.fcp_eq_hdl[index]);
8725 else
8726 rc = request_irq(
8727 phba->sli4_hba.msix_entries[index].vector,
James Smart67d12732012-08-03 12:36:13 -04008728 &lpfc_sli4_hba_intr_handler, IRQF_SHARED,
James Smart4305f182012-08-03 12:36:33 -04008729 (char *)&phba->sli4_hba.handler_name[index],
James Smart67d12732012-08-03 12:36:13 -04008730 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartda0436e2009-05-22 14:51:39 -04008731 if (rc) {
8732 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8733 "0486 MSI-X fast-path (%d) "
8734 "request_irq failed (%d)\n", index, rc);
8735 goto cfg_fail_out;
8736 }
8737 }
8738
James Smart1ba981f2014-02-20 09:56:45 -05008739 if (phba->cfg_fof)
8740 vectors--;
8741
James Smart82c3e9b2012-09-29 11:29:50 -04008742 if (vectors != phba->cfg_fcp_io_channel) {
8743 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8744 "3238 Reducing IO channels to match number of "
8745 "MSI-X vectors, requested %d got %d\n",
8746 phba->cfg_fcp_io_channel, vectors);
8747 phba->cfg_fcp_io_channel = vectors;
8748 }
James Smart7bb03bb2013-04-17 20:19:16 -04008749
8750 lpfc_sli4_set_affinity(phba, vectors);
James Smartda0436e2009-05-22 14:51:39 -04008751 return rc;
8752
8753cfg_fail_out:
8754 /* free the irq already requested */
James Smartacbd8612013-09-06 12:21:19 -04008755 for (--index; index >= 0; index--) {
8756 irq_set_affinity_hint(phba->sli4_hba.msix_entries[index].
8757 vector, NULL);
James Smart67d12732012-08-03 12:36:13 -04008758 free_irq(phba->sli4_hba.msix_entries[index].vector,
8759 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartacbd8612013-09-06 12:21:19 -04008760 }
James Smartda0436e2009-05-22 14:51:39 -04008761
8762msi_fail_out:
8763 /* Unconfigure MSI-X capability structure */
8764 pci_disable_msix(phba->pcidev);
8765 return rc;
8766}
8767
8768/**
8769 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
8770 * @phba: pointer to lpfc hba data structure.
8771 *
8772 * This routine is invoked to release the MSI-X vectors and then disable the
8773 * MSI-X interrupt mode to device with SLI-4 interface spec.
8774 **/
8775static void
8776lpfc_sli4_disable_msix(struct lpfc_hba *phba)
8777{
8778 int index;
8779
8780 /* Free up MSI-X multi-message vectors */
James Smartacbd8612013-09-06 12:21:19 -04008781 for (index = 0; index < phba->cfg_fcp_io_channel; index++) {
8782 irq_set_affinity_hint(phba->sli4_hba.msix_entries[index].
8783 vector, NULL);
James Smartda0436e2009-05-22 14:51:39 -04008784 free_irq(phba->sli4_hba.msix_entries[index].vector,
James Smart67d12732012-08-03 12:36:13 -04008785 &phba->sli4_hba.fcp_eq_hdl[index]);
James Smartacbd8612013-09-06 12:21:19 -04008786 }
James Smart1ba981f2014-02-20 09:56:45 -05008787 if (phba->cfg_fof) {
8788 free_irq(phba->sli4_hba.msix_entries[index].vector,
8789 &phba->sli4_hba.fcp_eq_hdl[index]);
8790 }
James Smartda0436e2009-05-22 14:51:39 -04008791 /* Disable MSI-X */
8792 pci_disable_msix(phba->pcidev);
8793
8794 return;
8795}
8796
8797/**
8798 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
8799 * @phba: pointer to lpfc hba data structure.
8800 *
8801 * This routine is invoked to enable the MSI interrupt mode to device with
8802 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
8803 * to enable the MSI vector. The device driver is responsible for calling
8804 * the request_irq() to register MSI vector with a interrupt the handler,
8805 * which is done in this function.
8806 *
8807 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008808 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008809 * other values - error
8810 **/
8811static int
8812lpfc_sli4_enable_msi(struct lpfc_hba *phba)
8813{
8814 int rc, index;
8815
8816 rc = pci_enable_msi(phba->pcidev);
8817 if (!rc)
8818 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8819 "0487 PCI enable MSI mode success.\n");
8820 else {
8821 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8822 "0488 PCI enable MSI mode failed (%d)\n", rc);
8823 return rc;
8824 }
8825
8826 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
8827 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8828 if (rc) {
8829 pci_disable_msi(phba->pcidev);
8830 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
8831 "0490 MSI request_irq failed (%d)\n", rc);
James Smart75baf692010-06-08 18:31:21 -04008832 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008833 }
8834
James Smart67d12732012-08-03 12:36:13 -04008835 for (index = 0; index < phba->cfg_fcp_io_channel; index++) {
James Smartda0436e2009-05-22 14:51:39 -04008836 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8837 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
8838 }
8839
James Smart1ba981f2014-02-20 09:56:45 -05008840 if (phba->cfg_fof) {
8841 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8842 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
8843 }
James Smart75baf692010-06-08 18:31:21 -04008844 return 0;
James Smartda0436e2009-05-22 14:51:39 -04008845}
8846
8847/**
8848 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
8849 * @phba: pointer to lpfc hba data structure.
8850 *
8851 * This routine is invoked to disable the MSI interrupt mode to device with
8852 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
8853 * done request_irq() on before calling pci_disable_msi(). Failure to do so
8854 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
8855 * its vector.
8856 **/
8857static void
8858lpfc_sli4_disable_msi(struct lpfc_hba *phba)
8859{
8860 free_irq(phba->pcidev->irq, phba);
8861 pci_disable_msi(phba->pcidev);
8862 return;
8863}
8864
8865/**
8866 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
8867 * @phba: pointer to lpfc hba data structure.
8868 *
8869 * This routine is invoked to enable device interrupt and associate driver's
8870 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
8871 * interface spec. Depends on the interrupt mode configured to the driver,
8872 * the driver will try to fallback from the configured interrupt mode to an
8873 * interrupt mode which is supported by the platform, kernel, and device in
8874 * the order of:
8875 * MSI-X -> MSI -> IRQ.
8876 *
8877 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02008878 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04008879 * other values - error
8880 **/
8881static uint32_t
8882lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
8883{
8884 uint32_t intr_mode = LPFC_INTR_ERROR;
8885 int retval, index;
8886
8887 if (cfg_mode == 2) {
8888 /* Preparation before conf_msi mbox cmd */
8889 retval = 0;
8890 if (!retval) {
8891 /* Now, try to enable MSI-X interrupt mode */
8892 retval = lpfc_sli4_enable_msix(phba);
8893 if (!retval) {
8894 /* Indicate initialization to MSI-X mode */
8895 phba->intr_type = MSIX;
8896 intr_mode = 2;
8897 }
8898 }
8899 }
8900
8901 /* Fallback to MSI if MSI-X initialization failed */
8902 if (cfg_mode >= 1 && phba->intr_type == NONE) {
8903 retval = lpfc_sli4_enable_msi(phba);
8904 if (!retval) {
8905 /* Indicate initialization to MSI mode */
8906 phba->intr_type = MSI;
8907 intr_mode = 1;
8908 }
8909 }
8910
8911 /* Fallback to INTx if both MSI-X/MSI initalization failed */
8912 if (phba->intr_type == NONE) {
8913 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
8914 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
8915 if (!retval) {
8916 /* Indicate initialization to INTx mode */
8917 phba->intr_type = INTx;
8918 intr_mode = 0;
James Smart67d12732012-08-03 12:36:13 -04008919 for (index = 0; index < phba->cfg_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -04008920 index++) {
8921 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8922 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
James Smartba20c852012-08-03 12:36:52 -04008923 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].
8924 fcp_eq_in_use, 1);
James Smartda0436e2009-05-22 14:51:39 -04008925 }
James Smart1ba981f2014-02-20 09:56:45 -05008926 if (phba->cfg_fof) {
8927 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
8928 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
8929 atomic_set(&phba->sli4_hba.fcp_eq_hdl[index].
8930 fcp_eq_in_use, 1);
8931 }
James Smartda0436e2009-05-22 14:51:39 -04008932 }
8933 }
8934 return intr_mode;
8935}
8936
8937/**
8938 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
8939 * @phba: pointer to lpfc hba data structure.
8940 *
8941 * This routine is invoked to disable device interrupt and disassociate
8942 * the driver's interrupt handler(s) from interrupt vector(s) to device
8943 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
8944 * will release the interrupt vector(s) for the message signaled interrupt.
8945 **/
8946static void
8947lpfc_sli4_disable_intr(struct lpfc_hba *phba)
8948{
8949 /* Disable the currently initialized interrupt mode */
8950 if (phba->intr_type == MSIX)
8951 lpfc_sli4_disable_msix(phba);
8952 else if (phba->intr_type == MSI)
8953 lpfc_sli4_disable_msi(phba);
8954 else if (phba->intr_type == INTx)
8955 free_irq(phba->pcidev->irq, phba);
8956
8957 /* Reset interrupt management states */
8958 phba->intr_type = NONE;
8959 phba->sli.slistat.sli_intr = 0;
8960
8961 return;
8962}
8963
8964/**
James Smart3772a992009-05-22 14:50:54 -04008965 * lpfc_unset_hba - Unset SLI3 hba device initialization
8966 * @phba: pointer to lpfc hba data structure.
8967 *
8968 * This routine is invoked to unset the HBA device initialization steps to
8969 * a device with SLI-3 interface spec.
8970 **/
8971static void
8972lpfc_unset_hba(struct lpfc_hba *phba)
8973{
8974 struct lpfc_vport *vport = phba->pport;
8975 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
8976
8977 spin_lock_irq(shost->host_lock);
8978 vport->load_flag |= FC_UNLOADING;
8979 spin_unlock_irq(shost->host_lock);
8980
James Smart72859902012-01-18 16:25:38 -05008981 kfree(phba->vpi_bmask);
8982 kfree(phba->vpi_ids);
8983
James Smart3772a992009-05-22 14:50:54 -04008984 lpfc_stop_hba_timers(phba);
8985
8986 phba->pport->work_port_events = 0;
8987
8988 lpfc_sli_hba_down(phba);
8989
8990 lpfc_sli_brdrestart(phba);
8991
8992 lpfc_sli_disable_intr(phba);
8993
8994 return;
8995}
8996
8997/**
James Smart5af5eee2010-10-22 11:06:38 -04008998 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
8999 * @phba: Pointer to HBA context object.
9000 *
9001 * This function is called in the SLI4 code path to wait for completion
9002 * of device's XRIs exchange busy. It will check the XRI exchange busy
9003 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
9004 * that, it will check the XRI exchange busy on outstanding FCP and ELS
9005 * I/Os every 30 seconds, log error message, and wait forever. Only when
9006 * all XRI exchange busy complete, the driver unload shall proceed with
9007 * invoking the function reset ioctl mailbox command to the CNA and the
9008 * the rest of the driver unload resource release.
9009 **/
9010static void
9011lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
9012{
9013 int wait_time = 0;
9014 int fcp_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
9015 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
9016
9017 while (!fcp_xri_cmpl || !els_xri_cmpl) {
9018 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
9019 if (!fcp_xri_cmpl)
9020 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9021 "2877 FCP XRI exchange busy "
9022 "wait time: %d seconds.\n",
9023 wait_time/1000);
9024 if (!els_xri_cmpl)
9025 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9026 "2878 ELS XRI exchange busy "
9027 "wait time: %d seconds.\n",
9028 wait_time/1000);
9029 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
9030 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
9031 } else {
9032 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
9033 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
9034 }
9035 fcp_xri_cmpl =
9036 list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
9037 els_xri_cmpl =
9038 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
9039 }
9040}
9041
9042/**
James Smartda0436e2009-05-22 14:51:39 -04009043 * lpfc_sli4_hba_unset - Unset the fcoe hba
9044 * @phba: Pointer to HBA context object.
9045 *
9046 * This function is called in the SLI4 code path to reset the HBA's FCoE
9047 * function. The caller is not required to hold any lock. This routine
9048 * issues PCI function reset mailbox command to reset the FCoE function.
9049 * At the end of the function, it calls lpfc_hba_down_post function to
9050 * free any pending commands.
9051 **/
9052static void
9053lpfc_sli4_hba_unset(struct lpfc_hba *phba)
9054{
9055 int wait_cnt = 0;
9056 LPFC_MBOXQ_t *mboxq;
James Smart912e3ac2011-05-24 11:42:11 -04009057 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -04009058
9059 lpfc_stop_hba_timers(phba);
9060 phba->sli4_hba.intr_enable = 0;
9061
9062 /*
9063 * Gracefully wait out the potential current outstanding asynchronous
9064 * mailbox command.
9065 */
9066
9067 /* First, block any pending async mailbox command from posted */
9068 spin_lock_irq(&phba->hbalock);
9069 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
9070 spin_unlock_irq(&phba->hbalock);
9071 /* Now, trying to wait it out if we can */
9072 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9073 msleep(10);
9074 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
9075 break;
9076 }
9077 /* Forcefully release the outstanding mailbox command if timed out */
9078 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9079 spin_lock_irq(&phba->hbalock);
9080 mboxq = phba->sli.mbox_active;
9081 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
9082 __lpfc_mbox_cmpl_put(phba, mboxq);
9083 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9084 phba->sli.mbox_active = NULL;
9085 spin_unlock_irq(&phba->hbalock);
9086 }
9087
James Smart5af5eee2010-10-22 11:06:38 -04009088 /* Abort all iocbs associated with the hba */
9089 lpfc_sli_hba_iocb_abort(phba);
9090
9091 /* Wait for completion of device XRI exchange busy */
9092 lpfc_sli4_xri_exchange_busy_wait(phba);
9093
James Smartda0436e2009-05-22 14:51:39 -04009094 /* Disable PCI subsystem interrupt */
9095 lpfc_sli4_disable_intr(phba);
9096
James Smart912e3ac2011-05-24 11:42:11 -04009097 /* Disable SR-IOV if enabled */
9098 if (phba->cfg_sriov_nr_virtfn)
9099 pci_disable_sriov(pdev);
9100
James Smartda0436e2009-05-22 14:51:39 -04009101 /* Stop kthread signal shall trigger work_done one more time */
9102 kthread_stop(phba->worker_thread);
9103
James Smart3677a3a2010-09-29 11:19:14 -04009104 /* Reset SLI4 HBA FCoE function */
9105 lpfc_pci_function_reset(phba);
James Smart5350d872011-10-10 21:33:49 -04009106 lpfc_sli4_queue_destroy(phba);
James Smart3677a3a2010-09-29 11:19:14 -04009107
James Smartda0436e2009-05-22 14:51:39 -04009108 /* Stop the SLI4 device port */
9109 phba->pport->work_port_events = 0;
9110}
9111
James Smart28baac72010-02-12 14:42:03 -05009112 /**
9113 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
9114 * @phba: Pointer to HBA context object.
9115 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
9116 *
9117 * This function is called in the SLI4 code path to read the port's
9118 * sli4 capabilities.
9119 *
9120 * This function may be be called from any context that can block-wait
9121 * for the completion. The expectation is that this routine is called
9122 * typically from probe_one or from the online routine.
9123 **/
9124int
9125lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9126{
9127 int rc;
9128 struct lpfc_mqe *mqe;
9129 struct lpfc_pc_sli4_params *sli4_params;
9130 uint32_t mbox_tmo;
9131
9132 rc = 0;
9133 mqe = &mboxq->u.mqe;
9134
9135 /* Read the port's SLI4 Parameters port capabilities */
James Smartfedd3b72011-02-16 12:39:24 -05009136 lpfc_pc_sli4_params(mboxq);
James Smart28baac72010-02-12 14:42:03 -05009137 if (!phba->sli4_hba.intr_enable)
9138 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9139 else {
James Smarta183a152011-10-10 21:32:43 -04009140 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart28baac72010-02-12 14:42:03 -05009141 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
9142 }
9143
9144 if (unlikely(rc))
9145 return 1;
9146
9147 sli4_params = &phba->sli4_hba.pc_sli4_params;
9148 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
9149 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
9150 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
9151 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
9152 &mqe->un.sli4_params);
9153 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
9154 &mqe->un.sli4_params);
9155 sli4_params->proto_types = mqe->un.sli4_params.word3;
9156 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
9157 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
9158 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
9159 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
9160 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
9161 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
9162 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
9163 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
9164 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
9165 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
9166 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
9167 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
9168 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
9169 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
9170 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
9171 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
9172 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
9173 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
9174 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
9175 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
James Smart05580562011-05-24 11:40:48 -04009176
9177 /* Make sure that sge_supp_len can be handled by the driver */
9178 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
9179 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
9180
James Smart28baac72010-02-12 14:42:03 -05009181 return rc;
9182}
9183
James Smartda0436e2009-05-22 14:51:39 -04009184/**
James Smartfedd3b72011-02-16 12:39:24 -05009185 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
9186 * @phba: Pointer to HBA context object.
9187 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
9188 *
9189 * This function is called in the SLI4 code path to read the port's
9190 * sli4 capabilities.
9191 *
9192 * This function may be be called from any context that can block-wait
9193 * for the completion. The expectation is that this routine is called
9194 * typically from probe_one or from the online routine.
9195 **/
9196int
9197lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9198{
9199 int rc;
9200 struct lpfc_mqe *mqe = &mboxq->u.mqe;
9201 struct lpfc_pc_sli4_params *sli4_params;
James Smarta183a152011-10-10 21:32:43 -04009202 uint32_t mbox_tmo;
James Smartfedd3b72011-02-16 12:39:24 -05009203 int length;
9204 struct lpfc_sli4_parameters *mbx_sli4_parameters;
9205
James Smart6d368e52011-05-24 11:44:12 -04009206 /*
9207 * By default, the driver assumes the SLI4 port requires RPI
9208 * header postings. The SLI4_PARAM response will correct this
9209 * assumption.
9210 */
9211 phba->sli4_hba.rpi_hdrs_in_use = 1;
9212
James Smartfedd3b72011-02-16 12:39:24 -05009213 /* Read the port's SLI4 Config Parameters */
9214 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
9215 sizeof(struct lpfc_sli4_cfg_mhdr));
9216 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
9217 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
9218 length, LPFC_SLI4_MBX_EMBED);
9219 if (!phba->sli4_hba.intr_enable)
9220 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -04009221 else {
9222 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
9223 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
9224 }
James Smartfedd3b72011-02-16 12:39:24 -05009225 if (unlikely(rc))
9226 return rc;
9227 sli4_params = &phba->sli4_hba.pc_sli4_params;
9228 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
9229 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
9230 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
9231 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
9232 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
9233 mbx_sli4_parameters);
9234 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
9235 mbx_sli4_parameters);
9236 if (bf_get(cfg_phwq, mbx_sli4_parameters))
9237 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
9238 else
9239 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
9240 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
9241 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
James Smart1ba981f2014-02-20 09:56:45 -05009242 sli4_params->oas_supported = bf_get(cfg_oas, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -05009243 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
9244 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
9245 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
9246 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
James Smart0c651872013-07-15 18:33:23 -04009247 sli4_params->wqsize = bf_get(cfg_wqsize, mbx_sli4_parameters);
James Smartfedd3b72011-02-16 12:39:24 -05009248 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
9249 mbx_sli4_parameters);
9250 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
9251 mbx_sli4_parameters);
James Smart6d368e52011-05-24 11:44:12 -04009252 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
9253 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
James Smart05580562011-05-24 11:40:48 -04009254
9255 /* Make sure that sge_supp_len can be handled by the driver */
9256 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
9257 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
9258
James Smartfedd3b72011-02-16 12:39:24 -05009259 return 0;
9260}
9261
9262/**
James Smart3772a992009-05-22 14:50:54 -04009263 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
9264 * @pdev: pointer to PCI device
9265 * @pid: pointer to PCI device identifier
9266 *
9267 * This routine is to be called to attach a device with SLI-3 interface spec
9268 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
9269 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
9270 * information of the device and driver to see if the driver state that it can
9271 * support this kind of device. If the match is successful, the driver core
9272 * invokes this routine. If this routine determines it can claim the HBA, it
9273 * does all the initialization that it needs to do to handle the HBA properly.
9274 *
9275 * Return code
9276 * 0 - driver can claim the device
9277 * negative value - driver can not claim the device
9278 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009279static int
James Smart3772a992009-05-22 14:50:54 -04009280lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
9281{
9282 struct lpfc_hba *phba;
9283 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04009284 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -04009285 int error;
9286 uint32_t cfg_mode, intr_mode;
9287
9288 /* Allocate memory for HBA structure */
9289 phba = lpfc_hba_alloc(pdev);
9290 if (!phba)
9291 return -ENOMEM;
9292
9293 /* Perform generic PCI device enabling operation */
9294 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -04009295 if (error)
James Smart3772a992009-05-22 14:50:54 -04009296 goto out_free_phba;
James Smart3772a992009-05-22 14:50:54 -04009297
9298 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
9299 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
9300 if (error)
9301 goto out_disable_pci_dev;
9302
9303 /* Set up SLI-3 specific device PCI memory space */
9304 error = lpfc_sli_pci_mem_setup(phba);
9305 if (error) {
9306 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9307 "1402 Failed to set up pci memory space.\n");
9308 goto out_disable_pci_dev;
9309 }
9310
9311 /* Set up phase-1 common device driver resources */
9312 error = lpfc_setup_driver_resource_phase1(phba);
9313 if (error) {
9314 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9315 "1403 Failed to set up driver resource.\n");
9316 goto out_unset_pci_mem_s3;
9317 }
9318
9319 /* Set up SLI-3 specific device driver resources */
9320 error = lpfc_sli_driver_resource_setup(phba);
9321 if (error) {
9322 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9323 "1404 Failed to set up driver resource.\n");
9324 goto out_unset_pci_mem_s3;
9325 }
9326
9327 /* Initialize and populate the iocb list per host */
9328 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
9329 if (error) {
9330 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9331 "1405 Failed to initialize iocb list.\n");
9332 goto out_unset_driver_resource_s3;
9333 }
9334
9335 /* Set up common device driver resources */
9336 error = lpfc_setup_driver_resource_phase2(phba);
9337 if (error) {
9338 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9339 "1406 Failed to set up driver resource.\n");
9340 goto out_free_iocb_list;
9341 }
9342
James Smart079b5c92011-08-21 21:48:49 -04009343 /* Get the default values for Model Name and Description */
9344 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
9345
James Smart3772a992009-05-22 14:50:54 -04009346 /* Create SCSI host to the physical port */
9347 error = lpfc_create_shost(phba);
9348 if (error) {
9349 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9350 "1407 Failed to create scsi host.\n");
9351 goto out_unset_driver_resource;
9352 }
9353
9354 /* Configure sysfs attributes */
9355 vport = phba->pport;
9356 error = lpfc_alloc_sysfs_attr(vport);
9357 if (error) {
9358 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9359 "1476 Failed to allocate sysfs attr\n");
9360 goto out_destroy_shost;
9361 }
9362
James Smart6669f9b2009-10-02 15:16:45 -04009363 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -04009364 /* Now, trying to enable interrupt and bring up the device */
9365 cfg_mode = phba->cfg_use_msi;
9366 while (true) {
9367 /* Put device to a known state before enabling interrupt */
9368 lpfc_stop_port(phba);
9369 /* Configure and enable interrupt */
9370 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
9371 if (intr_mode == LPFC_INTR_ERROR) {
9372 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9373 "0431 Failed to enable interrupt.\n");
9374 error = -ENODEV;
9375 goto out_free_sysfs_attr;
9376 }
9377 /* SLI-3 HBA setup */
9378 if (lpfc_sli_hba_setup(phba)) {
9379 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9380 "1477 Failed to set up hba\n");
9381 error = -ENODEV;
9382 goto out_remove_device;
9383 }
9384
9385 /* Wait 50ms for the interrupts of previous mailbox commands */
9386 msleep(50);
9387 /* Check active interrupts on message signaled interrupts */
9388 if (intr_mode == 0 ||
9389 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
9390 /* Log the current active interrupt mode */
9391 phba->intr_mode = intr_mode;
9392 lpfc_log_intr_mode(phba, intr_mode);
9393 break;
9394 } else {
9395 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9396 "0447 Configure interrupt mode (%d) "
9397 "failed active interrupt test.\n",
9398 intr_mode);
9399 /* Disable the current interrupt mode */
9400 lpfc_sli_disable_intr(phba);
9401 /* Try next level of interrupt mode */
9402 cfg_mode = --intr_mode;
9403 }
9404 }
9405
9406 /* Perform post initialization setup */
9407 lpfc_post_init_setup(phba);
9408
9409 /* Check if there are static vports to be created. */
9410 lpfc_create_static_vport(phba);
9411
9412 return 0;
9413
9414out_remove_device:
9415 lpfc_unset_hba(phba);
9416out_free_sysfs_attr:
9417 lpfc_free_sysfs_attr(vport);
9418out_destroy_shost:
9419 lpfc_destroy_shost(phba);
9420out_unset_driver_resource:
9421 lpfc_unset_driver_resource_phase2(phba);
9422out_free_iocb_list:
9423 lpfc_free_iocb_list(phba);
9424out_unset_driver_resource_s3:
9425 lpfc_sli_driver_resource_unset(phba);
9426out_unset_pci_mem_s3:
9427 lpfc_sli_pci_mem_unset(phba);
9428out_disable_pci_dev:
9429 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04009430 if (shost)
9431 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -04009432out_free_phba:
9433 lpfc_hba_free(phba);
9434 return error;
9435}
9436
9437/**
9438 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -04009439 * @pdev: pointer to PCI device
9440 *
James Smart3772a992009-05-22 14:50:54 -04009441 * This routine is to be called to disattach a device with SLI-3 interface
9442 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
9443 * removed from PCI bus, it performs all the necessary cleanup for the HBA
9444 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -04009445 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08009446static void
James Smart3772a992009-05-22 14:50:54 -04009447lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -05009448{
James Smart2e0fef82007-06-17 19:56:36 -05009449 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9450 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -05009451 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -05009452 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -05009453 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05009454 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
9455
James Smart549e55c2007-08-02 11:09:51 -04009456 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04009457 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04009458 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009459
James Smart858c9f62007-06-17 19:56:39 -05009460 lpfc_free_sysfs_attr(vport);
9461
James Smarteada2722008-12-04 22:39:13 -05009462 /* Release all the vports against this physical port */
9463 vports = lpfc_create_vport_work_array(phba);
9464 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -04009465 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
9466 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
9467 continue;
James Smarteada2722008-12-04 22:39:13 -05009468 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -04009469 }
James Smarteada2722008-12-04 22:39:13 -05009470 lpfc_destroy_vport_work_array(phba, vports);
9471
9472 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -05009473 fc_remove_host(shost);
9474 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04009475 lpfc_cleanup(vport);
9476
James Smart2e0fef82007-06-17 19:56:36 -05009477 /*
9478 * Bring down the SLI Layer. This step disable all interrupts,
9479 * clears the rings, discards all mailbox commands, and resets
9480 * the HBA.
9481 */
James Smarta257bf92009-04-06 18:48:10 -04009482
Justin P. Mattock48e34d02010-12-30 15:07:58 -08009483 /* HBA interrupt will be disabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -05009484 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -04009485 /* Stop kthread signal shall trigger work_done one more time */
9486 kthread_stop(phba->worker_thread);
9487 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -05009488 lpfc_sli_brdrestart(phba);
9489
James Smart72859902012-01-18 16:25:38 -05009490 kfree(phba->vpi_bmask);
9491 kfree(phba->vpi_ids);
9492
James Smart3772a992009-05-22 14:50:54 -04009493 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05009494 spin_lock_irq(&phba->hbalock);
9495 list_del_init(&vport->listentry);
9496 spin_unlock_irq(&phba->hbalock);
9497
James Smart858c9f62007-06-17 19:56:39 -05009498 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05009499
James Smart912e3ac2011-05-24 11:42:11 -04009500 /* Disable SR-IOV if enabled */
9501 if (phba->cfg_sriov_nr_virtfn)
9502 pci_disable_sriov(pdev);
9503
James Smart5b75da22008-12-04 22:39:35 -05009504 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009505 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -05009506
James Smart858c9f62007-06-17 19:56:39 -05009507 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05009508
9509 /*
9510 * Call scsi_free before mem_free since scsi bufs are released to their
9511 * corresponding pools here.
9512 */
9513 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -04009514 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -05009515
James Smart34b02dc2008-08-24 21:49:55 -04009516 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
9517 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -05009518
James Smart2e0fef82007-06-17 19:56:36 -05009519 /* Free resources associated with SLI2 interface */
9520 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -04009521 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -05009522
9523 /* unmap adapter SLIM and Control Registers */
9524 iounmap(phba->ctrl_regs_memmap_p);
9525 iounmap(phba->slim_memmap_p);
9526
James Smart3772a992009-05-22 14:50:54 -04009527 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -05009528
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05009529 pci_release_selected_regions(pdev, bars);
James Smart2e0fef82007-06-17 19:56:36 -05009530 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05009531}
9532
Linas Vepstas8d63f372007-02-14 14:28:36 -06009533/**
James Smart3772a992009-05-22 14:50:54 -04009534 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05009535 * @pdev: pointer to PCI device
9536 * @msg: power management message
9537 *
James Smart3772a992009-05-22 14:50:54 -04009538 * This routine is to be called from the kernel's PCI subsystem to support
9539 * system Power Management (PM) to device with SLI-3 interface spec. When
9540 * PM invokes this method, it quiesces the device by stopping the driver's
9541 * worker thread for the device, turning off device's interrupt and DMA,
9542 * and bring the device offline. Note that as the driver implements the
9543 * minimum PM requirements to a power-aware driver's PM support for the
9544 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
9545 * to the suspend() method call will be treated as SUSPEND and the driver will
9546 * fully reinitialize its device during resume() method call, the driver will
9547 * set device to PCI_D3hot state in PCI config space instead of setting it
9548 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -05009549 *
9550 * Return code
James Smart3772a992009-05-22 14:50:54 -04009551 * 0 - driver suspended the device
9552 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05009553 **/
9554static int
James Smart3772a992009-05-22 14:50:54 -04009555lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -05009556{
9557 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9558 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9559
9560 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9561 "0473 PCI device Power Management suspend.\n");
9562
9563 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -04009564 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart3a55b532008-12-04 22:38:54 -05009565 lpfc_offline(phba);
9566 kthread_stop(phba->worker_thread);
9567
9568 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -04009569 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -05009570
9571 /* Save device state to PCI config space */
9572 pci_save_state(pdev);
9573 pci_set_power_state(pdev, PCI_D3hot);
9574
9575 return 0;
9576}
9577
9578/**
James Smart3772a992009-05-22 14:50:54 -04009579 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05009580 * @pdev: pointer to PCI device
9581 *
James Smart3772a992009-05-22 14:50:54 -04009582 * This routine is to be called from the kernel's PCI subsystem to support
9583 * system Power Management (PM) to device with SLI-3 interface spec. When PM
9584 * invokes this method, it restores the device's PCI config space state and
9585 * fully reinitializes the device and brings it online. Note that as the
9586 * driver implements the minimum PM requirements to a power-aware driver's
9587 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
9588 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
9589 * driver will fully reinitialize its device during resume() method call,
9590 * the device will be set to PCI_D0 directly in PCI config space before
9591 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -05009592 *
9593 * Return code
James Smart3772a992009-05-22 14:50:54 -04009594 * 0 - driver suspended the device
9595 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05009596 **/
9597static int
James Smart3772a992009-05-22 14:50:54 -04009598lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -05009599{
9600 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9601 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -05009602 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05009603 int error;
9604
9605 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9606 "0452 PCI device Power Management resume.\n");
9607
9608 /* Restore device state from PCI config space */
9609 pci_set_power_state(pdev, PCI_D0);
9610 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -04009611
James Smart1dfb5a42010-02-12 14:40:50 -05009612 /*
9613 * As the new kernel behavior of pci_restore_state() API call clears
9614 * device saved_state flag, need to save the restored state again.
9615 */
9616 pci_save_state(pdev);
9617
James Smart3a55b532008-12-04 22:38:54 -05009618 if (pdev->is_busmaster)
9619 pci_set_master(pdev);
9620
9621 /* Startup the kernel thread for this host adapter. */
9622 phba->worker_thread = kthread_run(lpfc_do_work, phba,
9623 "lpfc_worker_%d", phba->brd_no);
9624 if (IS_ERR(phba->worker_thread)) {
9625 error = PTR_ERR(phba->worker_thread);
9626 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9627 "0434 PM resume failed to start worker "
9628 "thread: error=x%x.\n", error);
9629 return error;
9630 }
9631
James Smart5b75da22008-12-04 22:39:35 -05009632 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009633 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05009634 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -05009635 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -05009636 "0430 PM resume Failed to enable interrupt\n");
9637 return -EIO;
9638 } else
9639 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05009640
9641 /* Restart HBA and bring it online */
9642 lpfc_sli_brdrestart(phba);
9643 lpfc_online(phba);
9644
James Smart5b75da22008-12-04 22:39:35 -05009645 /* Log the current active interrupt mode */
9646 lpfc_log_intr_mode(phba, phba->intr_mode);
9647
James Smart3a55b532008-12-04 22:38:54 -05009648 return 0;
9649}
9650
9651/**
James Smart891478a2009-11-18 15:40:23 -05009652 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
9653 * @phba: pointer to lpfc hba data structure.
9654 *
9655 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -04009656 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -05009657 **/
9658static void
9659lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
9660{
James Smarte2af0d22010-03-15 11:25:32 -04009661 struct lpfc_sli *psli = &phba->sli;
9662 struct lpfc_sli_ring *pring;
9663
James Smart891478a2009-11-18 15:40:23 -05009664 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9665 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -04009666
9667 /*
9668 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
9669 * and let the SCSI mid-layer to retry them to recover.
9670 */
9671 pring = &psli->ring[psli->fcp_ring];
9672 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart891478a2009-11-18 15:40:23 -05009673}
9674
9675/**
James Smart0d878412009-10-02 15:16:56 -04009676 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
9677 * @phba: pointer to lpfc hba data structure.
9678 *
9679 * This routine is called to prepare the SLI3 device for PCI slot reset. It
9680 * disables the device interrupt and pci device, and aborts the internal FCP
9681 * pending I/Os.
9682 **/
9683static void
9684lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
9685{
James Smart0d878412009-10-02 15:16:56 -04009686 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05009687 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -04009688
James Smart75baf692010-06-08 18:31:21 -04009689 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -04009690 lpfc_block_mgmt_io(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -04009691
James Smarte2af0d22010-03-15 11:25:32 -04009692 /* Block all SCSI devices' I/Os on the host */
9693 lpfc_scsi_dev_block(phba);
9694
James Smartea714f32013-04-17 20:18:47 -04009695 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
9696 lpfc_sli_flush_fcp_rings(phba);
9697
James Smarte2af0d22010-03-15 11:25:32 -04009698 /* stop all timers */
9699 lpfc_stop_hba_timers(phba);
9700
James Smart0d878412009-10-02 15:16:56 -04009701 /* Disable interrupt and pci device */
9702 lpfc_sli_disable_intr(phba);
9703 pci_disable_device(phba->pcidev);
James Smart0d878412009-10-02 15:16:56 -04009704}
9705
9706/**
9707 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
9708 * @phba: pointer to lpfc hba data structure.
9709 *
9710 * This routine is called to prepare the SLI3 device for PCI slot permanently
9711 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
9712 * pending I/Os.
9713 **/
9714static void
James Smart75baf692010-06-08 18:31:21 -04009715lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
James Smart0d878412009-10-02 15:16:56 -04009716{
9717 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05009718 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -04009719 /* Block all SCSI devices' I/Os on the host */
9720 lpfc_scsi_dev_block(phba);
9721
9722 /* stop all timers */
9723 lpfc_stop_hba_timers(phba);
9724
James Smart0d878412009-10-02 15:16:56 -04009725 /* Clean up all driver's outstanding SCSI I/Os */
9726 lpfc_sli_flush_fcp_rings(phba);
9727}
9728
9729/**
James Smart3772a992009-05-22 14:50:54 -04009730 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -04009731 * @pdev: pointer to PCI device.
9732 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009733 *
James Smart3772a992009-05-22 14:50:54 -04009734 * This routine is called from the PCI subsystem for I/O error handling to
9735 * device with SLI-3 interface spec. This function is called by the PCI
9736 * subsystem after a PCI bus error affecting this device has been detected.
9737 * When this function is invoked, it will need to stop all the I/Os and
9738 * interrupt(s) to the device. Once that is done, it will return
9739 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
9740 * as desired.
James Smarte59058c2008-08-24 21:49:00 -04009741 *
9742 * Return codes
James Smart0d878412009-10-02 15:16:56 -04009743 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -04009744 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
9745 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -04009746 **/
James Smart3772a992009-05-22 14:50:54 -04009747static pci_ers_result_t
9748lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009749{
James Smart51ef4c22007-08-02 11:10:31 -04009750 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9751 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009752
James Smart0d878412009-10-02 15:16:56 -04009753 switch (state) {
9754 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -05009755 /* Non-fatal error, prepare for recovery */
9756 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -04009757 return PCI_ERS_RESULT_CAN_RECOVER;
9758 case pci_channel_io_frozen:
9759 /* Fatal error, prepare for slot reset */
9760 lpfc_sli_prep_dev_for_reset(phba);
9761 return PCI_ERS_RESULT_NEED_RESET;
9762 case pci_channel_io_perm_failure:
9763 /* Permanent failure, prepare for device down */
James Smart75baf692010-06-08 18:31:21 -04009764 lpfc_sli_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009765 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -04009766 default:
9767 /* Unknown state, prepare and request slot reset */
9768 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9769 "0472 Unknown PCI error state: x%x\n", state);
9770 lpfc_sli_prep_dev_for_reset(phba);
9771 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -04009772 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06009773}
9774
9775/**
James Smart3772a992009-05-22 14:50:54 -04009776 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -04009777 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009778 *
James Smart3772a992009-05-22 14:50:54 -04009779 * This routine is called from the PCI subsystem for error handling to
9780 * device with SLI-3 interface spec. This is called after PCI bus has been
9781 * reset to restart the PCI card from scratch, as if from a cold-boot.
9782 * During the PCI subsystem error recovery, after driver returns
9783 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
9784 * recovery and then call this routine before calling the .resume method
9785 * to recover the device. This function will initialize the HBA device,
9786 * enable the interrupt, but it will just put the HBA to offline state
9787 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -04009788 *
9789 * Return codes
James Smart3772a992009-05-22 14:50:54 -04009790 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
9791 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -06009792 */
James Smart3772a992009-05-22 14:50:54 -04009793static pci_ers_result_t
9794lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009795{
James Smart51ef4c22007-08-02 11:10:31 -04009796 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9797 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009798 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -05009799 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009800
9801 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +11009802 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -06009803 printk(KERN_ERR "lpfc: Cannot re-enable "
9804 "PCI device after reset.\n");
9805 return PCI_ERS_RESULT_DISCONNECT;
9806 }
9807
James Smart97207482008-12-04 22:39:19 -05009808 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05009809
9810 /*
9811 * As the new kernel behavior of pci_restore_state() API call clears
9812 * device saved_state flag, need to save the restored state again.
9813 */
9814 pci_save_state(pdev);
9815
James Smart97207482008-12-04 22:39:19 -05009816 if (pdev->is_busmaster)
9817 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009818
James Smart92d7f7b2007-06-17 19:56:38 -05009819 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04009820 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05009821 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009822
James Smart5b75da22008-12-04 22:39:35 -05009823 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04009824 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05009825 if (intr_mode == LPFC_INTR_ERROR) {
9826 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9827 "0427 Cannot re-enable interrupt after "
9828 "slot reset.\n");
9829 return PCI_ERS_RESULT_DISCONNECT;
9830 } else
9831 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009832
James Smart75baf692010-06-08 18:31:21 -04009833 /* Take device offline, it will perform cleanup */
James Smart618a5232012-06-12 13:54:36 -04009834 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009835 lpfc_offline(phba);
9836 lpfc_sli_brdrestart(phba);
9837
James Smart5b75da22008-12-04 22:39:35 -05009838 /* Log the current active interrupt mode */
9839 lpfc_log_intr_mode(phba, phba->intr_mode);
9840
Linas Vepstas8d63f372007-02-14 14:28:36 -06009841 return PCI_ERS_RESULT_RECOVERED;
9842}
9843
9844/**
James Smart3772a992009-05-22 14:50:54 -04009845 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -04009846 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -06009847 *
James Smart3772a992009-05-22 14:50:54 -04009848 * This routine is called from the PCI subsystem for error handling to device
9849 * with SLI-3 interface spec. It is called when kernel error recovery tells
9850 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
9851 * error recovery. After this call, traffic can start to flow from this device
9852 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -06009853 */
James Smart3772a992009-05-22 14:50:54 -04009854static void
9855lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06009856{
James Smart51ef4c22007-08-02 11:10:31 -04009857 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9858 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06009859
James Smarte2af0d22010-03-15 11:25:32 -04009860 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -04009861 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -04009862
9863 /* Clean up Advanced Error Reporting (AER) if needed */
9864 if (phba->hba_flag & HBA_AER_ENABLED)
9865 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06009866}
9867
James Smart3772a992009-05-22 14:50:54 -04009868/**
James Smartda0436e2009-05-22 14:51:39 -04009869 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
9870 * @phba: pointer to lpfc hba data structure.
9871 *
9872 * returns the number of ELS/CT IOCBs to reserve
9873 **/
9874int
9875lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
9876{
9877 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
9878
James Smartf1126682009-06-10 17:22:44 -04009879 if (phba->sli_rev == LPFC_SLI_REV4) {
9880 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -04009881 return 10;
James Smartf1126682009-06-10 17:22:44 -04009882 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -04009883 return 25;
James Smartf1126682009-06-10 17:22:44 -04009884 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -04009885 return 50;
James Smartf1126682009-06-10 17:22:44 -04009886 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -04009887 return 100;
James Smart8a9d2e82012-05-09 21:16:12 -04009888 else if (max_xri <= 1536)
James Smart6a9c52c2009-10-02 15:16:51 -04009889 return 150;
James Smart8a9d2e82012-05-09 21:16:12 -04009890 else if (max_xri <= 2048)
9891 return 200;
9892 else
9893 return 250;
James Smartf1126682009-06-10 17:22:44 -04009894 } else
9895 return 0;
James Smartda0436e2009-05-22 14:51:39 -04009896}
9897
9898/**
James Smart52d52442011-05-24 11:42:45 -04009899 * lpfc_write_firmware - attempt to write a firmware image to the port
James Smart52d52442011-05-24 11:42:45 -04009900 * @fw: pointer to firmware image returned from request_firmware.
James Smartce396282012-09-29 11:30:56 -04009901 * @phba: pointer to lpfc hba data structure.
James Smart52d52442011-05-24 11:42:45 -04009902 *
James Smart52d52442011-05-24 11:42:45 -04009903 **/
James Smartce396282012-09-29 11:30:56 -04009904static void
9905lpfc_write_firmware(const struct firmware *fw, void *context)
James Smart52d52442011-05-24 11:42:45 -04009906{
James Smartce396282012-09-29 11:30:56 -04009907 struct lpfc_hba *phba = (struct lpfc_hba *)context;
James Smart6b5151f2012-01-18 16:24:06 -05009908 char fwrev[FW_REV_STR_SIZE];
James Smartce396282012-09-29 11:30:56 -04009909 struct lpfc_grp_hdr *image;
James Smart52d52442011-05-24 11:42:45 -04009910 struct list_head dma_buffer_list;
9911 int i, rc = 0;
9912 struct lpfc_dmabuf *dmabuf, *next;
9913 uint32_t offset = 0, temp_offset = 0;
9914
James Smartc71ab862012-10-31 14:44:33 -04009915 /* It can be null in no-wait mode, sanity check */
James Smartce396282012-09-29 11:30:56 -04009916 if (!fw) {
9917 rc = -ENXIO;
9918 goto out;
9919 }
9920 image = (struct lpfc_grp_hdr *)fw->data;
9921
James Smart52d52442011-05-24 11:42:45 -04009922 INIT_LIST_HEAD(&dma_buffer_list);
James Smart079b5c92011-08-21 21:48:49 -04009923 if ((be32_to_cpu(image->magic_number) != LPFC_GROUP_OJECT_MAGIC_NUM) ||
9924 (bf_get_be32(lpfc_grp_hdr_file_type, image) !=
9925 LPFC_FILE_TYPE_GROUP) ||
9926 (bf_get_be32(lpfc_grp_hdr_id, image) != LPFC_FILE_ID_GROUP) ||
9927 (be32_to_cpu(image->size) != fw->size)) {
James Smart52d52442011-05-24 11:42:45 -04009928 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9929 "3022 Invalid FW image found. "
James Smart079b5c92011-08-21 21:48:49 -04009930 "Magic:%x Type:%x ID:%x\n",
9931 be32_to_cpu(image->magic_number),
9932 bf_get_be32(lpfc_grp_hdr_file_type, image),
9933 bf_get_be32(lpfc_grp_hdr_id, image));
James Smartce396282012-09-29 11:30:56 -04009934 rc = -EINVAL;
9935 goto release_out;
James Smart52d52442011-05-24 11:42:45 -04009936 }
9937 lpfc_decode_firmware_rev(phba, fwrev, 1);
James Smart88a2cfb2011-07-22 18:36:33 -04009938 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
James Smart52d52442011-05-24 11:42:45 -04009939 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartce396282012-09-29 11:30:56 -04009940 "3023 Updating Firmware, Current Version:%s "
James Smart52d52442011-05-24 11:42:45 -04009941 "New Version:%s\n",
James Smart88a2cfb2011-07-22 18:36:33 -04009942 fwrev, image->revision);
James Smart52d52442011-05-24 11:42:45 -04009943 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
9944 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
9945 GFP_KERNEL);
9946 if (!dmabuf) {
9947 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -04009948 goto release_out;
James Smart52d52442011-05-24 11:42:45 -04009949 }
9950 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
9951 SLI4_PAGE_SIZE,
9952 &dmabuf->phys,
9953 GFP_KERNEL);
9954 if (!dmabuf->virt) {
9955 kfree(dmabuf);
9956 rc = -ENOMEM;
James Smartce396282012-09-29 11:30:56 -04009957 goto release_out;
James Smart52d52442011-05-24 11:42:45 -04009958 }
9959 list_add_tail(&dmabuf->list, &dma_buffer_list);
9960 }
9961 while (offset < fw->size) {
9962 temp_offset = offset;
9963 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
James Smart079b5c92011-08-21 21:48:49 -04009964 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
James Smart52d52442011-05-24 11:42:45 -04009965 memcpy(dmabuf->virt,
9966 fw->data + temp_offset,
James Smart079b5c92011-08-21 21:48:49 -04009967 fw->size - temp_offset);
9968 temp_offset = fw->size;
James Smart52d52442011-05-24 11:42:45 -04009969 break;
9970 }
James Smart52d52442011-05-24 11:42:45 -04009971 memcpy(dmabuf->virt, fw->data + temp_offset,
9972 SLI4_PAGE_SIZE);
James Smart88a2cfb2011-07-22 18:36:33 -04009973 temp_offset += SLI4_PAGE_SIZE;
James Smart52d52442011-05-24 11:42:45 -04009974 }
9975 rc = lpfc_wr_object(phba, &dma_buffer_list,
9976 (fw->size - offset), &offset);
James Smartce396282012-09-29 11:30:56 -04009977 if (rc)
9978 goto release_out;
James Smart52d52442011-05-24 11:42:45 -04009979 }
9980 rc = offset;
9981 }
James Smartce396282012-09-29 11:30:56 -04009982
9983release_out:
James Smart52d52442011-05-24 11:42:45 -04009984 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
9985 list_del(&dmabuf->list);
9986 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
9987 dmabuf->virt, dmabuf->phys);
9988 kfree(dmabuf);
9989 }
James Smartce396282012-09-29 11:30:56 -04009990 release_firmware(fw);
9991out:
9992 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartc71ab862012-10-31 14:44:33 -04009993 "3024 Firmware update done: %d.\n", rc);
James Smartce396282012-09-29 11:30:56 -04009994 return;
James Smart52d52442011-05-24 11:42:45 -04009995}
9996
9997/**
James Smartc71ab862012-10-31 14:44:33 -04009998 * lpfc_sli4_request_firmware_update - Request linux generic firmware upgrade
9999 * @phba: pointer to lpfc hba data structure.
10000 *
10001 * This routine is called to perform Linux generic firmware upgrade on device
10002 * that supports such feature.
10003 **/
10004int
10005lpfc_sli4_request_firmware_update(struct lpfc_hba *phba, uint8_t fw_upgrade)
10006{
10007 uint8_t file_name[ELX_MODEL_NAME_SIZE];
10008 int ret;
10009 const struct firmware *fw;
10010
10011 /* Only supported on SLI4 interface type 2 for now */
10012 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
10013 LPFC_SLI_INTF_IF_TYPE_2)
10014 return -EPERM;
10015
10016 snprintf(file_name, ELX_MODEL_NAME_SIZE, "%s.grp", phba->ModelName);
10017
10018 if (fw_upgrade == INT_FW_UPGRADE) {
10019 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
10020 file_name, &phba->pcidev->dev,
10021 GFP_KERNEL, (void *)phba,
10022 lpfc_write_firmware);
10023 } else if (fw_upgrade == RUN_FW_UPGRADE) {
10024 ret = request_firmware(&fw, file_name, &phba->pcidev->dev);
10025 if (!ret)
10026 lpfc_write_firmware(fw, (void *)phba);
10027 } else {
10028 ret = -EINVAL;
10029 }
10030
10031 return ret;
10032}
10033
10034/**
James Smartda0436e2009-05-22 14:51:39 -040010035 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
10036 * @pdev: pointer to PCI device
10037 * @pid: pointer to PCI device identifier
10038 *
10039 * This routine is called from the kernel's PCI subsystem to device with
10040 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
10041 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
10042 * information of the device and driver to see if the driver state that it
10043 * can support this kind of device. If the match is successful, the driver
10044 * core invokes this routine. If this routine determines it can claim the HBA,
10045 * it does all the initialization that it needs to do to handle the HBA
10046 * properly.
10047 *
10048 * Return code
10049 * 0 - driver can claim the device
10050 * negative value - driver can not claim the device
10051 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010052static int
James Smartda0436e2009-05-22 14:51:39 -040010053lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
10054{
10055 struct lpfc_hba *phba;
10056 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -040010057 struct Scsi_Host *shost = NULL;
James Smartce396282012-09-29 11:30:56 -040010058 int error, ret;
James Smartda0436e2009-05-22 14:51:39 -040010059 uint32_t cfg_mode, intr_mode;
James Smart67d12732012-08-03 12:36:13 -040010060 int adjusted_fcp_io_channel;
James Smartda0436e2009-05-22 14:51:39 -040010061
10062 /* Allocate memory for HBA structure */
10063 phba = lpfc_hba_alloc(pdev);
10064 if (!phba)
10065 return -ENOMEM;
10066
10067 /* Perform generic PCI device enabling operation */
10068 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -040010069 if (error)
James Smartda0436e2009-05-22 14:51:39 -040010070 goto out_free_phba;
James Smartda0436e2009-05-22 14:51:39 -040010071
10072 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
10073 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
10074 if (error)
10075 goto out_disable_pci_dev;
10076
10077 /* Set up SLI-4 specific device PCI memory space */
10078 error = lpfc_sli4_pci_mem_setup(phba);
10079 if (error) {
10080 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10081 "1410 Failed to set up pci memory space.\n");
10082 goto out_disable_pci_dev;
10083 }
10084
10085 /* Set up phase-1 common device driver resources */
10086 error = lpfc_setup_driver_resource_phase1(phba);
10087 if (error) {
10088 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10089 "1411 Failed to set up driver resource.\n");
10090 goto out_unset_pci_mem_s4;
10091 }
10092
10093 /* Set up SLI-4 Specific device driver resources */
10094 error = lpfc_sli4_driver_resource_setup(phba);
10095 if (error) {
10096 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10097 "1412 Failed to set up driver resource.\n");
10098 goto out_unset_pci_mem_s4;
10099 }
10100
10101 /* Initialize and populate the iocb list per host */
James Smart2a9bf3d2010-06-07 15:24:45 -040010102
10103 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10104 "2821 initialize iocb list %d.\n",
10105 phba->cfg_iocb_cnt*1024);
10106 error = lpfc_init_iocb_list(phba, phba->cfg_iocb_cnt*1024);
10107
James Smartda0436e2009-05-22 14:51:39 -040010108 if (error) {
10109 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10110 "1413 Failed to initialize iocb list.\n");
10111 goto out_unset_driver_resource_s4;
10112 }
10113
James Smart19ca7602010-11-20 23:11:55 -050010114 INIT_LIST_HEAD(&phba->active_rrq_list);
James Smart7d791df2011-07-22 18:37:52 -040010115 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
James Smart19ca7602010-11-20 23:11:55 -050010116
James Smartda0436e2009-05-22 14:51:39 -040010117 /* Set up common device driver resources */
10118 error = lpfc_setup_driver_resource_phase2(phba);
10119 if (error) {
10120 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10121 "1414 Failed to set up driver resource.\n");
10122 goto out_free_iocb_list;
10123 }
10124
James Smart079b5c92011-08-21 21:48:49 -040010125 /* Get the default values for Model Name and Description */
10126 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
10127
James Smartda0436e2009-05-22 14:51:39 -040010128 /* Create SCSI host to the physical port */
10129 error = lpfc_create_shost(phba);
10130 if (error) {
10131 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10132 "1415 Failed to create scsi host.\n");
10133 goto out_unset_driver_resource;
10134 }
10135
10136 /* Configure sysfs attributes */
10137 vport = phba->pport;
10138 error = lpfc_alloc_sysfs_attr(vport);
10139 if (error) {
10140 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10141 "1416 Failed to allocate sysfs attr\n");
10142 goto out_destroy_shost;
10143 }
10144
James Smart6669f9b2009-10-02 15:16:45 -040010145 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -040010146 /* Now, trying to enable interrupt and bring up the device */
10147 cfg_mode = phba->cfg_use_msi;
James Smartda0436e2009-05-22 14:51:39 -040010148
James Smart7b15db32013-01-03 15:43:29 -050010149 /* Put device to a known state before enabling interrupt */
10150 lpfc_stop_port(phba);
10151 /* Configure and enable interrupt */
10152 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
10153 if (intr_mode == LPFC_INTR_ERROR) {
10154 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10155 "0426 Failed to enable interrupt.\n");
10156 error = -ENODEV;
10157 goto out_free_sysfs_attr;
James Smartda0436e2009-05-22 14:51:39 -040010158 }
James Smart7b15db32013-01-03 15:43:29 -050010159 /* Default to single EQ for non-MSI-X */
10160 if (phba->intr_type != MSIX)
10161 adjusted_fcp_io_channel = 1;
10162 else
10163 adjusted_fcp_io_channel = phba->cfg_fcp_io_channel;
10164 phba->cfg_fcp_io_channel = adjusted_fcp_io_channel;
10165 /* Set up SLI-4 HBA */
10166 if (lpfc_sli4_hba_setup(phba)) {
10167 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10168 "1421 Failed to set up hba\n");
10169 error = -ENODEV;
10170 goto out_disable_intr;
10171 }
10172
10173 /* Log the current active interrupt mode */
10174 phba->intr_mode = intr_mode;
10175 lpfc_log_intr_mode(phba, intr_mode);
James Smartda0436e2009-05-22 14:51:39 -040010176
10177 /* Perform post initialization setup */
10178 lpfc_post_init_setup(phba);
10179
James Smartc71ab862012-10-31 14:44:33 -040010180 /* check for firmware upgrade or downgrade */
10181 if (phba->cfg_request_firmware_upgrade)
10182 ret = lpfc_sli4_request_firmware_update(phba, INT_FW_UPGRADE);
James Smart52d52442011-05-24 11:42:45 -040010183
James Smart1c6834a2009-07-19 10:01:26 -040010184 /* Check if there are static vports to be created. */
10185 lpfc_create_static_vport(phba);
James Smartda0436e2009-05-22 14:51:39 -040010186 return 0;
10187
10188out_disable_intr:
10189 lpfc_sli4_disable_intr(phba);
10190out_free_sysfs_attr:
10191 lpfc_free_sysfs_attr(vport);
10192out_destroy_shost:
10193 lpfc_destroy_shost(phba);
10194out_unset_driver_resource:
10195 lpfc_unset_driver_resource_phase2(phba);
10196out_free_iocb_list:
10197 lpfc_free_iocb_list(phba);
10198out_unset_driver_resource_s4:
10199 lpfc_sli4_driver_resource_unset(phba);
10200out_unset_pci_mem_s4:
10201 lpfc_sli4_pci_mem_unset(phba);
10202out_disable_pci_dev:
10203 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -040010204 if (shost)
10205 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -040010206out_free_phba:
10207 lpfc_hba_free(phba);
10208 return error;
10209}
10210
10211/**
10212 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
10213 * @pdev: pointer to PCI device
10214 *
10215 * This routine is called from the kernel's PCI subsystem to device with
10216 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
10217 * removed from PCI bus, it performs all the necessary cleanup for the HBA
10218 * device to be removed from the PCI subsystem properly.
10219 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010220static void
James Smartda0436e2009-05-22 14:51:39 -040010221lpfc_pci_remove_one_s4(struct pci_dev *pdev)
10222{
10223 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10224 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
10225 struct lpfc_vport **vports;
10226 struct lpfc_hba *phba = vport->phba;
10227 int i;
10228
10229 /* Mark the device unloading flag */
10230 spin_lock_irq(&phba->hbalock);
10231 vport->load_flag |= FC_UNLOADING;
10232 spin_unlock_irq(&phba->hbalock);
10233
10234 /* Free the HBA sysfs attributes */
10235 lpfc_free_sysfs_attr(vport);
10236
10237 /* Release all the vports against this physical port */
10238 vports = lpfc_create_vport_work_array(phba);
10239 if (vports != NULL)
James Smart587a37f2012-05-09 21:16:03 -040010240 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
10241 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
10242 continue;
James Smartda0436e2009-05-22 14:51:39 -040010243 fc_vport_terminate(vports[i]->fc_vport);
James Smart587a37f2012-05-09 21:16:03 -040010244 }
James Smartda0436e2009-05-22 14:51:39 -040010245 lpfc_destroy_vport_work_array(phba, vports);
10246
10247 /* Remove FC host and then SCSI host with the physical port */
10248 fc_remove_host(shost);
10249 scsi_remove_host(shost);
10250
10251 /* Perform cleanup on the physical port */
10252 lpfc_cleanup(vport);
10253
10254 /*
10255 * Bring down the SLI Layer. This step disables all interrupts,
10256 * clears the rings, discards all mailbox commands, and resets
10257 * the HBA FCoE function.
10258 */
10259 lpfc_debugfs_terminate(vport);
10260 lpfc_sli4_hba_unset(phba);
10261
10262 spin_lock_irq(&phba->hbalock);
10263 list_del_init(&vport->listentry);
10264 spin_unlock_irq(&phba->hbalock);
10265
James Smart3677a3a2010-09-29 11:19:14 -040010266 /* Perform scsi free before driver resource_unset since scsi
James Smartda0436e2009-05-22 14:51:39 -040010267 * buffers are released to their corresponding pools here.
10268 */
10269 lpfc_scsi_free(phba);
James Smart67d12732012-08-03 12:36:13 -040010270
James Smartda0436e2009-05-22 14:51:39 -040010271 lpfc_sli4_driver_resource_unset(phba);
10272
10273 /* Unmap adapter Control and Doorbell registers */
10274 lpfc_sli4_pci_mem_unset(phba);
10275
10276 /* Release PCI resources and disable device's PCI function */
10277 scsi_host_put(shost);
10278 lpfc_disable_pci_dev(phba);
10279
10280 /* Finally, free the driver's device data structure */
10281 lpfc_hba_free(phba);
10282
10283 return;
10284}
10285
10286/**
10287 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
10288 * @pdev: pointer to PCI device
10289 * @msg: power management message
10290 *
10291 * This routine is called from the kernel's PCI subsystem to support system
10292 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
10293 * this method, it quiesces the device by stopping the driver's worker
10294 * thread for the device, turning off device's interrupt and DMA, and bring
10295 * the device offline. Note that as the driver implements the minimum PM
10296 * requirements to a power-aware driver's PM support for suspend/resume -- all
10297 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
10298 * method call will be treated as SUSPEND and the driver will fully
10299 * reinitialize its device during resume() method call, the driver will set
10300 * device to PCI_D3hot state in PCI config space instead of setting it
10301 * according to the @msg provided by the PM.
10302 *
10303 * Return code
10304 * 0 - driver suspended the device
10305 * Error otherwise
10306 **/
10307static int
10308lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
10309{
10310 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10311 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10312
10313 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart75baf692010-06-08 18:31:21 -040010314 "2843 PCI device Power Management suspend.\n");
James Smartda0436e2009-05-22 14:51:39 -040010315
10316 /* Bring down the device */
James Smart618a5232012-06-12 13:54:36 -040010317 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smartda0436e2009-05-22 14:51:39 -040010318 lpfc_offline(phba);
10319 kthread_stop(phba->worker_thread);
10320
10321 /* Disable interrupt from device */
10322 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040010323 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -040010324
10325 /* Save device state to PCI config space */
10326 pci_save_state(pdev);
10327 pci_set_power_state(pdev, PCI_D3hot);
10328
10329 return 0;
10330}
10331
10332/**
10333 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
10334 * @pdev: pointer to PCI device
10335 *
10336 * This routine is called from the kernel's PCI subsystem to support system
10337 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
10338 * this method, it restores the device's PCI config space state and fully
10339 * reinitializes the device and brings it online. Note that as the driver
10340 * implements the minimum PM requirements to a power-aware driver's PM for
10341 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
10342 * to the suspend() method call will be treated as SUSPEND and the driver
10343 * will fully reinitialize its device during resume() method call, the device
10344 * will be set to PCI_D0 directly in PCI config space before restoring the
10345 * state.
10346 *
10347 * Return code
10348 * 0 - driver suspended the device
10349 * Error otherwise
10350 **/
10351static int
10352lpfc_pci_resume_one_s4(struct pci_dev *pdev)
10353{
10354 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10355 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10356 uint32_t intr_mode;
10357 int error;
10358
10359 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
10360 "0292 PCI device Power Management resume.\n");
10361
10362 /* Restore device state from PCI config space */
10363 pci_set_power_state(pdev, PCI_D0);
10364 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -050010365
10366 /*
10367 * As the new kernel behavior of pci_restore_state() API call clears
10368 * device saved_state flag, need to save the restored state again.
10369 */
10370 pci_save_state(pdev);
10371
James Smartda0436e2009-05-22 14:51:39 -040010372 if (pdev->is_busmaster)
10373 pci_set_master(pdev);
10374
10375 /* Startup the kernel thread for this host adapter. */
10376 phba->worker_thread = kthread_run(lpfc_do_work, phba,
10377 "lpfc_worker_%d", phba->brd_no);
10378 if (IS_ERR(phba->worker_thread)) {
10379 error = PTR_ERR(phba->worker_thread);
10380 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10381 "0293 PM resume failed to start worker "
10382 "thread: error=x%x.\n", error);
10383 return error;
10384 }
10385
10386 /* Configure and enable interrupt */
10387 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
10388 if (intr_mode == LPFC_INTR_ERROR) {
10389 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10390 "0294 PM resume Failed to enable interrupt\n");
10391 return -EIO;
10392 } else
10393 phba->intr_mode = intr_mode;
10394
10395 /* Restart HBA and bring it online */
10396 lpfc_sli_brdrestart(phba);
10397 lpfc_online(phba);
10398
10399 /* Log the current active interrupt mode */
10400 lpfc_log_intr_mode(phba, phba->intr_mode);
10401
10402 return 0;
10403}
10404
10405/**
James Smart75baf692010-06-08 18:31:21 -040010406 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
10407 * @phba: pointer to lpfc hba data structure.
10408 *
10409 * This routine is called to prepare the SLI4 device for PCI slot recover. It
10410 * aborts all the outstanding SCSI I/Os to the pci device.
10411 **/
10412static void
10413lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
10414{
10415 struct lpfc_sli *psli = &phba->sli;
10416 struct lpfc_sli_ring *pring;
10417
10418 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10419 "2828 PCI channel I/O abort preparing for recovery\n");
10420 /*
10421 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
10422 * and let the SCSI mid-layer to retry them to recover.
10423 */
10424 pring = &psli->ring[psli->fcp_ring];
10425 lpfc_sli_abort_iocb_ring(phba, pring);
10426}
10427
10428/**
10429 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
10430 * @phba: pointer to lpfc hba data structure.
10431 *
10432 * This routine is called to prepare the SLI4 device for PCI slot reset. It
10433 * disables the device interrupt and pci device, and aborts the internal FCP
10434 * pending I/Os.
10435 **/
10436static void
10437lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
10438{
10439 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10440 "2826 PCI channel disable preparing for reset\n");
10441
10442 /* Block any management I/Os to the device */
James Smart618a5232012-06-12 13:54:36 -040010443 lpfc_block_mgmt_io(phba, LPFC_MBX_NO_WAIT);
James Smart75baf692010-06-08 18:31:21 -040010444
10445 /* Block all SCSI devices' I/Os on the host */
10446 lpfc_scsi_dev_block(phba);
10447
James Smartea714f32013-04-17 20:18:47 -040010448 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
10449 lpfc_sli_flush_fcp_rings(phba);
10450
James Smart75baf692010-06-08 18:31:21 -040010451 /* stop all timers */
10452 lpfc_stop_hba_timers(phba);
10453
10454 /* Disable interrupt and pci device */
10455 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -040010456 lpfc_sli4_queue_destroy(phba);
James Smart75baf692010-06-08 18:31:21 -040010457 pci_disable_device(phba->pcidev);
James Smart75baf692010-06-08 18:31:21 -040010458}
10459
10460/**
10461 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
10462 * @phba: pointer to lpfc hba data structure.
10463 *
10464 * This routine is called to prepare the SLI4 device for PCI slot permanently
10465 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
10466 * pending I/Os.
10467 **/
10468static void
10469lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
10470{
10471 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10472 "2827 PCI channel permanent disable for failure\n");
10473
10474 /* Block all SCSI devices' I/Os on the host */
10475 lpfc_scsi_dev_block(phba);
10476
10477 /* stop all timers */
10478 lpfc_stop_hba_timers(phba);
10479
10480 /* Clean up all driver's outstanding SCSI I/Os */
10481 lpfc_sli_flush_fcp_rings(phba);
10482}
10483
10484/**
James Smartda0436e2009-05-22 14:51:39 -040010485 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
10486 * @pdev: pointer to PCI device.
10487 * @state: the current PCI connection state.
10488 *
10489 * This routine is called from the PCI subsystem for error handling to device
10490 * with SLI-4 interface spec. This function is called by the PCI subsystem
10491 * after a PCI bus error affecting this device has been detected. When this
10492 * function is invoked, it will need to stop all the I/Os and interrupt(s)
10493 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
10494 * for the PCI subsystem to perform proper recovery as desired.
10495 *
10496 * Return codes
10497 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10498 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10499 **/
10500static pci_ers_result_t
10501lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
10502{
James Smart75baf692010-06-08 18:31:21 -040010503 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10504 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10505
10506 switch (state) {
10507 case pci_channel_io_normal:
10508 /* Non-fatal error, prepare for recovery */
10509 lpfc_sli4_prep_dev_for_recover(phba);
10510 return PCI_ERS_RESULT_CAN_RECOVER;
10511 case pci_channel_io_frozen:
10512 /* Fatal error, prepare for slot reset */
10513 lpfc_sli4_prep_dev_for_reset(phba);
10514 return PCI_ERS_RESULT_NEED_RESET;
10515 case pci_channel_io_perm_failure:
10516 /* Permanent failure, prepare for device down */
10517 lpfc_sli4_prep_dev_for_perm_failure(phba);
10518 return PCI_ERS_RESULT_DISCONNECT;
10519 default:
10520 /* Unknown state, prepare and request slot reset */
10521 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10522 "2825 Unknown PCI error state: x%x\n", state);
10523 lpfc_sli4_prep_dev_for_reset(phba);
10524 return PCI_ERS_RESULT_NEED_RESET;
10525 }
James Smartda0436e2009-05-22 14:51:39 -040010526}
10527
10528/**
10529 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
10530 * @pdev: pointer to PCI device.
10531 *
10532 * This routine is called from the PCI subsystem for error handling to device
10533 * with SLI-4 interface spec. It is called after PCI bus has been reset to
10534 * restart the PCI card from scratch, as if from a cold-boot. During the
10535 * PCI subsystem error recovery, after the driver returns
10536 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
10537 * recovery and then call this routine before calling the .resume method to
10538 * recover the device. This function will initialize the HBA device, enable
10539 * the interrupt, but it will just put the HBA to offline state without
10540 * passing any I/O traffic.
10541 *
10542 * Return codes
10543 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10544 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10545 */
10546static pci_ers_result_t
10547lpfc_io_slot_reset_s4(struct pci_dev *pdev)
10548{
James Smart75baf692010-06-08 18:31:21 -040010549 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10550 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10551 struct lpfc_sli *psli = &phba->sli;
10552 uint32_t intr_mode;
10553
10554 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
10555 if (pci_enable_device_mem(pdev)) {
10556 printk(KERN_ERR "lpfc: Cannot re-enable "
10557 "PCI device after reset.\n");
10558 return PCI_ERS_RESULT_DISCONNECT;
10559 }
10560
10561 pci_restore_state(pdev);
James Smart0a96e972011-07-22 18:37:28 -040010562
10563 /*
10564 * As the new kernel behavior of pci_restore_state() API call clears
10565 * device saved_state flag, need to save the restored state again.
10566 */
10567 pci_save_state(pdev);
10568
James Smart75baf692010-06-08 18:31:21 -040010569 if (pdev->is_busmaster)
10570 pci_set_master(pdev);
10571
10572 spin_lock_irq(&phba->hbalock);
10573 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
10574 spin_unlock_irq(&phba->hbalock);
10575
10576 /* Configure and enable interrupt */
10577 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
10578 if (intr_mode == LPFC_INTR_ERROR) {
10579 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10580 "2824 Cannot re-enable interrupt after "
10581 "slot reset.\n");
10582 return PCI_ERS_RESULT_DISCONNECT;
10583 } else
10584 phba->intr_mode = intr_mode;
10585
10586 /* Log the current active interrupt mode */
10587 lpfc_log_intr_mode(phba, phba->intr_mode);
10588
James Smartda0436e2009-05-22 14:51:39 -040010589 return PCI_ERS_RESULT_RECOVERED;
10590}
10591
10592/**
10593 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
10594 * @pdev: pointer to PCI device
10595 *
10596 * This routine is called from the PCI subsystem for error handling to device
10597 * with SLI-4 interface spec. It is called when kernel error recovery tells
10598 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
10599 * error recovery. After this call, traffic can start to flow from this device
10600 * again.
10601 **/
10602static void
10603lpfc_io_resume_s4(struct pci_dev *pdev)
10604{
James Smart75baf692010-06-08 18:31:21 -040010605 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10606 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10607
10608 /*
10609 * In case of slot reset, as function reset is performed through
10610 * mailbox command which needs DMA to be enabled, this operation
10611 * has to be moved to the io resume phase. Taking device offline
10612 * will perform the necessary cleanup.
10613 */
10614 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
10615 /* Perform device reset */
James Smart618a5232012-06-12 13:54:36 -040010616 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart75baf692010-06-08 18:31:21 -040010617 lpfc_offline(phba);
10618 lpfc_sli_brdrestart(phba);
10619 /* Bring the device back online */
10620 lpfc_online(phba);
10621 }
10622
10623 /* Clean up Advanced Error Reporting (AER) if needed */
10624 if (phba->hba_flag & HBA_AER_ENABLED)
10625 pci_cleanup_aer_uncorrect_error_status(pdev);
James Smartda0436e2009-05-22 14:51:39 -040010626}
10627
10628/**
James Smart3772a992009-05-22 14:50:54 -040010629 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
10630 * @pdev: pointer to PCI device
10631 * @pid: pointer to PCI device identifier
10632 *
10633 * This routine is to be registered to the kernel's PCI subsystem. When an
10634 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
10635 * at PCI device-specific information of the device and driver to see if the
10636 * driver state that it can support this kind of device. If the match is
10637 * successful, the driver core invokes this routine. This routine dispatches
10638 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
10639 * do all the initialization that it needs to do to handle the HBA device
10640 * properly.
10641 *
10642 * Return code
10643 * 0 - driver can claim the device
10644 * negative value - driver can not claim the device
10645 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010646static int
James Smart3772a992009-05-22 14:50:54 -040010647lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
10648{
10649 int rc;
James Smart8fa38512009-07-19 10:01:03 -040010650 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -040010651
James Smart28baac72010-02-12 14:42:03 -050010652 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -040010653 return -ENODEV;
10654
James Smart8fa38512009-07-19 10:01:03 -040010655 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -050010656 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -040010657 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040010658 else
James Smart3772a992009-05-22 14:50:54 -040010659 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -040010660
James Smart3772a992009-05-22 14:50:54 -040010661 return rc;
10662}
10663
10664/**
10665 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
10666 * @pdev: pointer to PCI device
10667 *
10668 * This routine is to be registered to the kernel's PCI subsystem. When an
10669 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
10670 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
10671 * remove routine, which will perform all the necessary cleanup for the
10672 * device to be removed from the PCI subsystem properly.
10673 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080010674static void
James Smart3772a992009-05-22 14:50:54 -040010675lpfc_pci_remove_one(struct pci_dev *pdev)
10676{
10677 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10678 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10679
10680 switch (phba->pci_dev_grp) {
10681 case LPFC_PCI_DEV_LP:
10682 lpfc_pci_remove_one_s3(pdev);
10683 break;
James Smartda0436e2009-05-22 14:51:39 -040010684 case LPFC_PCI_DEV_OC:
10685 lpfc_pci_remove_one_s4(pdev);
10686 break;
James Smart3772a992009-05-22 14:50:54 -040010687 default:
10688 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10689 "1424 Invalid PCI device group: 0x%x\n",
10690 phba->pci_dev_grp);
10691 break;
10692 }
10693 return;
10694}
10695
10696/**
10697 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
10698 * @pdev: pointer to PCI device
10699 * @msg: power management message
10700 *
10701 * This routine is to be registered to the kernel's PCI subsystem to support
10702 * system Power Management (PM). When PM invokes this method, it dispatches
10703 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
10704 * suspend the device.
10705 *
10706 * Return code
10707 * 0 - driver suspended the device
10708 * Error otherwise
10709 **/
10710static int
10711lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
10712{
10713 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10714 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10715 int rc = -ENODEV;
10716
10717 switch (phba->pci_dev_grp) {
10718 case LPFC_PCI_DEV_LP:
10719 rc = lpfc_pci_suspend_one_s3(pdev, msg);
10720 break;
James Smartda0436e2009-05-22 14:51:39 -040010721 case LPFC_PCI_DEV_OC:
10722 rc = lpfc_pci_suspend_one_s4(pdev, msg);
10723 break;
James Smart3772a992009-05-22 14:50:54 -040010724 default:
10725 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10726 "1425 Invalid PCI device group: 0x%x\n",
10727 phba->pci_dev_grp);
10728 break;
10729 }
10730 return rc;
10731}
10732
10733/**
10734 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
10735 * @pdev: pointer to PCI device
10736 *
10737 * This routine is to be registered to the kernel's PCI subsystem to support
10738 * system Power Management (PM). When PM invokes this method, it dispatches
10739 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
10740 * resume the device.
10741 *
10742 * Return code
10743 * 0 - driver suspended the device
10744 * Error otherwise
10745 **/
10746static int
10747lpfc_pci_resume_one(struct pci_dev *pdev)
10748{
10749 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10750 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10751 int rc = -ENODEV;
10752
10753 switch (phba->pci_dev_grp) {
10754 case LPFC_PCI_DEV_LP:
10755 rc = lpfc_pci_resume_one_s3(pdev);
10756 break;
James Smartda0436e2009-05-22 14:51:39 -040010757 case LPFC_PCI_DEV_OC:
10758 rc = lpfc_pci_resume_one_s4(pdev);
10759 break;
James Smart3772a992009-05-22 14:50:54 -040010760 default:
10761 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10762 "1426 Invalid PCI device group: 0x%x\n",
10763 phba->pci_dev_grp);
10764 break;
10765 }
10766 return rc;
10767}
10768
10769/**
10770 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
10771 * @pdev: pointer to PCI device.
10772 * @state: the current PCI connection state.
10773 *
10774 * This routine is registered to the PCI subsystem for error handling. This
10775 * function is called by the PCI subsystem after a PCI bus error affecting
10776 * this device has been detected. When this routine is invoked, it dispatches
10777 * the action to the proper SLI-3 or SLI-4 device error detected handling
10778 * routine, which will perform the proper error detected operation.
10779 *
10780 * Return codes
10781 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
10782 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10783 **/
10784static pci_ers_result_t
10785lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
10786{
10787 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10788 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10789 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
10790
10791 switch (phba->pci_dev_grp) {
10792 case LPFC_PCI_DEV_LP:
10793 rc = lpfc_io_error_detected_s3(pdev, state);
10794 break;
James Smartda0436e2009-05-22 14:51:39 -040010795 case LPFC_PCI_DEV_OC:
10796 rc = lpfc_io_error_detected_s4(pdev, state);
10797 break;
James Smart3772a992009-05-22 14:50:54 -040010798 default:
10799 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10800 "1427 Invalid PCI device group: 0x%x\n",
10801 phba->pci_dev_grp);
10802 break;
10803 }
10804 return rc;
10805}
10806
10807/**
10808 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
10809 * @pdev: pointer to PCI device.
10810 *
10811 * This routine is registered to the PCI subsystem for error handling. This
10812 * function is called after PCI bus has been reset to restart the PCI card
10813 * from scratch, as if from a cold-boot. When this routine is invoked, it
10814 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
10815 * routine, which will perform the proper device reset.
10816 *
10817 * Return codes
10818 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
10819 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
10820 **/
10821static pci_ers_result_t
10822lpfc_io_slot_reset(struct pci_dev *pdev)
10823{
10824 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10825 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10826 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
10827
10828 switch (phba->pci_dev_grp) {
10829 case LPFC_PCI_DEV_LP:
10830 rc = lpfc_io_slot_reset_s3(pdev);
10831 break;
James Smartda0436e2009-05-22 14:51:39 -040010832 case LPFC_PCI_DEV_OC:
10833 rc = lpfc_io_slot_reset_s4(pdev);
10834 break;
James Smart3772a992009-05-22 14:50:54 -040010835 default:
10836 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10837 "1428 Invalid PCI device group: 0x%x\n",
10838 phba->pci_dev_grp);
10839 break;
10840 }
10841 return rc;
10842}
10843
10844/**
10845 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
10846 * @pdev: pointer to PCI device
10847 *
10848 * This routine is registered to the PCI subsystem for error handling. It
10849 * is called when kernel error recovery tells the lpfc driver that it is
10850 * OK to resume normal PCI operation after PCI bus error recovery. When
10851 * this routine is invoked, it dispatches the action to the proper SLI-3
10852 * or SLI-4 device io_resume routine, which will resume the device operation.
10853 **/
10854static void
10855lpfc_io_resume(struct pci_dev *pdev)
10856{
10857 struct Scsi_Host *shost = pci_get_drvdata(pdev);
10858 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
10859
10860 switch (phba->pci_dev_grp) {
10861 case LPFC_PCI_DEV_LP:
10862 lpfc_io_resume_s3(pdev);
10863 break;
James Smartda0436e2009-05-22 14:51:39 -040010864 case LPFC_PCI_DEV_OC:
10865 lpfc_io_resume_s4(pdev);
10866 break;
James Smart3772a992009-05-22 14:50:54 -040010867 default:
10868 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10869 "1429 Invalid PCI device group: 0x%x\n",
10870 phba->pci_dev_grp);
10871 break;
10872 }
10873 return;
10874}
10875
James Smart1ba981f2014-02-20 09:56:45 -050010876/**
10877 * lpfc_sli4_oas_verify - Verify OAS is supported by this adapter
10878 * @phba: pointer to lpfc hba data structure.
10879 *
10880 * This routine checks to see if OAS is supported for this adapter. If
10881 * supported, the configure Flash Optimized Fabric flag is set. Otherwise,
10882 * the enable oas flag is cleared and the pool created for OAS device data
10883 * is destroyed.
10884 *
10885 **/
10886void
10887lpfc_sli4_oas_verify(struct lpfc_hba *phba)
10888{
10889
10890 if (!phba->cfg_EnableXLane)
10891 return;
10892
10893 if (phba->sli4_hba.pc_sli4_params.oas_supported) {
10894 phba->cfg_fof = 1;
10895 } else {
10896 phba->cfg_EnableXLane = 0;
10897 if (phba->device_data_mem_pool)
10898 mempool_destroy(phba->device_data_mem_pool);
10899 phba->device_data_mem_pool = NULL;
10900 }
10901
10902 return;
10903}
10904
10905/**
10906 * lpfc_fof_queue_setup - Set up all the fof queues
10907 * @phba: pointer to lpfc hba data structure.
10908 *
10909 * This routine is invoked to set up all the fof queues for the FC HBA
10910 * operation.
10911 *
10912 * Return codes
10913 * 0 - successful
10914 * -ENOMEM - No available memory
10915 **/
10916int
10917lpfc_fof_queue_setup(struct lpfc_hba *phba)
10918{
10919 struct lpfc_sli *psli = &phba->sli;
10920 int rc;
10921
10922 rc = lpfc_eq_create(phba, phba->sli4_hba.fof_eq, LPFC_MAX_IMAX);
10923 if (rc)
10924 return -ENOMEM;
10925
10926 if (phba->cfg_EnableXLane) {
10927
10928 rc = lpfc_cq_create(phba, phba->sli4_hba.oas_cq,
10929 phba->sli4_hba.fof_eq, LPFC_WCQ, LPFC_FCP);
10930 if (rc)
10931 goto out_oas_cq;
10932
10933 rc = lpfc_wq_create(phba, phba->sli4_hba.oas_wq,
10934 phba->sli4_hba.oas_cq, LPFC_FCP);
10935 if (rc)
10936 goto out_oas_wq;
10937
10938 phba->sli4_hba.oas_cq->pring = &psli->ring[LPFC_FCP_OAS_RING];
10939 phba->sli4_hba.oas_ring = &psli->ring[LPFC_FCP_OAS_RING];
10940 }
10941
10942 return 0;
10943
10944out_oas_wq:
10945 if (phba->cfg_EnableXLane)
10946 lpfc_cq_destroy(phba, phba->sli4_hba.oas_cq);
10947out_oas_cq:
10948 lpfc_eq_destroy(phba, phba->sli4_hba.fof_eq);
10949 return rc;
10950
10951}
10952
10953/**
10954 * lpfc_fof_queue_create - Create all the fof queues
10955 * @phba: pointer to lpfc hba data structure.
10956 *
10957 * This routine is invoked to allocate all the fof queues for the FC HBA
10958 * operation. For each SLI4 queue type, the parameters such as queue entry
10959 * count (queue depth) shall be taken from the module parameter. For now,
10960 * we just use some constant number as place holder.
10961 *
10962 * Return codes
10963 * 0 - successful
10964 * -ENOMEM - No availble memory
10965 * -EIO - The mailbox failed to complete successfully.
10966 **/
10967int
10968lpfc_fof_queue_create(struct lpfc_hba *phba)
10969{
10970 struct lpfc_queue *qdesc;
10971
10972 /* Create FOF EQ */
10973 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
10974 phba->sli4_hba.eq_ecount);
10975 if (!qdesc)
10976 goto out_error;
10977
10978 phba->sli4_hba.fof_eq = qdesc;
10979
10980 if (phba->cfg_EnableXLane) {
10981
10982 /* Create OAS CQ */
10983 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
10984 phba->sli4_hba.cq_ecount);
10985 if (!qdesc)
10986 goto out_error;
10987
10988 phba->sli4_hba.oas_cq = qdesc;
10989
10990 /* Create OAS WQ */
10991 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
10992 phba->sli4_hba.wq_ecount);
10993 if (!qdesc)
10994 goto out_error;
10995
10996 phba->sli4_hba.oas_wq = qdesc;
10997
10998 }
10999 return 0;
11000
11001out_error:
11002 lpfc_fof_queue_destroy(phba);
11003 return -ENOMEM;
11004}
11005
11006/**
11007 * lpfc_fof_queue_destroy - Destroy all the fof queues
11008 * @phba: pointer to lpfc hba data structure.
11009 *
11010 * This routine is invoked to release all the SLI4 queues with the FC HBA
11011 * operation.
11012 *
11013 * Return codes
11014 * 0 - successful
11015 **/
11016int
11017lpfc_fof_queue_destroy(struct lpfc_hba *phba)
11018{
11019 /* Release FOF Event queue */
11020 if (phba->sli4_hba.fof_eq != NULL) {
11021 lpfc_sli4_queue_free(phba->sli4_hba.fof_eq);
11022 phba->sli4_hba.fof_eq = NULL;
11023 }
11024
11025 /* Release OAS Completion queue */
11026 if (phba->sli4_hba.oas_cq != NULL) {
11027 lpfc_sli4_queue_free(phba->sli4_hba.oas_cq);
11028 phba->sli4_hba.oas_cq = NULL;
11029 }
11030
11031 /* Release OAS Work queue */
11032 if (phba->sli4_hba.oas_wq != NULL) {
11033 lpfc_sli4_queue_free(phba->sli4_hba.oas_wq);
11034 phba->sli4_hba.oas_wq = NULL;
11035 }
11036 return 0;
11037}
11038
dea31012005-04-17 16:05:31 -050011039static struct pci_device_id lpfc_id_table[] = {
11040 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
11041 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -040011042 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
11043 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011044 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
11045 PCI_ANY_ID, PCI_ANY_ID, },
11046 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
11047 PCI_ANY_ID, PCI_ANY_ID, },
11048 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
11049 PCI_ANY_ID, PCI_ANY_ID, },
11050 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
11051 PCI_ANY_ID, PCI_ANY_ID, },
11052 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
11053 PCI_ANY_ID, PCI_ANY_ID, },
11054 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
11055 PCI_ANY_ID, PCI_ANY_ID, },
11056 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
11057 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050011058 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
11059 PCI_ANY_ID, PCI_ANY_ID, },
11060 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
11061 PCI_ANY_ID, PCI_ANY_ID, },
11062 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
11063 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011064 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
11065 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050011066 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
11067 PCI_ANY_ID, PCI_ANY_ID, },
11068 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
11069 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011070 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
11071 PCI_ANY_ID, PCI_ANY_ID, },
11072 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
11073 PCI_ANY_ID, PCI_ANY_ID, },
11074 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
11075 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -040011076 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
11077 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050011078 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
11079 PCI_ANY_ID, PCI_ANY_ID, },
11080 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
11081 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011082 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
11083 PCI_ANY_ID, PCI_ANY_ID, },
11084 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
11085 PCI_ANY_ID, PCI_ANY_ID, },
11086 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
11087 PCI_ANY_ID, PCI_ANY_ID, },
11088 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
11089 PCI_ANY_ID, PCI_ANY_ID, },
11090 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
11091 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -050011092 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
11093 PCI_ANY_ID, PCI_ANY_ID, },
11094 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
11095 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -040011096 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
11097 PCI_ANY_ID, PCI_ANY_ID, },
11098 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
11099 PCI_ANY_ID, PCI_ANY_ID, },
11100 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
11101 PCI_ANY_ID, PCI_ANY_ID, },
11102 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
11103 PCI_ANY_ID, PCI_ANY_ID, },
11104 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
11105 PCI_ANY_ID, PCI_ANY_ID, },
11106 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
11107 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -040011108 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
11109 PCI_ANY_ID, PCI_ANY_ID, },
11110 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
11111 PCI_ANY_ID, PCI_ANY_ID, },
11112 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
11113 PCI_ANY_ID, PCI_ANY_ID, },
James Smart3772a992009-05-22 14:50:54 -040011114 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
11115 PCI_ANY_ID, PCI_ANY_ID, },
James Smarta747c9c2009-11-18 15:41:10 -050011116 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
11117 PCI_ANY_ID, PCI_ANY_ID, },
11118 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
James Smart6669f9b2009-10-02 15:16:45 -040011119 PCI_ANY_ID, PCI_ANY_ID, },
James Smart98fc5dd2010-06-07 15:24:29 -040011120 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BALIUS,
11121 PCI_ANY_ID, PCI_ANY_ID, },
James Smart085c6472010-11-20 23:11:37 -050011122 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC,
11123 PCI_ANY_ID, PCI_ANY_ID, },
11124 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE,
11125 PCI_ANY_ID, PCI_ANY_ID, },
James Smartc0c11512011-05-24 11:41:34 -040011126 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC_VF,
11127 PCI_ANY_ID, PCI_ANY_ID, },
11128 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE_VF,
11129 PCI_ANY_ID, PCI_ANY_ID, },
James Smartf8cafd32012-08-03 12:42:51 -040011130 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SKYHAWK,
11131 PCI_ANY_ID, PCI_ANY_ID, },
11132 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SKYHAWK_VF,
11133 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -050011134 { 0 }
11135};
11136
11137MODULE_DEVICE_TABLE(pci, lpfc_id_table);
11138
Stephen Hemmingera55b2d22012-09-07 09:33:16 -070011139static const struct pci_error_handlers lpfc_err_handler = {
Linas Vepstas8d63f372007-02-14 14:28:36 -060011140 .error_detected = lpfc_io_error_detected,
11141 .slot_reset = lpfc_io_slot_reset,
11142 .resume = lpfc_io_resume,
11143};
11144
dea31012005-04-17 16:05:31 -050011145static struct pci_driver lpfc_driver = {
11146 .name = LPFC_DRIVER_NAME,
11147 .id_table = lpfc_id_table,
11148 .probe = lpfc_pci_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080011149 .remove = lpfc_pci_remove_one,
James Smart3a55b532008-12-04 22:38:54 -050011150 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -040011151 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -050011152 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -050011153};
11154
James Smart3ef6d242012-01-18 16:23:48 -050011155static const struct file_operations lpfc_mgmt_fop = {
Al Viro858feac2013-04-14 22:39:37 -040011156 .owner = THIS_MODULE,
James Smart3ef6d242012-01-18 16:23:48 -050011157};
11158
11159static struct miscdevice lpfc_mgmt_dev = {
11160 .minor = MISC_DYNAMIC_MINOR,
11161 .name = "lpfcmgmt",
11162 .fops = &lpfc_mgmt_fop,
11163};
11164
James Smarte59058c2008-08-24 21:49:00 -040011165/**
James Smart3621a712009-04-06 18:47:14 -040011166 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -040011167 *
11168 * This routine is to be invoked when the lpfc module is loaded into the
11169 * kernel. The special kernel macro module_init() is used to indicate the
11170 * role of this routine to the kernel as lpfc module entry point.
11171 *
11172 * Return codes
11173 * 0 - successful
11174 * -ENOMEM - FC attach transport failed
11175 * all others - failed
11176 */
dea31012005-04-17 16:05:31 -050011177static int __init
11178lpfc_init(void)
11179{
James Smart7bb03bb2013-04-17 20:19:16 -040011180 int cpu;
dea31012005-04-17 16:05:31 -050011181 int error = 0;
11182
11183 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040011184 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -050011185
James Smart3ef6d242012-01-18 16:23:48 -050011186 error = misc_register(&lpfc_mgmt_dev);
11187 if (error)
11188 printk(KERN_ERR "Could not register lpfcmgmt device, "
11189 "misc_register returned with status %d", error);
11190
James Smart7ee5d432007-10-27 13:37:17 -040011191 if (lpfc_enable_npiv) {
11192 lpfc_transport_functions.vport_create = lpfc_vport_create;
11193 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
11194 }
dea31012005-04-17 16:05:31 -050011195 lpfc_transport_template =
11196 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -040011197 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -050011198 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -040011199 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -040011200 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -040011201 fc_attach_transport(&lpfc_vport_transport_functions);
11202 if (lpfc_vport_transport_template == NULL) {
11203 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040011204 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -040011205 }
James Smart7ee5d432007-10-27 13:37:17 -040011206 }
James Smart7bb03bb2013-04-17 20:19:16 -040011207
11208 /* Initialize in case vector mapping is needed */
James Smartb246de12013-05-31 17:03:07 -040011209 lpfc_used_cpu = NULL;
11210 lpfc_present_cpu = 0;
11211 for_each_present_cpu(cpu)
11212 lpfc_present_cpu++;
James Smart7bb03bb2013-04-17 20:19:16 -040011213
dea31012005-04-17 16:05:31 -050011214 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -050011215 if (error) {
dea31012005-04-17 16:05:31 -050011216 fc_release_transport(lpfc_transport_template);
James Smartd7c255b2008-08-24 21:50:00 -040011217 if (lpfc_enable_npiv)
11218 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -050011219 }
dea31012005-04-17 16:05:31 -050011220
11221 return error;
11222}
11223
James Smarte59058c2008-08-24 21:49:00 -040011224/**
James Smart3621a712009-04-06 18:47:14 -040011225 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -040011226 *
11227 * This routine is invoked when the lpfc module is removed from the kernel.
11228 * The special kernel macro module_exit() is used to indicate the role of
11229 * this routine to the kernel as lpfc module exit point.
11230 */
dea31012005-04-17 16:05:31 -050011231static void __exit
11232lpfc_exit(void)
11233{
James Smart3ef6d242012-01-18 16:23:48 -050011234 misc_deregister(&lpfc_mgmt_dev);
dea31012005-04-17 16:05:31 -050011235 pci_unregister_driver(&lpfc_driver);
11236 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040011237 if (lpfc_enable_npiv)
11238 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -050011239 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040011240 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
11241 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050011242 (1L << _dump_buf_data_order), _dump_buf_data);
11243 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
11244 }
11245
11246 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -040011247 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
11248 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050011249 (1L << _dump_buf_dif_order), _dump_buf_dif);
11250 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
11251 }
James Smartb246de12013-05-31 17:03:07 -040011252 kfree(lpfc_used_cpu);
dea31012005-04-17 16:05:31 -050011253}
11254
11255module_init(lpfc_init);
11256module_exit(lpfc_exit);
11257MODULE_LICENSE("GPL");
11258MODULE_DESCRIPTION(LPFC_MODULE_DESC);
11259MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
11260MODULE_VERSION("0:" LPFC_DRIVER_VERSION);