blob: 55bc4fc7376f5a753f08e2d8aa44b3846cc377e6 [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>
Paul Gortmakeracf3368f2011-05-27 09:47:43 -040027#include <linux/module.h>
dea31012005-04-17 16:05:31 -050028#include <linux/kthread.h>
29#include <linux/pci.h>
30#include <linux/spinlock.h>
James Smart92d7f7b2007-06-17 19:56:38 -050031#include <linux/ctype.h>
James Smart0d878412009-10-02 15:16:56 -040032#include <linux/aer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
James Smart52d52442011-05-24 11:42:45 -040034#include <linux/firmware.h>
dea31012005-04-17 16:05:31 -050035
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040036#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050037#include <scsi/scsi_device.h>
38#include <scsi/scsi_host.h>
39#include <scsi/scsi_transport_fc.h>
40
James Smartda0436e2009-05-22 14:51:39 -040041#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050042#include "lpfc_hw.h"
43#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040044#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040045#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050046#include "lpfc_disc.h"
47#include "lpfc_scsi.h"
48#include "lpfc.h"
49#include "lpfc_logmsg.h"
50#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050051#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050052#include "lpfc_version.h"
53
James Smart81301a92008-12-04 22:39:46 -050054char *_dump_buf_data;
55unsigned long _dump_buf_data_order;
56char *_dump_buf_dif;
57unsigned long _dump_buf_dif_order;
58spinlock_t _dump_buf_lock;
59
dea31012005-04-17 16:05:31 -050060static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
61static int lpfc_post_rcv_buf(struct lpfc_hba *);
James Smart5350d872011-10-10 21:33:49 -040062static int lpfc_sli4_queue_verify(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040063static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
64static int lpfc_setup_endian_order(struct lpfc_hba *);
65static int lpfc_sli4_read_config(struct lpfc_hba *);
66static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
67static void lpfc_free_sgl_list(struct lpfc_hba *);
68static int lpfc_init_sgl_list(struct lpfc_hba *);
69static int lpfc_init_active_sgl_array(struct lpfc_hba *);
70static void lpfc_free_active_sgl(struct lpfc_hba *);
71static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
72static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
73static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
74static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
75static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
dea31012005-04-17 16:05:31 -050076
77static struct scsi_transport_template *lpfc_transport_template = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050078static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea31012005-04-17 16:05:31 -050079static DEFINE_IDR(lpfc_hba_index);
80
James Smarte59058c2008-08-24 21:49:00 -040081/**
James Smart3621a712009-04-06 18:47:14 -040082 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
James Smarte59058c2008-08-24 21:49:00 -040083 * @phba: pointer to lpfc hba data structure.
84 *
85 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
86 * mailbox command. It retrieves the revision information from the HBA and
87 * collects the Vital Product Data (VPD) about the HBA for preparing the
88 * configuration of the HBA.
89 *
90 * Return codes:
91 * 0 - success.
92 * -ERESTART - requests the SLI layer to reset the HBA and try again.
93 * Any other value - indicates an error.
94 **/
dea31012005-04-17 16:05:31 -050095int
James Smart2e0fef82007-06-17 19:56:36 -050096lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050097{
98 lpfc_vpd_t *vp = &phba->vpd;
99 int i = 0, rc;
100 LPFC_MBOXQ_t *pmb;
101 MAILBOX_t *mb;
102 char *lpfc_vpd_data = NULL;
103 uint16_t offset = 0;
104 static char licensed[56] =
105 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -0400106 static int init_key = 1;
dea31012005-04-17 16:05:31 -0500107
108 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
109 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500110 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500111 return -ENOMEM;
112 }
113
James Smart04c68492009-05-22 14:52:52 -0400114 mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -0500115 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500116
117 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -0400118 if (init_key) {
119 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -0500120
James Smart65a29c12006-07-06 15:50:50 -0400121 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
122 *ptext = cpu_to_be32(*ptext);
123 init_key = 0;
124 }
dea31012005-04-17 16:05:31 -0500125
126 lpfc_read_nv(phba, pmb);
127 memset((char*)mb->un.varRDnvp.rsvd3, 0,
128 sizeof (mb->un.varRDnvp.rsvd3));
129 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
130 sizeof (licensed));
131
132 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
133
134 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500135 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smarte8b62012007-08-02 11:10:09 -0400136 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500137 "error, mbxCmd x%x READ_NVPARM, "
138 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500139 mb->mbxCommand, mb->mbxStatus);
140 mempool_free(pmb, phba->mbox_mem_pool);
141 return -ERESTART;
142 }
143 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500144 sizeof(phba->wwnn));
145 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
146 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500147 }
148
James Smart92d7f7b2007-06-17 19:56:38 -0500149 phba->sli3_options = 0x0;
150
dea31012005-04-17 16:05:31 -0500151 /* Setup and issue mailbox READ REV command */
152 lpfc_read_rev(phba, pmb);
153 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
154 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500155 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400156 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500157 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500158 mb->mbxCommand, mb->mbxStatus);
159 mempool_free( pmb, phba->mbox_mem_pool);
160 return -ERESTART;
161 }
162
James Smart92d7f7b2007-06-17 19:56:38 -0500163
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500164 /*
165 * The value of rr must be 1 since the driver set the cv field to 1.
166 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500167 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500168 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500169 vp->rev.rBit = 0;
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500170 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400171 "0440 Adapter failed to init, READ_REV has "
172 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500173 mempool_free(pmb, phba->mbox_mem_pool);
174 return -ERESTART;
dea31012005-04-17 16:05:31 -0500175 }
176
James Smart495a7142008-06-14 22:52:59 -0400177 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
178 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -0500179 return -EINVAL;
James Smart495a7142008-06-14 22:52:59 -0400180 }
James Smarted957682007-06-17 19:56:37 -0500181
dea31012005-04-17 16:05:31 -0500182 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500183 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500184 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500185 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
186 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
187 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
188 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500189 vp->rev.biuRev = mb->un.varRdRev.biuRev;
190 vp->rev.smRev = mb->un.varRdRev.smRev;
191 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
192 vp->rev.endecRev = mb->un.varRdRev.endecRev;
193 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
194 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
195 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
196 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
197 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
198 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
199
James Smart92d7f7b2007-06-17 19:56:38 -0500200 /* If the sli feature level is less then 9, we must
201 * tear down all RPIs and VPIs on link down if NPIV
202 * is enabled.
203 */
204 if (vp->rev.feaLevelHigh < 9)
205 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
206
dea31012005-04-17 16:05:31 -0500207 if (lpfc_is_LC_HBA(phba->pcidev->device))
208 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
209 sizeof (phba->RandomData));
210
dea31012005-04-17 16:05:31 -0500211 /* Get adapter VPD information */
dea31012005-04-17 16:05:31 -0500212 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
213 if (!lpfc_vpd_data)
James Smartd7c255b2008-08-24 21:50:00 -0400214 goto out_free_mbox;
dea31012005-04-17 16:05:31 -0500215 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 */
James Smarta0c87cb2009-07-19 10:01:10 -0400605 phba->link_state = LPFC_HBA_ERROR;
606 if (rc != MBX_BUSY)
607 mempool_free(pmb, phba->mbox_mem_pool);
608 return -EIO;
609 }
dea31012005-04-17 16:05:31 -0500610 }
611 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400612 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500613 if (!pmb) {
614 phba->link_state = LPFC_HBA_ERROR;
615 return -ENOMEM;
616 }
617
James Smart57127f12007-10-27 13:37:05 -0400618 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
619 pmb->mbox_cmpl = lpfc_config_async_cmpl;
620 pmb->vport = phba->pport;
621 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500622
James Smart57127f12007-10-27 13:37:05 -0400623 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
624 lpfc_printf_log(phba,
625 KERN_ERR,
626 LOG_INIT,
627 "0456 Adapter failed to issue "
James Smarte4e74272009-07-19 10:01:38 -0400628 "ASYNCEVT_ENABLE mbox status x%x\n",
James Smart57127f12007-10-27 13:37:05 -0400629 rc);
630 mempool_free(pmb, phba->mbox_mem_pool);
631 }
James Smart97207482008-12-04 22:39:19 -0500632
633 /* Get Option rom version */
634 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500635 if (!pmb) {
636 phba->link_state = LPFC_HBA_ERROR;
637 return -ENOMEM;
638 }
639
James Smart97207482008-12-04 22:39:19 -0500640 lpfc_dump_wakeup_param(phba, pmb);
641 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
642 pmb->vport = phba->pport;
643 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
644
645 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
646 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
James Smarte4e74272009-07-19 10:01:38 -0400647 "to get Option ROM version status x%x\n", rc);
James Smart97207482008-12-04 22:39:19 -0500648 mempool_free(pmb, phba->mbox_mem_pool);
649 }
650
James Smartd7c255b2008-08-24 21:50:00 -0400651 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400652}
653
James Smarte59058c2008-08-24 21:49:00 -0400654/**
James Smart84d1b002010-02-12 14:42:33 -0500655 * lpfc_hba_init_link - Initialize the FC link
656 * @phba: pointer to lpfc hba data structure.
James Smart6e7288d2010-06-07 15:23:35 -0400657 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500658 *
659 * This routine will issue the INIT_LINK mailbox command call.
660 * It is available to other drivers through the lpfc_hba data
661 * structure for use as a delayed link up mechanism with the
662 * module parameter lpfc_suppress_link_up.
663 *
664 * Return code
665 * 0 - success
666 * Any other value - error
667 **/
668int
James Smart6e7288d2010-06-07 15:23:35 -0400669lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500670{
671 struct lpfc_vport *vport = phba->pport;
672 LPFC_MBOXQ_t *pmb;
673 MAILBOX_t *mb;
674 int rc;
675
676 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
677 if (!pmb) {
678 phba->link_state = LPFC_HBA_ERROR;
679 return -ENOMEM;
680 }
681 mb = &pmb->u.mb;
682 pmb->vport = vport;
683
James Smart76a95d72010-11-20 23:11:48 -0500684 lpfc_init_link(phba, pmb, phba->cfg_topology, phba->cfg_link_speed);
James Smart84d1b002010-02-12 14:42:33 -0500685 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
686 lpfc_set_loopback_flag(phba);
James Smart6e7288d2010-06-07 15:23:35 -0400687 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart76a95d72010-11-20 23:11:48 -0500688 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
James Smart84d1b002010-02-12 14:42:33 -0500689 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
690 "0498 Adapter failed to init, mbxCmd x%x "
691 "INIT_LINK, mbxStatus x%x\n",
692 mb->mbxCommand, mb->mbxStatus);
James Smart76a95d72010-11-20 23:11:48 -0500693 if (phba->sli_rev <= LPFC_SLI_REV3) {
694 /* Clear all interrupt enable conditions */
695 writel(0, phba->HCregaddr);
696 readl(phba->HCregaddr); /* flush */
697 /* Clear all pending interrupts */
698 writel(0xffffffff, phba->HAregaddr);
699 readl(phba->HAregaddr); /* flush */
700 }
James Smart84d1b002010-02-12 14:42:33 -0500701 phba->link_state = LPFC_HBA_ERROR;
James Smart6e7288d2010-06-07 15:23:35 -0400702 if (rc != MBX_BUSY || flag == MBX_POLL)
James Smart84d1b002010-02-12 14:42:33 -0500703 mempool_free(pmb, phba->mbox_mem_pool);
704 return -EIO;
705 }
James Smarte40a02c2010-02-26 14:13:54 -0500706 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
James Smart6e7288d2010-06-07 15:23:35 -0400707 if (flag == MBX_POLL)
708 mempool_free(pmb, phba->mbox_mem_pool);
James Smart84d1b002010-02-12 14:42:33 -0500709
710 return 0;
711}
712
713/**
714 * lpfc_hba_down_link - this routine downs the FC link
James Smart6e7288d2010-06-07 15:23:35 -0400715 * @phba: pointer to lpfc hba data structure.
716 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500717 *
718 * This routine will issue the DOWN_LINK mailbox command call.
719 * It is available to other drivers through the lpfc_hba data
720 * structure for use to stop the link.
721 *
722 * Return code
723 * 0 - success
724 * Any other value - error
725 **/
726int
James Smart6e7288d2010-06-07 15:23:35 -0400727lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500728{
729 LPFC_MBOXQ_t *pmb;
730 int rc;
731
732 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
733 if (!pmb) {
734 phba->link_state = LPFC_HBA_ERROR;
735 return -ENOMEM;
736 }
737
738 lpfc_printf_log(phba,
739 KERN_ERR, LOG_INIT,
740 "0491 Adapter Link is disabled.\n");
741 lpfc_down_link(phba, pmb);
742 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6e7288d2010-06-07 15:23:35 -0400743 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart84d1b002010-02-12 14:42:33 -0500744 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
745 lpfc_printf_log(phba,
746 KERN_ERR, LOG_INIT,
747 "2522 Adapter failed to issue DOWN_LINK"
748 " mbox command rc 0x%x\n", rc);
749
750 mempool_free(pmb, phba->mbox_mem_pool);
751 return -EIO;
752 }
James Smart6e7288d2010-06-07 15:23:35 -0400753 if (flag == MBX_POLL)
754 mempool_free(pmb, phba->mbox_mem_pool);
755
James Smart84d1b002010-02-12 14:42:33 -0500756 return 0;
757}
758
759/**
James Smart3621a712009-04-06 18:47:14 -0400760 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400761 * @phba: pointer to lpfc HBA data structure.
762 *
763 * This routine will do LPFC uninitialization before the HBA is reset when
764 * bringing down the SLI Layer.
765 *
766 * Return codes
767 * 0 - success.
768 * Any other value - error.
769 **/
dea31012005-04-17 16:05:31 -0500770int
James Smart2e0fef82007-06-17 19:56:36 -0500771lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500772{
James Smart1b32f6a2008-02-08 18:49:39 -0500773 struct lpfc_vport **vports;
774 int i;
James Smart3772a992009-05-22 14:50:54 -0400775
776 if (phba->sli_rev <= LPFC_SLI_REV3) {
777 /* Disable interrupts */
778 writel(0, phba->HCregaddr);
779 readl(phba->HCregaddr); /* flush */
780 }
dea31012005-04-17 16:05:31 -0500781
James Smart1b32f6a2008-02-08 18:49:39 -0500782 if (phba->pport->load_flag & FC_UNLOADING)
783 lpfc_cleanup_discovery_resources(phba->pport);
784 else {
785 vports = lpfc_create_vport_work_array(phba);
786 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400787 for (i = 0; i <= phba->max_vports &&
788 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500789 lpfc_cleanup_discovery_resources(vports[i]);
790 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500791 }
792 return 0;
dea31012005-04-17 16:05:31 -0500793}
794
James Smarte59058c2008-08-24 21:49:00 -0400795/**
James Smart3772a992009-05-22 14:50:54 -0400796 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400797 * @phba: pointer to lpfc HBA data structure.
798 *
799 * This routine will do uninitialization after the HBA is reset when bring
800 * down the SLI Layer.
801 *
802 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200803 * 0 - success.
James Smarte59058c2008-08-24 21:49:00 -0400804 * Any other value - error.
805 **/
James Smart3772a992009-05-22 14:50:54 -0400806static int
807lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -0500808{
809 struct lpfc_sli *psli = &phba->sli;
810 struct lpfc_sli_ring *pring;
811 struct lpfc_dmabuf *mp, *next_mp;
James Smart09372822008-01-11 01:52:54 -0500812 LIST_HEAD(completions);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500813 int i;
814
James Smart92d7f7b2007-06-17 19:56:38 -0500815 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
816 lpfc_sli_hbqbuf_free_all(phba);
817 else {
818 /* Cleanup preposted buffers on the ELS ring */
819 pring = &psli->ring[LPFC_ELS_RING];
820 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
821 list_del(&mp->list);
822 pring->postbufq_cnt--;
823 lpfc_mbuf_free(phba, mp->virt, mp->phys);
824 kfree(mp);
825 }
Jamie Wellnitz41415862006-02-28 19:25:27 -0500826 }
827
James Smart09372822008-01-11 01:52:54 -0500828 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500829 for (i = 0; i < psli->num_rings; i++) {
830 pring = &psli->ring[i];
James Smart09372822008-01-11 01:52:54 -0500831
832 /* At this point in time the HBA is either reset or DOA. Either
833 * way, nothing should be on txcmplq as it will NEVER complete.
834 */
835 list_splice_init(&pring->txcmplq, &completions);
836 pring->txcmplq_cnt = 0;
837 spin_unlock_irq(&phba->hbalock);
838
James Smarta257bf92009-04-06 18:48:10 -0400839 /* Cancel all the IOCBs from the completions list */
840 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
841 IOERR_SLI_ABORTED);
James Smart09372822008-01-11 01:52:54 -0500842
Jamie Wellnitz41415862006-02-28 19:25:27 -0500843 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart09372822008-01-11 01:52:54 -0500844 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500845 }
James Smart09372822008-01-11 01:52:54 -0500846 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500847
848 return 0;
849}
James Smart5af5eee2010-10-22 11:06:38 -0400850
James Smartda0436e2009-05-22 14:51:39 -0400851/**
852 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
853 * @phba: pointer to lpfc HBA data structure.
854 *
855 * This routine will do uninitialization after the HBA is reset when bring
856 * down the SLI Layer.
857 *
858 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200859 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400860 * Any other value - error.
861 **/
862static int
863lpfc_hba_down_post_s4(struct lpfc_hba *phba)
864{
865 struct lpfc_scsi_buf *psb, *psb_next;
866 LIST_HEAD(aborts);
867 int ret;
868 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500869 struct lpfc_sglq *sglq_entry = NULL;
870
James Smartda0436e2009-05-22 14:51:39 -0400871 ret = lpfc_hba_down_post_s3(phba);
872 if (ret)
873 return ret;
874 /* At this point in time the HBA is either reset or DOA. Either
875 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
876 * on the lpfc_sgl_list so that it can either be freed if the
877 * driver is unloading or reposted if the driver is restarting
878 * the port.
879 */
880 spin_lock_irq(&phba->hbalock); /* required for lpfc_sgl_list and */
881 /* scsl_buf_list */
882 /* abts_sgl_list_lock required because worker thread uses this
883 * list.
884 */
885 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -0500886 list_for_each_entry(sglq_entry,
887 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
888 sglq_entry->state = SGL_FREED;
889
James Smartda0436e2009-05-22 14:51:39 -0400890 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
891 &phba->sli4_hba.lpfc_sgl_list);
892 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
893 /* abts_scsi_buf_list_lock required because worker thread uses this
894 * list.
895 */
896 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
897 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
898 &aborts);
899 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
900 spin_unlock_irq(&phba->hbalock);
901
902 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
903 psb->pCmd = NULL;
904 psb->status = IOSTAT_SUCCESS;
905 }
906 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
907 list_splice(&aborts, &phba->lpfc_scsi_buf_list);
908 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
909 return 0;
910}
911
912/**
913 * lpfc_hba_down_post - Wrapper func for hba down post routine
914 * @phba: pointer to lpfc HBA data structure.
915 *
916 * This routine wraps the actual SLI3 or SLI4 routine for performing
917 * uninitialization after the HBA is reset when bring down the SLI Layer.
918 *
919 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200920 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400921 * Any other value - error.
922 **/
923int
924lpfc_hba_down_post(struct lpfc_hba *phba)
925{
926 return (*phba->lpfc_hba_down_post)(phba);
927}
Jamie Wellnitz41415862006-02-28 19:25:27 -0500928
James Smarte59058c2008-08-24 21:49:00 -0400929/**
James Smart3621a712009-04-06 18:47:14 -0400930 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -0400931 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
932 *
933 * This is the HBA-timer timeout handler registered to the lpfc driver. When
934 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
935 * work-port-events bitmap and the worker thread is notified. This timeout
936 * event will be used by the worker thread to invoke the actual timeout
937 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
938 * be performed in the timeout handler and the HBA timeout event bit shall
939 * be cleared by the worker thread after it has taken the event bitmap out.
940 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100941static void
James Smart858c9f62007-06-17 19:56:39 -0500942lpfc_hb_timeout(unsigned long ptr)
943{
944 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400945 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -0500946 unsigned long iflag;
947
948 phba = (struct lpfc_hba *)ptr;
James Smart93996272008-08-24 21:50:30 -0400949
950 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -0500951 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -0400952 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
953 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -0500954 phba->pport->work_port_events |= WORKER_HB_TMO;
955 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
956
James Smart93996272008-08-24 21:50:30 -0400957 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -0400958 if (!tmo_posted)
959 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -0500960 return;
961}
962
James Smarte59058c2008-08-24 21:49:00 -0400963/**
James Smart19ca7602010-11-20 23:11:55 -0500964 * lpfc_rrq_timeout - The RRQ-timer timeout handler
965 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
966 *
967 * This is the RRQ-timer timeout handler registered to the lpfc driver. When
968 * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
969 * work-port-events bitmap and the worker thread is notified. This timeout
970 * event will be used by the worker thread to invoke the actual timeout
971 * handler routine, lpfc_rrq_handler. Any periodical operations will
972 * be performed in the timeout handler and the RRQ timeout event bit shall
973 * be cleared by the worker thread after it has taken the event bitmap out.
974 **/
975static void
976lpfc_rrq_timeout(unsigned long ptr)
977{
978 struct lpfc_hba *phba;
James Smart19ca7602010-11-20 23:11:55 -0500979 unsigned long iflag;
980
981 phba = (struct lpfc_hba *)ptr;
982 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -0500983 phba->hba_flag |= HBA_RRQ_ACTIVE;
James Smart19ca7602010-11-20 23:11:55 -0500984 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -0500985 lpfc_worker_wake_up(phba);
James Smart19ca7602010-11-20 23:11:55 -0500986}
987
988/**
James Smart3621a712009-04-06 18:47:14 -0400989 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -0400990 * @phba: pointer to lpfc hba data structure.
991 * @pmboxq: pointer to the driver internal queue element for mailbox command.
992 *
993 * This is the callback function to the lpfc heart-beat mailbox command.
994 * If configured, the lpfc driver issues the heart-beat mailbox command to
995 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
996 * heart-beat mailbox command is issued, the driver shall set up heart-beat
997 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
998 * heart-beat outstanding state. Once the mailbox command comes back and
999 * no error conditions detected, the heart-beat mailbox command timer is
1000 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1001 * state is cleared for the next heart-beat. If the timer expired with the
1002 * heart-beat outstanding state set, the driver will put the HBA offline.
1003 **/
James Smart858c9f62007-06-17 19:56:39 -05001004static void
1005lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1006{
1007 unsigned long drvr_flag;
1008
1009 spin_lock_irqsave(&phba->hbalock, drvr_flag);
1010 phba->hb_outstanding = 0;
1011 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1012
James Smart93996272008-08-24 21:50:30 -04001013 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -05001014 mempool_free(pmboxq, phba->mbox_mem_pool);
1015 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1016 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -04001017 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -05001018 mod_timer(&phba->hb_tmofunc,
1019 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1020 return;
1021}
1022
James Smarte59058c2008-08-24 21:49:00 -04001023/**
James Smart3621a712009-04-06 18:47:14 -04001024 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -04001025 * @phba: pointer to lpfc hba data structure.
1026 *
1027 * This is the actual HBA-timer timeout handler to be invoked by the worker
1028 * thread whenever the HBA timer fired and HBA-timeout event posted. This
1029 * handler performs any periodic operations needed for the device. If such
1030 * periodic event has already been attended to either in the interrupt handler
1031 * or by processing slow-ring or fast-ring events within the HBA-timer
1032 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1033 * the timer for the next timeout period. If lpfc heart-beat mailbox command
1034 * is configured and there is no heart-beat mailbox command outstanding, a
1035 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1036 * has been a heart-beat mailbox command outstanding, the HBA shall be put
1037 * to offline.
1038 **/
James Smart858c9f62007-06-17 19:56:39 -05001039void
1040lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1041{
James Smart45ed1192009-10-02 15:17:02 -04001042 struct lpfc_vport **vports;
James Smart858c9f62007-06-17 19:56:39 -05001043 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -05001044 struct lpfc_dmabuf *buf_ptr;
James Smart45ed1192009-10-02 15:17:02 -04001045 int retval, i;
James Smart858c9f62007-06-17 19:56:39 -05001046 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -05001047 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -05001048
James Smart45ed1192009-10-02 15:17:02 -04001049 vports = lpfc_create_vport_work_array(phba);
1050 if (vports != NULL)
1051 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
1052 lpfc_rcv_seq_check_edtov(vports[i]);
1053 lpfc_destroy_vport_work_array(phba, vports);
1054
James Smart858c9f62007-06-17 19:56:39 -05001055 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -04001056 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -05001057 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1058 return;
1059
1060 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -05001061
1062 if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
1063 jiffies)) {
1064 spin_unlock_irq(&phba->pport->work_port_lock);
1065 if (!phba->hb_outstanding)
1066 mod_timer(&phba->hb_tmofunc,
1067 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1068 else
1069 mod_timer(&phba->hb_tmofunc,
1070 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
1071 return;
1072 }
1073 spin_unlock_irq(&phba->pport->work_port_lock);
1074
James Smart0ff10d42008-01-11 01:52:36 -05001075 if (phba->elsbuf_cnt &&
1076 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1077 spin_lock_irq(&phba->hbalock);
1078 list_splice_init(&phba->elsbuf, &completions);
1079 phba->elsbuf_cnt = 0;
1080 phba->elsbuf_prev_cnt = 0;
1081 spin_unlock_irq(&phba->hbalock);
1082
1083 while (!list_empty(&completions)) {
1084 list_remove_head(&completions, buf_ptr,
1085 struct lpfc_dmabuf, list);
1086 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1087 kfree(buf_ptr);
1088 }
1089 }
1090 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1091
James Smart858c9f62007-06-17 19:56:39 -05001092 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -05001093 if (phba->cfg_enable_hba_heartbeat) {
1094 if (!phba->hb_outstanding) {
James Smartbc739052010-08-04 16:11:18 -04001095 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1096 (list_empty(&psli->mboxq))) {
1097 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1098 GFP_KERNEL);
1099 if (!pmboxq) {
1100 mod_timer(&phba->hb_tmofunc,
1101 jiffies +
1102 HZ * LPFC_HB_MBOX_INTERVAL);
1103 return;
1104 }
James Smart13815c82008-01-11 01:52:48 -05001105
James Smartbc739052010-08-04 16:11:18 -04001106 lpfc_heart_beat(phba, pmboxq);
1107 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1108 pmboxq->vport = phba->pport;
1109 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1110 MBX_NOWAIT);
James Smart13815c82008-01-11 01:52:48 -05001111
James Smartbc739052010-08-04 16:11:18 -04001112 if (retval != MBX_BUSY &&
1113 retval != MBX_SUCCESS) {
1114 mempool_free(pmboxq,
1115 phba->mbox_mem_pool);
1116 mod_timer(&phba->hb_tmofunc,
1117 jiffies +
1118 HZ * LPFC_HB_MBOX_INTERVAL);
1119 return;
1120 }
1121 phba->skipped_hb = 0;
1122 phba->hb_outstanding = 1;
1123 } else if (time_before_eq(phba->last_completion_time,
1124 phba->skipped_hb)) {
1125 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1126 "2857 Last completion time not "
1127 " updated in %d ms\n",
1128 jiffies_to_msecs(jiffies
1129 - phba->last_completion_time));
1130 } else
1131 phba->skipped_hb = jiffies;
1132
James Smart858c9f62007-06-17 19:56:39 -05001133 mod_timer(&phba->hb_tmofunc,
James Smart13815c82008-01-11 01:52:48 -05001134 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
James Smart858c9f62007-06-17 19:56:39 -05001135 return;
James Smart13815c82008-01-11 01:52:48 -05001136 } else {
1137 /*
1138 * If heart beat timeout called with hb_outstanding set
James Smartdcf2a4e2010-09-29 11:18:53 -04001139 * we need to give the hb mailbox cmd a chance to
1140 * complete or TMO.
James Smart13815c82008-01-11 01:52:48 -05001141 */
James Smartdcf2a4e2010-09-29 11:18:53 -04001142 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1143 "0459 Adapter heartbeat still out"
1144 "standing:last compl time was %d ms.\n",
1145 jiffies_to_msecs(jiffies
1146 - phba->last_completion_time));
1147 mod_timer(&phba->hb_tmofunc,
1148 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
James Smart858c9f62007-06-17 19:56:39 -05001149 }
James Smart858c9f62007-06-17 19:56:39 -05001150 }
1151}
1152
James Smarte59058c2008-08-24 21:49:00 -04001153/**
James Smart3621a712009-04-06 18:47:14 -04001154 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -04001155 * @phba: pointer to lpfc hba data structure.
1156 *
1157 * This routine is called to bring the HBA offline when HBA hardware error
1158 * other than Port Error 6 has been detected.
1159 **/
James Smart09372822008-01-11 01:52:54 -05001160static void
1161lpfc_offline_eratt(struct lpfc_hba *phba)
1162{
1163 struct lpfc_sli *psli = &phba->sli;
1164
1165 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001166 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -05001167 spin_unlock_irq(&phba->hbalock);
1168 lpfc_offline_prep(phba);
1169
1170 lpfc_offline(phba);
1171 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001172 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001173 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001174 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001175 lpfc_hba_down_post(phba);
1176 lpfc_sli_brdready(phba, HS_MBRDY);
1177 lpfc_unblock_mgmt_io(phba);
1178 phba->link_state = LPFC_HBA_ERROR;
1179 return;
1180}
1181
James Smarte59058c2008-08-24 21:49:00 -04001182/**
James Smartda0436e2009-05-22 14:51:39 -04001183 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1184 * @phba: pointer to lpfc hba data structure.
1185 *
1186 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1187 * other than Port Error 6 has been detected.
1188 **/
1189static void
1190lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1191{
1192 lpfc_offline_prep(phba);
1193 lpfc_offline(phba);
1194 lpfc_sli4_brdreset(phba);
1195 lpfc_hba_down_post(phba);
1196 lpfc_sli4_post_status_check(phba);
1197 lpfc_unblock_mgmt_io(phba);
1198 phba->link_state = LPFC_HBA_ERROR;
1199}
1200
1201/**
James Smarta257bf92009-04-06 18:48:10 -04001202 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1203 * @phba: pointer to lpfc hba data structure.
1204 *
1205 * This routine is invoked to handle the deferred HBA hardware error
1206 * conditions. This type of error is indicated by HBA by setting ER1
1207 * and another ER bit in the host status register. The driver will
1208 * wait until the ER1 bit clears before handling the error condition.
1209 **/
1210static void
1211lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1212{
1213 uint32_t old_host_status = phba->work_hs;
1214 struct lpfc_sli_ring *pring;
1215 struct lpfc_sli *psli = &phba->sli;
1216
James Smartf4b4c682009-05-22 14:53:12 -04001217 /* If the pci channel is offline, ignore possible errors,
1218 * since we cannot communicate with the pci card anyway.
1219 */
1220 if (pci_channel_offline(phba->pcidev)) {
1221 spin_lock_irq(&phba->hbalock);
1222 phba->hba_flag &= ~DEFER_ERATT;
1223 spin_unlock_irq(&phba->hbalock);
1224 return;
1225 }
1226
James Smarta257bf92009-04-06 18:48:10 -04001227 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1228 "0479 Deferred Adapter Hardware Error "
1229 "Data: x%x x%x x%x\n",
1230 phba->work_hs,
1231 phba->work_status[0], phba->work_status[1]);
1232
1233 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001234 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001235 spin_unlock_irq(&phba->hbalock);
1236
1237
1238 /*
1239 * Firmware stops when it triggred erratt. That could cause the I/Os
1240 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1241 * SCSI layer retry it after re-establishing link.
1242 */
1243 pring = &psli->ring[psli->fcp_ring];
1244 lpfc_sli_abort_iocb_ring(phba, pring);
1245
1246 /*
1247 * There was a firmware error. Take the hba offline and then
1248 * attempt to restart it.
1249 */
1250 lpfc_offline_prep(phba);
1251 lpfc_offline(phba);
1252
1253 /* Wait for the ER1 bit to clear.*/
1254 while (phba->work_hs & HS_FFER1) {
1255 msleep(100);
James Smart9940b972011-03-11 16:06:12 -05001256 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1257 phba->work_hs = UNPLUG_ERR ;
1258 break;
1259 }
James Smarta257bf92009-04-06 18:48:10 -04001260 /* If driver is unloading let the worker thread continue */
1261 if (phba->pport->load_flag & FC_UNLOADING) {
1262 phba->work_hs = 0;
1263 break;
1264 }
1265 }
1266
1267 /*
1268 * This is to ptrotect against a race condition in which
1269 * first write to the host attention register clear the
1270 * host status register.
1271 */
1272 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1273 phba->work_hs = old_host_status & ~HS_FFER1;
1274
James Smart3772a992009-05-22 14:50:54 -04001275 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001276 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001277 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001278 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1279 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1280}
1281
James Smart3772a992009-05-22 14:50:54 -04001282static void
1283lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1284{
1285 struct lpfc_board_event_header board_event;
1286 struct Scsi_Host *shost;
1287
1288 board_event.event_type = FC_REG_BOARD_EVENT;
1289 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1290 shost = lpfc_shost_from_vport(phba->pport);
1291 fc_host_post_vendor_event(shost, fc_get_event_number(),
1292 sizeof(board_event),
1293 (char *) &board_event,
1294 LPFC_NL_VENDOR_ID);
1295}
1296
James Smarta257bf92009-04-06 18:48:10 -04001297/**
James Smart3772a992009-05-22 14:50:54 -04001298 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001299 * @phba: pointer to lpfc hba data structure.
1300 *
1301 * This routine is invoked to handle the following HBA hardware error
1302 * conditions:
1303 * 1 - HBA error attention interrupt
1304 * 2 - DMA ring index out of range
1305 * 3 - Mailbox command came back as unknown
1306 **/
James Smart3772a992009-05-22 14:50:54 -04001307static void
1308lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001309{
James Smart2e0fef82007-06-17 19:56:36 -05001310 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001311 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001312 struct lpfc_sli_ring *pring;
James Smartd2873e42006-08-18 17:46:43 -04001313 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001314 unsigned long temperature;
1315 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001316 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001317
Linas Vepstas8d63f372007-02-14 14:28:36 -06001318 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001319 * since we cannot communicate with the pci card anyway.
1320 */
1321 if (pci_channel_offline(phba->pcidev)) {
1322 spin_lock_irq(&phba->hbalock);
1323 phba->hba_flag &= ~DEFER_ERATT;
1324 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001325 return;
James Smart3772a992009-05-22 14:50:54 -04001326 }
1327
James Smart13815c82008-01-11 01:52:48 -05001328 /* If resets are disabled then leave the HBA alone and return */
1329 if (!phba->cfg_enable_hba_reset)
1330 return;
dea31012005-04-17 16:05:31 -05001331
James Smartea2151b2008-09-07 11:52:10 -04001332 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001333 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001334
James Smarta257bf92009-04-06 18:48:10 -04001335 if (phba->hba_flag & DEFER_ERATT)
1336 lpfc_handle_deferred_eratt(phba);
1337
James Smartdcf2a4e2010-09-29 11:18:53 -04001338 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1339 if (phba->work_hs & HS_FFER6)
1340 /* Re-establishing Link */
1341 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1342 "1301 Re-establishing Link "
1343 "Data: x%x x%x x%x\n",
1344 phba->work_hs, phba->work_status[0],
1345 phba->work_status[1]);
1346 if (phba->work_hs & HS_FFER8)
1347 /* Device Zeroization */
1348 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1349 "2861 Host Authentication device "
1350 "zeroization Data:x%x x%x x%x\n",
1351 phba->work_hs, phba->work_status[0],
1352 phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001353
James Smart92d7f7b2007-06-17 19:56:38 -05001354 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001355 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001356 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001357
1358 /*
1359 * Firmware stops when it triggled erratt with HS_FFER6.
1360 * That could cause the I/Os dropped by the firmware.
1361 * Error iocb (I/O) on txcmplq and let the SCSI layer
1362 * retry it after re-establishing link.
1363 */
1364 pring = &psli->ring[psli->fcp_ring];
1365 lpfc_sli_abort_iocb_ring(phba, pring);
1366
dea31012005-04-17 16:05:31 -05001367 /*
1368 * There was a firmware error. Take the hba offline and then
1369 * attempt to restart it.
1370 */
James Smart46fa3112007-04-25 09:51:45 -04001371 lpfc_offline_prep(phba);
dea31012005-04-17 16:05:31 -05001372 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001373 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001374 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001375 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001376 return;
1377 }
James Smart46fa3112007-04-25 09:51:45 -04001378 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001379 } else if (phba->work_hs & HS_CRIT_TEMP) {
1380 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1381 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1382 temp_event_data.event_code = LPFC_CRIT_TEMP;
1383 temp_event_data.data = (uint32_t)temperature;
1384
1385 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001386 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001387 "(%ld), taking this port offline "
1388 "Data: x%x x%x x%x\n",
1389 temperature, phba->work_hs,
1390 phba->work_status[0], phba->work_status[1]);
1391
1392 shost = lpfc_shost_from_vport(phba->pport);
1393 fc_host_post_vendor_event(shost, fc_get_event_number(),
1394 sizeof(temp_event_data),
1395 (char *) &temp_event_data,
1396 SCSI_NL_VID_TYPE_PCI
1397 | PCI_VENDOR_ID_EMULEX);
1398
James Smart7af67052007-10-27 13:38:11 -04001399 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001400 phba->over_temp_state = HBA_OVER_TEMP;
1401 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001402 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001403
dea31012005-04-17 16:05:31 -05001404 } else {
1405 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001406 * failure is a value other than FFER6. Do not call the offline
1407 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001408 */
1409 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001410 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001411 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001412 phba->work_hs,
dea31012005-04-17 16:05:31 -05001413 phba->work_status[0], phba->work_status[1]);
1414
James Smartd2873e42006-08-18 17:46:43 -04001415 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001416 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001417 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001418 sizeof(event_data), (char *) &event_data,
1419 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1420
James Smart09372822008-01-11 01:52:54 -05001421 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001422 }
James Smart93996272008-08-24 21:50:30 -04001423 return;
dea31012005-04-17 16:05:31 -05001424}
1425
James Smarte59058c2008-08-24 21:49:00 -04001426/**
James Smartda0436e2009-05-22 14:51:39 -04001427 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1428 * @phba: pointer to lpfc hba data structure.
1429 *
1430 * This routine is invoked to handle the SLI4 HBA hardware error attention
1431 * conditions.
1432 **/
1433static void
1434lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1435{
1436 struct lpfc_vport *vport = phba->pport;
1437 uint32_t event_data;
1438 struct Scsi_Host *shost;
James Smart2fcee4b2010-12-15 17:57:46 -05001439 uint32_t if_type;
1440 struct lpfc_register portstat_reg;
James Smart73d91e52011-10-10 21:32:10 -04001441 int rc;
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 }
James Smart73d91e52011-10-10 21:32:10 -04001484 /*
1485 * On error status condition, driver need to wait for port
1486 * ready before performing reset.
1487 */
1488 rc = lpfc_sli4_pdev_status_reg_wait(phba);
1489 if (!rc) {
James Smart079b5c92011-08-21 21:48:49 -04001490 /* need reset: attempt for port recovery */
James Smart2fcee4b2010-12-15 17:57:46 -05001491 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1492 "2887 Port Error: Attempting "
1493 "Port Recovery\n");
James Smart079b5c92011-08-21 21:48:49 -04001494 lpfc_offline_prep(phba);
1495 lpfc_offline(phba);
1496 lpfc_sli_brdrestart(phba);
1497 if (lpfc_online(phba) == 0) {
1498 lpfc_unblock_mgmt_io(phba);
1499 return;
1500 }
1501 /* fall through for not able to recover */
James Smart2fcee4b2010-12-15 17:57:46 -05001502 }
1503 lpfc_sli4_offline_eratt(phba);
1504 break;
1505 case LPFC_SLI_INTF_IF_TYPE_1:
1506 default:
1507 break;
1508 }
James Smartda0436e2009-05-22 14:51:39 -04001509}
1510
1511/**
1512 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1513 * @phba: pointer to lpfc HBA data structure.
1514 *
1515 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1516 * routine from the API jump table function pointer from the lpfc_hba struct.
1517 *
1518 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001519 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001520 * Any other value - error.
1521 **/
1522void
1523lpfc_handle_eratt(struct lpfc_hba *phba)
1524{
1525 (*phba->lpfc_handle_eratt)(phba);
1526}
1527
1528/**
James Smart3621a712009-04-06 18:47:14 -04001529 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04001530 * @phba: pointer to lpfc hba data structure.
1531 *
1532 * This routine is invoked from the worker thread to handle a HBA host
1533 * attention link event.
1534 **/
dea31012005-04-17 16:05:31 -05001535void
James Smart2e0fef82007-06-17 19:56:36 -05001536lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001537{
James Smart2e0fef82007-06-17 19:56:36 -05001538 struct lpfc_vport *vport = phba->pport;
1539 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001540 LPFC_MBOXQ_t *pmb;
1541 volatile uint32_t control;
1542 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05001543 int rc = 0;
dea31012005-04-17 16:05:31 -05001544
1545 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001546 if (!pmb) {
1547 rc = 1;
dea31012005-04-17 16:05:31 -05001548 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05001549 }
dea31012005-04-17 16:05:31 -05001550
1551 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001552 if (!mp) {
1553 rc = 2;
dea31012005-04-17 16:05:31 -05001554 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05001555 }
dea31012005-04-17 16:05:31 -05001556
1557 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05001558 if (!mp->virt) {
1559 rc = 3;
dea31012005-04-17 16:05:31 -05001560 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05001561 }
dea31012005-04-17 16:05:31 -05001562
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05001563 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04001564 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05001565
1566 psli->slistat.link_event++;
James Smart76a95d72010-11-20 23:11:48 -05001567 lpfc_read_topology(phba, pmb, mp);
1568 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smart2e0fef82007-06-17 19:56:36 -05001569 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04001570 /* Block ELS IOCBs until we have processed this mbox command */
1571 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04001572 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05001573 if (rc == MBX_NOT_FINISHED) {
1574 rc = 4;
James Smart14691152006-12-02 13:34:28 -05001575 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05001576 }
dea31012005-04-17 16:05:31 -05001577
1578 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05001579 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001580 writel(HA_LATT, phba->HAregaddr);
1581 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001582 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001583
1584 return;
1585
James Smart14691152006-12-02 13:34:28 -05001586lpfc_handle_latt_free_mbuf:
James Smart0d2b6b82008-06-14 22:52:47 -04001587 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05001588 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001589lpfc_handle_latt_free_mp:
1590 kfree(mp);
1591lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04001592 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001593lpfc_handle_latt_err_exit:
1594 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001595 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001596 psli->sli_flag |= LPFC_PROCESS_LA;
1597 control = readl(phba->HCregaddr);
1598 control |= HC_LAINT_ENA;
1599 writel(control, phba->HCregaddr);
1600 readl(phba->HCregaddr); /* flush */
1601
1602 /* Clear Link Attention in HA REG */
1603 writel(HA_LATT, phba->HAregaddr);
1604 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001605 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001606 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001607 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001608
James Smart09372822008-01-11 01:52:54 -05001609 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1610 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05001611
1612 return;
1613}
1614
James Smarte59058c2008-08-24 21:49:00 -04001615/**
James Smart3621a712009-04-06 18:47:14 -04001616 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04001617 * @phba: pointer to lpfc hba data structure.
1618 * @vpd: pointer to the vital product data.
1619 * @len: length of the vital product data in bytes.
1620 *
1621 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1622 * an array of characters. In this routine, the ModelName, ProgramType, and
1623 * ModelDesc, etc. fields of the phba data structure will be populated.
1624 *
1625 * Return codes
1626 * 0 - pointer to the VPD passed in is NULL
1627 * 1 - success
1628 **/
James Smart3772a992009-05-22 14:50:54 -04001629int
James Smart2e0fef82007-06-17 19:56:36 -05001630lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05001631{
1632 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05001633 int Length;
dea31012005-04-17 16:05:31 -05001634 int i, j;
1635 int finished = 0;
1636 int index = 0;
1637
1638 if (!vpd)
1639 return 0;
1640
1641 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05001642 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001643 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05001644 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1645 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001646 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05001647 switch (vpd[index]) {
1648 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001649 case 0x91:
dea31012005-04-17 16:05:31 -05001650 index += 1;
1651 lenlo = vpd[index];
1652 index += 1;
1653 lenhi = vpd[index];
1654 index += 1;
1655 i = ((((unsigned short)lenhi) << 8) + lenlo);
1656 index += i;
1657 break;
1658 case 0x90:
1659 index += 1;
1660 lenlo = vpd[index];
1661 index += 1;
1662 lenhi = vpd[index];
1663 index += 1;
1664 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001665 if (Length > len - index)
1666 Length = len - index;
dea31012005-04-17 16:05:31 -05001667 while (Length > 0) {
1668 /* Look for Serial Number */
1669 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1670 index += 2;
1671 i = vpd[index];
1672 index += 1;
1673 j = 0;
1674 Length -= (3+i);
1675 while(i--) {
1676 phba->SerialNumber[j++] = vpd[index++];
1677 if (j == 31)
1678 break;
1679 }
1680 phba->SerialNumber[j] = 0;
1681 continue;
1682 }
1683 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1684 phba->vpd_flag |= VPD_MODEL_DESC;
1685 index += 2;
1686 i = vpd[index];
1687 index += 1;
1688 j = 0;
1689 Length -= (3+i);
1690 while(i--) {
1691 phba->ModelDesc[j++] = vpd[index++];
1692 if (j == 255)
1693 break;
1694 }
1695 phba->ModelDesc[j] = 0;
1696 continue;
1697 }
1698 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1699 phba->vpd_flag |= VPD_MODEL_NAME;
1700 index += 2;
1701 i = vpd[index];
1702 index += 1;
1703 j = 0;
1704 Length -= (3+i);
1705 while(i--) {
1706 phba->ModelName[j++] = vpd[index++];
1707 if (j == 79)
1708 break;
1709 }
1710 phba->ModelName[j] = 0;
1711 continue;
1712 }
1713 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1714 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1715 index += 2;
1716 i = vpd[index];
1717 index += 1;
1718 j = 0;
1719 Length -= (3+i);
1720 while(i--) {
1721 phba->ProgramType[j++] = vpd[index++];
1722 if (j == 255)
1723 break;
1724 }
1725 phba->ProgramType[j] = 0;
1726 continue;
1727 }
1728 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1729 phba->vpd_flag |= VPD_PORT;
1730 index += 2;
1731 i = vpd[index];
1732 index += 1;
1733 j = 0;
1734 Length -= (3+i);
1735 while(i--) {
James Smartcd1c8302011-10-10 21:33:25 -04001736 if ((phba->sli_rev == LPFC_SLI_REV4) &&
1737 (phba->sli4_hba.pport_name_sta ==
1738 LPFC_SLI4_PPNAME_GET)) {
1739 j++;
1740 index++;
1741 } else
1742 phba->Port[j++] = vpd[index++];
1743 if (j == 19)
1744 break;
dea31012005-04-17 16:05:31 -05001745 }
James Smartcd1c8302011-10-10 21:33:25 -04001746 if ((phba->sli_rev != LPFC_SLI_REV4) ||
1747 (phba->sli4_hba.pport_name_sta ==
1748 LPFC_SLI4_PPNAME_NON))
1749 phba->Port[j] = 0;
dea31012005-04-17 16:05:31 -05001750 continue;
1751 }
1752 else {
1753 index += 2;
1754 i = vpd[index];
1755 index += 1;
1756 index += i;
1757 Length -= (3 + i);
1758 }
1759 }
1760 finished = 0;
1761 break;
1762 case 0x78:
1763 finished = 1;
1764 break;
1765 default:
1766 index ++;
1767 break;
1768 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001769 }
dea31012005-04-17 16:05:31 -05001770
1771 return(1);
1772}
1773
James Smarte59058c2008-08-24 21:49:00 -04001774/**
James Smart3621a712009-04-06 18:47:14 -04001775 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04001776 * @phba: pointer to lpfc hba data structure.
1777 * @mdp: pointer to the data structure to hold the derived model name.
1778 * @descp: pointer to the data structure to hold the derived description.
1779 *
1780 * This routine retrieves HBA's description based on its registered PCI device
1781 * ID. The @descp passed into this function points to an array of 256 chars. It
1782 * shall be returned with the model name, maximum speed, and the host bus type.
1783 * The @mdp passed into this function points to an array of 80 chars. When the
1784 * function returns, the @mdp will be filled with the model name.
1785 **/
dea31012005-04-17 16:05:31 -05001786static void
James Smart2e0fef82007-06-17 19:56:36 -05001787lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05001788{
1789 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05001790 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001791 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04001792 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04001793 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001794 struct {
James Smarta747c9c2009-11-18 15:41:10 -05001795 char *name;
1796 char *bus;
1797 char *function;
1798 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001799
1800 if (mdp && mdp[0] != '\0'
1801 && descp && descp[0] != '\0')
1802 return;
1803
James Smartc0c11512011-05-24 11:41:34 -04001804 if (phba->lmt & LMT_16Gb)
1805 max_speed = 16;
1806 else if (phba->lmt & LMT_10Gb)
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001807 max_speed = 10;
1808 else if (phba->lmt & LMT_8Gb)
1809 max_speed = 8;
1810 else if (phba->lmt & LMT_4Gb)
1811 max_speed = 4;
1812 else if (phba->lmt & LMT_2Gb)
1813 max_speed = 2;
1814 else
1815 max_speed = 1;
dea31012005-04-17 16:05:31 -05001816
1817 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05001818
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001819 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001820 case PCI_DEVICE_ID_FIREFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001821 m = (typeof(m)){"LP6000", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001822 break;
dea31012005-04-17 16:05:31 -05001823 case PCI_DEVICE_ID_SUPERFLY:
1824 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smarta747c9c2009-11-18 15:41:10 -05001825 m = (typeof(m)){"LP7000", "PCI",
1826 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001827 else
James Smarta747c9c2009-11-18 15:41:10 -05001828 m = (typeof(m)){"LP7000E", "PCI",
1829 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001830 break;
1831 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001832 m = (typeof(m)){"LP8000", "PCI",
1833 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001834 break;
1835 case PCI_DEVICE_ID_CENTAUR:
1836 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smarta747c9c2009-11-18 15:41:10 -05001837 m = (typeof(m)){"LP9002", "PCI",
1838 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001839 else
James Smarta747c9c2009-11-18 15:41:10 -05001840 m = (typeof(m)){"LP9000", "PCI",
1841 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001842 break;
1843 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001844 m = (typeof(m)){"LP952", "PCI",
1845 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001846 break;
1847 case PCI_DEVICE_ID_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05001848 m = (typeof(m)){"LP9802", "PCI-X",
1849 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001850 break;
1851 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05001852 m = (typeof(m)){"LP10000", "PCI-X",
1853 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001854 break;
1855 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05001856 m = (typeof(m)){"LPX1000", "PCI-X",
1857 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001858 break;
1859 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001860 m = (typeof(m)){"LP982", "PCI-X",
1861 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001862 break;
1863 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001864 m = (typeof(m)){"LP1050", "PCI-X",
1865 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001866 break;
1867 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05001868 m = (typeof(m)){"LP11000", "PCI-X2",
1869 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001870 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001871 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001872 m = (typeof(m)){"LP11000-SP", "PCI-X2",
1873 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001874 break;
1875 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001876 m = (typeof(m)){"LP11002-SP", "PCI-X2",
1877 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001878 break;
1879 case PCI_DEVICE_ID_NEPTUNE:
James Smarta747c9c2009-11-18 15:41:10 -05001880 m = (typeof(m)){"LPe1000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001881 break;
1882 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001883 m = (typeof(m)){"LPe1000-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001884 break;
1885 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001886 m = (typeof(m)){"LPe1002-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001887 break;
dea31012005-04-17 16:05:31 -05001888 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05001889 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001890 break;
1891 case PCI_DEVICE_ID_BSMB:
James Smarta747c9c2009-11-18 15:41:10 -05001892 m = (typeof(m)){"LP111", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001893 break;
1894 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05001895 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001896 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001897 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001898 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001899 break;
1900 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001901 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04001902 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001903 break;
dea31012005-04-17 16:05:31 -05001904 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05001905 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001906 break;
1907 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05001908 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001909 break;
1910 case PCI_DEVICE_ID_LP101:
James Smarta747c9c2009-11-18 15:41:10 -05001911 m = (typeof(m)){"LP101", "PCI-X", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001912 break;
1913 case PCI_DEVICE_ID_LP10000S:
James Smarta747c9c2009-11-18 15:41:10 -05001914 m = (typeof(m)){"LP10000-S", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001915 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001916 case PCI_DEVICE_ID_LP11000S:
James Smarta747c9c2009-11-18 15:41:10 -05001917 m = (typeof(m)){"LP11000-S", "PCI-X2", "Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05001918 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001919 case PCI_DEVICE_ID_LPE11000S:
James Smarta747c9c2009-11-18 15:41:10 -05001920 m = (typeof(m)){"LPe11000-S", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05001921 break;
James Smartb87eab32007-04-25 09:53:28 -04001922 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05001923 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001924 break;
1925 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05001926 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001927 break;
1928 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05001929 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001930 break;
1931 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001932 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001933 break;
1934 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001935 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001936 break;
1937 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05001938 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001939 break;
James Smart84774a42008-08-24 21:50:06 -04001940 case PCI_DEVICE_ID_HORNET:
James Smarta747c9c2009-11-18 15:41:10 -05001941 m = (typeof(m)){"LP21000", "PCIe", "FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001942 GE = 1;
1943 break;
1944 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05001945 m = (typeof(m)){"LPev12000", "PCIe IOV",
1946 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001947 break;
1948 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05001949 m = (typeof(m)){"LPev12000", "PCIe IOV",
1950 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001951 break;
1952 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05001953 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
1954 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001955 break;
James Smartda0436e2009-05-22 14:51:39 -04001956 case PCI_DEVICE_ID_TIGERSHARK:
1957 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05001958 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04001959 break;
James Smarta747c9c2009-11-18 15:41:10 -05001960 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04001961 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05001962 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
1963 break;
1964 case PCI_DEVICE_ID_FALCON:
1965 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
1966 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04001967 break;
James Smart98fc5dd2010-06-07 15:24:29 -04001968 case PCI_DEVICE_ID_BALIUS:
1969 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
1970 "Fibre Channel Adapter"};
1971 break;
James Smart085c6472010-11-20 23:11:37 -05001972 case PCI_DEVICE_ID_LANCER_FC:
James Smartc0c11512011-05-24 11:41:34 -04001973 case PCI_DEVICE_ID_LANCER_FC_VF:
1974 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
James Smart085c6472010-11-20 23:11:37 -05001975 break;
1976 case PCI_DEVICE_ID_LANCER_FCOE:
James Smartc0c11512011-05-24 11:41:34 -04001977 case PCI_DEVICE_ID_LANCER_FCOE_VF:
James Smart085c6472010-11-20 23:11:37 -05001978 oneConnect = 1;
James Smart079b5c92011-08-21 21:48:49 -04001979 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
James Smart085c6472010-11-20 23:11:37 -05001980 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05001981 default:
James Smarta747c9c2009-11-18 15:41:10 -05001982 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001983 break;
dea31012005-04-17 16:05:31 -05001984 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001985
1986 if (mdp && mdp[0] == '\0')
1987 snprintf(mdp, 79,"%s", m.name);
James Smartc0c11512011-05-24 11:41:34 -04001988 /*
1989 * oneConnect hba requires special processing, they are all initiators
James Smartda0436e2009-05-22 14:51:39 -04001990 * and we put the port number on the end
1991 */
1992 if (descp && descp[0] == '\0') {
1993 if (oneConnect)
1994 snprintf(descp, 255,
James Smarta747c9c2009-11-18 15:41:10 -05001995 "Emulex OneConnect %s, %s Initiator, Port %s",
1996 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04001997 phba->Port);
1998 else
1999 snprintf(descp, 255,
2000 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05002001 m.name, max_speed, (GE) ? "GE" : "Gb",
2002 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04002003 }
dea31012005-04-17 16:05:31 -05002004}
2005
James Smarte59058c2008-08-24 21:49:00 -04002006/**
James Smart3621a712009-04-06 18:47:14 -04002007 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04002008 * @phba: pointer to lpfc hba data structure.
2009 * @pring: pointer to a IOCB ring.
2010 * @cnt: the number of IOCBs to be posted to the IOCB ring.
2011 *
2012 * This routine posts a given number of IOCBs with the associated DMA buffer
2013 * descriptors specified by the cnt argument to the given IOCB ring.
2014 *
2015 * Return codes
2016 * The number of IOCBs NOT able to be posted to the IOCB ring.
2017 **/
dea31012005-04-17 16:05:31 -05002018int
James Smart495a7142008-06-14 22:52:59 -04002019lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05002020{
2021 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002022 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05002023 struct lpfc_dmabuf *mp1, *mp2;
2024
2025 cnt += pring->missbufcnt;
2026
2027 /* While there are buffers to post */
2028 while (cnt > 0) {
2029 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002030 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002031 if (iocb == NULL) {
2032 pring->missbufcnt = cnt;
2033 return cnt;
2034 }
dea31012005-04-17 16:05:31 -05002035 icmd = &iocb->iocb;
2036
2037 /* 2 buffers can be posted per command */
2038 /* Allocate buffer to post */
2039 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2040 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04002041 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2042 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002043 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002044 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002045 pring->missbufcnt = cnt;
2046 return cnt;
2047 }
2048
2049 INIT_LIST_HEAD(&mp1->list);
2050 /* Allocate buffer to post */
2051 if (cnt > 1) {
2052 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2053 if (mp2)
2054 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2055 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04002056 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08002057 kfree(mp2);
dea31012005-04-17 16:05:31 -05002058 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2059 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05002060 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002061 pring->missbufcnt = cnt;
2062 return cnt;
2063 }
2064
2065 INIT_LIST_HEAD(&mp2->list);
2066 } else {
2067 mp2 = NULL;
2068 }
2069
2070 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2071 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2072 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2073 icmd->ulpBdeCount = 1;
2074 cnt--;
2075 if (mp2) {
2076 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2077 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2078 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2079 cnt--;
2080 icmd->ulpBdeCount = 2;
2081 }
2082
2083 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2084 icmd->ulpLe = 1;
2085
James Smart3772a992009-05-22 14:50:54 -04002086 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2087 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05002088 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2089 kfree(mp1);
2090 cnt++;
2091 if (mp2) {
2092 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2093 kfree(mp2);
2094 cnt++;
2095 }
James Bottomley604a3e32005-10-29 10:28:33 -05002096 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05002097 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05002098 return cnt;
2099 }
dea31012005-04-17 16:05:31 -05002100 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05002101 if (mp2)
dea31012005-04-17 16:05:31 -05002102 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05002103 }
2104 pring->missbufcnt = 0;
2105 return 0;
2106}
2107
James Smarte59058c2008-08-24 21:49:00 -04002108/**
James Smart3621a712009-04-06 18:47:14 -04002109 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04002110 * @phba: pointer to lpfc hba data structure.
2111 *
2112 * This routine posts initial receive IOCB buffers to the ELS ring. The
2113 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
2114 * set to 64 IOCBs.
2115 *
2116 * Return codes
2117 * 0 - success (currently always success)
2118 **/
dea31012005-04-17 16:05:31 -05002119static int
James Smart2e0fef82007-06-17 19:56:36 -05002120lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002121{
2122 struct lpfc_sli *psli = &phba->sli;
2123
2124 /* Ring 0, ELS / CT buffers */
James Smart495a7142008-06-14 22:52:59 -04002125 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05002126 /* Ring 2 - FCP no buffers needed */
2127
2128 return 0;
2129}
2130
2131#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2132
James Smarte59058c2008-08-24 21:49:00 -04002133/**
James Smart3621a712009-04-06 18:47:14 -04002134 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04002135 * @HashResultPointer: pointer to an array as hash table.
2136 *
2137 * This routine sets up the initial values to the array of hash table entries
2138 * for the LC HBAs.
2139 **/
dea31012005-04-17 16:05:31 -05002140static void
2141lpfc_sha_init(uint32_t * HashResultPointer)
2142{
2143 HashResultPointer[0] = 0x67452301;
2144 HashResultPointer[1] = 0xEFCDAB89;
2145 HashResultPointer[2] = 0x98BADCFE;
2146 HashResultPointer[3] = 0x10325476;
2147 HashResultPointer[4] = 0xC3D2E1F0;
2148}
2149
James Smarte59058c2008-08-24 21:49:00 -04002150/**
James Smart3621a712009-04-06 18:47:14 -04002151 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04002152 * @HashResultPointer: pointer to an initial/result hash table.
2153 * @HashWorkingPointer: pointer to an working hash table.
2154 *
2155 * This routine iterates an initial hash table pointed by @HashResultPointer
2156 * with the values from the working hash table pointeed by @HashWorkingPointer.
2157 * The results are putting back to the initial hash table, returned through
2158 * the @HashResultPointer as the result hash table.
2159 **/
dea31012005-04-17 16:05:31 -05002160static void
2161lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2162{
2163 int t;
2164 uint32_t TEMP;
2165 uint32_t A, B, C, D, E;
2166 t = 16;
2167 do {
2168 HashWorkingPointer[t] =
2169 S(1,
2170 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2171 8] ^
2172 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2173 } while (++t <= 79);
2174 t = 0;
2175 A = HashResultPointer[0];
2176 B = HashResultPointer[1];
2177 C = HashResultPointer[2];
2178 D = HashResultPointer[3];
2179 E = HashResultPointer[4];
2180
2181 do {
2182 if (t < 20) {
2183 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2184 } else if (t < 40) {
2185 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2186 } else if (t < 60) {
2187 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2188 } else {
2189 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2190 }
2191 TEMP += S(5, A) + E + HashWorkingPointer[t];
2192 E = D;
2193 D = C;
2194 C = S(30, B);
2195 B = A;
2196 A = TEMP;
2197 } while (++t <= 79);
2198
2199 HashResultPointer[0] += A;
2200 HashResultPointer[1] += B;
2201 HashResultPointer[2] += C;
2202 HashResultPointer[3] += D;
2203 HashResultPointer[4] += E;
2204
2205}
2206
James Smarte59058c2008-08-24 21:49:00 -04002207/**
James Smart3621a712009-04-06 18:47:14 -04002208 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002209 * @RandomChallenge: pointer to the entry of host challenge random number array.
2210 * @HashWorking: pointer to the entry of the working hash array.
2211 *
2212 * This routine calculates the working hash array referred by @HashWorking
2213 * from the challenge random numbers associated with the host, referred by
2214 * @RandomChallenge. The result is put into the entry of the working hash
2215 * array and returned by reference through @HashWorking.
2216 **/
dea31012005-04-17 16:05:31 -05002217static void
2218lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2219{
2220 *HashWorking = (*RandomChallenge ^ *HashWorking);
2221}
2222
James Smarte59058c2008-08-24 21:49:00 -04002223/**
James Smart3621a712009-04-06 18:47:14 -04002224 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002225 * @phba: pointer to lpfc hba data structure.
2226 * @hbainit: pointer to an array of unsigned 32-bit integers.
2227 *
2228 * This routine performs the special handling for LC HBA initialization.
2229 **/
dea31012005-04-17 16:05:31 -05002230void
2231lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2232{
2233 int t;
2234 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002235 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002236
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002237 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002238 if (!HashWorking)
2239 return;
2240
dea31012005-04-17 16:05:31 -05002241 HashWorking[0] = HashWorking[78] = *pwwnn++;
2242 HashWorking[1] = HashWorking[79] = *pwwnn;
2243
2244 for (t = 0; t < 7; t++)
2245 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2246
2247 lpfc_sha_init(hbainit);
2248 lpfc_sha_iterate(hbainit, HashWorking);
2249 kfree(HashWorking);
2250}
2251
James Smarte59058c2008-08-24 21:49:00 -04002252/**
James Smart3621a712009-04-06 18:47:14 -04002253 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002254 * @vport: pointer to a virtual N_Port data structure.
2255 *
2256 * This routine performs the necessary cleanups before deleting the @vport.
2257 * It invokes the discovery state machine to perform necessary state
2258 * transitions and to release the ndlps associated with the @vport. Note,
2259 * the physical port is treated as @vport 0.
2260 **/
James Smart87af33f2007-10-27 13:37:43 -04002261void
James Smart2e0fef82007-06-17 19:56:36 -05002262lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002263{
James Smart87af33f2007-10-27 13:37:43 -04002264 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002265 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002266 int i = 0;
dea31012005-04-17 16:05:31 -05002267
James Smart87af33f2007-10-27 13:37:43 -04002268 if (phba->link_state > LPFC_LINK_DOWN)
2269 lpfc_port_link_failure(vport);
2270
2271 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002272 if (!NLP_CHK_NODE_ACT(ndlp)) {
2273 ndlp = lpfc_enable_node(vport, ndlp,
2274 NLP_STE_UNUSED_NODE);
2275 if (!ndlp)
2276 continue;
2277 spin_lock_irq(&phba->ndlp_lock);
2278 NLP_SET_FREE_REQ(ndlp);
2279 spin_unlock_irq(&phba->ndlp_lock);
2280 /* Trigger the release of the ndlp memory */
2281 lpfc_nlp_put(ndlp);
2282 continue;
2283 }
2284 spin_lock_irq(&phba->ndlp_lock);
2285 if (NLP_CHK_FREE_REQ(ndlp)) {
2286 /* The ndlp should not be in memory free mode already */
2287 spin_unlock_irq(&phba->ndlp_lock);
2288 continue;
2289 } else
2290 /* Indicate request for freeing ndlp memory */
2291 NLP_SET_FREE_REQ(ndlp);
2292 spin_unlock_irq(&phba->ndlp_lock);
2293
James Smart58da1ff2008-04-07 10:15:56 -04002294 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2295 ndlp->nlp_DID == Fabric_DID) {
2296 /* Just free up ndlp with Fabric_DID for vports */
2297 lpfc_nlp_put(ndlp);
2298 continue;
2299 }
2300
James Smart87af33f2007-10-27 13:37:43 -04002301 if (ndlp->nlp_type & NLP_FABRIC)
2302 lpfc_disc_state_machine(vport, ndlp, NULL,
2303 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002304
James Smart87af33f2007-10-27 13:37:43 -04002305 lpfc_disc_state_machine(vport, ndlp, NULL,
2306 NLP_EVT_DEVICE_RM);
James Smart495a7142008-06-14 22:52:59 -04002307
James Smart87af33f2007-10-27 13:37:43 -04002308 }
2309
James Smarta8adb832007-10-27 13:37:53 -04002310 /* At this point, ALL ndlp's should be gone
2311 * because of the previous NLP_EVT_DEVICE_RM.
2312 * Lets wait for this to happen, if needed.
2313 */
James Smart87af33f2007-10-27 13:37:43 -04002314 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002315 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002316 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002317 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002318 list_for_each_entry_safe(ndlp, next_ndlp,
2319 &vport->fc_nodes, nlp_listp) {
2320 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2321 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002322 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002323 "usgmap:x%x refcnt:%d\n",
2324 ndlp->nlp_DID, (void *)ndlp,
2325 ndlp->nlp_usg_map,
2326 atomic_read(
2327 &ndlp->kref.refcount));
2328 }
James Smarta8adb832007-10-27 13:37:53 -04002329 break;
James Smart87af33f2007-10-27 13:37:43 -04002330 }
James Smarta8adb832007-10-27 13:37:53 -04002331
2332 /* Wait for any activity on ndlps to settle */
2333 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002334 }
James Smart1151e3e2011-02-16 12:39:35 -05002335 lpfc_cleanup_vports_rrqs(vport, NULL);
dea31012005-04-17 16:05:31 -05002336}
2337
James Smarte59058c2008-08-24 21:49:00 -04002338/**
James Smart3621a712009-04-06 18:47:14 -04002339 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002340 * @vport: pointer to a virtual N_Port data structure.
2341 *
2342 * This routine stops all the timers associated with a @vport. This function
2343 * is invoked before disabling or deleting a @vport. Note that the physical
2344 * port is treated as @vport 0.
2345 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002346void
2347lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002348{
James Smart92d7f7b2007-06-17 19:56:38 -05002349 del_timer_sync(&vport->els_tmofunc);
2350 del_timer_sync(&vport->fc_fdmitmo);
James Smart92494142011-02-16 12:39:44 -05002351 del_timer_sync(&vport->delayed_disc_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002352 lpfc_can_disctmo(vport);
2353 return;
dea31012005-04-17 16:05:31 -05002354}
2355
James Smarte59058c2008-08-24 21:49:00 -04002356/**
James Smartecfd03c2010-02-12 14:41:27 -05002357 * __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. The
2361 * caller of this routine should already hold the host lock.
2362 **/
2363void
2364__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2365{
James Smart5ac6b302010-10-22 11:05:36 -04002366 /* Clear pending FCF rediscovery wait flag */
2367 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2368
James Smartecfd03c2010-02-12 14:41:27 -05002369 /* Now, try to stop the timer */
2370 del_timer(&phba->fcf.redisc_wait);
2371}
2372
2373/**
2374 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2375 * @phba: pointer to lpfc hba data structure.
2376 *
2377 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2378 * checks whether the FCF rediscovery wait timer is pending with the host
2379 * lock held before proceeding with disabling the timer and clearing the
2380 * wait timer pendig flag.
2381 **/
2382void
2383lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2384{
2385 spin_lock_irq(&phba->hbalock);
2386 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2387 /* FCF rediscovery timer already fired or stopped */
2388 spin_unlock_irq(&phba->hbalock);
2389 return;
2390 }
2391 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart5ac6b302010-10-22 11:05:36 -04002392 /* Clear failover in progress flags */
2393 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05002394 spin_unlock_irq(&phba->hbalock);
2395}
2396
2397/**
James Smart3772a992009-05-22 14:50:54 -04002398 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002399 * @phba: pointer to lpfc hba data structure.
2400 *
2401 * This routine stops all the timers associated with a HBA. This function is
2402 * invoked before either putting a HBA offline or unloading the driver.
2403 **/
James Smart3772a992009-05-22 14:50:54 -04002404void
2405lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002406{
James Smart51ef4c22007-08-02 11:10:31 -04002407 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05002408 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002409 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002410 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002411 del_timer_sync(&phba->hb_tmofunc);
James Smart1151e3e2011-02-16 12:39:35 -05002412 if (phba->sli_rev == LPFC_SLI_REV4) {
2413 del_timer_sync(&phba->rrq_tmr);
2414 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2415 }
James Smart3772a992009-05-22 14:50:54 -04002416 phba->hb_outstanding = 0;
2417
2418 switch (phba->pci_dev_grp) {
2419 case LPFC_PCI_DEV_LP:
2420 /* Stop any LightPulse device specific driver timers */
2421 del_timer_sync(&phba->fcp_poll_timer);
2422 break;
2423 case LPFC_PCI_DEV_OC:
2424 /* Stop any OneConnect device sepcific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05002425 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04002426 break;
2427 default:
2428 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2429 "0297 Invalid device group (x%x)\n",
2430 phba->pci_dev_grp);
2431 break;
2432 }
James Smart2e0fef82007-06-17 19:56:36 -05002433 return;
dea31012005-04-17 16:05:31 -05002434}
2435
James Smarte59058c2008-08-24 21:49:00 -04002436/**
James Smart3621a712009-04-06 18:47:14 -04002437 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002438 * @phba: pointer to lpfc hba data structure.
2439 *
2440 * This routine marks a HBA's management interface as blocked. Once the HBA's
2441 * management interface is marked as blocked, all the user space access to
2442 * the HBA, whether they are from sysfs interface or libdfc interface will
2443 * all be blocked. The HBA is set to block the management interface when the
2444 * driver prepares the HBA interface for online or offline.
2445 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002446static void
2447lpfc_block_mgmt_io(struct lpfc_hba * phba)
2448{
2449 unsigned long iflag;
James Smart6e7288d2010-06-07 15:23:35 -04002450 uint8_t actcmd = MBX_HEARTBEAT;
2451 unsigned long timeout;
2452
James Smarta183a152011-10-10 21:32:43 -04002453 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
Adrian Bunka6ababd2007-11-05 18:07:33 +01002454 spin_lock_irqsave(&phba->hbalock, iflag);
2455 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
James Smarta183a152011-10-10 21:32:43 -04002456 if (phba->sli.mbox_active) {
James Smart6e7288d2010-06-07 15:23:35 -04002457 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
James Smarta183a152011-10-10 21:32:43 -04002458 /* Determine how long we might wait for the active mailbox
2459 * command to be gracefully completed by firmware.
2460 */
2461 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
2462 phba->sli.mbox_active) * 1000) + jiffies;
2463 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002464 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta183a152011-10-10 21:32:43 -04002465
James Smart6e7288d2010-06-07 15:23:35 -04002466 /* Wait for the outstnading mailbox command to complete */
2467 while (phba->sli.mbox_active) {
2468 /* Check active mailbox complete status every 2ms */
2469 msleep(2);
2470 if (time_after(jiffies, timeout)) {
2471 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2472 "2813 Mgmt IO is Blocked %x "
2473 "- mbox cmd %x still active\n",
2474 phba->sli.sli_flag, actcmd);
2475 break;
2476 }
2477 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002478}
2479
James Smarte59058c2008-08-24 21:49:00 -04002480/**
James Smart3621a712009-04-06 18:47:14 -04002481 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04002482 * @phba: pointer to lpfc hba data structure.
2483 *
2484 * This routine initializes the HBA and brings a HBA online. During this
2485 * process, the management interface is blocked to prevent user space access
2486 * to the HBA interfering with the driver initialization.
2487 *
2488 * Return codes
2489 * 0 - successful
2490 * 1 - failed
2491 **/
dea31012005-04-17 16:05:31 -05002492int
James Smart2e0fef82007-06-17 19:56:36 -05002493lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002494{
Julia Lawall372bd282008-12-16 16:15:08 +01002495 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04002496 struct lpfc_vport **vports;
2497 int i;
James Smart2e0fef82007-06-17 19:56:36 -05002498
dea31012005-04-17 16:05:31 -05002499 if (!phba)
2500 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01002501 vport = phba->pport;
dea31012005-04-17 16:05:31 -05002502
James Smart2e0fef82007-06-17 19:56:36 -05002503 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05002504 return 0;
2505
James Smarted957682007-06-17 19:56:37 -05002506 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002507 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05002508
James Smart46fa3112007-04-25 09:51:45 -04002509 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002510
James Smart46fa3112007-04-25 09:51:45 -04002511 if (!lpfc_sli_queue_setup(phba)) {
2512 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002513 return 1;
James Smart46fa3112007-04-25 09:51:45 -04002514 }
2515
James Smartda0436e2009-05-22 14:51:39 -04002516 if (phba->sli_rev == LPFC_SLI_REV4) {
2517 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2518 lpfc_unblock_mgmt_io(phba);
2519 return 1;
2520 }
2521 } else {
2522 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2523 lpfc_unblock_mgmt_io(phba);
2524 return 1;
2525 }
James Smart46fa3112007-04-25 09:51:45 -04002526 }
dea31012005-04-17 16:05:31 -05002527
James Smart549e55c2007-08-02 11:09:51 -04002528 vports = lpfc_create_vport_work_array(phba);
2529 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002530 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002531 struct Scsi_Host *shost;
2532 shost = lpfc_shost_from_vport(vports[i]);
2533 spin_lock_irq(shost->host_lock);
2534 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2535 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2536 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart1c6834a2009-07-19 10:01:26 -04002537 if (phba->sli_rev == LPFC_SLI_REV4)
2538 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart549e55c2007-08-02 11:09:51 -04002539 spin_unlock_irq(shost->host_lock);
2540 }
James Smart09372822008-01-11 01:52:54 -05002541 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002542
James Smart46fa3112007-04-25 09:51:45 -04002543 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002544 return 0;
2545}
2546
James Smarte59058c2008-08-24 21:49:00 -04002547/**
James Smart3621a712009-04-06 18:47:14 -04002548 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04002549 * @phba: pointer to lpfc hba data structure.
2550 *
2551 * This routine marks a HBA's management interface as not blocked. Once the
2552 * HBA's management interface is marked as not blocked, all the user space
2553 * access to the HBA, whether they are from sysfs interface or libdfc
2554 * interface will be allowed. The HBA is set to block the management interface
2555 * when the driver prepares the HBA interface for online or offline and then
2556 * set to unblock the management interface afterwards.
2557 **/
James Smart46fa3112007-04-25 09:51:45 -04002558void
James Smart46fa3112007-04-25 09:51:45 -04002559lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2560{
2561 unsigned long iflag;
2562
James Smart2e0fef82007-06-17 19:56:36 -05002563 spin_lock_irqsave(&phba->hbalock, iflag);
2564 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2565 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04002566}
2567
James Smarte59058c2008-08-24 21:49:00 -04002568/**
James Smart3621a712009-04-06 18:47:14 -04002569 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04002570 * @phba: pointer to lpfc hba data structure.
2571 *
2572 * This routine is invoked to prepare a HBA to be brought offline. It performs
2573 * unregistration login to all the nodes on all vports and flushes the mailbox
2574 * queue to make it ready to be brought offline.
2575 **/
James Smart46fa3112007-04-25 09:51:45 -04002576void
2577lpfc_offline_prep(struct lpfc_hba * phba)
2578{
James Smart2e0fef82007-06-17 19:56:36 -05002579 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04002580 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04002581 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05002582 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04002583 int i;
dea31012005-04-17 16:05:31 -05002584
James Smart2e0fef82007-06-17 19:56:36 -05002585 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002586 return;
dea31012005-04-17 16:05:31 -05002587
James Smart46fa3112007-04-25 09:51:45 -04002588 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002589
2590 lpfc_linkdown(phba);
2591
James Smart87af33f2007-10-27 13:37:43 -04002592 /* Issue an unreg_login to all nodes on all vports */
2593 vports = lpfc_create_vport_work_array(phba);
2594 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04002595 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04002596 if (vports[i]->load_flag & FC_UNLOADING)
2597 continue;
James Smart72100cc2010-02-12 14:43:01 -05002598 shost = lpfc_shost_from_vport(vports[i]);
2599 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002600 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002601 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2602 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05002603 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002604
James Smart87af33f2007-10-27 13:37:43 -04002605 shost = lpfc_shost_from_vport(vports[i]);
2606 list_for_each_entry_safe(ndlp, next_ndlp,
2607 &vports[i]->fc_nodes,
2608 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002609 if (!NLP_CHK_NODE_ACT(ndlp))
2610 continue;
James Smart87af33f2007-10-27 13:37:43 -04002611 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2612 continue;
2613 if (ndlp->nlp_type & NLP_FABRIC) {
2614 lpfc_disc_state_machine(vports[i], ndlp,
2615 NULL, NLP_EVT_DEVICE_RECOVERY);
2616 lpfc_disc_state_machine(vports[i], ndlp,
2617 NULL, NLP_EVT_DEVICE_RM);
2618 }
2619 spin_lock_irq(shost->host_lock);
2620 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2621 spin_unlock_irq(shost->host_lock);
2622 lpfc_unreg_rpi(vports[i], ndlp);
2623 }
2624 }
2625 }
James Smart09372822008-01-11 01:52:54 -05002626 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002627
James Smart04c68492009-05-22 14:52:52 -04002628 lpfc_sli_mbox_sys_shutdown(phba);
James Smart46fa3112007-04-25 09:51:45 -04002629}
2630
James Smarte59058c2008-08-24 21:49:00 -04002631/**
James Smart3621a712009-04-06 18:47:14 -04002632 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04002633 * @phba: pointer to lpfc hba data structure.
2634 *
2635 * This routine actually brings a HBA offline. It stops all the timers
2636 * associated with the HBA, brings down the SLI layer, and eventually
2637 * marks the HBA as in offline state for the upper layer protocol.
2638 **/
James Smart46fa3112007-04-25 09:51:45 -04002639void
James Smart2e0fef82007-06-17 19:56:36 -05002640lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04002641{
James Smart549e55c2007-08-02 11:09:51 -04002642 struct Scsi_Host *shost;
2643 struct lpfc_vport **vports;
2644 int i;
James Smart46fa3112007-04-25 09:51:45 -04002645
James Smart549e55c2007-08-02 11:09:51 -04002646 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002647 return;
James Smart688a8862006-07-06 15:49:56 -04002648
James Smartda0436e2009-05-22 14:51:39 -04002649 /* stop port and all timers associated with this hba */
2650 lpfc_stop_port(phba);
James Smart51ef4c22007-08-02 11:10:31 -04002651 vports = lpfc_create_vport_work_array(phba);
2652 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002653 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04002654 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05002655 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05002656 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002657 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05002658 /* Bring down the SLI Layer and cleanup. The HBA is offline
2659 now. */
2660 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002661 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04002662 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002663 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04002664 vports = lpfc_create_vport_work_array(phba);
2665 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002666 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002667 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04002668 spin_lock_irq(shost->host_lock);
2669 vports[i]->work_port_events = 0;
2670 vports[i]->fc_flag |= FC_OFFLINE_MODE;
2671 spin_unlock_irq(shost->host_lock);
2672 }
James Smart09372822008-01-11 01:52:54 -05002673 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002674}
2675
James Smarte59058c2008-08-24 21:49:00 -04002676/**
James Smart3621a712009-04-06 18:47:14 -04002677 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04002678 * @phba: pointer to lpfc hba data structure.
2679 *
2680 * This routine is to free all the SCSI buffers and IOCBs from the driver
2681 * list back to kernel. It is called from lpfc_pci_remove_one to free
2682 * the internal resources before the device is removed from the system.
2683 *
2684 * Return codes
2685 * 0 - successful (for now, it always returns 0)
2686 **/
dea31012005-04-17 16:05:31 -05002687static int
James Smart2e0fef82007-06-17 19:56:36 -05002688lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002689{
2690 struct lpfc_scsi_buf *sb, *sb_next;
2691 struct lpfc_iocbq *io, *io_next;
2692
James Smart2e0fef82007-06-17 19:56:36 -05002693 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002694 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smart1c6f4ef52009-11-18 15:40:49 -05002695 spin_lock(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05002696 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
2697 list_del(&sb->list);
2698 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05002699 sb->dma_handle);
dea31012005-04-17 16:05:31 -05002700 kfree(sb);
2701 phba->total_scsi_bufs--;
2702 }
James Smart1c6f4ef52009-11-18 15:40:49 -05002703 spin_unlock(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05002704
2705 /* Release all the lpfc_iocbq entries maintained by this host. */
2706 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2707 list_del(&io->list);
2708 kfree(io);
2709 phba->total_iocbq_bufs--;
2710 }
James Smart6d368e52011-05-24 11:44:12 -04002711
James Smart2e0fef82007-06-17 19:56:36 -05002712 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002713 return 0;
2714}
2715
James Smarte59058c2008-08-24 21:49:00 -04002716/**
James Smart3621a712009-04-06 18:47:14 -04002717 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04002718 * @phba: pointer to lpfc hba data structure.
2719 * @instance: a unique integer ID to this FC port.
2720 * @dev: pointer to the device data structure.
2721 *
2722 * This routine creates a FC port for the upper layer protocol. The FC port
2723 * can be created on top of either a physical port or a virtual port provided
2724 * by the HBA. This routine also allocates a SCSI host data structure (shost)
2725 * and associates the FC port created before adding the shost into the SCSI
2726 * layer.
2727 *
2728 * Return codes
2729 * @vport - pointer to the virtual N_Port data structure.
2730 * NULL - port create failed.
2731 **/
James Smart2e0fef82007-06-17 19:56:36 -05002732struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04002733lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04002734{
James Smart2e0fef82007-06-17 19:56:36 -05002735 struct lpfc_vport *vport;
2736 struct Scsi_Host *shost;
2737 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04002738
James Smart3de2a652007-08-02 11:09:59 -04002739 if (dev != &phba->pcidev->dev)
2740 shost = scsi_host_alloc(&lpfc_vport_template,
2741 sizeof(struct lpfc_vport));
2742 else
2743 shost = scsi_host_alloc(&lpfc_template,
2744 sizeof(struct lpfc_vport));
James Smart2e0fef82007-06-17 19:56:36 -05002745 if (!shost)
2746 goto out;
James Smart47a86172007-04-25 09:53:22 -04002747
James Smart2e0fef82007-06-17 19:56:36 -05002748 vport = (struct lpfc_vport *) shost->hostdata;
2749 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05002750 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05002751 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05002752 vport->fc_rscn_flush = 0;
James Smart47a86172007-04-25 09:53:22 -04002753
James Smart3de2a652007-08-02 11:09:59 -04002754 lpfc_get_vport_cfgparam(vport);
James Smart2e0fef82007-06-17 19:56:36 -05002755 shost->unique_id = instance;
2756 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04002757 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05002758 shost->this_id = -1;
2759 shost->max_cmd_len = 16;
James Smartda0436e2009-05-22 14:51:39 -04002760 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart28baac72010-02-12 14:42:03 -05002761 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04002762 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smartda0436e2009-05-22 14:51:39 -04002763 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
2764 }
James Smart81301a92008-12-04 22:39:46 -05002765
James Smart47a86172007-04-25 09:53:22 -04002766 /*
James Smart2e0fef82007-06-17 19:56:36 -05002767 * Set initial can_queue value since 0 is no longer supported and
2768 * scsi_add_host will fail. This will be adjusted later based on the
2769 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04002770 */
James Smart2e0fef82007-06-17 19:56:36 -05002771 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04002772 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05002773 shost->transportt = lpfc_vport_transport_template;
2774 vport->port_type = LPFC_NPIV_PORT;
2775 } else {
2776 shost->transportt = lpfc_transport_template;
2777 vport->port_type = LPFC_PHYSICAL_PORT;
2778 }
James Smart47a86172007-04-25 09:53:22 -04002779
James Smart2e0fef82007-06-17 19:56:36 -05002780 /* Initialize all internally managed lists. */
2781 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04002782 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05002783 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04002784
James Smart2e0fef82007-06-17 19:56:36 -05002785 init_timer(&vport->fc_disctmo);
2786 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05002787 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002788
James Smart2e0fef82007-06-17 19:56:36 -05002789 init_timer(&vport->fc_fdmitmo);
2790 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
James Smart92d7f7b2007-06-17 19:56:38 -05002791 vport->fc_fdmitmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002792
James Smart2e0fef82007-06-17 19:56:36 -05002793 init_timer(&vport->els_tmofunc);
2794 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05002795 vport->els_tmofunc.data = (unsigned long)vport;
James Smart92494142011-02-16 12:39:44 -05002796
2797 init_timer(&vport->delayed_disc_tmo);
2798 vport->delayed_disc_tmo.function = lpfc_delayed_disc_tmo;
2799 vport->delayed_disc_tmo.data = (unsigned long)vport;
2800
James Bottomleyd139b9b2009-11-05 13:33:12 -06002801 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05002802 if (error)
2803 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04002804
James Smart549e55c2007-08-02 11:09:51 -04002805 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002806 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04002807 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002808 return vport;
James Smart47a86172007-04-25 09:53:22 -04002809
James Smart2e0fef82007-06-17 19:56:36 -05002810out_put_shost:
2811 scsi_host_put(shost);
2812out:
2813 return NULL;
James Smart47a86172007-04-25 09:53:22 -04002814}
2815
James Smarte59058c2008-08-24 21:49:00 -04002816/**
James Smart3621a712009-04-06 18:47:14 -04002817 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04002818 * @vport: pointer to an lpfc virtual N_Port data structure.
2819 *
2820 * This routine destroys a FC port from the upper layer protocol. All the
2821 * resources associated with the port are released.
2822 **/
James Smart2e0fef82007-06-17 19:56:36 -05002823void
2824destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04002825{
James Smart92d7f7b2007-06-17 19:56:38 -05002826 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2827 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002828
James Smart858c9f62007-06-17 19:56:39 -05002829 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05002830 fc_remove_host(shost);
2831 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04002832
James Smart92d7f7b2007-06-17 19:56:38 -05002833 spin_lock_irq(&phba->hbalock);
2834 list_del_init(&vport->listentry);
2835 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04002836
James Smart92d7f7b2007-06-17 19:56:38 -05002837 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04002838 return;
James Smart47a86172007-04-25 09:53:22 -04002839}
2840
James Smarte59058c2008-08-24 21:49:00 -04002841/**
James Smart3621a712009-04-06 18:47:14 -04002842 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04002843 *
2844 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
2845 * uses the kernel idr facility to perform the task.
2846 *
2847 * Return codes:
2848 * instance - a unique integer ID allocated as the new instance.
2849 * -1 - lpfc get instance failed.
2850 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002851int
2852lpfc_get_instance(void)
2853{
2854 int instance = 0;
2855
2856 /* Assign an unused number */
2857 if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
2858 return -1;
2859 if (idr_get_new(&lpfc_hba_index, NULL, &instance))
2860 return -1;
2861 return instance;
2862}
2863
James Smarte59058c2008-08-24 21:49:00 -04002864/**
James Smart3621a712009-04-06 18:47:14 -04002865 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04002866 * @shost: pointer to SCSI host data structure.
2867 * @time: elapsed time of the scan in jiffies.
2868 *
2869 * This routine is called by the SCSI layer with a SCSI host to determine
2870 * whether the scan host is finished.
2871 *
2872 * Note: there is no scan_start function as adapter initialization will have
2873 * asynchronously kicked off the link initialization.
2874 *
2875 * Return codes
2876 * 0 - SCSI host scan is not over yet.
2877 * 1 - SCSI host scan is over.
2878 **/
James Smart47a86172007-04-25 09:53:22 -04002879int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2880{
James Smart2e0fef82007-06-17 19:56:36 -05002881 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2882 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05002883 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04002884
James Smart858c9f62007-06-17 19:56:39 -05002885 spin_lock_irq(shost->host_lock);
2886
James Smart51ef4c22007-08-02 11:10:31 -04002887 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05002888 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002889 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05002890 }
James Smart2e0fef82007-06-17 19:56:36 -05002891 if (time >= 30 * HZ) {
2892 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002893 "0461 Scanning longer than 30 "
2894 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05002895 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002896 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002897 }
2898 if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
2899 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002900 "0465 Link down longer than 15 "
2901 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05002902 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05002903 goto finished;
James Smart47a86172007-04-25 09:53:22 -04002904 }
2905
James Smart2e0fef82007-06-17 19:56:36 -05002906 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05002907 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002908 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05002909 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002910 if (vport->fc_map_cnt == 0 && time < 2 * HZ)
James Smart858c9f62007-06-17 19:56:39 -05002911 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002912 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05002913 goto finished;
2914
2915 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002916
2917finished:
James Smart858c9f62007-06-17 19:56:39 -05002918 spin_unlock_irq(shost->host_lock);
2919 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05002920}
2921
James Smarte59058c2008-08-24 21:49:00 -04002922/**
James Smart3621a712009-04-06 18:47:14 -04002923 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04002924 * @shost: pointer to SCSI host data structure.
2925 *
2926 * This routine initializes a given SCSI host attributes on a FC port. The
2927 * SCSI host can be either on top of a physical port or a virtual port.
2928 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002929void lpfc_host_attrib_init(struct Scsi_Host *shost)
2930{
2931 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2932 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002933 /*
James Smart2e0fef82007-06-17 19:56:36 -05002934 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04002935 */
2936
James Smart2e0fef82007-06-17 19:56:36 -05002937 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
2938 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04002939 fc_host_supported_classes(shost) = FC_COS_CLASS3;
2940
2941 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05002942 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04002943 fc_host_supported_fc4s(shost)[2] = 1;
2944 fc_host_supported_fc4s(shost)[7] = 1;
2945
James Smart92d7f7b2007-06-17 19:56:38 -05002946 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
2947 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04002948
2949 fc_host_supported_speeds(shost) = 0;
James Smart88a2cfb2011-07-22 18:36:33 -04002950 if (phba->lmt & LMT_16Gb)
2951 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
James Smart47a86172007-04-25 09:53:22 -04002952 if (phba->lmt & LMT_10Gb)
2953 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04002954 if (phba->lmt & LMT_8Gb)
2955 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04002956 if (phba->lmt & LMT_4Gb)
2957 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
2958 if (phba->lmt & LMT_2Gb)
2959 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
2960 if (phba->lmt & LMT_1Gb)
2961 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
2962
2963 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05002964 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
2965 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04002966
Mike Christie0af5d702010-09-15 16:52:31 -05002967 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
2968
James Smart47a86172007-04-25 09:53:22 -04002969 /* This value is also unchanging */
2970 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05002971 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04002972 fc_host_active_fc4s(shost)[2] = 1;
2973 fc_host_active_fc4s(shost)[7] = 1;
2974
James Smart92d7f7b2007-06-17 19:56:38 -05002975 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04002976 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04002977 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04002978 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04002979}
dea31012005-04-17 16:05:31 -05002980
James Smarte59058c2008-08-24 21:49:00 -04002981/**
James Smartda0436e2009-05-22 14:51:39 -04002982 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04002983 * @phba: pointer to lpfc hba data structure.
2984 *
James Smartda0436e2009-05-22 14:51:39 -04002985 * This routine is invoked to stop an SLI3 device port, it stops the device
2986 * from generating interrupts and stops the device driver's timers for the
2987 * device.
James Smarte59058c2008-08-24 21:49:00 -04002988 **/
James Smartdb2378e2008-02-08 18:49:51 -05002989static void
James Smartda0436e2009-05-22 14:51:39 -04002990lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05002991{
James Smartda0436e2009-05-22 14:51:39 -04002992 /* Clear all interrupt enable conditions */
2993 writel(0, phba->HCregaddr);
2994 readl(phba->HCregaddr); /* flush */
2995 /* Clear all pending interrupts */
2996 writel(0xffffffff, phba->HAregaddr);
2997 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04002998
James Smartda0436e2009-05-22 14:51:39 -04002999 /* Reset some HBA SLI setup states */
3000 lpfc_stop_hba_timers(phba);
3001 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05003002}
3003
James Smarte59058c2008-08-24 21:49:00 -04003004/**
James Smartda0436e2009-05-22 14:51:39 -04003005 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05003006 * @phba: pointer to lpfc hba data structure.
3007 *
James Smartda0436e2009-05-22 14:51:39 -04003008 * This routine is invoked to stop an SLI4 device port, it stops the device
3009 * from generating interrupts and stops the device driver's timers for the
3010 * device.
3011 **/
3012static void
3013lpfc_stop_port_s4(struct lpfc_hba *phba)
3014{
3015 /* Reset some HBA SLI4 setup states */
3016 lpfc_stop_hba_timers(phba);
3017 phba->pport->work_port_events = 0;
3018 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04003019}
3020
3021/**
3022 * lpfc_stop_port - Wrapper function for stopping hba port
3023 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05003024 *
James Smartda0436e2009-05-22 14:51:39 -04003025 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
3026 * the API jump table function pointer from the lpfc_hba struct.
3027 **/
3028void
3029lpfc_stop_port(struct lpfc_hba *phba)
3030{
3031 phba->lpfc_stop_port(phba);
3032}
3033
3034/**
James Smartecfd03c2010-02-12 14:41:27 -05003035 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
3036 * @phba: Pointer to hba for which this call is being executed.
3037 *
3038 * This routine starts the timer waiting for the FCF rediscovery to complete.
3039 **/
3040void
3041lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
3042{
3043 unsigned long fcf_redisc_wait_tmo =
3044 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
3045 /* Start fcf rediscovery wait period timer */
3046 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
3047 spin_lock_irq(&phba->hbalock);
3048 /* Allow action to new fcf asynchronous event */
3049 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
3050 /* Mark the FCF rediscovery pending state */
3051 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
3052 spin_unlock_irq(&phba->hbalock);
3053}
3054
3055/**
3056 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
3057 * @ptr: Map to lpfc_hba data structure pointer.
3058 *
3059 * This routine is invoked when waiting for FCF table rediscover has been
3060 * timed out. If new FCF record(s) has (have) been discovered during the
3061 * wait period, a new FCF event shall be added to the FCOE async event
3062 * list, and then worker thread shall be waked up for processing from the
3063 * worker thread context.
3064 **/
3065void
3066lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
3067{
3068 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
3069
3070 /* Don't send FCF rediscovery event if timer cancelled */
3071 spin_lock_irq(&phba->hbalock);
3072 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
3073 spin_unlock_irq(&phba->hbalock);
3074 return;
3075 }
3076 /* Clear FCF rediscovery timer pending flag */
3077 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
3078 /* FCF rediscovery event to worker thread */
3079 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
3080 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003081 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04003082 "2776 FCF rediscover quiescent timer expired\n");
James Smartecfd03c2010-02-12 14:41:27 -05003083 /* wake up worker thread */
3084 lpfc_worker_wake_up(phba);
3085}
3086
3087/**
James Smartda0436e2009-05-22 14:51:39 -04003088 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
3089 * @phba: pointer to lpfc hba data structure.
3090 * @acqe_link: pointer to the async link completion queue entry.
3091 *
3092 * This routine is to parse the SLI4 link-attention link fault code and
3093 * translate it into the base driver's read link attention mailbox command
3094 * status.
3095 *
3096 * Return: Link-attention status in terms of base driver's coding.
3097 **/
3098static uint16_t
3099lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
3100 struct lpfc_acqe_link *acqe_link)
3101{
3102 uint16_t latt_fault;
3103
3104 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
3105 case LPFC_ASYNC_LINK_FAULT_NONE:
3106 case LPFC_ASYNC_LINK_FAULT_LOCAL:
3107 case LPFC_ASYNC_LINK_FAULT_REMOTE:
3108 latt_fault = 0;
3109 break;
3110 default:
3111 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3112 "0398 Invalid link fault code: x%x\n",
3113 bf_get(lpfc_acqe_link_fault, acqe_link));
3114 latt_fault = MBXERR_ERROR;
3115 break;
3116 }
3117 return latt_fault;
3118}
3119
3120/**
3121 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
3122 * @phba: pointer to lpfc hba data structure.
3123 * @acqe_link: pointer to the async link completion queue entry.
3124 *
3125 * This routine is to parse the SLI4 link attention type and translate it
3126 * into the base driver's link attention type coding.
3127 *
3128 * Return: Link attention type in terms of base driver's coding.
3129 **/
3130static uint8_t
3131lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
3132 struct lpfc_acqe_link *acqe_link)
3133{
3134 uint8_t att_type;
3135
3136 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
3137 case LPFC_ASYNC_LINK_STATUS_DOWN:
3138 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
James Smart76a95d72010-11-20 23:11:48 -05003139 att_type = LPFC_ATT_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04003140 break;
3141 case LPFC_ASYNC_LINK_STATUS_UP:
3142 /* Ignore physical link up events - wait for logical link up */
James Smart76a95d72010-11-20 23:11:48 -05003143 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003144 break;
3145 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
James Smart76a95d72010-11-20 23:11:48 -05003146 att_type = LPFC_ATT_LINK_UP;
James Smartda0436e2009-05-22 14:51:39 -04003147 break;
3148 default:
3149 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3150 "0399 Invalid link attention type: x%x\n",
3151 bf_get(lpfc_acqe_link_status, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003152 att_type = LPFC_ATT_RESERVED;
James Smartda0436e2009-05-22 14:51:39 -04003153 break;
3154 }
3155 return att_type;
3156}
3157
3158/**
3159 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
3160 * @phba: pointer to lpfc hba data structure.
3161 * @acqe_link: pointer to the async link completion queue entry.
3162 *
3163 * This routine is to parse the SLI4 link-attention link speed and translate
3164 * it into the base driver's link-attention link speed coding.
3165 *
3166 * Return: Link-attention link speed in terms of base driver's coding.
3167 **/
3168static uint8_t
3169lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
3170 struct lpfc_acqe_link *acqe_link)
3171{
3172 uint8_t link_speed;
3173
3174 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
3175 case LPFC_ASYNC_LINK_SPEED_ZERO:
James Smartda0436e2009-05-22 14:51:39 -04003176 case LPFC_ASYNC_LINK_SPEED_10MBPS:
James Smartda0436e2009-05-22 14:51:39 -04003177 case LPFC_ASYNC_LINK_SPEED_100MBPS:
James Smart76a95d72010-11-20 23:11:48 -05003178 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003179 break;
3180 case LPFC_ASYNC_LINK_SPEED_1GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003181 link_speed = LPFC_LINK_SPEED_1GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003182 break;
3183 case LPFC_ASYNC_LINK_SPEED_10GBPS:
James Smart76a95d72010-11-20 23:11:48 -05003184 link_speed = LPFC_LINK_SPEED_10GHZ;
James Smartda0436e2009-05-22 14:51:39 -04003185 break;
3186 default:
3187 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3188 "0483 Invalid link-attention link speed: x%x\n",
3189 bf_get(lpfc_acqe_link_speed, acqe_link));
James Smart76a95d72010-11-20 23:11:48 -05003190 link_speed = LPFC_LINK_SPEED_UNKNOWN;
James Smartda0436e2009-05-22 14:51:39 -04003191 break;
3192 }
3193 return link_speed;
3194}
3195
3196/**
James Smart70f3c072010-12-15 17:57:33 -05003197 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
James Smartda0436e2009-05-22 14:51:39 -04003198 * @phba: pointer to lpfc hba data structure.
3199 * @acqe_link: pointer to the async link completion queue entry.
3200 *
James Smart70f3c072010-12-15 17:57:33 -05003201 * This routine is to handle the SLI4 asynchronous FCoE link event.
James Smartda0436e2009-05-22 14:51:39 -04003202 **/
3203static void
3204lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3205 struct lpfc_acqe_link *acqe_link)
3206{
3207 struct lpfc_dmabuf *mp;
3208 LPFC_MBOXQ_t *pmb;
3209 MAILBOX_t *mb;
James Smart76a95d72010-11-20 23:11:48 -05003210 struct lpfc_mbx_read_top *la;
James Smartda0436e2009-05-22 14:51:39 -04003211 uint8_t att_type;
James Smart76a95d72010-11-20 23:11:48 -05003212 int rc;
James Smartda0436e2009-05-22 14:51:39 -04003213
3214 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
James Smart76a95d72010-11-20 23:11:48 -05003215 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
James Smartda0436e2009-05-22 14:51:39 -04003216 return;
James Smart32b97932009-07-19 10:01:21 -04003217 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003218 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3219 if (!pmb) {
3220 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3221 "0395 The mboxq allocation failed\n");
3222 return;
3223 }
3224 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3225 if (!mp) {
3226 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3227 "0396 The lpfc_dmabuf allocation failed\n");
3228 goto out_free_pmb;
3229 }
3230 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3231 if (!mp->virt) {
3232 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3233 "0397 The mbuf allocation failed\n");
3234 goto out_free_dmabuf;
3235 }
3236
3237 /* Cleanup any outstanding ELS commands */
3238 lpfc_els_flush_all_cmd(phba);
3239
3240 /* Block ELS IOCBs until we have done process link event */
3241 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3242
3243 /* Update link event statistics */
3244 phba->sli.slistat.link_event++;
3245
James Smart76a95d72010-11-20 23:11:48 -05003246 /* Create lpfc_handle_latt mailbox command from link ACQE */
3247 lpfc_read_topology(phba, pmb, mp);
3248 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
James Smartda0436e2009-05-22 14:51:39 -04003249 pmb->vport = phba->pport;
3250
James Smartda0436e2009-05-22 14:51:39 -04003251 /* Keep the link status for extra SLI4 state machine reference */
3252 phba->sli4_hba.link_state.speed =
3253 bf_get(lpfc_acqe_link_speed, acqe_link);
3254 phba->sli4_hba.link_state.duplex =
3255 bf_get(lpfc_acqe_link_duplex, acqe_link);
3256 phba->sli4_hba.link_state.status =
3257 bf_get(lpfc_acqe_link_status, acqe_link);
James Smart70f3c072010-12-15 17:57:33 -05003258 phba->sli4_hba.link_state.type =
3259 bf_get(lpfc_acqe_link_type, acqe_link);
3260 phba->sli4_hba.link_state.number =
3261 bf_get(lpfc_acqe_link_number, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04003262 phba->sli4_hba.link_state.fault =
3263 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05003264 phba->sli4_hba.link_state.logical_speed =
James Smart70f3c072010-12-15 17:57:33 -05003265 bf_get(lpfc_acqe_logical_link_speed, acqe_link);
3266 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartc31098c2011-04-16 11:03:33 -04003267 "2900 Async FC/FCoE Link event - Speed:%dGBit "
3268 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
3269 "Logical speed:%dMbps Fault:%d\n",
James Smart70f3c072010-12-15 17:57:33 -05003270 phba->sli4_hba.link_state.speed,
3271 phba->sli4_hba.link_state.topology,
3272 phba->sli4_hba.link_state.status,
3273 phba->sli4_hba.link_state.type,
3274 phba->sli4_hba.link_state.number,
3275 phba->sli4_hba.link_state.logical_speed * 10,
3276 phba->sli4_hba.link_state.fault);
James Smart76a95d72010-11-20 23:11:48 -05003277 /*
3278 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
3279 * topology info. Note: Optional for non FC-AL ports.
3280 */
3281 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3282 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3283 if (rc == MBX_NOT_FINISHED)
3284 goto out_free_dmabuf;
3285 return;
3286 }
3287 /*
3288 * For FCoE Mode: fill in all the topology information we need and call
3289 * the READ_TOPOLOGY completion routine to continue without actually
3290 * sending the READ_TOPOLOGY mailbox command to the port.
3291 */
3292 /* Parse and translate status field */
3293 mb = &pmb->u.mb;
3294 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
3295
3296 /* Parse and translate link attention fields */
3297 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
3298 la->eventTag = acqe_link->event_tag;
3299 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
3300 bf_set(lpfc_mbx_read_top_link_spd, la,
3301 lpfc_sli4_parse_latt_link_speed(phba, acqe_link));
3302
3303 /* Fake the the following irrelvant fields */
3304 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
3305 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
3306 bf_set(lpfc_mbx_read_top_il, la, 0);
3307 bf_set(lpfc_mbx_read_top_pb, la, 0);
3308 bf_set(lpfc_mbx_read_top_fa, la, 0);
3309 bf_set(lpfc_mbx_read_top_mm, la, 0);
James Smartda0436e2009-05-22 14:51:39 -04003310
3311 /* Invoke the lpfc_handle_latt mailbox command callback function */
James Smart76a95d72010-11-20 23:11:48 -05003312 lpfc_mbx_cmpl_read_topology(phba, pmb);
James Smartda0436e2009-05-22 14:51:39 -04003313
3314 return;
3315
3316out_free_dmabuf:
3317 kfree(mp);
3318out_free_pmb:
3319 mempool_free(pmb, phba->mbox_mem_pool);
3320}
3321
3322/**
James Smart70f3c072010-12-15 17:57:33 -05003323 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
3324 * @phba: pointer to lpfc hba data structure.
3325 * @acqe_fc: pointer to the async fc completion queue entry.
3326 *
3327 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
3328 * that the event was received and then issue a read_topology mailbox command so
3329 * that the rest of the driver will treat it the same as SLI3.
3330 **/
3331static void
3332lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
3333{
3334 struct lpfc_dmabuf *mp;
3335 LPFC_MBOXQ_t *pmb;
3336 int rc;
3337
3338 if (bf_get(lpfc_trailer_type, acqe_fc) !=
3339 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
3340 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3341 "2895 Non FC link Event detected.(%d)\n",
3342 bf_get(lpfc_trailer_type, acqe_fc));
3343 return;
3344 }
3345 /* Keep the link status for extra SLI4 state machine reference */
3346 phba->sli4_hba.link_state.speed =
3347 bf_get(lpfc_acqe_fc_la_speed, acqe_fc);
3348 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
3349 phba->sli4_hba.link_state.topology =
3350 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
3351 phba->sli4_hba.link_state.status =
3352 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
3353 phba->sli4_hba.link_state.type =
3354 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
3355 phba->sli4_hba.link_state.number =
3356 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
3357 phba->sli4_hba.link_state.fault =
3358 bf_get(lpfc_acqe_link_fault, acqe_fc);
3359 phba->sli4_hba.link_state.logical_speed =
3360 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc);
3361 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3362 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
3363 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
3364 "%dMbps Fault:%d\n",
3365 phba->sli4_hba.link_state.speed,
3366 phba->sli4_hba.link_state.topology,
3367 phba->sli4_hba.link_state.status,
3368 phba->sli4_hba.link_state.type,
3369 phba->sli4_hba.link_state.number,
3370 phba->sli4_hba.link_state.logical_speed * 10,
3371 phba->sli4_hba.link_state.fault);
3372 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3373 if (!pmb) {
3374 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3375 "2897 The mboxq allocation failed\n");
3376 return;
3377 }
3378 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3379 if (!mp) {
3380 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3381 "2898 The lpfc_dmabuf allocation failed\n");
3382 goto out_free_pmb;
3383 }
3384 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3385 if (!mp->virt) {
3386 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3387 "2899 The mbuf allocation failed\n");
3388 goto out_free_dmabuf;
3389 }
3390
3391 /* Cleanup any outstanding ELS commands */
3392 lpfc_els_flush_all_cmd(phba);
3393
3394 /* Block ELS IOCBs until we have done process link event */
3395 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3396
3397 /* Update link event statistics */
3398 phba->sli.slistat.link_event++;
3399
3400 /* Create lpfc_handle_latt mailbox command from link ACQE */
3401 lpfc_read_topology(phba, pmb, mp);
3402 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
3403 pmb->vport = phba->pport;
3404
3405 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3406 if (rc == MBX_NOT_FINISHED)
3407 goto out_free_dmabuf;
3408 return;
3409
3410out_free_dmabuf:
3411 kfree(mp);
3412out_free_pmb:
3413 mempool_free(pmb, phba->mbox_mem_pool);
3414}
3415
3416/**
3417 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
3418 * @phba: pointer to lpfc hba data structure.
3419 * @acqe_fc: pointer to the async SLI completion queue entry.
3420 *
3421 * This routine is to handle the SLI4 asynchronous SLI events.
3422 **/
3423static void
3424lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
3425{
3426 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3427 "2901 Async SLI event - Event Data1:x%08x Event Data2:"
3428 "x%08x SLI Event Type:%d",
3429 acqe_sli->event_data1, acqe_sli->event_data2,
3430 bf_get(lpfc_trailer_type, acqe_sli));
3431 return;
3432}
3433
3434/**
James Smartfc2b9892010-02-26 14:15:29 -05003435 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
3436 * @vport: pointer to vport data structure.
3437 *
3438 * This routine is to perform Clear Virtual Link (CVL) on a vport in
3439 * response to a CVL event.
3440 *
3441 * Return the pointer to the ndlp with the vport if successful, otherwise
3442 * return NULL.
3443 **/
3444static struct lpfc_nodelist *
3445lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
3446{
3447 struct lpfc_nodelist *ndlp;
3448 struct Scsi_Host *shost;
3449 struct lpfc_hba *phba;
3450
3451 if (!vport)
3452 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05003453 phba = vport->phba;
3454 if (!phba)
3455 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04003456 ndlp = lpfc_findnode_did(vport, Fabric_DID);
3457 if (!ndlp) {
3458 /* Cannot find existing Fabric ndlp, so allocate a new one */
3459 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
3460 if (!ndlp)
3461 return 0;
3462 lpfc_nlp_init(vport, ndlp, Fabric_DID);
3463 /* Set the node type */
3464 ndlp->nlp_type |= NLP_FABRIC;
3465 /* Put ndlp onto node list */
3466 lpfc_enqueue_node(vport, ndlp);
3467 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
3468 /* re-setup ndlp without removing from node list */
3469 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
3470 if (!ndlp)
3471 return 0;
3472 }
James Smart63e801c2010-11-20 23:14:19 -05003473 if ((phba->pport->port_state < LPFC_FLOGI) &&
3474 (phba->pport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05003475 return NULL;
3476 /* If virtual link is not yet instantiated ignore CVL */
James Smart63e801c2010-11-20 23:14:19 -05003477 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
3478 && (vport->port_state != LPFC_VPORT_FAILED))
James Smartfc2b9892010-02-26 14:15:29 -05003479 return NULL;
3480 shost = lpfc_shost_from_vport(vport);
3481 if (!shost)
3482 return NULL;
3483 lpfc_linkdown_port(vport);
3484 lpfc_cleanup_pending_mbox(vport);
3485 spin_lock_irq(shost->host_lock);
3486 vport->fc_flag |= FC_VPORT_CVL_RCVD;
3487 spin_unlock_irq(shost->host_lock);
3488
3489 return ndlp;
3490}
3491
3492/**
3493 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
3494 * @vport: pointer to lpfc hba data structure.
3495 *
3496 * This routine is to perform Clear Virtual Link (CVL) on all vports in
3497 * response to a FCF dead event.
3498 **/
3499static void
3500lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
3501{
3502 struct lpfc_vport **vports;
3503 int i;
3504
3505 vports = lpfc_create_vport_work_array(phba);
3506 if (vports)
3507 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
3508 lpfc_sli4_perform_vport_cvl(vports[i]);
3509 lpfc_destroy_vport_work_array(phba, vports);
3510}
3511
3512/**
James Smart76a95d72010-11-20 23:11:48 -05003513 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
James Smartda0436e2009-05-22 14:51:39 -04003514 * @phba: pointer to lpfc hba data structure.
3515 * @acqe_link: pointer to the async fcoe completion queue entry.
3516 *
3517 * This routine is to handle the SLI4 asynchronous fcoe event.
3518 **/
3519static void
James Smart76a95d72010-11-20 23:11:48 -05003520lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
James Smart70f3c072010-12-15 17:57:33 -05003521 struct lpfc_acqe_fip *acqe_fip)
James Smartda0436e2009-05-22 14:51:39 -04003522{
James Smart70f3c072010-12-15 17:57:33 -05003523 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04003524 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04003525 struct lpfc_vport *vport;
3526 struct lpfc_nodelist *ndlp;
3527 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05003528 int active_vlink_present;
3529 struct lpfc_vport **vports;
3530 int i;
James Smartda0436e2009-05-22 14:51:39 -04003531
James Smart70f3c072010-12-15 17:57:33 -05003532 phba->fc_eventTag = acqe_fip->event_tag;
3533 phba->fcoe_eventtag = acqe_fip->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003534 switch (event_type) {
James Smart70f3c072010-12-15 17:57:33 -05003535 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
3536 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
3537 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
James Smart999d8132010-03-15 11:24:56 -04003538 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3539 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003540 "2546 New FCF event, evt_tag:x%x, "
3541 "index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05003542 acqe_fip->event_tag,
3543 acqe_fip->index);
James Smart999d8132010-03-15 11:24:56 -04003544 else
3545 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
3546 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003547 "2788 FCF param modified event, "
3548 "evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05003549 acqe_fip->event_tag,
3550 acqe_fip->index);
James Smart38b92ef2010-08-04 16:11:39 -04003551 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smart0c9ab6f2010-02-26 14:15:57 -05003552 /*
3553 * During period of FCF discovery, read the FCF
3554 * table record indexed by the event to update
James Smarta93ff372010-10-22 11:06:08 -04003555 * FCF roundrobin failover eligible FCF bmask.
James Smart0c9ab6f2010-02-26 14:15:57 -05003556 */
3557 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
3558 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003559 "2779 Read FCF (x%x) for updating "
3560 "roundrobin FCF failover bmask\n",
James Smart70f3c072010-12-15 17:57:33 -05003561 acqe_fip->index);
3562 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05003563 }
James Smart38b92ef2010-08-04 16:11:39 -04003564
3565 /* If the FCF discovery is in progress, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04003566 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04003567 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart38b92ef2010-08-04 16:11:39 -04003568 spin_unlock_irq(&phba->hbalock);
3569 break;
3570 }
3571 /* If fast FCF failover rescan event is pending, do nothing */
3572 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
3573 spin_unlock_irq(&phba->hbalock);
3574 break;
3575 }
3576
3577 /* If the FCF has been in discovered state, do nothing. */
James Smart3804dc82010-07-14 15:31:37 -04003578 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
3579 spin_unlock_irq(&phba->hbalock);
3580 break;
3581 }
3582 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04003583
James Smart0c9ab6f2010-02-26 14:15:57 -05003584 /* Otherwise, scan the entire FCF table and re-discover SAN */
3585 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003586 "2770 Start FCF table scan per async FCF "
3587 "event, evt_tag:x%x, index:x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05003588 acqe_fip->event_tag, acqe_fip->index);
James Smart0c9ab6f2010-02-26 14:15:57 -05003589 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3590 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04003591 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05003592 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3593 "2547 Issue FCF scan read FCF mailbox "
James Smarta93ff372010-10-22 11:06:08 -04003594 "command failed (x%x)\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04003595 break;
3596
James Smart70f3c072010-12-15 17:57:33 -05003597 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
James Smartda0436e2009-05-22 14:51:39 -04003598 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04003599 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smart70f3c072010-12-15 17:57:33 -05003600 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
3601 acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04003602 break;
3603
James Smart70f3c072010-12-15 17:57:33 -05003604 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
James Smart0c9ab6f2010-02-26 14:15:57 -05003605 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003606 "2549 FCF (x%x) disconnected from network, "
James Smart70f3c072010-12-15 17:57:33 -05003607 "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart38b92ef2010-08-04 16:11:39 -04003608 /*
3609 * If we are in the middle of FCF failover process, clear
3610 * the corresponding FCF bit in the roundrobin bitmap.
James Smartda0436e2009-05-22 14:51:39 -04003611 */
James Smartfc2b9892010-02-26 14:15:29 -05003612 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003613 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05003614 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003615 /* Update FLOGI FCF failover eligible FCF bmask */
James Smart70f3c072010-12-15 17:57:33 -05003616 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05003617 break;
3618 }
James Smart38b92ef2010-08-04 16:11:39 -04003619 spin_unlock_irq(&phba->hbalock);
3620
3621 /* If the event is not for currently used fcf do nothing */
James Smart70f3c072010-12-15 17:57:33 -05003622 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
James Smart38b92ef2010-08-04 16:11:39 -04003623 break;
3624
3625 /*
3626 * Otherwise, request the port to rediscover the entire FCF
3627 * table for a fast recovery from case that the current FCF
3628 * is no longer valid as we are not in the middle of FCF
3629 * failover process already.
3630 */
3631 spin_lock_irq(&phba->hbalock);
James Smartfc2b9892010-02-26 14:15:29 -05003632 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05003633 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003634 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04003635
James Smart0c9ab6f2010-02-26 14:15:57 -05003636 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3637 "2771 Start FCF fast failover process due to "
3638 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
James Smart70f3c072010-12-15 17:57:33 -05003639 "\n", acqe_fip->event_tag, acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05003640 rc = lpfc_sli4_redisc_fcf_table(phba);
3641 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05003642 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3643 LOG_DISCOVERY,
3644 "2772 Issue FCF rediscover mabilbox "
3645 "command failed, fail through to FCF "
3646 "dead event\n");
James Smartfc2b9892010-02-26 14:15:29 -05003647 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003648 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003649 spin_unlock_irq(&phba->hbalock);
3650 /*
3651 * Last resort will fail over by treating this
3652 * as a link down to FCF registration.
3653 */
3654 lpfc_sli4_fcf_dead_failthrough(phba);
James Smart38b92ef2010-08-04 16:11:39 -04003655 } else {
3656 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04003657 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart38b92ef2010-08-04 16:11:39 -04003658 /*
3659 * Handling fast FCF failover to a DEAD FCF event is
3660 * considered equalivant to receiving CVL to all vports.
James Smartfc2b9892010-02-26 14:15:29 -05003661 */
3662 lpfc_sli4_perform_all_vport_cvl(phba);
James Smart38b92ef2010-08-04 16:11:39 -04003663 }
James Smartda0436e2009-05-22 14:51:39 -04003664 break;
James Smart70f3c072010-12-15 17:57:33 -05003665 case LPFC_FIP_EVENT_TYPE_CVL:
James Smart0c9ab6f2010-02-26 14:15:57 -05003666 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6669f9b2009-10-02 15:16:45 -04003667 "2718 Clear Virtual Link Received for VPI 0x%x"
James Smart70f3c072010-12-15 17:57:33 -05003668 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
James Smart6d368e52011-05-24 11:44:12 -04003669
James Smart6669f9b2009-10-02 15:16:45 -04003670 vport = lpfc_find_vport_by_vpid(phba,
James Smart5248a742011-07-22 18:37:06 -04003671 acqe_fip->index);
James Smartfc2b9892010-02-26 14:15:29 -05003672 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04003673 if (!ndlp)
3674 break;
James Smart695a8142010-01-26 23:08:03 -05003675 active_vlink_present = 0;
3676
3677 vports = lpfc_create_vport_work_array(phba);
3678 if (vports) {
3679 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
3680 i++) {
3681 if ((!(vports[i]->fc_flag &
3682 FC_VPORT_CVL_RCVD)) &&
3683 (vports[i]->port_state > LPFC_FDISC)) {
3684 active_vlink_present = 1;
3685 break;
3686 }
3687 }
3688 lpfc_destroy_vport_work_array(phba, vports);
3689 }
3690
3691 if (active_vlink_present) {
3692 /*
3693 * If there are other active VLinks present,
3694 * re-instantiate the Vlink using FDISC.
3695 */
James Smart6669f9b2009-10-02 15:16:45 -04003696 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
James Smartfc2b9892010-02-26 14:15:29 -05003697 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04003698 spin_lock_irq(shost->host_lock);
3699 ndlp->nlp_flag |= NLP_DELAY_TMO;
3700 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05003701 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
3702 vport->port_state = LPFC_FDISC;
3703 } else {
James Smartecfd03c2010-02-12 14:41:27 -05003704 /*
3705 * Otherwise, we request port to rediscover
3706 * the entire FCF table for a fast recovery
3707 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05003708 * is no longer valid if we are not already
3709 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05003710 */
James Smartfc2b9892010-02-26 14:15:29 -05003711 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003712 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05003713 spin_unlock_irq(&phba->hbalock);
3714 break;
3715 }
3716 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05003717 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003718 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003719 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
3720 LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003721 "2773 Start FCF failover per CVL, "
James Smart70f3c072010-12-15 17:57:33 -05003722 "evt_tag:x%x\n", acqe_fip->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05003723 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05003724 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05003725 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3726 LOG_DISCOVERY,
3727 "2774 Issue FCF rediscover "
3728 "mabilbox command failed, "
3729 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05003730 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003731 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003732 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05003733 /*
3734 * Last resort will be re-try on the
3735 * the current registered FCF entry.
3736 */
3737 lpfc_retry_pport_discovery(phba);
James Smart38b92ef2010-08-04 16:11:39 -04003738 } else
3739 /*
3740 * Reset FCF roundrobin bmask for new
3741 * discovery.
3742 */
James Smart7d791df2011-07-22 18:37:52 -04003743 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart6669f9b2009-10-02 15:16:45 -04003744 }
3745 break;
James Smartda0436e2009-05-22 14:51:39 -04003746 default:
3747 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3748 "0288 Unknown FCoE event type 0x%x event tag "
James Smart70f3c072010-12-15 17:57:33 -05003749 "0x%x\n", event_type, acqe_fip->event_tag);
James Smartda0436e2009-05-22 14:51:39 -04003750 break;
3751 }
3752}
3753
3754/**
3755 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
3756 * @phba: pointer to lpfc hba data structure.
3757 * @acqe_link: pointer to the async dcbx completion queue entry.
3758 *
3759 * This routine is to handle the SLI4 asynchronous dcbx event.
3760 **/
3761static void
3762lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
3763 struct lpfc_acqe_dcbx *acqe_dcbx)
3764{
James Smart4d9ab992009-10-02 15:16:39 -04003765 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003766 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3767 "0290 The SLI4 DCBX asynchronous event is not "
3768 "handled yet\n");
3769}
3770
3771/**
James Smartb19a0612010-04-06 14:48:51 -04003772 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
3773 * @phba: pointer to lpfc hba data structure.
3774 * @acqe_link: pointer to the async grp5 completion queue entry.
3775 *
3776 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
3777 * is an asynchronous notified of a logical link speed change. The Port
3778 * reports the logical link speed in units of 10Mbps.
3779 **/
3780static void
3781lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
3782 struct lpfc_acqe_grp5 *acqe_grp5)
3783{
3784 uint16_t prev_ll_spd;
3785
3786 phba->fc_eventTag = acqe_grp5->event_tag;
3787 phba->fcoe_eventtag = acqe_grp5->event_tag;
3788 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
3789 phba->sli4_hba.link_state.logical_speed =
3790 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5));
3791 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3792 "2789 GRP5 Async Event: Updating logical link speed "
3793 "from %dMbps to %dMbps\n", (prev_ll_spd * 10),
3794 (phba->sli4_hba.link_state.logical_speed*10));
3795}
3796
3797/**
James Smartda0436e2009-05-22 14:51:39 -04003798 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
3799 * @phba: pointer to lpfc hba data structure.
3800 *
3801 * This routine is invoked by the worker thread to process all the pending
3802 * SLI4 asynchronous events.
3803 **/
3804void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
3805{
3806 struct lpfc_cq_event *cq_event;
3807
3808 /* First, declare the async event has been handled */
3809 spin_lock_irq(&phba->hbalock);
3810 phba->hba_flag &= ~ASYNC_EVENT;
3811 spin_unlock_irq(&phba->hbalock);
3812 /* Now, handle all the async events */
3813 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
3814 /* Get the first event from the head of the event queue */
3815 spin_lock_irq(&phba->hbalock);
3816 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
3817 cq_event, struct lpfc_cq_event, list);
3818 spin_unlock_irq(&phba->hbalock);
3819 /* Process the asynchronous event */
3820 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
3821 case LPFC_TRAILER_CODE_LINK:
3822 lpfc_sli4_async_link_evt(phba,
3823 &cq_event->cqe.acqe_link);
3824 break;
3825 case LPFC_TRAILER_CODE_FCOE:
James Smart70f3c072010-12-15 17:57:33 -05003826 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
James Smartda0436e2009-05-22 14:51:39 -04003827 break;
3828 case LPFC_TRAILER_CODE_DCBX:
3829 lpfc_sli4_async_dcbx_evt(phba,
3830 &cq_event->cqe.acqe_dcbx);
3831 break;
James Smartb19a0612010-04-06 14:48:51 -04003832 case LPFC_TRAILER_CODE_GRP5:
3833 lpfc_sli4_async_grp5_evt(phba,
3834 &cq_event->cqe.acqe_grp5);
3835 break;
James Smart70f3c072010-12-15 17:57:33 -05003836 case LPFC_TRAILER_CODE_FC:
3837 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
3838 break;
3839 case LPFC_TRAILER_CODE_SLI:
3840 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
3841 break;
James Smartda0436e2009-05-22 14:51:39 -04003842 default:
3843 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3844 "1804 Invalid asynchrous event code: "
3845 "x%x\n", bf_get(lpfc_trailer_code,
3846 &cq_event->cqe.mcqe_cmpl));
3847 break;
3848 }
3849 /* Free the completion event processed to the free pool */
3850 lpfc_sli4_cq_event_release(phba, cq_event);
3851 }
3852}
3853
3854/**
James Smartecfd03c2010-02-12 14:41:27 -05003855 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
3856 * @phba: pointer to lpfc hba data structure.
3857 *
3858 * This routine is invoked by the worker thread to process FCF table
3859 * rediscovery pending completion event.
3860 **/
3861void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
3862{
3863 int rc;
3864
3865 spin_lock_irq(&phba->hbalock);
3866 /* Clear FCF rediscovery timeout event */
3867 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
3868 /* Clear driver fast failover FCF record flag */
3869 phba->fcf.failover_rec.flag = 0;
3870 /* Set state for FCF fast failover */
3871 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
3872 spin_unlock_irq(&phba->hbalock);
3873
3874 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05003875 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
James Smarta93ff372010-10-22 11:06:08 -04003876 "2777 Start post-quiescent FCF table scan\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05003877 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05003878 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05003879 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3880 "2747 Issue FCF scan read FCF mailbox "
3881 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05003882}
3883
3884/**
James Smartda0436e2009-05-22 14:51:39 -04003885 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
3886 * @phba: pointer to lpfc hba data structure.
3887 * @dev_grp: The HBA PCI-Device group number.
3888 *
3889 * This routine is invoked to set up the per HBA PCI-Device group function
3890 * API jump table entries.
3891 *
3892 * Return: 0 if success, otherwise -ENODEV
3893 **/
3894int
3895lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05003896{
3897 int rc;
3898
James Smartda0436e2009-05-22 14:51:39 -04003899 /* Set up lpfc PCI-device group */
3900 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05003901
James Smartda0436e2009-05-22 14:51:39 -04003902 /* The LPFC_PCI_DEV_OC uses SLI4 */
3903 if (dev_grp == LPFC_PCI_DEV_OC)
3904 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05003905
James Smartda0436e2009-05-22 14:51:39 -04003906 /* Set up device INIT API function jump table */
3907 rc = lpfc_init_api_table_setup(phba, dev_grp);
3908 if (rc)
3909 return -ENODEV;
3910 /* Set up SCSI API function jump table */
3911 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
3912 if (rc)
3913 return -ENODEV;
3914 /* Set up SLI API function jump table */
3915 rc = lpfc_sli_api_table_setup(phba, dev_grp);
3916 if (rc)
3917 return -ENODEV;
3918 /* Set up MBOX API function jump table */
3919 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
3920 if (rc)
3921 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05003922
James Smartda0436e2009-05-22 14:51:39 -04003923 return 0;
James Smart5b75da22008-12-04 22:39:35 -05003924}
3925
3926/**
James Smart3621a712009-04-06 18:47:14 -04003927 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05003928 * @phba: pointer to lpfc hba data structure.
3929 * @intr_mode: active interrupt mode adopted.
3930 *
3931 * This routine it invoked to log the currently used active interrupt mode
3932 * to the device.
James Smart3772a992009-05-22 14:50:54 -04003933 **/
3934static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05003935{
3936 switch (intr_mode) {
3937 case 0:
3938 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3939 "0470 Enable INTx interrupt mode.\n");
3940 break;
3941 case 1:
3942 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3943 "0481 Enabled MSI interrupt mode.\n");
3944 break;
3945 case 2:
3946 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3947 "0480 Enabled MSI-X interrupt mode.\n");
3948 break;
3949 default:
3950 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3951 "0482 Illegal interrupt mode.\n");
3952 break;
3953 }
3954 return;
3955}
3956
James Smart5b75da22008-12-04 22:39:35 -05003957/**
James Smart3772a992009-05-22 14:50:54 -04003958 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05003959 * @phba: pointer to lpfc hba data structure.
3960 *
James Smart3772a992009-05-22 14:50:54 -04003961 * This routine is invoked to enable the PCI device that is common to all
3962 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05003963 *
3964 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003965 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04003966 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05003967 **/
James Smart3772a992009-05-22 14:50:54 -04003968static int
3969lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05003970{
James Smart3772a992009-05-22 14:50:54 -04003971 struct pci_dev *pdev;
James Smart079b5c92011-08-21 21:48:49 -04003972 int bars = 0;
James Smart5b75da22008-12-04 22:39:35 -05003973
James Smart3772a992009-05-22 14:50:54 -04003974 /* Obtain PCI device reference */
3975 if (!phba->pcidev)
3976 goto out_error;
3977 else
3978 pdev = phba->pcidev;
3979 /* Select PCI BARs */
3980 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3981 /* Enable PCI device */
3982 if (pci_enable_device_mem(pdev))
3983 goto out_error;
3984 /* Request PCI resource for the device */
3985 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
3986 goto out_disable_device;
3987 /* Set up device as PCI master and save state for EEH */
3988 pci_set_master(pdev);
3989 pci_try_set_mwi(pdev);
3990 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05003991
James Smart05580562011-05-24 11:40:48 -04003992 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
3993 if (pci_find_capability(pdev, PCI_CAP_ID_EXP))
3994 pdev->needs_freset = 1;
3995
James Smart3772a992009-05-22 14:50:54 -04003996 return 0;
James Smart5b75da22008-12-04 22:39:35 -05003997
James Smart3772a992009-05-22 14:50:54 -04003998out_disable_device:
3999 pci_disable_device(pdev);
4000out_error:
James Smart079b5c92011-08-21 21:48:49 -04004001 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4002 "1401 Failed to enable pci device, bars:x%x\n", bars);
James Smart3772a992009-05-22 14:50:54 -04004003 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05004004}
4005
4006/**
James Smart3772a992009-05-22 14:50:54 -04004007 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05004008 * @phba: pointer to lpfc hba data structure.
4009 *
James Smart3772a992009-05-22 14:50:54 -04004010 * This routine is invoked to disable the PCI device that is common to all
4011 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05004012 **/
4013static void
James Smart3772a992009-05-22 14:50:54 -04004014lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05004015{
James Smart3772a992009-05-22 14:50:54 -04004016 struct pci_dev *pdev;
4017 int bars;
James Smart5b75da22008-12-04 22:39:35 -05004018
James Smart3772a992009-05-22 14:50:54 -04004019 /* Obtain PCI device reference */
4020 if (!phba->pcidev)
4021 return;
4022 else
4023 pdev = phba->pcidev;
4024 /* Select PCI BARs */
4025 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4026 /* Release PCI resource and disable PCI device */
4027 pci_release_selected_regions(pdev, bars);
4028 pci_disable_device(pdev);
4029 /* Null out PCI private reference to driver */
4030 pci_set_drvdata(pdev, NULL);
James Smart5b75da22008-12-04 22:39:35 -05004031
4032 return;
4033}
4034
4035/**
James Smart3772a992009-05-22 14:50:54 -04004036 * lpfc_reset_hba - Reset a hba
4037 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04004038 *
James Smart3772a992009-05-22 14:50:54 -04004039 * This routine is invoked to reset a hba device. It brings the HBA
4040 * offline, performs a board restart, and then brings the board back
4041 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
4042 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04004043 **/
James Smart3772a992009-05-22 14:50:54 -04004044void
4045lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004046{
James Smart3772a992009-05-22 14:50:54 -04004047 /* If resets are disabled then set error state and return. */
4048 if (!phba->cfg_enable_hba_reset) {
4049 phba->link_state = LPFC_HBA_ERROR;
4050 return;
4051 }
4052 lpfc_offline_prep(phba);
4053 lpfc_offline(phba);
4054 lpfc_sli_brdrestart(phba);
4055 lpfc_online(phba);
4056 lpfc_unblock_mgmt_io(phba);
4057}
dea31012005-04-17 16:05:31 -05004058
James Smart3772a992009-05-22 14:50:54 -04004059/**
James Smart0a96e972011-07-22 18:37:28 -04004060 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
4061 * @phba: pointer to lpfc hba data structure.
4062 *
4063 * This function enables the PCI SR-IOV virtual functions to a physical
4064 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4065 * enable the number of virtual functions to the physical function. As
4066 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4067 * API call does not considered as an error condition for most of the device.
4068 **/
4069uint16_t
4070lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
4071{
4072 struct pci_dev *pdev = phba->pcidev;
4073 uint16_t nr_virtfn;
4074 int pos;
4075
James Smart0a96e972011-07-22 18:37:28 -04004076 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
4077 if (pos == 0)
4078 return 0;
4079
4080 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
4081 return nr_virtfn;
4082}
4083
4084/**
James Smart912e3ac2011-05-24 11:42:11 -04004085 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
4086 * @phba: pointer to lpfc hba data structure.
4087 * @nr_vfn: number of virtual functions to be enabled.
4088 *
4089 * This function enables the PCI SR-IOV virtual functions to a physical
4090 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4091 * enable the number of virtual functions to the physical function. As
4092 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4093 * API call does not considered as an error condition for most of the device.
4094 **/
4095int
4096lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
4097{
4098 struct pci_dev *pdev = phba->pcidev;
James Smart0a96e972011-07-22 18:37:28 -04004099 uint16_t max_nr_vfn;
James Smart912e3ac2011-05-24 11:42:11 -04004100 int rc;
4101
James Smart0a96e972011-07-22 18:37:28 -04004102 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
4103 if (nr_vfn > max_nr_vfn) {
4104 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4105 "3057 Requested vfs (%d) greater than "
4106 "supported vfs (%d)", nr_vfn, max_nr_vfn);
4107 return -EINVAL;
4108 }
4109
James Smart912e3ac2011-05-24 11:42:11 -04004110 rc = pci_enable_sriov(pdev, nr_vfn);
4111 if (rc) {
4112 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4113 "2806 Failed to enable sriov on this device "
4114 "with vfn number nr_vf:%d, rc:%d\n",
4115 nr_vfn, rc);
4116 } else
4117 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4118 "2807 Successful enable sriov on this device "
4119 "with vfn number nr_vf:%d\n", nr_vfn);
4120 return rc;
4121}
4122
4123/**
James Smart3772a992009-05-22 14:50:54 -04004124 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
4125 * @phba: pointer to lpfc hba data structure.
4126 *
4127 * This routine is invoked to set up the driver internal resources specific to
4128 * support the SLI-3 HBA device it attached to.
4129 *
4130 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004131 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004132 * other values - error
4133 **/
4134static int
4135lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
4136{
4137 struct lpfc_sli *psli;
James Smart912e3ac2011-05-24 11:42:11 -04004138 int rc;
dea31012005-04-17 16:05:31 -05004139
James Smart3772a992009-05-22 14:50:54 -04004140 /*
4141 * Initialize timers used by driver
4142 */
dea31012005-04-17 16:05:31 -05004143
James Smart3772a992009-05-22 14:50:54 -04004144 /* Heartbeat timer */
4145 init_timer(&phba->hb_tmofunc);
4146 phba->hb_tmofunc.function = lpfc_hb_timeout;
4147 phba->hb_tmofunc.data = (unsigned long)phba;
4148
4149 psli = &phba->sli;
4150 /* MBOX heartbeat timer */
4151 init_timer(&psli->mbox_tmo);
4152 psli->mbox_tmo.function = lpfc_mbox_timeout;
4153 psli->mbox_tmo.data = (unsigned long) phba;
4154 /* FCP polling mode timer */
4155 init_timer(&phba->fcp_poll_timer);
4156 phba->fcp_poll_timer.function = lpfc_poll_timeout;
4157 phba->fcp_poll_timer.data = (unsigned long) phba;
4158 /* Fabric block timer */
4159 init_timer(&phba->fabric_block_timer);
4160 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4161 phba->fabric_block_timer.data = (unsigned long) phba;
4162 /* EA polling mode timer */
4163 init_timer(&phba->eratt_poll);
4164 phba->eratt_poll.function = lpfc_poll_eratt;
4165 phba->eratt_poll.data = (unsigned long) phba;
4166
4167 /* Host attention work mask setup */
4168 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
4169 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
4170
4171 /* Get all the module params for configuring this host */
4172 lpfc_get_cfgparam(phba);
James Smart49198b32010-04-06 15:04:33 -04004173 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
4174 phba->menlo_flag |= HBA_MENLO_SUPPORT;
4175 /* check for menlo minimum sg count */
4176 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
4177 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
4178 }
4179
James Smart3772a992009-05-22 14:50:54 -04004180 /*
4181 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
4182 * used to create the sg_dma_buf_pool must be dynamically calculated.
4183 * 2 segments are added since the IOCB needs a command and response bde.
4184 */
4185 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4186 sizeof(struct fcp_rsp) +
4187 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
4188
4189 if (phba->cfg_enable_bg) {
4190 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT;
4191 phba->cfg_sg_dma_buf_size +=
4192 phba->cfg_prot_sg_seg_cnt * sizeof(struct ulp_bde64);
4193 }
4194
4195 /* Also reinitialize the host templates with new values. */
4196 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4197 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4198
4199 phba->max_vpi = LPFC_MAX_VPI;
4200 /* This will be set to correct value after config_port mbox */
4201 phba->max_vports = 0;
4202
4203 /*
4204 * Initialize the SLI Layer to run with lpfc HBAs.
4205 */
4206 lpfc_sli_setup(phba);
4207 lpfc_sli_queue_setup(phba);
4208
4209 /* Allocate device driver memory */
4210 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
4211 return -ENOMEM;
4212
James Smart912e3ac2011-05-24 11:42:11 -04004213 /*
4214 * Enable sr-iov virtual functions if supported and configured
4215 * through the module parameter.
4216 */
4217 if (phba->cfg_sriov_nr_virtfn > 0) {
4218 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
4219 phba->cfg_sriov_nr_virtfn);
4220 if (rc) {
4221 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4222 "2808 Requested number of SR-IOV "
4223 "virtual functions (%d) is not "
4224 "supported\n",
4225 phba->cfg_sriov_nr_virtfn);
4226 phba->cfg_sriov_nr_virtfn = 0;
4227 }
4228 }
4229
James Smart3772a992009-05-22 14:50:54 -04004230 return 0;
4231}
4232
4233/**
4234 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
4235 * @phba: pointer to lpfc hba data structure.
4236 *
4237 * This routine is invoked to unset the driver internal resources set up
4238 * specific for supporting the SLI-3 HBA device it attached to.
4239 **/
4240static void
4241lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
4242{
4243 /* Free device driver memory allocated */
4244 lpfc_mem_free_all(phba);
4245
4246 return;
4247}
4248
4249/**
James Smartda0436e2009-05-22 14:51:39 -04004250 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
4251 * @phba: pointer to lpfc hba data structure.
4252 *
4253 * This routine is invoked to set up the driver internal resources specific to
4254 * support the SLI-4 HBA device it attached to.
4255 *
4256 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004257 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004258 * other values - error
4259 **/
4260static int
4261lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
4262{
4263 struct lpfc_sli *psli;
James Smart28baac72010-02-12 14:42:03 -05004264 LPFC_MBOXQ_t *mboxq;
4265 int rc, i, hbq_count, buf_size, dma_buf_size, max_buf_size;
4266 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
4267 struct lpfc_mqe *mqe;
James Smart085c6472010-11-20 23:11:37 -05004268 int longs, sli_family;
James Smartda0436e2009-05-22 14:51:39 -04004269
4270 /* Before proceed, wait for POST done and device ready */
4271 rc = lpfc_sli4_post_status_check(phba);
4272 if (rc)
4273 return -ENODEV;
4274
4275 /*
4276 * Initialize timers used by driver
4277 */
4278
4279 /* Heartbeat timer */
4280 init_timer(&phba->hb_tmofunc);
4281 phba->hb_tmofunc.function = lpfc_hb_timeout;
4282 phba->hb_tmofunc.data = (unsigned long)phba;
James Smart19ca7602010-11-20 23:11:55 -05004283 init_timer(&phba->rrq_tmr);
4284 phba->rrq_tmr.function = lpfc_rrq_timeout;
4285 phba->rrq_tmr.data = (unsigned long)phba;
James Smartda0436e2009-05-22 14:51:39 -04004286
4287 psli = &phba->sli;
4288 /* MBOX heartbeat timer */
4289 init_timer(&psli->mbox_tmo);
4290 psli->mbox_tmo.function = lpfc_mbox_timeout;
4291 psli->mbox_tmo.data = (unsigned long) phba;
4292 /* Fabric block timer */
4293 init_timer(&phba->fabric_block_timer);
4294 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4295 phba->fabric_block_timer.data = (unsigned long) phba;
4296 /* EA polling mode timer */
4297 init_timer(&phba->eratt_poll);
4298 phba->eratt_poll.function = lpfc_poll_eratt;
4299 phba->eratt_poll.data = (unsigned long) phba;
James Smartecfd03c2010-02-12 14:41:27 -05004300 /* FCF rediscover timer */
4301 init_timer(&phba->fcf.redisc_wait);
4302 phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
4303 phba->fcf.redisc_wait.data = (unsigned long)phba;
4304
James Smartda0436e2009-05-22 14:51:39 -04004305 /*
James Smart7ad20aa2011-05-24 11:44:28 -04004306 * Control structure for handling external multi-buffer mailbox
4307 * command pass-through.
4308 */
4309 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
4310 sizeof(struct lpfc_mbox_ext_buf_ctx));
4311 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
4312
4313 /*
James Smartda0436e2009-05-22 14:51:39 -04004314 * We need to do a READ_CONFIG mailbox command here before
4315 * calling lpfc_get_cfgparam. For VFs this will report the
4316 * MAX_XRI, MAX_VPI, MAX_RPI, MAX_IOCB, and MAX_VFI settings.
4317 * All of the resources allocated
4318 * for this Port are tied to these values.
4319 */
4320 /* Get all the module params for configuring this host */
4321 lpfc_get_cfgparam(phba);
4322 phba->max_vpi = LPFC_MAX_VPI;
4323 /* This will be set to correct value after the read_config mbox */
4324 phba->max_vports = 0;
4325
4326 /* Program the default value of vlan_id and fc_map */
4327 phba->valid_vlan = 0;
4328 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4329 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4330 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4331
4332 /*
4333 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
4334 * used to create the sg_dma_buf_pool must be dynamically calculated.
4335 * 2 segments are added since the IOCB needs a command and response bde.
4336 * To insure that the scsi sgl does not cross a 4k page boundary only
James Smart28baac72010-02-12 14:42:03 -05004337 * sgl sizes of must be a power of 2.
James Smartda0436e2009-05-22 14:51:39 -04004338 */
James Smart28baac72010-02-12 14:42:03 -05004339 buf_size = (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp) +
4340 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge)));
James Smart085c6472010-11-20 23:11:37 -05004341
4342 sli_family = bf_get(lpfc_sli_intf_sli_family, &phba->sli4_hba.sli_intf);
4343 max_buf_size = LPFC_SLI4_MAX_BUF_SIZE;
4344 switch (sli_family) {
4345 case LPFC_SLI_INTF_FAMILY_BE2:
4346 case LPFC_SLI_INTF_FAMILY_BE3:
4347 /* There is a single hint for BE - 2 pages per BPL. */
4348 if (bf_get(lpfc_sli_intf_sli_hint1, &phba->sli4_hba.sli_intf) ==
4349 LPFC_SLI_INTF_SLI_HINT1_1)
4350 max_buf_size = LPFC_SLI4_FL1_MAX_BUF_SIZE;
4351 break;
4352 case LPFC_SLI_INTF_FAMILY_LNCR_A0:
4353 case LPFC_SLI_INTF_FAMILY_LNCR_B0:
4354 default:
4355 break;
4356 }
James Smart28baac72010-02-12 14:42:03 -05004357 for (dma_buf_size = LPFC_SLI4_MIN_BUF_SIZE;
4358 dma_buf_size < max_buf_size && buf_size > dma_buf_size;
4359 dma_buf_size = dma_buf_size << 1)
4360 ;
4361 if (dma_buf_size == max_buf_size)
4362 phba->cfg_sg_seg_cnt = (dma_buf_size -
4363 sizeof(struct fcp_cmnd) - sizeof(struct fcp_rsp) -
4364 (2 * sizeof(struct sli4_sge))) /
4365 sizeof(struct sli4_sge);
4366 phba->cfg_sg_dma_buf_size = dma_buf_size;
James Smartda0436e2009-05-22 14:51:39 -04004367
4368 /* Initialize buffer queue management fields */
4369 hbq_count = lpfc_sli_hbq_count();
4370 for (i = 0; i < hbq_count; ++i)
4371 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
4372 INIT_LIST_HEAD(&phba->rb_pend_list);
4373 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
4374 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
4375
4376 /*
4377 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
4378 */
4379 /* Initialize the Abort scsi buffer list used by driver */
4380 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
4381 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
4382 /* This abort list used by worker thread */
4383 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
4384
4385 /*
James Smart6d368e52011-05-24 11:44:12 -04004386 * Initialize driver internal slow-path work queues
James Smartda0436e2009-05-22 14:51:39 -04004387 */
4388
4389 /* Driver internel slow-path CQ Event pool */
4390 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
4391 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04004392 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04004393 /* Asynchronous event CQ Event work queue list */
4394 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
4395 /* Fast-path XRI aborted CQ Event work queue list */
4396 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
4397 /* Slow-path XRI aborted CQ Event work queue list */
4398 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
4399 /* Receive queue CQ Event work queue list */
4400 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
4401
James Smart6d368e52011-05-24 11:44:12 -04004402 /* Initialize extent block lists. */
4403 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
4404 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
4405 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
4406 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
4407
James Smartda0436e2009-05-22 14:51:39 -04004408 /* Initialize the driver internal SLI layer lists. */
4409 lpfc_sli_setup(phba);
4410 lpfc_sli_queue_setup(phba);
4411
4412 /* Allocate device driver memory */
4413 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
4414 if (rc)
4415 return -ENOMEM;
4416
James Smart2fcee4b2010-12-15 17:57:46 -05004417 /* IF Type 2 ports get initialized now. */
4418 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
4419 LPFC_SLI_INTF_IF_TYPE_2) {
4420 rc = lpfc_pci_function_reset(phba);
4421 if (unlikely(rc))
4422 return -ENODEV;
4423 }
4424
James Smartda0436e2009-05-22 14:51:39 -04004425 /* Create the bootstrap mailbox command */
4426 rc = lpfc_create_bootstrap_mbox(phba);
4427 if (unlikely(rc))
4428 goto out_free_mem;
4429
4430 /* Set up the host's endian order with the device. */
4431 rc = lpfc_setup_endian_order(phba);
4432 if (unlikely(rc))
4433 goto out_free_bsmbx;
4434
4435 /* Set up the hba's configuration parameters. */
4436 rc = lpfc_sli4_read_config(phba);
4437 if (unlikely(rc))
4438 goto out_free_bsmbx;
4439
James Smart2fcee4b2010-12-15 17:57:46 -05004440 /* IF Type 0 ports get initialized now. */
4441 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
4442 LPFC_SLI_INTF_IF_TYPE_0) {
4443 rc = lpfc_pci_function_reset(phba);
4444 if (unlikely(rc))
4445 goto out_free_bsmbx;
4446 }
James Smartda0436e2009-05-22 14:51:39 -04004447
James Smartcb5172e2010-03-15 11:25:07 -04004448 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
4449 GFP_KERNEL);
4450 if (!mboxq) {
4451 rc = -ENOMEM;
4452 goto out_free_bsmbx;
4453 }
4454
James Smartfedd3b72011-02-16 12:39:24 -05004455 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
James Smartcb5172e2010-03-15 11:25:07 -04004456 lpfc_supported_pages(mboxq);
4457 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smartfedd3b72011-02-16 12:39:24 -05004458 if (!rc) {
4459 mqe = &mboxq->u.mqe;
4460 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
4461 LPFC_MAX_SUPPORTED_PAGES);
4462 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
4463 switch (pn_page[i]) {
4464 case LPFC_SLI4_PARAMETERS:
4465 phba->sli4_hba.pc_sli4_params.supported = 1;
4466 break;
4467 default:
4468 break;
4469 }
4470 }
4471 /* Read the port's SLI4 Parameters capabilities if supported. */
4472 if (phba->sli4_hba.pc_sli4_params.supported)
4473 rc = lpfc_pc_sli4_params_get(phba, mboxq);
4474 if (rc) {
4475 mempool_free(mboxq, phba->mbox_mem_pool);
4476 rc = -EIO;
4477 goto out_free_bsmbx;
James Smartcb5172e2010-03-15 11:25:07 -04004478 }
4479 }
James Smartfedd3b72011-02-16 12:39:24 -05004480 /*
4481 * Get sli4 parameters that override parameters from Port capabilities.
James Smart6d368e52011-05-24 11:44:12 -04004482 * If this call fails, it isn't critical unless the SLI4 parameters come
4483 * back in conflict.
James Smartfedd3b72011-02-16 12:39:24 -05004484 */
James Smart6d368e52011-05-24 11:44:12 -04004485 rc = lpfc_get_sli4_parameters(phba, mboxq);
4486 if (rc) {
4487 if (phba->sli4_hba.extents_in_use &&
4488 phba->sli4_hba.rpi_hdrs_in_use) {
4489 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4490 "2999 Unsupported SLI4 Parameters "
4491 "Extents and RPI headers enabled.\n");
4492 goto out_free_bsmbx;
4493 }
4494 }
James Smartcb5172e2010-03-15 11:25:07 -04004495 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart5350d872011-10-10 21:33:49 -04004496 /* Verify all the SLI4 queues */
4497 rc = lpfc_sli4_queue_verify(phba);
James Smartda0436e2009-05-22 14:51:39 -04004498 if (rc)
4499 goto out_free_bsmbx;
4500
4501 /* Create driver internal CQE event pool */
4502 rc = lpfc_sli4_cq_event_pool_create(phba);
4503 if (rc)
James Smart5350d872011-10-10 21:33:49 -04004504 goto out_free_bsmbx;
James Smartda0436e2009-05-22 14:51:39 -04004505
4506 /* Initialize and populate the iocb list per host */
4507 rc = lpfc_init_sgl_list(phba);
4508 if (rc) {
4509 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4510 "1400 Failed to initialize sgl list.\n");
4511 goto out_destroy_cq_event_pool;
4512 }
4513 rc = lpfc_init_active_sgl_array(phba);
4514 if (rc) {
4515 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4516 "1430 Failed to initialize sgl list.\n");
4517 goto out_free_sgl_list;
4518 }
James Smartda0436e2009-05-22 14:51:39 -04004519 rc = lpfc_sli4_init_rpi_hdrs(phba);
4520 if (rc) {
4521 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4522 "1432 Failed to initialize rpi headers.\n");
4523 goto out_free_active_sgl;
4524 }
4525
James Smarta93ff372010-10-22 11:06:08 -04004526 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
James Smart0c9ab6f2010-02-26 14:15:57 -05004527 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
4528 phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
4529 GFP_KERNEL);
4530 if (!phba->fcf.fcf_rr_bmask) {
4531 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4532 "2759 Failed allocate memory for FCF round "
4533 "robin failover bmask\n");
James Smart05580562011-05-24 11:40:48 -04004534 rc = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -05004535 goto out_remove_rpi_hdrs;
4536 }
4537
James Smart5350d872011-10-10 21:33:49 -04004538 /*
4539 * The cfg_fcp_eq_count can be zero whenever there is exactly one
4540 * interrupt vector. This is not an error
4541 */
4542 if (phba->cfg_fcp_eq_count) {
4543 phba->sli4_hba.fcp_eq_hdl =
4544 kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
James Smartda0436e2009-05-22 14:51:39 -04004545 phba->cfg_fcp_eq_count), GFP_KERNEL);
James Smart5350d872011-10-10 21:33:49 -04004546 if (!phba->sli4_hba.fcp_eq_hdl) {
4547 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4548 "2572 Failed allocate memory for "
4549 "fast-path per-EQ handle array\n");
4550 rc = -ENOMEM;
4551 goto out_free_fcf_rr_bmask;
4552 }
James Smartda0436e2009-05-22 14:51:39 -04004553 }
4554
4555 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
4556 phba->sli4_hba.cfg_eqn), GFP_KERNEL);
4557 if (!phba->sli4_hba.msix_entries) {
4558 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4559 "2573 Failed allocate memory for msi-x "
4560 "interrupt vector entries\n");
James Smart05580562011-05-24 11:40:48 -04004561 rc = -ENOMEM;
James Smartda0436e2009-05-22 14:51:39 -04004562 goto out_free_fcp_eq_hdl;
4563 }
4564
James Smart912e3ac2011-05-24 11:42:11 -04004565 /*
4566 * Enable sr-iov virtual functions if supported and configured
4567 * through the module parameter.
4568 */
4569 if (phba->cfg_sriov_nr_virtfn > 0) {
4570 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
4571 phba->cfg_sriov_nr_virtfn);
4572 if (rc) {
4573 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4574 "3020 Requested number of SR-IOV "
4575 "virtual functions (%d) is not "
4576 "supported\n",
4577 phba->cfg_sriov_nr_virtfn);
4578 phba->cfg_sriov_nr_virtfn = 0;
4579 }
4580 }
4581
James Smart5248a742011-07-22 18:37:06 -04004582 return 0;
James Smartda0436e2009-05-22 14:51:39 -04004583
4584out_free_fcp_eq_hdl:
4585 kfree(phba->sli4_hba.fcp_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05004586out_free_fcf_rr_bmask:
4587 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04004588out_remove_rpi_hdrs:
4589 lpfc_sli4_remove_rpi_hdrs(phba);
4590out_free_active_sgl:
4591 lpfc_free_active_sgl(phba);
4592out_free_sgl_list:
4593 lpfc_free_sgl_list(phba);
4594out_destroy_cq_event_pool:
4595 lpfc_sli4_cq_event_pool_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04004596out_free_bsmbx:
4597 lpfc_destroy_bootstrap_mbox(phba);
4598out_free_mem:
4599 lpfc_mem_free(phba);
4600 return rc;
4601}
4602
4603/**
4604 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
4605 * @phba: pointer to lpfc hba data structure.
4606 *
4607 * This routine is invoked to unset the driver internal resources set up
4608 * specific for supporting the SLI-4 HBA device it attached to.
4609 **/
4610static void
4611lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
4612{
4613 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
4614
James Smartda0436e2009-05-22 14:51:39 -04004615 /* Free memory allocated for msi-x interrupt vector entries */
4616 kfree(phba->sli4_hba.msix_entries);
4617
4618 /* Free memory allocated for fast-path work queue handles */
4619 kfree(phba->sli4_hba.fcp_eq_hdl);
4620
4621 /* Free the allocated rpi headers. */
4622 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04004623 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04004624
James Smart0c9ab6f2010-02-26 14:15:57 -05004625 /* Free eligible FCF index bmask */
4626 kfree(phba->fcf.fcf_rr_bmask);
4627
James Smartda0436e2009-05-22 14:51:39 -04004628 /* Free the ELS sgl list */
4629 lpfc_free_active_sgl(phba);
4630 lpfc_free_sgl_list(phba);
4631
4632 /* Free the SCSI sgl management array */
4633 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
4634
James Smartda0436e2009-05-22 14:51:39 -04004635 /* Free the completion queue EQ event pool */
4636 lpfc_sli4_cq_event_release_all(phba);
4637 lpfc_sli4_cq_event_pool_destroy(phba);
4638
James Smart6d368e52011-05-24 11:44:12 -04004639 /* Release resource identifiers. */
4640 lpfc_sli4_dealloc_resource_identifiers(phba);
4641
James Smartda0436e2009-05-22 14:51:39 -04004642 /* Free the bsmbx region. */
4643 lpfc_destroy_bootstrap_mbox(phba);
4644
4645 /* Free the SLI Layer memory with SLI4 HBAs */
4646 lpfc_mem_free_all(phba);
4647
4648 /* Free the current connect table */
4649 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04004650 &phba->fcf_conn_rec_list, list) {
4651 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04004652 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04004653 }
James Smartda0436e2009-05-22 14:51:39 -04004654
4655 return;
4656}
4657
4658/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004659 * lpfc_init_api_table_setup - Set up init api function jump table
James Smart3772a992009-05-22 14:50:54 -04004660 * @phba: The hba struct for which this call is being executed.
4661 * @dev_grp: The HBA PCI-Device group number.
4662 *
4663 * This routine sets up the device INIT interface API function jump table
4664 * in @phba struct.
4665 *
4666 * Returns: 0 - success, -ENODEV - failure.
4667 **/
4668int
4669lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4670{
James Smart84d1b002010-02-12 14:42:33 -05004671 phba->lpfc_hba_init_link = lpfc_hba_init_link;
4672 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart7f860592011-03-11 16:05:52 -05004673 phba->lpfc_selective_reset = lpfc_selective_reset;
James Smart3772a992009-05-22 14:50:54 -04004674 switch (dev_grp) {
4675 case LPFC_PCI_DEV_LP:
4676 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
4677 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
4678 phba->lpfc_stop_port = lpfc_stop_port_s3;
4679 break;
James Smartda0436e2009-05-22 14:51:39 -04004680 case LPFC_PCI_DEV_OC:
4681 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
4682 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
4683 phba->lpfc_stop_port = lpfc_stop_port_s4;
4684 break;
James Smart3772a992009-05-22 14:50:54 -04004685 default:
4686 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4687 "1431 Invalid HBA PCI-device group: 0x%x\n",
4688 dev_grp);
4689 return -ENODEV;
4690 break;
4691 }
4692 return 0;
4693}
4694
4695/**
4696 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
4697 * @phba: pointer to lpfc hba data structure.
4698 *
4699 * This routine is invoked to set up the driver internal resources before the
4700 * device specific resource setup to support the HBA device it attached to.
4701 *
4702 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004703 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004704 * other values - error
4705 **/
4706static int
4707lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
4708{
4709 /*
4710 * Driver resources common to all SLI revisions
4711 */
James Smartea2151b2008-09-07 11:52:10 -04004712 atomic_set(&phba->fast_event_count, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004713 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004714
James Smarte47c9092008-02-08 18:49:26 -05004715 /* Initialize ndlp management spinlock */
4716 spin_lock_init(&phba->ndlp_lock);
4717
James Smart2e0fef82007-06-17 19:56:36 -05004718 INIT_LIST_HEAD(&phba->port_list);
James Smart3772a992009-05-22 14:50:54 -04004719 INIT_LIST_HEAD(&phba->work_list);
James Smart84774a42008-08-24 21:50:06 -04004720 init_waitqueue_head(&phba->wait_4_mlo_m_q);
dea31012005-04-17 16:05:31 -05004721
James Smart3772a992009-05-22 14:50:54 -04004722 /* Initialize the wait queue head for the kernel thread */
4723 init_waitqueue_head(&phba->work_waitq);
James Smart858c9f62007-06-17 19:56:39 -05004724
James Smart3772a992009-05-22 14:50:54 -04004725 /* Initialize the scsi buffer list used by driver for scsi IO */
4726 spin_lock_init(&phba->scsi_buf_list_lock);
4727 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
dea31012005-04-17 16:05:31 -05004728
James Smart3772a992009-05-22 14:50:54 -04004729 /* Initialize the fabric iocb list */
4730 INIT_LIST_HEAD(&phba->fabric_iocb_list);
dea31012005-04-17 16:05:31 -05004731
James Smart3772a992009-05-22 14:50:54 -04004732 /* Initialize list to save ELS buffers */
4733 INIT_LIST_HEAD(&phba->elsbuf);
dea31012005-04-17 16:05:31 -05004734
James Smart3772a992009-05-22 14:50:54 -04004735 /* Initialize FCF connection rec list */
4736 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
dea31012005-04-17 16:05:31 -05004737
James Smart3772a992009-05-22 14:50:54 -04004738 return 0;
4739}
dea31012005-04-17 16:05:31 -05004740
James Smart3772a992009-05-22 14:50:54 -04004741/**
4742 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
4743 * @phba: pointer to lpfc hba data structure.
4744 *
4745 * This routine is invoked to set up the driver internal resources after the
4746 * device specific resource setup to support the HBA device it attached to.
4747 *
4748 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004749 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004750 * other values - error
4751 **/
4752static int
4753lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
4754{
4755 int error;
4756
4757 /* Startup the kernel thread for this host adapter. */
4758 phba->worker_thread = kthread_run(lpfc_do_work, phba,
4759 "lpfc_worker_%d", phba->brd_no);
4760 if (IS_ERR(phba->worker_thread)) {
4761 error = PTR_ERR(phba->worker_thread);
4762 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05004763 }
4764
James Smart3772a992009-05-22 14:50:54 -04004765 return 0;
4766}
4767
4768/**
4769 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
4770 * @phba: pointer to lpfc hba data structure.
4771 *
4772 * This routine is invoked to unset the driver internal resources set up after
4773 * the device specific resource setup for supporting the HBA device it
4774 * attached to.
4775 **/
4776static void
4777lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
4778{
4779 /* Stop kernel worker thread */
4780 kthread_stop(phba->worker_thread);
4781}
4782
4783/**
4784 * lpfc_free_iocb_list - Free iocb list.
4785 * @phba: pointer to lpfc hba data structure.
4786 *
4787 * This routine is invoked to free the driver's IOCB list and memory.
4788 **/
4789static void
4790lpfc_free_iocb_list(struct lpfc_hba *phba)
4791{
4792 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
4793
4794 spin_lock_irq(&phba->hbalock);
4795 list_for_each_entry_safe(iocbq_entry, iocbq_next,
4796 &phba->lpfc_iocb_list, list) {
4797 list_del(&iocbq_entry->list);
4798 kfree(iocbq_entry);
4799 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05004800 }
James Smart3772a992009-05-22 14:50:54 -04004801 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004802
James Smart3772a992009-05-22 14:50:54 -04004803 return;
4804}
dea31012005-04-17 16:05:31 -05004805
James Smart3772a992009-05-22 14:50:54 -04004806/**
4807 * lpfc_init_iocb_list - Allocate and initialize iocb list.
4808 * @phba: pointer to lpfc hba data structure.
4809 *
4810 * This routine is invoked to allocate and initizlize the driver's IOCB
4811 * list and set up the IOCB tag array accordingly.
4812 *
4813 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004814 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004815 * other values - error
4816 **/
4817static int
4818lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
4819{
4820 struct lpfc_iocbq *iocbq_entry = NULL;
4821 uint16_t iotag;
4822 int i;
dea31012005-04-17 16:05:31 -05004823
4824 /* Initialize and populate the iocb list per host. */
4825 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04004826 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07004827 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05004828 if (iocbq_entry == NULL) {
4829 printk(KERN_ERR "%s: only allocated %d iocbs of "
4830 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07004831 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05004832 goto out_free_iocbq;
4833 }
4834
James Bottomley604a3e32005-10-29 10:28:33 -05004835 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
4836 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04004837 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05004838 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04004839 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05004840 goto out_free_iocbq;
4841 }
James Smart6d368e52011-05-24 11:44:12 -04004842 iocbq_entry->sli4_lxritag = NO_XRI;
James Smart3772a992009-05-22 14:50:54 -04004843 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05004844
4845 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004846 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
4847 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05004848 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004849 }
4850
James Smart3772a992009-05-22 14:50:54 -04004851 return 0;
4852
4853out_free_iocbq:
4854 lpfc_free_iocb_list(phba);
4855
4856 return -ENOMEM;
4857}
4858
4859/**
James Smartda0436e2009-05-22 14:51:39 -04004860 * lpfc_free_sgl_list - Free sgl list.
4861 * @phba: pointer to lpfc hba data structure.
4862 *
4863 * This routine is invoked to free the driver's sgl list and memory.
4864 **/
4865static void
4866lpfc_free_sgl_list(struct lpfc_hba *phba)
4867{
4868 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
4869 LIST_HEAD(sglq_list);
James Smartda0436e2009-05-22 14:51:39 -04004870
4871 spin_lock_irq(&phba->hbalock);
4872 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
4873 spin_unlock_irq(&phba->hbalock);
4874
4875 list_for_each_entry_safe(sglq_entry, sglq_next,
4876 &sglq_list, list) {
4877 list_del(&sglq_entry->list);
4878 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
4879 kfree(sglq_entry);
4880 phba->sli4_hba.total_sglq_bufs--;
4881 }
James Smartda0436e2009-05-22 14:51:39 -04004882 kfree(phba->sli4_hba.lpfc_els_sgl_array);
4883}
4884
4885/**
4886 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
4887 * @phba: pointer to lpfc hba data structure.
4888 *
4889 * This routine is invoked to allocate the driver's active sgl memory.
4890 * This array will hold the sglq_entry's for active IOs.
4891 **/
4892static int
4893lpfc_init_active_sgl_array(struct lpfc_hba *phba)
4894{
4895 int size;
4896 size = sizeof(struct lpfc_sglq *);
4897 size *= phba->sli4_hba.max_cfg_param.max_xri;
4898
4899 phba->sli4_hba.lpfc_sglq_active_list =
4900 kzalloc(size, GFP_KERNEL);
4901 if (!phba->sli4_hba.lpfc_sglq_active_list)
4902 return -ENOMEM;
4903 return 0;
4904}
4905
4906/**
4907 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
4908 * @phba: pointer to lpfc hba data structure.
4909 *
4910 * This routine is invoked to walk through the array of active sglq entries
4911 * and free all of the resources.
4912 * This is just a place holder for now.
4913 **/
4914static void
4915lpfc_free_active_sgl(struct lpfc_hba *phba)
4916{
4917 kfree(phba->sli4_hba.lpfc_sglq_active_list);
4918}
4919
4920/**
4921 * lpfc_init_sgl_list - Allocate and initialize sgl list.
4922 * @phba: pointer to lpfc hba data structure.
4923 *
4924 * This routine is invoked to allocate and initizlize the driver's sgl
4925 * list and set up the sgl xritag tag array accordingly.
4926 *
4927 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004928 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004929 * other values - error
4930 **/
4931static int
4932lpfc_init_sgl_list(struct lpfc_hba *phba)
4933{
4934 struct lpfc_sglq *sglq_entry = NULL;
4935 int i;
4936 int els_xri_cnt;
4937
4938 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
4939 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart6d368e52011-05-24 11:44:12 -04004940 "2400 ELS XRI count %d.\n",
James Smartda0436e2009-05-22 14:51:39 -04004941 els_xri_cnt);
4942 /* Initialize and populate the sglq list per host/VF. */
4943 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
4944 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
4945
4946 /* Sanity check on XRI management */
4947 if (phba->sli4_hba.max_cfg_param.max_xri <= els_xri_cnt) {
4948 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4949 "2562 No room left for SCSI XRI allocation: "
4950 "max_xri=%d, els_xri=%d\n",
4951 phba->sli4_hba.max_cfg_param.max_xri,
4952 els_xri_cnt);
4953 return -ENOMEM;
4954 }
4955
4956 /* Allocate memory for the ELS XRI management array */
4957 phba->sli4_hba.lpfc_els_sgl_array =
4958 kzalloc((sizeof(struct lpfc_sglq *) * els_xri_cnt),
4959 GFP_KERNEL);
4960
4961 if (!phba->sli4_hba.lpfc_els_sgl_array) {
4962 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4963 "2401 Failed to allocate memory for ELS "
4964 "XRI management array of size %d.\n",
4965 els_xri_cnt);
4966 return -ENOMEM;
4967 }
4968
4969 /* Keep the SCSI XRI into the XRI management array */
4970 phba->sli4_hba.scsi_xri_max =
4971 phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
4972 phba->sli4_hba.scsi_xri_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04004973 phba->sli4_hba.lpfc_scsi_psb_array =
4974 kzalloc((sizeof(struct lpfc_scsi_buf *) *
4975 phba->sli4_hba.scsi_xri_max), GFP_KERNEL);
4976
4977 if (!phba->sli4_hba.lpfc_scsi_psb_array) {
4978 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4979 "2563 Failed to allocate memory for SCSI "
4980 "XRI management array of size %d.\n",
4981 phba->sli4_hba.scsi_xri_max);
4982 kfree(phba->sli4_hba.lpfc_els_sgl_array);
4983 return -ENOMEM;
4984 }
4985
4986 for (i = 0; i < els_xri_cnt; i++) {
4987 sglq_entry = kzalloc(sizeof(struct lpfc_sglq), GFP_KERNEL);
4988 if (sglq_entry == NULL) {
4989 printk(KERN_ERR "%s: only allocated %d sgls of "
4990 "expected %d count. Unloading driver.\n",
4991 __func__, i, els_xri_cnt);
4992 goto out_free_mem;
4993 }
4994
James Smartda0436e2009-05-22 14:51:39 -04004995 sglq_entry->buff_type = GEN_BUFF_TYPE;
4996 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0, &sglq_entry->phys);
4997 if (sglq_entry->virt == NULL) {
4998 kfree(sglq_entry);
4999 printk(KERN_ERR "%s: failed to allocate mbuf.\n"
5000 "Unloading driver.\n", __func__);
5001 goto out_free_mem;
5002 }
5003 sglq_entry->sgl = sglq_entry->virt;
5004 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
5005
5006 /* The list order is used by later block SGL registraton */
5007 spin_lock_irq(&phba->hbalock);
James Smart0f65ff62010-02-26 14:14:23 -05005008 sglq_entry->state = SGL_FREED;
James Smartda0436e2009-05-22 14:51:39 -04005009 list_add_tail(&sglq_entry->list, &phba->sli4_hba.lpfc_sgl_list);
5010 phba->sli4_hba.lpfc_els_sgl_array[i] = sglq_entry;
5011 phba->sli4_hba.total_sglq_bufs++;
5012 spin_unlock_irq(&phba->hbalock);
5013 }
5014 return 0;
5015
5016out_free_mem:
5017 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
5018 lpfc_free_sgl_list(phba);
5019 return -ENOMEM;
5020}
5021
5022/**
5023 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
5024 * @phba: pointer to lpfc hba data structure.
5025 *
5026 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -04005027 * port for those SLI4 ports that do not support extents. This routine
James Smartda0436e2009-05-22 14:51:39 -04005028 * posts a PAGE_SIZE memory region to the port to hold up to
James Smart88a2cfb2011-07-22 18:36:33 -04005029 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
5030 * and should be called only when interrupts are disabled.
James Smartda0436e2009-05-22 14:51:39 -04005031 *
5032 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005033 * 0 - successful
James Smart88a2cfb2011-07-22 18:36:33 -04005034 * -ERROR - otherwise.
James Smartda0436e2009-05-22 14:51:39 -04005035 **/
5036int
5037lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
5038{
5039 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04005040 struct lpfc_rpi_hdr *rpi_hdr;
5041
5042 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
James Smartda0436e2009-05-22 14:51:39 -04005043 /*
James Smart6d368e52011-05-24 11:44:12 -04005044 * If the SLI4 port supports extents, posting the rpi header isn't
5045 * required. Set the expected maximum count and let the actual value
5046 * get set when extents are fully allocated.
James Smartda0436e2009-05-22 14:51:39 -04005047 */
James Smart6d368e52011-05-24 11:44:12 -04005048 if (!phba->sli4_hba.rpi_hdrs_in_use) {
5049 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
5050 return rc;
5051 }
5052 if (phba->sli4_hba.extents_in_use)
5053 return -EIO;
James Smartda0436e2009-05-22 14:51:39 -04005054
5055 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
5056 if (!rpi_hdr) {
5057 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5058 "0391 Error during rpi post operation\n");
5059 lpfc_sli4_remove_rpis(phba);
5060 rc = -ENODEV;
5061 }
5062
5063 return rc;
5064}
5065
5066/**
5067 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
5068 * @phba: pointer to lpfc hba data structure.
5069 *
5070 * This routine is invoked to allocate a single 4KB memory region to
5071 * support rpis and stores them in the phba. This single region
5072 * provides support for up to 64 rpis. The region is used globally
5073 * by the device.
5074 *
5075 * Returns:
5076 * A valid rpi hdr on success.
5077 * A NULL pointer on any failure.
5078 **/
5079struct lpfc_rpi_hdr *
5080lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
5081{
5082 uint16_t rpi_limit, curr_rpi_range;
5083 struct lpfc_dmabuf *dmabuf;
5084 struct lpfc_rpi_hdr *rpi_hdr;
James Smart9589b0622011-04-16 11:03:17 -04005085 uint32_t rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04005086
James Smart6d368e52011-05-24 11:44:12 -04005087 /*
5088 * If the SLI4 port supports extents, posting the rpi header isn't
5089 * required. Set the expected maximum count and let the actual value
5090 * get set when extents are fully allocated.
5091 */
5092 if (!phba->sli4_hba.rpi_hdrs_in_use)
5093 return NULL;
5094 if (phba->sli4_hba.extents_in_use)
5095 return NULL;
5096
5097 /* The limit on the logical index is just the max_rpi count. */
James Smartda0436e2009-05-22 14:51:39 -04005098 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
James Smart6d368e52011-05-24 11:44:12 -04005099 phba->sli4_hba.max_cfg_param.max_rpi - 1;
James Smartda0436e2009-05-22 14:51:39 -04005100
5101 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04005102 /*
5103 * Establish the starting RPI in this header block. The starting
5104 * rpi is normalized to a zero base because the physical rpi is
5105 * port based.
5106 */
5107 curr_rpi_range = phba->sli4_hba.next_rpi -
5108 phba->sli4_hba.max_cfg_param.rpi_base;
James Smartda0436e2009-05-22 14:51:39 -04005109 spin_unlock_irq(&phba->hbalock);
5110
5111 /*
5112 * The port has a limited number of rpis. The increment here
5113 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
5114 * and to allow the full max_rpi range per port.
5115 */
5116 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
James Smart9589b0622011-04-16 11:03:17 -04005117 rpi_count = rpi_limit - curr_rpi_range;
5118 else
5119 rpi_count = LPFC_RPI_HDR_COUNT;
James Smartda0436e2009-05-22 14:51:39 -04005120
James Smart6d368e52011-05-24 11:44:12 -04005121 if (!rpi_count)
5122 return NULL;
James Smartda0436e2009-05-22 14:51:39 -04005123 /*
5124 * First allocate the protocol header region for the port. The
5125 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
5126 */
5127 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5128 if (!dmabuf)
5129 return NULL;
5130
5131 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5132 LPFC_HDR_TEMPLATE_SIZE,
5133 &dmabuf->phys,
5134 GFP_KERNEL);
5135 if (!dmabuf->virt) {
5136 rpi_hdr = NULL;
5137 goto err_free_dmabuf;
5138 }
5139
5140 memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
5141 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
5142 rpi_hdr = NULL;
5143 goto err_free_coherent;
5144 }
5145
5146 /* Save the rpi header data for cleanup later. */
5147 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
5148 if (!rpi_hdr)
5149 goto err_free_coherent;
5150
5151 rpi_hdr->dmabuf = dmabuf;
5152 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
5153 rpi_hdr->page_count = 1;
5154 spin_lock_irq(&phba->hbalock);
James Smart6d368e52011-05-24 11:44:12 -04005155
5156 /* The rpi_hdr stores the logical index only. */
5157 rpi_hdr->start_rpi = curr_rpi_range;
James Smartda0436e2009-05-22 14:51:39 -04005158 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
5159
5160 /*
James Smart6d368e52011-05-24 11:44:12 -04005161 * The next_rpi stores the next logical module-64 rpi value used
5162 * to post physical rpis in subsequent rpi postings.
James Smartda0436e2009-05-22 14:51:39 -04005163 */
James Smart9589b0622011-04-16 11:03:17 -04005164 phba->sli4_hba.next_rpi += rpi_count;
James Smartda0436e2009-05-22 14:51:39 -04005165 spin_unlock_irq(&phba->hbalock);
5166 return rpi_hdr;
5167
5168 err_free_coherent:
5169 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
5170 dmabuf->virt, dmabuf->phys);
5171 err_free_dmabuf:
5172 kfree(dmabuf);
5173 return NULL;
5174}
5175
5176/**
5177 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
5178 * @phba: pointer to lpfc hba data structure.
5179 *
5180 * This routine is invoked to remove all memory resources allocated
James Smart6d368e52011-05-24 11:44:12 -04005181 * to support rpis for SLI4 ports not supporting extents. This routine
5182 * presumes the caller has released all rpis consumed by fabric or port
5183 * logins and is prepared to have the header pages removed.
James Smartda0436e2009-05-22 14:51:39 -04005184 **/
5185void
5186lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
5187{
5188 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
5189
James Smart6d368e52011-05-24 11:44:12 -04005190 if (!phba->sli4_hba.rpi_hdrs_in_use)
5191 goto exit;
5192
James Smartda0436e2009-05-22 14:51:39 -04005193 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
5194 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
5195 list_del(&rpi_hdr->list);
5196 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
5197 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
5198 kfree(rpi_hdr->dmabuf);
5199 kfree(rpi_hdr);
5200 }
James Smart6d368e52011-05-24 11:44:12 -04005201 exit:
5202 /* There are no rpis available to the port now. */
5203 phba->sli4_hba.next_rpi = 0;
James Smartda0436e2009-05-22 14:51:39 -04005204}
5205
5206/**
James Smart3772a992009-05-22 14:50:54 -04005207 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
5208 * @pdev: pointer to pci device data structure.
5209 *
5210 * This routine is invoked to allocate the driver hba data structure for an
5211 * HBA device. If the allocation is successful, the phba reference to the
5212 * PCI device data structure is set.
5213 *
5214 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005215 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04005216 * NULL - error
5217 **/
5218static struct lpfc_hba *
5219lpfc_hba_alloc(struct pci_dev *pdev)
5220{
5221 struct lpfc_hba *phba;
5222
5223 /* Allocate memory for HBA structure */
5224 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
5225 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02005226 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04005227 return NULL;
5228 }
5229
5230 /* Set reference to PCI device in HBA structure */
5231 phba->pcidev = pdev;
5232
5233 /* Assign an unused board number */
5234 phba->brd_no = lpfc_get_instance();
5235 if (phba->brd_no < 0) {
5236 kfree(phba);
5237 return NULL;
5238 }
5239
James Smart4fede782010-01-26 23:08:55 -05005240 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04005241 INIT_LIST_HEAD(&phba->ct_ev_waiters);
5242
James Smart3772a992009-05-22 14:50:54 -04005243 return phba;
5244}
5245
5246/**
5247 * lpfc_hba_free - Free driver hba data structure with a device.
5248 * @phba: pointer to lpfc hba data structure.
5249 *
5250 * This routine is invoked to free the driver hba data structure with an
5251 * HBA device.
5252 **/
5253static void
5254lpfc_hba_free(struct lpfc_hba *phba)
5255{
5256 /* Release the driver assigned board number */
5257 idr_remove(&lpfc_hba_index, phba->brd_no);
5258
5259 kfree(phba);
5260 return;
5261}
5262
5263/**
5264 * lpfc_create_shost - Create hba physical port with associated scsi host.
5265 * @phba: pointer to lpfc hba data structure.
5266 *
5267 * This routine is invoked to create HBA physical port and associate a SCSI
5268 * host with it.
5269 *
5270 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005271 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005272 * other values - error
5273 **/
5274static int
5275lpfc_create_shost(struct lpfc_hba *phba)
5276{
5277 struct lpfc_vport *vport;
5278 struct Scsi_Host *shost;
5279
5280 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05005281 phba->fc_edtov = FF_DEF_EDTOV;
5282 phba->fc_ratov = FF_DEF_RATOV;
5283 phba->fc_altov = FF_DEF_ALTOV;
5284 phba->fc_arbtov = FF_DEF_ARBTOV;
5285
James Smartd7c47992010-06-08 18:31:54 -04005286 atomic_set(&phba->sdev_cnt, 0);
James Smart3de2a652007-08-02 11:09:59 -04005287 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05005288 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04005289 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05005290
5291 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05005292 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05005293 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04005294 /* Put reference to SCSI host to driver's device private data */
5295 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05005296
James Smart3772a992009-05-22 14:50:54 -04005297 return 0;
5298}
dea31012005-04-17 16:05:31 -05005299
James Smart3772a992009-05-22 14:50:54 -04005300/**
5301 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
5302 * @phba: pointer to lpfc hba data structure.
5303 *
5304 * This routine is invoked to destroy HBA physical port and the associated
5305 * SCSI host.
5306 **/
5307static void
5308lpfc_destroy_shost(struct lpfc_hba *phba)
5309{
5310 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04005311
James Smart3772a992009-05-22 14:50:54 -04005312 /* Destroy physical port that associated with the SCSI host */
5313 destroy_port(vport);
5314
5315 return;
5316}
5317
5318/**
5319 * lpfc_setup_bg - Setup Block guard structures and debug areas.
5320 * @phba: pointer to lpfc hba data structure.
5321 * @shost: the shost to be used to detect Block guard settings.
5322 *
5323 * This routine sets up the local Block guard protocol settings for @shost.
5324 * This routine also allocates memory for debugging bg buffers.
5325 **/
5326static void
5327lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
5328{
5329 int pagecnt = 10;
5330 if (lpfc_prot_mask && lpfc_prot_guard) {
5331 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5332 "1478 Registering BlockGuard with the "
5333 "SCSI layer\n");
5334 scsi_host_set_prot(shost, lpfc_prot_mask);
5335 scsi_host_set_guard(shost, lpfc_prot_guard);
James Smart98c9ea52007-10-27 13:37:33 -04005336 }
James Smart81301a92008-12-04 22:39:46 -05005337 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05005338 while (pagecnt) {
5339 spin_lock_init(&_dump_buf_lock);
5340 _dump_buf_data =
5341 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
5342 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04005343 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5344 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04005345 "_dump_buf_data at 0x%p\n",
5346 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05005347 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04005348 memset(_dump_buf_data, 0,
5349 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05005350 break;
James Smart3772a992009-05-22 14:50:54 -04005351 } else
James Smart81301a92008-12-04 22:39:46 -05005352 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05005353 }
James Smart81301a92008-12-04 22:39:46 -05005354 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04005355 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5356 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04005357 "memory for hexdump\n");
5358 } else
James Smart6a9c52c2009-10-02 15:16:51 -04005359 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5360 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05005361 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05005362 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05005363 while (pagecnt) {
5364 _dump_buf_dif =
5365 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
5366 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04005367 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5368 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04005369 "_dump_buf_dif at 0x%p\n",
5370 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05005371 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04005372 memset(_dump_buf_dif, 0,
5373 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05005374 break;
James Smart3772a992009-05-22 14:50:54 -04005375 } else
James Smart81301a92008-12-04 22:39:46 -05005376 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05005377 }
James Smart81301a92008-12-04 22:39:46 -05005378 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04005379 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5380 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04005381 "memory for hexdump\n");
5382 } else
James Smart6a9c52c2009-10-02 15:16:51 -04005383 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5384 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04005385 _dump_buf_dif);
5386}
5387
5388/**
5389 * lpfc_post_init_setup - Perform necessary device post initialization setup.
5390 * @phba: pointer to lpfc hba data structure.
5391 *
5392 * This routine is invoked to perform all the necessary post initialization
5393 * setup for the device.
5394 **/
5395static void
5396lpfc_post_init_setup(struct lpfc_hba *phba)
5397{
5398 struct Scsi_Host *shost;
5399 struct lpfc_adapter_event_header adapter_event;
5400
5401 /* Get the default values for Model Name and Description */
5402 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
5403
5404 /*
5405 * hba setup may have changed the hba_queue_depth so we need to
5406 * adjust the value of can_queue.
5407 */
5408 shost = pci_get_drvdata(phba->pcidev);
5409 shost->can_queue = phba->cfg_hba_queue_depth - 10;
5410 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
5411 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05005412
5413 lpfc_host_attrib_init(shost);
5414
James Smart2e0fef82007-06-17 19:56:36 -05005415 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
5416 spin_lock_irq(shost->host_lock);
5417 lpfc_poll_start_timer(phba);
5418 spin_unlock_irq(shost->host_lock);
5419 }
James Smart8f6d98d2006-08-01 07:34:00 -04005420
James Smart93996272008-08-24 21:50:30 -04005421 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5422 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04005423 /* Send board arrival event to upper layer */
5424 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
5425 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
5426 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04005427 sizeof(adapter_event),
5428 (char *) &adapter_event,
5429 LPFC_NL_VENDOR_ID);
5430 return;
5431}
5432
5433/**
5434 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
5435 * @phba: pointer to lpfc hba data structure.
5436 *
5437 * This routine is invoked to set up the PCI device memory space for device
5438 * with SLI-3 interface spec.
5439 *
5440 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005441 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005442 * other values - error
5443 **/
5444static int
5445lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
5446{
5447 struct pci_dev *pdev;
5448 unsigned long bar0map_len, bar2map_len;
5449 int i, hbq_count;
5450 void *ptr;
5451 int error = -ENODEV;
5452
5453 /* Obtain PCI device reference */
5454 if (!phba->pcidev)
5455 return error;
5456 else
5457 pdev = phba->pcidev;
5458
5459 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05005460 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
5461 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
5462 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
5463 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smart3772a992009-05-22 14:50:54 -04005464 return error;
Michael Reed8e685972009-09-18 12:02:05 -05005465 }
5466 }
James Smart3772a992009-05-22 14:50:54 -04005467
5468 /* Get the bus address of Bar0 and Bar2 and the number of bytes
5469 * required by each mapping.
5470 */
5471 phba->pci_bar0_map = pci_resource_start(pdev, 0);
5472 bar0map_len = pci_resource_len(pdev, 0);
5473
5474 phba->pci_bar2_map = pci_resource_start(pdev, 2);
5475 bar2map_len = pci_resource_len(pdev, 2);
5476
5477 /* Map HBA SLIM to a kernel virtual address. */
5478 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
5479 if (!phba->slim_memmap_p) {
5480 dev_printk(KERN_ERR, &pdev->dev,
5481 "ioremap failed for SLIM memory.\n");
5482 goto out;
5483 }
5484
5485 /* Map HBA Control Registers to a kernel virtual address. */
5486 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
5487 if (!phba->ctrl_regs_memmap_p) {
5488 dev_printk(KERN_ERR, &pdev->dev,
5489 "ioremap failed for HBA control registers.\n");
5490 goto out_iounmap_slim;
5491 }
5492
5493 /* Allocate memory for SLI-2 structures */
5494 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
5495 SLI2_SLIM_SIZE,
5496 &phba->slim2p.phys,
5497 GFP_KERNEL);
5498 if (!phba->slim2p.virt)
5499 goto out_iounmap;
5500
5501 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
5502 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04005503 phba->mbox_ext = (phba->slim2p.virt +
5504 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04005505 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
5506 phba->IOCBs = (phba->slim2p.virt +
5507 offsetof(struct lpfc_sli2_slim, IOCBs));
5508
5509 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
5510 lpfc_sli_hbq_size(),
5511 &phba->hbqslimp.phys,
5512 GFP_KERNEL);
5513 if (!phba->hbqslimp.virt)
5514 goto out_free_slim;
5515
5516 hbq_count = lpfc_sli_hbq_count();
5517 ptr = phba->hbqslimp.virt;
5518 for (i = 0; i < hbq_count; ++i) {
5519 phba->hbqs[i].hbq_virt = ptr;
5520 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
5521 ptr += (lpfc_hbq_defs[i]->entry_count *
5522 sizeof(struct lpfc_hbq_entry));
5523 }
5524 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
5525 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
5526
5527 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
5528
5529 INIT_LIST_HEAD(&phba->rb_pend_list);
5530
5531 phba->MBslimaddr = phba->slim_memmap_p;
5532 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
5533 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
5534 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
5535 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04005536
dea31012005-04-17 16:05:31 -05005537 return 0;
5538
dea31012005-04-17 16:05:31 -05005539out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04005540 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
5541 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05005542out_iounmap:
5543 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005544out_iounmap_slim:
dea31012005-04-17 16:05:31 -05005545 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05005546out:
5547 return error;
5548}
5549
James Smarte59058c2008-08-24 21:49:00 -04005550/**
James Smart3772a992009-05-22 14:50:54 -04005551 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
5552 * @phba: pointer to lpfc hba data structure.
5553 *
5554 * This routine is invoked to unset the PCI device memory space for device
5555 * with SLI-3 interface spec.
5556 **/
5557static void
5558lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
5559{
5560 struct pci_dev *pdev;
5561
5562 /* Obtain PCI device reference */
5563 if (!phba->pcidev)
5564 return;
5565 else
5566 pdev = phba->pcidev;
5567
5568 /* Free coherent DMA memory allocated */
5569 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
5570 phba->hbqslimp.virt, phba->hbqslimp.phys);
5571 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
5572 phba->slim2p.virt, phba->slim2p.phys);
5573
5574 /* I/O memory unmap */
5575 iounmap(phba->ctrl_regs_memmap_p);
5576 iounmap(phba->slim_memmap_p);
5577
5578 return;
5579}
5580
5581/**
James Smartda0436e2009-05-22 14:51:39 -04005582 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
5583 * @phba: pointer to lpfc hba data structure.
5584 *
5585 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
5586 * done and check status.
5587 *
5588 * Return 0 if successful, otherwise -ENODEV.
5589 **/
5590int
5591lpfc_sli4_post_status_check(struct lpfc_hba *phba)
5592{
James Smart2fcee4b2010-12-15 17:57:46 -05005593 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
5594 struct lpfc_register reg_data;
5595 int i, port_error = 0;
5596 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04005597
James Smart9940b972011-03-11 16:06:12 -05005598 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
5599 memset(&reg_data, 0, sizeof(reg_data));
James Smart2fcee4b2010-12-15 17:57:46 -05005600 if (!phba->sli4_hba.PSMPHRregaddr)
James Smartda0436e2009-05-22 14:51:39 -04005601 return -ENODEV;
5602
James Smartda0436e2009-05-22 14:51:39 -04005603 /* Wait up to 30 seconds for the SLI Port POST done and ready */
5604 for (i = 0; i < 3000; i++) {
James Smart9940b972011-03-11 16:06:12 -05005605 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
5606 &portsmphr_reg.word0) ||
5607 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
James Smart2fcee4b2010-12-15 17:57:46 -05005608 /* Port has a fatal POST error, break out */
James Smartda0436e2009-05-22 14:51:39 -04005609 port_error = -ENODEV;
5610 break;
5611 }
James Smart2fcee4b2010-12-15 17:57:46 -05005612 if (LPFC_POST_STAGE_PORT_READY ==
5613 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
James Smartda0436e2009-05-22 14:51:39 -04005614 break;
James Smartda0436e2009-05-22 14:51:39 -04005615 msleep(10);
5616 }
5617
James Smart2fcee4b2010-12-15 17:57:46 -05005618 /*
5619 * If there was a port error during POST, then don't proceed with
5620 * other register reads as the data may not be valid. Just exit.
5621 */
5622 if (port_error) {
James Smartda0436e2009-05-22 14:51:39 -04005623 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05005624 "1408 Port Failed POST - portsmphr=0x%x, "
5625 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
5626 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
5627 portsmphr_reg.word0,
5628 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
5629 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
5630 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
5631 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
5632 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
5633 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
5634 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
5635 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
5636 } else {
James Smart28baac72010-02-12 14:42:03 -05005637 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -05005638 "2534 Device Info: SLIFamily=0x%x, "
5639 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
5640 "SLIHint_2=0x%x, FT=0x%x\n",
James Smart28baac72010-02-12 14:42:03 -05005641 bf_get(lpfc_sli_intf_sli_family,
5642 &phba->sli4_hba.sli_intf),
5643 bf_get(lpfc_sli_intf_slirev,
5644 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05005645 bf_get(lpfc_sli_intf_if_type,
James Smart28baac72010-02-12 14:42:03 -05005646 &phba->sli4_hba.sli_intf),
James Smart085c6472010-11-20 23:11:37 -05005647 bf_get(lpfc_sli_intf_sli_hint1,
5648 &phba->sli4_hba.sli_intf),
5649 bf_get(lpfc_sli_intf_sli_hint2,
5650 &phba->sli4_hba.sli_intf),
5651 bf_get(lpfc_sli_intf_func_type,
James Smart28baac72010-02-12 14:42:03 -05005652 &phba->sli4_hba.sli_intf));
James Smart2fcee4b2010-12-15 17:57:46 -05005653 /*
5654 * Check for other Port errors during the initialization
5655 * process. Fail the load if the port did not come up
5656 * correctly.
5657 */
5658 if_type = bf_get(lpfc_sli_intf_if_type,
5659 &phba->sli4_hba.sli_intf);
5660 switch (if_type) {
5661 case LPFC_SLI_INTF_IF_TYPE_0:
5662 phba->sli4_hba.ue_mask_lo =
5663 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
5664 phba->sli4_hba.ue_mask_hi =
5665 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
5666 uerrlo_reg.word0 =
5667 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
5668 uerrhi_reg.word0 =
5669 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
5670 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
5671 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
5672 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5673 "1422 Unrecoverable Error "
5674 "Detected during POST "
5675 "uerr_lo_reg=0x%x, "
5676 "uerr_hi_reg=0x%x, "
5677 "ue_mask_lo_reg=0x%x, "
5678 "ue_mask_hi_reg=0x%x\n",
5679 uerrlo_reg.word0,
5680 uerrhi_reg.word0,
5681 phba->sli4_hba.ue_mask_lo,
5682 phba->sli4_hba.ue_mask_hi);
5683 port_error = -ENODEV;
5684 }
5685 break;
5686 case LPFC_SLI_INTF_IF_TYPE_2:
5687 /* Final checks. The port status should be clean. */
James Smart9940b972011-03-11 16:06:12 -05005688 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
5689 &reg_data.word0) ||
James Smart05580562011-05-24 11:40:48 -04005690 (bf_get(lpfc_sliport_status_err, &reg_data) &&
5691 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
James Smart2fcee4b2010-12-15 17:57:46 -05005692 phba->work_status[0] =
5693 readl(phba->sli4_hba.u.if_type2.
5694 ERR1regaddr);
5695 phba->work_status[1] =
5696 readl(phba->sli4_hba.u.if_type2.
5697 ERR2regaddr);
5698 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5699 "2888 Port Error Detected "
5700 "during POST: "
5701 "port status reg 0x%x, "
5702 "port_smphr reg 0x%x, "
5703 "error 1=0x%x, error 2=0x%x\n",
5704 reg_data.word0,
5705 portsmphr_reg.word0,
5706 phba->work_status[0],
5707 phba->work_status[1]);
5708 port_error = -ENODEV;
5709 }
5710 break;
5711 case LPFC_SLI_INTF_IF_TYPE_1:
5712 default:
5713 break;
5714 }
James Smart28baac72010-02-12 14:42:03 -05005715 }
James Smartda0436e2009-05-22 14:51:39 -04005716 return port_error;
5717}
5718
5719/**
5720 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
5721 * @phba: pointer to lpfc hba data structure.
James Smart2fcee4b2010-12-15 17:57:46 -05005722 * @if_type: The SLI4 interface type getting configured.
James Smartda0436e2009-05-22 14:51:39 -04005723 *
5724 * This routine is invoked to set up SLI4 BAR0 PCI config space register
5725 * memory map.
5726 **/
5727static void
James Smart2fcee4b2010-12-15 17:57:46 -05005728lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
James Smartda0436e2009-05-22 14:51:39 -04005729{
James Smart2fcee4b2010-12-15 17:57:46 -05005730 switch (if_type) {
5731 case LPFC_SLI_INTF_IF_TYPE_0:
5732 phba->sli4_hba.u.if_type0.UERRLOregaddr =
5733 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
5734 phba->sli4_hba.u.if_type0.UERRHIregaddr =
5735 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
5736 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
5737 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
5738 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
5739 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
5740 phba->sli4_hba.SLIINTFregaddr =
5741 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
5742 break;
5743 case LPFC_SLI_INTF_IF_TYPE_2:
5744 phba->sli4_hba.u.if_type2.ERR1regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04005745 phba->sli4_hba.conf_regs_memmap_p +
5746 LPFC_CTL_PORT_ER1_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05005747 phba->sli4_hba.u.if_type2.ERR2regaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04005748 phba->sli4_hba.conf_regs_memmap_p +
5749 LPFC_CTL_PORT_ER2_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05005750 phba->sli4_hba.u.if_type2.CTRLregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04005751 phba->sli4_hba.conf_regs_memmap_p +
5752 LPFC_CTL_PORT_CTL_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05005753 phba->sli4_hba.u.if_type2.STATUSregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04005754 phba->sli4_hba.conf_regs_memmap_p +
5755 LPFC_CTL_PORT_STA_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05005756 phba->sli4_hba.SLIINTFregaddr =
5757 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
5758 phba->sli4_hba.PSMPHRregaddr =
James Smart88a2cfb2011-07-22 18:36:33 -04005759 phba->sli4_hba.conf_regs_memmap_p +
5760 LPFC_CTL_PORT_SEM_OFFSET;
James Smart2fcee4b2010-12-15 17:57:46 -05005761 phba->sli4_hba.RQDBregaddr =
5762 phba->sli4_hba.conf_regs_memmap_p + LPFC_RQ_DOORBELL;
5763 phba->sli4_hba.WQDBregaddr =
5764 phba->sli4_hba.conf_regs_memmap_p + LPFC_WQ_DOORBELL;
5765 phba->sli4_hba.EQCQDBregaddr =
5766 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
5767 phba->sli4_hba.MQDBregaddr =
5768 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
5769 phba->sli4_hba.BMBXregaddr =
5770 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
5771 break;
5772 case LPFC_SLI_INTF_IF_TYPE_1:
5773 default:
5774 dev_printk(KERN_ERR, &phba->pcidev->dev,
5775 "FATAL - unsupported SLI4 interface type - %d\n",
5776 if_type);
5777 break;
5778 }
James Smartda0436e2009-05-22 14:51:39 -04005779}
5780
5781/**
5782 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
5783 * @phba: pointer to lpfc hba data structure.
5784 *
5785 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
5786 * memory map.
5787 **/
5788static void
5789lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
5790{
James Smart2fcee4b2010-12-15 17:57:46 -05005791 phba->sli4_hba.PSMPHRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5792 LPFC_SLIPORT_IF0_SMPHR;
James Smartda0436e2009-05-22 14:51:39 -04005793 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05005794 LPFC_HST_ISR0;
James Smartda0436e2009-05-22 14:51:39 -04005795 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05005796 LPFC_HST_IMR0;
James Smartda0436e2009-05-22 14:51:39 -04005797 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
James Smart2fcee4b2010-12-15 17:57:46 -05005798 LPFC_HST_ISCR0;
James Smartda0436e2009-05-22 14:51:39 -04005799}
5800
5801/**
5802 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
5803 * @phba: pointer to lpfc hba data structure.
5804 * @vf: virtual function number
5805 *
5806 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
5807 * based on the given viftual function number, @vf.
5808 *
5809 * Return 0 if successful, otherwise -ENODEV.
5810 **/
5811static int
5812lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
5813{
5814 if (vf > LPFC_VIR_FUNC_MAX)
5815 return -ENODEV;
5816
5817 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5818 vf * LPFC_VFR_PAGE_SIZE + LPFC_RQ_DOORBELL);
5819 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5820 vf * LPFC_VFR_PAGE_SIZE + LPFC_WQ_DOORBELL);
5821 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5822 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
5823 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5824 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
5825 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5826 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
5827 return 0;
5828}
5829
5830/**
5831 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
5832 * @phba: pointer to lpfc hba data structure.
5833 *
5834 * This routine is invoked to create the bootstrap mailbox
5835 * region consistent with the SLI-4 interface spec. This
5836 * routine allocates all memory necessary to communicate
5837 * mailbox commands to the port and sets up all alignment
5838 * needs. No locks are expected to be held when calling
5839 * this routine.
5840 *
5841 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005842 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005843 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04005844 **/
5845static int
5846lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
5847{
5848 uint32_t bmbx_size;
5849 struct lpfc_dmabuf *dmabuf;
5850 struct dma_address *dma_address;
5851 uint32_t pa_addr;
5852 uint64_t phys_addr;
5853
5854 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5855 if (!dmabuf)
5856 return -ENOMEM;
5857
5858 /*
5859 * The bootstrap mailbox region is comprised of 2 parts
5860 * plus an alignment restriction of 16 bytes.
5861 */
5862 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
5863 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5864 bmbx_size,
5865 &dmabuf->phys,
5866 GFP_KERNEL);
5867 if (!dmabuf->virt) {
5868 kfree(dmabuf);
5869 return -ENOMEM;
5870 }
5871 memset(dmabuf->virt, 0, bmbx_size);
5872
5873 /*
5874 * Initialize the bootstrap mailbox pointers now so that the register
5875 * operations are simple later. The mailbox dma address is required
5876 * to be 16-byte aligned. Also align the virtual memory as each
5877 * maibox is copied into the bmbx mailbox region before issuing the
5878 * command to the port.
5879 */
5880 phba->sli4_hba.bmbx.dmabuf = dmabuf;
5881 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
5882
5883 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
5884 LPFC_ALIGN_16_BYTE);
5885 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
5886 LPFC_ALIGN_16_BYTE);
5887
5888 /*
5889 * Set the high and low physical addresses now. The SLI4 alignment
5890 * requirement is 16 bytes and the mailbox is posted to the port
5891 * as two 30-bit addresses. The other data is a bit marking whether
5892 * the 30-bit address is the high or low address.
5893 * Upcast bmbx aphys to 64bits so shift instruction compiles
5894 * clean on 32 bit machines.
5895 */
5896 dma_address = &phba->sli4_hba.bmbx.dma_address;
5897 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
5898 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
5899 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
5900 LPFC_BMBX_BIT1_ADDR_HI);
5901
5902 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
5903 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
5904 LPFC_BMBX_BIT1_ADDR_LO);
5905 return 0;
5906}
5907
5908/**
5909 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
5910 * @phba: pointer to lpfc hba data structure.
5911 *
5912 * This routine is invoked to teardown the bootstrap mailbox
5913 * region and release all host resources. This routine requires
5914 * the caller to ensure all mailbox commands recovered, no
5915 * additional mailbox comands are sent, and interrupts are disabled
5916 * before calling this routine.
5917 *
5918 **/
5919static void
5920lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
5921{
5922 dma_free_coherent(&phba->pcidev->dev,
5923 phba->sli4_hba.bmbx.bmbx_size,
5924 phba->sli4_hba.bmbx.dmabuf->virt,
5925 phba->sli4_hba.bmbx.dmabuf->phys);
5926
5927 kfree(phba->sli4_hba.bmbx.dmabuf);
5928 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
5929}
5930
5931/**
5932 * lpfc_sli4_read_config - Get the config parameters.
5933 * @phba: pointer to lpfc hba data structure.
5934 *
5935 * This routine is invoked to read the configuration parameters from the HBA.
5936 * The configuration parameters are used to set the base and maximum values
5937 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
5938 * allocation for the port.
5939 *
5940 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005941 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005942 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04005943 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04005944 **/
5945static int
5946lpfc_sli4_read_config(struct lpfc_hba *phba)
5947{
5948 LPFC_MBOXQ_t *pmb;
5949 struct lpfc_mbx_read_config *rd_config;
James Smart912e3ac2011-05-24 11:42:11 -04005950 union lpfc_sli4_cfg_shdr *shdr;
5951 uint32_t shdr_status, shdr_add_status;
5952 struct lpfc_mbx_get_func_cfg *get_func_cfg;
5953 struct lpfc_rsrc_desc_fcfcoe *desc;
5954 uint32_t desc_count;
5955 int length, i, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04005956
5957 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5958 if (!pmb) {
5959 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5960 "2011 Unable to allocate memory for issuing "
5961 "SLI_CONFIG_SPECIAL mailbox command\n");
5962 return -ENOMEM;
5963 }
5964
5965 lpfc_read_config(phba, pmb);
5966
5967 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
5968 if (rc != MBX_SUCCESS) {
5969 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5970 "2012 Mailbox failed , mbxCmd x%x "
5971 "READ_CONFIG, mbxStatus x%x\n",
5972 bf_get(lpfc_mqe_command, &pmb->u.mqe),
5973 bf_get(lpfc_mqe_status, &pmb->u.mqe));
5974 rc = -EIO;
5975 } else {
5976 rd_config = &pmb->u.mqe.un.rd_config;
James Smart6d368e52011-05-24 11:44:12 -04005977 phba->sli4_hba.extents_in_use =
5978 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04005979 phba->sli4_hba.max_cfg_param.max_xri =
5980 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
5981 phba->sli4_hba.max_cfg_param.xri_base =
5982 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
5983 phba->sli4_hba.max_cfg_param.max_vpi =
5984 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
5985 phba->sli4_hba.max_cfg_param.vpi_base =
5986 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
5987 phba->sli4_hba.max_cfg_param.max_rpi =
5988 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
5989 phba->sli4_hba.max_cfg_param.rpi_base =
5990 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
5991 phba->sli4_hba.max_cfg_param.max_vfi =
5992 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
5993 phba->sli4_hba.max_cfg_param.vfi_base =
5994 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
5995 phba->sli4_hba.max_cfg_param.max_fcfi =
5996 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
James Smartda0436e2009-05-22 14:51:39 -04005997 phba->sli4_hba.max_cfg_param.max_eq =
5998 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
5999 phba->sli4_hba.max_cfg_param.max_rq =
6000 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
6001 phba->sli4_hba.max_cfg_param.max_wq =
6002 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
6003 phba->sli4_hba.max_cfg_param.max_cq =
6004 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
6005 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
6006 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
6007 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
6008 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
6009 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
James Smart5ffc2662009-11-18 15:39:44 -05006010 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
6011 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04006012 phba->max_vports = phba->max_vpi;
6013 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart6d368e52011-05-24 11:44:12 -04006014 "2003 cfg params Extents? %d "
6015 "XRI(B:%d M:%d), "
James Smartda0436e2009-05-22 14:51:39 -04006016 "VPI(B:%d M:%d) "
6017 "VFI(B:%d M:%d) "
6018 "RPI(B:%d M:%d) "
James Smart6d368e52011-05-24 11:44:12 -04006019 "FCFI(Count:%d)\n",
6020 phba->sli4_hba.extents_in_use,
James Smartda0436e2009-05-22 14:51:39 -04006021 phba->sli4_hba.max_cfg_param.xri_base,
6022 phba->sli4_hba.max_cfg_param.max_xri,
6023 phba->sli4_hba.max_cfg_param.vpi_base,
6024 phba->sli4_hba.max_cfg_param.max_vpi,
6025 phba->sli4_hba.max_cfg_param.vfi_base,
6026 phba->sli4_hba.max_cfg_param.max_vfi,
6027 phba->sli4_hba.max_cfg_param.rpi_base,
6028 phba->sli4_hba.max_cfg_param.max_rpi,
James Smartda0436e2009-05-22 14:51:39 -04006029 phba->sli4_hba.max_cfg_param.max_fcfi);
6030 }
James Smart912e3ac2011-05-24 11:42:11 -04006031
6032 if (rc)
6033 goto read_cfg_out;
James Smartda0436e2009-05-22 14:51:39 -04006034
6035 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
James Smartb92938b2010-06-07 15:24:12 -04006036 if (phba->cfg_hba_queue_depth >
6037 (phba->sli4_hba.max_cfg_param.max_xri -
6038 lpfc_sli4_get_els_iocb_cnt(phba)))
James Smartda0436e2009-05-22 14:51:39 -04006039 phba->cfg_hba_queue_depth =
James Smartb92938b2010-06-07 15:24:12 -04006040 phba->sli4_hba.max_cfg_param.max_xri -
6041 lpfc_sli4_get_els_iocb_cnt(phba);
James Smart912e3ac2011-05-24 11:42:11 -04006042
6043 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
6044 LPFC_SLI_INTF_IF_TYPE_2)
6045 goto read_cfg_out;
6046
6047 /* get the pf# and vf# for SLI4 if_type 2 port */
6048 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
6049 sizeof(struct lpfc_sli4_cfg_mhdr));
6050 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
6051 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
6052 length, LPFC_SLI4_MBX_EMBED);
6053
6054 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6055 shdr = (union lpfc_sli4_cfg_shdr *)
6056 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
6057 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6058 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6059 if (rc || shdr_status || shdr_add_status) {
6060 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6061 "3026 Mailbox failed , mbxCmd x%x "
6062 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
6063 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6064 bf_get(lpfc_mqe_status, &pmb->u.mqe));
6065 rc = -EIO;
6066 goto read_cfg_out;
6067 }
6068
6069 /* search for fc_fcoe resrouce descriptor */
6070 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
6071 desc_count = get_func_cfg->func_cfg.rsrc_desc_count;
6072
6073 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
6074 desc = (struct lpfc_rsrc_desc_fcfcoe *)
6075 &get_func_cfg->func_cfg.desc[i];
6076 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
6077 bf_get(lpfc_rsrc_desc_pcie_type, desc)) {
6078 phba->sli4_hba.iov.pf_number =
6079 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
6080 phba->sli4_hba.iov.vf_number =
6081 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
6082 break;
6083 }
6084 }
6085
6086 if (i < LPFC_RSRC_DESC_MAX_NUM)
6087 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6088 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
6089 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
6090 phba->sli4_hba.iov.vf_number);
6091 else {
6092 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6093 "3028 GET_FUNCTION_CONFIG: failed to find "
6094 "Resrouce Descriptor:x%x\n",
6095 LPFC_RSRC_DESC_TYPE_FCFCOE);
6096 rc = -EIO;
6097 }
6098
6099read_cfg_out:
6100 mempool_free(pmb, phba->mbox_mem_pool);
James Smartda0436e2009-05-22 14:51:39 -04006101 return rc;
6102}
6103
6104/**
James Smart2fcee4b2010-12-15 17:57:46 -05006105 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
James Smartda0436e2009-05-22 14:51:39 -04006106 * @phba: pointer to lpfc hba data structure.
6107 *
James Smart2fcee4b2010-12-15 17:57:46 -05006108 * This routine is invoked to setup the port-side endian order when
6109 * the port if_type is 0. This routine has no function for other
6110 * if_types.
James Smartda0436e2009-05-22 14:51:39 -04006111 *
6112 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006113 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006114 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006115 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006116 **/
6117static int
6118lpfc_setup_endian_order(struct lpfc_hba *phba)
6119{
6120 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05006121 uint32_t if_type, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04006122 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
6123 HOST_ENDIAN_HIGH_WORD1};
6124
James Smart2fcee4b2010-12-15 17:57:46 -05006125 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
6126 switch (if_type) {
6127 case LPFC_SLI_INTF_IF_TYPE_0:
6128 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6129 GFP_KERNEL);
6130 if (!mboxq) {
6131 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6132 "0492 Unable to allocate memory for "
6133 "issuing SLI_CONFIG_SPECIAL mailbox "
6134 "command\n");
6135 return -ENOMEM;
6136 }
James Smartda0436e2009-05-22 14:51:39 -04006137
James Smart2fcee4b2010-12-15 17:57:46 -05006138 /*
6139 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
6140 * two words to contain special data values and no other data.
6141 */
6142 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
6143 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
6144 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6145 if (rc != MBX_SUCCESS) {
6146 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6147 "0493 SLI_CONFIG_SPECIAL mailbox "
6148 "failed with status x%x\n",
6149 rc);
6150 rc = -EIO;
6151 }
6152 mempool_free(mboxq, phba->mbox_mem_pool);
6153 break;
6154 case LPFC_SLI_INTF_IF_TYPE_2:
6155 case LPFC_SLI_INTF_IF_TYPE_1:
6156 default:
6157 break;
James Smartda0436e2009-05-22 14:51:39 -04006158 }
James Smartda0436e2009-05-22 14:51:39 -04006159 return rc;
6160}
6161
6162/**
James Smart5350d872011-10-10 21:33:49 -04006163 * lpfc_sli4_queue_verify - Verify and update EQ and CQ counts
James Smartda0436e2009-05-22 14:51:39 -04006164 * @phba: pointer to lpfc hba data structure.
6165 *
James Smart5350d872011-10-10 21:33:49 -04006166 * This routine is invoked to check the user settable queue counts for EQs and
6167 * CQs. after this routine is called the counts will be set to valid values that
6168 * adhere to the constraints of the system's interrupt vectors and the port's
6169 * queue resources.
James Smartda0436e2009-05-22 14:51:39 -04006170 *
6171 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006172 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006173 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04006174 **/
6175static int
James Smart5350d872011-10-10 21:33:49 -04006176lpfc_sli4_queue_verify(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04006177{
James Smartda0436e2009-05-22 14:51:39 -04006178 int cfg_fcp_wq_count;
6179 int cfg_fcp_eq_count;
6180
6181 /*
6182 * Sanity check for confiugred queue parameters against the run-time
6183 * device parameters
6184 */
6185
6186 /* Sanity check on FCP fast-path WQ parameters */
6187 cfg_fcp_wq_count = phba->cfg_fcp_wq_count;
6188 if (cfg_fcp_wq_count >
6189 (phba->sli4_hba.max_cfg_param.max_wq - LPFC_SP_WQN_DEF)) {
6190 cfg_fcp_wq_count = phba->sli4_hba.max_cfg_param.max_wq -
6191 LPFC_SP_WQN_DEF;
6192 if (cfg_fcp_wq_count < LPFC_FP_WQN_MIN) {
6193 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6194 "2581 Not enough WQs (%d) from "
6195 "the pci function for supporting "
6196 "FCP WQs (%d)\n",
6197 phba->sli4_hba.max_cfg_param.max_wq,
6198 phba->cfg_fcp_wq_count);
6199 goto out_error;
6200 }
6201 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6202 "2582 Not enough WQs (%d) from the pci "
6203 "function for supporting the requested "
6204 "FCP WQs (%d), the actual FCP WQs can "
6205 "be supported: %d\n",
6206 phba->sli4_hba.max_cfg_param.max_wq,
6207 phba->cfg_fcp_wq_count, cfg_fcp_wq_count);
6208 }
6209 /* The actual number of FCP work queues adopted */
6210 phba->cfg_fcp_wq_count = cfg_fcp_wq_count;
6211
6212 /* Sanity check on FCP fast-path EQ parameters */
6213 cfg_fcp_eq_count = phba->cfg_fcp_eq_count;
6214 if (cfg_fcp_eq_count >
6215 (phba->sli4_hba.max_cfg_param.max_eq - LPFC_SP_EQN_DEF)) {
6216 cfg_fcp_eq_count = phba->sli4_hba.max_cfg_param.max_eq -
6217 LPFC_SP_EQN_DEF;
6218 if (cfg_fcp_eq_count < LPFC_FP_EQN_MIN) {
6219 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6220 "2574 Not enough EQs (%d) from the "
6221 "pci function for supporting FCP "
6222 "EQs (%d)\n",
6223 phba->sli4_hba.max_cfg_param.max_eq,
6224 phba->cfg_fcp_eq_count);
6225 goto out_error;
6226 }
6227 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6228 "2575 Not enough EQs (%d) from the pci "
6229 "function for supporting the requested "
6230 "FCP EQs (%d), the actual FCP EQs can "
6231 "be supported: %d\n",
6232 phba->sli4_hba.max_cfg_param.max_eq,
6233 phba->cfg_fcp_eq_count, cfg_fcp_eq_count);
6234 }
6235 /* It does not make sense to have more EQs than WQs */
6236 if (cfg_fcp_eq_count > phba->cfg_fcp_wq_count) {
6237 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smart6a9c52c2009-10-02 15:16:51 -04006238 "2593 The FCP EQ count(%d) cannot be greater "
6239 "than the FCP WQ count(%d), limiting the "
6240 "FCP EQ count to %d\n", cfg_fcp_eq_count,
James Smartda0436e2009-05-22 14:51:39 -04006241 phba->cfg_fcp_wq_count,
6242 phba->cfg_fcp_wq_count);
6243 cfg_fcp_eq_count = phba->cfg_fcp_wq_count;
6244 }
6245 /* The actual number of FCP event queues adopted */
6246 phba->cfg_fcp_eq_count = cfg_fcp_eq_count;
6247 /* The overall number of event queues used */
6248 phba->sli4_hba.cfg_eqn = phba->cfg_fcp_eq_count + LPFC_SP_EQN_DEF;
6249
James Smartda0436e2009-05-22 14:51:39 -04006250 /* Get EQ depth from module parameter, fake the default for now */
6251 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
6252 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
6253
James Smart5350d872011-10-10 21:33:49 -04006254 /* Get CQ depth from module parameter, fake the default for now */
6255 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
6256 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
6257
6258 return 0;
6259out_error:
6260 return -ENOMEM;
6261}
6262
6263/**
6264 * lpfc_sli4_queue_create - Create all the SLI4 queues
6265 * @phba: pointer to lpfc hba data structure.
6266 *
6267 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
6268 * operation. For each SLI4 queue type, the parameters such as queue entry
6269 * count (queue depth) shall be taken from the module parameter. For now,
6270 * we just use some constant number as place holder.
6271 *
6272 * Return codes
6273 * 0 - sucessful
6274 * -ENOMEM - No availble memory
6275 * -EIO - The mailbox failed to complete successfully.
6276 **/
6277int
6278lpfc_sli4_queue_create(struct lpfc_hba *phba)
6279{
6280 struct lpfc_queue *qdesc;
6281 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
6282
6283 /*
6284 * Create Event Queues (EQs)
6285 */
6286
James Smartda0436e2009-05-22 14:51:39 -04006287 /* Create slow path event queue */
6288 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
6289 phba->sli4_hba.eq_ecount);
6290 if (!qdesc) {
6291 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6292 "0496 Failed allocate slow-path EQ\n");
6293 goto out_error;
6294 }
6295 phba->sli4_hba.sp_eq = qdesc;
6296
James Smart5350d872011-10-10 21:33:49 -04006297 /*
6298 * Create fast-path FCP Event Queue(s). The cfg_fcp_eq_count can be
6299 * zero whenever there is exactly one interrupt vector. This is not
6300 * an error.
6301 */
6302 if (phba->cfg_fcp_eq_count) {
6303 phba->sli4_hba.fp_eq = kzalloc((sizeof(struct lpfc_queue *) *
6304 phba->cfg_fcp_eq_count), GFP_KERNEL);
6305 if (!phba->sli4_hba.fp_eq) {
6306 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6307 "2576 Failed allocate memory for "
6308 "fast-path EQ record array\n");
6309 goto out_free_sp_eq;
6310 }
James Smartda0436e2009-05-22 14:51:39 -04006311 }
6312 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
6313 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
6314 phba->sli4_hba.eq_ecount);
6315 if (!qdesc) {
6316 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6317 "0497 Failed allocate fast-path EQ\n");
6318 goto out_free_fp_eq;
6319 }
6320 phba->sli4_hba.fp_eq[fcp_eqidx] = qdesc;
6321 }
6322
6323 /*
6324 * Create Complete Queues (CQs)
6325 */
6326
James Smartda0436e2009-05-22 14:51:39 -04006327 /* Create slow-path Mailbox Command Complete Queue */
6328 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6329 phba->sli4_hba.cq_ecount);
6330 if (!qdesc) {
6331 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6332 "0500 Failed allocate slow-path mailbox CQ\n");
6333 goto out_free_fp_eq;
6334 }
6335 phba->sli4_hba.mbx_cq = qdesc;
6336
6337 /* Create slow-path ELS Complete Queue */
6338 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6339 phba->sli4_hba.cq_ecount);
6340 if (!qdesc) {
6341 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6342 "0501 Failed allocate slow-path ELS CQ\n");
6343 goto out_free_mbx_cq;
6344 }
6345 phba->sli4_hba.els_cq = qdesc;
6346
James Smartda0436e2009-05-22 14:51:39 -04006347
James Smart5350d872011-10-10 21:33:49 -04006348 /*
6349 * Create fast-path FCP Completion Queue(s), one-to-one with FCP EQs.
6350 * If there are no FCP EQs then create exactly one FCP CQ.
6351 */
6352 if (phba->cfg_fcp_eq_count)
6353 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
6354 phba->cfg_fcp_eq_count),
6355 GFP_KERNEL);
6356 else
6357 phba->sli4_hba.fcp_cq = kzalloc(sizeof(struct lpfc_queue *),
6358 GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04006359 if (!phba->sli4_hba.fcp_cq) {
6360 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6361 "2577 Failed allocate memory for fast-path "
6362 "CQ record array\n");
James Smart4d9ab992009-10-02 15:16:39 -04006363 goto out_free_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04006364 }
James Smart5350d872011-10-10 21:33:49 -04006365 fcp_cqidx = 0;
6366 do {
James Smartda0436e2009-05-22 14:51:39 -04006367 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6368 phba->sli4_hba.cq_ecount);
6369 if (!qdesc) {
6370 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6371 "0499 Failed allocate fast-path FCP "
6372 "CQ (%d)\n", fcp_cqidx);
6373 goto out_free_fcp_cq;
6374 }
6375 phba->sli4_hba.fcp_cq[fcp_cqidx] = qdesc;
James Smart5350d872011-10-10 21:33:49 -04006376 } while (++fcp_cqidx < phba->cfg_fcp_eq_count);
James Smartda0436e2009-05-22 14:51:39 -04006377
6378 /* Create Mailbox Command Queue */
6379 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
6380 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
6381
6382 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
6383 phba->sli4_hba.mq_ecount);
6384 if (!qdesc) {
6385 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6386 "0505 Failed allocate slow-path MQ\n");
6387 goto out_free_fcp_cq;
6388 }
6389 phba->sli4_hba.mbx_wq = qdesc;
6390
6391 /*
6392 * Create all the Work Queues (WQs)
6393 */
6394 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
6395 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
6396
6397 /* Create slow-path ELS Work Queue */
6398 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
6399 phba->sli4_hba.wq_ecount);
6400 if (!qdesc) {
6401 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6402 "0504 Failed allocate slow-path ELS WQ\n");
6403 goto out_free_mbx_wq;
6404 }
6405 phba->sli4_hba.els_wq = qdesc;
6406
6407 /* Create fast-path FCP Work Queue(s) */
6408 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
6409 phba->cfg_fcp_wq_count), GFP_KERNEL);
6410 if (!phba->sli4_hba.fcp_wq) {
6411 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6412 "2578 Failed allocate memory for fast-path "
6413 "WQ record array\n");
6414 goto out_free_els_wq;
6415 }
6416 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
6417 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
6418 phba->sli4_hba.wq_ecount);
6419 if (!qdesc) {
6420 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6421 "0503 Failed allocate fast-path FCP "
6422 "WQ (%d)\n", fcp_wqidx);
6423 goto out_free_fcp_wq;
6424 }
6425 phba->sli4_hba.fcp_wq[fcp_wqidx] = qdesc;
6426 }
6427
6428 /*
6429 * Create Receive Queue (RQ)
6430 */
6431 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
6432 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
6433
6434 /* Create Receive Queue for header */
6435 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
6436 phba->sli4_hba.rq_ecount);
6437 if (!qdesc) {
6438 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6439 "0506 Failed allocate receive HRQ\n");
6440 goto out_free_fcp_wq;
6441 }
6442 phba->sli4_hba.hdr_rq = qdesc;
6443
6444 /* Create Receive Queue for data */
6445 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
6446 phba->sli4_hba.rq_ecount);
6447 if (!qdesc) {
6448 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6449 "0507 Failed allocate receive DRQ\n");
6450 goto out_free_hdr_rq;
6451 }
6452 phba->sli4_hba.dat_rq = qdesc;
6453
6454 return 0;
6455
6456out_free_hdr_rq:
6457 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
6458 phba->sli4_hba.hdr_rq = NULL;
6459out_free_fcp_wq:
6460 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--) {
6461 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_wqidx]);
6462 phba->sli4_hba.fcp_wq[fcp_wqidx] = NULL;
6463 }
6464 kfree(phba->sli4_hba.fcp_wq);
6465out_free_els_wq:
6466 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
6467 phba->sli4_hba.els_wq = NULL;
6468out_free_mbx_wq:
6469 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
6470 phba->sli4_hba.mbx_wq = NULL;
6471out_free_fcp_cq:
6472 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--) {
6473 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_cqidx]);
6474 phba->sli4_hba.fcp_cq[fcp_cqidx] = NULL;
6475 }
6476 kfree(phba->sli4_hba.fcp_cq);
James Smartda0436e2009-05-22 14:51:39 -04006477out_free_els_cq:
6478 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
6479 phba->sli4_hba.els_cq = NULL;
6480out_free_mbx_cq:
6481 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
6482 phba->sli4_hba.mbx_cq = NULL;
6483out_free_fp_eq:
6484 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--) {
6485 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_eqidx]);
6486 phba->sli4_hba.fp_eq[fcp_eqidx] = NULL;
6487 }
6488 kfree(phba->sli4_hba.fp_eq);
6489out_free_sp_eq:
6490 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
6491 phba->sli4_hba.sp_eq = NULL;
6492out_error:
6493 return -ENOMEM;
6494}
6495
6496/**
6497 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
6498 * @phba: pointer to lpfc hba data structure.
6499 *
6500 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
6501 * operation.
6502 *
6503 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006504 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006505 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006506 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006507 **/
James Smart5350d872011-10-10 21:33:49 -04006508void
James Smartda0436e2009-05-22 14:51:39 -04006509lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
6510{
6511 int fcp_qidx;
6512
6513 /* Release mailbox command work queue */
6514 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
6515 phba->sli4_hba.mbx_wq = NULL;
6516
6517 /* Release ELS work queue */
6518 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
6519 phba->sli4_hba.els_wq = NULL;
6520
6521 /* Release FCP work queue */
6522 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
6523 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_qidx]);
6524 kfree(phba->sli4_hba.fcp_wq);
6525 phba->sli4_hba.fcp_wq = NULL;
6526
6527 /* Release unsolicited receive queue */
6528 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
6529 phba->sli4_hba.hdr_rq = NULL;
6530 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
6531 phba->sli4_hba.dat_rq = NULL;
6532
James Smartda0436e2009-05-22 14:51:39 -04006533 /* Release ELS complete queue */
6534 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
6535 phba->sli4_hba.els_cq = NULL;
6536
6537 /* Release mailbox command complete queue */
6538 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
6539 phba->sli4_hba.mbx_cq = NULL;
6540
6541 /* Release FCP response complete queue */
James Smart05580562011-05-24 11:40:48 -04006542 fcp_qidx = 0;
6543 do
James Smartda0436e2009-05-22 14:51:39 -04006544 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_qidx]);
James Smart05580562011-05-24 11:40:48 -04006545 while (++fcp_qidx < phba->cfg_fcp_eq_count);
James Smartda0436e2009-05-22 14:51:39 -04006546 kfree(phba->sli4_hba.fcp_cq);
6547 phba->sli4_hba.fcp_cq = NULL;
6548
6549 /* Release fast-path event queue */
6550 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
6551 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_qidx]);
6552 kfree(phba->sli4_hba.fp_eq);
6553 phba->sli4_hba.fp_eq = NULL;
6554
6555 /* Release slow-path event queue */
6556 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
6557 phba->sli4_hba.sp_eq = NULL;
6558
6559 return;
6560}
6561
6562/**
6563 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
6564 * @phba: pointer to lpfc hba data structure.
6565 *
6566 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
6567 * operation.
6568 *
6569 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006570 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006571 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006572 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006573 **/
6574int
6575lpfc_sli4_queue_setup(struct lpfc_hba *phba)
6576{
6577 int rc = -ENOMEM;
6578 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
6579 int fcp_cq_index = 0;
6580
6581 /*
6582 * Set up Event Queues (EQs)
6583 */
6584
6585 /* Set up slow-path event queue */
6586 if (!phba->sli4_hba.sp_eq) {
6587 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6588 "0520 Slow-path EQ not allocated\n");
6589 goto out_error;
6590 }
6591 rc = lpfc_eq_create(phba, phba->sli4_hba.sp_eq,
6592 LPFC_SP_DEF_IMAX);
6593 if (rc) {
6594 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6595 "0521 Failed setup of slow-path EQ: "
6596 "rc = 0x%x\n", rc);
6597 goto out_error;
6598 }
6599 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6600 "2583 Slow-path EQ setup: queue-id=%d\n",
6601 phba->sli4_hba.sp_eq->queue_id);
6602
6603 /* Set up fast-path event queue */
6604 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
6605 if (!phba->sli4_hba.fp_eq[fcp_eqidx]) {
6606 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6607 "0522 Fast-path EQ (%d) not "
6608 "allocated\n", fcp_eqidx);
6609 goto out_destroy_fp_eq;
6610 }
6611 rc = lpfc_eq_create(phba, phba->sli4_hba.fp_eq[fcp_eqidx],
6612 phba->cfg_fcp_imax);
6613 if (rc) {
6614 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6615 "0523 Failed setup of fast-path EQ "
6616 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
6617 goto out_destroy_fp_eq;
6618 }
6619 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6620 "2584 Fast-path EQ setup: "
6621 "queue[%d]-id=%d\n", fcp_eqidx,
6622 phba->sli4_hba.fp_eq[fcp_eqidx]->queue_id);
6623 }
6624
6625 /*
6626 * Set up Complete Queues (CQs)
6627 */
6628
6629 /* Set up slow-path MBOX Complete Queue as the first CQ */
6630 if (!phba->sli4_hba.mbx_cq) {
6631 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6632 "0528 Mailbox CQ not allocated\n");
6633 goto out_destroy_fp_eq;
6634 }
6635 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq, phba->sli4_hba.sp_eq,
6636 LPFC_MCQ, LPFC_MBOX);
6637 if (rc) {
6638 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6639 "0529 Failed setup of slow-path mailbox CQ: "
6640 "rc = 0x%x\n", rc);
6641 goto out_destroy_fp_eq;
6642 }
6643 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6644 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
6645 phba->sli4_hba.mbx_cq->queue_id,
6646 phba->sli4_hba.sp_eq->queue_id);
6647
6648 /* Set up slow-path ELS Complete Queue */
6649 if (!phba->sli4_hba.els_cq) {
6650 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6651 "0530 ELS CQ not allocated\n");
6652 goto out_destroy_mbx_cq;
6653 }
6654 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq, phba->sli4_hba.sp_eq,
6655 LPFC_WCQ, LPFC_ELS);
6656 if (rc) {
6657 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6658 "0531 Failed setup of slow-path ELS CQ: "
6659 "rc = 0x%x\n", rc);
6660 goto out_destroy_mbx_cq;
6661 }
6662 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6663 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
6664 phba->sli4_hba.els_cq->queue_id,
6665 phba->sli4_hba.sp_eq->queue_id);
6666
James Smartda0436e2009-05-22 14:51:39 -04006667 /* Set up fast-path FCP Response Complete Queue */
James Smart05580562011-05-24 11:40:48 -04006668 fcp_cqidx = 0;
6669 do {
James Smartda0436e2009-05-22 14:51:39 -04006670 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
6671 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6672 "0526 Fast-path FCP CQ (%d) not "
6673 "allocated\n", fcp_cqidx);
6674 goto out_destroy_fcp_cq;
6675 }
James Smart05580562011-05-24 11:40:48 -04006676 if (phba->cfg_fcp_eq_count)
6677 rc = lpfc_cq_create(phba,
6678 phba->sli4_hba.fcp_cq[fcp_cqidx],
6679 phba->sli4_hba.fp_eq[fcp_cqidx],
6680 LPFC_WCQ, LPFC_FCP);
6681 else
6682 rc = lpfc_cq_create(phba,
6683 phba->sli4_hba.fcp_cq[fcp_cqidx],
6684 phba->sli4_hba.sp_eq,
6685 LPFC_WCQ, LPFC_FCP);
James Smartda0436e2009-05-22 14:51:39 -04006686 if (rc) {
6687 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6688 "0527 Failed setup of fast-path FCP "
6689 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
6690 goto out_destroy_fcp_cq;
6691 }
6692 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6693 "2588 FCP CQ setup: cq[%d]-id=%d, "
James Smart05580562011-05-24 11:40:48 -04006694 "parent %seq[%d]-id=%d\n",
James Smartda0436e2009-05-22 14:51:39 -04006695 fcp_cqidx,
6696 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
James Smart05580562011-05-24 11:40:48 -04006697 (phba->cfg_fcp_eq_count) ? "" : "sp_",
James Smartda0436e2009-05-22 14:51:39 -04006698 fcp_cqidx,
James Smart05580562011-05-24 11:40:48 -04006699 (phba->cfg_fcp_eq_count) ?
6700 phba->sli4_hba.fp_eq[fcp_cqidx]->queue_id :
6701 phba->sli4_hba.sp_eq->queue_id);
6702 } while (++fcp_cqidx < phba->cfg_fcp_eq_count);
James Smartda0436e2009-05-22 14:51:39 -04006703
6704 /*
6705 * Set up all the Work Queues (WQs)
6706 */
6707
6708 /* Set up Mailbox Command Queue */
6709 if (!phba->sli4_hba.mbx_wq) {
6710 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6711 "0538 Slow-path MQ not allocated\n");
6712 goto out_destroy_fcp_cq;
6713 }
6714 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
6715 phba->sli4_hba.mbx_cq, LPFC_MBOX);
6716 if (rc) {
6717 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6718 "0539 Failed setup of slow-path MQ: "
6719 "rc = 0x%x\n", rc);
6720 goto out_destroy_fcp_cq;
6721 }
6722 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6723 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
6724 phba->sli4_hba.mbx_wq->queue_id,
6725 phba->sli4_hba.mbx_cq->queue_id);
6726
6727 /* Set up slow-path ELS Work Queue */
6728 if (!phba->sli4_hba.els_wq) {
6729 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6730 "0536 Slow-path ELS WQ not allocated\n");
6731 goto out_destroy_mbx_wq;
6732 }
6733 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
6734 phba->sli4_hba.els_cq, LPFC_ELS);
6735 if (rc) {
6736 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6737 "0537 Failed setup of slow-path ELS WQ: "
6738 "rc = 0x%x\n", rc);
6739 goto out_destroy_mbx_wq;
6740 }
6741 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6742 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
6743 phba->sli4_hba.els_wq->queue_id,
6744 phba->sli4_hba.els_cq->queue_id);
6745
6746 /* Set up fast-path FCP Work Queue */
6747 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
6748 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
6749 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6750 "0534 Fast-path FCP WQ (%d) not "
6751 "allocated\n", fcp_wqidx);
6752 goto out_destroy_fcp_wq;
6753 }
6754 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
6755 phba->sli4_hba.fcp_cq[fcp_cq_index],
6756 LPFC_FCP);
6757 if (rc) {
6758 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6759 "0535 Failed setup of fast-path FCP "
6760 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
6761 goto out_destroy_fcp_wq;
6762 }
6763 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6764 "2591 FCP WQ setup: wq[%d]-id=%d, "
6765 "parent cq[%d]-id=%d\n",
6766 fcp_wqidx,
6767 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
6768 fcp_cq_index,
6769 phba->sli4_hba.fcp_cq[fcp_cq_index]->queue_id);
6770 /* Round robin FCP Work Queue's Completion Queue assignment */
James Smart05580562011-05-24 11:40:48 -04006771 if (phba->cfg_fcp_eq_count)
6772 fcp_cq_index = ((fcp_cq_index + 1) %
6773 phba->cfg_fcp_eq_count);
James Smartda0436e2009-05-22 14:51:39 -04006774 }
6775
6776 /*
6777 * Create Receive Queue (RQ)
6778 */
6779 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
6780 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6781 "0540 Receive Queue not allocated\n");
6782 goto out_destroy_fcp_wq;
6783 }
James Smart73d91e52011-10-10 21:32:10 -04006784
6785 lpfc_rq_adjust_repost(phba, phba->sli4_hba.hdr_rq, LPFC_ELS_HBQ);
6786 lpfc_rq_adjust_repost(phba, phba->sli4_hba.dat_rq, LPFC_ELS_HBQ);
6787
James Smartda0436e2009-05-22 14:51:39 -04006788 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04006789 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04006790 if (rc) {
6791 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6792 "0541 Failed setup of Receive Queue: "
6793 "rc = 0x%x\n", rc);
6794 goto out_destroy_fcp_wq;
6795 }
James Smart73d91e52011-10-10 21:32:10 -04006796
James Smartda0436e2009-05-22 14:51:39 -04006797 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6798 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
6799 "parent cq-id=%d\n",
6800 phba->sli4_hba.hdr_rq->queue_id,
6801 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04006802 phba->sli4_hba.els_cq->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04006803 return 0;
6804
6805out_destroy_fcp_wq:
6806 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
6807 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
6808 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
6809out_destroy_mbx_wq:
6810 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
6811out_destroy_fcp_cq:
6812 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
6813 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
James Smartda0436e2009-05-22 14:51:39 -04006814 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
6815out_destroy_mbx_cq:
6816 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
6817out_destroy_fp_eq:
6818 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
6819 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_eqidx]);
6820 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
6821out_error:
6822 return rc;
6823}
6824
6825/**
6826 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
6827 * @phba: pointer to lpfc hba data structure.
6828 *
6829 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
6830 * operation.
6831 *
6832 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006833 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006834 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04006835 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04006836 **/
6837void
6838lpfc_sli4_queue_unset(struct lpfc_hba *phba)
6839{
6840 int fcp_qidx;
6841
6842 /* Unset mailbox command work queue */
6843 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
6844 /* Unset ELS work queue */
6845 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
6846 /* Unset unsolicited receive queue */
6847 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
6848 /* Unset FCP work queue */
6849 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
6850 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
6851 /* Unset mailbox command complete queue */
6852 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
6853 /* Unset ELS complete queue */
6854 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
James Smartda0436e2009-05-22 14:51:39 -04006855 /* Unset FCP response complete queue */
James Smart5350d872011-10-10 21:33:49 -04006856 fcp_qidx = 0;
6857 do {
James Smartda0436e2009-05-22 14:51:39 -04006858 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
James Smart5350d872011-10-10 21:33:49 -04006859 } while (++fcp_qidx < phba->cfg_fcp_eq_count);
James Smartda0436e2009-05-22 14:51:39 -04006860 /* Unset fast-path event queue */
6861 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
6862 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_qidx]);
6863 /* Unset slow-path event queue */
6864 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
6865}
6866
6867/**
6868 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
6869 * @phba: pointer to lpfc hba data structure.
6870 *
6871 * This routine is invoked to allocate and set up a pool of completion queue
6872 * events. The body of the completion queue event is a completion queue entry
6873 * CQE. For now, this pool is used for the interrupt service routine to queue
6874 * the following HBA completion queue events for the worker thread to process:
6875 * - Mailbox asynchronous events
6876 * - Receive queue completion unsolicited events
6877 * Later, this can be used for all the slow-path events.
6878 *
6879 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006880 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006881 * -ENOMEM - No available memory
James Smartda0436e2009-05-22 14:51:39 -04006882 **/
6883static int
6884lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
6885{
6886 struct lpfc_cq_event *cq_event;
6887 int i;
6888
6889 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
6890 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
6891 if (!cq_event)
6892 goto out_pool_create_fail;
6893 list_add_tail(&cq_event->list,
6894 &phba->sli4_hba.sp_cqe_event_pool);
6895 }
6896 return 0;
6897
6898out_pool_create_fail:
6899 lpfc_sli4_cq_event_pool_destroy(phba);
6900 return -ENOMEM;
6901}
6902
6903/**
6904 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
6905 * @phba: pointer to lpfc hba data structure.
6906 *
6907 * This routine is invoked to free the pool of completion queue events at
6908 * driver unload time. Note that, it is the responsibility of the driver
6909 * cleanup routine to free all the outstanding completion-queue events
6910 * allocated from this pool back into the pool before invoking this routine
6911 * to destroy the pool.
6912 **/
6913static void
6914lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
6915{
6916 struct lpfc_cq_event *cq_event, *next_cq_event;
6917
6918 list_for_each_entry_safe(cq_event, next_cq_event,
6919 &phba->sli4_hba.sp_cqe_event_pool, list) {
6920 list_del(&cq_event->list);
6921 kfree(cq_event);
6922 }
6923}
6924
6925/**
6926 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
6927 * @phba: pointer to lpfc hba data structure.
6928 *
6929 * This routine is the lock free version of the API invoked to allocate a
6930 * completion-queue event from the free pool.
6931 *
6932 * Return: Pointer to the newly allocated completion-queue event if successful
6933 * NULL otherwise.
6934 **/
6935struct lpfc_cq_event *
6936__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
6937{
6938 struct lpfc_cq_event *cq_event = NULL;
6939
6940 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
6941 struct lpfc_cq_event, list);
6942 return cq_event;
6943}
6944
6945/**
6946 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
6947 * @phba: pointer to lpfc hba data structure.
6948 *
6949 * This routine is the lock version of the API invoked to allocate a
6950 * completion-queue event from the free pool.
6951 *
6952 * Return: Pointer to the newly allocated completion-queue event if successful
6953 * NULL otherwise.
6954 **/
6955struct lpfc_cq_event *
6956lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
6957{
6958 struct lpfc_cq_event *cq_event;
6959 unsigned long iflags;
6960
6961 spin_lock_irqsave(&phba->hbalock, iflags);
6962 cq_event = __lpfc_sli4_cq_event_alloc(phba);
6963 spin_unlock_irqrestore(&phba->hbalock, iflags);
6964 return cq_event;
6965}
6966
6967/**
6968 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
6969 * @phba: pointer to lpfc hba data structure.
6970 * @cq_event: pointer to the completion queue event to be freed.
6971 *
6972 * This routine is the lock free version of the API invoked to release a
6973 * completion-queue event back into the free pool.
6974 **/
6975void
6976__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
6977 struct lpfc_cq_event *cq_event)
6978{
6979 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
6980}
6981
6982/**
6983 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
6984 * @phba: pointer to lpfc hba data structure.
6985 * @cq_event: pointer to the completion queue event to be freed.
6986 *
6987 * This routine is the lock version of the API invoked to release a
6988 * completion-queue event back into the free pool.
6989 **/
6990void
6991lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
6992 struct lpfc_cq_event *cq_event)
6993{
6994 unsigned long iflags;
6995 spin_lock_irqsave(&phba->hbalock, iflags);
6996 __lpfc_sli4_cq_event_release(phba, cq_event);
6997 spin_unlock_irqrestore(&phba->hbalock, iflags);
6998}
6999
7000/**
7001 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
7002 * @phba: pointer to lpfc hba data structure.
7003 *
7004 * This routine is to free all the pending completion-queue events to the
7005 * back into the free pool for device reset.
7006 **/
7007static void
7008lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
7009{
7010 LIST_HEAD(cqelist);
7011 struct lpfc_cq_event *cqe;
7012 unsigned long iflags;
7013
7014 /* Retrieve all the pending WCQEs from pending WCQE lists */
7015 spin_lock_irqsave(&phba->hbalock, iflags);
7016 /* Pending FCP XRI abort events */
7017 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
7018 &cqelist);
7019 /* Pending ELS XRI abort events */
7020 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
7021 &cqelist);
7022 /* Pending asynnc events */
7023 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
7024 &cqelist);
7025 spin_unlock_irqrestore(&phba->hbalock, iflags);
7026
7027 while (!list_empty(&cqelist)) {
7028 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
7029 lpfc_sli4_cq_event_release(phba, cqe);
7030 }
7031}
7032
7033/**
7034 * lpfc_pci_function_reset - Reset pci function.
7035 * @phba: pointer to lpfc hba data structure.
7036 *
7037 * This routine is invoked to request a PCI function reset. It will destroys
7038 * all resources assigned to the PCI function which originates this request.
7039 *
7040 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007041 * 0 - successful
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007042 * -ENOMEM - No available memory
James Smartd439d282010-09-29 11:18:45 -04007043 * -EIO - The mailbox failed to complete successfully.
James Smartda0436e2009-05-22 14:51:39 -04007044 **/
7045int
7046lpfc_pci_function_reset(struct lpfc_hba *phba)
7047{
7048 LPFC_MBOXQ_t *mboxq;
James Smart2fcee4b2010-12-15 17:57:46 -05007049 uint32_t rc = 0, if_type;
James Smartda0436e2009-05-22 14:51:39 -04007050 uint32_t shdr_status, shdr_add_status;
James Smart2fcee4b2010-12-15 17:57:46 -05007051 uint32_t rdy_chk, num_resets = 0, reset_again = 0;
James Smartda0436e2009-05-22 14:51:39 -04007052 union lpfc_sli4_cfg_shdr *shdr;
James Smart2fcee4b2010-12-15 17:57:46 -05007053 struct lpfc_register reg_data;
James Smartda0436e2009-05-22 14:51:39 -04007054
James Smart2fcee4b2010-12-15 17:57:46 -05007055 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7056 switch (if_type) {
7057 case LPFC_SLI_INTF_IF_TYPE_0:
7058 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
7059 GFP_KERNEL);
7060 if (!mboxq) {
7061 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7062 "0494 Unable to allocate memory for "
7063 "issuing SLI_FUNCTION_RESET mailbox "
7064 "command\n");
7065 return -ENOMEM;
7066 }
7067
7068 /* Setup PCI function reset mailbox-ioctl command */
7069 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7070 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
7071 LPFC_SLI4_MBX_EMBED);
7072 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7073 shdr = (union lpfc_sli4_cfg_shdr *)
7074 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7075 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7076 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
7077 &shdr->response);
7078 if (rc != MBX_TIMEOUT)
7079 mempool_free(mboxq, phba->mbox_mem_pool);
7080 if (shdr_status || shdr_add_status || rc) {
7081 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7082 "0495 SLI_FUNCTION_RESET mailbox "
7083 "failed with status x%x add_status x%x,"
7084 " mbx status x%x\n",
7085 shdr_status, shdr_add_status, rc);
7086 rc = -ENXIO;
7087 }
7088 break;
7089 case LPFC_SLI_INTF_IF_TYPE_2:
7090 for (num_resets = 0;
7091 num_resets < MAX_IF_TYPE_2_RESETS;
7092 num_resets++) {
7093 reg_data.word0 = 0;
7094 bf_set(lpfc_sliport_ctrl_end, &reg_data,
7095 LPFC_SLIPORT_LITTLE_ENDIAN);
7096 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
7097 LPFC_SLIPORT_INIT_PORT);
7098 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
7099 CTRLregaddr);
7100
7101 /*
7102 * Poll the Port Status Register and wait for RDY for
7103 * up to 10 seconds. If the port doesn't respond, treat
7104 * it as an error. If the port responds with RN, start
7105 * the loop again.
7106 */
7107 for (rdy_chk = 0; rdy_chk < 1000; rdy_chk++) {
James Smart73d91e52011-10-10 21:32:10 -04007108 msleep(10);
James Smart9940b972011-03-11 16:06:12 -05007109 if (lpfc_readl(phba->sli4_hba.u.if_type2.
7110 STATUSregaddr, &reg_data.word0)) {
7111 rc = -ENODEV;
James Smart73d91e52011-10-10 21:32:10 -04007112 goto out;
James Smart9940b972011-03-11 16:06:12 -05007113 }
James Smart2fcee4b2010-12-15 17:57:46 -05007114 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
7115 break;
7116 if (bf_get(lpfc_sliport_status_rn, &reg_data)) {
7117 reset_again++;
7118 break;
7119 }
James Smart2fcee4b2010-12-15 17:57:46 -05007120 }
7121
7122 /*
7123 * If the port responds to the init request with
7124 * reset needed, delay for a bit and restart the loop.
7125 */
7126 if (reset_again) {
7127 msleep(10);
7128 reset_again = 0;
7129 continue;
7130 }
7131
7132 /* Detect any port errors. */
James Smart2fcee4b2010-12-15 17:57:46 -05007133 if ((bf_get(lpfc_sliport_status_err, &reg_data)) ||
7134 (rdy_chk >= 1000)) {
7135 phba->work_status[0] = readl(
7136 phba->sli4_hba.u.if_type2.ERR1regaddr);
7137 phba->work_status[1] = readl(
7138 phba->sli4_hba.u.if_type2.ERR2regaddr);
7139 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7140 "2890 Port Error Detected "
7141 "during Port Reset: "
7142 "port status reg 0x%x, "
7143 "error 1=0x%x, error 2=0x%x\n",
7144 reg_data.word0,
7145 phba->work_status[0],
7146 phba->work_status[1]);
7147 rc = -ENODEV;
7148 }
7149
7150 /*
7151 * Terminate the outer loop provided the Port indicated
7152 * ready within 10 seconds.
7153 */
7154 if (rdy_chk < 1000)
7155 break;
7156 }
James Smart05580562011-05-24 11:40:48 -04007157 /* delay driver action following IF_TYPE_2 function reset */
7158 msleep(100);
James Smart2fcee4b2010-12-15 17:57:46 -05007159 break;
7160 case LPFC_SLI_INTF_IF_TYPE_1:
7161 default:
7162 break;
James Smartda0436e2009-05-22 14:51:39 -04007163 }
7164
James Smart73d91e52011-10-10 21:32:10 -04007165out:
James Smart2fcee4b2010-12-15 17:57:46 -05007166 /* Catch the not-ready port failure after a port reset. */
7167 if (num_resets >= MAX_IF_TYPE_2_RESETS)
7168 rc = -ENODEV;
7169
James Smartda0436e2009-05-22 14:51:39 -04007170 return rc;
7171}
7172
7173/**
7174 * lpfc_sli4_send_nop_mbox_cmds - Send sli-4 nop mailbox commands
7175 * @phba: pointer to lpfc hba data structure.
7176 * @cnt: number of nop mailbox commands to send.
7177 *
7178 * This routine is invoked to send a number @cnt of NOP mailbox command and
7179 * wait for each command to complete.
7180 *
7181 * Return: the number of NOP mailbox command completed.
7182 **/
7183static int
7184lpfc_sli4_send_nop_mbox_cmds(struct lpfc_hba *phba, uint32_t cnt)
7185{
7186 LPFC_MBOXQ_t *mboxq;
7187 int length, cmdsent;
7188 uint32_t mbox_tmo;
7189 uint32_t rc = 0;
7190 uint32_t shdr_status, shdr_add_status;
7191 union lpfc_sli4_cfg_shdr *shdr;
7192
7193 if (cnt == 0) {
7194 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7195 "2518 Requested to send 0 NOP mailbox cmd\n");
7196 return cnt;
7197 }
7198
7199 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7200 if (!mboxq) {
7201 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7202 "2519 Unable to allocate memory for issuing "
7203 "NOP mailbox command\n");
7204 return 0;
7205 }
7206
7207 /* Set up NOP SLI4_CONFIG mailbox-ioctl command */
7208 length = (sizeof(struct lpfc_mbx_nop) -
7209 sizeof(struct lpfc_sli4_cfg_mhdr));
7210 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7211 LPFC_MBOX_OPCODE_NOP, length, LPFC_SLI4_MBX_EMBED);
7212
James Smartda0436e2009-05-22 14:51:39 -04007213 for (cmdsent = 0; cmdsent < cnt; cmdsent++) {
7214 if (!phba->sli4_hba.intr_enable)
7215 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -04007216 else {
7217 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04007218 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smarta183a152011-10-10 21:32:43 -04007219 }
James Smartda0436e2009-05-22 14:51:39 -04007220 if (rc == MBX_TIMEOUT)
7221 break;
7222 /* Check return status */
7223 shdr = (union lpfc_sli4_cfg_shdr *)
7224 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7225 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7226 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
7227 &shdr->response);
7228 if (shdr_status || shdr_add_status || rc) {
7229 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7230 "2520 NOP mailbox command failed "
7231 "status x%x add_status x%x mbx "
7232 "status x%x\n", shdr_status,
7233 shdr_add_status, rc);
7234 break;
7235 }
7236 }
7237
7238 if (rc != MBX_TIMEOUT)
7239 mempool_free(mboxq, phba->mbox_mem_pool);
7240
7241 return cmdsent;
7242}
7243
7244/**
James Smartda0436e2009-05-22 14:51:39 -04007245 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
7246 * @phba: pointer to lpfc hba data structure.
7247 *
7248 * This routine is invoked to set up the PCI device memory space for device
7249 * with SLI-4 interface spec.
7250 *
7251 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007252 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007253 * other values - error
7254 **/
7255static int
7256lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
7257{
7258 struct pci_dev *pdev;
7259 unsigned long bar0map_len, bar1map_len, bar2map_len;
7260 int error = -ENODEV;
James Smart2fcee4b2010-12-15 17:57:46 -05007261 uint32_t if_type;
James Smartda0436e2009-05-22 14:51:39 -04007262
7263 /* Obtain PCI device reference */
7264 if (!phba->pcidev)
7265 return error;
7266 else
7267 pdev = phba->pcidev;
7268
7269 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05007270 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
7271 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
7272 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
7273 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smartda0436e2009-05-22 14:51:39 -04007274 return error;
Michael Reed8e685972009-09-18 12:02:05 -05007275 }
7276 }
James Smartda0436e2009-05-22 14:51:39 -04007277
James Smart2fcee4b2010-12-15 17:57:46 -05007278 /*
7279 * The BARs and register set definitions and offset locations are
7280 * dependent on the if_type.
7281 */
7282 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
7283 &phba->sli4_hba.sli_intf.word0)) {
7284 return error;
7285 }
7286
7287 /* There is no SLI3 failback for SLI4 devices. */
7288 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
7289 LPFC_SLI_INTF_VALID) {
7290 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7291 "2894 SLI_INTF reg contents invalid "
7292 "sli_intf reg 0x%x\n",
7293 phba->sli4_hba.sli_intf.word0);
7294 return error;
7295 }
7296
7297 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7298 /*
7299 * Get the bus address of SLI4 device Bar regions and the
7300 * number of bytes required by each mapping. The mapping of the
7301 * particular PCI BARs regions is dependent on the type of
7302 * SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -04007303 */
James Smart1dfb5a42010-02-12 14:40:50 -05007304 if (pci_resource_start(pdev, 0)) {
7305 phba->pci_bar0_map = pci_resource_start(pdev, 0);
7306 bar0map_len = pci_resource_len(pdev, 0);
James Smart2fcee4b2010-12-15 17:57:46 -05007307
7308 /*
7309 * Map SLI4 PCI Config Space Register base to a kernel virtual
7310 * addr
7311 */
7312 phba->sli4_hba.conf_regs_memmap_p =
7313 ioremap(phba->pci_bar0_map, bar0map_len);
7314 if (!phba->sli4_hba.conf_regs_memmap_p) {
7315 dev_printk(KERN_ERR, &pdev->dev,
7316 "ioremap failed for SLI4 PCI config "
7317 "registers.\n");
7318 goto out;
7319 }
7320 /* Set up BAR0 PCI config space register memory map */
7321 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smart1dfb5a42010-02-12 14:40:50 -05007322 } else {
7323 phba->pci_bar0_map = pci_resource_start(pdev, 1);
7324 bar0map_len = pci_resource_len(pdev, 1);
James Smart2fcee4b2010-12-15 17:57:46 -05007325 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
7326 dev_printk(KERN_ERR, &pdev->dev,
7327 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
7328 goto out;
7329 }
7330 phba->sli4_hba.conf_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04007331 ioremap(phba->pci_bar0_map, bar0map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05007332 if (!phba->sli4_hba.conf_regs_memmap_p) {
7333 dev_printk(KERN_ERR, &pdev->dev,
7334 "ioremap failed for SLI4 PCI config "
7335 "registers.\n");
7336 goto out;
7337 }
7338 lpfc_sli4_bar0_register_memmap(phba, if_type);
James Smartda0436e2009-05-22 14:51:39 -04007339 }
7340
James Smartc31098c2011-04-16 11:03:33 -04007341 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
7342 (pci_resource_start(pdev, 2))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007343 /*
7344 * Map SLI4 if type 0 HBA Control Register base to a kernel
7345 * virtual address and setup the registers.
7346 */
7347 phba->pci_bar1_map = pci_resource_start(pdev, 2);
7348 bar1map_len = pci_resource_len(pdev, 2);
7349 phba->sli4_hba.ctrl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04007350 ioremap(phba->pci_bar1_map, bar1map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05007351 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
7352 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04007353 "ioremap failed for SLI4 HBA control registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05007354 goto out_iounmap_conf;
7355 }
7356 lpfc_sli4_bar1_register_memmap(phba);
James Smartda0436e2009-05-22 14:51:39 -04007357 }
7358
James Smartc31098c2011-04-16 11:03:33 -04007359 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
7360 (pci_resource_start(pdev, 4))) {
James Smart2fcee4b2010-12-15 17:57:46 -05007361 /*
7362 * Map SLI4 if type 0 HBA Doorbell Register base to a kernel
7363 * virtual address and setup the registers.
7364 */
7365 phba->pci_bar2_map = pci_resource_start(pdev, 4);
7366 bar2map_len = pci_resource_len(pdev, 4);
7367 phba->sli4_hba.drbl_regs_memmap_p =
James Smartda0436e2009-05-22 14:51:39 -04007368 ioremap(phba->pci_bar2_map, bar2map_len);
James Smart2fcee4b2010-12-15 17:57:46 -05007369 if (!phba->sli4_hba.drbl_regs_memmap_p) {
7370 dev_printk(KERN_ERR, &pdev->dev,
James Smartda0436e2009-05-22 14:51:39 -04007371 "ioremap failed for SLI4 HBA doorbell registers.\n");
James Smart2fcee4b2010-12-15 17:57:46 -05007372 goto out_iounmap_ctrl;
7373 }
7374 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
7375 if (error)
7376 goto out_iounmap_all;
James Smartda0436e2009-05-22 14:51:39 -04007377 }
7378
James Smartda0436e2009-05-22 14:51:39 -04007379 return 0;
7380
7381out_iounmap_all:
7382 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
7383out_iounmap_ctrl:
7384 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
7385out_iounmap_conf:
7386 iounmap(phba->sli4_hba.conf_regs_memmap_p);
7387out:
7388 return error;
7389}
7390
7391/**
7392 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
7393 * @phba: pointer to lpfc hba data structure.
7394 *
7395 * This routine is invoked to unset the PCI device memory space for device
7396 * with SLI-4 interface spec.
7397 **/
7398static void
7399lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
7400{
7401 struct pci_dev *pdev;
7402
7403 /* Obtain PCI device reference */
7404 if (!phba->pcidev)
7405 return;
7406 else
7407 pdev = phba->pcidev;
7408
7409 /* Free coherent DMA memory allocated */
7410
7411 /* Unmap I/O memory space */
7412 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
7413 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
7414 iounmap(phba->sli4_hba.conf_regs_memmap_p);
7415
7416 return;
7417}
7418
7419/**
James Smart3772a992009-05-22 14:50:54 -04007420 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
7421 * @phba: pointer to lpfc hba data structure.
7422 *
7423 * This routine is invoked to enable the MSI-X interrupt vectors to device
7424 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
7425 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
7426 * invoked, enables either all or nothing, depending on the current
7427 * availability of PCI vector resources. The device driver is responsible
7428 * for calling the individual request_irq() to register each MSI-X vector
7429 * with a interrupt handler, which is done in this function. Note that
7430 * later when device is unloading, the driver should always call free_irq()
7431 * on all MSI-X vectors it has done request_irq() on before calling
7432 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
7433 * will be left with MSI-X enabled and leaks its vectors.
7434 *
7435 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007436 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007437 * other values - error
7438 **/
7439static int
7440lpfc_sli_enable_msix(struct lpfc_hba *phba)
7441{
7442 int rc, i;
7443 LPFC_MBOXQ_t *pmb;
7444
7445 /* Set up MSI-X multi-message vectors */
7446 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
7447 phba->msix_entries[i].entry = i;
7448
7449 /* Configure MSI-X capability structure */
7450 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
7451 ARRAY_SIZE(phba->msix_entries));
7452 if (rc) {
7453 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7454 "0420 PCI enable MSI-X failed (%d)\n", rc);
7455 goto msi_fail_out;
7456 }
7457 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
7458 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7459 "0477 MSI-X entry[%d]: vector=x%x "
7460 "message=%d\n", i,
7461 phba->msix_entries[i].vector,
7462 phba->msix_entries[i].entry);
7463 /*
7464 * Assign MSI-X vectors to interrupt handlers
7465 */
7466
7467 /* vector-0 is associated to slow-path handler */
7468 rc = request_irq(phba->msix_entries[0].vector,
7469 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
7470 LPFC_SP_DRIVER_HANDLER_NAME, phba);
7471 if (rc) {
7472 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7473 "0421 MSI-X slow-path request_irq failed "
7474 "(%d)\n", rc);
7475 goto msi_fail_out;
7476 }
7477
7478 /* vector-1 is associated to fast-path handler */
7479 rc = request_irq(phba->msix_entries[1].vector,
7480 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
7481 LPFC_FP_DRIVER_HANDLER_NAME, phba);
7482
7483 if (rc) {
7484 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7485 "0429 MSI-X fast-path request_irq failed "
7486 "(%d)\n", rc);
7487 goto irq_fail_out;
7488 }
7489
7490 /*
7491 * Configure HBA MSI-X attention conditions to messages
7492 */
7493 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7494
7495 if (!pmb) {
7496 rc = -ENOMEM;
7497 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7498 "0474 Unable to allocate memory for issuing "
7499 "MBOX_CONFIG_MSI command\n");
7500 goto mem_fail_out;
7501 }
7502 rc = lpfc_config_msi(phba, pmb);
7503 if (rc)
7504 goto mbx_fail_out;
7505 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
7506 if (rc != MBX_SUCCESS) {
7507 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
7508 "0351 Config MSI mailbox command failed, "
7509 "mbxCmd x%x, mbxStatus x%x\n",
7510 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
7511 goto mbx_fail_out;
7512 }
7513
7514 /* Free memory allocated for mailbox command */
7515 mempool_free(pmb, phba->mbox_mem_pool);
7516 return rc;
7517
7518mbx_fail_out:
7519 /* Free memory allocated for mailbox command */
7520 mempool_free(pmb, phba->mbox_mem_pool);
7521
7522mem_fail_out:
7523 /* free the irq already requested */
7524 free_irq(phba->msix_entries[1].vector, phba);
7525
7526irq_fail_out:
7527 /* free the irq already requested */
7528 free_irq(phba->msix_entries[0].vector, phba);
7529
7530msi_fail_out:
7531 /* Unconfigure MSI-X capability structure */
7532 pci_disable_msix(phba->pcidev);
7533 return rc;
7534}
7535
7536/**
7537 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
7538 * @phba: pointer to lpfc hba data structure.
7539 *
7540 * This routine is invoked to release the MSI-X vectors and then disable the
7541 * MSI-X interrupt mode to device with SLI-3 interface spec.
7542 **/
7543static void
7544lpfc_sli_disable_msix(struct lpfc_hba *phba)
7545{
7546 int i;
7547
7548 /* Free up MSI-X multi-message vectors */
7549 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
7550 free_irq(phba->msix_entries[i].vector, phba);
7551 /* Disable MSI-X */
7552 pci_disable_msix(phba->pcidev);
7553
7554 return;
7555}
7556
7557/**
7558 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
7559 * @phba: pointer to lpfc hba data structure.
7560 *
7561 * This routine is invoked to enable the MSI interrupt mode to device with
7562 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
7563 * enable the MSI vector. The device driver is responsible for calling the
7564 * request_irq() to register MSI vector with a interrupt the handler, which
7565 * is done in this function.
7566 *
7567 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007568 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007569 * other values - error
7570 */
7571static int
7572lpfc_sli_enable_msi(struct lpfc_hba *phba)
7573{
7574 int rc;
7575
7576 rc = pci_enable_msi(phba->pcidev);
7577 if (!rc)
7578 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7579 "0462 PCI enable MSI mode success.\n");
7580 else {
7581 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7582 "0471 PCI enable MSI mode failed (%d)\n", rc);
7583 return rc;
7584 }
7585
7586 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
7587 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7588 if (rc) {
7589 pci_disable_msi(phba->pcidev);
7590 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7591 "0478 MSI request_irq failed (%d)\n", rc);
7592 }
7593 return rc;
7594}
7595
7596/**
7597 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
7598 * @phba: pointer to lpfc hba data structure.
7599 *
7600 * This routine is invoked to disable the MSI interrupt mode to device with
7601 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
7602 * done request_irq() on before calling pci_disable_msi(). Failure to do so
7603 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
7604 * its vector.
7605 */
7606static void
7607lpfc_sli_disable_msi(struct lpfc_hba *phba)
7608{
7609 free_irq(phba->pcidev->irq, phba);
7610 pci_disable_msi(phba->pcidev);
7611 return;
7612}
7613
7614/**
7615 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
7616 * @phba: pointer to lpfc hba data structure.
7617 *
7618 * This routine is invoked to enable device interrupt and associate driver's
7619 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
7620 * spec. Depends on the interrupt mode configured to the driver, the driver
7621 * will try to fallback from the configured interrupt mode to an interrupt
7622 * mode which is supported by the platform, kernel, and device in the order
7623 * of:
7624 * MSI-X -> MSI -> IRQ.
7625 *
7626 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007627 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04007628 * other values - error
7629 **/
7630static uint32_t
7631lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
7632{
7633 uint32_t intr_mode = LPFC_INTR_ERROR;
7634 int retval;
7635
7636 if (cfg_mode == 2) {
7637 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
7638 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
7639 if (!retval) {
7640 /* Now, try to enable MSI-X interrupt mode */
7641 retval = lpfc_sli_enable_msix(phba);
7642 if (!retval) {
7643 /* Indicate initialization to MSI-X mode */
7644 phba->intr_type = MSIX;
7645 intr_mode = 2;
7646 }
7647 }
7648 }
7649
7650 /* Fallback to MSI if MSI-X initialization failed */
7651 if (cfg_mode >= 1 && phba->intr_type == NONE) {
7652 retval = lpfc_sli_enable_msi(phba);
7653 if (!retval) {
7654 /* Indicate initialization to MSI mode */
7655 phba->intr_type = MSI;
7656 intr_mode = 1;
7657 }
7658 }
7659
7660 /* Fallback to INTx if both MSI-X/MSI initalization failed */
7661 if (phba->intr_type == NONE) {
7662 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
7663 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7664 if (!retval) {
7665 /* Indicate initialization to INTx mode */
7666 phba->intr_type = INTx;
7667 intr_mode = 0;
7668 }
7669 }
7670 return intr_mode;
7671}
7672
7673/**
7674 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
7675 * @phba: pointer to lpfc hba data structure.
7676 *
7677 * This routine is invoked to disable device interrupt and disassociate the
7678 * driver's interrupt handler(s) from interrupt vector(s) to device with
7679 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
7680 * release the interrupt vector(s) for the message signaled interrupt.
7681 **/
7682static void
7683lpfc_sli_disable_intr(struct lpfc_hba *phba)
7684{
7685 /* Disable the currently initialized interrupt mode */
7686 if (phba->intr_type == MSIX)
7687 lpfc_sli_disable_msix(phba);
7688 else if (phba->intr_type == MSI)
7689 lpfc_sli_disable_msi(phba);
7690 else if (phba->intr_type == INTx)
7691 free_irq(phba->pcidev->irq, phba);
7692
7693 /* Reset interrupt management states */
7694 phba->intr_type = NONE;
7695 phba->sli.slistat.sli_intr = 0;
7696
7697 return;
7698}
7699
7700/**
James Smartda0436e2009-05-22 14:51:39 -04007701 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
7702 * @phba: pointer to lpfc hba data structure.
7703 *
7704 * This routine is invoked to enable the MSI-X interrupt vectors to device
7705 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
7706 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
7707 * enables either all or nothing, depending on the current availability of
7708 * PCI vector resources. The device driver is responsible for calling the
7709 * individual request_irq() to register each MSI-X vector with a interrupt
7710 * handler, which is done in this function. Note that later when device is
7711 * unloading, the driver should always call free_irq() on all MSI-X vectors
7712 * it has done request_irq() on before calling pci_disable_msix(). Failure
7713 * to do so results in a BUG_ON() and a device will be left with MSI-X
7714 * enabled and leaks its vectors.
7715 *
7716 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007717 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007718 * other values - error
7719 **/
7720static int
7721lpfc_sli4_enable_msix(struct lpfc_hba *phba)
7722{
James Smart75baf692010-06-08 18:31:21 -04007723 int vectors, rc, index;
James Smartda0436e2009-05-22 14:51:39 -04007724
7725 /* Set up MSI-X multi-message vectors */
7726 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
7727 phba->sli4_hba.msix_entries[index].entry = index;
7728
7729 /* Configure MSI-X capability structure */
James Smart75baf692010-06-08 18:31:21 -04007730 vectors = phba->sli4_hba.cfg_eqn;
7731enable_msix_vectors:
James Smartda0436e2009-05-22 14:51:39 -04007732 rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
James Smart75baf692010-06-08 18:31:21 -04007733 vectors);
7734 if (rc > 1) {
7735 vectors = rc;
7736 goto enable_msix_vectors;
7737 } else if (rc) {
James Smartda0436e2009-05-22 14:51:39 -04007738 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7739 "0484 PCI enable MSI-X failed (%d)\n", rc);
7740 goto msi_fail_out;
7741 }
James Smart75baf692010-06-08 18:31:21 -04007742
James Smartda0436e2009-05-22 14:51:39 -04007743 /* Log MSI-X vector assignment */
James Smart75baf692010-06-08 18:31:21 -04007744 for (index = 0; index < vectors; index++)
James Smartda0436e2009-05-22 14:51:39 -04007745 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7746 "0489 MSI-X entry[%d]: vector=x%x "
7747 "message=%d\n", index,
7748 phba->sli4_hba.msix_entries[index].vector,
7749 phba->sli4_hba.msix_entries[index].entry);
7750 /*
7751 * Assign MSI-X vectors to interrupt handlers
7752 */
James Smart05580562011-05-24 11:40:48 -04007753 if (vectors > 1)
7754 rc = request_irq(phba->sli4_hba.msix_entries[0].vector,
7755 &lpfc_sli4_sp_intr_handler, IRQF_SHARED,
7756 LPFC_SP_DRIVER_HANDLER_NAME, phba);
7757 else
7758 /* All Interrupts need to be handled by one EQ */
7759 rc = request_irq(phba->sli4_hba.msix_entries[0].vector,
7760 &lpfc_sli4_intr_handler, IRQF_SHARED,
7761 LPFC_DRIVER_NAME, phba);
James Smartda0436e2009-05-22 14:51:39 -04007762 if (rc) {
7763 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7764 "0485 MSI-X slow-path request_irq failed "
7765 "(%d)\n", rc);
7766 goto msi_fail_out;
7767 }
7768
7769 /* The rest of the vector(s) are associated to fast-path handler(s) */
James Smart75baf692010-06-08 18:31:21 -04007770 for (index = 1; index < vectors; index++) {
James Smartda0436e2009-05-22 14:51:39 -04007771 phba->sli4_hba.fcp_eq_hdl[index - 1].idx = index - 1;
7772 phba->sli4_hba.fcp_eq_hdl[index - 1].phba = phba;
7773 rc = request_irq(phba->sli4_hba.msix_entries[index].vector,
7774 &lpfc_sli4_fp_intr_handler, IRQF_SHARED,
7775 LPFC_FP_DRIVER_HANDLER_NAME,
7776 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
7777 if (rc) {
7778 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7779 "0486 MSI-X fast-path (%d) "
7780 "request_irq failed (%d)\n", index, rc);
7781 goto cfg_fail_out;
7782 }
7783 }
James Smart75baf692010-06-08 18:31:21 -04007784 phba->sli4_hba.msix_vec_nr = vectors;
James Smartda0436e2009-05-22 14:51:39 -04007785
7786 return rc;
7787
7788cfg_fail_out:
7789 /* free the irq already requested */
7790 for (--index; index >= 1; index--)
7791 free_irq(phba->sli4_hba.msix_entries[index - 1].vector,
7792 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
7793
7794 /* free the irq already requested */
7795 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
7796
7797msi_fail_out:
7798 /* Unconfigure MSI-X capability structure */
7799 pci_disable_msix(phba->pcidev);
7800 return rc;
7801}
7802
7803/**
7804 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
7805 * @phba: pointer to lpfc hba data structure.
7806 *
7807 * This routine is invoked to release the MSI-X vectors and then disable the
7808 * MSI-X interrupt mode to device with SLI-4 interface spec.
7809 **/
7810static void
7811lpfc_sli4_disable_msix(struct lpfc_hba *phba)
7812{
7813 int index;
7814
7815 /* Free up MSI-X multi-message vectors */
7816 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
7817
James Smart75baf692010-06-08 18:31:21 -04007818 for (index = 1; index < phba->sli4_hba.msix_vec_nr; index++)
James Smartda0436e2009-05-22 14:51:39 -04007819 free_irq(phba->sli4_hba.msix_entries[index].vector,
7820 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
James Smart75baf692010-06-08 18:31:21 -04007821
James Smartda0436e2009-05-22 14:51:39 -04007822 /* Disable MSI-X */
7823 pci_disable_msix(phba->pcidev);
7824
7825 return;
7826}
7827
7828/**
7829 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
7830 * @phba: pointer to lpfc hba data structure.
7831 *
7832 * This routine is invoked to enable the MSI interrupt mode to device with
7833 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
7834 * to enable the MSI vector. The device driver is responsible for calling
7835 * the request_irq() to register MSI vector with a interrupt the handler,
7836 * which is done in this function.
7837 *
7838 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007839 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007840 * other values - error
7841 **/
7842static int
7843lpfc_sli4_enable_msi(struct lpfc_hba *phba)
7844{
7845 int rc, index;
7846
7847 rc = pci_enable_msi(phba->pcidev);
7848 if (!rc)
7849 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7850 "0487 PCI enable MSI mode success.\n");
7851 else {
7852 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7853 "0488 PCI enable MSI mode failed (%d)\n", rc);
7854 return rc;
7855 }
7856
7857 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
7858 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7859 if (rc) {
7860 pci_disable_msi(phba->pcidev);
7861 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7862 "0490 MSI request_irq failed (%d)\n", rc);
James Smart75baf692010-06-08 18:31:21 -04007863 return rc;
James Smartda0436e2009-05-22 14:51:39 -04007864 }
7865
7866 for (index = 0; index < phba->cfg_fcp_eq_count; index++) {
7867 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
7868 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
7869 }
7870
James Smart75baf692010-06-08 18:31:21 -04007871 return 0;
James Smartda0436e2009-05-22 14:51:39 -04007872}
7873
7874/**
7875 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
7876 * @phba: pointer to lpfc hba data structure.
7877 *
7878 * This routine is invoked to disable the MSI interrupt mode to device with
7879 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
7880 * done request_irq() on before calling pci_disable_msi(). Failure to do so
7881 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
7882 * its vector.
7883 **/
7884static void
7885lpfc_sli4_disable_msi(struct lpfc_hba *phba)
7886{
7887 free_irq(phba->pcidev->irq, phba);
7888 pci_disable_msi(phba->pcidev);
7889 return;
7890}
7891
7892/**
7893 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
7894 * @phba: pointer to lpfc hba data structure.
7895 *
7896 * This routine is invoked to enable device interrupt and associate driver's
7897 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
7898 * interface spec. Depends on the interrupt mode configured to the driver,
7899 * the driver will try to fallback from the configured interrupt mode to an
7900 * interrupt mode which is supported by the platform, kernel, and device in
7901 * the order of:
7902 * MSI-X -> MSI -> IRQ.
7903 *
7904 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007905 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007906 * other values - error
7907 **/
7908static uint32_t
7909lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
7910{
7911 uint32_t intr_mode = LPFC_INTR_ERROR;
7912 int retval, index;
7913
7914 if (cfg_mode == 2) {
7915 /* Preparation before conf_msi mbox cmd */
7916 retval = 0;
7917 if (!retval) {
7918 /* Now, try to enable MSI-X interrupt mode */
7919 retval = lpfc_sli4_enable_msix(phba);
7920 if (!retval) {
7921 /* Indicate initialization to MSI-X mode */
7922 phba->intr_type = MSIX;
7923 intr_mode = 2;
7924 }
7925 }
7926 }
7927
7928 /* Fallback to MSI if MSI-X initialization failed */
7929 if (cfg_mode >= 1 && phba->intr_type == NONE) {
7930 retval = lpfc_sli4_enable_msi(phba);
7931 if (!retval) {
7932 /* Indicate initialization to MSI mode */
7933 phba->intr_type = MSI;
7934 intr_mode = 1;
7935 }
7936 }
7937
7938 /* Fallback to INTx if both MSI-X/MSI initalization failed */
7939 if (phba->intr_type == NONE) {
7940 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
7941 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7942 if (!retval) {
7943 /* Indicate initialization to INTx mode */
7944 phba->intr_type = INTx;
7945 intr_mode = 0;
7946 for (index = 0; index < phba->cfg_fcp_eq_count;
7947 index++) {
7948 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
7949 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
7950 }
7951 }
7952 }
7953 return intr_mode;
7954}
7955
7956/**
7957 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
7958 * @phba: pointer to lpfc hba data structure.
7959 *
7960 * This routine is invoked to disable device interrupt and disassociate
7961 * the driver's interrupt handler(s) from interrupt vector(s) to device
7962 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
7963 * will release the interrupt vector(s) for the message signaled interrupt.
7964 **/
7965static void
7966lpfc_sli4_disable_intr(struct lpfc_hba *phba)
7967{
7968 /* Disable the currently initialized interrupt mode */
7969 if (phba->intr_type == MSIX)
7970 lpfc_sli4_disable_msix(phba);
7971 else if (phba->intr_type == MSI)
7972 lpfc_sli4_disable_msi(phba);
7973 else if (phba->intr_type == INTx)
7974 free_irq(phba->pcidev->irq, phba);
7975
7976 /* Reset interrupt management states */
7977 phba->intr_type = NONE;
7978 phba->sli.slistat.sli_intr = 0;
7979
7980 return;
7981}
7982
7983/**
James Smart3772a992009-05-22 14:50:54 -04007984 * lpfc_unset_hba - Unset SLI3 hba device initialization
7985 * @phba: pointer to lpfc hba data structure.
7986 *
7987 * This routine is invoked to unset the HBA device initialization steps to
7988 * a device with SLI-3 interface spec.
7989 **/
7990static void
7991lpfc_unset_hba(struct lpfc_hba *phba)
7992{
7993 struct lpfc_vport *vport = phba->pport;
7994 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7995
7996 spin_lock_irq(shost->host_lock);
7997 vport->load_flag |= FC_UNLOADING;
7998 spin_unlock_irq(shost->host_lock);
7999
8000 lpfc_stop_hba_timers(phba);
8001
8002 phba->pport->work_port_events = 0;
8003
8004 lpfc_sli_hba_down(phba);
8005
8006 lpfc_sli_brdrestart(phba);
8007
8008 lpfc_sli_disable_intr(phba);
8009
8010 return;
8011}
8012
8013/**
James Smartda0436e2009-05-22 14:51:39 -04008014 * lpfc_sli4_unset_hba - Unset SLI4 hba device initialization.
8015 * @phba: pointer to lpfc hba data structure.
8016 *
8017 * This routine is invoked to unset the HBA device initialization steps to
8018 * a device with SLI-4 interface spec.
8019 **/
8020static void
8021lpfc_sli4_unset_hba(struct lpfc_hba *phba)
8022{
8023 struct lpfc_vport *vport = phba->pport;
8024 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
8025
8026 spin_lock_irq(shost->host_lock);
8027 vport->load_flag |= FC_UNLOADING;
8028 spin_unlock_irq(shost->host_lock);
8029
8030 phba->pport->work_port_events = 0;
8031
James Smart3677a3a2010-09-29 11:19:14 -04008032 /* Stop the SLI4 device port */
8033 lpfc_stop_port(phba);
James Smartda0436e2009-05-22 14:51:39 -04008034
8035 lpfc_sli4_disable_intr(phba);
8036
James Smart3677a3a2010-09-29 11:19:14 -04008037 /* Reset SLI4 HBA FCoE function */
8038 lpfc_pci_function_reset(phba);
James Smart5350d872011-10-10 21:33:49 -04008039 lpfc_sli4_queue_destroy(phba);
James Smart3677a3a2010-09-29 11:19:14 -04008040
James Smartda0436e2009-05-22 14:51:39 -04008041 return;
8042}
8043
8044/**
James Smart5af5eee2010-10-22 11:06:38 -04008045 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
8046 * @phba: Pointer to HBA context object.
8047 *
8048 * This function is called in the SLI4 code path to wait for completion
8049 * of device's XRIs exchange busy. It will check the XRI exchange busy
8050 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
8051 * that, it will check the XRI exchange busy on outstanding FCP and ELS
8052 * I/Os every 30 seconds, log error message, and wait forever. Only when
8053 * all XRI exchange busy complete, the driver unload shall proceed with
8054 * invoking the function reset ioctl mailbox command to the CNA and the
8055 * the rest of the driver unload resource release.
8056 **/
8057static void
8058lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
8059{
8060 int wait_time = 0;
8061 int fcp_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
8062 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
8063
8064 while (!fcp_xri_cmpl || !els_xri_cmpl) {
8065 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
8066 if (!fcp_xri_cmpl)
8067 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8068 "2877 FCP XRI exchange busy "
8069 "wait time: %d seconds.\n",
8070 wait_time/1000);
8071 if (!els_xri_cmpl)
8072 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8073 "2878 ELS XRI exchange busy "
8074 "wait time: %d seconds.\n",
8075 wait_time/1000);
8076 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
8077 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
8078 } else {
8079 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
8080 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
8081 }
8082 fcp_xri_cmpl =
8083 list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
8084 els_xri_cmpl =
8085 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
8086 }
8087}
8088
8089/**
James Smartda0436e2009-05-22 14:51:39 -04008090 * lpfc_sli4_hba_unset - Unset the fcoe hba
8091 * @phba: Pointer to HBA context object.
8092 *
8093 * This function is called in the SLI4 code path to reset the HBA's FCoE
8094 * function. The caller is not required to hold any lock. This routine
8095 * issues PCI function reset mailbox command to reset the FCoE function.
8096 * At the end of the function, it calls lpfc_hba_down_post function to
8097 * free any pending commands.
8098 **/
8099static void
8100lpfc_sli4_hba_unset(struct lpfc_hba *phba)
8101{
8102 int wait_cnt = 0;
8103 LPFC_MBOXQ_t *mboxq;
James Smart912e3ac2011-05-24 11:42:11 -04008104 struct pci_dev *pdev = phba->pcidev;
James Smartda0436e2009-05-22 14:51:39 -04008105
8106 lpfc_stop_hba_timers(phba);
8107 phba->sli4_hba.intr_enable = 0;
8108
8109 /*
8110 * Gracefully wait out the potential current outstanding asynchronous
8111 * mailbox command.
8112 */
8113
8114 /* First, block any pending async mailbox command from posted */
8115 spin_lock_irq(&phba->hbalock);
8116 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
8117 spin_unlock_irq(&phba->hbalock);
8118 /* Now, trying to wait it out if we can */
8119 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8120 msleep(10);
8121 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
8122 break;
8123 }
8124 /* Forcefully release the outstanding mailbox command if timed out */
8125 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8126 spin_lock_irq(&phba->hbalock);
8127 mboxq = phba->sli.mbox_active;
8128 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8129 __lpfc_mbox_cmpl_put(phba, mboxq);
8130 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8131 phba->sli.mbox_active = NULL;
8132 spin_unlock_irq(&phba->hbalock);
8133 }
8134
James Smart5af5eee2010-10-22 11:06:38 -04008135 /* Abort all iocbs associated with the hba */
8136 lpfc_sli_hba_iocb_abort(phba);
8137
8138 /* Wait for completion of device XRI exchange busy */
8139 lpfc_sli4_xri_exchange_busy_wait(phba);
8140
James Smartda0436e2009-05-22 14:51:39 -04008141 /* Disable PCI subsystem interrupt */
8142 lpfc_sli4_disable_intr(phba);
8143
James Smart912e3ac2011-05-24 11:42:11 -04008144 /* Disable SR-IOV if enabled */
8145 if (phba->cfg_sriov_nr_virtfn)
8146 pci_disable_sriov(pdev);
8147
James Smartda0436e2009-05-22 14:51:39 -04008148 /* Stop kthread signal shall trigger work_done one more time */
8149 kthread_stop(phba->worker_thread);
8150
James Smart3677a3a2010-09-29 11:19:14 -04008151 /* Reset SLI4 HBA FCoE function */
8152 lpfc_pci_function_reset(phba);
James Smart5350d872011-10-10 21:33:49 -04008153 lpfc_sli4_queue_destroy(phba);
James Smart3677a3a2010-09-29 11:19:14 -04008154
James Smartda0436e2009-05-22 14:51:39 -04008155 /* Stop the SLI4 device port */
8156 phba->pport->work_port_events = 0;
8157}
8158
James Smart28baac72010-02-12 14:42:03 -05008159 /**
8160 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
8161 * @phba: Pointer to HBA context object.
8162 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
8163 *
8164 * This function is called in the SLI4 code path to read the port's
8165 * sli4 capabilities.
8166 *
8167 * This function may be be called from any context that can block-wait
8168 * for the completion. The expectation is that this routine is called
8169 * typically from probe_one or from the online routine.
8170 **/
8171int
8172lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8173{
8174 int rc;
8175 struct lpfc_mqe *mqe;
8176 struct lpfc_pc_sli4_params *sli4_params;
8177 uint32_t mbox_tmo;
8178
8179 rc = 0;
8180 mqe = &mboxq->u.mqe;
8181
8182 /* Read the port's SLI4 Parameters port capabilities */
James Smartfedd3b72011-02-16 12:39:24 -05008183 lpfc_pc_sli4_params(mboxq);
James Smart28baac72010-02-12 14:42:03 -05008184 if (!phba->sli4_hba.intr_enable)
8185 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8186 else {
James Smarta183a152011-10-10 21:32:43 -04008187 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart28baac72010-02-12 14:42:03 -05008188 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
8189 }
8190
8191 if (unlikely(rc))
8192 return 1;
8193
8194 sli4_params = &phba->sli4_hba.pc_sli4_params;
8195 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
8196 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
8197 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
8198 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
8199 &mqe->un.sli4_params);
8200 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
8201 &mqe->un.sli4_params);
8202 sli4_params->proto_types = mqe->un.sli4_params.word3;
8203 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
8204 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
8205 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
8206 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
8207 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
8208 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
8209 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
8210 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
8211 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
8212 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
8213 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
8214 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
8215 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
8216 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
8217 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
8218 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
8219 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
8220 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
8221 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
8222 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
James Smart05580562011-05-24 11:40:48 -04008223
8224 /* Make sure that sge_supp_len can be handled by the driver */
8225 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
8226 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
8227
James Smart28baac72010-02-12 14:42:03 -05008228 return rc;
8229}
8230
James Smartda0436e2009-05-22 14:51:39 -04008231/**
James Smartfedd3b72011-02-16 12:39:24 -05008232 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
8233 * @phba: Pointer to HBA context object.
8234 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
8235 *
8236 * This function is called in the SLI4 code path to read the port's
8237 * sli4 capabilities.
8238 *
8239 * This function may be be called from any context that can block-wait
8240 * for the completion. The expectation is that this routine is called
8241 * typically from probe_one or from the online routine.
8242 **/
8243int
8244lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8245{
8246 int rc;
8247 struct lpfc_mqe *mqe = &mboxq->u.mqe;
8248 struct lpfc_pc_sli4_params *sli4_params;
James Smarta183a152011-10-10 21:32:43 -04008249 uint32_t mbox_tmo;
James Smartfedd3b72011-02-16 12:39:24 -05008250 int length;
8251 struct lpfc_sli4_parameters *mbx_sli4_parameters;
8252
James Smart6d368e52011-05-24 11:44:12 -04008253 /*
8254 * By default, the driver assumes the SLI4 port requires RPI
8255 * header postings. The SLI4_PARAM response will correct this
8256 * assumption.
8257 */
8258 phba->sli4_hba.rpi_hdrs_in_use = 1;
8259
James Smartfedd3b72011-02-16 12:39:24 -05008260 /* Read the port's SLI4 Config Parameters */
8261 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
8262 sizeof(struct lpfc_sli4_cfg_mhdr));
8263 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
8264 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
8265 length, LPFC_SLI4_MBX_EMBED);
8266 if (!phba->sli4_hba.intr_enable)
8267 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smarta183a152011-10-10 21:32:43 -04008268 else {
8269 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
8270 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
8271 }
James Smartfedd3b72011-02-16 12:39:24 -05008272 if (unlikely(rc))
8273 return rc;
8274 sli4_params = &phba->sli4_hba.pc_sli4_params;
8275 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
8276 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
8277 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
8278 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
8279 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
8280 mbx_sli4_parameters);
8281 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
8282 mbx_sli4_parameters);
8283 if (bf_get(cfg_phwq, mbx_sli4_parameters))
8284 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
8285 else
8286 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
8287 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
8288 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
8289 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
8290 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
8291 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
8292 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
8293 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
8294 mbx_sli4_parameters);
8295 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
8296 mbx_sli4_parameters);
James Smart6d368e52011-05-24 11:44:12 -04008297 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
8298 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
James Smart05580562011-05-24 11:40:48 -04008299
8300 /* Make sure that sge_supp_len can be handled by the driver */
8301 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
8302 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
8303
James Smartfedd3b72011-02-16 12:39:24 -05008304 return 0;
8305}
8306
8307/**
James Smart3772a992009-05-22 14:50:54 -04008308 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
8309 * @pdev: pointer to PCI device
8310 * @pid: pointer to PCI device identifier
8311 *
8312 * This routine is to be called to attach a device with SLI-3 interface spec
8313 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
8314 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
8315 * information of the device and driver to see if the driver state that it can
8316 * support this kind of device. If the match is successful, the driver core
8317 * invokes this routine. If this routine determines it can claim the HBA, it
8318 * does all the initialization that it needs to do to handle the HBA properly.
8319 *
8320 * Return code
8321 * 0 - driver can claim the device
8322 * negative value - driver can not claim the device
8323 **/
8324static int __devinit
8325lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
8326{
8327 struct lpfc_hba *phba;
8328 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04008329 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -04008330 int error;
8331 uint32_t cfg_mode, intr_mode;
8332
8333 /* Allocate memory for HBA structure */
8334 phba = lpfc_hba_alloc(pdev);
8335 if (!phba)
8336 return -ENOMEM;
8337
8338 /* Perform generic PCI device enabling operation */
8339 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -04008340 if (error)
James Smart3772a992009-05-22 14:50:54 -04008341 goto out_free_phba;
James Smart3772a992009-05-22 14:50:54 -04008342
8343 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
8344 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
8345 if (error)
8346 goto out_disable_pci_dev;
8347
8348 /* Set up SLI-3 specific device PCI memory space */
8349 error = lpfc_sli_pci_mem_setup(phba);
8350 if (error) {
8351 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8352 "1402 Failed to set up pci memory space.\n");
8353 goto out_disable_pci_dev;
8354 }
8355
8356 /* Set up phase-1 common device driver resources */
8357 error = lpfc_setup_driver_resource_phase1(phba);
8358 if (error) {
8359 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8360 "1403 Failed to set up driver resource.\n");
8361 goto out_unset_pci_mem_s3;
8362 }
8363
8364 /* Set up SLI-3 specific device driver resources */
8365 error = lpfc_sli_driver_resource_setup(phba);
8366 if (error) {
8367 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8368 "1404 Failed to set up driver resource.\n");
8369 goto out_unset_pci_mem_s3;
8370 }
8371
8372 /* Initialize and populate the iocb list per host */
8373 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
8374 if (error) {
8375 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8376 "1405 Failed to initialize iocb list.\n");
8377 goto out_unset_driver_resource_s3;
8378 }
8379
8380 /* Set up common device driver resources */
8381 error = lpfc_setup_driver_resource_phase2(phba);
8382 if (error) {
8383 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8384 "1406 Failed to set up driver resource.\n");
8385 goto out_free_iocb_list;
8386 }
8387
James Smart079b5c92011-08-21 21:48:49 -04008388 /* Get the default values for Model Name and Description */
8389 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
8390
James Smart3772a992009-05-22 14:50:54 -04008391 /* Create SCSI host to the physical port */
8392 error = lpfc_create_shost(phba);
8393 if (error) {
8394 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8395 "1407 Failed to create scsi host.\n");
8396 goto out_unset_driver_resource;
8397 }
8398
8399 /* Configure sysfs attributes */
8400 vport = phba->pport;
8401 error = lpfc_alloc_sysfs_attr(vport);
8402 if (error) {
8403 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8404 "1476 Failed to allocate sysfs attr\n");
8405 goto out_destroy_shost;
8406 }
8407
James Smart6669f9b2009-10-02 15:16:45 -04008408 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -04008409 /* Now, trying to enable interrupt and bring up the device */
8410 cfg_mode = phba->cfg_use_msi;
8411 while (true) {
8412 /* Put device to a known state before enabling interrupt */
8413 lpfc_stop_port(phba);
8414 /* Configure and enable interrupt */
8415 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
8416 if (intr_mode == LPFC_INTR_ERROR) {
8417 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8418 "0431 Failed to enable interrupt.\n");
8419 error = -ENODEV;
8420 goto out_free_sysfs_attr;
8421 }
8422 /* SLI-3 HBA setup */
8423 if (lpfc_sli_hba_setup(phba)) {
8424 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8425 "1477 Failed to set up hba\n");
8426 error = -ENODEV;
8427 goto out_remove_device;
8428 }
8429
8430 /* Wait 50ms for the interrupts of previous mailbox commands */
8431 msleep(50);
8432 /* Check active interrupts on message signaled interrupts */
8433 if (intr_mode == 0 ||
8434 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
8435 /* Log the current active interrupt mode */
8436 phba->intr_mode = intr_mode;
8437 lpfc_log_intr_mode(phba, intr_mode);
8438 break;
8439 } else {
8440 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8441 "0447 Configure interrupt mode (%d) "
8442 "failed active interrupt test.\n",
8443 intr_mode);
8444 /* Disable the current interrupt mode */
8445 lpfc_sli_disable_intr(phba);
8446 /* Try next level of interrupt mode */
8447 cfg_mode = --intr_mode;
8448 }
8449 }
8450
8451 /* Perform post initialization setup */
8452 lpfc_post_init_setup(phba);
8453
8454 /* Check if there are static vports to be created. */
8455 lpfc_create_static_vport(phba);
8456
8457 return 0;
8458
8459out_remove_device:
8460 lpfc_unset_hba(phba);
8461out_free_sysfs_attr:
8462 lpfc_free_sysfs_attr(vport);
8463out_destroy_shost:
8464 lpfc_destroy_shost(phba);
8465out_unset_driver_resource:
8466 lpfc_unset_driver_resource_phase2(phba);
8467out_free_iocb_list:
8468 lpfc_free_iocb_list(phba);
8469out_unset_driver_resource_s3:
8470 lpfc_sli_driver_resource_unset(phba);
8471out_unset_pci_mem_s3:
8472 lpfc_sli_pci_mem_unset(phba);
8473out_disable_pci_dev:
8474 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04008475 if (shost)
8476 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -04008477out_free_phba:
8478 lpfc_hba_free(phba);
8479 return error;
8480}
8481
8482/**
8483 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -04008484 * @pdev: pointer to PCI device
8485 *
James Smart3772a992009-05-22 14:50:54 -04008486 * This routine is to be called to disattach a device with SLI-3 interface
8487 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
8488 * removed from PCI bus, it performs all the necessary cleanup for the HBA
8489 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -04008490 **/
dea31012005-04-17 16:05:31 -05008491static void __devexit
James Smart3772a992009-05-22 14:50:54 -04008492lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -05008493{
James Smart2e0fef82007-06-17 19:56:36 -05008494 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8495 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -05008496 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -05008497 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -05008498 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05008499 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
8500
James Smart549e55c2007-08-02 11:09:51 -04008501 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04008502 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04008503 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008504
James Smart858c9f62007-06-17 19:56:39 -05008505 lpfc_free_sysfs_attr(vport);
8506
James Smarteada2722008-12-04 22:39:13 -05008507 /* Release all the vports against this physical port */
8508 vports = lpfc_create_vport_work_array(phba);
8509 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -04008510 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
James Smarteada2722008-12-04 22:39:13 -05008511 fc_vport_terminate(vports[i]->fc_vport);
8512 lpfc_destroy_vport_work_array(phba, vports);
8513
8514 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -05008515 fc_remove_host(shost);
8516 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04008517 lpfc_cleanup(vport);
8518
James Smart2e0fef82007-06-17 19:56:36 -05008519 /*
8520 * Bring down the SLI Layer. This step disable all interrupts,
8521 * clears the rings, discards all mailbox commands, and resets
8522 * the HBA.
8523 */
James Smarta257bf92009-04-06 18:48:10 -04008524
Justin P. Mattock48e34d02010-12-30 15:07:58 -08008525 /* HBA interrupt will be disabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -05008526 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -04008527 /* Stop kthread signal shall trigger work_done one more time */
8528 kthread_stop(phba->worker_thread);
8529 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -05008530 lpfc_sli_brdrestart(phba);
8531
James Smart3772a992009-05-22 14:50:54 -04008532 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05008533 spin_lock_irq(&phba->hbalock);
8534 list_del_init(&vport->listentry);
8535 spin_unlock_irq(&phba->hbalock);
8536
James Smart858c9f62007-06-17 19:56:39 -05008537 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05008538
James Smart912e3ac2011-05-24 11:42:11 -04008539 /* Disable SR-IOV if enabled */
8540 if (phba->cfg_sriov_nr_virtfn)
8541 pci_disable_sriov(pdev);
8542
James Smart5b75da22008-12-04 22:39:35 -05008543 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04008544 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -05008545
8546 pci_set_drvdata(pdev, NULL);
James Smart858c9f62007-06-17 19:56:39 -05008547 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05008548
8549 /*
8550 * Call scsi_free before mem_free since scsi bufs are released to their
8551 * corresponding pools here.
8552 */
8553 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -04008554 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -05008555
James Smart34b02dc2008-08-24 21:49:55 -04008556 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
8557 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -05008558
James Smart2e0fef82007-06-17 19:56:36 -05008559 /* Free resources associated with SLI2 interface */
8560 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -04008561 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -05008562
8563 /* unmap adapter SLIM and Control Registers */
8564 iounmap(phba->ctrl_regs_memmap_p);
8565 iounmap(phba->slim_memmap_p);
8566
James Smart3772a992009-05-22 14:50:54 -04008567 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -05008568
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05008569 pci_release_selected_regions(pdev, bars);
James Smart2e0fef82007-06-17 19:56:36 -05008570 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05008571}
8572
Linas Vepstas8d63f372007-02-14 14:28:36 -06008573/**
James Smart3772a992009-05-22 14:50:54 -04008574 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05008575 * @pdev: pointer to PCI device
8576 * @msg: power management message
8577 *
James Smart3772a992009-05-22 14:50:54 -04008578 * This routine is to be called from the kernel's PCI subsystem to support
8579 * system Power Management (PM) to device with SLI-3 interface spec. When
8580 * PM invokes this method, it quiesces the device by stopping the driver's
8581 * worker thread for the device, turning off device's interrupt and DMA,
8582 * and bring the device offline. Note that as the driver implements the
8583 * minimum PM requirements to a power-aware driver's PM support for the
8584 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
8585 * to the suspend() method call will be treated as SUSPEND and the driver will
8586 * fully reinitialize its device during resume() method call, the driver will
8587 * set device to PCI_D3hot state in PCI config space instead of setting it
8588 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -05008589 *
8590 * Return code
James Smart3772a992009-05-22 14:50:54 -04008591 * 0 - driver suspended the device
8592 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05008593 **/
8594static int
James Smart3772a992009-05-22 14:50:54 -04008595lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -05008596{
8597 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8598 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8599
8600 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8601 "0473 PCI device Power Management suspend.\n");
8602
8603 /* Bring down the device */
8604 lpfc_offline_prep(phba);
8605 lpfc_offline(phba);
8606 kthread_stop(phba->worker_thread);
8607
8608 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -04008609 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -05008610
8611 /* Save device state to PCI config space */
8612 pci_save_state(pdev);
8613 pci_set_power_state(pdev, PCI_D3hot);
8614
8615 return 0;
8616}
8617
8618/**
James Smart3772a992009-05-22 14:50:54 -04008619 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05008620 * @pdev: pointer to PCI device
8621 *
James Smart3772a992009-05-22 14:50:54 -04008622 * This routine is to be called from the kernel's PCI subsystem to support
8623 * system Power Management (PM) to device with SLI-3 interface spec. When PM
8624 * invokes this method, it restores the device's PCI config space state and
8625 * fully reinitializes the device and brings it online. Note that as the
8626 * driver implements the minimum PM requirements to a power-aware driver's
8627 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
8628 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
8629 * driver will fully reinitialize its device during resume() method call,
8630 * the device will be set to PCI_D0 directly in PCI config space before
8631 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -05008632 *
8633 * Return code
James Smart3772a992009-05-22 14:50:54 -04008634 * 0 - driver suspended the device
8635 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05008636 **/
8637static int
James Smart3772a992009-05-22 14:50:54 -04008638lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -05008639{
8640 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8641 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -05008642 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05008643 int error;
8644
8645 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8646 "0452 PCI device Power Management resume.\n");
8647
8648 /* Restore device state from PCI config space */
8649 pci_set_power_state(pdev, PCI_D0);
8650 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -04008651
James Smart1dfb5a42010-02-12 14:40:50 -05008652 /*
8653 * As the new kernel behavior of pci_restore_state() API call clears
8654 * device saved_state flag, need to save the restored state again.
8655 */
8656 pci_save_state(pdev);
8657
James Smart3a55b532008-12-04 22:38:54 -05008658 if (pdev->is_busmaster)
8659 pci_set_master(pdev);
8660
8661 /* Startup the kernel thread for this host adapter. */
8662 phba->worker_thread = kthread_run(lpfc_do_work, phba,
8663 "lpfc_worker_%d", phba->brd_no);
8664 if (IS_ERR(phba->worker_thread)) {
8665 error = PTR_ERR(phba->worker_thread);
8666 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8667 "0434 PM resume failed to start worker "
8668 "thread: error=x%x.\n", error);
8669 return error;
8670 }
8671
James Smart5b75da22008-12-04 22:39:35 -05008672 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04008673 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05008674 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -05008675 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -05008676 "0430 PM resume Failed to enable interrupt\n");
8677 return -EIO;
8678 } else
8679 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05008680
8681 /* Restart HBA and bring it online */
8682 lpfc_sli_brdrestart(phba);
8683 lpfc_online(phba);
8684
James Smart5b75da22008-12-04 22:39:35 -05008685 /* Log the current active interrupt mode */
8686 lpfc_log_intr_mode(phba, phba->intr_mode);
8687
James Smart3a55b532008-12-04 22:38:54 -05008688 return 0;
8689}
8690
8691/**
James Smart891478a2009-11-18 15:40:23 -05008692 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
8693 * @phba: pointer to lpfc hba data structure.
8694 *
8695 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -04008696 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -05008697 **/
8698static void
8699lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
8700{
James Smarte2af0d22010-03-15 11:25:32 -04008701 struct lpfc_sli *psli = &phba->sli;
8702 struct lpfc_sli_ring *pring;
8703
James Smart891478a2009-11-18 15:40:23 -05008704 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8705 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -04008706
8707 /*
8708 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
8709 * and let the SCSI mid-layer to retry them to recover.
8710 */
8711 pring = &psli->ring[psli->fcp_ring];
8712 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart891478a2009-11-18 15:40:23 -05008713}
8714
8715/**
James Smart0d878412009-10-02 15:16:56 -04008716 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
8717 * @phba: pointer to lpfc hba data structure.
8718 *
8719 * This routine is called to prepare the SLI3 device for PCI slot reset. It
8720 * disables the device interrupt and pci device, and aborts the internal FCP
8721 * pending I/Os.
8722 **/
8723static void
8724lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
8725{
James Smart0d878412009-10-02 15:16:56 -04008726 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05008727 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -04008728
James Smart75baf692010-06-08 18:31:21 -04008729 /* Block any management I/Os to the device */
8730 lpfc_block_mgmt_io(phba);
8731
James Smarte2af0d22010-03-15 11:25:32 -04008732 /* Block all SCSI devices' I/Os on the host */
8733 lpfc_scsi_dev_block(phba);
8734
8735 /* stop all timers */
8736 lpfc_stop_hba_timers(phba);
8737
James Smart0d878412009-10-02 15:16:56 -04008738 /* Disable interrupt and pci device */
8739 lpfc_sli_disable_intr(phba);
8740 pci_disable_device(phba->pcidev);
James Smart75baf692010-06-08 18:31:21 -04008741
James Smarte2af0d22010-03-15 11:25:32 -04008742 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
8743 lpfc_sli_flush_fcp_rings(phba);
James Smart0d878412009-10-02 15:16:56 -04008744}
8745
8746/**
8747 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
8748 * @phba: pointer to lpfc hba data structure.
8749 *
8750 * This routine is called to prepare the SLI3 device for PCI slot permanently
8751 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
8752 * pending I/Os.
8753 **/
8754static void
James Smart75baf692010-06-08 18:31:21 -04008755lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
James Smart0d878412009-10-02 15:16:56 -04008756{
8757 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05008758 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -04008759 /* Block all SCSI devices' I/Os on the host */
8760 lpfc_scsi_dev_block(phba);
8761
8762 /* stop all timers */
8763 lpfc_stop_hba_timers(phba);
8764
James Smart0d878412009-10-02 15:16:56 -04008765 /* Clean up all driver's outstanding SCSI I/Os */
8766 lpfc_sli_flush_fcp_rings(phba);
8767}
8768
8769/**
James Smart3772a992009-05-22 14:50:54 -04008770 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -04008771 * @pdev: pointer to PCI device.
8772 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -06008773 *
James Smart3772a992009-05-22 14:50:54 -04008774 * This routine is called from the PCI subsystem for I/O error handling to
8775 * device with SLI-3 interface spec. This function is called by the PCI
8776 * subsystem after a PCI bus error affecting this device has been detected.
8777 * When this function is invoked, it will need to stop all the I/Os and
8778 * interrupt(s) to the device. Once that is done, it will return
8779 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
8780 * as desired.
James Smarte59058c2008-08-24 21:49:00 -04008781 *
8782 * Return codes
James Smart0d878412009-10-02 15:16:56 -04008783 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -04008784 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
8785 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -04008786 **/
James Smart3772a992009-05-22 14:50:54 -04008787static pci_ers_result_t
8788lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -06008789{
James Smart51ef4c22007-08-02 11:10:31 -04008790 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8791 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06008792
James Smart0d878412009-10-02 15:16:56 -04008793 switch (state) {
8794 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -05008795 /* Non-fatal error, prepare for recovery */
8796 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -04008797 return PCI_ERS_RESULT_CAN_RECOVER;
8798 case pci_channel_io_frozen:
8799 /* Fatal error, prepare for slot reset */
8800 lpfc_sli_prep_dev_for_reset(phba);
8801 return PCI_ERS_RESULT_NEED_RESET;
8802 case pci_channel_io_perm_failure:
8803 /* Permanent failure, prepare for device down */
James Smart75baf692010-06-08 18:31:21 -04008804 lpfc_sli_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06008805 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -04008806 default:
8807 /* Unknown state, prepare and request slot reset */
8808 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8809 "0472 Unknown PCI error state: x%x\n", state);
8810 lpfc_sli_prep_dev_for_reset(phba);
8811 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -04008812 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06008813}
8814
8815/**
James Smart3772a992009-05-22 14:50:54 -04008816 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -04008817 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -06008818 *
James Smart3772a992009-05-22 14:50:54 -04008819 * This routine is called from the PCI subsystem for error handling to
8820 * device with SLI-3 interface spec. This is called after PCI bus has been
8821 * reset to restart the PCI card from scratch, as if from a cold-boot.
8822 * During the PCI subsystem error recovery, after driver returns
8823 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
8824 * recovery and then call this routine before calling the .resume method
8825 * to recover the device. This function will initialize the HBA device,
8826 * enable the interrupt, but it will just put the HBA to offline state
8827 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -04008828 *
8829 * Return codes
James Smart3772a992009-05-22 14:50:54 -04008830 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
8831 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -06008832 */
James Smart3772a992009-05-22 14:50:54 -04008833static pci_ers_result_t
8834lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06008835{
James Smart51ef4c22007-08-02 11:10:31 -04008836 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8837 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06008838 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -05008839 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06008840
8841 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +11008842 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -06008843 printk(KERN_ERR "lpfc: Cannot re-enable "
8844 "PCI device after reset.\n");
8845 return PCI_ERS_RESULT_DISCONNECT;
8846 }
8847
James Smart97207482008-12-04 22:39:19 -05008848 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05008849
8850 /*
8851 * As the new kernel behavior of pci_restore_state() API call clears
8852 * device saved_state flag, need to save the restored state again.
8853 */
8854 pci_save_state(pdev);
8855
James Smart97207482008-12-04 22:39:19 -05008856 if (pdev->is_busmaster)
8857 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06008858
James Smart92d7f7b2007-06-17 19:56:38 -05008859 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04008860 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05008861 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06008862
James Smart5b75da22008-12-04 22:39:35 -05008863 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04008864 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05008865 if (intr_mode == LPFC_INTR_ERROR) {
8866 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8867 "0427 Cannot re-enable interrupt after "
8868 "slot reset.\n");
8869 return PCI_ERS_RESULT_DISCONNECT;
8870 } else
8871 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06008872
James Smart75baf692010-06-08 18:31:21 -04008873 /* Take device offline, it will perform cleanup */
8874 lpfc_offline_prep(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06008875 lpfc_offline(phba);
8876 lpfc_sli_brdrestart(phba);
8877
James Smart5b75da22008-12-04 22:39:35 -05008878 /* Log the current active interrupt mode */
8879 lpfc_log_intr_mode(phba, phba->intr_mode);
8880
Linas Vepstas8d63f372007-02-14 14:28:36 -06008881 return PCI_ERS_RESULT_RECOVERED;
8882}
8883
8884/**
James Smart3772a992009-05-22 14:50:54 -04008885 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -04008886 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -06008887 *
James Smart3772a992009-05-22 14:50:54 -04008888 * This routine is called from the PCI subsystem for error handling to device
8889 * with SLI-3 interface spec. It is called when kernel error recovery tells
8890 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
8891 * error recovery. After this call, traffic can start to flow from this device
8892 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -06008893 */
James Smart3772a992009-05-22 14:50:54 -04008894static void
8895lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06008896{
James Smart51ef4c22007-08-02 11:10:31 -04008897 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8898 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06008899
James Smarte2af0d22010-03-15 11:25:32 -04008900 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -04008901 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -04008902
8903 /* Clean up Advanced Error Reporting (AER) if needed */
8904 if (phba->hba_flag & HBA_AER_ENABLED)
8905 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06008906}
8907
James Smart3772a992009-05-22 14:50:54 -04008908/**
James Smartda0436e2009-05-22 14:51:39 -04008909 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
8910 * @phba: pointer to lpfc hba data structure.
8911 *
8912 * returns the number of ELS/CT IOCBs to reserve
8913 **/
8914int
8915lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
8916{
8917 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
8918
James Smartf1126682009-06-10 17:22:44 -04008919 if (phba->sli_rev == LPFC_SLI_REV4) {
8920 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -04008921 return 10;
James Smartf1126682009-06-10 17:22:44 -04008922 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -04008923 return 25;
James Smartf1126682009-06-10 17:22:44 -04008924 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -04008925 return 50;
James Smartf1126682009-06-10 17:22:44 -04008926 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -04008927 return 100;
James Smartf1126682009-06-10 17:22:44 -04008928 else
James Smart6a9c52c2009-10-02 15:16:51 -04008929 return 150;
James Smartf1126682009-06-10 17:22:44 -04008930 } else
8931 return 0;
James Smartda0436e2009-05-22 14:51:39 -04008932}
8933
8934/**
James Smart52d52442011-05-24 11:42:45 -04008935 * lpfc_write_firmware - attempt to write a firmware image to the port
8936 * @phba: pointer to lpfc hba data structure.
8937 * @fw: pointer to firmware image returned from request_firmware.
8938 *
8939 * returns the number of bytes written if write is successful.
8940 * returns a negative error value if there were errors.
8941 * returns 0 if firmware matches currently active firmware on port.
8942 **/
8943int
8944lpfc_write_firmware(struct lpfc_hba *phba, const struct firmware *fw)
8945{
8946 char fwrev[32];
8947 struct lpfc_grp_hdr *image = (struct lpfc_grp_hdr *)fw->data;
8948 struct list_head dma_buffer_list;
8949 int i, rc = 0;
8950 struct lpfc_dmabuf *dmabuf, *next;
8951 uint32_t offset = 0, temp_offset = 0;
8952
8953 INIT_LIST_HEAD(&dma_buffer_list);
James Smart079b5c92011-08-21 21:48:49 -04008954 if ((be32_to_cpu(image->magic_number) != LPFC_GROUP_OJECT_MAGIC_NUM) ||
8955 (bf_get_be32(lpfc_grp_hdr_file_type, image) !=
8956 LPFC_FILE_TYPE_GROUP) ||
8957 (bf_get_be32(lpfc_grp_hdr_id, image) != LPFC_FILE_ID_GROUP) ||
8958 (be32_to_cpu(image->size) != fw->size)) {
James Smart52d52442011-05-24 11:42:45 -04008959 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8960 "3022 Invalid FW image found. "
James Smart079b5c92011-08-21 21:48:49 -04008961 "Magic:%x Type:%x ID:%x\n",
8962 be32_to_cpu(image->magic_number),
8963 bf_get_be32(lpfc_grp_hdr_file_type, image),
8964 bf_get_be32(lpfc_grp_hdr_id, image));
James Smart52d52442011-05-24 11:42:45 -04008965 return -EINVAL;
8966 }
8967 lpfc_decode_firmware_rev(phba, fwrev, 1);
James Smart88a2cfb2011-07-22 18:36:33 -04008968 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
James Smart52d52442011-05-24 11:42:45 -04008969 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8970 "3023 Updating Firmware. Current Version:%s "
8971 "New Version:%s\n",
James Smart88a2cfb2011-07-22 18:36:33 -04008972 fwrev, image->revision);
James Smart52d52442011-05-24 11:42:45 -04008973 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
8974 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
8975 GFP_KERNEL);
8976 if (!dmabuf) {
8977 rc = -ENOMEM;
8978 goto out;
8979 }
8980 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
8981 SLI4_PAGE_SIZE,
8982 &dmabuf->phys,
8983 GFP_KERNEL);
8984 if (!dmabuf->virt) {
8985 kfree(dmabuf);
8986 rc = -ENOMEM;
8987 goto out;
8988 }
8989 list_add_tail(&dmabuf->list, &dma_buffer_list);
8990 }
8991 while (offset < fw->size) {
8992 temp_offset = offset;
8993 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
James Smart079b5c92011-08-21 21:48:49 -04008994 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
James Smart52d52442011-05-24 11:42:45 -04008995 memcpy(dmabuf->virt,
8996 fw->data + temp_offset,
James Smart079b5c92011-08-21 21:48:49 -04008997 fw->size - temp_offset);
8998 temp_offset = fw->size;
James Smart52d52442011-05-24 11:42:45 -04008999 break;
9000 }
James Smart52d52442011-05-24 11:42:45 -04009001 memcpy(dmabuf->virt, fw->data + temp_offset,
9002 SLI4_PAGE_SIZE);
James Smart88a2cfb2011-07-22 18:36:33 -04009003 temp_offset += SLI4_PAGE_SIZE;
James Smart52d52442011-05-24 11:42:45 -04009004 }
9005 rc = lpfc_wr_object(phba, &dma_buffer_list,
9006 (fw->size - offset), &offset);
9007 if (rc) {
9008 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9009 "3024 Firmware update failed. "
9010 "%d\n", rc);
9011 goto out;
9012 }
9013 }
9014 rc = offset;
9015 }
9016out:
9017 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
9018 list_del(&dmabuf->list);
9019 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
9020 dmabuf->virt, dmabuf->phys);
9021 kfree(dmabuf);
9022 }
9023 return rc;
9024}
9025
9026/**
James Smartda0436e2009-05-22 14:51:39 -04009027 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
9028 * @pdev: pointer to PCI device
9029 * @pid: pointer to PCI device identifier
9030 *
9031 * This routine is called from the kernel's PCI subsystem to device with
9032 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
9033 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
9034 * information of the device and driver to see if the driver state that it
9035 * can support this kind of device. If the match is successful, the driver
9036 * core invokes this routine. If this routine determines it can claim the HBA,
9037 * it does all the initialization that it needs to do to handle the HBA
9038 * properly.
9039 *
9040 * Return code
9041 * 0 - driver can claim the device
9042 * negative value - driver can not claim the device
9043 **/
9044static int __devinit
9045lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
9046{
9047 struct lpfc_hba *phba;
9048 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04009049 struct Scsi_Host *shost = NULL;
James Smartda0436e2009-05-22 14:51:39 -04009050 int error;
9051 uint32_t cfg_mode, intr_mode;
9052 int mcnt;
James Smart05580562011-05-24 11:40:48 -04009053 int adjusted_fcp_eq_count;
James Smart52d52442011-05-24 11:42:45 -04009054 const struct firmware *fw;
9055 uint8_t file_name[16];
James Smartda0436e2009-05-22 14:51:39 -04009056
9057 /* Allocate memory for HBA structure */
9058 phba = lpfc_hba_alloc(pdev);
9059 if (!phba)
9060 return -ENOMEM;
9061
9062 /* Perform generic PCI device enabling operation */
9063 error = lpfc_enable_pci_dev(phba);
James Smart079b5c92011-08-21 21:48:49 -04009064 if (error)
James Smartda0436e2009-05-22 14:51:39 -04009065 goto out_free_phba;
James Smartda0436e2009-05-22 14:51:39 -04009066
9067 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
9068 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
9069 if (error)
9070 goto out_disable_pci_dev;
9071
9072 /* Set up SLI-4 specific device PCI memory space */
9073 error = lpfc_sli4_pci_mem_setup(phba);
9074 if (error) {
9075 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9076 "1410 Failed to set up pci memory space.\n");
9077 goto out_disable_pci_dev;
9078 }
9079
9080 /* Set up phase-1 common device driver resources */
9081 error = lpfc_setup_driver_resource_phase1(phba);
9082 if (error) {
9083 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9084 "1411 Failed to set up driver resource.\n");
9085 goto out_unset_pci_mem_s4;
9086 }
9087
9088 /* Set up SLI-4 Specific device driver resources */
9089 error = lpfc_sli4_driver_resource_setup(phba);
9090 if (error) {
9091 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9092 "1412 Failed to set up driver resource.\n");
9093 goto out_unset_pci_mem_s4;
9094 }
9095
9096 /* Initialize and populate the iocb list per host */
James Smart2a9bf3d2010-06-07 15:24:45 -04009097
9098 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9099 "2821 initialize iocb list %d.\n",
9100 phba->cfg_iocb_cnt*1024);
9101 error = lpfc_init_iocb_list(phba, phba->cfg_iocb_cnt*1024);
9102
James Smartda0436e2009-05-22 14:51:39 -04009103 if (error) {
9104 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9105 "1413 Failed to initialize iocb list.\n");
9106 goto out_unset_driver_resource_s4;
9107 }
9108
James Smart19ca7602010-11-20 23:11:55 -05009109 INIT_LIST_HEAD(&phba->active_rrq_list);
James Smart7d791df2011-07-22 18:37:52 -04009110 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
James Smart19ca7602010-11-20 23:11:55 -05009111
James Smartda0436e2009-05-22 14:51:39 -04009112 /* Set up common device driver resources */
9113 error = lpfc_setup_driver_resource_phase2(phba);
9114 if (error) {
9115 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9116 "1414 Failed to set up driver resource.\n");
9117 goto out_free_iocb_list;
9118 }
9119
James Smart079b5c92011-08-21 21:48:49 -04009120 /* Get the default values for Model Name and Description */
9121 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
9122
James Smartda0436e2009-05-22 14:51:39 -04009123 /* Create SCSI host to the physical port */
9124 error = lpfc_create_shost(phba);
9125 if (error) {
9126 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9127 "1415 Failed to create scsi host.\n");
9128 goto out_unset_driver_resource;
9129 }
9130
9131 /* Configure sysfs attributes */
9132 vport = phba->pport;
9133 error = lpfc_alloc_sysfs_attr(vport);
9134 if (error) {
9135 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9136 "1416 Failed to allocate sysfs attr\n");
9137 goto out_destroy_shost;
9138 }
9139
James Smart6669f9b2009-10-02 15:16:45 -04009140 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -04009141 /* Now, trying to enable interrupt and bring up the device */
9142 cfg_mode = phba->cfg_use_msi;
9143 while (true) {
9144 /* Put device to a known state before enabling interrupt */
9145 lpfc_stop_port(phba);
9146 /* Configure and enable interrupt */
9147 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
9148 if (intr_mode == LPFC_INTR_ERROR) {
9149 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9150 "0426 Failed to enable interrupt.\n");
9151 error = -ENODEV;
9152 goto out_free_sysfs_attr;
9153 }
James Smart05580562011-05-24 11:40:48 -04009154 /* Default to single EQ for non-MSI-X */
James Smartdef9c7a2009-12-21 17:02:28 -05009155 if (phba->intr_type != MSIX)
James Smart05580562011-05-24 11:40:48 -04009156 adjusted_fcp_eq_count = 0;
9157 else if (phba->sli4_hba.msix_vec_nr <
9158 phba->cfg_fcp_eq_count + 1)
9159 adjusted_fcp_eq_count = phba->sli4_hba.msix_vec_nr - 1;
9160 else
9161 adjusted_fcp_eq_count = phba->cfg_fcp_eq_count;
James Smart05580562011-05-24 11:40:48 -04009162 phba->cfg_fcp_eq_count = adjusted_fcp_eq_count;
James Smartda0436e2009-05-22 14:51:39 -04009163 /* Set up SLI-4 HBA */
9164 if (lpfc_sli4_hba_setup(phba)) {
9165 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9166 "1421 Failed to set up hba\n");
9167 error = -ENODEV;
9168 goto out_disable_intr;
9169 }
9170
9171 /* Send NOP mbx cmds for non-INTx mode active interrupt test */
9172 if (intr_mode != 0)
9173 mcnt = lpfc_sli4_send_nop_mbox_cmds(phba,
9174 LPFC_ACT_INTR_CNT);
9175
9176 /* Check active interrupts received only for MSI/MSI-X */
9177 if (intr_mode == 0 ||
9178 phba->sli.slistat.sli_intr >= LPFC_ACT_INTR_CNT) {
9179 /* Log the current active interrupt mode */
9180 phba->intr_mode = intr_mode;
9181 lpfc_log_intr_mode(phba, intr_mode);
9182 break;
9183 }
9184 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9185 "0451 Configure interrupt mode (%d) "
9186 "failed active interrupt test.\n",
9187 intr_mode);
James Smart2fcee4b2010-12-15 17:57:46 -05009188 /* Unset the previous SLI-4 HBA setup. */
9189 /*
9190 * TODO: Is this operation compatible with IF TYPE 2
9191 * devices? All port state is deleted and cleared.
9192 */
James Smartda0436e2009-05-22 14:51:39 -04009193 lpfc_sli4_unset_hba(phba);
9194 /* Try next level of interrupt mode */
9195 cfg_mode = --intr_mode;
9196 }
9197
9198 /* Perform post initialization setup */
9199 lpfc_post_init_setup(phba);
9200
James Smart52d52442011-05-24 11:42:45 -04009201 /* check for firmware upgrade or downgrade */
9202 snprintf(file_name, 16, "%s.grp", phba->ModelName);
9203 error = request_firmware(&fw, file_name, &phba->pcidev->dev);
9204 if (!error) {
9205 lpfc_write_firmware(phba, fw);
9206 release_firmware(fw);
9207 }
9208
James Smart1c6834a2009-07-19 10:01:26 -04009209 /* Check if there are static vports to be created. */
9210 lpfc_create_static_vport(phba);
James Smartda0436e2009-05-22 14:51:39 -04009211 return 0;
9212
9213out_disable_intr:
9214 lpfc_sli4_disable_intr(phba);
9215out_free_sysfs_attr:
9216 lpfc_free_sysfs_attr(vport);
9217out_destroy_shost:
9218 lpfc_destroy_shost(phba);
9219out_unset_driver_resource:
9220 lpfc_unset_driver_resource_phase2(phba);
9221out_free_iocb_list:
9222 lpfc_free_iocb_list(phba);
9223out_unset_driver_resource_s4:
9224 lpfc_sli4_driver_resource_unset(phba);
9225out_unset_pci_mem_s4:
9226 lpfc_sli4_pci_mem_unset(phba);
9227out_disable_pci_dev:
9228 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04009229 if (shost)
9230 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -04009231out_free_phba:
9232 lpfc_hba_free(phba);
9233 return error;
9234}
9235
9236/**
9237 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
9238 * @pdev: pointer to PCI device
9239 *
9240 * This routine is called from the kernel's PCI subsystem to device with
9241 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
9242 * removed from PCI bus, it performs all the necessary cleanup for the HBA
9243 * device to be removed from the PCI subsystem properly.
9244 **/
9245static void __devexit
9246lpfc_pci_remove_one_s4(struct pci_dev *pdev)
9247{
9248 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9249 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
9250 struct lpfc_vport **vports;
9251 struct lpfc_hba *phba = vport->phba;
9252 int i;
9253
9254 /* Mark the device unloading flag */
9255 spin_lock_irq(&phba->hbalock);
9256 vport->load_flag |= FC_UNLOADING;
9257 spin_unlock_irq(&phba->hbalock);
9258
9259 /* Free the HBA sysfs attributes */
9260 lpfc_free_sysfs_attr(vport);
9261
9262 /* Release all the vports against this physical port */
9263 vports = lpfc_create_vport_work_array(phba);
9264 if (vports != NULL)
9265 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
9266 fc_vport_terminate(vports[i]->fc_vport);
9267 lpfc_destroy_vport_work_array(phba, vports);
9268
9269 /* Remove FC host and then SCSI host with the physical port */
9270 fc_remove_host(shost);
9271 scsi_remove_host(shost);
9272
9273 /* Perform cleanup on the physical port */
9274 lpfc_cleanup(vport);
9275
9276 /*
9277 * Bring down the SLI Layer. This step disables all interrupts,
9278 * clears the rings, discards all mailbox commands, and resets
9279 * the HBA FCoE function.
9280 */
9281 lpfc_debugfs_terminate(vport);
9282 lpfc_sli4_hba_unset(phba);
9283
9284 spin_lock_irq(&phba->hbalock);
9285 list_del_init(&vport->listentry);
9286 spin_unlock_irq(&phba->hbalock);
9287
James Smart3677a3a2010-09-29 11:19:14 -04009288 /* Perform scsi free before driver resource_unset since scsi
James Smartda0436e2009-05-22 14:51:39 -04009289 * buffers are released to their corresponding pools here.
9290 */
9291 lpfc_scsi_free(phba);
9292 lpfc_sli4_driver_resource_unset(phba);
9293
9294 /* Unmap adapter Control and Doorbell registers */
9295 lpfc_sli4_pci_mem_unset(phba);
9296
9297 /* Release PCI resources and disable device's PCI function */
9298 scsi_host_put(shost);
9299 lpfc_disable_pci_dev(phba);
9300
9301 /* Finally, free the driver's device data structure */
9302 lpfc_hba_free(phba);
9303
9304 return;
9305}
9306
9307/**
9308 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
9309 * @pdev: pointer to PCI device
9310 * @msg: power management message
9311 *
9312 * This routine is called from the kernel's PCI subsystem to support system
9313 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
9314 * this method, it quiesces the device by stopping the driver's worker
9315 * thread for the device, turning off device's interrupt and DMA, and bring
9316 * the device offline. Note that as the driver implements the minimum PM
9317 * requirements to a power-aware driver's PM support for suspend/resume -- all
9318 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
9319 * method call will be treated as SUSPEND and the driver will fully
9320 * reinitialize its device during resume() method call, the driver will set
9321 * device to PCI_D3hot state in PCI config space instead of setting it
9322 * according to the @msg provided by the PM.
9323 *
9324 * Return code
9325 * 0 - driver suspended the device
9326 * Error otherwise
9327 **/
9328static int
9329lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
9330{
9331 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9332 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9333
9334 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart75baf692010-06-08 18:31:21 -04009335 "2843 PCI device Power Management suspend.\n");
James Smartda0436e2009-05-22 14:51:39 -04009336
9337 /* Bring down the device */
9338 lpfc_offline_prep(phba);
9339 lpfc_offline(phba);
9340 kthread_stop(phba->worker_thread);
9341
9342 /* Disable interrupt from device */
9343 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -04009344 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04009345
9346 /* Save device state to PCI config space */
9347 pci_save_state(pdev);
9348 pci_set_power_state(pdev, PCI_D3hot);
9349
9350 return 0;
9351}
9352
9353/**
9354 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
9355 * @pdev: pointer to PCI device
9356 *
9357 * This routine is called from the kernel's PCI subsystem to support system
9358 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
9359 * this method, it restores the device's PCI config space state and fully
9360 * reinitializes the device and brings it online. Note that as the driver
9361 * implements the minimum PM requirements to a power-aware driver's PM for
9362 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
9363 * to the suspend() method call will be treated as SUSPEND and the driver
9364 * will fully reinitialize its device during resume() method call, the device
9365 * will be set to PCI_D0 directly in PCI config space before restoring the
9366 * state.
9367 *
9368 * Return code
9369 * 0 - driver suspended the device
9370 * Error otherwise
9371 **/
9372static int
9373lpfc_pci_resume_one_s4(struct pci_dev *pdev)
9374{
9375 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9376 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9377 uint32_t intr_mode;
9378 int error;
9379
9380 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9381 "0292 PCI device Power Management resume.\n");
9382
9383 /* Restore device state from PCI config space */
9384 pci_set_power_state(pdev, PCI_D0);
9385 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05009386
9387 /*
9388 * As the new kernel behavior of pci_restore_state() API call clears
9389 * device saved_state flag, need to save the restored state again.
9390 */
9391 pci_save_state(pdev);
9392
James Smartda0436e2009-05-22 14:51:39 -04009393 if (pdev->is_busmaster)
9394 pci_set_master(pdev);
9395
9396 /* Startup the kernel thread for this host adapter. */
9397 phba->worker_thread = kthread_run(lpfc_do_work, phba,
9398 "lpfc_worker_%d", phba->brd_no);
9399 if (IS_ERR(phba->worker_thread)) {
9400 error = PTR_ERR(phba->worker_thread);
9401 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9402 "0293 PM resume failed to start worker "
9403 "thread: error=x%x.\n", error);
9404 return error;
9405 }
9406
9407 /* Configure and enable interrupt */
9408 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
9409 if (intr_mode == LPFC_INTR_ERROR) {
9410 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9411 "0294 PM resume Failed to enable interrupt\n");
9412 return -EIO;
9413 } else
9414 phba->intr_mode = intr_mode;
9415
9416 /* Restart HBA and bring it online */
9417 lpfc_sli_brdrestart(phba);
9418 lpfc_online(phba);
9419
9420 /* Log the current active interrupt mode */
9421 lpfc_log_intr_mode(phba, phba->intr_mode);
9422
9423 return 0;
9424}
9425
9426/**
James Smart75baf692010-06-08 18:31:21 -04009427 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
9428 * @phba: pointer to lpfc hba data structure.
9429 *
9430 * This routine is called to prepare the SLI4 device for PCI slot recover. It
9431 * aborts all the outstanding SCSI I/Os to the pci device.
9432 **/
9433static void
9434lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
9435{
9436 struct lpfc_sli *psli = &phba->sli;
9437 struct lpfc_sli_ring *pring;
9438
9439 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9440 "2828 PCI channel I/O abort preparing for recovery\n");
9441 /*
9442 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
9443 * and let the SCSI mid-layer to retry them to recover.
9444 */
9445 pring = &psli->ring[psli->fcp_ring];
9446 lpfc_sli_abort_iocb_ring(phba, pring);
9447}
9448
9449/**
9450 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
9451 * @phba: pointer to lpfc hba data structure.
9452 *
9453 * This routine is called to prepare the SLI4 device for PCI slot reset. It
9454 * disables the device interrupt and pci device, and aborts the internal FCP
9455 * pending I/Os.
9456 **/
9457static void
9458lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
9459{
9460 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9461 "2826 PCI channel disable preparing for reset\n");
9462
9463 /* Block any management I/Os to the device */
9464 lpfc_block_mgmt_io(phba);
9465
9466 /* Block all SCSI devices' I/Os on the host */
9467 lpfc_scsi_dev_block(phba);
9468
9469 /* stop all timers */
9470 lpfc_stop_hba_timers(phba);
9471
9472 /* Disable interrupt and pci device */
9473 lpfc_sli4_disable_intr(phba);
James Smart5350d872011-10-10 21:33:49 -04009474 lpfc_sli4_queue_destroy(phba);
James Smart75baf692010-06-08 18:31:21 -04009475 pci_disable_device(phba->pcidev);
9476
9477 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
9478 lpfc_sli_flush_fcp_rings(phba);
9479}
9480
9481/**
9482 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
9483 * @phba: pointer to lpfc hba data structure.
9484 *
9485 * This routine is called to prepare the SLI4 device for PCI slot permanently
9486 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
9487 * pending I/Os.
9488 **/
9489static void
9490lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
9491{
9492 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9493 "2827 PCI channel permanent disable for failure\n");
9494
9495 /* Block all SCSI devices' I/Os on the host */
9496 lpfc_scsi_dev_block(phba);
9497
9498 /* stop all timers */
9499 lpfc_stop_hba_timers(phba);
9500
9501 /* Clean up all driver's outstanding SCSI I/Os */
9502 lpfc_sli_flush_fcp_rings(phba);
9503}
9504
9505/**
James Smartda0436e2009-05-22 14:51:39 -04009506 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
9507 * @pdev: pointer to PCI device.
9508 * @state: the current PCI connection state.
9509 *
9510 * This routine is called from the PCI subsystem for error handling to device
9511 * with SLI-4 interface spec. This function is called by the PCI subsystem
9512 * after a PCI bus error affecting this device has been detected. When this
9513 * function is invoked, it will need to stop all the I/Os and interrupt(s)
9514 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
9515 * for the PCI subsystem to perform proper recovery as desired.
9516 *
9517 * Return codes
9518 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
9519 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
9520 **/
9521static pci_ers_result_t
9522lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
9523{
James Smart75baf692010-06-08 18:31:21 -04009524 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9525 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9526
9527 switch (state) {
9528 case pci_channel_io_normal:
9529 /* Non-fatal error, prepare for recovery */
9530 lpfc_sli4_prep_dev_for_recover(phba);
9531 return PCI_ERS_RESULT_CAN_RECOVER;
9532 case pci_channel_io_frozen:
9533 /* Fatal error, prepare for slot reset */
9534 lpfc_sli4_prep_dev_for_reset(phba);
9535 return PCI_ERS_RESULT_NEED_RESET;
9536 case pci_channel_io_perm_failure:
9537 /* Permanent failure, prepare for device down */
9538 lpfc_sli4_prep_dev_for_perm_failure(phba);
9539 return PCI_ERS_RESULT_DISCONNECT;
9540 default:
9541 /* Unknown state, prepare and request slot reset */
9542 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9543 "2825 Unknown PCI error state: x%x\n", state);
9544 lpfc_sli4_prep_dev_for_reset(phba);
9545 return PCI_ERS_RESULT_NEED_RESET;
9546 }
James Smartda0436e2009-05-22 14:51:39 -04009547}
9548
9549/**
9550 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
9551 * @pdev: pointer to PCI device.
9552 *
9553 * This routine is called from the PCI subsystem for error handling to device
9554 * with SLI-4 interface spec. It is called after PCI bus has been reset to
9555 * restart the PCI card from scratch, as if from a cold-boot. During the
9556 * PCI subsystem error recovery, after the driver returns
9557 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
9558 * recovery and then call this routine before calling the .resume method to
9559 * recover the device. This function will initialize the HBA device, enable
9560 * the interrupt, but it will just put the HBA to offline state without
9561 * passing any I/O traffic.
9562 *
9563 * Return codes
9564 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
9565 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
9566 */
9567static pci_ers_result_t
9568lpfc_io_slot_reset_s4(struct pci_dev *pdev)
9569{
James Smart75baf692010-06-08 18:31:21 -04009570 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9571 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9572 struct lpfc_sli *psli = &phba->sli;
9573 uint32_t intr_mode;
9574
9575 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
9576 if (pci_enable_device_mem(pdev)) {
9577 printk(KERN_ERR "lpfc: Cannot re-enable "
9578 "PCI device after reset.\n");
9579 return PCI_ERS_RESULT_DISCONNECT;
9580 }
9581
9582 pci_restore_state(pdev);
James Smart0a96e972011-07-22 18:37:28 -04009583
9584 /*
9585 * As the new kernel behavior of pci_restore_state() API call clears
9586 * device saved_state flag, need to save the restored state again.
9587 */
9588 pci_save_state(pdev);
9589
James Smart75baf692010-06-08 18:31:21 -04009590 if (pdev->is_busmaster)
9591 pci_set_master(pdev);
9592
9593 spin_lock_irq(&phba->hbalock);
9594 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
9595 spin_unlock_irq(&phba->hbalock);
9596
9597 /* Configure and enable interrupt */
9598 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
9599 if (intr_mode == LPFC_INTR_ERROR) {
9600 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9601 "2824 Cannot re-enable interrupt after "
9602 "slot reset.\n");
9603 return PCI_ERS_RESULT_DISCONNECT;
9604 } else
9605 phba->intr_mode = intr_mode;
9606
9607 /* Log the current active interrupt mode */
9608 lpfc_log_intr_mode(phba, phba->intr_mode);
9609
James Smartda0436e2009-05-22 14:51:39 -04009610 return PCI_ERS_RESULT_RECOVERED;
9611}
9612
9613/**
9614 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
9615 * @pdev: pointer to PCI device
9616 *
9617 * This routine is called from the PCI subsystem for error handling to device
9618 * with SLI-4 interface spec. It is called when kernel error recovery tells
9619 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
9620 * error recovery. After this call, traffic can start to flow from this device
9621 * again.
9622 **/
9623static void
9624lpfc_io_resume_s4(struct pci_dev *pdev)
9625{
James Smart75baf692010-06-08 18:31:21 -04009626 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9627 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9628
9629 /*
9630 * In case of slot reset, as function reset is performed through
9631 * mailbox command which needs DMA to be enabled, this operation
9632 * has to be moved to the io resume phase. Taking device offline
9633 * will perform the necessary cleanup.
9634 */
9635 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
9636 /* Perform device reset */
9637 lpfc_offline_prep(phba);
9638 lpfc_offline(phba);
9639 lpfc_sli_brdrestart(phba);
9640 /* Bring the device back online */
9641 lpfc_online(phba);
9642 }
9643
9644 /* Clean up Advanced Error Reporting (AER) if needed */
9645 if (phba->hba_flag & HBA_AER_ENABLED)
9646 pci_cleanup_aer_uncorrect_error_status(pdev);
James Smartda0436e2009-05-22 14:51:39 -04009647}
9648
9649/**
James Smart3772a992009-05-22 14:50:54 -04009650 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
9651 * @pdev: pointer to PCI device
9652 * @pid: pointer to PCI device identifier
9653 *
9654 * This routine is to be registered to the kernel's PCI subsystem. When an
9655 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
9656 * at PCI device-specific information of the device and driver to see if the
9657 * driver state that it can support this kind of device. If the match is
9658 * successful, the driver core invokes this routine. This routine dispatches
9659 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
9660 * do all the initialization that it needs to do to handle the HBA device
9661 * properly.
9662 *
9663 * Return code
9664 * 0 - driver can claim the device
9665 * negative value - driver can not claim the device
9666 **/
9667static int __devinit
9668lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
9669{
9670 int rc;
James Smart8fa38512009-07-19 10:01:03 -04009671 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -04009672
James Smart28baac72010-02-12 14:42:03 -05009673 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -04009674 return -ENODEV;
9675
James Smart8fa38512009-07-19 10:01:03 -04009676 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -05009677 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -04009678 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04009679 else
James Smart3772a992009-05-22 14:50:54 -04009680 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04009681
James Smart3772a992009-05-22 14:50:54 -04009682 return rc;
9683}
9684
9685/**
9686 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
9687 * @pdev: pointer to PCI device
9688 *
9689 * This routine is to be registered to the kernel's PCI subsystem. When an
9690 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
9691 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
9692 * remove routine, which will perform all the necessary cleanup for the
9693 * device to be removed from the PCI subsystem properly.
9694 **/
9695static void __devexit
9696lpfc_pci_remove_one(struct pci_dev *pdev)
9697{
9698 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9699 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9700
9701 switch (phba->pci_dev_grp) {
9702 case LPFC_PCI_DEV_LP:
9703 lpfc_pci_remove_one_s3(pdev);
9704 break;
James Smartda0436e2009-05-22 14:51:39 -04009705 case LPFC_PCI_DEV_OC:
9706 lpfc_pci_remove_one_s4(pdev);
9707 break;
James Smart3772a992009-05-22 14:50:54 -04009708 default:
9709 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9710 "1424 Invalid PCI device group: 0x%x\n",
9711 phba->pci_dev_grp);
9712 break;
9713 }
9714 return;
9715}
9716
9717/**
9718 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
9719 * @pdev: pointer to PCI device
9720 * @msg: power management message
9721 *
9722 * This routine is to be registered to the kernel's PCI subsystem to support
9723 * system Power Management (PM). When PM invokes this method, it dispatches
9724 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
9725 * suspend the device.
9726 *
9727 * Return code
9728 * 0 - driver suspended the device
9729 * Error otherwise
9730 **/
9731static int
9732lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
9733{
9734 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9735 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9736 int rc = -ENODEV;
9737
9738 switch (phba->pci_dev_grp) {
9739 case LPFC_PCI_DEV_LP:
9740 rc = lpfc_pci_suspend_one_s3(pdev, msg);
9741 break;
James Smartda0436e2009-05-22 14:51:39 -04009742 case LPFC_PCI_DEV_OC:
9743 rc = lpfc_pci_suspend_one_s4(pdev, msg);
9744 break;
James Smart3772a992009-05-22 14:50:54 -04009745 default:
9746 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9747 "1425 Invalid PCI device group: 0x%x\n",
9748 phba->pci_dev_grp);
9749 break;
9750 }
9751 return rc;
9752}
9753
9754/**
9755 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
9756 * @pdev: pointer to PCI device
9757 *
9758 * This routine is to be registered to the kernel's PCI subsystem to support
9759 * system Power Management (PM). When PM invokes this method, it dispatches
9760 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
9761 * resume the device.
9762 *
9763 * Return code
9764 * 0 - driver suspended the device
9765 * Error otherwise
9766 **/
9767static int
9768lpfc_pci_resume_one(struct pci_dev *pdev)
9769{
9770 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9771 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9772 int rc = -ENODEV;
9773
9774 switch (phba->pci_dev_grp) {
9775 case LPFC_PCI_DEV_LP:
9776 rc = lpfc_pci_resume_one_s3(pdev);
9777 break;
James Smartda0436e2009-05-22 14:51:39 -04009778 case LPFC_PCI_DEV_OC:
9779 rc = lpfc_pci_resume_one_s4(pdev);
9780 break;
James Smart3772a992009-05-22 14:50:54 -04009781 default:
9782 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9783 "1426 Invalid PCI device group: 0x%x\n",
9784 phba->pci_dev_grp);
9785 break;
9786 }
9787 return rc;
9788}
9789
9790/**
9791 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
9792 * @pdev: pointer to PCI device.
9793 * @state: the current PCI connection state.
9794 *
9795 * This routine is registered to the PCI subsystem for error handling. This
9796 * function is called by the PCI subsystem after a PCI bus error affecting
9797 * this device has been detected. When this routine is invoked, it dispatches
9798 * the action to the proper SLI-3 or SLI-4 device error detected handling
9799 * routine, which will perform the proper error detected operation.
9800 *
9801 * Return codes
9802 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
9803 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
9804 **/
9805static pci_ers_result_t
9806lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
9807{
9808 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9809 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9810 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
9811
9812 switch (phba->pci_dev_grp) {
9813 case LPFC_PCI_DEV_LP:
9814 rc = lpfc_io_error_detected_s3(pdev, state);
9815 break;
James Smartda0436e2009-05-22 14:51:39 -04009816 case LPFC_PCI_DEV_OC:
9817 rc = lpfc_io_error_detected_s4(pdev, state);
9818 break;
James Smart3772a992009-05-22 14:50:54 -04009819 default:
9820 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9821 "1427 Invalid PCI device group: 0x%x\n",
9822 phba->pci_dev_grp);
9823 break;
9824 }
9825 return rc;
9826}
9827
9828/**
9829 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
9830 * @pdev: pointer to PCI device.
9831 *
9832 * This routine is registered to the PCI subsystem for error handling. This
9833 * function is called after PCI bus has been reset to restart the PCI card
9834 * from scratch, as if from a cold-boot. When this routine is invoked, it
9835 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
9836 * routine, which will perform the proper device reset.
9837 *
9838 * Return codes
9839 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
9840 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
9841 **/
9842static pci_ers_result_t
9843lpfc_io_slot_reset(struct pci_dev *pdev)
9844{
9845 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9846 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9847 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
9848
9849 switch (phba->pci_dev_grp) {
9850 case LPFC_PCI_DEV_LP:
9851 rc = lpfc_io_slot_reset_s3(pdev);
9852 break;
James Smartda0436e2009-05-22 14:51:39 -04009853 case LPFC_PCI_DEV_OC:
9854 rc = lpfc_io_slot_reset_s4(pdev);
9855 break;
James Smart3772a992009-05-22 14:50:54 -04009856 default:
9857 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9858 "1428 Invalid PCI device group: 0x%x\n",
9859 phba->pci_dev_grp);
9860 break;
9861 }
9862 return rc;
9863}
9864
9865/**
9866 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
9867 * @pdev: pointer to PCI device
9868 *
9869 * This routine is registered to the PCI subsystem for error handling. It
9870 * is called when kernel error recovery tells the lpfc driver that it is
9871 * OK to resume normal PCI operation after PCI bus error recovery. When
9872 * this routine is invoked, it dispatches the action to the proper SLI-3
9873 * or SLI-4 device io_resume routine, which will resume the device operation.
9874 **/
9875static void
9876lpfc_io_resume(struct pci_dev *pdev)
9877{
9878 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9879 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9880
9881 switch (phba->pci_dev_grp) {
9882 case LPFC_PCI_DEV_LP:
9883 lpfc_io_resume_s3(pdev);
9884 break;
James Smartda0436e2009-05-22 14:51:39 -04009885 case LPFC_PCI_DEV_OC:
9886 lpfc_io_resume_s4(pdev);
9887 break;
James Smart3772a992009-05-22 14:50:54 -04009888 default:
9889 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9890 "1429 Invalid PCI device group: 0x%x\n",
9891 phba->pci_dev_grp);
9892 break;
9893 }
9894 return;
9895}
9896
dea31012005-04-17 16:05:31 -05009897static struct pci_device_id lpfc_id_table[] = {
9898 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
9899 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04009900 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
9901 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05009902 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
9903 PCI_ANY_ID, PCI_ANY_ID, },
9904 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
9905 PCI_ANY_ID, PCI_ANY_ID, },
9906 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
9907 PCI_ANY_ID, PCI_ANY_ID, },
9908 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
9909 PCI_ANY_ID, PCI_ANY_ID, },
9910 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
9911 PCI_ANY_ID, PCI_ANY_ID, },
9912 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
9913 PCI_ANY_ID, PCI_ANY_ID, },
9914 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
9915 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05009916 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
9917 PCI_ANY_ID, PCI_ANY_ID, },
9918 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
9919 PCI_ANY_ID, PCI_ANY_ID, },
9920 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
9921 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05009922 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
9923 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05009924 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
9925 PCI_ANY_ID, PCI_ANY_ID, },
9926 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
9927 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05009928 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
9929 PCI_ANY_ID, PCI_ANY_ID, },
9930 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
9931 PCI_ANY_ID, PCI_ANY_ID, },
9932 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
9933 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -04009934 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
9935 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05009936 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
9937 PCI_ANY_ID, PCI_ANY_ID, },
9938 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
9939 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05009940 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
9941 PCI_ANY_ID, PCI_ANY_ID, },
9942 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
9943 PCI_ANY_ID, PCI_ANY_ID, },
9944 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
9945 PCI_ANY_ID, PCI_ANY_ID, },
9946 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
9947 PCI_ANY_ID, PCI_ANY_ID, },
9948 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
9949 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05009950 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
9951 PCI_ANY_ID, PCI_ANY_ID, },
9952 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
9953 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -04009954 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
9955 PCI_ANY_ID, PCI_ANY_ID, },
9956 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
9957 PCI_ANY_ID, PCI_ANY_ID, },
9958 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
9959 PCI_ANY_ID, PCI_ANY_ID, },
9960 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
9961 PCI_ANY_ID, PCI_ANY_ID, },
9962 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
9963 PCI_ANY_ID, PCI_ANY_ID, },
9964 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
9965 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -04009966 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
9967 PCI_ANY_ID, PCI_ANY_ID, },
9968 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
9969 PCI_ANY_ID, PCI_ANY_ID, },
9970 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
9971 PCI_ANY_ID, PCI_ANY_ID, },
James Smart3772a992009-05-22 14:50:54 -04009972 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
9973 PCI_ANY_ID, PCI_ANY_ID, },
James Smarta747c9c2009-11-18 15:41:10 -05009974 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
9975 PCI_ANY_ID, PCI_ANY_ID, },
9976 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
James Smart6669f9b2009-10-02 15:16:45 -04009977 PCI_ANY_ID, PCI_ANY_ID, },
James Smart98fc5dd2010-06-07 15:24:29 -04009978 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BALIUS,
9979 PCI_ANY_ID, PCI_ANY_ID, },
James Smart085c6472010-11-20 23:11:37 -05009980 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC,
9981 PCI_ANY_ID, PCI_ANY_ID, },
9982 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE,
9983 PCI_ANY_ID, PCI_ANY_ID, },
James Smartc0c11512011-05-24 11:41:34 -04009984 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC_VF,
9985 PCI_ANY_ID, PCI_ANY_ID, },
9986 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE_VF,
9987 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05009988 { 0 }
9989};
9990
9991MODULE_DEVICE_TABLE(pci, lpfc_id_table);
9992
Linas Vepstas8d63f372007-02-14 14:28:36 -06009993static struct pci_error_handlers lpfc_err_handler = {
9994 .error_detected = lpfc_io_error_detected,
9995 .slot_reset = lpfc_io_slot_reset,
9996 .resume = lpfc_io_resume,
9997};
9998
dea31012005-04-17 16:05:31 -05009999static struct pci_driver lpfc_driver = {
10000 .name = LPFC_DRIVER_NAME,
10001 .id_table = lpfc_id_table,
10002 .probe = lpfc_pci_probe_one,
10003 .remove = __devexit_p(lpfc_pci_remove_one),
James Smart3a55b532008-12-04 22:38:54 -050010004 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -040010005 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -050010006 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -050010007};
10008
James Smarte59058c2008-08-24 21:49:00 -040010009/**
James Smart3621a712009-04-06 18:47:14 -040010010 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -040010011 *
10012 * This routine is to be invoked when the lpfc module is loaded into the
10013 * kernel. The special kernel macro module_init() is used to indicate the
10014 * role of this routine to the kernel as lpfc module entry point.
10015 *
10016 * Return codes
10017 * 0 - successful
10018 * -ENOMEM - FC attach transport failed
10019 * all others - failed
10020 */
dea31012005-04-17 16:05:31 -050010021static int __init
10022lpfc_init(void)
10023{
10024 int error = 0;
10025
10026 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010027 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -050010028
James Smart7ee5d432007-10-27 13:37:17 -040010029 if (lpfc_enable_npiv) {
10030 lpfc_transport_functions.vport_create = lpfc_vport_create;
10031 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
10032 }
dea31012005-04-17 16:05:31 -050010033 lpfc_transport_template =
10034 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -040010035 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -050010036 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -040010037 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -040010038 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -040010039 fc_attach_transport(&lpfc_vport_transport_functions);
10040 if (lpfc_vport_transport_template == NULL) {
10041 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040010042 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -040010043 }
James Smart7ee5d432007-10-27 13:37:17 -040010044 }
dea31012005-04-17 16:05:31 -050010045 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -050010046 if (error) {
dea31012005-04-17 16:05:31 -050010047 fc_release_transport(lpfc_transport_template);
James Smartd7c255b2008-08-24 21:50:00 -040010048 if (lpfc_enable_npiv)
10049 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -050010050 }
dea31012005-04-17 16:05:31 -050010051
10052 return error;
10053}
10054
James Smarte59058c2008-08-24 21:49:00 -040010055/**
James Smart3621a712009-04-06 18:47:14 -040010056 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -040010057 *
10058 * This routine is invoked when the lpfc module is removed from the kernel.
10059 * The special kernel macro module_exit() is used to indicate the role of
10060 * this routine to the kernel as lpfc module exit point.
10061 */
dea31012005-04-17 16:05:31 -050010062static void __exit
10063lpfc_exit(void)
10064{
10065 pci_unregister_driver(&lpfc_driver);
10066 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -040010067 if (lpfc_enable_npiv)
10068 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -050010069 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040010070 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
10071 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050010072 (1L << _dump_buf_data_order), _dump_buf_data);
10073 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
10074 }
10075
10076 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -040010077 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
10078 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -050010079 (1L << _dump_buf_dif_order), _dump_buf_dif);
10080 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
10081 }
dea31012005-04-17 16:05:31 -050010082}
10083
10084module_init(lpfc_init);
10085module_exit(lpfc_exit);
10086MODULE_LICENSE("GPL");
10087MODULE_DESCRIPTION(LPFC_MODULE_DESC);
10088MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
10089MODULE_VERSION("0:" LPFC_DRIVER_VERSION);