blob: 8da8fc69227f45df808d7b70ef16ee087ce317ed [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 Smart4fede782010-01-26 23:08:55 -05004 * Copyright (C) 2004-2010 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/delay.h>
24#include <linux/dma-mapping.h>
25#include <linux/idr.h>
26#include <linux/interrupt.h>
27#include <linux/kthread.h>
28#include <linux/pci.h>
29#include <linux/spinlock.h>
James Smart92d7f7b2007-06-17 19:56:38 -050030#include <linux/ctype.h>
James Smart0d878412009-10-02 15:16:56 -040031#include <linux/aer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
dea31012005-04-17 16:05:31 -050033
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040034#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050035#include <scsi/scsi_device.h>
36#include <scsi/scsi_host.h>
37#include <scsi/scsi_transport_fc.h>
38
James Smartda0436e2009-05-22 14:51:39 -040039#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050040#include "lpfc_hw.h"
41#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040042#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040043#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050044#include "lpfc_disc.h"
45#include "lpfc_scsi.h"
46#include "lpfc.h"
47#include "lpfc_logmsg.h"
48#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050049#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050050#include "lpfc_version.h"
51
James Smart81301a92008-12-04 22:39:46 -050052char *_dump_buf_data;
53unsigned long _dump_buf_data_order;
54char *_dump_buf_dif;
55unsigned long _dump_buf_dif_order;
56spinlock_t _dump_buf_lock;
57
dea31012005-04-17 16:05:31 -050058static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
59static int lpfc_post_rcv_buf(struct lpfc_hba *);
James Smartda0436e2009-05-22 14:51:39 -040060static int lpfc_sli4_queue_create(struct lpfc_hba *);
61static void lpfc_sli4_queue_destroy(struct lpfc_hba *);
62static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
63static int lpfc_setup_endian_order(struct lpfc_hba *);
64static int lpfc_sli4_read_config(struct lpfc_hba *);
65static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
66static void lpfc_free_sgl_list(struct lpfc_hba *);
67static int lpfc_init_sgl_list(struct lpfc_hba *);
68static int lpfc_init_active_sgl_array(struct lpfc_hba *);
69static void lpfc_free_active_sgl(struct lpfc_hba *);
70static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
71static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
72static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
73static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
74static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
dea31012005-04-17 16:05:31 -050075
76static struct scsi_transport_template *lpfc_transport_template = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050077static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
dea31012005-04-17 16:05:31 -050078static DEFINE_IDR(lpfc_hba_index);
79
James Smarte59058c2008-08-24 21:49:00 -040080/**
James Smart3621a712009-04-06 18:47:14 -040081 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
James Smarte59058c2008-08-24 21:49:00 -040082 * @phba: pointer to lpfc hba data structure.
83 *
84 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
85 * mailbox command. It retrieves the revision information from the HBA and
86 * collects the Vital Product Data (VPD) about the HBA for preparing the
87 * configuration of the HBA.
88 *
89 * Return codes:
90 * 0 - success.
91 * -ERESTART - requests the SLI layer to reset the HBA and try again.
92 * Any other value - indicates an error.
93 **/
dea31012005-04-17 16:05:31 -050094int
James Smart2e0fef82007-06-17 19:56:36 -050095lpfc_config_port_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050096{
97 lpfc_vpd_t *vp = &phba->vpd;
98 int i = 0, rc;
99 LPFC_MBOXQ_t *pmb;
100 MAILBOX_t *mb;
101 char *lpfc_vpd_data = NULL;
102 uint16_t offset = 0;
103 static char licensed[56] =
104 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -0400105 static int init_key = 1;
dea31012005-04-17 16:05:31 -0500106
107 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
108 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500109 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500110 return -ENOMEM;
111 }
112
James Smart04c68492009-05-22 14:52:52 -0400113 mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -0500114 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500115
116 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -0400117 if (init_key) {
118 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -0500119
James Smart65a29c12006-07-06 15:50:50 -0400120 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
121 *ptext = cpu_to_be32(*ptext);
122 init_key = 0;
123 }
dea31012005-04-17 16:05:31 -0500124
125 lpfc_read_nv(phba, pmb);
126 memset((char*)mb->un.varRDnvp.rsvd3, 0,
127 sizeof (mb->un.varRDnvp.rsvd3));
128 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
129 sizeof (licensed));
130
131 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
132
133 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500134 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smarte8b62012007-08-02 11:10:09 -0400135 "0324 Config Port initialization "
dea31012005-04-17 16:05:31 -0500136 "error, mbxCmd x%x READ_NVPARM, "
137 "mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500138 mb->mbxCommand, mb->mbxStatus);
139 mempool_free(pmb, phba->mbox_mem_pool);
140 return -ERESTART;
141 }
142 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
James Smart2e0fef82007-06-17 19:56:36 -0500143 sizeof(phba->wwnn));
144 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
145 sizeof(phba->wwpn));
dea31012005-04-17 16:05:31 -0500146 }
147
James Smart92d7f7b2007-06-17 19:56:38 -0500148 phba->sli3_options = 0x0;
149
dea31012005-04-17 16:05:31 -0500150 /* Setup and issue mailbox READ REV command */
151 lpfc_read_rev(phba, pmb);
152 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
153 if (rc != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500154 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400155 "0439 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500156 "READ_REV, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500157 mb->mbxCommand, mb->mbxStatus);
158 mempool_free( pmb, phba->mbox_mem_pool);
159 return -ERESTART;
160 }
161
James Smart92d7f7b2007-06-17 19:56:38 -0500162
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500163 /*
164 * The value of rr must be 1 since the driver set the cv field to 1.
165 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500166 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500167 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500168 vp->rev.rBit = 0;
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500169 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400170 "0440 Adapter failed to init, READ_REV has "
171 "missing revision information.\n");
dea31012005-04-17 16:05:31 -0500172 mempool_free(pmb, phba->mbox_mem_pool);
173 return -ERESTART;
dea31012005-04-17 16:05:31 -0500174 }
175
James Smart495a7142008-06-14 22:52:59 -0400176 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
177 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -0500178 return -EINVAL;
James Smart495a7142008-06-14 22:52:59 -0400179 }
James Smarted957682007-06-17 19:56:37 -0500180
dea31012005-04-17 16:05:31 -0500181 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500182 vp->rev.rBit = 1;
James Smart92d7f7b2007-06-17 19:56:38 -0500183 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500184 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
185 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
186 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
187 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500188 vp->rev.biuRev = mb->un.varRdRev.biuRev;
189 vp->rev.smRev = mb->un.varRdRev.smRev;
190 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
191 vp->rev.endecRev = mb->un.varRdRev.endecRev;
192 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
193 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
194 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
195 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
196 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
197 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
198
James Smart92d7f7b2007-06-17 19:56:38 -0500199 /* If the sli feature level is less then 9, we must
200 * tear down all RPIs and VPIs on link down if NPIV
201 * is enabled.
202 */
203 if (vp->rev.feaLevelHigh < 9)
204 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
205
dea31012005-04-17 16:05:31 -0500206 if (lpfc_is_LC_HBA(phba->pcidev->device))
207 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
208 sizeof (phba->RandomData));
209
dea31012005-04-17 16:05:31 -0500210 /* Get adapter VPD information */
dea31012005-04-17 16:05:31 -0500211 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
212 if (!lpfc_vpd_data)
James Smartd7c255b2008-08-24 21:50:00 -0400213 goto out_free_mbox;
dea31012005-04-17 16:05:31 -0500214
215 do {
James Smarta0c87cb2009-07-19 10:01:10 -0400216 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
dea31012005-04-17 16:05:31 -0500217 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
218
219 if (rc != MBX_SUCCESS) {
220 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400221 "0441 VPD not present on adapter, "
dea31012005-04-17 16:05:31 -0500222 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500223 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500224 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500225 }
James Smart04c68492009-05-22 14:52:52 -0400226 /* dump mem may return a zero when finished or we got a
227 * mailbox error, either way we are done.
228 */
229 if (mb->un.varDmp.word_cnt == 0)
230 break;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500231 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
232 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
James Smartd7c255b2008-08-24 21:50:00 -0400233 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
234 lpfc_vpd_data + offset,
James Smart92d7f7b2007-06-17 19:56:38 -0500235 mb->un.varDmp.word_cnt);
dea31012005-04-17 16:05:31 -0500236 offset += mb->un.varDmp.word_cnt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500237 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
238 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500239
240 kfree(lpfc_vpd_data);
dea31012005-04-17 16:05:31 -0500241out_free_mbox:
242 mempool_free(pmb, phba->mbox_mem_pool);
243 return 0;
244}
245
James Smarte59058c2008-08-24 21:49:00 -0400246/**
James Smart3621a712009-04-06 18:47:14 -0400247 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
James Smarte59058c2008-08-24 21:49:00 -0400248 * @phba: pointer to lpfc hba data structure.
249 * @pmboxq: pointer to the driver internal queue element for mailbox command.
250 *
251 * This is the completion handler for driver's configuring asynchronous event
252 * mailbox command to the device. If the mailbox command returns successfully,
253 * it will set internal async event support flag to 1; otherwise, it will
254 * set internal async event support flag to 0.
255 **/
James Smart57127f12007-10-27 13:37:05 -0400256static void
257lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
258{
James Smart04c68492009-05-22 14:52:52 -0400259 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
James Smart57127f12007-10-27 13:37:05 -0400260 phba->temp_sensor_support = 1;
261 else
262 phba->temp_sensor_support = 0;
263 mempool_free(pmboxq, phba->mbox_mem_pool);
264 return;
265}
266
James Smarte59058c2008-08-24 21:49:00 -0400267/**
James Smart3621a712009-04-06 18:47:14 -0400268 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
James Smart97207482008-12-04 22:39:19 -0500269 * @phba: pointer to lpfc hba data structure.
270 * @pmboxq: pointer to the driver internal queue element for mailbox command.
271 *
272 * This is the completion handler for dump mailbox command for getting
273 * wake up parameters. When this command complete, the response contain
274 * Option rom version of the HBA. This function translate the version number
275 * into a human readable string and store it in OptionROMVersion.
276 **/
277static void
278lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
279{
280 struct prog_id *prg;
281 uint32_t prog_id_word;
282 char dist = ' ';
283 /* character array used for decoding dist type. */
284 char dist_char[] = "nabx";
285
James Smart04c68492009-05-22 14:52:52 -0400286 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
James Smart9f1e1b52008-12-04 22:39:40 -0500287 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500288 return;
James Smart9f1e1b52008-12-04 22:39:40 -0500289 }
James Smart97207482008-12-04 22:39:19 -0500290
291 prg = (struct prog_id *) &prog_id_word;
292
293 /* word 7 contain option rom version */
James Smart04c68492009-05-22 14:52:52 -0400294 prog_id_word = pmboxq->u.mb.un.varWords[7];
James Smart97207482008-12-04 22:39:19 -0500295
296 /* Decode the Option rom version word to a readable string */
297 if (prg->dist < 4)
298 dist = dist_char[prg->dist];
299
300 if ((prg->dist == 3) && (prg->num == 0))
301 sprintf(phba->OptionROMVersion, "%d.%d%d",
302 prg->ver, prg->rev, prg->lev);
303 else
304 sprintf(phba->OptionROMVersion, "%d.%d%d%c%d",
305 prg->ver, prg->rev, prg->lev,
306 dist, prg->num);
James Smart9f1e1b52008-12-04 22:39:40 -0500307 mempool_free(pmboxq, phba->mbox_mem_pool);
James Smart97207482008-12-04 22:39:19 -0500308 return;
309}
310
311/**
James Smart3621a712009-04-06 18:47:14 -0400312 * lpfc_config_port_post - Perform lpfc initialization after config port
James Smarte59058c2008-08-24 21:49:00 -0400313 * @phba: pointer to lpfc hba data structure.
314 *
315 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
316 * command call. It performs all internal resource and state setups on the
317 * port: post IOCB buffers, enable appropriate host interrupt attentions,
318 * ELS ring timers, etc.
319 *
320 * Return codes
321 * 0 - success.
322 * Any other value - error.
323 **/
dea31012005-04-17 16:05:31 -0500324int
James Smart2e0fef82007-06-17 19:56:36 -0500325lpfc_config_port_post(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500326{
James Smart2e0fef82007-06-17 19:56:36 -0500327 struct lpfc_vport *vport = phba->pport;
James Smarta257bf92009-04-06 18:48:10 -0400328 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -0500329 LPFC_MBOXQ_t *pmb;
330 MAILBOX_t *mb;
331 struct lpfc_dmabuf *mp;
332 struct lpfc_sli *psli = &phba->sli;
333 uint32_t status, timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500334 int i, j;
335 int rc;
dea31012005-04-17 16:05:31 -0500336
James Smart7af67052007-10-27 13:38:11 -0400337 spin_lock_irq(&phba->hbalock);
338 /*
339 * If the Config port completed correctly the HBA is not
340 * over heated any more.
341 */
342 if (phba->over_temp_state == HBA_OVER_TEMP)
343 phba->over_temp_state = HBA_NORMAL_TEMP;
344 spin_unlock_irq(&phba->hbalock);
345
dea31012005-04-17 16:05:31 -0500346 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
347 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -0500348 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500349 return -ENOMEM;
350 }
James Smart04c68492009-05-22 14:52:52 -0400351 mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500352
dea31012005-04-17 16:05:31 -0500353 /* Get login parameters for NID. */
James Smart9f1177a2010-02-26 14:12:57 -0500354 rc = lpfc_read_sparam(phba, pmb, 0);
355 if (rc) {
356 mempool_free(pmb, phba->mbox_mem_pool);
357 return -ENOMEM;
358 }
359
James Smarted957682007-06-17 19:56:37 -0500360 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500361 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500362 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400363 "0448 Adapter failed init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500364 "READ_SPARM mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500365 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500366 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500367 mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart9f1177a2010-02-26 14:12:57 -0500368 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500369 lpfc_mbuf_free(phba, mp->virt, mp->phys);
370 kfree(mp);
371 return -EIO;
372 }
373
374 mp = (struct lpfc_dmabuf *) pmb->context1;
375
James Smart2e0fef82007-06-17 19:56:36 -0500376 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
dea31012005-04-17 16:05:31 -0500377 lpfc_mbuf_free(phba, mp->virt, mp->phys);
378 kfree(mp);
379 pmb->context1 = NULL;
380
James Smarta12e07b2006-12-02 13:35:30 -0500381 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -0500382 u64_to_wwn(phba->cfg_soft_wwnn,
383 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -0400384 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -0500385 u64_to_wwn(phba->cfg_soft_wwpn,
386 vport->fc_sparam.portName.u.wwn);
387 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
dea31012005-04-17 16:05:31 -0500388 sizeof (struct lpfc_name));
James Smart2e0fef82007-06-17 19:56:36 -0500389 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
dea31012005-04-17 16:05:31 -0500390 sizeof (struct lpfc_name));
James Smarta257bf92009-04-06 18:48:10 -0400391
392 /* Update the fc_host data structures with new wwn. */
393 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
394 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart21e9a0a2009-05-22 14:53:21 -0400395 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smarta257bf92009-04-06 18:48:10 -0400396
dea31012005-04-17 16:05:31 -0500397 /* If no serial number in VPD data, use low 6 bytes of WWNN */
398 /* This should be consolidated into parse_vpd ? - mr */
399 if (phba->SerialNumber[0] == 0) {
400 uint8_t *outptr;
401
James Smart2e0fef82007-06-17 19:56:36 -0500402 outptr = &vport->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500403 for (i = 0; i < 12; i++) {
404 status = *outptr++;
405 j = ((status & 0xf0) >> 4);
406 if (j <= 9)
407 phba->SerialNumber[i] =
408 (char)((uint8_t) 0x30 + (uint8_t) j);
409 else
410 phba->SerialNumber[i] =
411 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
412 i++;
413 j = (status & 0xf);
414 if (j <= 9)
415 phba->SerialNumber[i] =
416 (char)((uint8_t) 0x30 + (uint8_t) j);
417 else
418 phba->SerialNumber[i] =
419 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
420 }
421 }
422
dea31012005-04-17 16:05:31 -0500423 lpfc_read_config(phba, pmb);
James Smarted957682007-06-17 19:56:37 -0500424 pmb->vport = vport;
dea31012005-04-17 16:05:31 -0500425 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
James Smarted957682007-06-17 19:56:37 -0500426 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400427 "0453 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500428 "READ_CONFIG, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500429 mb->mbxCommand, mb->mbxStatus);
James Smart2e0fef82007-06-17 19:56:36 -0500430 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500431 mempool_free( pmb, phba->mbox_mem_pool);
432 return -EIO;
433 }
434
James Smarta0c87cb2009-07-19 10:01:10 -0400435 /* Check if the port is disabled */
436 lpfc_sli_read_link_ste(phba);
437
dea31012005-04-17 16:05:31 -0500438 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
439 if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
440 phba->cfg_hba_queue_depth =
James Smartf1126682009-06-10 17:22:44 -0400441 (mb->un.varRdConfig.max_xri + 1) -
442 lpfc_sli4_get_els_iocb_cnt(phba);
dea31012005-04-17 16:05:31 -0500443
444 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500445
446 /* Get the default values for Model Name and Description */
447 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
448
449 if ((phba->cfg_link_speed > LINK_SPEED_10G)
450 || ((phba->cfg_link_speed == LINK_SPEED_1G)
451 && !(phba->lmt & LMT_1Gb))
452 || ((phba->cfg_link_speed == LINK_SPEED_2G)
453 && !(phba->lmt & LMT_2Gb))
454 || ((phba->cfg_link_speed == LINK_SPEED_4G)
455 && !(phba->lmt & LMT_4Gb))
456 || ((phba->cfg_link_speed == LINK_SPEED_8G)
457 && !(phba->lmt & LMT_8Gb))
458 || ((phba->cfg_link_speed == LINK_SPEED_10G)
459 && !(phba->lmt & LMT_10Gb))) {
460 /* Reset link speed to auto */
James Smarted957682007-06-17 19:56:37 -0500461 lpfc_printf_log(phba, KERN_WARNING, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -0400462 "1302 Invalid speed for this board: "
dea31012005-04-17 16:05:31 -0500463 "Reset link speed to auto: x%x\n",
dea31012005-04-17 16:05:31 -0500464 phba->cfg_link_speed);
465 phba->cfg_link_speed = LINK_SPEED_AUTO;
466 }
467
James Smart2e0fef82007-06-17 19:56:36 -0500468 phba->link_state = LPFC_LINK_DOWN;
dea31012005-04-17 16:05:31 -0500469
James Smart0b727fe2007-10-27 13:37:25 -0400470 /* Only process IOCBs on ELS ring till hba_state is READY */
James Smarta4bc3372006-12-02 13:34:16 -0500471 if (psli->ring[psli->extra_ring].cmdringaddr)
472 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500473 if (psli->ring[psli->fcp_ring].cmdringaddr)
474 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
475 if (psli->ring[psli->next_ring].cmdringaddr)
476 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
477
478 /* Post receive buffers for desired rings */
James Smarted957682007-06-17 19:56:37 -0500479 if (phba->sli_rev != 3)
480 lpfc_post_rcv_buf(phba);
dea31012005-04-17 16:05:31 -0500481
James Smart93996272008-08-24 21:50:30 -0400482 /*
483 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
484 */
485 if (phba->intr_type == MSIX) {
486 rc = lpfc_config_msi(phba, pmb);
487 if (rc) {
488 mempool_free(pmb, phba->mbox_mem_pool);
489 return -EIO;
490 }
491 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
492 if (rc != MBX_SUCCESS) {
493 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
494 "0352 Config MSI mailbox command "
495 "failed, mbxCmd x%x, mbxStatus x%x\n",
James Smart04c68492009-05-22 14:52:52 -0400496 pmb->u.mb.mbxCommand,
497 pmb->u.mb.mbxStatus);
James Smart93996272008-08-24 21:50:30 -0400498 mempool_free(pmb, phba->mbox_mem_pool);
499 return -EIO;
500 }
501 }
502
James Smart04c68492009-05-22 14:52:52 -0400503 spin_lock_irq(&phba->hbalock);
James Smart93996272008-08-24 21:50:30 -0400504 /* Initialize ERATT handling flag */
505 phba->hba_flag &= ~HBA_ERATT_HANDLED;
506
dea31012005-04-17 16:05:31 -0500507 /* Enable appropriate host interrupts */
dea31012005-04-17 16:05:31 -0500508 status = readl(phba->HCregaddr);
509 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
510 if (psli->num_rings > 0)
511 status |= HC_R0INT_ENA;
512 if (psli->num_rings > 1)
513 status |= HC_R1INT_ENA;
514 if (psli->num_rings > 2)
515 status |= HC_R2INT_ENA;
516 if (psli->num_rings > 3)
517 status |= HC_R3INT_ENA;
518
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500519 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
520 (phba->cfg_poll & DISABLE_FCP_RING_INT))
James Smart93996272008-08-24 21:50:30 -0400521 status &= ~(HC_R0INT_ENA);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500522
dea31012005-04-17 16:05:31 -0500523 writel(status, phba->HCregaddr);
524 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -0500525 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500526
James Smart93996272008-08-24 21:50:30 -0400527 /* Set up ring-0 (ELS) timer */
528 timeout = phba->fc_ratov * 2;
James Smart2e0fef82007-06-17 19:56:36 -0500529 mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
James Smart93996272008-08-24 21:50:30 -0400530 /* Set up heart beat (HB) timer */
James Smart858c9f62007-06-17 19:56:39 -0500531 mod_timer(&phba->hb_tmofunc, jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
532 phba->hb_outstanding = 0;
533 phba->last_completion_time = jiffies;
James Smart93996272008-08-24 21:50:30 -0400534 /* Set up error attention (ERATT) polling timer */
535 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
dea31012005-04-17 16:05:31 -0500536
James Smarta0c87cb2009-07-19 10:01:10 -0400537 if (phba->hba_flag & LINK_DISABLED) {
538 lpfc_printf_log(phba,
539 KERN_ERR, LOG_INIT,
540 "2598 Adapter Link is disabled.\n");
541 lpfc_down_link(phba, pmb);
542 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
543 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
544 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
545 lpfc_printf_log(phba,
546 KERN_ERR, LOG_INIT,
547 "2599 Adapter failed to issue DOWN_LINK"
548 " mbox command rc 0x%x\n", rc);
549
550 mempool_free(pmb, phba->mbox_mem_pool);
551 return -EIO;
552 }
James Smarte40a02c2010-02-26 14:13:54 -0500553 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smarta0c87cb2009-07-19 10:01:10 -0400554 lpfc_init_link(phba, pmb, phba->cfg_topology,
555 phba->cfg_link_speed);
556 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
557 lpfc_set_loopback_flag(phba);
558 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
559 if (rc != MBX_SUCCESS) {
560 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400561 "0454 Adapter failed to init, mbxCmd x%x "
dea31012005-04-17 16:05:31 -0500562 "INIT_LINK, mbxStatus x%x\n",
dea31012005-04-17 16:05:31 -0500563 mb->mbxCommand, mb->mbxStatus);
564
James Smarta0c87cb2009-07-19 10:01:10 -0400565 /* Clear all interrupt enable conditions */
566 writel(0, phba->HCregaddr);
567 readl(phba->HCregaddr); /* flush */
568 /* Clear all pending interrupts */
569 writel(0xffffffff, phba->HAregaddr);
570 readl(phba->HAregaddr); /* flush */
dea31012005-04-17 16:05:31 -0500571
James Smarta0c87cb2009-07-19 10:01:10 -0400572 phba->link_state = LPFC_HBA_ERROR;
573 if (rc != MBX_BUSY)
574 mempool_free(pmb, phba->mbox_mem_pool);
575 return -EIO;
576 }
dea31012005-04-17 16:05:31 -0500577 }
578 /* MBOX buffer will be freed in mbox compl */
James Smart57127f12007-10-27 13:37:05 -0400579 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500580 if (!pmb) {
581 phba->link_state = LPFC_HBA_ERROR;
582 return -ENOMEM;
583 }
584
James Smart57127f12007-10-27 13:37:05 -0400585 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
586 pmb->mbox_cmpl = lpfc_config_async_cmpl;
587 pmb->vport = phba->pport;
588 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -0500589
James Smart57127f12007-10-27 13:37:05 -0400590 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
591 lpfc_printf_log(phba,
592 KERN_ERR,
593 LOG_INIT,
594 "0456 Adapter failed to issue "
James Smarte4e74272009-07-19 10:01:38 -0400595 "ASYNCEVT_ENABLE mbox status x%x\n",
James Smart57127f12007-10-27 13:37:05 -0400596 rc);
597 mempool_free(pmb, phba->mbox_mem_pool);
598 }
James Smart97207482008-12-04 22:39:19 -0500599
600 /* Get Option rom version */
601 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart9f1177a2010-02-26 14:12:57 -0500602 if (!pmb) {
603 phba->link_state = LPFC_HBA_ERROR;
604 return -ENOMEM;
605 }
606
James Smart97207482008-12-04 22:39:19 -0500607 lpfc_dump_wakeup_param(phba, pmb);
608 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
609 pmb->vport = phba->pport;
610 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
611
612 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
613 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
James Smarte4e74272009-07-19 10:01:38 -0400614 "to get Option ROM version status x%x\n", rc);
James Smart97207482008-12-04 22:39:19 -0500615 mempool_free(pmb, phba->mbox_mem_pool);
616 }
617
James Smartd7c255b2008-08-24 21:50:00 -0400618 return 0;
James Smartce8b3ce2006-07-06 15:50:36 -0400619}
620
James Smarte59058c2008-08-24 21:49:00 -0400621/**
James Smart84d1b002010-02-12 14:42:33 -0500622 * lpfc_hba_init_link - Initialize the FC link
623 * @phba: pointer to lpfc hba data structure.
James Smart6e7288d2010-06-07 15:23:35 -0400624 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500625 *
626 * This routine will issue the INIT_LINK mailbox command call.
627 * It is available to other drivers through the lpfc_hba data
628 * structure for use as a delayed link up mechanism with the
629 * module parameter lpfc_suppress_link_up.
630 *
631 * Return code
632 * 0 - success
633 * Any other value - error
634 **/
635int
James Smart6e7288d2010-06-07 15:23:35 -0400636lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500637{
638 struct lpfc_vport *vport = phba->pport;
639 LPFC_MBOXQ_t *pmb;
640 MAILBOX_t *mb;
641 int rc;
642
643 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
644 if (!pmb) {
645 phba->link_state = LPFC_HBA_ERROR;
646 return -ENOMEM;
647 }
648 mb = &pmb->u.mb;
649 pmb->vport = vport;
650
651 lpfc_init_link(phba, pmb, phba->cfg_topology,
652 phba->cfg_link_speed);
653 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
654 lpfc_set_loopback_flag(phba);
James Smart6e7288d2010-06-07 15:23:35 -0400655 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart84d1b002010-02-12 14:42:33 -0500656 if (rc != MBX_SUCCESS) {
657 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
658 "0498 Adapter failed to init, mbxCmd x%x "
659 "INIT_LINK, mbxStatus x%x\n",
660 mb->mbxCommand, mb->mbxStatus);
661 /* Clear all interrupt enable conditions */
662 writel(0, phba->HCregaddr);
663 readl(phba->HCregaddr); /* flush */
664 /* Clear all pending interrupts */
665 writel(0xffffffff, phba->HAregaddr);
666 readl(phba->HAregaddr); /* flush */
667 phba->link_state = LPFC_HBA_ERROR;
James Smart6e7288d2010-06-07 15:23:35 -0400668 if (rc != MBX_BUSY || flag == MBX_POLL)
James Smart84d1b002010-02-12 14:42:33 -0500669 mempool_free(pmb, phba->mbox_mem_pool);
670 return -EIO;
671 }
James Smarte40a02c2010-02-26 14:13:54 -0500672 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
James Smart6e7288d2010-06-07 15:23:35 -0400673 if (flag == MBX_POLL)
674 mempool_free(pmb, phba->mbox_mem_pool);
James Smart84d1b002010-02-12 14:42:33 -0500675
676 return 0;
677}
678
679/**
680 * lpfc_hba_down_link - this routine downs the FC link
James Smart6e7288d2010-06-07 15:23:35 -0400681 * @phba: pointer to lpfc hba data structure.
682 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
James Smart84d1b002010-02-12 14:42:33 -0500683 *
684 * This routine will issue the DOWN_LINK mailbox command call.
685 * It is available to other drivers through the lpfc_hba data
686 * structure for use to stop the link.
687 *
688 * Return code
689 * 0 - success
690 * Any other value - error
691 **/
692int
James Smart6e7288d2010-06-07 15:23:35 -0400693lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
James Smart84d1b002010-02-12 14:42:33 -0500694{
695 LPFC_MBOXQ_t *pmb;
696 int rc;
697
698 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
699 if (!pmb) {
700 phba->link_state = LPFC_HBA_ERROR;
701 return -ENOMEM;
702 }
703
704 lpfc_printf_log(phba,
705 KERN_ERR, LOG_INIT,
706 "0491 Adapter Link is disabled.\n");
707 lpfc_down_link(phba, pmb);
708 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6e7288d2010-06-07 15:23:35 -0400709 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
James Smart84d1b002010-02-12 14:42:33 -0500710 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
711 lpfc_printf_log(phba,
712 KERN_ERR, LOG_INIT,
713 "2522 Adapter failed to issue DOWN_LINK"
714 " mbox command rc 0x%x\n", rc);
715
716 mempool_free(pmb, phba->mbox_mem_pool);
717 return -EIO;
718 }
James Smart6e7288d2010-06-07 15:23:35 -0400719 if (flag == MBX_POLL)
720 mempool_free(pmb, phba->mbox_mem_pool);
721
James Smart84d1b002010-02-12 14:42:33 -0500722 return 0;
723}
724
725/**
James Smart3621a712009-04-06 18:47:14 -0400726 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400727 * @phba: pointer to lpfc HBA data structure.
728 *
729 * This routine will do LPFC uninitialization before the HBA is reset when
730 * bringing down the SLI Layer.
731 *
732 * Return codes
733 * 0 - success.
734 * Any other value - error.
735 **/
dea31012005-04-17 16:05:31 -0500736int
James Smart2e0fef82007-06-17 19:56:36 -0500737lpfc_hba_down_prep(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500738{
James Smart1b32f6a2008-02-08 18:49:39 -0500739 struct lpfc_vport **vports;
740 int i;
James Smart3772a992009-05-22 14:50:54 -0400741
742 if (phba->sli_rev <= LPFC_SLI_REV3) {
743 /* Disable interrupts */
744 writel(0, phba->HCregaddr);
745 readl(phba->HCregaddr); /* flush */
746 }
dea31012005-04-17 16:05:31 -0500747
James Smart1b32f6a2008-02-08 18:49:39 -0500748 if (phba->pport->load_flag & FC_UNLOADING)
749 lpfc_cleanup_discovery_resources(phba->pport);
750 else {
751 vports = lpfc_create_vport_work_array(phba);
752 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -0400753 for (i = 0; i <= phba->max_vports &&
754 vports[i] != NULL; i++)
James Smart1b32f6a2008-02-08 18:49:39 -0500755 lpfc_cleanup_discovery_resources(vports[i]);
756 lpfc_destroy_vport_work_array(phba, vports);
James Smart7f5f3d02008-02-08 18:50:14 -0500757 }
758 return 0;
dea31012005-04-17 16:05:31 -0500759}
760
James Smarte59058c2008-08-24 21:49:00 -0400761/**
James Smart3772a992009-05-22 14:50:54 -0400762 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
James Smarte59058c2008-08-24 21:49:00 -0400763 * @phba: pointer to lpfc HBA data structure.
764 *
765 * This routine will do uninitialization after the HBA is reset when bring
766 * down the SLI Layer.
767 *
768 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200769 * 0 - success.
James Smarte59058c2008-08-24 21:49:00 -0400770 * Any other value - error.
771 **/
James Smart3772a992009-05-22 14:50:54 -0400772static int
773lpfc_hba_down_post_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -0500774{
775 struct lpfc_sli *psli = &phba->sli;
776 struct lpfc_sli_ring *pring;
777 struct lpfc_dmabuf *mp, *next_mp;
James Smart09372822008-01-11 01:52:54 -0500778 LIST_HEAD(completions);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500779 int i;
780
James Smart92d7f7b2007-06-17 19:56:38 -0500781 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
782 lpfc_sli_hbqbuf_free_all(phba);
783 else {
784 /* Cleanup preposted buffers on the ELS ring */
785 pring = &psli->ring[LPFC_ELS_RING];
786 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
787 list_del(&mp->list);
788 pring->postbufq_cnt--;
789 lpfc_mbuf_free(phba, mp->virt, mp->phys);
790 kfree(mp);
791 }
Jamie Wellnitz41415862006-02-28 19:25:27 -0500792 }
793
James Smart09372822008-01-11 01:52:54 -0500794 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500795 for (i = 0; i < psli->num_rings; i++) {
796 pring = &psli->ring[i];
James Smart09372822008-01-11 01:52:54 -0500797
798 /* At this point in time the HBA is either reset or DOA. Either
799 * way, nothing should be on txcmplq as it will NEVER complete.
800 */
801 list_splice_init(&pring->txcmplq, &completions);
802 pring->txcmplq_cnt = 0;
803 spin_unlock_irq(&phba->hbalock);
804
James Smarta257bf92009-04-06 18:48:10 -0400805 /* Cancel all the IOCBs from the completions list */
806 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
807 IOERR_SLI_ABORTED);
James Smart09372822008-01-11 01:52:54 -0500808
Jamie Wellnitz41415862006-02-28 19:25:27 -0500809 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart09372822008-01-11 01:52:54 -0500810 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500811 }
James Smart09372822008-01-11 01:52:54 -0500812 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500813
814 return 0;
815}
James Smartda0436e2009-05-22 14:51:39 -0400816/**
817 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
818 * @phba: pointer to lpfc HBA data structure.
819 *
820 * This routine will do uninitialization after the HBA is reset when bring
821 * down the SLI Layer.
822 *
823 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200824 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400825 * Any other value - error.
826 **/
827static int
828lpfc_hba_down_post_s4(struct lpfc_hba *phba)
829{
830 struct lpfc_scsi_buf *psb, *psb_next;
831 LIST_HEAD(aborts);
832 int ret;
833 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500834 struct lpfc_sglq *sglq_entry = NULL;
835
James Smartda0436e2009-05-22 14:51:39 -0400836 ret = lpfc_hba_down_post_s3(phba);
837 if (ret)
838 return ret;
839 /* At this point in time the HBA is either reset or DOA. Either
840 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
841 * on the lpfc_sgl_list so that it can either be freed if the
842 * driver is unloading or reposted if the driver is restarting
843 * the port.
844 */
845 spin_lock_irq(&phba->hbalock); /* required for lpfc_sgl_list and */
846 /* scsl_buf_list */
847 /* abts_sgl_list_lock required because worker thread uses this
848 * list.
849 */
850 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
James Smart0f65ff62010-02-26 14:14:23 -0500851 list_for_each_entry(sglq_entry,
852 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
853 sglq_entry->state = SGL_FREED;
854
James Smartda0436e2009-05-22 14:51:39 -0400855 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
856 &phba->sli4_hba.lpfc_sgl_list);
857 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
858 /* abts_scsi_buf_list_lock required because worker thread uses this
859 * list.
860 */
861 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
862 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
863 &aborts);
864 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
865 spin_unlock_irq(&phba->hbalock);
866
867 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
868 psb->pCmd = NULL;
869 psb->status = IOSTAT_SUCCESS;
870 }
871 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
872 list_splice(&aborts, &phba->lpfc_scsi_buf_list);
873 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
874 return 0;
875}
876
877/**
878 * lpfc_hba_down_post - Wrapper func for hba down post routine
879 * @phba: pointer to lpfc HBA data structure.
880 *
881 * This routine wraps the actual SLI3 or SLI4 routine for performing
882 * uninitialization after the HBA is reset when bring down the SLI Layer.
883 *
884 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200885 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -0400886 * Any other value - error.
887 **/
888int
889lpfc_hba_down_post(struct lpfc_hba *phba)
890{
891 return (*phba->lpfc_hba_down_post)(phba);
892}
Jamie Wellnitz41415862006-02-28 19:25:27 -0500893
James Smarte59058c2008-08-24 21:49:00 -0400894/**
James Smart3621a712009-04-06 18:47:14 -0400895 * lpfc_hb_timeout - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -0400896 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
897 *
898 * This is the HBA-timer timeout handler registered to the lpfc driver. When
899 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
900 * work-port-events bitmap and the worker thread is notified. This timeout
901 * event will be used by the worker thread to invoke the actual timeout
902 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
903 * be performed in the timeout handler and the HBA timeout event bit shall
904 * be cleared by the worker thread after it has taken the event bitmap out.
905 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100906static void
James Smart858c9f62007-06-17 19:56:39 -0500907lpfc_hb_timeout(unsigned long ptr)
908{
909 struct lpfc_hba *phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400910 uint32_t tmo_posted;
James Smart858c9f62007-06-17 19:56:39 -0500911 unsigned long iflag;
912
913 phba = (struct lpfc_hba *)ptr;
James Smart93996272008-08-24 21:50:30 -0400914
915 /* Check for heart beat timeout conditions */
James Smart858c9f62007-06-17 19:56:39 -0500916 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -0400917 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
918 if (!tmo_posted)
James Smart858c9f62007-06-17 19:56:39 -0500919 phba->pport->work_port_events |= WORKER_HB_TMO;
920 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
921
James Smart93996272008-08-24 21:50:30 -0400922 /* Tell the worker thread there is work to do */
James Smart5e9d9b82008-06-14 22:52:53 -0400923 if (!tmo_posted)
924 lpfc_worker_wake_up(phba);
James Smart858c9f62007-06-17 19:56:39 -0500925 return;
926}
927
James Smarte59058c2008-08-24 21:49:00 -0400928/**
James Smart3621a712009-04-06 18:47:14 -0400929 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
James Smarte59058c2008-08-24 21:49:00 -0400930 * @phba: pointer to lpfc hba data structure.
931 * @pmboxq: pointer to the driver internal queue element for mailbox command.
932 *
933 * This is the callback function to the lpfc heart-beat mailbox command.
934 * If configured, the lpfc driver issues the heart-beat mailbox command to
935 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
936 * heart-beat mailbox command is issued, the driver shall set up heart-beat
937 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
938 * heart-beat outstanding state. Once the mailbox command comes back and
939 * no error conditions detected, the heart-beat mailbox command timer is
940 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
941 * state is cleared for the next heart-beat. If the timer expired with the
942 * heart-beat outstanding state set, the driver will put the HBA offline.
943 **/
James Smart858c9f62007-06-17 19:56:39 -0500944static void
945lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
946{
947 unsigned long drvr_flag;
948
949 spin_lock_irqsave(&phba->hbalock, drvr_flag);
950 phba->hb_outstanding = 0;
951 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
952
James Smart93996272008-08-24 21:50:30 -0400953 /* Check and reset heart-beat timer is necessary */
James Smart858c9f62007-06-17 19:56:39 -0500954 mempool_free(pmboxq, phba->mbox_mem_pool);
955 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
956 !(phba->link_state == LPFC_HBA_ERROR) &&
James Smart51ef4c22007-08-02 11:10:31 -0400957 !(phba->pport->load_flag & FC_UNLOADING))
James Smart858c9f62007-06-17 19:56:39 -0500958 mod_timer(&phba->hb_tmofunc,
959 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
960 return;
961}
962
James Smarte59058c2008-08-24 21:49:00 -0400963/**
James Smart3621a712009-04-06 18:47:14 -0400964 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
James Smarte59058c2008-08-24 21:49:00 -0400965 * @phba: pointer to lpfc hba data structure.
966 *
967 * This is the actual HBA-timer timeout handler to be invoked by the worker
968 * thread whenever the HBA timer fired and HBA-timeout event posted. This
969 * handler performs any periodic operations needed for the device. If such
970 * periodic event has already been attended to either in the interrupt handler
971 * or by processing slow-ring or fast-ring events within the HBA-timer
972 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
973 * the timer for the next timeout period. If lpfc heart-beat mailbox command
974 * is configured and there is no heart-beat mailbox command outstanding, a
975 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
976 * has been a heart-beat mailbox command outstanding, the HBA shall be put
977 * to offline.
978 **/
James Smart858c9f62007-06-17 19:56:39 -0500979void
980lpfc_hb_timeout_handler(struct lpfc_hba *phba)
981{
James Smart45ed1192009-10-02 15:17:02 -0400982 struct lpfc_vport **vports;
James Smart858c9f62007-06-17 19:56:39 -0500983 LPFC_MBOXQ_t *pmboxq;
James Smart0ff10d42008-01-11 01:52:36 -0500984 struct lpfc_dmabuf *buf_ptr;
James Smart45ed1192009-10-02 15:17:02 -0400985 int retval, i;
James Smart858c9f62007-06-17 19:56:39 -0500986 struct lpfc_sli *psli = &phba->sli;
James Smart0ff10d42008-01-11 01:52:36 -0500987 LIST_HEAD(completions);
James Smart858c9f62007-06-17 19:56:39 -0500988
James Smart45ed1192009-10-02 15:17:02 -0400989 vports = lpfc_create_vport_work_array(phba);
990 if (vports != NULL)
991 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
992 lpfc_rcv_seq_check_edtov(vports[i]);
993 lpfc_destroy_vport_work_array(phba, vports);
994
James Smart858c9f62007-06-17 19:56:39 -0500995 if ((phba->link_state == LPFC_HBA_ERROR) ||
James Smart51ef4c22007-08-02 11:10:31 -0400996 (phba->pport->load_flag & FC_UNLOADING) ||
James Smart858c9f62007-06-17 19:56:39 -0500997 (phba->pport->fc_flag & FC_OFFLINE_MODE))
998 return;
999
1000 spin_lock_irq(&phba->pport->work_port_lock);
James Smart858c9f62007-06-17 19:56:39 -05001001
1002 if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
1003 jiffies)) {
1004 spin_unlock_irq(&phba->pport->work_port_lock);
1005 if (!phba->hb_outstanding)
1006 mod_timer(&phba->hb_tmofunc,
1007 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1008 else
1009 mod_timer(&phba->hb_tmofunc,
1010 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
1011 return;
1012 }
1013 spin_unlock_irq(&phba->pport->work_port_lock);
1014
James Smart0ff10d42008-01-11 01:52:36 -05001015 if (phba->elsbuf_cnt &&
1016 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1017 spin_lock_irq(&phba->hbalock);
1018 list_splice_init(&phba->elsbuf, &completions);
1019 phba->elsbuf_cnt = 0;
1020 phba->elsbuf_prev_cnt = 0;
1021 spin_unlock_irq(&phba->hbalock);
1022
1023 while (!list_empty(&completions)) {
1024 list_remove_head(&completions, buf_ptr,
1025 struct lpfc_dmabuf, list);
1026 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1027 kfree(buf_ptr);
1028 }
1029 }
1030 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1031
James Smart858c9f62007-06-17 19:56:39 -05001032 /* If there is no heart beat outstanding, issue a heartbeat command */
James Smart13815c82008-01-11 01:52:48 -05001033 if (phba->cfg_enable_hba_heartbeat) {
1034 if (!phba->hb_outstanding) {
1035 pmboxq = mempool_alloc(phba->mbox_mem_pool,GFP_KERNEL);
1036 if (!pmboxq) {
1037 mod_timer(&phba->hb_tmofunc,
1038 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1039 return;
1040 }
1041
1042 lpfc_heart_beat(phba, pmboxq);
1043 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1044 pmboxq->vport = phba->pport;
1045 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
1046
1047 if (retval != MBX_BUSY && retval != MBX_SUCCESS) {
1048 mempool_free(pmboxq, phba->mbox_mem_pool);
1049 mod_timer(&phba->hb_tmofunc,
1050 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1051 return;
1052 }
James Smart858c9f62007-06-17 19:56:39 -05001053 mod_timer(&phba->hb_tmofunc,
James Smart13815c82008-01-11 01:52:48 -05001054 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
1055 phba->hb_outstanding = 1;
James Smart858c9f62007-06-17 19:56:39 -05001056 return;
James Smart13815c82008-01-11 01:52:48 -05001057 } else {
1058 /*
1059 * If heart beat timeout called with hb_outstanding set
1060 * we need to take the HBA offline.
1061 */
1062 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1063 "0459 Adapter heartbeat failure, "
1064 "taking this port offline.\n");
1065
1066 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001067 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart13815c82008-01-11 01:52:48 -05001068 spin_unlock_irq(&phba->hbalock);
1069
1070 lpfc_offline_prep(phba);
1071 lpfc_offline(phba);
1072 lpfc_unblock_mgmt_io(phba);
1073 phba->link_state = LPFC_HBA_ERROR;
1074 lpfc_hba_down_post(phba);
James Smart858c9f62007-06-17 19:56:39 -05001075 }
James Smart858c9f62007-06-17 19:56:39 -05001076 }
1077}
1078
James Smarte59058c2008-08-24 21:49:00 -04001079/**
James Smart3621a712009-04-06 18:47:14 -04001080 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
James Smarte59058c2008-08-24 21:49:00 -04001081 * @phba: pointer to lpfc hba data structure.
1082 *
1083 * This routine is called to bring the HBA offline when HBA hardware error
1084 * other than Port Error 6 has been detected.
1085 **/
James Smart09372822008-01-11 01:52:54 -05001086static void
1087lpfc_offline_eratt(struct lpfc_hba *phba)
1088{
1089 struct lpfc_sli *psli = &phba->sli;
1090
1091 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001092 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart09372822008-01-11 01:52:54 -05001093 spin_unlock_irq(&phba->hbalock);
1094 lpfc_offline_prep(phba);
1095
1096 lpfc_offline(phba);
1097 lpfc_reset_barrier(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001098 spin_lock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001099 lpfc_sli_brdreset(phba);
James Smartf4b4c682009-05-22 14:53:12 -04001100 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001101 lpfc_hba_down_post(phba);
1102 lpfc_sli_brdready(phba, HS_MBRDY);
1103 lpfc_unblock_mgmt_io(phba);
1104 phba->link_state = LPFC_HBA_ERROR;
1105 return;
1106}
1107
James Smarte59058c2008-08-24 21:49:00 -04001108/**
James Smartda0436e2009-05-22 14:51:39 -04001109 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1110 * @phba: pointer to lpfc hba data structure.
1111 *
1112 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1113 * other than Port Error 6 has been detected.
1114 **/
1115static void
1116lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1117{
1118 lpfc_offline_prep(phba);
1119 lpfc_offline(phba);
1120 lpfc_sli4_brdreset(phba);
1121 lpfc_hba_down_post(phba);
1122 lpfc_sli4_post_status_check(phba);
1123 lpfc_unblock_mgmt_io(phba);
1124 phba->link_state = LPFC_HBA_ERROR;
1125}
1126
1127/**
James Smarta257bf92009-04-06 18:48:10 -04001128 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1129 * @phba: pointer to lpfc hba data structure.
1130 *
1131 * This routine is invoked to handle the deferred HBA hardware error
1132 * conditions. This type of error is indicated by HBA by setting ER1
1133 * and another ER bit in the host status register. The driver will
1134 * wait until the ER1 bit clears before handling the error condition.
1135 **/
1136static void
1137lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1138{
1139 uint32_t old_host_status = phba->work_hs;
1140 struct lpfc_sli_ring *pring;
1141 struct lpfc_sli *psli = &phba->sli;
1142
James Smartf4b4c682009-05-22 14:53:12 -04001143 /* If the pci channel is offline, ignore possible errors,
1144 * since we cannot communicate with the pci card anyway.
1145 */
1146 if (pci_channel_offline(phba->pcidev)) {
1147 spin_lock_irq(&phba->hbalock);
1148 phba->hba_flag &= ~DEFER_ERATT;
1149 spin_unlock_irq(&phba->hbalock);
1150 return;
1151 }
1152
James Smarta257bf92009-04-06 18:48:10 -04001153 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1154 "0479 Deferred Adapter Hardware Error "
1155 "Data: x%x x%x x%x\n",
1156 phba->work_hs,
1157 phba->work_status[0], phba->work_status[1]);
1158
1159 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001160 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smarta257bf92009-04-06 18:48:10 -04001161 spin_unlock_irq(&phba->hbalock);
1162
1163
1164 /*
1165 * Firmware stops when it triggred erratt. That could cause the I/Os
1166 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1167 * SCSI layer retry it after re-establishing link.
1168 */
1169 pring = &psli->ring[psli->fcp_ring];
1170 lpfc_sli_abort_iocb_ring(phba, pring);
1171
1172 /*
1173 * There was a firmware error. Take the hba offline and then
1174 * attempt to restart it.
1175 */
1176 lpfc_offline_prep(phba);
1177 lpfc_offline(phba);
1178
1179 /* Wait for the ER1 bit to clear.*/
1180 while (phba->work_hs & HS_FFER1) {
1181 msleep(100);
1182 phba->work_hs = readl(phba->HSregaddr);
1183 /* If driver is unloading let the worker thread continue */
1184 if (phba->pport->load_flag & FC_UNLOADING) {
1185 phba->work_hs = 0;
1186 break;
1187 }
1188 }
1189
1190 /*
1191 * This is to ptrotect against a race condition in which
1192 * first write to the host attention register clear the
1193 * host status register.
1194 */
1195 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1196 phba->work_hs = old_host_status & ~HS_FFER1;
1197
James Smart3772a992009-05-22 14:50:54 -04001198 spin_lock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001199 phba->hba_flag &= ~DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04001200 spin_unlock_irq(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -04001201 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1202 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1203}
1204
James Smart3772a992009-05-22 14:50:54 -04001205static void
1206lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1207{
1208 struct lpfc_board_event_header board_event;
1209 struct Scsi_Host *shost;
1210
1211 board_event.event_type = FC_REG_BOARD_EVENT;
1212 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1213 shost = lpfc_shost_from_vport(phba->pport);
1214 fc_host_post_vendor_event(shost, fc_get_event_number(),
1215 sizeof(board_event),
1216 (char *) &board_event,
1217 LPFC_NL_VENDOR_ID);
1218}
1219
James Smarta257bf92009-04-06 18:48:10 -04001220/**
James Smart3772a992009-05-22 14:50:54 -04001221 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
James Smarte59058c2008-08-24 21:49:00 -04001222 * @phba: pointer to lpfc hba data structure.
1223 *
1224 * This routine is invoked to handle the following HBA hardware error
1225 * conditions:
1226 * 1 - HBA error attention interrupt
1227 * 2 - DMA ring index out of range
1228 * 3 - Mailbox command came back as unknown
1229 **/
James Smart3772a992009-05-22 14:50:54 -04001230static void
1231lpfc_handle_eratt_s3(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001232{
James Smart2e0fef82007-06-17 19:56:36 -05001233 struct lpfc_vport *vport = phba->pport;
James Smart2e0fef82007-06-17 19:56:36 -05001234 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001235 struct lpfc_sli_ring *pring;
James Smartd2873e42006-08-18 17:46:43 -04001236 uint32_t event_data;
James Smart57127f12007-10-27 13:37:05 -04001237 unsigned long temperature;
1238 struct temp_event temp_event_data;
James Smart92d7f7b2007-06-17 19:56:38 -05001239 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05001240
Linas Vepstas8d63f372007-02-14 14:28:36 -06001241 /* If the pci channel is offline, ignore possible errors,
James Smart3772a992009-05-22 14:50:54 -04001242 * since we cannot communicate with the pci card anyway.
1243 */
1244 if (pci_channel_offline(phba->pcidev)) {
1245 spin_lock_irq(&phba->hbalock);
1246 phba->hba_flag &= ~DEFER_ERATT;
1247 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06001248 return;
James Smart3772a992009-05-22 14:50:54 -04001249 }
1250
James Smart13815c82008-01-11 01:52:48 -05001251 /* If resets are disabled then leave the HBA alone and return */
1252 if (!phba->cfg_enable_hba_reset)
1253 return;
dea31012005-04-17 16:05:31 -05001254
James Smartea2151b2008-09-07 11:52:10 -04001255 /* Send an internal error event to mgmt application */
James Smart3772a992009-05-22 14:50:54 -04001256 lpfc_board_errevt_to_mgmt(phba);
James Smartea2151b2008-09-07 11:52:10 -04001257
James Smarta257bf92009-04-06 18:48:10 -04001258 if (phba->hba_flag & DEFER_ERATT)
1259 lpfc_handle_deferred_eratt(phba);
1260
James Smart97eab632008-04-07 10:16:05 -04001261 if (phba->work_hs & HS_FFER6) {
dea31012005-04-17 16:05:31 -05001262 /* Re-establishing Link */
1263 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001264 "1301 Re-establishing Link "
dea31012005-04-17 16:05:31 -05001265 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001266 phba->work_hs,
dea31012005-04-17 16:05:31 -05001267 phba->work_status[0], phba->work_status[1]);
James Smart58da1ff2008-04-07 10:15:56 -04001268
James Smart92d7f7b2007-06-17 19:56:38 -05001269 spin_lock_irq(&phba->hbalock);
James Smartf4b4c682009-05-22 14:53:12 -04001270 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05001271 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001272
1273 /*
1274 * Firmware stops when it triggled erratt with HS_FFER6.
1275 * That could cause the I/Os dropped by the firmware.
1276 * Error iocb (I/O) on txcmplq and let the SCSI layer
1277 * retry it after re-establishing link.
1278 */
1279 pring = &psli->ring[psli->fcp_ring];
1280 lpfc_sli_abort_iocb_ring(phba, pring);
1281
dea31012005-04-17 16:05:31 -05001282 /*
1283 * There was a firmware error. Take the hba offline and then
1284 * attempt to restart it.
1285 */
James Smart46fa3112007-04-25 09:51:45 -04001286 lpfc_offline_prep(phba);
dea31012005-04-17 16:05:31 -05001287 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001288 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001289 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
James Smart46fa3112007-04-25 09:51:45 -04001290 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05001291 return;
1292 }
James Smart46fa3112007-04-25 09:51:45 -04001293 lpfc_unblock_mgmt_io(phba);
James Smart57127f12007-10-27 13:37:05 -04001294 } else if (phba->work_hs & HS_CRIT_TEMP) {
1295 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1296 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1297 temp_event_data.event_code = LPFC_CRIT_TEMP;
1298 temp_event_data.data = (uint32_t)temperature;
1299
1300 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04001301 "0406 Adapter maximum temperature exceeded "
James Smart57127f12007-10-27 13:37:05 -04001302 "(%ld), taking this port offline "
1303 "Data: x%x x%x x%x\n",
1304 temperature, phba->work_hs,
1305 phba->work_status[0], phba->work_status[1]);
1306
1307 shost = lpfc_shost_from_vport(phba->pport);
1308 fc_host_post_vendor_event(shost, fc_get_event_number(),
1309 sizeof(temp_event_data),
1310 (char *) &temp_event_data,
1311 SCSI_NL_VID_TYPE_PCI
1312 | PCI_VENDOR_ID_EMULEX);
1313
James Smart7af67052007-10-27 13:38:11 -04001314 spin_lock_irq(&phba->hbalock);
James Smart7af67052007-10-27 13:38:11 -04001315 phba->over_temp_state = HBA_OVER_TEMP;
1316 spin_unlock_irq(&phba->hbalock);
James Smart09372822008-01-11 01:52:54 -05001317 lpfc_offline_eratt(phba);
James Smart57127f12007-10-27 13:37:05 -04001318
dea31012005-04-17 16:05:31 -05001319 } else {
1320 /* The if clause above forces this code path when the status
James Smart93996272008-08-24 21:50:30 -04001321 * failure is a value other than FFER6. Do not call the offline
1322 * twice. This is the adapter hardware error path.
dea31012005-04-17 16:05:31 -05001323 */
1324 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001325 "0457 Adapter Hardware Error "
dea31012005-04-17 16:05:31 -05001326 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04001327 phba->work_hs,
dea31012005-04-17 16:05:31 -05001328 phba->work_status[0], phba->work_status[1]);
1329
James Smartd2873e42006-08-18 17:46:43 -04001330 event_data = FC_REG_DUMP_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05001331 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05001332 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smartd2873e42006-08-18 17:46:43 -04001333 sizeof(event_data), (char *) &event_data,
1334 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1335
James Smart09372822008-01-11 01:52:54 -05001336 lpfc_offline_eratt(phba);
dea31012005-04-17 16:05:31 -05001337 }
James Smart93996272008-08-24 21:50:30 -04001338 return;
dea31012005-04-17 16:05:31 -05001339}
1340
James Smarte59058c2008-08-24 21:49:00 -04001341/**
James Smartda0436e2009-05-22 14:51:39 -04001342 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1343 * @phba: pointer to lpfc hba data structure.
1344 *
1345 * This routine is invoked to handle the SLI4 HBA hardware error attention
1346 * conditions.
1347 **/
1348static void
1349lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1350{
1351 struct lpfc_vport *vport = phba->pport;
1352 uint32_t event_data;
1353 struct Scsi_Host *shost;
1354
1355 /* If the pci channel is offline, ignore possible errors, since
1356 * we cannot communicate with the pci card anyway.
1357 */
1358 if (pci_channel_offline(phba->pcidev))
1359 return;
1360 /* If resets are disabled then leave the HBA alone and return */
1361 if (!phba->cfg_enable_hba_reset)
1362 return;
1363
1364 /* Send an internal error event to mgmt application */
1365 lpfc_board_errevt_to_mgmt(phba);
1366
1367 /* For now, the actual action for SLI4 device handling is not
1368 * specified yet, just treated it as adaptor hardware failure
1369 */
1370 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1371 "0143 SLI4 Adapter Hardware Error Data: x%x x%x\n",
1372 phba->work_status[0], phba->work_status[1]);
1373
1374 event_data = FC_REG_DUMP_EVENT;
1375 shost = lpfc_shost_from_vport(vport);
1376 fc_host_post_vendor_event(shost, fc_get_event_number(),
1377 sizeof(event_data), (char *) &event_data,
1378 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1379
1380 lpfc_sli4_offline_eratt(phba);
1381}
1382
1383/**
1384 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1385 * @phba: pointer to lpfc HBA data structure.
1386 *
1387 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1388 * routine from the API jump table function pointer from the lpfc_hba struct.
1389 *
1390 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001391 * 0 - success.
James Smartda0436e2009-05-22 14:51:39 -04001392 * Any other value - error.
1393 **/
1394void
1395lpfc_handle_eratt(struct lpfc_hba *phba)
1396{
1397 (*phba->lpfc_handle_eratt)(phba);
1398}
1399
1400/**
James Smart3621a712009-04-06 18:47:14 -04001401 * lpfc_handle_latt - The HBA link event handler
James Smarte59058c2008-08-24 21:49:00 -04001402 * @phba: pointer to lpfc hba data structure.
1403 *
1404 * This routine is invoked from the worker thread to handle a HBA host
1405 * attention link event.
1406 **/
dea31012005-04-17 16:05:31 -05001407void
James Smart2e0fef82007-06-17 19:56:36 -05001408lpfc_handle_latt(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001409{
James Smart2e0fef82007-06-17 19:56:36 -05001410 struct lpfc_vport *vport = phba->pport;
1411 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001412 LPFC_MBOXQ_t *pmb;
1413 volatile uint32_t control;
1414 struct lpfc_dmabuf *mp;
James Smart09372822008-01-11 01:52:54 -05001415 int rc = 0;
dea31012005-04-17 16:05:31 -05001416
1417 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001418 if (!pmb) {
1419 rc = 1;
dea31012005-04-17 16:05:31 -05001420 goto lpfc_handle_latt_err_exit;
James Smart09372822008-01-11 01:52:54 -05001421 }
dea31012005-04-17 16:05:31 -05001422
1423 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
James Smart09372822008-01-11 01:52:54 -05001424 if (!mp) {
1425 rc = 2;
dea31012005-04-17 16:05:31 -05001426 goto lpfc_handle_latt_free_pmb;
James Smart09372822008-01-11 01:52:54 -05001427 }
dea31012005-04-17 16:05:31 -05001428
1429 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
James Smart09372822008-01-11 01:52:54 -05001430 if (!mp->virt) {
1431 rc = 3;
dea31012005-04-17 16:05:31 -05001432 goto lpfc_handle_latt_free_mp;
James Smart09372822008-01-11 01:52:54 -05001433 }
dea31012005-04-17 16:05:31 -05001434
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -05001435 /* Cleanup any outstanding ELS commands */
James Smart549e55c2007-08-02 11:09:51 -04001436 lpfc_els_flush_all_cmd(phba);
dea31012005-04-17 16:05:31 -05001437
1438 psli->slistat.link_event++;
1439 lpfc_read_la(phba, pmb, mp);
1440 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_la;
James Smart2e0fef82007-06-17 19:56:36 -05001441 pmb->vport = vport;
James Smart0d2b6b82008-06-14 22:52:47 -04001442 /* Block ELS IOCBs until we have processed this mbox command */
1443 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
James Smart0b727fe2007-10-27 13:37:25 -04001444 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
James Smart09372822008-01-11 01:52:54 -05001445 if (rc == MBX_NOT_FINISHED) {
1446 rc = 4;
James Smart14691152006-12-02 13:34:28 -05001447 goto lpfc_handle_latt_free_mbuf;
James Smart09372822008-01-11 01:52:54 -05001448 }
dea31012005-04-17 16:05:31 -05001449
1450 /* Clear Link Attention in HA REG */
James Smart2e0fef82007-06-17 19:56:36 -05001451 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001452 writel(HA_LATT, phba->HAregaddr);
1453 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001454 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001455
1456 return;
1457
James Smart14691152006-12-02 13:34:28 -05001458lpfc_handle_latt_free_mbuf:
James Smart0d2b6b82008-06-14 22:52:47 -04001459 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart14691152006-12-02 13:34:28 -05001460 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05001461lpfc_handle_latt_free_mp:
1462 kfree(mp);
1463lpfc_handle_latt_free_pmb:
James Smart1dcb58e2007-04-25 09:51:30 -04001464 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001465lpfc_handle_latt_err_exit:
1466 /* Enable Link attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001467 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001468 psli->sli_flag |= LPFC_PROCESS_LA;
1469 control = readl(phba->HCregaddr);
1470 control |= HC_LAINT_ENA;
1471 writel(control, phba->HCregaddr);
1472 readl(phba->HCregaddr); /* flush */
1473
1474 /* Clear Link Attention in HA REG */
1475 writel(HA_LATT, phba->HAregaddr);
1476 readl(phba->HAregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001477 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001478 lpfc_linkdown(phba);
James Smart2e0fef82007-06-17 19:56:36 -05001479 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001480
James Smart09372822008-01-11 01:52:54 -05001481 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1482 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
dea31012005-04-17 16:05:31 -05001483
1484 return;
1485}
1486
James Smarte59058c2008-08-24 21:49:00 -04001487/**
James Smart3621a712009-04-06 18:47:14 -04001488 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
James Smarte59058c2008-08-24 21:49:00 -04001489 * @phba: pointer to lpfc hba data structure.
1490 * @vpd: pointer to the vital product data.
1491 * @len: length of the vital product data in bytes.
1492 *
1493 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1494 * an array of characters. In this routine, the ModelName, ProgramType, and
1495 * ModelDesc, etc. fields of the phba data structure will be populated.
1496 *
1497 * Return codes
1498 * 0 - pointer to the VPD passed in is NULL
1499 * 1 - success
1500 **/
James Smart3772a992009-05-22 14:50:54 -04001501int
James Smart2e0fef82007-06-17 19:56:36 -05001502lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
dea31012005-04-17 16:05:31 -05001503{
1504 uint8_t lenlo, lenhi;
Anton Blanchard07da60c2007-03-21 08:41:47 -05001505 int Length;
dea31012005-04-17 16:05:31 -05001506 int i, j;
1507 int finished = 0;
1508 int index = 0;
1509
1510 if (!vpd)
1511 return 0;
1512
1513 /* Vital Product */
James Smarted957682007-06-17 19:56:37 -05001514 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001515 "0455 Vital Product Data: x%x x%x x%x x%x\n",
dea31012005-04-17 16:05:31 -05001516 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1517 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001518 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -05001519 switch (vpd[index]) {
1520 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001521 case 0x91:
dea31012005-04-17 16:05:31 -05001522 index += 1;
1523 lenlo = vpd[index];
1524 index += 1;
1525 lenhi = vpd[index];
1526 index += 1;
1527 i = ((((unsigned short)lenhi) << 8) + lenlo);
1528 index += i;
1529 break;
1530 case 0x90:
1531 index += 1;
1532 lenlo = vpd[index];
1533 index += 1;
1534 lenhi = vpd[index];
1535 index += 1;
1536 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001537 if (Length > len - index)
1538 Length = len - index;
dea31012005-04-17 16:05:31 -05001539 while (Length > 0) {
1540 /* Look for Serial Number */
1541 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1542 index += 2;
1543 i = vpd[index];
1544 index += 1;
1545 j = 0;
1546 Length -= (3+i);
1547 while(i--) {
1548 phba->SerialNumber[j++] = vpd[index++];
1549 if (j == 31)
1550 break;
1551 }
1552 phba->SerialNumber[j] = 0;
1553 continue;
1554 }
1555 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1556 phba->vpd_flag |= VPD_MODEL_DESC;
1557 index += 2;
1558 i = vpd[index];
1559 index += 1;
1560 j = 0;
1561 Length -= (3+i);
1562 while(i--) {
1563 phba->ModelDesc[j++] = vpd[index++];
1564 if (j == 255)
1565 break;
1566 }
1567 phba->ModelDesc[j] = 0;
1568 continue;
1569 }
1570 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1571 phba->vpd_flag |= VPD_MODEL_NAME;
1572 index += 2;
1573 i = vpd[index];
1574 index += 1;
1575 j = 0;
1576 Length -= (3+i);
1577 while(i--) {
1578 phba->ModelName[j++] = vpd[index++];
1579 if (j == 79)
1580 break;
1581 }
1582 phba->ModelName[j] = 0;
1583 continue;
1584 }
1585 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1586 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1587 index += 2;
1588 i = vpd[index];
1589 index += 1;
1590 j = 0;
1591 Length -= (3+i);
1592 while(i--) {
1593 phba->ProgramType[j++] = vpd[index++];
1594 if (j == 255)
1595 break;
1596 }
1597 phba->ProgramType[j] = 0;
1598 continue;
1599 }
1600 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1601 phba->vpd_flag |= VPD_PORT;
1602 index += 2;
1603 i = vpd[index];
1604 index += 1;
1605 j = 0;
1606 Length -= (3+i);
1607 while(i--) {
1608 phba->Port[j++] = vpd[index++];
1609 if (j == 19)
1610 break;
1611 }
1612 phba->Port[j] = 0;
1613 continue;
1614 }
1615 else {
1616 index += 2;
1617 i = vpd[index];
1618 index += 1;
1619 index += i;
1620 Length -= (3 + i);
1621 }
1622 }
1623 finished = 0;
1624 break;
1625 case 0x78:
1626 finished = 1;
1627 break;
1628 default:
1629 index ++;
1630 break;
1631 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001632 }
dea31012005-04-17 16:05:31 -05001633
1634 return(1);
1635}
1636
James Smarte59058c2008-08-24 21:49:00 -04001637/**
James Smart3621a712009-04-06 18:47:14 -04001638 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
James Smarte59058c2008-08-24 21:49:00 -04001639 * @phba: pointer to lpfc hba data structure.
1640 * @mdp: pointer to the data structure to hold the derived model name.
1641 * @descp: pointer to the data structure to hold the derived description.
1642 *
1643 * This routine retrieves HBA's description based on its registered PCI device
1644 * ID. The @descp passed into this function points to an array of 256 chars. It
1645 * shall be returned with the model name, maximum speed, and the host bus type.
1646 * The @mdp passed into this function points to an array of 80 chars. When the
1647 * function returns, the @mdp will be filled with the model name.
1648 **/
dea31012005-04-17 16:05:31 -05001649static void
James Smart2e0fef82007-06-17 19:56:36 -05001650lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
dea31012005-04-17 16:05:31 -05001651{
1652 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -05001653 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001654 int max_speed;
James Smart84774a42008-08-24 21:50:06 -04001655 int GE = 0;
James Smartda0436e2009-05-22 14:51:39 -04001656 int oneConnect = 0; /* default is not a oneConnect */
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001657 struct {
James Smarta747c9c2009-11-18 15:41:10 -05001658 char *name;
1659 char *bus;
1660 char *function;
1661 } m = {"<Unknown>", "", ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001662
1663 if (mdp && mdp[0] != '\0'
1664 && descp && descp[0] != '\0')
1665 return;
1666
1667 if (phba->lmt & LMT_10Gb)
1668 max_speed = 10;
1669 else if (phba->lmt & LMT_8Gb)
1670 max_speed = 8;
1671 else if (phba->lmt & LMT_4Gb)
1672 max_speed = 4;
1673 else if (phba->lmt & LMT_2Gb)
1674 max_speed = 2;
1675 else
1676 max_speed = 1;
dea31012005-04-17 16:05:31 -05001677
1678 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -05001679
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001680 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001681 case PCI_DEVICE_ID_FIREFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001682 m = (typeof(m)){"LP6000", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001683 break;
dea31012005-04-17 16:05:31 -05001684 case PCI_DEVICE_ID_SUPERFLY:
1685 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smarta747c9c2009-11-18 15:41:10 -05001686 m = (typeof(m)){"LP7000", "PCI",
1687 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001688 else
James Smarta747c9c2009-11-18 15:41:10 -05001689 m = (typeof(m)){"LP7000E", "PCI",
1690 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001691 break;
1692 case PCI_DEVICE_ID_DRAGONFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001693 m = (typeof(m)){"LP8000", "PCI",
1694 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001695 break;
1696 case PCI_DEVICE_ID_CENTAUR:
1697 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smarta747c9c2009-11-18 15:41:10 -05001698 m = (typeof(m)){"LP9002", "PCI",
1699 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001700 else
James Smarta747c9c2009-11-18 15:41:10 -05001701 m = (typeof(m)){"LP9000", "PCI",
1702 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001703 break;
1704 case PCI_DEVICE_ID_RFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001705 m = (typeof(m)){"LP952", "PCI",
1706 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001707 break;
1708 case PCI_DEVICE_ID_PEGASUS:
James Smarta747c9c2009-11-18 15:41:10 -05001709 m = (typeof(m)){"LP9802", "PCI-X",
1710 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001711 break;
1712 case PCI_DEVICE_ID_THOR:
James Smarta747c9c2009-11-18 15:41:10 -05001713 m = (typeof(m)){"LP10000", "PCI-X",
1714 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001715 break;
1716 case PCI_DEVICE_ID_VIPER:
James Smarta747c9c2009-11-18 15:41:10 -05001717 m = (typeof(m)){"LPX1000", "PCI-X",
1718 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001719 break;
1720 case PCI_DEVICE_ID_PFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001721 m = (typeof(m)){"LP982", "PCI-X",
1722 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001723 break;
1724 case PCI_DEVICE_ID_TFLY:
James Smarta747c9c2009-11-18 15:41:10 -05001725 m = (typeof(m)){"LP1050", "PCI-X",
1726 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001727 break;
1728 case PCI_DEVICE_ID_HELIOS:
James Smarta747c9c2009-11-18 15:41:10 -05001729 m = (typeof(m)){"LP11000", "PCI-X2",
1730 "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001731 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001732 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001733 m = (typeof(m)){"LP11000-SP", "PCI-X2",
1734 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001735 break;
1736 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001737 m = (typeof(m)){"LP11002-SP", "PCI-X2",
1738 "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001739 break;
1740 case PCI_DEVICE_ID_NEPTUNE:
James Smarta747c9c2009-11-18 15:41:10 -05001741 m = (typeof(m)){"LPe1000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001742 break;
1743 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001744 m = (typeof(m)){"LPe1000-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001745 break;
1746 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001747 m = (typeof(m)){"LPe1002-SP", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001748 break;
dea31012005-04-17 16:05:31 -05001749 case PCI_DEVICE_ID_BMID:
James Smarta747c9c2009-11-18 15:41:10 -05001750 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001751 break;
1752 case PCI_DEVICE_ID_BSMB:
James Smarta747c9c2009-11-18 15:41:10 -05001753 m = (typeof(m)){"LP111", "PCI-X2", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001754 break;
1755 case PCI_DEVICE_ID_ZEPHYR:
James Smarta747c9c2009-11-18 15:41:10 -05001756 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001757 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001758 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001759 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001760 break;
1761 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001762 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
James Smarta257bf92009-04-06 18:48:10 -04001763 GE = 1;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001764 break;
dea31012005-04-17 16:05:31 -05001765 case PCI_DEVICE_ID_ZMID:
James Smarta747c9c2009-11-18 15:41:10 -05001766 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001767 break;
1768 case PCI_DEVICE_ID_ZSMB:
James Smarta747c9c2009-11-18 15:41:10 -05001769 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001770 break;
1771 case PCI_DEVICE_ID_LP101:
James Smarta747c9c2009-11-18 15:41:10 -05001772 m = (typeof(m)){"LP101", "PCI-X", "Fibre Channel Adapter"};
dea31012005-04-17 16:05:31 -05001773 break;
1774 case PCI_DEVICE_ID_LP10000S:
James Smarta747c9c2009-11-18 15:41:10 -05001775 m = (typeof(m)){"LP10000-S", "PCI", "Fibre Channel Adapter"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001776 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001777 case PCI_DEVICE_ID_LP11000S:
James Smarta747c9c2009-11-18 15:41:10 -05001778 m = (typeof(m)){"LP11000-S", "PCI-X2", "Fibre Channel Adapter"};
James Smart18a3b592006-12-02 13:35:08 -05001779 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001780 case PCI_DEVICE_ID_LPE11000S:
James Smarta747c9c2009-11-18 15:41:10 -05001781 m = (typeof(m)){"LPe11000-S", "PCIe", "Fibre Channel Adapter"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05001782 break;
James Smartb87eab32007-04-25 09:53:28 -04001783 case PCI_DEVICE_ID_SAT:
James Smarta747c9c2009-11-18 15:41:10 -05001784 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001785 break;
1786 case PCI_DEVICE_ID_SAT_MID:
James Smarta747c9c2009-11-18 15:41:10 -05001787 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001788 break;
1789 case PCI_DEVICE_ID_SAT_SMB:
James Smarta747c9c2009-11-18 15:41:10 -05001790 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001791 break;
1792 case PCI_DEVICE_ID_SAT_DCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001793 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001794 break;
1795 case PCI_DEVICE_ID_SAT_SCSP:
James Smarta747c9c2009-11-18 15:41:10 -05001796 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001797 break;
1798 case PCI_DEVICE_ID_SAT_S:
James Smarta747c9c2009-11-18 15:41:10 -05001799 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
James Smartb87eab32007-04-25 09:53:28 -04001800 break;
James Smart84774a42008-08-24 21:50:06 -04001801 case PCI_DEVICE_ID_HORNET:
James Smarta747c9c2009-11-18 15:41:10 -05001802 m = (typeof(m)){"LP21000", "PCIe", "FCoE Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001803 GE = 1;
1804 break;
1805 case PCI_DEVICE_ID_PROTEUS_VF:
James Smarta747c9c2009-11-18 15:41:10 -05001806 m = (typeof(m)){"LPev12000", "PCIe IOV",
1807 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001808 break;
1809 case PCI_DEVICE_ID_PROTEUS_PF:
James Smarta747c9c2009-11-18 15:41:10 -05001810 m = (typeof(m)){"LPev12000", "PCIe IOV",
1811 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001812 break;
1813 case PCI_DEVICE_ID_PROTEUS_S:
James Smarta747c9c2009-11-18 15:41:10 -05001814 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
1815 "Fibre Channel Adapter"};
James Smart84774a42008-08-24 21:50:06 -04001816 break;
James Smartda0436e2009-05-22 14:51:39 -04001817 case PCI_DEVICE_ID_TIGERSHARK:
1818 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05001819 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
James Smartda0436e2009-05-22 14:51:39 -04001820 break;
James Smarta747c9c2009-11-18 15:41:10 -05001821 case PCI_DEVICE_ID_TOMCAT:
James Smart6669f9b2009-10-02 15:16:45 -04001822 oneConnect = 1;
James Smarta747c9c2009-11-18 15:41:10 -05001823 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
1824 break;
1825 case PCI_DEVICE_ID_FALCON:
1826 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
1827 "EmulexSecure Fibre"};
James Smart6669f9b2009-10-02 15:16:45 -04001828 break;
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -05001829 default:
James Smarta747c9c2009-11-18 15:41:10 -05001830 m = (typeof(m)){"Unknown", "", ""};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001831 break;
dea31012005-04-17 16:05:31 -05001832 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001833
1834 if (mdp && mdp[0] == '\0')
1835 snprintf(mdp, 79,"%s", m.name);
James Smartda0436e2009-05-22 14:51:39 -04001836 /* oneConnect hba requires special processing, they are all initiators
1837 * and we put the port number on the end
1838 */
1839 if (descp && descp[0] == '\0') {
1840 if (oneConnect)
1841 snprintf(descp, 255,
James Smarta747c9c2009-11-18 15:41:10 -05001842 "Emulex OneConnect %s, %s Initiator, Port %s",
1843 m.name, m.function,
James Smartda0436e2009-05-22 14:51:39 -04001844 phba->Port);
1845 else
1846 snprintf(descp, 255,
1847 "Emulex %s %d%s %s %s",
James Smarta747c9c2009-11-18 15:41:10 -05001848 m.name, max_speed, (GE) ? "GE" : "Gb",
1849 m.bus, m.function);
James Smartda0436e2009-05-22 14:51:39 -04001850 }
dea31012005-04-17 16:05:31 -05001851}
1852
James Smarte59058c2008-08-24 21:49:00 -04001853/**
James Smart3621a712009-04-06 18:47:14 -04001854 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
James Smarte59058c2008-08-24 21:49:00 -04001855 * @phba: pointer to lpfc hba data structure.
1856 * @pring: pointer to a IOCB ring.
1857 * @cnt: the number of IOCBs to be posted to the IOCB ring.
1858 *
1859 * This routine posts a given number of IOCBs with the associated DMA buffer
1860 * descriptors specified by the cnt argument to the given IOCB ring.
1861 *
1862 * Return codes
1863 * The number of IOCBs NOT able to be posted to the IOCB ring.
1864 **/
dea31012005-04-17 16:05:31 -05001865int
James Smart495a7142008-06-14 22:52:59 -04001866lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
dea31012005-04-17 16:05:31 -05001867{
1868 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001869 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -05001870 struct lpfc_dmabuf *mp1, *mp2;
1871
1872 cnt += pring->missbufcnt;
1873
1874 /* While there are buffers to post */
1875 while (cnt > 0) {
1876 /* Allocate buffer for command iocb */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001877 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05001878 if (iocb == NULL) {
1879 pring->missbufcnt = cnt;
1880 return cnt;
1881 }
dea31012005-04-17 16:05:31 -05001882 icmd = &iocb->iocb;
1883
1884 /* 2 buffers can be posted per command */
1885 /* Allocate buffer to post */
1886 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1887 if (mp1)
James Smart98c9ea52007-10-27 13:37:33 -04001888 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
1889 if (!mp1 || !mp1->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001890 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05001891 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001892 pring->missbufcnt = cnt;
1893 return cnt;
1894 }
1895
1896 INIT_LIST_HEAD(&mp1->list);
1897 /* Allocate buffer to post */
1898 if (cnt > 1) {
1899 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
1900 if (mp2)
1901 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
1902 &mp2->phys);
James Smart98c9ea52007-10-27 13:37:33 -04001903 if (!mp2 || !mp2->virt) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001904 kfree(mp2);
dea31012005-04-17 16:05:31 -05001905 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1906 kfree(mp1);
James Bottomley604a3e32005-10-29 10:28:33 -05001907 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001908 pring->missbufcnt = cnt;
1909 return cnt;
1910 }
1911
1912 INIT_LIST_HEAD(&mp2->list);
1913 } else {
1914 mp2 = NULL;
1915 }
1916
1917 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
1918 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
1919 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
1920 icmd->ulpBdeCount = 1;
1921 cnt--;
1922 if (mp2) {
1923 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
1924 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
1925 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
1926 cnt--;
1927 icmd->ulpBdeCount = 2;
1928 }
1929
1930 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
1931 icmd->ulpLe = 1;
1932
James Smart3772a992009-05-22 14:50:54 -04001933 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
1934 IOCB_ERROR) {
dea31012005-04-17 16:05:31 -05001935 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1936 kfree(mp1);
1937 cnt++;
1938 if (mp2) {
1939 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
1940 kfree(mp2);
1941 cnt++;
1942 }
James Bottomley604a3e32005-10-29 10:28:33 -05001943 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001944 pring->missbufcnt = cnt;
dea31012005-04-17 16:05:31 -05001945 return cnt;
1946 }
dea31012005-04-17 16:05:31 -05001947 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
James Smart92d7f7b2007-06-17 19:56:38 -05001948 if (mp2)
dea31012005-04-17 16:05:31 -05001949 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
dea31012005-04-17 16:05:31 -05001950 }
1951 pring->missbufcnt = 0;
1952 return 0;
1953}
1954
James Smarte59058c2008-08-24 21:49:00 -04001955/**
James Smart3621a712009-04-06 18:47:14 -04001956 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
James Smarte59058c2008-08-24 21:49:00 -04001957 * @phba: pointer to lpfc hba data structure.
1958 *
1959 * This routine posts initial receive IOCB buffers to the ELS ring. The
1960 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
1961 * set to 64 IOCBs.
1962 *
1963 * Return codes
1964 * 0 - success (currently always success)
1965 **/
dea31012005-04-17 16:05:31 -05001966static int
James Smart2e0fef82007-06-17 19:56:36 -05001967lpfc_post_rcv_buf(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001968{
1969 struct lpfc_sli *psli = &phba->sli;
1970
1971 /* Ring 0, ELS / CT buffers */
James Smart495a7142008-06-14 22:52:59 -04001972 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
dea31012005-04-17 16:05:31 -05001973 /* Ring 2 - FCP no buffers needed */
1974
1975 return 0;
1976}
1977
1978#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
1979
James Smarte59058c2008-08-24 21:49:00 -04001980/**
James Smart3621a712009-04-06 18:47:14 -04001981 * lpfc_sha_init - Set up initial array of hash table entries
James Smarte59058c2008-08-24 21:49:00 -04001982 * @HashResultPointer: pointer to an array as hash table.
1983 *
1984 * This routine sets up the initial values to the array of hash table entries
1985 * for the LC HBAs.
1986 **/
dea31012005-04-17 16:05:31 -05001987static void
1988lpfc_sha_init(uint32_t * HashResultPointer)
1989{
1990 HashResultPointer[0] = 0x67452301;
1991 HashResultPointer[1] = 0xEFCDAB89;
1992 HashResultPointer[2] = 0x98BADCFE;
1993 HashResultPointer[3] = 0x10325476;
1994 HashResultPointer[4] = 0xC3D2E1F0;
1995}
1996
James Smarte59058c2008-08-24 21:49:00 -04001997/**
James Smart3621a712009-04-06 18:47:14 -04001998 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
James Smarte59058c2008-08-24 21:49:00 -04001999 * @HashResultPointer: pointer to an initial/result hash table.
2000 * @HashWorkingPointer: pointer to an working hash table.
2001 *
2002 * This routine iterates an initial hash table pointed by @HashResultPointer
2003 * with the values from the working hash table pointeed by @HashWorkingPointer.
2004 * The results are putting back to the initial hash table, returned through
2005 * the @HashResultPointer as the result hash table.
2006 **/
dea31012005-04-17 16:05:31 -05002007static void
2008lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2009{
2010 int t;
2011 uint32_t TEMP;
2012 uint32_t A, B, C, D, E;
2013 t = 16;
2014 do {
2015 HashWorkingPointer[t] =
2016 S(1,
2017 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2018 8] ^
2019 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2020 } while (++t <= 79);
2021 t = 0;
2022 A = HashResultPointer[0];
2023 B = HashResultPointer[1];
2024 C = HashResultPointer[2];
2025 D = HashResultPointer[3];
2026 E = HashResultPointer[4];
2027
2028 do {
2029 if (t < 20) {
2030 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2031 } else if (t < 40) {
2032 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2033 } else if (t < 60) {
2034 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2035 } else {
2036 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2037 }
2038 TEMP += S(5, A) + E + HashWorkingPointer[t];
2039 E = D;
2040 D = C;
2041 C = S(30, B);
2042 B = A;
2043 A = TEMP;
2044 } while (++t <= 79);
2045
2046 HashResultPointer[0] += A;
2047 HashResultPointer[1] += B;
2048 HashResultPointer[2] += C;
2049 HashResultPointer[3] += D;
2050 HashResultPointer[4] += E;
2051
2052}
2053
James Smarte59058c2008-08-24 21:49:00 -04002054/**
James Smart3621a712009-04-06 18:47:14 -04002055 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
James Smarte59058c2008-08-24 21:49:00 -04002056 * @RandomChallenge: pointer to the entry of host challenge random number array.
2057 * @HashWorking: pointer to the entry of the working hash array.
2058 *
2059 * This routine calculates the working hash array referred by @HashWorking
2060 * from the challenge random numbers associated with the host, referred by
2061 * @RandomChallenge. The result is put into the entry of the working hash
2062 * array and returned by reference through @HashWorking.
2063 **/
dea31012005-04-17 16:05:31 -05002064static void
2065lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2066{
2067 *HashWorking = (*RandomChallenge ^ *HashWorking);
2068}
2069
James Smarte59058c2008-08-24 21:49:00 -04002070/**
James Smart3621a712009-04-06 18:47:14 -04002071 * lpfc_hba_init - Perform special handling for LC HBA initialization
James Smarte59058c2008-08-24 21:49:00 -04002072 * @phba: pointer to lpfc hba data structure.
2073 * @hbainit: pointer to an array of unsigned 32-bit integers.
2074 *
2075 * This routine performs the special handling for LC HBA initialization.
2076 **/
dea31012005-04-17 16:05:31 -05002077void
2078lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2079{
2080 int t;
2081 uint32_t *HashWorking;
James Smart2e0fef82007-06-17 19:56:36 -05002082 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
dea31012005-04-17 16:05:31 -05002083
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02002084 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05002085 if (!HashWorking)
2086 return;
2087
dea31012005-04-17 16:05:31 -05002088 HashWorking[0] = HashWorking[78] = *pwwnn++;
2089 HashWorking[1] = HashWorking[79] = *pwwnn;
2090
2091 for (t = 0; t < 7; t++)
2092 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2093
2094 lpfc_sha_init(hbainit);
2095 lpfc_sha_iterate(hbainit, HashWorking);
2096 kfree(HashWorking);
2097}
2098
James Smarte59058c2008-08-24 21:49:00 -04002099/**
James Smart3621a712009-04-06 18:47:14 -04002100 * lpfc_cleanup - Performs vport cleanups before deleting a vport
James Smarte59058c2008-08-24 21:49:00 -04002101 * @vport: pointer to a virtual N_Port data structure.
2102 *
2103 * This routine performs the necessary cleanups before deleting the @vport.
2104 * It invokes the discovery state machine to perform necessary state
2105 * transitions and to release the ndlps associated with the @vport. Note,
2106 * the physical port is treated as @vport 0.
2107 **/
James Smart87af33f2007-10-27 13:37:43 -04002108void
James Smart2e0fef82007-06-17 19:56:36 -05002109lpfc_cleanup(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002110{
James Smart87af33f2007-10-27 13:37:43 -04002111 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002112 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smarta8adb832007-10-27 13:37:53 -04002113 int i = 0;
dea31012005-04-17 16:05:31 -05002114
James Smart87af33f2007-10-27 13:37:43 -04002115 if (phba->link_state > LPFC_LINK_DOWN)
2116 lpfc_port_link_failure(vport);
2117
2118 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002119 if (!NLP_CHK_NODE_ACT(ndlp)) {
2120 ndlp = lpfc_enable_node(vport, ndlp,
2121 NLP_STE_UNUSED_NODE);
2122 if (!ndlp)
2123 continue;
2124 spin_lock_irq(&phba->ndlp_lock);
2125 NLP_SET_FREE_REQ(ndlp);
2126 spin_unlock_irq(&phba->ndlp_lock);
2127 /* Trigger the release of the ndlp memory */
2128 lpfc_nlp_put(ndlp);
2129 continue;
2130 }
2131 spin_lock_irq(&phba->ndlp_lock);
2132 if (NLP_CHK_FREE_REQ(ndlp)) {
2133 /* The ndlp should not be in memory free mode already */
2134 spin_unlock_irq(&phba->ndlp_lock);
2135 continue;
2136 } else
2137 /* Indicate request for freeing ndlp memory */
2138 NLP_SET_FREE_REQ(ndlp);
2139 spin_unlock_irq(&phba->ndlp_lock);
2140
James Smart58da1ff2008-04-07 10:15:56 -04002141 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2142 ndlp->nlp_DID == Fabric_DID) {
2143 /* Just free up ndlp with Fabric_DID for vports */
2144 lpfc_nlp_put(ndlp);
2145 continue;
2146 }
2147
James Smart87af33f2007-10-27 13:37:43 -04002148 if (ndlp->nlp_type & NLP_FABRIC)
2149 lpfc_disc_state_machine(vport, ndlp, NULL,
2150 NLP_EVT_DEVICE_RECOVERY);
James Smarte47c9092008-02-08 18:49:26 -05002151
James Smart87af33f2007-10-27 13:37:43 -04002152 lpfc_disc_state_machine(vport, ndlp, NULL,
2153 NLP_EVT_DEVICE_RM);
James Smart495a7142008-06-14 22:52:59 -04002154
James Smart87af33f2007-10-27 13:37:43 -04002155 }
2156
James Smarta8adb832007-10-27 13:37:53 -04002157 /* At this point, ALL ndlp's should be gone
2158 * because of the previous NLP_EVT_DEVICE_RM.
2159 * Lets wait for this to happen, if needed.
2160 */
James Smart87af33f2007-10-27 13:37:43 -04002161 while (!list_empty(&vport->fc_nodes)) {
James Smarta8adb832007-10-27 13:37:53 -04002162 if (i++ > 3000) {
James Smart87af33f2007-10-27 13:37:43 -04002163 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smarta8adb832007-10-27 13:37:53 -04002164 "0233 Nodelist not empty\n");
James Smarte47c9092008-02-08 18:49:26 -05002165 list_for_each_entry_safe(ndlp, next_ndlp,
2166 &vport->fc_nodes, nlp_listp) {
2167 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2168 LOG_NODE,
James Smartd7c255b2008-08-24 21:50:00 -04002169 "0282 did:x%x ndlp:x%p "
James Smarte47c9092008-02-08 18:49:26 -05002170 "usgmap:x%x refcnt:%d\n",
2171 ndlp->nlp_DID, (void *)ndlp,
2172 ndlp->nlp_usg_map,
2173 atomic_read(
2174 &ndlp->kref.refcount));
2175 }
James Smarta8adb832007-10-27 13:37:53 -04002176 break;
James Smart87af33f2007-10-27 13:37:43 -04002177 }
James Smarta8adb832007-10-27 13:37:53 -04002178
2179 /* Wait for any activity on ndlps to settle */
2180 msleep(10);
James Smart87af33f2007-10-27 13:37:43 -04002181 }
dea31012005-04-17 16:05:31 -05002182}
2183
James Smarte59058c2008-08-24 21:49:00 -04002184/**
James Smart3621a712009-04-06 18:47:14 -04002185 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
James Smarte59058c2008-08-24 21:49:00 -04002186 * @vport: pointer to a virtual N_Port data structure.
2187 *
2188 * This routine stops all the timers associated with a @vport. This function
2189 * is invoked before disabling or deleting a @vport. Note that the physical
2190 * port is treated as @vport 0.
2191 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002192void
2193lpfc_stop_vport_timers(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05002194{
James Smart92d7f7b2007-06-17 19:56:38 -05002195 del_timer_sync(&vport->els_tmofunc);
2196 del_timer_sync(&vport->fc_fdmitmo);
2197 lpfc_can_disctmo(vport);
2198 return;
dea31012005-04-17 16:05:31 -05002199}
2200
James Smarte59058c2008-08-24 21:49:00 -04002201/**
James Smartecfd03c2010-02-12 14:41:27 -05002202 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2203 * @phba: pointer to lpfc hba data structure.
2204 *
2205 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2206 * caller of this routine should already hold the host lock.
2207 **/
2208void
2209__lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2210{
James Smartfc2b9892010-02-26 14:15:29 -05002211 /* Clear pending FCF rediscovery wait and failover in progress flags */
2212 phba->fcf.fcf_flag &= ~(FCF_REDISC_PEND |
James Smart0c9ab6f2010-02-26 14:15:57 -05002213 FCF_DEAD_DISC |
2214 FCF_ACVL_DISC);
James Smartecfd03c2010-02-12 14:41:27 -05002215 /* Now, try to stop the timer */
2216 del_timer(&phba->fcf.redisc_wait);
2217}
2218
2219/**
2220 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2221 * @phba: pointer to lpfc hba data structure.
2222 *
2223 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2224 * checks whether the FCF rediscovery wait timer is pending with the host
2225 * lock held before proceeding with disabling the timer and clearing the
2226 * wait timer pendig flag.
2227 **/
2228void
2229lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2230{
2231 spin_lock_irq(&phba->hbalock);
2232 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2233 /* FCF rediscovery timer already fired or stopped */
2234 spin_unlock_irq(&phba->hbalock);
2235 return;
2236 }
2237 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
2238 spin_unlock_irq(&phba->hbalock);
2239}
2240
2241/**
James Smart3772a992009-05-22 14:50:54 -04002242 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
James Smarte59058c2008-08-24 21:49:00 -04002243 * @phba: pointer to lpfc hba data structure.
2244 *
2245 * This routine stops all the timers associated with a HBA. This function is
2246 * invoked before either putting a HBA offline or unloading the driver.
2247 **/
James Smart3772a992009-05-22 14:50:54 -04002248void
2249lpfc_stop_hba_timers(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002250{
James Smart51ef4c22007-08-02 11:10:31 -04002251 lpfc_stop_vport_timers(phba->pport);
James Smart2e0fef82007-06-17 19:56:36 -05002252 del_timer_sync(&phba->sli.mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05002253 del_timer_sync(&phba->fabric_block_timer);
James Smart93996272008-08-24 21:50:30 -04002254 del_timer_sync(&phba->eratt_poll);
James Smart3772a992009-05-22 14:50:54 -04002255 del_timer_sync(&phba->hb_tmofunc);
2256 phba->hb_outstanding = 0;
2257
2258 switch (phba->pci_dev_grp) {
2259 case LPFC_PCI_DEV_LP:
2260 /* Stop any LightPulse device specific driver timers */
2261 del_timer_sync(&phba->fcp_poll_timer);
2262 break;
2263 case LPFC_PCI_DEV_OC:
2264 /* Stop any OneConnect device sepcific driver timers */
James Smartecfd03c2010-02-12 14:41:27 -05002265 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
James Smart3772a992009-05-22 14:50:54 -04002266 break;
2267 default:
2268 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2269 "0297 Invalid device group (x%x)\n",
2270 phba->pci_dev_grp);
2271 break;
2272 }
James Smart2e0fef82007-06-17 19:56:36 -05002273 return;
dea31012005-04-17 16:05:31 -05002274}
2275
James Smarte59058c2008-08-24 21:49:00 -04002276/**
James Smart3621a712009-04-06 18:47:14 -04002277 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
James Smarte59058c2008-08-24 21:49:00 -04002278 * @phba: pointer to lpfc hba data structure.
2279 *
2280 * This routine marks a HBA's management interface as blocked. Once the HBA's
2281 * management interface is marked as blocked, all the user space access to
2282 * the HBA, whether they are from sysfs interface or libdfc interface will
2283 * all be blocked. The HBA is set to block the management interface when the
2284 * driver prepares the HBA interface for online or offline.
2285 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002286static void
2287lpfc_block_mgmt_io(struct lpfc_hba * phba)
2288{
2289 unsigned long iflag;
James Smart6e7288d2010-06-07 15:23:35 -04002290 uint8_t actcmd = MBX_HEARTBEAT;
2291 unsigned long timeout;
2292
Adrian Bunka6ababd2007-11-05 18:07:33 +01002293
2294 spin_lock_irqsave(&phba->hbalock, iflag);
2295 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
James Smart6e7288d2010-06-07 15:23:35 -04002296 if (phba->sli.mbox_active)
2297 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
Adrian Bunka6ababd2007-11-05 18:07:33 +01002298 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6e7288d2010-06-07 15:23:35 -04002299 /* Determine how long we might wait for the active mailbox
2300 * command to be gracefully completed by firmware.
2301 */
2302 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
2303 jiffies;
2304 /* Wait for the outstnading mailbox command to complete */
2305 while (phba->sli.mbox_active) {
2306 /* Check active mailbox complete status every 2ms */
2307 msleep(2);
2308 if (time_after(jiffies, timeout)) {
2309 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2310 "2813 Mgmt IO is Blocked %x "
2311 "- mbox cmd %x still active\n",
2312 phba->sli.sli_flag, actcmd);
2313 break;
2314 }
2315 }
Adrian Bunka6ababd2007-11-05 18:07:33 +01002316}
2317
James Smarte59058c2008-08-24 21:49:00 -04002318/**
James Smart3621a712009-04-06 18:47:14 -04002319 * lpfc_online - Initialize and bring a HBA online
James Smarte59058c2008-08-24 21:49:00 -04002320 * @phba: pointer to lpfc hba data structure.
2321 *
2322 * This routine initializes the HBA and brings a HBA online. During this
2323 * process, the management interface is blocked to prevent user space access
2324 * to the HBA interfering with the driver initialization.
2325 *
2326 * Return codes
2327 * 0 - successful
2328 * 1 - failed
2329 **/
dea31012005-04-17 16:05:31 -05002330int
James Smart2e0fef82007-06-17 19:56:36 -05002331lpfc_online(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002332{
Julia Lawall372bd282008-12-16 16:15:08 +01002333 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -04002334 struct lpfc_vport **vports;
2335 int i;
James Smart2e0fef82007-06-17 19:56:36 -05002336
dea31012005-04-17 16:05:31 -05002337 if (!phba)
2338 return 0;
Julia Lawall372bd282008-12-16 16:15:08 +01002339 vport = phba->pport;
dea31012005-04-17 16:05:31 -05002340
James Smart2e0fef82007-06-17 19:56:36 -05002341 if (!(vport->fc_flag & FC_OFFLINE_MODE))
dea31012005-04-17 16:05:31 -05002342 return 0;
2343
James Smarted957682007-06-17 19:56:37 -05002344 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002345 "0458 Bring Adapter online\n");
dea31012005-04-17 16:05:31 -05002346
James Smart46fa3112007-04-25 09:51:45 -04002347 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002348
James Smart46fa3112007-04-25 09:51:45 -04002349 if (!lpfc_sli_queue_setup(phba)) {
2350 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002351 return 1;
James Smart46fa3112007-04-25 09:51:45 -04002352 }
2353
James Smartda0436e2009-05-22 14:51:39 -04002354 if (phba->sli_rev == LPFC_SLI_REV4) {
2355 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2356 lpfc_unblock_mgmt_io(phba);
2357 return 1;
2358 }
2359 } else {
2360 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2361 lpfc_unblock_mgmt_io(phba);
2362 return 1;
2363 }
James Smart46fa3112007-04-25 09:51:45 -04002364 }
dea31012005-04-17 16:05:31 -05002365
James Smart549e55c2007-08-02 11:09:51 -04002366 vports = lpfc_create_vport_work_array(phba);
2367 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002368 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002369 struct Scsi_Host *shost;
2370 shost = lpfc_shost_from_vport(vports[i]);
2371 spin_lock_irq(shost->host_lock);
2372 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2373 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2374 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart1c6834a2009-07-19 10:01:26 -04002375 if (phba->sli_rev == LPFC_SLI_REV4)
2376 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smart549e55c2007-08-02 11:09:51 -04002377 spin_unlock_irq(shost->host_lock);
2378 }
James Smart09372822008-01-11 01:52:54 -05002379 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002380
James Smart46fa3112007-04-25 09:51:45 -04002381 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002382 return 0;
2383}
2384
James Smarte59058c2008-08-24 21:49:00 -04002385/**
James Smart3621a712009-04-06 18:47:14 -04002386 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
James Smarte59058c2008-08-24 21:49:00 -04002387 * @phba: pointer to lpfc hba data structure.
2388 *
2389 * This routine marks a HBA's management interface as not blocked. Once the
2390 * HBA's management interface is marked as not blocked, all the user space
2391 * access to the HBA, whether they are from sysfs interface or libdfc
2392 * interface will be allowed. The HBA is set to block the management interface
2393 * when the driver prepares the HBA interface for online or offline and then
2394 * set to unblock the management interface afterwards.
2395 **/
James Smart46fa3112007-04-25 09:51:45 -04002396void
James Smart46fa3112007-04-25 09:51:45 -04002397lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2398{
2399 unsigned long iflag;
2400
James Smart2e0fef82007-06-17 19:56:36 -05002401 spin_lock_irqsave(&phba->hbalock, iflag);
2402 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2403 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart46fa3112007-04-25 09:51:45 -04002404}
2405
James Smarte59058c2008-08-24 21:49:00 -04002406/**
James Smart3621a712009-04-06 18:47:14 -04002407 * lpfc_offline_prep - Prepare a HBA to be brought offline
James Smarte59058c2008-08-24 21:49:00 -04002408 * @phba: pointer to lpfc hba data structure.
2409 *
2410 * This routine is invoked to prepare a HBA to be brought offline. It performs
2411 * unregistration login to all the nodes on all vports and flushes the mailbox
2412 * queue to make it ready to be brought offline.
2413 **/
James Smart46fa3112007-04-25 09:51:45 -04002414void
2415lpfc_offline_prep(struct lpfc_hba * phba)
2416{
James Smart2e0fef82007-06-17 19:56:36 -05002417 struct lpfc_vport *vport = phba->pport;
James Smart46fa3112007-04-25 09:51:45 -04002418 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart87af33f2007-10-27 13:37:43 -04002419 struct lpfc_vport **vports;
James Smart72100cc2010-02-12 14:43:01 -05002420 struct Scsi_Host *shost;
James Smart87af33f2007-10-27 13:37:43 -04002421 int i;
dea31012005-04-17 16:05:31 -05002422
James Smart2e0fef82007-06-17 19:56:36 -05002423 if (vport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002424 return;
dea31012005-04-17 16:05:31 -05002425
James Smart46fa3112007-04-25 09:51:45 -04002426 lpfc_block_mgmt_io(phba);
dea31012005-04-17 16:05:31 -05002427
2428 lpfc_linkdown(phba);
2429
James Smart87af33f2007-10-27 13:37:43 -04002430 /* Issue an unreg_login to all nodes on all vports */
2431 vports = lpfc_create_vport_work_array(phba);
2432 if (vports != NULL) {
James Smartda0436e2009-05-22 14:51:39 -04002433 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8adb832007-10-27 13:37:53 -04002434 if (vports[i]->load_flag & FC_UNLOADING)
2435 continue;
James Smart72100cc2010-02-12 14:43:01 -05002436 shost = lpfc_shost_from_vport(vports[i]);
2437 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002438 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002439 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2440 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05002441 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002442
James Smart87af33f2007-10-27 13:37:43 -04002443 shost = lpfc_shost_from_vport(vports[i]);
2444 list_for_each_entry_safe(ndlp, next_ndlp,
2445 &vports[i]->fc_nodes,
2446 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05002447 if (!NLP_CHK_NODE_ACT(ndlp))
2448 continue;
James Smart87af33f2007-10-27 13:37:43 -04002449 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2450 continue;
2451 if (ndlp->nlp_type & NLP_FABRIC) {
2452 lpfc_disc_state_machine(vports[i], ndlp,
2453 NULL, NLP_EVT_DEVICE_RECOVERY);
2454 lpfc_disc_state_machine(vports[i], ndlp,
2455 NULL, NLP_EVT_DEVICE_RM);
2456 }
2457 spin_lock_irq(shost->host_lock);
2458 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2459 spin_unlock_irq(shost->host_lock);
2460 lpfc_unreg_rpi(vports[i], ndlp);
2461 }
2462 }
2463 }
James Smart09372822008-01-11 01:52:54 -05002464 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002465
James Smart04c68492009-05-22 14:52:52 -04002466 lpfc_sli_mbox_sys_shutdown(phba);
James Smart46fa3112007-04-25 09:51:45 -04002467}
2468
James Smarte59058c2008-08-24 21:49:00 -04002469/**
James Smart3621a712009-04-06 18:47:14 -04002470 * lpfc_offline - Bring a HBA offline
James Smarte59058c2008-08-24 21:49:00 -04002471 * @phba: pointer to lpfc hba data structure.
2472 *
2473 * This routine actually brings a HBA offline. It stops all the timers
2474 * associated with the HBA, brings down the SLI layer, and eventually
2475 * marks the HBA as in offline state for the upper layer protocol.
2476 **/
James Smart46fa3112007-04-25 09:51:45 -04002477void
James Smart2e0fef82007-06-17 19:56:36 -05002478lpfc_offline(struct lpfc_hba *phba)
James Smart46fa3112007-04-25 09:51:45 -04002479{
James Smart549e55c2007-08-02 11:09:51 -04002480 struct Scsi_Host *shost;
2481 struct lpfc_vport **vports;
2482 int i;
James Smart46fa3112007-04-25 09:51:45 -04002483
James Smart549e55c2007-08-02 11:09:51 -04002484 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
James Smart46fa3112007-04-25 09:51:45 -04002485 return;
James Smart688a8862006-07-06 15:49:56 -04002486
James Smartda0436e2009-05-22 14:51:39 -04002487 /* stop port and all timers associated with this hba */
2488 lpfc_stop_port(phba);
James Smart51ef4c22007-08-02 11:10:31 -04002489 vports = lpfc_create_vport_work_array(phba);
2490 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002491 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart51ef4c22007-08-02 11:10:31 -04002492 lpfc_stop_vport_timers(vports[i]);
James Smart09372822008-01-11 01:52:54 -05002493 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -05002494 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002495 "0460 Bring Adapter offline\n");
dea31012005-04-17 16:05:31 -05002496 /* Bring down the SLI Layer and cleanup. The HBA is offline
2497 now. */
2498 lpfc_sli_hba_down(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002499 spin_lock_irq(&phba->hbalock);
James Smart7054a602007-04-25 09:52:34 -04002500 phba->work_ha = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002501 spin_unlock_irq(&phba->hbalock);
James Smart549e55c2007-08-02 11:09:51 -04002502 vports = lpfc_create_vport_work_array(phba);
2503 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -04002504 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -04002505 shost = lpfc_shost_from_vport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -04002506 spin_lock_irq(shost->host_lock);
2507 vports[i]->work_port_events = 0;
2508 vports[i]->fc_flag |= FC_OFFLINE_MODE;
2509 spin_unlock_irq(shost->host_lock);
2510 }
James Smart09372822008-01-11 01:52:54 -05002511 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05002512}
2513
James Smarte59058c2008-08-24 21:49:00 -04002514/**
James Smart3621a712009-04-06 18:47:14 -04002515 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
James Smarte59058c2008-08-24 21:49:00 -04002516 * @phba: pointer to lpfc hba data structure.
2517 *
2518 * This routine is to free all the SCSI buffers and IOCBs from the driver
2519 * list back to kernel. It is called from lpfc_pci_remove_one to free
2520 * the internal resources before the device is removed from the system.
2521 *
2522 * Return codes
2523 * 0 - successful (for now, it always returns 0)
2524 **/
dea31012005-04-17 16:05:31 -05002525static int
James Smart2e0fef82007-06-17 19:56:36 -05002526lpfc_scsi_free(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002527{
2528 struct lpfc_scsi_buf *sb, *sb_next;
2529 struct lpfc_iocbq *io, *io_next;
2530
James Smart2e0fef82007-06-17 19:56:36 -05002531 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002532 /* Release all the lpfc_scsi_bufs maintained by this host. */
James Smart1c6f4ef52009-11-18 15:40:49 -05002533 spin_lock(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05002534 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
2535 list_del(&sb->list);
2536 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
James Smart92d7f7b2007-06-17 19:56:38 -05002537 sb->dma_handle);
dea31012005-04-17 16:05:31 -05002538 kfree(sb);
2539 phba->total_scsi_bufs--;
2540 }
James Smart1c6f4ef52009-11-18 15:40:49 -05002541 spin_unlock(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05002542
2543 /* Release all the lpfc_iocbq entries maintained by this host. */
2544 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2545 list_del(&io->list);
2546 kfree(io);
2547 phba->total_iocbq_bufs--;
2548 }
James Smart2e0fef82007-06-17 19:56:36 -05002549 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002550 return 0;
2551}
2552
James Smarte59058c2008-08-24 21:49:00 -04002553/**
James Smart3621a712009-04-06 18:47:14 -04002554 * lpfc_create_port - Create an FC port
James Smarte59058c2008-08-24 21:49:00 -04002555 * @phba: pointer to lpfc hba data structure.
2556 * @instance: a unique integer ID to this FC port.
2557 * @dev: pointer to the device data structure.
2558 *
2559 * This routine creates a FC port for the upper layer protocol. The FC port
2560 * can be created on top of either a physical port or a virtual port provided
2561 * by the HBA. This routine also allocates a SCSI host data structure (shost)
2562 * and associates the FC port created before adding the shost into the SCSI
2563 * layer.
2564 *
2565 * Return codes
2566 * @vport - pointer to the virtual N_Port data structure.
2567 * NULL - port create failed.
2568 **/
James Smart2e0fef82007-06-17 19:56:36 -05002569struct lpfc_vport *
James Smart3de2a652007-08-02 11:09:59 -04002570lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
James Smart47a86172007-04-25 09:53:22 -04002571{
James Smart2e0fef82007-06-17 19:56:36 -05002572 struct lpfc_vport *vport;
2573 struct Scsi_Host *shost;
2574 int error = 0;
James Smart47a86172007-04-25 09:53:22 -04002575
James Smart3de2a652007-08-02 11:09:59 -04002576 if (dev != &phba->pcidev->dev)
2577 shost = scsi_host_alloc(&lpfc_vport_template,
2578 sizeof(struct lpfc_vport));
2579 else
2580 shost = scsi_host_alloc(&lpfc_template,
2581 sizeof(struct lpfc_vport));
James Smart2e0fef82007-06-17 19:56:36 -05002582 if (!shost)
2583 goto out;
James Smart47a86172007-04-25 09:53:22 -04002584
James Smart2e0fef82007-06-17 19:56:36 -05002585 vport = (struct lpfc_vport *) shost->hostdata;
2586 vport->phba = phba;
James Smart2e0fef82007-06-17 19:56:36 -05002587 vport->load_flag |= FC_LOADING;
James Smart92d7f7b2007-06-17 19:56:38 -05002588 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart7f5f3d02008-02-08 18:50:14 -05002589 vport->fc_rscn_flush = 0;
James Smart47a86172007-04-25 09:53:22 -04002590
James Smart3de2a652007-08-02 11:09:59 -04002591 lpfc_get_vport_cfgparam(vport);
James Smart2e0fef82007-06-17 19:56:36 -05002592 shost->unique_id = instance;
2593 shost->max_id = LPFC_MAX_TARGET;
James Smart3de2a652007-08-02 11:09:59 -04002594 shost->max_lun = vport->cfg_max_luns;
James Smart2e0fef82007-06-17 19:56:36 -05002595 shost->this_id = -1;
2596 shost->max_cmd_len = 16;
James Smartda0436e2009-05-22 14:51:39 -04002597 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart28baac72010-02-12 14:42:03 -05002598 shost->dma_boundary =
James Smartcb5172e2010-03-15 11:25:07 -04002599 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
James Smartda0436e2009-05-22 14:51:39 -04002600 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
2601 }
James Smart81301a92008-12-04 22:39:46 -05002602
James Smart47a86172007-04-25 09:53:22 -04002603 /*
James Smart2e0fef82007-06-17 19:56:36 -05002604 * Set initial can_queue value since 0 is no longer supported and
2605 * scsi_add_host will fail. This will be adjusted later based on the
2606 * max xri value determined in hba setup.
James Smart47a86172007-04-25 09:53:22 -04002607 */
James Smart2e0fef82007-06-17 19:56:36 -05002608 shost->can_queue = phba->cfg_hba_queue_depth - 10;
James Smart3de2a652007-08-02 11:09:59 -04002609 if (dev != &phba->pcidev->dev) {
James Smart92d7f7b2007-06-17 19:56:38 -05002610 shost->transportt = lpfc_vport_transport_template;
2611 vport->port_type = LPFC_NPIV_PORT;
2612 } else {
2613 shost->transportt = lpfc_transport_template;
2614 vport->port_type = LPFC_PHYSICAL_PORT;
2615 }
James Smart47a86172007-04-25 09:53:22 -04002616
James Smart2e0fef82007-06-17 19:56:36 -05002617 /* Initialize all internally managed lists. */
2618 INIT_LIST_HEAD(&vport->fc_nodes);
James Smartda0436e2009-05-22 14:51:39 -04002619 INIT_LIST_HEAD(&vport->rcv_buffer_list);
James Smart2e0fef82007-06-17 19:56:36 -05002620 spin_lock_init(&vport->work_port_lock);
James Smart47a86172007-04-25 09:53:22 -04002621
James Smart2e0fef82007-06-17 19:56:36 -05002622 init_timer(&vport->fc_disctmo);
2623 vport->fc_disctmo.function = lpfc_disc_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05002624 vport->fc_disctmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002625
James Smart2e0fef82007-06-17 19:56:36 -05002626 init_timer(&vport->fc_fdmitmo);
2627 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
James Smart92d7f7b2007-06-17 19:56:38 -05002628 vport->fc_fdmitmo.data = (unsigned long)vport;
James Smart47a86172007-04-25 09:53:22 -04002629
James Smart2e0fef82007-06-17 19:56:36 -05002630 init_timer(&vport->els_tmofunc);
2631 vport->els_tmofunc.function = lpfc_els_timeout;
James Smart92d7f7b2007-06-17 19:56:38 -05002632 vport->els_tmofunc.data = (unsigned long)vport;
James Bottomleyd139b9b2009-11-05 13:33:12 -06002633 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05002634 if (error)
2635 goto out_put_shost;
James Smart47a86172007-04-25 09:53:22 -04002636
James Smart549e55c2007-08-02 11:09:51 -04002637 spin_lock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002638 list_add_tail(&vport->listentry, &phba->port_list);
James Smart549e55c2007-08-02 11:09:51 -04002639 spin_unlock_irq(&phba->hbalock);
James Smart2e0fef82007-06-17 19:56:36 -05002640 return vport;
James Smart47a86172007-04-25 09:53:22 -04002641
James Smart2e0fef82007-06-17 19:56:36 -05002642out_put_shost:
2643 scsi_host_put(shost);
2644out:
2645 return NULL;
James Smart47a86172007-04-25 09:53:22 -04002646}
2647
James Smarte59058c2008-08-24 21:49:00 -04002648/**
James Smart3621a712009-04-06 18:47:14 -04002649 * destroy_port - destroy an FC port
James Smarte59058c2008-08-24 21:49:00 -04002650 * @vport: pointer to an lpfc virtual N_Port data structure.
2651 *
2652 * This routine destroys a FC port from the upper layer protocol. All the
2653 * resources associated with the port are released.
2654 **/
James Smart2e0fef82007-06-17 19:56:36 -05002655void
2656destroy_port(struct lpfc_vport *vport)
James Smart47a86172007-04-25 09:53:22 -04002657{
James Smart92d7f7b2007-06-17 19:56:38 -05002658 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2659 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002660
James Smart858c9f62007-06-17 19:56:39 -05002661 lpfc_debugfs_terminate(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05002662 fc_remove_host(shost);
2663 scsi_remove_host(shost);
James Smart47a86172007-04-25 09:53:22 -04002664
James Smart92d7f7b2007-06-17 19:56:38 -05002665 spin_lock_irq(&phba->hbalock);
2666 list_del_init(&vport->listentry);
2667 spin_unlock_irq(&phba->hbalock);
James Smart47a86172007-04-25 09:53:22 -04002668
James Smart92d7f7b2007-06-17 19:56:38 -05002669 lpfc_cleanup(vport);
James Smart47a86172007-04-25 09:53:22 -04002670 return;
James Smart47a86172007-04-25 09:53:22 -04002671}
2672
James Smarte59058c2008-08-24 21:49:00 -04002673/**
James Smart3621a712009-04-06 18:47:14 -04002674 * lpfc_get_instance - Get a unique integer ID
James Smarte59058c2008-08-24 21:49:00 -04002675 *
2676 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
2677 * uses the kernel idr facility to perform the task.
2678 *
2679 * Return codes:
2680 * instance - a unique integer ID allocated as the new instance.
2681 * -1 - lpfc get instance failed.
2682 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002683int
2684lpfc_get_instance(void)
2685{
2686 int instance = 0;
2687
2688 /* Assign an unused number */
2689 if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
2690 return -1;
2691 if (idr_get_new(&lpfc_hba_index, NULL, &instance))
2692 return -1;
2693 return instance;
2694}
2695
James Smarte59058c2008-08-24 21:49:00 -04002696/**
James Smart3621a712009-04-06 18:47:14 -04002697 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
James Smarte59058c2008-08-24 21:49:00 -04002698 * @shost: pointer to SCSI host data structure.
2699 * @time: elapsed time of the scan in jiffies.
2700 *
2701 * This routine is called by the SCSI layer with a SCSI host to determine
2702 * whether the scan host is finished.
2703 *
2704 * Note: there is no scan_start function as adapter initialization will have
2705 * asynchronously kicked off the link initialization.
2706 *
2707 * Return codes
2708 * 0 - SCSI host scan is not over yet.
2709 * 1 - SCSI host scan is over.
2710 **/
James Smart47a86172007-04-25 09:53:22 -04002711int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2712{
James Smart2e0fef82007-06-17 19:56:36 -05002713 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2714 struct lpfc_hba *phba = vport->phba;
James Smart858c9f62007-06-17 19:56:39 -05002715 int stat = 0;
James Smart47a86172007-04-25 09:53:22 -04002716
James Smart858c9f62007-06-17 19:56:39 -05002717 spin_lock_irq(shost->host_lock);
2718
James Smart51ef4c22007-08-02 11:10:31 -04002719 if (vport->load_flag & FC_UNLOADING) {
James Smart858c9f62007-06-17 19:56:39 -05002720 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002721 goto finished;
James Smart858c9f62007-06-17 19:56:39 -05002722 }
James Smart2e0fef82007-06-17 19:56:36 -05002723 if (time >= 30 * HZ) {
2724 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002725 "0461 Scanning longer than 30 "
2726 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05002727 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002728 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002729 }
2730 if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
2731 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04002732 "0465 Link down longer than 15 "
2733 "seconds. Continuing initialization\n");
James Smart858c9f62007-06-17 19:56:39 -05002734 stat = 1;
James Smart2e0fef82007-06-17 19:56:36 -05002735 goto finished;
James Smart47a86172007-04-25 09:53:22 -04002736 }
2737
James Smart2e0fef82007-06-17 19:56:36 -05002738 if (vport->port_state != LPFC_VPORT_READY)
James Smart858c9f62007-06-17 19:56:39 -05002739 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002740 if (vport->num_disc_nodes || vport->fc_prli_sent)
James Smart858c9f62007-06-17 19:56:39 -05002741 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002742 if (vport->fc_map_cnt == 0 && time < 2 * HZ)
James Smart858c9f62007-06-17 19:56:39 -05002743 goto finished;
James Smart2e0fef82007-06-17 19:56:36 -05002744 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
James Smart858c9f62007-06-17 19:56:39 -05002745 goto finished;
2746
2747 stat = 1;
James Smart47a86172007-04-25 09:53:22 -04002748
2749finished:
James Smart858c9f62007-06-17 19:56:39 -05002750 spin_unlock_irq(shost->host_lock);
2751 return stat;
James Smart92d7f7b2007-06-17 19:56:38 -05002752}
2753
James Smarte59058c2008-08-24 21:49:00 -04002754/**
James Smart3621a712009-04-06 18:47:14 -04002755 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
James Smarte59058c2008-08-24 21:49:00 -04002756 * @shost: pointer to SCSI host data structure.
2757 *
2758 * This routine initializes a given SCSI host attributes on a FC port. The
2759 * SCSI host can be either on top of a physical port or a virtual port.
2760 **/
James Smart92d7f7b2007-06-17 19:56:38 -05002761void lpfc_host_attrib_init(struct Scsi_Host *shost)
2762{
2763 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2764 struct lpfc_hba *phba = vport->phba;
James Smart47a86172007-04-25 09:53:22 -04002765 /*
James Smart2e0fef82007-06-17 19:56:36 -05002766 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
James Smart47a86172007-04-25 09:53:22 -04002767 */
2768
James Smart2e0fef82007-06-17 19:56:36 -05002769 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
2770 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart47a86172007-04-25 09:53:22 -04002771 fc_host_supported_classes(shost) = FC_COS_CLASS3;
2772
2773 memset(fc_host_supported_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05002774 sizeof(fc_host_supported_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04002775 fc_host_supported_fc4s(shost)[2] = 1;
2776 fc_host_supported_fc4s(shost)[7] = 1;
2777
James Smart92d7f7b2007-06-17 19:56:38 -05002778 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
2779 sizeof fc_host_symbolic_name(shost));
James Smart47a86172007-04-25 09:53:22 -04002780
2781 fc_host_supported_speeds(shost) = 0;
2782 if (phba->lmt & LMT_10Gb)
2783 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
James Smarta8adb832007-10-27 13:37:53 -04002784 if (phba->lmt & LMT_8Gb)
2785 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
James Smart47a86172007-04-25 09:53:22 -04002786 if (phba->lmt & LMT_4Gb)
2787 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
2788 if (phba->lmt & LMT_2Gb)
2789 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
2790 if (phba->lmt & LMT_1Gb)
2791 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
2792
2793 fc_host_maxframe_size(shost) =
James Smart2e0fef82007-06-17 19:56:36 -05002794 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
2795 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
James Smart47a86172007-04-25 09:53:22 -04002796
2797 /* This value is also unchanging */
2798 memset(fc_host_active_fc4s(shost), 0,
James Smart2e0fef82007-06-17 19:56:36 -05002799 sizeof(fc_host_active_fc4s(shost)));
James Smart47a86172007-04-25 09:53:22 -04002800 fc_host_active_fc4s(shost)[2] = 1;
2801 fc_host_active_fc4s(shost)[7] = 1;
2802
James Smart92d7f7b2007-06-17 19:56:38 -05002803 fc_host_max_npiv_vports(shost) = phba->max_vpi;
James Smart47a86172007-04-25 09:53:22 -04002804 spin_lock_irq(shost->host_lock);
James Smart51ef4c22007-08-02 11:10:31 -04002805 vport->load_flag &= ~FC_LOADING;
James Smart47a86172007-04-25 09:53:22 -04002806 spin_unlock_irq(shost->host_lock);
James Smart47a86172007-04-25 09:53:22 -04002807}
dea31012005-04-17 16:05:31 -05002808
James Smarte59058c2008-08-24 21:49:00 -04002809/**
James Smartda0436e2009-05-22 14:51:39 -04002810 * lpfc_stop_port_s3 - Stop SLI3 device port
James Smarte59058c2008-08-24 21:49:00 -04002811 * @phba: pointer to lpfc hba data structure.
2812 *
James Smartda0436e2009-05-22 14:51:39 -04002813 * This routine is invoked to stop an SLI3 device port, it stops the device
2814 * from generating interrupts and stops the device driver's timers for the
2815 * device.
James Smarte59058c2008-08-24 21:49:00 -04002816 **/
James Smartdb2378e2008-02-08 18:49:51 -05002817static void
James Smartda0436e2009-05-22 14:51:39 -04002818lpfc_stop_port_s3(struct lpfc_hba *phba)
James Smartdb2378e2008-02-08 18:49:51 -05002819{
James Smartda0436e2009-05-22 14:51:39 -04002820 /* Clear all interrupt enable conditions */
2821 writel(0, phba->HCregaddr);
2822 readl(phba->HCregaddr); /* flush */
2823 /* Clear all pending interrupts */
2824 writel(0xffffffff, phba->HAregaddr);
2825 readl(phba->HAregaddr); /* flush */
James Smart93996272008-08-24 21:50:30 -04002826
James Smartda0436e2009-05-22 14:51:39 -04002827 /* Reset some HBA SLI setup states */
2828 lpfc_stop_hba_timers(phba);
2829 phba->pport->work_port_events = 0;
James Smartdb2378e2008-02-08 18:49:51 -05002830}
2831
James Smarte59058c2008-08-24 21:49:00 -04002832/**
James Smartda0436e2009-05-22 14:51:39 -04002833 * lpfc_stop_port_s4 - Stop SLI4 device port
James Smart5b75da22008-12-04 22:39:35 -05002834 * @phba: pointer to lpfc hba data structure.
2835 *
James Smartda0436e2009-05-22 14:51:39 -04002836 * This routine is invoked to stop an SLI4 device port, it stops the device
2837 * from generating interrupts and stops the device driver's timers for the
2838 * device.
2839 **/
2840static void
2841lpfc_stop_port_s4(struct lpfc_hba *phba)
2842{
2843 /* Reset some HBA SLI4 setup states */
2844 lpfc_stop_hba_timers(phba);
2845 phba->pport->work_port_events = 0;
2846 phba->sli4_hba.intr_enable = 0;
James Smartda0436e2009-05-22 14:51:39 -04002847}
2848
2849/**
2850 * lpfc_stop_port - Wrapper function for stopping hba port
2851 * @phba: Pointer to HBA context object.
James Smart5b75da22008-12-04 22:39:35 -05002852 *
James Smartda0436e2009-05-22 14:51:39 -04002853 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
2854 * the API jump table function pointer from the lpfc_hba struct.
2855 **/
2856void
2857lpfc_stop_port(struct lpfc_hba *phba)
2858{
2859 phba->lpfc_stop_port(phba);
2860}
2861
2862/**
2863 * lpfc_sli4_remove_dflt_fcf - Remove the driver default fcf record from the port.
2864 * @phba: pointer to lpfc hba data structure.
2865 *
2866 * This routine is invoked to remove the driver default fcf record from
2867 * the port. This routine currently acts on FCF Index 0.
2868 *
2869 **/
2870void
2871lpfc_sli_remove_dflt_fcf(struct lpfc_hba *phba)
2872{
2873 int rc = 0;
2874 LPFC_MBOXQ_t *mboxq;
2875 struct lpfc_mbx_del_fcf_tbl_entry *del_fcf_record;
2876 uint32_t mbox_tmo, req_len;
2877 uint32_t shdr_status, shdr_add_status;
2878
2879 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2880 if (!mboxq) {
2881 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2882 "2020 Failed to allocate mbox for ADD_FCF cmd\n");
2883 return;
2884 }
2885
2886 req_len = sizeof(struct lpfc_mbx_del_fcf_tbl_entry) -
2887 sizeof(struct lpfc_sli4_cfg_mhdr);
2888 rc = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
2889 LPFC_MBOX_OPCODE_FCOE_DELETE_FCF,
2890 req_len, LPFC_SLI4_MBX_EMBED);
2891 /*
2892 * In phase 1, there is a single FCF index, 0. In phase2, the driver
2893 * supports multiple FCF indices.
2894 */
2895 del_fcf_record = &mboxq->u.mqe.un.del_fcf_entry;
2896 bf_set(lpfc_mbx_del_fcf_tbl_count, del_fcf_record, 1);
2897 bf_set(lpfc_mbx_del_fcf_tbl_index, del_fcf_record,
James Smartecfd03c2010-02-12 14:41:27 -05002898 phba->fcf.current_rec.fcf_indx);
James Smartda0436e2009-05-22 14:51:39 -04002899
2900 if (!phba->sli4_hba.intr_enable)
2901 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
2902 else {
2903 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
2904 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
2905 }
2906 /* The IOCTL status is embedded in the mailbox subheader. */
2907 shdr_status = bf_get(lpfc_mbox_hdr_status,
2908 &del_fcf_record->header.cfg_shdr.response);
2909 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
2910 &del_fcf_record->header.cfg_shdr.response);
2911 if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
2912 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2913 "2516 DEL FCF of default FCF Index failed "
2914 "mbx status x%x, status x%x add_status x%x\n",
2915 rc, shdr_status, shdr_add_status);
2916 }
2917 if (rc != MBX_TIMEOUT)
2918 mempool_free(mboxq, phba->mbox_mem_pool);
2919}
2920
2921/**
James Smartecfd03c2010-02-12 14:41:27 -05002922 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
2923 * @phba: Pointer to hba for which this call is being executed.
2924 *
2925 * This routine starts the timer waiting for the FCF rediscovery to complete.
2926 **/
2927void
2928lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
2929{
2930 unsigned long fcf_redisc_wait_tmo =
2931 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
2932 /* Start fcf rediscovery wait period timer */
2933 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
2934 spin_lock_irq(&phba->hbalock);
2935 /* Allow action to new fcf asynchronous event */
2936 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
2937 /* Mark the FCF rediscovery pending state */
2938 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
2939 spin_unlock_irq(&phba->hbalock);
2940}
2941
2942/**
2943 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
2944 * @ptr: Map to lpfc_hba data structure pointer.
2945 *
2946 * This routine is invoked when waiting for FCF table rediscover has been
2947 * timed out. If new FCF record(s) has (have) been discovered during the
2948 * wait period, a new FCF event shall be added to the FCOE async event
2949 * list, and then worker thread shall be waked up for processing from the
2950 * worker thread context.
2951 **/
2952void
2953lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
2954{
2955 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
2956
2957 /* Don't send FCF rediscovery event if timer cancelled */
2958 spin_lock_irq(&phba->hbalock);
2959 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2960 spin_unlock_irq(&phba->hbalock);
2961 return;
2962 }
2963 /* Clear FCF rediscovery timer pending flag */
2964 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2965 /* FCF rediscovery event to worker thread */
2966 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
2967 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05002968 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2969 "2776 FCF rediscover wait timer expired, post "
2970 "a worker thread event for FCF table scan\n");
James Smartecfd03c2010-02-12 14:41:27 -05002971 /* wake up worker thread */
2972 lpfc_worker_wake_up(phba);
2973}
2974
2975/**
James Smart6669f9b2009-10-02 15:16:45 -04002976 * lpfc_sli4_fw_cfg_check - Read the firmware config and verify FCoE support
2977 * @phba: pointer to lpfc hba data structure.
2978 *
2979 * This function uses the QUERY_FW_CFG mailbox command to determine if the
2980 * firmware loaded supports FCoE. A return of zero indicates that the mailbox
2981 * was successful and the firmware supports FCoE. Any other return indicates
2982 * a error. It is assumed that this function will be called before interrupts
2983 * are enabled.
2984 **/
2985static int
2986lpfc_sli4_fw_cfg_check(struct lpfc_hba *phba)
2987{
2988 int rc = 0;
2989 LPFC_MBOXQ_t *mboxq;
2990 struct lpfc_mbx_query_fw_cfg *query_fw_cfg;
2991 uint32_t length;
2992 uint32_t shdr_status, shdr_add_status;
2993
2994 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2995 if (!mboxq) {
2996 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2997 "2621 Failed to allocate mbox for "
2998 "query firmware config cmd\n");
2999 return -ENOMEM;
3000 }
3001 query_fw_cfg = &mboxq->u.mqe.un.query_fw_cfg;
3002 length = (sizeof(struct lpfc_mbx_query_fw_cfg) -
3003 sizeof(struct lpfc_sli4_cfg_mhdr));
3004 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
3005 LPFC_MBOX_OPCODE_QUERY_FW_CFG,
3006 length, LPFC_SLI4_MBX_EMBED);
3007 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
3008 /* The IOCTL status is embedded in the mailbox subheader. */
3009 shdr_status = bf_get(lpfc_mbox_hdr_status,
3010 &query_fw_cfg->header.cfg_shdr.response);
3011 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
3012 &query_fw_cfg->header.cfg_shdr.response);
3013 if (shdr_status || shdr_add_status || rc != MBX_SUCCESS) {
3014 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3015 "2622 Query Firmware Config failed "
3016 "mbx status x%x, status x%x add_status x%x\n",
3017 rc, shdr_status, shdr_add_status);
3018 return -EINVAL;
3019 }
3020 if (!bf_get(lpfc_function_mode_fcoe_i, query_fw_cfg)) {
3021 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3022 "2623 FCoE Function not supported by firmware. "
3023 "Function mode = %08x\n",
3024 query_fw_cfg->function_mode);
3025 return -EINVAL;
3026 }
3027 if (rc != MBX_TIMEOUT)
3028 mempool_free(mboxq, phba->mbox_mem_pool);
3029 return 0;
3030}
3031
3032/**
James Smartda0436e2009-05-22 14:51:39 -04003033 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
3034 * @phba: pointer to lpfc hba data structure.
3035 * @acqe_link: pointer to the async link completion queue entry.
3036 *
3037 * This routine is to parse the SLI4 link-attention link fault code and
3038 * translate it into the base driver's read link attention mailbox command
3039 * status.
3040 *
3041 * Return: Link-attention status in terms of base driver's coding.
3042 **/
3043static uint16_t
3044lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
3045 struct lpfc_acqe_link *acqe_link)
3046{
3047 uint16_t latt_fault;
3048
3049 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
3050 case LPFC_ASYNC_LINK_FAULT_NONE:
3051 case LPFC_ASYNC_LINK_FAULT_LOCAL:
3052 case LPFC_ASYNC_LINK_FAULT_REMOTE:
3053 latt_fault = 0;
3054 break;
3055 default:
3056 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3057 "0398 Invalid link fault code: x%x\n",
3058 bf_get(lpfc_acqe_link_fault, acqe_link));
3059 latt_fault = MBXERR_ERROR;
3060 break;
3061 }
3062 return latt_fault;
3063}
3064
3065/**
3066 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
3067 * @phba: pointer to lpfc hba data structure.
3068 * @acqe_link: pointer to the async link completion queue entry.
3069 *
3070 * This routine is to parse the SLI4 link attention type and translate it
3071 * into the base driver's link attention type coding.
3072 *
3073 * Return: Link attention type in terms of base driver's coding.
3074 **/
3075static uint8_t
3076lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
3077 struct lpfc_acqe_link *acqe_link)
3078{
3079 uint8_t att_type;
3080
3081 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
3082 case LPFC_ASYNC_LINK_STATUS_DOWN:
3083 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
3084 att_type = AT_LINK_DOWN;
3085 break;
3086 case LPFC_ASYNC_LINK_STATUS_UP:
3087 /* Ignore physical link up events - wait for logical link up */
3088 att_type = AT_RESERVED;
3089 break;
3090 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
3091 att_type = AT_LINK_UP;
3092 break;
3093 default:
3094 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3095 "0399 Invalid link attention type: x%x\n",
3096 bf_get(lpfc_acqe_link_status, acqe_link));
3097 att_type = AT_RESERVED;
3098 break;
3099 }
3100 return att_type;
3101}
3102
3103/**
3104 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
3105 * @phba: pointer to lpfc hba data structure.
3106 * @acqe_link: pointer to the async link completion queue entry.
3107 *
3108 * This routine is to parse the SLI4 link-attention link speed and translate
3109 * it into the base driver's link-attention link speed coding.
3110 *
3111 * Return: Link-attention link speed in terms of base driver's coding.
3112 **/
3113static uint8_t
3114lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
3115 struct lpfc_acqe_link *acqe_link)
3116{
3117 uint8_t link_speed;
3118
3119 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
3120 case LPFC_ASYNC_LINK_SPEED_ZERO:
3121 link_speed = LA_UNKNW_LINK;
3122 break;
3123 case LPFC_ASYNC_LINK_SPEED_10MBPS:
3124 link_speed = LA_UNKNW_LINK;
3125 break;
3126 case LPFC_ASYNC_LINK_SPEED_100MBPS:
3127 link_speed = LA_UNKNW_LINK;
3128 break;
3129 case LPFC_ASYNC_LINK_SPEED_1GBPS:
3130 link_speed = LA_1GHZ_LINK;
3131 break;
3132 case LPFC_ASYNC_LINK_SPEED_10GBPS:
3133 link_speed = LA_10GHZ_LINK;
3134 break;
3135 default:
3136 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3137 "0483 Invalid link-attention link speed: x%x\n",
3138 bf_get(lpfc_acqe_link_speed, acqe_link));
3139 link_speed = LA_UNKNW_LINK;
3140 break;
3141 }
3142 return link_speed;
3143}
3144
3145/**
3146 * lpfc_sli4_async_link_evt - Process the asynchronous link event
3147 * @phba: pointer to lpfc hba data structure.
3148 * @acqe_link: pointer to the async link completion queue entry.
3149 *
3150 * This routine is to handle the SLI4 asynchronous link event.
3151 **/
3152static void
3153lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3154 struct lpfc_acqe_link *acqe_link)
3155{
3156 struct lpfc_dmabuf *mp;
3157 LPFC_MBOXQ_t *pmb;
3158 MAILBOX_t *mb;
3159 READ_LA_VAR *la;
3160 uint8_t att_type;
3161
3162 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
3163 if (att_type != AT_LINK_DOWN && att_type != AT_LINK_UP)
3164 return;
James Smart32b97932009-07-19 10:01:21 -04003165 phba->fcoe_eventtag = acqe_link->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003166 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3167 if (!pmb) {
3168 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3169 "0395 The mboxq allocation failed\n");
3170 return;
3171 }
3172 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3173 if (!mp) {
3174 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3175 "0396 The lpfc_dmabuf allocation failed\n");
3176 goto out_free_pmb;
3177 }
3178 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3179 if (!mp->virt) {
3180 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3181 "0397 The mbuf allocation failed\n");
3182 goto out_free_dmabuf;
3183 }
3184
3185 /* Cleanup any outstanding ELS commands */
3186 lpfc_els_flush_all_cmd(phba);
3187
3188 /* Block ELS IOCBs until we have done process link event */
3189 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3190
3191 /* Update link event statistics */
3192 phba->sli.slistat.link_event++;
3193
3194 /* Create pseudo lpfc_handle_latt mailbox command from link ACQE */
3195 lpfc_read_la(phba, pmb, mp);
3196 pmb->vport = phba->pport;
3197
3198 /* Parse and translate status field */
3199 mb = &pmb->u.mb;
3200 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
3201
3202 /* Parse and translate link attention fields */
3203 la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
3204 la->eventTag = acqe_link->event_tag;
3205 la->attType = att_type;
3206 la->UlnkSpeed = lpfc_sli4_parse_latt_link_speed(phba, acqe_link);
3207
3208 /* Fake the the following irrelvant fields */
3209 la->topology = TOPOLOGY_PT_PT;
3210 la->granted_AL_PA = 0;
3211 la->il = 0;
3212 la->pb = 0;
3213 la->fa = 0;
3214 la->mm = 0;
3215
3216 /* Keep the link status for extra SLI4 state machine reference */
3217 phba->sli4_hba.link_state.speed =
3218 bf_get(lpfc_acqe_link_speed, acqe_link);
3219 phba->sli4_hba.link_state.duplex =
3220 bf_get(lpfc_acqe_link_duplex, acqe_link);
3221 phba->sli4_hba.link_state.status =
3222 bf_get(lpfc_acqe_link_status, acqe_link);
3223 phba->sli4_hba.link_state.physical =
3224 bf_get(lpfc_acqe_link_physical, acqe_link);
3225 phba->sli4_hba.link_state.fault =
3226 bf_get(lpfc_acqe_link_fault, acqe_link);
James Smart65467b62010-01-26 23:08:29 -05003227 phba->sli4_hba.link_state.logical_speed =
3228 bf_get(lpfc_acqe_qos_link_speed, acqe_link);
James Smartda0436e2009-05-22 14:51:39 -04003229
3230 /* Invoke the lpfc_handle_latt mailbox command callback function */
3231 lpfc_mbx_cmpl_read_la(phba, pmb);
3232
3233 return;
3234
3235out_free_dmabuf:
3236 kfree(mp);
3237out_free_pmb:
3238 mempool_free(pmb, phba->mbox_mem_pool);
3239}
3240
3241/**
James Smartfc2b9892010-02-26 14:15:29 -05003242 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
3243 * @vport: pointer to vport data structure.
3244 *
3245 * This routine is to perform Clear Virtual Link (CVL) on a vport in
3246 * response to a CVL event.
3247 *
3248 * Return the pointer to the ndlp with the vport if successful, otherwise
3249 * return NULL.
3250 **/
3251static struct lpfc_nodelist *
3252lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
3253{
3254 struct lpfc_nodelist *ndlp;
3255 struct Scsi_Host *shost;
3256 struct lpfc_hba *phba;
3257
3258 if (!vport)
3259 return NULL;
James Smartfc2b9892010-02-26 14:15:29 -05003260 phba = vport->phba;
3261 if (!phba)
3262 return NULL;
James Smart78730cf2010-04-06 15:06:30 -04003263 ndlp = lpfc_findnode_did(vport, Fabric_DID);
3264 if (!ndlp) {
3265 /* Cannot find existing Fabric ndlp, so allocate a new one */
3266 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
3267 if (!ndlp)
3268 return 0;
3269 lpfc_nlp_init(vport, ndlp, Fabric_DID);
3270 /* Set the node type */
3271 ndlp->nlp_type |= NLP_FABRIC;
3272 /* Put ndlp onto node list */
3273 lpfc_enqueue_node(vport, ndlp);
3274 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
3275 /* re-setup ndlp without removing from node list */
3276 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
3277 if (!ndlp)
3278 return 0;
3279 }
James Smartfc2b9892010-02-26 14:15:29 -05003280 if (phba->pport->port_state <= LPFC_FLOGI)
3281 return NULL;
3282 /* If virtual link is not yet instantiated ignore CVL */
3283 if (vport->port_state <= LPFC_FDISC)
3284 return NULL;
3285 shost = lpfc_shost_from_vport(vport);
3286 if (!shost)
3287 return NULL;
3288 lpfc_linkdown_port(vport);
3289 lpfc_cleanup_pending_mbox(vport);
3290 spin_lock_irq(shost->host_lock);
3291 vport->fc_flag |= FC_VPORT_CVL_RCVD;
3292 spin_unlock_irq(shost->host_lock);
3293
3294 return ndlp;
3295}
3296
3297/**
3298 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
3299 * @vport: pointer to lpfc hba data structure.
3300 *
3301 * This routine is to perform Clear Virtual Link (CVL) on all vports in
3302 * response to a FCF dead event.
3303 **/
3304static void
3305lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
3306{
3307 struct lpfc_vport **vports;
3308 int i;
3309
3310 vports = lpfc_create_vport_work_array(phba);
3311 if (vports)
3312 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
3313 lpfc_sli4_perform_vport_cvl(vports[i]);
3314 lpfc_destroy_vport_work_array(phba, vports);
3315}
3316
3317/**
James Smartda0436e2009-05-22 14:51:39 -04003318 * lpfc_sli4_async_fcoe_evt - Process the asynchronous fcoe event
3319 * @phba: pointer to lpfc hba data structure.
3320 * @acqe_link: pointer to the async fcoe completion queue entry.
3321 *
3322 * This routine is to handle the SLI4 asynchronous fcoe event.
3323 **/
3324static void
3325lpfc_sli4_async_fcoe_evt(struct lpfc_hba *phba,
3326 struct lpfc_acqe_fcoe *acqe_fcoe)
3327{
3328 uint8_t event_type = bf_get(lpfc_acqe_fcoe_event_type, acqe_fcoe);
3329 int rc;
James Smart6669f9b2009-10-02 15:16:45 -04003330 struct lpfc_vport *vport;
3331 struct lpfc_nodelist *ndlp;
3332 struct Scsi_Host *shost;
James Smart695a8142010-01-26 23:08:03 -05003333 int active_vlink_present;
3334 struct lpfc_vport **vports;
3335 int i;
James Smartda0436e2009-05-22 14:51:39 -04003336
James Smart4d9ab992009-10-02 15:16:39 -04003337 phba->fc_eventTag = acqe_fcoe->event_tag;
James Smart32b97932009-07-19 10:01:21 -04003338 phba->fcoe_eventtag = acqe_fcoe->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003339 switch (event_type) {
3340 case LPFC_FCOE_EVENT_TYPE_NEW_FCF:
James Smartecfd03c2010-02-12 14:41:27 -05003341 case LPFC_FCOE_EVENT_TYPE_FCF_PARAM_MOD:
James Smart999d8132010-03-15 11:24:56 -04003342 if (event_type == LPFC_FCOE_EVENT_TYPE_NEW_FCF)
3343 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3344 LOG_DISCOVERY,
3345 "2546 New FCF found event: "
3346 "evt_tag:x%x, fcf_index:x%x\n",
3347 acqe_fcoe->event_tag,
3348 acqe_fcoe->index);
3349 else
3350 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
3351 LOG_DISCOVERY,
3352 "2788 FCF parameter modified event: "
3353 "evt_tag:x%x, fcf_index:x%x\n",
3354 acqe_fcoe->event_tag,
3355 acqe_fcoe->index);
James Smartda0436e2009-05-22 14:51:39 -04003356 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05003357 if ((phba->fcf.fcf_flag & FCF_SCAN_DONE) ||
3358 (phba->hba_flag & FCF_DISC_INPROGRESS)) {
3359 /*
3360 * If the current FCF is in discovered state or
3361 * FCF discovery is in progress, do nothing.
3362 */
3363 spin_unlock_irq(&phba->hbalock);
3364 break;
3365 }
James Smart0c9ab6f2010-02-26 14:15:57 -05003366
James Smartecfd03c2010-02-12 14:41:27 -05003367 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
3368 /*
3369 * If fast FCF failover rescan event is pending,
3370 * do nothing.
3371 */
James Smartda0436e2009-05-22 14:51:39 -04003372 spin_unlock_irq(&phba->hbalock);
3373 break;
3374 }
3375 spin_unlock_irq(&phba->hbalock);
3376
James Smart0c9ab6f2010-02-26 14:15:57 -05003377 if ((phba->fcf.fcf_flag & FCF_DISCOVERY) &&
3378 !(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
3379 /*
3380 * During period of FCF discovery, read the FCF
3381 * table record indexed by the event to update
3382 * FCF round robin failover eligible FCF bmask.
3383 */
3384 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
3385 LOG_DISCOVERY,
3386 "2779 Read new FCF record with "
3387 "fcf_index:x%x for updating FCF "
3388 "round robin failover bmask\n",
3389 acqe_fcoe->index);
3390 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fcoe->index);
3391 }
3392
3393 /* Otherwise, scan the entire FCF table and re-discover SAN */
3394 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3395 "2770 Start FCF table scan due to new FCF "
3396 "event: evt_tag:x%x, fcf_index:x%x\n",
3397 acqe_fcoe->event_tag, acqe_fcoe->index);
3398 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3399 LPFC_FCOE_FCF_GET_FIRST);
James Smartda0436e2009-05-22 14:51:39 -04003400 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05003401 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3402 "2547 Issue FCF scan read FCF mailbox "
3403 "command failed 0x%x\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04003404 break;
3405
3406 case LPFC_FCOE_EVENT_TYPE_FCF_TABLE_FULL:
3407 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte4e74272009-07-19 10:01:38 -04003408 "2548 FCF Table full count 0x%x tag 0x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04003409 bf_get(lpfc_acqe_fcoe_fcf_count, acqe_fcoe),
3410 acqe_fcoe->event_tag);
3411 break;
3412
3413 case LPFC_FCOE_EVENT_TYPE_FCF_DEAD:
James Smart0c9ab6f2010-02-26 14:15:57 -05003414 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6a9c52c2009-10-02 15:16:51 -04003415 "2549 FCF disconnected from network index 0x%x"
James Smart6669f9b2009-10-02 15:16:45 -04003416 " tag 0x%x\n", acqe_fcoe->index,
James Smartda0436e2009-05-22 14:51:39 -04003417 acqe_fcoe->event_tag);
3418 /* If the event is not for currently used fcf do nothing */
James Smartecfd03c2010-02-12 14:41:27 -05003419 if (phba->fcf.current_rec.fcf_indx != acqe_fcoe->index)
James Smartda0436e2009-05-22 14:51:39 -04003420 break;
James Smartfc2b9892010-02-26 14:15:29 -05003421 /* We request port to rediscover the entire FCF table for
3422 * a fast recovery from case that the current FCF record
James Smart0c9ab6f2010-02-26 14:15:57 -05003423 * is no longer valid if we are not in the middle of FCF
3424 * failover process already.
James Smartda0436e2009-05-22 14:51:39 -04003425 */
James Smartfc2b9892010-02-26 14:15:29 -05003426 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003427 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05003428 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003429 /* Update FLOGI FCF failover eligible FCF bmask */
3430 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fcoe->index);
James Smartfc2b9892010-02-26 14:15:29 -05003431 break;
3432 }
3433 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05003434 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003435 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003436 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3437 "2771 Start FCF fast failover process due to "
3438 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
3439 "\n", acqe_fcoe->event_tag, acqe_fcoe->index);
James Smartfc2b9892010-02-26 14:15:29 -05003440 rc = lpfc_sli4_redisc_fcf_table(phba);
3441 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05003442 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3443 LOG_DISCOVERY,
3444 "2772 Issue FCF rediscover mabilbox "
3445 "command failed, fail through to FCF "
3446 "dead event\n");
James Smartfc2b9892010-02-26 14:15:29 -05003447 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003448 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003449 spin_unlock_irq(&phba->hbalock);
3450 /*
3451 * Last resort will fail over by treating this
3452 * as a link down to FCF registration.
3453 */
3454 lpfc_sli4_fcf_dead_failthrough(phba);
3455 } else
3456 /* Handling fast FCF failover to a DEAD FCF event
3457 * is considered equalivant to receiving CVL to all
3458 * vports.
3459 */
3460 lpfc_sli4_perform_all_vport_cvl(phba);
James Smartda0436e2009-05-22 14:51:39 -04003461 break;
James Smart6669f9b2009-10-02 15:16:45 -04003462 case LPFC_FCOE_EVENT_TYPE_CVL:
James Smart0c9ab6f2010-02-26 14:15:57 -05003463 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
James Smart6669f9b2009-10-02 15:16:45 -04003464 "2718 Clear Virtual Link Received for VPI 0x%x"
3465 " tag 0x%x\n", acqe_fcoe->index, acqe_fcoe->event_tag);
3466 vport = lpfc_find_vport_by_vpid(phba,
James Smartc8685952009-11-18 15:39:16 -05003467 acqe_fcoe->index - phba->vpi_base);
James Smartfc2b9892010-02-26 14:15:29 -05003468 ndlp = lpfc_sli4_perform_vport_cvl(vport);
James Smart6669f9b2009-10-02 15:16:45 -04003469 if (!ndlp)
3470 break;
James Smart695a8142010-01-26 23:08:03 -05003471 active_vlink_present = 0;
3472
3473 vports = lpfc_create_vport_work_array(phba);
3474 if (vports) {
3475 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
3476 i++) {
3477 if ((!(vports[i]->fc_flag &
3478 FC_VPORT_CVL_RCVD)) &&
3479 (vports[i]->port_state > LPFC_FDISC)) {
3480 active_vlink_present = 1;
3481 break;
3482 }
3483 }
3484 lpfc_destroy_vport_work_array(phba, vports);
3485 }
3486
3487 if (active_vlink_present) {
3488 /*
3489 * If there are other active VLinks present,
3490 * re-instantiate the Vlink using FDISC.
3491 */
James Smart6669f9b2009-10-02 15:16:45 -04003492 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
James Smartfc2b9892010-02-26 14:15:29 -05003493 shost = lpfc_shost_from_vport(vport);
James Smart6669f9b2009-10-02 15:16:45 -04003494 spin_lock_irq(shost->host_lock);
3495 ndlp->nlp_flag |= NLP_DELAY_TMO;
3496 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05003497 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
3498 vport->port_state = LPFC_FDISC;
3499 } else {
James Smartecfd03c2010-02-12 14:41:27 -05003500 /*
3501 * Otherwise, we request port to rediscover
3502 * the entire FCF table for a fast recovery
3503 * from possible case that the current FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05003504 * is no longer valid if we are not already
3505 * in the FCF failover process.
James Smartecfd03c2010-02-12 14:41:27 -05003506 */
James Smartfc2b9892010-02-26 14:15:29 -05003507 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003508 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
James Smartfc2b9892010-02-26 14:15:29 -05003509 spin_unlock_irq(&phba->hbalock);
3510 break;
3511 }
3512 /* Mark the fast failover process in progress */
James Smart0c9ab6f2010-02-26 14:15:57 -05003513 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003514 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003515 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
3516 LOG_DISCOVERY,
3517 "2773 Start FCF fast failover due "
3518 "to CVL event: evt_tag:x%x\n",
3519 acqe_fcoe->event_tag);
James Smartecfd03c2010-02-12 14:41:27 -05003520 rc = lpfc_sli4_redisc_fcf_table(phba);
James Smartfc2b9892010-02-26 14:15:29 -05003521 if (rc) {
James Smart0c9ab6f2010-02-26 14:15:57 -05003522 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3523 LOG_DISCOVERY,
3524 "2774 Issue FCF rediscover "
3525 "mabilbox command failed, "
3526 "through to CVL event\n");
James Smartfc2b9892010-02-26 14:15:29 -05003527 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003528 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -05003529 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05003530 /*
3531 * Last resort will be re-try on the
3532 * the current registered FCF entry.
3533 */
3534 lpfc_retry_pport_discovery(phba);
James Smartfc2b9892010-02-26 14:15:29 -05003535 }
James Smart6669f9b2009-10-02 15:16:45 -04003536 }
3537 break;
James Smartda0436e2009-05-22 14:51:39 -04003538 default:
3539 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3540 "0288 Unknown FCoE event type 0x%x event tag "
3541 "0x%x\n", event_type, acqe_fcoe->event_tag);
3542 break;
3543 }
3544}
3545
3546/**
3547 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
3548 * @phba: pointer to lpfc hba data structure.
3549 * @acqe_link: pointer to the async dcbx completion queue entry.
3550 *
3551 * This routine is to handle the SLI4 asynchronous dcbx event.
3552 **/
3553static void
3554lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
3555 struct lpfc_acqe_dcbx *acqe_dcbx)
3556{
James Smart4d9ab992009-10-02 15:16:39 -04003557 phba->fc_eventTag = acqe_dcbx->event_tag;
James Smartda0436e2009-05-22 14:51:39 -04003558 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3559 "0290 The SLI4 DCBX asynchronous event is not "
3560 "handled yet\n");
3561}
3562
3563/**
James Smartb19a0612010-04-06 14:48:51 -04003564 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
3565 * @phba: pointer to lpfc hba data structure.
3566 * @acqe_link: pointer to the async grp5 completion queue entry.
3567 *
3568 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
3569 * is an asynchronous notified of a logical link speed change. The Port
3570 * reports the logical link speed in units of 10Mbps.
3571 **/
3572static void
3573lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
3574 struct lpfc_acqe_grp5 *acqe_grp5)
3575{
3576 uint16_t prev_ll_spd;
3577
3578 phba->fc_eventTag = acqe_grp5->event_tag;
3579 phba->fcoe_eventtag = acqe_grp5->event_tag;
3580 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
3581 phba->sli4_hba.link_state.logical_speed =
3582 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5));
3583 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3584 "2789 GRP5 Async Event: Updating logical link speed "
3585 "from %dMbps to %dMbps\n", (prev_ll_spd * 10),
3586 (phba->sli4_hba.link_state.logical_speed*10));
3587}
3588
3589/**
James Smartda0436e2009-05-22 14:51:39 -04003590 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
3591 * @phba: pointer to lpfc hba data structure.
3592 *
3593 * This routine is invoked by the worker thread to process all the pending
3594 * SLI4 asynchronous events.
3595 **/
3596void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
3597{
3598 struct lpfc_cq_event *cq_event;
3599
3600 /* First, declare the async event has been handled */
3601 spin_lock_irq(&phba->hbalock);
3602 phba->hba_flag &= ~ASYNC_EVENT;
3603 spin_unlock_irq(&phba->hbalock);
3604 /* Now, handle all the async events */
3605 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
3606 /* Get the first event from the head of the event queue */
3607 spin_lock_irq(&phba->hbalock);
3608 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
3609 cq_event, struct lpfc_cq_event, list);
3610 spin_unlock_irq(&phba->hbalock);
3611 /* Process the asynchronous event */
3612 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
3613 case LPFC_TRAILER_CODE_LINK:
3614 lpfc_sli4_async_link_evt(phba,
3615 &cq_event->cqe.acqe_link);
3616 break;
3617 case LPFC_TRAILER_CODE_FCOE:
3618 lpfc_sli4_async_fcoe_evt(phba,
3619 &cq_event->cqe.acqe_fcoe);
3620 break;
3621 case LPFC_TRAILER_CODE_DCBX:
3622 lpfc_sli4_async_dcbx_evt(phba,
3623 &cq_event->cqe.acqe_dcbx);
3624 break;
James Smartb19a0612010-04-06 14:48:51 -04003625 case LPFC_TRAILER_CODE_GRP5:
3626 lpfc_sli4_async_grp5_evt(phba,
3627 &cq_event->cqe.acqe_grp5);
3628 break;
James Smartda0436e2009-05-22 14:51:39 -04003629 default:
3630 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3631 "1804 Invalid asynchrous event code: "
3632 "x%x\n", bf_get(lpfc_trailer_code,
3633 &cq_event->cqe.mcqe_cmpl));
3634 break;
3635 }
3636 /* Free the completion event processed to the free pool */
3637 lpfc_sli4_cq_event_release(phba, cq_event);
3638 }
3639}
3640
3641/**
James Smartecfd03c2010-02-12 14:41:27 -05003642 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
3643 * @phba: pointer to lpfc hba data structure.
3644 *
3645 * This routine is invoked by the worker thread to process FCF table
3646 * rediscovery pending completion event.
3647 **/
3648void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
3649{
3650 int rc;
3651
3652 spin_lock_irq(&phba->hbalock);
3653 /* Clear FCF rediscovery timeout event */
3654 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
3655 /* Clear driver fast failover FCF record flag */
3656 phba->fcf.failover_rec.flag = 0;
3657 /* Set state for FCF fast failover */
3658 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
3659 spin_unlock_irq(&phba->hbalock);
3660
3661 /* Scan FCF table from the first entry to re-discover SAN */
James Smart0c9ab6f2010-02-26 14:15:57 -05003662 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3663 "2777 Start FCF table scan after FCF "
3664 "rediscovery quiescent period over\n");
3665 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smartecfd03c2010-02-12 14:41:27 -05003666 if (rc)
James Smart0c9ab6f2010-02-26 14:15:57 -05003667 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3668 "2747 Issue FCF scan read FCF mailbox "
3669 "command failed 0x%x\n", rc);
James Smartecfd03c2010-02-12 14:41:27 -05003670}
3671
3672/**
James Smartda0436e2009-05-22 14:51:39 -04003673 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
3674 * @phba: pointer to lpfc hba data structure.
3675 * @dev_grp: The HBA PCI-Device group number.
3676 *
3677 * This routine is invoked to set up the per HBA PCI-Device group function
3678 * API jump table entries.
3679 *
3680 * Return: 0 if success, otherwise -ENODEV
3681 **/
3682int
3683lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
James Smart5b75da22008-12-04 22:39:35 -05003684{
3685 int rc;
3686
James Smartda0436e2009-05-22 14:51:39 -04003687 /* Set up lpfc PCI-device group */
3688 phba->pci_dev_grp = dev_grp;
James Smart5b75da22008-12-04 22:39:35 -05003689
James Smartda0436e2009-05-22 14:51:39 -04003690 /* The LPFC_PCI_DEV_OC uses SLI4 */
3691 if (dev_grp == LPFC_PCI_DEV_OC)
3692 phba->sli_rev = LPFC_SLI_REV4;
James Smart5b75da22008-12-04 22:39:35 -05003693
James Smartda0436e2009-05-22 14:51:39 -04003694 /* Set up device INIT API function jump table */
3695 rc = lpfc_init_api_table_setup(phba, dev_grp);
3696 if (rc)
3697 return -ENODEV;
3698 /* Set up SCSI API function jump table */
3699 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
3700 if (rc)
3701 return -ENODEV;
3702 /* Set up SLI API function jump table */
3703 rc = lpfc_sli_api_table_setup(phba, dev_grp);
3704 if (rc)
3705 return -ENODEV;
3706 /* Set up MBOX API function jump table */
3707 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
3708 if (rc)
3709 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05003710
James Smartda0436e2009-05-22 14:51:39 -04003711 return 0;
James Smart5b75da22008-12-04 22:39:35 -05003712}
3713
3714/**
James Smart3621a712009-04-06 18:47:14 -04003715 * lpfc_log_intr_mode - Log the active interrupt mode
James Smart5b75da22008-12-04 22:39:35 -05003716 * @phba: pointer to lpfc hba data structure.
3717 * @intr_mode: active interrupt mode adopted.
3718 *
3719 * This routine it invoked to log the currently used active interrupt mode
3720 * to the device.
James Smart3772a992009-05-22 14:50:54 -04003721 **/
3722static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
James Smart5b75da22008-12-04 22:39:35 -05003723{
3724 switch (intr_mode) {
3725 case 0:
3726 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3727 "0470 Enable INTx interrupt mode.\n");
3728 break;
3729 case 1:
3730 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3731 "0481 Enabled MSI interrupt mode.\n");
3732 break;
3733 case 2:
3734 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3735 "0480 Enabled MSI-X interrupt mode.\n");
3736 break;
3737 default:
3738 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3739 "0482 Illegal interrupt mode.\n");
3740 break;
3741 }
3742 return;
3743}
3744
James Smart5b75da22008-12-04 22:39:35 -05003745/**
James Smart3772a992009-05-22 14:50:54 -04003746 * lpfc_enable_pci_dev - Enable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05003747 * @phba: pointer to lpfc hba data structure.
3748 *
James Smart3772a992009-05-22 14:50:54 -04003749 * This routine is invoked to enable the PCI device that is common to all
3750 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05003751 *
3752 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003753 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04003754 * other values - error
James Smart5b75da22008-12-04 22:39:35 -05003755 **/
James Smart3772a992009-05-22 14:50:54 -04003756static int
3757lpfc_enable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05003758{
James Smart3772a992009-05-22 14:50:54 -04003759 struct pci_dev *pdev;
3760 int bars;
James Smart5b75da22008-12-04 22:39:35 -05003761
James Smart3772a992009-05-22 14:50:54 -04003762 /* Obtain PCI device reference */
3763 if (!phba->pcidev)
3764 goto out_error;
3765 else
3766 pdev = phba->pcidev;
3767 /* Select PCI BARs */
3768 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3769 /* Enable PCI device */
3770 if (pci_enable_device_mem(pdev))
3771 goto out_error;
3772 /* Request PCI resource for the device */
3773 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
3774 goto out_disable_device;
3775 /* Set up device as PCI master and save state for EEH */
3776 pci_set_master(pdev);
3777 pci_try_set_mwi(pdev);
3778 pci_save_state(pdev);
James Smart5b75da22008-12-04 22:39:35 -05003779
James Smart3772a992009-05-22 14:50:54 -04003780 return 0;
James Smart5b75da22008-12-04 22:39:35 -05003781
James Smart3772a992009-05-22 14:50:54 -04003782out_disable_device:
3783 pci_disable_device(pdev);
3784out_error:
3785 return -ENODEV;
James Smart5b75da22008-12-04 22:39:35 -05003786}
3787
3788/**
James Smart3772a992009-05-22 14:50:54 -04003789 * lpfc_disable_pci_dev - Disable a generic PCI device.
James Smart5b75da22008-12-04 22:39:35 -05003790 * @phba: pointer to lpfc hba data structure.
3791 *
James Smart3772a992009-05-22 14:50:54 -04003792 * This routine is invoked to disable the PCI device that is common to all
3793 * PCI devices.
James Smart5b75da22008-12-04 22:39:35 -05003794 **/
3795static void
James Smart3772a992009-05-22 14:50:54 -04003796lpfc_disable_pci_dev(struct lpfc_hba *phba)
James Smart5b75da22008-12-04 22:39:35 -05003797{
James Smart3772a992009-05-22 14:50:54 -04003798 struct pci_dev *pdev;
3799 int bars;
James Smart5b75da22008-12-04 22:39:35 -05003800
James Smart3772a992009-05-22 14:50:54 -04003801 /* Obtain PCI device reference */
3802 if (!phba->pcidev)
3803 return;
3804 else
3805 pdev = phba->pcidev;
3806 /* Select PCI BARs */
3807 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3808 /* Release PCI resource and disable PCI device */
3809 pci_release_selected_regions(pdev, bars);
3810 pci_disable_device(pdev);
3811 /* Null out PCI private reference to driver */
3812 pci_set_drvdata(pdev, NULL);
James Smart5b75da22008-12-04 22:39:35 -05003813
3814 return;
3815}
3816
3817/**
James Smart3772a992009-05-22 14:50:54 -04003818 * lpfc_reset_hba - Reset a hba
3819 * @phba: pointer to lpfc hba data structure.
James Smarte59058c2008-08-24 21:49:00 -04003820 *
James Smart3772a992009-05-22 14:50:54 -04003821 * This routine is invoked to reset a hba device. It brings the HBA
3822 * offline, performs a board restart, and then brings the board back
3823 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
3824 * on outstanding mailbox commands.
James Smarte59058c2008-08-24 21:49:00 -04003825 **/
James Smart3772a992009-05-22 14:50:54 -04003826void
3827lpfc_reset_hba(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003828{
James Smart3772a992009-05-22 14:50:54 -04003829 /* If resets are disabled then set error state and return. */
3830 if (!phba->cfg_enable_hba_reset) {
3831 phba->link_state = LPFC_HBA_ERROR;
3832 return;
3833 }
3834 lpfc_offline_prep(phba);
3835 lpfc_offline(phba);
3836 lpfc_sli_brdrestart(phba);
3837 lpfc_online(phba);
3838 lpfc_unblock_mgmt_io(phba);
3839}
dea31012005-04-17 16:05:31 -05003840
James Smart3772a992009-05-22 14:50:54 -04003841/**
3842 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
3843 * @phba: pointer to lpfc hba data structure.
3844 *
3845 * This routine is invoked to set up the driver internal resources specific to
3846 * support the SLI-3 HBA device it attached to.
3847 *
3848 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003849 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04003850 * other values - error
3851 **/
3852static int
3853lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
3854{
3855 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003856
James Smart3772a992009-05-22 14:50:54 -04003857 /*
3858 * Initialize timers used by driver
3859 */
dea31012005-04-17 16:05:31 -05003860
James Smart3772a992009-05-22 14:50:54 -04003861 /* Heartbeat timer */
3862 init_timer(&phba->hb_tmofunc);
3863 phba->hb_tmofunc.function = lpfc_hb_timeout;
3864 phba->hb_tmofunc.data = (unsigned long)phba;
3865
3866 psli = &phba->sli;
3867 /* MBOX heartbeat timer */
3868 init_timer(&psli->mbox_tmo);
3869 psli->mbox_tmo.function = lpfc_mbox_timeout;
3870 psli->mbox_tmo.data = (unsigned long) phba;
3871 /* FCP polling mode timer */
3872 init_timer(&phba->fcp_poll_timer);
3873 phba->fcp_poll_timer.function = lpfc_poll_timeout;
3874 phba->fcp_poll_timer.data = (unsigned long) phba;
3875 /* Fabric block timer */
3876 init_timer(&phba->fabric_block_timer);
3877 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
3878 phba->fabric_block_timer.data = (unsigned long) phba;
3879 /* EA polling mode timer */
3880 init_timer(&phba->eratt_poll);
3881 phba->eratt_poll.function = lpfc_poll_eratt;
3882 phba->eratt_poll.data = (unsigned long) phba;
3883
3884 /* Host attention work mask setup */
3885 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
3886 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
3887
3888 /* Get all the module params for configuring this host */
3889 lpfc_get_cfgparam(phba);
James Smart49198b32010-04-06 15:04:33 -04003890 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
3891 phba->menlo_flag |= HBA_MENLO_SUPPORT;
3892 /* check for menlo minimum sg count */
3893 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
3894 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
3895 }
3896
James Smart3772a992009-05-22 14:50:54 -04003897 /*
3898 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
3899 * used to create the sg_dma_buf_pool must be dynamically calculated.
3900 * 2 segments are added since the IOCB needs a command and response bde.
3901 */
3902 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
3903 sizeof(struct fcp_rsp) +
3904 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
3905
3906 if (phba->cfg_enable_bg) {
3907 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT;
3908 phba->cfg_sg_dma_buf_size +=
3909 phba->cfg_prot_sg_seg_cnt * sizeof(struct ulp_bde64);
3910 }
3911
3912 /* Also reinitialize the host templates with new values. */
3913 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
3914 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
3915
3916 phba->max_vpi = LPFC_MAX_VPI;
3917 /* This will be set to correct value after config_port mbox */
3918 phba->max_vports = 0;
3919
3920 /*
3921 * Initialize the SLI Layer to run with lpfc HBAs.
3922 */
3923 lpfc_sli_setup(phba);
3924 lpfc_sli_queue_setup(phba);
3925
3926 /* Allocate device driver memory */
3927 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
3928 return -ENOMEM;
3929
3930 return 0;
3931}
3932
3933/**
3934 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
3935 * @phba: pointer to lpfc hba data structure.
3936 *
3937 * This routine is invoked to unset the driver internal resources set up
3938 * specific for supporting the SLI-3 HBA device it attached to.
3939 **/
3940static void
3941lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
3942{
3943 /* Free device driver memory allocated */
3944 lpfc_mem_free_all(phba);
3945
3946 return;
3947}
3948
3949/**
James Smartda0436e2009-05-22 14:51:39 -04003950 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
3951 * @phba: pointer to lpfc hba data structure.
3952 *
3953 * This routine is invoked to set up the driver internal resources specific to
3954 * support the SLI-4 HBA device it attached to.
3955 *
3956 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003957 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04003958 * other values - error
3959 **/
3960static int
3961lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
3962{
3963 struct lpfc_sli *psli;
James Smart28baac72010-02-12 14:42:03 -05003964 LPFC_MBOXQ_t *mboxq;
3965 int rc, i, hbq_count, buf_size, dma_buf_size, max_buf_size;
3966 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
3967 struct lpfc_mqe *mqe;
James Smart0c9ab6f2010-02-26 14:15:57 -05003968 int longs;
James Smartda0436e2009-05-22 14:51:39 -04003969
3970 /* Before proceed, wait for POST done and device ready */
3971 rc = lpfc_sli4_post_status_check(phba);
3972 if (rc)
3973 return -ENODEV;
3974
3975 /*
3976 * Initialize timers used by driver
3977 */
3978
3979 /* Heartbeat timer */
3980 init_timer(&phba->hb_tmofunc);
3981 phba->hb_tmofunc.function = lpfc_hb_timeout;
3982 phba->hb_tmofunc.data = (unsigned long)phba;
3983
3984 psli = &phba->sli;
3985 /* MBOX heartbeat timer */
3986 init_timer(&psli->mbox_tmo);
3987 psli->mbox_tmo.function = lpfc_mbox_timeout;
3988 psli->mbox_tmo.data = (unsigned long) phba;
3989 /* Fabric block timer */
3990 init_timer(&phba->fabric_block_timer);
3991 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
3992 phba->fabric_block_timer.data = (unsigned long) phba;
3993 /* EA polling mode timer */
3994 init_timer(&phba->eratt_poll);
3995 phba->eratt_poll.function = lpfc_poll_eratt;
3996 phba->eratt_poll.data = (unsigned long) phba;
James Smartecfd03c2010-02-12 14:41:27 -05003997 /* FCF rediscover timer */
3998 init_timer(&phba->fcf.redisc_wait);
3999 phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
4000 phba->fcf.redisc_wait.data = (unsigned long)phba;
4001
James Smartda0436e2009-05-22 14:51:39 -04004002 /*
4003 * We need to do a READ_CONFIG mailbox command here before
4004 * calling lpfc_get_cfgparam. For VFs this will report the
4005 * MAX_XRI, MAX_VPI, MAX_RPI, MAX_IOCB, and MAX_VFI settings.
4006 * All of the resources allocated
4007 * for this Port are tied to these values.
4008 */
4009 /* Get all the module params for configuring this host */
4010 lpfc_get_cfgparam(phba);
4011 phba->max_vpi = LPFC_MAX_VPI;
4012 /* This will be set to correct value after the read_config mbox */
4013 phba->max_vports = 0;
4014
4015 /* Program the default value of vlan_id and fc_map */
4016 phba->valid_vlan = 0;
4017 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4018 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4019 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4020
4021 /*
4022 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
4023 * used to create the sg_dma_buf_pool must be dynamically calculated.
4024 * 2 segments are added since the IOCB needs a command and response bde.
4025 * To insure that the scsi sgl does not cross a 4k page boundary only
James Smart28baac72010-02-12 14:42:03 -05004026 * sgl sizes of must be a power of 2.
James Smartda0436e2009-05-22 14:51:39 -04004027 */
James Smart28baac72010-02-12 14:42:03 -05004028 buf_size = (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp) +
4029 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge)));
4030 /* Feature Level 1 hardware is limited to 2 pages */
4031 if ((bf_get(lpfc_sli_intf_featurelevel1, &phba->sli4_hba.sli_intf) ==
4032 LPFC_SLI_INTF_FEATURELEVEL1_1))
4033 max_buf_size = LPFC_SLI4_FL1_MAX_BUF_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04004034 else
James Smart28baac72010-02-12 14:42:03 -05004035 max_buf_size = LPFC_SLI4_MAX_BUF_SIZE;
4036 for (dma_buf_size = LPFC_SLI4_MIN_BUF_SIZE;
4037 dma_buf_size < max_buf_size && buf_size > dma_buf_size;
4038 dma_buf_size = dma_buf_size << 1)
4039 ;
4040 if (dma_buf_size == max_buf_size)
4041 phba->cfg_sg_seg_cnt = (dma_buf_size -
4042 sizeof(struct fcp_cmnd) - sizeof(struct fcp_rsp) -
4043 (2 * sizeof(struct sli4_sge))) /
4044 sizeof(struct sli4_sge);
4045 phba->cfg_sg_dma_buf_size = dma_buf_size;
James Smartda0436e2009-05-22 14:51:39 -04004046
4047 /* Initialize buffer queue management fields */
4048 hbq_count = lpfc_sli_hbq_count();
4049 for (i = 0; i < hbq_count; ++i)
4050 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
4051 INIT_LIST_HEAD(&phba->rb_pend_list);
4052 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
4053 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
4054
4055 /*
4056 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
4057 */
4058 /* Initialize the Abort scsi buffer list used by driver */
4059 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
4060 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
4061 /* This abort list used by worker thread */
4062 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
4063
4064 /*
4065 * Initialize dirver internal slow-path work queues
4066 */
4067
4068 /* Driver internel slow-path CQ Event pool */
4069 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
4070 /* Response IOCB work queue list */
James Smart45ed1192009-10-02 15:17:02 -04004071 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
James Smartda0436e2009-05-22 14:51:39 -04004072 /* Asynchronous event CQ Event work queue list */
4073 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
4074 /* Fast-path XRI aborted CQ Event work queue list */
4075 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
4076 /* Slow-path XRI aborted CQ Event work queue list */
4077 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
4078 /* Receive queue CQ Event work queue list */
4079 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
4080
4081 /* Initialize the driver internal SLI layer lists. */
4082 lpfc_sli_setup(phba);
4083 lpfc_sli_queue_setup(phba);
4084
4085 /* Allocate device driver memory */
4086 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
4087 if (rc)
4088 return -ENOMEM;
4089
4090 /* Create the bootstrap mailbox command */
4091 rc = lpfc_create_bootstrap_mbox(phba);
4092 if (unlikely(rc))
4093 goto out_free_mem;
4094
4095 /* Set up the host's endian order with the device. */
4096 rc = lpfc_setup_endian_order(phba);
4097 if (unlikely(rc))
4098 goto out_free_bsmbx;
4099
James Smart6669f9b2009-10-02 15:16:45 -04004100 rc = lpfc_sli4_fw_cfg_check(phba);
4101 if (unlikely(rc))
4102 goto out_free_bsmbx;
4103
James Smartda0436e2009-05-22 14:51:39 -04004104 /* Set up the hba's configuration parameters. */
4105 rc = lpfc_sli4_read_config(phba);
4106 if (unlikely(rc))
4107 goto out_free_bsmbx;
4108
4109 /* Perform a function reset */
4110 rc = lpfc_pci_function_reset(phba);
4111 if (unlikely(rc))
4112 goto out_free_bsmbx;
4113
James Smartcb5172e2010-03-15 11:25:07 -04004114 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
4115 GFP_KERNEL);
4116 if (!mboxq) {
4117 rc = -ENOMEM;
4118 goto out_free_bsmbx;
4119 }
4120
4121 /* Get the Supported Pages. It is always available. */
4122 lpfc_supported_pages(mboxq);
4123 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4124 if (unlikely(rc)) {
4125 rc = -EIO;
4126 mempool_free(mboxq, phba->mbox_mem_pool);
4127 goto out_free_bsmbx;
4128 }
4129
4130 mqe = &mboxq->u.mqe;
4131 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
4132 LPFC_MAX_SUPPORTED_PAGES);
4133 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
4134 switch (pn_page[i]) {
4135 case LPFC_SLI4_PARAMETERS:
4136 phba->sli4_hba.pc_sli4_params.supported = 1;
4137 break;
4138 default:
4139 break;
4140 }
4141 }
4142
4143 /* Read the port's SLI4 Parameters capabilities if supported. */
4144 if (phba->sli4_hba.pc_sli4_params.supported)
4145 rc = lpfc_pc_sli4_params_get(phba, mboxq);
4146 mempool_free(mboxq, phba->mbox_mem_pool);
4147 if (rc) {
4148 rc = -EIO;
4149 goto out_free_bsmbx;
4150 }
James Smartda0436e2009-05-22 14:51:39 -04004151 /* Create all the SLI4 queues */
4152 rc = lpfc_sli4_queue_create(phba);
4153 if (rc)
4154 goto out_free_bsmbx;
4155
4156 /* Create driver internal CQE event pool */
4157 rc = lpfc_sli4_cq_event_pool_create(phba);
4158 if (rc)
4159 goto out_destroy_queue;
4160
4161 /* Initialize and populate the iocb list per host */
4162 rc = lpfc_init_sgl_list(phba);
4163 if (rc) {
4164 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4165 "1400 Failed to initialize sgl list.\n");
4166 goto out_destroy_cq_event_pool;
4167 }
4168 rc = lpfc_init_active_sgl_array(phba);
4169 if (rc) {
4170 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4171 "1430 Failed to initialize sgl list.\n");
4172 goto out_free_sgl_list;
4173 }
4174
4175 rc = lpfc_sli4_init_rpi_hdrs(phba);
4176 if (rc) {
4177 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4178 "1432 Failed to initialize rpi headers.\n");
4179 goto out_free_active_sgl;
4180 }
4181
James Smart0c9ab6f2010-02-26 14:15:57 -05004182 /* Allocate eligible FCF bmask memory for FCF round robin failover */
4183 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
4184 phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
4185 GFP_KERNEL);
4186 if (!phba->fcf.fcf_rr_bmask) {
4187 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4188 "2759 Failed allocate memory for FCF round "
4189 "robin failover bmask\n");
4190 goto out_remove_rpi_hdrs;
4191 }
4192
James Smartda0436e2009-05-22 14:51:39 -04004193 phba->sli4_hba.fcp_eq_hdl = kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
4194 phba->cfg_fcp_eq_count), GFP_KERNEL);
4195 if (!phba->sli4_hba.fcp_eq_hdl) {
4196 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4197 "2572 Failed allocate memory for fast-path "
4198 "per-EQ handle array\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05004199 goto out_free_fcf_rr_bmask;
James Smartda0436e2009-05-22 14:51:39 -04004200 }
4201
4202 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
4203 phba->sli4_hba.cfg_eqn), GFP_KERNEL);
4204 if (!phba->sli4_hba.msix_entries) {
4205 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4206 "2573 Failed allocate memory for msi-x "
4207 "interrupt vector entries\n");
4208 goto out_free_fcp_eq_hdl;
4209 }
4210
4211 return rc;
4212
4213out_free_fcp_eq_hdl:
4214 kfree(phba->sli4_hba.fcp_eq_hdl);
James Smart0c9ab6f2010-02-26 14:15:57 -05004215out_free_fcf_rr_bmask:
4216 kfree(phba->fcf.fcf_rr_bmask);
James Smartda0436e2009-05-22 14:51:39 -04004217out_remove_rpi_hdrs:
4218 lpfc_sli4_remove_rpi_hdrs(phba);
4219out_free_active_sgl:
4220 lpfc_free_active_sgl(phba);
4221out_free_sgl_list:
4222 lpfc_free_sgl_list(phba);
4223out_destroy_cq_event_pool:
4224 lpfc_sli4_cq_event_pool_destroy(phba);
4225out_destroy_queue:
4226 lpfc_sli4_queue_destroy(phba);
4227out_free_bsmbx:
4228 lpfc_destroy_bootstrap_mbox(phba);
4229out_free_mem:
4230 lpfc_mem_free(phba);
4231 return rc;
4232}
4233
4234/**
4235 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
4236 * @phba: pointer to lpfc hba data structure.
4237 *
4238 * This routine is invoked to unset the driver internal resources set up
4239 * specific for supporting the SLI-4 HBA device it attached to.
4240 **/
4241static void
4242lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
4243{
4244 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
4245
4246 /* unregister default FCFI from the HBA */
4247 lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
4248
4249 /* Free the default FCR table */
4250 lpfc_sli_remove_dflt_fcf(phba);
4251
4252 /* Free memory allocated for msi-x interrupt vector entries */
4253 kfree(phba->sli4_hba.msix_entries);
4254
4255 /* Free memory allocated for fast-path work queue handles */
4256 kfree(phba->sli4_hba.fcp_eq_hdl);
4257
4258 /* Free the allocated rpi headers. */
4259 lpfc_sli4_remove_rpi_hdrs(phba);
James Smartd11e31d2009-06-10 17:23:06 -04004260 lpfc_sli4_remove_rpis(phba);
James Smartda0436e2009-05-22 14:51:39 -04004261
James Smart0c9ab6f2010-02-26 14:15:57 -05004262 /* Free eligible FCF index bmask */
4263 kfree(phba->fcf.fcf_rr_bmask);
4264
James Smartda0436e2009-05-22 14:51:39 -04004265 /* Free the ELS sgl list */
4266 lpfc_free_active_sgl(phba);
4267 lpfc_free_sgl_list(phba);
4268
4269 /* Free the SCSI sgl management array */
4270 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
4271
4272 /* Free the SLI4 queues */
4273 lpfc_sli4_queue_destroy(phba);
4274
4275 /* Free the completion queue EQ event pool */
4276 lpfc_sli4_cq_event_release_all(phba);
4277 lpfc_sli4_cq_event_pool_destroy(phba);
4278
4279 /* Reset SLI4 HBA FCoE function */
4280 lpfc_pci_function_reset(phba);
4281
4282 /* Free the bsmbx region. */
4283 lpfc_destroy_bootstrap_mbox(phba);
4284
4285 /* Free the SLI Layer memory with SLI4 HBAs */
4286 lpfc_mem_free_all(phba);
4287
4288 /* Free the current connect table */
4289 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04004290 &phba->fcf_conn_rec_list, list) {
4291 list_del_init(&conn_entry->list);
James Smartda0436e2009-05-22 14:51:39 -04004292 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04004293 }
James Smartda0436e2009-05-22 14:51:39 -04004294
4295 return;
4296}
4297
4298/**
James Smart3772a992009-05-22 14:50:54 -04004299 * lpfc_init_api_table_setup - Set up init api fucntion jump table
4300 * @phba: The hba struct for which this call is being executed.
4301 * @dev_grp: The HBA PCI-Device group number.
4302 *
4303 * This routine sets up the device INIT interface API function jump table
4304 * in @phba struct.
4305 *
4306 * Returns: 0 - success, -ENODEV - failure.
4307 **/
4308int
4309lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4310{
James Smart84d1b002010-02-12 14:42:33 -05004311 phba->lpfc_hba_init_link = lpfc_hba_init_link;
4312 phba->lpfc_hba_down_link = lpfc_hba_down_link;
James Smart3772a992009-05-22 14:50:54 -04004313 switch (dev_grp) {
4314 case LPFC_PCI_DEV_LP:
4315 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
4316 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
4317 phba->lpfc_stop_port = lpfc_stop_port_s3;
4318 break;
James Smartda0436e2009-05-22 14:51:39 -04004319 case LPFC_PCI_DEV_OC:
4320 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
4321 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
4322 phba->lpfc_stop_port = lpfc_stop_port_s4;
4323 break;
James Smart3772a992009-05-22 14:50:54 -04004324 default:
4325 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4326 "1431 Invalid HBA PCI-device group: 0x%x\n",
4327 dev_grp);
4328 return -ENODEV;
4329 break;
4330 }
4331 return 0;
4332}
4333
4334/**
4335 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
4336 * @phba: pointer to lpfc hba data structure.
4337 *
4338 * This routine is invoked to set up the driver internal resources before the
4339 * device specific resource setup to support the HBA device it attached to.
4340 *
4341 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004342 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004343 * other values - error
4344 **/
4345static int
4346lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
4347{
4348 /*
4349 * Driver resources common to all SLI revisions
4350 */
James Smartea2151b2008-09-07 11:52:10 -04004351 atomic_set(&phba->fast_event_count, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004352 spin_lock_init(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004353
James Smarte47c9092008-02-08 18:49:26 -05004354 /* Initialize ndlp management spinlock */
4355 spin_lock_init(&phba->ndlp_lock);
4356
James Smart2e0fef82007-06-17 19:56:36 -05004357 INIT_LIST_HEAD(&phba->port_list);
James Smart3772a992009-05-22 14:50:54 -04004358 INIT_LIST_HEAD(&phba->work_list);
James Smart84774a42008-08-24 21:50:06 -04004359 init_waitqueue_head(&phba->wait_4_mlo_m_q);
dea31012005-04-17 16:05:31 -05004360
James Smart3772a992009-05-22 14:50:54 -04004361 /* Initialize the wait queue head for the kernel thread */
4362 init_waitqueue_head(&phba->work_waitq);
James Smart858c9f62007-06-17 19:56:39 -05004363
James Smart3772a992009-05-22 14:50:54 -04004364 /* Initialize the scsi buffer list used by driver for scsi IO */
4365 spin_lock_init(&phba->scsi_buf_list_lock);
4366 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
dea31012005-04-17 16:05:31 -05004367
James Smart3772a992009-05-22 14:50:54 -04004368 /* Initialize the fabric iocb list */
4369 INIT_LIST_HEAD(&phba->fabric_iocb_list);
dea31012005-04-17 16:05:31 -05004370
James Smart3772a992009-05-22 14:50:54 -04004371 /* Initialize list to save ELS buffers */
4372 INIT_LIST_HEAD(&phba->elsbuf);
dea31012005-04-17 16:05:31 -05004373
James Smart3772a992009-05-22 14:50:54 -04004374 /* Initialize FCF connection rec list */
4375 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
dea31012005-04-17 16:05:31 -05004376
James Smart3772a992009-05-22 14:50:54 -04004377 return 0;
4378}
dea31012005-04-17 16:05:31 -05004379
James Smart3772a992009-05-22 14:50:54 -04004380/**
4381 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
4382 * @phba: pointer to lpfc hba data structure.
4383 *
4384 * This routine is invoked to set up the driver internal resources after the
4385 * device specific resource setup to support the HBA device it attached to.
4386 *
4387 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004388 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004389 * other values - error
4390 **/
4391static int
4392lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
4393{
4394 int error;
4395
4396 /* Startup the kernel thread for this host adapter. */
4397 phba->worker_thread = kthread_run(lpfc_do_work, phba,
4398 "lpfc_worker_%d", phba->brd_no);
4399 if (IS_ERR(phba->worker_thread)) {
4400 error = PTR_ERR(phba->worker_thread);
4401 return error;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05004402 }
4403
James Smart3772a992009-05-22 14:50:54 -04004404 return 0;
4405}
4406
4407/**
4408 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
4409 * @phba: pointer to lpfc hba data structure.
4410 *
4411 * This routine is invoked to unset the driver internal resources set up after
4412 * the device specific resource setup for supporting the HBA device it
4413 * attached to.
4414 **/
4415static void
4416lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
4417{
4418 /* Stop kernel worker thread */
4419 kthread_stop(phba->worker_thread);
4420}
4421
4422/**
4423 * lpfc_free_iocb_list - Free iocb list.
4424 * @phba: pointer to lpfc hba data structure.
4425 *
4426 * This routine is invoked to free the driver's IOCB list and memory.
4427 **/
4428static void
4429lpfc_free_iocb_list(struct lpfc_hba *phba)
4430{
4431 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
4432
4433 spin_lock_irq(&phba->hbalock);
4434 list_for_each_entry_safe(iocbq_entry, iocbq_next,
4435 &phba->lpfc_iocb_list, list) {
4436 list_del(&iocbq_entry->list);
4437 kfree(iocbq_entry);
4438 phba->total_iocbq_bufs--;
Jamie Wellnitz901a9202006-02-28 19:25:19 -05004439 }
James Smart3772a992009-05-22 14:50:54 -04004440 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004441
James Smart3772a992009-05-22 14:50:54 -04004442 return;
4443}
dea31012005-04-17 16:05:31 -05004444
James Smart3772a992009-05-22 14:50:54 -04004445/**
4446 * lpfc_init_iocb_list - Allocate and initialize iocb list.
4447 * @phba: pointer to lpfc hba data structure.
4448 *
4449 * This routine is invoked to allocate and initizlize the driver's IOCB
4450 * list and set up the IOCB tag array accordingly.
4451 *
4452 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004453 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004454 * other values - error
4455 **/
4456static int
4457lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
4458{
4459 struct lpfc_iocbq *iocbq_entry = NULL;
4460 uint16_t iotag;
4461 int i;
dea31012005-04-17 16:05:31 -05004462
4463 /* Initialize and populate the iocb list per host. */
4464 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
James Smart3772a992009-05-22 14:50:54 -04004465 for (i = 0; i < iocb_count; i++) {
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07004466 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
dea31012005-04-17 16:05:31 -05004467 if (iocbq_entry == NULL) {
4468 printk(KERN_ERR "%s: only allocated %d iocbs of "
4469 "expected %d count. Unloading driver.\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07004470 __func__, i, LPFC_IOCB_LIST_CNT);
dea31012005-04-17 16:05:31 -05004471 goto out_free_iocbq;
4472 }
4473
James Bottomley604a3e32005-10-29 10:28:33 -05004474 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
4475 if (iotag == 0) {
James Smart3772a992009-05-22 14:50:54 -04004476 kfree(iocbq_entry);
James Bottomley604a3e32005-10-29 10:28:33 -05004477 printk(KERN_ERR "%s: failed to allocate IOTAG. "
James Smart3772a992009-05-22 14:50:54 -04004478 "Unloading driver.\n", __func__);
James Bottomley604a3e32005-10-29 10:28:33 -05004479 goto out_free_iocbq;
4480 }
James Smart3772a992009-05-22 14:50:54 -04004481 iocbq_entry->sli4_xritag = NO_XRI;
James Smart2e0fef82007-06-17 19:56:36 -05004482
4483 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004484 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
4485 phba->total_iocbq_bufs++;
James Smart2e0fef82007-06-17 19:56:36 -05004486 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004487 }
4488
James Smart3772a992009-05-22 14:50:54 -04004489 return 0;
4490
4491out_free_iocbq:
4492 lpfc_free_iocb_list(phba);
4493
4494 return -ENOMEM;
4495}
4496
4497/**
James Smartda0436e2009-05-22 14:51:39 -04004498 * lpfc_free_sgl_list - Free sgl list.
4499 * @phba: pointer to lpfc hba data structure.
4500 *
4501 * This routine is invoked to free the driver's sgl list and memory.
4502 **/
4503static void
4504lpfc_free_sgl_list(struct lpfc_hba *phba)
4505{
4506 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
4507 LIST_HEAD(sglq_list);
4508 int rc = 0;
4509
4510 spin_lock_irq(&phba->hbalock);
4511 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
4512 spin_unlock_irq(&phba->hbalock);
4513
4514 list_for_each_entry_safe(sglq_entry, sglq_next,
4515 &sglq_list, list) {
4516 list_del(&sglq_entry->list);
4517 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
4518 kfree(sglq_entry);
4519 phba->sli4_hba.total_sglq_bufs--;
4520 }
4521 rc = lpfc_sli4_remove_all_sgl_pages(phba);
4522 if (rc) {
4523 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04004524 "2005 Unable to deregister pages from HBA: %x\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04004525 }
4526 kfree(phba->sli4_hba.lpfc_els_sgl_array);
4527}
4528
4529/**
4530 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
4531 * @phba: pointer to lpfc hba data structure.
4532 *
4533 * This routine is invoked to allocate the driver's active sgl memory.
4534 * This array will hold the sglq_entry's for active IOs.
4535 **/
4536static int
4537lpfc_init_active_sgl_array(struct lpfc_hba *phba)
4538{
4539 int size;
4540 size = sizeof(struct lpfc_sglq *);
4541 size *= phba->sli4_hba.max_cfg_param.max_xri;
4542
4543 phba->sli4_hba.lpfc_sglq_active_list =
4544 kzalloc(size, GFP_KERNEL);
4545 if (!phba->sli4_hba.lpfc_sglq_active_list)
4546 return -ENOMEM;
4547 return 0;
4548}
4549
4550/**
4551 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
4552 * @phba: pointer to lpfc hba data structure.
4553 *
4554 * This routine is invoked to walk through the array of active sglq entries
4555 * and free all of the resources.
4556 * This is just a place holder for now.
4557 **/
4558static void
4559lpfc_free_active_sgl(struct lpfc_hba *phba)
4560{
4561 kfree(phba->sli4_hba.lpfc_sglq_active_list);
4562}
4563
4564/**
4565 * lpfc_init_sgl_list - Allocate and initialize sgl list.
4566 * @phba: pointer to lpfc hba data structure.
4567 *
4568 * This routine is invoked to allocate and initizlize the driver's sgl
4569 * list and set up the sgl xritag tag array accordingly.
4570 *
4571 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004572 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004573 * other values - error
4574 **/
4575static int
4576lpfc_init_sgl_list(struct lpfc_hba *phba)
4577{
4578 struct lpfc_sglq *sglq_entry = NULL;
4579 int i;
4580 int els_xri_cnt;
4581
4582 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
4583 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4584 "2400 lpfc_init_sgl_list els %d.\n",
4585 els_xri_cnt);
4586 /* Initialize and populate the sglq list per host/VF. */
4587 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
4588 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
4589
4590 /* Sanity check on XRI management */
4591 if (phba->sli4_hba.max_cfg_param.max_xri <= els_xri_cnt) {
4592 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4593 "2562 No room left for SCSI XRI allocation: "
4594 "max_xri=%d, els_xri=%d\n",
4595 phba->sli4_hba.max_cfg_param.max_xri,
4596 els_xri_cnt);
4597 return -ENOMEM;
4598 }
4599
4600 /* Allocate memory for the ELS XRI management array */
4601 phba->sli4_hba.lpfc_els_sgl_array =
4602 kzalloc((sizeof(struct lpfc_sglq *) * els_xri_cnt),
4603 GFP_KERNEL);
4604
4605 if (!phba->sli4_hba.lpfc_els_sgl_array) {
4606 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4607 "2401 Failed to allocate memory for ELS "
4608 "XRI management array of size %d.\n",
4609 els_xri_cnt);
4610 return -ENOMEM;
4611 }
4612
4613 /* Keep the SCSI XRI into the XRI management array */
4614 phba->sli4_hba.scsi_xri_max =
4615 phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
4616 phba->sli4_hba.scsi_xri_cnt = 0;
4617
4618 phba->sli4_hba.lpfc_scsi_psb_array =
4619 kzalloc((sizeof(struct lpfc_scsi_buf *) *
4620 phba->sli4_hba.scsi_xri_max), GFP_KERNEL);
4621
4622 if (!phba->sli4_hba.lpfc_scsi_psb_array) {
4623 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4624 "2563 Failed to allocate memory for SCSI "
4625 "XRI management array of size %d.\n",
4626 phba->sli4_hba.scsi_xri_max);
4627 kfree(phba->sli4_hba.lpfc_els_sgl_array);
4628 return -ENOMEM;
4629 }
4630
4631 for (i = 0; i < els_xri_cnt; i++) {
4632 sglq_entry = kzalloc(sizeof(struct lpfc_sglq), GFP_KERNEL);
4633 if (sglq_entry == NULL) {
4634 printk(KERN_ERR "%s: only allocated %d sgls of "
4635 "expected %d count. Unloading driver.\n",
4636 __func__, i, els_xri_cnt);
4637 goto out_free_mem;
4638 }
4639
4640 sglq_entry->sli4_xritag = lpfc_sli4_next_xritag(phba);
4641 if (sglq_entry->sli4_xritag == NO_XRI) {
4642 kfree(sglq_entry);
4643 printk(KERN_ERR "%s: failed to allocate XRI.\n"
4644 "Unloading driver.\n", __func__);
4645 goto out_free_mem;
4646 }
4647 sglq_entry->buff_type = GEN_BUFF_TYPE;
4648 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0, &sglq_entry->phys);
4649 if (sglq_entry->virt == NULL) {
4650 kfree(sglq_entry);
4651 printk(KERN_ERR "%s: failed to allocate mbuf.\n"
4652 "Unloading driver.\n", __func__);
4653 goto out_free_mem;
4654 }
4655 sglq_entry->sgl = sglq_entry->virt;
4656 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
4657
4658 /* The list order is used by later block SGL registraton */
4659 spin_lock_irq(&phba->hbalock);
James Smart0f65ff62010-02-26 14:14:23 -05004660 sglq_entry->state = SGL_FREED;
James Smartda0436e2009-05-22 14:51:39 -04004661 list_add_tail(&sglq_entry->list, &phba->sli4_hba.lpfc_sgl_list);
4662 phba->sli4_hba.lpfc_els_sgl_array[i] = sglq_entry;
4663 phba->sli4_hba.total_sglq_bufs++;
4664 spin_unlock_irq(&phba->hbalock);
4665 }
4666 return 0;
4667
4668out_free_mem:
4669 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
4670 lpfc_free_sgl_list(phba);
4671 return -ENOMEM;
4672}
4673
4674/**
4675 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
4676 * @phba: pointer to lpfc hba data structure.
4677 *
4678 * This routine is invoked to post rpi header templates to the
4679 * HBA consistent with the SLI-4 interface spec. This routine
4680 * posts a PAGE_SIZE memory region to the port to hold up to
4681 * PAGE_SIZE modulo 64 rpi context headers.
4682 * No locks are held here because this is an initialization routine
4683 * called only from probe or lpfc_online when interrupts are not
4684 * enabled and the driver is reinitializing the device.
4685 *
4686 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004687 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04004688 * ENOMEM - No availble memory
4689 * EIO - The mailbox failed to complete successfully.
4690 **/
4691int
4692lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
4693{
4694 int rc = 0;
4695 int longs;
4696 uint16_t rpi_count;
4697 struct lpfc_rpi_hdr *rpi_hdr;
4698
4699 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
4700
4701 /*
4702 * Provision an rpi bitmask range for discovery. The total count
4703 * is the difference between max and base + 1.
4704 */
4705 rpi_count = phba->sli4_hba.max_cfg_param.rpi_base +
4706 phba->sli4_hba.max_cfg_param.max_rpi - 1;
4707
4708 longs = ((rpi_count) + BITS_PER_LONG - 1) / BITS_PER_LONG;
4709 phba->sli4_hba.rpi_bmask = kzalloc(longs * sizeof(unsigned long),
4710 GFP_KERNEL);
4711 if (!phba->sli4_hba.rpi_bmask)
4712 return -ENOMEM;
4713
4714 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
4715 if (!rpi_hdr) {
4716 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4717 "0391 Error during rpi post operation\n");
4718 lpfc_sli4_remove_rpis(phba);
4719 rc = -ENODEV;
4720 }
4721
4722 return rc;
4723}
4724
4725/**
4726 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
4727 * @phba: pointer to lpfc hba data structure.
4728 *
4729 * This routine is invoked to allocate a single 4KB memory region to
4730 * support rpis and stores them in the phba. This single region
4731 * provides support for up to 64 rpis. The region is used globally
4732 * by the device.
4733 *
4734 * Returns:
4735 * A valid rpi hdr on success.
4736 * A NULL pointer on any failure.
4737 **/
4738struct lpfc_rpi_hdr *
4739lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
4740{
4741 uint16_t rpi_limit, curr_rpi_range;
4742 struct lpfc_dmabuf *dmabuf;
4743 struct lpfc_rpi_hdr *rpi_hdr;
4744
4745 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
4746 phba->sli4_hba.max_cfg_param.max_rpi - 1;
4747
4748 spin_lock_irq(&phba->hbalock);
4749 curr_rpi_range = phba->sli4_hba.next_rpi;
4750 spin_unlock_irq(&phba->hbalock);
4751
4752 /*
4753 * The port has a limited number of rpis. The increment here
4754 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
4755 * and to allow the full max_rpi range per port.
4756 */
4757 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
4758 return NULL;
4759
4760 /*
4761 * First allocate the protocol header region for the port. The
4762 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
4763 */
4764 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4765 if (!dmabuf)
4766 return NULL;
4767
4768 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4769 LPFC_HDR_TEMPLATE_SIZE,
4770 &dmabuf->phys,
4771 GFP_KERNEL);
4772 if (!dmabuf->virt) {
4773 rpi_hdr = NULL;
4774 goto err_free_dmabuf;
4775 }
4776
4777 memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
4778 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
4779 rpi_hdr = NULL;
4780 goto err_free_coherent;
4781 }
4782
4783 /* Save the rpi header data for cleanup later. */
4784 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
4785 if (!rpi_hdr)
4786 goto err_free_coherent;
4787
4788 rpi_hdr->dmabuf = dmabuf;
4789 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
4790 rpi_hdr->page_count = 1;
4791 spin_lock_irq(&phba->hbalock);
4792 rpi_hdr->start_rpi = phba->sli4_hba.next_rpi;
4793 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
4794
4795 /*
4796 * The next_rpi stores the next module-64 rpi value to post
4797 * in any subsequent rpi memory region postings.
4798 */
4799 phba->sli4_hba.next_rpi += LPFC_RPI_HDR_COUNT;
4800 spin_unlock_irq(&phba->hbalock);
4801 return rpi_hdr;
4802
4803 err_free_coherent:
4804 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
4805 dmabuf->virt, dmabuf->phys);
4806 err_free_dmabuf:
4807 kfree(dmabuf);
4808 return NULL;
4809}
4810
4811/**
4812 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
4813 * @phba: pointer to lpfc hba data structure.
4814 *
4815 * This routine is invoked to remove all memory resources allocated
4816 * to support rpis. This routine presumes the caller has released all
4817 * rpis consumed by fabric or port logins and is prepared to have
4818 * the header pages removed.
4819 **/
4820void
4821lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
4822{
4823 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
4824
4825 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
4826 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
4827 list_del(&rpi_hdr->list);
4828 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
4829 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
4830 kfree(rpi_hdr->dmabuf);
4831 kfree(rpi_hdr);
4832 }
4833
4834 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
4835 memset(phba->sli4_hba.rpi_bmask, 0, sizeof(*phba->sli4_hba.rpi_bmask));
4836}
4837
4838/**
James Smart3772a992009-05-22 14:50:54 -04004839 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
4840 * @pdev: pointer to pci device data structure.
4841 *
4842 * This routine is invoked to allocate the driver hba data structure for an
4843 * HBA device. If the allocation is successful, the phba reference to the
4844 * PCI device data structure is set.
4845 *
4846 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004847 * pointer to @phba - successful
James Smart3772a992009-05-22 14:50:54 -04004848 * NULL - error
4849 **/
4850static struct lpfc_hba *
4851lpfc_hba_alloc(struct pci_dev *pdev)
4852{
4853 struct lpfc_hba *phba;
4854
4855 /* Allocate memory for HBA structure */
4856 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
4857 if (!phba) {
Jiri Slabye34ccdf2009-07-13 23:25:54 +02004858 dev_err(&pdev->dev, "failed to allocate hba struct\n");
James Smart3772a992009-05-22 14:50:54 -04004859 return NULL;
4860 }
4861
4862 /* Set reference to PCI device in HBA structure */
4863 phba->pcidev = pdev;
4864
4865 /* Assign an unused board number */
4866 phba->brd_no = lpfc_get_instance();
4867 if (phba->brd_no < 0) {
4868 kfree(phba);
4869 return NULL;
4870 }
4871
James Smart4fede782010-01-26 23:08:55 -05004872 spin_lock_init(&phba->ct_ev_lock);
James Smartf1c3b0f2009-07-19 10:01:32 -04004873 INIT_LIST_HEAD(&phba->ct_ev_waiters);
4874
James Smart3772a992009-05-22 14:50:54 -04004875 return phba;
4876}
4877
4878/**
4879 * lpfc_hba_free - Free driver hba data structure with a device.
4880 * @phba: pointer to lpfc hba data structure.
4881 *
4882 * This routine is invoked to free the driver hba data structure with an
4883 * HBA device.
4884 **/
4885static void
4886lpfc_hba_free(struct lpfc_hba *phba)
4887{
4888 /* Release the driver assigned board number */
4889 idr_remove(&lpfc_hba_index, phba->brd_no);
4890
4891 kfree(phba);
4892 return;
4893}
4894
4895/**
4896 * lpfc_create_shost - Create hba physical port with associated scsi host.
4897 * @phba: pointer to lpfc hba data structure.
4898 *
4899 * This routine is invoked to create HBA physical port and associate a SCSI
4900 * host with it.
4901 *
4902 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004903 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04004904 * other values - error
4905 **/
4906static int
4907lpfc_create_shost(struct lpfc_hba *phba)
4908{
4909 struct lpfc_vport *vport;
4910 struct Scsi_Host *shost;
4911
4912 /* Initialize HBA FC structure */
dea31012005-04-17 16:05:31 -05004913 phba->fc_edtov = FF_DEF_EDTOV;
4914 phba->fc_ratov = FF_DEF_RATOV;
4915 phba->fc_altov = FF_DEF_ALTOV;
4916 phba->fc_arbtov = FF_DEF_ARBTOV;
4917
James Smart3de2a652007-08-02 11:09:59 -04004918 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
James Smart2e0fef82007-06-17 19:56:36 -05004919 if (!vport)
James Smart3772a992009-05-22 14:50:54 -04004920 return -ENODEV;
James Smart2e0fef82007-06-17 19:56:36 -05004921
4922 shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05004923 phba->pport = vport;
James Smart858c9f62007-06-17 19:56:39 -05004924 lpfc_debugfs_initialize(vport);
James Smart3772a992009-05-22 14:50:54 -04004925 /* Put reference to SCSI host to driver's device private data */
4926 pci_set_drvdata(phba->pcidev, shost);
James Smart2e0fef82007-06-17 19:56:36 -05004927
James Smart3772a992009-05-22 14:50:54 -04004928 return 0;
4929}
dea31012005-04-17 16:05:31 -05004930
James Smart3772a992009-05-22 14:50:54 -04004931/**
4932 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
4933 * @phba: pointer to lpfc hba data structure.
4934 *
4935 * This routine is invoked to destroy HBA physical port and the associated
4936 * SCSI host.
4937 **/
4938static void
4939lpfc_destroy_shost(struct lpfc_hba *phba)
4940{
4941 struct lpfc_vport *vport = phba->pport;
James Smart93996272008-08-24 21:50:30 -04004942
James Smart3772a992009-05-22 14:50:54 -04004943 /* Destroy physical port that associated with the SCSI host */
4944 destroy_port(vport);
4945
4946 return;
4947}
4948
4949/**
4950 * lpfc_setup_bg - Setup Block guard structures and debug areas.
4951 * @phba: pointer to lpfc hba data structure.
4952 * @shost: the shost to be used to detect Block guard settings.
4953 *
4954 * This routine sets up the local Block guard protocol settings for @shost.
4955 * This routine also allocates memory for debugging bg buffers.
4956 **/
4957static void
4958lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
4959{
4960 int pagecnt = 10;
4961 if (lpfc_prot_mask && lpfc_prot_guard) {
4962 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4963 "1478 Registering BlockGuard with the "
4964 "SCSI layer\n");
4965 scsi_host_set_prot(shost, lpfc_prot_mask);
4966 scsi_host_set_guard(shost, lpfc_prot_guard);
James Smart98c9ea52007-10-27 13:37:33 -04004967 }
James Smart81301a92008-12-04 22:39:46 -05004968 if (!_dump_buf_data) {
James Smart81301a92008-12-04 22:39:46 -05004969 while (pagecnt) {
4970 spin_lock_init(&_dump_buf_lock);
4971 _dump_buf_data =
4972 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
4973 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04004974 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4975 "9043 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04004976 "_dump_buf_data at 0x%p\n",
4977 (1 << pagecnt), _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05004978 _dump_buf_data_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04004979 memset(_dump_buf_data, 0,
4980 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05004981 break;
James Smart3772a992009-05-22 14:50:54 -04004982 } else
James Smart81301a92008-12-04 22:39:46 -05004983 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05004984 }
James Smart81301a92008-12-04 22:39:46 -05004985 if (!_dump_buf_data_order)
James Smart6a9c52c2009-10-02 15:16:51 -04004986 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4987 "9044 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04004988 "memory for hexdump\n");
4989 } else
James Smart6a9c52c2009-10-02 15:16:51 -04004990 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4991 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
James Smart81301a92008-12-04 22:39:46 -05004992 "\n", _dump_buf_data);
James Smart81301a92008-12-04 22:39:46 -05004993 if (!_dump_buf_dif) {
James Smart81301a92008-12-04 22:39:46 -05004994 while (pagecnt) {
4995 _dump_buf_dif =
4996 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
4997 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04004998 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4999 "9046 BLKGRD: allocated %d pages for "
James Smart3772a992009-05-22 14:50:54 -04005000 "_dump_buf_dif at 0x%p\n",
5001 (1 << pagecnt), _dump_buf_dif);
James Smart81301a92008-12-04 22:39:46 -05005002 _dump_buf_dif_order = pagecnt;
James Smart3772a992009-05-22 14:50:54 -04005003 memset(_dump_buf_dif, 0,
5004 ((1 << PAGE_SHIFT) << pagecnt));
James Smart81301a92008-12-04 22:39:46 -05005005 break;
James Smart3772a992009-05-22 14:50:54 -04005006 } else
James Smart81301a92008-12-04 22:39:46 -05005007 --pagecnt;
James Smart81301a92008-12-04 22:39:46 -05005008 }
James Smart81301a92008-12-04 22:39:46 -05005009 if (!_dump_buf_dif_order)
James Smart6a9c52c2009-10-02 15:16:51 -04005010 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5011 "9047 BLKGRD: ERROR unable to allocate "
James Smart3772a992009-05-22 14:50:54 -04005012 "memory for hexdump\n");
5013 } else
James Smart6a9c52c2009-10-02 15:16:51 -04005014 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5015 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
James Smart3772a992009-05-22 14:50:54 -04005016 _dump_buf_dif);
5017}
5018
5019/**
5020 * lpfc_post_init_setup - Perform necessary device post initialization setup.
5021 * @phba: pointer to lpfc hba data structure.
5022 *
5023 * This routine is invoked to perform all the necessary post initialization
5024 * setup for the device.
5025 **/
5026static void
5027lpfc_post_init_setup(struct lpfc_hba *phba)
5028{
5029 struct Scsi_Host *shost;
5030 struct lpfc_adapter_event_header adapter_event;
5031
5032 /* Get the default values for Model Name and Description */
5033 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
5034
5035 /*
5036 * hba setup may have changed the hba_queue_depth so we need to
5037 * adjust the value of can_queue.
5038 */
5039 shost = pci_get_drvdata(phba->pcidev);
5040 shost->can_queue = phba->cfg_hba_queue_depth - 10;
5041 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
5042 lpfc_setup_bg(phba, shost);
James Smart858c9f62007-06-17 19:56:39 -05005043
5044 lpfc_host_attrib_init(shost);
5045
James Smart2e0fef82007-06-17 19:56:36 -05005046 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
5047 spin_lock_irq(shost->host_lock);
5048 lpfc_poll_start_timer(phba);
5049 spin_unlock_irq(shost->host_lock);
5050 }
James Smart8f6d98d2006-08-01 07:34:00 -04005051
James Smart93996272008-08-24 21:50:30 -04005052 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5053 "0428 Perform SCSI scan\n");
James Smartea2151b2008-09-07 11:52:10 -04005054 /* Send board arrival event to upper layer */
5055 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
5056 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
5057 fc_host_post_vendor_event(shost, fc_get_event_number(),
James Smart3772a992009-05-22 14:50:54 -04005058 sizeof(adapter_event),
5059 (char *) &adapter_event,
5060 LPFC_NL_VENDOR_ID);
5061 return;
5062}
5063
5064/**
5065 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
5066 * @phba: pointer to lpfc hba data structure.
5067 *
5068 * This routine is invoked to set up the PCI device memory space for device
5069 * with SLI-3 interface spec.
5070 *
5071 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005072 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04005073 * other values - error
5074 **/
5075static int
5076lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
5077{
5078 struct pci_dev *pdev;
5079 unsigned long bar0map_len, bar2map_len;
5080 int i, hbq_count;
5081 void *ptr;
5082 int error = -ENODEV;
5083
5084 /* Obtain PCI device reference */
5085 if (!phba->pcidev)
5086 return error;
5087 else
5088 pdev = phba->pcidev;
5089
5090 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05005091 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
5092 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
5093 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
5094 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smart3772a992009-05-22 14:50:54 -04005095 return error;
Michael Reed8e685972009-09-18 12:02:05 -05005096 }
5097 }
James Smart3772a992009-05-22 14:50:54 -04005098
5099 /* Get the bus address of Bar0 and Bar2 and the number of bytes
5100 * required by each mapping.
5101 */
5102 phba->pci_bar0_map = pci_resource_start(pdev, 0);
5103 bar0map_len = pci_resource_len(pdev, 0);
5104
5105 phba->pci_bar2_map = pci_resource_start(pdev, 2);
5106 bar2map_len = pci_resource_len(pdev, 2);
5107
5108 /* Map HBA SLIM to a kernel virtual address. */
5109 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
5110 if (!phba->slim_memmap_p) {
5111 dev_printk(KERN_ERR, &pdev->dev,
5112 "ioremap failed for SLIM memory.\n");
5113 goto out;
5114 }
5115
5116 /* Map HBA Control Registers to a kernel virtual address. */
5117 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
5118 if (!phba->ctrl_regs_memmap_p) {
5119 dev_printk(KERN_ERR, &pdev->dev,
5120 "ioremap failed for HBA control registers.\n");
5121 goto out_iounmap_slim;
5122 }
5123
5124 /* Allocate memory for SLI-2 structures */
5125 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
5126 SLI2_SLIM_SIZE,
5127 &phba->slim2p.phys,
5128 GFP_KERNEL);
5129 if (!phba->slim2p.virt)
5130 goto out_iounmap;
5131
5132 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
5133 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
James Smart7a470272010-03-15 11:25:20 -04005134 phba->mbox_ext = (phba->slim2p.virt +
5135 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
James Smart3772a992009-05-22 14:50:54 -04005136 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
5137 phba->IOCBs = (phba->slim2p.virt +
5138 offsetof(struct lpfc_sli2_slim, IOCBs));
5139
5140 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
5141 lpfc_sli_hbq_size(),
5142 &phba->hbqslimp.phys,
5143 GFP_KERNEL);
5144 if (!phba->hbqslimp.virt)
5145 goto out_free_slim;
5146
5147 hbq_count = lpfc_sli_hbq_count();
5148 ptr = phba->hbqslimp.virt;
5149 for (i = 0; i < hbq_count; ++i) {
5150 phba->hbqs[i].hbq_virt = ptr;
5151 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
5152 ptr += (lpfc_hbq_defs[i]->entry_count *
5153 sizeof(struct lpfc_hbq_entry));
5154 }
5155 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
5156 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
5157
5158 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
5159
5160 INIT_LIST_HEAD(&phba->rb_pend_list);
5161
5162 phba->MBslimaddr = phba->slim_memmap_p;
5163 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
5164 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
5165 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
5166 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
James Smartea2151b2008-09-07 11:52:10 -04005167
dea31012005-04-17 16:05:31 -05005168 return 0;
5169
dea31012005-04-17 16:05:31 -05005170out_free_slim:
James Smart34b02dc2008-08-24 21:49:55 -04005171 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
5172 phba->slim2p.virt, phba->slim2p.phys);
dea31012005-04-17 16:05:31 -05005173out_iounmap:
5174 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05005175out_iounmap_slim:
dea31012005-04-17 16:05:31 -05005176 iounmap(phba->slim_memmap_p);
dea31012005-04-17 16:05:31 -05005177out:
5178 return error;
5179}
5180
James Smarte59058c2008-08-24 21:49:00 -04005181/**
James Smart3772a992009-05-22 14:50:54 -04005182 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
5183 * @phba: pointer to lpfc hba data structure.
5184 *
5185 * This routine is invoked to unset the PCI device memory space for device
5186 * with SLI-3 interface spec.
5187 **/
5188static void
5189lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
5190{
5191 struct pci_dev *pdev;
5192
5193 /* Obtain PCI device reference */
5194 if (!phba->pcidev)
5195 return;
5196 else
5197 pdev = phba->pcidev;
5198
5199 /* Free coherent DMA memory allocated */
5200 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
5201 phba->hbqslimp.virt, phba->hbqslimp.phys);
5202 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
5203 phba->slim2p.virt, phba->slim2p.phys);
5204
5205 /* I/O memory unmap */
5206 iounmap(phba->ctrl_regs_memmap_p);
5207 iounmap(phba->slim_memmap_p);
5208
5209 return;
5210}
5211
5212/**
James Smartda0436e2009-05-22 14:51:39 -04005213 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
5214 * @phba: pointer to lpfc hba data structure.
5215 *
5216 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
5217 * done and check status.
5218 *
5219 * Return 0 if successful, otherwise -ENODEV.
5220 **/
5221int
5222lpfc_sli4_post_status_check(struct lpfc_hba *phba)
5223{
James Smart28baac72010-02-12 14:42:03 -05005224 struct lpfc_register sta_reg, uerrlo_reg, uerrhi_reg;
James Smartda0436e2009-05-22 14:51:39 -04005225 int i, port_error = -ENODEV;
5226
5227 if (!phba->sli4_hba.STAregaddr)
5228 return -ENODEV;
5229
James Smartda0436e2009-05-22 14:51:39 -04005230 /* Wait up to 30 seconds for the SLI Port POST done and ready */
5231 for (i = 0; i < 3000; i++) {
5232 sta_reg.word0 = readl(phba->sli4_hba.STAregaddr);
5233 /* Encounter fatal POST error, break out */
5234 if (bf_get(lpfc_hst_state_perr, &sta_reg)) {
5235 port_error = -ENODEV;
5236 break;
5237 }
5238 if (LPFC_POST_STAGE_ARMFW_READY ==
5239 bf_get(lpfc_hst_state_port_status, &sta_reg)) {
5240 port_error = 0;
5241 break;
5242 }
5243 msleep(10);
5244 }
5245
5246 if (port_error)
5247 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5248 "1408 Failure HBA POST Status: sta_reg=0x%x, "
5249 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, xrom=x%x, "
5250 "dl=x%x, pstatus=x%x\n", sta_reg.word0,
5251 bf_get(lpfc_hst_state_perr, &sta_reg),
5252 bf_get(lpfc_hst_state_sfi, &sta_reg),
5253 bf_get(lpfc_hst_state_nip, &sta_reg),
5254 bf_get(lpfc_hst_state_ipc, &sta_reg),
5255 bf_get(lpfc_hst_state_xrom, &sta_reg),
5256 bf_get(lpfc_hst_state_dl, &sta_reg),
5257 bf_get(lpfc_hst_state_port_status, &sta_reg));
5258
5259 /* Log device information */
James Smart28baac72010-02-12 14:42:03 -05005260 phba->sli4_hba.sli_intf.word0 = readl(phba->sli4_hba.SLIINTFregaddr);
5261 if (bf_get(lpfc_sli_intf_valid,
5262 &phba->sli4_hba.sli_intf) == LPFC_SLI_INTF_VALID) {
5263 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5264 "2534 Device Info: ChipType=0x%x, SliRev=0x%x, "
5265 "FeatureL1=0x%x, FeatureL2=0x%x\n",
5266 bf_get(lpfc_sli_intf_sli_family,
5267 &phba->sli4_hba.sli_intf),
5268 bf_get(lpfc_sli_intf_slirev,
5269 &phba->sli4_hba.sli_intf),
5270 bf_get(lpfc_sli_intf_featurelevel1,
5271 &phba->sli4_hba.sli_intf),
5272 bf_get(lpfc_sli_intf_featurelevel2,
5273 &phba->sli4_hba.sli_intf));
5274 }
James Smarta747c9c2009-11-18 15:41:10 -05005275 phba->sli4_hba.ue_mask_lo = readl(phba->sli4_hba.UEMASKLOregaddr);
5276 phba->sli4_hba.ue_mask_hi = readl(phba->sli4_hba.UEMASKHIregaddr);
James Smart8fa38512009-07-19 10:01:03 -04005277 /* With uncoverable error, log the error message and return error */
James Smarta747c9c2009-11-18 15:41:10 -05005278 uerrlo_reg.word0 = readl(phba->sli4_hba.UERRLOregaddr);
5279 uerrhi_reg.word0 = readl(phba->sli4_hba.UERRHIregaddr);
5280 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
5281 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
5282 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5283 "1422 HBA Unrecoverable error: "
5284 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
5285 "ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
5286 uerrlo_reg.word0, uerrhi_reg.word0,
5287 phba->sli4_hba.ue_mask_lo,
5288 phba->sli4_hba.ue_mask_hi);
James Smart8fa38512009-07-19 10:01:03 -04005289 return -ENODEV;
5290 }
5291
James Smartda0436e2009-05-22 14:51:39 -04005292 return port_error;
5293}
5294
5295/**
5296 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
5297 * @phba: pointer to lpfc hba data structure.
5298 *
5299 * This routine is invoked to set up SLI4 BAR0 PCI config space register
5300 * memory map.
5301 **/
5302static void
5303lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba)
5304{
5305 phba->sli4_hba.UERRLOregaddr = phba->sli4_hba.conf_regs_memmap_p +
5306 LPFC_UERR_STATUS_LO;
5307 phba->sli4_hba.UERRHIregaddr = phba->sli4_hba.conf_regs_memmap_p +
5308 LPFC_UERR_STATUS_HI;
James Smarta747c9c2009-11-18 15:41:10 -05005309 phba->sli4_hba.UEMASKLOregaddr = phba->sli4_hba.conf_regs_memmap_p +
5310 LPFC_UE_MASK_LO;
5311 phba->sli4_hba.UEMASKHIregaddr = phba->sli4_hba.conf_regs_memmap_p +
5312 LPFC_UE_MASK_HI;
James Smart28baac72010-02-12 14:42:03 -05005313 phba->sli4_hba.SLIINTFregaddr = phba->sli4_hba.conf_regs_memmap_p +
5314 LPFC_SLI_INTF;
James Smartda0436e2009-05-22 14:51:39 -04005315}
5316
5317/**
5318 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
5319 * @phba: pointer to lpfc hba data structure.
5320 *
5321 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
5322 * memory map.
5323 **/
5324static void
5325lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
5326{
5327
5328 phba->sli4_hba.STAregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5329 LPFC_HST_STATE;
5330 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5331 LPFC_HST_ISR0;
5332 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5333 LPFC_HST_IMR0;
5334 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5335 LPFC_HST_ISCR0;
5336 return;
5337}
5338
5339/**
5340 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
5341 * @phba: pointer to lpfc hba data structure.
5342 * @vf: virtual function number
5343 *
5344 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
5345 * based on the given viftual function number, @vf.
5346 *
5347 * Return 0 if successful, otherwise -ENODEV.
5348 **/
5349static int
5350lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
5351{
5352 if (vf > LPFC_VIR_FUNC_MAX)
5353 return -ENODEV;
5354
5355 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5356 vf * LPFC_VFR_PAGE_SIZE + LPFC_RQ_DOORBELL);
5357 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5358 vf * LPFC_VFR_PAGE_SIZE + LPFC_WQ_DOORBELL);
5359 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5360 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
5361 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5362 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
5363 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5364 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
5365 return 0;
5366}
5367
5368/**
5369 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
5370 * @phba: pointer to lpfc hba data structure.
5371 *
5372 * This routine is invoked to create the bootstrap mailbox
5373 * region consistent with the SLI-4 interface spec. This
5374 * routine allocates all memory necessary to communicate
5375 * mailbox commands to the port and sets up all alignment
5376 * needs. No locks are expected to be held when calling
5377 * this routine.
5378 *
5379 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005380 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005381 * ENOMEM - could not allocated memory.
5382 **/
5383static int
5384lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
5385{
5386 uint32_t bmbx_size;
5387 struct lpfc_dmabuf *dmabuf;
5388 struct dma_address *dma_address;
5389 uint32_t pa_addr;
5390 uint64_t phys_addr;
5391
5392 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5393 if (!dmabuf)
5394 return -ENOMEM;
5395
5396 /*
5397 * The bootstrap mailbox region is comprised of 2 parts
5398 * plus an alignment restriction of 16 bytes.
5399 */
5400 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
5401 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5402 bmbx_size,
5403 &dmabuf->phys,
5404 GFP_KERNEL);
5405 if (!dmabuf->virt) {
5406 kfree(dmabuf);
5407 return -ENOMEM;
5408 }
5409 memset(dmabuf->virt, 0, bmbx_size);
5410
5411 /*
5412 * Initialize the bootstrap mailbox pointers now so that the register
5413 * operations are simple later. The mailbox dma address is required
5414 * to be 16-byte aligned. Also align the virtual memory as each
5415 * maibox is copied into the bmbx mailbox region before issuing the
5416 * command to the port.
5417 */
5418 phba->sli4_hba.bmbx.dmabuf = dmabuf;
5419 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
5420
5421 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
5422 LPFC_ALIGN_16_BYTE);
5423 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
5424 LPFC_ALIGN_16_BYTE);
5425
5426 /*
5427 * Set the high and low physical addresses now. The SLI4 alignment
5428 * requirement is 16 bytes and the mailbox is posted to the port
5429 * as two 30-bit addresses. The other data is a bit marking whether
5430 * the 30-bit address is the high or low address.
5431 * Upcast bmbx aphys to 64bits so shift instruction compiles
5432 * clean on 32 bit machines.
5433 */
5434 dma_address = &phba->sli4_hba.bmbx.dma_address;
5435 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
5436 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
5437 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
5438 LPFC_BMBX_BIT1_ADDR_HI);
5439
5440 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
5441 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
5442 LPFC_BMBX_BIT1_ADDR_LO);
5443 return 0;
5444}
5445
5446/**
5447 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
5448 * @phba: pointer to lpfc hba data structure.
5449 *
5450 * This routine is invoked to teardown the bootstrap mailbox
5451 * region and release all host resources. This routine requires
5452 * the caller to ensure all mailbox commands recovered, no
5453 * additional mailbox comands are sent, and interrupts are disabled
5454 * before calling this routine.
5455 *
5456 **/
5457static void
5458lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
5459{
5460 dma_free_coherent(&phba->pcidev->dev,
5461 phba->sli4_hba.bmbx.bmbx_size,
5462 phba->sli4_hba.bmbx.dmabuf->virt,
5463 phba->sli4_hba.bmbx.dmabuf->phys);
5464
5465 kfree(phba->sli4_hba.bmbx.dmabuf);
5466 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
5467}
5468
5469/**
5470 * lpfc_sli4_read_config - Get the config parameters.
5471 * @phba: pointer to lpfc hba data structure.
5472 *
5473 * This routine is invoked to read the configuration parameters from the HBA.
5474 * The configuration parameters are used to set the base and maximum values
5475 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
5476 * allocation for the port.
5477 *
5478 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005479 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005480 * ENOMEM - No availble memory
5481 * EIO - The mailbox failed to complete successfully.
5482 **/
5483static int
5484lpfc_sli4_read_config(struct lpfc_hba *phba)
5485{
5486 LPFC_MBOXQ_t *pmb;
5487 struct lpfc_mbx_read_config *rd_config;
5488 uint32_t rc = 0;
5489
5490 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5491 if (!pmb) {
5492 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5493 "2011 Unable to allocate memory for issuing "
5494 "SLI_CONFIG_SPECIAL mailbox command\n");
5495 return -ENOMEM;
5496 }
5497
5498 lpfc_read_config(phba, pmb);
5499
5500 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
5501 if (rc != MBX_SUCCESS) {
5502 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5503 "2012 Mailbox failed , mbxCmd x%x "
5504 "READ_CONFIG, mbxStatus x%x\n",
5505 bf_get(lpfc_mqe_command, &pmb->u.mqe),
5506 bf_get(lpfc_mqe_status, &pmb->u.mqe));
5507 rc = -EIO;
5508 } else {
5509 rd_config = &pmb->u.mqe.un.rd_config;
5510 phba->sli4_hba.max_cfg_param.max_xri =
5511 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
5512 phba->sli4_hba.max_cfg_param.xri_base =
5513 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
5514 phba->sli4_hba.max_cfg_param.max_vpi =
5515 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
5516 phba->sli4_hba.max_cfg_param.vpi_base =
5517 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
5518 phba->sli4_hba.max_cfg_param.max_rpi =
5519 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
5520 phba->sli4_hba.max_cfg_param.rpi_base =
5521 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
5522 phba->sli4_hba.max_cfg_param.max_vfi =
5523 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
5524 phba->sli4_hba.max_cfg_param.vfi_base =
5525 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
5526 phba->sli4_hba.max_cfg_param.max_fcfi =
5527 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
5528 phba->sli4_hba.max_cfg_param.fcfi_base =
5529 bf_get(lpfc_mbx_rd_conf_fcfi_base, rd_config);
5530 phba->sli4_hba.max_cfg_param.max_eq =
5531 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
5532 phba->sli4_hba.max_cfg_param.max_rq =
5533 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
5534 phba->sli4_hba.max_cfg_param.max_wq =
5535 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
5536 phba->sli4_hba.max_cfg_param.max_cq =
5537 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
5538 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
5539 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
5540 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
5541 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
5542 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
James Smart5ffc2662009-11-18 15:39:44 -05005543 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
5544 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
James Smartda0436e2009-05-22 14:51:39 -04005545 phba->max_vports = phba->max_vpi;
5546 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5547 "2003 cfg params XRI(B:%d M:%d), "
5548 "VPI(B:%d M:%d) "
5549 "VFI(B:%d M:%d) "
5550 "RPI(B:%d M:%d) "
5551 "FCFI(B:%d M:%d)\n",
5552 phba->sli4_hba.max_cfg_param.xri_base,
5553 phba->sli4_hba.max_cfg_param.max_xri,
5554 phba->sli4_hba.max_cfg_param.vpi_base,
5555 phba->sli4_hba.max_cfg_param.max_vpi,
5556 phba->sli4_hba.max_cfg_param.vfi_base,
5557 phba->sli4_hba.max_cfg_param.max_vfi,
5558 phba->sli4_hba.max_cfg_param.rpi_base,
5559 phba->sli4_hba.max_cfg_param.max_rpi,
5560 phba->sli4_hba.max_cfg_param.fcfi_base,
5561 phba->sli4_hba.max_cfg_param.max_fcfi);
5562 }
5563 mempool_free(pmb, phba->mbox_mem_pool);
5564
5565 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
5566 if (phba->cfg_hba_queue_depth > (phba->sli4_hba.max_cfg_param.max_xri))
5567 phba->cfg_hba_queue_depth =
5568 phba->sli4_hba.max_cfg_param.max_xri;
5569 return rc;
5570}
5571
5572/**
5573 * lpfc_dev_endian_order_setup - Notify the port of the host's endian order.
5574 * @phba: pointer to lpfc hba data structure.
5575 *
5576 * This routine is invoked to setup the host-side endian order to the
5577 * HBA consistent with the SLI-4 interface spec.
5578 *
5579 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005580 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005581 * ENOMEM - No availble memory
5582 * EIO - The mailbox failed to complete successfully.
5583 **/
5584static int
5585lpfc_setup_endian_order(struct lpfc_hba *phba)
5586{
5587 LPFC_MBOXQ_t *mboxq;
5588 uint32_t rc = 0;
5589 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
5590 HOST_ENDIAN_HIGH_WORD1};
5591
5592 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5593 if (!mboxq) {
5594 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5595 "0492 Unable to allocate memory for issuing "
5596 "SLI_CONFIG_SPECIAL mailbox command\n");
5597 return -ENOMEM;
5598 }
5599
5600 /*
5601 * The SLI4_CONFIG_SPECIAL mailbox command requires the first two
5602 * words to contain special data values and no other data.
5603 */
5604 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
5605 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
5606 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5607 if (rc != MBX_SUCCESS) {
5608 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5609 "0493 SLI_CONFIG_SPECIAL mailbox failed with "
5610 "status x%x\n",
5611 rc);
5612 rc = -EIO;
5613 }
5614
5615 mempool_free(mboxq, phba->mbox_mem_pool);
5616 return rc;
5617}
5618
5619/**
5620 * lpfc_sli4_queue_create - Create all the SLI4 queues
5621 * @phba: pointer to lpfc hba data structure.
5622 *
5623 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
5624 * operation. For each SLI4 queue type, the parameters such as queue entry
5625 * count (queue depth) shall be taken from the module parameter. For now,
5626 * we just use some constant number as place holder.
5627 *
5628 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005629 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005630 * ENOMEM - No availble memory
5631 * EIO - The mailbox failed to complete successfully.
5632 **/
5633static int
5634lpfc_sli4_queue_create(struct lpfc_hba *phba)
5635{
5636 struct lpfc_queue *qdesc;
5637 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
5638 int cfg_fcp_wq_count;
5639 int cfg_fcp_eq_count;
5640
5641 /*
5642 * Sanity check for confiugred queue parameters against the run-time
5643 * device parameters
5644 */
5645
5646 /* Sanity check on FCP fast-path WQ parameters */
5647 cfg_fcp_wq_count = phba->cfg_fcp_wq_count;
5648 if (cfg_fcp_wq_count >
5649 (phba->sli4_hba.max_cfg_param.max_wq - LPFC_SP_WQN_DEF)) {
5650 cfg_fcp_wq_count = phba->sli4_hba.max_cfg_param.max_wq -
5651 LPFC_SP_WQN_DEF;
5652 if (cfg_fcp_wq_count < LPFC_FP_WQN_MIN) {
5653 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5654 "2581 Not enough WQs (%d) from "
5655 "the pci function for supporting "
5656 "FCP WQs (%d)\n",
5657 phba->sli4_hba.max_cfg_param.max_wq,
5658 phba->cfg_fcp_wq_count);
5659 goto out_error;
5660 }
5661 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5662 "2582 Not enough WQs (%d) from the pci "
5663 "function for supporting the requested "
5664 "FCP WQs (%d), the actual FCP WQs can "
5665 "be supported: %d\n",
5666 phba->sli4_hba.max_cfg_param.max_wq,
5667 phba->cfg_fcp_wq_count, cfg_fcp_wq_count);
5668 }
5669 /* The actual number of FCP work queues adopted */
5670 phba->cfg_fcp_wq_count = cfg_fcp_wq_count;
5671
5672 /* Sanity check on FCP fast-path EQ parameters */
5673 cfg_fcp_eq_count = phba->cfg_fcp_eq_count;
5674 if (cfg_fcp_eq_count >
5675 (phba->sli4_hba.max_cfg_param.max_eq - LPFC_SP_EQN_DEF)) {
5676 cfg_fcp_eq_count = phba->sli4_hba.max_cfg_param.max_eq -
5677 LPFC_SP_EQN_DEF;
5678 if (cfg_fcp_eq_count < LPFC_FP_EQN_MIN) {
5679 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5680 "2574 Not enough EQs (%d) from the "
5681 "pci function for supporting FCP "
5682 "EQs (%d)\n",
5683 phba->sli4_hba.max_cfg_param.max_eq,
5684 phba->cfg_fcp_eq_count);
5685 goto out_error;
5686 }
5687 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5688 "2575 Not enough EQs (%d) from the pci "
5689 "function for supporting the requested "
5690 "FCP EQs (%d), the actual FCP EQs can "
5691 "be supported: %d\n",
5692 phba->sli4_hba.max_cfg_param.max_eq,
5693 phba->cfg_fcp_eq_count, cfg_fcp_eq_count);
5694 }
5695 /* It does not make sense to have more EQs than WQs */
5696 if (cfg_fcp_eq_count > phba->cfg_fcp_wq_count) {
5697 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smart6a9c52c2009-10-02 15:16:51 -04005698 "2593 The FCP EQ count(%d) cannot be greater "
5699 "than the FCP WQ count(%d), limiting the "
5700 "FCP EQ count to %d\n", cfg_fcp_eq_count,
James Smartda0436e2009-05-22 14:51:39 -04005701 phba->cfg_fcp_wq_count,
5702 phba->cfg_fcp_wq_count);
5703 cfg_fcp_eq_count = phba->cfg_fcp_wq_count;
5704 }
5705 /* The actual number of FCP event queues adopted */
5706 phba->cfg_fcp_eq_count = cfg_fcp_eq_count;
5707 /* The overall number of event queues used */
5708 phba->sli4_hba.cfg_eqn = phba->cfg_fcp_eq_count + LPFC_SP_EQN_DEF;
5709
5710 /*
5711 * Create Event Queues (EQs)
5712 */
5713
5714 /* Get EQ depth from module parameter, fake the default for now */
5715 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
5716 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
5717
5718 /* Create slow path event queue */
5719 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5720 phba->sli4_hba.eq_ecount);
5721 if (!qdesc) {
5722 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5723 "0496 Failed allocate slow-path EQ\n");
5724 goto out_error;
5725 }
5726 phba->sli4_hba.sp_eq = qdesc;
5727
5728 /* Create fast-path FCP Event Queue(s) */
5729 phba->sli4_hba.fp_eq = kzalloc((sizeof(struct lpfc_queue *) *
5730 phba->cfg_fcp_eq_count), GFP_KERNEL);
5731 if (!phba->sli4_hba.fp_eq) {
5732 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5733 "2576 Failed allocate memory for fast-path "
5734 "EQ record array\n");
5735 goto out_free_sp_eq;
5736 }
5737 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
5738 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
5739 phba->sli4_hba.eq_ecount);
5740 if (!qdesc) {
5741 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5742 "0497 Failed allocate fast-path EQ\n");
5743 goto out_free_fp_eq;
5744 }
5745 phba->sli4_hba.fp_eq[fcp_eqidx] = qdesc;
5746 }
5747
5748 /*
5749 * Create Complete Queues (CQs)
5750 */
5751
5752 /* Get CQ depth from module parameter, fake the default for now */
5753 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
5754 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
5755
5756 /* Create slow-path Mailbox Command Complete Queue */
5757 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5758 phba->sli4_hba.cq_ecount);
5759 if (!qdesc) {
5760 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5761 "0500 Failed allocate slow-path mailbox CQ\n");
5762 goto out_free_fp_eq;
5763 }
5764 phba->sli4_hba.mbx_cq = qdesc;
5765
5766 /* Create slow-path ELS Complete Queue */
5767 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5768 phba->sli4_hba.cq_ecount);
5769 if (!qdesc) {
5770 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5771 "0501 Failed allocate slow-path ELS CQ\n");
5772 goto out_free_mbx_cq;
5773 }
5774 phba->sli4_hba.els_cq = qdesc;
5775
James Smartda0436e2009-05-22 14:51:39 -04005776
5777 /* Create fast-path FCP Completion Queue(s), one-to-one with EQs */
5778 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
5779 phba->cfg_fcp_eq_count), GFP_KERNEL);
5780 if (!phba->sli4_hba.fcp_cq) {
5781 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5782 "2577 Failed allocate memory for fast-path "
5783 "CQ record array\n");
James Smart4d9ab992009-10-02 15:16:39 -04005784 goto out_free_els_cq;
James Smartda0436e2009-05-22 14:51:39 -04005785 }
5786 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
5787 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
5788 phba->sli4_hba.cq_ecount);
5789 if (!qdesc) {
5790 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5791 "0499 Failed allocate fast-path FCP "
5792 "CQ (%d)\n", fcp_cqidx);
5793 goto out_free_fcp_cq;
5794 }
5795 phba->sli4_hba.fcp_cq[fcp_cqidx] = qdesc;
5796 }
5797
5798 /* Create Mailbox Command Queue */
5799 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
5800 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
5801
5802 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
5803 phba->sli4_hba.mq_ecount);
5804 if (!qdesc) {
5805 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5806 "0505 Failed allocate slow-path MQ\n");
5807 goto out_free_fcp_cq;
5808 }
5809 phba->sli4_hba.mbx_wq = qdesc;
5810
5811 /*
5812 * Create all the Work Queues (WQs)
5813 */
5814 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
5815 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
5816
5817 /* Create slow-path ELS Work Queue */
5818 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
5819 phba->sli4_hba.wq_ecount);
5820 if (!qdesc) {
5821 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5822 "0504 Failed allocate slow-path ELS WQ\n");
5823 goto out_free_mbx_wq;
5824 }
5825 phba->sli4_hba.els_wq = qdesc;
5826
5827 /* Create fast-path FCP Work Queue(s) */
5828 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
5829 phba->cfg_fcp_wq_count), GFP_KERNEL);
5830 if (!phba->sli4_hba.fcp_wq) {
5831 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5832 "2578 Failed allocate memory for fast-path "
5833 "WQ record array\n");
5834 goto out_free_els_wq;
5835 }
5836 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
5837 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
5838 phba->sli4_hba.wq_ecount);
5839 if (!qdesc) {
5840 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5841 "0503 Failed allocate fast-path FCP "
5842 "WQ (%d)\n", fcp_wqidx);
5843 goto out_free_fcp_wq;
5844 }
5845 phba->sli4_hba.fcp_wq[fcp_wqidx] = qdesc;
5846 }
5847
5848 /*
5849 * Create Receive Queue (RQ)
5850 */
5851 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
5852 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
5853
5854 /* Create Receive Queue for header */
5855 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
5856 phba->sli4_hba.rq_ecount);
5857 if (!qdesc) {
5858 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5859 "0506 Failed allocate receive HRQ\n");
5860 goto out_free_fcp_wq;
5861 }
5862 phba->sli4_hba.hdr_rq = qdesc;
5863
5864 /* Create Receive Queue for data */
5865 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
5866 phba->sli4_hba.rq_ecount);
5867 if (!qdesc) {
5868 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5869 "0507 Failed allocate receive DRQ\n");
5870 goto out_free_hdr_rq;
5871 }
5872 phba->sli4_hba.dat_rq = qdesc;
5873
5874 return 0;
5875
5876out_free_hdr_rq:
5877 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
5878 phba->sli4_hba.hdr_rq = NULL;
5879out_free_fcp_wq:
5880 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--) {
5881 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_wqidx]);
5882 phba->sli4_hba.fcp_wq[fcp_wqidx] = NULL;
5883 }
5884 kfree(phba->sli4_hba.fcp_wq);
5885out_free_els_wq:
5886 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
5887 phba->sli4_hba.els_wq = NULL;
5888out_free_mbx_wq:
5889 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
5890 phba->sli4_hba.mbx_wq = NULL;
5891out_free_fcp_cq:
5892 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--) {
5893 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_cqidx]);
5894 phba->sli4_hba.fcp_cq[fcp_cqidx] = NULL;
5895 }
5896 kfree(phba->sli4_hba.fcp_cq);
James Smartda0436e2009-05-22 14:51:39 -04005897out_free_els_cq:
5898 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
5899 phba->sli4_hba.els_cq = NULL;
5900out_free_mbx_cq:
5901 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
5902 phba->sli4_hba.mbx_cq = NULL;
5903out_free_fp_eq:
5904 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--) {
5905 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_eqidx]);
5906 phba->sli4_hba.fp_eq[fcp_eqidx] = NULL;
5907 }
5908 kfree(phba->sli4_hba.fp_eq);
5909out_free_sp_eq:
5910 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
5911 phba->sli4_hba.sp_eq = NULL;
5912out_error:
5913 return -ENOMEM;
5914}
5915
5916/**
5917 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
5918 * @phba: pointer to lpfc hba data structure.
5919 *
5920 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
5921 * operation.
5922 *
5923 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005924 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005925 * ENOMEM - No availble memory
5926 * EIO - The mailbox failed to complete successfully.
5927 **/
5928static void
5929lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
5930{
5931 int fcp_qidx;
5932
5933 /* Release mailbox command work queue */
5934 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
5935 phba->sli4_hba.mbx_wq = NULL;
5936
5937 /* Release ELS work queue */
5938 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
5939 phba->sli4_hba.els_wq = NULL;
5940
5941 /* Release FCP work queue */
5942 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
5943 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_qidx]);
5944 kfree(phba->sli4_hba.fcp_wq);
5945 phba->sli4_hba.fcp_wq = NULL;
5946
5947 /* Release unsolicited receive queue */
5948 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
5949 phba->sli4_hba.hdr_rq = NULL;
5950 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
5951 phba->sli4_hba.dat_rq = NULL;
5952
James Smartda0436e2009-05-22 14:51:39 -04005953 /* Release ELS complete queue */
5954 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
5955 phba->sli4_hba.els_cq = NULL;
5956
5957 /* Release mailbox command complete queue */
5958 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
5959 phba->sli4_hba.mbx_cq = NULL;
5960
5961 /* Release FCP response complete queue */
5962 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5963 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_qidx]);
5964 kfree(phba->sli4_hba.fcp_cq);
5965 phba->sli4_hba.fcp_cq = NULL;
5966
5967 /* Release fast-path event queue */
5968 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
5969 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_qidx]);
5970 kfree(phba->sli4_hba.fp_eq);
5971 phba->sli4_hba.fp_eq = NULL;
5972
5973 /* Release slow-path event queue */
5974 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
5975 phba->sli4_hba.sp_eq = NULL;
5976
5977 return;
5978}
5979
5980/**
5981 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
5982 * @phba: pointer to lpfc hba data structure.
5983 *
5984 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
5985 * operation.
5986 *
5987 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005988 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04005989 * ENOMEM - No availble memory
5990 * EIO - The mailbox failed to complete successfully.
5991 **/
5992int
5993lpfc_sli4_queue_setup(struct lpfc_hba *phba)
5994{
5995 int rc = -ENOMEM;
5996 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
5997 int fcp_cq_index = 0;
5998
5999 /*
6000 * Set up Event Queues (EQs)
6001 */
6002
6003 /* Set up slow-path event queue */
6004 if (!phba->sli4_hba.sp_eq) {
6005 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6006 "0520 Slow-path EQ not allocated\n");
6007 goto out_error;
6008 }
6009 rc = lpfc_eq_create(phba, phba->sli4_hba.sp_eq,
6010 LPFC_SP_DEF_IMAX);
6011 if (rc) {
6012 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6013 "0521 Failed setup of slow-path EQ: "
6014 "rc = 0x%x\n", rc);
6015 goto out_error;
6016 }
6017 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6018 "2583 Slow-path EQ setup: queue-id=%d\n",
6019 phba->sli4_hba.sp_eq->queue_id);
6020
6021 /* Set up fast-path event queue */
6022 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
6023 if (!phba->sli4_hba.fp_eq[fcp_eqidx]) {
6024 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6025 "0522 Fast-path EQ (%d) not "
6026 "allocated\n", fcp_eqidx);
6027 goto out_destroy_fp_eq;
6028 }
6029 rc = lpfc_eq_create(phba, phba->sli4_hba.fp_eq[fcp_eqidx],
6030 phba->cfg_fcp_imax);
6031 if (rc) {
6032 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6033 "0523 Failed setup of fast-path EQ "
6034 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
6035 goto out_destroy_fp_eq;
6036 }
6037 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6038 "2584 Fast-path EQ setup: "
6039 "queue[%d]-id=%d\n", fcp_eqidx,
6040 phba->sli4_hba.fp_eq[fcp_eqidx]->queue_id);
6041 }
6042
6043 /*
6044 * Set up Complete Queues (CQs)
6045 */
6046
6047 /* Set up slow-path MBOX Complete Queue as the first CQ */
6048 if (!phba->sli4_hba.mbx_cq) {
6049 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6050 "0528 Mailbox CQ not allocated\n");
6051 goto out_destroy_fp_eq;
6052 }
6053 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq, phba->sli4_hba.sp_eq,
6054 LPFC_MCQ, LPFC_MBOX);
6055 if (rc) {
6056 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6057 "0529 Failed setup of slow-path mailbox CQ: "
6058 "rc = 0x%x\n", rc);
6059 goto out_destroy_fp_eq;
6060 }
6061 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6062 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
6063 phba->sli4_hba.mbx_cq->queue_id,
6064 phba->sli4_hba.sp_eq->queue_id);
6065
6066 /* Set up slow-path ELS Complete Queue */
6067 if (!phba->sli4_hba.els_cq) {
6068 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6069 "0530 ELS CQ not allocated\n");
6070 goto out_destroy_mbx_cq;
6071 }
6072 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq, phba->sli4_hba.sp_eq,
6073 LPFC_WCQ, LPFC_ELS);
6074 if (rc) {
6075 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6076 "0531 Failed setup of slow-path ELS CQ: "
6077 "rc = 0x%x\n", rc);
6078 goto out_destroy_mbx_cq;
6079 }
6080 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6081 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
6082 phba->sli4_hba.els_cq->queue_id,
6083 phba->sli4_hba.sp_eq->queue_id);
6084
James Smartda0436e2009-05-22 14:51:39 -04006085 /* Set up fast-path FCP Response Complete Queue */
6086 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
6087 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
6088 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6089 "0526 Fast-path FCP CQ (%d) not "
6090 "allocated\n", fcp_cqidx);
6091 goto out_destroy_fcp_cq;
6092 }
6093 rc = lpfc_cq_create(phba, phba->sli4_hba.fcp_cq[fcp_cqidx],
6094 phba->sli4_hba.fp_eq[fcp_cqidx],
6095 LPFC_WCQ, LPFC_FCP);
6096 if (rc) {
6097 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6098 "0527 Failed setup of fast-path FCP "
6099 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
6100 goto out_destroy_fcp_cq;
6101 }
6102 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6103 "2588 FCP CQ setup: cq[%d]-id=%d, "
6104 "parent eq[%d]-id=%d\n",
6105 fcp_cqidx,
6106 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
6107 fcp_cqidx,
6108 phba->sli4_hba.fp_eq[fcp_cqidx]->queue_id);
6109 }
6110
6111 /*
6112 * Set up all the Work Queues (WQs)
6113 */
6114
6115 /* Set up Mailbox Command Queue */
6116 if (!phba->sli4_hba.mbx_wq) {
6117 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6118 "0538 Slow-path MQ not allocated\n");
6119 goto out_destroy_fcp_cq;
6120 }
6121 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
6122 phba->sli4_hba.mbx_cq, LPFC_MBOX);
6123 if (rc) {
6124 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6125 "0539 Failed setup of slow-path MQ: "
6126 "rc = 0x%x\n", rc);
6127 goto out_destroy_fcp_cq;
6128 }
6129 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6130 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
6131 phba->sli4_hba.mbx_wq->queue_id,
6132 phba->sli4_hba.mbx_cq->queue_id);
6133
6134 /* Set up slow-path ELS Work Queue */
6135 if (!phba->sli4_hba.els_wq) {
6136 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6137 "0536 Slow-path ELS WQ not allocated\n");
6138 goto out_destroy_mbx_wq;
6139 }
6140 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
6141 phba->sli4_hba.els_cq, LPFC_ELS);
6142 if (rc) {
6143 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6144 "0537 Failed setup of slow-path ELS WQ: "
6145 "rc = 0x%x\n", rc);
6146 goto out_destroy_mbx_wq;
6147 }
6148 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6149 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
6150 phba->sli4_hba.els_wq->queue_id,
6151 phba->sli4_hba.els_cq->queue_id);
6152
6153 /* Set up fast-path FCP Work Queue */
6154 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
6155 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
6156 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6157 "0534 Fast-path FCP WQ (%d) not "
6158 "allocated\n", fcp_wqidx);
6159 goto out_destroy_fcp_wq;
6160 }
6161 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
6162 phba->sli4_hba.fcp_cq[fcp_cq_index],
6163 LPFC_FCP);
6164 if (rc) {
6165 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6166 "0535 Failed setup of fast-path FCP "
6167 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
6168 goto out_destroy_fcp_wq;
6169 }
6170 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6171 "2591 FCP WQ setup: wq[%d]-id=%d, "
6172 "parent cq[%d]-id=%d\n",
6173 fcp_wqidx,
6174 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
6175 fcp_cq_index,
6176 phba->sli4_hba.fcp_cq[fcp_cq_index]->queue_id);
6177 /* Round robin FCP Work Queue's Completion Queue assignment */
6178 fcp_cq_index = ((fcp_cq_index + 1) % phba->cfg_fcp_eq_count);
6179 }
6180
6181 /*
6182 * Create Receive Queue (RQ)
6183 */
6184 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
6185 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6186 "0540 Receive Queue not allocated\n");
6187 goto out_destroy_fcp_wq;
6188 }
6189 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
James Smart4d9ab992009-10-02 15:16:39 -04006190 phba->sli4_hba.els_cq, LPFC_USOL);
James Smartda0436e2009-05-22 14:51:39 -04006191 if (rc) {
6192 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6193 "0541 Failed setup of Receive Queue: "
6194 "rc = 0x%x\n", rc);
6195 goto out_destroy_fcp_wq;
6196 }
6197 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6198 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
6199 "parent cq-id=%d\n",
6200 phba->sli4_hba.hdr_rq->queue_id,
6201 phba->sli4_hba.dat_rq->queue_id,
James Smart4d9ab992009-10-02 15:16:39 -04006202 phba->sli4_hba.els_cq->queue_id);
James Smartda0436e2009-05-22 14:51:39 -04006203 return 0;
6204
6205out_destroy_fcp_wq:
6206 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
6207 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
6208 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
6209out_destroy_mbx_wq:
6210 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
6211out_destroy_fcp_cq:
6212 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
6213 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
James Smartda0436e2009-05-22 14:51:39 -04006214 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
6215out_destroy_mbx_cq:
6216 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
6217out_destroy_fp_eq:
6218 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
6219 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_eqidx]);
6220 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
6221out_error:
6222 return rc;
6223}
6224
6225/**
6226 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
6227 * @phba: pointer to lpfc hba data structure.
6228 *
6229 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
6230 * operation.
6231 *
6232 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006233 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006234 * ENOMEM - No availble memory
6235 * EIO - The mailbox failed to complete successfully.
6236 **/
6237void
6238lpfc_sli4_queue_unset(struct lpfc_hba *phba)
6239{
6240 int fcp_qidx;
6241
6242 /* Unset mailbox command work queue */
6243 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
6244 /* Unset ELS work queue */
6245 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
6246 /* Unset unsolicited receive queue */
6247 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
6248 /* Unset FCP work queue */
6249 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
6250 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
6251 /* Unset mailbox command complete queue */
6252 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
6253 /* Unset ELS complete queue */
6254 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
James Smartda0436e2009-05-22 14:51:39 -04006255 /* Unset FCP response complete queue */
6256 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
6257 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
6258 /* Unset fast-path event queue */
6259 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
6260 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_qidx]);
6261 /* Unset slow-path event queue */
6262 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
6263}
6264
6265/**
6266 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
6267 * @phba: pointer to lpfc hba data structure.
6268 *
6269 * This routine is invoked to allocate and set up a pool of completion queue
6270 * events. The body of the completion queue event is a completion queue entry
6271 * CQE. For now, this pool is used for the interrupt service routine to queue
6272 * the following HBA completion queue events for the worker thread to process:
6273 * - Mailbox asynchronous events
6274 * - Receive queue completion unsolicited events
6275 * Later, this can be used for all the slow-path events.
6276 *
6277 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006278 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006279 * -ENOMEM - No availble memory
6280 **/
6281static int
6282lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
6283{
6284 struct lpfc_cq_event *cq_event;
6285 int i;
6286
6287 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
6288 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
6289 if (!cq_event)
6290 goto out_pool_create_fail;
6291 list_add_tail(&cq_event->list,
6292 &phba->sli4_hba.sp_cqe_event_pool);
6293 }
6294 return 0;
6295
6296out_pool_create_fail:
6297 lpfc_sli4_cq_event_pool_destroy(phba);
6298 return -ENOMEM;
6299}
6300
6301/**
6302 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
6303 * @phba: pointer to lpfc hba data structure.
6304 *
6305 * This routine is invoked to free the pool of completion queue events at
6306 * driver unload time. Note that, it is the responsibility of the driver
6307 * cleanup routine to free all the outstanding completion-queue events
6308 * allocated from this pool back into the pool before invoking this routine
6309 * to destroy the pool.
6310 **/
6311static void
6312lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
6313{
6314 struct lpfc_cq_event *cq_event, *next_cq_event;
6315
6316 list_for_each_entry_safe(cq_event, next_cq_event,
6317 &phba->sli4_hba.sp_cqe_event_pool, list) {
6318 list_del(&cq_event->list);
6319 kfree(cq_event);
6320 }
6321}
6322
6323/**
6324 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
6325 * @phba: pointer to lpfc hba data structure.
6326 *
6327 * This routine is the lock free version of the API invoked to allocate a
6328 * completion-queue event from the free pool.
6329 *
6330 * Return: Pointer to the newly allocated completion-queue event if successful
6331 * NULL otherwise.
6332 **/
6333struct lpfc_cq_event *
6334__lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
6335{
6336 struct lpfc_cq_event *cq_event = NULL;
6337
6338 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
6339 struct lpfc_cq_event, list);
6340 return cq_event;
6341}
6342
6343/**
6344 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
6345 * @phba: pointer to lpfc hba data structure.
6346 *
6347 * This routine is the lock version of the API invoked to allocate a
6348 * completion-queue event from the free pool.
6349 *
6350 * Return: Pointer to the newly allocated completion-queue event if successful
6351 * NULL otherwise.
6352 **/
6353struct lpfc_cq_event *
6354lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
6355{
6356 struct lpfc_cq_event *cq_event;
6357 unsigned long iflags;
6358
6359 spin_lock_irqsave(&phba->hbalock, iflags);
6360 cq_event = __lpfc_sli4_cq_event_alloc(phba);
6361 spin_unlock_irqrestore(&phba->hbalock, iflags);
6362 return cq_event;
6363}
6364
6365/**
6366 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
6367 * @phba: pointer to lpfc hba data structure.
6368 * @cq_event: pointer to the completion queue event to be freed.
6369 *
6370 * This routine is the lock free version of the API invoked to release a
6371 * completion-queue event back into the free pool.
6372 **/
6373void
6374__lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
6375 struct lpfc_cq_event *cq_event)
6376{
6377 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
6378}
6379
6380/**
6381 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
6382 * @phba: pointer to lpfc hba data structure.
6383 * @cq_event: pointer to the completion queue event to be freed.
6384 *
6385 * This routine is the lock version of the API invoked to release a
6386 * completion-queue event back into the free pool.
6387 **/
6388void
6389lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
6390 struct lpfc_cq_event *cq_event)
6391{
6392 unsigned long iflags;
6393 spin_lock_irqsave(&phba->hbalock, iflags);
6394 __lpfc_sli4_cq_event_release(phba, cq_event);
6395 spin_unlock_irqrestore(&phba->hbalock, iflags);
6396}
6397
6398/**
6399 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
6400 * @phba: pointer to lpfc hba data structure.
6401 *
6402 * This routine is to free all the pending completion-queue events to the
6403 * back into the free pool for device reset.
6404 **/
6405static void
6406lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
6407{
6408 LIST_HEAD(cqelist);
6409 struct lpfc_cq_event *cqe;
6410 unsigned long iflags;
6411
6412 /* Retrieve all the pending WCQEs from pending WCQE lists */
6413 spin_lock_irqsave(&phba->hbalock, iflags);
6414 /* Pending FCP XRI abort events */
6415 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
6416 &cqelist);
6417 /* Pending ELS XRI abort events */
6418 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
6419 &cqelist);
6420 /* Pending asynnc events */
6421 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
6422 &cqelist);
6423 spin_unlock_irqrestore(&phba->hbalock, iflags);
6424
6425 while (!list_empty(&cqelist)) {
6426 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
6427 lpfc_sli4_cq_event_release(phba, cqe);
6428 }
6429}
6430
6431/**
6432 * lpfc_pci_function_reset - Reset pci function.
6433 * @phba: pointer to lpfc hba data structure.
6434 *
6435 * This routine is invoked to request a PCI function reset. It will destroys
6436 * all resources assigned to the PCI function which originates this request.
6437 *
6438 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006439 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006440 * ENOMEM - No availble memory
6441 * EIO - The mailbox failed to complete successfully.
6442 **/
6443int
6444lpfc_pci_function_reset(struct lpfc_hba *phba)
6445{
6446 LPFC_MBOXQ_t *mboxq;
6447 uint32_t rc = 0;
6448 uint32_t shdr_status, shdr_add_status;
6449 union lpfc_sli4_cfg_shdr *shdr;
6450
6451 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6452 if (!mboxq) {
6453 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6454 "0494 Unable to allocate memory for issuing "
6455 "SLI_FUNCTION_RESET mailbox command\n");
6456 return -ENOMEM;
6457 }
6458
6459 /* Set up PCI function reset SLI4_CONFIG mailbox-ioctl command */
6460 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
6461 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
6462 LPFC_SLI4_MBX_EMBED);
6463 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6464 shdr = (union lpfc_sli4_cfg_shdr *)
6465 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
6466 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6467 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6468 if (rc != MBX_TIMEOUT)
6469 mempool_free(mboxq, phba->mbox_mem_pool);
6470 if (shdr_status || shdr_add_status || rc) {
6471 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6472 "0495 SLI_FUNCTION_RESET mailbox failed with "
6473 "status x%x add_status x%x, mbx status x%x\n",
6474 shdr_status, shdr_add_status, rc);
6475 rc = -ENXIO;
6476 }
6477 return rc;
6478}
6479
6480/**
6481 * lpfc_sli4_send_nop_mbox_cmds - Send sli-4 nop mailbox commands
6482 * @phba: pointer to lpfc hba data structure.
6483 * @cnt: number of nop mailbox commands to send.
6484 *
6485 * This routine is invoked to send a number @cnt of NOP mailbox command and
6486 * wait for each command to complete.
6487 *
6488 * Return: the number of NOP mailbox command completed.
6489 **/
6490static int
6491lpfc_sli4_send_nop_mbox_cmds(struct lpfc_hba *phba, uint32_t cnt)
6492{
6493 LPFC_MBOXQ_t *mboxq;
6494 int length, cmdsent;
6495 uint32_t mbox_tmo;
6496 uint32_t rc = 0;
6497 uint32_t shdr_status, shdr_add_status;
6498 union lpfc_sli4_cfg_shdr *shdr;
6499
6500 if (cnt == 0) {
6501 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6502 "2518 Requested to send 0 NOP mailbox cmd\n");
6503 return cnt;
6504 }
6505
6506 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6507 if (!mboxq) {
6508 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6509 "2519 Unable to allocate memory for issuing "
6510 "NOP mailbox command\n");
6511 return 0;
6512 }
6513
6514 /* Set up NOP SLI4_CONFIG mailbox-ioctl command */
6515 length = (sizeof(struct lpfc_mbx_nop) -
6516 sizeof(struct lpfc_sli4_cfg_mhdr));
6517 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
6518 LPFC_MBOX_OPCODE_NOP, length, LPFC_SLI4_MBX_EMBED);
6519
6520 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
6521 for (cmdsent = 0; cmdsent < cnt; cmdsent++) {
6522 if (!phba->sli4_hba.intr_enable)
6523 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6524 else
6525 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
6526 if (rc == MBX_TIMEOUT)
6527 break;
6528 /* Check return status */
6529 shdr = (union lpfc_sli4_cfg_shdr *)
6530 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
6531 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6532 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
6533 &shdr->response);
6534 if (shdr_status || shdr_add_status || rc) {
6535 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6536 "2520 NOP mailbox command failed "
6537 "status x%x add_status x%x mbx "
6538 "status x%x\n", shdr_status,
6539 shdr_add_status, rc);
6540 break;
6541 }
6542 }
6543
6544 if (rc != MBX_TIMEOUT)
6545 mempool_free(mboxq, phba->mbox_mem_pool);
6546
6547 return cmdsent;
6548}
6549
6550/**
6551 * lpfc_sli4_fcfi_unreg - Unregister fcfi to device
6552 * @phba: pointer to lpfc hba data structure.
6553 * @fcfi: fcf index.
6554 *
6555 * This routine is invoked to unregister a FCFI from device.
6556 **/
6557void
6558lpfc_sli4_fcfi_unreg(struct lpfc_hba *phba, uint16_t fcfi)
6559{
6560 LPFC_MBOXQ_t *mbox;
6561 uint32_t mbox_tmo;
6562 int rc;
6563 unsigned long flags;
6564
6565 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6566
6567 if (!mbox)
6568 return;
6569
6570 lpfc_unreg_fcfi(mbox, fcfi);
6571
6572 if (!phba->sli4_hba.intr_enable)
6573 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6574 else {
6575 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
6576 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6577 }
6578 if (rc != MBX_TIMEOUT)
6579 mempool_free(mbox, phba->mbox_mem_pool);
6580 if (rc != MBX_SUCCESS)
6581 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6582 "2517 Unregister FCFI command failed "
6583 "status %d, mbxStatus x%x\n", rc,
6584 bf_get(lpfc_mqe_status, &mbox->u.mqe));
6585 else {
6586 spin_lock_irqsave(&phba->hbalock, flags);
6587 /* Mark the FCFI is no longer registered */
6588 phba->fcf.fcf_flag &=
James Smartecfd03c2010-02-12 14:41:27 -05006589 ~(FCF_AVAILABLE | FCF_REGISTERED | FCF_SCAN_DONE);
James Smartda0436e2009-05-22 14:51:39 -04006590 spin_unlock_irqrestore(&phba->hbalock, flags);
6591 }
6592}
6593
6594/**
6595 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
6596 * @phba: pointer to lpfc hba data structure.
6597 *
6598 * This routine is invoked to set up the PCI device memory space for device
6599 * with SLI-4 interface spec.
6600 *
6601 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006602 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04006603 * other values - error
6604 **/
6605static int
6606lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
6607{
6608 struct pci_dev *pdev;
6609 unsigned long bar0map_len, bar1map_len, bar2map_len;
6610 int error = -ENODEV;
6611
6612 /* Obtain PCI device reference */
6613 if (!phba->pcidev)
6614 return error;
6615 else
6616 pdev = phba->pcidev;
6617
6618 /* Set the device DMA mask size */
Michael Reed8e685972009-09-18 12:02:05 -05006619 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
6620 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
6621 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
6622 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
James Smartda0436e2009-05-22 14:51:39 -04006623 return error;
Michael Reed8e685972009-09-18 12:02:05 -05006624 }
6625 }
James Smartda0436e2009-05-22 14:51:39 -04006626
6627 /* Get the bus address of SLI4 device Bar0, Bar1, and Bar2 and the
6628 * number of bytes required by each mapping. They are actually
James Smart1dfb5a42010-02-12 14:40:50 -05006629 * mapping to the PCI BAR regions 0 or 1, 2, and 4 by the SLI4 device.
James Smartda0436e2009-05-22 14:51:39 -04006630 */
James Smart1dfb5a42010-02-12 14:40:50 -05006631 if (pci_resource_start(pdev, 0)) {
6632 phba->pci_bar0_map = pci_resource_start(pdev, 0);
6633 bar0map_len = pci_resource_len(pdev, 0);
6634 } else {
6635 phba->pci_bar0_map = pci_resource_start(pdev, 1);
6636 bar0map_len = pci_resource_len(pdev, 1);
6637 }
6638 phba->pci_bar1_map = pci_resource_start(pdev, 2);
6639 bar1map_len = pci_resource_len(pdev, 2);
James Smartda0436e2009-05-22 14:51:39 -04006640
James Smart1dfb5a42010-02-12 14:40:50 -05006641 phba->pci_bar2_map = pci_resource_start(pdev, 4);
6642 bar2map_len = pci_resource_len(pdev, 4);
James Smartda0436e2009-05-22 14:51:39 -04006643
6644 /* Map SLI4 PCI Config Space Register base to a kernel virtual addr */
6645 phba->sli4_hba.conf_regs_memmap_p =
6646 ioremap(phba->pci_bar0_map, bar0map_len);
6647 if (!phba->sli4_hba.conf_regs_memmap_p) {
6648 dev_printk(KERN_ERR, &pdev->dev,
6649 "ioremap failed for SLI4 PCI config registers.\n");
6650 goto out;
6651 }
6652
6653 /* Map SLI4 HBA Control Register base to a kernel virtual address. */
6654 phba->sli4_hba.ctrl_regs_memmap_p =
6655 ioremap(phba->pci_bar1_map, bar1map_len);
6656 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
6657 dev_printk(KERN_ERR, &pdev->dev,
6658 "ioremap failed for SLI4 HBA control registers.\n");
6659 goto out_iounmap_conf;
6660 }
6661
6662 /* Map SLI4 HBA Doorbell Register base to a kernel virtual address. */
6663 phba->sli4_hba.drbl_regs_memmap_p =
6664 ioremap(phba->pci_bar2_map, bar2map_len);
6665 if (!phba->sli4_hba.drbl_regs_memmap_p) {
6666 dev_printk(KERN_ERR, &pdev->dev,
6667 "ioremap failed for SLI4 HBA doorbell registers.\n");
6668 goto out_iounmap_ctrl;
6669 }
6670
6671 /* Set up BAR0 PCI config space register memory map */
6672 lpfc_sli4_bar0_register_memmap(phba);
6673
6674 /* Set up BAR1 register memory map */
6675 lpfc_sli4_bar1_register_memmap(phba);
6676
6677 /* Set up BAR2 register memory map */
6678 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
6679 if (error)
6680 goto out_iounmap_all;
6681
6682 return 0;
6683
6684out_iounmap_all:
6685 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
6686out_iounmap_ctrl:
6687 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
6688out_iounmap_conf:
6689 iounmap(phba->sli4_hba.conf_regs_memmap_p);
6690out:
6691 return error;
6692}
6693
6694/**
6695 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
6696 * @phba: pointer to lpfc hba data structure.
6697 *
6698 * This routine is invoked to unset the PCI device memory space for device
6699 * with SLI-4 interface spec.
6700 **/
6701static void
6702lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
6703{
6704 struct pci_dev *pdev;
6705
6706 /* Obtain PCI device reference */
6707 if (!phba->pcidev)
6708 return;
6709 else
6710 pdev = phba->pcidev;
6711
6712 /* Free coherent DMA memory allocated */
6713
6714 /* Unmap I/O memory space */
6715 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
6716 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
6717 iounmap(phba->sli4_hba.conf_regs_memmap_p);
6718
6719 return;
6720}
6721
6722/**
James Smart3772a992009-05-22 14:50:54 -04006723 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
6724 * @phba: pointer to lpfc hba data structure.
6725 *
6726 * This routine is invoked to enable the MSI-X interrupt vectors to device
6727 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
6728 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
6729 * invoked, enables either all or nothing, depending on the current
6730 * availability of PCI vector resources. The device driver is responsible
6731 * for calling the individual request_irq() to register each MSI-X vector
6732 * with a interrupt handler, which is done in this function. Note that
6733 * later when device is unloading, the driver should always call free_irq()
6734 * on all MSI-X vectors it has done request_irq() on before calling
6735 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
6736 * will be left with MSI-X enabled and leaks its vectors.
6737 *
6738 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006739 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006740 * other values - error
6741 **/
6742static int
6743lpfc_sli_enable_msix(struct lpfc_hba *phba)
6744{
6745 int rc, i;
6746 LPFC_MBOXQ_t *pmb;
6747
6748 /* Set up MSI-X multi-message vectors */
6749 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6750 phba->msix_entries[i].entry = i;
6751
6752 /* Configure MSI-X capability structure */
6753 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
6754 ARRAY_SIZE(phba->msix_entries));
6755 if (rc) {
6756 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6757 "0420 PCI enable MSI-X failed (%d)\n", rc);
6758 goto msi_fail_out;
6759 }
6760 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6761 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6762 "0477 MSI-X entry[%d]: vector=x%x "
6763 "message=%d\n", i,
6764 phba->msix_entries[i].vector,
6765 phba->msix_entries[i].entry);
6766 /*
6767 * Assign MSI-X vectors to interrupt handlers
6768 */
6769
6770 /* vector-0 is associated to slow-path handler */
6771 rc = request_irq(phba->msix_entries[0].vector,
6772 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
6773 LPFC_SP_DRIVER_HANDLER_NAME, phba);
6774 if (rc) {
6775 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6776 "0421 MSI-X slow-path request_irq failed "
6777 "(%d)\n", rc);
6778 goto msi_fail_out;
6779 }
6780
6781 /* vector-1 is associated to fast-path handler */
6782 rc = request_irq(phba->msix_entries[1].vector,
6783 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
6784 LPFC_FP_DRIVER_HANDLER_NAME, phba);
6785
6786 if (rc) {
6787 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6788 "0429 MSI-X fast-path request_irq failed "
6789 "(%d)\n", rc);
6790 goto irq_fail_out;
6791 }
6792
6793 /*
6794 * Configure HBA MSI-X attention conditions to messages
6795 */
6796 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6797
6798 if (!pmb) {
6799 rc = -ENOMEM;
6800 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6801 "0474 Unable to allocate memory for issuing "
6802 "MBOX_CONFIG_MSI command\n");
6803 goto mem_fail_out;
6804 }
6805 rc = lpfc_config_msi(phba, pmb);
6806 if (rc)
6807 goto mbx_fail_out;
6808 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6809 if (rc != MBX_SUCCESS) {
6810 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
6811 "0351 Config MSI mailbox command failed, "
6812 "mbxCmd x%x, mbxStatus x%x\n",
6813 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
6814 goto mbx_fail_out;
6815 }
6816
6817 /* Free memory allocated for mailbox command */
6818 mempool_free(pmb, phba->mbox_mem_pool);
6819 return rc;
6820
6821mbx_fail_out:
6822 /* Free memory allocated for mailbox command */
6823 mempool_free(pmb, phba->mbox_mem_pool);
6824
6825mem_fail_out:
6826 /* free the irq already requested */
6827 free_irq(phba->msix_entries[1].vector, phba);
6828
6829irq_fail_out:
6830 /* free the irq already requested */
6831 free_irq(phba->msix_entries[0].vector, phba);
6832
6833msi_fail_out:
6834 /* Unconfigure MSI-X capability structure */
6835 pci_disable_msix(phba->pcidev);
6836 return rc;
6837}
6838
6839/**
6840 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
6841 * @phba: pointer to lpfc hba data structure.
6842 *
6843 * This routine is invoked to release the MSI-X vectors and then disable the
6844 * MSI-X interrupt mode to device with SLI-3 interface spec.
6845 **/
6846static void
6847lpfc_sli_disable_msix(struct lpfc_hba *phba)
6848{
6849 int i;
6850
6851 /* Free up MSI-X multi-message vectors */
6852 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
6853 free_irq(phba->msix_entries[i].vector, phba);
6854 /* Disable MSI-X */
6855 pci_disable_msix(phba->pcidev);
6856
6857 return;
6858}
6859
6860/**
6861 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
6862 * @phba: pointer to lpfc hba data structure.
6863 *
6864 * This routine is invoked to enable the MSI interrupt mode to device with
6865 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
6866 * enable the MSI vector. The device driver is responsible for calling the
6867 * request_irq() to register MSI vector with a interrupt the handler, which
6868 * is done in this function.
6869 *
6870 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006871 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006872 * other values - error
6873 */
6874static int
6875lpfc_sli_enable_msi(struct lpfc_hba *phba)
6876{
6877 int rc;
6878
6879 rc = pci_enable_msi(phba->pcidev);
6880 if (!rc)
6881 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6882 "0462 PCI enable MSI mode success.\n");
6883 else {
6884 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6885 "0471 PCI enable MSI mode failed (%d)\n", rc);
6886 return rc;
6887 }
6888
6889 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
6890 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6891 if (rc) {
6892 pci_disable_msi(phba->pcidev);
6893 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6894 "0478 MSI request_irq failed (%d)\n", rc);
6895 }
6896 return rc;
6897}
6898
6899/**
6900 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
6901 * @phba: pointer to lpfc hba data structure.
6902 *
6903 * This routine is invoked to disable the MSI interrupt mode to device with
6904 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
6905 * done request_irq() on before calling pci_disable_msi(). Failure to do so
6906 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
6907 * its vector.
6908 */
6909static void
6910lpfc_sli_disable_msi(struct lpfc_hba *phba)
6911{
6912 free_irq(phba->pcidev->irq, phba);
6913 pci_disable_msi(phba->pcidev);
6914 return;
6915}
6916
6917/**
6918 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
6919 * @phba: pointer to lpfc hba data structure.
6920 *
6921 * This routine is invoked to enable device interrupt and associate driver's
6922 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
6923 * spec. Depends on the interrupt mode configured to the driver, the driver
6924 * will try to fallback from the configured interrupt mode to an interrupt
6925 * mode which is supported by the platform, kernel, and device in the order
6926 * of:
6927 * MSI-X -> MSI -> IRQ.
6928 *
6929 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02006930 * 0 - successful
James Smart3772a992009-05-22 14:50:54 -04006931 * other values - error
6932 **/
6933static uint32_t
6934lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
6935{
6936 uint32_t intr_mode = LPFC_INTR_ERROR;
6937 int retval;
6938
6939 if (cfg_mode == 2) {
6940 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
6941 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
6942 if (!retval) {
6943 /* Now, try to enable MSI-X interrupt mode */
6944 retval = lpfc_sli_enable_msix(phba);
6945 if (!retval) {
6946 /* Indicate initialization to MSI-X mode */
6947 phba->intr_type = MSIX;
6948 intr_mode = 2;
6949 }
6950 }
6951 }
6952
6953 /* Fallback to MSI if MSI-X initialization failed */
6954 if (cfg_mode >= 1 && phba->intr_type == NONE) {
6955 retval = lpfc_sli_enable_msi(phba);
6956 if (!retval) {
6957 /* Indicate initialization to MSI mode */
6958 phba->intr_type = MSI;
6959 intr_mode = 1;
6960 }
6961 }
6962
6963 /* Fallback to INTx if both MSI-X/MSI initalization failed */
6964 if (phba->intr_type == NONE) {
6965 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
6966 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
6967 if (!retval) {
6968 /* Indicate initialization to INTx mode */
6969 phba->intr_type = INTx;
6970 intr_mode = 0;
6971 }
6972 }
6973 return intr_mode;
6974}
6975
6976/**
6977 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
6978 * @phba: pointer to lpfc hba data structure.
6979 *
6980 * This routine is invoked to disable device interrupt and disassociate the
6981 * driver's interrupt handler(s) from interrupt vector(s) to device with
6982 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
6983 * release the interrupt vector(s) for the message signaled interrupt.
6984 **/
6985static void
6986lpfc_sli_disable_intr(struct lpfc_hba *phba)
6987{
6988 /* Disable the currently initialized interrupt mode */
6989 if (phba->intr_type == MSIX)
6990 lpfc_sli_disable_msix(phba);
6991 else if (phba->intr_type == MSI)
6992 lpfc_sli_disable_msi(phba);
6993 else if (phba->intr_type == INTx)
6994 free_irq(phba->pcidev->irq, phba);
6995
6996 /* Reset interrupt management states */
6997 phba->intr_type = NONE;
6998 phba->sli.slistat.sli_intr = 0;
6999
7000 return;
7001}
7002
7003/**
James Smartda0436e2009-05-22 14:51:39 -04007004 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
7005 * @phba: pointer to lpfc hba data structure.
7006 *
7007 * This routine is invoked to enable the MSI-X interrupt vectors to device
7008 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
7009 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
7010 * enables either all or nothing, depending on the current availability of
7011 * PCI vector resources. The device driver is responsible for calling the
7012 * individual request_irq() to register each MSI-X vector with a interrupt
7013 * handler, which is done in this function. Note that later when device is
7014 * unloading, the driver should always call free_irq() on all MSI-X vectors
7015 * it has done request_irq() on before calling pci_disable_msix(). Failure
7016 * to do so results in a BUG_ON() and a device will be left with MSI-X
7017 * enabled and leaks its vectors.
7018 *
7019 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007020 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007021 * other values - error
7022 **/
7023static int
7024lpfc_sli4_enable_msix(struct lpfc_hba *phba)
7025{
7026 int rc, index;
7027
7028 /* Set up MSI-X multi-message vectors */
7029 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
7030 phba->sli4_hba.msix_entries[index].entry = index;
7031
7032 /* Configure MSI-X capability structure */
7033 rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
7034 phba->sli4_hba.cfg_eqn);
7035 if (rc) {
7036 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7037 "0484 PCI enable MSI-X failed (%d)\n", rc);
7038 goto msi_fail_out;
7039 }
7040 /* Log MSI-X vector assignment */
7041 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
7042 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7043 "0489 MSI-X entry[%d]: vector=x%x "
7044 "message=%d\n", index,
7045 phba->sli4_hba.msix_entries[index].vector,
7046 phba->sli4_hba.msix_entries[index].entry);
7047 /*
7048 * Assign MSI-X vectors to interrupt handlers
7049 */
7050
7051 /* The first vector must associated to slow-path handler for MQ */
7052 rc = request_irq(phba->sli4_hba.msix_entries[0].vector,
7053 &lpfc_sli4_sp_intr_handler, IRQF_SHARED,
7054 LPFC_SP_DRIVER_HANDLER_NAME, phba);
7055 if (rc) {
7056 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7057 "0485 MSI-X slow-path request_irq failed "
7058 "(%d)\n", rc);
7059 goto msi_fail_out;
7060 }
7061
7062 /* The rest of the vector(s) are associated to fast-path handler(s) */
7063 for (index = 1; index < phba->sli4_hba.cfg_eqn; index++) {
7064 phba->sli4_hba.fcp_eq_hdl[index - 1].idx = index - 1;
7065 phba->sli4_hba.fcp_eq_hdl[index - 1].phba = phba;
7066 rc = request_irq(phba->sli4_hba.msix_entries[index].vector,
7067 &lpfc_sli4_fp_intr_handler, IRQF_SHARED,
7068 LPFC_FP_DRIVER_HANDLER_NAME,
7069 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
7070 if (rc) {
7071 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7072 "0486 MSI-X fast-path (%d) "
7073 "request_irq failed (%d)\n", index, rc);
7074 goto cfg_fail_out;
7075 }
7076 }
7077
7078 return rc;
7079
7080cfg_fail_out:
7081 /* free the irq already requested */
7082 for (--index; index >= 1; index--)
7083 free_irq(phba->sli4_hba.msix_entries[index - 1].vector,
7084 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
7085
7086 /* free the irq already requested */
7087 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
7088
7089msi_fail_out:
7090 /* Unconfigure MSI-X capability structure */
7091 pci_disable_msix(phba->pcidev);
7092 return rc;
7093}
7094
7095/**
7096 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
7097 * @phba: pointer to lpfc hba data structure.
7098 *
7099 * This routine is invoked to release the MSI-X vectors and then disable the
7100 * MSI-X interrupt mode to device with SLI-4 interface spec.
7101 **/
7102static void
7103lpfc_sli4_disable_msix(struct lpfc_hba *phba)
7104{
7105 int index;
7106
7107 /* Free up MSI-X multi-message vectors */
7108 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
7109
7110 for (index = 1; index < phba->sli4_hba.cfg_eqn; index++)
7111 free_irq(phba->sli4_hba.msix_entries[index].vector,
7112 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
7113 /* Disable MSI-X */
7114 pci_disable_msix(phba->pcidev);
7115
7116 return;
7117}
7118
7119/**
7120 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
7121 * @phba: pointer to lpfc hba data structure.
7122 *
7123 * This routine is invoked to enable the MSI interrupt mode to device with
7124 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
7125 * to enable the MSI vector. The device driver is responsible for calling
7126 * the request_irq() to register MSI vector with a interrupt the handler,
7127 * which is done in this function.
7128 *
7129 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007130 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007131 * other values - error
7132 **/
7133static int
7134lpfc_sli4_enable_msi(struct lpfc_hba *phba)
7135{
7136 int rc, index;
7137
7138 rc = pci_enable_msi(phba->pcidev);
7139 if (!rc)
7140 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7141 "0487 PCI enable MSI mode success.\n");
7142 else {
7143 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7144 "0488 PCI enable MSI mode failed (%d)\n", rc);
7145 return rc;
7146 }
7147
7148 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
7149 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7150 if (rc) {
7151 pci_disable_msi(phba->pcidev);
7152 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7153 "0490 MSI request_irq failed (%d)\n", rc);
7154 }
7155
7156 for (index = 0; index < phba->cfg_fcp_eq_count; index++) {
7157 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
7158 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
7159 }
7160
7161 return rc;
7162}
7163
7164/**
7165 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
7166 * @phba: pointer to lpfc hba data structure.
7167 *
7168 * This routine is invoked to disable the MSI interrupt mode to device with
7169 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
7170 * done request_irq() on before calling pci_disable_msi(). Failure to do so
7171 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
7172 * its vector.
7173 **/
7174static void
7175lpfc_sli4_disable_msi(struct lpfc_hba *phba)
7176{
7177 free_irq(phba->pcidev->irq, phba);
7178 pci_disable_msi(phba->pcidev);
7179 return;
7180}
7181
7182/**
7183 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
7184 * @phba: pointer to lpfc hba data structure.
7185 *
7186 * This routine is invoked to enable device interrupt and associate driver's
7187 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
7188 * interface spec. Depends on the interrupt mode configured to the driver,
7189 * the driver will try to fallback from the configured interrupt mode to an
7190 * interrupt mode which is supported by the platform, kernel, and device in
7191 * the order of:
7192 * MSI-X -> MSI -> IRQ.
7193 *
7194 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02007195 * 0 - successful
James Smartda0436e2009-05-22 14:51:39 -04007196 * other values - error
7197 **/
7198static uint32_t
7199lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
7200{
7201 uint32_t intr_mode = LPFC_INTR_ERROR;
7202 int retval, index;
7203
7204 if (cfg_mode == 2) {
7205 /* Preparation before conf_msi mbox cmd */
7206 retval = 0;
7207 if (!retval) {
7208 /* Now, try to enable MSI-X interrupt mode */
7209 retval = lpfc_sli4_enable_msix(phba);
7210 if (!retval) {
7211 /* Indicate initialization to MSI-X mode */
7212 phba->intr_type = MSIX;
7213 intr_mode = 2;
7214 }
7215 }
7216 }
7217
7218 /* Fallback to MSI if MSI-X initialization failed */
7219 if (cfg_mode >= 1 && phba->intr_type == NONE) {
7220 retval = lpfc_sli4_enable_msi(phba);
7221 if (!retval) {
7222 /* Indicate initialization to MSI mode */
7223 phba->intr_type = MSI;
7224 intr_mode = 1;
7225 }
7226 }
7227
7228 /* Fallback to INTx if both MSI-X/MSI initalization failed */
7229 if (phba->intr_type == NONE) {
7230 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
7231 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7232 if (!retval) {
7233 /* Indicate initialization to INTx mode */
7234 phba->intr_type = INTx;
7235 intr_mode = 0;
7236 for (index = 0; index < phba->cfg_fcp_eq_count;
7237 index++) {
7238 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
7239 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
7240 }
7241 }
7242 }
7243 return intr_mode;
7244}
7245
7246/**
7247 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
7248 * @phba: pointer to lpfc hba data structure.
7249 *
7250 * This routine is invoked to disable device interrupt and disassociate
7251 * the driver's interrupt handler(s) from interrupt vector(s) to device
7252 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
7253 * will release the interrupt vector(s) for the message signaled interrupt.
7254 **/
7255static void
7256lpfc_sli4_disable_intr(struct lpfc_hba *phba)
7257{
7258 /* Disable the currently initialized interrupt mode */
7259 if (phba->intr_type == MSIX)
7260 lpfc_sli4_disable_msix(phba);
7261 else if (phba->intr_type == MSI)
7262 lpfc_sli4_disable_msi(phba);
7263 else if (phba->intr_type == INTx)
7264 free_irq(phba->pcidev->irq, phba);
7265
7266 /* Reset interrupt management states */
7267 phba->intr_type = NONE;
7268 phba->sli.slistat.sli_intr = 0;
7269
7270 return;
7271}
7272
7273/**
James Smart3772a992009-05-22 14:50:54 -04007274 * lpfc_unset_hba - Unset SLI3 hba device initialization
7275 * @phba: pointer to lpfc hba data structure.
7276 *
7277 * This routine is invoked to unset the HBA device initialization steps to
7278 * a device with SLI-3 interface spec.
7279 **/
7280static void
7281lpfc_unset_hba(struct lpfc_hba *phba)
7282{
7283 struct lpfc_vport *vport = phba->pport;
7284 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7285
7286 spin_lock_irq(shost->host_lock);
7287 vport->load_flag |= FC_UNLOADING;
7288 spin_unlock_irq(shost->host_lock);
7289
7290 lpfc_stop_hba_timers(phba);
7291
7292 phba->pport->work_port_events = 0;
7293
7294 lpfc_sli_hba_down(phba);
7295
7296 lpfc_sli_brdrestart(phba);
7297
7298 lpfc_sli_disable_intr(phba);
7299
7300 return;
7301}
7302
7303/**
James Smartda0436e2009-05-22 14:51:39 -04007304 * lpfc_sli4_unset_hba - Unset SLI4 hba device initialization.
7305 * @phba: pointer to lpfc hba data structure.
7306 *
7307 * This routine is invoked to unset the HBA device initialization steps to
7308 * a device with SLI-4 interface spec.
7309 **/
7310static void
7311lpfc_sli4_unset_hba(struct lpfc_hba *phba)
7312{
7313 struct lpfc_vport *vport = phba->pport;
7314 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7315
7316 spin_lock_irq(shost->host_lock);
7317 vport->load_flag |= FC_UNLOADING;
7318 spin_unlock_irq(shost->host_lock);
7319
7320 phba->pport->work_port_events = 0;
7321
7322 lpfc_sli4_hba_down(phba);
7323
7324 lpfc_sli4_disable_intr(phba);
7325
7326 return;
7327}
7328
7329/**
7330 * lpfc_sli4_hba_unset - Unset the fcoe hba
7331 * @phba: Pointer to HBA context object.
7332 *
7333 * This function is called in the SLI4 code path to reset the HBA's FCoE
7334 * function. The caller is not required to hold any lock. This routine
7335 * issues PCI function reset mailbox command to reset the FCoE function.
7336 * At the end of the function, it calls lpfc_hba_down_post function to
7337 * free any pending commands.
7338 **/
7339static void
7340lpfc_sli4_hba_unset(struct lpfc_hba *phba)
7341{
7342 int wait_cnt = 0;
7343 LPFC_MBOXQ_t *mboxq;
7344
7345 lpfc_stop_hba_timers(phba);
7346 phba->sli4_hba.intr_enable = 0;
7347
7348 /*
7349 * Gracefully wait out the potential current outstanding asynchronous
7350 * mailbox command.
7351 */
7352
7353 /* First, block any pending async mailbox command from posted */
7354 spin_lock_irq(&phba->hbalock);
7355 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7356 spin_unlock_irq(&phba->hbalock);
7357 /* Now, trying to wait it out if we can */
7358 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7359 msleep(10);
7360 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
7361 break;
7362 }
7363 /* Forcefully release the outstanding mailbox command if timed out */
7364 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7365 spin_lock_irq(&phba->hbalock);
7366 mboxq = phba->sli.mbox_active;
7367 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7368 __lpfc_mbox_cmpl_put(phba, mboxq);
7369 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7370 phba->sli.mbox_active = NULL;
7371 spin_unlock_irq(&phba->hbalock);
7372 }
7373
7374 /* Tear down the queues in the HBA */
7375 lpfc_sli4_queue_unset(phba);
7376
7377 /* Disable PCI subsystem interrupt */
7378 lpfc_sli4_disable_intr(phba);
7379
7380 /* Stop kthread signal shall trigger work_done one more time */
7381 kthread_stop(phba->worker_thread);
7382
7383 /* Stop the SLI4 device port */
7384 phba->pport->work_port_events = 0;
7385}
7386
James Smart28baac72010-02-12 14:42:03 -05007387 /**
7388 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
7389 * @phba: Pointer to HBA context object.
7390 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
7391 *
7392 * This function is called in the SLI4 code path to read the port's
7393 * sli4 capabilities.
7394 *
7395 * This function may be be called from any context that can block-wait
7396 * for the completion. The expectation is that this routine is called
7397 * typically from probe_one or from the online routine.
7398 **/
7399int
7400lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7401{
7402 int rc;
7403 struct lpfc_mqe *mqe;
7404 struct lpfc_pc_sli4_params *sli4_params;
7405 uint32_t mbox_tmo;
7406
7407 rc = 0;
7408 mqe = &mboxq->u.mqe;
7409
7410 /* Read the port's SLI4 Parameters port capabilities */
7411 lpfc_sli4_params(mboxq);
7412 if (!phba->sli4_hba.intr_enable)
7413 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7414 else {
7415 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_PORT_CAPABILITIES);
7416 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
7417 }
7418
7419 if (unlikely(rc))
7420 return 1;
7421
7422 sli4_params = &phba->sli4_hba.pc_sli4_params;
7423 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
7424 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
7425 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
7426 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
7427 &mqe->un.sli4_params);
7428 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
7429 &mqe->un.sli4_params);
7430 sli4_params->proto_types = mqe->un.sli4_params.word3;
7431 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
7432 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
7433 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
7434 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
7435 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
7436 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
7437 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
7438 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
7439 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
7440 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
7441 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
7442 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
7443 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
7444 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
7445 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
7446 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
7447 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
7448 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
7449 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
7450 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
7451 return rc;
7452}
7453
James Smartda0436e2009-05-22 14:51:39 -04007454/**
James Smart3772a992009-05-22 14:50:54 -04007455 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
7456 * @pdev: pointer to PCI device
7457 * @pid: pointer to PCI device identifier
7458 *
7459 * This routine is to be called to attach a device with SLI-3 interface spec
7460 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
7461 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
7462 * information of the device and driver to see if the driver state that it can
7463 * support this kind of device. If the match is successful, the driver core
7464 * invokes this routine. If this routine determines it can claim the HBA, it
7465 * does all the initialization that it needs to do to handle the HBA properly.
7466 *
7467 * Return code
7468 * 0 - driver can claim the device
7469 * negative value - driver can not claim the device
7470 **/
7471static int __devinit
7472lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
7473{
7474 struct lpfc_hba *phba;
7475 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04007476 struct Scsi_Host *shost = NULL;
James Smart3772a992009-05-22 14:50:54 -04007477 int error;
7478 uint32_t cfg_mode, intr_mode;
7479
7480 /* Allocate memory for HBA structure */
7481 phba = lpfc_hba_alloc(pdev);
7482 if (!phba)
7483 return -ENOMEM;
7484
7485 /* Perform generic PCI device enabling operation */
7486 error = lpfc_enable_pci_dev(phba);
7487 if (error) {
7488 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7489 "1401 Failed to enable pci device.\n");
7490 goto out_free_phba;
7491 }
7492
7493 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
7494 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
7495 if (error)
7496 goto out_disable_pci_dev;
7497
7498 /* Set up SLI-3 specific device PCI memory space */
7499 error = lpfc_sli_pci_mem_setup(phba);
7500 if (error) {
7501 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7502 "1402 Failed to set up pci memory space.\n");
7503 goto out_disable_pci_dev;
7504 }
7505
7506 /* Set up phase-1 common device driver resources */
7507 error = lpfc_setup_driver_resource_phase1(phba);
7508 if (error) {
7509 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7510 "1403 Failed to set up driver resource.\n");
7511 goto out_unset_pci_mem_s3;
7512 }
7513
7514 /* Set up SLI-3 specific device driver resources */
7515 error = lpfc_sli_driver_resource_setup(phba);
7516 if (error) {
7517 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7518 "1404 Failed to set up driver resource.\n");
7519 goto out_unset_pci_mem_s3;
7520 }
7521
7522 /* Initialize and populate the iocb list per host */
7523 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
7524 if (error) {
7525 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7526 "1405 Failed to initialize iocb list.\n");
7527 goto out_unset_driver_resource_s3;
7528 }
7529
7530 /* Set up common device driver resources */
7531 error = lpfc_setup_driver_resource_phase2(phba);
7532 if (error) {
7533 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7534 "1406 Failed to set up driver resource.\n");
7535 goto out_free_iocb_list;
7536 }
7537
7538 /* Create SCSI host to the physical port */
7539 error = lpfc_create_shost(phba);
7540 if (error) {
7541 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7542 "1407 Failed to create scsi host.\n");
7543 goto out_unset_driver_resource;
7544 }
7545
7546 /* Configure sysfs attributes */
7547 vport = phba->pport;
7548 error = lpfc_alloc_sysfs_attr(vport);
7549 if (error) {
7550 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7551 "1476 Failed to allocate sysfs attr\n");
7552 goto out_destroy_shost;
7553 }
7554
James Smart6669f9b2009-10-02 15:16:45 -04007555 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smart3772a992009-05-22 14:50:54 -04007556 /* Now, trying to enable interrupt and bring up the device */
7557 cfg_mode = phba->cfg_use_msi;
7558 while (true) {
7559 /* Put device to a known state before enabling interrupt */
7560 lpfc_stop_port(phba);
7561 /* Configure and enable interrupt */
7562 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
7563 if (intr_mode == LPFC_INTR_ERROR) {
7564 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7565 "0431 Failed to enable interrupt.\n");
7566 error = -ENODEV;
7567 goto out_free_sysfs_attr;
7568 }
7569 /* SLI-3 HBA setup */
7570 if (lpfc_sli_hba_setup(phba)) {
7571 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7572 "1477 Failed to set up hba\n");
7573 error = -ENODEV;
7574 goto out_remove_device;
7575 }
7576
7577 /* Wait 50ms for the interrupts of previous mailbox commands */
7578 msleep(50);
7579 /* Check active interrupts on message signaled interrupts */
7580 if (intr_mode == 0 ||
7581 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
7582 /* Log the current active interrupt mode */
7583 phba->intr_mode = intr_mode;
7584 lpfc_log_intr_mode(phba, intr_mode);
7585 break;
7586 } else {
7587 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7588 "0447 Configure interrupt mode (%d) "
7589 "failed active interrupt test.\n",
7590 intr_mode);
7591 /* Disable the current interrupt mode */
7592 lpfc_sli_disable_intr(phba);
7593 /* Try next level of interrupt mode */
7594 cfg_mode = --intr_mode;
7595 }
7596 }
7597
7598 /* Perform post initialization setup */
7599 lpfc_post_init_setup(phba);
7600
7601 /* Check if there are static vports to be created. */
7602 lpfc_create_static_vport(phba);
7603
7604 return 0;
7605
7606out_remove_device:
7607 lpfc_unset_hba(phba);
7608out_free_sysfs_attr:
7609 lpfc_free_sysfs_attr(vport);
7610out_destroy_shost:
7611 lpfc_destroy_shost(phba);
7612out_unset_driver_resource:
7613 lpfc_unset_driver_resource_phase2(phba);
7614out_free_iocb_list:
7615 lpfc_free_iocb_list(phba);
7616out_unset_driver_resource_s3:
7617 lpfc_sli_driver_resource_unset(phba);
7618out_unset_pci_mem_s3:
7619 lpfc_sli_pci_mem_unset(phba);
7620out_disable_pci_dev:
7621 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04007622 if (shost)
7623 scsi_host_put(shost);
James Smart3772a992009-05-22 14:50:54 -04007624out_free_phba:
7625 lpfc_hba_free(phba);
7626 return error;
7627}
7628
7629/**
7630 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
James Smarte59058c2008-08-24 21:49:00 -04007631 * @pdev: pointer to PCI device
7632 *
James Smart3772a992009-05-22 14:50:54 -04007633 * This routine is to be called to disattach a device with SLI-3 interface
7634 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
7635 * removed from PCI bus, it performs all the necessary cleanup for the HBA
7636 * device to be removed from the PCI subsystem properly.
James Smarte59058c2008-08-24 21:49:00 -04007637 **/
dea31012005-04-17 16:05:31 -05007638static void __devexit
James Smart3772a992009-05-22 14:50:54 -04007639lpfc_pci_remove_one_s3(struct pci_dev *pdev)
dea31012005-04-17 16:05:31 -05007640{
James Smart2e0fef82007-06-17 19:56:36 -05007641 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7642 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
James Smarteada2722008-12-04 22:39:13 -05007643 struct lpfc_vport **vports;
James Smart2e0fef82007-06-17 19:56:36 -05007644 struct lpfc_hba *phba = vport->phba;
James Smarteada2722008-12-04 22:39:13 -05007645 int i;
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05007646 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
7647
James Smart549e55c2007-08-02 11:09:51 -04007648 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04007649 vport->load_flag |= FC_UNLOADING;
James Smart549e55c2007-08-02 11:09:51 -04007650 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007651
James Smart858c9f62007-06-17 19:56:39 -05007652 lpfc_free_sysfs_attr(vport);
7653
James Smarteada2722008-12-04 22:39:13 -05007654 /* Release all the vports against this physical port */
7655 vports = lpfc_create_vport_work_array(phba);
7656 if (vports != NULL)
James Smart3772a992009-05-22 14:50:54 -04007657 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
James Smarteada2722008-12-04 22:39:13 -05007658 fc_vport_terminate(vports[i]->fc_vport);
7659 lpfc_destroy_vport_work_array(phba, vports);
7660
7661 /* Remove FC host and then SCSI host with the physical port */
James Smart858c9f62007-06-17 19:56:39 -05007662 fc_remove_host(shost);
7663 scsi_remove_host(shost);
James Smart87af33f2007-10-27 13:37:43 -04007664 lpfc_cleanup(vport);
7665
James Smart2e0fef82007-06-17 19:56:36 -05007666 /*
7667 * Bring down the SLI Layer. This step disable all interrupts,
7668 * clears the rings, discards all mailbox commands, and resets
7669 * the HBA.
7670 */
James Smarta257bf92009-04-06 18:48:10 -04007671
7672 /* HBA interrupt will be diabled after this call */
James Smart2e0fef82007-06-17 19:56:36 -05007673 lpfc_sli_hba_down(phba);
James Smarta257bf92009-04-06 18:48:10 -04007674 /* Stop kthread signal shall trigger work_done one more time */
7675 kthread_stop(phba->worker_thread);
7676 /* Final cleanup of txcmplq and reset the HBA */
James Smart2e0fef82007-06-17 19:56:36 -05007677 lpfc_sli_brdrestart(phba);
7678
James Smart3772a992009-05-22 14:50:54 -04007679 lpfc_stop_hba_timers(phba);
James Smart858c9f62007-06-17 19:56:39 -05007680 spin_lock_irq(&phba->hbalock);
7681 list_del_init(&vport->listentry);
7682 spin_unlock_irq(&phba->hbalock);
7683
James Smart858c9f62007-06-17 19:56:39 -05007684 lpfc_debugfs_terminate(vport);
James Smart2e0fef82007-06-17 19:56:36 -05007685
James Smart5b75da22008-12-04 22:39:35 -05007686 /* Disable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007687 lpfc_sli_disable_intr(phba);
dea31012005-04-17 16:05:31 -05007688
7689 pci_set_drvdata(pdev, NULL);
James Smart858c9f62007-06-17 19:56:39 -05007690 scsi_host_put(shost);
James Smart2e0fef82007-06-17 19:56:36 -05007691
7692 /*
7693 * Call scsi_free before mem_free since scsi bufs are released to their
7694 * corresponding pools here.
7695 */
7696 lpfc_scsi_free(phba);
James Smart3772a992009-05-22 14:50:54 -04007697 lpfc_mem_free_all(phba);
James Smart2e0fef82007-06-17 19:56:36 -05007698
James Smart34b02dc2008-08-24 21:49:55 -04007699 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
7700 phba->hbqslimp.virt, phba->hbqslimp.phys);
James Smarted957682007-06-17 19:56:37 -05007701
James Smart2e0fef82007-06-17 19:56:36 -05007702 /* Free resources associated with SLI2 interface */
7703 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
James Smart34b02dc2008-08-24 21:49:55 -04007704 phba->slim2p.virt, phba->slim2p.phys);
James Smart2e0fef82007-06-17 19:56:36 -05007705
7706 /* unmap adapter SLIM and Control Registers */
7707 iounmap(phba->ctrl_regs_memmap_p);
7708 iounmap(phba->slim_memmap_p);
7709
James Smart3772a992009-05-22 14:50:54 -04007710 lpfc_hba_free(phba);
James Smart2e0fef82007-06-17 19:56:36 -05007711
Tomohiro Kusumi8a4df1202008-01-11 01:53:00 -05007712 pci_release_selected_regions(pdev, bars);
James Smart2e0fef82007-06-17 19:56:36 -05007713 pci_disable_device(pdev);
dea31012005-04-17 16:05:31 -05007714}
7715
Linas Vepstas8d63f372007-02-14 14:28:36 -06007716/**
James Smart3772a992009-05-22 14:50:54 -04007717 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05007718 * @pdev: pointer to PCI device
7719 * @msg: power management message
7720 *
James Smart3772a992009-05-22 14:50:54 -04007721 * This routine is to be called from the kernel's PCI subsystem to support
7722 * system Power Management (PM) to device with SLI-3 interface spec. When
7723 * PM invokes this method, it quiesces the device by stopping the driver's
7724 * worker thread for the device, turning off device's interrupt and DMA,
7725 * and bring the device offline. Note that as the driver implements the
7726 * minimum PM requirements to a power-aware driver's PM support for the
7727 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
7728 * to the suspend() method call will be treated as SUSPEND and the driver will
7729 * fully reinitialize its device during resume() method call, the driver will
7730 * set device to PCI_D3hot state in PCI config space instead of setting it
7731 * according to the @msg provided by the PM.
James Smart3a55b532008-12-04 22:38:54 -05007732 *
7733 * Return code
James Smart3772a992009-05-22 14:50:54 -04007734 * 0 - driver suspended the device
7735 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05007736 **/
7737static int
James Smart3772a992009-05-22 14:50:54 -04007738lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
James Smart3a55b532008-12-04 22:38:54 -05007739{
7740 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7741 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
7742
7743 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7744 "0473 PCI device Power Management suspend.\n");
7745
7746 /* Bring down the device */
7747 lpfc_offline_prep(phba);
7748 lpfc_offline(phba);
7749 kthread_stop(phba->worker_thread);
7750
7751 /* Disable interrupt from device */
James Smart3772a992009-05-22 14:50:54 -04007752 lpfc_sli_disable_intr(phba);
James Smart3a55b532008-12-04 22:38:54 -05007753
7754 /* Save device state to PCI config space */
7755 pci_save_state(pdev);
7756 pci_set_power_state(pdev, PCI_D3hot);
7757
7758 return 0;
7759}
7760
7761/**
James Smart3772a992009-05-22 14:50:54 -04007762 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
James Smart3a55b532008-12-04 22:38:54 -05007763 * @pdev: pointer to PCI device
7764 *
James Smart3772a992009-05-22 14:50:54 -04007765 * This routine is to be called from the kernel's PCI subsystem to support
7766 * system Power Management (PM) to device with SLI-3 interface spec. When PM
7767 * invokes this method, it restores the device's PCI config space state and
7768 * fully reinitializes the device and brings it online. Note that as the
7769 * driver implements the minimum PM requirements to a power-aware driver's
7770 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
7771 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
7772 * driver will fully reinitialize its device during resume() method call,
7773 * the device will be set to PCI_D0 directly in PCI config space before
7774 * restoring the state.
James Smart3a55b532008-12-04 22:38:54 -05007775 *
7776 * Return code
James Smart3772a992009-05-22 14:50:54 -04007777 * 0 - driver suspended the device
7778 * Error otherwise
James Smart3a55b532008-12-04 22:38:54 -05007779 **/
7780static int
James Smart3772a992009-05-22 14:50:54 -04007781lpfc_pci_resume_one_s3(struct pci_dev *pdev)
James Smart3a55b532008-12-04 22:38:54 -05007782{
7783 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7784 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
James Smart5b75da22008-12-04 22:39:35 -05007785 uint32_t intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05007786 int error;
7787
7788 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7789 "0452 PCI device Power Management resume.\n");
7790
7791 /* Restore device state from PCI config space */
7792 pci_set_power_state(pdev, PCI_D0);
7793 pci_restore_state(pdev);
James Smart0d878412009-10-02 15:16:56 -04007794
James Smart1dfb5a42010-02-12 14:40:50 -05007795 /*
7796 * As the new kernel behavior of pci_restore_state() API call clears
7797 * device saved_state flag, need to save the restored state again.
7798 */
7799 pci_save_state(pdev);
7800
James Smart3a55b532008-12-04 22:38:54 -05007801 if (pdev->is_busmaster)
7802 pci_set_master(pdev);
7803
7804 /* Startup the kernel thread for this host adapter. */
7805 phba->worker_thread = kthread_run(lpfc_do_work, phba,
7806 "lpfc_worker_%d", phba->brd_no);
7807 if (IS_ERR(phba->worker_thread)) {
7808 error = PTR_ERR(phba->worker_thread);
7809 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7810 "0434 PM resume failed to start worker "
7811 "thread: error=x%x.\n", error);
7812 return error;
7813 }
7814
James Smart5b75da22008-12-04 22:39:35 -05007815 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04007816 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05007817 if (intr_mode == LPFC_INTR_ERROR) {
James Smart3a55b532008-12-04 22:38:54 -05007818 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart5b75da22008-12-04 22:39:35 -05007819 "0430 PM resume Failed to enable interrupt\n");
7820 return -EIO;
7821 } else
7822 phba->intr_mode = intr_mode;
James Smart3a55b532008-12-04 22:38:54 -05007823
7824 /* Restart HBA and bring it online */
7825 lpfc_sli_brdrestart(phba);
7826 lpfc_online(phba);
7827
James Smart5b75da22008-12-04 22:39:35 -05007828 /* Log the current active interrupt mode */
7829 lpfc_log_intr_mode(phba, phba->intr_mode);
7830
James Smart3a55b532008-12-04 22:38:54 -05007831 return 0;
7832}
7833
7834/**
James Smart891478a2009-11-18 15:40:23 -05007835 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
7836 * @phba: pointer to lpfc hba data structure.
7837 *
7838 * This routine is called to prepare the SLI3 device for PCI slot recover. It
James Smarte2af0d22010-03-15 11:25:32 -04007839 * aborts all the outstanding SCSI I/Os to the pci device.
James Smart891478a2009-11-18 15:40:23 -05007840 **/
7841static void
7842lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
7843{
James Smarte2af0d22010-03-15 11:25:32 -04007844 struct lpfc_sli *psli = &phba->sli;
7845 struct lpfc_sli_ring *pring;
7846
James Smart891478a2009-11-18 15:40:23 -05007847 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7848 "2723 PCI channel I/O abort preparing for recovery\n");
James Smarte2af0d22010-03-15 11:25:32 -04007849
7850 /*
7851 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
7852 * and let the SCSI mid-layer to retry them to recover.
7853 */
7854 pring = &psli->ring[psli->fcp_ring];
7855 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart891478a2009-11-18 15:40:23 -05007856}
7857
7858/**
James Smart0d878412009-10-02 15:16:56 -04007859 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
7860 * @phba: pointer to lpfc hba data structure.
7861 *
7862 * This routine is called to prepare the SLI3 device for PCI slot reset. It
7863 * disables the device interrupt and pci device, and aborts the internal FCP
7864 * pending I/Os.
7865 **/
7866static void
7867lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
7868{
James Smart0d878412009-10-02 15:16:56 -04007869 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05007870 "2710 PCI channel disable preparing for reset\n");
James Smarte2af0d22010-03-15 11:25:32 -04007871
7872 /* Block all SCSI devices' I/Os on the host */
7873 lpfc_scsi_dev_block(phba);
7874
7875 /* stop all timers */
7876 lpfc_stop_hba_timers(phba);
7877
James Smart0d878412009-10-02 15:16:56 -04007878 /* Disable interrupt and pci device */
7879 lpfc_sli_disable_intr(phba);
7880 pci_disable_device(phba->pcidev);
James Smarte2af0d22010-03-15 11:25:32 -04007881 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
7882 lpfc_sli_flush_fcp_rings(phba);
James Smart0d878412009-10-02 15:16:56 -04007883}
7884
7885/**
7886 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
7887 * @phba: pointer to lpfc hba data structure.
7888 *
7889 * This routine is called to prepare the SLI3 device for PCI slot permanently
7890 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
7891 * pending I/Os.
7892 **/
7893static void
7894lpfc_prep_dev_for_perm_failure(struct lpfc_hba *phba)
7895{
7896 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart891478a2009-11-18 15:40:23 -05007897 "2711 PCI channel permanent disable for failure\n");
James Smarte2af0d22010-03-15 11:25:32 -04007898 /* Block all SCSI devices' I/Os on the host */
7899 lpfc_scsi_dev_block(phba);
7900
7901 /* stop all timers */
7902 lpfc_stop_hba_timers(phba);
7903
James Smart0d878412009-10-02 15:16:56 -04007904 /* Clean up all driver's outstanding SCSI I/Os */
7905 lpfc_sli_flush_fcp_rings(phba);
7906}
7907
7908/**
James Smart3772a992009-05-22 14:50:54 -04007909 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
James Smarte59058c2008-08-24 21:49:00 -04007910 * @pdev: pointer to PCI device.
7911 * @state: the current PCI connection state.
Linas Vepstas8d63f372007-02-14 14:28:36 -06007912 *
James Smart3772a992009-05-22 14:50:54 -04007913 * This routine is called from the PCI subsystem for I/O error handling to
7914 * device with SLI-3 interface spec. This function is called by the PCI
7915 * subsystem after a PCI bus error affecting this device has been detected.
7916 * When this function is invoked, it will need to stop all the I/Os and
7917 * interrupt(s) to the device. Once that is done, it will return
7918 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
7919 * as desired.
James Smarte59058c2008-08-24 21:49:00 -04007920 *
7921 * Return codes
James Smart0d878412009-10-02 15:16:56 -04007922 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
James Smart3772a992009-05-22 14:50:54 -04007923 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
7924 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
James Smarte59058c2008-08-24 21:49:00 -04007925 **/
James Smart3772a992009-05-22 14:50:54 -04007926static pci_ers_result_t
7927lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
Linas Vepstas8d63f372007-02-14 14:28:36 -06007928{
James Smart51ef4c22007-08-02 11:10:31 -04007929 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7930 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007931
James Smart0d878412009-10-02 15:16:56 -04007932 switch (state) {
7933 case pci_channel_io_normal:
James Smart891478a2009-11-18 15:40:23 -05007934 /* Non-fatal error, prepare for recovery */
7935 lpfc_sli_prep_dev_for_recover(phba);
James Smart0d878412009-10-02 15:16:56 -04007936 return PCI_ERS_RESULT_CAN_RECOVER;
7937 case pci_channel_io_frozen:
7938 /* Fatal error, prepare for slot reset */
7939 lpfc_sli_prep_dev_for_reset(phba);
7940 return PCI_ERS_RESULT_NEED_RESET;
7941 case pci_channel_io_perm_failure:
7942 /* Permanent failure, prepare for device down */
7943 lpfc_prep_dev_for_perm_failure(phba);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007944 return PCI_ERS_RESULT_DISCONNECT;
James Smart0d878412009-10-02 15:16:56 -04007945 default:
7946 /* Unknown state, prepare and request slot reset */
7947 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7948 "0472 Unknown PCI error state: x%x\n", state);
7949 lpfc_sli_prep_dev_for_reset(phba);
7950 return PCI_ERS_RESULT_NEED_RESET;
James Smarta8e497d2008-08-24 21:50:11 -04007951 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06007952}
7953
7954/**
James Smart3772a992009-05-22 14:50:54 -04007955 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
James Smarte59058c2008-08-24 21:49:00 -04007956 * @pdev: pointer to PCI device.
Linas Vepstas8d63f372007-02-14 14:28:36 -06007957 *
James Smart3772a992009-05-22 14:50:54 -04007958 * This routine is called from the PCI subsystem for error handling to
7959 * device with SLI-3 interface spec. This is called after PCI bus has been
7960 * reset to restart the PCI card from scratch, as if from a cold-boot.
7961 * During the PCI subsystem error recovery, after driver returns
7962 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
7963 * recovery and then call this routine before calling the .resume method
7964 * to recover the device. This function will initialize the HBA device,
7965 * enable the interrupt, but it will just put the HBA to offline state
7966 * without passing any I/O traffic.
James Smarte59058c2008-08-24 21:49:00 -04007967 *
7968 * Return codes
James Smart3772a992009-05-22 14:50:54 -04007969 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
7970 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
Linas Vepstas8d63f372007-02-14 14:28:36 -06007971 */
James Smart3772a992009-05-22 14:50:54 -04007972static pci_ers_result_t
7973lpfc_io_slot_reset_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06007974{
James Smart51ef4c22007-08-02 11:10:31 -04007975 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7976 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007977 struct lpfc_sli *psli = &phba->sli;
James Smart5b75da22008-12-04 22:39:35 -05007978 uint32_t intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06007979
7980 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
Benjamin Herrenschmidt09483912007-12-20 15:28:09 +11007981 if (pci_enable_device_mem(pdev)) {
Linas Vepstas8d63f372007-02-14 14:28:36 -06007982 printk(KERN_ERR "lpfc: Cannot re-enable "
7983 "PCI device after reset.\n");
7984 return PCI_ERS_RESULT_DISCONNECT;
7985 }
7986
James Smart97207482008-12-04 22:39:19 -05007987 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05007988
7989 /*
7990 * As the new kernel behavior of pci_restore_state() API call clears
7991 * device saved_state flag, need to save the restored state again.
7992 */
7993 pci_save_state(pdev);
7994
James Smart97207482008-12-04 22:39:19 -05007995 if (pdev->is_busmaster)
7996 pci_set_master(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06007997
James Smart92d7f7b2007-06-17 19:56:38 -05007998 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007999 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart92d7f7b2007-06-17 19:56:38 -05008000 spin_unlock_irq(&phba->hbalock);
Linas Vepstas8d63f372007-02-14 14:28:36 -06008001
James Smart5b75da22008-12-04 22:39:35 -05008002 /* Configure and enable interrupt */
James Smart3772a992009-05-22 14:50:54 -04008003 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
James Smart5b75da22008-12-04 22:39:35 -05008004 if (intr_mode == LPFC_INTR_ERROR) {
8005 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8006 "0427 Cannot re-enable interrupt after "
8007 "slot reset.\n");
8008 return PCI_ERS_RESULT_DISCONNECT;
8009 } else
8010 phba->intr_mode = intr_mode;
Linas Vepstas8d63f372007-02-14 14:28:36 -06008011
8012 /* Take device offline; this will perform cleanup */
8013 lpfc_offline(phba);
8014 lpfc_sli_brdrestart(phba);
8015
James Smart5b75da22008-12-04 22:39:35 -05008016 /* Log the current active interrupt mode */
8017 lpfc_log_intr_mode(phba, phba->intr_mode);
8018
Linas Vepstas8d63f372007-02-14 14:28:36 -06008019 return PCI_ERS_RESULT_RECOVERED;
8020}
8021
8022/**
James Smart3772a992009-05-22 14:50:54 -04008023 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
James Smarte59058c2008-08-24 21:49:00 -04008024 * @pdev: pointer to PCI device
Linas Vepstas8d63f372007-02-14 14:28:36 -06008025 *
James Smart3772a992009-05-22 14:50:54 -04008026 * This routine is called from the PCI subsystem for error handling to device
8027 * with SLI-3 interface spec. It is called when kernel error recovery tells
8028 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
8029 * error recovery. After this call, traffic can start to flow from this device
8030 * again.
Linas Vepstas8d63f372007-02-14 14:28:36 -06008031 */
James Smart3772a992009-05-22 14:50:54 -04008032static void
8033lpfc_io_resume_s3(struct pci_dev *pdev)
Linas Vepstas8d63f372007-02-14 14:28:36 -06008034{
James Smart51ef4c22007-08-02 11:10:31 -04008035 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8036 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
Linas Vepstas8d63f372007-02-14 14:28:36 -06008037
James Smarte2af0d22010-03-15 11:25:32 -04008038 /* Bring device online, it will be no-op for non-fatal error resume */
James Smart58da1ff2008-04-07 10:15:56 -04008039 lpfc_online(phba);
James Smart0d878412009-10-02 15:16:56 -04008040
8041 /* Clean up Advanced Error Reporting (AER) if needed */
8042 if (phba->hba_flag & HBA_AER_ENABLED)
8043 pci_cleanup_aer_uncorrect_error_status(pdev);
Linas Vepstas8d63f372007-02-14 14:28:36 -06008044}
8045
James Smart3772a992009-05-22 14:50:54 -04008046/**
James Smartda0436e2009-05-22 14:51:39 -04008047 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
8048 * @phba: pointer to lpfc hba data structure.
8049 *
8050 * returns the number of ELS/CT IOCBs to reserve
8051 **/
8052int
8053lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
8054{
8055 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
8056
James Smartf1126682009-06-10 17:22:44 -04008057 if (phba->sli_rev == LPFC_SLI_REV4) {
8058 if (max_xri <= 100)
James Smart6a9c52c2009-10-02 15:16:51 -04008059 return 10;
James Smartf1126682009-06-10 17:22:44 -04008060 else if (max_xri <= 256)
James Smart6a9c52c2009-10-02 15:16:51 -04008061 return 25;
James Smartf1126682009-06-10 17:22:44 -04008062 else if (max_xri <= 512)
James Smart6a9c52c2009-10-02 15:16:51 -04008063 return 50;
James Smartf1126682009-06-10 17:22:44 -04008064 else if (max_xri <= 1024)
James Smart6a9c52c2009-10-02 15:16:51 -04008065 return 100;
James Smartf1126682009-06-10 17:22:44 -04008066 else
James Smart6a9c52c2009-10-02 15:16:51 -04008067 return 150;
James Smartf1126682009-06-10 17:22:44 -04008068 } else
8069 return 0;
James Smartda0436e2009-05-22 14:51:39 -04008070}
8071
8072/**
8073 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
8074 * @pdev: pointer to PCI device
8075 * @pid: pointer to PCI device identifier
8076 *
8077 * This routine is called from the kernel's PCI subsystem to device with
8078 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
8079 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
8080 * information of the device and driver to see if the driver state that it
8081 * can support this kind of device. If the match is successful, the driver
8082 * core invokes this routine. If this routine determines it can claim the HBA,
8083 * it does all the initialization that it needs to do to handle the HBA
8084 * properly.
8085 *
8086 * Return code
8087 * 0 - driver can claim the device
8088 * negative value - driver can not claim the device
8089 **/
8090static int __devinit
8091lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
8092{
8093 struct lpfc_hba *phba;
8094 struct lpfc_vport *vport = NULL;
James Smart6669f9b2009-10-02 15:16:45 -04008095 struct Scsi_Host *shost = NULL;
James Smartda0436e2009-05-22 14:51:39 -04008096 int error;
8097 uint32_t cfg_mode, intr_mode;
8098 int mcnt;
8099
8100 /* Allocate memory for HBA structure */
8101 phba = lpfc_hba_alloc(pdev);
8102 if (!phba)
8103 return -ENOMEM;
8104
8105 /* Perform generic PCI device enabling operation */
8106 error = lpfc_enable_pci_dev(phba);
8107 if (error) {
8108 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8109 "1409 Failed to enable pci device.\n");
8110 goto out_free_phba;
8111 }
8112
8113 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
8114 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
8115 if (error)
8116 goto out_disable_pci_dev;
8117
8118 /* Set up SLI-4 specific device PCI memory space */
8119 error = lpfc_sli4_pci_mem_setup(phba);
8120 if (error) {
8121 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8122 "1410 Failed to set up pci memory space.\n");
8123 goto out_disable_pci_dev;
8124 }
8125
8126 /* Set up phase-1 common device driver resources */
8127 error = lpfc_setup_driver_resource_phase1(phba);
8128 if (error) {
8129 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8130 "1411 Failed to set up driver resource.\n");
8131 goto out_unset_pci_mem_s4;
8132 }
8133
8134 /* Set up SLI-4 Specific device driver resources */
8135 error = lpfc_sli4_driver_resource_setup(phba);
8136 if (error) {
8137 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8138 "1412 Failed to set up driver resource.\n");
8139 goto out_unset_pci_mem_s4;
8140 }
8141
8142 /* Initialize and populate the iocb list per host */
8143 error = lpfc_init_iocb_list(phba,
8144 phba->sli4_hba.max_cfg_param.max_xri);
8145 if (error) {
8146 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8147 "1413 Failed to initialize iocb list.\n");
8148 goto out_unset_driver_resource_s4;
8149 }
8150
8151 /* Set up common device driver resources */
8152 error = lpfc_setup_driver_resource_phase2(phba);
8153 if (error) {
8154 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8155 "1414 Failed to set up driver resource.\n");
8156 goto out_free_iocb_list;
8157 }
8158
8159 /* Create SCSI host to the physical port */
8160 error = lpfc_create_shost(phba);
8161 if (error) {
8162 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8163 "1415 Failed to create scsi host.\n");
8164 goto out_unset_driver_resource;
8165 }
8166
8167 /* Configure sysfs attributes */
8168 vport = phba->pport;
8169 error = lpfc_alloc_sysfs_attr(vport);
8170 if (error) {
8171 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8172 "1416 Failed to allocate sysfs attr\n");
8173 goto out_destroy_shost;
8174 }
8175
James Smart6669f9b2009-10-02 15:16:45 -04008176 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
James Smartda0436e2009-05-22 14:51:39 -04008177 /* Now, trying to enable interrupt and bring up the device */
8178 cfg_mode = phba->cfg_use_msi;
8179 while (true) {
8180 /* Put device to a known state before enabling interrupt */
8181 lpfc_stop_port(phba);
8182 /* Configure and enable interrupt */
8183 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
8184 if (intr_mode == LPFC_INTR_ERROR) {
8185 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8186 "0426 Failed to enable interrupt.\n");
8187 error = -ENODEV;
8188 goto out_free_sysfs_attr;
8189 }
James Smartdef9c7a2009-12-21 17:02:28 -05008190 /* Default to single FCP EQ for non-MSI-X */
8191 if (phba->intr_type != MSIX)
8192 phba->cfg_fcp_eq_count = 1;
James Smartda0436e2009-05-22 14:51:39 -04008193 /* Set up SLI-4 HBA */
8194 if (lpfc_sli4_hba_setup(phba)) {
8195 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8196 "1421 Failed to set up hba\n");
8197 error = -ENODEV;
8198 goto out_disable_intr;
8199 }
8200
8201 /* Send NOP mbx cmds for non-INTx mode active interrupt test */
8202 if (intr_mode != 0)
8203 mcnt = lpfc_sli4_send_nop_mbox_cmds(phba,
8204 LPFC_ACT_INTR_CNT);
8205
8206 /* Check active interrupts received only for MSI/MSI-X */
8207 if (intr_mode == 0 ||
8208 phba->sli.slistat.sli_intr >= LPFC_ACT_INTR_CNT) {
8209 /* Log the current active interrupt mode */
8210 phba->intr_mode = intr_mode;
8211 lpfc_log_intr_mode(phba, intr_mode);
8212 break;
8213 }
8214 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8215 "0451 Configure interrupt mode (%d) "
8216 "failed active interrupt test.\n",
8217 intr_mode);
8218 /* Unset the preivous SLI-4 HBA setup */
8219 lpfc_sli4_unset_hba(phba);
8220 /* Try next level of interrupt mode */
8221 cfg_mode = --intr_mode;
8222 }
8223
8224 /* Perform post initialization setup */
8225 lpfc_post_init_setup(phba);
8226
James Smart1c6834a2009-07-19 10:01:26 -04008227 /* Check if there are static vports to be created. */
8228 lpfc_create_static_vport(phba);
8229
James Smartda0436e2009-05-22 14:51:39 -04008230 return 0;
8231
8232out_disable_intr:
8233 lpfc_sli4_disable_intr(phba);
8234out_free_sysfs_attr:
8235 lpfc_free_sysfs_attr(vport);
8236out_destroy_shost:
8237 lpfc_destroy_shost(phba);
8238out_unset_driver_resource:
8239 lpfc_unset_driver_resource_phase2(phba);
8240out_free_iocb_list:
8241 lpfc_free_iocb_list(phba);
8242out_unset_driver_resource_s4:
8243 lpfc_sli4_driver_resource_unset(phba);
8244out_unset_pci_mem_s4:
8245 lpfc_sli4_pci_mem_unset(phba);
8246out_disable_pci_dev:
8247 lpfc_disable_pci_dev(phba);
James Smart6669f9b2009-10-02 15:16:45 -04008248 if (shost)
8249 scsi_host_put(shost);
James Smartda0436e2009-05-22 14:51:39 -04008250out_free_phba:
8251 lpfc_hba_free(phba);
8252 return error;
8253}
8254
8255/**
8256 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
8257 * @pdev: pointer to PCI device
8258 *
8259 * This routine is called from the kernel's PCI subsystem to device with
8260 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
8261 * removed from PCI bus, it performs all the necessary cleanup for the HBA
8262 * device to be removed from the PCI subsystem properly.
8263 **/
8264static void __devexit
8265lpfc_pci_remove_one_s4(struct pci_dev *pdev)
8266{
8267 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8268 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
8269 struct lpfc_vport **vports;
8270 struct lpfc_hba *phba = vport->phba;
8271 int i;
8272
8273 /* Mark the device unloading flag */
8274 spin_lock_irq(&phba->hbalock);
8275 vport->load_flag |= FC_UNLOADING;
8276 spin_unlock_irq(&phba->hbalock);
8277
8278 /* Free the HBA sysfs attributes */
8279 lpfc_free_sysfs_attr(vport);
8280
8281 /* Release all the vports against this physical port */
8282 vports = lpfc_create_vport_work_array(phba);
8283 if (vports != NULL)
8284 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
8285 fc_vport_terminate(vports[i]->fc_vport);
8286 lpfc_destroy_vport_work_array(phba, vports);
8287
8288 /* Remove FC host and then SCSI host with the physical port */
8289 fc_remove_host(shost);
8290 scsi_remove_host(shost);
8291
8292 /* Perform cleanup on the physical port */
8293 lpfc_cleanup(vport);
8294
8295 /*
8296 * Bring down the SLI Layer. This step disables all interrupts,
8297 * clears the rings, discards all mailbox commands, and resets
8298 * the HBA FCoE function.
8299 */
8300 lpfc_debugfs_terminate(vport);
8301 lpfc_sli4_hba_unset(phba);
8302
8303 spin_lock_irq(&phba->hbalock);
8304 list_del_init(&vport->listentry);
8305 spin_unlock_irq(&phba->hbalock);
8306
8307 /* Call scsi_free before lpfc_sli4_driver_resource_unset since scsi
8308 * buffers are released to their corresponding pools here.
8309 */
8310 lpfc_scsi_free(phba);
8311 lpfc_sli4_driver_resource_unset(phba);
8312
8313 /* Unmap adapter Control and Doorbell registers */
8314 lpfc_sli4_pci_mem_unset(phba);
8315
8316 /* Release PCI resources and disable device's PCI function */
8317 scsi_host_put(shost);
8318 lpfc_disable_pci_dev(phba);
8319
8320 /* Finally, free the driver's device data structure */
8321 lpfc_hba_free(phba);
8322
8323 return;
8324}
8325
8326/**
8327 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
8328 * @pdev: pointer to PCI device
8329 * @msg: power management message
8330 *
8331 * This routine is called from the kernel's PCI subsystem to support system
8332 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
8333 * this method, it quiesces the device by stopping the driver's worker
8334 * thread for the device, turning off device's interrupt and DMA, and bring
8335 * the device offline. Note that as the driver implements the minimum PM
8336 * requirements to a power-aware driver's PM support for suspend/resume -- all
8337 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
8338 * method call will be treated as SUSPEND and the driver will fully
8339 * reinitialize its device during resume() method call, the driver will set
8340 * device to PCI_D3hot state in PCI config space instead of setting it
8341 * according to the @msg provided by the PM.
8342 *
8343 * Return code
8344 * 0 - driver suspended the device
8345 * Error otherwise
8346 **/
8347static int
8348lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
8349{
8350 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8351 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8352
8353 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8354 "0298 PCI device Power Management suspend.\n");
8355
8356 /* Bring down the device */
8357 lpfc_offline_prep(phba);
8358 lpfc_offline(phba);
8359 kthread_stop(phba->worker_thread);
8360
8361 /* Disable interrupt from device */
8362 lpfc_sli4_disable_intr(phba);
8363
8364 /* Save device state to PCI config space */
8365 pci_save_state(pdev);
8366 pci_set_power_state(pdev, PCI_D3hot);
8367
8368 return 0;
8369}
8370
8371/**
8372 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
8373 * @pdev: pointer to PCI device
8374 *
8375 * This routine is called from the kernel's PCI subsystem to support system
8376 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
8377 * this method, it restores the device's PCI config space state and fully
8378 * reinitializes the device and brings it online. Note that as the driver
8379 * implements the minimum PM requirements to a power-aware driver's PM for
8380 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
8381 * to the suspend() method call will be treated as SUSPEND and the driver
8382 * will fully reinitialize its device during resume() method call, the device
8383 * will be set to PCI_D0 directly in PCI config space before restoring the
8384 * state.
8385 *
8386 * Return code
8387 * 0 - driver suspended the device
8388 * Error otherwise
8389 **/
8390static int
8391lpfc_pci_resume_one_s4(struct pci_dev *pdev)
8392{
8393 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8394 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8395 uint32_t intr_mode;
8396 int error;
8397
8398 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8399 "0292 PCI device Power Management resume.\n");
8400
8401 /* Restore device state from PCI config space */
8402 pci_set_power_state(pdev, PCI_D0);
8403 pci_restore_state(pdev);
James Smart1dfb5a42010-02-12 14:40:50 -05008404
8405 /*
8406 * As the new kernel behavior of pci_restore_state() API call clears
8407 * device saved_state flag, need to save the restored state again.
8408 */
8409 pci_save_state(pdev);
8410
James Smartda0436e2009-05-22 14:51:39 -04008411 if (pdev->is_busmaster)
8412 pci_set_master(pdev);
8413
8414 /* Startup the kernel thread for this host adapter. */
8415 phba->worker_thread = kthread_run(lpfc_do_work, phba,
8416 "lpfc_worker_%d", phba->brd_no);
8417 if (IS_ERR(phba->worker_thread)) {
8418 error = PTR_ERR(phba->worker_thread);
8419 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8420 "0293 PM resume failed to start worker "
8421 "thread: error=x%x.\n", error);
8422 return error;
8423 }
8424
8425 /* Configure and enable interrupt */
8426 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
8427 if (intr_mode == LPFC_INTR_ERROR) {
8428 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8429 "0294 PM resume Failed to enable interrupt\n");
8430 return -EIO;
8431 } else
8432 phba->intr_mode = intr_mode;
8433
8434 /* Restart HBA and bring it online */
8435 lpfc_sli_brdrestart(phba);
8436 lpfc_online(phba);
8437
8438 /* Log the current active interrupt mode */
8439 lpfc_log_intr_mode(phba, phba->intr_mode);
8440
8441 return 0;
8442}
8443
8444/**
8445 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
8446 * @pdev: pointer to PCI device.
8447 * @state: the current PCI connection state.
8448 *
8449 * This routine is called from the PCI subsystem for error handling to device
8450 * with SLI-4 interface spec. This function is called by the PCI subsystem
8451 * after a PCI bus error affecting this device has been detected. When this
8452 * function is invoked, it will need to stop all the I/Os and interrupt(s)
8453 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
8454 * for the PCI subsystem to perform proper recovery as desired.
8455 *
8456 * Return codes
8457 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
8458 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8459 **/
8460static pci_ers_result_t
8461lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
8462{
8463 return PCI_ERS_RESULT_NEED_RESET;
8464}
8465
8466/**
8467 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
8468 * @pdev: pointer to PCI device.
8469 *
8470 * This routine is called from the PCI subsystem for error handling to device
8471 * with SLI-4 interface spec. It is called after PCI bus has been reset to
8472 * restart the PCI card from scratch, as if from a cold-boot. During the
8473 * PCI subsystem error recovery, after the driver returns
8474 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
8475 * recovery and then call this routine before calling the .resume method to
8476 * recover the device. This function will initialize the HBA device, enable
8477 * the interrupt, but it will just put the HBA to offline state without
8478 * passing any I/O traffic.
8479 *
8480 * Return codes
8481 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
8482 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8483 */
8484static pci_ers_result_t
8485lpfc_io_slot_reset_s4(struct pci_dev *pdev)
8486{
8487 return PCI_ERS_RESULT_RECOVERED;
8488}
8489
8490/**
8491 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
8492 * @pdev: pointer to PCI device
8493 *
8494 * This routine is called from the PCI subsystem for error handling to device
8495 * with SLI-4 interface spec. It is called when kernel error recovery tells
8496 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
8497 * error recovery. After this call, traffic can start to flow from this device
8498 * again.
8499 **/
8500static void
8501lpfc_io_resume_s4(struct pci_dev *pdev)
8502{
8503 return;
8504}
8505
8506/**
James Smart3772a992009-05-22 14:50:54 -04008507 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
8508 * @pdev: pointer to PCI device
8509 * @pid: pointer to PCI device identifier
8510 *
8511 * This routine is to be registered to the kernel's PCI subsystem. When an
8512 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
8513 * at PCI device-specific information of the device and driver to see if the
8514 * driver state that it can support this kind of device. If the match is
8515 * successful, the driver core invokes this routine. This routine dispatches
8516 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
8517 * do all the initialization that it needs to do to handle the HBA device
8518 * properly.
8519 *
8520 * Return code
8521 * 0 - driver can claim the device
8522 * negative value - driver can not claim the device
8523 **/
8524static int __devinit
8525lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
8526{
8527 int rc;
James Smart8fa38512009-07-19 10:01:03 -04008528 struct lpfc_sli_intf intf;
James Smart3772a992009-05-22 14:50:54 -04008529
James Smart28baac72010-02-12 14:42:03 -05008530 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
James Smart3772a992009-05-22 14:50:54 -04008531 return -ENODEV;
8532
James Smart8fa38512009-07-19 10:01:03 -04008533 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
James Smart28baac72010-02-12 14:42:03 -05008534 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
James Smartda0436e2009-05-22 14:51:39 -04008535 rc = lpfc_pci_probe_one_s4(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04008536 else
James Smart3772a992009-05-22 14:50:54 -04008537 rc = lpfc_pci_probe_one_s3(pdev, pid);
James Smart8fa38512009-07-19 10:01:03 -04008538
James Smart3772a992009-05-22 14:50:54 -04008539 return rc;
8540}
8541
8542/**
8543 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
8544 * @pdev: pointer to PCI device
8545 *
8546 * This routine is to be registered to the kernel's PCI subsystem. When an
8547 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
8548 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
8549 * remove routine, which will perform all the necessary cleanup for the
8550 * device to be removed from the PCI subsystem properly.
8551 **/
8552static void __devexit
8553lpfc_pci_remove_one(struct pci_dev *pdev)
8554{
8555 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8556 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8557
8558 switch (phba->pci_dev_grp) {
8559 case LPFC_PCI_DEV_LP:
8560 lpfc_pci_remove_one_s3(pdev);
8561 break;
James Smartda0436e2009-05-22 14:51:39 -04008562 case LPFC_PCI_DEV_OC:
8563 lpfc_pci_remove_one_s4(pdev);
8564 break;
James Smart3772a992009-05-22 14:50:54 -04008565 default:
8566 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8567 "1424 Invalid PCI device group: 0x%x\n",
8568 phba->pci_dev_grp);
8569 break;
8570 }
8571 return;
8572}
8573
8574/**
8575 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
8576 * @pdev: pointer to PCI device
8577 * @msg: power management message
8578 *
8579 * This routine is to be registered to the kernel's PCI subsystem to support
8580 * system Power Management (PM). When PM invokes this method, it dispatches
8581 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
8582 * suspend the device.
8583 *
8584 * Return code
8585 * 0 - driver suspended the device
8586 * Error otherwise
8587 **/
8588static int
8589lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
8590{
8591 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8592 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8593 int rc = -ENODEV;
8594
8595 switch (phba->pci_dev_grp) {
8596 case LPFC_PCI_DEV_LP:
8597 rc = lpfc_pci_suspend_one_s3(pdev, msg);
8598 break;
James Smartda0436e2009-05-22 14:51:39 -04008599 case LPFC_PCI_DEV_OC:
8600 rc = lpfc_pci_suspend_one_s4(pdev, msg);
8601 break;
James Smart3772a992009-05-22 14:50:54 -04008602 default:
8603 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8604 "1425 Invalid PCI device group: 0x%x\n",
8605 phba->pci_dev_grp);
8606 break;
8607 }
8608 return rc;
8609}
8610
8611/**
8612 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
8613 * @pdev: pointer to PCI device
8614 *
8615 * This routine is to be registered to the kernel's PCI subsystem to support
8616 * system Power Management (PM). When PM invokes this method, it dispatches
8617 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
8618 * resume the device.
8619 *
8620 * Return code
8621 * 0 - driver suspended the device
8622 * Error otherwise
8623 **/
8624static int
8625lpfc_pci_resume_one(struct pci_dev *pdev)
8626{
8627 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8628 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8629 int rc = -ENODEV;
8630
8631 switch (phba->pci_dev_grp) {
8632 case LPFC_PCI_DEV_LP:
8633 rc = lpfc_pci_resume_one_s3(pdev);
8634 break;
James Smartda0436e2009-05-22 14:51:39 -04008635 case LPFC_PCI_DEV_OC:
8636 rc = lpfc_pci_resume_one_s4(pdev);
8637 break;
James Smart3772a992009-05-22 14:50:54 -04008638 default:
8639 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8640 "1426 Invalid PCI device group: 0x%x\n",
8641 phba->pci_dev_grp);
8642 break;
8643 }
8644 return rc;
8645}
8646
8647/**
8648 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
8649 * @pdev: pointer to PCI device.
8650 * @state: the current PCI connection state.
8651 *
8652 * This routine is registered to the PCI subsystem for error handling. This
8653 * function is called by the PCI subsystem after a PCI bus error affecting
8654 * this device has been detected. When this routine is invoked, it dispatches
8655 * the action to the proper SLI-3 or SLI-4 device error detected handling
8656 * routine, which will perform the proper error detected operation.
8657 *
8658 * Return codes
8659 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
8660 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8661 **/
8662static pci_ers_result_t
8663lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
8664{
8665 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8666 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8667 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
8668
8669 switch (phba->pci_dev_grp) {
8670 case LPFC_PCI_DEV_LP:
8671 rc = lpfc_io_error_detected_s3(pdev, state);
8672 break;
James Smartda0436e2009-05-22 14:51:39 -04008673 case LPFC_PCI_DEV_OC:
8674 rc = lpfc_io_error_detected_s4(pdev, state);
8675 break;
James Smart3772a992009-05-22 14:50:54 -04008676 default:
8677 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8678 "1427 Invalid PCI device group: 0x%x\n",
8679 phba->pci_dev_grp);
8680 break;
8681 }
8682 return rc;
8683}
8684
8685/**
8686 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
8687 * @pdev: pointer to PCI device.
8688 *
8689 * This routine is registered to the PCI subsystem for error handling. This
8690 * function is called after PCI bus has been reset to restart the PCI card
8691 * from scratch, as if from a cold-boot. When this routine is invoked, it
8692 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
8693 * routine, which will perform the proper device reset.
8694 *
8695 * Return codes
8696 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
8697 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8698 **/
8699static pci_ers_result_t
8700lpfc_io_slot_reset(struct pci_dev *pdev)
8701{
8702 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8703 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8704 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
8705
8706 switch (phba->pci_dev_grp) {
8707 case LPFC_PCI_DEV_LP:
8708 rc = lpfc_io_slot_reset_s3(pdev);
8709 break;
James Smartda0436e2009-05-22 14:51:39 -04008710 case LPFC_PCI_DEV_OC:
8711 rc = lpfc_io_slot_reset_s4(pdev);
8712 break;
James Smart3772a992009-05-22 14:50:54 -04008713 default:
8714 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8715 "1428 Invalid PCI device group: 0x%x\n",
8716 phba->pci_dev_grp);
8717 break;
8718 }
8719 return rc;
8720}
8721
8722/**
8723 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
8724 * @pdev: pointer to PCI device
8725 *
8726 * This routine is registered to the PCI subsystem for error handling. It
8727 * is called when kernel error recovery tells the lpfc driver that it is
8728 * OK to resume normal PCI operation after PCI bus error recovery. When
8729 * this routine is invoked, it dispatches the action to the proper SLI-3
8730 * or SLI-4 device io_resume routine, which will resume the device operation.
8731 **/
8732static void
8733lpfc_io_resume(struct pci_dev *pdev)
8734{
8735 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8736 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8737
8738 switch (phba->pci_dev_grp) {
8739 case LPFC_PCI_DEV_LP:
8740 lpfc_io_resume_s3(pdev);
8741 break;
James Smartda0436e2009-05-22 14:51:39 -04008742 case LPFC_PCI_DEV_OC:
8743 lpfc_io_resume_s4(pdev);
8744 break;
James Smart3772a992009-05-22 14:50:54 -04008745 default:
8746 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8747 "1429 Invalid PCI device group: 0x%x\n",
8748 phba->pci_dev_grp);
8749 break;
8750 }
8751 return;
8752}
8753
dea31012005-04-17 16:05:31 -05008754static struct pci_device_id lpfc_id_table[] = {
8755 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
8756 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04008757 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
8758 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008759 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
8760 PCI_ANY_ID, PCI_ANY_ID, },
8761 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
8762 PCI_ANY_ID, PCI_ANY_ID, },
8763 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
8764 PCI_ANY_ID, PCI_ANY_ID, },
8765 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
8766 PCI_ANY_ID, PCI_ANY_ID, },
8767 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
8768 PCI_ANY_ID, PCI_ANY_ID, },
8769 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
8770 PCI_ANY_ID, PCI_ANY_ID, },
8771 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
8772 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008773 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
8774 PCI_ANY_ID, PCI_ANY_ID, },
8775 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
8776 PCI_ANY_ID, PCI_ANY_ID, },
8777 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
8778 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008779 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
8780 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008781 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
8782 PCI_ANY_ID, PCI_ANY_ID, },
8783 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
8784 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008785 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
8786 PCI_ANY_ID, PCI_ANY_ID, },
8787 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
8788 PCI_ANY_ID, PCI_ANY_ID, },
8789 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
8790 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -04008791 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
8792 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008793 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
8794 PCI_ANY_ID, PCI_ANY_ID, },
8795 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
8796 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008797 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
8798 PCI_ANY_ID, PCI_ANY_ID, },
8799 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
8800 PCI_ANY_ID, PCI_ANY_ID, },
8801 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
8802 PCI_ANY_ID, PCI_ANY_ID, },
8803 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
8804 PCI_ANY_ID, PCI_ANY_ID, },
8805 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
8806 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05008807 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
8808 PCI_ANY_ID, PCI_ANY_ID, },
8809 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
8810 PCI_ANY_ID, PCI_ANY_ID, },
James Smartb87eab32007-04-25 09:53:28 -04008811 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
8812 PCI_ANY_ID, PCI_ANY_ID, },
8813 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
8814 PCI_ANY_ID, PCI_ANY_ID, },
8815 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
8816 PCI_ANY_ID, PCI_ANY_ID, },
8817 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
8818 PCI_ANY_ID, PCI_ANY_ID, },
8819 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
8820 PCI_ANY_ID, PCI_ANY_ID, },
8821 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
8822 PCI_ANY_ID, PCI_ANY_ID, },
James Smart84774a42008-08-24 21:50:06 -04008823 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
8824 PCI_ANY_ID, PCI_ANY_ID, },
8825 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
8826 PCI_ANY_ID, PCI_ANY_ID, },
8827 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
8828 PCI_ANY_ID, PCI_ANY_ID, },
James Smart3772a992009-05-22 14:50:54 -04008829 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
8830 PCI_ANY_ID, PCI_ANY_ID, },
James Smarta747c9c2009-11-18 15:41:10 -05008831 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
8832 PCI_ANY_ID, PCI_ANY_ID, },
8833 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
James Smart6669f9b2009-10-02 15:16:45 -04008834 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05008835 { 0 }
8836};
8837
8838MODULE_DEVICE_TABLE(pci, lpfc_id_table);
8839
Linas Vepstas8d63f372007-02-14 14:28:36 -06008840static struct pci_error_handlers lpfc_err_handler = {
8841 .error_detected = lpfc_io_error_detected,
8842 .slot_reset = lpfc_io_slot_reset,
8843 .resume = lpfc_io_resume,
8844};
8845
dea31012005-04-17 16:05:31 -05008846static struct pci_driver lpfc_driver = {
8847 .name = LPFC_DRIVER_NAME,
8848 .id_table = lpfc_id_table,
8849 .probe = lpfc_pci_probe_one,
8850 .remove = __devexit_p(lpfc_pci_remove_one),
James Smart3a55b532008-12-04 22:38:54 -05008851 .suspend = lpfc_pci_suspend_one,
James Smart3772a992009-05-22 14:50:54 -04008852 .resume = lpfc_pci_resume_one,
James Smart2e0fef82007-06-17 19:56:36 -05008853 .err_handler = &lpfc_err_handler,
dea31012005-04-17 16:05:31 -05008854};
8855
James Smarte59058c2008-08-24 21:49:00 -04008856/**
James Smart3621a712009-04-06 18:47:14 -04008857 * lpfc_init - lpfc module initialization routine
James Smarte59058c2008-08-24 21:49:00 -04008858 *
8859 * This routine is to be invoked when the lpfc module is loaded into the
8860 * kernel. The special kernel macro module_init() is used to indicate the
8861 * role of this routine to the kernel as lpfc module entry point.
8862 *
8863 * Return codes
8864 * 0 - successful
8865 * -ENOMEM - FC attach transport failed
8866 * all others - failed
8867 */
dea31012005-04-17 16:05:31 -05008868static int __init
8869lpfc_init(void)
8870{
8871 int error = 0;
8872
8873 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04008874 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -05008875
James Smart7ee5d432007-10-27 13:37:17 -04008876 if (lpfc_enable_npiv) {
8877 lpfc_transport_functions.vport_create = lpfc_vport_create;
8878 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
8879 }
dea31012005-04-17 16:05:31 -05008880 lpfc_transport_template =
8881 fc_attach_transport(&lpfc_transport_functions);
James Smart7ee5d432007-10-27 13:37:17 -04008882 if (lpfc_transport_template == NULL)
dea31012005-04-17 16:05:31 -05008883 return -ENOMEM;
James Smart7ee5d432007-10-27 13:37:17 -04008884 if (lpfc_enable_npiv) {
James Smart7ee5d432007-10-27 13:37:17 -04008885 lpfc_vport_transport_template =
James Smart98c9ea52007-10-27 13:37:33 -04008886 fc_attach_transport(&lpfc_vport_transport_functions);
8887 if (lpfc_vport_transport_template == NULL) {
8888 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -04008889 return -ENOMEM;
James Smart98c9ea52007-10-27 13:37:33 -04008890 }
James Smart7ee5d432007-10-27 13:37:17 -04008891 }
dea31012005-04-17 16:05:31 -05008892 error = pci_register_driver(&lpfc_driver);
James Smart92d7f7b2007-06-17 19:56:38 -05008893 if (error) {
dea31012005-04-17 16:05:31 -05008894 fc_release_transport(lpfc_transport_template);
James Smartd7c255b2008-08-24 21:50:00 -04008895 if (lpfc_enable_npiv)
8896 fc_release_transport(lpfc_vport_transport_template);
James Smart92d7f7b2007-06-17 19:56:38 -05008897 }
dea31012005-04-17 16:05:31 -05008898
8899 return error;
8900}
8901
James Smarte59058c2008-08-24 21:49:00 -04008902/**
James Smart3621a712009-04-06 18:47:14 -04008903 * lpfc_exit - lpfc module removal routine
James Smarte59058c2008-08-24 21:49:00 -04008904 *
8905 * This routine is invoked when the lpfc module is removed from the kernel.
8906 * The special kernel macro module_exit() is used to indicate the role of
8907 * this routine to the kernel as lpfc module exit point.
8908 */
dea31012005-04-17 16:05:31 -05008909static void __exit
8910lpfc_exit(void)
8911{
8912 pci_unregister_driver(&lpfc_driver);
8913 fc_release_transport(lpfc_transport_template);
James Smart7ee5d432007-10-27 13:37:17 -04008914 if (lpfc_enable_npiv)
8915 fc_release_transport(lpfc_vport_transport_template);
James Smart81301a92008-12-04 22:39:46 -05008916 if (_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -04008917 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
8918 "_dump_buf_data at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -05008919 (1L << _dump_buf_data_order), _dump_buf_data);
8920 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
8921 }
8922
8923 if (_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -04008924 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
8925 "_dump_buf_dif at 0x%p\n",
James Smart81301a92008-12-04 22:39:46 -05008926 (1L << _dump_buf_dif_order), _dump_buf_dif);
8927 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
8928 }
dea31012005-04-17 16:05:31 -05008929}
8930
8931module_init(lpfc_init);
8932module_exit(lpfc_exit);
8933MODULE_LICENSE("GPL");
8934MODULE_DESCRIPTION(LPFC_MODULE_DESC);
8935MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
8936MODULE_VERSION("0:" LPFC_DRIVER_VERSION);