blob: e81912cd257e8c0bfa79bfa7217842fadfb523c2 [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 Smart792581d2011-03-11 16:06:44 -05004 * Copyright (C) 2004-2011 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>
27#include <linux/kthread.h>
28#include <linux/pci.h>
29#include <linux/spinlock.h>
James Smart92d7f7b2007-06-17 19:56:38 -050030#include <linux/ctype.h>
James Smart0d878412009-10-02 15:16:56 -040031#include <linux/aer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
dea31012005-04-17 16:05:31 -050033
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040034#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050035#include <scsi/scsi_device.h>
36#include <scsi/scsi_host.h>
37#include <scsi/scsi_transport_fc.h>
38
James Smartda0436e2009-05-22 14:51:39 -040039#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050040#include "lpfc_hw.h"
41#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040042#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040043#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050044#include "lpfc_disc.h"
45#include "lpfc_scsi.h"
46#include "lpfc.h"
47#include "lpfc_logmsg.h"
48#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050049#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050050#include "lpfc_version.h"
51
James Smart81301a92008-12-04 22:39:46 -050052char *_dump_buf_data;
53unsigned long _dump_buf_data_order;
54char *_dump_buf_dif;
55unsigned long _dump_buf_dif_order;
56spinlock_t _dump_buf_lock;
57
dea31012005-04-17 16:05:31 -050058static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
59static int lpfc_post_rcv_buf(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040060static int lpfc_sli4_queue_create(struct lpfc_hba *);
61static void lpfc_sli4_queue_destroy(struct lpfc_hba *);
62static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
63static int lpfc_setup_endian_order(struct lpfc_hba *);
64static int lpfc_sli4_read_config(struct lpfc_hba *);
65static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
66static void lpfc_free_sgl_list(struct lpfc_hba *);
67static int lpfc_init_sgl_list(struct lpfc_hba *);
68static int lpfc_init_active_sgl_array(struct lpfc_hba *);
69static void lpfc_free_active_sgl(struct lpfc_hba *);
70static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
71static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
72static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
73static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
74static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
dea31012005-04-17 16:05:31 -050075
76static struct scsi_transport_template *lpfc_transport_template = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050077static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea31012005-04-17 16:05:31 -050078static DEFINE_IDR(lpfc_hba_index);
79
James Smarte59058c2008-08-24 21:49:00 -040080/**
James Smart3621a712009-04-06 18:47:14 -040081 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
James Smarte59058c2008-08-24 21:49:00 -040082 * @phba: pointer to lpfc hba data structure.
83 *
84 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
85 * mailbox command. It retrieves the revision information from the HBA and
86 * collects the Vital Product Data (VPD) about the HBA for preparing the
87 * configuration of the HBA.
88 *
89 * Return codes:
90 * 0 - success.
91 * -ERESTART - requests the SLI layer to reset the HBA and try again.
92 * Any other value - indicates an error.
93 **/
dea31012005-04-17 16:05:31 -050094int
James Smart2e0fef82007-06-17 19:56:36 -050095lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050096{
97 lpfc_vpd_t *vp = &phba->vpd;
98 int i = 0, rc;
99 LPFC_MBOXQ_t *pmb;
100 MAILBOX_t *mb;
101 char *lpfc_vpd_data = NULL;
102 uint16_t offset = 0;
103 static char licensed[56] =
104 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -0400105 static int init_key = 1;
dea31012005-04-17 16:05:31 -0500106
107 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
108 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500109 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500110 return -ENOMEM;
111 }
112
James Smart04c68492009-05-22 14:52:52 -0400113 mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -0500114 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500115
116 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -0400117 if (init_key) {
118 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -0500119
James Smart65a29c12006-07-06 15:50:50 -0400120 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
121 *ptext = cpu_to_be32(*ptext);
122 init_key = 0;
123 }
dea31012005-04-17 16:05:31 -0500124
125 lpfc_read_nv(phba, pmb);
126 memset((char*)mb->un.varRDnvp.rsvd3, 0,
127 sizeof (mb->un.varRDnvp.rsvd3));
128 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
129 sizeof (licensed));
130
131 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
132
133 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500134 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smarte8b62012007-08-02 11:10:09 -0400135 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500136 "error, mbxCmd x%x READ_NVPARM, "
137 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500138 mb->mbxCommand, mb->mbxStatus);
139 mempool_free(pmb, phba->mbox_mem_pool);
140 return -ERESTART;
141 }
142 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500143 sizeof(phba->wwnn));
144 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
145 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500146 }
147
James Smart92d7f7b2007-06-17 19:56:38 -0500148 phba->sli3_options = 0x0;
149
dea31012005-04-17 16:05:31 -0500150 /* Setup and issue mailbox READ REV command */
151 lpfc_read_rev(phba, pmb);
152 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
153 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500154 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400155 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500156 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500157 mb->mbxCommand, mb->mbxStatus);
158 mempool_free( pmb, phba->mbox_mem_pool);
159 return -ERESTART;
160 }
161
James Smart92d7f7b2007-06-17 19:56:38 -0500162
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500163 /*
164 * The value of rr must be 1 since the driver set the cv field to 1.
165 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500166 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500167 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500168 vp->rev.rBit = 0;
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500169 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400170 "0440 Adapter failed to init, READ_REV has "
171 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500172 mempool_free(pmb, phba->mbox_mem_pool);
173 return -ERESTART;
dea31012005-04-17 16:05:31 -0500174 }
175
James Smart495a7142008-06-14 22:52:59 -0400176 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
177 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -0500178 return -EINVAL;
James Smart495a7142008-06-14 22:52:59 -0400179 }
James Smarted957682007-06-17 19:56:37 -0500180
dea31012005-04-17 16:05:31 -0500181 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500182 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500183 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500184 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
185 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
186 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
187 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500188 vp->rev.biuRev = mb->un.varRdRev.biuRev;
189 vp->rev.smRev = mb->un.varRdRev.smRev;
190 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
191 vp->rev.endecRev = mb->un.varRdRev.endecRev;
192 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
193 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
194 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
195 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
196 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
197 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
198
James Smart92d7f7b2007-06-17 19:56:38 -0500199 /* If the sli feature level is less then 9, we must
200 * tear down all RPIs and VPIs on link down if NPIV
201 * is enabled.
202 */
203 if (vp->rev.feaLevelHigh < 9)
204 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
205
dea31012005-04-17 16:05:31 -0500206 if (lpfc_is_LC_HBA(phba->pcidev->device))
207 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
208 sizeof (phba->RandomData));
209
dea31012005-04-17 16:05:31 -0500210 /* Get adapter VPD information */
dea31012005-04-17 16:05:31 -0500211 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
212 if (!lpfc_vpd_data)
James Smartd7c255b2008-08-24 21:50:00 -0400213 goto out_free_mbox;
dea31012005-04-17 16:05:31 -0500214
215 do {
James Smarta0c87cb2009-07-19 10:01:10 -0400216 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
dea31012005-04-17 16:05:31 -0500217 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
218
219 if (rc != MBX_SUCCESS) {
220 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400221 "0441 VPD not present on adapter, "
dea31012005-04-17 16:05:31 -0500222 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500223 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500224 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500225 }
James Smart04c68492009-05-22 14:52:52 -0400226 /* dump mem may return a zero when finished or we got a
227 * mailbox error, either way we are done.
228 */
229 if (mb->un.varDmp.word_cnt == 0)
230 break;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500231 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
232 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
James Smartd7c255b2008-08-24 21:50:00 -0400233 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
234 lpfc_vpd_data + offset,
James Smart92d7f7b2007-06-17 19:56:38 -0500235 mb->un.varDmp.word_cnt);
dea31012005-04-17 16:05:31 -0500236 offset += mb->un.varDmp.word_cnt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500237 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
238 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500239
240 kfree(lpfc_vpd_data);
dea31012005-04-17 16:05:31 -0500241out_free_mbox:
242 mempool_free(pmb, phba->mbox_mem_pool);
243 return 0;
244}
245
James Smarte59058c2008-08-24 21:49:00 -0400246/**
James Smart3621a712009-04-06 18:47:14 -0400247 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
James Smarte59058c2008-08-24 21:49:00 -0400248 * @phba: pointer to lpfc hba data structure.
249 * @pmboxq: pointer to the driver internal queue element for mailbox command.
250 *
251 * This is the completion handler for driver's configuring asynchronous event
252 * mailbox command to the device. If the mailbox command returns successfully,
253 * it will set internal async event support flag to 1; otherwise, it will
254 * set internal async event support flag to 0.
255 **/
James Smart57127f12007-10-27 13:37:05 -0400256static void
257lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
258{
James Smart04c68492009-05-22 14:52:52 -0400259 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
James Smart57127f12007-10-27 13:37:05 -0400260 phba->temp_sensor_support = 1;
261 else
262 phba->temp_sensor_support = 0;
263 mempool_free(pmboxq, phba->mbox_mem_pool);
264 return;
265}
266
James Smarte59058c2008-08-24 21:49:00 -0400267/**
James Smart3621a712009-04-06 18:47:14 -0400268 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
James Smart97207482008-12-04 22:39:19 -0500269 * @phba: pointer to lpfc hba data structure.
270 * @pmboxq: pointer to the driver internal queue element for mailbox command.
271 *
272 * This is the completion handler for dump mailbox command for getting
273 * wake up parameters. When this command complete, the response contain
274 * Option rom version of the HBA. This function translate the version number
275 * into a human readable string and store it in OptionROMVersion.
276 **/
277static void
278lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
279{
280 struct prog_id *prg;
281 uint32_t prog_id_word;
282 char dist = ' ';
283 /* character array used for decoding dist type. */
284 char dist_char[] = "nabx";
285
James Smart04c68492009-05-22 14:52:52 -0400286 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
James Smart9f1e1b52008-12-04 22:39:40 -0500287 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500288 return;
James Smart9f1e1b52008-12-04 22:39:40 -0500289 }
James Smart97207482008-12-04 22:39:19 -0500290
291 prg = (struct prog_id *) &prog_id_word;
292
293 /* word 7 contain option rom version */
James Smart04c68492009-05-22 14:52:52 -0400294 prog_id_word = pmboxq->u.mb.un.varWords[7];
James Smart97207482008-12-04 22:39:19 -0500295
296 /* Decode the Option rom version word to a readable string */
297 if (prg->dist < 4)
298 dist = dist_char[prg->dist];
299
300 if ((prg->dist == 3) && (prg->num == 0))
301 sprintf(phba->OptionROMVersion, "%d.%d%d",
302 prg->ver, prg->rev, prg->lev);
303 else
304 sprintf(phba->OptionROMVersion, "%d.%d%d%c%d",
305 prg->ver, prg->rev, prg->lev,
306 dist, prg->num);
James Smart9f1e1b52008-12-04 22:39:40 -0500307 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500308 return;
309}
310
311/**
James Smart05580562011-05-24 11:40:48 -0400312 * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
313 * cfg_soft_wwnn, cfg_soft_wwpn
314 * @vport: pointer to lpfc vport data structure.
315 *
316 *
317 * Return codes
318 * None.
319 **/
320void
321lpfc_update_vport_wwn(struct lpfc_vport *vport)
322{
323 /* If the soft name exists then update it using the service params */
324 if (vport->phba->cfg_soft_wwnn)
325 u64_to_wwn(vport->phba->cfg_soft_wwnn,
326 vport->fc_sparam.nodeName.u.wwn);
327 if (vport->phba->cfg_soft_wwpn)
328 u64_to_wwn(vport->phba->cfg_soft_wwpn,
329 vport->fc_sparam.portName.u.wwn);
330
331 /*
332 * If the name is empty or there exists a soft name
333 * then copy the service params name, otherwise use the fc name
334 */
335 if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
336 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
337 sizeof(struct lpfc_name));
338 else
339 memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
340 sizeof(struct lpfc_name));
341
342 if (vport->fc_portname.u.wwn[0] == 0 || vport->phba->cfg_soft_wwpn)
343 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
344 sizeof(struct lpfc_name));
345 else
346 memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
347 sizeof(struct lpfc_name));
348}
349
350/**
James Smart3621a712009-04-06 18:47:14 -0400351 * lpfc_config_port_post - Perform lpfc initialization after config port
James Smarte59058c2008-08-24 21:49:00 -0400352 * @phba: pointer to lpfc hba data structure.
353 *
354 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
355 * command call. It performs all internal resource and state setups on the
356 * port: post IOCB buffers, enable appropriate host interrupt attentions,
357 * ELS ring timers, etc.
358 *
359 * Return codes
360 * 0 - success.
361 * Any other value - error.
362 **/
dea31012005-04-17 16:05:31 -0500363int
James Smart2e0fef82007-06-17 19:56:36 -0500364lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500365{
James Smart2e0fef82007-06-17 19:56:36 -0500366 struct lpfc_vport *vport = phba->pport;
James Smarta257bf92009-04-06 18:48:10 -0400367 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -0500368 LPFC_MBOXQ_t *pmb;
369 MAILBOX_t *mb;
370 struct lpfc_dmabuf *mp;
371 struct lpfc_sli *psli = &phba->sli;
372 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500373 int i, j;
374 int rc;
dea31012005-04-17 16:05:31 -0500375
James Smart7af67052007-10-27 13:38:11 -0400376 spin_lock_irq(&phba->hbalock);
377 /*
378 * If the Config port completed correctly the HBA is not
379 * over heated any more.
380 */
381 if (phba->over_temp_state == HBA_OVER_TEMP)
382 phba->over_temp_state = HBA_NORMAL_TEMP;
383 spin_unlock_irq(&phba->hbalock);
384
dea31012005-04-17 16:05:31 -0500385 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
386 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500387 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500388 return -ENOMEM;
389 }
James Smart04c68492009-05-22 14:52:52 -0400390 mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500391
dea31012005-04-17 16:05:31 -0500392 /* Get login parameters for NID. */
James Smart9f1177a2010-02-26 14:12:57 -0500393 rc = lpfc_read_sparam(phba, pmb, 0);
394 if (rc) {
395 mempool_free(pmb, phba->mbox_mem_pool);
396 return -ENOMEM;
397 }
398
James Smarted957682007-06-17 19:56:37 -0500399 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500400 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500401 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400402 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500403 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500404 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500405 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500406 mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart9f1177a2010-02-26 14:12:57 -0500407 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500408 lpfc_mbuf_free(phba, mp->virt, mp->phys);
409 kfree(mp);
410 return -EIO;
411 }
412
413 mp = (struct lpfc_dmabuf *) pmb->context1;
414
James Smart2e0fef82007-06-17 19:56:36 -0500415 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500416 lpfc_mbuf_free(phba, mp->virt, mp->phys);
417 kfree(mp);
418 pmb->context1 = NULL;
James Smart05580562011-05-24 11:40:48 -0400419 lpfc_update_vport_wwn(vport);
James Smarta257bf92009-04-06 18:48:10 -0400420
421 /* Update the fc_host data structures with new wwn. */
422 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
423 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart21e9a0a2009-05-22 14:53:21 -0400424 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smarta257bf92009-04-06 18:48:10 -0400425
dea31012005-04-17 16:05:31 -0500426 /* If no serial number in VPD data, use low 6 bytes of WWNN */
427 /* This should be consolidated into parse_vpd ? - mr */
428 if (phba->SerialNumber[0] == 0) {
429 uint8_t *outptr;
430
James Smart2e0fef82007-06-17 19:56:36 -0500431 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500432 for (i = 0; i < 12; i++) {
433 status = *outptr++;
434 j = ((status & 0xf0) >> 4);
435 if (j <= 9)
436 phba->SerialNumber[i] =
437 (char)((uint8_t) 0x30 + (uint8_t) j);
438 else
439 phba->SerialNumber[i] =
440 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
441 i++;
442 j = (status & 0xf);
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 }
450 }
451
dea31012005-04-17 16:05:31 -0500452 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500453 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500454 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500455 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400456 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500457 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500458 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500459 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500460 mempool_free( pmb, phba->mbox_mem_pool);
461 return -EIO;
462 }
463
James Smarta0c87cb2009-07-19 10:01:10 -0400464 /* Check if the port is disabled */
465 lpfc_sli_read_link_ste(phba);
466
dea31012005-04-17 16:05:31 -0500467 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
468 if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
469 phba->cfg_hba_queue_depth =
James Smartf1126682009-06-10 17:22:44 -0400470 (mb->un.varRdConfig.max_xri + 1) -
471 lpfc_sli4_get_els_iocb_cnt(phba);
dea31012005-04-17 16:05:31 -0500472
473 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500474
475 /* Get the default values for Model Name and Description */
476 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
477
James Smart76a95d72010-11-20 23:11:48 -0500478 if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_16G)
479 || ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500480 && !(phba->lmt & LMT_1Gb))
James Smart76a95d72010-11-20 23:11:48 -0500481 || ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500482 && !(phba->lmt & LMT_2Gb))
James Smart76a95d72010-11-20 23:11:48 -0500483 || ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500484 && !(phba->lmt & LMT_4Gb))
James Smart76a95d72010-11-20 23:11:48 -0500485 || ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500486 && !(phba->lmt & LMT_8Gb))
James Smart76a95d72010-11-20 23:11:48 -0500487 || ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G)
488 && !(phba->lmt & LMT_10Gb))
489 || ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G)
490 && !(phba->lmt & LMT_16Gb))) {
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500491 /* Reset link speed to auto */
James Smartab56dc22011-02-16 12:39:57 -0500492 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -0400493 "1302 Invalid speed for this board: "
dea31012005-04-17 16:05:31 -0500494 "Reset link speed to auto: x%x\n",
dea31012005-04-17 16:05:31 -0500495 phba->cfg_link_speed);
James Smart76a95d72010-11-20 23:11:48 -0500496 phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
dea31012005-04-17 16:05:31 -0500497 }
498
James Smart2e0fef82007-06-17 19:56:36 -0500499 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500500
James Smart0b727fe2007-10-27 13:37:25 -0400501 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smarta4bc3372006-12-02 13:34:16 -0500502 if (psli->ring[psli->extra_ring].cmdringaddr)
503 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500504 if (psli->ring[psli->fcp_ring].cmdringaddr)
505 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
506 if (psli->ring[psli->next_ring].cmdringaddr)
507 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
508
509 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500510 if (phba->sli_rev != 3)
511 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500512
James Smart93996272008-08-24 21:50:30 -0400513 /*
514 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
515 */
516 if (phba->intr_type == MSIX) {
517 rc = lpfc_config_msi(phba, pmb);
518 if (rc) {
519 mempool_free(pmb, phba->mbox_mem_pool);
520 return -EIO;
521 }
522 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
523 if (rc != MBX_SUCCESS) {
524 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
525 "0352 Config MSI mailbox command "
526 "failed, mbxCmd x%x, mbxStatus x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400527 pmb->u.mb.mbxCommand,
528 pmb->u.mb.mbxStatus);
James Smart93996272008-08-24 21:50:30 -0400529 mempool_free(pmb, phba->mbox_mem_pool);
530 return -EIO;
531 }
532 }
533
James Smart04c68492009-05-22 14:52:52 -0400534 spin_lock_irq(&phba->hbalock);
James Smart93996272008-08-24 21:50:30 -0400535 /* Initialize ERATT handling flag */
536 phba->hba_flag &= ~HBA_ERATT_HANDLED;
537
dea31012005-04-17 16:05:31 -0500538 /* Enable appropriate host interrupts */
James Smart9940b972011-03-11 16:06:12 -0500539 if (lpfc_readl(phba->HCregaddr, &status)) {
540 spin_unlock_irq(&phba->hbalock);
541 return -EIO;
542 }
dea31012005-04-17 16:05:31 -0500543 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
544 if (psli->num_rings > 0)
545 status |= HC_R0INT_ENA;
546 if (psli->num_rings > 1)
547 status |= HC_R1INT_ENA;
548 if (psli->num_rings > 2)
549 status |= HC_R2INT_ENA;
550 if (psli->num_rings > 3)
551 status |= HC_R3INT_ENA;
552
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500553 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
554 (phba->cfg_poll & DISABLE_FCP_RING_INT))
James Smart93996272008-08-24 21:50:30 -0400555 status &= ~(HC_R0INT_ENA);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500556
dea31012005-04-17 16:05:31 -0500557 writel(status, phba->HCregaddr);
558 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500559 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500560
James Smart93996272008-08-24 21:50:30 -0400561 /* Set up ring-0 (ELS) timer */
562 timeout = phba->fc_ratov * 2;
James Smart2e0fef82007-06-17 19:56:36 -0500563 mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
James Smart93996272008-08-24 21:50:30 -0400564 /* Set up heart beat (HB) timer */
James Smart858c9f62007-06-17 19:56:39 -0500565 mod_timer(&phba->hb_tmofunc, jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
566 phba->hb_outstanding = 0;
567 phba->last_completion_time = jiffies;
James Smart93996272008-08-24 21:50:30 -0400568 /* Set up error attention (ERATT) polling timer */
569 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
dea31012005-04-17 16:05:31 -0500570
James Smarta0c87cb2009-07-19 10:01:10 -0400571 if (phba->hba_flag & LINK_DISABLED) {
572 lpfc_printf_log(phba,
573 KERN_ERR, LOG_INIT,
574 "2598 Adapter Link is disabled.\n");
575 lpfc_down_link(phba, pmb);
576 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
577 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
578 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
579 lpfc_printf_log(phba,
580 KERN_ERR, LOG_INIT,
581 "2599 Adapter failed to issue DOWN_LINK"
582 " mbox command rc 0x%x\n", rc);
583
584 mempool_free(pmb, phba->mbox_mem_pool);
585 return -EIO;
586 }
James Smarte40a02c2010-02-26 14:13:54 -0500587 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smarta0c87cb2009-07-19 10:01:10 -0400588 lpfc_init_link(phba, pmb, phba->cfg_topology,
589 phba->cfg_link_speed);
590 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
591 lpfc_set_loopback_flag(phba);
592 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
593 if (rc != MBX_SUCCESS) {
594 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400595 "0454 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500596 "INIT_LINK, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500597 mb->mbxCommand, mb->mbxStatus);
598
James Smarta0c87cb2009-07-19 10:01:10 -0400599 /* Clear all interrupt enable conditions */
600 writel(0, phba->HCregaddr);
601 readl(phba->HCregaddr); /* flush */
602 /* Clear all pending interrupts */
603 writel(0xffffffff, phba->HAregaddr);
604 readl(phba->HAregaddr); /* flush */
dea31012005-04-17 16:05:31 -0500605
James Smarta0c87cb2009-07-19 10:01:10 -0400606 phba->link_state = LPFC_HBA_ERROR;
607 if (rc != MBX_BUSY)
608 mempool_free(pmb, phba->mbox_mem_pool);
609 return -EIO;
610 }
dea31012005-04-17 16:05:31 -0500611 }
612 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400613 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500614 if (!pmb) {
615 phba->link_state = LPFC_HBA_ERROR;
616 return -ENOMEM;
617 }
618
James Smart57127f12007-10-27 13:37:05 -0400619 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
620 pmb->mbox_cmpl = lpfc_config_async_cmpl;
621 pmb->vport = phba->pport;
622 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500623
James Smart57127f12007-10-27 13:37:05 -0400624 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
625 lpfc_printf_log(phba,
626 KERN_ERR,
627 LOG_INIT,
628 "0456 Adapter failed to issue "
James Smarte4e74272009-07-19 10:01:38 -0400629 "ASYNCEVT_ENABLE mbox status x%x\n",
James Smart57127f12007-10-27 13:37:05 -0400630 rc);
631 mempool_free(pmb, phba->mbox_mem_pool);
632 }
James Smart97207482008-12-04 22:39:19 -0500633
634 /* Get Option rom version */
635 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500636 if (!pmb) {
637 phba->link_state = LPFC_HBA_ERROR;
638 return -ENOMEM;
639 }
640
James Smart97207482008-12-04 22:39:19 -0500641 lpfc_dump_wakeup_param(phba, pmb);
642 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
643 pmb->vport = phba->pport;
644 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
645
646 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
647 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
James Smarte4e74272009-07-19 10:01:38 -0400648 "to get Option ROM version status x%x\n", rc);
James Smart97207482008-12-04 22:39:19 -0500649 mempool_free(pmb, phba->mbox_mem_pool);
650 }
651
James Smartd7c255b2008-08-24 21:50:00 -0400652 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400653}
654
James Smarte59058c2008-08-24 21:49:00 -0400655/**
James Smart84d1b002010-02-12 14:42:33 -0500656 * lpfc_hba_init_link - Initialize the FC link
657 * @phba: pointer to lpfc hba data structure.
James Smart6e7288d2010-06-07 15:23:35 -0400658 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500659 *
660 * This routine will issue the INIT_LINK mailbox command call.
661 * It is available to other drivers through the lpfc_hba data
662 * structure for use as a delayed link up mechanism with the
663 * module parameter lpfc_suppress_link_up.
664 *
665 * Return code
666 * 0 - success
667 * Any other value - error
668 **/
669int
James Smart6e7288d2010-06-07 15:23:35 -0400670lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500671{
672 struct lpfc_vport *vport = phba->pport;
673 LPFC_MBOXQ_t *pmb;
674 MAILBOX_t *mb;
675 int rc;
676
677 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
678 if (!pmb) {
679 phba->link_state = LPFC_HBA_ERROR;
680 return -ENOMEM;
681 }
682 mb = &pmb->u.mb;
683 pmb->vport = vport;
684
James Smart76a95d72010-11-20 23:11:48 -0500685 lpfc_init_link(phba, pmb, phba->cfg_topology, phba->cfg_link_speed);
James Smart84d1b002010-02-12 14:42:33 -0500686 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
687 lpfc_set_loopback_flag(phba);
James Smart6e7288d2010-06-07 15:23:35 -0400688 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart76a95d72010-11-20 23:11:48 -0500689 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
James Smart84d1b002010-02-12 14:42:33 -0500690 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
691 "0498 Adapter failed to init, mbxCmd x%x "
692 "INIT_LINK, mbxStatus x%x\n",
693 mb->mbxCommand, mb->mbxStatus);
James Smart76a95d72010-11-20 23:11:48 -0500694 if (phba->sli_rev <= LPFC_SLI_REV3) {
695 /* Clear all interrupt enable conditions */
696 writel(0, phba->HCregaddr);
697 readl(phba->HCregaddr); /* flush */
698 /* Clear all pending interrupts */
699 writel(0xffffffff, phba->HAregaddr);
700 readl(phba->HAregaddr); /* flush */
701 }
James Smart84d1b002010-02-12 14:42:33 -0500702 phba->link_state = LPFC_HBA_ERROR;
James Smart6e7288d2010-06-07 15:23:35 -0400703 if (rc != MBX_BUSY || flag == MBX_POLL)
James Smart84d1b002010-02-12 14:42:33 -0500704 mempool_free(pmb, phba->mbox_mem_pool);
705 return -EIO;
706 }
James Smarte40a02c2010-02-26 14:13:54 -0500707 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
James Smart6e7288d2010-06-07 15:23:35 -0400708 if (flag == MBX_POLL)
709 mempool_free(pmb, phba->mbox_mem_pool);
James Smart84d1b002010-02-12 14:42:33 -0500710
711 return 0;
712}
713
714/**
715 * lpfc_hba_down_link - this routine downs the FC link
James Smart6e7288d2010-06-07 15:23:35 -0400716 * @phba: pointer to lpfc hba data structure.
717 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500718 *
719 * This routine will issue the DOWN_LINK mailbox command call.
720 * It is available to other drivers through the lpfc_hba data
721 * structure for use to stop the link.
722 *
723 * Return code
724 * 0 - success
725 * Any other value - error
726 **/
727int
James Smart6e7288d2010-06-07 15:23:35 -0400728lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500729{
730 LPFC_MBOXQ_t *pmb;
731 int rc;
732
733 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
734 if (!pmb) {
735 phba->link_state = LPFC_HBA_ERROR;
736 return -ENOMEM;
737 }
738
739 lpfc_printf_log(phba,
740 KERN_ERR, LOG_INIT,
741 "0491 Adapter Link is disabled.\n");
742 lpfc_down_link(phba, pmb);
743 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6e7288d2010-06-07 15:23:35 -0400744 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart84d1b002010-02-12 14:42:33 -0500745 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
746 lpfc_printf_log(phba,
747 KERN_ERR, LOG_INIT,
748 "2522 Adapter failed to issue DOWN_LINK"
749 " mbox command rc 0x%x\n", rc);
750
751 mempool_free(pmb, phba->mbox_mem_pool);
752 return -EIO;
753 }
James Smart6e7288d2010-06-07 15:23:35 -0400754 if (flag == MBX_POLL)
755 mempool_free(pmb, phba->mbox_mem_pool);
756
James Smart84d1b002010-02-12 14:42:33 -0500757 return 0;
758}
759
760/**
James Smart3621a712009-04-06 18:47:14 -0400761 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400762 * @phba: pointer to lpfc HBA data structure.
763 *
764 * This routine will do LPFC uninitialization before the HBA is reset when
765 * bringing down the SLI Layer.
766 *
767 * Return codes
768 * 0 - success.
769 * Any other value - error.
770 **/
dea31012005-04-17 16:05:31 -0500771int
James Smart2e0fef82007-06-17 19:56:36 -0500772lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500773{
James Smart1b32f6a2008-02-08 18:49:39 -0500774 struct lpfc_vport **vports;
775 int i;
James Smart3772a992009-05-22 14:50:54 -0400776
777 if (phba->sli_rev <= LPFC_SLI_REV3) {
778 /* Disable interrupts */
779 writel(0, phba->HCregaddr);
780 readl(phba->HCregaddr); /* flush */
781 }
dea31012005-04-17 16:05:31 -0500782
James Smart1b32f6a2008-02-08 18:49:39 -0500783 if (phba->pport->load_flag & FC_UNLOADING)
784 lpfc_cleanup_discovery_resources(phba->pport);
785 else {
786 vports = lpfc_create_vport_work_array(phba);
787 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400788 for (i = 0; i <= phba->max_vports &&
789 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500790 lpfc_cleanup_discovery_resources(vports[i]);
791 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500792 }
793 return 0;
dea31012005-04-17 16:05:31 -0500794}
795
James Smarte59058c2008-08-24 21:49:00 -0400796/**
James Smart3772a992009-05-22 14:50:54 -0400797 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400798 * @phba: pointer to lpfc HBA data structure.
799 *
800 * This routine will do uninitialization after the HBA is reset when bring
801 * down the SLI Layer.
802 *
803 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200804 * 0 - success.
James Smarte59058c2008-08-24 21:49:00 -0400805 * Any other value - error.
806 **/
James Smart3772a992009-05-22 14:50:54 -0400807static int
808lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -0500809{
810 struct lpfc_sli *psli = &phba->sli;
811 struct lpfc_sli_ring *pring;
812 struct lpfc_dmabuf *mp, *next_mp;
James Smart09372822008-01-11 01:52:54 -0500813 LIST_HEAD(completions);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500814 int i;
815
James Smart92d7f7b2007-06-17 19:56:38 -0500816 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
817 lpfc_sli_hbqbuf_free_all(phba);
818 else {
819 /* Cleanup preposted buffers on the ELS ring */
820 pring = &psli->ring[LPFC_ELS_RING];
821 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
822 list_del(&mp->list);
823 pring->postbufq_cnt--;
824 lpfc_mbuf_free(phba, mp->virt, mp->phys);
825 kfree(mp);
826 }
Jamie Wellnitz41415862006-02-28 19:25:27 -0500827 }
828
James Smart09372822008-01-11 01:52:54 -0500829 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500830 for (i = 0; i < psli->num_rings; i++) {
831 pring = &psli->ring[i];
James Smart09372822008-01-11 01:52:54 -0500832
833 /* At this point in time the HBA is either reset or DOA. Either
834 * way, nothing should be on txcmplq as it will NEVER complete.
835 */
836 list_splice_init(&pring->txcmplq, &completions);
837 pring->txcmplq_cnt = 0;
838 spin_unlock_irq(&phba->hbalock);
839
James Smarta257bf92009-04-06 18:48:10 -0400840 /* Cancel all the IOCBs from the completions list */
841 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
842 IOERR_SLI_ABORTED);
James Smart09372822008-01-11 01:52:54 -0500843
Jamie Wellnitz41415862006-02-28 19:25:27 -0500844 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart09372822008-01-11 01:52:54 -0500845 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500846 }
James Smart09372822008-01-11 01:52:54 -0500847 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500848
849 return 0;
850}
James Smart5af5eee2010-10-22 11:06:38 -0400851
James Smartda0436e2009-05-22 14:51:39 -0400852/**
853 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
854 * @phba: pointer to lpfc HBA data structure.
855 *
856 * This routine will do uninitialization after the HBA is reset when bring
857 * down the SLI Layer.
858 *
859 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200860 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400861 * Any other value - error.
862 **/
863static int
864lpfc_hba_down_post_s4(struct lpfc_hba *phba)
865{
866 struct lpfc_scsi_buf *psb, *psb_next;
867 LIST_HEAD(aborts);
868 int ret;
869 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500870 struct lpfc_sglq *sglq_entry = NULL;
871
James Smartda0436e2009-05-22 14:51:39 -0400872 ret = lpfc_hba_down_post_s3(phba);
873 if (ret)
874 return ret;
875 /* At this point in time the HBA is either reset or DOA. Either
876 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
877 * on the lpfc_sgl_list so that it can either be freed if the
878 * driver is unloading or reposted if the driver is restarting
879 * the port.
880 */
881 spin_lock_irq(&phba->hbalock); /* required for lpfc_sgl_list and */
882 /* scsl_buf_list */
883 /* abts_sgl_list_lock required because worker thread uses this
884 * list.
885 */
886 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -0500887 list_for_each_entry(sglq_entry,
888 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
889 sglq_entry->state = SGL_FREED;
890
James Smartda0436e2009-05-22 14:51:39 -0400891 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
892 &phba->sli4_hba.lpfc_sgl_list);
893 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
894 /* abts_scsi_buf_list_lock required because worker thread uses this
895 * list.
896 */
897 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
898 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
899 &aborts);
900 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
901 spin_unlock_irq(&phba->hbalock);
902
903 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
904 psb->pCmd = NULL;
905 psb->status = IOSTAT_SUCCESS;
906 }
907 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
908 list_splice(&aborts, &phba->lpfc_scsi_buf_list);
909 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
910 return 0;
911}
912
913/**
914 * lpfc_hba_down_post - Wrapper func for hba down post routine
915 * @phba: pointer to lpfc HBA data structure.
916 *
917 * This routine wraps the actual SLI3 or SLI4 routine for performing
918 * uninitialization after the HBA is reset when bring down the SLI Layer.
919 *
920 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200921 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400922 * Any other value - error.
923 **/
924int
925lpfc_hba_down_post(struct lpfc_hba *phba)
926{
927 return (*phba->lpfc_hba_down_post)(phba);
928}
Jamie Wellnitz41415862006-02-28 19:25:27 -0500929
James Smarte59058c2008-08-24 21:49:00 -0400930/**
James Smart3621a712009-04-06 18:47:14 -0400931 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -0400932 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
933 *
934 * This is the HBA-timer timeout handler registered to the lpfc driver. When
935 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
936 * work-port-events bitmap and the worker thread is notified. This timeout
937 * event will be used by the worker thread to invoke the actual timeout
938 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
939 * be performed in the timeout handler and the HBA timeout event bit shall
940 * be cleared by the worker thread after it has taken the event bitmap out.
941 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100942static void
James Smart858c9f62007-06-17 19:56:39 -0500943lpfc_hb_timeout(unsigned long ptr)
944{
945 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400946 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -0500947 unsigned long iflag;
948
949 phba = (struct lpfc_hba *)ptr;
James Smart93996272008-08-24 21:50:30 -0400950
951 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -0500952 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -0400953 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
954 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -0500955 phba->pport->work_port_events |= WORKER_HB_TMO;
956 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
957
James Smart93996272008-08-24 21:50:30 -0400958 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -0400959 if (!tmo_posted)
960 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -0500961 return;
962}
963
James Smarte59058c2008-08-24 21:49:00 -0400964/**
James Smart19ca7602010-11-20 23:11:55 -0500965 * lpfc_rrq_timeout - The RRQ-timer timeout handler
966 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
967 *
968 * This is the RRQ-timer timeout handler registered to the lpfc driver. When
969 * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
970 * work-port-events bitmap and the worker thread is notified. This timeout
971 * event will be used by the worker thread to invoke the actual timeout
972 * handler routine, lpfc_rrq_handler. Any periodical operations will
973 * be performed in the timeout handler and the RRQ timeout event bit shall
974 * be cleared by the worker thread after it has taken the event bitmap out.
975 **/
976static void
977lpfc_rrq_timeout(unsigned long ptr)
978{
979 struct lpfc_hba *phba;
James Smart19ca7602010-11-20 23:11:55 -0500980 unsigned long iflag;
981
982 phba = (struct lpfc_hba *)ptr;
983 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -0500984 phba->hba_flag |= HBA_RRQ_ACTIVE;
James Smart19ca7602010-11-20 23:11:55 -0500985 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -0500986 lpfc_worker_wake_up(phba);
James Smart19ca7602010-11-20 23:11:55 -0500987}
988
989/**
James Smart3621a712009-04-06 18:47:14 -0400990 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -0400991 * @phba: pointer to lpfc hba data structure.
992 * @pmboxq: pointer to the driver internal queue element for mailbox command.
993 *
994 * This is the callback function to the lpfc heart-beat mailbox command.
995 * If configured, the lpfc driver issues the heart-beat mailbox command to
996 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
997 * heart-beat mailbox command is issued, the driver shall set up heart-beat
998 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
999 * heart-beat outstanding state. Once the mailbox command comes back and
1000 * no error conditions detected, the heart-beat mailbox command timer is
1001 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1002 * state is cleared for the next heart-beat. If the timer expired with the
1003 * heart-beat outstanding state set, the driver will put the HBA offline.
1004 **/
James Smart858c9f62007-06-17 19:56:39 -05001005static void
1006lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1007{
1008 unsigned long drvr_flag;
1009
1010 spin_lock_irqsave(&phba->hbalock, drvr_flag);
1011 phba->hb_outstanding = 0;
1012 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1013
James Smart93996272008-08-24 21:50:30 -04001014 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -05001015 mempool_free(pmboxq, phba->mbox_mem_pool);
1016 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1017 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -04001018 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -05001019 mod_timer(&phba->hb_tmofunc,
1020 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1021 return;
1022}
1023
James Smarte59058c2008-08-24 21:49:00 -04001024/**
James Smart3621a712009-04-06 18:47:14 -04001025 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001026 * @phba: pointer to lpfc hba data structure.
1027 *
1028 * This is the actual HBA-timer timeout handler to be invoked by the worker
1029 * thread whenever the HBA timer fired and HBA-timeout event posted. This
1030 * handler performs any periodic operations needed for the device. If such
1031 * periodic event has already been attended to either in the interrupt handler
1032 * or by processing slow-ring or fast-ring events within the HBA-timer
1033 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1034 * the timer for the next timeout period. If lpfc heart-beat mailbox command
1035 * is configured and there is no heart-beat mailbox command outstanding, a
1036 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1037 * has been a heart-beat mailbox command outstanding, the HBA shall be put
1038 * to offline.
1039 **/
James Smart858c9f62007-06-17 19:56:39 -05001040void
1041lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1042{
James Smart45ed1192009-10-02 15:17:02 -04001043 struct lpfc_vport **vports;
James Smart858c9f62007-06-17 19:56:39 -05001044 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -05001045 struct lpfc_dmabuf *buf_ptr;
James Smart45ed1192009-10-02 15:17:02 -04001046 int retval, i;
James Smart858c9f62007-06-17 19:56:39 -05001047 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -05001048 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -05001049
James Smart45ed1192009-10-02 15:17:02 -04001050 vports = lpfc_create_vport_work_array(phba);
1051 if (vports != NULL)
1052 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
1053 lpfc_rcv_seq_check_edtov(vports[i]);
1054 lpfc_destroy_vport_work_array(phba, vports);
1055
James Smart858c9f62007-06-17 19:56:39 -05001056 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -04001057 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -05001058 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1059 return;
1060
1061 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -05001062
1063 if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
1064 jiffies)) {
1065 spin_unlock_irq(&phba->pport->work_port_lock);
1066 if (!phba->hb_outstanding)
1067 mod_timer(&phba->hb_tmofunc,
1068 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1069 else
1070 mod_timer(&phba->hb_tmofunc,
1071 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
1072 return;
1073 }
1074 spin_unlock_irq(&phba->pport->work_port_lock);
1075
James Smart0ff10d42008-01-11 01:52:36 -05001076 if (phba->elsbuf_cnt &&
1077 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1078 spin_lock_irq(&phba->hbalock);
1079 list_splice_init(&phba->elsbuf, &completions);
1080 phba->elsbuf_cnt = 0;
1081 phba->elsbuf_prev_cnt = 0;
1082 spin_unlock_irq(&phba->hbalock);
1083
1084 while (!list_empty(&completions)) {
1085 list_remove_head(&completions, buf_ptr,
1086 struct lpfc_dmabuf, list);
1087 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1088 kfree(buf_ptr);
1089 }
1090 }
1091 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1092
James Smart858c9f62007-06-17 19:56:39 -05001093 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -05001094 if (phba->cfg_enable_hba_heartbeat) {
1095 if (!phba->hb_outstanding) {
James Smartbc739052010-08-04 16:11:18 -04001096 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1097 (list_empty(&psli->mboxq))) {
1098 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1099 GFP_KERNEL);
1100 if (!pmboxq) {
1101 mod_timer(&phba->hb_tmofunc,
1102 jiffies +
1103 HZ * LPFC_HB_MBOX_INTERVAL);
1104 return;
1105 }
James Smart13815c82008-01-11 01:52:48 -05001106
James Smartbc739052010-08-04 16:11:18 -04001107 lpfc_heart_beat(phba, pmboxq);
1108 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1109 pmboxq->vport = phba->pport;
1110 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1111 MBX_NOWAIT);
James Smart13815c82008-01-11 01:52:48 -05001112
James Smartbc739052010-08-04 16:11:18 -04001113 if (retval != MBX_BUSY &&
1114 retval != MBX_SUCCESS) {
1115 mempool_free(pmboxq,
1116 phba->mbox_mem_pool);
1117 mod_timer(&phba->hb_tmofunc,
1118 jiffies +
1119 HZ * LPFC_HB_MBOX_INTERVAL);
1120 return;
1121 }
1122 phba->skipped_hb = 0;
1123 phba->hb_outstanding = 1;
1124 } else if (time_before_eq(phba->last_completion_time,
1125 phba->skipped_hb)) {
1126 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1127 "2857 Last completion time not "
1128 " updated in %d ms\n",
1129 jiffies_to_msecs(jiffies
1130 - phba->last_completion_time));
1131 } else
1132 phba->skipped_hb = jiffies;
1133
James Smart858c9f62007-06-17 19:56:39 -05001134 mod_timer(&phba->hb_tmofunc,
James Smart13815c82008-01-11 01:52:48 -05001135 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
James Smart858c9f62007-06-17 19:56:39 -05001136 return;
James Smart13815c82008-01-11 01:52:48 -05001137 } else {
1138 /*
1139 * If heart beat timeout called with hb_outstanding set
James Smartdcf2a4e2010-09-29 11:18:53 -04001140 * we need to give the hb mailbox cmd a chance to
1141 * complete or TMO.
James Smart13815c82008-01-11 01:52:48 -05001142 */
James Smartdcf2a4e2010-09-29 11:18:53 -04001143 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1144 "0459 Adapter heartbeat still out"
1145 "standing:last compl time was %d ms.\n",
1146 jiffies_to_msecs(jiffies
1147 - phba->last_completion_time));
1148 mod_timer(&phba->hb_tmofunc,
1149 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
James Smart858c9f62007-06-17 19:56:39 -05001150 }
James Smart858c9f62007-06-17 19:56:39 -05001151 }
1152}
1153
James Smarte59058c2008-08-24 21:49:00 -04001154/**
James Smart3621a712009-04-06 18:47:14 -04001155 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -04001156 * @phba: pointer to lpfc hba data structure.
1157 *
1158 * This routine is called to bring the HBA offline when HBA hardware error
1159 * other than Port Error 6 has been detected.
1160 **/
James Smart09372822008-01-11 01:52:54 -05001161static void
1162lpfc_offline_eratt(struct lpfc_hba *phba)
1163{
1164 struct lpfc_sli *psli = &phba->sli;
1165
1166 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001167 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -05001168 spin_unlock_irq(&phba->hbalock);
1169 lpfc_offline_prep(phba);
1170
1171 lpfc_offline(phba);
1172 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001173 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001174 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001175 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001176 lpfc_hba_down_post(phba);
1177 lpfc_sli_brdready(phba, HS_MBRDY);
1178 lpfc_unblock_mgmt_io(phba);
1179 phba->link_state = LPFC_HBA_ERROR;
1180 return;
1181}
1182
James Smarte59058c2008-08-24 21:49:00 -04001183/**
James Smartda0436e2009-05-22 14:51:39 -04001184 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1185 * @phba: pointer to lpfc hba data structure.
1186 *
1187 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1188 * other than Port Error 6 has been detected.
1189 **/
1190static void
1191lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1192{
1193 lpfc_offline_prep(phba);
1194 lpfc_offline(phba);
1195 lpfc_sli4_brdreset(phba);
1196 lpfc_hba_down_post(phba);
1197 lpfc_sli4_post_status_check(phba);
1198 lpfc_unblock_mgmt_io(phba);
1199 phba->link_state = LPFC_HBA_ERROR;
1200}
1201
1202/**
James Smarta257bf92009-04-06 18:48:10 -04001203 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1204 * @phba: pointer to lpfc hba data structure.
1205 *
1206 * This routine is invoked to handle the deferred HBA hardware error
1207 * conditions. This type of error is indicated by HBA by setting ER1
1208 * and another ER bit in the host status register. The driver will
1209 * wait until the ER1 bit clears before handling the error condition.
1210 **/
1211static void
1212lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1213{
1214 uint32_t old_host_status = phba->work_hs;
1215 struct lpfc_sli_ring *pring;
1216 struct lpfc_sli *psli = &phba->sli;
1217
James Smartf4b4c682009-05-22 14:53:12 -04001218 /* If the pci channel is offline, ignore possible errors,
1219 * since we cannot communicate with the pci card anyway.
1220 */
1221 if (pci_channel_offline(phba->pcidev)) {
1222 spin_lock_irq(&phba->hbalock);
1223 phba->hba_flag &= ~DEFER_ERATT;
1224 spin_unlock_irq(&phba->hbalock);
1225 return;
1226 }
1227
James Smarta257bf92009-04-06 18:48:10 -04001228 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1229 "0479 Deferred Adapter Hardware Error "
1230 "Data: x%x x%x x%x\n",
1231 phba->work_hs,
1232 phba->work_status[0], phba->work_status[1]);
1233
1234 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001235 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001236 spin_unlock_irq(&phba->hbalock);
1237
1238
1239 /*
1240 * Firmware stops when it triggred erratt. That could cause the I/Os
1241 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1242 * SCSI layer retry it after re-establishing link.
1243 */
1244 pring = &psli->ring[psli->fcp_ring];
1245 lpfc_sli_abort_iocb_ring(phba, pring);
1246
1247 /*
1248 * There was a firmware error. Take the hba offline and then
1249 * attempt to restart it.
1250 */
1251 lpfc_offline_prep(phba);
1252 lpfc_offline(phba);
1253
1254 /* Wait for the ER1 bit to clear.*/
1255 while (phba->work_hs & HS_FFER1) {
1256 msleep(100);
James Smart9940b972011-03-11 16:06:12 -05001257 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1258 phba->work_hs = UNPLUG_ERR ;
1259 break;
1260 }
James Smarta257bf92009-04-06 18:48:10 -04001261 /* If driver is unloading let the worker thread continue */
1262 if (phba->pport->load_flag & FC_UNLOADING) {
1263 phba->work_hs = 0;
1264 break;
1265 }
1266 }
1267
1268 /*
1269 * This is to ptrotect against a race condition in which
1270 * first write to the host attention register clear the
1271 * host status register.
1272 */
1273 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1274 phba->work_hs = old_host_status & ~HS_FFER1;
1275
James Smart3772a992009-05-22 14:50:54 -04001276 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001277 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001278 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001279 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1280 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1281}
1282
James Smart3772a992009-05-22 14:50:54 -04001283static void
1284lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1285{
1286 struct lpfc_board_event_header board_event;
1287 struct Scsi_Host *shost;
1288
1289 board_event.event_type = FC_REG_BOARD_EVENT;
1290 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1291 shost = lpfc_shost_from_vport(phba->pport);
1292 fc_host_post_vendor_event(shost, fc_get_event_number(),
1293 sizeof(board_event),
1294 (char *) &board_event,
1295 LPFC_NL_VENDOR_ID);
1296}
1297
James Smarta257bf92009-04-06 18:48:10 -04001298/**
James Smart3772a992009-05-22 14:50:54 -04001299 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001300 * @phba: pointer to lpfc hba data structure.
1301 *
1302 * This routine is invoked to handle the following HBA hardware error
1303 * conditions:
1304 * 1 - HBA error attention interrupt
1305 * 2 - DMA ring index out of range
1306 * 3 - Mailbox command came back as unknown
1307 **/
James Smart3772a992009-05-22 14:50:54 -04001308static void
1309lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001310{
James Smart2e0fef82007-06-17 19:56:36 -05001311 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001312 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001313 struct lpfc_sli_ring *pring;
James Smartd2873e42006-08-18 17:46:43 -04001314 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001315 unsigned long temperature;
1316 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001317 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001318
Linas Vepstas8d63f372007-02-14 14:28:36 -06001319 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001320 * since we cannot communicate with the pci card anyway.
1321 */
1322 if (pci_channel_offline(phba->pcidev)) {
1323 spin_lock_irq(&phba->hbalock);
1324 phba->hba_flag &= ~DEFER_ERATT;
1325 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001326 return;
James Smart3772a992009-05-22 14:50:54 -04001327 }
1328
James Smart13815c82008-01-11 01:52:48 -05001329 /* If resets are disabled then leave the HBA alone and return */
1330 if (!phba->cfg_enable_hba_reset)
1331 return;
dea31012005-04-17 16:05:31 -05001332
James Smartea2151b2008-09-07 11:52:10 -04001333 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001334 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001335
James Smarta257bf92009-04-06 18:48:10 -04001336 if (phba->hba_flag & DEFER_ERATT)
1337 lpfc_handle_deferred_eratt(phba);
1338
James Smartdcf2a4e2010-09-29 11:18:53 -04001339 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1340 if (phba->work_hs & HS_FFER6)
1341 /* Re-establishing Link */
1342 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1343 "1301 Re-establishing Link "
1344 "Data: x%x x%x x%x\n",
1345 phba->work_hs, phba->work_status[0],
1346 phba->work_status[1]);
1347 if (phba->work_hs & HS_FFER8)
1348 /* Device Zeroization */
1349 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1350 "2861 Host Authentication device "
1351 "zeroization Data:x%x x%x x%x\n",
1352 phba->work_hs, phba->work_status[0],
1353 phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001354
James Smart92d7f7b2007-06-17 19:56:38 -05001355 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001356 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001357 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001358
1359 /*
1360 * Firmware stops when it triggled erratt with HS_FFER6.
1361 * That could cause the I/Os dropped by the firmware.
1362 * Error iocb (I/O) on txcmplq and let the SCSI layer
1363 * retry it after re-establishing link.
1364 */
1365 pring = &psli->ring[psli->fcp_ring];
1366 lpfc_sli_abort_iocb_ring(phba, pring);
1367
dea31012005-04-17 16:05:31 -05001368 /*
1369 * There was a firmware error. Take the hba offline and then
1370 * attempt to restart it.
1371 */
James Smart46fa3112007-04-25 09:51:45 -04001372 lpfc_offline_prep(phba);
dea31012005-04-17 16:05:31 -05001373 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001374 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001375 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001376 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001377 return;
1378 }
James Smart46fa3112007-04-25 09:51:45 -04001379 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001380 } else if (phba->work_hs & HS_CRIT_TEMP) {
1381 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1382 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1383 temp_event_data.event_code = LPFC_CRIT_TEMP;
1384 temp_event_data.data = (uint32_t)temperature;
1385
1386 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001387 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001388 "(%ld), taking this port offline "
1389 "Data: x%x x%x x%x\n",
1390 temperature, phba->work_hs,
1391 phba->work_status[0], phba->work_status[1]);
1392
1393 shost = lpfc_shost_from_vport(phba->pport);
1394 fc_host_post_vendor_event(shost, fc_get_event_number(),
1395 sizeof(temp_event_data),
1396 (char *) &temp_event_data,
1397 SCSI_NL_VID_TYPE_PCI
1398 | PCI_VENDOR_ID_EMULEX);
1399
James Smart7af67052007-10-27 13:38:11 -04001400 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001401 phba->over_temp_state = HBA_OVER_TEMP;
1402 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001403 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001404
dea31012005-04-17 16:05:31 -05001405 } else {
1406 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001407 * failure is a value other than FFER6. Do not call the offline
1408 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001409 */
1410 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001411 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001412 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001413 phba->work_hs,
dea31012005-04-17 16:05:31 -05001414 phba->work_status[0], phba->work_status[1]);
1415
James Smartd2873e42006-08-18 17:46:43 -04001416 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001417 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001418 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001419 sizeof(event_data), (char *) &event_data,
1420 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1421
James Smart09372822008-01-11 01:52:54 -05001422 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001423 }
James Smart93996272008-08-24 21:50:30 -04001424 return;
dea31012005-04-17 16:05:31 -05001425}
1426
James Smarte59058c2008-08-24 21:49:00 -04001427/**
James Smartda0436e2009-05-22 14:51:39 -04001428 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1429 * @phba: pointer to lpfc hba data structure.
1430 *
1431 * This routine is invoked to handle the SLI4 HBA hardware error attention
1432 * conditions.
1433 **/
1434static void
1435lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1436{
1437 struct lpfc_vport *vport = phba->pport;
1438 uint32_t event_data;
1439 struct Scsi_Host *shost;
James Smart2fcee4b2010-12-15 17:57:46 -05001440 uint32_t if_type;
1441 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -04001442
1443 /* If the pci channel is offline, ignore possible errors, since
1444 * we cannot communicate with the pci card anyway.
1445 */
1446 if (pci_channel_offline(phba->pcidev))
1447 return;
1448 /* If resets are disabled then leave the HBA alone and return */
1449 if (!phba->cfg_enable_hba_reset)
1450 return;
1451
1452 /* Send an internal error event to mgmt application */
1453 lpfc_board_errevt_to_mgmt(phba);
1454
1455 /* For now, the actual action for SLI4 device handling is not
1456 * specified yet, just treated it as adaptor hardware failure
1457 */
James Smartda0436e2009-05-22 14:51:39 -04001458 event_data = FC_REG_DUMP_EVENT;
1459 shost = lpfc_shost_from_vport(vport);
1460 fc_host_post_vendor_event(shost, fc_get_event_number(),
1461 sizeof(event_data), (char *) &event_data,
1462 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1463
James Smart2fcee4b2010-12-15 17:57:46 -05001464 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1465 switch (if_type) {
1466 case LPFC_SLI_INTF_IF_TYPE_0:
1467 lpfc_sli4_offline_eratt(phba);
1468 break;
1469 case LPFC_SLI_INTF_IF_TYPE_2:
1470 portstat_reg.word0 =
1471 readl(phba->sli4_hba.u.if_type2.STATUSregaddr);
1472
1473 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
1474 /* TODO: Register for Overtemp async events. */
1475 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1476 "2889 Port Overtemperature event, "
1477 "taking port\n");
1478 spin_lock_irq(&phba->hbalock);
1479 phba->over_temp_state = HBA_OVER_TEMP;
1480 spin_unlock_irq(&phba->hbalock);
1481 lpfc_sli4_offline_eratt(phba);
1482 return;
1483 }
1484 if (bf_get(lpfc_sliport_status_rn, &portstat_reg)) {
1485 /*
1486 * TODO: Attempt port recovery via a port reset.
1487 * When fully implemented, the driver should
1488 * attempt to recover the port here and return.
1489 * For now, log an error and take the port offline.
1490 */
1491 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1492 "2887 Port Error: Attempting "
1493 "Port Recovery\n");
1494 }
1495 lpfc_sli4_offline_eratt(phba);
1496 break;
1497 case LPFC_SLI_INTF_IF_TYPE_1:
1498 default:
1499 break;
1500 }
James Smartda0436e2009-05-22 14:51:39 -04001501}
1502
1503/**
1504 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1505 * @phba: pointer to lpfc HBA data structure.
1506 *
1507 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1508 * routine from the API jump table function pointer from the lpfc_hba struct.
1509 *
1510 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001511 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001512 * Any other value - error.
1513 **/
1514void
1515lpfc_handle_eratt(struct lpfc_hba *phba)
1516{
1517 (*phba->lpfc_handle_eratt)(phba);
1518}
1519
1520/**
James Smart3621a712009-04-06 18:47:14 -04001521 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04001522 * @phba: pointer to lpfc hba data structure.
1523 *
1524 * This routine is invoked from the worker thread to handle a HBA host
1525 * attention link event.
1526 **/
dea31012005-04-17 16:05:31 -05001527void
James Smart2e0fef82007-06-17 19:56:36 -05001528lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001529{
James Smart2e0fef82007-06-17 19:56:36 -05001530 struct lpfc_vport *vport = phba->pport;
1531 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001532 LPFC_MBOXQ_t *pmb;
1533 volatile uint32_t control;
1534 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05001535 int rc = 0;
dea31012005-04-17 16:05:31 -05001536
1537 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001538 if (!pmb) {
1539 rc = 1;
dea31012005-04-17 16:05:31 -05001540 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05001541 }
dea31012005-04-17 16:05:31 -05001542
1543 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001544 if (!mp) {
1545 rc = 2;
dea31012005-04-17 16:05:31 -05001546 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05001547 }
dea31012005-04-17 16:05:31 -05001548
1549 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05001550 if (!mp->virt) {
1551 rc = 3;
dea31012005-04-17 16:05:31 -05001552 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05001553 }
dea31012005-04-17 16:05:31 -05001554
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05001555 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04001556 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05001557
1558 psli->slistat.link_event++;
James Smart76a95d72010-11-20 23:11:48 -05001559 lpfc_read_topology(phba, pmb, mp);
1560 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smart2e0fef82007-06-17 19:56:36 -05001561 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04001562 /* Block ELS IOCBs until we have processed this mbox command */
1563 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04001564 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05001565 if (rc == MBX_NOT_FINISHED) {
1566 rc = 4;
James Smart14691152006-12-02 13:34:28 -05001567 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05001568 }
dea31012005-04-17 16:05:31 -05001569
1570 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05001571 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001572 writel(HA_LATT, phba->HAregaddr);
1573 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001574 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001575
1576 return;
1577
James Smart14691152006-12-02 13:34:28 -05001578lpfc_handle_latt_free_mbuf:
James Smart0d2b6b82008-06-14 22:52:47 -04001579 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05001580 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001581lpfc_handle_latt_free_mp:
1582 kfree(mp);
1583lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04001584 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001585lpfc_handle_latt_err_exit:
1586 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001587 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001588 psli->sli_flag |= LPFC_PROCESS_LA;
1589 control = readl(phba->HCregaddr);
1590 control |= HC_LAINT_ENA;
1591 writel(control, phba->HCregaddr);
1592 readl(phba->HCregaddr); /* flush */
1593
1594 /* Clear Link Attention in HA REG */
1595 writel(HA_LATT, phba->HAregaddr);
1596 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001597 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001598 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001599 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001600
James Smart09372822008-01-11 01:52:54 -05001601 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1602 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05001603
1604 return;
1605}
1606
James Smarte59058c2008-08-24 21:49:00 -04001607/**
James Smart3621a712009-04-06 18:47:14 -04001608 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04001609 * @phba: pointer to lpfc hba data structure.
1610 * @vpd: pointer to the vital product data.
1611 * @len: length of the vital product data in bytes.
1612 *
1613 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1614 * an array of characters. In this routine, the ModelName, ProgramType, and
1615 * ModelDesc, etc. fields of the phba data structure will be populated.
1616 *
1617 * Return codes
1618 * 0 - pointer to the VPD passed in is NULL
1619 * 1 - success
1620 **/
James Smart3772a992009-05-22 14:50:54 -04001621int
James Smart2e0fef82007-06-17 19:56:36 -05001622lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05001623{
1624 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05001625 int Length;
dea31012005-04-17 16:05:31 -05001626 int i, j;
1627 int finished = 0;
1628 int index = 0;
1629
1630 if (!vpd)
1631 return 0;
1632
1633 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05001634 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001635 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05001636 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1637 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001638 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05001639 switch (vpd[index]) {
1640 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001641 case 0x91:
dea31012005-04-17 16:05:31 -05001642 index += 1;
1643 lenlo = vpd[index];
1644 index += 1;
1645 lenhi = vpd[index];
1646 index += 1;
1647 i = ((((unsigned short)lenhi) << 8) + lenlo);
1648 index += i;
1649 break;
1650 case 0x90:
1651 index += 1;
1652 lenlo = vpd[index];
1653 index += 1;
1654 lenhi = vpd[index];
1655 index += 1;
1656 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001657 if (Length > len - index)
1658 Length = len - index;
dea31012005-04-17 16:05:31 -05001659 while (Length > 0) {
1660 /* Look for Serial Number */
1661 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1662 index += 2;
1663 i = vpd[index];
1664 index += 1;
1665 j = 0;
1666 Length -= (3+i);
1667 while(i--) {
1668 phba->SerialNumber[j++] = vpd[index++];
1669 if (j == 31)
1670 break;
1671 }
1672 phba->SerialNumber[j] = 0;
1673 continue;
1674 }
1675 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1676 phba->vpd_flag |= VPD_MODEL_DESC;
1677 index += 2;
1678 i = vpd[index];
1679 index += 1;
1680 j = 0;
1681 Length -= (3+i);
1682 while(i--) {
1683 phba->ModelDesc[j++] = vpd[index++];
1684 if (j == 255)
1685 break;
1686 }
1687 phba->ModelDesc[j] = 0;
1688 continue;
1689 }
1690 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1691 phba->vpd_flag |= VPD_MODEL_NAME;
1692 index += 2;
1693 i = vpd[index];
1694 index += 1;
1695 j = 0;
1696 Length -= (3+i);
1697 while(i--) {
1698 phba->ModelName[j++] = vpd[index++];
1699 if (j == 79)
1700 break;
1701 }
1702 phba->ModelName[j] = 0;
1703 continue;
1704 }
1705 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1706 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1707 index += 2;
1708 i = vpd[index];
1709 index += 1;
1710 j = 0;
1711 Length -= (3+i);
1712 while(i--) {
1713 phba->ProgramType[j++] = vpd[index++];
1714 if (j == 255)
1715 break;
1716 }
1717 phba->ProgramType[j] = 0;
1718 continue;
1719 }
1720 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1721 phba->vpd_flag |= VPD_PORT;
1722 index += 2;
1723 i = vpd[index];
1724 index += 1;
1725 j = 0;
1726 Length -= (3+i);
1727 while(i--) {
1728 phba->Port[j++] = vpd[index++];
1729 if (j == 19)
1730 break;
1731 }
1732 phba->Port[j] = 0;
1733 continue;
1734 }
1735 else {
1736 index += 2;
1737 i = vpd[index];
1738 index += 1;
1739 index += i;
1740 Length -= (3 + i);
1741 }
1742 }
1743 finished = 0;
1744 break;
1745 case 0x78:
1746 finished = 1;
1747 break;
1748 default:
1749 index ++;
1750 break;
1751 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001752 }
dea31012005-04-17 16:05:31 -05001753
1754 return(1);
1755}
1756
James Smarte59058c2008-08-24 21:49:00 -04001757/**
James Smart3621a712009-04-06 18:47:14 -04001758 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04001759 * @phba: pointer to lpfc hba data structure.
1760 * @mdp: pointer to the data structure to hold the derived model name.
1761 * @descp: pointer to the data structure to hold the derived description.
1762 *
1763 * This routine retrieves HBA's description based on its registered PCI device
1764 * ID. The @descp passed into this function points to an array of 256 chars. It
1765 * shall be returned with the model name, maximum speed, and the host bus type.
1766 * The @mdp passed into this function points to an array of 80 chars. When the
1767 * function returns, the @mdp will be filled with the model name.
1768 **/
dea31012005-04-17 16:05:31 -05001769static void
James Smart2e0fef82007-06-17 19:56:36 -05001770lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05001771{
1772 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05001773 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001774 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04001775 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04001776 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001777 struct {
James Smarta747c9c2009-11-18 15:41:10 -05001778 char *name;
1779 char *bus;
1780 char *function;
1781 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001782
1783 if (mdp && mdp[0] != '\0'
1784 && descp && descp[0] != '\0')
1785 return;
1786
James Smartc0c11512011-05-24 11:41:34 -04001787 if (phba->lmt & LMT_16Gb)
1788 max_speed = 16;
1789 else if (phba->lmt & LMT_10Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001790 max_speed = 10;
1791 else if (phba->lmt & LMT_8Gb)
1792 max_speed = 8;
1793 else if (phba->lmt & LMT_4Gb)
1794 max_speed = 4;
1795 else if (phba->lmt & LMT_2Gb)
1796 max_speed = 2;
1797 else
1798 max_speed = 1;
dea31012005-04-17 16:05:31 -05001799
1800 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05001801
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001802 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001803 case PCI_DEVICE_ID_FIREFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001804 m = (typeof(m)){"LP6000", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001805 break;
dea31012005-04-17 16:05:31 -05001806 case PCI_DEVICE_ID_SUPERFLY:
1807 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smarta747c9c2009-11-18 15:41:10 -05001808 m = (typeof(m)){"LP7000", "PCI",
1809 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001810 else
James Smarta747c9c2009-11-18 15:41:10 -05001811 m = (typeof(m)){"LP7000E", "PCI",
1812 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001813 break;
1814 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001815 m = (typeof(m)){"LP8000", "PCI",
1816 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001817 break;
1818 case PCI_DEVICE_ID_CENTAUR:
1819 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smarta747c9c2009-11-18 15:41:10 -05001820 m = (typeof(m)){"LP9002", "PCI",
1821 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001822 else
James Smarta747c9c2009-11-18 15:41:10 -05001823 m = (typeof(m)){"LP9000", "PCI",
1824 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001825 break;
1826 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001827 m = (typeof(m)){"LP952", "PCI",
1828 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001829 break;
1830 case PCI_DEVICE_ID_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05001831 m = (typeof(m)){"LP9802", "PCI-X",
1832 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001833 break;
1834 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05001835 m = (typeof(m)){"LP10000", "PCI-X",
1836 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001837 break;
1838 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05001839 m = (typeof(m)){"LPX1000", "PCI-X",
1840 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001841 break;
1842 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001843 m = (typeof(m)){"LP982", "PCI-X",
1844 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001845 break;
1846 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001847 m = (typeof(m)){"LP1050", "PCI-X",
1848 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001849 break;
1850 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05001851 m = (typeof(m)){"LP11000", "PCI-X2",
1852 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001853 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001854 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001855 m = (typeof(m)){"LP11000-SP", "PCI-X2",
1856 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001857 break;
1858 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001859 m = (typeof(m)){"LP11002-SP", "PCI-X2",
1860 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001861 break;
1862 case PCI_DEVICE_ID_NEPTUNE:
James Smarta747c9c2009-11-18 15:41:10 -05001863 m = (typeof(m)){"LPe1000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001864 break;
1865 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001866 m = (typeof(m)){"LPe1000-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001867 break;
1868 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001869 m = (typeof(m)){"LPe1002-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001870 break;
dea31012005-04-17 16:05:31 -05001871 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05001872 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001873 break;
1874 case PCI_DEVICE_ID_BSMB:
James Smarta747c9c2009-11-18 15:41:10 -05001875 m = (typeof(m)){"LP111", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001876 break;
1877 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05001878 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001879 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001880 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001881 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001882 break;
1883 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001884 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04001885 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001886 break;
dea31012005-04-17 16:05:31 -05001887 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05001888 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001889 break;
1890 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05001891 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001892 break;
1893 case PCI_DEVICE_ID_LP101:
James Smarta747c9c2009-11-18 15:41:10 -05001894 m = (typeof(m)){"LP101", "PCI-X", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001895 break;
1896 case PCI_DEVICE_ID_LP10000S:
James Smarta747c9c2009-11-18 15:41:10 -05001897 m = (typeof(m)){"LP10000-S", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001898 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001899 case PCI_DEVICE_ID_LP11000S:
James Smarta747c9c2009-11-18 15:41:10 -05001900 m = (typeof(m)){"LP11000-S", "PCI-X2", "Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05001901 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001902 case PCI_DEVICE_ID_LPE11000S:
James Smarta747c9c2009-11-18 15:41:10 -05001903 m = (typeof(m)){"LPe11000-S", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05001904 break;
James Smartb87eab32007-04-25 09:53:28 -04001905 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05001906 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001907 break;
1908 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05001909 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001910 break;
1911 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05001912 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001913 break;
1914 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001915 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001916 break;
1917 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001918 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001919 break;
1920 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05001921 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001922 break;
James Smart84774a42008-08-24 21:50:06 -04001923 case PCI_DEVICE_ID_HORNET:
James Smarta747c9c2009-11-18 15:41:10 -05001924 m = (typeof(m)){"LP21000", "PCIe", "FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001925 GE = 1;
1926 break;
1927 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05001928 m = (typeof(m)){"LPev12000", "PCIe IOV",
1929 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001930 break;
1931 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05001932 m = (typeof(m)){"LPev12000", "PCIe IOV",
1933 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001934 break;
1935 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05001936 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
1937 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001938 break;
James Smartda0436e2009-05-22 14:51:39 -04001939 case PCI_DEVICE_ID_TIGERSHARK:
1940 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05001941 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04001942 break;
James Smarta747c9c2009-11-18 15:41:10 -05001943 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04001944 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05001945 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
1946 break;
1947 case PCI_DEVICE_ID_FALCON:
1948 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
1949 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04001950 break;
James Smart98fc5dd2010-06-07 15:24:29 -04001951 case PCI_DEVICE_ID_BALIUS:
1952 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
1953 "Fibre Channel Adapter"};
1954 break;
James Smart085c6472010-11-20 23:11:37 -05001955 case PCI_DEVICE_ID_LANCER_FC:
James Smartc0c11512011-05-24 11:41:34 -04001956 case PCI_DEVICE_ID_LANCER_FC_VF:
1957 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
James Smart085c6472010-11-20 23:11:37 -05001958 break;
1959 case PCI_DEVICE_ID_LANCER_FCOE:
James Smartc0c11512011-05-24 11:41:34 -04001960 case PCI_DEVICE_ID_LANCER_FCOE_VF:
James Smart085c6472010-11-20 23:11:37 -05001961 oneConnect = 1;
James Smartc0c11512011-05-24 11:41:34 -04001962 m = (typeof(m)){"OCe50100", "PCIe", "FCoE"};
James Smart085c6472010-11-20 23:11:37 -05001963 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05001964 default:
James Smarta747c9c2009-11-18 15:41:10 -05001965 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001966 break;
dea31012005-04-17 16:05:31 -05001967 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001968
1969 if (mdp && mdp[0] == '\0')
1970 snprintf(mdp, 79,"%s", m.name);
James Smartc0c11512011-05-24 11:41:34 -04001971 /*
1972 * oneConnect hba requires special processing, they are all initiators
James Smartda0436e2009-05-22 14:51:39 -04001973 * and we put the port number on the end
1974 */
1975 if (descp && descp[0] == '\0') {
1976 if (oneConnect)
1977 snprintf(descp, 255,
James Smarta747c9c2009-11-18 15:41:10 -05001978 "Emulex OneConnect %s, %s Initiator, Port %s",
1979 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04001980 phba->Port);
1981 else
1982 snprintf(descp, 255,
1983 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05001984 m.name, max_speed, (GE) ? "GE" : "Gb",
1985 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04001986 }
dea31012005-04-17 16:05:31 -05001987}
1988
James Smarte59058c2008-08-24 21:49:00 -04001989/**
James Smart3621a712009-04-06 18:47:14 -04001990 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04001991 * @phba: pointer to lpfc hba data structure.
1992 * @pring: pointer to a IOCB ring.
1993 * @cnt: the number of IOCBs to be posted to the IOCB ring.
1994 *
1995 * This routine posts a given number of IOCBs with the associated DMA buffer
1996 * descriptors specified by the cnt argument to the given IOCB ring.
1997 *
1998 * Return codes
1999 * The number of IOCBs NOT able to be posted to the IOCB ring.
2000 **/
dea31012005-04-17 16:05:31 -05002001int
James Smart495a7142008-06-14 22:52:59 -04002002lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05002003{
2004 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002005 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05002006 struct lpfc_dmabuf *mp1, *mp2;
2007
2008 cnt += pring->missbufcnt;
2009
2010 /* While there are buffers to post */
2011 while (cnt > 0) {
2012 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002013 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002014 if (iocb == NULL) {
2015 pring->missbufcnt = cnt;
2016 return cnt;
2017 }
dea31012005-04-17 16:05:31 -05002018 icmd = &iocb->iocb;
2019
2020 /* 2 buffers can be posted per command */
2021 /* Allocate buffer to post */
2022 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2023 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04002024 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2025 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002026 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002027 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002028 pring->missbufcnt = cnt;
2029 return cnt;
2030 }
2031
2032 INIT_LIST_HEAD(&mp1->list);
2033 /* Allocate buffer to post */
2034 if (cnt > 1) {
2035 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2036 if (mp2)
2037 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2038 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04002039 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002040 kfree(mp2);
dea31012005-04-17 16:05:31 -05002041 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2042 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002043 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002044 pring->missbufcnt = cnt;
2045 return cnt;
2046 }
2047
2048 INIT_LIST_HEAD(&mp2->list);
2049 } else {
2050 mp2 = NULL;
2051 }
2052
2053 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2054 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2055 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2056 icmd->ulpBdeCount = 1;
2057 cnt--;
2058 if (mp2) {
2059 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2060 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2061 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2062 cnt--;
2063 icmd->ulpBdeCount = 2;
2064 }
2065
2066 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2067 icmd->ulpLe = 1;
2068
James Smart3772a992009-05-22 14:50:54 -04002069 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2070 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05002071 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2072 kfree(mp1);
2073 cnt++;
2074 if (mp2) {
2075 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2076 kfree(mp2);
2077 cnt++;
2078 }
James Bottomley604a3e32005-10-29 10:28:33 -05002079 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002080 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05002081 return cnt;
2082 }
dea31012005-04-17 16:05:31 -05002083 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05002084 if (mp2)
dea31012005-04-17 16:05:31 -05002085 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05002086 }
2087 pring->missbufcnt = 0;
2088 return 0;
2089}
2090
James Smarte59058c2008-08-24 21:49:00 -04002091/**
James Smart3621a712009-04-06 18:47:14 -04002092 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04002093 * @phba: pointer to lpfc hba data structure.
2094 *
2095 * This routine posts initial receive IOCB buffers to the ELS ring. The
2096 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
2097 * set to 64 IOCBs.
2098 *
2099 * Return codes
2100 * 0 - success (currently always success)
2101 **/
dea31012005-04-17 16:05:31 -05002102static int
James Smart2e0fef82007-06-17 19:56:36 -05002103lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002104{
2105 struct lpfc_sli *psli = &phba->sli;
2106
2107 /* Ring 0, ELS / CT buffers */
James Smart495a7142008-06-14 22:52:59 -04002108 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05002109 /* Ring 2 - FCP no buffers needed */
2110
2111 return 0;
2112}
2113
2114#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2115
James Smarte59058c2008-08-24 21:49:00 -04002116/**
James Smart3621a712009-04-06 18:47:14 -04002117 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04002118 * @HashResultPointer: pointer to an array as hash table.
2119 *
2120 * This routine sets up the initial values to the array of hash table entries
2121 * for the LC HBAs.
2122 **/
dea31012005-04-17 16:05:31 -05002123static void
2124lpfc_sha_init(uint32_t * HashResultPointer)
2125{
2126 HashResultPointer[0] = 0x67452301;
2127 HashResultPointer[1] = 0xEFCDAB89;
2128 HashResultPointer[2] = 0x98BADCFE;
2129 HashResultPointer[3] = 0x10325476;
2130 HashResultPointer[4] = 0xC3D2E1F0;
2131}
2132
James Smarte59058c2008-08-24 21:49:00 -04002133/**
James Smart3621a712009-04-06 18:47:14 -04002134 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04002135 * @HashResultPointer: pointer to an initial/result hash table.
2136 * @HashWorkingPointer: pointer to an working hash table.
2137 *
2138 * This routine iterates an initial hash table pointed by @HashResultPointer
2139 * with the values from the working hash table pointeed by @HashWorkingPointer.
2140 * The results are putting back to the initial hash table, returned through
2141 * the @HashResultPointer as the result hash table.
2142 **/
dea31012005-04-17 16:05:31 -05002143static void
2144lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2145{
2146 int t;
2147 uint32_t TEMP;
2148 uint32_t A, B, C, D, E;
2149 t = 16;
2150 do {
2151 HashWorkingPointer[t] =
2152 S(1,
2153 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2154 8] ^
2155 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2156 } while (++t <= 79);
2157 t = 0;
2158 A = HashResultPointer[0];
2159 B = HashResultPointer[1];
2160 C = HashResultPointer[2];
2161 D = HashResultPointer[3];
2162 E = HashResultPointer[4];
2163
2164 do {
2165 if (t < 20) {
2166 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2167 } else if (t < 40) {
2168 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2169 } else if (t < 60) {
2170 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2171 } else {
2172 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2173 }
2174 TEMP += S(5, A) + E + HashWorkingPointer[t];
2175 E = D;
2176 D = C;
2177 C = S(30, B);
2178 B = A;
2179 A = TEMP;
2180 } while (++t <= 79);
2181
2182 HashResultPointer[0] += A;
2183 HashResultPointer[1] += B;
2184 HashResultPointer[2] += C;
2185 HashResultPointer[3] += D;
2186 HashResultPointer[4] += E;
2187
2188}
2189
James Smarte59058c2008-08-24 21:49:00 -04002190/**
James Smart3621a712009-04-06 18:47:14 -04002191 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002192 * @RandomChallenge: pointer to the entry of host challenge random number array.
2193 * @HashWorking: pointer to the entry of the working hash array.
2194 *
2195 * This routine calculates the working hash array referred by @HashWorking
2196 * from the challenge random numbers associated with the host, referred by
2197 * @RandomChallenge. The result is put into the entry of the working hash
2198 * array and returned by reference through @HashWorking.
2199 **/
dea31012005-04-17 16:05:31 -05002200static void
2201lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2202{
2203 *HashWorking = (*RandomChallenge ^ *HashWorking);
2204}
2205
James Smarte59058c2008-08-24 21:49:00 -04002206/**
James Smart3621a712009-04-06 18:47:14 -04002207 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002208 * @phba: pointer to lpfc hba data structure.
2209 * @hbainit: pointer to an array of unsigned 32-bit integers.
2210 *
2211 * This routine performs the special handling for LC HBA initialization.
2212 **/
dea31012005-04-17 16:05:31 -05002213void
2214lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2215{
2216 int t;
2217 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002218 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002219
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002220 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002221 if (!HashWorking)
2222 return;
2223
dea31012005-04-17 16:05:31 -05002224 HashWorking[0] = HashWorking[78] = *pwwnn++;
2225 HashWorking[1] = HashWorking[79] = *pwwnn;
2226
2227 for (t = 0; t < 7; t++)
2228 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2229
2230 lpfc_sha_init(hbainit);
2231 lpfc_sha_iterate(hbainit, HashWorking);
2232 kfree(HashWorking);
2233}
2234
James Smarte59058c2008-08-24 21:49:00 -04002235/**
James Smart3621a712009-04-06 18:47:14 -04002236 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002237 * @vport: pointer to a virtual N_Port data structure.
2238 *
2239 * This routine performs the necessary cleanups before deleting the @vport.
2240 * It invokes the discovery state machine to perform necessary state
2241 * transitions and to release the ndlps associated with the @vport. Note,
2242 * the physical port is treated as @vport 0.
2243 **/
James Smart87af33f2007-10-27 13:37:43 -04002244void
James Smart2e0fef82007-06-17 19:56:36 -05002245lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002246{
James Smart87af33f2007-10-27 13:37:43 -04002247 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002248 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002249 int i = 0;
dea31012005-04-17 16:05:31 -05002250
James Smart87af33f2007-10-27 13:37:43 -04002251 if (phba->link_state > LPFC_LINK_DOWN)
2252 lpfc_port_link_failure(vport);
2253
2254 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002255 if (!NLP_CHK_NODE_ACT(ndlp)) {
2256 ndlp = lpfc_enable_node(vport, ndlp,
2257 NLP_STE_UNUSED_NODE);
2258 if (!ndlp)
2259 continue;
2260 spin_lock_irq(&phba->ndlp_lock);
2261 NLP_SET_FREE_REQ(ndlp);
2262 spin_unlock_irq(&phba->ndlp_lock);
2263 /* Trigger the release of the ndlp memory */
2264 lpfc_nlp_put(ndlp);
2265 continue;
2266 }
2267 spin_lock_irq(&phba->ndlp_lock);
2268 if (NLP_CHK_FREE_REQ(ndlp)) {
2269 /* The ndlp should not be in memory free mode already */
2270 spin_unlock_irq(&phba->ndlp_lock);
2271 continue;
2272 } else
2273 /* Indicate request for freeing ndlp memory */
2274 NLP_SET_FREE_REQ(ndlp);
2275 spin_unlock_irq(&phba->ndlp_lock);
2276
James Smart58da1ff2008-04-07 10:15:56 -04002277 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2278 ndlp->nlp_DID == Fabric_DID) {
2279 /* Just free up ndlp with Fabric_DID for vports */
2280 lpfc_nlp_put(ndlp);
2281 continue;
2282 }
2283
James Smart87af33f2007-10-27 13:37:43 -04002284 if (ndlp->nlp_type & NLP_FABRIC)
2285 lpfc_disc_state_machine(vport, ndlp, NULL,
2286 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002287
James Smart87af33f2007-10-27 13:37:43 -04002288 lpfc_disc_state_machine(vport, ndlp, NULL,
2289 NLP_EVT_DEVICE_RM);
James Smart495a7142008-06-14 22:52:59 -04002290
James Smart87af33f2007-10-27 13:37:43 -04002291 }
2292
James Smarta8adb832007-10-27 13:37:53 -04002293 /* At this point, ALL ndlp's should be gone
2294 * because of the previous NLP_EVT_DEVICE_RM.
2295 * Lets wait for this to happen, if needed.
2296 */
James Smart87af33f2007-10-27 13:37:43 -04002297 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002298 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002299 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002300 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002301 list_for_each_entry_safe(ndlp, next_ndlp,
2302 &vport->fc_nodes, nlp_listp) {
2303 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2304 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002305 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002306 "usgmap:x%x refcnt:%d\n",
2307 ndlp->nlp_DID, (void *)ndlp,
2308 ndlp->nlp_usg_map,
2309 atomic_read(
2310 &ndlp->kref.refcount));
2311 }
James Smarta8adb832007-10-27 13:37:53 -04002312 break;
James Smart87af33f2007-10-27 13:37:43 -04002313 }
James Smarta8adb832007-10-27 13:37:53 -04002314
2315 /* Wait for any activity on ndlps to settle */
2316 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002317 }
James Smart1151e3e2011-02-16 12:39:35 -05002318 lpfc_cleanup_vports_rrqs(vport, NULL);
dea31012005-04-17 16:05:31 -05002319}
2320
James Smarte59058c2008-08-24 21:49:00 -04002321/**
James Smart3621a712009-04-06 18:47:14 -04002322 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002323 * @vport: pointer to a virtual N_Port data structure.
2324 *
2325 * This routine stops all the timers associated with a @vport. This function
2326 * is invoked before disabling or deleting a @vport. Note that the physical
2327 * port is treated as @vport 0.
2328 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002329void
2330lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002331{
James Smart92d7f7b2007-06-17 19:56:38 -05002332 del_timer_sync(&vport->els_tmofunc);
2333 del_timer_sync(&vport->fc_fdmitmo);
James Smart92494142011-02-16 12:39:44 -05002334 del_timer_sync(&vport->delayed_disc_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002335 lpfc_can_disctmo(vport);
2336 return;
dea31012005-04-17 16:05:31 -05002337}
2338
James Smarte59058c2008-08-24 21:49:00 -04002339/**
James Smartecfd03c2010-02-12 14:41:27 -05002340 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2341 * @phba: pointer to lpfc hba data structure.
2342 *
2343 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2344 * caller of this routine should already hold the host lock.
2345 **/
2346void
2347__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2348{
James Smart5ac6b302010-10-22 11:05:36 -04002349 /* Clear pending FCF rediscovery wait flag */
2350 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2351
James Smartecfd03c2010-02-12 14:41:27 -05002352 /* Now, try to stop the timer */
2353 del_timer(&phba->fcf.redisc_wait);
2354}
2355
2356/**
2357 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2358 * @phba: pointer to lpfc hba data structure.
2359 *
2360 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2361 * checks whether the FCF rediscovery wait timer is pending with the host
2362 * lock held before proceeding with disabling the timer and clearing the
2363 * wait timer pendig flag.
2364 **/
2365void
2366lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2367{
2368 spin_lock_irq(&phba->hbalock);
2369 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2370 /* FCF rediscovery timer already fired or stopped */
2371 spin_unlock_irq(&phba->hbalock);
2372 return;
2373 }
2374 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart5ac6b302010-10-22 11:05:36 -04002375 /* Clear failover in progress flags */
2376 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05002377 spin_unlock_irq(&phba->hbalock);
2378}
2379
2380/**
James Smart3772a992009-05-22 14:50:54 -04002381 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002382 * @phba: pointer to lpfc hba data structure.
2383 *
2384 * This routine stops all the timers associated with a HBA. This function is
2385 * invoked before either putting a HBA offline or unloading the driver.
2386 **/
James Smart3772a992009-05-22 14:50:54 -04002387void
2388lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002389{
James Smart51ef4c22007-08-02 11:10:31 -04002390 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05002391 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002392 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002393 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002394 del_timer_sync(&phba->hb_tmofunc);
James Smart1151e3e2011-02-16 12:39:35 -05002395 if (phba->sli_rev == LPFC_SLI_REV4) {
2396 del_timer_sync(&phba->rrq_tmr);
2397 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2398 }
James Smart3772a992009-05-22 14:50:54 -04002399 phba->hb_outstanding = 0;
2400
2401 switch (phba->pci_dev_grp) {
2402 case LPFC_PCI_DEV_LP:
2403 /* Stop any LightPulse device specific driver timers */
2404 del_timer_sync(&phba->fcp_poll_timer);
2405 break;
2406 case LPFC_PCI_DEV_OC:
2407 /* Stop any OneConnect device sepcific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05002408 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04002409 break;
2410 default:
2411 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2412 "0297 Invalid device group (x%x)\n",
2413 phba->pci_dev_grp);
2414 break;
2415 }
James Smart2e0fef82007-06-17 19:56:36 -05002416 return;
dea31012005-04-17 16:05:31 -05002417}
2418
James Smarte59058c2008-08-24 21:49:00 -04002419/**
James Smart3621a712009-04-06 18:47:14 -04002420 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002421 * @phba: pointer to lpfc hba data structure.
2422 *
2423 * This routine marks a HBA's management interface as blocked. Once the HBA's
2424 * management interface is marked as blocked, all the user space access to
2425 * the HBA, whether they are from sysfs interface or libdfc interface will
2426 * all be blocked. The HBA is set to block the management interface when the
2427 * driver prepares the HBA interface for online or offline.
2428 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002429static void
2430lpfc_block_mgmt_io(struct lpfc_hba * phba)
2431{
2432 unsigned long iflag;
James Smart6e7288d2010-06-07 15:23:35 -04002433 uint8_t actcmd = MBX_HEARTBEAT;
2434 unsigned long timeout;
2435
Adrian Bunka6ababd2007-11-05 18:07:33 +01002436
2437 spin_lock_irqsave(&phba->hbalock, iflag);
2438 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
James Smart6e7288d2010-06-07 15:23:35 -04002439 if (phba->sli.mbox_active)
2440 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
Adrian Bunka6ababd2007-11-05 18:07:33 +01002441 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6e7288d2010-06-07 15:23:35 -04002442 /* Determine how long we might wait for the active mailbox
2443 * command to be gracefully completed by firmware.
2444 */
2445 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
2446 jiffies;
2447 /* Wait for the outstnading mailbox command to complete */
2448 while (phba->sli.mbox_active) {
2449 /* Check active mailbox complete status every 2ms */
2450 msleep(2);
2451 if (time_after(jiffies, timeout)) {
2452 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2453 "2813 Mgmt IO is Blocked %x "
2454 "- mbox cmd %x still active\n",
2455 phba->sli.sli_flag, actcmd);
2456 break;
2457 }
2458 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002459}
2460
James Smarte59058c2008-08-24 21:49:00 -04002461/**
James Smart3621a712009-04-06 18:47:14 -04002462 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04002463 * @phba: pointer to lpfc hba data structure.
2464 *
2465 * This routine initializes the HBA and brings a HBA online. During this
2466 * process, the management interface is blocked to prevent user space access
2467 * to the HBA interfering with the driver initialization.
2468 *
2469 * Return codes
2470 * 0 - successful
2471 * 1 - failed
2472 **/
dea31012005-04-17 16:05:31 -05002473int
James Smart2e0fef82007-06-17 19:56:36 -05002474lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002475{
Julia Lawall372bd282008-12-16 16:15:08 +01002476 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04002477 struct lpfc_vport **vports;
2478 int i;
James Smart2e0fef82007-06-17 19:56:36 -05002479
dea31012005-04-17 16:05:31 -05002480 if (!phba)
2481 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01002482 vport = phba->pport;
dea31012005-04-17 16:05:31 -05002483
James Smart2e0fef82007-06-17 19:56:36 -05002484 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05002485 return 0;
2486
James Smarted957682007-06-17 19:56:37 -05002487 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002488 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05002489
James Smart46fa3112007-04-25 09:51:45 -04002490 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002491
James Smart46fa3112007-04-25 09:51:45 -04002492 if (!lpfc_sli_queue_setup(phba)) {
2493 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002494 return 1;
James Smart46fa3112007-04-25 09:51:45 -04002495 }
2496
James Smartda0436e2009-05-22 14:51:39 -04002497 if (phba->sli_rev == LPFC_SLI_REV4) {
2498 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2499 lpfc_unblock_mgmt_io(phba);
2500 return 1;
2501 }
2502 } else {
2503 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2504 lpfc_unblock_mgmt_io(phba);
2505 return 1;
2506 }
James Smart46fa3112007-04-25 09:51:45 -04002507 }
dea31012005-04-17 16:05:31 -05002508
James Smart549e55c2007-08-02 11:09:51 -04002509 vports = lpfc_create_vport_work_array(phba);
2510 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002511 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002512 struct Scsi_Host *shost;
2513 shost = lpfc_shost_from_vport(vports[i]);
2514 spin_lock_irq(shost->host_lock);
2515 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2516 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2517 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart1c6834a2009-07-19 10:01:26 -04002518 if (phba->sli_rev == LPFC_SLI_REV4)
2519 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart549e55c2007-08-02 11:09:51 -04002520 spin_unlock_irq(shost->host_lock);
2521 }
James Smart09372822008-01-11 01:52:54 -05002522 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002523
James Smart46fa3112007-04-25 09:51:45 -04002524 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002525 return 0;
2526}
2527
James Smarte59058c2008-08-24 21:49:00 -04002528/**
James Smart3621a712009-04-06 18:47:14 -04002529 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04002530 * @phba: pointer to lpfc hba data structure.
2531 *
2532 * This routine marks a HBA's management interface as not blocked. Once the
2533 * HBA's management interface is marked as not blocked, all the user space
2534 * access to the HBA, whether they are from sysfs interface or libdfc
2535 * interface will be allowed. The HBA is set to block the management interface
2536 * when the driver prepares the HBA interface for online or offline and then
2537 * set to unblock the management interface afterwards.
2538 **/
James Smart46fa3112007-04-25 09:51:45 -04002539void
James Smart46fa3112007-04-25 09:51:45 -04002540lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2541{
2542 unsigned long iflag;
2543
James Smart2e0fef82007-06-17 19:56:36 -05002544 spin_lock_irqsave(&phba->hbalock, iflag);
2545 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2546 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04002547}
2548
James Smarte59058c2008-08-24 21:49:00 -04002549/**
James Smart3621a712009-04-06 18:47:14 -04002550 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04002551 * @phba: pointer to lpfc hba data structure.
2552 *
2553 * This routine is invoked to prepare a HBA to be brought offline. It performs
2554 * unregistration login to all the nodes on all vports and flushes the mailbox
2555 * queue to make it ready to be brought offline.
2556 **/
James Smart46fa3112007-04-25 09:51:45 -04002557void
2558lpfc_offline_prep(struct lpfc_hba * phba)
2559{
James Smart2e0fef82007-06-17 19:56:36 -05002560 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04002561 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04002562 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05002563 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04002564 int i;
dea31012005-04-17 16:05:31 -05002565
James Smart2e0fef82007-06-17 19:56:36 -05002566 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002567 return;
dea31012005-04-17 16:05:31 -05002568
James Smart46fa3112007-04-25 09:51:45 -04002569 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002570
2571 lpfc_linkdown(phba);
2572
James Smart87af33f2007-10-27 13:37:43 -04002573 /* Issue an unreg_login to all nodes on all vports */
2574 vports = lpfc_create_vport_work_array(phba);
2575 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04002576 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04002577 if (vports[i]->load_flag & FC_UNLOADING)
2578 continue;
James Smart72100cc2010-02-12 14:43:01 -05002579 shost = lpfc_shost_from_vport(vports[i]);
2580 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002581 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002582 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2583 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05002584 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002585
James Smart87af33f2007-10-27 13:37:43 -04002586 shost = lpfc_shost_from_vport(vports[i]);
2587 list_for_each_entry_safe(ndlp, next_ndlp,
2588 &vports[i]->fc_nodes,
2589 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002590 if (!NLP_CHK_NODE_ACT(ndlp))
2591 continue;
James Smart87af33f2007-10-27 13:37:43 -04002592 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2593 continue;
2594 if (ndlp->nlp_type & NLP_FABRIC) {
2595 lpfc_disc_state_machine(vports[i], ndlp,
2596 NULL, NLP_EVT_DEVICE_RECOVERY);
2597 lpfc_disc_state_machine(vports[i], ndlp,
2598 NULL, NLP_EVT_DEVICE_RM);
2599 }
2600 spin_lock_irq(shost->host_lock);
2601 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2602 spin_unlock_irq(shost->host_lock);
2603 lpfc_unreg_rpi(vports[i], ndlp);
2604 }
2605 }
2606 }
James Smart09372822008-01-11 01:52:54 -05002607 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002608
James Smart04c68492009-05-22 14:52:52 -04002609 lpfc_sli_mbox_sys_shutdown(phba);
James Smart46fa3112007-04-25 09:51:45 -04002610}
2611
James Smarte59058c2008-08-24 21:49:00 -04002612/**
James Smart3621a712009-04-06 18:47:14 -04002613 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04002614 * @phba: pointer to lpfc hba data structure.
2615 *
2616 * This routine actually brings a HBA offline. It stops all the timers
2617 * associated with the HBA, brings down the SLI layer, and eventually
2618 * marks the HBA as in offline state for the upper layer protocol.
2619 **/
James Smart46fa3112007-04-25 09:51:45 -04002620void
James Smart2e0fef82007-06-17 19:56:36 -05002621lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04002622{
James Smart549e55c2007-08-02 11:09:51 -04002623 struct Scsi_Host *shost;
2624 struct lpfc_vport **vports;
2625 int i;
James Smart46fa3112007-04-25 09:51:45 -04002626
James Smart549e55c2007-08-02 11:09:51 -04002627 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002628 return;
James Smart688a8862006-07-06 15:49:56 -04002629
James Smartda0436e2009-05-22 14:51:39 -04002630 /* stop port and all timers associated with this hba */
2631 lpfc_stop_port(phba);
James Smart51ef4c22007-08-02 11:10:31 -04002632 vports = lpfc_create_vport_work_array(phba);
2633 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002634 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04002635 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05002636 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05002637 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002638 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05002639 /* Bring down the SLI Layer and cleanup. The HBA is offline
2640 now. */
2641 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002642 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04002643 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002644 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04002645 vports = lpfc_create_vport_work_array(phba);
2646 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002647 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002648 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04002649 spin_lock_irq(shost->host_lock);
2650 vports[i]->work_port_events = 0;
2651 vports[i]->fc_flag |= FC_OFFLINE_MODE;
2652 spin_unlock_irq(shost->host_lock);
2653 }
James Smart09372822008-01-11 01:52:54 -05002654 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002655}
2656
James Smarte59058c2008-08-24 21:49:00 -04002657/**
James Smart3621a712009-04-06 18:47:14 -04002658 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04002659 * @phba: pointer to lpfc hba data structure.
2660 *
2661 * This routine is to free all the SCSI buffers and IOCBs from the driver
2662 * list back to kernel. It is called from lpfc_pci_remove_one to free
2663 * the internal resources before the device is removed from the system.
2664 *
2665 * Return codes
2666 * 0 - successful (for now, it always returns 0)
2667 **/
dea31012005-04-17 16:05:31 -05002668static int
James Smart2e0fef82007-06-17 19:56:36 -05002669lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002670{
2671 struct lpfc_scsi_buf *sb, *sb_next;
2672 struct lpfc_iocbq *io, *io_next;
2673
James Smart2e0fef82007-06-17 19:56:36 -05002674 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002675 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smart1c6f4ef52009-11-18 15:40:49 -05002676 spin_lock(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05002677 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
2678 list_del(&sb->list);
2679 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05002680 sb->dma_handle);
dea31012005-04-17 16:05:31 -05002681 kfree(sb);
2682 phba->total_scsi_bufs--;
2683 }
James Smart1c6f4ef52009-11-18 15:40:49 -05002684 spin_unlock(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05002685
2686 /* Release all the lpfc_iocbq entries maintained by this host. */
2687 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2688 list_del(&io->list);
2689 kfree(io);
2690 phba->total_iocbq_bufs--;
2691 }
James Smart2e0fef82007-06-17 19:56:36 -05002692 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002693 return 0;
2694}
2695
James Smarte59058c2008-08-24 21:49:00 -04002696/**
James Smart3621a712009-04-06 18:47:14 -04002697 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04002698 * @phba: pointer to lpfc hba data structure.
2699 * @instance: a unique integer ID to this FC port.
2700 * @dev: pointer to the device data structure.
2701 *
2702 * This routine creates a FC port for the upper layer protocol. The FC port
2703 * can be created on top of either a physical port or a virtual port provided
2704 * by the HBA. This routine also allocates a SCSI host data structure (shost)
2705 * and associates the FC port created before adding the shost into the SCSI
2706 * layer.
2707 *
2708 * Return codes
2709 * @vport - pointer to the virtual N_Port data structure.
2710 * NULL - port create failed.
2711 **/
James Smart2e0fef82007-06-17 19:56:36 -05002712struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04002713lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04002714{
James Smart2e0fef82007-06-17 19:56:36 -05002715 struct lpfc_vport *vport;
2716 struct Scsi_Host *shost;
2717 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04002718
James Smart3de2a652007-08-02 11:09:59 -04002719 if (dev != &phba->pcidev->dev)
2720 shost = scsi_host_alloc(&lpfc_vport_template,
2721 sizeof(struct lpfc_vport));
2722 else
2723 shost = scsi_host_alloc(&lpfc_template,
2724 sizeof(struct lpfc_vport));
James Smart2e0fef82007-06-17 19:56:36 -05002725 if (!shost)
2726 goto out;
James Smart47a86172007-04-25 09:53:22 -04002727
James Smart2e0fef82007-06-17 19:56:36 -05002728 vport = (struct lpfc_vport *) shost->hostdata;
2729 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05002730 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05002731 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05002732 vport->fc_rscn_flush = 0;
James Smart47a86172007-04-25 09:53:22 -04002733
James Smart3de2a652007-08-02 11:09:59 -04002734 lpfc_get_vport_cfgparam(vport);
James Smart2e0fef82007-06-17 19:56:36 -05002735 shost->unique_id = instance;
2736 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04002737 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05002738 shost->this_id = -1;
2739 shost->max_cmd_len = 16;
James Smartda0436e2009-05-22 14:51:39 -04002740 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart28baac72010-02-12 14:42:03 -05002741 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04002742 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smartda0436e2009-05-22 14:51:39 -04002743 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
2744 }
James Smart81301a92008-12-04 22:39:46 -05002745
James Smart47a86172007-04-25 09:53:22 -04002746 /*
James Smart2e0fef82007-06-17 19:56:36 -05002747 * Set initial can_queue value since 0 is no longer supported and
2748 * scsi_add_host will fail. This will be adjusted later based on the
2749 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04002750 */
James Smart2e0fef82007-06-17 19:56:36 -05002751 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04002752 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05002753 shost->transportt = lpfc_vport_transport_template;
2754 vport->port_type = LPFC_NPIV_PORT;
2755 } else {
2756 shost->transportt = lpfc_transport_template;
2757 vport->port_type = LPFC_PHYSICAL_PORT;
2758 }
James Smart47a86172007-04-25 09:53:22 -04002759
James Smart2e0fef82007-06-17 19:56:36 -05002760 /* Initialize all internally managed lists. */
2761 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04002762 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05002763 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04002764
James Smart2e0fef82007-06-17 19:56:36 -05002765 init_timer(&vport->fc_disctmo);
2766 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05002767 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002768
James Smart2e0fef82007-06-17 19:56:36 -05002769 init_timer(&vport->fc_fdmitmo);
2770 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
James Smart92d7f7b2007-06-17 19:56:38 -05002771 vport->fc_fdmitmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002772
James Smart2e0fef82007-06-17 19:56:36 -05002773 init_timer(&vport->els_tmofunc);
2774 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05002775 vport->els_tmofunc.data = (unsigned long)vport;
James Smart92494142011-02-16 12:39:44 -05002776
2777 init_timer(&vport->delayed_disc_tmo);
2778 vport->delayed_disc_tmo.function = lpfc_delayed_disc_tmo;
2779 vport->delayed_disc_tmo.data = (unsigned long)vport;
2780
James Bottomleyd139b9b2009-11-05 13:33:12 -06002781 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05002782 if (error)
2783 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04002784
James Smart549e55c2007-08-02 11:09:51 -04002785 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002786 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04002787 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002788 return vport;
James Smart47a86172007-04-25 09:53:22 -04002789
James Smart2e0fef82007-06-17 19:56:36 -05002790out_put_shost:
2791 scsi_host_put(shost);
2792out:
2793 return NULL;
James Smart47a86172007-04-25 09:53:22 -04002794}
2795
James Smarte59058c2008-08-24 21:49:00 -04002796/**
James Smart3621a712009-04-06 18:47:14 -04002797 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04002798 * @vport: pointer to an lpfc virtual N_Port data structure.
2799 *
2800 * This routine destroys a FC port from the upper layer protocol. All the
2801 * resources associated with the port are released.
2802 **/
James Smart2e0fef82007-06-17 19:56:36 -05002803void
2804destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04002805{
James Smart92d7f7b2007-06-17 19:56:38 -05002806 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2807 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002808
James Smart858c9f62007-06-17 19:56:39 -05002809 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05002810 fc_remove_host(shost);
2811 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04002812
James Smart92d7f7b2007-06-17 19:56:38 -05002813 spin_lock_irq(&phba->hbalock);
2814 list_del_init(&vport->listentry);
2815 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04002816
James Smart92d7f7b2007-06-17 19:56:38 -05002817 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04002818 return;
James Smart47a86172007-04-25 09:53:22 -04002819}
2820
James Smarte59058c2008-08-24 21:49:00 -04002821/**
James Smart3621a712009-04-06 18:47:14 -04002822 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04002823 *
2824 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
2825 * uses the kernel idr facility to perform the task.
2826 *
2827 * Return codes:
2828 * instance - a unique integer ID allocated as the new instance.
2829 * -1 - lpfc get instance failed.
2830 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002831int
2832lpfc_get_instance(void)
2833{
2834 int instance = 0;
2835
2836 /* Assign an unused number */
2837 if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
2838 return -1;
2839 if (idr_get_new(&lpfc_hba_index, NULL, &instance))
2840 return -1;
2841 return instance;
2842}
2843
James Smarte59058c2008-08-24 21:49:00 -04002844/**
James Smart3621a712009-04-06 18:47:14 -04002845 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04002846 * @shost: pointer to SCSI host data structure.
2847 * @time: elapsed time of the scan in jiffies.
2848 *
2849 * This routine is called by the SCSI layer with a SCSI host to determine
2850 * whether the scan host is finished.
2851 *
2852 * Note: there is no scan_start function as adapter initialization will have
2853 * asynchronously kicked off the link initialization.
2854 *
2855 * Return codes
2856 * 0 - SCSI host scan is not over yet.
2857 * 1 - SCSI host scan is over.
2858 **/
James Smart47a86172007-04-25 09:53:22 -04002859int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2860{
James Smart2e0fef82007-06-17 19:56:36 -05002861 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2862 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05002863 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04002864
James Smart858c9f62007-06-17 19:56:39 -05002865 spin_lock_irq(shost->host_lock);
2866
James Smart51ef4c22007-08-02 11:10:31 -04002867 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05002868 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002869 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05002870 }
James Smart2e0fef82007-06-17 19:56:36 -05002871 if (time >= 30 * HZ) {
2872 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002873 "0461 Scanning longer than 30 "
2874 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05002875 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002876 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002877 }
2878 if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
2879 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002880 "0465 Link down longer than 15 "
2881 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05002882 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05002883 goto finished;
James Smart47a86172007-04-25 09:53:22 -04002884 }
2885
James Smart2e0fef82007-06-17 19:56:36 -05002886 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05002887 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002888 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05002889 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002890 if (vport->fc_map_cnt == 0 && time < 2 * HZ)
James Smart858c9f62007-06-17 19:56:39 -05002891 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002892 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05002893 goto finished;
2894
2895 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002896
2897finished:
James Smart858c9f62007-06-17 19:56:39 -05002898 spin_unlock_irq(shost->host_lock);
2899 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05002900}
2901
James Smarte59058c2008-08-24 21:49:00 -04002902/**
James Smart3621a712009-04-06 18:47:14 -04002903 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04002904 * @shost: pointer to SCSI host data structure.
2905 *
2906 * This routine initializes a given SCSI host attributes on a FC port. The
2907 * SCSI host can be either on top of a physical port or a virtual port.
2908 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002909void lpfc_host_attrib_init(struct Scsi_Host *shost)
2910{
2911 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2912 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002913 /*
James Smart2e0fef82007-06-17 19:56:36 -05002914 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04002915 */
2916
James Smart2e0fef82007-06-17 19:56:36 -05002917 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
2918 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04002919 fc_host_supported_classes(shost) = FC_COS_CLASS3;
2920
2921 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05002922 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04002923 fc_host_supported_fc4s(shost)[2] = 1;
2924 fc_host_supported_fc4s(shost)[7] = 1;
2925
James Smart92d7f7b2007-06-17 19:56:38 -05002926 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
2927 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04002928
2929 fc_host_supported_speeds(shost) = 0;
2930 if (phba->lmt & LMT_10Gb)
2931 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04002932 if (phba->lmt & LMT_8Gb)
2933 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04002934 if (phba->lmt & LMT_4Gb)
2935 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
2936 if (phba->lmt & LMT_2Gb)
2937 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
2938 if (phba->lmt & LMT_1Gb)
2939 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
2940
2941 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05002942 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
2943 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04002944
Mike Christie0af5d702010-09-15 16:52:31 -05002945 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
2946
James Smart47a86172007-04-25 09:53:22 -04002947 /* This value is also unchanging */
2948 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05002949 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04002950 fc_host_active_fc4s(shost)[2] = 1;
2951 fc_host_active_fc4s(shost)[7] = 1;
2952
James Smart92d7f7b2007-06-17 19:56:38 -05002953 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04002954 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04002955 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04002956 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04002957}
dea31012005-04-17 16:05:31 -05002958
James Smarte59058c2008-08-24 21:49:00 -04002959/**
James Smartda0436e2009-05-22 14:51:39 -04002960 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04002961 * @phba: pointer to lpfc hba data structure.
2962 *
James Smartda0436e2009-05-22 14:51:39 -04002963 * This routine is invoked to stop an SLI3 device port, it stops the device
2964 * from generating interrupts and stops the device driver's timers for the
2965 * device.
James Smarte59058c2008-08-24 21:49:00 -04002966 **/
James Smartdb2378e2008-02-08 18:49:51 -05002967static void
James Smartda0436e2009-05-22 14:51:39 -04002968lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05002969{
James Smartda0436e2009-05-22 14:51:39 -04002970 /* Clear all interrupt enable conditions */
2971 writel(0, phba->HCregaddr);
2972 readl(phba->HCregaddr); /* flush */
2973 /* Clear all pending interrupts */
2974 writel(0xffffffff, phba->HAregaddr);
2975 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04002976
James Smartda0436e2009-05-22 14:51:39 -04002977 /* Reset some HBA SLI setup states */
2978 lpfc_stop_hba_timers(phba);
2979 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05002980}
2981
James Smarte59058c2008-08-24 21:49:00 -04002982/**
James Smartda0436e2009-05-22 14:51:39 -04002983 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05002984 * @phba: pointer to lpfc hba data structure.
2985 *
James Smartda0436e2009-05-22 14:51:39 -04002986 * This routine is invoked to stop an SLI4 device port, it stops the device
2987 * from generating interrupts and stops the device driver's timers for the
2988 * device.
2989 **/
2990static void
2991lpfc_stop_port_s4(struct lpfc_hba *phba)
2992{
2993 /* Reset some HBA SLI4 setup states */
2994 lpfc_stop_hba_timers(phba);
2995 phba->pport->work_port_events = 0;
2996 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04002997}
2998
2999/**
3000 * lpfc_stop_port - Wrapper function for stopping hba port
3001 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05003002 *
James Smartda0436e2009-05-22 14:51:39 -04003003 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
3004 * the API jump table function pointer from the lpfc_hba struct.
3005 **/
3006void
3007lpfc_stop_port(struct lpfc_hba *phba)
3008{
3009 phba->lpfc_stop_port(phba);
3010}
3011
3012/**
James Smartecfd03c2010-02-12 14:41:27 -05003013 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
3014 * @phba: Pointer to hba for which this call is being executed.
3015 *
3016 * This routine starts the timer waiting for the FCF rediscovery to complete.
3017 **/
3018void
3019lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
3020{
3021 unsigned long fcf_redisc_wait_tmo =
3022 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
3023 /* Start fcf rediscovery wait period timer */
3024 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
3025 spin_lock_irq(&phba->hbalock);
3026 /* Allow action to new fcf asynchronous event */
3027 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
3028 /* Mark the FCF rediscovery pending state */
3029 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
3030 spin_unlock_irq(&phba->hbalock);
3031}
3032
3033/**
3034 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
3035 * @ptr: Map to lpfc_hba data structure pointer.
3036 *
3037 * This routine is invoked when waiting for FCF table rediscover has been
3038 * timed out. If new FCF record(s) has (have) been discovered during the
3039 * wait period, a new FCF event shall be added to the FCOE async event
3040 * list, and then worker thread shall be waked up for processing from the
3041 * worker thread context.
3042 **/
3043void
3044lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
3045{
3046 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
3047
3048 /* Don't send FCF rediscovery event if timer cancelled */
3049 spin_lock_irq(&phba->hbalock);
3050 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
3051 spin_unlock_irq(&phba->hbalock);
3052 return;
3053 }
3054 /* Clear FCF rediscovery timer pending flag */
3055 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
3056 /* FCF rediscovery event to worker thread */
3057 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
3058 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003059 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04003060 "2776 FCF rediscover quiescent timer expired\n");
James Smartecfd03c2010-02-12 14:41:27 -05003061 /* wake up worker thread */
3062 lpfc_worker_wake_up(phba);
3063}
3064
3065/**
James Smartda0436e2009-05-22 14:51:39 -04003066 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
3067 * @phba: pointer to lpfc hba data structure.
3068 * @acqe_link: pointer to the async link completion queue entry.
3069 *
3070 * This routine is to parse the SLI4 link-attention link fault code and
3071 * translate it into the base driver's read link attention mailbox command
3072 * status.
3073 *
3074 * Return: Link-attention status in terms of base driver's coding.
3075 **/
3076static uint16_t
3077lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
3078 struct lpfc_acqe_link *acqe_link)
3079{
3080 uint16_t latt_fault;
3081
3082 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
3083 case LPFC_ASYNC_LINK_FAULT_NONE:
3084 case LPFC_ASYNC_LINK_FAULT_LOCAL:
3085 case LPFC_ASYNC_LINK_FAULT_REMOTE:
3086 latt_fault = 0;
3087 break;
3088 default:
3089 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3090 "0398 Invalid link fault code: x%x\n",
3091 bf_get(lpfc_acqe_link_fault, acqe_link));
3092 latt_fault = MBXERR_ERROR;
3093 break;
3094 }
3095 return latt_fault;
3096}
3097
3098/**
3099 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
3100 * @phba: pointer to lpfc hba data structure.
3101 * @acqe_link: pointer to the async link completion queue entry.
3102 *
3103 * This routine is to parse the SLI4 link attention type and translate it
3104 * into the base driver's link attention type coding.
3105 *
3106 * Return: Link attention type in terms of base driver's coding.
3107 **/
3108static uint8_t
3109lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
3110 struct lpfc_acqe_link *acqe_link)
3111{
3112 uint8_t att_type;
3113
3114 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
3115 case LPFC_ASYNC_LINK_STATUS_DOWN:
3116 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
James Smart76a95d72010-11-20 23:11:48 -05003117 att_type = LPFC_ATT_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04003118 break;
3119 case LPFC_ASYNC_LINK_STATUS_UP:
3120 /* Ignore physical link up events - wait for logical link up */
James Smart76a95d72010-11-20 23:11:48 -05003121 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003122 break;
3123 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
James Smart76a95d72010-11-20 23:11:48 -05003124 att_type = LPFC_ATT_LINK_UP;
James Smartda0436e2009-05-22 14:51:39 -04003125 break;
3126 default:
3127 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3128 "0399 Invalid link attention type: x%x\n",
3129 bf_get(lpfc_acqe_link_status, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003130 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003131 break;
3132 }
3133 return att_type;
3134}
3135
3136/**
3137 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
3138 * @phba: pointer to lpfc hba data structure.
3139 * @acqe_link: pointer to the async link completion queue entry.
3140 *
3141 * This routine is to parse the SLI4 link-attention link speed and translate
3142 * it into the base driver's link-attention link speed coding.
3143 *
3144 * Return: Link-attention link speed in terms of base driver's coding.
3145 **/
3146static uint8_t
3147lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
3148 struct lpfc_acqe_link *acqe_link)
3149{
3150 uint8_t link_speed;
3151
3152 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
3153 case LPFC_ASYNC_LINK_SPEED_ZERO:
James Smartda0436e2009-05-22 14:51:39 -04003154 case LPFC_ASYNC_LINK_SPEED_10MBPS:
James Smartda0436e2009-05-22 14:51:39 -04003155 case LPFC_ASYNC_LINK_SPEED_100MBPS:
James Smart76a95d72010-11-20 23:11:48 -05003156 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003157 break;
3158 case LPFC_ASYNC_LINK_SPEED_1GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003159 link_speed = LPFC_LINK_SPEED_1GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003160 break;
3161 case LPFC_ASYNC_LINK_SPEED_10GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003162 link_speed = LPFC_LINK_SPEED_10GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003163 break;
3164 default:
3165 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3166 "0483 Invalid link-attention link speed: x%x\n",
3167 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003168 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003169 break;
3170 }
3171 return link_speed;
3172}
3173
3174/**
James Smart70f3c072010-12-15 17:57:33 -05003175 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
James Smartda0436e2009-05-22 14:51:39 -04003176 * @phba: pointer to lpfc hba data structure.
3177 * @acqe_link: pointer to the async link completion queue entry.
3178 *
James Smart70f3c072010-12-15 17:57:33 -05003179 * This routine is to handle the SLI4 asynchronous FCoE link event.
James Smartda0436e2009-05-22 14:51:39 -04003180 **/
3181static void
3182lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3183 struct lpfc_acqe_link *acqe_link)
3184{
3185 struct lpfc_dmabuf *mp;
3186 LPFC_MBOXQ_t *pmb;
3187 MAILBOX_t *mb;
James Smart76a95d72010-11-20 23:11:48 -05003188 struct lpfc_mbx_read_top *la;
James Smartda0436e2009-05-22 14:51:39 -04003189 uint8_t att_type;
James Smart76a95d72010-11-20 23:11:48 -05003190 int rc;
James Smartda0436e2009-05-22 14:51:39 -04003191
3192 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05003193 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
James Smartda0436e2009-05-22 14:51:39 -04003194 return;
James Smart32b97932009-07-19 10:01:21 -04003195 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003196 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3197 if (!pmb) {
3198 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3199 "0395 The mboxq allocation failed\n");
3200 return;
3201 }
3202 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3203 if (!mp) {
3204 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3205 "0396 The lpfc_dmabuf allocation failed\n");
3206 goto out_free_pmb;
3207 }
3208 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3209 if (!mp->virt) {
3210 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3211 "0397 The mbuf allocation failed\n");
3212 goto out_free_dmabuf;
3213 }
3214
3215 /* Cleanup any outstanding ELS commands */
3216 lpfc_els_flush_all_cmd(phba);
3217
3218 /* Block ELS IOCBs until we have done process link event */
3219 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3220
3221 /* Update link event statistics */
3222 phba->sli.slistat.link_event++;
3223
James Smart76a95d72010-11-20 23:11:48 -05003224 /* Create lpfc_handle_latt mailbox command from link ACQE */
3225 lpfc_read_topology(phba, pmb, mp);
3226 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smartda0436e2009-05-22 14:51:39 -04003227 pmb->vport = phba->pport;
3228
James Smartda0436e2009-05-22 14:51:39 -04003229 /* Keep the link status for extra SLI4 state machine reference */
3230 phba->sli4_hba.link_state.speed =
3231 bf_get(lpfc_acqe_link_speed, acqe_link);
3232 phba->sli4_hba.link_state.duplex =
3233 bf_get(lpfc_acqe_link_duplex, acqe_link);
3234 phba->sli4_hba.link_state.status =
3235 bf_get(lpfc_acqe_link_status, acqe_link);
James Smart70f3c072010-12-15 17:57:33 -05003236 phba->sli4_hba.link_state.type =
3237 bf_get(lpfc_acqe_link_type, acqe_link);
3238 phba->sli4_hba.link_state.number =
3239 bf_get(lpfc_acqe_link_number, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04003240 phba->sli4_hba.link_state.fault =
3241 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05003242 phba->sli4_hba.link_state.logical_speed =
James Smart70f3c072010-12-15 17:57:33 -05003243 bf_get(lpfc_acqe_logical_link_speed, acqe_link);
3244 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartc31098c2011-04-16 11:03:33 -04003245 "2900 Async FC/FCoE Link event - Speed:%dGBit "
3246 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
3247 "Logical speed:%dMbps Fault:%d\n",
James Smart70f3c072010-12-15 17:57:33 -05003248 phba->sli4_hba.link_state.speed,
3249 phba->sli4_hba.link_state.topology,
3250 phba->sli4_hba.link_state.status,
3251 phba->sli4_hba.link_state.type,
3252 phba->sli4_hba.link_state.number,
3253 phba->sli4_hba.link_state.logical_speed * 10,
3254 phba->sli4_hba.link_state.fault);
James Smart76a95d72010-11-20 23:11:48 -05003255 /*
3256 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
3257 * topology info. Note: Optional for non FC-AL ports.
3258 */
3259 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3260 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3261 if (rc == MBX_NOT_FINISHED)
3262 goto out_free_dmabuf;
3263 return;
3264 }
3265 /*
3266 * For FCoE Mode: fill in all the topology information we need and call
3267 * the READ_TOPOLOGY completion routine to continue without actually
3268 * sending the READ_TOPOLOGY mailbox command to the port.
3269 */
3270 /* Parse and translate status field */
3271 mb = &pmb->u.mb;
3272 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
3273
3274 /* Parse and translate link attention fields */
3275 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
3276 la->eventTag = acqe_link->event_tag;
3277 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
3278 bf_set(lpfc_mbx_read_top_link_spd, la,
3279 lpfc_sli4_parse_latt_link_speed(phba, acqe_link));
3280
3281 /* Fake the the following irrelvant fields */
3282 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
3283 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
3284 bf_set(lpfc_mbx_read_top_il, la, 0);
3285 bf_set(lpfc_mbx_read_top_pb, la, 0);
3286 bf_set(lpfc_mbx_read_top_fa, la, 0);
3287 bf_set(lpfc_mbx_read_top_mm, la, 0);
James Smartda0436e2009-05-22 14:51:39 -04003288
3289 /* Invoke the lpfc_handle_latt mailbox command callback function */
James Smart76a95d72010-11-20 23:11:48 -05003290 lpfc_mbx_cmpl_read_topology(phba, pmb);
James Smartda0436e2009-05-22 14:51:39 -04003291
3292 return;
3293
3294out_free_dmabuf:
3295 kfree(mp);
3296out_free_pmb:
3297 mempool_free(pmb, phba->mbox_mem_pool);
3298}
3299
3300/**
James Smart70f3c072010-12-15 17:57:33 -05003301 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
3302 * @phba: pointer to lpfc hba data structure.
3303 * @acqe_fc: pointer to the async fc completion queue entry.
3304 *
3305 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
3306 * that the event was received and then issue a read_topology mailbox command so
3307 * that the rest of the driver will treat it the same as SLI3.
3308 **/
3309static void
3310lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
3311{
3312 struct lpfc_dmabuf *mp;
3313 LPFC_MBOXQ_t *pmb;
3314 int rc;
3315
3316 if (bf_get(lpfc_trailer_type, acqe_fc) !=
3317 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
3318 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3319 "2895 Non FC link Event detected.(%d)\n",
3320 bf_get(lpfc_trailer_type, acqe_fc));
3321 return;
3322 }
3323 /* Keep the link status for extra SLI4 state machine reference */
3324 phba->sli4_hba.link_state.speed =
3325 bf_get(lpfc_acqe_fc_la_speed, acqe_fc);
3326 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
3327 phba->sli4_hba.link_state.topology =
3328 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
3329 phba->sli4_hba.link_state.status =
3330 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
3331 phba->sli4_hba.link_state.type =
3332 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
3333 phba->sli4_hba.link_state.number =
3334 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
3335 phba->sli4_hba.link_state.fault =
3336 bf_get(lpfc_acqe_link_fault, acqe_fc);
3337 phba->sli4_hba.link_state.logical_speed =
3338 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc);
3339 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3340 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
3341 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
3342 "%dMbps Fault:%d\n",
3343 phba->sli4_hba.link_state.speed,
3344 phba->sli4_hba.link_state.topology,
3345 phba->sli4_hba.link_state.status,
3346 phba->sli4_hba.link_state.type,
3347 phba->sli4_hba.link_state.number,
3348 phba->sli4_hba.link_state.logical_speed * 10,
3349 phba->sli4_hba.link_state.fault);
3350 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3351 if (!pmb) {
3352 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3353 "2897 The mboxq allocation failed\n");
3354 return;
3355 }
3356 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3357 if (!mp) {
3358 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3359 "2898 The lpfc_dmabuf allocation failed\n");
3360 goto out_free_pmb;
3361 }
3362 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3363 if (!mp->virt) {
3364 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3365 "2899 The mbuf allocation failed\n");
3366 goto out_free_dmabuf;
3367 }
3368
3369 /* Cleanup any outstanding ELS commands */
3370 lpfc_els_flush_all_cmd(phba);
3371
3372 /* Block ELS IOCBs until we have done process link event */
3373 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3374
3375 /* Update link event statistics */
3376 phba->sli.slistat.link_event++;
3377
3378 /* Create lpfc_handle_latt mailbox command from link ACQE */
3379 lpfc_read_topology(phba, pmb, mp);
3380 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
3381 pmb->vport = phba->pport;
3382
3383 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3384 if (rc == MBX_NOT_FINISHED)
3385 goto out_free_dmabuf;
3386 return;
3387
3388out_free_dmabuf:
3389 kfree(mp);
3390out_free_pmb:
3391 mempool_free(pmb, phba->mbox_mem_pool);
3392}
3393
3394/**
3395 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
3396 * @phba: pointer to lpfc hba data structure.
3397 * @acqe_fc: pointer to the async SLI completion queue entry.
3398 *
3399 * This routine is to handle the SLI4 asynchronous SLI events.
3400 **/
3401static void
3402lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
3403{
3404 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3405 "2901 Async SLI event - Event Data1:x%08x Event Data2:"
3406 "x%08x SLI Event Type:%d",
3407 acqe_sli->event_data1, acqe_sli->event_data2,
3408 bf_get(lpfc_trailer_type, acqe_sli));
3409 return;
3410}
3411
3412/**
James Smartfc2b9892010-02-26 14:15:29 -05003413 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
3414 * @vport: pointer to vport data structure.
3415 *
3416 * This routine is to perform Clear Virtual Link (CVL) on a vport in
3417 * response to a CVL event.
3418 *
3419 * Return the pointer to the ndlp with the vport if successful, otherwise
3420 * return NULL.
3421 **/
3422static struct lpfc_nodelist *
3423lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
3424{
3425 struct lpfc_nodelist *ndlp;
3426 struct Scsi_Host *shost;
3427 struct lpfc_hba *phba;
3428
3429 if (!vport)
3430 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05003431 phba = vport->phba;
3432 if (!phba)
3433 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04003434 ndlp = lpfc_findnode_did(vport, Fabric_DID);
3435 if (!ndlp) {
3436 /* Cannot find existing Fabric ndlp, so allocate a new one */
3437 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
3438 if (!ndlp)
3439 return 0;
3440 lpfc_nlp_init(vport, ndlp, Fabric_DID);
3441 /* Set the node type */
3442 ndlp->nlp_type |= NLP_FABRIC;
3443 /* Put ndlp onto node list */
3444 lpfc_enqueue_node(vport, ndlp);
3445 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
3446 /* re-setup ndlp without removing from node list */
3447 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
3448 if (!ndlp)
3449 return 0;
3450 }
James Smart63e801c2010-11-20 23:14:19 -05003451 if ((phba->pport->port_state < LPFC_FLOGI) &&
3452 (phba->pport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05003453 return NULL;
3454 /* If virtual link is not yet instantiated ignore CVL */
James Smart63e801c2010-11-20 23:14:19 -05003455 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
3456 && (vport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05003457 return NULL;
3458 shost = lpfc_shost_from_vport(vport);
3459 if (!shost)
3460 return NULL;
3461 lpfc_linkdown_port(vport);
3462 lpfc_cleanup_pending_mbox(vport);
3463 spin_lock_irq(shost->host_lock);
3464 vport->fc_flag |= FC_VPORT_CVL_RCVD;
3465 spin_unlock_irq(shost->host_lock);
3466
3467 return ndlp;
3468}
3469
3470/**
3471 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
3472 * @vport: pointer to lpfc hba data structure.
3473 *
3474 * This routine is to perform Clear Virtual Link (CVL) on all vports in
3475 * response to a FCF dead event.
3476 **/
3477static void
3478lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
3479{
3480 struct lpfc_vport **vports;
3481 int i;
3482
3483 vports = lpfc_create_vport_work_array(phba);
3484 if (vports)
3485 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
3486 lpfc_sli4_perform_vport_cvl(vports[i]);
3487 lpfc_destroy_vport_work_array(phba, vports);
3488}
3489
3490/**
James Smart76a95d72010-11-20 23:11:48 -05003491 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
James Smartda0436e2009-05-22 14:51:39 -04003492 * @phba: pointer to lpfc hba data structure.
3493 * @acqe_link: pointer to the async fcoe completion queue entry.
3494 *
3495 * This routine is to handle the SLI4 asynchronous fcoe event.
3496 **/
3497static void
James Smart76a95d72010-11-20 23:11:48 -05003498lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
James Smart70f3c072010-12-15 17:57:33 -05003499 struct lpfc_acqe_fip *acqe_fip)
James Smartda0436e2009-05-22 14:51:39 -04003500{
James Smart70f3c072010-12-15 17:57:33 -05003501 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04003502 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04003503 struct lpfc_vport *vport;
3504 struct lpfc_nodelist *ndlp;
3505 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05003506 int active_vlink_present;
3507 struct lpfc_vport **vports;
3508 int i;
James Smartda0436e2009-05-22 14:51:39 -04003509
James Smart70f3c072010-12-15 17:57:33 -05003510 phba->fc_eventTag = acqe_fip->event_tag;
3511 phba->fcoe_eventtag = acqe_fip->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003512 switch (event_type) {
James Smart70f3c072010-12-15 17:57:33 -05003513 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
3514 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
3515 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
James Smart999d8132010-03-15 11:24:56 -04003516 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3517 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003518 "2546 New FCF event, evt_tag:x%x, "
3519 "index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05003520 acqe_fip->event_tag,
3521 acqe_fip->index);
James Smart999d8132010-03-15 11:24:56 -04003522 else
3523 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
3524 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003525 "2788 FCF param modified event, "
3526 "evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05003527 acqe_fip->event_tag,
3528 acqe_fip->index);
James Smart38b92ef2010-08-04 16:11:39 -04003529 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smart0c9ab6f2010-02-26 14:15:57 -05003530 /*
3531 * During period of FCF discovery, read the FCF
3532 * table record indexed by the event to update
James Smarta93ff372010-10-22 11:06:08 -04003533 * FCF roundrobin failover eligible FCF bmask.
James Smart0c9ab6f2010-02-26 14:15:57 -05003534 */
3535 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
3536 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003537 "2779 Read FCF (x%x) for updating "
3538 "roundrobin FCF failover bmask\n",
James Smart70f3c072010-12-15 17:57:33 -05003539 acqe_fip->index);
3540 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05003541 }
James Smart38b92ef2010-08-04 16:11:39 -04003542
3543 /* If the FCF discovery is in progress, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04003544 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04003545 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart38b92ef2010-08-04 16:11:39 -04003546 spin_unlock_irq(&phba->hbalock);
3547 break;
3548 }
3549 /* If fast FCF failover rescan event is pending, do nothing */
3550 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
3551 spin_unlock_irq(&phba->hbalock);
3552 break;
3553 }
3554
3555 /* If the FCF has been in discovered state, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04003556 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
3557 spin_unlock_irq(&phba->hbalock);
3558 break;
3559 }
3560 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04003561
James Smart0c9ab6f2010-02-26 14:15:57 -05003562 /* Otherwise, scan the entire FCF table and re-discover SAN */
3563 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003564 "2770 Start FCF table scan per async FCF "
3565 "event, evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05003566 acqe_fip->event_tag, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05003567 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3568 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04003569 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05003570 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3571 "2547 Issue FCF scan read FCF mailbox "
James Smarta93ff372010-10-22 11:06:08 -04003572 "command failed (x%x)\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04003573 break;
3574
James Smart70f3c072010-12-15 17:57:33 -05003575 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
James Smartda0436e2009-05-22 14:51:39 -04003576 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04003577 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05003578 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
3579 acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04003580 break;
3581
James Smart70f3c072010-12-15 17:57:33 -05003582 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
James Smart0c9ab6f2010-02-26 14:15:57 -05003583 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003584 "2549 FCF (x%x) disconnected from network, "
James Smart70f3c072010-12-15 17:57:33 -05003585 "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart38b92ef2010-08-04 16:11:39 -04003586 /*
3587 * If we are in the middle of FCF failover process, clear
3588 * the corresponding FCF bit in the roundrobin bitmap.
James Smartda0436e2009-05-22 14:51:39 -04003589 */
James Smartfc2b9892010-02-26 14:15:29 -05003590 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003591 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05003592 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003593 /* Update FLOGI FCF failover eligible FCF bmask */
James Smart70f3c072010-12-15 17:57:33 -05003594 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05003595 break;
3596 }
James Smart38b92ef2010-08-04 16:11:39 -04003597 spin_unlock_irq(&phba->hbalock);
3598
3599 /* If the event is not for currently used fcf do nothing */
James Smart70f3c072010-12-15 17:57:33 -05003600 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
James Smart38b92ef2010-08-04 16:11:39 -04003601 break;
3602
3603 /*
3604 * Otherwise, request the port to rediscover the entire FCF
3605 * table for a fast recovery from case that the current FCF
3606 * is no longer valid as we are not in the middle of FCF
3607 * failover process already.
3608 */
3609 spin_lock_irq(&phba->hbalock);
James Smartfc2b9892010-02-26 14:15:29 -05003610 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05003611 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003612 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04003613
James Smart0c9ab6f2010-02-26 14:15:57 -05003614 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3615 "2771 Start FCF fast failover process due to "
3616 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
James Smart70f3c072010-12-15 17:57:33 -05003617 "\n", acqe_fip->event_tag, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05003618 rc = lpfc_sli4_redisc_fcf_table(phba);
3619 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05003620 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3621 LOG_DISCOVERY,
3622 "2772 Issue FCF rediscover mabilbox "
3623 "command failed, fail through to FCF "
3624 "dead event\n");
James Smartfc2b9892010-02-26 14:15:29 -05003625 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003626 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003627 spin_unlock_irq(&phba->hbalock);
3628 /*
3629 * Last resort will fail over by treating this
3630 * as a link down to FCF registration.
3631 */
3632 lpfc_sli4_fcf_dead_failthrough(phba);
James Smart38b92ef2010-08-04 16:11:39 -04003633 } else {
3634 /* Reset FCF roundrobin bmask for new discovery */
3635 memset(phba->fcf.fcf_rr_bmask, 0,
3636 sizeof(*phba->fcf.fcf_rr_bmask));
3637 /*
3638 * Handling fast FCF failover to a DEAD FCF event is
3639 * considered equalivant to receiving CVL to all vports.
James Smartfc2b9892010-02-26 14:15:29 -05003640 */
3641 lpfc_sli4_perform_all_vport_cvl(phba);
James Smart38b92ef2010-08-04 16:11:39 -04003642 }
James Smartda0436e2009-05-22 14:51:39 -04003643 break;
James Smart70f3c072010-12-15 17:57:33 -05003644 case LPFC_FIP_EVENT_TYPE_CVL:
James Smart0c9ab6f2010-02-26 14:15:57 -05003645 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6669f9b2009-10-02 15:16:45 -04003646 "2718 Clear Virtual Link Received for VPI 0x%x"
James Smart70f3c072010-12-15 17:57:33 -05003647 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart6669f9b2009-10-02 15:16:45 -04003648 vport = lpfc_find_vport_by_vpid(phba,
James Smart70f3c072010-12-15 17:57:33 -05003649 acqe_fip->index - phba->vpi_base);
James Smartfc2b9892010-02-26 14:15:29 -05003650 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04003651 if (!ndlp)
3652 break;
James Smart695a8142010-01-26 23:08:03 -05003653 active_vlink_present = 0;
3654
3655 vports = lpfc_create_vport_work_array(phba);
3656 if (vports) {
3657 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
3658 i++) {
3659 if ((!(vports[i]->fc_flag &
3660 FC_VPORT_CVL_RCVD)) &&
3661 (vports[i]->port_state > LPFC_FDISC)) {
3662 active_vlink_present = 1;
3663 break;
3664 }
3665 }
3666 lpfc_destroy_vport_work_array(phba, vports);
3667 }
3668
3669 if (active_vlink_present) {
3670 /*
3671 * If there are other active VLinks present,
3672 * re-instantiate the Vlink using FDISC.
3673 */
James Smart6669f9b2009-10-02 15:16:45 -04003674 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
James Smartfc2b9892010-02-26 14:15:29 -05003675 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04003676 spin_lock_irq(shost->host_lock);
3677 ndlp->nlp_flag |= NLP_DELAY_TMO;
3678 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05003679 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
3680 vport->port_state = LPFC_FDISC;
3681 } else {
James Smartecfd03c2010-02-12 14:41:27 -05003682 /*
3683 * Otherwise, we request port to rediscover
3684 * the entire FCF table for a fast recovery
3685 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05003686 * is no longer valid if we are not already
3687 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05003688 */
James Smartfc2b9892010-02-26 14:15:29 -05003689 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003690 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05003691 spin_unlock_irq(&phba->hbalock);
3692 break;
3693 }
3694 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05003695 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003696 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003697 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
3698 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003699 "2773 Start FCF failover per CVL, "
James Smart70f3c072010-12-15 17:57:33 -05003700 "evt_tag:x%x\n", acqe_fip->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05003701 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05003702 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05003703 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3704 LOG_DISCOVERY,
3705 "2774 Issue FCF rediscover "
3706 "mabilbox command failed, "
3707 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05003708 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003709 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003710 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05003711 /*
3712 * Last resort will be re-try on the
3713 * the current registered FCF entry.
3714 */
3715 lpfc_retry_pport_discovery(phba);
James Smart38b92ef2010-08-04 16:11:39 -04003716 } else
3717 /*
3718 * Reset FCF roundrobin bmask for new
3719 * discovery.
3720 */
3721 memset(phba->fcf.fcf_rr_bmask, 0,
3722 sizeof(*phba->fcf.fcf_rr_bmask));
James Smart6669f9b2009-10-02 15:16:45 -04003723 }
3724 break;
James Smartda0436e2009-05-22 14:51:39 -04003725 default:
3726 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3727 "0288 Unknown FCoE event type 0x%x event tag "
James Smart70f3c072010-12-15 17:57:33 -05003728 "0x%x\n", event_type, acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04003729 break;
3730 }
3731}
3732
3733/**
3734 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
3735 * @phba: pointer to lpfc hba data structure.
3736 * @acqe_link: pointer to the async dcbx completion queue entry.
3737 *
3738 * This routine is to handle the SLI4 asynchronous dcbx event.
3739 **/
3740static void
3741lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
3742 struct lpfc_acqe_dcbx *acqe_dcbx)
3743{
James Smart4d9ab992009-10-02 15:16:39 -04003744 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003745 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3746 "0290 The SLI4 DCBX asynchronous event is not "
3747 "handled yet\n");
3748}
3749
3750/**
James Smartb19a0612010-04-06 14:48:51 -04003751 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
3752 * @phba: pointer to lpfc hba data structure.
3753 * @acqe_link: pointer to the async grp5 completion queue entry.
3754 *
3755 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
3756 * is an asynchronous notified of a logical link speed change. The Port
3757 * reports the logical link speed in units of 10Mbps.
3758 **/
3759static void
3760lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
3761 struct lpfc_acqe_grp5 *acqe_grp5)
3762{
3763 uint16_t prev_ll_spd;
3764
3765 phba->fc_eventTag = acqe_grp5->event_tag;
3766 phba->fcoe_eventtag = acqe_grp5->event_tag;
3767 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
3768 phba->sli4_hba.link_state.logical_speed =
3769 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5));
3770 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3771 "2789 GRP5 Async Event: Updating logical link speed "
3772 "from %dMbps to %dMbps\n", (prev_ll_spd * 10),
3773 (phba->sli4_hba.link_state.logical_speed*10));
3774}
3775
3776/**
James Smartda0436e2009-05-22 14:51:39 -04003777 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
3778 * @phba: pointer to lpfc hba data structure.
3779 *
3780 * This routine is invoked by the worker thread to process all the pending
3781 * SLI4 asynchronous events.
3782 **/
3783void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
3784{
3785 struct lpfc_cq_event *cq_event;
3786
3787 /* First, declare the async event has been handled */
3788 spin_lock_irq(&phba->hbalock);
3789 phba->hba_flag &= ~ASYNC_EVENT;
3790 spin_unlock_irq(&phba->hbalock);
3791 /* Now, handle all the async events */
3792 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
3793 /* Get the first event from the head of the event queue */
3794 spin_lock_irq(&phba->hbalock);
3795 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
3796 cq_event, struct lpfc_cq_event, list);
3797 spin_unlock_irq(&phba->hbalock);
3798 /* Process the asynchronous event */
3799 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
3800 case LPFC_TRAILER_CODE_LINK:
3801 lpfc_sli4_async_link_evt(phba,
3802 &cq_event->cqe.acqe_link);
3803 break;
3804 case LPFC_TRAILER_CODE_FCOE:
James Smart70f3c072010-12-15 17:57:33 -05003805 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04003806 break;
3807 case LPFC_TRAILER_CODE_DCBX:
3808 lpfc_sli4_async_dcbx_evt(phba,
3809 &cq_event->cqe.acqe_dcbx);
3810 break;
James Smartb19a0612010-04-06 14:48:51 -04003811 case LPFC_TRAILER_CODE_GRP5:
3812 lpfc_sli4_async_grp5_evt(phba,
3813 &cq_event->cqe.acqe_grp5);
3814 break;
James Smart70f3c072010-12-15 17:57:33 -05003815 case LPFC_TRAILER_CODE_FC:
3816 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
3817 break;
3818 case LPFC_TRAILER_CODE_SLI:
3819 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
3820 break;
James Smartda0436e2009-05-22 14:51:39 -04003821 default:
3822 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3823 "1804 Invalid asynchrous event code: "
3824 "x%x\n", bf_get(lpfc_trailer_code,
3825 &cq_event->cqe.mcqe_cmpl));
3826 break;
3827 }
3828 /* Free the completion event processed to the free pool */
3829 lpfc_sli4_cq_event_release(phba, cq_event);
3830 }
3831}
3832
3833/**
James Smartecfd03c2010-02-12 14:41:27 -05003834 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
3835 * @phba: pointer to lpfc hba data structure.
3836 *
3837 * This routine is invoked by the worker thread to process FCF table
3838 * rediscovery pending completion event.
3839 **/
3840void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
3841{
3842 int rc;
3843
3844 spin_lock_irq(&phba->hbalock);
3845 /* Clear FCF rediscovery timeout event */
3846 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
3847 /* Clear driver fast failover FCF record flag */
3848 phba->fcf.failover_rec.flag = 0;
3849 /* Set state for FCF fast failover */
3850 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
3851 spin_unlock_irq(&phba->hbalock);
3852
3853 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05003854 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003855 "2777 Start post-quiescent FCF table scan\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05003856 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05003857 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05003858 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3859 "2747 Issue FCF scan read FCF mailbox "
3860 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05003861}
3862
3863/**
James Smartda0436e2009-05-22 14:51:39 -04003864 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
3865 * @phba: pointer to lpfc hba data structure.
3866 * @dev_grp: The HBA PCI-Device group number.
3867 *
3868 * This routine is invoked to set up the per HBA PCI-Device group function
3869 * API jump table entries.
3870 *
3871 * Return: 0 if success, otherwise -ENODEV
3872 **/
3873int
3874lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05003875{
3876 int rc;
3877
James Smartda0436e2009-05-22 14:51:39 -04003878 /* Set up lpfc PCI-device group */
3879 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05003880
James Smartda0436e2009-05-22 14:51:39 -04003881 /* The LPFC_PCI_DEV_OC uses SLI4 */
3882 if (dev_grp == LPFC_PCI_DEV_OC)
3883 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05003884
James Smartda0436e2009-05-22 14:51:39 -04003885 /* Set up device INIT API function jump table */
3886 rc = lpfc_init_api_table_setup(phba, dev_grp);
3887 if (rc)
3888 return -ENODEV;
3889 /* Set up SCSI API function jump table */
3890 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
3891 if (rc)
3892 return -ENODEV;
3893 /* Set up SLI API function jump table */
3894 rc = lpfc_sli_api_table_setup(phba, dev_grp);
3895 if (rc)
3896 return -ENODEV;
3897 /* Set up MBOX API function jump table */
3898 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
3899 if (rc)
3900 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05003901
James Smartda0436e2009-05-22 14:51:39 -04003902 return 0;
James Smart5b75da22008-12-04 22:39:35 -05003903}
3904
3905/**
James Smart3621a712009-04-06 18:47:14 -04003906 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05003907 * @phba: pointer to lpfc hba data structure.
3908 * @intr_mode: active interrupt mode adopted.
3909 *
3910 * This routine it invoked to log the currently used active interrupt mode
3911 * to the device.
James Smart3772a992009-05-22 14:50:54 -04003912 **/
3913static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05003914{
3915 switch (intr_mode) {
3916 case 0:
3917 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3918 "0470 Enable INTx interrupt mode.\n");
3919 break;
3920 case 1:
3921 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3922 "0481 Enabled MSI interrupt mode.\n");
3923 break;
3924 case 2:
3925 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3926 "0480 Enabled MSI-X interrupt mode.\n");
3927 break;
3928 default:
3929 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3930 "0482 Illegal interrupt mode.\n");
3931 break;
3932 }
3933 return;
3934}
3935
James Smart5b75da22008-12-04 22:39:35 -05003936/**
James Smart3772a992009-05-22 14:50:54 -04003937 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05003938 * @phba: pointer to lpfc hba data structure.
3939 *
James Smart3772a992009-05-22 14:50:54 -04003940 * This routine is invoked to enable the PCI device that is common to all
3941 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05003942 *
3943 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003944 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04003945 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05003946 **/
James Smart3772a992009-05-22 14:50:54 -04003947static int
3948lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05003949{
James Smart3772a992009-05-22 14:50:54 -04003950 struct pci_dev *pdev;
3951 int bars;
James Smart5b75da22008-12-04 22:39:35 -05003952
James Smart3772a992009-05-22 14:50:54 -04003953 /* Obtain PCI device reference */
3954 if (!phba->pcidev)
3955 goto out_error;
3956 else
3957 pdev = phba->pcidev;
3958 /* Select PCI BARs */
3959 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3960 /* Enable PCI device */
3961 if (pci_enable_device_mem(pdev))
3962 goto out_error;
3963 /* Request PCI resource for the device */
3964 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
3965 goto out_disable_device;
3966 /* Set up device as PCI master and save state for EEH */
3967 pci_set_master(pdev);
3968 pci_try_set_mwi(pdev);
3969 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05003970
James Smart05580562011-05-24 11:40:48 -04003971 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
3972 if (pci_find_capability(pdev, PCI_CAP_ID_EXP))
3973 pdev->needs_freset = 1;
3974
James Smart3772a992009-05-22 14:50:54 -04003975 return 0;
James Smart5b75da22008-12-04 22:39:35 -05003976
James Smart3772a992009-05-22 14:50:54 -04003977out_disable_device:
3978 pci_disable_device(pdev);
3979out_error:
3980 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05003981}
3982
3983/**
James Smart3772a992009-05-22 14:50:54 -04003984 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05003985 * @phba: pointer to lpfc hba data structure.
3986 *
James Smart3772a992009-05-22 14:50:54 -04003987 * This routine is invoked to disable the PCI device that is common to all
3988 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05003989 **/
3990static void
James Smart3772a992009-05-22 14:50:54 -04003991lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05003992{
James Smart3772a992009-05-22 14:50:54 -04003993 struct pci_dev *pdev;
3994 int bars;
James Smart5b75da22008-12-04 22:39:35 -05003995
James Smart3772a992009-05-22 14:50:54 -04003996 /* Obtain PCI device reference */
3997 if (!phba->pcidev)
3998 return;
3999 else
4000 pdev = phba->pcidev;
4001 /* Select PCI BARs */
4002 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4003 /* Release PCI resource and disable PCI device */
4004 pci_release_selected_regions(pdev, bars);
4005 pci_disable_device(pdev);
4006 /* Null out PCI private reference to driver */
4007 pci_set_drvdata(pdev, NULL);
James Smart5b75da22008-12-04 22:39:35 -05004008
4009 return;
4010}
4011
4012/**
James Smart3772a992009-05-22 14:50:54 -04004013 * lpfc_reset_hba - Reset a hba
4014 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04004015 *
James Smart3772a992009-05-22 14:50:54 -04004016 * This routine is invoked to reset a hba device. It brings the HBA
4017 * offline, performs a board restart, and then brings the board back
4018 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
4019 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04004020 **/
James Smart3772a992009-05-22 14:50:54 -04004021void
4022lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004023{
James Smart3772a992009-05-22 14:50:54 -04004024 /* If resets are disabled then set error state and return. */
4025 if (!phba->cfg_enable_hba_reset) {
4026 phba->link_state = LPFC_HBA_ERROR;
4027 return;
4028 }
4029 lpfc_offline_prep(phba);
4030 lpfc_offline(phba);
4031 lpfc_sli_brdrestart(phba);
4032 lpfc_online(phba);
4033 lpfc_unblock_mgmt_io(phba);
4034}
dea31012005-04-17 16:05:31 -05004035
James Smart3772a992009-05-22 14:50:54 -04004036/**
James Smart912e3ac2011-05-24 11:42:11 -04004037 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
4038 * @phba: pointer to lpfc hba data structure.
4039 * @nr_vfn: number of virtual functions to be enabled.
4040 *
4041 * This function enables the PCI SR-IOV virtual functions to a physical
4042 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4043 * enable the number of virtual functions to the physical function. As
4044 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4045 * API call does not considered as an error condition for most of the device.
4046 **/
4047int
4048lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
4049{
4050 struct pci_dev *pdev = phba->pcidev;
4051 int rc;
4052
4053 rc = pci_enable_sriov(pdev, nr_vfn);
4054 if (rc) {
4055 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4056 "2806 Failed to enable sriov on this device "
4057 "with vfn number nr_vf:%d, rc:%d\n",
4058 nr_vfn, rc);
4059 } else
4060 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4061 "2807 Successful enable sriov on this device "
4062 "with vfn number nr_vf:%d\n", nr_vfn);
4063 return rc;
4064}
4065
4066/**
James Smart3772a992009-05-22 14:50:54 -04004067 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
4068 * @phba: pointer to lpfc hba data structure.
4069 *
4070 * This routine is invoked to set up the driver internal resources specific to
4071 * support the SLI-3 HBA device it attached to.
4072 *
4073 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004074 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004075 * other values - error
4076 **/
4077static int
4078lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
4079{
4080 struct lpfc_sli *psli;
James Smart912e3ac2011-05-24 11:42:11 -04004081 int rc;
dea31012005-04-17 16:05:31 -05004082
James Smart3772a992009-05-22 14:50:54 -04004083 /*
4084 * Initialize timers used by driver
4085 */
dea31012005-04-17 16:05:31 -05004086
James Smart3772a992009-05-22 14:50:54 -04004087 /* Heartbeat timer */
4088 init_timer(&phba->hb_tmofunc);
4089 phba->hb_tmofunc.function = lpfc_hb_timeout;
4090 phba->hb_tmofunc.data = (unsigned long)phba;
4091
4092 psli = &phba->sli;
4093 /* MBOX heartbeat timer */
4094 init_timer(&psli->mbox_tmo);
4095 psli->mbox_tmo.function = lpfc_mbox_timeout;
4096 psli->mbox_tmo.data = (unsigned long) phba;
4097 /* FCP polling mode timer */
4098 init_timer(&phba->fcp_poll_timer);
4099 phba->fcp_poll_timer.function = lpfc_poll_timeout;
4100 phba->fcp_poll_timer.data = (unsigned long) phba;
4101 /* Fabric block timer */
4102 init_timer(&phba->fabric_block_timer);
4103 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4104 phba->fabric_block_timer.data = (unsigned long) phba;
4105 /* EA polling mode timer */
4106 init_timer(&phba->eratt_poll);
4107 phba->eratt_poll.function = lpfc_poll_eratt;
4108 phba->eratt_poll.data = (unsigned long) phba;
4109
4110 /* Host attention work mask setup */
4111 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
4112 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
4113
4114 /* Get all the module params for configuring this host */
4115 lpfc_get_cfgparam(phba);
James Smart49198b32010-04-06 15:04:33 -04004116 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
4117 phba->menlo_flag |= HBA_MENLO_SUPPORT;
4118 /* check for menlo minimum sg count */
4119 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
4120 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
4121 }
4122
James Smart3772a992009-05-22 14:50:54 -04004123 /*
4124 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
4125 * used to create the sg_dma_buf_pool must be dynamically calculated.
4126 * 2 segments are added since the IOCB needs a command and response bde.
4127 */
4128 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4129 sizeof(struct fcp_rsp) +
4130 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
4131
4132 if (phba->cfg_enable_bg) {
4133 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT;
4134 phba->cfg_sg_dma_buf_size +=
4135 phba->cfg_prot_sg_seg_cnt * sizeof(struct ulp_bde64);
4136 }
4137
4138 /* Also reinitialize the host templates with new values. */
4139 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4140 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4141
4142 phba->max_vpi = LPFC_MAX_VPI;
4143 /* This will be set to correct value after config_port mbox */
4144 phba->max_vports = 0;
4145
4146 /*
4147 * Initialize the SLI Layer to run with lpfc HBAs.
4148 */
4149 lpfc_sli_setup(phba);
4150 lpfc_sli_queue_setup(phba);
4151
4152 /* Allocate device driver memory */
4153 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
4154 return -ENOMEM;
4155
James Smart912e3ac2011-05-24 11:42:11 -04004156 /*
4157 * Enable sr-iov virtual functions if supported and configured
4158 * through the module parameter.
4159 */
4160 if (phba->cfg_sriov_nr_virtfn > 0) {
4161 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
4162 phba->cfg_sriov_nr_virtfn);
4163 if (rc) {
4164 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4165 "2808 Requested number of SR-IOV "
4166 "virtual functions (%d) is not "
4167 "supported\n",
4168 phba->cfg_sriov_nr_virtfn);
4169 phba->cfg_sriov_nr_virtfn = 0;
4170 }
4171 }
4172
James Smart3772a992009-05-22 14:50:54 -04004173 return 0;
4174}
4175
4176/**
4177 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
4178 * @phba: pointer to lpfc hba data structure.
4179 *
4180 * This routine is invoked to unset the driver internal resources set up
4181 * specific for supporting the SLI-3 HBA device it attached to.
4182 **/
4183static void
4184lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
4185{
4186 /* Free device driver memory allocated */
4187 lpfc_mem_free_all(phba);
4188
4189 return;
4190}
4191
4192/**
James Smartda0436e2009-05-22 14:51:39 -04004193 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
4194 * @phba: pointer to lpfc hba data structure.
4195 *
4196 * This routine is invoked to set up the driver internal resources specific to
4197 * support the SLI-4 HBA device it attached to.
4198 *
4199 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004200 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004201 * other values - error
4202 **/
4203static int
4204lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
4205{
4206 struct lpfc_sli *psli;
James Smart28baac72010-02-12 14:42:03 -05004207 LPFC_MBOXQ_t *mboxq;
4208 int rc, i, hbq_count, buf_size, dma_buf_size, max_buf_size;
4209 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
4210 struct lpfc_mqe *mqe;
James Smart085c6472010-11-20 23:11:37 -05004211 int longs, sli_family;
James Smartda0436e2009-05-22 14:51:39 -04004212
4213 /* Before proceed, wait for POST done and device ready */
4214 rc = lpfc_sli4_post_status_check(phba);
4215 if (rc)
4216 return -ENODEV;
4217
4218 /*
4219 * Initialize timers used by driver
4220 */
4221
4222 /* Heartbeat timer */
4223 init_timer(&phba->hb_tmofunc);
4224 phba->hb_tmofunc.function = lpfc_hb_timeout;
4225 phba->hb_tmofunc.data = (unsigned long)phba;
James Smart19ca7602010-11-20 23:11:55 -05004226 init_timer(&phba->rrq_tmr);
4227 phba->rrq_tmr.function = lpfc_rrq_timeout;
4228 phba->rrq_tmr.data = (unsigned long)phba;
James Smartda0436e2009-05-22 14:51:39 -04004229
4230 psli = &phba->sli;
4231 /* MBOX heartbeat timer */
4232 init_timer(&psli->mbox_tmo);
4233 psli->mbox_tmo.function = lpfc_mbox_timeout;
4234 psli->mbox_tmo.data = (unsigned long) phba;
4235 /* Fabric block timer */
4236 init_timer(&phba->fabric_block_timer);
4237 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4238 phba->fabric_block_timer.data = (unsigned long) phba;
4239 /* EA polling mode timer */
4240 init_timer(&phba->eratt_poll);
4241 phba->eratt_poll.function = lpfc_poll_eratt;
4242 phba->eratt_poll.data = (unsigned long) phba;
James Smartecfd03c2010-02-12 14:41:27 -05004243 /* FCF rediscover timer */
4244 init_timer(&phba->fcf.redisc_wait);
4245 phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
4246 phba->fcf.redisc_wait.data = (unsigned long)phba;
4247
James Smartda0436e2009-05-22 14:51:39 -04004248 /*
4249 * We need to do a READ_CONFIG mailbox command here before
4250 * calling lpfc_get_cfgparam. For VFs this will report the
4251 * MAX_XRI, MAX_VPI, MAX_RPI, MAX_IOCB, and MAX_VFI settings.
4252 * All of the resources allocated
4253 * for this Port are tied to these values.
4254 */
4255 /* Get all the module params for configuring this host */
4256 lpfc_get_cfgparam(phba);
4257 phba->max_vpi = LPFC_MAX_VPI;
4258 /* This will be set to correct value after the read_config mbox */
4259 phba->max_vports = 0;
4260
4261 /* Program the default value of vlan_id and fc_map */
4262 phba->valid_vlan = 0;
4263 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4264 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4265 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4266
4267 /*
4268 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
4269 * used to create the sg_dma_buf_pool must be dynamically calculated.
4270 * 2 segments are added since the IOCB needs a command and response bde.
4271 * To insure that the scsi sgl does not cross a 4k page boundary only
James Smart28baac72010-02-12 14:42:03 -05004272 * sgl sizes of must be a power of 2.
James Smartda0436e2009-05-22 14:51:39 -04004273 */
James Smart28baac72010-02-12 14:42:03 -05004274 buf_size = (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp) +
4275 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge)));
James Smart085c6472010-11-20 23:11:37 -05004276
4277 sli_family = bf_get(lpfc_sli_intf_sli_family, &phba->sli4_hba.sli_intf);
4278 max_buf_size = LPFC_SLI4_MAX_BUF_SIZE;
4279 switch (sli_family) {
4280 case LPFC_SLI_INTF_FAMILY_BE2:
4281 case LPFC_SLI_INTF_FAMILY_BE3:
4282 /* There is a single hint for BE - 2 pages per BPL. */
4283 if (bf_get(lpfc_sli_intf_sli_hint1, &phba->sli4_hba.sli_intf) ==
4284 LPFC_SLI_INTF_SLI_HINT1_1)
4285 max_buf_size = LPFC_SLI4_FL1_MAX_BUF_SIZE;
4286 break;
4287 case LPFC_SLI_INTF_FAMILY_LNCR_A0:
4288 case LPFC_SLI_INTF_FAMILY_LNCR_B0:
4289 default:
4290 break;
4291 }
James Smart28baac72010-02-12 14:42:03 -05004292 for (dma_buf_size = LPFC_SLI4_MIN_BUF_SIZE;
4293 dma_buf_size < max_buf_size && buf_size > dma_buf_size;
4294 dma_buf_size = dma_buf_size << 1)
4295 ;
4296 if (dma_buf_size == max_buf_size)
4297 phba->cfg_sg_seg_cnt = (dma_buf_size -
4298 sizeof(struct fcp_cmnd) - sizeof(struct fcp_rsp) -
4299 (2 * sizeof(struct sli4_sge))) /
4300 sizeof(struct sli4_sge);
4301 phba->cfg_sg_dma_buf_size = dma_buf_size;
James Smartda0436e2009-05-22 14:51:39 -04004302
4303 /* Initialize buffer queue management fields */
4304 hbq_count = lpfc_sli_hbq_count();
4305 for (i = 0; i < hbq_count; ++i)
4306 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
4307 INIT_LIST_HEAD(&phba->rb_pend_list);
4308 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
4309 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
4310
4311 /*
4312 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
4313 */
4314 /* Initialize the Abort scsi buffer list used by driver */
4315 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
4316 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
4317 /* This abort list used by worker thread */
4318 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
4319
4320 /*
4321 * Initialize dirver internal slow-path work queues
4322 */
4323
4324 /* Driver internel slow-path CQ Event pool */
4325 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
4326 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04004327 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04004328 /* Asynchronous event CQ Event work queue list */
4329 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
4330 /* Fast-path XRI aborted CQ Event work queue list */
4331 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
4332 /* Slow-path XRI aborted CQ Event work queue list */
4333 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
4334 /* Receive queue CQ Event work queue list */
4335 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
4336
4337 /* Initialize the driver internal SLI layer lists. */
4338 lpfc_sli_setup(phba);
4339 lpfc_sli_queue_setup(phba);
4340
4341 /* Allocate device driver memory */
4342 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
4343 if (rc)
4344 return -ENOMEM;
4345
James Smart2fcee4b2010-12-15 17:57:46 -05004346 /* IF Type 2 ports get initialized now. */
4347 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
4348 LPFC_SLI_INTF_IF_TYPE_2) {
4349 rc = lpfc_pci_function_reset(phba);
4350 if (unlikely(rc))
4351 return -ENODEV;
4352 }
4353
James Smartda0436e2009-05-22 14:51:39 -04004354 /* Create the bootstrap mailbox command */
4355 rc = lpfc_create_bootstrap_mbox(phba);
4356 if (unlikely(rc))
4357 goto out_free_mem;
4358
4359 /* Set up the host's endian order with the device. */
4360 rc = lpfc_setup_endian_order(phba);
4361 if (unlikely(rc))
4362 goto out_free_bsmbx;
4363
4364 /* Set up the hba's configuration parameters. */
4365 rc = lpfc_sli4_read_config(phba);
4366 if (unlikely(rc))
4367 goto out_free_bsmbx;
4368
James Smart2fcee4b2010-12-15 17:57:46 -05004369 /* IF Type 0 ports get initialized now. */
4370 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
4371 LPFC_SLI_INTF_IF_TYPE_0) {
4372 rc = lpfc_pci_function_reset(phba);
4373 if (unlikely(rc))
4374 goto out_free_bsmbx;
4375 }
James Smartda0436e2009-05-22 14:51:39 -04004376
James Smartcb5172e2010-03-15 11:25:07 -04004377 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
4378 GFP_KERNEL);
4379 if (!mboxq) {
4380 rc = -ENOMEM;
4381 goto out_free_bsmbx;
4382 }
4383
James Smartfedd3b72011-02-16 12:39:24 -05004384 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
James Smartcb5172e2010-03-15 11:25:07 -04004385 lpfc_supported_pages(mboxq);
4386 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smartfedd3b72011-02-16 12:39:24 -05004387 if (!rc) {
4388 mqe = &mboxq->u.mqe;
4389 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
4390 LPFC_MAX_SUPPORTED_PAGES);
4391 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
4392 switch (pn_page[i]) {
4393 case LPFC_SLI4_PARAMETERS:
4394 phba->sli4_hba.pc_sli4_params.supported = 1;
4395 break;
4396 default:
4397 break;
4398 }
4399 }
4400 /* Read the port's SLI4 Parameters capabilities if supported. */
4401 if (phba->sli4_hba.pc_sli4_params.supported)
4402 rc = lpfc_pc_sli4_params_get(phba, mboxq);
4403 if (rc) {
4404 mempool_free(mboxq, phba->mbox_mem_pool);
4405 rc = -EIO;
4406 goto out_free_bsmbx;
James Smartcb5172e2010-03-15 11:25:07 -04004407 }
4408 }
James Smartfedd3b72011-02-16 12:39:24 -05004409 /*
4410 * Get sli4 parameters that override parameters from Port capabilities.
4411 * If this call fails it is not a critical error so continue loading.
4412 */
4413 lpfc_get_sli4_parameters(phba, mboxq);
James Smartcb5172e2010-03-15 11:25:07 -04004414 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04004415 /* Create all the SLI4 queues */
4416 rc = lpfc_sli4_queue_create(phba);
4417 if (rc)
4418 goto out_free_bsmbx;
4419
4420 /* Create driver internal CQE event pool */
4421 rc = lpfc_sli4_cq_event_pool_create(phba);
4422 if (rc)
4423 goto out_destroy_queue;
4424
4425 /* Initialize and populate the iocb list per host */
4426 rc = lpfc_init_sgl_list(phba);
4427 if (rc) {
4428 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4429 "1400 Failed to initialize sgl list.\n");
4430 goto out_destroy_cq_event_pool;
4431 }
4432 rc = lpfc_init_active_sgl_array(phba);
4433 if (rc) {
4434 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4435 "1430 Failed to initialize sgl list.\n");
4436 goto out_free_sgl_list;
4437 }
4438
4439 rc = lpfc_sli4_init_rpi_hdrs(phba);
4440 if (rc) {
4441 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4442 "1432 Failed to initialize rpi headers.\n");
4443 goto out_free_active_sgl;
4444 }
4445
James Smarta93ff372010-10-22 11:06:08 -04004446 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
James Smart0c9ab6f2010-02-26 14:15:57 -05004447 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
4448 phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
4449 GFP_KERNEL);
4450 if (!phba->fcf.fcf_rr_bmask) {
4451 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4452 "2759 Failed allocate memory for FCF round "
4453 "robin failover bmask\n");
James Smart05580562011-05-24 11:40:48 -04004454 rc = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -05004455 goto out_remove_rpi_hdrs;
4456 }
4457
James Smartda0436e2009-05-22 14:51:39 -04004458 phba->sli4_hba.fcp_eq_hdl = kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
4459 phba->cfg_fcp_eq_count), GFP_KERNEL);
4460 if (!phba->sli4_hba.fcp_eq_hdl) {
4461 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4462 "2572 Failed allocate memory for fast-path "
4463 "per-EQ handle array\n");
James Smart05580562011-05-24 11:40:48 -04004464 rc = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -05004465 goto out_free_fcf_rr_bmask;
James Smartda0436e2009-05-22 14:51:39 -04004466 }
4467
4468 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
4469 phba->sli4_hba.cfg_eqn), GFP_KERNEL);
4470 if (!phba->sli4_hba.msix_entries) {
4471 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4472 "2573 Failed allocate memory for msi-x "
4473 "interrupt vector entries\n");
James Smart05580562011-05-24 11:40:48 -04004474 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04004475 goto out_free_fcp_eq_hdl;
4476 }
4477
James Smart912e3ac2011-05-24 11:42:11 -04004478 /*
4479 * Enable sr-iov virtual functions if supported and configured
4480 * through the module parameter.
4481 */
4482 if (phba->cfg_sriov_nr_virtfn > 0) {
4483 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
4484 phba->cfg_sriov_nr_virtfn);
4485 if (rc) {
4486 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4487 "3020 Requested number of SR-IOV "
4488 "virtual functions (%d) is not "
4489 "supported\n",
4490 phba->cfg_sriov_nr_virtfn);
4491 phba->cfg_sriov_nr_virtfn = 0;
4492 }
4493 }
4494
James Smartda0436e2009-05-22 14:51:39 -04004495 return rc;
4496
4497out_free_fcp_eq_hdl:
4498 kfree(phba->sli4_hba.fcp_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05004499out_free_fcf_rr_bmask:
4500 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04004501out_remove_rpi_hdrs:
4502 lpfc_sli4_remove_rpi_hdrs(phba);
4503out_free_active_sgl:
4504 lpfc_free_active_sgl(phba);
4505out_free_sgl_list:
4506 lpfc_free_sgl_list(phba);
4507out_destroy_cq_event_pool:
4508 lpfc_sli4_cq_event_pool_destroy(phba);
4509out_destroy_queue:
4510 lpfc_sli4_queue_destroy(phba);
4511out_free_bsmbx:
4512 lpfc_destroy_bootstrap_mbox(phba);
4513out_free_mem:
4514 lpfc_mem_free(phba);
4515 return rc;
4516}
4517
4518/**
4519 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
4520 * @phba: pointer to lpfc hba data structure.
4521 *
4522 * This routine is invoked to unset the driver internal resources set up
4523 * specific for supporting the SLI-4 HBA device it attached to.
4524 **/
4525static void
4526lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
4527{
4528 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
4529
James Smartda0436e2009-05-22 14:51:39 -04004530 /* Free memory allocated for msi-x interrupt vector entries */
4531 kfree(phba->sli4_hba.msix_entries);
4532
4533 /* Free memory allocated for fast-path work queue handles */
4534 kfree(phba->sli4_hba.fcp_eq_hdl);
4535
4536 /* Free the allocated rpi headers. */
4537 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04004538 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04004539
James Smart0c9ab6f2010-02-26 14:15:57 -05004540 /* Free eligible FCF index bmask */
4541 kfree(phba->fcf.fcf_rr_bmask);
4542
James Smartda0436e2009-05-22 14:51:39 -04004543 /* Free the ELS sgl list */
4544 lpfc_free_active_sgl(phba);
4545 lpfc_free_sgl_list(phba);
4546
4547 /* Free the SCSI sgl management array */
4548 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
4549
4550 /* Free the SLI4 queues */
4551 lpfc_sli4_queue_destroy(phba);
4552
4553 /* Free the completion queue EQ event pool */
4554 lpfc_sli4_cq_event_release_all(phba);
4555 lpfc_sli4_cq_event_pool_destroy(phba);
4556
James Smartda0436e2009-05-22 14:51:39 -04004557 /* Free the bsmbx region. */
4558 lpfc_destroy_bootstrap_mbox(phba);
4559
4560 /* Free the SLI Layer memory with SLI4 HBAs */
4561 lpfc_mem_free_all(phba);
4562
4563 /* Free the current connect table */
4564 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04004565 &phba->fcf_conn_rec_list, list) {
4566 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04004567 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04004568 }
James Smartda0436e2009-05-22 14:51:39 -04004569
4570 return;
4571}
4572
4573/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004574 * lpfc_init_api_table_setup - Set up init api function jump table
James Smart3772a992009-05-22 14:50:54 -04004575 * @phba: The hba struct for which this call is being executed.
4576 * @dev_grp: The HBA PCI-Device group number.
4577 *
4578 * This routine sets up the device INIT interface API function jump table
4579 * in @phba struct.
4580 *
4581 * Returns: 0 - success, -ENODEV - failure.
4582 **/
4583int
4584lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4585{
James Smart84d1b002010-02-12 14:42:33 -05004586 phba->lpfc_hba_init_link = lpfc_hba_init_link;
4587 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart7f860592011-03-11 16:05:52 -05004588 phba->lpfc_selective_reset = lpfc_selective_reset;
James Smart3772a992009-05-22 14:50:54 -04004589 switch (dev_grp) {
4590 case LPFC_PCI_DEV_LP:
4591 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
4592 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
4593 phba->lpfc_stop_port = lpfc_stop_port_s3;
4594 break;
James Smartda0436e2009-05-22 14:51:39 -04004595 case LPFC_PCI_DEV_OC:
4596 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
4597 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
4598 phba->lpfc_stop_port = lpfc_stop_port_s4;
4599 break;
James Smart3772a992009-05-22 14:50:54 -04004600 default:
4601 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4602 "1431 Invalid HBA PCI-device group: 0x%x\n",
4603 dev_grp);
4604 return -ENODEV;
4605 break;
4606 }
4607 return 0;
4608}
4609
4610/**
4611 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
4612 * @phba: pointer to lpfc hba data structure.
4613 *
4614 * This routine is invoked to set up the driver internal resources before the
4615 * device specific resource setup to support the HBA device it attached to.
4616 *
4617 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004618 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004619 * other values - error
4620 **/
4621static int
4622lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
4623{
4624 /*
4625 * Driver resources common to all SLI revisions
4626 */
James Smartea2151b2008-09-07 11:52:10 -04004627 atomic_set(&phba->fast_event_count, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004628 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004629
James Smarte47c9092008-02-08 18:49:26 -05004630 /* Initialize ndlp management spinlock */
4631 spin_lock_init(&phba->ndlp_lock);
4632
James Smart2e0fef82007-06-17 19:56:36 -05004633 INIT_LIST_HEAD(&phba->port_list);
James Smart3772a992009-05-22 14:50:54 -04004634 INIT_LIST_HEAD(&phba->work_list);
James Smart84774a42008-08-24 21:50:06 -04004635 init_waitqueue_head(&phba->wait_4_mlo_m_q);
dea31012005-04-17 16:05:31 -05004636
James Smart3772a992009-05-22 14:50:54 -04004637 /* Initialize the wait queue head for the kernel thread */
4638 init_waitqueue_head(&phba->work_waitq);
James Smart858c9f62007-06-17 19:56:39 -05004639
James Smart3772a992009-05-22 14:50:54 -04004640 /* Initialize the scsi buffer list used by driver for scsi IO */
4641 spin_lock_init(&phba->scsi_buf_list_lock);
4642 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
dea31012005-04-17 16:05:31 -05004643
James Smart3772a992009-05-22 14:50:54 -04004644 /* Initialize the fabric iocb list */
4645 INIT_LIST_HEAD(&phba->fabric_iocb_list);
dea31012005-04-17 16:05:31 -05004646
James Smart3772a992009-05-22 14:50:54 -04004647 /* Initialize list to save ELS buffers */
4648 INIT_LIST_HEAD(&phba->elsbuf);
dea31012005-04-17 16:05:31 -05004649
James Smart3772a992009-05-22 14:50:54 -04004650 /* Initialize FCF connection rec list */
4651 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
dea31012005-04-17 16:05:31 -05004652
James Smart3772a992009-05-22 14:50:54 -04004653 return 0;
4654}
dea31012005-04-17 16:05:31 -05004655
James Smart3772a992009-05-22 14:50:54 -04004656/**
4657 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
4658 * @phba: pointer to lpfc hba data structure.
4659 *
4660 * This routine is invoked to set up the driver internal resources after the
4661 * device specific resource setup to support the HBA device it attached to.
4662 *
4663 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004664 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004665 * other values - error
4666 **/
4667static int
4668lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
4669{
4670 int error;
4671
4672 /* Startup the kernel thread for this host adapter. */
4673 phba->worker_thread = kthread_run(lpfc_do_work, phba,
4674 "lpfc_worker_%d", phba->brd_no);
4675 if (IS_ERR(phba->worker_thread)) {
4676 error = PTR_ERR(phba->worker_thread);
4677 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05004678 }
4679
James Smart3772a992009-05-22 14:50:54 -04004680 return 0;
4681}
4682
4683/**
4684 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
4685 * @phba: pointer to lpfc hba data structure.
4686 *
4687 * This routine is invoked to unset the driver internal resources set up after
4688 * the device specific resource setup for supporting the HBA device it
4689 * attached to.
4690 **/
4691static void
4692lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
4693{
4694 /* Stop kernel worker thread */
4695 kthread_stop(phba->worker_thread);
4696}
4697
4698/**
4699 * lpfc_free_iocb_list - Free iocb list.
4700 * @phba: pointer to lpfc hba data structure.
4701 *
4702 * This routine is invoked to free the driver's IOCB list and memory.
4703 **/
4704static void
4705lpfc_free_iocb_list(struct lpfc_hba *phba)
4706{
4707 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
4708
4709 spin_lock_irq(&phba->hbalock);
4710 list_for_each_entry_safe(iocbq_entry, iocbq_next,
4711 &phba->lpfc_iocb_list, list) {
4712 list_del(&iocbq_entry->list);
4713 kfree(iocbq_entry);
4714 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05004715 }
James Smart3772a992009-05-22 14:50:54 -04004716 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004717
James Smart3772a992009-05-22 14:50:54 -04004718 return;
4719}
dea31012005-04-17 16:05:31 -05004720
James Smart3772a992009-05-22 14:50:54 -04004721/**
4722 * lpfc_init_iocb_list - Allocate and initialize iocb list.
4723 * @phba: pointer to lpfc hba data structure.
4724 *
4725 * This routine is invoked to allocate and initizlize the driver's IOCB
4726 * list and set up the IOCB tag array accordingly.
4727 *
4728 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004729 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004730 * other values - error
4731 **/
4732static int
4733lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
4734{
4735 struct lpfc_iocbq *iocbq_entry = NULL;
4736 uint16_t iotag;
4737 int i;
dea31012005-04-17 16:05:31 -05004738
4739 /* Initialize and populate the iocb list per host. */
4740 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04004741 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07004742 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05004743 if (iocbq_entry == NULL) {
4744 printk(KERN_ERR "%s: only allocated %d iocbs of "
4745 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07004746 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05004747 goto out_free_iocbq;
4748 }
4749
James Bottomley604a3e32005-10-29 10:28:33 -05004750 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
4751 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04004752 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05004753 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04004754 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05004755 goto out_free_iocbq;
4756 }
James Smart3772a992009-05-22 14:50:54 -04004757 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05004758
4759 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004760 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
4761 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05004762 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004763 }
4764
James Smart3772a992009-05-22 14:50:54 -04004765 return 0;
4766
4767out_free_iocbq:
4768 lpfc_free_iocb_list(phba);
4769
4770 return -ENOMEM;
4771}
4772
4773/**
James Smartda0436e2009-05-22 14:51:39 -04004774 * lpfc_free_sgl_list - Free sgl list.
4775 * @phba: pointer to lpfc hba data structure.
4776 *
4777 * This routine is invoked to free the driver's sgl list and memory.
4778 **/
4779static void
4780lpfc_free_sgl_list(struct lpfc_hba *phba)
4781{
4782 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
4783 LIST_HEAD(sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04004784
4785 spin_lock_irq(&phba->hbalock);
4786 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
4787 spin_unlock_irq(&phba->hbalock);
4788
4789 list_for_each_entry_safe(sglq_entry, sglq_next,
4790 &sglq_list, list) {
4791 list_del(&sglq_entry->list);
4792 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
4793 kfree(sglq_entry);
4794 phba->sli4_hba.total_sglq_bufs--;
4795 }
James Smartda0436e2009-05-22 14:51:39 -04004796 kfree(phba->sli4_hba.lpfc_els_sgl_array);
4797}
4798
4799/**
4800 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
4801 * @phba: pointer to lpfc hba data structure.
4802 *
4803 * This routine is invoked to allocate the driver's active sgl memory.
4804 * This array will hold the sglq_entry's for active IOs.
4805 **/
4806static int
4807lpfc_init_active_sgl_array(struct lpfc_hba *phba)
4808{
4809 int size;
4810 size = sizeof(struct lpfc_sglq *);
4811 size *= phba->sli4_hba.max_cfg_param.max_xri;
4812
4813 phba->sli4_hba.lpfc_sglq_active_list =
4814 kzalloc(size, GFP_KERNEL);
4815 if (!phba->sli4_hba.lpfc_sglq_active_list)
4816 return -ENOMEM;
4817 return 0;
4818}
4819
4820/**
4821 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
4822 * @phba: pointer to lpfc hba data structure.
4823 *
4824 * This routine is invoked to walk through the array of active sglq entries
4825 * and free all of the resources.
4826 * This is just a place holder for now.
4827 **/
4828static void
4829lpfc_free_active_sgl(struct lpfc_hba *phba)
4830{
4831 kfree(phba->sli4_hba.lpfc_sglq_active_list);
4832}
4833
4834/**
4835 * lpfc_init_sgl_list - Allocate and initialize sgl list.
4836 * @phba: pointer to lpfc hba data structure.
4837 *
4838 * This routine is invoked to allocate and initizlize the driver's sgl
4839 * list and set up the sgl xritag tag array accordingly.
4840 *
4841 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004842 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004843 * other values - error
4844 **/
4845static int
4846lpfc_init_sgl_list(struct lpfc_hba *phba)
4847{
4848 struct lpfc_sglq *sglq_entry = NULL;
4849 int i;
4850 int els_xri_cnt;
4851
4852 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
4853 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4854 "2400 lpfc_init_sgl_list els %d.\n",
4855 els_xri_cnt);
4856 /* Initialize and populate the sglq list per host/VF. */
4857 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
4858 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
4859
4860 /* Sanity check on XRI management */
4861 if (phba->sli4_hba.max_cfg_param.max_xri <= els_xri_cnt) {
4862 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4863 "2562 No room left for SCSI XRI allocation: "
4864 "max_xri=%d, els_xri=%d\n",
4865 phba->sli4_hba.max_cfg_param.max_xri,
4866 els_xri_cnt);
4867 return -ENOMEM;
4868 }
4869
4870 /* Allocate memory for the ELS XRI management array */
4871 phba->sli4_hba.lpfc_els_sgl_array =
4872 kzalloc((sizeof(struct lpfc_sglq *) * els_xri_cnt),
4873 GFP_KERNEL);
4874
4875 if (!phba->sli4_hba.lpfc_els_sgl_array) {
4876 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4877 "2401 Failed to allocate memory for ELS "
4878 "XRI management array of size %d.\n",
4879 els_xri_cnt);
4880 return -ENOMEM;
4881 }
4882
4883 /* Keep the SCSI XRI into the XRI management array */
4884 phba->sli4_hba.scsi_xri_max =
4885 phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
4886 phba->sli4_hba.scsi_xri_cnt = 0;
4887
4888 phba->sli4_hba.lpfc_scsi_psb_array =
4889 kzalloc((sizeof(struct lpfc_scsi_buf *) *
4890 phba->sli4_hba.scsi_xri_max), GFP_KERNEL);
4891
4892 if (!phba->sli4_hba.lpfc_scsi_psb_array) {
4893 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4894 "2563 Failed to allocate memory for SCSI "
4895 "XRI management array of size %d.\n",
4896 phba->sli4_hba.scsi_xri_max);
4897 kfree(phba->sli4_hba.lpfc_els_sgl_array);
4898 return -ENOMEM;
4899 }
4900
4901 for (i = 0; i < els_xri_cnt; i++) {
4902 sglq_entry = kzalloc(sizeof(struct lpfc_sglq), GFP_KERNEL);
4903 if (sglq_entry == NULL) {
4904 printk(KERN_ERR "%s: only allocated %d sgls of "
4905 "expected %d count. Unloading driver.\n",
4906 __func__, i, els_xri_cnt);
4907 goto out_free_mem;
4908 }
4909
4910 sglq_entry->sli4_xritag = lpfc_sli4_next_xritag(phba);
4911 if (sglq_entry->sli4_xritag == NO_XRI) {
4912 kfree(sglq_entry);
4913 printk(KERN_ERR "%s: failed to allocate XRI.\n"
4914 "Unloading driver.\n", __func__);
4915 goto out_free_mem;
4916 }
4917 sglq_entry->buff_type = GEN_BUFF_TYPE;
4918 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0, &sglq_entry->phys);
4919 if (sglq_entry->virt == NULL) {
4920 kfree(sglq_entry);
4921 printk(KERN_ERR "%s: failed to allocate mbuf.\n"
4922 "Unloading driver.\n", __func__);
4923 goto out_free_mem;
4924 }
4925 sglq_entry->sgl = sglq_entry->virt;
4926 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
4927
4928 /* The list order is used by later block SGL registraton */
4929 spin_lock_irq(&phba->hbalock);
James Smart0f65ff62010-02-26 14:14:23 -05004930 sglq_entry->state = SGL_FREED;
James Smartda0436e2009-05-22 14:51:39 -04004931 list_add_tail(&sglq_entry->list, &phba->sli4_hba.lpfc_sgl_list);
4932 phba->sli4_hba.lpfc_els_sgl_array[i] = sglq_entry;
4933 phba->sli4_hba.total_sglq_bufs++;
4934 spin_unlock_irq(&phba->hbalock);
4935 }
4936 return 0;
4937
4938out_free_mem:
4939 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
4940 lpfc_free_sgl_list(phba);
4941 return -ENOMEM;
4942}
4943
4944/**
4945 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
4946 * @phba: pointer to lpfc hba data structure.
4947 *
4948 * This routine is invoked to post rpi header templates to the
4949 * HBA consistent with the SLI-4 interface spec. This routine
4950 * posts a PAGE_SIZE memory region to the port to hold up to
4951 * PAGE_SIZE modulo 64 rpi context headers.
4952 * No locks are held here because this is an initialization routine
4953 * called only from probe or lpfc_online when interrupts are not
4954 * enabled and the driver is reinitializing the device.
4955 *
4956 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004957 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004958 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04004959 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04004960 **/
4961int
4962lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
4963{
4964 int rc = 0;
4965 int longs;
4966 uint16_t rpi_count;
4967 struct lpfc_rpi_hdr *rpi_hdr;
4968
4969 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
4970
4971 /*
4972 * Provision an rpi bitmask range for discovery. The total count
4973 * is the difference between max and base + 1.
4974 */
4975 rpi_count = phba->sli4_hba.max_cfg_param.rpi_base +
4976 phba->sli4_hba.max_cfg_param.max_rpi - 1;
4977
4978 longs = ((rpi_count) + BITS_PER_LONG - 1) / BITS_PER_LONG;
4979 phba->sli4_hba.rpi_bmask = kzalloc(longs * sizeof(unsigned long),
4980 GFP_KERNEL);
4981 if (!phba->sli4_hba.rpi_bmask)
4982 return -ENOMEM;
4983
4984 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
4985 if (!rpi_hdr) {
4986 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4987 "0391 Error during rpi post operation\n");
4988 lpfc_sli4_remove_rpis(phba);
4989 rc = -ENODEV;
4990 }
4991
4992 return rc;
4993}
4994
4995/**
4996 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
4997 * @phba: pointer to lpfc hba data structure.
4998 *
4999 * This routine is invoked to allocate a single 4KB memory region to
5000 * support rpis and stores them in the phba. This single region
5001 * provides support for up to 64 rpis. The region is used globally
5002 * by the device.
5003 *
5004 * Returns:
5005 * A valid rpi hdr on success.
5006 * A NULL pointer on any failure.
5007 **/
5008struct lpfc_rpi_hdr *
5009lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
5010{
5011 uint16_t rpi_limit, curr_rpi_range;
5012 struct lpfc_dmabuf *dmabuf;
5013 struct lpfc_rpi_hdr *rpi_hdr;
James Smart9589b0622011-04-16 11:03:17 -04005014 uint32_t rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04005015
5016 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
5017 phba->sli4_hba.max_cfg_param.max_rpi - 1;
5018
5019 spin_lock_irq(&phba->hbalock);
5020 curr_rpi_range = phba->sli4_hba.next_rpi;
5021 spin_unlock_irq(&phba->hbalock);
5022
5023 /*
5024 * The port has a limited number of rpis. The increment here
5025 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
5026 * and to allow the full max_rpi range per port.
5027 */
5028 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
James Smart9589b0622011-04-16 11:03:17 -04005029 rpi_count = rpi_limit - curr_rpi_range;
5030 else
5031 rpi_count = LPFC_RPI_HDR_COUNT;
James Smartda0436e2009-05-22 14:51:39 -04005032
5033 /*
5034 * First allocate the protocol header region for the port. The
5035 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
5036 */
5037 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5038 if (!dmabuf)
5039 return NULL;
5040
5041 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5042 LPFC_HDR_TEMPLATE_SIZE,
5043 &dmabuf->phys,
5044 GFP_KERNEL);
5045 if (!dmabuf->virt) {
5046 rpi_hdr = NULL;
5047 goto err_free_dmabuf;
5048 }
5049
5050 memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
5051 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
5052 rpi_hdr = NULL;
5053 goto err_free_coherent;
5054 }
5055
5056 /* Save the rpi header data for cleanup later. */
5057 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
5058 if (!rpi_hdr)
5059 goto err_free_coherent;
5060
5061 rpi_hdr->dmabuf = dmabuf;
5062 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
5063 rpi_hdr->page_count = 1;
5064 spin_lock_irq(&phba->hbalock);
5065 rpi_hdr->start_rpi = phba->sli4_hba.next_rpi;
5066 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
5067
5068 /*
5069 * The next_rpi stores the next module-64 rpi value to post
5070 * in any subsequent rpi memory region postings.
5071 */
James Smart9589b0622011-04-16 11:03:17 -04005072 phba->sli4_hba.next_rpi += rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04005073 spin_unlock_irq(&phba->hbalock);
5074 return rpi_hdr;
5075
5076 err_free_coherent:
5077 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
5078 dmabuf->virt, dmabuf->phys);
5079 err_free_dmabuf:
5080 kfree(dmabuf);
5081 return NULL;
5082}
5083
5084/**
5085 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
5086 * @phba: pointer to lpfc hba data structure.
5087 *
5088 * This routine is invoked to remove all memory resources allocated
5089 * to support rpis. This routine presumes the caller has released all
5090 * rpis consumed by fabric or port logins and is prepared to have
5091 * the header pages removed.
5092 **/
5093void
5094lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
5095{
5096 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
5097
5098 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
5099 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
5100 list_del(&rpi_hdr->list);
5101 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
5102 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
5103 kfree(rpi_hdr->dmabuf);
5104 kfree(rpi_hdr);
5105 }
James Smartda0436e2009-05-22 14:51:39 -04005106 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
James Smartda0436e2009-05-22 14:51:39 -04005107}
5108
5109/**
James Smart3772a992009-05-22 14:50:54 -04005110 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
5111 * @pdev: pointer to pci device data structure.
5112 *
5113 * This routine is invoked to allocate the driver hba data structure for an
5114 * HBA device. If the allocation is successful, the phba reference to the
5115 * PCI device data structure is set.
5116 *
5117 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005118 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04005119 * NULL - error
5120 **/
5121static struct lpfc_hba *
5122lpfc_hba_alloc(struct pci_dev *pdev)
5123{
5124 struct lpfc_hba *phba;
5125
5126 /* Allocate memory for HBA structure */
5127 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
5128 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02005129 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04005130 return NULL;
5131 }
5132
5133 /* Set reference to PCI device in HBA structure */
5134 phba->pcidev = pdev;
5135
5136 /* Assign an unused board number */
5137 phba->brd_no = lpfc_get_instance();
5138 if (phba->brd_no < 0) {
5139 kfree(phba);
5140 return NULL;
5141 }
5142
James Smart4fede782010-01-26 23:08:55 -05005143 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04005144 INIT_LIST_HEAD(&phba->ct_ev_waiters);
5145
James Smart3772a992009-05-22 14:50:54 -04005146 return phba;
5147}
5148
5149/**
5150 * lpfc_hba_free - Free driver hba data structure with a device.
5151 * @phba: pointer to lpfc hba data structure.
5152 *
5153 * This routine is invoked to free the driver hba data structure with an
5154 * HBA device.
5155 **/
5156static void
5157lpfc_hba_free(struct lpfc_hba *phba)
5158{
5159 /* Release the driver assigned board number */
5160 idr_remove(&lpfc_hba_index, phba->brd_no);
5161
5162 kfree(phba);
5163 return;
5164}
5165
5166/**
5167 * lpfc_create_shost - Create hba physical port with associated scsi host.
5168 * @phba: pointer to lpfc hba data structure.
5169 *
5170 * This routine is invoked to create HBA physical port and associate a SCSI
5171 * host with it.
5172 *
5173 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005174 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005175 * other values - error
5176 **/
5177static int
5178lpfc_create_shost(struct lpfc_hba *phba)
5179{
5180 struct lpfc_vport *vport;
5181 struct Scsi_Host *shost;
5182
5183 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05005184 phba->fc_edtov = FF_DEF_EDTOV;
5185 phba->fc_ratov = FF_DEF_RATOV;
5186 phba->fc_altov = FF_DEF_ALTOV;
5187 phba->fc_arbtov = FF_DEF_ARBTOV;
5188
James Smartd7c47992010-06-08 18:31:54 -04005189 atomic_set(&phba->sdev_cnt, 0);
James Smart3de2a652007-08-02 11:09:59 -04005190 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05005191 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04005192 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05005193
5194 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05005195 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05005196 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04005197 /* Put reference to SCSI host to driver's device private data */
5198 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05005199
James Smart3772a992009-05-22 14:50:54 -04005200 return 0;
5201}
dea31012005-04-17 16:05:31 -05005202
James Smart3772a992009-05-22 14:50:54 -04005203/**
5204 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
5205 * @phba: pointer to lpfc hba data structure.
5206 *
5207 * This routine is invoked to destroy HBA physical port and the associated
5208 * SCSI host.
5209 **/
5210static void
5211lpfc_destroy_shost(struct lpfc_hba *phba)
5212{
5213 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04005214
James Smart3772a992009-05-22 14:50:54 -04005215 /* Destroy physical port that associated with the SCSI host */
5216 destroy_port(vport);
5217
5218 return;
5219}
5220
5221/**
5222 * lpfc_setup_bg - Setup Block guard structures and debug areas.
5223 * @phba: pointer to lpfc hba data structure.
5224 * @shost: the shost to be used to detect Block guard settings.
5225 *
5226 * This routine sets up the local Block guard protocol settings for @shost.
5227 * This routine also allocates memory for debugging bg buffers.
5228 **/
5229static void
5230lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
5231{
5232 int pagecnt = 10;
5233 if (lpfc_prot_mask && lpfc_prot_guard) {
5234 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5235 "1478 Registering BlockGuard with the "
5236 "SCSI layer\n");
5237 scsi_host_set_prot(shost, lpfc_prot_mask);
5238 scsi_host_set_guard(shost, lpfc_prot_guard);
James Smart98c9ea52007-10-27 13:37:33 -04005239 }
James Smart81301a92008-12-04 22:39:46 -05005240 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05005241 while (pagecnt) {
5242 spin_lock_init(&_dump_buf_lock);
5243 _dump_buf_data =
5244 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
5245 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04005246 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5247 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04005248 "_dump_buf_data at 0x%p\n",
5249 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05005250 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04005251 memset(_dump_buf_data, 0,
5252 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05005253 break;
James Smart3772a992009-05-22 14:50:54 -04005254 } else
James Smart81301a92008-12-04 22:39:46 -05005255 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05005256 }
James Smart81301a92008-12-04 22:39:46 -05005257 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04005258 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5259 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04005260 "memory for hexdump\n");
5261 } else
James Smart6a9c52c2009-10-02 15:16:51 -04005262 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5263 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05005264 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05005265 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05005266 while (pagecnt) {
5267 _dump_buf_dif =
5268 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
5269 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04005270 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5271 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04005272 "_dump_buf_dif at 0x%p\n",
5273 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05005274 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04005275 memset(_dump_buf_dif, 0,
5276 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05005277 break;
James Smart3772a992009-05-22 14:50:54 -04005278 } else
James Smart81301a92008-12-04 22:39:46 -05005279 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05005280 }
James Smart81301a92008-12-04 22:39:46 -05005281 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04005282 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5283 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04005284 "memory for hexdump\n");
5285 } else
James Smart6a9c52c2009-10-02 15:16:51 -04005286 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5287 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04005288 _dump_buf_dif);
5289}
5290
5291/**
5292 * lpfc_post_init_setup - Perform necessary device post initialization setup.
5293 * @phba: pointer to lpfc hba data structure.
5294 *
5295 * This routine is invoked to perform all the necessary post initialization
5296 * setup for the device.
5297 **/
5298static void
5299lpfc_post_init_setup(struct lpfc_hba *phba)
5300{
5301 struct Scsi_Host *shost;
5302 struct lpfc_adapter_event_header adapter_event;
5303
5304 /* Get the default values for Model Name and Description */
5305 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
5306
5307 /*
5308 * hba setup may have changed the hba_queue_depth so we need to
5309 * adjust the value of can_queue.
5310 */
5311 shost = pci_get_drvdata(phba->pcidev);
5312 shost->can_queue = phba->cfg_hba_queue_depth - 10;
5313 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
5314 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05005315
5316 lpfc_host_attrib_init(shost);
5317
James Smart2e0fef82007-06-17 19:56:36 -05005318 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
5319 spin_lock_irq(shost->host_lock);
5320 lpfc_poll_start_timer(phba);
5321 spin_unlock_irq(shost->host_lock);
5322 }
James Smart8f6d98d2006-08-01 07:34:00 -04005323
James Smart93996272008-08-24 21:50:30 -04005324 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5325 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04005326 /* Send board arrival event to upper layer */
5327 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
5328 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
5329 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04005330 sizeof(adapter_event),
5331 (char *) &adapter_event,
5332 LPFC_NL_VENDOR_ID);
5333 return;
5334}
5335
5336/**
5337 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
5338 * @phba: pointer to lpfc hba data structure.
5339 *
5340 * This routine is invoked to set up the PCI device memory space for device
5341 * with SLI-3 interface spec.
5342 *
5343 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005344 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005345 * other values - error
5346 **/
5347static int
5348lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
5349{
5350 struct pci_dev *pdev;
5351 unsigned long bar0map_len, bar2map_len;
5352 int i, hbq_count;
5353 void *ptr;
5354 int error = -ENODEV;
5355
5356 /* Obtain PCI device reference */
5357 if (!phba->pcidev)
5358 return error;
5359 else
5360 pdev = phba->pcidev;
5361
5362 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05005363 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
5364 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
5365 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
5366 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smart3772a992009-05-22 14:50:54 -04005367 return error;
Michael Reed8e685972009-09-18 12:02:05 -05005368 }
5369 }
James Smart3772a992009-05-22 14:50:54 -04005370
5371 /* Get the bus address of Bar0 and Bar2 and the number of bytes
5372 * required by each mapping.
5373 */
5374 phba->pci_bar0_map = pci_resource_start(pdev, 0);
5375 bar0map_len = pci_resource_len(pdev, 0);
5376
5377 phba->pci_bar2_map = pci_resource_start(pdev, 2);
5378 bar2map_len = pci_resource_len(pdev, 2);
5379
5380 /* Map HBA SLIM to a kernel virtual address. */
5381 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
5382 if (!phba->slim_memmap_p) {
5383 dev_printk(KERN_ERR, &pdev->dev,
5384 "ioremap failed for SLIM memory.\n");
5385 goto out;
5386 }
5387
5388 /* Map HBA Control Registers to a kernel virtual address. */
5389 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
5390 if (!phba->ctrl_regs_memmap_p) {
5391 dev_printk(KERN_ERR, &pdev->dev,
5392 "ioremap failed for HBA control registers.\n");
5393 goto out_iounmap_slim;
5394 }
5395
5396 /* Allocate memory for SLI-2 structures */
5397 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
5398 SLI2_SLIM_SIZE,
5399 &phba->slim2p.phys,
5400 GFP_KERNEL);
5401 if (!phba->slim2p.virt)
5402 goto out_iounmap;
5403
5404 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
5405 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04005406 phba->mbox_ext = (phba->slim2p.virt +
5407 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04005408 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
5409 phba->IOCBs = (phba->slim2p.virt +
5410 offsetof(struct lpfc_sli2_slim, IOCBs));
5411
5412 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
5413 lpfc_sli_hbq_size(),
5414 &phba->hbqslimp.phys,
5415 GFP_KERNEL);
5416 if (!phba->hbqslimp.virt)
5417 goto out_free_slim;
5418
5419 hbq_count = lpfc_sli_hbq_count();
5420 ptr = phba->hbqslimp.virt;
5421 for (i = 0; i < hbq_count; ++i) {
5422 phba->hbqs[i].hbq_virt = ptr;
5423 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
5424 ptr += (lpfc_hbq_defs[i]->entry_count *
5425 sizeof(struct lpfc_hbq_entry));
5426 }
5427 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
5428 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
5429
5430 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
5431
5432 INIT_LIST_HEAD(&phba->rb_pend_list);
5433
5434 phba->MBslimaddr = phba->slim_memmap_p;
5435 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
5436 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
5437 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
5438 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04005439
dea31012005-04-17 16:05:31 -05005440 return 0;
5441
dea31012005-04-17 16:05:31 -05005442out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04005443 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
5444 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05005445out_iounmap:
5446 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005447out_iounmap_slim:
dea31012005-04-17 16:05:31 -05005448 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05005449out:
5450 return error;
5451}
5452
James Smarte59058c2008-08-24 21:49:00 -04005453/**
James Smart3772a992009-05-22 14:50:54 -04005454 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
5455 * @phba: pointer to lpfc hba data structure.
5456 *
5457 * This routine is invoked to unset the PCI device memory space for device
5458 * with SLI-3 interface spec.
5459 **/
5460static void
5461lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
5462{
5463 struct pci_dev *pdev;
5464
5465 /* Obtain PCI device reference */
5466 if (!phba->pcidev)
5467 return;
5468 else
5469 pdev = phba->pcidev;
5470
5471 /* Free coherent DMA memory allocated */
5472 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
5473 phba->hbqslimp.virt, phba->hbqslimp.phys);
5474 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
5475 phba->slim2p.virt, phba->slim2p.phys);
5476
5477 /* I/O memory unmap */
5478 iounmap(phba->ctrl_regs_memmap_p);
5479 iounmap(phba->slim_memmap_p);
5480
5481 return;
5482}
5483
5484/**
James Smartda0436e2009-05-22 14:51:39 -04005485 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
5486 * @phba: pointer to lpfc hba data structure.
5487 *
5488 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
5489 * done and check status.
5490 *
5491 * Return 0 if successful, otherwise -ENODEV.
5492 **/
5493int
5494lpfc_sli4_post_status_check(struct lpfc_hba *phba)
5495{
James Smart2fcee4b2010-12-15 17:57:46 -05005496 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
5497 struct lpfc_register reg_data;
5498 int i, port_error = 0;
5499 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04005500
James Smart9940b972011-03-11 16:06:12 -05005501 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
5502 memset(&reg_data, 0, sizeof(reg_data));
James Smart2fcee4b2010-12-15 17:57:46 -05005503 if (!phba->sli4_hba.PSMPHRregaddr)
James Smartda0436e2009-05-22 14:51:39 -04005504 return -ENODEV;
5505
James Smartda0436e2009-05-22 14:51:39 -04005506 /* Wait up to 30 seconds for the SLI Port POST done and ready */
5507 for (i = 0; i < 3000; i++) {
James Smart9940b972011-03-11 16:06:12 -05005508 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
5509 &portsmphr_reg.word0) ||
5510 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
James Smart2fcee4b2010-12-15 17:57:46 -05005511 /* Port has a fatal POST error, break out */
James Smartda0436e2009-05-22 14:51:39 -04005512 port_error = -ENODEV;
5513 break;
5514 }
James Smart2fcee4b2010-12-15 17:57:46 -05005515 if (LPFC_POST_STAGE_PORT_READY ==
5516 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
James Smartda0436e2009-05-22 14:51:39 -04005517 break;
James Smartda0436e2009-05-22 14:51:39 -04005518 msleep(10);
5519 }
5520
James Smart2fcee4b2010-12-15 17:57:46 -05005521 /*
5522 * If there was a port error during POST, then don't proceed with
5523 * other register reads as the data may not be valid. Just exit.
5524 */
5525 if (port_error) {
James Smartda0436e2009-05-22 14:51:39 -04005526 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05005527 "1408 Port Failed POST - portsmphr=0x%x, "
5528 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
5529 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
5530 portsmphr_reg.word0,
5531 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
5532 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
5533 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
5534 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
5535 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
5536 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
5537 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
5538 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
5539 } else {
James Smart28baac72010-02-12 14:42:03 -05005540 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05005541 "2534 Device Info: SLIFamily=0x%x, "
5542 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
5543 "SLIHint_2=0x%x, FT=0x%x\n",
James Smart28baac72010-02-12 14:42:03 -05005544 bf_get(lpfc_sli_intf_sli_family,
5545 &phba->sli4_hba.sli_intf),
5546 bf_get(lpfc_sli_intf_slirev,
5547 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05005548 bf_get(lpfc_sli_intf_if_type,
James Smart28baac72010-02-12 14:42:03 -05005549 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05005550 bf_get(lpfc_sli_intf_sli_hint1,
5551 &phba->sli4_hba.sli_intf),
5552 bf_get(lpfc_sli_intf_sli_hint2,
5553 &phba->sli4_hba.sli_intf),
5554 bf_get(lpfc_sli_intf_func_type,
James Smart28baac72010-02-12 14:42:03 -05005555 &phba->sli4_hba.sli_intf));
James Smart2fcee4b2010-12-15 17:57:46 -05005556 /*
5557 * Check for other Port errors during the initialization
5558 * process. Fail the load if the port did not come up
5559 * correctly.
5560 */
5561 if_type = bf_get(lpfc_sli_intf_if_type,
5562 &phba->sli4_hba.sli_intf);
5563 switch (if_type) {
5564 case LPFC_SLI_INTF_IF_TYPE_0:
5565 phba->sli4_hba.ue_mask_lo =
5566 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
5567 phba->sli4_hba.ue_mask_hi =
5568 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
5569 uerrlo_reg.word0 =
5570 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
5571 uerrhi_reg.word0 =
5572 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
5573 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
5574 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
5575 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5576 "1422 Unrecoverable Error "
5577 "Detected during POST "
5578 "uerr_lo_reg=0x%x, "
5579 "uerr_hi_reg=0x%x, "
5580 "ue_mask_lo_reg=0x%x, "
5581 "ue_mask_hi_reg=0x%x\n",
5582 uerrlo_reg.word0,
5583 uerrhi_reg.word0,
5584 phba->sli4_hba.ue_mask_lo,
5585 phba->sli4_hba.ue_mask_hi);
5586 port_error = -ENODEV;
5587 }
5588 break;
5589 case LPFC_SLI_INTF_IF_TYPE_2:
5590 /* Final checks. The port status should be clean. */
James Smart9940b972011-03-11 16:06:12 -05005591 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
5592 &reg_data.word0) ||
James Smart05580562011-05-24 11:40:48 -04005593 (bf_get(lpfc_sliport_status_err, &reg_data) &&
5594 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
James Smart2fcee4b2010-12-15 17:57:46 -05005595 phba->work_status[0] =
5596 readl(phba->sli4_hba.u.if_type2.
5597 ERR1regaddr);
5598 phba->work_status[1] =
5599 readl(phba->sli4_hba.u.if_type2.
5600 ERR2regaddr);
5601 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5602 "2888 Port Error Detected "
5603 "during POST: "
5604 "port status reg 0x%x, "
5605 "port_smphr reg 0x%x, "
5606 "error 1=0x%x, error 2=0x%x\n",
5607 reg_data.word0,
5608 portsmphr_reg.word0,
5609 phba->work_status[0],
5610 phba->work_status[1]);
5611 port_error = -ENODEV;
5612 }
5613 break;
5614 case LPFC_SLI_INTF_IF_TYPE_1:
5615 default:
5616 break;
5617 }
James Smart28baac72010-02-12 14:42:03 -05005618 }
James Smartda0436e2009-05-22 14:51:39 -04005619 return port_error;
5620}
5621
5622/**
5623 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
5624 * @phba: pointer to lpfc hba data structure.
James Smart2fcee4b2010-12-15 17:57:46 -05005625 * @if_type: The SLI4 interface type getting configured.
James Smartda0436e2009-05-22 14:51:39 -04005626 *
5627 * This routine is invoked to set up SLI4 BAR0 PCI config space register
5628 * memory map.
5629 **/
5630static void
James Smart2fcee4b2010-12-15 17:57:46 -05005631lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04005632{
James Smart2fcee4b2010-12-15 17:57:46 -05005633 switch (if_type) {
5634 case LPFC_SLI_INTF_IF_TYPE_0:
5635 phba->sli4_hba.u.if_type0.UERRLOregaddr =
5636 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
5637 phba->sli4_hba.u.if_type0.UERRHIregaddr =
5638 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
5639 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
5640 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
5641 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
5642 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
5643 phba->sli4_hba.SLIINTFregaddr =
5644 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
5645 break;
5646 case LPFC_SLI_INTF_IF_TYPE_2:
5647 phba->sli4_hba.u.if_type2.ERR1regaddr =
5648 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLIPORT_ERR_1;
5649 phba->sli4_hba.u.if_type2.ERR2regaddr =
5650 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLIPORT_ERR_2;
5651 phba->sli4_hba.u.if_type2.CTRLregaddr =
5652 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLIPORT_CNTRL;
5653 phba->sli4_hba.u.if_type2.STATUSregaddr =
5654 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLIPORT_STATUS;
5655 phba->sli4_hba.SLIINTFregaddr =
5656 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
5657 phba->sli4_hba.PSMPHRregaddr =
5658 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLIPORT_IF2_SMPHR;
5659 phba->sli4_hba.RQDBregaddr =
5660 phba->sli4_hba.conf_regs_memmap_p + LPFC_RQ_DOORBELL;
5661 phba->sli4_hba.WQDBregaddr =
5662 phba->sli4_hba.conf_regs_memmap_p + LPFC_WQ_DOORBELL;
5663 phba->sli4_hba.EQCQDBregaddr =
5664 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
5665 phba->sli4_hba.MQDBregaddr =
5666 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
5667 phba->sli4_hba.BMBXregaddr =
5668 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
5669 break;
5670 case LPFC_SLI_INTF_IF_TYPE_1:
5671 default:
5672 dev_printk(KERN_ERR, &phba->pcidev->dev,
5673 "FATAL - unsupported SLI4 interface type - %d\n",
5674 if_type);
5675 break;
5676 }
James Smartda0436e2009-05-22 14:51:39 -04005677}
5678
5679/**
5680 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
5681 * @phba: pointer to lpfc hba data structure.
5682 *
5683 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
5684 * memory map.
5685 **/
5686static void
5687lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
5688{
James Smart2fcee4b2010-12-15 17:57:46 -05005689 phba->sli4_hba.PSMPHRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5690 LPFC_SLIPORT_IF0_SMPHR;
James Smartda0436e2009-05-22 14:51:39 -04005691 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05005692 LPFC_HST_ISR0;
James Smartda0436e2009-05-22 14:51:39 -04005693 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05005694 LPFC_HST_IMR0;
James Smartda0436e2009-05-22 14:51:39 -04005695 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05005696 LPFC_HST_ISCR0;
James Smartda0436e2009-05-22 14:51:39 -04005697}
5698
5699/**
5700 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
5701 * @phba: pointer to lpfc hba data structure.
5702 * @vf: virtual function number
5703 *
5704 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
5705 * based on the given viftual function number, @vf.
5706 *
5707 * Return 0 if successful, otherwise -ENODEV.
5708 **/
5709static int
5710lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
5711{
5712 if (vf > LPFC_VIR_FUNC_MAX)
5713 return -ENODEV;
5714
5715 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5716 vf * LPFC_VFR_PAGE_SIZE + LPFC_RQ_DOORBELL);
5717 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5718 vf * LPFC_VFR_PAGE_SIZE + LPFC_WQ_DOORBELL);
5719 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5720 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
5721 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5722 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
5723 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5724 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
5725 return 0;
5726}
5727
5728/**
5729 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
5730 * @phba: pointer to lpfc hba data structure.
5731 *
5732 * This routine is invoked to create the bootstrap mailbox
5733 * region consistent with the SLI-4 interface spec. This
5734 * routine allocates all memory necessary to communicate
5735 * mailbox commands to the port and sets up all alignment
5736 * needs. No locks are expected to be held when calling
5737 * this routine.
5738 *
5739 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005740 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005741 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04005742 **/
5743static int
5744lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
5745{
5746 uint32_t bmbx_size;
5747 struct lpfc_dmabuf *dmabuf;
5748 struct dma_address *dma_address;
5749 uint32_t pa_addr;
5750 uint64_t phys_addr;
5751
5752 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5753 if (!dmabuf)
5754 return -ENOMEM;
5755
5756 /*
5757 * The bootstrap mailbox region is comprised of 2 parts
5758 * plus an alignment restriction of 16 bytes.
5759 */
5760 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
5761 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5762 bmbx_size,
5763 &dmabuf->phys,
5764 GFP_KERNEL);
5765 if (!dmabuf->virt) {
5766 kfree(dmabuf);
5767 return -ENOMEM;
5768 }
5769 memset(dmabuf->virt, 0, bmbx_size);
5770
5771 /*
5772 * Initialize the bootstrap mailbox pointers now so that the register
5773 * operations are simple later. The mailbox dma address is required
5774 * to be 16-byte aligned. Also align the virtual memory as each
5775 * maibox is copied into the bmbx mailbox region before issuing the
5776 * command to the port.
5777 */
5778 phba->sli4_hba.bmbx.dmabuf = dmabuf;
5779 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
5780
5781 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
5782 LPFC_ALIGN_16_BYTE);
5783 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
5784 LPFC_ALIGN_16_BYTE);
5785
5786 /*
5787 * Set the high and low physical addresses now. The SLI4 alignment
5788 * requirement is 16 bytes and the mailbox is posted to the port
5789 * as two 30-bit addresses. The other data is a bit marking whether
5790 * the 30-bit address is the high or low address.
5791 * Upcast bmbx aphys to 64bits so shift instruction compiles
5792 * clean on 32 bit machines.
5793 */
5794 dma_address = &phba->sli4_hba.bmbx.dma_address;
5795 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
5796 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
5797 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
5798 LPFC_BMBX_BIT1_ADDR_HI);
5799
5800 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
5801 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
5802 LPFC_BMBX_BIT1_ADDR_LO);
5803 return 0;
5804}
5805
5806/**
5807 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
5808 * @phba: pointer to lpfc hba data structure.
5809 *
5810 * This routine is invoked to teardown the bootstrap mailbox
5811 * region and release all host resources. This routine requires
5812 * the caller to ensure all mailbox commands recovered, no
5813 * additional mailbox comands are sent, and interrupts are disabled
5814 * before calling this routine.
5815 *
5816 **/
5817static void
5818lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
5819{
5820 dma_free_coherent(&phba->pcidev->dev,
5821 phba->sli4_hba.bmbx.bmbx_size,
5822 phba->sli4_hba.bmbx.dmabuf->virt,
5823 phba->sli4_hba.bmbx.dmabuf->phys);
5824
5825 kfree(phba->sli4_hba.bmbx.dmabuf);
5826 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
5827}
5828
5829/**
5830 * lpfc_sli4_read_config - Get the config parameters.
5831 * @phba: pointer to lpfc hba data structure.
5832 *
5833 * This routine is invoked to read the configuration parameters from the HBA.
5834 * The configuration parameters are used to set the base and maximum values
5835 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
5836 * allocation for the port.
5837 *
5838 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005839 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005840 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04005841 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04005842 **/
5843static int
5844lpfc_sli4_read_config(struct lpfc_hba *phba)
5845{
5846 LPFC_MBOXQ_t *pmb;
5847 struct lpfc_mbx_read_config *rd_config;
James Smart912e3ac2011-05-24 11:42:11 -04005848 union lpfc_sli4_cfg_shdr *shdr;
5849 uint32_t shdr_status, shdr_add_status;
5850 struct lpfc_mbx_get_func_cfg *get_func_cfg;
5851 struct lpfc_rsrc_desc_fcfcoe *desc;
5852 uint32_t desc_count;
5853 int length, i, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04005854
5855 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5856 if (!pmb) {
5857 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5858 "2011 Unable to allocate memory for issuing "
5859 "SLI_CONFIG_SPECIAL mailbox command\n");
5860 return -ENOMEM;
5861 }
5862
5863 lpfc_read_config(phba, pmb);
5864
5865 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
5866 if (rc != MBX_SUCCESS) {
5867 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5868 "2012 Mailbox failed , mbxCmd x%x "
5869 "READ_CONFIG, mbxStatus x%x\n",
5870 bf_get(lpfc_mqe_command, &pmb->u.mqe),
5871 bf_get(lpfc_mqe_status, &pmb->u.mqe));
5872 rc = -EIO;
5873 } else {
5874 rd_config = &pmb->u.mqe.un.rd_config;
5875 phba->sli4_hba.max_cfg_param.max_xri =
5876 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
5877 phba->sli4_hba.max_cfg_param.xri_base =
5878 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
5879 phba->sli4_hba.max_cfg_param.max_vpi =
5880 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
5881 phba->sli4_hba.max_cfg_param.vpi_base =
5882 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
5883 phba->sli4_hba.max_cfg_param.max_rpi =
5884 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
5885 phba->sli4_hba.max_cfg_param.rpi_base =
5886 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
5887 phba->sli4_hba.max_cfg_param.max_vfi =
5888 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
5889 phba->sli4_hba.max_cfg_param.vfi_base =
5890 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
5891 phba->sli4_hba.max_cfg_param.max_fcfi =
5892 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
5893 phba->sli4_hba.max_cfg_param.fcfi_base =
5894 bf_get(lpfc_mbx_rd_conf_fcfi_base, rd_config);
5895 phba->sli4_hba.max_cfg_param.max_eq =
5896 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
5897 phba->sli4_hba.max_cfg_param.max_rq =
5898 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
5899 phba->sli4_hba.max_cfg_param.max_wq =
5900 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
5901 phba->sli4_hba.max_cfg_param.max_cq =
5902 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
5903 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
5904 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
5905 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
5906 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
5907 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
James Smart5ffc2662009-11-18 15:39:44 -05005908 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
5909 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04005910 phba->max_vports = phba->max_vpi;
5911 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5912 "2003 cfg params XRI(B:%d M:%d), "
5913 "VPI(B:%d M:%d) "
5914 "VFI(B:%d M:%d) "
5915 "RPI(B:%d M:%d) "
5916 "FCFI(B:%d M:%d)\n",
5917 phba->sli4_hba.max_cfg_param.xri_base,
5918 phba->sli4_hba.max_cfg_param.max_xri,
5919 phba->sli4_hba.max_cfg_param.vpi_base,
5920 phba->sli4_hba.max_cfg_param.max_vpi,
5921 phba->sli4_hba.max_cfg_param.vfi_base,
5922 phba->sli4_hba.max_cfg_param.max_vfi,
5923 phba->sli4_hba.max_cfg_param.rpi_base,
5924 phba->sli4_hba.max_cfg_param.max_rpi,
5925 phba->sli4_hba.max_cfg_param.fcfi_base,
5926 phba->sli4_hba.max_cfg_param.max_fcfi);
5927 }
James Smart912e3ac2011-05-24 11:42:11 -04005928
5929 if (rc)
5930 goto read_cfg_out;
James Smartda0436e2009-05-22 14:51:39 -04005931
5932 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smartb92938b2010-06-07 15:24:12 -04005933 if (phba->cfg_hba_queue_depth >
5934 (phba->sli4_hba.max_cfg_param.max_xri -
5935 lpfc_sli4_get_els_iocb_cnt(phba)))
James Smartda0436e2009-05-22 14:51:39 -04005936 phba->cfg_hba_queue_depth =
James Smartb92938b2010-06-07 15:24:12 -04005937 phba->sli4_hba.max_cfg_param.max_xri -
5938 lpfc_sli4_get_els_iocb_cnt(phba);
James Smart912e3ac2011-05-24 11:42:11 -04005939
5940 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
5941 LPFC_SLI_INTF_IF_TYPE_2)
5942 goto read_cfg_out;
5943
5944 /* get the pf# and vf# for SLI4 if_type 2 port */
5945 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
5946 sizeof(struct lpfc_sli4_cfg_mhdr));
5947 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
5948 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
5949 length, LPFC_SLI4_MBX_EMBED);
5950
5951 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
5952 shdr = (union lpfc_sli4_cfg_shdr *)
5953 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
5954 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5955 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5956 if (rc || shdr_status || shdr_add_status) {
5957 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5958 "3026 Mailbox failed , mbxCmd x%x "
5959 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
5960 bf_get(lpfc_mqe_command, &pmb->u.mqe),
5961 bf_get(lpfc_mqe_status, &pmb->u.mqe));
5962 rc = -EIO;
5963 goto read_cfg_out;
5964 }
5965
5966 /* search for fc_fcoe resrouce descriptor */
5967 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
5968 desc_count = get_func_cfg->func_cfg.rsrc_desc_count;
5969
5970 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
5971 desc = (struct lpfc_rsrc_desc_fcfcoe *)
5972 &get_func_cfg->func_cfg.desc[i];
5973 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
5974 bf_get(lpfc_rsrc_desc_pcie_type, desc)) {
5975 phba->sli4_hba.iov.pf_number =
5976 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
5977 phba->sli4_hba.iov.vf_number =
5978 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
5979 break;
5980 }
5981 }
5982
5983 if (i < LPFC_RSRC_DESC_MAX_NUM)
5984 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5985 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
5986 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
5987 phba->sli4_hba.iov.vf_number);
5988 else {
5989 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5990 "3028 GET_FUNCTION_CONFIG: failed to find "
5991 "Resrouce Descriptor:x%x\n",
5992 LPFC_RSRC_DESC_TYPE_FCFCOE);
5993 rc = -EIO;
5994 }
5995
5996read_cfg_out:
5997 mempool_free(pmb, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04005998 return rc;
5999}
6000
6001/**
James Smart2fcee4b2010-12-15 17:57:46 -05006002 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
James Smartda0436e2009-05-22 14:51:39 -04006003 * @phba: pointer to lpfc hba data structure.
6004 *
James Smart2fcee4b2010-12-15 17:57:46 -05006005 * This routine is invoked to setup the port-side endian order when
6006 * the port if_type is 0. This routine has no function for other
6007 * if_types.
James Smartda0436e2009-05-22 14:51:39 -04006008 *
6009 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006010 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006011 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006012 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006013 **/
6014static int
6015lpfc_setup_endian_order(struct lpfc_hba *phba)
6016{
6017 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05006018 uint32_t if_type, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04006019 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
6020 HOST_ENDIAN_HIGH_WORD1};
6021
James Smart2fcee4b2010-12-15 17:57:46 -05006022 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
6023 switch (if_type) {
6024 case LPFC_SLI_INTF_IF_TYPE_0:
6025 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6026 GFP_KERNEL);
6027 if (!mboxq) {
6028 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6029 "0492 Unable to allocate memory for "
6030 "issuing SLI_CONFIG_SPECIAL mailbox "
6031 "command\n");
6032 return -ENOMEM;
6033 }
James Smartda0436e2009-05-22 14:51:39 -04006034
James Smart2fcee4b2010-12-15 17:57:46 -05006035 /*
6036 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
6037 * two words to contain special data values and no other data.
6038 */
6039 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
6040 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
6041 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6042 if (rc != MBX_SUCCESS) {
6043 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6044 "0493 SLI_CONFIG_SPECIAL mailbox "
6045 "failed with status x%x\n",
6046 rc);
6047 rc = -EIO;
6048 }
6049 mempool_free(mboxq, phba->mbox_mem_pool);
6050 break;
6051 case LPFC_SLI_INTF_IF_TYPE_2:
6052 case LPFC_SLI_INTF_IF_TYPE_1:
6053 default:
6054 break;
James Smartda0436e2009-05-22 14:51:39 -04006055 }
James Smartda0436e2009-05-22 14:51:39 -04006056 return rc;
6057}
6058
6059/**
6060 * lpfc_sli4_queue_create - Create all the SLI4 queues
6061 * @phba: pointer to lpfc hba data structure.
6062 *
6063 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
6064 * operation. For each SLI4 queue type, the parameters such as queue entry
6065 * count (queue depth) shall be taken from the module parameter. For now,
6066 * we just use some constant number as place holder.
6067 *
6068 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006069 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006070 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006071 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006072 **/
6073static int
6074lpfc_sli4_queue_create(struct lpfc_hba *phba)
6075{
6076 struct lpfc_queue *qdesc;
6077 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
6078 int cfg_fcp_wq_count;
6079 int cfg_fcp_eq_count;
6080
6081 /*
6082 * Sanity check for confiugred queue parameters against the run-time
6083 * device parameters
6084 */
6085
6086 /* Sanity check on FCP fast-path WQ parameters */
6087 cfg_fcp_wq_count = phba->cfg_fcp_wq_count;
6088 if (cfg_fcp_wq_count >
6089 (phba->sli4_hba.max_cfg_param.max_wq - LPFC_SP_WQN_DEF)) {
6090 cfg_fcp_wq_count = phba->sli4_hba.max_cfg_param.max_wq -
6091 LPFC_SP_WQN_DEF;
6092 if (cfg_fcp_wq_count < LPFC_FP_WQN_MIN) {
6093 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6094 "2581 Not enough WQs (%d) from "
6095 "the pci function for supporting "
6096 "FCP WQs (%d)\n",
6097 phba->sli4_hba.max_cfg_param.max_wq,
6098 phba->cfg_fcp_wq_count);
6099 goto out_error;
6100 }
6101 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6102 "2582 Not enough WQs (%d) from the pci "
6103 "function for supporting the requested "
6104 "FCP WQs (%d), the actual FCP WQs can "
6105 "be supported: %d\n",
6106 phba->sli4_hba.max_cfg_param.max_wq,
6107 phba->cfg_fcp_wq_count, cfg_fcp_wq_count);
6108 }
6109 /* The actual number of FCP work queues adopted */
6110 phba->cfg_fcp_wq_count = cfg_fcp_wq_count;
6111
6112 /* Sanity check on FCP fast-path EQ parameters */
6113 cfg_fcp_eq_count = phba->cfg_fcp_eq_count;
6114 if (cfg_fcp_eq_count >
6115 (phba->sli4_hba.max_cfg_param.max_eq - LPFC_SP_EQN_DEF)) {
6116 cfg_fcp_eq_count = phba->sli4_hba.max_cfg_param.max_eq -
6117 LPFC_SP_EQN_DEF;
6118 if (cfg_fcp_eq_count < LPFC_FP_EQN_MIN) {
6119 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6120 "2574 Not enough EQs (%d) from the "
6121 "pci function for supporting FCP "
6122 "EQs (%d)\n",
6123 phba->sli4_hba.max_cfg_param.max_eq,
6124 phba->cfg_fcp_eq_count);
6125 goto out_error;
6126 }
6127 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6128 "2575 Not enough EQs (%d) from the pci "
6129 "function for supporting the requested "
6130 "FCP EQs (%d), the actual FCP EQs can "
6131 "be supported: %d\n",
6132 phba->sli4_hba.max_cfg_param.max_eq,
6133 phba->cfg_fcp_eq_count, cfg_fcp_eq_count);
6134 }
6135 /* It does not make sense to have more EQs than WQs */
6136 if (cfg_fcp_eq_count > phba->cfg_fcp_wq_count) {
6137 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smart6a9c52c2009-10-02 15:16:51 -04006138 "2593 The FCP EQ count(%d) cannot be greater "
6139 "than the FCP WQ count(%d), limiting the "
6140 "FCP EQ count to %d\n", cfg_fcp_eq_count,
James Smartda0436e2009-05-22 14:51:39 -04006141 phba->cfg_fcp_wq_count,
6142 phba->cfg_fcp_wq_count);
6143 cfg_fcp_eq_count = phba->cfg_fcp_wq_count;
6144 }
6145 /* The actual number of FCP event queues adopted */
6146 phba->cfg_fcp_eq_count = cfg_fcp_eq_count;
6147 /* The overall number of event queues used */
6148 phba->sli4_hba.cfg_eqn = phba->cfg_fcp_eq_count + LPFC_SP_EQN_DEF;
6149
6150 /*
6151 * Create Event Queues (EQs)
6152 */
6153
6154 /* Get EQ depth from module parameter, fake the default for now */
6155 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
6156 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
6157
6158 /* Create slow path event queue */
6159 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
6160 phba->sli4_hba.eq_ecount);
6161 if (!qdesc) {
6162 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6163 "0496 Failed allocate slow-path EQ\n");
6164 goto out_error;
6165 }
6166 phba->sli4_hba.sp_eq = qdesc;
6167
6168 /* Create fast-path FCP Event Queue(s) */
6169 phba->sli4_hba.fp_eq = kzalloc((sizeof(struct lpfc_queue *) *
6170 phba->cfg_fcp_eq_count), GFP_KERNEL);
6171 if (!phba->sli4_hba.fp_eq) {
6172 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6173 "2576 Failed allocate memory for fast-path "
6174 "EQ record array\n");
6175 goto out_free_sp_eq;
6176 }
6177 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
6178 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
6179 phba->sli4_hba.eq_ecount);
6180 if (!qdesc) {
6181 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6182 "0497 Failed allocate fast-path EQ\n");
6183 goto out_free_fp_eq;
6184 }
6185 phba->sli4_hba.fp_eq[fcp_eqidx] = qdesc;
6186 }
6187
6188 /*
6189 * Create Complete Queues (CQs)
6190 */
6191
6192 /* Get CQ depth from module parameter, fake the default for now */
6193 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
6194 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
6195
6196 /* Create slow-path Mailbox Command Complete Queue */
6197 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6198 phba->sli4_hba.cq_ecount);
6199 if (!qdesc) {
6200 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6201 "0500 Failed allocate slow-path mailbox CQ\n");
6202 goto out_free_fp_eq;
6203 }
6204 phba->sli4_hba.mbx_cq = qdesc;
6205
6206 /* Create slow-path ELS Complete Queue */
6207 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6208 phba->sli4_hba.cq_ecount);
6209 if (!qdesc) {
6210 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6211 "0501 Failed allocate slow-path ELS CQ\n");
6212 goto out_free_mbx_cq;
6213 }
6214 phba->sli4_hba.els_cq = qdesc;
6215
James Smartda0436e2009-05-22 14:51:39 -04006216
6217 /* Create fast-path FCP Completion Queue(s), one-to-one with EQs */
6218 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
6219 phba->cfg_fcp_eq_count), GFP_KERNEL);
6220 if (!phba->sli4_hba.fcp_cq) {
6221 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6222 "2577 Failed allocate memory for fast-path "
6223 "CQ record array\n");
James Smart4d9ab992009-10-02 15:16:39 -04006224 goto out_free_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04006225 }
6226 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
6227 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6228 phba->sli4_hba.cq_ecount);
6229 if (!qdesc) {
6230 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6231 "0499 Failed allocate fast-path FCP "
6232 "CQ (%d)\n", fcp_cqidx);
6233 goto out_free_fcp_cq;
6234 }
6235 phba->sli4_hba.fcp_cq[fcp_cqidx] = qdesc;
6236 }
6237
6238 /* Create Mailbox Command Queue */
6239 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
6240 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
6241
6242 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
6243 phba->sli4_hba.mq_ecount);
6244 if (!qdesc) {
6245 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6246 "0505 Failed allocate slow-path MQ\n");
6247 goto out_free_fcp_cq;
6248 }
6249 phba->sli4_hba.mbx_wq = qdesc;
6250
6251 /*
6252 * Create all the Work Queues (WQs)
6253 */
6254 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
6255 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
6256
6257 /* Create slow-path ELS Work Queue */
6258 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
6259 phba->sli4_hba.wq_ecount);
6260 if (!qdesc) {
6261 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6262 "0504 Failed allocate slow-path ELS WQ\n");
6263 goto out_free_mbx_wq;
6264 }
6265 phba->sli4_hba.els_wq = qdesc;
6266
6267 /* Create fast-path FCP Work Queue(s) */
6268 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
6269 phba->cfg_fcp_wq_count), GFP_KERNEL);
6270 if (!phba->sli4_hba.fcp_wq) {
6271 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6272 "2578 Failed allocate memory for fast-path "
6273 "WQ record array\n");
6274 goto out_free_els_wq;
6275 }
6276 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
6277 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
6278 phba->sli4_hba.wq_ecount);
6279 if (!qdesc) {
6280 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6281 "0503 Failed allocate fast-path FCP "
6282 "WQ (%d)\n", fcp_wqidx);
6283 goto out_free_fcp_wq;
6284 }
6285 phba->sli4_hba.fcp_wq[fcp_wqidx] = qdesc;
6286 }
6287
6288 /*
6289 * Create Receive Queue (RQ)
6290 */
6291 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
6292 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
6293
6294 /* Create Receive Queue for header */
6295 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
6296 phba->sli4_hba.rq_ecount);
6297 if (!qdesc) {
6298 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6299 "0506 Failed allocate receive HRQ\n");
6300 goto out_free_fcp_wq;
6301 }
6302 phba->sli4_hba.hdr_rq = qdesc;
6303
6304 /* Create Receive Queue for data */
6305 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
6306 phba->sli4_hba.rq_ecount);
6307 if (!qdesc) {
6308 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6309 "0507 Failed allocate receive DRQ\n");
6310 goto out_free_hdr_rq;
6311 }
6312 phba->sli4_hba.dat_rq = qdesc;
6313
6314 return 0;
6315
6316out_free_hdr_rq:
6317 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
6318 phba->sli4_hba.hdr_rq = NULL;
6319out_free_fcp_wq:
6320 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--) {
6321 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_wqidx]);
6322 phba->sli4_hba.fcp_wq[fcp_wqidx] = NULL;
6323 }
6324 kfree(phba->sli4_hba.fcp_wq);
6325out_free_els_wq:
6326 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
6327 phba->sli4_hba.els_wq = NULL;
6328out_free_mbx_wq:
6329 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
6330 phba->sli4_hba.mbx_wq = NULL;
6331out_free_fcp_cq:
6332 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--) {
6333 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_cqidx]);
6334 phba->sli4_hba.fcp_cq[fcp_cqidx] = NULL;
6335 }
6336 kfree(phba->sli4_hba.fcp_cq);
James Smartda0436e2009-05-22 14:51:39 -04006337out_free_els_cq:
6338 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
6339 phba->sli4_hba.els_cq = NULL;
6340out_free_mbx_cq:
6341 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
6342 phba->sli4_hba.mbx_cq = NULL;
6343out_free_fp_eq:
6344 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--) {
6345 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_eqidx]);
6346 phba->sli4_hba.fp_eq[fcp_eqidx] = NULL;
6347 }
6348 kfree(phba->sli4_hba.fp_eq);
6349out_free_sp_eq:
6350 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
6351 phba->sli4_hba.sp_eq = NULL;
6352out_error:
6353 return -ENOMEM;
6354}
6355
6356/**
6357 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
6358 * @phba: pointer to lpfc hba data structure.
6359 *
6360 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
6361 * operation.
6362 *
6363 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006364 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006365 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006366 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006367 **/
6368static void
6369lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
6370{
6371 int fcp_qidx;
6372
6373 /* Release mailbox command work queue */
6374 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
6375 phba->sli4_hba.mbx_wq = NULL;
6376
6377 /* Release ELS work queue */
6378 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
6379 phba->sli4_hba.els_wq = NULL;
6380
6381 /* Release FCP work queue */
6382 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
6383 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_qidx]);
6384 kfree(phba->sli4_hba.fcp_wq);
6385 phba->sli4_hba.fcp_wq = NULL;
6386
6387 /* Release unsolicited receive queue */
6388 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
6389 phba->sli4_hba.hdr_rq = NULL;
6390 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
6391 phba->sli4_hba.dat_rq = NULL;
6392
James Smartda0436e2009-05-22 14:51:39 -04006393 /* Release ELS complete queue */
6394 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
6395 phba->sli4_hba.els_cq = NULL;
6396
6397 /* Release mailbox command complete queue */
6398 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
6399 phba->sli4_hba.mbx_cq = NULL;
6400
6401 /* Release FCP response complete queue */
James Smart05580562011-05-24 11:40:48 -04006402 fcp_qidx = 0;
6403 do
James Smartda0436e2009-05-22 14:51:39 -04006404 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_qidx]);
James Smart05580562011-05-24 11:40:48 -04006405 while (++fcp_qidx < phba->cfg_fcp_eq_count);
James Smartda0436e2009-05-22 14:51:39 -04006406 kfree(phba->sli4_hba.fcp_cq);
6407 phba->sli4_hba.fcp_cq = NULL;
6408
6409 /* Release fast-path event queue */
6410 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
6411 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_qidx]);
6412 kfree(phba->sli4_hba.fp_eq);
6413 phba->sli4_hba.fp_eq = NULL;
6414
6415 /* Release slow-path event queue */
6416 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
6417 phba->sli4_hba.sp_eq = NULL;
6418
6419 return;
6420}
6421
6422/**
6423 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
6424 * @phba: pointer to lpfc hba data structure.
6425 *
6426 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
6427 * operation.
6428 *
6429 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006430 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006431 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006432 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006433 **/
6434int
6435lpfc_sli4_queue_setup(struct lpfc_hba *phba)
6436{
6437 int rc = -ENOMEM;
6438 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
6439 int fcp_cq_index = 0;
6440
6441 /*
6442 * Set up Event Queues (EQs)
6443 */
6444
6445 /* Set up slow-path event queue */
6446 if (!phba->sli4_hba.sp_eq) {
6447 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6448 "0520 Slow-path EQ not allocated\n");
6449 goto out_error;
6450 }
6451 rc = lpfc_eq_create(phba, phba->sli4_hba.sp_eq,
6452 LPFC_SP_DEF_IMAX);
6453 if (rc) {
6454 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6455 "0521 Failed setup of slow-path EQ: "
6456 "rc = 0x%x\n", rc);
6457 goto out_error;
6458 }
6459 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6460 "2583 Slow-path EQ setup: queue-id=%d\n",
6461 phba->sli4_hba.sp_eq->queue_id);
6462
6463 /* Set up fast-path event queue */
6464 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
6465 if (!phba->sli4_hba.fp_eq[fcp_eqidx]) {
6466 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6467 "0522 Fast-path EQ (%d) not "
6468 "allocated\n", fcp_eqidx);
6469 goto out_destroy_fp_eq;
6470 }
6471 rc = lpfc_eq_create(phba, phba->sli4_hba.fp_eq[fcp_eqidx],
6472 phba->cfg_fcp_imax);
6473 if (rc) {
6474 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6475 "0523 Failed setup of fast-path EQ "
6476 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
6477 goto out_destroy_fp_eq;
6478 }
6479 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6480 "2584 Fast-path EQ setup: "
6481 "queue[%d]-id=%d\n", fcp_eqidx,
6482 phba->sli4_hba.fp_eq[fcp_eqidx]->queue_id);
6483 }
6484
6485 /*
6486 * Set up Complete Queues (CQs)
6487 */
6488
6489 /* Set up slow-path MBOX Complete Queue as the first CQ */
6490 if (!phba->sli4_hba.mbx_cq) {
6491 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6492 "0528 Mailbox CQ not allocated\n");
6493 goto out_destroy_fp_eq;
6494 }
6495 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq, phba->sli4_hba.sp_eq,
6496 LPFC_MCQ, LPFC_MBOX);
6497 if (rc) {
6498 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6499 "0529 Failed setup of slow-path mailbox CQ: "
6500 "rc = 0x%x\n", rc);
6501 goto out_destroy_fp_eq;
6502 }
6503 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6504 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
6505 phba->sli4_hba.mbx_cq->queue_id,
6506 phba->sli4_hba.sp_eq->queue_id);
6507
6508 /* Set up slow-path ELS Complete Queue */
6509 if (!phba->sli4_hba.els_cq) {
6510 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6511 "0530 ELS CQ not allocated\n");
6512 goto out_destroy_mbx_cq;
6513 }
6514 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq, phba->sli4_hba.sp_eq,
6515 LPFC_WCQ, LPFC_ELS);
6516 if (rc) {
6517 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6518 "0531 Failed setup of slow-path ELS CQ: "
6519 "rc = 0x%x\n", rc);
6520 goto out_destroy_mbx_cq;
6521 }
6522 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6523 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
6524 phba->sli4_hba.els_cq->queue_id,
6525 phba->sli4_hba.sp_eq->queue_id);
6526
James Smartda0436e2009-05-22 14:51:39 -04006527 /* Set up fast-path FCP Response Complete Queue */
James Smart05580562011-05-24 11:40:48 -04006528 fcp_cqidx = 0;
6529 do {
James Smartda0436e2009-05-22 14:51:39 -04006530 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
6531 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6532 "0526 Fast-path FCP CQ (%d) not "
6533 "allocated\n", fcp_cqidx);
6534 goto out_destroy_fcp_cq;
6535 }
James Smart05580562011-05-24 11:40:48 -04006536 if (phba->cfg_fcp_eq_count)
6537 rc = lpfc_cq_create(phba,
6538 phba->sli4_hba.fcp_cq[fcp_cqidx],
6539 phba->sli4_hba.fp_eq[fcp_cqidx],
6540 LPFC_WCQ, LPFC_FCP);
6541 else
6542 rc = lpfc_cq_create(phba,
6543 phba->sli4_hba.fcp_cq[fcp_cqidx],
6544 phba->sli4_hba.sp_eq,
6545 LPFC_WCQ, LPFC_FCP);
James Smartda0436e2009-05-22 14:51:39 -04006546 if (rc) {
6547 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6548 "0527 Failed setup of fast-path FCP "
6549 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
6550 goto out_destroy_fcp_cq;
6551 }
6552 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6553 "2588 FCP CQ setup: cq[%d]-id=%d, "
James Smart05580562011-05-24 11:40:48 -04006554 "parent %seq[%d]-id=%d\n",
James Smartda0436e2009-05-22 14:51:39 -04006555 fcp_cqidx,
6556 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
James Smart05580562011-05-24 11:40:48 -04006557 (phba->cfg_fcp_eq_count) ? "" : "sp_",
James Smartda0436e2009-05-22 14:51:39 -04006558 fcp_cqidx,
James Smart05580562011-05-24 11:40:48 -04006559 (phba->cfg_fcp_eq_count) ?
6560 phba->sli4_hba.fp_eq[fcp_cqidx]->queue_id :
6561 phba->sli4_hba.sp_eq->queue_id);
6562 } while (++fcp_cqidx < phba->cfg_fcp_eq_count);
James Smartda0436e2009-05-22 14:51:39 -04006563
6564 /*
6565 * Set up all the Work Queues (WQs)
6566 */
6567
6568 /* Set up Mailbox Command Queue */
6569 if (!phba->sli4_hba.mbx_wq) {
6570 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6571 "0538 Slow-path MQ not allocated\n");
6572 goto out_destroy_fcp_cq;
6573 }
6574 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
6575 phba->sli4_hba.mbx_cq, LPFC_MBOX);
6576 if (rc) {
6577 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6578 "0539 Failed setup of slow-path MQ: "
6579 "rc = 0x%x\n", rc);
6580 goto out_destroy_fcp_cq;
6581 }
6582 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6583 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
6584 phba->sli4_hba.mbx_wq->queue_id,
6585 phba->sli4_hba.mbx_cq->queue_id);
6586
6587 /* Set up slow-path ELS Work Queue */
6588 if (!phba->sli4_hba.els_wq) {
6589 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6590 "0536 Slow-path ELS WQ not allocated\n");
6591 goto out_destroy_mbx_wq;
6592 }
6593 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
6594 phba->sli4_hba.els_cq, LPFC_ELS);
6595 if (rc) {
6596 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6597 "0537 Failed setup of slow-path ELS WQ: "
6598 "rc = 0x%x\n", rc);
6599 goto out_destroy_mbx_wq;
6600 }
6601 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6602 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
6603 phba->sli4_hba.els_wq->queue_id,
6604 phba->sli4_hba.els_cq->queue_id);
6605
6606 /* Set up fast-path FCP Work Queue */
6607 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
6608 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
6609 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6610 "0534 Fast-path FCP WQ (%d) not "
6611 "allocated\n", fcp_wqidx);
6612 goto out_destroy_fcp_wq;
6613 }
6614 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
6615 phba->sli4_hba.fcp_cq[fcp_cq_index],
6616 LPFC_FCP);
6617 if (rc) {
6618 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6619 "0535 Failed setup of fast-path FCP "
6620 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
6621 goto out_destroy_fcp_wq;
6622 }
6623 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6624 "2591 FCP WQ setup: wq[%d]-id=%d, "
6625 "parent cq[%d]-id=%d\n",
6626 fcp_wqidx,
6627 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
6628 fcp_cq_index,
6629 phba->sli4_hba.fcp_cq[fcp_cq_index]->queue_id);
6630 /* Round robin FCP Work Queue's Completion Queue assignment */
James Smart05580562011-05-24 11:40:48 -04006631 if (phba->cfg_fcp_eq_count)
6632 fcp_cq_index = ((fcp_cq_index + 1) %
6633 phba->cfg_fcp_eq_count);
James Smartda0436e2009-05-22 14:51:39 -04006634 }
6635
6636 /*
6637 * Create Receive Queue (RQ)
6638 */
6639 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
6640 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6641 "0540 Receive Queue not allocated\n");
6642 goto out_destroy_fcp_wq;
6643 }
6644 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04006645 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04006646 if (rc) {
6647 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6648 "0541 Failed setup of Receive Queue: "
6649 "rc = 0x%x\n", rc);
6650 goto out_destroy_fcp_wq;
6651 }
6652 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6653 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
6654 "parent cq-id=%d\n",
6655 phba->sli4_hba.hdr_rq->queue_id,
6656 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04006657 phba->sli4_hba.els_cq->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04006658 return 0;
6659
6660out_destroy_fcp_wq:
6661 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
6662 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
6663 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
6664out_destroy_mbx_wq:
6665 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
6666out_destroy_fcp_cq:
6667 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
6668 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
James Smartda0436e2009-05-22 14:51:39 -04006669 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
6670out_destroy_mbx_cq:
6671 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
6672out_destroy_fp_eq:
6673 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
6674 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_eqidx]);
6675 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
6676out_error:
6677 return rc;
6678}
6679
6680/**
6681 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
6682 * @phba: pointer to lpfc hba data structure.
6683 *
6684 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
6685 * operation.
6686 *
6687 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006688 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006689 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006690 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006691 **/
6692void
6693lpfc_sli4_queue_unset(struct lpfc_hba *phba)
6694{
6695 int fcp_qidx;
6696
6697 /* Unset mailbox command work queue */
6698 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
6699 /* Unset ELS work queue */
6700 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
6701 /* Unset unsolicited receive queue */
6702 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
6703 /* Unset FCP work queue */
6704 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
6705 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
6706 /* Unset mailbox command complete queue */
6707 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
6708 /* Unset ELS complete queue */
6709 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
James Smartda0436e2009-05-22 14:51:39 -04006710 /* Unset FCP response complete queue */
6711 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
6712 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
6713 /* Unset fast-path event queue */
6714 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
6715 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_qidx]);
6716 /* Unset slow-path event queue */
6717 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
6718}
6719
6720/**
6721 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
6722 * @phba: pointer to lpfc hba data structure.
6723 *
6724 * This routine is invoked to allocate and set up a pool of completion queue
6725 * events. The body of the completion queue event is a completion queue entry
6726 * CQE. For now, this pool is used for the interrupt service routine to queue
6727 * the following HBA completion queue events for the worker thread to process:
6728 * - Mailbox asynchronous events
6729 * - Receive queue completion unsolicited events
6730 * Later, this can be used for all the slow-path events.
6731 *
6732 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006733 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006734 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04006735 **/
6736static int
6737lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
6738{
6739 struct lpfc_cq_event *cq_event;
6740 int i;
6741
6742 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
6743 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
6744 if (!cq_event)
6745 goto out_pool_create_fail;
6746 list_add_tail(&cq_event->list,
6747 &phba->sli4_hba.sp_cqe_event_pool);
6748 }
6749 return 0;
6750
6751out_pool_create_fail:
6752 lpfc_sli4_cq_event_pool_destroy(phba);
6753 return -ENOMEM;
6754}
6755
6756/**
6757 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
6758 * @phba: pointer to lpfc hba data structure.
6759 *
6760 * This routine is invoked to free the pool of completion queue events at
6761 * driver unload time. Note that, it is the responsibility of the driver
6762 * cleanup routine to free all the outstanding completion-queue events
6763 * allocated from this pool back into the pool before invoking this routine
6764 * to destroy the pool.
6765 **/
6766static void
6767lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
6768{
6769 struct lpfc_cq_event *cq_event, *next_cq_event;
6770
6771 list_for_each_entry_safe(cq_event, next_cq_event,
6772 &phba->sli4_hba.sp_cqe_event_pool, list) {
6773 list_del(&cq_event->list);
6774 kfree(cq_event);
6775 }
6776}
6777
6778/**
6779 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
6780 * @phba: pointer to lpfc hba data structure.
6781 *
6782 * This routine is the lock free version of the API invoked to allocate a
6783 * completion-queue event from the free pool.
6784 *
6785 * Return: Pointer to the newly allocated completion-queue event if successful
6786 * NULL otherwise.
6787 **/
6788struct lpfc_cq_event *
6789__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
6790{
6791 struct lpfc_cq_event *cq_event = NULL;
6792
6793 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
6794 struct lpfc_cq_event, list);
6795 return cq_event;
6796}
6797
6798/**
6799 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
6800 * @phba: pointer to lpfc hba data structure.
6801 *
6802 * This routine is the lock version of the API invoked to allocate a
6803 * completion-queue event from the free pool.
6804 *
6805 * Return: Pointer to the newly allocated completion-queue event if successful
6806 * NULL otherwise.
6807 **/
6808struct lpfc_cq_event *
6809lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
6810{
6811 struct lpfc_cq_event *cq_event;
6812 unsigned long iflags;
6813
6814 spin_lock_irqsave(&phba->hbalock, iflags);
6815 cq_event = __lpfc_sli4_cq_event_alloc(phba);
6816 spin_unlock_irqrestore(&phba->hbalock, iflags);
6817 return cq_event;
6818}
6819
6820/**
6821 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
6822 * @phba: pointer to lpfc hba data structure.
6823 * @cq_event: pointer to the completion queue event to be freed.
6824 *
6825 * This routine is the lock free version of the API invoked to release a
6826 * completion-queue event back into the free pool.
6827 **/
6828void
6829__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
6830 struct lpfc_cq_event *cq_event)
6831{
6832 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
6833}
6834
6835/**
6836 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
6837 * @phba: pointer to lpfc hba data structure.
6838 * @cq_event: pointer to the completion queue event to be freed.
6839 *
6840 * This routine is the lock version of the API invoked to release a
6841 * completion-queue event back into the free pool.
6842 **/
6843void
6844lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
6845 struct lpfc_cq_event *cq_event)
6846{
6847 unsigned long iflags;
6848 spin_lock_irqsave(&phba->hbalock, iflags);
6849 __lpfc_sli4_cq_event_release(phba, cq_event);
6850 spin_unlock_irqrestore(&phba->hbalock, iflags);
6851}
6852
6853/**
6854 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
6855 * @phba: pointer to lpfc hba data structure.
6856 *
6857 * This routine is to free all the pending completion-queue events to the
6858 * back into the free pool for device reset.
6859 **/
6860static void
6861lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
6862{
6863 LIST_HEAD(cqelist);
6864 struct lpfc_cq_event *cqe;
6865 unsigned long iflags;
6866
6867 /* Retrieve all the pending WCQEs from pending WCQE lists */
6868 spin_lock_irqsave(&phba->hbalock, iflags);
6869 /* Pending FCP XRI abort events */
6870 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
6871 &cqelist);
6872 /* Pending ELS XRI abort events */
6873 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
6874 &cqelist);
6875 /* Pending asynnc events */
6876 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
6877 &cqelist);
6878 spin_unlock_irqrestore(&phba->hbalock, iflags);
6879
6880 while (!list_empty(&cqelist)) {
6881 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
6882 lpfc_sli4_cq_event_release(phba, cqe);
6883 }
6884}
6885
6886/**
6887 * lpfc_pci_function_reset - Reset pci function.
6888 * @phba: pointer to lpfc hba data structure.
6889 *
6890 * This routine is invoked to request a PCI function reset. It will destroys
6891 * all resources assigned to the PCI function which originates this request.
6892 *
6893 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006894 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006895 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006896 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006897 **/
6898int
6899lpfc_pci_function_reset(struct lpfc_hba *phba)
6900{
6901 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05006902 uint32_t rc = 0, if_type;
James Smartda0436e2009-05-22 14:51:39 -04006903 uint32_t shdr_status, shdr_add_status;
James Smart2fcee4b2010-12-15 17:57:46 -05006904 uint32_t rdy_chk, num_resets = 0, reset_again = 0;
James Smartda0436e2009-05-22 14:51:39 -04006905 union lpfc_sli4_cfg_shdr *shdr;
James Smart2fcee4b2010-12-15 17:57:46 -05006906 struct lpfc_register reg_data;
James Smartda0436e2009-05-22 14:51:39 -04006907
James Smart2fcee4b2010-12-15 17:57:46 -05006908 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
6909 switch (if_type) {
6910 case LPFC_SLI_INTF_IF_TYPE_0:
6911 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6912 GFP_KERNEL);
6913 if (!mboxq) {
6914 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6915 "0494 Unable to allocate memory for "
6916 "issuing SLI_FUNCTION_RESET mailbox "
6917 "command\n");
6918 return -ENOMEM;
6919 }
6920
6921 /* Setup PCI function reset mailbox-ioctl command */
6922 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
6923 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
6924 LPFC_SLI4_MBX_EMBED);
6925 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6926 shdr = (union lpfc_sli4_cfg_shdr *)
6927 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
6928 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6929 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
6930 &shdr->response);
6931 if (rc != MBX_TIMEOUT)
6932 mempool_free(mboxq, phba->mbox_mem_pool);
6933 if (shdr_status || shdr_add_status || rc) {
6934 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6935 "0495 SLI_FUNCTION_RESET mailbox "
6936 "failed with status x%x add_status x%x,"
6937 " mbx status x%x\n",
6938 shdr_status, shdr_add_status, rc);
6939 rc = -ENXIO;
6940 }
6941 break;
6942 case LPFC_SLI_INTF_IF_TYPE_2:
6943 for (num_resets = 0;
6944 num_resets < MAX_IF_TYPE_2_RESETS;
6945 num_resets++) {
6946 reg_data.word0 = 0;
6947 bf_set(lpfc_sliport_ctrl_end, &reg_data,
6948 LPFC_SLIPORT_LITTLE_ENDIAN);
6949 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
6950 LPFC_SLIPORT_INIT_PORT);
6951 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
6952 CTRLregaddr);
6953
6954 /*
6955 * Poll the Port Status Register and wait for RDY for
6956 * up to 10 seconds. If the port doesn't respond, treat
6957 * it as an error. If the port responds with RN, start
6958 * the loop again.
6959 */
6960 for (rdy_chk = 0; rdy_chk < 1000; rdy_chk++) {
James Smart9940b972011-03-11 16:06:12 -05006961 if (lpfc_readl(phba->sli4_hba.u.if_type2.
6962 STATUSregaddr, &reg_data.word0)) {
6963 rc = -ENODEV;
6964 break;
6965 }
James Smart2fcee4b2010-12-15 17:57:46 -05006966 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
6967 break;
6968 if (bf_get(lpfc_sliport_status_rn, &reg_data)) {
6969 reset_again++;
6970 break;
6971 }
6972 msleep(10);
6973 }
6974
6975 /*
6976 * If the port responds to the init request with
6977 * reset needed, delay for a bit and restart the loop.
6978 */
6979 if (reset_again) {
6980 msleep(10);
6981 reset_again = 0;
6982 continue;
6983 }
6984
6985 /* Detect any port errors. */
James Smart9940b972011-03-11 16:06:12 -05006986 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
6987 &reg_data.word0)) {
6988 rc = -ENODEV;
6989 break;
6990 }
James Smart2fcee4b2010-12-15 17:57:46 -05006991 if ((bf_get(lpfc_sliport_status_err, &reg_data)) ||
6992 (rdy_chk >= 1000)) {
6993 phba->work_status[0] = readl(
6994 phba->sli4_hba.u.if_type2.ERR1regaddr);
6995 phba->work_status[1] = readl(
6996 phba->sli4_hba.u.if_type2.ERR2regaddr);
6997 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6998 "2890 Port Error Detected "
6999 "during Port Reset: "
7000 "port status reg 0x%x, "
7001 "error 1=0x%x, error 2=0x%x\n",
7002 reg_data.word0,
7003 phba->work_status[0],
7004 phba->work_status[1]);
7005 rc = -ENODEV;
7006 }
7007
7008 /*
7009 * Terminate the outer loop provided the Port indicated
7010 * ready within 10 seconds.
7011 */
7012 if (rdy_chk < 1000)
7013 break;
7014 }
James Smart05580562011-05-24 11:40:48 -04007015 /* delay driver action following IF_TYPE_2 function reset */
7016 msleep(100);
James Smart2fcee4b2010-12-15 17:57:46 -05007017 break;
7018 case LPFC_SLI_INTF_IF_TYPE_1:
7019 default:
7020 break;
James Smartda0436e2009-05-22 14:51:39 -04007021 }
7022
James Smart2fcee4b2010-12-15 17:57:46 -05007023 /* Catch the not-ready port failure after a port reset. */
7024 if (num_resets >= MAX_IF_TYPE_2_RESETS)
7025 rc = -ENODEV;
7026
James Smartda0436e2009-05-22 14:51:39 -04007027 return rc;
7028}
7029
7030/**
7031 * lpfc_sli4_send_nop_mbox_cmds - Send sli-4 nop mailbox commands
7032 * @phba: pointer to lpfc hba data structure.
7033 * @cnt: number of nop mailbox commands to send.
7034 *
7035 * This routine is invoked to send a number @cnt of NOP mailbox command and
7036 * wait for each command to complete.
7037 *
7038 * Return: the number of NOP mailbox command completed.
7039 **/
7040static int
7041lpfc_sli4_send_nop_mbox_cmds(struct lpfc_hba *phba, uint32_t cnt)
7042{
7043 LPFC_MBOXQ_t *mboxq;
7044 int length, cmdsent;
7045 uint32_t mbox_tmo;
7046 uint32_t rc = 0;
7047 uint32_t shdr_status, shdr_add_status;
7048 union lpfc_sli4_cfg_shdr *shdr;
7049
7050 if (cnt == 0) {
7051 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7052 "2518 Requested to send 0 NOP mailbox cmd\n");
7053 return cnt;
7054 }
7055
7056 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7057 if (!mboxq) {
7058 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7059 "2519 Unable to allocate memory for issuing "
7060 "NOP mailbox command\n");
7061 return 0;
7062 }
7063
7064 /* Set up NOP SLI4_CONFIG mailbox-ioctl command */
7065 length = (sizeof(struct lpfc_mbx_nop) -
7066 sizeof(struct lpfc_sli4_cfg_mhdr));
7067 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7068 LPFC_MBOX_OPCODE_NOP, length, LPFC_SLI4_MBX_EMBED);
7069
7070 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
7071 for (cmdsent = 0; cmdsent < cnt; cmdsent++) {
7072 if (!phba->sli4_hba.intr_enable)
7073 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7074 else
7075 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
7076 if (rc == MBX_TIMEOUT)
7077 break;
7078 /* Check return status */
7079 shdr = (union lpfc_sli4_cfg_shdr *)
7080 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7081 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7082 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
7083 &shdr->response);
7084 if (shdr_status || shdr_add_status || rc) {
7085 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7086 "2520 NOP mailbox command failed "
7087 "status x%x add_status x%x mbx "
7088 "status x%x\n", shdr_status,
7089 shdr_add_status, rc);
7090 break;
7091 }
7092 }
7093
7094 if (rc != MBX_TIMEOUT)
7095 mempool_free(mboxq, phba->mbox_mem_pool);
7096
7097 return cmdsent;
7098}
7099
7100/**
James Smartda0436e2009-05-22 14:51:39 -04007101 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
7102 * @phba: pointer to lpfc hba data structure.
7103 *
7104 * This routine is invoked to set up the PCI device memory space for device
7105 * with SLI-4 interface spec.
7106 *
7107 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007108 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007109 * other values - error
7110 **/
7111static int
7112lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
7113{
7114 struct pci_dev *pdev;
7115 unsigned long bar0map_len, bar1map_len, bar2map_len;
7116 int error = -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -05007117 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04007118
7119 /* Obtain PCI device reference */
7120 if (!phba->pcidev)
7121 return error;
7122 else
7123 pdev = phba->pcidev;
7124
7125 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05007126 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
7127 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
7128 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
7129 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smartda0436e2009-05-22 14:51:39 -04007130 return error;
Michael Reed8e685972009-09-18 12:02:05 -05007131 }
7132 }
James Smartda0436e2009-05-22 14:51:39 -04007133
James Smart2fcee4b2010-12-15 17:57:46 -05007134 /*
7135 * The BARs and register set definitions and offset locations are
7136 * dependent on the if_type.
7137 */
7138 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
7139 &phba->sli4_hba.sli_intf.word0)) {
7140 return error;
7141 }
7142
7143 /* There is no SLI3 failback for SLI4 devices. */
7144 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
7145 LPFC_SLI_INTF_VALID) {
7146 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7147 "2894 SLI_INTF reg contents invalid "
7148 "sli_intf reg 0x%x\n",
7149 phba->sli4_hba.sli_intf.word0);
7150 return error;
7151 }
7152
7153 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7154 /*
7155 * Get the bus address of SLI4 device Bar regions and the
7156 * number of bytes required by each mapping. The mapping of the
7157 * particular PCI BARs regions is dependent on the type of
7158 * SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -04007159 */
James Smart1dfb5a42010-02-12 14:40:50 -05007160 if (pci_resource_start(pdev, 0)) {
7161 phba->pci_bar0_map = pci_resource_start(pdev, 0);
7162 bar0map_len = pci_resource_len(pdev, 0);
James Smart2fcee4b2010-12-15 17:57:46 -05007163
7164 /*
7165 * Map SLI4 PCI Config Space Register base to a kernel virtual
7166 * addr
7167 */
7168 phba->sli4_hba.conf_regs_memmap_p =
7169 ioremap(phba->pci_bar0_map, bar0map_len);
7170 if (!phba->sli4_hba.conf_regs_memmap_p) {
7171 dev_printk(KERN_ERR, &pdev->dev,
7172 "ioremap failed for SLI4 PCI config "
7173 "registers.\n");
7174 goto out;
7175 }
7176 /* Set up BAR0 PCI config space register memory map */
7177 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smart1dfb5a42010-02-12 14:40:50 -05007178 } else {
7179 phba->pci_bar0_map = pci_resource_start(pdev, 1);
7180 bar0map_len = pci_resource_len(pdev, 1);
James Smart2fcee4b2010-12-15 17:57:46 -05007181 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
7182 dev_printk(KERN_ERR, &pdev->dev,
7183 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
7184 goto out;
7185 }
7186 phba->sli4_hba.conf_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04007187 ioremap(phba->pci_bar0_map, bar0map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05007188 if (!phba->sli4_hba.conf_regs_memmap_p) {
7189 dev_printk(KERN_ERR, &pdev->dev,
7190 "ioremap failed for SLI4 PCI config "
7191 "registers.\n");
7192 goto out;
7193 }
7194 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smartda0436e2009-05-22 14:51:39 -04007195 }
7196
James Smartc31098c2011-04-16 11:03:33 -04007197 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
7198 (pci_resource_start(pdev, 2))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007199 /*
7200 * Map SLI4 if type 0 HBA Control Register base to a kernel
7201 * virtual address and setup the registers.
7202 */
7203 phba->pci_bar1_map = pci_resource_start(pdev, 2);
7204 bar1map_len = pci_resource_len(pdev, 2);
7205 phba->sli4_hba.ctrl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04007206 ioremap(phba->pci_bar1_map, bar1map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05007207 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
7208 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04007209 "ioremap failed for SLI4 HBA control registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05007210 goto out_iounmap_conf;
7211 }
7212 lpfc_sli4_bar1_register_memmap(phba);
James Smartda0436e2009-05-22 14:51:39 -04007213 }
7214
James Smartc31098c2011-04-16 11:03:33 -04007215 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
7216 (pci_resource_start(pdev, 4))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007217 /*
7218 * Map SLI4 if type 0 HBA Doorbell Register base to a kernel
7219 * virtual address and setup the registers.
7220 */
7221 phba->pci_bar2_map = pci_resource_start(pdev, 4);
7222 bar2map_len = pci_resource_len(pdev, 4);
7223 phba->sli4_hba.drbl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04007224 ioremap(phba->pci_bar2_map, bar2map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05007225 if (!phba->sli4_hba.drbl_regs_memmap_p) {
7226 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04007227 "ioremap failed for SLI4 HBA doorbell registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05007228 goto out_iounmap_ctrl;
7229 }
7230 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
7231 if (error)
7232 goto out_iounmap_all;
James Smartda0436e2009-05-22 14:51:39 -04007233 }
7234
James Smartda0436e2009-05-22 14:51:39 -04007235 return 0;
7236
7237out_iounmap_all:
7238 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
7239out_iounmap_ctrl:
7240 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
7241out_iounmap_conf:
7242 iounmap(phba->sli4_hba.conf_regs_memmap_p);
7243out:
7244 return error;
7245}
7246
7247/**
7248 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
7249 * @phba: pointer to lpfc hba data structure.
7250 *
7251 * This routine is invoked to unset the PCI device memory space for device
7252 * with SLI-4 interface spec.
7253 **/
7254static void
7255lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
7256{
7257 struct pci_dev *pdev;
7258
7259 /* Obtain PCI device reference */
7260 if (!phba->pcidev)
7261 return;
7262 else
7263 pdev = phba->pcidev;
7264
7265 /* Free coherent DMA memory allocated */
7266
7267 /* Unmap I/O memory space */
7268 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
7269 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
7270 iounmap(phba->sli4_hba.conf_regs_memmap_p);
7271
7272 return;
7273}
7274
7275/**
James Smart3772a992009-05-22 14:50:54 -04007276 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
7277 * @phba: pointer to lpfc hba data structure.
7278 *
7279 * This routine is invoked to enable the MSI-X interrupt vectors to device
7280 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
7281 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
7282 * invoked, enables either all or nothing, depending on the current
7283 * availability of PCI vector resources. The device driver is responsible
7284 * for calling the individual request_irq() to register each MSI-X vector
7285 * with a interrupt handler, which is done in this function. Note that
7286 * later when device is unloading, the driver should always call free_irq()
7287 * on all MSI-X vectors it has done request_irq() on before calling
7288 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
7289 * will be left with MSI-X enabled and leaks its vectors.
7290 *
7291 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007292 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007293 * other values - error
7294 **/
7295static int
7296lpfc_sli_enable_msix(struct lpfc_hba *phba)
7297{
7298 int rc, i;
7299 LPFC_MBOXQ_t *pmb;
7300
7301 /* Set up MSI-X multi-message vectors */
7302 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
7303 phba->msix_entries[i].entry = i;
7304
7305 /* Configure MSI-X capability structure */
7306 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
7307 ARRAY_SIZE(phba->msix_entries));
7308 if (rc) {
7309 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7310 "0420 PCI enable MSI-X failed (%d)\n", rc);
7311 goto msi_fail_out;
7312 }
7313 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
7314 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7315 "0477 MSI-X entry[%d]: vector=x%x "
7316 "message=%d\n", i,
7317 phba->msix_entries[i].vector,
7318 phba->msix_entries[i].entry);
7319 /*
7320 * Assign MSI-X vectors to interrupt handlers
7321 */
7322
7323 /* vector-0 is associated to slow-path handler */
7324 rc = request_irq(phba->msix_entries[0].vector,
7325 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
7326 LPFC_SP_DRIVER_HANDLER_NAME, phba);
7327 if (rc) {
7328 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7329 "0421 MSI-X slow-path request_irq failed "
7330 "(%d)\n", rc);
7331 goto msi_fail_out;
7332 }
7333
7334 /* vector-1 is associated to fast-path handler */
7335 rc = request_irq(phba->msix_entries[1].vector,
7336 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
7337 LPFC_FP_DRIVER_HANDLER_NAME, phba);
7338
7339 if (rc) {
7340 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7341 "0429 MSI-X fast-path request_irq failed "
7342 "(%d)\n", rc);
7343 goto irq_fail_out;
7344 }
7345
7346 /*
7347 * Configure HBA MSI-X attention conditions to messages
7348 */
7349 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7350
7351 if (!pmb) {
7352 rc = -ENOMEM;
7353 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7354 "0474 Unable to allocate memory for issuing "
7355 "MBOX_CONFIG_MSI command\n");
7356 goto mem_fail_out;
7357 }
7358 rc = lpfc_config_msi(phba, pmb);
7359 if (rc)
7360 goto mbx_fail_out;
7361 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
7362 if (rc != MBX_SUCCESS) {
7363 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
7364 "0351 Config MSI mailbox command failed, "
7365 "mbxCmd x%x, mbxStatus x%x\n",
7366 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
7367 goto mbx_fail_out;
7368 }
7369
7370 /* Free memory allocated for mailbox command */
7371 mempool_free(pmb, phba->mbox_mem_pool);
7372 return rc;
7373
7374mbx_fail_out:
7375 /* Free memory allocated for mailbox command */
7376 mempool_free(pmb, phba->mbox_mem_pool);
7377
7378mem_fail_out:
7379 /* free the irq already requested */
7380 free_irq(phba->msix_entries[1].vector, phba);
7381
7382irq_fail_out:
7383 /* free the irq already requested */
7384 free_irq(phba->msix_entries[0].vector, phba);
7385
7386msi_fail_out:
7387 /* Unconfigure MSI-X capability structure */
7388 pci_disable_msix(phba->pcidev);
7389 return rc;
7390}
7391
7392/**
7393 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
7394 * @phba: pointer to lpfc hba data structure.
7395 *
7396 * This routine is invoked to release the MSI-X vectors and then disable the
7397 * MSI-X interrupt mode to device with SLI-3 interface spec.
7398 **/
7399static void
7400lpfc_sli_disable_msix(struct lpfc_hba *phba)
7401{
7402 int i;
7403
7404 /* Free up MSI-X multi-message vectors */
7405 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
7406 free_irq(phba->msix_entries[i].vector, phba);
7407 /* Disable MSI-X */
7408 pci_disable_msix(phba->pcidev);
7409
7410 return;
7411}
7412
7413/**
7414 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
7415 * @phba: pointer to lpfc hba data structure.
7416 *
7417 * This routine is invoked to enable the MSI interrupt mode to device with
7418 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
7419 * enable the MSI vector. The device driver is responsible for calling the
7420 * request_irq() to register MSI vector with a interrupt the handler, which
7421 * is done in this function.
7422 *
7423 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007424 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007425 * other values - error
7426 */
7427static int
7428lpfc_sli_enable_msi(struct lpfc_hba *phba)
7429{
7430 int rc;
7431
7432 rc = pci_enable_msi(phba->pcidev);
7433 if (!rc)
7434 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7435 "0462 PCI enable MSI mode success.\n");
7436 else {
7437 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7438 "0471 PCI enable MSI mode failed (%d)\n", rc);
7439 return rc;
7440 }
7441
7442 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
7443 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7444 if (rc) {
7445 pci_disable_msi(phba->pcidev);
7446 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7447 "0478 MSI request_irq failed (%d)\n", rc);
7448 }
7449 return rc;
7450}
7451
7452/**
7453 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
7454 * @phba: pointer to lpfc hba data structure.
7455 *
7456 * This routine is invoked to disable the MSI interrupt mode to device with
7457 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
7458 * done request_irq() on before calling pci_disable_msi(). Failure to do so
7459 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
7460 * its vector.
7461 */
7462static void
7463lpfc_sli_disable_msi(struct lpfc_hba *phba)
7464{
7465 free_irq(phba->pcidev->irq, phba);
7466 pci_disable_msi(phba->pcidev);
7467 return;
7468}
7469
7470/**
7471 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
7472 * @phba: pointer to lpfc hba data structure.
7473 *
7474 * This routine is invoked to enable device interrupt and associate driver's
7475 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
7476 * spec. Depends on the interrupt mode configured to the driver, the driver
7477 * will try to fallback from the configured interrupt mode to an interrupt
7478 * mode which is supported by the platform, kernel, and device in the order
7479 * of:
7480 * MSI-X -> MSI -> IRQ.
7481 *
7482 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007483 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007484 * other values - error
7485 **/
7486static uint32_t
7487lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
7488{
7489 uint32_t intr_mode = LPFC_INTR_ERROR;
7490 int retval;
7491
7492 if (cfg_mode == 2) {
7493 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
7494 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
7495 if (!retval) {
7496 /* Now, try to enable MSI-X interrupt mode */
7497 retval = lpfc_sli_enable_msix(phba);
7498 if (!retval) {
7499 /* Indicate initialization to MSI-X mode */
7500 phba->intr_type = MSIX;
7501 intr_mode = 2;
7502 }
7503 }
7504 }
7505
7506 /* Fallback to MSI if MSI-X initialization failed */
7507 if (cfg_mode >= 1 && phba->intr_type == NONE) {
7508 retval = lpfc_sli_enable_msi(phba);
7509 if (!retval) {
7510 /* Indicate initialization to MSI mode */
7511 phba->intr_type = MSI;
7512 intr_mode = 1;
7513 }
7514 }
7515
7516 /* Fallback to INTx if both MSI-X/MSI initalization failed */
7517 if (phba->intr_type == NONE) {
7518 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
7519 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7520 if (!retval) {
7521 /* Indicate initialization to INTx mode */
7522 phba->intr_type = INTx;
7523 intr_mode = 0;
7524 }
7525 }
7526 return intr_mode;
7527}
7528
7529/**
7530 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
7531 * @phba: pointer to lpfc hba data structure.
7532 *
7533 * This routine is invoked to disable device interrupt and disassociate the
7534 * driver's interrupt handler(s) from interrupt vector(s) to device with
7535 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
7536 * release the interrupt vector(s) for the message signaled interrupt.
7537 **/
7538static void
7539lpfc_sli_disable_intr(struct lpfc_hba *phba)
7540{
7541 /* Disable the currently initialized interrupt mode */
7542 if (phba->intr_type == MSIX)
7543 lpfc_sli_disable_msix(phba);
7544 else if (phba->intr_type == MSI)
7545 lpfc_sli_disable_msi(phba);
7546 else if (phba->intr_type == INTx)
7547 free_irq(phba->pcidev->irq, phba);
7548
7549 /* Reset interrupt management states */
7550 phba->intr_type = NONE;
7551 phba->sli.slistat.sli_intr = 0;
7552
7553 return;
7554}
7555
7556/**
James Smartda0436e2009-05-22 14:51:39 -04007557 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
7558 * @phba: pointer to lpfc hba data structure.
7559 *
7560 * This routine is invoked to enable the MSI-X interrupt vectors to device
7561 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
7562 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
7563 * enables either all or nothing, depending on the current availability of
7564 * PCI vector resources. The device driver is responsible for calling the
7565 * individual request_irq() to register each MSI-X vector with a interrupt
7566 * handler, which is done in this function. Note that later when device is
7567 * unloading, the driver should always call free_irq() on all MSI-X vectors
7568 * it has done request_irq() on before calling pci_disable_msix(). Failure
7569 * to do so results in a BUG_ON() and a device will be left with MSI-X
7570 * enabled and leaks its vectors.
7571 *
7572 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007573 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007574 * other values - error
7575 **/
7576static int
7577lpfc_sli4_enable_msix(struct lpfc_hba *phba)
7578{
James Smart75baf692010-06-08 18:31:21 -04007579 int vectors, rc, index;
James Smartda0436e2009-05-22 14:51:39 -04007580
7581 /* Set up MSI-X multi-message vectors */
7582 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
7583 phba->sli4_hba.msix_entries[index].entry = index;
7584
7585 /* Configure MSI-X capability structure */
James Smart75baf692010-06-08 18:31:21 -04007586 vectors = phba->sli4_hba.cfg_eqn;
7587enable_msix_vectors:
James Smartda0436e2009-05-22 14:51:39 -04007588 rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
James Smart75baf692010-06-08 18:31:21 -04007589 vectors);
7590 if (rc > 1) {
7591 vectors = rc;
7592 goto enable_msix_vectors;
7593 } else if (rc) {
James Smartda0436e2009-05-22 14:51:39 -04007594 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7595 "0484 PCI enable MSI-X failed (%d)\n", rc);
7596 goto msi_fail_out;
7597 }
James Smart75baf692010-06-08 18:31:21 -04007598
James Smartda0436e2009-05-22 14:51:39 -04007599 /* Log MSI-X vector assignment */
James Smart75baf692010-06-08 18:31:21 -04007600 for (index = 0; index < vectors; index++)
James Smartda0436e2009-05-22 14:51:39 -04007601 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7602 "0489 MSI-X entry[%d]: vector=x%x "
7603 "message=%d\n", index,
7604 phba->sli4_hba.msix_entries[index].vector,
7605 phba->sli4_hba.msix_entries[index].entry);
7606 /*
7607 * Assign MSI-X vectors to interrupt handlers
7608 */
James Smart05580562011-05-24 11:40:48 -04007609 if (vectors > 1)
7610 rc = request_irq(phba->sli4_hba.msix_entries[0].vector,
7611 &lpfc_sli4_sp_intr_handler, IRQF_SHARED,
7612 LPFC_SP_DRIVER_HANDLER_NAME, phba);
7613 else
7614 /* All Interrupts need to be handled by one EQ */
7615 rc = request_irq(phba->sli4_hba.msix_entries[0].vector,
7616 &lpfc_sli4_intr_handler, IRQF_SHARED,
7617 LPFC_DRIVER_NAME, phba);
James Smartda0436e2009-05-22 14:51:39 -04007618 if (rc) {
7619 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7620 "0485 MSI-X slow-path request_irq failed "
7621 "(%d)\n", rc);
7622 goto msi_fail_out;
7623 }
7624
7625 /* The rest of the vector(s) are associated to fast-path handler(s) */
James Smart75baf692010-06-08 18:31:21 -04007626 for (index = 1; index < vectors; index++) {
James Smartda0436e2009-05-22 14:51:39 -04007627 phba->sli4_hba.fcp_eq_hdl[index - 1].idx = index - 1;
7628 phba->sli4_hba.fcp_eq_hdl[index - 1].phba = phba;
7629 rc = request_irq(phba->sli4_hba.msix_entries[index].vector,
7630 &lpfc_sli4_fp_intr_handler, IRQF_SHARED,
7631 LPFC_FP_DRIVER_HANDLER_NAME,
7632 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
7633 if (rc) {
7634 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7635 "0486 MSI-X fast-path (%d) "
7636 "request_irq failed (%d)\n", index, rc);
7637 goto cfg_fail_out;
7638 }
7639 }
James Smart75baf692010-06-08 18:31:21 -04007640 phba->sli4_hba.msix_vec_nr = vectors;
James Smartda0436e2009-05-22 14:51:39 -04007641
7642 return rc;
7643
7644cfg_fail_out:
7645 /* free the irq already requested */
7646 for (--index; index >= 1; index--)
7647 free_irq(phba->sli4_hba.msix_entries[index - 1].vector,
7648 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
7649
7650 /* free the irq already requested */
7651 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
7652
7653msi_fail_out:
7654 /* Unconfigure MSI-X capability structure */
7655 pci_disable_msix(phba->pcidev);
7656 return rc;
7657}
7658
7659/**
7660 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
7661 * @phba: pointer to lpfc hba data structure.
7662 *
7663 * This routine is invoked to release the MSI-X vectors and then disable the
7664 * MSI-X interrupt mode to device with SLI-4 interface spec.
7665 **/
7666static void
7667lpfc_sli4_disable_msix(struct lpfc_hba *phba)
7668{
7669 int index;
7670
7671 /* Free up MSI-X multi-message vectors */
7672 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
7673
James Smart75baf692010-06-08 18:31:21 -04007674 for (index = 1; index < phba->sli4_hba.msix_vec_nr; index++)
James Smartda0436e2009-05-22 14:51:39 -04007675 free_irq(phba->sli4_hba.msix_entries[index].vector,
7676 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
James Smart75baf692010-06-08 18:31:21 -04007677
James Smartda0436e2009-05-22 14:51:39 -04007678 /* Disable MSI-X */
7679 pci_disable_msix(phba->pcidev);
7680
7681 return;
7682}
7683
7684/**
7685 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
7686 * @phba: pointer to lpfc hba data structure.
7687 *
7688 * This routine is invoked to enable the MSI interrupt mode to device with
7689 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
7690 * to enable the MSI vector. The device driver is responsible for calling
7691 * the request_irq() to register MSI vector with a interrupt the handler,
7692 * which is done in this function.
7693 *
7694 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007695 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007696 * other values - error
7697 **/
7698static int
7699lpfc_sli4_enable_msi(struct lpfc_hba *phba)
7700{
7701 int rc, index;
7702
7703 rc = pci_enable_msi(phba->pcidev);
7704 if (!rc)
7705 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7706 "0487 PCI enable MSI mode success.\n");
7707 else {
7708 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7709 "0488 PCI enable MSI mode failed (%d)\n", rc);
7710 return rc;
7711 }
7712
7713 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
7714 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7715 if (rc) {
7716 pci_disable_msi(phba->pcidev);
7717 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7718 "0490 MSI request_irq failed (%d)\n", rc);
James Smart75baf692010-06-08 18:31:21 -04007719 return rc;
James Smartda0436e2009-05-22 14:51:39 -04007720 }
7721
7722 for (index = 0; index < phba->cfg_fcp_eq_count; index++) {
7723 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
7724 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
7725 }
7726
James Smart75baf692010-06-08 18:31:21 -04007727 return 0;
James Smartda0436e2009-05-22 14:51:39 -04007728}
7729
7730/**
7731 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
7732 * @phba: pointer to lpfc hba data structure.
7733 *
7734 * This routine is invoked to disable the MSI interrupt mode to device with
7735 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
7736 * done request_irq() on before calling pci_disable_msi(). Failure to do so
7737 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
7738 * its vector.
7739 **/
7740static void
7741lpfc_sli4_disable_msi(struct lpfc_hba *phba)
7742{
7743 free_irq(phba->pcidev->irq, phba);
7744 pci_disable_msi(phba->pcidev);
7745 return;
7746}
7747
7748/**
7749 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
7750 * @phba: pointer to lpfc hba data structure.
7751 *
7752 * This routine is invoked to enable device interrupt and associate driver's
7753 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
7754 * interface spec. Depends on the interrupt mode configured to the driver,
7755 * the driver will try to fallback from the configured interrupt mode to an
7756 * interrupt mode which is supported by the platform, kernel, and device in
7757 * the order of:
7758 * MSI-X -> MSI -> IRQ.
7759 *
7760 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007761 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007762 * other values - error
7763 **/
7764static uint32_t
7765lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
7766{
7767 uint32_t intr_mode = LPFC_INTR_ERROR;
7768 int retval, index;
7769
7770 if (cfg_mode == 2) {
7771 /* Preparation before conf_msi mbox cmd */
7772 retval = 0;
7773 if (!retval) {
7774 /* Now, try to enable MSI-X interrupt mode */
7775 retval = lpfc_sli4_enable_msix(phba);
7776 if (!retval) {
7777 /* Indicate initialization to MSI-X mode */
7778 phba->intr_type = MSIX;
7779 intr_mode = 2;
7780 }
7781 }
7782 }
7783
7784 /* Fallback to MSI if MSI-X initialization failed */
7785 if (cfg_mode >= 1 && phba->intr_type == NONE) {
7786 retval = lpfc_sli4_enable_msi(phba);
7787 if (!retval) {
7788 /* Indicate initialization to MSI mode */
7789 phba->intr_type = MSI;
7790 intr_mode = 1;
7791 }
7792 }
7793
7794 /* Fallback to INTx if both MSI-X/MSI initalization failed */
7795 if (phba->intr_type == NONE) {
7796 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
7797 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7798 if (!retval) {
7799 /* Indicate initialization to INTx mode */
7800 phba->intr_type = INTx;
7801 intr_mode = 0;
7802 for (index = 0; index < phba->cfg_fcp_eq_count;
7803 index++) {
7804 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
7805 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
7806 }
7807 }
7808 }
7809 return intr_mode;
7810}
7811
7812/**
7813 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
7814 * @phba: pointer to lpfc hba data structure.
7815 *
7816 * This routine is invoked to disable device interrupt and disassociate
7817 * the driver's interrupt handler(s) from interrupt vector(s) to device
7818 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
7819 * will release the interrupt vector(s) for the message signaled interrupt.
7820 **/
7821static void
7822lpfc_sli4_disable_intr(struct lpfc_hba *phba)
7823{
7824 /* Disable the currently initialized interrupt mode */
7825 if (phba->intr_type == MSIX)
7826 lpfc_sli4_disable_msix(phba);
7827 else if (phba->intr_type == MSI)
7828 lpfc_sli4_disable_msi(phba);
7829 else if (phba->intr_type == INTx)
7830 free_irq(phba->pcidev->irq, phba);
7831
7832 /* Reset interrupt management states */
7833 phba->intr_type = NONE;
7834 phba->sli.slistat.sli_intr = 0;
7835
7836 return;
7837}
7838
7839/**
James Smart3772a992009-05-22 14:50:54 -04007840 * lpfc_unset_hba - Unset SLI3 hba device initialization
7841 * @phba: pointer to lpfc hba data structure.
7842 *
7843 * This routine is invoked to unset the HBA device initialization steps to
7844 * a device with SLI-3 interface spec.
7845 **/
7846static void
7847lpfc_unset_hba(struct lpfc_hba *phba)
7848{
7849 struct lpfc_vport *vport = phba->pport;
7850 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7851
7852 spin_lock_irq(shost->host_lock);
7853 vport->load_flag |= FC_UNLOADING;
7854 spin_unlock_irq(shost->host_lock);
7855
7856 lpfc_stop_hba_timers(phba);
7857
7858 phba->pport->work_port_events = 0;
7859
7860 lpfc_sli_hba_down(phba);
7861
7862 lpfc_sli_brdrestart(phba);
7863
7864 lpfc_sli_disable_intr(phba);
7865
7866 return;
7867}
7868
7869/**
James Smartda0436e2009-05-22 14:51:39 -04007870 * lpfc_sli4_unset_hba - Unset SLI4 hba device initialization.
7871 * @phba: pointer to lpfc hba data structure.
7872 *
7873 * This routine is invoked to unset the HBA device initialization steps to
7874 * a device with SLI-4 interface spec.
7875 **/
7876static void
7877lpfc_sli4_unset_hba(struct lpfc_hba *phba)
7878{
7879 struct lpfc_vport *vport = phba->pport;
7880 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7881
7882 spin_lock_irq(shost->host_lock);
7883 vport->load_flag |= FC_UNLOADING;
7884 spin_unlock_irq(shost->host_lock);
7885
7886 phba->pport->work_port_events = 0;
7887
James Smart3677a3a2010-09-29 11:19:14 -04007888 /* Stop the SLI4 device port */
7889 lpfc_stop_port(phba);
James Smartda0436e2009-05-22 14:51:39 -04007890
7891 lpfc_sli4_disable_intr(phba);
7892
James Smart3677a3a2010-09-29 11:19:14 -04007893 /* Reset SLI4 HBA FCoE function */
7894 lpfc_pci_function_reset(phba);
7895
James Smartda0436e2009-05-22 14:51:39 -04007896 return;
7897}
7898
7899/**
James Smart5af5eee2010-10-22 11:06:38 -04007900 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
7901 * @phba: Pointer to HBA context object.
7902 *
7903 * This function is called in the SLI4 code path to wait for completion
7904 * of device's XRIs exchange busy. It will check the XRI exchange busy
7905 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
7906 * that, it will check the XRI exchange busy on outstanding FCP and ELS
7907 * I/Os every 30 seconds, log error message, and wait forever. Only when
7908 * all XRI exchange busy complete, the driver unload shall proceed with
7909 * invoking the function reset ioctl mailbox command to the CNA and the
7910 * the rest of the driver unload resource release.
7911 **/
7912static void
7913lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
7914{
7915 int wait_time = 0;
7916 int fcp_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
7917 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
7918
7919 while (!fcp_xri_cmpl || !els_xri_cmpl) {
7920 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
7921 if (!fcp_xri_cmpl)
7922 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7923 "2877 FCP XRI exchange busy "
7924 "wait time: %d seconds.\n",
7925 wait_time/1000);
7926 if (!els_xri_cmpl)
7927 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7928 "2878 ELS XRI exchange busy "
7929 "wait time: %d seconds.\n",
7930 wait_time/1000);
7931 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
7932 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
7933 } else {
7934 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
7935 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
7936 }
7937 fcp_xri_cmpl =
7938 list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
7939 els_xri_cmpl =
7940 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
7941 }
7942}
7943
7944/**
James Smartda0436e2009-05-22 14:51:39 -04007945 * lpfc_sli4_hba_unset - Unset the fcoe hba
7946 * @phba: Pointer to HBA context object.
7947 *
7948 * This function is called in the SLI4 code path to reset the HBA's FCoE
7949 * function. The caller is not required to hold any lock. This routine
7950 * issues PCI function reset mailbox command to reset the FCoE function.
7951 * At the end of the function, it calls lpfc_hba_down_post function to
7952 * free any pending commands.
7953 **/
7954static void
7955lpfc_sli4_hba_unset(struct lpfc_hba *phba)
7956{
7957 int wait_cnt = 0;
7958 LPFC_MBOXQ_t *mboxq;
James Smart912e3ac2011-05-24 11:42:11 -04007959 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -04007960
7961 lpfc_stop_hba_timers(phba);
7962 phba->sli4_hba.intr_enable = 0;
7963
7964 /*
7965 * Gracefully wait out the potential current outstanding asynchronous
7966 * mailbox command.
7967 */
7968
7969 /* First, block any pending async mailbox command from posted */
7970 spin_lock_irq(&phba->hbalock);
7971 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7972 spin_unlock_irq(&phba->hbalock);
7973 /* Now, trying to wait it out if we can */
7974 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7975 msleep(10);
7976 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
7977 break;
7978 }
7979 /* Forcefully release the outstanding mailbox command if timed out */
7980 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7981 spin_lock_irq(&phba->hbalock);
7982 mboxq = phba->sli.mbox_active;
7983 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7984 __lpfc_mbox_cmpl_put(phba, mboxq);
7985 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7986 phba->sli.mbox_active = NULL;
7987 spin_unlock_irq(&phba->hbalock);
7988 }
7989
James Smart5af5eee2010-10-22 11:06:38 -04007990 /* Abort all iocbs associated with the hba */
7991 lpfc_sli_hba_iocb_abort(phba);
7992
7993 /* Wait for completion of device XRI exchange busy */
7994 lpfc_sli4_xri_exchange_busy_wait(phba);
7995
James Smartda0436e2009-05-22 14:51:39 -04007996 /* Disable PCI subsystem interrupt */
7997 lpfc_sli4_disable_intr(phba);
7998
James Smart912e3ac2011-05-24 11:42:11 -04007999 /* Disable SR-IOV if enabled */
8000 if (phba->cfg_sriov_nr_virtfn)
8001 pci_disable_sriov(pdev);
8002
James Smartda0436e2009-05-22 14:51:39 -04008003 /* Stop kthread signal shall trigger work_done one more time */
8004 kthread_stop(phba->worker_thread);
8005
James Smart3677a3a2010-09-29 11:19:14 -04008006 /* Reset SLI4 HBA FCoE function */
8007 lpfc_pci_function_reset(phba);
8008
James Smartda0436e2009-05-22 14:51:39 -04008009 /* Stop the SLI4 device port */
8010 phba->pport->work_port_events = 0;
8011}
8012
James Smart28baac72010-02-12 14:42:03 -05008013 /**
8014 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
8015 * @phba: Pointer to HBA context object.
8016 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
8017 *
8018 * This function is called in the SLI4 code path to read the port's
8019 * sli4 capabilities.
8020 *
8021 * This function may be be called from any context that can block-wait
8022 * for the completion. The expectation is that this routine is called
8023 * typically from probe_one or from the online routine.
8024 **/
8025int
8026lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8027{
8028 int rc;
8029 struct lpfc_mqe *mqe;
8030 struct lpfc_pc_sli4_params *sli4_params;
8031 uint32_t mbox_tmo;
8032
8033 rc = 0;
8034 mqe = &mboxq->u.mqe;
8035
8036 /* Read the port's SLI4 Parameters port capabilities */
James Smartfedd3b72011-02-16 12:39:24 -05008037 lpfc_pc_sli4_params(mboxq);
James Smart28baac72010-02-12 14:42:03 -05008038 if (!phba->sli4_hba.intr_enable)
8039 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8040 else {
8041 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_PORT_CAPABILITIES);
8042 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
8043 }
8044
8045 if (unlikely(rc))
8046 return 1;
8047
8048 sli4_params = &phba->sli4_hba.pc_sli4_params;
8049 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
8050 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
8051 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
8052 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
8053 &mqe->un.sli4_params);
8054 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
8055 &mqe->un.sli4_params);
8056 sli4_params->proto_types = mqe->un.sli4_params.word3;
8057 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
8058 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
8059 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
8060 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
8061 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
8062 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
8063 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
8064 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
8065 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
8066 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
8067 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
8068 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
8069 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
8070 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
8071 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
8072 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
8073 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
8074 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
8075 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
8076 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
James Smart05580562011-05-24 11:40:48 -04008077
8078 /* Make sure that sge_supp_len can be handled by the driver */
8079 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
8080 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
8081
James Smart28baac72010-02-12 14:42:03 -05008082 return rc;
8083}
8084
James Smartda0436e2009-05-22 14:51:39 -04008085/**
James Smartfedd3b72011-02-16 12:39:24 -05008086 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
8087 * @phba: Pointer to HBA context object.
8088 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
8089 *
8090 * This function is called in the SLI4 code path to read the port's
8091 * sli4 capabilities.
8092 *
8093 * This function may be be called from any context that can block-wait
8094 * for the completion. The expectation is that this routine is called
8095 * typically from probe_one or from the online routine.
8096 **/
8097int
8098lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8099{
8100 int rc;
8101 struct lpfc_mqe *mqe = &mboxq->u.mqe;
8102 struct lpfc_pc_sli4_params *sli4_params;
8103 int length;
8104 struct lpfc_sli4_parameters *mbx_sli4_parameters;
8105
8106 /* Read the port's SLI4 Config Parameters */
8107 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
8108 sizeof(struct lpfc_sli4_cfg_mhdr));
8109 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
8110 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
8111 length, LPFC_SLI4_MBX_EMBED);
8112 if (!phba->sli4_hba.intr_enable)
8113 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8114 else
8115 rc = lpfc_sli_issue_mbox_wait(phba, mboxq,
8116 lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG));
8117 if (unlikely(rc))
8118 return rc;
8119 sli4_params = &phba->sli4_hba.pc_sli4_params;
8120 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
8121 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
8122 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
8123 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
8124 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
8125 mbx_sli4_parameters);
8126 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
8127 mbx_sli4_parameters);
8128 if (bf_get(cfg_phwq, mbx_sli4_parameters))
8129 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
8130 else
8131 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
8132 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
8133 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
8134 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
8135 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
8136 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
8137 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
8138 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
8139 mbx_sli4_parameters);
8140 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
8141 mbx_sli4_parameters);
James Smart05580562011-05-24 11:40:48 -04008142
8143 /* Make sure that sge_supp_len can be handled by the driver */
8144 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
8145 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
8146
James Smartfedd3b72011-02-16 12:39:24 -05008147 return 0;
8148}
8149
8150/**
James Smart3772a992009-05-22 14:50:54 -04008151 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
8152 * @pdev: pointer to PCI device
8153 * @pid: pointer to PCI device identifier
8154 *
8155 * This routine is to be called to attach a device with SLI-3 interface spec
8156 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
8157 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
8158 * information of the device and driver to see if the driver state that it can
8159 * support this kind of device. If the match is successful, the driver core
8160 * invokes this routine. If this routine determines it can claim the HBA, it
8161 * does all the initialization that it needs to do to handle the HBA properly.
8162 *
8163 * Return code
8164 * 0 - driver can claim the device
8165 * negative value - driver can not claim the device
8166 **/
8167static int __devinit
8168lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
8169{
8170 struct lpfc_hba *phba;
8171 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04008172 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -04008173 int error;
8174 uint32_t cfg_mode, intr_mode;
8175
8176 /* Allocate memory for HBA structure */
8177 phba = lpfc_hba_alloc(pdev);
8178 if (!phba)
8179 return -ENOMEM;
8180
8181 /* Perform generic PCI device enabling operation */
8182 error = lpfc_enable_pci_dev(phba);
8183 if (error) {
8184 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8185 "1401 Failed to enable pci device.\n");
8186 goto out_free_phba;
8187 }
8188
8189 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
8190 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
8191 if (error)
8192 goto out_disable_pci_dev;
8193
8194 /* Set up SLI-3 specific device PCI memory space */
8195 error = lpfc_sli_pci_mem_setup(phba);
8196 if (error) {
8197 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8198 "1402 Failed to set up pci memory space.\n");
8199 goto out_disable_pci_dev;
8200 }
8201
8202 /* Set up phase-1 common device driver resources */
8203 error = lpfc_setup_driver_resource_phase1(phba);
8204 if (error) {
8205 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8206 "1403 Failed to set up driver resource.\n");
8207 goto out_unset_pci_mem_s3;
8208 }
8209
8210 /* Set up SLI-3 specific device driver resources */
8211 error = lpfc_sli_driver_resource_setup(phba);
8212 if (error) {
8213 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8214 "1404 Failed to set up driver resource.\n");
8215 goto out_unset_pci_mem_s3;
8216 }
8217
8218 /* Initialize and populate the iocb list per host */
8219 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
8220 if (error) {
8221 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8222 "1405 Failed to initialize iocb list.\n");
8223 goto out_unset_driver_resource_s3;
8224 }
8225
8226 /* Set up common device driver resources */
8227 error = lpfc_setup_driver_resource_phase2(phba);
8228 if (error) {
8229 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8230 "1406 Failed to set up driver resource.\n");
8231 goto out_free_iocb_list;
8232 }
8233
8234 /* Create SCSI host to the physical port */
8235 error = lpfc_create_shost(phba);
8236 if (error) {
8237 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8238 "1407 Failed to create scsi host.\n");
8239 goto out_unset_driver_resource;
8240 }
8241
8242 /* Configure sysfs attributes */
8243 vport = phba->pport;
8244 error = lpfc_alloc_sysfs_attr(vport);
8245 if (error) {
8246 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8247 "1476 Failed to allocate sysfs attr\n");
8248 goto out_destroy_shost;
8249 }
8250
James Smart6669f9b2009-10-02 15:16:45 -04008251 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -04008252 /* Now, trying to enable interrupt and bring up the device */
8253 cfg_mode = phba->cfg_use_msi;
8254 while (true) {
8255 /* Put device to a known state before enabling interrupt */
8256 lpfc_stop_port(phba);
8257 /* Configure and enable interrupt */
8258 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
8259 if (intr_mode == LPFC_INTR_ERROR) {
8260 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8261 "0431 Failed to enable interrupt.\n");
8262 error = -ENODEV;
8263 goto out_free_sysfs_attr;
8264 }
8265 /* SLI-3 HBA setup */
8266 if (lpfc_sli_hba_setup(phba)) {
8267 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8268 "1477 Failed to set up hba\n");
8269 error = -ENODEV;
8270 goto out_remove_device;
8271 }
8272
8273 /* Wait 50ms for the interrupts of previous mailbox commands */
8274 msleep(50);
8275 /* Check active interrupts on message signaled interrupts */
8276 if (intr_mode == 0 ||
8277 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
8278 /* Log the current active interrupt mode */
8279 phba->intr_mode = intr_mode;
8280 lpfc_log_intr_mode(phba, intr_mode);
8281 break;
8282 } else {
8283 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8284 "0447 Configure interrupt mode (%d) "
8285 "failed active interrupt test.\n",
8286 intr_mode);
8287 /* Disable the current interrupt mode */
8288 lpfc_sli_disable_intr(phba);
8289 /* Try next level of interrupt mode */
8290 cfg_mode = --intr_mode;
8291 }
8292 }
8293
8294 /* Perform post initialization setup */
8295 lpfc_post_init_setup(phba);
8296
8297 /* Check if there are static vports to be created. */
8298 lpfc_create_static_vport(phba);
8299
8300 return 0;
8301
8302out_remove_device:
8303 lpfc_unset_hba(phba);
8304out_free_sysfs_attr:
8305 lpfc_free_sysfs_attr(vport);
8306out_destroy_shost:
8307 lpfc_destroy_shost(phba);
8308out_unset_driver_resource:
8309 lpfc_unset_driver_resource_phase2(phba);
8310out_free_iocb_list:
8311 lpfc_free_iocb_list(phba);
8312out_unset_driver_resource_s3:
8313 lpfc_sli_driver_resource_unset(phba);
8314out_unset_pci_mem_s3:
8315 lpfc_sli_pci_mem_unset(phba);
8316out_disable_pci_dev:
8317 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04008318 if (shost)
8319 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -04008320out_free_phba:
8321 lpfc_hba_free(phba);
8322 return error;
8323}
8324
8325/**
8326 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -04008327 * @pdev: pointer to PCI device
8328 *
James Smart3772a992009-05-22 14:50:54 -04008329 * This routine is to be called to disattach a device with SLI-3 interface
8330 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
8331 * removed from PCI bus, it performs all the necessary cleanup for the HBA
8332 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -04008333 **/
dea31012005-04-17 16:05:31 -05008334static void __devexit
James Smart3772a992009-05-22 14:50:54 -04008335lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -05008336{
James Smart2e0fef82007-06-17 19:56:36 -05008337 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8338 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -05008339 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -05008340 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -05008341 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05008342 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
8343
James Smart549e55c2007-08-02 11:09:51 -04008344 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04008345 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04008346 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008347
James Smart858c9f62007-06-17 19:56:39 -05008348 lpfc_free_sysfs_attr(vport);
8349
James Smarteada2722008-12-04 22:39:13 -05008350 /* Release all the vports against this physical port */
8351 vports = lpfc_create_vport_work_array(phba);
8352 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -04008353 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
James Smarteada2722008-12-04 22:39:13 -05008354 fc_vport_terminate(vports[i]->fc_vport);
8355 lpfc_destroy_vport_work_array(phba, vports);
8356
8357 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -05008358 fc_remove_host(shost);
8359 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04008360 lpfc_cleanup(vport);
8361
James Smart2e0fef82007-06-17 19:56:36 -05008362 /*
8363 * Bring down the SLI Layer. This step disable all interrupts,
8364 * clears the rings, discards all mailbox commands, and resets
8365 * the HBA.
8366 */
James Smarta257bf92009-04-06 18:48:10 -04008367
Justin P. Mattock48e34d02010-12-30 15:07:58 -08008368 /* HBA interrupt will be disabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -05008369 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -04008370 /* Stop kthread signal shall trigger work_done one more time */
8371 kthread_stop(phba->worker_thread);
8372 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -05008373 lpfc_sli_brdrestart(phba);
8374
James Smart3772a992009-05-22 14:50:54 -04008375 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05008376 spin_lock_irq(&phba->hbalock);
8377 list_del_init(&vport->listentry);
8378 spin_unlock_irq(&phba->hbalock);
8379
James Smart858c9f62007-06-17 19:56:39 -05008380 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05008381
James Smart912e3ac2011-05-24 11:42:11 -04008382 /* Disable SR-IOV if enabled */
8383 if (phba->cfg_sriov_nr_virtfn)
8384 pci_disable_sriov(pdev);
8385
James Smart5b75da22008-12-04 22:39:35 -05008386 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04008387 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -05008388
8389 pci_set_drvdata(pdev, NULL);
James Smart858c9f62007-06-17 19:56:39 -05008390 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05008391
8392 /*
8393 * Call scsi_free before mem_free since scsi bufs are released to their
8394 * corresponding pools here.
8395 */
8396 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -04008397 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -05008398
James Smart34b02dc2008-08-24 21:49:55 -04008399 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
8400 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -05008401
James Smart2e0fef82007-06-17 19:56:36 -05008402 /* Free resources associated with SLI2 interface */
8403 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -04008404 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -05008405
8406 /* unmap adapter SLIM and Control Registers */
8407 iounmap(phba->ctrl_regs_memmap_p);
8408 iounmap(phba->slim_memmap_p);
8409
James Smart3772a992009-05-22 14:50:54 -04008410 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -05008411
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05008412 pci_release_selected_regions(pdev, bars);
James Smart2e0fef82007-06-17 19:56:36 -05008413 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05008414}
8415
Linas Vepstas8d63f372007-02-14 14:28:36 -06008416/**
James Smart3772a992009-05-22 14:50:54 -04008417 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05008418 * @pdev: pointer to PCI device
8419 * @msg: power management message
8420 *
James Smart3772a992009-05-22 14:50:54 -04008421 * This routine is to be called from the kernel's PCI subsystem to support
8422 * system Power Management (PM) to device with SLI-3 interface spec. When
8423 * PM invokes this method, it quiesces the device by stopping the driver's
8424 * worker thread for the device, turning off device's interrupt and DMA,
8425 * and bring the device offline. Note that as the driver implements the
8426 * minimum PM requirements to a power-aware driver's PM support for the
8427 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
8428 * to the suspend() method call will be treated as SUSPEND and the driver will
8429 * fully reinitialize its device during resume() method call, the driver will
8430 * set device to PCI_D3hot state in PCI config space instead of setting it
8431 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -05008432 *
8433 * Return code
James Smart3772a992009-05-22 14:50:54 -04008434 * 0 - driver suspended the device
8435 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05008436 **/
8437static int
James Smart3772a992009-05-22 14:50:54 -04008438lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -05008439{
8440 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8441 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8442
8443 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8444 "0473 PCI device Power Management suspend.\n");
8445
8446 /* Bring down the device */
8447 lpfc_offline_prep(phba);
8448 lpfc_offline(phba);
8449 kthread_stop(phba->worker_thread);
8450
8451 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -04008452 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -05008453
8454 /* Save device state to PCI config space */
8455 pci_save_state(pdev);
8456 pci_set_power_state(pdev, PCI_D3hot);
8457
8458 return 0;
8459}
8460
8461/**
James Smart3772a992009-05-22 14:50:54 -04008462 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05008463 * @pdev: pointer to PCI device
8464 *
James Smart3772a992009-05-22 14:50:54 -04008465 * This routine is to be called from the kernel's PCI subsystem to support
8466 * system Power Management (PM) to device with SLI-3 interface spec. When PM
8467 * invokes this method, it restores the device's PCI config space state and
8468 * fully reinitializes the device and brings it online. Note that as the
8469 * driver implements the minimum PM requirements to a power-aware driver's
8470 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
8471 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
8472 * driver will fully reinitialize its device during resume() method call,
8473 * the device will be set to PCI_D0 directly in PCI config space before
8474 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -05008475 *
8476 * Return code
James Smart3772a992009-05-22 14:50:54 -04008477 * 0 - driver suspended the device
8478 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05008479 **/
8480static int
James Smart3772a992009-05-22 14:50:54 -04008481lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -05008482{
8483 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8484 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -05008485 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05008486 int error;
8487
8488 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8489 "0452 PCI device Power Management resume.\n");
8490
8491 /* Restore device state from PCI config space */
8492 pci_set_power_state(pdev, PCI_D0);
8493 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -04008494
James Smart1dfb5a42010-02-12 14:40:50 -05008495 /*
8496 * As the new kernel behavior of pci_restore_state() API call clears
8497 * device saved_state flag, need to save the restored state again.
8498 */
8499 pci_save_state(pdev);
8500
James Smart3a55b532008-12-04 22:38:54 -05008501 if (pdev->is_busmaster)
8502 pci_set_master(pdev);
8503
8504 /* Startup the kernel thread for this host adapter. */
8505 phba->worker_thread = kthread_run(lpfc_do_work, phba,
8506 "lpfc_worker_%d", phba->brd_no);
8507 if (IS_ERR(phba->worker_thread)) {
8508 error = PTR_ERR(phba->worker_thread);
8509 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8510 "0434 PM resume failed to start worker "
8511 "thread: error=x%x.\n", error);
8512 return error;
8513 }
8514
James Smart5b75da22008-12-04 22:39:35 -05008515 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04008516 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05008517 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -05008518 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -05008519 "0430 PM resume Failed to enable interrupt\n");
8520 return -EIO;
8521 } else
8522 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05008523
8524 /* Restart HBA and bring it online */
8525 lpfc_sli_brdrestart(phba);
8526 lpfc_online(phba);
8527
James Smart5b75da22008-12-04 22:39:35 -05008528 /* Log the current active interrupt mode */
8529 lpfc_log_intr_mode(phba, phba->intr_mode);
8530
James Smart3a55b532008-12-04 22:38:54 -05008531 return 0;
8532}
8533
8534/**
James Smart891478a2009-11-18 15:40:23 -05008535 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
8536 * @phba: pointer to lpfc hba data structure.
8537 *
8538 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -04008539 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -05008540 **/
8541static void
8542lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
8543{
James Smarte2af0d22010-03-15 11:25:32 -04008544 struct lpfc_sli *psli = &phba->sli;
8545 struct lpfc_sli_ring *pring;
8546
James Smart891478a2009-11-18 15:40:23 -05008547 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8548 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -04008549
8550 /*
8551 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
8552 * and let the SCSI mid-layer to retry them to recover.
8553 */
8554 pring = &psli->ring[psli->fcp_ring];
8555 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart891478a2009-11-18 15:40:23 -05008556}
8557
8558/**
James Smart0d878412009-10-02 15:16:56 -04008559 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
8560 * @phba: pointer to lpfc hba data structure.
8561 *
8562 * This routine is called to prepare the SLI3 device for PCI slot reset. It
8563 * disables the device interrupt and pci device, and aborts the internal FCP
8564 * pending I/Os.
8565 **/
8566static void
8567lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
8568{
James Smart0d878412009-10-02 15:16:56 -04008569 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05008570 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -04008571
James Smart75baf692010-06-08 18:31:21 -04008572 /* Block any management I/Os to the device */
8573 lpfc_block_mgmt_io(phba);
8574
James Smarte2af0d22010-03-15 11:25:32 -04008575 /* Block all SCSI devices' I/Os on the host */
8576 lpfc_scsi_dev_block(phba);
8577
8578 /* stop all timers */
8579 lpfc_stop_hba_timers(phba);
8580
James Smart0d878412009-10-02 15:16:56 -04008581 /* Disable interrupt and pci device */
8582 lpfc_sli_disable_intr(phba);
8583 pci_disable_device(phba->pcidev);
James Smart75baf692010-06-08 18:31:21 -04008584
James Smarte2af0d22010-03-15 11:25:32 -04008585 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
8586 lpfc_sli_flush_fcp_rings(phba);
James Smart0d878412009-10-02 15:16:56 -04008587}
8588
8589/**
8590 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
8591 * @phba: pointer to lpfc hba data structure.
8592 *
8593 * This routine is called to prepare the SLI3 device for PCI slot permanently
8594 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
8595 * pending I/Os.
8596 **/
8597static void
James Smart75baf692010-06-08 18:31:21 -04008598lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
James Smart0d878412009-10-02 15:16:56 -04008599{
8600 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05008601 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -04008602 /* Block all SCSI devices' I/Os on the host */
8603 lpfc_scsi_dev_block(phba);
8604
8605 /* stop all timers */
8606 lpfc_stop_hba_timers(phba);
8607
James Smart0d878412009-10-02 15:16:56 -04008608 /* Clean up all driver's outstanding SCSI I/Os */
8609 lpfc_sli_flush_fcp_rings(phba);
8610}
8611
8612/**
James Smart3772a992009-05-22 14:50:54 -04008613 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -04008614 * @pdev: pointer to PCI device.
8615 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -06008616 *
James Smart3772a992009-05-22 14:50:54 -04008617 * This routine is called from the PCI subsystem for I/O error handling to
8618 * device with SLI-3 interface spec. This function is called by the PCI
8619 * subsystem after a PCI bus error affecting this device has been detected.
8620 * When this function is invoked, it will need to stop all the I/Os and
8621 * interrupt(s) to the device. Once that is done, it will return
8622 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
8623 * as desired.
James Smarte59058c2008-08-24 21:49:00 -04008624 *
8625 * Return codes
James Smart0d878412009-10-02 15:16:56 -04008626 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -04008627 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
8628 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -04008629 **/
James Smart3772a992009-05-22 14:50:54 -04008630static pci_ers_result_t
8631lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -06008632{
James Smart51ef4c22007-08-02 11:10:31 -04008633 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8634 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06008635
James Smart0d878412009-10-02 15:16:56 -04008636 switch (state) {
8637 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -05008638 /* Non-fatal error, prepare for recovery */
8639 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -04008640 return PCI_ERS_RESULT_CAN_RECOVER;
8641 case pci_channel_io_frozen:
8642 /* Fatal error, prepare for slot reset */
8643 lpfc_sli_prep_dev_for_reset(phba);
8644 return PCI_ERS_RESULT_NEED_RESET;
8645 case pci_channel_io_perm_failure:
8646 /* Permanent failure, prepare for device down */
James Smart75baf692010-06-08 18:31:21 -04008647 lpfc_sli_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06008648 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -04008649 default:
8650 /* Unknown state, prepare and request slot reset */
8651 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8652 "0472 Unknown PCI error state: x%x\n", state);
8653 lpfc_sli_prep_dev_for_reset(phba);
8654 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -04008655 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06008656}
8657
8658/**
James Smart3772a992009-05-22 14:50:54 -04008659 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -04008660 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -06008661 *
James Smart3772a992009-05-22 14:50:54 -04008662 * This routine is called from the PCI subsystem for error handling to
8663 * device with SLI-3 interface spec. This is called after PCI bus has been
8664 * reset to restart the PCI card from scratch, as if from a cold-boot.
8665 * During the PCI subsystem error recovery, after driver returns
8666 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
8667 * recovery and then call this routine before calling the .resume method
8668 * to recover the device. This function will initialize the HBA device,
8669 * enable the interrupt, but it will just put the HBA to offline state
8670 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -04008671 *
8672 * Return codes
James Smart3772a992009-05-22 14:50:54 -04008673 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
8674 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -06008675 */
James Smart3772a992009-05-22 14:50:54 -04008676static pci_ers_result_t
8677lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06008678{
James Smart51ef4c22007-08-02 11:10:31 -04008679 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8680 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06008681 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -05008682 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06008683
8684 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +11008685 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -06008686 printk(KERN_ERR "lpfc: Cannot re-enable "
8687 "PCI device after reset.\n");
8688 return PCI_ERS_RESULT_DISCONNECT;
8689 }
8690
James Smart97207482008-12-04 22:39:19 -05008691 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05008692
8693 /*
8694 * As the new kernel behavior of pci_restore_state() API call clears
8695 * device saved_state flag, need to save the restored state again.
8696 */
8697 pci_save_state(pdev);
8698
James Smart97207482008-12-04 22:39:19 -05008699 if (pdev->is_busmaster)
8700 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06008701
James Smart92d7f7b2007-06-17 19:56:38 -05008702 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04008703 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05008704 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06008705
James Smart5b75da22008-12-04 22:39:35 -05008706 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04008707 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05008708 if (intr_mode == LPFC_INTR_ERROR) {
8709 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8710 "0427 Cannot re-enable interrupt after "
8711 "slot reset.\n");
8712 return PCI_ERS_RESULT_DISCONNECT;
8713 } else
8714 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06008715
James Smart75baf692010-06-08 18:31:21 -04008716 /* Take device offline, it will perform cleanup */
8717 lpfc_offline_prep(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06008718 lpfc_offline(phba);
8719 lpfc_sli_brdrestart(phba);
8720
James Smart5b75da22008-12-04 22:39:35 -05008721 /* Log the current active interrupt mode */
8722 lpfc_log_intr_mode(phba, phba->intr_mode);
8723
Linas Vepstas8d63f372007-02-14 14:28:36 -06008724 return PCI_ERS_RESULT_RECOVERED;
8725}
8726
8727/**
James Smart3772a992009-05-22 14:50:54 -04008728 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -04008729 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -06008730 *
James Smart3772a992009-05-22 14:50:54 -04008731 * This routine is called from the PCI subsystem for error handling to device
8732 * with SLI-3 interface spec. It is called when kernel error recovery tells
8733 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
8734 * error recovery. After this call, traffic can start to flow from this device
8735 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -06008736 */
James Smart3772a992009-05-22 14:50:54 -04008737static void
8738lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06008739{
James Smart51ef4c22007-08-02 11:10:31 -04008740 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8741 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06008742
James Smarte2af0d22010-03-15 11:25:32 -04008743 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -04008744 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -04008745
8746 /* Clean up Advanced Error Reporting (AER) if needed */
8747 if (phba->hba_flag & HBA_AER_ENABLED)
8748 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06008749}
8750
James Smart3772a992009-05-22 14:50:54 -04008751/**
James Smartda0436e2009-05-22 14:51:39 -04008752 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
8753 * @phba: pointer to lpfc hba data structure.
8754 *
8755 * returns the number of ELS/CT IOCBs to reserve
8756 **/
8757int
8758lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
8759{
8760 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
8761
James Smartf1126682009-06-10 17:22:44 -04008762 if (phba->sli_rev == LPFC_SLI_REV4) {
8763 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -04008764 return 10;
James Smartf1126682009-06-10 17:22:44 -04008765 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -04008766 return 25;
James Smartf1126682009-06-10 17:22:44 -04008767 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -04008768 return 50;
James Smartf1126682009-06-10 17:22:44 -04008769 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -04008770 return 100;
James Smartf1126682009-06-10 17:22:44 -04008771 else
James Smart6a9c52c2009-10-02 15:16:51 -04008772 return 150;
James Smartf1126682009-06-10 17:22:44 -04008773 } else
8774 return 0;
James Smartda0436e2009-05-22 14:51:39 -04008775}
8776
8777/**
8778 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
8779 * @pdev: pointer to PCI device
8780 * @pid: pointer to PCI device identifier
8781 *
8782 * This routine is called from the kernel's PCI subsystem to device with
8783 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
8784 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
8785 * information of the device and driver to see if the driver state that it
8786 * can support this kind of device. If the match is successful, the driver
8787 * core invokes this routine. If this routine determines it can claim the HBA,
8788 * it does all the initialization that it needs to do to handle the HBA
8789 * properly.
8790 *
8791 * Return code
8792 * 0 - driver can claim the device
8793 * negative value - driver can not claim the device
8794 **/
8795static int __devinit
8796lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
8797{
8798 struct lpfc_hba *phba;
8799 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04008800 struct Scsi_Host *shost = NULL;
James Smartda0436e2009-05-22 14:51:39 -04008801 int error;
8802 uint32_t cfg_mode, intr_mode;
8803 int mcnt;
James Smart05580562011-05-24 11:40:48 -04008804 int adjusted_fcp_eq_count;
8805 int fcp_qidx;
James Smartda0436e2009-05-22 14:51:39 -04008806
8807 /* Allocate memory for HBA structure */
8808 phba = lpfc_hba_alloc(pdev);
8809 if (!phba)
8810 return -ENOMEM;
8811
8812 /* Perform generic PCI device enabling operation */
8813 error = lpfc_enable_pci_dev(phba);
8814 if (error) {
8815 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8816 "1409 Failed to enable pci device.\n");
8817 goto out_free_phba;
8818 }
8819
8820 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
8821 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
8822 if (error)
8823 goto out_disable_pci_dev;
8824
8825 /* Set up SLI-4 specific device PCI memory space */
8826 error = lpfc_sli4_pci_mem_setup(phba);
8827 if (error) {
8828 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8829 "1410 Failed to set up pci memory space.\n");
8830 goto out_disable_pci_dev;
8831 }
8832
8833 /* Set up phase-1 common device driver resources */
8834 error = lpfc_setup_driver_resource_phase1(phba);
8835 if (error) {
8836 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8837 "1411 Failed to set up driver resource.\n");
8838 goto out_unset_pci_mem_s4;
8839 }
8840
8841 /* Set up SLI-4 Specific device driver resources */
8842 error = lpfc_sli4_driver_resource_setup(phba);
8843 if (error) {
8844 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8845 "1412 Failed to set up driver resource.\n");
8846 goto out_unset_pci_mem_s4;
8847 }
8848
8849 /* Initialize and populate the iocb list per host */
James Smart2a9bf3d2010-06-07 15:24:45 -04008850
8851 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8852 "2821 initialize iocb list %d.\n",
8853 phba->cfg_iocb_cnt*1024);
8854 error = lpfc_init_iocb_list(phba, phba->cfg_iocb_cnt*1024);
8855
James Smartda0436e2009-05-22 14:51:39 -04008856 if (error) {
8857 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8858 "1413 Failed to initialize iocb list.\n");
8859 goto out_unset_driver_resource_s4;
8860 }
8861
James Smart19ca7602010-11-20 23:11:55 -05008862 INIT_LIST_HEAD(&phba->active_rrq_list);
8863
James Smartda0436e2009-05-22 14:51:39 -04008864 /* Set up common device driver resources */
8865 error = lpfc_setup_driver_resource_phase2(phba);
8866 if (error) {
8867 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8868 "1414 Failed to set up driver resource.\n");
8869 goto out_free_iocb_list;
8870 }
8871
8872 /* Create SCSI host to the physical port */
8873 error = lpfc_create_shost(phba);
8874 if (error) {
8875 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8876 "1415 Failed to create scsi host.\n");
8877 goto out_unset_driver_resource;
8878 }
8879
8880 /* Configure sysfs attributes */
8881 vport = phba->pport;
8882 error = lpfc_alloc_sysfs_attr(vport);
8883 if (error) {
8884 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8885 "1416 Failed to allocate sysfs attr\n");
8886 goto out_destroy_shost;
8887 }
8888
James Smart6669f9b2009-10-02 15:16:45 -04008889 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -04008890 /* Now, trying to enable interrupt and bring up the device */
8891 cfg_mode = phba->cfg_use_msi;
8892 while (true) {
8893 /* Put device to a known state before enabling interrupt */
8894 lpfc_stop_port(phba);
8895 /* Configure and enable interrupt */
8896 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
8897 if (intr_mode == LPFC_INTR_ERROR) {
8898 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8899 "0426 Failed to enable interrupt.\n");
8900 error = -ENODEV;
8901 goto out_free_sysfs_attr;
8902 }
James Smart05580562011-05-24 11:40:48 -04008903 /* Default to single EQ for non-MSI-X */
James Smartdef9c7a2009-12-21 17:02:28 -05008904 if (phba->intr_type != MSIX)
James Smart05580562011-05-24 11:40:48 -04008905 adjusted_fcp_eq_count = 0;
8906 else if (phba->sli4_hba.msix_vec_nr <
8907 phba->cfg_fcp_eq_count + 1)
8908 adjusted_fcp_eq_count = phba->sli4_hba.msix_vec_nr - 1;
8909 else
8910 adjusted_fcp_eq_count = phba->cfg_fcp_eq_count;
8911 /* Free unused EQs */
8912 for (fcp_qidx = adjusted_fcp_eq_count;
8913 fcp_qidx < phba->cfg_fcp_eq_count;
8914 fcp_qidx++) {
8915 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_qidx]);
8916 /* do not delete the first fcp_cq */
8917 if (fcp_qidx)
8918 lpfc_sli4_queue_free(
8919 phba->sli4_hba.fcp_cq[fcp_qidx]);
8920 }
8921 phba->cfg_fcp_eq_count = adjusted_fcp_eq_count;
James Smartda0436e2009-05-22 14:51:39 -04008922 /* Set up SLI-4 HBA */
8923 if (lpfc_sli4_hba_setup(phba)) {
8924 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8925 "1421 Failed to set up hba\n");
8926 error = -ENODEV;
8927 goto out_disable_intr;
8928 }
8929
8930 /* Send NOP mbx cmds for non-INTx mode active interrupt test */
8931 if (intr_mode != 0)
8932 mcnt = lpfc_sli4_send_nop_mbox_cmds(phba,
8933 LPFC_ACT_INTR_CNT);
8934
8935 /* Check active interrupts received only for MSI/MSI-X */
8936 if (intr_mode == 0 ||
8937 phba->sli.slistat.sli_intr >= LPFC_ACT_INTR_CNT) {
8938 /* Log the current active interrupt mode */
8939 phba->intr_mode = intr_mode;
8940 lpfc_log_intr_mode(phba, intr_mode);
8941 break;
8942 }
8943 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8944 "0451 Configure interrupt mode (%d) "
8945 "failed active interrupt test.\n",
8946 intr_mode);
James Smart2fcee4b2010-12-15 17:57:46 -05008947 /* Unset the previous SLI-4 HBA setup. */
8948 /*
8949 * TODO: Is this operation compatible with IF TYPE 2
8950 * devices? All port state is deleted and cleared.
8951 */
James Smartda0436e2009-05-22 14:51:39 -04008952 lpfc_sli4_unset_hba(phba);
8953 /* Try next level of interrupt mode */
8954 cfg_mode = --intr_mode;
8955 }
8956
8957 /* Perform post initialization setup */
8958 lpfc_post_init_setup(phba);
8959
James Smart1c6834a2009-07-19 10:01:26 -04008960 /* Check if there are static vports to be created. */
8961 lpfc_create_static_vport(phba);
8962
James Smartda0436e2009-05-22 14:51:39 -04008963 return 0;
8964
8965out_disable_intr:
8966 lpfc_sli4_disable_intr(phba);
8967out_free_sysfs_attr:
8968 lpfc_free_sysfs_attr(vport);
8969out_destroy_shost:
8970 lpfc_destroy_shost(phba);
8971out_unset_driver_resource:
8972 lpfc_unset_driver_resource_phase2(phba);
8973out_free_iocb_list:
8974 lpfc_free_iocb_list(phba);
8975out_unset_driver_resource_s4:
8976 lpfc_sli4_driver_resource_unset(phba);
8977out_unset_pci_mem_s4:
8978 lpfc_sli4_pci_mem_unset(phba);
8979out_disable_pci_dev:
8980 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04008981 if (shost)
8982 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -04008983out_free_phba:
8984 lpfc_hba_free(phba);
8985 return error;
8986}
8987
8988/**
8989 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
8990 * @pdev: pointer to PCI device
8991 *
8992 * This routine is called from the kernel's PCI subsystem to device with
8993 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
8994 * removed from PCI bus, it performs all the necessary cleanup for the HBA
8995 * device to be removed from the PCI subsystem properly.
8996 **/
8997static void __devexit
8998lpfc_pci_remove_one_s4(struct pci_dev *pdev)
8999{
9000 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9001 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
9002 struct lpfc_vport **vports;
9003 struct lpfc_hba *phba = vport->phba;
9004 int i;
9005
9006 /* Mark the device unloading flag */
9007 spin_lock_irq(&phba->hbalock);
9008 vport->load_flag |= FC_UNLOADING;
9009 spin_unlock_irq(&phba->hbalock);
9010
9011 /* Free the HBA sysfs attributes */
9012 lpfc_free_sysfs_attr(vport);
9013
9014 /* Release all the vports against this physical port */
9015 vports = lpfc_create_vport_work_array(phba);
9016 if (vports != NULL)
9017 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
9018 fc_vport_terminate(vports[i]->fc_vport);
9019 lpfc_destroy_vport_work_array(phba, vports);
9020
9021 /* Remove FC host and then SCSI host with the physical port */
9022 fc_remove_host(shost);
9023 scsi_remove_host(shost);
9024
9025 /* Perform cleanup on the physical port */
9026 lpfc_cleanup(vport);
9027
9028 /*
9029 * Bring down the SLI Layer. This step disables all interrupts,
9030 * clears the rings, discards all mailbox commands, and resets
9031 * the HBA FCoE function.
9032 */
9033 lpfc_debugfs_terminate(vport);
9034 lpfc_sli4_hba_unset(phba);
9035
9036 spin_lock_irq(&phba->hbalock);
9037 list_del_init(&vport->listentry);
9038 spin_unlock_irq(&phba->hbalock);
9039
James Smart3677a3a2010-09-29 11:19:14 -04009040 /* Perform scsi free before driver resource_unset since scsi
James Smartda0436e2009-05-22 14:51:39 -04009041 * buffers are released to their corresponding pools here.
9042 */
9043 lpfc_scsi_free(phba);
9044 lpfc_sli4_driver_resource_unset(phba);
9045
9046 /* Unmap adapter Control and Doorbell registers */
9047 lpfc_sli4_pci_mem_unset(phba);
9048
9049 /* Release PCI resources and disable device's PCI function */
9050 scsi_host_put(shost);
9051 lpfc_disable_pci_dev(phba);
9052
9053 /* Finally, free the driver's device data structure */
9054 lpfc_hba_free(phba);
9055
9056 return;
9057}
9058
9059/**
9060 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
9061 * @pdev: pointer to PCI device
9062 * @msg: power management message
9063 *
9064 * This routine is called from the kernel's PCI subsystem to support system
9065 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
9066 * this method, it quiesces the device by stopping the driver's worker
9067 * thread for the device, turning off device's interrupt and DMA, and bring
9068 * the device offline. Note that as the driver implements the minimum PM
9069 * requirements to a power-aware driver's PM support for suspend/resume -- all
9070 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
9071 * method call will be treated as SUSPEND and the driver will fully
9072 * reinitialize its device during resume() method call, the driver will set
9073 * device to PCI_D3hot state in PCI config space instead of setting it
9074 * according to the @msg provided by the PM.
9075 *
9076 * Return code
9077 * 0 - driver suspended the device
9078 * Error otherwise
9079 **/
9080static int
9081lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
9082{
9083 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9084 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9085
9086 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart75baf692010-06-08 18:31:21 -04009087 "2843 PCI device Power Management suspend.\n");
James Smartda0436e2009-05-22 14:51:39 -04009088
9089 /* Bring down the device */
9090 lpfc_offline_prep(phba);
9091 lpfc_offline(phba);
9092 kthread_stop(phba->worker_thread);
9093
9094 /* Disable interrupt from device */
9095 lpfc_sli4_disable_intr(phba);
9096
9097 /* Save device state to PCI config space */
9098 pci_save_state(pdev);
9099 pci_set_power_state(pdev, PCI_D3hot);
9100
9101 return 0;
9102}
9103
9104/**
9105 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
9106 * @pdev: pointer to PCI device
9107 *
9108 * This routine is called from the kernel's PCI subsystem to support system
9109 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
9110 * this method, it restores the device's PCI config space state and fully
9111 * reinitializes the device and brings it online. Note that as the driver
9112 * implements the minimum PM requirements to a power-aware driver's PM for
9113 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
9114 * to the suspend() method call will be treated as SUSPEND and the driver
9115 * will fully reinitialize its device during resume() method call, the device
9116 * will be set to PCI_D0 directly in PCI config space before restoring the
9117 * state.
9118 *
9119 * Return code
9120 * 0 - driver suspended the device
9121 * Error otherwise
9122 **/
9123static int
9124lpfc_pci_resume_one_s4(struct pci_dev *pdev)
9125{
9126 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9127 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9128 uint32_t intr_mode;
9129 int error;
9130
9131 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9132 "0292 PCI device Power Management resume.\n");
9133
9134 /* Restore device state from PCI config space */
9135 pci_set_power_state(pdev, PCI_D0);
9136 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05009137
9138 /*
9139 * As the new kernel behavior of pci_restore_state() API call clears
9140 * device saved_state flag, need to save the restored state again.
9141 */
9142 pci_save_state(pdev);
9143
James Smartda0436e2009-05-22 14:51:39 -04009144 if (pdev->is_busmaster)
9145 pci_set_master(pdev);
9146
9147 /* Startup the kernel thread for this host adapter. */
9148 phba->worker_thread = kthread_run(lpfc_do_work, phba,
9149 "lpfc_worker_%d", phba->brd_no);
9150 if (IS_ERR(phba->worker_thread)) {
9151 error = PTR_ERR(phba->worker_thread);
9152 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9153 "0293 PM resume failed to start worker "
9154 "thread: error=x%x.\n", error);
9155 return error;
9156 }
9157
9158 /* Configure and enable interrupt */
9159 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
9160 if (intr_mode == LPFC_INTR_ERROR) {
9161 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9162 "0294 PM resume Failed to enable interrupt\n");
9163 return -EIO;
9164 } else
9165 phba->intr_mode = intr_mode;
9166
9167 /* Restart HBA and bring it online */
9168 lpfc_sli_brdrestart(phba);
9169 lpfc_online(phba);
9170
9171 /* Log the current active interrupt mode */
9172 lpfc_log_intr_mode(phba, phba->intr_mode);
9173
9174 return 0;
9175}
9176
9177/**
James Smart75baf692010-06-08 18:31:21 -04009178 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
9179 * @phba: pointer to lpfc hba data structure.
9180 *
9181 * This routine is called to prepare the SLI4 device for PCI slot recover. It
9182 * aborts all the outstanding SCSI I/Os to the pci device.
9183 **/
9184static void
9185lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
9186{
9187 struct lpfc_sli *psli = &phba->sli;
9188 struct lpfc_sli_ring *pring;
9189
9190 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9191 "2828 PCI channel I/O abort preparing for recovery\n");
9192 /*
9193 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
9194 * and let the SCSI mid-layer to retry them to recover.
9195 */
9196 pring = &psli->ring[psli->fcp_ring];
9197 lpfc_sli_abort_iocb_ring(phba, pring);
9198}
9199
9200/**
9201 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
9202 * @phba: pointer to lpfc hba data structure.
9203 *
9204 * This routine is called to prepare the SLI4 device for PCI slot reset. It
9205 * disables the device interrupt and pci device, and aborts the internal FCP
9206 * pending I/Os.
9207 **/
9208static void
9209lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
9210{
9211 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9212 "2826 PCI channel disable preparing for reset\n");
9213
9214 /* Block any management I/Os to the device */
9215 lpfc_block_mgmt_io(phba);
9216
9217 /* Block all SCSI devices' I/Os on the host */
9218 lpfc_scsi_dev_block(phba);
9219
9220 /* stop all timers */
9221 lpfc_stop_hba_timers(phba);
9222
9223 /* Disable interrupt and pci device */
9224 lpfc_sli4_disable_intr(phba);
9225 pci_disable_device(phba->pcidev);
9226
9227 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
9228 lpfc_sli_flush_fcp_rings(phba);
9229}
9230
9231/**
9232 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
9233 * @phba: pointer to lpfc hba data structure.
9234 *
9235 * This routine is called to prepare the SLI4 device for PCI slot permanently
9236 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
9237 * pending I/Os.
9238 **/
9239static void
9240lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
9241{
9242 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9243 "2827 PCI channel permanent disable for failure\n");
9244
9245 /* Block all SCSI devices' I/Os on the host */
9246 lpfc_scsi_dev_block(phba);
9247
9248 /* stop all timers */
9249 lpfc_stop_hba_timers(phba);
9250
9251 /* Clean up all driver's outstanding SCSI I/Os */
9252 lpfc_sli_flush_fcp_rings(phba);
9253}
9254
9255/**
James Smartda0436e2009-05-22 14:51:39 -04009256 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
9257 * @pdev: pointer to PCI device.
9258 * @state: the current PCI connection state.
9259 *
9260 * This routine is called from the PCI subsystem for error handling to device
9261 * with SLI-4 interface spec. This function is called by the PCI subsystem
9262 * after a PCI bus error affecting this device has been detected. When this
9263 * function is invoked, it will need to stop all the I/Os and interrupt(s)
9264 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
9265 * for the PCI subsystem to perform proper recovery as desired.
9266 *
9267 * Return codes
9268 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
9269 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
9270 **/
9271static pci_ers_result_t
9272lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
9273{
James Smart75baf692010-06-08 18:31:21 -04009274 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9275 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9276
9277 switch (state) {
9278 case pci_channel_io_normal:
9279 /* Non-fatal error, prepare for recovery */
9280 lpfc_sli4_prep_dev_for_recover(phba);
9281 return PCI_ERS_RESULT_CAN_RECOVER;
9282 case pci_channel_io_frozen:
9283 /* Fatal error, prepare for slot reset */
9284 lpfc_sli4_prep_dev_for_reset(phba);
9285 return PCI_ERS_RESULT_NEED_RESET;
9286 case pci_channel_io_perm_failure:
9287 /* Permanent failure, prepare for device down */
9288 lpfc_sli4_prep_dev_for_perm_failure(phba);
9289 return PCI_ERS_RESULT_DISCONNECT;
9290 default:
9291 /* Unknown state, prepare and request slot reset */
9292 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9293 "2825 Unknown PCI error state: x%x\n", state);
9294 lpfc_sli4_prep_dev_for_reset(phba);
9295 return PCI_ERS_RESULT_NEED_RESET;
9296 }
James Smartda0436e2009-05-22 14:51:39 -04009297}
9298
9299/**
9300 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
9301 * @pdev: pointer to PCI device.
9302 *
9303 * This routine is called from the PCI subsystem for error handling to device
9304 * with SLI-4 interface spec. It is called after PCI bus has been reset to
9305 * restart the PCI card from scratch, as if from a cold-boot. During the
9306 * PCI subsystem error recovery, after the driver returns
9307 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
9308 * recovery and then call this routine before calling the .resume method to
9309 * recover the device. This function will initialize the HBA device, enable
9310 * the interrupt, but it will just put the HBA to offline state without
9311 * passing any I/O traffic.
9312 *
9313 * Return codes
9314 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
9315 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
9316 */
9317static pci_ers_result_t
9318lpfc_io_slot_reset_s4(struct pci_dev *pdev)
9319{
James Smart75baf692010-06-08 18:31:21 -04009320 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9321 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9322 struct lpfc_sli *psli = &phba->sli;
9323 uint32_t intr_mode;
9324
9325 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
9326 if (pci_enable_device_mem(pdev)) {
9327 printk(KERN_ERR "lpfc: Cannot re-enable "
9328 "PCI device after reset.\n");
9329 return PCI_ERS_RESULT_DISCONNECT;
9330 }
9331
9332 pci_restore_state(pdev);
9333 if (pdev->is_busmaster)
9334 pci_set_master(pdev);
9335
9336 spin_lock_irq(&phba->hbalock);
9337 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
9338 spin_unlock_irq(&phba->hbalock);
9339
9340 /* Configure and enable interrupt */
9341 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
9342 if (intr_mode == LPFC_INTR_ERROR) {
9343 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9344 "2824 Cannot re-enable interrupt after "
9345 "slot reset.\n");
9346 return PCI_ERS_RESULT_DISCONNECT;
9347 } else
9348 phba->intr_mode = intr_mode;
9349
9350 /* Log the current active interrupt mode */
9351 lpfc_log_intr_mode(phba, phba->intr_mode);
9352
James Smartda0436e2009-05-22 14:51:39 -04009353 return PCI_ERS_RESULT_RECOVERED;
9354}
9355
9356/**
9357 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
9358 * @pdev: pointer to PCI device
9359 *
9360 * This routine is called from the PCI subsystem for error handling to device
9361 * with SLI-4 interface spec. It is called when kernel error recovery tells
9362 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
9363 * error recovery. After this call, traffic can start to flow from this device
9364 * again.
9365 **/
9366static void
9367lpfc_io_resume_s4(struct pci_dev *pdev)
9368{
James Smart75baf692010-06-08 18:31:21 -04009369 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9370 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9371
9372 /*
9373 * In case of slot reset, as function reset is performed through
9374 * mailbox command which needs DMA to be enabled, this operation
9375 * has to be moved to the io resume phase. Taking device offline
9376 * will perform the necessary cleanup.
9377 */
9378 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
9379 /* Perform device reset */
9380 lpfc_offline_prep(phba);
9381 lpfc_offline(phba);
9382 lpfc_sli_brdrestart(phba);
9383 /* Bring the device back online */
9384 lpfc_online(phba);
9385 }
9386
9387 /* Clean up Advanced Error Reporting (AER) if needed */
9388 if (phba->hba_flag & HBA_AER_ENABLED)
9389 pci_cleanup_aer_uncorrect_error_status(pdev);
James Smartda0436e2009-05-22 14:51:39 -04009390}
9391
9392/**
James Smart3772a992009-05-22 14:50:54 -04009393 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
9394 * @pdev: pointer to PCI device
9395 * @pid: pointer to PCI device identifier
9396 *
9397 * This routine is to be registered to the kernel's PCI subsystem. When an
9398 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
9399 * at PCI device-specific information of the device and driver to see if the
9400 * driver state that it can support this kind of device. If the match is
9401 * successful, the driver core invokes this routine. This routine dispatches
9402 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
9403 * do all the initialization that it needs to do to handle the HBA device
9404 * properly.
9405 *
9406 * Return code
9407 * 0 - driver can claim the device
9408 * negative value - driver can not claim the device
9409 **/
9410static int __devinit
9411lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
9412{
9413 int rc;
James Smart8fa38512009-07-19 10:01:03 -04009414 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -04009415
James Smart28baac72010-02-12 14:42:03 -05009416 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -04009417 return -ENODEV;
9418
James Smart8fa38512009-07-19 10:01:03 -04009419 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -05009420 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -04009421 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04009422 else
James Smart3772a992009-05-22 14:50:54 -04009423 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04009424
James Smart3772a992009-05-22 14:50:54 -04009425 return rc;
9426}
9427
9428/**
9429 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
9430 * @pdev: pointer to PCI device
9431 *
9432 * This routine is to be registered to the kernel's PCI subsystem. When an
9433 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
9434 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
9435 * remove routine, which will perform all the necessary cleanup for the
9436 * device to be removed from the PCI subsystem properly.
9437 **/
9438static void __devexit
9439lpfc_pci_remove_one(struct pci_dev *pdev)
9440{
9441 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9442 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9443
9444 switch (phba->pci_dev_grp) {
9445 case LPFC_PCI_DEV_LP:
9446 lpfc_pci_remove_one_s3(pdev);
9447 break;
James Smartda0436e2009-05-22 14:51:39 -04009448 case LPFC_PCI_DEV_OC:
9449 lpfc_pci_remove_one_s4(pdev);
9450 break;
James Smart3772a992009-05-22 14:50:54 -04009451 default:
9452 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9453 "1424 Invalid PCI device group: 0x%x\n",
9454 phba->pci_dev_grp);
9455 break;
9456 }
9457 return;
9458}
9459
9460/**
9461 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
9462 * @pdev: pointer to PCI device
9463 * @msg: power management message
9464 *
9465 * This routine is to be registered to the kernel's PCI subsystem to support
9466 * system Power Management (PM). When PM invokes this method, it dispatches
9467 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
9468 * suspend the device.
9469 *
9470 * Return code
9471 * 0 - driver suspended the device
9472 * Error otherwise
9473 **/
9474static int
9475lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
9476{
9477 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9478 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9479 int rc = -ENODEV;
9480
9481 switch (phba->pci_dev_grp) {
9482 case LPFC_PCI_DEV_LP:
9483 rc = lpfc_pci_suspend_one_s3(pdev, msg);
9484 break;
James Smartda0436e2009-05-22 14:51:39 -04009485 case LPFC_PCI_DEV_OC:
9486 rc = lpfc_pci_suspend_one_s4(pdev, msg);
9487 break;
James Smart3772a992009-05-22 14:50:54 -04009488 default:
9489 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9490 "1425 Invalid PCI device group: 0x%x\n",
9491 phba->pci_dev_grp);
9492 break;
9493 }
9494 return rc;
9495}
9496
9497/**
9498 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
9499 * @pdev: pointer to PCI device
9500 *
9501 * This routine is to be registered to the kernel's PCI subsystem to support
9502 * system Power Management (PM). When PM invokes this method, it dispatches
9503 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
9504 * resume the device.
9505 *
9506 * Return code
9507 * 0 - driver suspended the device
9508 * Error otherwise
9509 **/
9510static int
9511lpfc_pci_resume_one(struct pci_dev *pdev)
9512{
9513 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9514 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9515 int rc = -ENODEV;
9516
9517 switch (phba->pci_dev_grp) {
9518 case LPFC_PCI_DEV_LP:
9519 rc = lpfc_pci_resume_one_s3(pdev);
9520 break;
James Smartda0436e2009-05-22 14:51:39 -04009521 case LPFC_PCI_DEV_OC:
9522 rc = lpfc_pci_resume_one_s4(pdev);
9523 break;
James Smart3772a992009-05-22 14:50:54 -04009524 default:
9525 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9526 "1426 Invalid PCI device group: 0x%x\n",
9527 phba->pci_dev_grp);
9528 break;
9529 }
9530 return rc;
9531}
9532
9533/**
9534 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
9535 * @pdev: pointer to PCI device.
9536 * @state: the current PCI connection state.
9537 *
9538 * This routine is registered to the PCI subsystem for error handling. This
9539 * function is called by the PCI subsystem after a PCI bus error affecting
9540 * this device has been detected. When this routine is invoked, it dispatches
9541 * the action to the proper SLI-3 or SLI-4 device error detected handling
9542 * routine, which will perform the proper error detected operation.
9543 *
9544 * Return codes
9545 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
9546 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
9547 **/
9548static pci_ers_result_t
9549lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
9550{
9551 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9552 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9553 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
9554
9555 switch (phba->pci_dev_grp) {
9556 case LPFC_PCI_DEV_LP:
9557 rc = lpfc_io_error_detected_s3(pdev, state);
9558 break;
James Smartda0436e2009-05-22 14:51:39 -04009559 case LPFC_PCI_DEV_OC:
9560 rc = lpfc_io_error_detected_s4(pdev, state);
9561 break;
James Smart3772a992009-05-22 14:50:54 -04009562 default:
9563 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9564 "1427 Invalid PCI device group: 0x%x\n",
9565 phba->pci_dev_grp);
9566 break;
9567 }
9568 return rc;
9569}
9570
9571/**
9572 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
9573 * @pdev: pointer to PCI device.
9574 *
9575 * This routine is registered to the PCI subsystem for error handling. This
9576 * function is called after PCI bus has been reset to restart the PCI card
9577 * from scratch, as if from a cold-boot. When this routine is invoked, it
9578 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
9579 * routine, which will perform the proper device reset.
9580 *
9581 * Return codes
9582 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
9583 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
9584 **/
9585static pci_ers_result_t
9586lpfc_io_slot_reset(struct pci_dev *pdev)
9587{
9588 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9589 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9590 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
9591
9592 switch (phba->pci_dev_grp) {
9593 case LPFC_PCI_DEV_LP:
9594 rc = lpfc_io_slot_reset_s3(pdev);
9595 break;
James Smartda0436e2009-05-22 14:51:39 -04009596 case LPFC_PCI_DEV_OC:
9597 rc = lpfc_io_slot_reset_s4(pdev);
9598 break;
James Smart3772a992009-05-22 14:50:54 -04009599 default:
9600 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9601 "1428 Invalid PCI device group: 0x%x\n",
9602 phba->pci_dev_grp);
9603 break;
9604 }
9605 return rc;
9606}
9607
9608/**
9609 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
9610 * @pdev: pointer to PCI device
9611 *
9612 * This routine is registered to the PCI subsystem for error handling. It
9613 * is called when kernel error recovery tells the lpfc driver that it is
9614 * OK to resume normal PCI operation after PCI bus error recovery. When
9615 * this routine is invoked, it dispatches the action to the proper SLI-3
9616 * or SLI-4 device io_resume routine, which will resume the device operation.
9617 **/
9618static void
9619lpfc_io_resume(struct pci_dev *pdev)
9620{
9621 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9622 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9623
9624 switch (phba->pci_dev_grp) {
9625 case LPFC_PCI_DEV_LP:
9626 lpfc_io_resume_s3(pdev);
9627 break;
James Smartda0436e2009-05-22 14:51:39 -04009628 case LPFC_PCI_DEV_OC:
9629 lpfc_io_resume_s4(pdev);
9630 break;
James Smart3772a992009-05-22 14:50:54 -04009631 default:
9632 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9633 "1429 Invalid PCI device group: 0x%x\n",
9634 phba->pci_dev_grp);
9635 break;
9636 }
9637 return;
9638}
9639
dea31012005-04-17 16:05:31 -05009640static struct pci_device_id lpfc_id_table[] = {
9641 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
9642 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04009643 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
9644 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05009645 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
9646 PCI_ANY_ID, PCI_ANY_ID, },
9647 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
9648 PCI_ANY_ID, PCI_ANY_ID, },
9649 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
9650 PCI_ANY_ID, PCI_ANY_ID, },
9651 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
9652 PCI_ANY_ID, PCI_ANY_ID, },
9653 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
9654 PCI_ANY_ID, PCI_ANY_ID, },
9655 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
9656 PCI_ANY_ID, PCI_ANY_ID, },
9657 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
9658 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05009659 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
9660 PCI_ANY_ID, PCI_ANY_ID, },
9661 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
9662 PCI_ANY_ID, PCI_ANY_ID, },
9663 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
9664 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05009665 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
9666 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05009667 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
9668 PCI_ANY_ID, PCI_ANY_ID, },
9669 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
9670 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05009671 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
9672 PCI_ANY_ID, PCI_ANY_ID, },
9673 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
9674 PCI_ANY_ID, PCI_ANY_ID, },
9675 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
9676 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -04009677 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
9678 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05009679 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
9680 PCI_ANY_ID, PCI_ANY_ID, },
9681 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
9682 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05009683 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
9684 PCI_ANY_ID, PCI_ANY_ID, },
9685 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
9686 PCI_ANY_ID, PCI_ANY_ID, },
9687 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
9688 PCI_ANY_ID, PCI_ANY_ID, },
9689 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
9690 PCI_ANY_ID, PCI_ANY_ID, },
9691 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
9692 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05009693 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
9694 PCI_ANY_ID, PCI_ANY_ID, },
9695 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
9696 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -04009697 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
9698 PCI_ANY_ID, PCI_ANY_ID, },
9699 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
9700 PCI_ANY_ID, PCI_ANY_ID, },
9701 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
9702 PCI_ANY_ID, PCI_ANY_ID, },
9703 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
9704 PCI_ANY_ID, PCI_ANY_ID, },
9705 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
9706 PCI_ANY_ID, PCI_ANY_ID, },
9707 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
9708 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -04009709 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
9710 PCI_ANY_ID, PCI_ANY_ID, },
9711 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
9712 PCI_ANY_ID, PCI_ANY_ID, },
9713 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
9714 PCI_ANY_ID, PCI_ANY_ID, },
James Smart3772a992009-05-22 14:50:54 -04009715 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
9716 PCI_ANY_ID, PCI_ANY_ID, },
James Smarta747c9c2009-11-18 15:41:10 -05009717 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
9718 PCI_ANY_ID, PCI_ANY_ID, },
9719 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
James Smart6669f9b2009-10-02 15:16:45 -04009720 PCI_ANY_ID, PCI_ANY_ID, },
James Smart98fc5dd2010-06-07 15:24:29 -04009721 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BALIUS,
9722 PCI_ANY_ID, PCI_ANY_ID, },
James Smart085c6472010-11-20 23:11:37 -05009723 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC,
9724 PCI_ANY_ID, PCI_ANY_ID, },
9725 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE,
9726 PCI_ANY_ID, PCI_ANY_ID, },
James Smartc0c11512011-05-24 11:41:34 -04009727 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC_VF,
9728 PCI_ANY_ID, PCI_ANY_ID, },
9729 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE_VF,
9730 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05009731 { 0 }
9732};
9733
9734MODULE_DEVICE_TABLE(pci, lpfc_id_table);
9735
Linas Vepstas8d63f372007-02-14 14:28:36 -06009736static struct pci_error_handlers lpfc_err_handler = {
9737 .error_detected = lpfc_io_error_detected,
9738 .slot_reset = lpfc_io_slot_reset,
9739 .resume = lpfc_io_resume,
9740};
9741
dea31012005-04-17 16:05:31 -05009742static struct pci_driver lpfc_driver = {
9743 .name = LPFC_DRIVER_NAME,
9744 .id_table = lpfc_id_table,
9745 .probe = lpfc_pci_probe_one,
9746 .remove = __devexit_p(lpfc_pci_remove_one),
James Smart3a55b532008-12-04 22:38:54 -05009747 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -04009748 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -05009749 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -05009750};
9751
James Smarte59058c2008-08-24 21:49:00 -04009752/**
James Smart3621a712009-04-06 18:47:14 -04009753 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -04009754 *
9755 * This routine is to be invoked when the lpfc module is loaded into the
9756 * kernel. The special kernel macro module_init() is used to indicate the
9757 * role of this routine to the kernel as lpfc module entry point.
9758 *
9759 * Return codes
9760 * 0 - successful
9761 * -ENOMEM - FC attach transport failed
9762 * all others - failed
9763 */
dea31012005-04-17 16:05:31 -05009764static int __init
9765lpfc_init(void)
9766{
9767 int error = 0;
9768
9769 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04009770 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -05009771
James Smart7ee5d432007-10-27 13:37:17 -04009772 if (lpfc_enable_npiv) {
9773 lpfc_transport_functions.vport_create = lpfc_vport_create;
9774 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
9775 }
dea31012005-04-17 16:05:31 -05009776 lpfc_transport_template =
9777 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -04009778 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -05009779 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -04009780 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -04009781 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -04009782 fc_attach_transport(&lpfc_vport_transport_functions);
9783 if (lpfc_vport_transport_template == NULL) {
9784 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -04009785 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -04009786 }
James Smart7ee5d432007-10-27 13:37:17 -04009787 }
dea31012005-04-17 16:05:31 -05009788 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -05009789 if (error) {
dea31012005-04-17 16:05:31 -05009790 fc_release_transport(lpfc_transport_template);
James Smartd7c255b2008-08-24 21:50:00 -04009791 if (lpfc_enable_npiv)
9792 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -05009793 }
dea31012005-04-17 16:05:31 -05009794
9795 return error;
9796}
9797
James Smarte59058c2008-08-24 21:49:00 -04009798/**
James Smart3621a712009-04-06 18:47:14 -04009799 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -04009800 *
9801 * This routine is invoked when the lpfc module is removed from the kernel.
9802 * The special kernel macro module_exit() is used to indicate the role of
9803 * this routine to the kernel as lpfc module exit point.
9804 */
dea31012005-04-17 16:05:31 -05009805static void __exit
9806lpfc_exit(void)
9807{
9808 pci_unregister_driver(&lpfc_driver);
9809 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -04009810 if (lpfc_enable_npiv)
9811 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -05009812 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04009813 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
9814 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -05009815 (1L << _dump_buf_data_order), _dump_buf_data);
9816 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
9817 }
9818
9819 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04009820 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
9821 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -05009822 (1L << _dump_buf_dif_order), _dump_buf_dif);
9823 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
9824 }
dea31012005-04-17 16:05:31 -05009825}
9826
9827module_init(lpfc_init);
9828module_exit(lpfc_exit);
9829MODULE_LICENSE("GPL");
9830MODULE_DESCRIPTION(LPFC_MODULE_DESC);
9831MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
9832MODULE_VERSION("0:" LPFC_DRIVER_VERSION);